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();
标签:polyData,颜色,0.0,vtk,writer,ply,vtkSmartPointer,colors,New From: https://www.cnblogs.com/lmao/p/18085690