VTK/Examples/Cxx/Boneyard/GeometricObjects/WriteFile/TriangleOnData
This example creates a triangle whose points are stored in an "external" array. This is how you must create triangles which will be added to a polydata (as the "external" array in this case is the polydata's Points array). This method, however, does not allow the triangles to exist alone, as they do not actually contain their geometry. This means that if you pass this triangle to a function, you will not be able to access its points in the function unless you also pass its external geometry array.
Contents
TriangleOnData.cxx
#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkXMLPolyDataWriter.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
int main(int, char *[])
{
//setup 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);
vtkSmartPointer<vtkCellArray> triangles =
vtkSmartPointer<vtkCellArray>::New();
vtkSmartPointer<vtkTriangle> triangle =
vtkSmartPointer<vtkTriangle>::New();
triangle->GetPointIds()->SetId(0, 0);
triangle->GetPointIds()->SetId(1, 1);
triangle->GetPointIds()->SetId(2, 2);
//this is an unfortunate example, as you cannot tell which 0,1, or 2 is which!
//A clearer statement is SetId(0, IndexOfPoint0)
triangles->InsertNextCell(triangle);
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->SetPolys(triangles);
vtkSmartPointer<vtkXMLPolyDataWriter> writer =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName("Triangle.vtp");
writer->SetInput(polydata);
writer->Write();
return EXIT_SUCCESS;
}
Please try the new VTKExamples website.
CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
PROJECT(TriangleOnData)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(TriangleOnData MACOSX_BUNDLE TriangleOnData.cxx)
if(VTK_LIBRARIES)
target_link_libraries(TriangleOnData ${VTK_LIBRARIES})
else()
target_link_libraries(TriangleOnData vtkHybrid vtkWidgets)
endif()
Download and Build TriangleOnData
Click here to download TriangleOnData. and its CMakeLists.txt file.
Once the tarball TriangleOnData.tar has been downloaded and extracted,
cd TriangleOnData/build
- If VTK is installed:
cmake ..
- If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./TriangleOnData
WINDOWS USERS PLEASE NOTE: Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.