简体   繁体   English

在VTK中修改polyData点/顶点

[英]Modifying the polyData points/vertices in VTK

I'm trying to use VTK to change points, ie I am changing coordinates (x,y,z) by a (+1/-1) value. 我正在尝试使用VTK来更改点,即我正在以(+ 1 / -1)值更改坐标(x,y,z)。 I am reading an .OBJ file and then accessing the points of a model and when I am trying to visualise the changes in the model the RenderWindow doesnt show anything. 我正在读取.OBJ文件,然后访问模型的点,并且在尝试可视化模型中的更改时,RenderWindow没有显示任何内容。 Below is my code:- 下面是我的代码:

vtkSmartPointer<vtkOBJReader> reader = vtkSmartPointer<vtkOBJReader>::New();
reader->SetFileName(filename.c_str());
reader->Update();

vtkSmartPointer<vtkPolyData> polyData = reader->GetOutput();
polyData->Update();

Point3d point;
std::vector<Point3d> vertices; 

double p[3];        
vtkPoints* points = vtkPoints::New();       
vtkDoubleArray* pcoord = vtkDoubleArray::New();

pcoord->SetNumberOfComponents(3);       
pcoord->SetNumberOfTuples(polyData->GetNumberOfPoints());

for(vtkIdType i = 0; i < polyData->GetNumberOfPoints(); i++) 
{  
    polyData->GetPoint(i,p);
    p[0] +=1;
    p[1] +=1;
    p[2] +=1;
    pcoord->SetTuple(i, p);       
}

points->SetData(pcoord);
polyData->SetPoints(points);   
polyData->Modified();

//Visualize Code

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();
renderer->AddActor(actor);

vtkRenderWindow* renderWindow = vtkRenderWindow::New();
renderWindow->AddRenderer(renderer);

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow(renderWindow);

renderWindowInteractor->Initialize();
renderWindowInteractor->Start();

I am new to this 3D and VTK stuff, Please check where I am missing since I want to change the coordinates of each vertex (x,y,z). 我是3D和VTK的新手,请检查缺少的位置,因为我想更改每个顶点(x,y,z)的坐标。 Do I need to further change something else as well? 我还需要进一步更改其他内容吗?

Many Thanks. 非常感谢。

First thing, you have alignment issues in the code that you included in your question. 首先,您在问题中包含的代码中存在对齐问题。 I believe you are missing a for loop too. 我相信您也缺少for循环。 (where is "i" defined?) (在哪里定义“ i”?)

The major issue is that you are not rendering polyData at all. 主要问题是您根本没有渲染polyData。 The only thing you are rendering is what is in the .obj file. 您要呈现的唯一内容是.obj文件中的内容。 Evident from this line: 从这一行可以看出:

mapper->SetInputConnection(reader->GetOutputPort());

In order to render polyData replace the above line with: 为了渲染polyData,将以上行替换为:

mapper->SetInputData(polyData);

This assumes polyData was created correctly. 这假设polyData是正确创建的。 I can't easily tell this from the code you provided. 我无法从您提供的代码中轻易看出这一点。

Here is an example that may help. 是一个可能有帮助的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM