在vtk中用RGB保存网格

时间:2013-07-22 09:08:42

标签: c++ vtk

我加载了一个网格(带颜色),并希望将其写入.ply文件并存储rgb信息。我目前有下面的代码,它使用vtkPLYWriter类,但这只是保存顶点而不是RGB信息。是否有内置的方法来做到这一点?

代码

vtkSmartPointer<vtkTransformPolyDataFilter> rotate_and_save_mesh(vtkSmartPointer<vtkPLYReader> mesh_reader, double rotation_angle, double x, double y, double z, 
        std::string& out_name, bool should_write = true){
    vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
    transform->RotateWXYZ(rotation_angle, x, y, z);
    vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter = vtkSmartPointer<vtkTransformPolyDataFilter>::New();

    transformFilter->SetTransform(transform);
    transformFilter->SetInputConnection(mesh_reader->GetOutputPort());
    transformFilter->Update();

    if(should_write){
        vtkSmartPointer<vtkPLYWriter> writer = vtkSmartPointer<vtkPLYWriter>::New();
        writer->SetInputConnection(transformFilter->GetOutputPort());
        writer->SetFileName(out_name.c_str());

        writer->Write();
    }
    return transformFilter;
}

1 个答案:

答案 0 :(得分:1)

这很尴尬。与vtkPolyDataXMLWriter和大多数其他VTK编写器不同,您必须指定

plyWriter->SetArrayName("Colors");

您的颜色数组的创建/命名方式如下:

vtkSmartPointer<vtkUnsignedCharArray> colors =
  vtkSmartPointer<vtkUnsignedCharArray>::New();
colors->SetNumberOfComponents(3);
colors->SetName("Colors");