很高兴在雪易的CSDN遇见你
【vtkWidgetRepresentation】第九期 vtk中的仿射变换
前言
本文分享VTK中的仿射变换,实际结果如下图所示,希望对各位小伙伴有所帮助!
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的点赞就是我的动力(^U^)ノ~YO
vtkAffineWidget
1. vtkAffineRepresentation
主要定义了仿射变换的几何表示。关键参数有:
1.1 GetTransform获取定义的线性变换矩阵。
1.2 SetTolerance,设置激活Handle的容差。
1.3 交互状态,包括:
Outside | 外部 |
Rotate | 旋转 |
Translate | 移动 |
TranslateX | 沿X轴移动 |
TranslateY | 沿Y轴移动 |
ScaleWEdge | 沿W边界缩放 |
ScaleEEdge | 沿E边界缩放 |
ScaleNEdge | 沿N边界缩放 |
ScaleSEdge | 沿S边界缩放 |
ShearWEdge | 沿W边界剪切 |
ShearEEdge | 沿E边界剪切 |
ShearNEdge | 沿N边界剪切 |
ShearSEdge | 沿S边界剪切 |
MoveOriginX | 中心点沿X轴移动 |
MoveOriginY | 中心点沿Y轴移动 |
MoveOrigin | 移动中心点 |
2. vtkAffineRepresentation2D
vtkAffineRepresentation2D包含三部分:一个矩形,一个圆形和一个十字;矩形用于缩放和剪切;圆形用于旋转;十字用于移动。这三部分在视平面上进行显示,并保持一个恒定的高度和宽度。其重要参数如下:
2.1 设置不同部位的宽度
SetBoxWidth,SetCircleWidth,SetAxesWidth
2.2 设置包围盒的原点
SetOrigin
2.3 获取选中和未选中的属性
Set/GetProperty,Set/GetSelectedProperty, Set/GetTextProperty
2.4 获取显示的文字
SetDisplayText/GetDisplayText
3. vtkAffineWidget及其应用
#include <vtkActor.h>
#include <vtkAffineRepresentation2D.h>
#include <vtkAffineWidget.h>
#include <vtkAppendPolyData.h>
#include <vtkCommand.h>
#include <vtkInteractorStyleSwitch.h>
#include <vtkPlaneSource.h>
#include <vtkPolyData.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTransform.h>class vtkAffineCallback : public vtkCommand
{
public:static vtkAffineCallback *New(){ return new vtkAffineCallback; }virtual void Execute(vtkObject *caller, unsigned long, void*);vtkAffineCallback():Actor(0),AffineRep(0){this->Transform = vtkTransform::New();}~vtkAffineCallback(){this->Transform->Delete();}vtkActor *Actor;vtkAffineRepresentation2D *AffineRep;vtkTransform *Transform;
};void vtkAffineCallback::Execute(vtkObject*, unsigned long vtkNotUsed(event), void*)
{this->AffineRep->GetTransform(this->Transform);this->Actor->SetUserTransform(this->Transform);
}int main(int, char *[])
{// Create two spheres: a larger one and a smaller one on top of the larger one// to show a reference point while rotatingvtkSmartPointer<vtkSphereSource> sphereSource =vtkSmartPointer<vtkSphereSource>::New();sphereSource->Update();vtkSmartPointer<vtkSphereSource> sphereSource2 =vtkSmartPointer<vtkSphereSource>::New();sphereSource2->SetRadius(0.075);sphereSource2->SetCenter(0,0.5,0);sphereSource2->Update();// Append the two spheres into one vtkPolyDatavtkSmartPointer<vtkAppendPolyData> append =vtkSmartPointer<vtkAppendPolyData>::New();append->AddInputConnection(sphereSource->GetOutputPort());append->AddInputConnection(sphereSource2->GetOutputPort());// Create a plane centered over the larger sphere with 4x4 sub sectionsvtkSmartPointer<vtkPlaneSource> planeSource =vtkSmartPointer<vtkPlaneSource>::New();planeSource->SetXResolution(4);planeSource->SetYResolution(4);planeSource->SetOrigin(-1,-1,0);planeSource->SetPoint1(1,-1,0);planeSource->SetPoint2(-1,1,0);// Create a mapper and actor for the plane: show it as a wireframevtkSmartPointer<vtkPolyDataMapper> planeMapper =vtkSmartPointer<vtkPolyDataMapper>::New();planeMapper->SetInputConnection(planeSource->GetOutputPort());vtkSmartPointer<vtkActor> planeActor =vtkSmartPointer<vtkActor>::New();planeActor->SetMapper(planeMapper);planeActor->GetProperty()->SetRepresentationToWireframe();planeActor->GetProperty()->SetColor(1,0,0);// Create a mapper and actor for the spheresvtkSmartPointer<vtkPolyDataMapper> mapper =vtkSmartPointer<vtkPolyDataMapper>::New();mapper->SetInputConnection(append->GetOutputPort());vtkSmartPointer<vtkActor> actor =vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);// Create a renderer and render windowvtkSmartPointer<vtkRenderer> renderer =vtkSmartPointer<vtkRenderer>::New();vtkSmartPointer<vtkRenderWindow> renderWindow =vtkSmartPointer<vtkRenderWindow>::New();renderWindow->AddRenderer(renderer);renderer->AddActor(actor);renderer->AddActor(planeActor);renderer->GradientBackgroundOn();renderer->SetBackground(1,1,1);renderer->SetBackground2(0,0,1);// Create an interactorvtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =vtkSmartPointer<vtkRenderWindowInteractor>::New();renderWindowInteractor->SetRenderWindow(renderWindow);dynamic_cast<vtkInteractorStyleSwitch*>(renderWindowInteractor->GetInteractorStyle())->SetCurrentStyleToTrackballCamera();// Create an affine widget to manipulate the actor// the widget currently only has a 2D representation and therefore applies transforms in the X-Y plane onlyvtkSmartPointer<vtkAffineWidget> affineWidget =vtkSmartPointer<vtkAffineWidget>::New();affineWidget->SetInteractor(renderWindowInteractor);affineWidget->CreateDefaultRepresentation();dynamic_cast<vtkAffineRepresentation2D*>(affineWidget->GetRepresentation())->PlaceWidget(actor->GetBounds());vtkSmartPointer<vtkAffineCallback> affineCallback =vtkSmartPointer<vtkAffineCallback>::New();affineCallback->Actor = actor;affineCallback->AffineRep = dynamic_cast<vtkAffineRepresentation2D*>(affineWidget->GetRepresentation());affineWidget->AddObserver(vtkCommand::InteractionEvent,affineCallback);affineWidget->AddObserver(vtkCommand::EndInteractionEvent,affineCallback);renderWindow->Render();renderWindowInteractor->Initialize();renderWindow->Render();affineWidget->On();// begin mouse interactionrenderWindowInteractor->Start();return EXIT_SUCCESS;
}
结论:
感谢各位小伙伴的点赞+关注,小易会继续努力分享,一起进步!
你的赞赏是我的最最最最大的动力(^U^)ノ~YO