[vtkusers] Map Scalar Values to colors using color lookup table

Gishara Indeewarie gish.777 at gmail.com
Mon Feb 6 02:23:57 EST 2012


Hi all,

I have created a shape with different colors using color lookup table
according to the example below:
http://www.vtk.org/Wiki/VTK/Examples/Cxx/Meshes/Color_a_mesh_by_height

It is working well, but I can't see where is set the scalar values to
colors. Below is my code.

vtkPoints *points = newPts;//ReadCFDData2();
  vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
  polydata->SetPoints(points);

  vtkSmartPointer<vtkDoubleArray> weights =
vtkSmartPointer<vtkDoubleArray>::New();
  weights->SetNumberOfValues(PoValues.GetSize());
  for(int i=0; i< PoValues.GetSize();i++){
  weights->SetValue(i, PoValues[i]);
  }
 // polydata->GetPointData()->SetScalars(weights);

  vtkSmartPointer<vtkVertexGlyphFilter> glyphFilter =
    vtkSmartPointer<vtkVertexGlyphFilter>::New();
#if VTK_MAJOR_VERSION <= 5
  glyphFilter->SetInputConnection(polydata->GetProducerPort());
#else
  glyphFilter->SetInputData(polydata);
#endif
  glyphFilter->Update();

  // Create a plane to cut
  vtkSmartPointer<vtkPlane> plane =
    vtkSmartPointer<vtkPlane>::New();
  plane->SetOrigin(polydata->GetCenter());
  plane->SetNormal(1,1,1);


  // Construct the surface and create isosurface.
  vtkSmartPointer<vtkSurfaceReconstructionFilter> surf =
    vtkSmartPointer<vtkSurfaceReconstructionFilter>::New();

  surf->SetInput(polydata);
 //
  vtkSmartPointer<vtkContourFilter> cf =
 vtkSmartPointer<vtkContourFilter>::New();
  cf->SetInputConnection(surf->GetOutputPort());
  cf->Update();
   vtkPolyData* outputPolyData = cf->GetOutput();

  double bounds[6];
  outputPolyData->GetBounds(bounds);

  // Find min and max z
  double minz = bounds[4];
  double maxz = bounds[5];

  std::cout << "minz: " << minz << std::endl;
  std::cout << "maxz: " << maxz << std::endl;

  // Create the color map
  vtkSmartPointer<vtkLookupTable> colorLookupTable =
vtkSmartPointer<vtkLookupTable>::New();
  colorLookupTable->SetTableRange(minz, maxz);
  colorLookupTable->SetHueRange(0.0,0.667);
  colorLookupTable->SetNumberOfColors(16);

  colorLookupTable->Build();
  //unsigned char *op;
  //vtkScalarsToColors::MapScalarsThroughTable(newScalars,op);

  // Generate the colors for each point based on the color map
  vtkSmartPointer<vtkUnsignedCharArray> colors =
    vtkSmartPointer<vtkUnsignedCharArray>::New();
  colors->SetNumberOfComponents(3);
  colors->SetName("Colors");
  for(int i = 0; i < outputPolyData->GetNumberOfPoints(); i++)
    {
    double p[3];
    outputPolyData->GetPoint(i,p);

    double dcolor[3];
    colorLookupTable->GetColor(p[2], dcolor);
    unsigned char color[3];
    for(unsigned int j = 0; j < 3; j++)
      {
      color[j] = static_cast<unsigned char>(255.0 * dcolor[j]);
      }

    colors->InsertNextTupleValue(color);
    }

  outputPolyData->GetPointData()->SetScalars(colors);

  // Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();

mapper->SetInputConnection(/*reverse->GetOutputPort()*/cf->GetOutputPort());
 mapper->ScalarVisibilityOn();
  mapper->SetLookupTable(colorLookupTable);
 // mapper->SetScalarRange(polydata->GetScalarRange());
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
   //actor->GetProperty()->SetColor(1.0, 0.8941, 0.7686); // bisque
  actor->SetMapper(mapper);

  //Create a renderer, render window, and interactor
  vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  //Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(.3, .6, .3); // Background color green

  //Render and interact
  renderWindow->Render();
  renderWindowInteractor->Start();

Can anyone tell me where should I map the scalar values to colors in the
above code?

Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20120206/aacddcad/attachment.htm>


More information about the vtkusers mailing list