PointInterpolator
VTKEx/Cxx/Meshes/PointInterpolator
Description¶
This example uses vtkPointInterpolator with a Gaussian Kernel (or other kernel) to interpolate and extrapolate more smoothly the fields inside and outside the probed area.
Info
This C++ code is translated from the python code that Kenichiro Yoshimi wrote to respond to Hosam. See the discourse discussion.
Other languages
See (Python)
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¶
PointInterpolator.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDelimitedTextReader.h>
#include <vtkGaussianKernel.h>
#include <vtkNamedColors.h>
#include <vtkPointData.h>
#include <vtkPointGaussianMapper.h>
#include <vtkPointInterpolator.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkTableToPolyData.h>
int main(int argc, char* argv[])
{
if (argc < 3)
{
std::cout << "Usage: " << argv[0]
<< " sparsePoints.txt InterpolatingOnSTL_final.stl" << std::endl;
return EXIT_FAILURE;
}
std::string pointsFile = argv[1];
std::string probeSurfaceFile = argv[2];
// Read a points data
auto pointsReader = vtkSmartPointer<vtkDelimitedTextReader>::New();
pointsReader->SetFileName(pointsFile.c_str());
pointsReader->DetectNumericColumnsOn();
pointsReader->SetFieldDelimiterCharacters("\t");
pointsReader->SetHaveHeaders(true);
auto tablePoints = vtkSmartPointer<vtkTableToPolyData>::New();
tablePoints->SetInputConnection(pointsReader->GetOutputPort());
tablePoints->SetXColumn("x");
tablePoints->SetYColumn("y");
tablePoints->SetZColumn("z");
tablePoints->Update();
vtkPolyData* points = tablePoints->GetOutput();
points->GetPointData()->SetActiveScalars("val");
double* range = points->GetPointData()->GetScalars()->GetRange();
// Read a probe surface
auto stlReader = vtkSmartPointer<vtkSTLReader>::New();
stlReader->SetFileName(probeSurfaceFile.c_str());
stlReader->Update();
vtkPolyData* surface = stlReader->GetOutput();
// double* bounds = surface->GetBounds();
// Gaussian kernel
auto gaussianKernel = vtkSmartPointer<vtkGaussianKernel>::New();
gaussianKernel->SetSharpness(2.0);
gaussianKernel->SetRadius(12.0);
auto interpolator = vtkSmartPointer<vtkPointInterpolator>::New();
interpolator->SetInputData(surface);
interpolator->SetSourceData(points);
interpolator->SetKernel(gaussianKernel);
// Visualize
auto colors = vtkSmartPointer<vtkNamedColors>::New();
auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(interpolator->GetOutputPort());
mapper->SetScalarRange(range);
auto actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
auto pointsMapper = vtkSmartPointer<vtkPointGaussianMapper>::New();
pointsMapper->SetInputData(points);
pointsMapper->SetScalarRange(range);
pointsMapper->SetScaleFactor(0.6);
pointsMapper->EmissiveOff();
pointsMapper->SetSplatShaderCode(
"//VTK::Color::Impl\n"
"float dist = dot(offsetVCVSOutput.xy,offsetVCVSOutput.xy);\n"
"if (dist > 1.0) {\n"
" discard;\n"
"} else {\n"
" float scale = (1.0 - dist);\n"
" ambientColor *= scale;\n"
" diffuseColor *= scale;\n"
"};\n");
auto pointsActor = vtkSmartPointer<vtkActor>::New();
pointsActor->SetMapper(pointsMapper);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->SetBackground(colors->GetColor3d("SlateGray").GetData());
auto renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindow->SetSize(640, 480);
renderWindow->SetWindowName("PointInterpolator");
auto iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
renderer->AddActor(pointsActor);
renderWindow->Render();
renderer->ResetCamera();
renderer->GetActiveCamera()->Elevation(-45);
renderWindow->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(PointInterpolator)
find_package(VTK COMPONENTS
vtkvtkCommonColor
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersGeneral
vtkvtkFiltersPoints
vtkvtkIOGeometry
vtkvtkIOInfovis
vtkvtkInteractionStyle
vtkvtkRenderingContextOpenGL2
vtkvtkRenderingCore
vtkvtkRenderingFreeType
vtkvtkRenderingGL2PSOpenGL2
vtkvtkRenderingOpenGL2 QUIET)
if (NOT VTK_FOUND)
message("Skipping PointInterpolator: ${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(PointInterpolator MACOSX_BUNDLE PointInterpolator.cxx )
target_link_libraries(PointInterpolator PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(PointInterpolator MACOSX_BUNDLE PointInterpolator.cxx )
target_link_libraries(PointInterpolator PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS PointInterpolator
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build PointInterpolator¶
Click here to download PointInterpolator and its CMakeLists.txt file. Once the tarball PointInterpolator.tar has been downloaded and extracted,
cd PointInterpolator/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:
./PointInterpolator
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.