TransformSphere

VTKExamples/Cxx/Rendering/TransformSphere


Description

This example extends the pipeline of the ColoredSphere.cxx example and shows the effects of type checking on the connectivity of process objects. We add a transform filter vtkTransformFilter) to nonuniformly scale the sphere in the x-y-z directions. The transform filter only operates on objects with explicit point coordinate representation (i.e., a subclass of vtkPointSet ). However, the elevation filter generates the more general form vtkDataSet as output. Hence we cannot connect the transform filter to the elevation filter. But we can connect the transform filter to the sphere source, and then the elevation filter to the transform filter. (Note: an alternative method is to use vtkCastToConcrete to perform run-time casting.)

Code

TransformSphere.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkElevationFilter.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>
#include <vtkTransformFilter.h>

int main(int, char *[])
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();
  vtkSmartPointer<vtkRenderer> renderer =
    vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renWin =
    vtkSmartPointer<vtkRenderWindow>::New();
  renWin->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> iren =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  iren->SetRenderWindow(renWin);

  vtkSmartPointer<vtkSphereSource> sphere =
    vtkSmartPointer<vtkSphereSource>::New();
  sphere->SetThetaResolution(12); sphere->SetPhiResolution(12);

  vtkSmartPointer<vtkTransform> aTransform =
    vtkSmartPointer<vtkTransform>::New();
  aTransform->Scale(1,1.5,2);

  vtkSmartPointer<vtkTransformFilter> transFilter =
    vtkSmartPointer<vtkTransformFilter>::New();
  transFilter->SetInputConnection(sphere->GetOutputPort());
  transFilter->SetTransform(aTransform);  

  vtkSmartPointer<vtkElevationFilter> colorIt =
    vtkSmartPointer<vtkElevationFilter>::New();
  colorIt->SetInputConnection(transFilter->GetOutputPort());
  colorIt->SetLowPoint(0,0,-1);
  colorIt->SetHighPoint(0,0,1);

  vtkSmartPointer<vtkLookupTable> lut =
    vtkSmartPointer<vtkLookupTable>::New();
  lut->SetHueRange(0.667, 0);
  lut->SetSaturationRange(1,1);
  lut->SetValueRange(1,1);

  vtkSmartPointer<vtkDataSetMapper> mapper =
    vtkSmartPointer<vtkDataSetMapper>::New();
  mapper->SetLookupTable(lut);
  mapper->SetInputConnection(colorIt->GetOutputPort());

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

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
  renderer->ResetCamera();
  renderer->GetActiveCamera()->Elevation(60.0);
  renderer->GetActiveCamera()->Azimuth(30.0);
  renderer->ResetCameraClippingRange();

  renWin->SetSize(640, 480);

  renWin->Render();

  // interact with data
  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

PROJECT(TransformSphere)

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

add_executable(TransformSphere MACOSX_BUNDLE TransformSphere.cxx )

target_link_libraries(TransformSphere ${VTK_LIBRARIES})

Download and Build TransformSphere

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

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

./TransformSphere

WINDOWS USERS

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