VTK變換類vtkTransform完全使用指南

1. 快速入門:創建和使用變換

1.1 基本使用流程

#include <vtkTransform.h>
#include <vtkSmartPointer.h>

// 創建變換對象
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();

// 應用變換操作
transform->Translate(10.0, 5.0, 0.0);  // 平移
transform->RotateZ(45.0);               // 繞Z軸旋轉45度
transform->Scale(2.0, 1.0, 1.0);        // X方向縮放2倍

// 應用到數據
vtkSmartPointer<vtkTransformFilter> filter = vtkSmartPointer<vtkTransformFilter>::New();
filter->SetTransform(transform);
filter->SetInputData(yourData);
filter->Update();

2. 變換矩陣數學原理

2.1 齊次座標與4×4變換矩陣

VTK使用4×4齊次座標矩陣來表示三維幾何變換。齊次座標在三維座標(x,y,z)基礎上增加一個w分量,形成(x,y,z,w)。

點變換公式:

P' = M × P

其中:

  • P = [x, y, z, 1]ᵀ (原始點)
  • P’ = [x’, y’, z’, 1]ᵀ (變換後的點)
  • M 是4×4變換矩陣

2.2 基本變換矩陣

平移變換矩陣:

[ 1  0  0  Tx ]
[ 0  1  0  Ty ]
[ 0  0  1  Tz ]
[ 0  0  0  1  ]

數學公式:x’ = x + Tx, y’ = y + Ty, z’ = z + Tz

縮放變換矩陣:

[ Sx  0   0   0 ]
[ 0   Sy  0   0 ]
[ 0   0   Sz  0 ]
[ 0   0   0   1 ]

數學公式:x’ = Sx × x, y’ = Sy × y, z’ = Sz × z

繞Z軸旋轉矩陣:

[ cosθ  -sinθ  0  0 ]
[ sinθ   cosθ  0  0 ]
[ 0      0     1  0 ]
[ 0      0     0  1 ]

數學公式:x’ = x×cosθ - y×sinθ, y’ = x×sinθ + y×cosθ, z’ = z

2.3 複合變換矩陣乘法

變換組合通過矩陣乘法實現。對於變換A和變換B:

  • 前乘(PreMultiply):M_result = B × A
  • 後乘(PostMultiply):M_result = A × B

矩陣乘法不滿足交換律,因此變換順序至關重要。

3. 核心變換操作詳解

3.1 平移變換

// 方法簽名:void Translate(double x, double y, double z)
// 參數説明:
//   x: X軸方向的平移量
//   y: Y軸方向的平移量  
//   z: Z軸方向的平移量

transform->Translate(10.0, 0.0, 0.0);    // 沿X軸平移10個單位
transform->Translate(0.0, 5.0, -3.0);    // 沿Y軸平移5,Z軸平移-3

// 使用數組參數的版本
double move[3] = {2.0, 3.0, 4.0};
transform->Translate(move);               // 等同於Translate(2,3,4)

3.2 旋轉變換

// 繞座標軸旋轉(最常用)
// 方法簽名:void RotateX(double angle), RotateY(double angle), RotateZ(double angle)
// 參數説明:angle - 旋轉角度(度)
transform->RotateX(30.0);    // 繞X軸旋轉30度
transform->RotateY(45.0);    // 繞Y軸旋轉45度  
transform->RotateZ(90.0);    // 繞Z軸旋轉90度

// 繞任意軸旋轉
// 方法簽名:void RotateWXYZ(double angle, double x, double y, double z)
// 參數説明:
//   angle: 旋轉角度(度)
//   x,y,z: 旋轉軸的方向向量
transform->RotateWXYZ(45.0, 1.0, 1.0, 0.0);  // 繞向量(1,1,0)旋轉45度

// 使用數組參數的版本
double axis[3] = {0.0, 0.0, 1.0};
transform->RotateWXYZ(90.0, axis);  // 繞Z軸旋轉90度

3.3 縮放變換

// 方法簽名:void Scale(double x, double y, double z)
// 參數説明:
//   x: X軸方向的縮放因子
//   y: Y軸方向的縮放因子
//   z: Z軸方向的縮放因子

