VTK/Examples/Cxx/PolyData/EmbedPointsIntoVolume
From KitwarePublic
This example demonstrates how to embed 3D points into a 3D volume. An example set of points (sampled from a sphere) is here. The output is a vti file that can be viewed in paraview by opening it and changing "Display -> Representation" to "Volume". There are many parameters that can be tweaked - see the documentation for more details.
EmbedPointsIntoVolume.cxx
#include <vtkVersion.h> #include <vtkPolyData.h> #include <vtkImageData.h> #include <vtkSmartPointer.h> #include <vtkXMLPolyDataReader.h> #include <vtkXMLImageDataWriter.h> #include <vtkGaussianSplatter.h> #include <vtkSphereSource.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkDataSetMapper.h> #include <vtkActor.h> int main(int argc, char *argv[]) { vtkSmartPointer<vtkPolyData> input = vtkSmartPointer<vtkPolyData>::New(); if(argc == 1) // Generate the data { vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New(); sphereSource->Update(); input->ShallowCopy(sphereSource->GetOutput()); } else // Read the data from a file { vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New(); reader->SetFileName(argv[1]); reader->Update(); input->ShallowCopy(reader->GetOutput()); return EXIT_FAILURE; } vtkSmartPointer<vtkGaussianSplatter> splatter = vtkSmartPointer<vtkGaussianSplatter>::New(); #if VTK_MAJOR_VERSION <= 5 splatter->SetInputConnection(input->GetProducerPort()); #else splatter->SetInputData(input); #endif unsigned int n = 10; splatter->SetSampleDimensions(n,n,n); //splatter->SetRadius(0.01); //this is supposed to control how big of an area (how many neighboring voxels) a point distributes its contribution to. However, if set to 0.01 or below, no voxels seem to get any contribution. I thought if Radius=0 the point would contribue all of its weight to the voxel that it is inside? splatter->Update(); // Visualize vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New(); mapper->SetInputConnection(splatter->GetOutputPort()); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->SetMapper(mapper); vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); renderer->AddActor(actor); renderer->SetBackground(1,1,1); // Background color white renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) PROJECT(EmbedPointsIntoVolume) find_package(VTK REQUIRED) include(${VTK_USE_FILE}) if (APPLE) add_executable(EmbedPointsIntoVolume MACOSX_BUNDLE EmbedPointsIntoVolume.cxx) else() add_executable(EmbedPointsIntoVolume EmbedPointsIntoVolume.cxx) endif() if(VTK_LIBRARIES) target_link_libraries(EmbedPointsIntoVolume ${VTK_LIBRARIES}) else() target_link_libraries(EmbedPointsIntoVolume vtkHybrid ) endif()