c++ 知道旋转前后矩阵向量值 求旋转矩阵c++/c#代码 知道两个向量求他们的旋转矩阵

原文作者:aircraft

原文链接:https://www.cnblogs.com/DOMLX/p/12115244.html

知道旋转前后矩阵向量值 如何去求旋转矩阵R 的c++/c#代码???

因为需要用到矩阵处理库所以需要先配置

一、Eigen库的配置(VS2017)

      1. Eigen库下载: http://eigen.tuxfamily.org/index.php?title=Main_Page
        下载文件并解压:

然后在自己的VS工程属性中的这个附加包含进去

注意看清楚了 是D:\Dependencies\eigen-eigen\eigen-eigen;      前面部分是你们自己的路径 后面的这个eigen-eigen\eigen-eigen; 代表的意思解压是点击进去选择里面那个名字跟外面一样的

二、实现代码

c++代码:

#include <cmath>
#include <iostream>
#include "Eigen/Dense"
#include "Eigen/LU"
#include "Eigen/Core"
#define PI 3.1415926

//计算旋转角
double calculateAngle(const Eigen::Vector3d &vectorBefore, const Eigen::Vector3d &vectorAfter)
{
    double ab, a1, b1, cosr;
    ab = vectorBefore.x()*vectorAfter.x() + vectorBefore.y()*vectorAfter.y() + vectorBefore.z()*vectorAfter.z();
    a1 = sqrt(vectorBefore.x()*vectorBefore.x() + vectorBefore.y()*vectorBefore.y() + vectorBefore.z()*vectorBefore.z());
    b1 = sqrt(vectorAfter.x()*vectorAfter.x() + vectorAfter.y()*vectorAfter.y() + vectorAfter.z()*vectorAfter.z());
    cosr = ab / a1 / b1;
    return (acos(cosr) * 180 / PI);
}
//计算旋转轴
inline Eigen::Vector3d calculateRotAxis(const Eigen::Vector3d &vectorBefore, const Eigen::Vector3d &vectorAfter)
{
    return Eigen::Vector3d(vectorBefore.y()*vectorAfter.z() - vectorBefore.z()*vectorAfter.y(),         vectorBefore.z()*vectorAfter.y() - vectorBefore.x()*vectorAfter.z(),         vectorBefore.x()*vectorAfter.y() - vectorBefore.y()*vectorAfter.x());
}
//计算旋转矩阵
void rotationMatrix(const Eigen::Vector3d &vectorBefore, const Eigen::Vector3d &vectorAfter, Eigen::Matrix3d &rotMatrix)
{
    Eigen::Vector3d vector = calculateRotAxis(vectorBefore, vectorAfter);
    double angle = calculateAngle(vectorBefore, vectorAfter);
    Eigen::AngleAxisd rotationVector(angle, vector.normalized());
    Eigen::Matrix3d rotationMatrix = Eigen::Matrix3d::Identity();
    rotMatrix =  rotationVector.toRotationMatrix();//所求旋转矩阵
}

int main()
{
    Eigen::Matrix3d rotMatrix;

    Eigen::Vector3d vectorBefore(0,0,1);
    Eigen::Vector3d vectorAfter(1,0,0);
    rotationMatrix(vectorBefore, vectorAfter, rotMatrix);
    std::cout << rotMatrix << std::endl;
    system("pause");
    return 0;
}

打印结果:

c#代码:

void Calculation(double[] vectorBefore, double[] vectorAfter)
{
    double[] rotationAxis;
    double rotationAngle;
    double[,] rotationMatrix;
    rotationAxis = CrossProduct(vectorBefore, vectorAfter);
    rotationAngle = Math.Acos(DotProduct(vectorBefore, vectorAfter) / Normalize(vectorBefore) / Normalize(vectorAfter));
    rotationMatrix = RotationMatrix(rotationAngle, rotationAxis);
}

