VTK/Examples/Cxx/Broken/Visualization/GlyphTable

From KitwarePublic
< VTK‎ | Examples‎ | Cxx
Jump to navigationJump to search

How to specify which values get which glyphs?

GlyphTable.cxx

#include <vtkSmartPointer.h>
#include <vtkPointData.h>
#include <vtkCubeSource.h>
#include <vtkConeSource.h>
#include <vtkSphereSource.h>
#include <vtkGlyph3D.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkFloatArray.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkSphereSource> bigSphereSource = 
    vtkSmartPointer<vtkSphereSource>::New();
  bigSphereSource->Update();
  
  vtkSmartPointer<vtkFloatArray> height = 
    vtkSmartPointer<vtkFloatArray>::New();
  height->SetNumberOfComponents(1);
  height->SetName("Height");
  
  for(vtkIdType i = 0; i < bigSphereSource->GetOutput()->GetNumberOfPoints(); i++)
    {
    double p[3];
    bigSphereSource->GetOutput()->GetPoint(i, p);
    height->InsertNextValue(p[2]);
    }
  
  vtkSmartPointer<vtkPolyData> input = 
    vtkSmartPointer<vtkPolyData>::New();
  input->ShallowCopy(bigSphereSource->GetOutput());
  input->GetPointData()->AddArray(height);
  
  vtkSmartPointer<vtkConeSource> coneSource =
    vtkSmartPointer<vtkConeSource>::New();
  coneSource->SetRadius(.1);
  coneSource->SetHeight(.1);
  coneSource->Update();
  
  vtkSmartPointer<vtkCubeSource> cubeSource =
    vtkSmartPointer<vtkCubeSource>::New();
  cubeSource->SetXLength(.1);
  cubeSource->SetYLength(.1);
  cubeSource->SetZLength(.1);
  cubeSource->Update();
  
  vtkSmartPointer<vtkGlyph3D> glyph3D = 
    vtkSmartPointer<vtkGlyph3D>::New();
  glyph3D->SetIndexModeToScalar();
  glyph3D->SetInputArrayToProcess(0,0,0,0,"Height");
  glyph3D->SetSource(0, cubeSource->GetOutput());
  glyph3D->SetSource(1, coneSource->GetOutput());
  glyph3D->SetInput(input);
  glyph3D->SetRange(-1, 1);
  glyph3D->Update();

    //Create a mapper and actor
  vtkSmartPointer<vtkPolyDataMapper> mapper = 
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(glyph3D->GetOutputPort());
 
  vtkSmartPointer<vtkActor> actor = 
    vtkSmartPointer<vtkActor>::New();
  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();
  
  return EXIT_SUCCESS;
}

Please try the new VTKExamples website.

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(GlyphTable)

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

add_executable(GlyphTable MACOSX_BUNDLE GlyphTable.cxx)

if(VTK_LIBRARIES)
  target_link_libraries(GlyphTable ${VTK_LIBRARIES})
else()
  target_link_libraries(GlyphTable vtkHybrid vtkWidgets)
endif()

Download and Build GlyphTable

Click here to download GlyphTable. and its CMakeLists.txt file.

Once the tarball GlyphTable.tar has been downloaded and extracted,

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

./GlyphTable

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