ImplicitPolyDataDistance
vtk-examples/Cxx/PolyData/ImplicitPolyDataDistance
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
ImplicitPolyDataDistance.cxx
#include <vtkActor.h>
#include <vtkFloatArray.h>
#include <vtkImplicitPolyDataDistance.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkVertexGlyphFilter.h>
int main(int, char*[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetCenter(0.0, 0.0, 0.0);
sphereSource->SetRadius(1.0);
sphereSource->Update();
vtkNew<vtkPolyDataMapper> sphereMapper;
sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
sphereMapper->ScalarVisibilityOff();
vtkNew<vtkActor> sphereActor;
sphereActor->SetMapper(sphereMapper);
sphereActor->GetProperty()->SetOpacity(0.3);
sphereActor->GetProperty()->SetColor(1, 0, 0);
vtkNew<vtkImplicitPolyDataDistance> implicitPolyDataDistance;
implicitPolyDataDistance->SetInput(sphereSource->GetOutput());
// Setup a grid
vtkNew<vtkPoints> points;
float step = 0.1;
for (float x = -2.0; x <= 2.0; x += step)
{
for (float y = -2.0; y <= 2.0; y += step)
{
for (float z = -2.0; z <= 2.0; z += step)
{
points->InsertNextPoint(x, y, z);
}
}
}
// Add distances to each point
vtkNew<vtkFloatArray> signedDistances;
signedDistances->SetNumberOfComponents(1);
signedDistances->SetName("SignedDistances");
// Evaluate the signed distance function at all of the grid points
for (vtkIdType pointId = 0; pointId < points->GetNumberOfPoints(); ++pointId)
{
double p[3];
points->GetPoint(pointId, p);
float signedDistance = implicitPolyDataDistance->EvaluateFunction(p);
signedDistances->InsertNextValue(signedDistance);
}
vtkNew<vtkPolyData> polyData;
polyData->SetPoints(points);
polyData->GetPointData()->SetScalars(signedDistances);
vtkNew<vtkVertexGlyphFilter> vertexGlyphFilter;
vertexGlyphFilter->SetInputData(polyData);
vertexGlyphFilter->Update();
vtkNew<vtkPolyDataMapper> signedDistanceMapper;
signedDistanceMapper->SetInputConnection(vertexGlyphFilter->GetOutputPort());
signedDistanceMapper->ScalarVisibilityOn();
vtkNew<vtkActor> signedDistanceActor;
signedDistanceActor->SetMapper(signedDistanceMapper);
vtkNew<vtkRenderer> renderer;
renderer->AddViewProp(sphereActor);
renderer->AddViewProp(signedDistanceActor);
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("ImplicitPolyDataDistance");
vtkNew<vtkRenderWindowInteractor> renWinInteractor;
renWinInteractor->SetRenderWindow(renderWindow);
renderWindow->Render();
renWinInteractor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(ImplicitPolyDataDistance)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "ImplicitPolyDataDistance: 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(ImplicitPolyDataDistance MACOSX_BUNDLE ImplicitPolyDataDistance.cxx )
target_link_libraries(ImplicitPolyDataDistance PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ImplicitPolyDataDistance
MODULES ${VTK_LIBRARIES}
)
Download and Build ImplicitPolyDataDistance¶
Click here to download ImplicitPolyDataDistance and its CMakeLists.txt file. Once the tarball ImplicitPolyDataDistance.tar has been downloaded and extracted,
cd ImplicitPolyDataDistance/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:
./ImplicitPolyDataDistance
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.