VTK中矩阵vtkMatrix4x4类的介绍和使用

发布于:2024-12-06 ⋅ 阅读:(211) ⋅ 点赞:(0)

1、矩阵-齐次坐标介绍

  常见的点一般是Pt(X,Y,Z),相当于一个1×3矩阵,而矩阵相乘的话一般是第一个矩阵的列数要等于第二个矩阵的行数。此处需要引入齐次坐标的概念:从广义上讲,齐次坐标就是用n+1维向量表示n 维向量,即将n维空间的点用 n+1维坐标表示。例如,一般笛卡尔坐标系中的二维点向量[x y]可用齐次坐标表示为[Hx Hx H],其中最后一维坐标是一个标量,称为比例因子。利用齐次坐标可以将平移、旋转、比例、投影等几何变换统一到矩阵的乘法上来,为图形变换提供方便。

  该矩阵在右手坐标系中定义,其中左上角部分产生比例、对称、错切和旋转变换,右上角部分产生平移变换;左下角部分产生透视变换;右下角部分产生全比例变换。
在这里插入图片描述

2、矩阵旋转

在这里插入图片描述
有兴趣可以将上述三个旋转矩阵按照不同的顺序进行相乘,得到的结果也是不一样的,例如:
1)先旋转x轴再旋转y轴再旋转z轴;
2)先旋转y轴再旋转x轴再旋转z轴;
3)先旋转z轴再旋转y轴再旋转x轴;

3、VTK矩阵-vtkMatrix4x4类

3.1 vtkMatrix4x4初始化
matrix1:
-0.013 -0.986 0.165 -133
-0.017 0.166 0.986 -35
-1 0.01 -0.02 40
0 0 0 1
	vtkMatrix4x4* matrix1 = vtkMatrix4x4::New();
	matrix1->Identity();
	matrix1->SetElement(0, 0, -0.013);
	matrix1->SetElement(0, 1, -0.986);
	matrix1->SetElement(0, 2, 0.165);
	matrix1->SetElement(0, 3, -133);

	matrix1->SetElement(1, 0, -0.017);
	matrix1->SetElement(1, 1, 0.166);
	matrix1->SetElement(1, 2, 0.986);
	matrix1->SetElement(1, 3, -35);

	matrix1->SetElement(2, 0, -1.0);
	matrix1->SetElement(2, 1, 0.01);
	matrix1->SetElement(2, 2, -0.02);
	matrix1->SetElement(2, 3, 40);
3.2 vtkMatrix4x4相乘

  下面两个矩阵的结果是不一样的,也就是常说的矩阵乘法左乘和右乘不一样。

	vtkMatrix4x4::Multiply4x4(matrix1, matrix2, matrix3);
	vtkMatrix4x4::Multiply4x4(matrix2, matrix1, matrix4);

  下面是完整的结果和代码:

在这里插入图片描述

#include <iostream>
#include <vtkMatrix4x4.h>

int main()
{
    vtkMatrix4x4* matrix1 = vtkMatrix4x4::New();
	matrix1->Identity();
	matrix1->SetElement(0, 0, -0.013);
	matrix1->SetElement(0, 1, -0.986);
	matrix1->SetElement(0, 2, 0.165);
	matrix1->SetElement(0, 3, -133);

	matrix1->SetElement(1, 0, -0.017);
	matrix1->SetElement(1, 1, 0.166);
	matrix1->SetElement(1, 2, 0.986);
	matrix1->SetElement(1, 3, -35);

	matrix1->SetElement(2, 0, -1.0);
	matrix1->SetElement(2, 1, 0.01);
	matrix1->SetElement(2, 2, -0.02);
	matrix1->SetElement(2, 3, 40);

	vtkMatrix4x4* matrix2 = vtkMatrix4x4::New();
	matrix2->Identity();
	matrix2->SetElement(0, 3, -140);
	matrix2->SetElement(1, 3, -140);
	matrix2->SetElement(2, 3, -143);

	vtkMatrix4x4* matrix3 = vtkMatrix4x4::New();
	matrix3->Identity();
	vtkMatrix4x4* matrix4 = vtkMatrix4x4::New();
	matrix4->Identity();

	vtkMatrix4x4::Multiply4x4(matrix1, matrix2, matrix3);
	vtkMatrix4x4::Multiply4x4(matrix2, matrix1, matrix4);
	// 打印结果矩阵以验证

	std::cout << "matrix1: " << std::endl;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			std::cout << matrix1->GetElement(i, j) << " ";
		}
		std::cout << std::endl;
	}

	std::cout << "matrix2: " << std::endl;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			std::cout << matrix2->GetElement(i, j) << " ";
		}
		std::cout << std::endl;
	}

	std::cout << std::endl;

	std::cout << "matrix1 * matrix2: " << std::endl;
	for (int i = 0; i < 4; ++i) 
	{
		for (int j = 0; j < 4; ++j) 
		{
			std::cout << matrix3->GetElement(i, j) << " ";
		}
		std::cout << std::endl;
	}

	std::cout << std::endl;

	std::cout << "matrix2 * matrix1: " << std::endl;
	for (int i = 0; i < 4; ++i)
	{
		for (int j = 0; j < 4; ++j)
		{
			std::cout << matrix4->GetElement(i, j) << " ";
		}
		std::cout << std::endl;
	}

	// 释放内存
	matrix1->Delete();
	matrix2->Delete();
	matrix3->Delete();
	matrix4->Delete();

	return 1;
}

网站公告

今日签到

点亮在社区的每一天
去签到