问题描述:有一个.vtk点集,带triangle mesh,这些点构成一个闭合的三维物体表面。想计算这些点围成的这个closed object的内部体积。
解决思路:用vtkMassProperties就像这里对vtkMassProperties的提示: Currently only triangles are processed. Use vtkTriangleFilter to convert any strips or polygons to triangles. 也就是说,目前该类只能用于triangle mesh connectivity closed surface.
代码,polyDataVolume.cpp文件:
#include <vtkPolyDataReader.h>
#include "vtkPolyData.h"
#include "vtkSmartPointer.h"
#include <vtkMassProperties.h>
int main( int argc, char* argv[] ){
// read in the .vtk file
vtkSmartPointer<vtkPolyDataReader> reader = vtkSmartPointer<vtkPolyDataReader>::New();
reader->SetFileName(argv[1]);
reader->Update();
vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New();
polyData = reader->GetOutput();
// evaluate the volume
vtkMassProperties *mass = vtkMassProperties::New();
mass->SetInput(polyData);
cout << "---- the volume of this vtk polydata is: " << mass->GetVolume() << endl;
return 0;
}
CMakeLists.txt文件:
cmake_minimum_required(VERSION 2.8)
PROJECT(polyDataVolume)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(polyDataVolume MACOSX_BUNDLE polyDataVolume)
if(VTK_LIBRARIES)
target_link_libraries(polyDataVolume ${VTK_LIBRARIES})
else()
target_link_libraries(polyDataVolume vtkHybrid vtkWidgets)
endif()
编译:
ccmake ../
make
运行:
./polyDataVolume ../test.vtk
运行结果:
---- the volume of this vtk polydatais: 0.00339752
这个小实验的代码已打包,可点这里下载。
参考:
http://www.vtk.org/pipermail/vtkusers/2013-June/080004.html
http://www.vtk.org/Wiki/VTK/Examples/Cxx/PolyData/EmbedPointsIntoVolume
http://markmail.org/message/6u6psqha5ckz2ec3
http://www.paraview.org/Wiki/VTK/Examples/Cxx/PolyData/PolyDataToImageData
http://permalink.gmane.org/gmane.comp.lib.vtk.user/67470
http://public.kitware.com/pipermail/vtkusers/2011-January/065437.html
http://vtk.1045678.n5.nabble.com/Polydata-volume-calculation-problem-td3347493.html
http://public.kitware.com/pipermail/vtkusers/2014-July/084469.html
http://www.paraview.org/Wiki/VTK/Examples/Cxx/Modelling/Delaunay3D