VTK/Examples/Cxx/PolyData/TriangleColoredPoints
From KitwarePublic
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 <vtkVersion.h> #include <vtkSmartPointer.h> #include <vtkPoints.h> #include <vtkXMLPolyDataWriter.h> #include <vtkPolyData.h> #include <vtkPointData.h> #include <vtkCellArray.h> #include <vtkTriangle.h> #include <vtkPolyDataMapper.h> #include <vtkActor.h> #include <vtkRenderWindow.h> #include <vtkRenderer.h> #include <vtkRenderWindowInteractor.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); // 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 a 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); // Visualize vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); #if VTK_MAJOR_VERSION <= 5 mapper->SetInputConnection(polydata->GetProducerPort()); #else mapper->SetInputData(polydata); #endif vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); renderer->AddActor(actor); renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) PROJECT(TriangleColoredPoints) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) if (APPLE) add_executable(TriangleColoredPoints MACOSX_BUNDLE TriangleColoredPoints.cxx) else() add_executable(TriangleColoredPoints TriangleColoredPoints.cxx) endif() if(VTK_LIBRARIES) target_link_libraries(TriangleColoredPoints ${VTK_LIBRARIES}) else() target_link_libraries(TriangleColoredPoints vtkHybrid ) endif()
