vtk保存与读取 ply 颜色
1 // 创建一个vtkPolyData对象 2 vtkSmartPointer<vtkPolyData> polyData = vtkSmartPointer<vtkPolyData>::New(); 3 4 // 创建点坐标 5 vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); 6 points->InsertNextPoint(0.0, 0.0, 0.0); 7 points->InsertNextPoint(1.0, 0.0, 0.0); 8 points->InsertNextPoint(0.0, 1.0, 0.0); 9 10 vtkSmartPointer<vtkCellArray> cells = vtkSmartPointer<vtkCellArray>::New(); 11 vtkSmartPointer<vtkTriangle> triangle = vtkSmartPointer<vtkTriangle>::New(); 12 13 triangle->GetPointIds()->SetId(0, 0); 14 triangle->GetPointIds()->SetId(1, 1); 15 triangle->GetPointIds()->SetId(2, 2); 16 cells->InsertNextCell(triangle); 17 18 // 设置点的颜色信息 19 vtkSmartPointer<vtkUnsignedCharArray> colors = vtkSmartPointer<vtkUnsignedCharArray>::New(); 20 colors->SetNumberOfComponents(3); // RGB 21 colors->SetName("Colors"); 22 unsigned char color1[3] = { 255, 0, 0 }; // Red 23 unsigned char color2[3] = { 0, 255, 0 }; // Green 24 unsigned char color3[3] = { 0, 0, 255 }; // Blue 25 colors->InsertNextTypedTuple(color1); 26 colors->InsertNextTypedTuple(color2); 27 colors->InsertNextTypedTuple(color3); 28 29 30 // 将点和颜色添加到vtkPolyData对象中 31 polyData->SetPoints(points); 32 polyData->SetPolys(cells); 33 polyData->Modified(); 34 polyData->GetPointData()->AddArray(colors); 35 36 std::cout << polyData->GetPointData()->GetArrayName(0) << std::endl; 37 38 // 保存PLY文件 39 vtkSmartPointer<vtkPLYWriter> writer = vtkSmartPointer<vtkPLYWriter>::New(); 40 writer->SetFileName("output_with_color.ply"); 41 writer->SetInputData(polyData); 42 writer->SetArrayName("Colors"); 43 writer->SetFileTypeToASCII(); 44 writer->Write();
//使用vtk 的方式来读取颜色 vtkNew<vtkPLYReader> read; read->SetFileName("C:\\Users\\6\\Desktop\\VTKQtTest\\VTKQtTest\\data\\Boolean_Result.ply"); read->Update(); vtkPolyData* polyData = read->GetOutput(); vtkDataArray *colorArray = polyData->GetPointData()->GetArray("RGB"); double color[3]; colorArray->GetTuple(0, color); std::cout <<"R: " << color[0] << " G: " << color[1] << " B: " << color[2] << std::endl;