简单来讲就是一个4*4的线性变换矩阵。初始化时,这个矩阵为恒等变换(identity transformation)。通过这个对象,可以实现:平移、旋转、连接(Translate, Rotate, and Concatenate)。
输入数据: 可以是其他的 transform.
模式:
- PreMultiply:添加的平移、连接等变换在当前设置的矩阵之前实现
- PostMultiply:添加的平移、连接等变换在当前设置的矩阵之后实现
这个类默认的是右手坐标系。
vtkSmartPointer<vtkTransform> transform = vtkSmartPointer<vtkTransform>::New();
//transform->RotateWXYZ(double angle, double x, double y, double z);
transform->RotateWXYZ(10, 0, 1, 0);
vtkSmartPointer<vtkTransformPolyDataFilter> transformFilter =
vtkSmartPointer<vtkTransformPolyDataFilter>::New();
transformFilter->SetTransform(transform);
transformFilter->SetInputConnection(coneSource->GetOutputPort());
transformFilter->Update();
//This example describes the transformation pipeline. Robotic arm was used to demonstrate an example.
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkTransform.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
int main (int, char *[])
{
vtkSmartPointer<vtkRenderer> ren1 = vtkSmartPointer<vtkRenderer>::New();
ren1->SetBackground( 0.1, 0.2, 0.4 );
vtkSmartPointer<vtkRenderWindow> renWin = vtkSmartPointer<vtkRenderWindow>::New();
renWin->AddRenderer(ren1);
renWin->SetSize(600, 600);
renWin->SetWindowName("Robotic Arm");
vtkSmartPointer<vtkRenderWindowInteractor> iren = vtkSmartPointer<vtkRenderWindowInteractor>::New();
iren->SetRenderWindow(renWin);
//arm
vtkSmartPointer<vtkCylinderSource> arm = vtkSmartPointer<vtkCylinderSource>::New();
arm->SetRadius(8);
arm->SetHeight(20);
arm->SetResolution(20);
vtkSmartPointer<vtkPolyDataMapper> armMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
armMapper->SetInputConnection(arm->GetOutputPort());
vtkSmartPointer<vtkTransform> armTransform = vtkSmartPointer<vtkTransform>::New();
vtkSmartPointer<vtkActor> armActor = vtkSmartPointer<vtkActor>::New();
armActor->SetUserTransform(armTransform);
armActor->SetMapper(armMapper);
armActor->GetProperty()->SetColor(0.7,0.6,0.3);
//forearm
vtkSmartPointer<vtkCylinderSource> forearm = vtkSmartPointer<vtkCylinderSource>::New();
forearm->SetRadius(6);
forearm->SetHeight(15);
forearm->SetResolution(20);
forearm->SetCenter(*(arm->GetCenter()),*(arm->GetCenter()+1)+forearm->GetHeight(),*(arm->GetCenter()+2));
vtkSmartPointer<vtkPolyDataMapper> forearmMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
forearmMapper->SetInputConnection(forearm->GetOutputPort());
vtkSmartPointer<vtkTransform> forearmTransform = vtkSmartPointer<vtkTransform>::New();
forearmTransform->SetInput(armTransform);
vtkSmartPointer<vtkActor> forearmActor = vtkSmartPointer<vtkActor>::New();
forearmActor->SetUserTransform(forearmTransform);
forearmActor->SetMapper(forearmMapper);
forearmActor->GetProperty()->SetColor(0.1,0.2,0.9);
//hand
vtkSmartPointer<vtkCylinderSource> hand = vtkSmartPointer<vtkCylinderSource>::New();
hand->SetRadius(4);
hand->SetHeight(10);
hand->SetResolution(20);
hand->SetCenter(*(forearm->GetCenter()),*(forearm->GetCenter()+1)+hand->GetHeight(),*(forearm->GetCenter()+2));
vtkSmartPointer<vtkPolyDataMapper> handMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
handMapper->SetInputConnection(hand->GetOutputPort());
vtkSmartPointer<vtkTransform> handTransform = vtkSmartPointer<vtkTransform>::New();
handTransform->SetInput(forearmTransform);
vtkSmartPointer<vtkActor> handActor = vtkSmartPointer<vtkActor>::New();
handActor->SetUserTransform(handTransform);
handActor->SetMapper(handMapper);
handActor->GetProperty()->SetColor(0.6,0.9,0.1);
ren1->AddActor(armActor);
ren1->AddActor(forearmActor);
ren1->AddActor(handActor);
renWin->Render();
//execution of robot arm motion
for (int i = 0; i <45; i++)
{
armTransform->Identity();
armTransform->RotateZ(-i);
renWin->Render();
}
//execution of robot forearm motion
for (int i = 0; i <45; i++)
{
forearmTransform->Identity();
forearmTransform->RotateZ(i);
renWin->Render();
}
iren->Initialize();
iren->Start();
return EXIT_SUCCESS;
}