ParametricObjects

VTKExamples/Cxx/GeometricObjects/ParametricObjects


Description

See Parametric Equations for Surfaces by Andrew Maclean for an excellent description of these beautiful parametric surfaces.

You can edit the following code by selecting any one of the functions and the corresponding object will be displayed.

Other Languages

See (Python), (Java), (CSharp)

Code

ParametricObjects.cxx

#include <vtkActor.h>
#include <vtkNamedColors.h>
#include <vtkParametricFunctionSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>

// Uncomment one of the following includes that correspond
//  to the object that you have selected below:
// #include <vtkParametricBoy.h>
// #include <vtkParametricConicSpiral.h>
// #include <vtkParametricCrossCap.h>
// #include <vtkParametricDini.h>
// #include <vtkParametricEllipsoid.h>
// #include <vtkParametricEnneper.h>
#include <vtkParametricFigure8Klein.h>
// #include <vtkParametricKlein.h>
// #include <vtkParametricMobius.h>
// #include <vtkParametricRandomHills.h>
// #include <vtkParametricRoman.h>
// #include <vtkParametricSpline.h>
// #include <vtkParametricSuperEllipsoid.h>
// #include <vtkParametricSuperToroid.h>
// #include <vtkParametricTorus.h>

#include <array>

int main(int, char* [])
{
  vtkSmartPointer<vtkNamedColors> colors =
    vtkSmartPointer<vtkNamedColors>::New();

  // Set the background color.
  std::array<unsigned char , 4> bkg{{26, 51, 102, 255}};
    colors->SetColor("BkgColor", bkg.data());

  // Uncomment one of the following and
  //   ensure the matching include (above) is umcommented).
  // vtkSmartPointer<vtkParametricBoy> parametricObject =
  // vtkSmartPointer<vtkParametricBoy>::New();
  // vtkSmartPointer<vtkParametricConicSpiral> parametricObject =
  // vtkSmartPointer<vtkParametricConicSpiral>::New();
  // vtkSmartPointer<vtkParametricCrossCap> parametricObject =
  // vtkSmartPointer<vtkParametricCrossCap>::New();
  // vtkSmartPointer<vtkParametricDini> parametricObject =
  // vtkSmartPointer<vtkParametricDini>::New();
  // vtkSmartPointer<vtkParametricEllipsoid> parametricObject =
  // vtkSmartPointer<vtkParametricEllipsoid>::New();
  // vtkSmartPointer<vtkParametricEnneper> parametricObject =
  // vtkSmartPointer<vtkParametricEnneper>::New();
  vtkSmartPointer<vtkParametricFigure8Klein> parametricObject =
  vtkSmartPointer<vtkParametricFigure8Klein>::New();
  // vtkSmartPointer<vtkParametricKlein> parametricObject =
  // vtkSmartPointer<vtkParametricKlein>::New();
  // vtkSmartPointer<vtkParametricMobius> parametricObject =
  // vtkSmartPointer<vtkParametricMobius>::New();
  // vtkSmartPointer<vtkParametricRandomHills> parametricObject =
  // vtkSmartPointer<vtkParametricRandomHills>::New();
  // vtkSmartPointer<vtkParametricRoman> parametricObject =
  // vtkSmartPointer<vtkParametricRoman>::New();
  // vtkSmartPointer<vtkParametricSpline> parametricObject =
  // vtkSmartPointer<vtkParametricSpline>::New();
  // vtkSmartPointer<vtkParametricSuperEllipsoid> parametricObject =
  // vtkSmartPointer<vtkParametricSuperEllipsoid>::New();
  // vtkSmartPointer<vtkParametricSuperToroid> parametricObject =
  // vtkSmartPointer<vtkParametricSuperToroid>::New();
  // vtkSmartPointer<vtkParametricTorus> parametricObject =
  // vtkSmartPointer<vtkParametricTorus>::New();

  vtkSmartPointer<vtkParametricFunctionSource> parametricFunctionSource =
    vtkSmartPointer<vtkParametricFunctionSource>::New();
  parametricFunctionSource->SetParametricFunction(parametricObject);
  parametricFunctionSource->Update();

  // Visualize

  vtkSmartPointer<vtkProperty> backProperty =
    vtkSmartPointer<vtkProperty>::New();
  backProperty->SetColor(colors->GetColor3d("Tomato").GetData());

  vtkSmartPointer<vtkPolyDataMapper> mapper =
    vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(parametricFunctionSource->GetOutputPort());

  // Create an actor for the contours
  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);
  actor->GetProperty()->SetDiffuseColor(
    colors->GetColor3d("Banana").GetData());
  actor->GetProperty()->SetSpecular(.5);
  actor->GetProperty()->SetSpecularPower(20);
  actor->SetBackfaceProperty(backProperty);
  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetWindowName("Parametric Objects");
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> interactor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  interactor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(ParametricObjects)

find_package(VTK COMPONENTS 
  vtkCommonColor
  vtkCommonComputationalGeometry
  vtkCommonCore
  vtkFiltersSources
  vtkInteractionStyle
  vtkRenderingCore
  vtkRenderingFreeType
  vtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
  message("Skipping ParametricObjects: ${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(ParametricObjects MACOSX_BUNDLE ParametricObjects.cxx )
  target_link_libraries(ParametricObjects PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(ParametricObjects MACOSX_BUNDLE ParametricObjects.cxx )
  target_link_libraries(ParametricObjects PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS ParametricObjects
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build ParametricObjects

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

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

./ParametricObjects

WINDOWS USERS

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