博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3D数学 矩阵和线性变换之旋转
阅读量:4087 次
发布时间:2019-05-25

本文共 7003 字,大约阅读时间需要 23 分钟。

矩阵和线性变换之旋转

1. 如何在3D世界中对坐标进行变换?

我们可以通过产生一个具有某种变换效果的矩形,用坐标上的某个点乘上这个矩阵,就会得到变换后的点。这是线性代数中线性变换的内容。

2. 具有旋转效果的矩阵如何生成?

首先说明下,这本书里用的都是左手坐标系。我们规定左手坐标系,拇指朝向旋转轴,其他手指的方向就是旋转的正方向。

左手坐标系
通过线性变换的数学推理,可以得到,
绕X轴旋转θ度的矩阵为:
绕x轴旋转
绕y轴旋转θ度的矩阵为:
绕y轴旋转
绕z轴旋转θ度的矩阵为:
绕z轴旋转

3. 旋转矩阵实现代码示例

enum E_Axis{Axis_x,Axis_y,Axis_z};void Matrix3X3::setRotate(E_Axis axis,float theta){    float sinValue,cosValue;    sinValue = sin(theta);    cosValue = cos(theta);    switch(axis)    {    case Axis_x:        {            m11 = 1;    m12 = 0;            m13 = 0;            m21 = 0;    m22 = cosValue;     m23 = sinValue;            m31 = 0;    m32 = -sinValue;    m33 = cosValue;            break;        }    case Axis_y:        {            m11 = cosValue; m12 = 0;    m13 = -sinValue;            m21 = 0;        m22 = 1;    m23 = 0;            m31 = sinValue; m32 = 0;    m33 = cosValue;            break;        }    case Axis_z:        {            m11 = cosValue;     m12 = sinValue; m13 = 0;            m21 = -sinValue;    m22 = cosValue; m23 = 0;            m31 = 0;            m32 = 0;        m33 = 1;            break;        }    default:        assert(false);    }}

4. 矩阵和线性变换之旋转完整程序示例代码

