转载:https://vtk.org/Wiki/VTK/Examples/Cxx/Points/ExtractSurface
链接:https://blog.csdn.net/hopefullight/article/details/79157144
//拟合类的算法以Hugues Hoppe提出的Signed Distance Function和Poisson方法为代表。所得曲面并不完全与数据点重合,而是进行了一定的合并和简化,并对噪声有一定鲁棒性。
//经典的Signed Distance Function重建算法主要流程如下:
//1、对每个数据点,搜索其邻域数据点,使用特征向量方法计算法线和法平面
//2、由于法线的方向可有正负两个不确定,故对全局使用最小生成树近似计算法线朝向
//3、以立方体素(voxel)为单位,沿着曲面滑动延展,计算每个格顶点到法平面的距离
//4、使用Marching Cube算法从立方体素中(插值地)提取三角面
//vtkExtractSurface使用了类似的方法,并可以利用Range的可见性信息以确定法线方向。
#include <vtkSmartPointer.h>
#include <vtkPLYReader.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkOBJReader.h>
#include <vtkSTLReader.h>
#include <vtkPointSource.h>
#include <vtkPCANormalEstimation.h>
#include <vtkSignedDistance.h>
#include <vtkExtractSurface.h>
#include <vtkPointData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>
#include <vtksys/SystemTools.hxx>
static vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName);
int main(int argc, char *argv[]) {
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName("../data/test.stl");
reader->Update();
vtkSmartPointer<vtkPolyData> polyData =
vtkSmartPointer<vtkPolyData>::New();
polyData = reader->GetOutput();
double bounds[6];
polyData->GetBounds(bounds);
double range[3];
for(int i = 0; i < 3; ++i) {
range[i] = bounds[2 * i + 1] - bounds[2 * i];
}
int sampleSize = polyData->GetNumberOfPoints() * .00005;
if(sampleSize < 10) {
sampleSize = 10;
}
std::cout << "Sample size is: " << sampleSize << std::endl;
// Do we need to estimate normals?
vtkSmartPointer<vtkSignedDistance> distance =
vtkSmartPointer<vtkSignedDistance>::New();
if(polyData->GetPointData()->GetNormals()) {
std::cout << "Using normals from input file" << std::endl;
distance->SetInputData(polyData);
} else {
std::cout << "Estimating normals using PCANormalEstimation" << std::endl;
vtkSmartPointer<vtkPCANormalEstimation> normals =
vtkSmartPointer<vtkPCANormalEstimation>::New();
normals->SetInputData(polyData);
normals->SetSampleSize(sampleSize);
normals->SetNormalOrientationToGraphTraversal();
normals->FlipNormalsOn();
distance->SetInputConnection(normals->GetOutputPort());
}
std::cout << "Range: "
<< range[0] << ", "
<< range[1] << ", "
<< range[2] << std::endl;
int dimension = 256;
double radius = range[0] * .02;
radius = range[0] / static_cast<double>(dimension) * 3; // ~3 voxels
std::cout << "Radius: " << radius << std::endl;
distance->SetRadius(radius);
distance->SetDimensions(dimension, dimension, dimension);
distance->SetBounds(
bounds[0] - range[0] * .1,
bounds[1] + range[0] * .1,
bounds[2] - range[1] * .1,
bounds[3] + range[1] * .1,
bounds[4] - range[2] * .1,
bounds[5] + range[2] * .1);
vtkSmartPointer<vtkExtractSurface> surface =
vtkSmartPointer<vtkExtractSurface>::New();
surface->SetInputConnection(distance->GetOutputPort());
surface->SetRadius(radius * .99);
surface->Update();
vtkSmartPointer<vtkPolyDataMapper> surfaceMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
surfaceMapper->SetInputConnection(surface->GetOutputPort());
vtkSmartPointer<vtkActor> surfaceActor =
vtkSmartPointer<vtkActor>::New();
surfaceActor->SetMapper(surfaceMapper);
surfaceActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
// Create graphics stuff
//
vtkSmartPointer<vtkRenderer> ren1 =
vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground(.3, .4, .6);
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
renWin->SetSize(512, 512);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
// Add the actors to the renderer, set the background and size
//
ren1->AddActor(surfaceActor);
// Generate an interesting view
//
ren1->ResetCamera();
ren1->GetActiveCamera()->Azimuth(120);
ren1->GetActiveCamera()->Elevation(30);
ren1->GetActiveCamera()->Dolly(1.0);
ren1->ResetCameraClippingRange();
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}
//下面这个是为读取不同形式的数据准备的
static vtkSmartPointer<vtkPolyData> ReadPolyData(const char *fileName) {
vtkSmartPointer<vtkPolyData> polyData;
std::string extension = vtksys::SystemTools::GetFilenameExtension(std::string(fileName));
if(extension == ".ply") {
vtkSmartPointer<vtkPLYReader> reader =
vtkSmartPointer<vtkPLYReader>::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
} else if(extension == ".vtp") {
vtkSmartPointer<vtkXMLPolyDataReader> reader =
vtkSmartPointer<vtkXMLPolyDataReader>::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
} else if(extension == ".obj") {
vtkSmartPointer<vtkOBJReader> reader =
vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
} else if(extension == ".stl") {
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(fileName);
reader->Update();
polyData = reader->GetOutput();
} else {
vtkSmartPointer<vtkPointSource> points =
vtkSmartPointer<vtkPointSource>::New();
points->SetNumberOfPoints(100000);
points->SetRadius(10.0);
points->SetCenter(vtkMath::Random(-100, 100),
vtkMath::Random(-100, 100),
vtkMath::Random(-100, 100));
points->SetDistributionToShell();
points->Update();
polyData = points->GetOutput();
}
return polyData;
}
int main() {
boost::progress_timer pt;
std::string filename = "../points.txt";
std::ifstream filestream(filename.c_str()); //文件流
std::string line;
vtkNew<vtkPoints> m_Points;
vtkNew<vtkCellArray> vertices ;
double x, y, z;
int i = 0;
while (std::getline(filestream, line)) { //整行读取文件
///double x, y, z;
std::stringstream linestream;
linestream << line;
//cout << line << endl;
linestream >> x >> y >> z;
m_Points->InsertNextPoint(x, y, z); //新读取的数据赋予点的几何结构
vertices->InsertNextCell(1); //_加入细胞顶点信息----用于渲染点集
vertices->InsertCellPoint(i++);
}
filestream.close(); //关闭文件流操作
//好,文件的读取到这就完成了
vtkNew<vtkPolyData> polyData ;
polyData->SetPoints(m_Points);获得网格模型中的几何数据:点集
polyData->SetPolys(vertices);
//下面就是点云曲面的重建
double bounds[6];
polyData->GetBounds(bounds);
double range[3];
for (int i = 0; i < 3; ++i) {
range[i] = bounds[2 * i + 1] - bounds[2 * i];
}
int sampleSize = polyData->GetNumberOfPoints() * .00005;//0.00005
if (sampleSize < 10) {
sampleSize = 10;//sampleSize = 10
}
std::cout << "Sample size is: " << sampleSize << std::endl;
// 法向量
vtkSmartPointer<vtkSignedDistance> distance =
vtkSmartPointer<vtkSignedDistance>::New();
if (polyData->GetPointData()->GetNormals()) {
std::cout << "Using normals from input file" << std::endl;
distance->SetInputData(polyData);
} else {
std::cout << "Estimating normals using PCANormalEstimation" << std::endl;
vtkSmartPointer<vtkPCANormalEstimation> normals =
vtkSmartPointer<vtkPCANormalEstimation>::New();
normals->SetInputData(polyData);
normals->SetSampleSize(sampleSize);
normals->SetNormalOrientationToGraphTraversal();
normals->FlipNormalsOn();
distance->SetInputConnection(normals->GetOutputPort());
}
std::cout << "Range: "
<< range[0] << ", "
<< range[1] << ", "
<< range[2] << std::endl;
int dimension = 300;//看情况作调整
double radius = range[0] * .02;
radius = range[0] / static_cast<double>(dimension) * 3; // ~3 voxels
std::cout << "Radius: " << radius << std::endl;
distance->SetRadius(radius);
distance->SetDimensions(dimension, dimension, dimension);
distance->SetBounds(
bounds[0] - range[0] * .1,
bounds[1] + range[0] * .1,
bounds[2] - range[1] * .1,
bounds[3] + range[1] * .1,
bounds[4] - range[2] * .1,
bounds[5] + range[2] * .1);
vtkSmartPointer<vtkExtractSurface> surface =
vtkSmartPointer<vtkExtractSurface>::New();
surface->SetInputConnection(distance->GetOutputPort());
surface->SetRadius(radius * 2.0); 参数自己看着调整
surface->Update();
vtkSmartPointer<vtkPolyDataMapper> surfaceMapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
surfaceMapper->SetInputConnection(surface->GetOutputPort());
vtkSmartPointer<vtkActor> surfaceActor =
vtkSmartPointer<vtkActor>::New();
surfaceActor->SetMapper(surfaceMapper);
surfaceActor->GetProperty()->SetColor(1.0000, 0.3882, 0.2784);
//--------------------------------------渲染管线可视化-----------------------------------------------
vtkSmartPointer<vtkRenderer> ren1 =
vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground(.3, .4, .6);
vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
renWin->SetSize(512, 512);
vtkSmartPointer<vtkRenderWindowInteractor> iren =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
renWin->Render();
renWin->SetWindowName("vtkExtractSurface");
ren1->AddActor(surfaceActor);
ren1->ResetCamera();
ren1->GetActiveCamera()->Azimuth(120);
ren1->GetActiveCamera()->Elevation(30);
ren1->GetActiveCamera()->Dolly(1.0);
ren1->ResetCameraClippingRange();
iren->Initialize();
iren->Start();
cout << "模型渲染完毕" << endl;
return EXIT_SUCCESS;
}