# ContourQuadric

VTKExamples/Cxx/VisualizationAlgorithms/ContourQuadric

### Description

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

Info

See Figure 6-39 in Chapter 6 the VTK Textbook.

Other Languages

See (Python)

### Code

ContourQuadric.cxx

    #include <vtkActor.h>
    #include <vtkCamera.h>
    #include <vtkContourFilter.h>
    #include <vtkNamedColors.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 *[] )
    {
      vtkSmartPointer<vtkNamedColors> colors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkRenderer> aren =
        vtkSmartPointer<vtkRenderer>::New();

      vtkSmartPointer<vtkRenderWindow> renWin =
        vtkSmartPointer<vtkRenderWindow>::New();
      renWin->AddRenderer(aren);

      vtkSmartPointer<vtkRenderWindowInteractor> iren =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renWin);

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

      vtkSmartPointer<vtkContourFilter> contour =
        vtkSmartPointer<vtkContourFilter>::New();
      contour->SetInputConnection(sample->GetOutputPort());
      contour->GenerateValues(5,0,1.2);

      vtkSmartPointer<vtkPolyDataMapper> contourMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      contourMapper->SetInputConnection(contour->GetOutputPort());
      contourMapper->SetScalarRange(0,1.2);

      vtkSmartPointer<vtkActor> contourActor =
        vtkSmartPointer<vtkActor>::New();
      contourActor->SetMapper(contourMapper);

      // Create outline
      vtkSmartPointer<vtkOutlineFilter> outline =
        vtkSmartPointer<vtkOutlineFilter>::New();
      outline->SetInputConnection(sample->GetOutputPort());

      vtkSmartPointer<vtkPolyDataMapper> outlineMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      outlineMapper->SetInputConnection(outline->GetOutputPort());

      vtkSmartPointer<vtkActor> outlineActor =
        vtkSmartPointer<vtkActor>::New();
      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();

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

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ContourQuadric)

    find_package(VTK COMPONENTS
      vtkCommonColor
      vtkCommonDataModel
      vtkFiltersCore
      vtkFiltersModeling
      vtkImagingHybrid
      vtkInteractionStyle
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ContourQuadric: ${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(ContourQuadric MACOSX_BUNDLE ContourQuadric.cxx )
      target_link_libraries(ContourQuadric PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      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}
        )
    endif ()

### 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.