Skip to content


# SurfaceFromUnorganizedPoints

VTKEx/Cxx/Filtering/SurfaceFromUnorganizedPoints

### Description

This example creates points on a sphere and then finds the surface through the points. If an optional polydata file is provided, the example operates on the points in that polydata.

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

SurfaceFromUnorganizedPoints.cxx

    #include <vtkCamera.h>
    #include <vtkContourFilter.h>
    #include <vtkNamedColors.h>
    #include <vtkPolyData.h>
    #include <vtkPolyDataMapper.h>
    #include <vtkProgrammableSource.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkReverseSense.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkSurfaceReconstructionFilter.h>
    #include <vtkXMLPolyDataReader.h>

    int main(int argc, char* argv[])
    {

      auto colors = vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkPolyData> input;
      if (argc > 1)
      {
        auto reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
        reader->SetFileName(argv[1]);
        reader->Update();
        input = reader->GetOutput();
      }
      else
      {
        auto sphereSource = vtkSmartPointer<vtkSphereSource>::New();
        sphereSource->Update();
        input = sphereSource->GetOutput();
      }

      auto polydata = vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints(input->GetPoints());

      // Construct the surface and create isosurface.
      auto surf = vtkSmartPointer<vtkSurfaceReconstructionFilter>::New();
      surf->SetInputData(polydata);

      auto cf = vtkSmartPointer<vtkContourFilter>::New();
      cf->SetInputConnection(surf->GetOutputPort());
      cf->SetValue(0, 0.0);

      // Sometimes the contouring algorithm can create a volume whose gradient
      // vector and ordering of polygon (using the right hand rule) are
      // inconsistent. vtkReverseSense cures this problem.
      auto reverse = vtkSmartPointer<vtkReverseSense>::New();
      reverse->SetInputConnection(cf->GetOutputPort());
      reverse->ReverseCellsOn();
      reverse->ReverseNormalsOn();

      auto map = vtkSmartPointer<vtkPolyDataMapper>::New();
      map->SetInputConnection(reverse->GetOutputPort());
      map->ScalarVisibilityOff();

      auto surfaceActor = vtkSmartPointer<vtkActor>::New();
      surfaceActor->SetMapper(map);
      surfaceActor->GetProperty()->SetDiffuseColor(
          colors->GetColor3d("Tomato").GetData());
      surfaceActor->GetProperty()->SetSpecularColor(
          colors->GetColor3d("Seashell").GetData());
      surfaceActor->GetProperty()->SetSpecular(.4);
      surfaceActor->GetProperty()->SetSpecularPower(50);

      // Create the RenderWindow, Renderer and both Actors
      auto renderer = vtkSmartPointer<vtkRenderer>::New();
      auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      renderWindow->SetSize(640, 480);

      auto interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
      interactor->SetRenderWindow(renderWindow);

      // Add the actors to the renderer, set the background and size
      renderer->AddActor(surfaceActor);
      renderer->SetBackground(colors->GetColor3d("Burlywood").GetData());

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

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(SurfaceFromUnorganizedPoints)

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

### Download and Build SurfaceFromUnorganizedPoints

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

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

    ./SurfaceFromUnorganizedPoints

WINDOWS USERS

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