Skip to content

FitImplicitFunction

vtk-examples/Cxx/Points/FitImplicitFunction


Other languages

See (Java)

Question

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

Code

FitImplicitFunction.cxx

#include <vtkBoundedPointSource.h>
#include <vtkCamera.h>
#include <vtkFitImplicitFunction.h>
#include <vtkGlyph3D.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphere.h>
#include <vtkSphereSource.h>

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

  double radius = 1.0;
  vtkNew<vtkBoundedPointSource> points;
  points->SetNumberOfPoints(1000000);
  points->SetBounds(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);

  vtkNew<vtkSphere> sphere;
  sphere->SetRadius(radius);

  vtkNew<vtkFitImplicitFunction> fit;
  fit->SetInputConnection(points->GetOutputPort());
  fit->SetImplicitFunction(sphere);
  fit->SetThreshold(0.01);
  fit->Update();
  std::cout << fit->GetOutput()->GetNumberOfPoints() << " out of "
            << points->GetNumberOfPoints() << " points are within "
            << fit->GetThreshold() << " of the implicit function" << std::endl;

  vtkNew<vtkSphereSource> sphereSource;
  sphereSource->SetRadius(radius * 0.05);

  vtkNew<vtkGlyph3D> glyph3D;
  glyph3D->SetInputConnection(fit->GetOutputPort());
  glyph3D->SetSourceConnection(sphereSource->GetOutputPort());
  glyph3D->ScalingOff();
  glyph3D->Update();

  vtkNew<vtkPolyDataMapper> glyph3DMapper;
  glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());

  vtkNew<vtkActor> glyph3DActor;
  glyph3DActor->SetMapper(glyph3DMapper);
  glyph3DActor->GetProperty()->SetColor(colors->GetColor3d("Banana").GetData());

  // Create graphics stuff.
  vtkNew<vtkRenderer> ren1;
  ren1->SetBackground(colors->GetColor3d("CornflowerBlue").GetData());

  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren1);
  renWin->SetSize(512, 512);
  renWin->SetWindowName("FitImplicitFunction");

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

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

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

  renWin->Render();
  iren->Initialize();
  iren->Start();
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(FitImplicitFunction)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersCore
  FiltersPoints
  FiltersSources
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

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

Download and Build FitImplicitFunction

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

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

./FitImplicitFunction

WINDOWS USERS

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