# TriangleSolidColor

VTKExamples/Cxx/PolyData/TriangleSolidColor

### Code

TriangleSolidColor.cxx

    #include <vtkVersion.h>
    #include <vtkSmartPointer.h>
    #include <vtkPoints.h>
    #include <vtkXMLPolyDataWriter.h>
    #include <vtkPolyData.h>
    #include <vtkCellData.h>
    #include <vtkCellArray.h>
    #include <vtkTriangle.h>
    #include <vtkUnsignedCharArray.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkActor.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindowInteractor.h>

    // For compatibility with new VTK generic data arrays
    #ifdef vtkGenericDataArray_h
    #define InsertNextTupleValue InsertNextTypedTuple
    #endif

    int main(int, char *[])
    {
      // Setup points
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();
      vtkSmartPointer<vtkCellArray> vertices =
        vtkSmartPointer<vtkCellArray>::New();
      points->InsertNextPoint(0.0, 0.0, 0.0);
      points->InsertNextPoint(1.0, 0.0, 0.0);
      points->InsertNextPoint(0.0, 1.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()->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);
      renderer->ResetCamera();

      renderWindow->Render();
      renderWindowInteractor->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 2.8)

    PROJECT(TriangleSolidColor)

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

    add_executable(TriangleSolidColor MACOSX_BUNDLE TriangleSolidColor.cxx )

    target_link_libraries(TriangleSolidColor ${VTK_LIBRARIES})

### Download and Build TriangleSolidColor

Click here to download TriangleSolidColor and its CMakeLists.txt file. Once the tarball TriangleSolidColor.tar has been downloaded and extracted,

    cd TriangleSolidColor/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:

    ./TriangleSolidColor

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.