Skip to content

TransformSphere

Repository source: 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.)

Other languages

See (Python), (PythonicAPI)

Question

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

Code

TransformSphere.cxx

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

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

  vtkNew<vtkSphereSource> sphere;
  sphere->SetThetaResolution(12);
  sphere->SetPhiResolution(12);

  vtkNew<vtkTransform> aTransform;
  aTransform->Scale(1, 1.5, 2);

  vtkNew<vtkTransformFilter> transFilter;
  transFilter->SetInputConnection(sphere->GetOutputPort());
  transFilter->SetTransform(aTransform);

  vtkNew<vtkElevationFilter> colorIt;
  colorIt->SetInputConnection(transFilter->GetOutputPort());
  colorIt->SetLowPoint(0, 0, -1);
  colorIt->SetHighPoint(0, 0, 1);

  vtkNew<vtkLookupTable> lut;
  lut->SetHueRange(0.667, 0);
  lut->SetSaturationRange(1, 1);
  lut->SetValueRange(1, 1);

  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetLookupTable(lut);
  mapper->SetInputConnection(colorIt->GetOutputPort());

  vtkNew<vtkActor> actor;
  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->SetWindowName("TransformSphere");

  renWin->Render();

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(TransformSphere)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonTransforms
  FiltersCore
  FiltersGeneral
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "TransformSphere: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(TransformSphere MACOSX_BUNDLE TransformSphere.cxx )
  target_link_libraries(TransformSphere PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS TransformSphere
  MODULES ${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.