Skip to content


# ExtractPointsDemo

VTKEx/Cxx/Points/ExtractPointsDemo

### Description

Demonstrates point extraction from four implicit functions: sphere, cone, cylinder and superquadric.

Warning

The classes used in this example require vtk 7.1 or later.

Question

If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum

### Code

ExtractPointsDemo.cxx

    #include <vtkBoundedPointSource.h>
    #include <vtkCamera.h>
    #include <vtkCone.h>
    #include <vtkCylinder.h>
    #include <vtkExtractPoints.h>
    #include <vtkGlyph3DMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphere.h>
    #include <vtkSphereSource.h>
    #include <vtkSuperquadric.h>

    #include <vector>

    int main(int /*argc*/, char* /* argv */[])
    {
      // Create implicit functions
      auto cone = vtkSmartPointer<vtkCone>::New();
      cone->SetAngle(30.0);
      auto sphere = vtkSmartPointer<vtkSphere>::New();
      auto cylinder = vtkSmartPointer<vtkCylinder>::New();
      auto superquadric = vtkSmartPointer<vtkSuperquadric>::New();
      superquadric->SetPhiRoundness(2.5);
      superquadric->SetThetaRoundness(.5);

      // Store the functions
      std::vector<vtkSmartPointer<vtkImplicitFunction>> functions;
      functions.push_back(sphere);
      functions.push_back(cone);
      functions.push_back(cylinder);
      functions.push_back(superquadric);

      auto pointSource = vtkSmartPointer<vtkBoundedPointSource>::New();
      pointSource->SetNumberOfPoints(100000);

      // Rows and columns
      unsigned int gridXDimensions = 2;
      unsigned int gridYDimensions = 2;

      // Need a renderer even if there is no actor
      std::vector<vtkSmartPointer<vtkRenderer>> renderers;
      double background[6] = {.4, .5, .6, .6, .5, .4};
      for (size_t i = 0; i < gridXDimensions * gridYDimensions; i++)
      {
        renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
        renderers[i]->SetBackground(background);
        auto fwdIt = std::rotate(background, background + 1, background + 6);
      }

      // Glyphs
      double radius = .02;
      auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetRadius(radius);

      // One render window
      auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
      int rendererSize = 256;
      renderWindow->SetSize(rendererSize * gridXDimensions,
                            rendererSize * gridYDimensions);

      // Create a pipeline for each function
      for (int row = 0; row < static_cast<int>(gridYDimensions); row++)
      {
        for (int col = 0; col < static_cast<int>(gridXDimensions); col++)
        {
          int index = row * gridXDimensions + col;

          // (xmin, ymin, xmax, ymax)
          double viewport[4] = {static_cast<double>(col) * rendererSize /
                                    (gridXDimensions * rendererSize),
                                static_cast<double>(gridYDimensions - (row + 1)) *
                                    rendererSize / (gridYDimensions * rendererSize),
                                static_cast<double>(col + 1) * rendererSize /
                                    (gridXDimensions * rendererSize),
                                static_cast<double>(gridYDimensions - row) *
                                    rendererSize /
                                    (gridYDimensions * rendererSize)};
          renderWindow->AddRenderer(renderers[index]);
          renderers[index]->SetViewport(viewport);
          if (index > static_cast<int>(functions.size() - 1))
          {
            continue;
          }
          // Define the pipeline
          auto extract = vtkSmartPointer<vtkExtractPoints>::New();
          extract->SetInputConnection(pointSource->GetOutputPort());
          extract->SetImplicitFunction(functions[index]);

          auto glyph = vtkSmartPointer<vtkGlyph3DMapper>::New();
          glyph->SetInputConnection(extract->GetOutputPort());
          glyph->SetSourceConnection(sphereSource->GetOutputPort());
          glyph->ScalingOff();

          auto glyphActor = vtkSmartPointer<vtkActor>::New();
          glyphActor->SetMapper(glyph);

          renderers[index]->AddActor(glyphActor);
          renderers[index]->ResetCamera();
          renderers[index]->GetActiveCamera()->Azimuth(30);
          renderers[index]->GetActiveCamera()->Elevation(-30);
          renderers[index]->GetActiveCamera()->Dolly(1.1);
          renderers[index]->ResetCameraClippingRange();
        }
      }
      auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
      iren->SetRenderWindow(renderWindow);

      renderWindow->Render();
      iren->Initialize();
      iren->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(ExtractPointsDemo)

    find_package(VTK COMPONENTS
      vtkvtkCommonCore
      vtkvtkCommonDataModel
      vtkvtkFiltersPoints
      vtkvtkFiltersSources
      vtkvtkInteractionStyle
      vtkvtkRenderingContextOpenGL2
      vtkvtkRenderingCore
      vtkvtkRenderingFreeType
      vtkvtkRenderingGL2PSOpenGL2
      vtkvtkRenderingOpenGL2 QUIET)
    if (NOT VTK_FOUND)
      message("Skipping ExtractPointsDemo: ${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(ExtractPointsDemo MACOSX_BUNDLE ExtractPointsDemo.cxx )
      target_link_libraries(ExtractPointsDemo PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(ExtractPointsDemo MACOSX_BUNDLE ExtractPointsDemo.cxx )
      target_link_libraries(ExtractPointsDemo PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS ExtractPointsDemo
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build ExtractPointsDemo

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

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

    ./ExtractPointsDemo

WINDOWS USERS

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