目标是从几个点画线 我有2个向量tab_recupere_P1和tab_recupere_P2,每个向量的大小是14。 我浏览了两个向量,我在每次迭代时得到PP1和PP2点以构建一条线。
我使用了几种方法,但都给出了相同的结果
这是我的代码:方法1:
for ( std::vector<Coord_point*>::iterator i = tab_recupere_P1.begin();
i != tab_recupere_P1.end();
++i)
{
new_p = *i;
pp1[0] = new_p->p0[0];
pp1[1] = new_p->p0[1];
pp1[2] = new_p->p0[2];
std::cout << " x : "<< pp1[0] << " y : " << pp1[1] << " z : " << pp1[2] <<std::endl;
for ( std::vector<Coord_point*>::iterator j = tab_recupere_P2.begin();
j != tab_recupere_P2.end();
j++)
{
new_p2 = *j;
pp2[0] = new_p2->p0[0];
pp2[1] = new_p2->p0[1];
pp2[2] = new_p2->p0[2];
std::cout << " x2 : "<< pp2[0] << " y2 : " << pp2[1] << " z2 : " << pp2[2] <<std::endl;
// Add the two poitns to a vtkPoints object
vtkSmartPointer<vtkPoints> pts = vtkSmartPointer<vtkPoints>::New();
pts->InsertNextPoint(pp1);
pts->InsertNextPoint(pp2);
// Create a line between the two points
vtkSmartPointer<vtkLine> line =
vtkSmartPointer<vtkLine>::New();
for(unsigned int h = 0; h < 2; h++)
{
line->GetPointIds()->SetId(h,h);
}
// Create a cell array to store the line in
vtkSmartPointer<vtkCellArray> lines =
vtkSmartPointer<vtkCellArray>::New();
lines->InsertNextCell(line);
// Create a polydata to store everything in
vtkSmartPointer<vtkPolyData> pdata =
vtkSmartPointer<vtkPolyData>::New();
// Add the points to the dataset
pdata->SetPoints(pts);
// Add the lines to the dataset
pdata->SetLines(lines);
#if VTK_MAJOR_VERSION <= 5
mapper->SetInput(pdata);
#else
mapper->SetInputData(pdata);
#endif
actor->SetMapper(mapper);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindowInteractorLine->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
ui.qvtkWidget_Visualisation->SetRenderWindow(renderWindow);
ui.qvtkWidget_Visualisation->update();
renderWindowInteractorLine->Initialize();
renderWindowInteractorLine->Start();
break;
}
}
方法numbre 2:我将行存储在一个向量中,然后我绘制每一行
for ( std::vector<Coord_point*>::iterator i = tab_recupere_P1.begin();
i != tab_recupere_P1.end();
++i)
{
new_p = *i;
pp1[0] = new_p->p0[0];
pp1[1] = new_p->p0[1];
pp1[2] = new_p->p0[2];
std::cout << " x : "<< pp1[0] << " y : " << pp1[1] << " z : " << pp1[2] <<std::endl;
for ( std::vector<Coord_point*>::iterator j = tab_recupere_P2.begin();
j != tab_recupere_P2.end();
j++)
{
new_p2 = *j;
pp2[0] = new_p2->p0[0];
pp2[1] = new_p2->p0[1];
pp2[2] = new_p2->p0[2];
std::cout << " x2 : "<< pp2[0] << " y2 : " << pp2[1] << " z2 : " << pp2[2] <<std::endl;
vtkSmartPointer<vtkLineSource> lineSource =
vtkSmartPointer<vtkLineSource>::New();
lineSource->SetPoint1(pp1);
lineSource->SetPoint2(pp2);
lineSource->Update();
tab_lineSource.push_back(lineSource);
break;
}
}
vtkSmartPointer<vtkLineSource> new_lineSource =
vtkSmartPointer<vtkLineSource>::New();
for ( std::vector<vtkLineSource*>::iterator l = tab_lineSource.begin();
l != tab_lineSource.end();
l++)
{
new_lineSource = *l;
mapper->SetInputConnection(new_lineSource->GetOutputPort());
actor->SetMapper(mapper);
actor->GetProperty()->SetLineWidth(1);
actor->GetProperty()->SetColor(1,0,0);
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer(renderer);
renderWindowInteractorLine->SetRenderWindow(renderWindow);
renderer->AddActor(actor);
ui.qvtkWidget_Visualisation->SetRenderWindow(renderWindow);
ui.qvtkWidget_Visualisation->updateGeometry();
renderWindowInteractorLine->Initialize();
renderWindowInteractorLine->Start();
}
无论方法如何,第一次迭代的行都将被绘制并且没有错误消息的问题。但我们需要的是从矢量中存储的点中绘制所有线条。
请帮助我!!请!!
答案 0 :(得分:0)
这两个例子都有很多问题。
在示例1中:
在示例2中:
这段代码有很多错误。你应该从一个更简单的例子开始。也尝试缩进你的代码,这种方式很难阅读(也适合你)。