transform->Scale(2.0, 2.0, 2.0);    // 整體放大2倍
transform->Scale(1.5, 1.0, 1.0);    // 僅X方向放大1.5倍
transform->Scale(0.5, 0.5, 1.0);    // XY平面縮小一半

// 使用數組參數的版本  
double scaleFactors[3] = {1.0, 2.0, 3.0};
transform->Scale(scaleFactors);      // 各方向不同縮放

4. 變換順序控制

4.1 默認模式:前乘(PreMultiply)

vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
// 默認就是PreMultiply模式,新操作在現有變換之前執行

transform->Translate(10, 0, 0);    // 第一步:平移
transform->RotateZ(45);           // 第二步:旋轉(在平移後的座標系中)
transform->Scale(2, 1, 1);        // 第三步:縮放(在旋轉後的座標系中)

// 實際執行順序:縮放 → 旋轉 → 平移

4.2 後乘模式(PostMultiply)

vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
transform->PostMultiply();  // 設置為後乘模式

transform->Translate(10, 0, 0);    // 第一步:平移
transform->RotateZ(45);           // 第二步:旋轉  
transform->Scale(2, 1, 1);        // 第三步:縮放

// 實際執行順序:平移 → 旋轉 → 縮放

5. 矩陣操作與複合變換

5.1 直接設置矩陣

// 從現有矩陣設置
vtkSmartPointer<vtkMatrix4x4> matrix = vtkSmartPointer<vtkMatrix4x4>::New();
matrix->SetElement(0, 3, 10.0);  // 設置平移分量
// ... 設置其他矩陣元素
transform->SetMatrix(matrix);

// 從數組設置
double elements[16] = {
  1.0, 0.0, 0.0, 10.0,  // 第1行
  0.0, 1.0, 0.0, 5.0,   // 第2行  
  0.0, 0.0, 1.0, 0.0,   // 第3行
  0.0, 0.0, 0.0, 1.0    // 第4行
};
transform->SetMatrix(elements);

5.2 連接其他變換

// 連接另一個變換對象
vtkSmartPointer<vtkTransform> anotherTransform = vtkSmartPointer<vtkTransform>::New();
anotherTransform->RotateX(30.0);
transform->Concatenate(anotherTransform);  // 將另一個變換連接到當前變換

// 連接矩陣
vtkSmartPointer<vtkMatrix4x4> customMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
// ... 設置矩陣
transform->Concatenate(customMatrix);

6. 狀態管理與信息獲取

6.1 重置與逆變換

// 重置為單位變換
transform->Identity();  // 清除所有變換,回到初始狀態

// 計算逆變換
transform->Inverse();   // 將當前變換替換為其逆變換

// 獲取逆變換矩陣(不改變當前變換)
vtkSmartPointer<vtkMatrix4x4> inverseMatrix = vtkSmartPointer<vtkMatrix4x4>::New();
transform->GetInverse(inverseMatrix);  // 逆矩陣存入inverseMatrix

6.2 獲取變換信息

// 獲取平移分量
double position[3];
transform->GetPosition(position);  // position[0],position[1],position[2]包含平移量

// 獲取縮放因子  
double scale[3];
transform->GetScale(scale);        // scale數組包含各方向縮放因子

// 獲取旋轉角度(歐拉角)
double orientation[3];
transform->GetOrientation(orientation);  // 返回ZYX歐拉角

// 獲取旋轉信息(軸角表示)
double wxyz[4];
transform->GetOrientationWXYZ(wxyz);  // wxyz[0]角度, [1][2][3]旋轉軸

6.3 狀態保存與恢復

transform->Push();    // 保存當前變換狀態到堆棧
// ... 執行一些變換操作
transform->Pop();     // 恢復之前保存的狀態

7. 醫學影像處理實戰

7.1 DICOM圖像座標變換

