VTK/Tutorials/TriangleGeometryOnly: Difference between revisions

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
No edit summary
 
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This example writes the coordinates of the corners of a triangle to a vtp file. There is geometry (points), but there is no topology (vertices), so if you open this file in Paraview, you will not see anything. You can "glyph" the points to see them, but generally some type of topology exists. We will see topology in the next example.
This example writes the coordinates of the corners of a triangle to a vtp file. There is geometry (points), but there is no topology (vertices), so if you open this file in Paraview, you will not see anything. You can "glyph" the points to see them, but generally some type of topology exists. We will see topology in the next example.


==TrianglePoints.cxx==
<source lang="cpp">
<source lang="cpp">
#include <iostream>
#include <vtkSmartPointer.h>
#include <vector>
#include <vtkPoints.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkPolyData.h>


#include "vtkCellArray.h"
int main(int argc, char *argv[])
#include "vtkPoints.h"
{
#include "vtkXMLPolyDataWriter.h"
  //create a set of points
#include "vtkPolyData.h"
  vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
  points->InsertNextPoint ( 1.0, 0.0, 0.0 );
  points->InsertNextPoint ( 0.0, 0.0, 0.0 );
  points->InsertNextPoint ( 0.0, 1.0, 0.0 );
 
  //create a polydata
  vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 
  //add the points to the polydata
  polydata->SetPoints ( points );
 
  //write the polydata to a file
  vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
  writer->SetFileName ( "TrianglePoints.vtp" );
#if VTK_MAJOR_VERSION <= 5
  writer->SetInput( polydata );
#else
  writer->SetInputData( polydata );
#endif
  writer->Write();
 
  return EXIT_SUCCESS;
}
 
</source>
 
==CMakeLists.txt==
<source lang="cmake">
cmake_minimum_required(VERSION 2.6)


struct Point
project(TrianglePoints)
{
double x,y,z;
Point(const double xin, const double yin, const double zin) : x(xin), y(yin), z(zin) {}
};


int main()
find_package(VTK REQUIRED)
{
include(${VTK_USE_FILE})
//setup points
std::vector<Point> Coords;
Coords.push_back(Point(-1.0, 1.0, 0.0));
Coords.push_back(Point(1.0, 1.0, 0.0));
Coords.push_back(Point(1.0, -1.0, 0.0));
vtkPoints* Points = vtkPoints::New();
for (unsigned int i = 0; i < Coords.size(); ++i )
{
Point P = Coords[i];
Points->InsertNextPoint(P.x, P.y, P.z);
}
vtkPolyData* polydata = vtkPolyData::New();


polydata->SetPoints(Points);
add_executable(TrianglePoints TrianglePoints.cxx)
if(VTK_LIBRARIES)
vtkXMLPolyDataWriter* writer = vtkXMLPolyDataWriter::New();
  target_link_libraries(TrianglePoints ${VTK_LIBRARIES})
writer->SetFileName("Square.vtp");
else()
writer->SetInput(polydata);
  target_link_libraries(TrianglePoints vtkHybrid)
writer->Write();
endif()


}</source>
</source>

Latest revision as of 19:14, 25 April 2014

This example writes the coordinates of the corners of a triangle to a vtp file. There is geometry (points), but there is no topology (vertices), so if you open this file in Paraview, you will not see anything. You can "glyph" the points to see them, but generally some type of topology exists. We will see topology in the next example.

TrianglePoints.cxx

<source lang="cpp">

  1. include <vtkSmartPointer.h>
  2. include <vtkPoints.h>
  3. include <vtkXMLPolyDataWriter.h>
  4. include <vtkPolyData.h>

int main(int argc, char *argv[]) {

 //create a set of points
 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
 points->InsertNextPoint ( 1.0, 0.0, 0.0 );
 points->InsertNextPoint ( 0.0, 0.0, 0.0 );
 points->InsertNextPoint ( 0.0, 1.0, 0.0 );
 //create a polydata
 vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New();
 //add the points to the polydata
 polydata->SetPoints ( points );
 //write the polydata to a file
 vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New();
 writer->SetFileName ( "TrianglePoints.vtp" );
  1. if VTK_MAJOR_VERSION <= 5
 writer->SetInput( polydata );
  1. else
 writer->SetInputData( polydata );
  1. endif
 writer->Write();
 return EXIT_SUCCESS;

}

</source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

project(TrianglePoints)

find_package(VTK REQUIRED) include(${VTK_USE_FILE})

add_executable(TrianglePoints TrianglePoints.cxx) if(VTK_LIBRARIES)

 target_link_libraries(TrianglePoints ${VTK_LIBRARIES})

else()

 target_link_libraries(TrianglePoints vtkHybrid)

endif()

</source>