对于三维空间中的模型,它是投射到二维平面显示,很多时候,我们需要知道它在坐标系的位置或者相对于另一个模型的位置,又或者是它的法向量等,这时候借助显示坐标系就很有必要,下面的方法可能会帮助到你。
方法一
借助vtkLineSource来画三条线,分别是X, Y, Z轴,再给三条轴设置不同的color加以区分。
vtkSmartPointer<vtkLineSource> lineSourceX =
vtkSmartPointer<vtkLineSource>::New();
lineSourceX->SetPoint1(0, 0, 0);
lineSourceX->SetPoint2(LINE_LEN, 0, 0);
lineSourceX->Update();
vtkSmartPointer<vtkLineSource> lineSourceY =
vtkSmartPointer<vtkLineSource>::New();
lineSourceY->SetPoint1(0, 0, 0);
lineSourceY->SetPoint2(0, LINE_LEN, 0);
lineSourceY->Update();
vtkSmartPointer<vtkLineSource> lineSourceZ =
vtkSmartPointer<vtkLineSource>::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<vtkAxesActor> actor2 =
vtkSmartPointer<vtkAxesActor>::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 <vtkLineSource.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkAxesActor.h>
#include <vtkConeSource.h>
int main(int, char *[])
{
vtkSmartPointer<vtkConeSource> sphereSource =
vtkSmartPointer<vtkConeSource>::New();
sphereSource->SetRadius(.5);
sphereSource->SetCenter(0, 0, 0);
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(sphereSource->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
#define LINE_LEN 2.
// #Method1
vtkSmartPointer<vtkLineSource> lineSourceX =
vtkSmartPointer<vtkLineSource>::New();
lineSourceX->SetPoint1(0, 0, 0);
lineSourceX->SetPoint2(LINE_LEN, 0, 0);
lineSourceX->Update();
vtkSmartPointer<vtkLineSource> lineSourceY =
vtkSmartPointer<vtkLineSource>::New();
lineSourceY->SetPoint1(0, 0, 0);
lineSourceY->SetPoint2(0, LINE_LEN, 0);
lineSourceY->Update();
vtkSmartPointer<vtkLineSource> lineSourceZ =
vtkSmartPointer<vtkLineSource>::New();
lineSourceZ->SetPoint1(0, 0, 0);
lineSourceZ->SetPoint2(0, 0, LINE_LEN);
lineSourceZ->Update();
vtkSmartPointer<vtkPolyDataMapper> MapperX =
vtkSmartPointer<vtkPolyDataMapper>::New();
MapperX->SetInputConnection(lineSourceX->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> MapperY =
vtkSmartPointer<vtkPolyDataMapper>::New();
MapperY->SetInputConnection(lineSourceY->GetOutputPort());
vtkSmartPointer<vtkPolyDataMapper> MapperZ =
vtkSmartPointer<vtkPolyDataMapper>::New();
MapperZ->SetInputConnection(lineSourceZ->GetOutputPort());
vtkSmartPointer<vtkActor> actor1[3];
for (int i = 0; i < 3; i++)
{
actor1[i] = vtkSmartPointer<vtkActor>::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<vtkAxesActor> actor2 =
vtkSmartPointer<vtkAxesActor>::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<vtkRenderer> renderer1 =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderer> renderer2 =
vtkSmartPointer<vtkRenderer>::New();
vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->SetSize(800, 400);
renderWindow->AddRenderer(renderer1);
renderWindow->AddRenderer(renderer2);
vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::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