double[] CrossProduct(double[] a, double[] b)
{
    double[] c = new double[3];

    c[0] = a[1] * b[2] - a[2] * b[1];
    c[1] = a[2] * b[0] - a[0] * b[2];
    c[2] = a[0] * b[1] - a[1] * b[0];

    return c;
}

double DotProduct(double[] a, double[] b)
{
    double result;
    result = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];

    return result;
}

double Normalize(double[] v)
{
    double result;

    result = Math.Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);

    return result;
}

double[,] RotationMatrix(double angle, double[] u)
{
    double norm = Normalize(u);
    double[,] rotatinMatrix = new double[3,3];

    u[0] = u[0] / norm;
    u[1] = u[1] / norm;
    u[2] = u[2] / norm;

    rotatinMatrix[0, 0] = Math.Cos(angle) + u[0] * u[0] * (1 - Math.Cos(angle));
    rotatinMatrix[0, 0] = u[0] * u[1] * (1 - Math.Cos(angle) - u[2] * Math.Sin(angle));
    rotatinMatrix[0, 0] = u[1] * Math.Sin(angle) + u[0] * u[2] * (1 - Math.Cos(angle));

    rotatinMatrix[0, 0] = u[2] * Math.Sin(angle) + u[0] * u[1] * (1 - Math.Cos(angle));
    rotatinMatrix[0, 0] = Math.Cos(angle) + u[1] * u[1] * (1 - Math.Cos(angle));
    rotatinMatrix[0, 0] = -u[0] * Math.Sin(angle) + u[1] * u[2] * (1 - Math.Cos(angle));

    rotatinMatrix[0, 0] = -u[1] * Math.Sin(angle) + u[0] * u[2] * (1 - Math.Cos(angle));
    rotatinMatrix[0, 0] = u[0] * Math.Sin(angle) + u[1] * u[2] * (1 - Math.Cos(angle));
    rotatinMatrix[0, 0] = Math.Cos(angle) + u[2] * u[2] * (1 - Math.Cos(angle));

    return rotatinMatrix;
}

三、实现原理

1.旋转角度

已知旋转前向量为P, 旋转后变为Q。由点积定义可知:

可推出P,Q之间的夹角为:

2. 旋转轴

由1中可知,旋转角所在的平面为有P和Q所构成的平面,那么旋转轴必垂直该平面。

假定旋转前向量为a(a1, a2, a3), 旋转后向量为b(b1, b2, b3)。由叉乘定义得:

所以旋转轴c(c1, c2, c3)为:

3.  罗德里格旋转公式(Rodrigues‘ rotation formula)

3.1 公式

已知单位向量 , 将它旋转θ角。由罗德里格旋转公式,可知对应的旋转矩阵

其中I是3x3的单位矩阵,

是叉乘中的反对称矩阵r:

3.2 公式证明

假设在坐标系(x, y, z)中,向量v=ax+by+cz,v绕z轴逆时针旋转θ角后得到新的向量v’。

根据2维(x,y)面上的旋转公式可得:

推出:

已知:

将上式带入v’的公式:

  将cz替换掉,可得:

将上式中的叉乘表示为反对称矩阵得:

另外:

最终可以推出:

上式即为罗德里格旋转公式。

参考博客:https://www.cnblogs.com/xpvincent/archive/2013/02/15/2912836.html

参考博客里的是c#的实现代码

我是参考完之后改了一个c++的版本出来

若有兴趣交流分享技术,可关注本人公众号,里面会不定期的分享各种编程教程,和共享源码,诸如研究分享关于c/c++,python,前端,后端,opencv,halcon,opengl,机器学习深度学习之类有关于基础编程,图像处理和机器视觉开发的知识

原文地址:https://www.cnblogs.com/DOMLX/p/12115244.html

时间: 2024-08-27 19:14:23

c++ 知道旋转前后矩阵向量值 求旋转矩阵c++/c#代码 知道两个向量求他们的旋转矩阵的相关文章

unity 由两个向量求夹角

