VTK——模型的旋转与平移

在计算机图形学中,三维坐标点用齐次坐标表示。利用其次坐标,可以将空间变换用4x4的矩阵来表示。这些变换都可以用矩阵的运算完成。
当从外界读入STL等三维模型时,其会按照它内部的坐标位置进行显示。因此它的位置和大小是确定的。但是在实际应用中,有可能需要人为地对这个模型在空间中进行旋转、平移或缩放等操作。VTK中有许多和旋转、平移相关的函数

VTK相关的类有:

vtkTransform, vtkTransformFilter, vtkMatrix4x4等

相关的方法有:

• RotateX(angle)、RotateY(angle)、RotateZ(angle)

• RotateWXYZ(angle,x,y,z)

• Scale(x,y,z)

• Translate(x,y,z)

• SetMatrix(m)、GetMatrix(m)

• PostMultiply()、PreMultiply()

• SetPosition(x,y,z)、GetPosition(x,y,z)

• SetOrientation(x,y,z)、 GetOrientation(x,y,z)

• SetScale(sx,sy,sz)

• SetOrigin(x,y,z)、GetOrigin

RotateX、RotateY、RotateZ(绕自身坐标轴旋转)

Rotate the Prop3D in degrees about the X/Y/Z axis using the right hand rule. The axis is the Prop3D’s X/Y/Z axis, which can change as other rotations are performed. 即前一次的旋转会影响到后一次。

假设物体坐标系开始与世界坐标系重合,先后调用RotateZ(90)、RotateX(90)的结果如下图所示:

使用GetOrientation可以获取当前物体的姿态信息,结果为(90, 0, 90)。GetOrientation:Returns the orientation of the Prop3D as s vector of X,Y and Z rotation. The ordering in which these rotations must be done to generate the same matrix is RotateZ, RotateX, and finally RotateY. 即按照Z-X-Y的顺序将世界坐标系绕自身旋转即可得到当前姿态。

RotateWXYZ(绕世界坐标系旋转)

Rotate the Prop3D in degrees about an arbitrary axis specified by the last three arguments. The axis is specified in world coordinates. 该函数的后三个参数指定旋转方向(可以是任意方向,不一定非得是坐标轴方向)。如按世界坐标系的X轴旋转可写为RotateWXYZ(deg, 1, 0, 0),按世界坐标系的Z轴旋转可写为RotateWXYZ(deg, 0, 0, 1)。同样是先后调用RotateWXYZ(90,0,0,1)、RotateWXYZ(90,1,0,0)得到的结果如下:
在这里插入图片描述
  此时调用GetOrientation返回值是:(0, -90, 90),即将世界坐标系先绕自身Z轴旋转90°,再绕X轴旋转0°,最后绕Y轴旋转-90°即可到达当前姿态。
SetOrientation(x, y, z) —— 通过先绕Z轴,然后绕X轴,最后绕Y轴旋转,从而来确定Prop的方向。
AddOrientation(a1, a2,a3) —— 在当前Prop方向增加a1, a2, a3增量。
SetOrigin(设置旋转中心点)
  Set the origin of the Prop3D. This is the point about which all rotations take place. 调用RotateX或RotateWXYZ等旋转函数时会默认旋转中心在原点,即会绕着(0, 0, 0)沿着某一方向旋转。通过调用SetOrigin函数可以改变旋转点的位置。例如一个长宽高都是100的立方体,绕自身Z轴旋转45°,下图左边是默认旋转中心在原点,右边是调用SetOrigin设置旋转中心在(50,0,0)的结果
在这里插入图片描述
在这里插入图片描述
SetPosition、AddPosition(设置物体在世界坐标系中的位置)
  SetPosition(x, y, z)—— 指定vtkProp3D对象在世界坐标系中的位置。AddPosition(deltaX, deltaY, deltaZ) —— 用指定的X、Y、Z三个方向的增量来平移Prop。如下图中立方体开始在原点处,调用SetPosition(50,0,0)后其位置变为(50,0,0)。可以通过GetPosition函数查看物体在世界坐标系中的位置。
