VTK笔记

您所在的位置:网站首页 python中坐标原点在哪里 VTK笔记

VTK笔记

2023-12-10 16:12| 来源: 网络整理| 查看: 265

对于三维空间中的模型,它是投射到二维平面显示,很多时候,我们需要知道它在坐标系的位置或者相对于另一个模型的位置,又或者是它的法向量等,这时候借助显示坐标系就很有必要,下面的方法可能会帮助到你。

方法一

借助vtkLineSource来画三条线,分别是X, Y, Z轴,再给三条轴设置不同的color加以区分。

vtkSmartPointer lineSourceX = vtkSmartPointer::New(); lineSourceX->SetPoint1(0, 0, 0); lineSourceX->SetPoint2(LINE_LEN, 0, 0); lineSourceX->Update(); vtkSmartPointer lineSourceY = vtkSmartPointer::New(); lineSourceY->SetPoint1(0, 0, 0); lineSourceY->SetPoint2(0, LINE_LEN, 0); lineSourceY->Update(); vtkSmartPointer lineSourceZ = vtkSmartPointer::New(); lineSourceZ->SetPoint1(0, 0, 0); lineSourceZ->SetPoint2(0, 0, LINE_LEN); lineSourceZ->Update(); //-------------------------------------------- actor1[0]->GetProperty()->SetColor(1, 0, 0); actor1[1]->GetProperty()->SetColor(0, 1, 0); actor1[2]->GetProperty()->SetColor(0, 0, 1); 方法二

直接使用vtkAxesActor来显示坐标系,这也是比较推荐的方法。

vtkSmartPointer actor2 = vtkSmartPointer::New(); actor2->SetPosition(0, 0, 0); actor2->SetTotalLength(AXIS_LEN, AXIS_LEN, AXIS_LEN); actor2->SetShaftType(0); actor2->SetAxisLabels(0); actor2->SetCylinderRadius(0.02);

效果如下图所示,Red, Green, Blue分别对应X,Y,Z三条轴:

示例代码

#include #include #include #include #include #include #include #include #include #include #include int main(int, char *[]) { vtkSmartPointer sphereSource = vtkSmartPointer::New(); sphereSource->SetRadius(.5); sphereSource->SetCenter(0, 0, 0); vtkSmartPointer mapper = vtkSmartPointer::New(); mapper->SetInputConnection(sphereSource->GetOutputPort()); vtkSmartPointer actor = vtkSmartPointer::New(); actor->SetMapper(mapper); #define LINE_LEN 2. // #Method1 vtkSmartPointer lineSourceX = vtkSmartPointer::New(); lineSourceX->SetPoint1(0, 0, 0); lineSourceX->SetPoint2(LINE_LEN, 0, 0); lineSourceX->Update(); vtkSmartPointer lineSourceY = vtkSmartPointer::New(); lineSourceY->SetPoint1(0, 0, 0); lineSourceY->SetPoint2(0, LINE_LEN, 0); lineSourceY->Update(); vtkSmartPointer lineSourceZ = vtkSmartPointer::New(); lineSourceZ->SetPoint1(0, 0, 0); lineSourceZ->SetPoint2(0, 0, LINE_LEN); lineSourceZ->Update(); vtkSmartPointer MapperX = vtkSmartPointer::New(); MapperX->SetInputConnection(lineSourceX->GetOutputPort()); vtkSmartPointer MapperY = vtkSmartPointer::New(); MapperY->SetInputConnection(lineSourceY->GetOutputPort()); vtkSmartPointer MapperZ = vtkSmartPointer::New(); MapperZ->SetInputConnection(lineSourceZ->GetOutputPort()); vtkSmartPointer actor1[3]; for (int i = 0; i < 3; i++) { actor1[i] = vtkSmartPointer::New(); } actor1[0]->SetMapper(MapperX); actor1[1]->SetMapper(MapperY); actor1[2]->SetMapper(MapperZ); actor1[0]->GetProperty()->SetColor(1, 0, 0); actor1[1]->GetProperty()->SetColor(0, 1, 0); actor1[2]->GetProperty()->SetColor(0, 0, 1); actor1[0]->SetPosition(0, 0, 0); actor1[1]->SetPosition(0, 0, 0); actor1[2]->SetPosition(0, 0, 0); // #Method2 vtkSmartPointer actor2 = vtkSmartPointer::New(); actor2->SetPosition(0, 0, 0); actor2->SetTotalLength(LINE_LEN, LINE_LEN, LINE_LEN); actor2->SetShaftType(0); actor2->SetAxisLabels(0); actor2->SetCylinderRadius(0.02); vtkSmartPointer renderer1 = vtkSmartPointer::New(); vtkSmartPointer renderer2 = vtkSmartPointer::New(); vtkSmartPointer renderWindow = vtkSmartPointer::New(); renderWindow->SetSize(800, 400); renderWindow->AddRenderer(renderer1); renderWindow->AddRenderer(renderer2); vtkSmartPointer renderWindowInteractor = vtkSmartPointer::New(); renderWindowInteractor->SetRenderWindow(renderWindow); double leftViewport[] = { 0.0, 0.0, 0.5, 1.0 }; double rightViewport[] = { 0.5, 0.0, 1.0, 1.0 }; for (int i = 0; i < 3; i++) { renderer1->AddActor(actor1[i]); } renderer1->AddActor(actor); renderer2->AddActor(actor2); renderer2->AddActor(actor); renderer1->SetBackground(.3, .3, .5); renderer2->SetBackground(.2, .4, .5); renderer1->SetViewport(leftViewport); renderer2->SetViewport(rightViewport); renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }

Reference

shows how to display the coordinate axes in the render window

shows how to position an AxesActor in 3D



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3