diff --git a/src/main/java/com/github/stephengold/joltjni/RMat44.java b/src/main/java/com/github/stephengold/joltjni/RMat44.java index 1687ac71..2be09c91 100644 --- a/src/main/java/com/github/stephengold/joltjni/RMat44.java +++ b/src/main/java/com/github/stephengold/joltjni/RMat44.java @@ -263,6 +263,22 @@ public Vec3 getAxisZ() { return result; } + /** + * Copy the diagonal elements to a {@code Vec3}. The matrix is unaffected. + * + * @return a new vector + */ + @Override + public Vec3 getDiagonal3() { + long matrixVa = va(); + float x = (float) getElement(matrixVa, 0, 0); + float y = (float) getElement(matrixVa, 1, 1); + float z = (float) getElement(matrixVa, 2, 2); + Vec3 result = new Vec3(x, y, z); + + return result; + } + /** * Return the specified element in double precision. The matrix is * unaffected. @@ -328,6 +344,20 @@ public RMat44 inversed() { return result; } + /** + * Return the inverse of the 3x3 portion. The current matrix is unaffected. + * + * @return a new matrix + */ + @Override + public RMat44 inversed3x3() { + long currentVa = va(); + long resultVa = inversed3x3(currentVa); + RMat44 result = new RMat44(resultVa, true); + + return result; + } + /** * Return the inverse of the current matrix, assuming the current matrix * consists entirely of rotation and translation. The current matrix is @@ -335,6 +365,7 @@ public RMat44 inversed() { * * @return a new matrix */ + @Override public RMat44 inversedRotationTranslation() { long currentVa = va(); long resultVa = inversedRotationTranslation(currentVa); @@ -359,6 +390,20 @@ public boolean isEqual(RMat44Arg m2) { return result; } + /** + * Test whether the current matrix is an identity matrix. The matrix is + * unaffected. + * + * @return {@code true} if exactly equal, otherwise {@code false} + */ + @Override + public boolean isIdentity() { + long matrixVa = va(); + boolean result = isIdentity(matrixVa); + + return result; + } + /** * Multiply the current matrix by the argument. The current matrix is * unaffected. @@ -376,6 +421,23 @@ public RMat44 multiply(RMat44Arg right) { return result; } + /** + * Multiply the current 3x3 matrix by the specified 3x3 matrix. The current + * matrix is unaffected. + * + * @param right the right factor (not null, unaffected) + * @return a new matrix + */ + @Override + public RMat44 multiply3x3(RMat44Arg right) { + long leftVa = va(); + long rightVa = right.targetVa(); + long productVa = multiply3x3(leftVa, rightVa); + RMat44 result = new RMat44(productVa, true); + + return result; + } + /** * Multiply the 3x3 matrix by the specified column vector. The matrix is * unaffected. @@ -449,12 +511,32 @@ public RVec3 multiply3x4(Vec3Arg vec3) { return result; } + /** + * Post multiply by the specified translation vector. The current matrix is + * unaffected. + * + * @param rvec3 the left factor (not null, unaffected) + * @return a new matrix + */ + @Override + public RMat44 postTranslated(RVec3Arg rvec3) { + long matrixVa = va(); + double xx = rvec3.xx(); + double yy = rvec3.yy(); + double zz = rvec3.zz(); + long resultVa = postTranslated(matrixVa, xx, yy, zz); + RMat44 result = new RMat44(resultVa, true); + + return result; + } + /** * Copy the current matrix to a new, single-precision matrix. The current * matrix is unaffected. * * @return the new matrix */ + @Override public Mat44 toMat44() { Mat44 result = new Mat44(this); return result; @@ -535,12 +617,18 @@ native private static void getQuaternion( native private static long inversed(long currentVa); + native private static long inversed3x3(long currentVa); + native private static long inversedRotationTranslation(long currentVa); + native private static boolean isIdentity(long matrixVa); + native private static long multiply(long leftVa, long rightVa); native private static void multiply3x3(long matrixVa, float[] tmpFloats); + native private static long multiply3x3(long leftVa, long rightVa); + native private static void multiply3x3Transposed( long matrixVa, float[] tmpFloats); @@ -549,6 +637,9 @@ native private static void multiply3x4( native private static void multiply3x4r(long matrixVa, double[] tmpDoubles); + native private static long postTranslated( + long matrixVa, double xx, double yy, double zz); + native private static void setElement( long matrixVa, int row, int column, double value); diff --git a/src/main/java/com/github/stephengold/joltjni/readonly/RMat44Arg.java b/src/main/java/com/github/stephengold/joltjni/readonly/RMat44Arg.java index f9b9c9e7..a693f69e 100644 --- a/src/main/java/com/github/stephengold/joltjni/readonly/RMat44Arg.java +++ b/src/main/java/com/github/stephengold/joltjni/readonly/RMat44Arg.java @@ -21,6 +21,7 @@ of this software and associated documentation files (the "Software"), to deal */ package com.github.stephengold.joltjni.readonly; +import com.github.stephengold.joltjni.Mat44; import com.github.stephengold.joltjni.Quat; import com.github.stephengold.joltjni.RMat44; import com.github.stephengold.joltjni.RVec3; @@ -56,6 +57,13 @@ public interface RMat44Arg extends ConstJoltPhysicsObject { */ Vec3 getAxisZ(); + /** + * Copy the diagonal elements to a {@code Vec3}. The matrix is unaffected. + * + * @return a new vector + */ + Vec3 getDiagonal3(); + /** * Return the specified element in double precision. The matrix is * unaffected. @@ -87,6 +95,22 @@ public interface RMat44Arg extends ConstJoltPhysicsObject { */ RMat44 inversed(); + /** + * Return the inverse of the 3x3 portion. The current matrix is unaffected. + * + * @return a new matrix + */ + RMat44 inversed3x3(); + + /** + * Return the inverse of the current matrix, assuming the current matrix + * consists entirely of rotation and translation. The current matrix is + * unaffected. + * + * @return a new matrix + */ + RMat44 inversedRotationTranslation(); + /** * Test whether the current matrix is equal to the argument. The current * matrix is unaffected. @@ -96,6 +120,14 @@ public interface RMat44Arg extends ConstJoltPhysicsObject { */ boolean isEqual(RMat44Arg m2); + /** + * Test whether the current matrix is an identity matrix. The matrix is + * unaffected. + * + * @return {@code true} if exactly equal, otherwise {@code false} + */ + boolean isIdentity(); + /** * Multiply the current matrix by the argument. The current matrix is * unaffected. @@ -105,6 +137,15 @@ public interface RMat44Arg extends ConstJoltPhysicsObject { */ RMat44 multiply(RMat44Arg right); + /** + * Multiply the current 3x3 matrix by the specified 3x3 matrix. The current + * matrix is unaffected. + * + * @param right the right factor (not null, unaffected) + * @return a new matrix + */ + RMat44 multiply3x3(RMat44Arg right); + /** * Multiply the 3x3 matrix by the specified column vector. The matrix is * unaffected. @@ -142,4 +183,21 @@ public interface RMat44Arg extends ConstJoltPhysicsObject { * @return a new vector */ RVec3 multiply3x4(Vec3Arg vec3); + + /** + * Post multiply by the specified translation vector. The current matrix is + * unaffected. + * + * @param rvec3 the left factor (not null, unaffected) + * @return a new matrix + */ + RMat44 postTranslated(RVec3Arg rvec3); + + /** + * Copy the current matrix to a new, single-precision matrix. The current + * matrix is unaffected. + * + * @return the new matrix + */ + Mat44 toMat44(); } diff --git a/src/main/native/glue/r/RMat44.cpp b/src/main/native/glue/r/RMat44.cpp index 15689fd4..d3ff028c 100644 --- a/src/main/native/glue/r/RMat44.cpp +++ b/src/main/native/glue/r/RMat44.cpp @@ -270,6 +270,20 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_inversed return reinterpret_cast (pResult); } +/* + * Class: com_github_stephengold_joltjni_RMat44 + * Method: inversed3x3 + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_inversed3x3 + (JNIEnv *, jclass, jlong currentVa) { + const RMat44 * const pCurrent = reinterpret_cast (currentVa); + RMat44 * const pResult = new RMat44(); + TRACE_NEW("RMat44", pResult) + *pResult = pCurrent->Inversed3x3(); + return reinterpret_cast (pResult); +} + /* * Class: com_github_stephengold_joltjni_RMat44 * Method: inversedRotationTranslation @@ -284,6 +298,18 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_inversedRotat return reinterpret_cast (pResult); } +/* + * Class: com_github_stephengold_joltjni_RMat44 + * Method: isIdentity + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_RMat44_isIdentity + (JNIEnv *, jclass, jlong matrixVa) { + const RMat44 * const pMatrix = reinterpret_cast (matrixVa); + const bool result = (*pMatrix == RMat44::sIdentity()); + return result; +} + /* * Class: com_github_stephengold_joltjni_RMat44 * Method: multiply @@ -304,7 +330,7 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply * Method: multiply3x3 * Signature: (J[F)V */ -JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply3x3 +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply3x3__J_3F (JNIEnv *pEnv, jclass, jlong matrixVa, jfloatArray tmpFloats) { const RMat44 * const pMatrix = reinterpret_cast (matrixVa); jboolean isCopy; @@ -318,6 +344,21 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply3x3 pEnv->ReleaseFloatArrayElements(tmpFloats, pTmpFloats, 0); } +/* + * Class: com_github_stephengold_joltjni_RMat44 + * Method: multiply3x3 + * Signature: (JJ)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply3x3__JJ + (JNIEnv *, jclass, jlong leftVa, jlong rightVa) { + const RMat44 * const pLeft = reinterpret_cast (leftVa); + const RMat44 * const pRight = reinterpret_cast (rightVa); + RMat44 * const pResult = new RMat44(); + TRACE_NEW("RMat44", pResult) + *pResult = pLeft->Multiply3x3(*pRight); + return reinterpret_cast (pResult); +} + /* * Class: com_github_stephengold_joltjni_RMat44 * Method: multiply3x3Transposed @@ -374,6 +415,21 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_multiply3x4r pEnv->ReleaseDoubleArrayElements(tmpDoubles, pTmpDoubles, 0); } +/* + * Class: com_github_stephengold_joltjni_RMat44 + * Method: postTranslated + * Signature: (JDDD)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_postTranslated + (JNIEnv *, jclass, jlong matrixVa, jdouble xx, jdouble yy, jdouble zz) { + const RMat44 * const pMatrix = reinterpret_cast (matrixVa); + const RVec3 offset(xx, yy, zz); + RMat44 * const pResult = new RMat44(); + TRACE_NEW("RMat44", pResult) + *pResult = pMatrix->PostTranslated(offset); + return reinterpret_cast (pResult); +} + /* * Class: com_github_stephengold_joltjni_RMat44 * Method: setElement