//MathUtil.h#pragma once#include 
enum E_Axis{Axis_x,Axis_y,Axis_z};const float Pi = 3.14159;
//Matrix3X3.h#pragma once#include "MathUtil.h"#include "Vector3.h"class Matrix3X3{public:    //矩阵相乘    Matrix3X3 operator*(Matrix3X3& rhs);    //矩阵乘等矩阵    Matrix3X3& operator*=(Matrix3X3& rhs);    void setRotate(E_Axis axis,float theta);public:    float m11,m12,m13;    float m21,m22,m23;    float m31,m32,m33;};//向量乘以矩阵Vector3 operator*(Vector3& vec,Matrix3X3& mat);//向量乘等矩阵Vector3& operator*=(Vector3& vec,Matrix3X3& mat);
//Matrix3X3.cpp#include "Matrix3X3.h"#include 
Matrix3X3 Matrix3X3::operator*(Matrix3X3& rhs){ Matrix3X3 tempMat; tempMat.m11 = this->m11 * rhs.m11 + this->m12 * rhs.m21 + this->m13 * rhs.m31; tempMat.m12 = this->m11 * rhs.m12 + this->m12 * rhs.m22 + this->m13 * rhs.m32; tempMat.m13 = this->m11 * rhs.m13 + this->m12 * rhs.m23 + this->m13 * rhs.m33; tempMat.m21 = this->m21 * rhs.m11 + this->m22 * rhs.m21 + this->m23 * rhs.m31; tempMat.m22 = this->m21 * rhs.m12 + this->m22 * rhs.m22 + this->m23 * rhs.m32; tempMat.m23 = this->m21 * rhs.m13 + this->m22 * rhs.m23 + this->m23 * rhs.m33; tempMat.m31 = this->m31 * rhs.m11 + this->m32 * rhs.m21 + this->m33 * rhs.m31; tempMat.m32 = this->m31 * rhs.m12 + this->m32 * rhs.m22 + this->m33 * rhs.m32; tempMat.m33 = this->m31 * rhs.m13 + this->m32 * rhs.m23 + this->m33 * rhs.m33; return tempMat;}Matrix3X3& Matrix3X3::operator*=(Matrix3X3& rhs){ *this = *this * rhs; return *this;}Vector3 operator*(Vector3& vec,Matrix3X3& mat){ Vector3 tempVec; tempVec.x = vec.x * mat.m11 + vec.y * mat.m21 + vec.z * mat.m31; tempVec.y = vec.x * mat.m12 + vec.y * mat.m22 + vec.z * mat.m32; tempVec.z = vec.x * mat.m13 + vec.y * mat.m23 + vec.z * mat.m33; return tempVec;}Vector3& operator*=(Vector3& vec,Matrix3X3& mat){ vec = vec * mat; return vec;}void Matrix3X3::setRotate(E_Axis axis,float theta){ float sinValue,cosValue; sinValue = sin(theta); cosValue = cos(theta); switch(axis) { case Axis_x: { m11 = 1; m12 = 0; m13 = 0; m21 = 0; m22 = cosValue; m23 = sinValue; m31 = 0; m32 = -sinValue; m33 = cosValue; break; } case Axis_y: { m11 = cosValue; m12 = 0; m13 = -sinValue; m21 = 0; m22 = 1; m23 = 0; m31 = sinValue; m32 = 0; m33 = cosValue; break; } case Axis_z: { m11 = cosValue; m12 = sinValue; m13 = 0; m21 = -sinValue; m22 = cosValue; m23 = 0; m31 = 0; m32 = 0; m33 = 1; break; } default: assert(false); }}
//Vector3.h#pragma onceclass Vector3{public:    Vector3();    Vector3(float X,float Y,float Z);    //变为零向量    void Zero();    //求负向量    Vector3 operator-() const;    //求向量大小(长度或模)    float Length() const;    //标准化该向量    void Normal();    //向量的加法    Vector3 operator+(Vector3 &rhs) const;    Vector3& operator+=(Vector3 &rhs);    //向量的减法    Vector3 operator-(Vector3 &rhs) const;    Vector3& operator-=(Vector3 &rhs);    //向量乘标量    Vector3 operator*(float scalar);    //向量乘等于标量    Vector3& operator*=(float scalar);    //向量除以等于标量    Vector3& operator/=(float scalar);    //向量除以标量    Vector3 operator/(float scalar);    //距离公式    float Distance(Vector3 &vec) const;    //向量点乘    float operator*(Vector3 &rhs) const;    //向量叉积    Vector3 CrossProduct(Vector3& vec) const;public:    float x,y,z;};//标量乘向量Vector3 operator*(float scalar, Vector3& vec);
//Vector3.cpp#include "Vector3.h"#include 
Vector3::Vector3():x(0.0),y(0.0),z(0.0){}Vector3::Vector3(float X,float Y,float Z):x(X),y(Y),z(Z){}void Vector3::Zero(){ x = y = z = 0;}Vector3 Vector3::operator-() const{ return Vector3(-x,-y,-z);}float Vector3::Length() const{ return sqrt(x*x+y*y+z*z);}Vector3 Vector3::operator*(float scalar){ return Vector3(this->x * scalar, this->y * scalar, this->z * scalar);}Vector3& Vector3::operator*=(float scalar){ return *this = *this * scalar;}Vector3& Vector3::operator/=(float scalar){ return *this = *this / scalar;}Vector3 operator*(float scalar, Vector3& vec){ return vec*scalar;}Vector3 Vector3::operator/(float scalar){ float temp = 1/ scalar; return *this * temp;}void Vector3::Normal(){ //计算机计算乘法的速度比除法快 float temp = 1 / Length(); x *= temp; y *= temp; z *= temp;}Vector3 Vector3::operator+(Vector3& rhs) const{ return Vector3(x+rhs.x,y+rhs.y,z+rhs.z);}Vector3& Vector3::operator+=(Vector3& rhs){ *this = *this + rhs; return *this;}Vector3 Vector3::operator-(Vector3& rhs) const{ return Vector3(x-rhs.x,y-rhs.y,z-rhs.z);}Vector3& Vector3::operator-=(Vector3& rhs){ *this = *this - rhs; return *this;}float Vector3::Distance(Vector3& vec) const{ return (*this - vec).Length();}float Vector3::operator*(Vector3& rhs) const{ return this->x * rhs.x + this->y * rhs.y + this->z * rhs.z;}Vector3 Vector3::CrossProduct(Vector3& vec) const{ return Vector3(this->y * vec.z - this->z * vec.y, this->z * vec.x - this->x * vec.z, this->x * vec.y - this->y * vec.x);}
//main.cpp#include 
#include "Vector3.h"#include "Matrix3X3.h"using namespace std;float ToZero(float num){ return (abs(num) < 0.0001 ? 0 : num);}void print_v(Vector3 v){ cout << "[ " << ToZero(v.x) << ", " << ToZero(v.y) << ", " << ToZero(v.z) << " ]" << endl; cout << endl;}void print_m(Matrix3X3 m){ cout << m.m11 << "\t" << m.m12 << "\t" << m.m13 << endl; cout << m.m21 << "\t" << m.m22 << "\t" << m.m23 << endl; cout << m.m31 << "\t" << m.m32 << "\t" << m.m33 << endl; cout << endl;}int main(){ Vector3 a(10,0,0),b; Matrix3X3 M; M.setRotate(Axis_z,Pi/2); b = a * M; print_v(b); system("pause"); return 0;}

运行结果:

[ 0, 10, 0 ]

你可能感兴趣的文章
Yolov5系列AI常见数据集(1)车辆,行人,自动驾驶,人脸,烟雾
查看>>
【Jetson-Nano】2.Tensorflow object API和Pytorch的安装
查看>>
荔枝派 Nano 全志 F1C100s 编译运行 Linux ubuntu并升级gcc
查看>>
C++ STL 四种智能指针
查看>>
基于sympy的python实现三层BP神经网络算法
查看>>
玩玩机器学习1——ubuntu16.04 64位安装TensorFlow GPU+python3+cuda8.0+cudnn8.0
查看>>
CentOS7 搭建Pulsar 消息队列环境,CentOS(Linux)部署Pulsar,亲测成功,以及Python操作Pulsar实例驱动
查看>>
Git报错: OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443
查看>>
Java学习笔记--带有验证码的登录案例
查看>>
数据结构与算法学习笔记(1)--数组
查看>>
jdk8和jdk11不能随意切换的问题
查看>>
2020-12-29
查看>>
2021-01-16
查看>>
AndroidStudio学习(二)-模拟小相册
查看>>
Python代码打开本地.mp4格式文件
查看>>
MySql服务无法启动:2003 can't connect to mysql 10061
查看>>
选择排序
查看>>
递归和快速
查看>>
分治算法(一)二分搜索技术
查看>>
分治算法(二)合并排序
查看>>