【VTK】使用vtkActor2D添加polyline

网友投稿 657 2022-08-27

【VTK】使用vtkActor2D添加polyline

【VTK】使用vtkActor2D添加polyline

具体实现如下:

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std;int main(){ vtkSmartPointer cone = vtkSmartPointer::New(); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputConnection( cone->GetOutputPort() ); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper( mapper ); vtkSmartPointer renderer = vtkSmartPointer::New(); renderer->AddActor(actor); renderer->SetBackground( 0, 0, 0 ); vtkSmartPointer renderWindow = vtkSmartPointer::New(); renderWindow->AddRenderer( renderer ); vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New(); renderWindowInteractor->SetRenderWindow( renderWindow ); // ------------- start to add red line ---------------- vtkSmartPointer v1mmMapper = vtkSmartPointer::New(); vtkSmartPointer v1mmPolyData = vtkSmartPointer::New(); vtkSmartPointer v1mmLines = vtkSmartPointer::New(); vtkSmartPointer v1mmPoints = vtkSmartPointer::New(); v1mmPolyData->SetPoints( v1mmPoints ); v1mmPolyData->SetLines( v1mmLines ); v1mmMapper->SetInputData( v1mmPolyData ); vtkSmartPointer v1mmLinesActor = vtkSmartPointer::New(); v1mmLinesActor->SetMapper( v1mmMapper ); v1mmLinesActor->GetProperty()->SetColor( 1, 0, 0 ); v1mmLinesActor->GetProperty()->SetLineWidth( 1 ); vtkSmartPointer normCoords = vtkSmartPointer::New(); normCoords->SetCoordinateSystemToView(); v1mmMapper->SetTransformCoordinate( normCoords ); // just for test vtkCoordinate *cd = v1mmMapper->GetTransformCoordinate(); assert( nullptr != cd ); printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() ); double linePoint1[3] = { 0, -1, 0.0 }; double linePoint2[3] = { 0, 1, 0.0 }; vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1); vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2); vtkIdType lineIds[2] = { pointId1, pointId2 }; v1mmLines->InsertNextCell(2, lineIds); linePoint1[0] = -1; linePoint1[1] = 0; linePoint1[2] = 0; linePoint2[0] = 1; linePoint2[1] = 0; linePoint2[2] = 0; lineIds[0] = v1mmPoints->InsertNextPoint(linePoint1); lineIds[1] = v1mmPoints->InsertNextPoint(linePoint2); v1mmLines->InsertNextCell(2, lineIds); v1mmPolyData->Modified(); // ------------- adding red line finished---------------- renderer->ResetCamera(); renderer->AddActor2D( v1mmLinesActor ); renderWindow->Render(); renderWindowInteractor->Start(); return 0;}

我们也可以使用viewport坐标系来绘图。 改动的代码只有vtkCoordinate的坐标系统设置以及线段两端点的位置设置。 因为view的坐标范围是[0,1],而viewport的坐标对应着像素值,所以我们需要使用​​​renderWindow->GetSize();​​获取窗体的像素尺寸。

vtkSmartPointer normCoords = vtkSmartPointer::New(); normCoords->SetCoordinateSystemToViewport(); v1mmMapper->SetTransformCoordinate( normCoords ); // just for test vtkCoordinate *cd = v1mmMapper->GetTransformCoordinate(); assert( nullptr != cd ); printf( "GetCoordinateSystemAsString: %s\n", cd->GetCoordinateSystemAsString() ); renderWindow->Render(); //after render, window's size is not (0, 0) any more. int *size = renderWindow->GetSize(); printf( "size: %d, %d\n", size[0], size[1] ); // viewport origin point is at left-bottom corner. double linePoint1[3] = { size[0]/2.0, 0, 0.0 }; double linePoint2[3] = { size[0]/2.0, size[1] * 1.0, 0.0 }; vtkIdType pointId1 = v1mmPoints->InsertNextPoint(linePoint1); vtkIdType pointId2 = v1mmPoints->InsertNextPoint(linePoint2); vtkIdType lineIds[2] = { pointId1, pointId2 }; v1mmLines->InsertNextCell(2, lineIds); linePoint1[0] = 0; linePoint1[1] = size[1] / 2; linePoint1[2] = 0; linePoint2[0] = size[0]; linePoint2[1] = size[1] / 2; linePoint2[2] = 0; lineIds[0] = v1mmPoints->InsertNextPoint(linePoint1); lineIds[1] = v1mmPoints->InsertNextPoint(linePoint2); v1mmLines->InsertNextCell(2, lineIds); v1mmPolyData->Modified(); // ------------- adding red line finished----------------

显示结果和view系统一样。 图略。在放大窗体后,view坐标里面的红线仍然布满整个窗体,但是viewport里的红线因为最初设置的寸尺比改变后的窗体寸尺小,所以就会有这样的放大效果:

如果不为render设置viewport,viewport和display的效果是一样的

//normCoords->SetCoordinateSystemToViewport(); normCoords->SetCoordinateSystemToDisplay();

但是,我们如果设置了viewport,那么差异就出现了: 在添加red line的代码块之前写上如下代码

renderer->SetViewport( 0.0, 0.0, 0.5, 1.0 );

效果:

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DBUS入门笔记
下一篇:树状数组
相关文章

 发表评论

暂时没有评论,来抢沙发吧~