Skip to content

Commit

Permalink
Add Quaternion.getEulerAnglesZYX()
Browse files Browse the repository at this point in the history
and simplify Quaternion.getEulerAnglesXYZ()
  • Loading branch information
httpdigest committed Dec 4, 2021
1 parent 1587fbc commit 6fe925b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/org/joml/Quaterniond.java
Original file line number Diff line number Diff line change
Expand Up @@ -2762,9 +2762,16 @@ public Quaterniond rotateYXZ(double angleY, double angleX, double angleZ, Quater
}

public Vector3d getEulerAnglesXYZ(Vector3d eulerAngles) {
eulerAngles.x = Math.atan2(2.0 * (x*w - y*z), 1.0 - 2.0 * (x*x + y*y));
eulerAngles.y = Math.safeAsin(2.0 * (x*z + y*w));
eulerAngles.z = Math.atan2(2.0 * (z*w - x*y), 1.0 - 2.0 * (y*y + z*z));
eulerAngles.x = Math.atan2(x * w - y * z, 0.5 - x * x - y * y);
eulerAngles.y = Math.safeAsin(2.0 * (x * z + y * w));
eulerAngles.z = Math.atan2(z * w - x * y, 0.5 - y * y - z * z);
return eulerAngles;
}

public Vector3d getEulerAnglesZYX(Vector3d eulerAngles) {
eulerAngles.x = Math.atan2(y * z + w * x, 0.5 - x * x + y * y);
eulerAngles.y = Math.safeAsin(-2.0 * (x * z - w * y));
eulerAngles.z = Math.atan2(x * y + w * z, 0.5 - y * y - z * z);
return eulerAngles;
}

Expand Down
16 changes: 16 additions & 0 deletions src/org/joml/Quaterniondc.java
Original file line number Diff line number Diff line change
Expand Up @@ -1751,13 +1751,29 @@ public interface Quaterniondc {
/**
* Get the euler angles in radians in rotation sequence <code>XYZ</code> of this quaternion and store them in the
* provided parameter <code>eulerAngles</code>.
* <p>
* The Euler angles are always returned as the angle around X in the {@link Vector3d#x} field, the angle around Y in the {@link Vector3d#y}
* field and the angle around Z in the {@link Vector3d#z} field of the supplied {@link Vector3d} instance.
*
* @param eulerAngles
* will hold the euler angles in radians
* @return the passed in vector
*/
Vector3d getEulerAnglesXYZ(Vector3d eulerAngles);

/**
* Get the euler angles in radians in rotation sequence <code>ZYX</code> of this quaternion and store them in the
* provided parameter <code>eulerAngles</code>.
* <p>
* The Euler angles are always returned as the angle around X in the {@link Vector3d#x} field, the angle around Y in the {@link Vector3d#y}
* field and the angle around Z in the {@link Vector3d#z} field of the supplied {@link Vector3d} instance.
*
* @param eulerAngles
* will hold the euler angles in radians
* @return the passed in vector
*/
Vector3d getEulerAnglesZYX(Vector3d eulerAngles);

/**
* Apply a rotation to <code>this</code> quaternion rotating the given radians about the specified axis
* and store the result in <code>dest</code>.
Expand Down
13 changes: 10 additions & 3 deletions src/org/joml/Quaternionf.java
Original file line number Diff line number Diff line change
Expand Up @@ -1853,9 +1853,16 @@ public Quaternionf rotateYXZ(float angleY, float angleX, float angleZ, Quaternio
}

public Vector3f getEulerAnglesXYZ(Vector3f eulerAngles) {
eulerAngles.x = Math.atan2(2.0f * (x*w - y*z), 1.0f - 2.0f * (x*x + y*y));
eulerAngles.y = Math.safeAsin(2.0f * (x*z + y*w));
eulerAngles.z = Math.atan2(2.0f * (z*w - x*y), 1.0f - 2.0f * (y*y + z*z));
eulerAngles.x = Math.atan2(x * w - y * z, 0.5f - x * x - y * y);
eulerAngles.y = Math.safeAsin(2.0f * (x * z + y * w));
eulerAngles.z = Math.atan2(z * w - x * y, 0.5f - y * y - z * z);
return eulerAngles;
}

public Vector3f getEulerAnglesZYX(Vector3f eulerAngles) {
eulerAngles.x = Math.atan2(y * z + w * x, 0.5f - x * x + y * y);
eulerAngles.y = Math.safeAsin(-2.0f * (x * z - w * y));
eulerAngles.z = Math.atan2(x * y + w * z, 0.5f - y * y - z * z);
return eulerAngles;
}

Expand Down
16 changes: 16 additions & 0 deletions src/org/joml/Quaternionfc.java
Original file line number Diff line number Diff line change
Expand Up @@ -1557,13 +1557,29 @@ public interface Quaternionfc {
/**
* Get the euler angles in radians in rotation sequence <code>XYZ</code> of this quaternion and store them in the
* provided parameter <code>eulerAngles</code>.
* <p>
* The Euler angles are always returned as the angle around X in the {@link Vector3f#x} field, the angle around Y in the {@link Vector3f#y}
* field and the angle around Z in the {@link Vector3f#z} field of the supplied {@link Vector3f} instance.
*
* @param eulerAngles
* will hold the euler angles in radians
* @return the passed in vector
*/
Vector3f getEulerAnglesXYZ(Vector3f eulerAngles);

/**
* Get the euler angles in radians in rotation sequence <code>ZYX</code> of this quaternion and store them in the
* provided parameter <code>eulerAngles</code>.
* <p>
* The Euler angles are always returned as the angle around X in the {@link Vector3f#x} field, the angle around Y in the {@link Vector3f#y}
* field and the angle around Z in the {@link Vector3f#z} field of the supplied {@link Vector3f} instance.
*
* @param eulerAngles
* will hold the euler angles in radians
* @return the passed in vector
*/
Vector3f getEulerAnglesZYX(Vector3f eulerAngles);

/**
* Return the square of the length of this quaternion.
*
Expand Down

0 comments on commit 6fe925b

Please sign in to comment.