FitImplicitFunction
VTKEx/Cxx/Points/FitImplicitFunction
Description¶
Warning
The classes used in this example require vtk 7.1 or later.
Other languages
See (Java)
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¶
FitImplicitFunction.cxx
#include <vtkSmartPointer.h>
#include <vtkFitImplicitFunction.h>
#include <vtkBoundedPointSource.h>
#include <vtkSphere.h>
#include <vtkSphereSource.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>
int main (int, char *[])
{
vtkMath::RandomSeed(4355412); // for test result consistency
double radius = 1.0;
vtkSmartPointer<vtkBoundedPointSource> points =
vtkSmartPointer<vtkBoundedPointSource>::New();
points->SetNumberOfPoints(1000000);
points->SetBounds(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
vtkSmartPointer<vtkSphere> sphere =
vtkSmartPointer<vtkSphere>::New();
sphere->SetRadius(radius);
vtkSmartPointer<vtkFitImplicitFunction> fit =
vtkSmartPointer<vtkFitImplicitFunction>::New();
fit->SetInputConnection(points->GetOutputPort());
fit->SetImplicitFunction(sphere);
fit->SetThreshold(.01);
fit->Update();
std::cout << fit->GetOutput()->GetNumberOfPoints() << " out of "
<< points->GetNumberOfPoints() << " points are within "
<< fit->GetThreshold() << " of the implicit function" << std::endl;
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetRadius(radius * .05);
vtkSmartPointer<vtkGlyph3D> glyph3D =
vtkSmartPointer<vtkGlyph3D>::New();
glyph3D->SetInputConnection(fit->GetOutputPort());
glyph3D->SetSourceConnection(sphereSource->GetOutputPort());
glyph3D->ScalingOff();
glyph3D->Update();
vtkSmartPointer<vtkPolyDataMapper> glyph3DMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());
vtkSmartPointer<vtkActor> glyph3DActor =
vtkSmartPointer<vtkActor>::New();
glyph3DActor->SetMapper(glyph3DMapper);
glyph3DActor->GetProperty()->SetColor(0.8900, 0.8100, 0.3400);
// Create graphics stuff
//
vtkSmartPointer<vtkRenderer> ren1 =
vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground(.3, .4, .6);
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
renWin->SetSize(512,512);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
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.3 FATAL_ERROR)
project(FitImplicitFunction)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore
vtkvtkFiltersPoints
vtkvtkFiltersSources
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping FitImplicitFunction: ${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(FitImplicitFunction MACOSX_BUNDLE FitImplicitFunction.cxx )
target_link_libraries(FitImplicitFunction PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
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}
)
endif ()
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.