Skip to content

Glyph3DMapper

vtk-examples/Cxx/Visualization/Glyph3DMapper


Question

If you have a question about this example, please use the VTK Discourse Forum

Code

Glyph3DMapper.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkCubeSource.h>
#include <vtkFloatArray.h>
#include <vtkGlyph3DMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnsignedCharArray.h>

int main(int, char*[])
{
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 1, 1);
  points->InsertNextPoint(2, 2, 2);

  vtkNew<vtkFloatArray> scaleFactors;
  scaleFactors->SetNumberOfComponents(3);
  scaleFactors->SetName("Scale Factors");
  scaleFactors->InsertNextTuple3(0.7, 1.0, 1.0);
  scaleFactors->InsertNextTuple3(1.0, 0.7, 1.0);
  scaleFactors->InsertNextTuple3(1.0, 1.0, 0.7);

  vtkNew<vtkNamedColors> namedColors;

  vtkNew<vtkUnsignedCharArray> colors;
  colors->SetName("Colors");
  colors->SetNumberOfComponents(3);
  colors->InsertNextTypedTuple(namedColors->GetColor3ub("Red").GetData());
  colors->InsertNextTypedTuple(namedColors->GetColor3ub("Green").GetData());
  colors->InsertNextTypedTuple(namedColors->GetColor3ub("Blue").GetData());

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);
  polydata->GetPointData()->AddArray(colors);
  polydata->GetPointData()->AddArray(scaleFactors);

  // Create anything you want here, we will use a cube for the demo.
  vtkNew<vtkCubeSource> cubeSource;

  vtkNew<vtkGlyph3DMapper> glyph3Dmapper;
  glyph3Dmapper->SetSourceConnection(cubeSource->GetOutputPort());
  glyph3Dmapper->SetInputData(polydata);
  glyph3Dmapper->SetScalarModeToUsePointFieldData();
  glyph3Dmapper->SetScaleArray("Scale Factors");
  glyph3Dmapper->SetScaleModeToScaleByVectorComponents();
  glyph3Dmapper->SelectColorArray("Colors");
  glyph3Dmapper->Update();

  vtkNew<vtkActor> actor;
  actor->SetMapper(glyph3Dmapper);

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("Glyph3DMapper");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());

  // Position the camera
  renderer->GetActiveCamera()->SetPosition(-10, 5, 0);
  renderer->GetActiveCamera()->SetFocalPoint(1, 1, 1);

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Glyph3DMapper)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonCore
  vtkCommonDataModel
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingContextOpenGL2
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingGL2PSOpenGL2
  vtkRenderingOpenGL2
  QUIET
)

if (NOT VTK_FOUND)
  message("Skipping Glyph3DMapper: ${VTK_NOT_FOUND_MESSAGE}")
  return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
  # old system
  include(${VTK_USE_FILE})
  add_executable(Glyph3DMapper MACOSX_BUNDLE Glyph3DMapper.cxx )
  target_link_libraries(Glyph3DMapper PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(Glyph3DMapper MACOSX_BUNDLE Glyph3DMapper.cxx )
  target_link_libraries(Glyph3DMapper PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS Glyph3DMapper
    MODULES ${VTK_LIBRARIES}
    )
endif ()

Download and Build Glyph3DMapper

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

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

./Glyph3DMapper

WINDOWS USERS

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