[vtkusers] Coloring Glyph2D by array values

Jong Youl Choi yyalli at gmail.com
Sat Apr 9 12:27:24 EDT 2011


Hi everyone,

I want to display 2D glyphs scaled and colored by using two
different vectors, say scales and group ids. I can successfully do
this using Glyph3D but I cannot do with Glyph2D. Especially I cannot
color them correctly. I'm enclosing my simple code for your reference.
Any help will be appreciated.

Thanks,
Jong

#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkPolyData.h>
#include <vtkPoints.h>
#include <vtkGlyph3D.h>
#include <vtkCellArray.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkUnsignedIntArray.h>
#include <vtkFloatArray.h>
#include <vtkPointData.h>
#include <vtkLabeledDataMapper.h>
#include <vtkActor2D.h>
#include <vtkTransformCoordinateSystems.h>
#include <vtkGlyphSource2D.h>
#include <vtkGlyph2D.h>
#include <vtkPolyDataMapper2D.h>

#define VTK_CREATE(type, name) vtkSmartPointer<type> name =
vtkSmartPointer<type>::New()

int main(int, char *[])
{
   // Setup renderer
   VTK_CREATE(vtkRenderer, renderer);
   VTK_CREATE(vtkRenderWindow, renderWindow);
   VTK_CREATE(vtkRenderWindowInteractor, renderWindowInteractor);
   renderWindow->AddRenderer(renderer);
   renderWindowInteractor->SetRenderWindow(renderWindow);

   // Build Data
   VTK_CREATE(vtkPoints, points);
   points->InsertNextPoint(0,0,0);
   points->InsertNextPoint(1,1,1);
   points->InsertNextPoint(2,2,2);

   VTK_CREATE(vtkPolyData, polydata);
   polydata->SetPoints(points);

   VTK_CREATE(vtkUnsignedIntArray, gid);
   gid->SetName("ID");
   gid->InsertNextValue(1);
   gid->InsertNextValue(2);
   gid->InsertNextValue(1);
   polydata->GetPointData()->AddArray(gid);

   VTK_CREATE(vtkFloatArray, scale);
   scale->SetName("SCALE");
   scale->InsertNextValue(3.0);
   scale->InsertNextValue(2.0);
   scale->InsertNextValue(1.0);
   polydata->GetPointData()->AddArray(scale);

   //polydata->GetPointData()->SetActiveScalars("SCALE");
   polydata->GetPointData()->SetActiveScalars("ID");

   // Build Glyph3D
   VTK_CREATE(vtkCubeSource, cubeSource);

   VTK_CREATE(vtkGlyph3D, glyph3D);
   glyph3D->SetSource(cubeSource->GetOutput());
   glyph3D->SetInput(polydata);
   glyph3D->SetScaleModeToScaleByScalar();
   glyph3D->SetColorModeToColorByScalar();
   glyph3D->SetScaleFactor(0.5);
   glyph3D->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_POINTS, "SCALE");

   VTK_CREATE(vtkPolyDataMapper, mapper);
   mapper->SetInputConnection(glyph3D->GetOutputPort());
   mapper->SetColorModeToMapScalars();
   mapper->SelectColorArray("ID");
   mapper->SetScalarRange(1, 2);

   vtkSmartPointer<vtkActor> actor =
   vtkSmartPointer<vtkActor>::New();
   actor->SetMapper(mapper);
   renderer->AddActor(actor);

   // Build Label
   VTK_CREATE(vtkLabeledDataMapper, labelMapper);
   labelMapper->SetInput(polydata);
   labelMapper->SetLabelModeToLabelIds();

   vtkSmartPointer<vtkActor2D> labelActor =
   vtkSmartPointer<vtkActor2D>::New();
   labelActor->SetMapper(labelMapper);
   renderer->AddActor(labelActor);

   // Build Glyph2D
   VTK_CREATE(vtkTransformCoordinateSystems, xform);
   xform->SetInput(polydata);
   xform->SetInputCoordinateSystemToWorld();
   xform->SetOutputCoordinateSystemToDisplay();
   xform->SetViewport(renderer);

   VTK_CREATE(vtkGlyphSource2D, gs);
   gs->SetGlyphTypeToCircle();
   gs->SetScale(40);
   gs->FilledOff();
   gs->CrossOn();

   VTK_CREATE(vtkGlyph2D, glyph2D);
   glyph2D->SetInput(xform->GetOutput());
   glyph2D->SetSource(gs->GetOutput());
   glyph2D->SetScaleModeToScaleByScalar();
   glyph2D->SetColorModeToColorByScalar();
   glyph2D->SetInputArrayToProcess(0, 0, 0,
vtkDataObject::FIELD_ASSOCIATION_POINTS, "SCALE");

  // I cannot color by using "ID"
   VTK_CREATE(vtkPolyDataMapper2D, mapper2D);
   mapper2D->SetInput(glyph2D->GetOutput());
   mapper2D->SetColorModeToMapScalars();
   mapper2D->ColorByArrayComponent((char*)"ID", 0);
   mapper2D->SetScalarRange(1, 2);

   VTK_CREATE(vtkActor2D, actor2D);
   actor2D->SetMapper(mapper2D);

   renderer->AddActor(actor2D);

   // Visualization
   renderer->SetBackground(.3, .6, .3); // Background color green

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

   return EXIT_SUCCESS;
}



More information about the vtkusers mailing list