Skip to content


# NormalEstimation

VTKEx/Cxx/Points/NormalEstimation

### Description

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

NormalEstimation.cxx

    #include <vtkSmartPointer.h>
    #include <vtkPCANormalEstimation.h>
    #include <vtkPointSource.h>

    #include <vtkSphereSource.h>
    #include <vtkArrowSource.h>
    #include <vtkGlyph3D.h>
    #include <vtkMath.h>

    #include <vtkPolyDataMapper.h>
    #include <vtkProperty.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkRenderer.h>
    #include <vtkCamera.h>
    #include <vtkNamedColors.h>

    namespace
    {
    void MakeGlyphs(vtkPolyData *src, double size, vtkGlyph3D *glyph);
    }

    int main (int, char *[])
    {
      vtkMath::RandomSeed(4355412); // for test result consistency

      double radius = 1.0;
      vtkSmartPointer<vtkPointSource> points =
        vtkSmartPointer<vtkPointSource>::New();
      points->SetNumberOfPoints(1000);
      points->SetRadius(radius);
      points->SetCenter(0.0, 0.0, 0.0);
      points->SetDistributionToShell();

      int sampleSize = 10;
      vtkSmartPointer<vtkPCANormalEstimation> normals =
        vtkSmartPointer<vtkPCANormalEstimation>::New();
      normals->SetInputConnection (points->GetOutputPort());
      normals->SetSampleSize(sampleSize);
      normals->SetNormalOrientationToGraphTraversal();
      normals->Update();

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

      vtkSmartPointer<vtkGlyph3D> glyph3D =
        vtkSmartPointer<vtkGlyph3D>::New();
      MakeGlyphs(normals->GetOutput(), radius * .2, glyph3D.GetPointer());

      vtkSmartPointer<vtkPolyDataMapper> glyph3DMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());

      vtkSmartPointer<vtkActor> glyph3DActor =
        vtkSmartPointer<vtkActor>::New();
      glyph3DActor->SetMapper(glyph3DMapper);
      glyph3DActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());

      vtkSmartPointer<vtkSphereSource> sphere =
        vtkSmartPointer<vtkSphereSource>::New();
      sphere->SetRadius(1.0);
      sphere->SetThetaResolution(41);
      sphere->SetPhiResolution(21);

      vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
        vtkSmartPointer<vtkPolyDataMapper>::New();
      sphereMapper->SetInputConnection(sphere->GetOutputPort());

      vtkSmartPointer<vtkActor> sphereActor =
        vtkSmartPointer<vtkActor>::New();
      sphereActor->SetMapper(sphereMapper);
      sphereActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Tomato").GetData());

      // Create graphics stuff
      //
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());

      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->AddRenderer(renderer);
      renderWindow->SetSize(640, 480);

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

      // Add the actors to the renderer, set the background and size
      //
      renderer->AddActor(glyph3DActor);
      renderer->AddActor(sphereActor);

      // Generate an interesting view
      //
      renderer->ResetCamera();
      renderer->GetActiveCamera()->Azimuth(120);
      renderer->GetActiveCamera()->Elevation(30);
      renderer->GetActiveCamera()->Dolly(1.0);
      renderer->ResetCameraClippingRange();

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

      return EXIT_SUCCESS;
    }
    namespace
    {
    void MakeGlyphs(vtkPolyData *src, double size, vtkGlyph3D *glyph)
    {
      // Source for the glyph filter
      vtkSmartPointer<vtkArrowSource> arrow =
        vtkSmartPointer<vtkArrowSource>::New();
      arrow->SetTipResolution(16);
      arrow->SetTipLength(.3);
      arrow->SetTipRadius(.1);

      glyph->SetSourceConnection(arrow->GetOutputPort());
      glyph->SetInputData(src);
      glyph->SetVectorModeToUseNormal();
      glyph->SetScaleModeToScaleByVector();
      glyph->SetScaleFactor(size);
      glyph->OrientOn();
      glyph->Update();
    }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(NormalEstimation)

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

### Download and Build NormalEstimation

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

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

    ./NormalEstimation

WINDOWS USERS

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