VTK/Examples/TriangleColoredPoints
From KitwarePublic
< VTK | Examples(Redirected from Write a file of a colored triangle)
This example shows how by adding a color to each vertex of a triangle, the triangle's color will be smoothly varying between the colors of the vertices.
TriangleColoredPoints.cxx
#include <vtkSmartPointer.h> #include <vtkPoints.h> #include <vtkXMLPolyDataWriter.h> #include <vtkPolyData.h> #include <vtkPointData.h> #include <vtkCellArray.h> #include <vtkTriangle.h> int main(int argc, char *argv[]) { //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 ); //define some colors unsigned char red[3] = {255, 0, 0}; unsigned char green[3] = {0, 255, 0}; unsigned char blue[3] = {0, 0, 255}; //setup the colors array vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); colors->SetNumberOfComponents ( 3 ); colors->SetName ( "Colors" ); //add the three colors we have created to the array colors->InsertNextTupleValue ( red ); colors->InsertNextTupleValue ( green ); colors->InsertNextTupleValue ( blue ); //create the triangle 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 ); //create a polydata object and add everything to it vtkSmartPointer<vtkPolyData> polydata = vtkSmartPointer<vtkPolyData>::New(); polydata->SetPoints ( points ); polydata->SetPolys ( triangles ); polydata->GetPointData()->SetScalars ( colors ); //write the polydata to a file vtkSmartPointer<vtkXMLPolyDataWriter> writer = vtkSmartPointer<vtkXMLPolyDataWriter>::New(); writer->SetFileName ( "TriangleColoredPoints.vtp" ); writer->SetInput ( polydata ); writer->Write(); return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.6) PROJECT(TriangleColoredPoints) FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE}) ADD_EXECUTABLE(TriangleColoredPoints TriangleColoredPoints.cxx) TARGET_LINK_LIBRARIES(TriangleColoredPoints vtkHybrid)

