In order to facilitate the observation of the direction, position and rotation of the image, I want to add a coordinate axis display in the window, and this coordinate axis changes with the rotation of the window. I found many examples on the Internet, but in the end the coordinate axis was not displayed.
Record it here.
1. A brief introduction to coordinate axis related classes
1.vtkAxesActor
SetPosition(double,double, double);Set the origin position of the coordinate system
void SetTotalLength(double x, double y, double z); Set the total length of the coordinate axis
void SetShaftType(int type); Set the axis type of the coordinate axis (int type)
0: cylindrical axis
1: linear axis bodyIf the type is 0: SetCylinderRadius(double): Set the cylindrical axis radius
SetTipType(int type); Set the axis top type of the coordinate axis
0: Cone
1: Sphere
If the type is 0: SetConeResolution(double); Set the number of cones
SetConeRadius(double); );Set the number of spheres
SetAxisLabels(bool); Whether to display labels
SetXAxisLabelText(const char *);
SetYAxisLabelText(const char *);
SetZAxisLabelText(const char *);
Set axis labels
GetXAxisShaftProperty
GetYAxisShaftProperty
GetZAxisShaftProperty
Get the axis properties and return vtkProperty to set the properties directly
GetXAxisTipProperty
GetYAxisTipProperty
GetZAxisTipProperty
Get the top attribute of the axis, return vtkProperty to set the attribute directly
2.vtkOrientationMarkerWidget
SetOrientationMarker(vtkProp *prop);Set the coordinate axis
SetInteractor(vtkRenderWindowInteractor* iren);Set the interactor
SetViewport(double minX, double minY, double maxX, double maxY); used to specify the position and size of the direction mark component in the rendering window.
minX: The horizontal position of the lower left corner of the direction mark component relative to the rendering window. The value range is [0.0, 1.0], where 0.0 means the left side and 1.0 means the right side.
minY: The vertical position of the lower left corner of the direction mark component relative to the rendering window. The value range is [0.0, 1.0], where 0.0 represents the bottom and 1.0 represents the top.
maxX: The horizontal position of the upper right corner of the direction mark component relative to the rendering window. The value range is [0.0, 1.0]. Generally, maxX > minX should be ensured.
maxY: The vertical position of the upper right corner of the direction mark component relative to the rendering window. The value range is [0.0, 1.0]. Generally, maxY > minY should be ensured.
SetEnabled(bool); whether to display
SetInteractive(bool); Whether it can be interactive 1 (yes): InteractiveOn 0 (no): InteractiveOff
Default is interactive
SetOutlineColor(double,double,double);Set the outer border color
2. Code examples
For specific reference to the library, modification of main.cpp, and addition of the QVTKOpenGLNativeWidget control, please refer to the previous detailed article. Only the main code is shown below.
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include "vtkAutoInit.h" // vtk初始化的方式
VTK_MODULE_INIT(vtkRenderingOpenGL2); // 渲染
VTK_MODULE_INIT(vtkInteractionStyle); // 相互做用方式
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2); //
VTK_MODULE_INIT(vtkRenderingFreeType);
#include <QWidget>
#include "vtkActor.h"
#include "vtkPolyDataMapper.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include <vtkNew.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkAxesActor.h>
#include <vtkOrientationMarkerWidget.h>
#include "vtkConeSource.h"
#include "vtkCamera.h"
#include <vtkProperty.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
vtkSmartPointer<vtkOrientationMarkerWidget> MarkerWidget;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();//在构造函数中进行初始化
//显示
vtkNew<vtkGenericOpenGLRenderWindow> renwindow;
renwindow->AddRenderer(renderer);
ui->vtk_widget->SetRenderWindow(renwindow.Get());
// 显示坐标系的vtk组件
vtkSmartPointer<vtkAxesActor> axes_actor = vtkSmartPointer<vtkAxesActor>::New();
axes_actor->SetPosition(0, 0, 0);
axes_actor->SetTotalLength(2, 2, 2);
axes_actor->SetShaftType(0);
axes_actor->SetCylinderRadius(0.03);
axes_actor->SetAxisLabels(1);
axes_actor->SetTipType(0);
axes_actor->SetXAxisLabelText("xxx");
axes_actor->GetXAxisShaftProperty()->SetColor(1,1,1);
axes_actor->GetYAxisTipProperty()->SetColor(1,1,1);
// 控制坐标系,使之随视角共同变化
MarkerWidget = vtkSmartPointer<vtkOrientationMarkerWidget>::New();
MarkerWidget->SetOrientationMarker(axes_actor);
MarkerWidget->SetInteractor(ui->vtk_widget->GetInteractor());
MarkerWidget->SetViewport(0.0, 0.0, 0.2, 0.2);
MarkerWidget->SetEnabled(1);
MarkerWidget->SetOutlineColor(1,0,0);
//锥
vtkSmartPointer<vtkConeSource> Cone = vtkSmartPointer<vtkConeSource>::New();
vtkSmartPointer<vtkPolyDataMapper> mapper_Cone = vtkSmartPointer<vtkPolyDataMapper>::New();
mapper_Cone->SetInputConnection(Cone->GetOutputPort());
vtkSmartPointer<vtkActor> actor_Cone = vtkSmartPointer<vtkActor>::New();
actor_Cone->SetMapper(mapper_Cone);
renderer->AddActor(actor_Cone);
renderer->ResetCamera();
}
Widget::~Widget()
{
delete ui;
}
Show results:
Note: I initially tried not to display the coordinate axes. I spent a whole day looking for this problem. Then I put the definition of vtkSmartPointer<vtkOrientationMarkerWidget> MarkerWidget; into the header file and it was displayed. I saw a saying that MarkerWidget was not parsed enough. . If anyone knows, please comment and let us know!