MarchingCubes
vtk-examples/Cxx/Modelling/MarchingCubes
Description¶
Creates a surface from a volume using Flying Edges or Marching Cubes.
Without arguments, the examples generates a voxelized sphere with vtkVoxelModeller.
Note
vtkVoxelModeller by default produces a VTK_BIT scalar image. Marching Cubes does not support this type. The scalar output is set to float for this example.
To generate a surface from a DICOM series, provide a folder containing the series and specify an isovalue for the surface.
This Midas Repository contains a number of DICOM datasets.
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
MarchingCubes.cxx
#include <vtkActor.h>
#include <vtkDICOMImageReader.h>
#include <vtkImageData.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSphereSource.h>
#include <vtkVersion.h>
#include <vtkVoxelModeller.h>
// vtkFlyingEdges3D was introduced in VTK >= 8.2
#if VTK_MAJOR_VERSION >= 9 || (VTK_MAJOR_VERSION >= 8 && VTK_MINOR_VERSION >= 2)
#define USE_FLYING_EDGES
#else
#undef USE_FLYING_EDGES
#endif
#ifdef USE_FLYING_EDGES
#include <vtkFlyingEdges3D.h>
#else
#include <vtkMarchingCubes.h>
#endif
int main(int argc, char* argv[])
{
vtkNew<vtkNamedColors> colors;
vtkNew<vtkImageData> volume;
double isoValue;
if (argc < 3)
{
vtkNew<vtkSphereSource> sphereSource;
sphereSource->SetPhiResolution(20);
sphereSource->SetThetaResolution(20);
sphereSource->Update();
double bounds[6];
sphereSource->GetOutput()->GetBounds(bounds);
for (unsigned int i = 0; i < 6; i += 2)
{
double range = bounds[i + 1] - bounds[i];
bounds[i] = bounds[i] - 0.1 * range;
bounds[i + 1] = bounds[i + 1] + 0.1 * range;
}
vtkNew<vtkVoxelModeller> voxelModeller;
voxelModeller->SetSampleDimensions(50, 50, 50);
voxelModeller->SetModelBounds(bounds);
voxelModeller->SetScalarTypeToFloat();
voxelModeller->SetMaximumDistance(0.1);
voxelModeller->SetInputConnection(sphereSource->GetOutputPort());
voxelModeller->Update();
isoValue = 0.5;
volume->DeepCopy(voxelModeller->GetOutput());
}
else
{
vtkNew<vtkDICOMImageReader> reader;
reader->SetDirectoryName(argv[1]);
reader->Update();
volume->DeepCopy(reader->GetOutput());
isoValue = atof(argv[2]);
}
#ifdef USE_FLYING_EDGES
vtkNew<vtkFlyingEdges3D> surface;
#else
vtkNew<vtkMarchingCubes> surface;
#endif
surface->SetInputData(volume);
surface->ComputeNormalsOn();
surface->SetValue(0, isoValue);
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(colors->GetColor3d("DarkSlateGray").GetData());
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->AddRenderer(renderer);
renderWindow->SetWindowName("MarchingCubes");
vtkNew<vtkRenderWindowInteractor> interactor;
interactor->SetRenderWindow(renderWindow);
vtkNew<vtkPolyDataMapper> mapper;
mapper->SetInputConnection(surface->GetOutputPort());
mapper->ScalarVisibilityOff();
vtkNew<vtkActor> actor;
actor->SetMapper(mapper);
actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());
renderer->AddActor(actor);
renderWindow->Render();
interactor->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(MarchingCubes)
find_package(VTK COMPONENTS
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "MarchingCubes: 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(MarchingCubes MACOSX_BUNDLE MarchingCubes.cxx )
target_link_libraries(MarchingCubes PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS MarchingCubes
MODULES ${VTK_LIBRARIES}
)
Download and Build MarchingCubes¶
Click here to download MarchingCubes and its CMakeLists.txt file. Once the tarball MarchingCubes.tar has been downloaded and extracted,
cd MarchingCubes/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:
./MarchingCubes
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.