在这里插入图片描述
SetUserTransform、GetUserTransform
  The most important aspect to applying transformation matrices is to understand the order in which the transformations are applied. Most of the methods for manipulating this transformation, e.g. Translate, Rotate, and Concatenate, can operate in either PreMultiply (the default) or PostMultiply mode. In PreMultiply mode, the translation, concatenation, etc. will occur before any transformations which are represented by the current matrix. In PostMultiply mode, the additional transformation will occur after any transformations represented by the current matrix.

进行变换时的顺序非常重要,对于变换矩阵VTK里是用以下的顺序来应用这些变换的:
  
TT=T(px+ox,py+oy,pz+oz)⋅RZ⋅RX⋅RY⋅S(sx,sy,sz)⋅T(−ox,−oy,−oz)TT=T(px+ox,py+oy,pz+oz)⋅RZ⋅RX⋅RY⋅S(sx,sy,sz)⋅T(−ox,−oy,−oz)

1. 移动Prop到原点;

2. 缩放;

3. 绕Y轴旋转;

4. 绕X轴旋转;

5. 绕Z轴旋转;

6. 从原点中移动回原来的位置;

7. 平移。

因为默认是进行左乘所以进行变换时先调用的最后才变换(即逆序),下面的代码实现了公式中的变换:

vtkTransform *myTrans = vtkTransform::New ();
myTrans->Translate (position[0],position[1],position[2]);
myTrans->Translate (origin[0],origin[1],origin[2]);
myTrans->RotateZ (orientation[2]);
myTrans->RotateX (orientation[0]);
myTrans->RotateZ (orientation[1];
myTrans->Scale (scale[0],scale[1],scale[2]);
myTrans->Translate (-origin[0],-origin[1],-origin[2]);

actor->SetUserTransform(myTrans);

vtkTransform::PreMultiply()用于设置左乘. Sets the internal state of the transform to PreMultiply. All subsequent operations will occur before those already represented in the current transformation. In homogeneous matrix notation, M = M*A where M is the current transformation matrix and A is the applied matrix. The default is PreMultiply.

vtkTransform::PostMultiply()用于设置右乘,Sets the internal state of the transform to PostMultiply. M = A*M where M is the current transformation matrix and A is the applied matrix.

如下所示,左边平面先旋转45°再沿X轴正方向移动2个单位;右边平面先沿X轴正方向移动2个单位,然后旋转45°(旋转中心在原点)。可以看出函数调用顺序颠倒一下产生了截然不同的两种结果(We always specify transformations in the reverse order of their application)
在这里插入图片描述
在这里插入图片描述
SetUserMatrix、GetUserMatrix
  The UserMatrix can be used in place of UserTransform. Transformation matrices can be combined by matrix multiplication to achieve combinations of translation, rotation, and scaling. It is possible for a single transformation matrix to represent all types of transformation simultaneously.

下面代码将物体绕Z轴旋转45°,并移动到(50, 50, 50)处
trans = [0.707107, -0.707107, 0, 50,
0.707107, 0.707107, 0, 50,
0, 0, 1, 50,
0, 0, 0, 1]

mat = vtk.vtkMatrix4x4()
mat.DeepCopy(trans)

actor.SetUserMatrix(mat)

在这里插入图片描述
GetMatrix
UserMatrix is one you can assign yourself to an actor or a top-level composite assembly of actors. Matrix is computed automatically from any position, rotation or scale the actor may have, in addition to the UserMatrix. Calling GetMatrix should always give you the exact matrix applied to an actor to get it to from whatever its own “model space” is to VTK world space. UserMatrix is nullptr until you set it, or until the actor is added as part of an assembly, where the parent assembly may set user matrices directly for its constituent parts. You can’t set the matrix directly, but you can set the UserMatrix directly.

GetMatrix与GetUserMatrix函数有很大区别:GetUserMatrix用来获取用户设置的变换矩阵,如果用户没有设置则会返回空,而GetMatrix则始终都会返回场景中物体相对世界坐标系的变换矩阵。

在这里插入图片描述
在这里插入图片描述
 先将平面绕Z轴旋转45°,再移动到(2,0,0)位置,三个函数输出如下图所示。由于没有显式调用SetUserMatrix或SetUserTransform设置UserMatrix,因此GetUserMatrix和GetUserTransform函数返回均为None。
在这里插入图片描述

然后将平面沿X轴负方向平移2个单位,再绕Z轴旋转-45°,将平面变换回初始位置(如果想先旋转再平移将其变换回初始位置,则要重新设置旋转中心,否则旋转时会绕原点旋转)。从下图可以看出GetMatrix函数返回了单位矩阵,即现在平面的位置和姿态与世界坐标系一致。GetUerMatrix和GetUserTransform返回的变换矩阵一样:T=Rz(-45°)·T(-2, 0, 0),为平移和旋转所构成的变换矩阵。
在这里插入图片描述
在这里插入图片描述

平面由初始位置变换到新位置再变换回去,两次操作互逆,因此变换矩阵互为逆矩阵。

注意:已知B→A的齐次变换矩阵T,则A→B的齐次变换矩阵T-1如下:

在这里插入图片描述
在这里插入图片描述

  • 9
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: vtk是一种图形库,用于可视化数据。在vtk中,“点”、“线”和“面”是常见的几何元素。下面是关于这些元素的一些示例: 1. 点(Point):点是空间中具有坐标位置的最基本的几何元素。在vtk中,我们可以创建一个或多个点,并指定它们的坐标。例如,我们可以创建一个点的集合,表示星空中的一些恒星的位置。每个点都有自己的坐标,可以在三维空间中进行可视化。 2. 线(Line):线是由两个或更多个连接起来的点组成的几何形状。在vtk中,我们可以使用点的坐标来创建一条直线。例如,我们可以创建一个由两个点组成的线,表示一条直线段的路径。这条直线可以用来表示一条河流的流程,或者是一辆车在道路上行驶的路径。 3. 面(Surface):面是由三个或更多个连接起来的点和线组成的几何形状。在vtk中,我们可以使用点的坐标和线的连接关系来创建一个面。例如,我们可以创建一个三角形的面,通过指定三个点的坐标来定义。这个三角形可以用来表示一个地面的形状,或者是一个房屋的屋顶的形状。 总之,vtk提供了一种便捷的方式来创建和可视化各种几何元素,包括点、线和面。通过使用vtk,我们可以将这些几何元素在三维空间中进行可视化,并用于表示和展示各种数据的形状和结构。 ### 回答2: VTK(Visualization Toolkit)是一个强大的图形可视化库,它提供了丰富的功能和工具,可以用于创建各种类型的图形对象和可视化效果。在VTK中,可以使用点、线和面作为基本的图形元素。 在VTK中,点是最简单的图形元素之一。它可以用于表示空间中的一个坐标点,具有位置属性。例如,我们可以使用VTK创建一个点的可视化效果,通过设置其位置和颜色来定义点的属性,从而实现对点的可视化表达。 线是由多个点连接而成的线段。在VTK中,可以使用一系列的点来创建一条线,并通过设置线的属性,如线宽、颜色、透明度等来实现线的可视化效果。例如,我们可以通过VTK创建一个由多个点组成的路径,用来表示动态的物体运动轨迹。 面是由多条连续的线组成的封闭区域。在VTK中,可以使用一系列的坐标点来定义一个封闭的多边形,并通过设置面的属性,如颜色、透明度、纹理等来实现面的可视化效果。例如,我们可以使用VTK创建一个立方体的可视化效果,通过定义六个面和对应的坐标点来表示立方体的各个面。 总结起来,VTK中的点、线和面都是用于表示图形对象的基本元素,可以通过设置它们的属性来实现各种形式的可视化效果。无论是单独使用还是组合使用,这些元素都为图形的创建和可视化提供了灵活和多样的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值