Skip to content

ContourQuadric

vtk-examples/Cxx/VisualizationAlgorithms/ContourQuadric


Description

Contouring quadric function. Pipeline topology, and C++ code

Other languages

See (Python)

Question

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

Code

ContourQuadric.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkContourFilter.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkOutlineFilter.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkQuadric.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSampleFunction.h>

int main(int, char*[])
{
  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> aren;

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(aren);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  //
  // Create surfaces F(x,y,z) = constant
  //
  // Sample quadric function
  vtkNew<vtkQuadric> quadric;
  quadric->SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 0, 0);
  vtkNew<vtkSampleFunction> sample;
  sample->SetSampleDimensions(50, 50, 50);
  sample->SetImplicitFunction(quadric);

  vtkNew<vtkContourFilter> contour;
  contour->SetInputConnection(sample->GetOutputPort());
  contour->GenerateValues(5, 0, 1.2);

  vtkNew<vtkPolyDataMapper> contourMapper;
  contourMapper->SetInputConnection(contour->GetOutputPort());
  contourMapper->SetScalarRange(0, 1.2);

  vtkNew<vtkActor> contourActor;
  contourActor->SetMapper(contourMapper);

  // Create outline
  vtkNew<vtkOutlineFilter> outline;
  outline->SetInputConnection(sample->GetOutputPort());

  vtkNew<vtkPolyDataMapper> outlineMapper;
  outlineMapper->SetInputConnection(outline->GetOutputPort());

  vtkNew<vtkActor> outlineActor;
  outlineActor->SetMapper(outlineMapper);
  outlineActor->GetProperty()->SetColor(colors->GetColor3d("Brown").GetData());
  outlineActor->GetProperty()->SetLineWidth(3.0);

  //
  // Rendering stuff
  //
  aren->SetBackground(colors->GetColor3d("SlateGray").GetData());
  aren->AddActor(contourActor);
  aren->AddActor(outlineActor);

  aren->ResetCamera();
  aren->GetActiveCamera()->Azimuth(30);
  aren->GetActiveCamera()->Elevation(30);

  renWin->SetSize(640, 512);
  renWin->Render();
  renWin->SetWindowName("ContourQuadric");

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

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ContourQuadric)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersModeling
  ImagingHybrid
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ContourQuadric: 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(ContourQuadric MACOSX_BUNDLE ContourQuadric.cxx )
  target_link_libraries(ContourQuadric PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ContourQuadric
  MODULES ${VTK_LIBRARIES}
)

Download and Build ContourQuadric

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

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

./ContourQuadric

WINDOWS USERS

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