如何理解这个旋转矩阵码?

时间:2018-06-14 08:40:37

标签: c# rotational-matrices

我正在尝试校准我的磁电数据传感器。我找到了一段代码,但我无法理解它是如何工作的,特别是旋转矩阵的一部分,知道第一步是从传感器获取数据在X + 0度,X + 180,x-0degres,x- 180。

但是,我不明白他是如何使用矢量旋转矩阵的。

  private void calculate_transformation_matrix()
    {
        //Axis X--------------------------------------------------------------------------------------------------
        double[] Xplus_center = new double[3];
        //Centers des cercle
        Xplus_center[0] = (double.Parse(textBoxXplus_X_0.Text) + double.Parse(textBoxXplus_X_180.Text)) / 2;
        Xplus_center[1] = (double.Parse(textBoxXplus_Y_0.Text) + double.Parse(textBoxXplus_Y_180.Text)) / 2;
        Xplus_center[2] = (double.Parse(textBoxXplus_Z_0.Text) + double.Parse(textBoxXplus_Z_180.Text)) / 2;
        //Centers des cercles
        double[] Xminus_center = new double[3];
        Xminus_center[0] = (double.Parse(textBoxXminus_X_0.Text) + double.Parse(textBoxXminus_X_180.Text)) / 2;
        Xminus_center[1] = (double.Parse(textBoxXminus_Y_0.Text) + double.Parse(textBoxXminus_Y_180.Text)) / 2;
        Xminus_center[2] = (double.Parse(textBoxXminus_Z_0.Text) + double.Parse(textBoxXminus_Z_180.Text)) / 2;
        //Vector from the center of minus circle to the center of plus circle
        double[] Xvector = new double[3];
        Xvector[0] = Xplus_center[0] - Xminus_center[0];
        Xvector[1] = Xplus_center[1] - Xminus_center[1];
        Xvector[2] = Xplus_center[2] - Xminus_center[2];

        //Axis Y--------------------------------------------------------------------------------------------------
        double[] Yplus_center = new double[3];
        //Centers des cercles
        Yplus_center[0] = (double.Parse(textBoxYplus_X_0.Text) + double.Parse(textBoxYplus_X_180.Text)) / 2;
        Yplus_center[1] = (double.Parse(textBoxYplus_Y_0.Text) + double.Parse(textBoxYplus_Y_180.Text)) / 2;
        Yplus_center[2] = (double.Parse(textBoxYplus_Z_0.Text) + double.Parse(textBoxYplus_Z_180.Text)) / 2;
        //Centers des cercles
        double[] Yminus_center = new double[3];
        Yminus_center[0] = (double.Parse(textBoxYminus_X_0.Text) + double.Parse(textBoxYminus_X_180.Text)) / 2;
        Yminus_center[1] = (double.Parse(textBoxYminus_Y_0.Text) + double.Parse(textBoxYminus_Y_180.Text)) / 2;
        Yminus_center[2] = (double.Parse(textBoxYminus_Z_0.Text) + double.Parse(textBoxYminus_Z_180.Text)) / 2;
        //Vector from the center of minus circle to the center of plus circle
        double[] Yvector = new double[3];
        Yvector[0] = Yplus_center[0] - Yminus_center[0];
        Yvector[1] = Yplus_center[1] - Yminus_center[1];
        Yvector[2] = Yplus_center[2] - Yminus_center[2];

        //Axis Z--------------------------------------------------------------------------------------------------
        double[] Zplus_center = new double[3];
        //Centers des cercles
        Zplus_center[0] = (double.Parse(textBoxZplus_X_0.Text) + double.Parse(textBoxZplus_X_180.Text)) / 2;
        Zplus_center[1] = (double.Parse(textBoxZplus_Y_0.Text) + double.Parse(textBoxZplus_Y_180.Text)) / 2;
        Zplus_center[2] = (double.Parse(textBoxZplus_Z_0.Text) + double.Parse(textBoxZplus_Z_180.Text)) / 2;
        //Centers des cercles
        double[] Zminus_center = new double[3];
        Zminus_center[0] = (double.Parse(textBoxZminus_X_0.Text) + double.Parse(textBoxZminus_X_180.Text)) / 2;
        Zminus_center[1] = (double.Parse(textBoxZminus_Y_0.Text) + double.Parse(textBoxZminus_Y_180.Text)) / 2;
        Zminus_center[2] = (double.Parse(textBoxZminus_Z_0.Text) + double.Parse(textBoxZminus_Z_180.Text)) / 2;
        //Vector from the center of minus circle to the center of plus circle
        double[] Zvector = new double[3];
        Zvector[0] = Zplus_center[0] - Zminus_center[0];
        Zvector[1] = Zplus_center[1] - Zminus_center[1];
        Zvector[2] = Zplus_center[2] - Zminus_center[2];

        // Rotation matrix--------------------------------------------------------------------------------------
        // rotation_matrix[a][b], a - number of the rows, b - number of the columbs
        rotation_matrix[0] = new double[3];
        rotation_matrix[1] = new double[3];
        rotation_matrix[2] = new double[3];
        //Deviding by main value, for example for X axis - deviding by X coordinate, for Y axis by Y coordinate, for Z axis by Z cordinate
        rotation_matrix[0][0] = Xvector[0] / Xvector[0]; rotation_matrix[0][1] = Yvector[0] / Yvector[1]; rotation_matrix[0][2] = Zvector[0] / Zvector[2];
        rotation_matrix[1][0] = Xvector[1] / Xvector[0]; rotation_matrix[1][1] = Yvector[1] / Yvector[1]; rotation_matrix[1][2] = Zvector[1] / Zvector[2];
        rotation_matrix[2][0] = Xvector[2] / Xvector[0]; rotation_matrix[2][1] = Yvector[2] / Yvector[1]; rotation_matrix[2][2] = Zvector[2] / Zvector[2];
        //Matrix inversion
        rotation_matrix = InvertMatrix(rotation_matrix);

        //determination des centre en multipliant par la matrice de rotation.
        Xplus_center = MatrixVectorMultiply(rotation_matrix, Xplus_center);
        Xminus_center = MatrixVectorMultiply(rotation_matrix, Xminus_center);
        Yplus_center = MatrixVectorMultiply(rotation_matrix, Yplus_center);
        Yminus_center = MatrixVectorMultiply(rotation_matrix, Yminus_center);
        Zplus_center = MatrixVectorMultiply(rotation_matrix, Zplus_center);
        Zminus_center = MatrixVectorMultiply(rotation_matrix, Zminus_center);

        //detrmination du centre de elipsoid---------------------------------------------------------------------------
        double[] center = new double[3];
        center[0] = (Xplus_center[0] + Xminus_center[0] + Yplus_center[0] + Yminus_center[0] + Zplus_center[0] + Zminus_center[0]) / 6;
        center[1] = (Xplus_center[1] + Xminus_center[1] + Yplus_center[1] + Yminus_center[1] + Zplus_center[1] + Zminus_center[1]) / 6;
        center[2] = (Xplus_center[2] + Xminus_center[2] + Yplus_center[2] + Yminus_center[2] + Zplus_center[2] + Zminus_center[2]) / 6;

        //determination du rayon du sphere-----------------------------------------------------------------------
        double x_length = Math.Abs(Xplus_center[0] - Xminus_center[0]) / 2;
        double y_length = Math.Abs(Yplus_center[1] - Yminus_center[1]) / 2;
        double z_length = Math.Abs(Zplus_center[2] - Zminus_center[2]) / 2;
        double[] Xplus_0 = new double[3];
        Xplus_0[0] = double.Parse(textBoxXplus_X_0.Text);
        Xplus_0[1] = double.Parse(textBoxXplus_Y_0.Text);
        Xplus_0[2] = double.Parse(textBoxXplus_Z_0.Text);
        Xplus_0 = MatrixVectorMultiply(rotation_matrix, Xplus_0);
        double[] Yplus_0 = new double[3];
        Yplus_0[0] = double.Parse(textBoxYplus_X_0.Text);
        Yplus_0[1] = double.Parse(textBoxYplus_Y_0.Text);
        Yplus_0[2] = double.Parse(textBoxYplus_Z_0.Text);
        Yplus_0 = MatrixVectorMultiply(rotation_matrix, Yplus_0);
        double[] Zplus_0 = new double[3];
        Zplus_0[0] = double.Parse(textBoxZplus_X_0.Text);
        Zplus_0[1] = double.Parse(textBoxZplus_Y_0.Text);
        Zplus_0[2] = double.Parse(textBoxZplus_Z_0.Text);
        Zplus_0 = MatrixVectorMultiply(rotation_matrix, Zplus_0);
        double x_abs = Math.Sqrt(x_length * x_length + Xplus_0[1] * Xplus_0[1] + Xplus_0[2] * Xplus_0[2]);
        double y_abs = Math.Sqrt(Yplus_0[0] * Yplus_0[0] + y_length * y_length + Yplus_0[2] * Yplus_0[2]);
        double z_abs = Math.Sqrt(Zplus_0[0] * Zplus_0[0] + Zplus_0[1] * Zplus_0[1] + z_length * z_length);
        //sphere radius
        double sphere_radius = (x_abs + y_abs + z_abs) / 3;

        //determination d'echel pour chaque axe------------------------------------------------
        //Diametre du sphere
        double diameter = sphere_radius * 2;
        double kx = Math.Abs(diameter / (Xplus_center[0] - Xminus_center[0]));
        double ky = Math.Abs(diameter / (Yplus_center[1] - Yminus_center[1]));
        double kz = Math.Abs(diameter / (Zplus_center[2] - Zminus_center[2]));

        //multiplication de l'élément de la matrice par l'echelle 
        rotation_matrix[0][0] = rotation_matrix[0][0] * kx; rotation_matrix[0][1] = rotation_matrix[0][1] * ky; rotation_matrix[0][2] = rotation_matrix[0][2] * kz;
        rotation_matrix[1][0] = rotation_matrix[1][0] * kx; rotation_matrix[1][1] = rotation_matrix[1][1] * ky; rotation_matrix[1][2] = rotation_matrix[1][2] * kz;
        rotation_matrix[2][0] = rotation_matrix[2][0] * kx; rotation_matrix[2][1] = rotation_matrix[2][1] * ky; rotation_matrix[2][2] = rotation_matrix[2][2] * kz;
}

0 个答案:

没有答案