void SetupDICOMTransform(vtkImageData* dicomImage, vtkTransform* transform)
{
  // 獲取DICOM圖像參數
  double spacing[3], origin[3];
  dicomImage->GetSpacing(spacing);    // 像素間距 (mm/pixel)
  dicomImage->GetOrigin(origin);      // 圖像原點 (mm)
  
  // 配置變換:體素座標 → 患者座標
  transform->Identity();
  transform->PostMultiply();  // 使用後乘模式,變換按聲明順序執行
  
  // 1. 應用像素間距縮放
  transform->Scale(spacing[0], spacing[1], spacing[2]);
  
  // 2. 應用圖像原點平移
  transform->Translate(origin[0], origin[1], origin[2]);
  
  // 3. 應用方向餘弦(如果需要)
  // transform->Concatenate(orientationMatrix);
}

7.2 多平面重建(MPR)

vtkSmartPointer<vtkTransform> CreateMPRTransform(
    double sliceCenter[3],     // 切片中心點(患者座標)
    double sliceNormal[3],     // 切片法向量  
    double viewUp[3])          // 視圖上方向
{
  vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
  transform->PostMultiply();  // 後乘模式,變換順序直觀
  
  // 1. 平移至切片中心
  transform->Translate(sliceCenter[0], sliceCenter[1], sliceCenter[2]);
  
  // 2. 計算旋轉使切面對齊法向量
  double zAxis[3] = {0.0, 0.0, 1.0};  // 默認Z軸(切面法向)
  double rotationAxis[3];
  vtkMath::Cross(zAxis, sliceNormal, rotationAxis);
  
  double angle = vtkMath::DegreesFromRadians(
    acos(vtkMath::Dot(zAxis, sliceNormal)));
    
  if (vtkMath::Norm(rotationAxis) > 1e-6) {
    vtkMath::Normalize(rotationAxis);
    transform->RotateWXYZ(angle, rotationAxis);
  }
  
  return transform;
}

8. 完整API參考表

函數類別

函數簽名

參數説明

使用示例

初始化

Identity()

無參數,重置為單位變換

transform->Identity()

平移

Translate(double x, double y, double z)

x,y,z: 各方向平移量

Translate(10,0,5)

Translate(const double x[3])

x: 包含3個平移量的數組

double t[3]={1,2,3}; Translate(t)

旋轉

RotateX(double angle)

angle: 繞X軸旋轉角度(度)

RotateX(45)

RotateY(double angle)

angle: 繞Y軸旋轉角度(度)

RotateY(30)

RotateZ(double angle)

angle: 繞Z軸旋轉角度(度)

RotateZ(90)

RotateWXYZ(double angle, double x, double y, double z)

angle: 旋轉角度, xyz: 旋轉軸

RotateWXYZ(45,1,1,0)

縮放

Scale(double x, double y, double z)

x,y,z: 各方向縮放因子

Scale(2,1,1)

Scale(const double s[3])

s: 包含3個縮放因子的數組

double s[3]={2,2,1}; Scale(s)

矩陣操作

SetMatrix(vtkMatrix4x4* matrix)

matrix: 要設置的4x4矩陣

SetMatrix(myMatrix)

SetMatrix(const double elements[16])

elements: 16個元素的數組

double m[16]={...}; SetMatrix(m)

Concatenate(vtkMatrix4x4* matrix)

matrix: 要連接的矩陣

Concatenate(addMatrix)

Concatenate(vtkLinearTransform* transform)

transform: 要連接的變換

Concatenate(otherTransform)

順序控制

PreMultiply()

無參數,設置為前乘模式

PreMultiply()

PostMultiply()

無參數,設置為後乘模式

PostMultiply()

信息獲取

GetPosition(double pos[3])

pos: 輸出平移分量的數組

double p[3]; GetPosition(p)

GetScale(double scale[3])

scale: 輸出縮放因子的數組

double s[3]; GetScale(s)

GetOrientation(double orient[3])

orient: 輸出歐拉角的數組

double o[3]; GetOrientation(o)

狀態管理

Push()

無參數,保存當前狀態

Push()

Pop()

無參數,恢復之前狀態

Pop()

通過這份完整的指南,您可以立即開始使用vtkTransform進行各種幾何變換操作。記住變換順序的控制是關鍵,根據您的需求選擇合適的PreMultiply或PostMultiply模式。