# 【VTK】平面的平移和旋转

  double Normal[3];
double Origin[3];


/**
* Set/get point through which plane passes. Plane is defined by point
* and normal.
*/
vtkSetVector3Macro(Origin,double);
vtkGetVectorMacro(Origin,double,3);

/**
* Set/get plane normal. Plane is defined by point and normal.
*/
vtkSetVector3Macro(Normal,double);
vtkGetVectorMacro(Normal,double,3);


### 方案1: 每次计算出新的origin与normal，再更新平面

#### 更新平面

vtkTransform提供了各类线性变换的方法: 旋转 RotateWXYZ，平移 Translate，缩放 Scale。这些借口一旦被调用，都会影响vtkTransform里面的变换矩阵vtkMatrix4x4，具体是PreMatrix，还是PostMatrix，取决于开发者的设置。比如trans->PostMultiply();就是将vtkTransform对象设置成后乘模式。有了这些作为基础，就可以计算新的vtkTransform对象，将其赋予PlaneActor，达到更新平面的目的。

### 方案2: 先进行平面的空间变换，再更新origin、normal

double q[4] = { radianDelta, 0, 0, 1 }; //rotate delta radian around Z axis
double r[3] = {0};	// r array store new origin normal

rotateVectorByWXYZ( originNormal.point, q, r );


### 更新平面

vtkActor提供了和vtkTransform类似的线性变换方法：

//旋转
RotateWXYZ
RotateX
RotateY
RotateZ

//缩放
Scale

//平移
AddPosition


main.cpp

#include <stdio.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkActor.h>
#include <vtkPlaneSource.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkLight.h>
#include <vtkCamera.h>
#include <vtkActor2D.h>

using namespace std;

int main()
{
setbuf( stdout, NULL );
vtkSmartPointer<vtkPlaneSource> plane =
vtkSmartPointer<vtkPlaneSource>::New();

vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection( plane->GetOutputPort() );

vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper( mapper );
double *origin = actor->GetOrigin();
// actor->AddPosition( 1, 1, 1 );

printf( "origin: %lf, %lf, %lf\n", origin[0], origin[1], origin[2] );
// origin: 0.000000, 0.000000, 0.000000

vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(actor);
renderer->SetBackground( 0, 0, 0 );

vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
renderWindow->AddRenderer( renderer );

vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
renderWindowInteractor->SetRenderWindow( renderWindow );

renderer->ResetCamera();
renderWindow->Render();
renderWindowInteractor->Start();
return 0;
}


#### 获取线性变换后的origin和normal

    actor->AddPosition( 1, 1, 1 );

printf( "origin: %lf, %lf, %lf\n", origin[0], origin[1], origin[2] );
// origin: 0.000000, 0.000000, 0.000000

vtkSmartPointer<vtkTransform> transform =
vtkSmartPointer<vtkTransform>::New();
transform->SetMatrix( actor->GetMatrix() );
double *newPos = transform->TransformDoublePoint( origin[0], origin[1], origin[2] );

printf( "newPos: %lf, %lf, %lf\n", newPos[0], newPos[1], newPos[2] );
// newPos: 1.000000, 1.000000, 1.000000


    double oldNormal[3] = { 0, -1, 0 };
printf( "oldNormal: %lf, %lf, %lf\n", oldNormal[0], oldNormal[1], oldNormal[2] );
// oldNormal: 0.000000, -1.000000, 0.000000

actor->RotateZ( 90 );

vtkSmartPointer<vtkTransform> transform =
vtkSmartPointer<vtkTransform>::New();
transform->SetMatrix( actor->GetMatrix() );
double *newNormal = transform->TransformDoubleVector( oldNormal );
printf( "newNormal: %lf, %lf, %lf\n", newNormal[0], newNormal[1], newNormal[2] );
// newNormal: 1.000000, -0.000000, 0.000000


【VTK】vtk actor空间变换后的点映射与向量映射

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120