Search code examples
c++vtkscatter-plotpoint-clouds

Use VTK to plot points of different colours


I would like to use Visualisation Toolkit to scatter plot points, each of which will have a different colour. I have used the advice given here to plot points in a gray colour, but am failing to understand how to give a colour to each one.

The relevant parts of the cube example are:

vtkPolyData *cube = vtkPolyData::New();
vtkPoints *points = vtkPoints::New();
vtkCellArray *polys = vtkCellArray::New();
vtkFloatArray *scalars = vtkFloatArray::New();

// Load the point, cell, and data attributes.
for (i=0; i<8; i++) points->InsertPoint(i,x[i]);
for (i=0; i<6; i++) polys->InsertNextCell(4,pts[i]);
for (i=0; i<8; i++) scalars->InsertTuple1(i,i);

// We now assign the pieces to the vtkPolyData.
cube->SetPoints(points);
points->Delete();
cube->SetVerts(polys);
polys->Delete();
cube->GetPointData()->SetScalars(scalars);
scalars->Delete();

How can I give each of the Verts a colour?


Solution

  • I found a basic tutorial for what I was trying to do. This shows how to add colours for each point:

    http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/ColoredPoints

    The relevant lines are as follows:

    // Setup colors
    vtkSmartPointer<vtkUnsignedCharArray> colors =
    vtkSmartPointer<vtkUnsignedCharArray>::New();
    colors->SetNumberOfComponents(3);
    colors->SetName ("Colors");
      for (int i = 0; i < nV; ++i)
      {
        unsigned char tempColor[3] = {(int)c[i],
                                      (int)c[i+nV],
                                      (int)c[i+2*nV]}; 
        colors->InsertNextTupleValue (tempColor);
      }
    
    polydata->GetPointData()->SetScalars(colors);