我有以下代码从stl文件中读取网格数据并在窗口中显示它。代码工作正常;但我需要访问顶点和索引数组才能进行一些数据处理;更改网格,然后渲染它。我想知道如何能够访问那些具有网格顶点坐标的数组以及它们在形成网格三角形时的索引号。这似乎是一个非常基本的步骤,但我在VTK的wiki中找不到任何答案。
int main ( int argc, char *argv[] ) { std::string inputFilename = "Scapula.stl"; vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); reader->SetFileName(inputFilename.c_str()); reader->Update(); // Visualize vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); mapper->SetInputConnection(reader->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(.3, .6, .3); // Background color green renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
答案 0 :(得分:2)
所有这些信息都存储在vtkPolyData中,可以从vtkSTLreader中提取。这是我可以获取存储在stl文件中的网格的顶点和索引的方法:
//Data source
std::string inputFilename = "reamer.stl";
vtkSmartPointer<vtkSTLReader> reader =
vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(inputFilename.c_str());
reader->Update();
vtkSmartPointer<vtkPolyData> mesh = reader->GetOutput();
vtkSmartPointer<vtkPoints> points = mesh->GetPoints();
vtkSmartPointer<vtkDataArray> dataArray = points->GetData();
vtkIdType numberOfFaces = mesh->GetNumberOfCells();
mesh->GetCellPoints(i, faceIndex);
vertexIndex = faceIndex->GetId(0);
vertexArray[0] = dataArray->GetComponent(vertexIndex, 0);
vertexArray[1] = dataArray->GetComponent(vertexIndex, 1);
vertexArray[2] = dataArray->GetComponent(vertexIndex, 2);
vertexIndex = faceIndex->GetId(1);
vertexArray[3] = dataArray->GetComponent(vertexIndex, 0);
vertexArray[4] = dataArray->GetComponent(vertexIndex, 1);
vertexArray[5] = dataArray->GetComponent(vertexIndex, 2);
vertexIndex = faceIndex->GetId(2);
vertexArray[6] = dataArray->GetComponent(vertexIndex, 0);
vertexArray[7] = dataArray->GetComponent(vertexIndex, 1);
vertexArray[8] = dataArray->GetComponent(vertexIndex, 2);
其中vertexArray是具有i = 0的网格的第一个索引三角形的坐标的数组。你可以遍历i以获得其余三角形的坐标。
答案 1 :(得分:1)
这里是一个使用 vtkSphereSource 生成 polydata 的相关示例:
vtkNew<vtkSphereSource> sphere;
sphere->SetCenter(0.0, 0.0, 0.0);
sphere->SetRadius(radius);
sphere->SetPhiResolution(resolution);
sphere->SetThetaResolution(resolution);
sphere->Update();
vtkSmartPointer<vtkPolyData> polydata = sphere->GetOutput();
vtkPoints* points = polydata->GetPoints();
vector<vector<double>> vertex_list;
int num_vertices = points->GetNumberOfPoints();
for (int i = 0; i < num_vertices; i++) {
double pt[3];
points->GetPoint(i, pt);
vertex_list.push_back(vector<double>{pt[0], pt[1], pt[2]});
}
vector<vector<int>> wall_list;
int num_walls = polydata->GetNumberOfCells();
for (int i = 0; i < num_walls; i++) {
vtkCell* cell = polydata->GetCell(i);
vector<int> wall;
for (int k = 0; k < 3; k++) {
wall.push_back(cell->GetPointId(k));
}
wall_list.push_back(wall);
}