1. float angle = Vector3.Angle (fromVector, toVector); //求出两向量之间的夹角 Vector3 normal = Vector3.Cross (fromVector,toVector);//叉乘求出法线向量 angle *= Mathf.Sign (Vector3.Dot(normal,upVector)); //求法线向量与物体上方向向量点乘,结果为1或-1,修正旋转方向 2. Vector3 velocity = Quaternion.

NYOJ 598 旋转圆柱矩阵

旋转圆柱矩阵 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 将圆柱体的侧面展开能得到一个m*n的矩形.这是姑且看成一个01矩阵, 我们需要通过特定的移动得到另一个矩阵(该矩阵其中任意的某一列全部为1),每一次只能移动一行,且只能移动一位,可以向左移动,也可以向右移动,求最小的移动步数,如不能通过移动得到结果,则输出"-1". 例如: 3*5的矩阵 00001 10000 00001 我们只需要将第二行向左移动一位即可得到所求. 输入 第一行一个整数N(N

机器学习中的矩阵向量求导(三) 矩阵向量求导之微分法

在机器学习中的矩阵向量求导(二) 矩阵向量求导之定义法中,我们讨论了定义法求解矩阵向量求导的方法,但是这个方法对于比较复杂的求导式子,中间运算会很复杂,同时排列求导出的结果也很麻烦.因此我们需要其他的一些求导方法.本文我们讨论使用微分法来求解标量对向量的求导,以及标量对矩阵的求导. 本文的标量对向量的求导,以及标量对矩阵的求导使用分母布局.如果遇到其他资料求导结果不同,请先确认布局是否一样. 1. 矩阵微分 在高数里面我们学习过标量的导数和微分,他们之间有这样的关系:$df =f'(x)dx$.

旋转正方形矩阵

Problem: 旋转正方形矩阵[题目] 给定一个整型正方形矩阵matrix, 请把该矩阵调整成顺时针旋转90度的样子. [要求] 额外空间复杂度为O(1). Solution: 同样,采用由外向内一圈一圈变换,找到元素变换位置的规律即可. 难点在于区分偶数维矩阵和奇数维矩阵的不同操作 Code: 1 #pragma once 2 3 #include <iostream> 4 using namespace std; 5 6 template<class T> 7 void Ro

poj2187 求平面最远点对,garham_scan算法求凸包

poj2187 求平面最远点对,garham_scan算法求凸包 Beauty Contest Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 29666   Accepted: 9180 Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest, earning the title 'M

两种方法求丑数

我们把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 方法1 : 暴力破解,逐个判断 代码: <pre name="code" class="cpp">#include <iostream> #include <vector> using namespace std; //判断是否是丑数 bool isUgly(int index){ while(index % 2

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果

编写一个程序,用户输入两个数,求出其加减乘除,并用消息框显示计算结果 import javax.swing.JOptionPane; public class Test{ public static void main(String[] args) { int n1=Integer.parseInt(JOptionPane.showInputDialog("Input number 1: ")); int n2=Integer.parseInt(JOptionPane.showInpu

数组去重和两个数组求交集

引言 今天在项目中一个功能要用到两个数组求交集的算法. 大概是这样: 两个表格分别用easyui datagrid实现,要把A表格的一列数据和B表格的一列数据取出来,然后去重,去重后求交集. 那么在计算出的交集中分别根据求出的数据选中两个表格的对应行. 故用到js数组去重和求交集. 交集 Array.prototype.intersect = function(b) { var flip = {}; var res = []; for(var i=0; i< b.length; i++) fli

求一棵普通树的两个结点的最低公共祖先

一棵普通树,树中的结点没有指向父节点的指针,求一棵普通树的两个结点的最低公共祖先. 代码如下,我太懒没有加注释,大家自己看吧! 1 #include <iostream> 2 #include <list> 3 #include <vector> 4 using namespace std; 5 6 struct TreeNode //节点 7 { 8 char m_nValue; 9 vector<TreeNode*> m_vChildren; 10 };