VTK/Examples/TriangleSolidColor
From KitwarePublic
< VTK | Examples(Redirected from Write a file of colored triangle vertices)
TriangleSolidColor.cxx
#include <vtkSmartPointer.h> #include <vtkPoints.h> #include <vtkXMLPolyDataWriter.h> #include <vtkPolyData.h> #include <vtkCellData.h> #include <vtkCellArray.h> #include <vtkTriangle.h> int main(int argc, char *argv[]) { //setup points vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); vtkSmartPointer<vtkCellArray> vertices = vtkSmartPointer<vtkCellArray>::New(); points->InsertNextPoint(1.0, 0.0, 0.0); points->InsertNextPoint(0.0, 0.0, 1.0); points->InsertNextPoint(0.0, 0.0, 0.0); //setup colors unsigned char red[3] = {255, 0, 0}; vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); colors->SetNumberOfComponents(3); colors->SetName("Colors"); colors->InsertNextTupleValue(red); 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); triangles->InsertNextCell(triangle); vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New(); polydata->SetPoints(points); polydata->SetPolys(triangles); polydata->GetCellData()->AddArray(colors); vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New(); writer->SetFileName("TriangleSolidColor.vtp"); writer->SetInput(polydata); writer->Write(); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(TriangleSolidColor) FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE}) ADD_EXECUTABLE(TriangleSolidColor TriangleSolidColor.cxx) TARGET_LINK_LIBRARIES(TriangleSolidColor vtkHybrid)

