原文链接:https://blog.csdn.net/theArcticOcean/article/details/86228697
///<vtkPolyDataNormals>输出点的法向量/
#include <vtkSmartPointer.h>
#include <vtkPolyDataReader.h>
#include <vtkPolyDataNormals.h> //计算法向量
#include <vtkMaskPoints.h>
#include <vtkArrowSource.h>
#include <vtkGlyph3D.h>
#include <vtkPointData.h>
#include <vtkProperty.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
//
#include <vtkSTLReader.h>
#include <vtkCellData.h>
int main() {
// ...
ofstream outfile1;//创建一个ofstream对象
outfile1.open("../save/Normal.dat");
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();//大的
reader->SetFileName("../data/test.stl");
reader->Update();
// 获取模型的顶点数量
int numPts = reader->GetOutput()->GetPoints()->GetNumberOfPoints();
cout << numPts << endl;
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkActor> surfaceActor =
vtkSmartPointer<vtkActor>::New();
surfaceActor->SetMapper(mapper);
vtkSmartPointer<vtkPolyDataNormals> pdNormals =
vtkSmartPointer<vtkPolyDataNormals>::New();
pdNormals->SetInputData(surfaceActor->GetMapper()->GetInput());
//pdNormals->ComputeCellNormalsOn();//作用同下两句
pdNormals->SetComputePointNormals(1);//开启点法向量计算
pdNormals->SetComputeCellNormals(0); //关闭单元法向量计算
pdNormals->SetAutoOrientNormals(1);
pdNormals->SetSplitting(0);
pdNormals->Update();
vtkPointData *ptData = pdNormals->GetOutput()->GetPointData();
vtkDataArray *ptNormals = pdNormals->GetOutput()->GetPointData()->GetNormals();
cout << "计算每个点的法向量: \n";
cout << ptNormals->GetNumberOfTuples() << endl;
for(int i = 0; i < ptNormals->GetNumberOfTuples(); ++i) {
double value[3];
ptNormals->GetTuple(i, value);
printf("Value: (%lf, %lf, %lf)\n", value[0], value[1], value[2]);
outfile1 << value[0] << " " << value[1] << " " << value[2] << endl;
}
cout << "计算每个面的法向量: \n";
if(pdNormals->GetOutput()->GetCellData() && pdNormals->GetOutput()->GetCellData()->GetNormals()) {
vtkDataArray *cellNormals = pdNormals->GetOutput()->GetCellData()->GetNormals();
cout << cellNormals->GetNumberOfTuples() << endl;
for(int i = 0; i < cellNormals->GetNumberOfTuples(); ++i) {
double value[3];
cellNormals->GetTuple(i, value);
printf("Value: (%lf, %lf, %lf)\n", value[0], value[1], value[2]);
}
}
return 0;
}