Skip to content

Commit

Permalink
Add Matrix3f.rotateLocalX/Y/Z()
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Apr 22, 2017
1 parent 490f06a commit 14f8764
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 0 deletions.
234 changes: 234 additions & 0 deletions src/org/joml/Matrix3f.java
Expand Up @@ -248,6 +248,18 @@ public Matrix3f rotateLocal(float ang, float x, float y, float z, Matrix3f dest)
return delegate.rotateLocal(ang, x, y, z, dest);
}

public Matrix3f rotateLocalX(float ang, Matrix3f dest) {
return delegate.rotateLocalX(ang, dest);
}

public Matrix3f rotateLocalY(float ang, Matrix3f dest) {
return delegate.rotateLocalY(ang, dest);
}

public Matrix3f rotateLocalZ(float ang, Matrix3f dest) {
return delegate.rotateLocalZ(ang, dest);
}

public Matrix3f rotate(Quaternionfc quat, Matrix3f dest) {
return delegate.rotate(quat, dest);
}
Expand Down Expand Up @@ -2525,6 +2537,228 @@ public Matrix3f rotateLocal(float ang, float x, float y, float z) {
return rotateLocal(ang, x, y, z, this);
}

/**
* Pre-multiply a rotation around the X axis to this matrix by rotating the given amount of radians
* about the X axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationX(float) rotationX()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationX(float)
*
* @param ang
* the angle in radians to rotate about the X axis
* @param dest
* will hold the result
* @return dest
*/
public Matrix3f rotateLocalX(float ang, Matrix3f dest) {
float sin = (float) Math.sin(ang);
float cos = (float) Math.cosFromSin(sin, ang);
float nm01 = cos * m01 - sin * m02;
float nm02 = sin * m01 + cos * m02;
float nm11 = cos * m11 - sin * m12;
float nm12 = sin * m11 + cos * m12;
float nm21 = cos * m21 - sin * m22;
float nm22 = sin * m21 + cos * m22;
dest.m00 = m00;
dest.m01 = nm01;
dest.m02 = nm02;
dest.m10 = m10;
dest.m11 = nm11;
dest.m12 = nm12;
dest.m20 = m20;
dest.m21 = nm21;
dest.m22 = nm22;
return dest;
}

/**
* Pre-multiply a rotation to this matrix by rotating the given amount of radians about the X axis.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationX(float) rotationX()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationX(float)
*
* @param ang
* the angle in radians to rotate about the X axis
* @return this
*/
public Matrix3f rotateLocalX(float ang) {
return rotateLocalX(ang, this);
}

/**
* Pre-multiply a rotation around the Y axis to this matrix by rotating the given amount of radians
* about the Y axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationY(float) rotationY()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationY(float)
*
* @param ang
* the angle in radians to rotate about the Y axis
* @param dest
* will hold the result
* @return dest
*/
public Matrix3f rotateLocalY(float ang, Matrix3f dest) {
float sin = (float) Math.sin(ang);
float cos = (float) Math.cosFromSin(sin, ang);
float nm00 = cos * m00 + sin * m02;
float nm02 = -sin * m00 + cos * m02;
float nm10 = cos * m10 + sin * m12;
float nm12 = -sin * m10 + cos * m12;
float nm20 = cos * m20 + sin * m22;
float nm22 = -sin * m20 + cos * m22;
dest.m00 = nm00;
dest.m01 = m01;
dest.m02 = nm02;
dest.m10 = nm10;
dest.m11 = m11;
dest.m12 = nm12;
dest.m20 = nm20;
dest.m21 = m21;
dest.m22 = nm22;
return dest;
}

/**
* Pre-multiply a rotation to this matrix by rotating the given amount of radians about the Y axis.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationY(float) rotationY()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationY(float)
*
* @param ang
* the angle in radians to rotate about the Y axis
* @return this
*/
public Matrix3f rotateLocalY(float ang) {
return rotateLocalY(ang, this);
}

/**
* Pre-multiply a rotation around the Z axis to this matrix by rotating the given amount of radians
* about the Z axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationZ(float) rotationZ()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationZ(float)
*
* @param ang
* the angle in radians to rotate about the Z axis
* @param dest
* will hold the result
* @return dest
*/
public Matrix3f rotateLocalZ(float ang, Matrix3f dest) {
float sin = (float) Math.sin(ang);
float cos = (float) Math.cosFromSin(sin, ang);
float nm00 = cos * m00 - sin * m01;
float nm01 = sin * m00 + cos * m01;
float nm10 = cos * m10 - sin * m11;
float nm11 = sin * m10 + cos * m11;
float nm20 = cos * m20 - sin * m21;
float nm21 = sin * m20 + cos * m21;
dest.m00 = nm00;
dest.m01 = nm01;
dest.m02 = m02;
dest.m10 = nm10;
dest.m11 = nm11;
dest.m12 = m12;
dest.m20 = nm20;
dest.m21 = nm21;
dest.m22 = m22;
return dest;
}

/**
* Pre-multiply a rotation to this matrix by rotating the given amount of radians about the Z axis.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* In order to set the matrix to a rotation matrix without pre-multiplying the rotation
* transformation, use {@link #rotationZ(float) rotationY()}.
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @see #rotationY(float)
*
* @param ang
* the angle in radians to rotate about the Z axis
* @return this
*/
public Matrix3f rotateLocalZ(float ang) {
return rotateLocalZ(ang, this);
}

/**
* Apply the rotation - and possibly scaling - transformation of the given {@link Quaternionfc} to this matrix.
* <p>
Expand Down
69 changes: 69 additions & 0 deletions src/org/joml/Matrix3fc.java
Expand Up @@ -736,6 +736,75 @@ public interface Matrix3fc {
*/
Matrix3f rotateLocal(float ang, float x, float y, float z, Matrix3f dest);

/**
* Pre-multiply a rotation around the X axis to this matrix by rotating the given amount of radians
* about the X axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @param ang
* the angle in radians to rotate about the X axis
* @param dest
* will hold the result
* @return dest
*/
Matrix3f rotateLocalX(float ang, Matrix3f dest);

/**
* Pre-multiply a rotation around the Y axis to this matrix by rotating the given amount of radians
* about the Y axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @param ang
* the angle in radians to rotate about the Y axis
* @param dest
* will hold the result
* @return dest
*/
Matrix3f rotateLocalY(float ang, Matrix3f dest);

/**
* Pre-multiply a rotation around the Z axis to this matrix by rotating the given amount of radians
* about the Z axis and store the result in <code>dest</code>.
* <p>
* When used with a right-handed coordinate system, the produced rotation will rotate a vector
* counter-clockwise around the rotation axis, when viewing along the negative axis direction towards the origin.
* When used with a left-handed coordinate system, the rotation is clockwise.
* <p>
* If <code>M</code> is <code>this</code> matrix and <code>R</code> the rotation matrix,
* then the new matrix will be <code>R * M</code>. So when transforming a
* vector <code>v</code> with the new matrix by using <code>R * M * v</code>, the
* rotation will be applied last!
* <p>
* Reference: <a href="http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle">http://en.wikipedia.org</a>
*
* @param ang
* the angle in radians to rotate about the Z axis
* @param dest
* will hold the result
* @return dest
*/
Matrix3f rotateLocalZ(float ang, Matrix3f dest);

/**
* Apply the rotation - and possibly scaling - transformation of the given {@link Quaternionfc} to this matrix and store
* the result in <code>dest</code>.
Expand Down

0 comments on commit 14f8764

Please sign in to comment.