📄 rotation.java
字号:
Math.atan2(v2.getY(), v2.getZ()) }; } else if (order == RotationOrder.XYX) { // r (Vector3D.plusI) coordinates are : // cos (theta), sin (phi1) sin (theta), -cos (phi1) sin (theta) // (-r) (Vector3D.plusI) coordinates are : // cos (theta), sin (theta) sin (phi2), sin (theta) cos (phi2) // and we can choose to have theta in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusI); Vector3D v2 = applyInverseTo(Vector3D.plusI); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getY(), -v1.getZ()), Math.acos(v2.getX()), Math.atan2(v2.getY(), v2.getZ()) }; } else if (order == RotationOrder.XZX) { // r (Vector3D.plusI) coordinates are : // cos (psi), cos (phi1) sin (psi), sin (phi1) sin (psi) // (-r) (Vector3D.plusI) coordinates are : // cos (psi), -sin (psi) cos (phi2), sin (psi) sin (phi2) // and we can choose to have psi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusI); Vector3D v2 = applyInverseTo(Vector3D.plusI); if ((v2.getX() < -0.9999999999) || (v2.getX() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getZ(), v1.getY()), Math.acos(v2.getX()), Math.atan2(v2.getZ(), -v2.getY()) }; } else if (order == RotationOrder.YXY) { // r (Vector3D.plusJ) coordinates are : // sin (theta1) sin (phi), cos (phi), cos (theta1) sin (phi) // (-r) (Vector3D.plusJ) coordinates are : // sin (phi) sin (theta2), cos (phi), -sin (phi) cos (theta2) // and we can choose to have phi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusJ); Vector3D v2 = applyInverseTo(Vector3D.plusJ); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getX(), v1.getZ()), Math.acos(v2.getY()), Math.atan2(v2.getX(), -v2.getZ()) }; } else if (order == RotationOrder.YZY) { // r (Vector3D.plusJ) coordinates are : // -cos (theta1) sin (psi), cos (psi), sin (theta1) sin (psi) // (-r) (Vector3D.plusJ) coordinates are : // sin (psi) cos (theta2), cos (psi), sin (psi) sin (theta2) // and we can choose to have psi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusJ); Vector3D v2 = applyInverseTo(Vector3D.plusJ); if ((v2.getY() < -0.9999999999) || (v2.getY() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getZ(), -v1.getX()), Math.acos(v2.getY()), Math.atan2(v2.getZ(), v2.getX()) }; } else if (order == RotationOrder.ZXZ) { // r (Vector3D.plusK) coordinates are : // sin (psi1) sin (phi), -cos (psi1) sin (phi), cos (phi) // (-r) (Vector3D.plusK) coordinates are : // sin (phi) sin (psi2), sin (phi) cos (psi2), cos (phi) // and we can choose to have phi in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusK); Vector3D v2 = applyInverseTo(Vector3D.plusK); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getX(), -v1.getY()), Math.acos(v2.getZ()), Math.atan2(v2.getX(), v2.getY()) }; } else { // last possibility is ZYZ // r (Vector3D.plusK) coordinates are : // cos (psi1) sin (theta), sin (psi1) sin (theta), cos (theta) // (-r) (Vector3D.plusK) coordinates are : // -sin (theta) cos (psi2), sin (theta) sin (psi2), cos (theta) // and we can choose to have theta in the interval [0 ; PI] Vector3D v1 = applyTo(Vector3D.plusK); Vector3D v2 = applyInverseTo(Vector3D.plusK); if ((v2.getZ() < -0.9999999999) || (v2.getZ() > 0.9999999999)) { throw new CardanEulerSingularityException(false); } return new double[] { Math.atan2(v1.getY(), v1.getX()), Math.acos(v2.getZ()), Math.atan2(v2.getY(), -v2.getX()) }; } } /** Get the 3X3 matrix corresponding to the instance * @return the matrix corresponding to the instance */ public double[][] getMatrix() { // products double q0q0 = q0 * q0; double q0q1 = q0 * q1; double q0q2 = q0 * q2; double q0q3 = q0 * q3; double q1q1 = q1 * q1; double q1q2 = q1 * q2; double q1q3 = q1 * q3; double q2q2 = q2 * q2; double q2q3 = q2 * q3; double q3q3 = q3 * q3; // create the matrix double[][] m = new double[3][]; m[0] = new double[3]; m[1] = new double[3]; m[2] = new double[3]; m [0][0] = 2.0 * (q0q0 + q1q1) - 1.0; m [1][0] = 2.0 * (q1q2 - q0q3); m [2][0] = 2.0 * (q1q3 + q0q2); m [0][1] = 2.0 * (q1q2 + q0q3); m [1][1] = 2.0 * (q0q0 + q2q2) - 1.0; m [2][1] = 2.0 * (q2q3 - q0q1); m [0][2] = 2.0 * (q1q3 - q0q2); m [1][2] = 2.0 * (q2q3 + q0q1); m [2][2] = 2.0 * (q0q0 + q3q3) - 1.0; return m; } /** Apply the rotation to a vector. * @param u vector to apply the rotation to * @return a new vector which is the image of u by the rotation */ public Vector3D applyTo(Vector3D u) { double x = u.getX(); double y = u.getY(); double z = u.getZ(); double s = q1 * x + q2 * y + q3 * z; return new Vector3D(2 * (q0 * (x * q0 - (q2 * z - q3 * y)) + s * q1) - x, 2 * (q0 * (y * q0 - (q3 * x - q1 * z)) + s * q2) - y, 2 * (q0 * (z * q0 - (q1 * y - q2 * x)) + s * q3) - z); } /** Apply the inverse of the rotation to a vector. * @param u vector to apply the inverse of the rotation to * @return a new vector which such that u is its image by the rotation */ public Vector3D applyInverseTo(Vector3D u) { double x = u.getX(); double y = u.getY(); double z = u.getZ(); double s = q1 * x + q2 * y + q3 * z; double m0 = -q0; return new Vector3D(2 * (m0 * (x * m0 - (q2 * z - q3 * y)) + s * q1) - x, 2 * (m0 * (y * m0 - (q3 * x - q1 * z)) + s * q2) - y, 2 * (m0 * (z * m0 - (q1 * y - q2 * x)) + s * q3) - z); } /** Apply the instance to another rotation. * Applying the instance to a rotation is computing the composition * in an order compliant with the following rule : let u be any * vector and v its image by r (i.e. r.applyTo(u) = v), let w be the image * of v by the instance (i.e. applyTo(v) = w), then w = comp.applyTo(u), * where comp = applyTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the instance */ public Rotation applyTo(Rotation r) { return new Rotation(r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), false); } /** Apply the inverse of the instance to another rotation. * Applying the inverse of the instance to a rotation is computing * the composition in an order compliant with the following rule : * let u be any vector and v its image by r (i.e. r.applyTo(u) = v), * let w be the inverse image of v by the instance * (i.e. applyInverseTo(v) = w), then w = comp.applyTo(u), where * comp = applyInverseTo(r). * @param r rotation to apply the rotation to * @return a new rotation which is the composition of r by the inverse * of the instance */ public Rotation applyInverseTo(Rotation r) { return new Rotation(-r.q0 * q0 - (r.q1 * q1 + r.q2 * q2 + r.q3 * q3), -r.q1 * q0 + r.q0 * q1 + (r.q2 * q3 - r.q3 * q2), -r.q2 * q0 + r.q0 * q2 + (r.q3 * q1 - r.q1 * q3), -r.q3 * q0 + r.q0 * q3 + (r.q1 * q2 - r.q2 * q1), false); } /** Perfect orthogonality on a 3X3 matrix. * @param m initial matrix (not exactly orthogonal) * @param threshold convergence threshold for the iterative * orthogonality correction (convergence is reached when the * difference between two steps of the Frobenius norm of the * correction is below this threshold) * @return an orthogonal matrix close to m * @exception NotARotationMatrixException if the matrix cannot be * orthogonalized with the given threshold after 10 iterations */ private double[][] orthogonalizeMatrix(double[][] m, double threshold) throws NotARotationMatrixException { double[] m0 = m[0]; double[] m1 = m[1]; double[] m2 = m[2]; double x00 = m0[0]; double x01 = m0[1]; double x02 = m0[2]; double x10 = m1[0]; double x11 = m1[1]; double x12 = m1[2]; double x20 = m2[0]; double x21 = m2[1]; double x22 = m2[2]; double fn = 0; double fn1; double[][] o = new double[3][3]; double[] o0 = o[0]; double[] o1 = o[1]; double[] o2 = o[2]; // iterative correction: Xn+1 = Xn - 0.5 * (Xn.Mt.Xn - M) int i = 0; while (++i < 11) { // Mt.Xn double mx00 = m0[0] * x00 + m1[0] * x10 + m2[0] * x20; double mx10 = m0[1] * x00 + m1[1] * x10 + m2[1] * x20; double mx20 = m0[2] * x00 + m1[2] * x10 + m2[2] * x20; double mx01 = m0[0] * x01 + m1[0] * x11 + m2[0] * x21; double mx11 = m0[1] * x01 + m1[1] * x11 + m2[1] * x21; double mx21 = m0[2] * x01 + m1[2] * x11 + m2[2] * x21; double mx02 = m0[0] * x02 + m1[0] * x12 + m2[0] * x22; double mx12 = m0[1] * x02 + m1[1] * x12 + m2[1] * x22; double mx22 = m0[2] * x02 + m1[2] * x12 + m2[2] * x22; // Xn+1 o0[0] = x00 - 0.5 * (x00 * mx00 + x01 * mx10 + x02 * mx20 - m0[0]); o0[1] = x01 - 0.5 * (x00 * mx01 + x01 * mx11 + x02 * mx21 - m0[1]); o0[2] = x02 - 0.5 * (x00 * mx02 + x01 * mx12 + x02 * mx22 - m0[2]); o1[0] = x10 - 0.5 * (x10 * mx00 + x11 * mx10 + x12 * mx20 - m1[0]); o1[1] = x11 - 0.5 * (x10 * mx01 + x11 * mx11 + x12 * mx21 - m1[1]); o1[2] = x12 - 0.5 * (x10 * mx02 + x11 * mx12 + x12 * mx22 - m1[2]); o2[0] = x20 - 0.5 * (x20 * mx00 + x21 * mx10 + x22 * mx20 - m2[0]); o2[1] = x21 - 0.5 * (x20 * mx01 + x21 * mx11 + x22 * mx21 - m2[1]); o2[2] = x22 - 0.5 * (x20 * mx02 + x21 * mx12 + x22 * mx22 - m2[2]); // correction on each elements double corr00 = o0[0] - m0[0]; double corr01 = o0[1] - m0[1]; double corr02 = o0[2] - m0[2]; double corr10 = o1[0] - m1[0]; double corr11 = o1[1] - m1[1]; double corr12 = o1[2] - m1[2]; double corr20 = o2[0] - m2[0]; double corr21 = o2[1] - m2[1]; double corr22 = o2[2] - m2[2]; // Frobenius norm of the correction fn1 = corr00 * corr00 + corr01 * corr01 + corr02 * corr02 + corr10 * corr10 + corr11 * corr11 + corr12 * corr12 + corr20 * corr20 + corr21 * corr21 + corr22 * corr22; // convergence test if (Math.abs(fn1 - fn) <= threshold) return o; // prepare next iteration x00 = o0[0]; x01 = o0[1]; x02 = o0[2]; x10 = o1[0]; x11 = o1[1]; x12 = o1[2]; x20 = o2[0]; x21 = o2[1]; x22 = o2[2]; fn = fn1; } // the algorithm did not converge after 10 iterations throw new NotARotationMatrixException("unable to orthogonalize matrix" + " in {0} iterations", new Object[] { Integer.toString(i - 1) }); } /** Scalar coordinate of the quaternion. */ private final double q0; /** First coordinate of the vectorial part of the quaternion. */ private final double q1; /** Second coordinate of the vectorial part of the quaternion. */ private final double q2; /** Third coordinate of the vectorial part of the quaternion. */ private final double q3; /** Serializable version identifier */ private static final long serialVersionUID = 8225864499430109352L;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -