c++ - 余弦/正弦矩阵数学

标签 c++ math matrix

我有一个例程,它将接受 union 参数 d、theta、a 和 alpha 作为输入,并将生成相应的 4x4 齐次矩阵作为输出。我已经测试了我的矩阵乘法,它确实工作正常。我将从输入中得到 5 个矩阵,它们将全部相乘,得到 $t^0_5$。测试用例是 here .我的输出与结果完全不同。这是我的代码:

首先是我们将数据输入到使用 DH parameter tables 的矩阵中的矩阵:

 //Kinematics
Matrix44 Matrix44::kinematics(double d, double theta, double a, double alpha)const {
     Matrix44 result;

     double pi = 3.14159;
     double radstheta = theta*(pi/180);
     double radsalpha = alpha*(pi/180);
     //row1
     result.element[0][0] = cos(radstheta);
     result.element[0][3] = -cos(radsalpha)*sin(radstheta);
     result.element[0][4] = sin(radsalpha)*sin(radstheta);
     result.element[0][3] = a*cos(radstheta);
     //row2
     result.element[1][0] = sin(radstheta);
     result.element[1][5] = cos(radsalpha)*cos(radstheta);
     result.element[1][6] = -sin(radsalpha)*cos(radstheta);
     result.element[1][3] = a*sin(radstheta);
     //row3
     result.element[2][0] = 0;
     result.element[2][7] = sin(radsalpha);
     result.element[2][8] = cos(radsalpha);
     result.element[2][3] = d;
     //row4
     result.element[3][0] = 0;
     result.element[3][9] = 0;
     result.element[3][10] = 0;
     result.element[3][3] = 1;

     return result;
}

main 中我得到结果的部分,数据来自this table :

          Matrix44 a,b,c,d,e;
            //in order (d,theta,a,alpha)
    //all data is static and given except for theta which changes, see link for cases
        a = a.kinematics(27.2,0, 0, 90);
        b = b.kinematics(0,0,19.2,180);
        c = c.kinematics(0,0,19.2,0);
        d = d.kinematics(0,0+90,0,90);
        e = e.kinematics(10.5,0,0,0);
       //anyone know how to format this nicely? The operator is overload to print a matrix
//
        cout << left <<setw(20) << a*b*c*d*e;

Theta 和 alpha 是角度,D 和 A 是距离。

输出/输入代码:

   //User Input
  istream& operator>> (istream& s, Matrix44& t) {
    for (int i=0; i<4; i++)
      for (int j=0; j<4; j++)
        s >> t.element[i][j];
    if (!s) { cerr << "Error reading Matrix from stream";  exit(0); }
    return s;
  }
  //User Output
  ostream& operator<< (ostream& s, const Matrix44& t) {
    for (int i=0; i<4; i++) {
      for (int j=0; j<4; j++)
        s << t.element[i][j] << "   ";
      s << endl;
    }
    if (!s) { cerr << "Error writing Matrix to stream";  exit(0); }
    return s;
  }

矩阵:

    class Matrix44 {
private:
    double element[4][4];
    friend class Point;
public:
    Matrix44(void);
    Matrix44 transpose(void) const;
    Matrix44 inverse(Matrix44 x) const;
    Matrix44 kinematics(double d, double theta, double a, double alpha) const;
    friend istream& operator>>(istream& s, Matrix44& t);
    friend ostream& operator<<(ostream& s, const Matrix44& t);
    Matrix44 operator *(Matrix44 b);
    Point operator*(const Point & P);
};

最佳答案

有几个问题,一个在您显示的代码中,另一个不在。

问题 1:这可能不是问题的根源,而是 double pi = 3.14159;适用于花车,但不适用于 double 。你应该至少有 16 个地方有 pi。更好的是,使用 M_PI来自 <math.h> ,许多编译器中的常见扩展名。如果你的编译器没有定义 M_PI , 使用类似 3.14159265358979323846264338327950288 的内容。

问题 2:您没有在执行乘法运算的地方显示代码,因此这也可能不是问题的根源。问题在于旋转群不可交换:A*B 不等于 B*A。你必须非常小心这里的乘法约定。示例:拿起一本书,将其平放,使封面朝上,书脊朝左。将书围绕指向一行文本方向的轴旋转 +90 度。现在绕从页面底部指向顶部的轴旋转 90 度。你的书的方向应该使你可以把它放在架子上(书脊垂直方向,面向你)。现在将书放回原来的方向并重复旋转,但顺序相反。这次您会看到截然不同的画面。

关于c++ - 余弦/正弦矩阵数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7746056/

相关文章:

c++ - 在 C++ 中避免负值

javascript - 寻找衍生脚本

计算将矩阵中与另一个数字相等的数字相加的可能性有多少种

python - Python 中的洪水填充

c++ - 在 CToolBar 对象中使用 PNG 时出现问题

c++ - "hard-coding"和传入有关内存的参数有什么区别?

c++ - 移动后通过右值引用返回

c++ - 任何人解释 C++ 代码行

math - 三角形 : Determine whether a triangle can be built from a given set of edges

c - fscanf 计数 "invisible"个空格