Skip to content

Commit

Permalink
RMat44: add a copy constructor and 7 public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 23, 2025
1 parent a00c48a commit 2afcf89
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
119 changes: 119 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/RMat44.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ public RMat44(Mat44Arg spMatrix) {
setVirtualAddress(matrixVa, () -> free(matrixVa));
}

/**
* Instantiate a copy of the specified matrix.
*
* @param original the matrix to duplicate (not null, unaffected)
*/
public RMat44(RMat44Arg original) {
long originalVa = original.targetVa();
long matrixVa = createCopy(originalVa);
setVirtualAddress(matrixVa, () -> free(matrixVa));
}

/**
* Instantiate a matrix with the specified columns.
*
Expand All @@ -94,6 +105,29 @@ public RMat44(Vec4Arg c1, Vec4Arg c2, Vec4Arg c3, RVec3Arg c4) {
// *************************************************************************
// new methods exposed

/**
* Set the current matrix to identity.
*/
public void loadIdentity() {
long matrixVa = va();
loadIdentity(matrixVa);
}

/**
* Return the product of the specified matrices.
*
* @param mArray an array of input matrices (not null, unaffected)
* @return a new matrix
*/
public static RMat44 product(RMat44Arg... mArray) {
RMat44 result = RMat44.sIdentity();
for (RMat44Arg arg : mArray) {
result.multiply(arg);
}

return result;
}

/**
* Copy all elements of the argument to the current matrix.
*
Expand All @@ -105,6 +139,45 @@ public void set(RMat44Arg source) {
assign(targetVa, sourceVa);
}

/**
* Set the first column to the specified vector.
*
* @param vec the vector to use (not null, unaffected)
*/
public void setAxisX(Vec3Arg vec) {
long matrixVa = va();
float x = vec.getX();
float y = vec.getY();
float z = vec.getZ();
setAxisX(matrixVa, x, y, z);
}

/**
* Set the 2nd column to the specified vector.
*
* @param vec the vector to use (not null, unaffected)
*/
public void setAxisY(Vec3Arg vec) {
long matrixVa = va();
float x = vec.getX();
float y = vec.getY();
float z = vec.getZ();
setAxisY(matrixVa, x, y, z);
}

/**
* Set the 3rd column to the specified vector.
*
* @param vec the vector to use (not null, unaffected)
*/
public void setAxisZ(Vec3Arg vec) {
long matrixVa = va();
float x = vec.getX();
float y = vec.getY();
float z = vec.getZ();
setAxisZ(matrixVa, x, y, z);
}

/**
* Alter the specified element in double precision.
*
Expand Down Expand Up @@ -184,6 +257,37 @@ public static RMat44 sRotationTranslation(
return result;
}

/**
* Create a uniform scaling matrix.
*
* @param factor the amount to scale each axis
* @return a new instance
*
*/
public static RMat44 sScale(float factor) {
long matrixVa = createScale(factor, factor, factor);
RMat44 result = new RMat44(matrixVa, true);

return result;
}

/**
* Create a pure scaling matrix.
*
* @param factors the amount to scale each axis (not null, unaffected)
* @return a new instance
*
*/
public static RMat44 sScale(Vec3Arg factors) {
float x = factors.getX();
float y = factors.getY();
float z = factors.getZ();
long matrixVa = createScale(x, y, z);
RMat44 result = new RMat44(matrixVa, true);

return result;
}

/**
* Create a pure translation matrix.
*
Expand Down Expand Up @@ -549,6 +653,8 @@ public String toString() {

native private static void assign(long targetVa, long sourceVa);

native private static long createCopy(long originalVa);

native private static long createFromRowMajor(
float[] floatArray, double m14, double m24, double m34);

Expand All @@ -562,6 +668,8 @@ native private static long createRotation(
native private static long createRotationTranslation(float qx, float qy,
float qz, float qw, double xx, double yy, double zz);

native private static long createScale(float x, float y, float z);

native private static long createTranslation(
double xx, double yy, double zz);

Expand Down Expand Up @@ -590,6 +698,8 @@ native private static void getQuaternion(

native private static boolean isIdentity(long matrixVa);

native private static void loadIdentity(long matrixVa);

native private static long multiply(long leftVa, long rightVa);

native private static void multiply3x3(long matrixVa, float[] tmpFloats);
Expand All @@ -605,6 +715,15 @@ native private static void multiply3x4(
native private static long postTranslated(
long matrixVa, double xx, double yy, double zz);

native private static void setAxisX(
long matrixVa, float x, float y, float z);

native private static void setAxisY(
long matrixVa, float x, float y, float z);

native private static void setAxisZ(
long matrixVa, float x, float y, float z);

native private static void setElement(
long matrixVa, int row, int column, double value);

Expand Down
74 changes: 74 additions & 0 deletions src/main/native/glue/r/RMat44.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_assign
*pTarget = *pSource;
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: createCopy
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_createCopy
(JNIEnv *, jclass, jlong originalVa) {
const RMat44 * const pOriginal = reinterpret_cast<RMat44 *> (originalVa);
RMat44 * const pResult = new RMat44(*pOriginal);
TRACE_NEW("RMat44", pResult)
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: createFromRowMajor
Expand Down Expand Up @@ -117,6 +130,20 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_createRotatio
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: createScale
* Signature: (FFF)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_createScale
(JNIEnv *, jclass, jfloat sx, jfloat sy, jfloat sz) {
const Vec3 factors(sx, sy, sz);
RMat44 * const pResult = new RMat44();
TRACE_NEW("RMat44", pResult)
*pResult = RMat44::sScale(factors);
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: createTranslation
Expand Down Expand Up @@ -296,6 +323,17 @@ JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_RMat44_isIdentity
return result;
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: loadIdentity
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_loadIdentity
(JNIEnv *, jclass, jlong matrixVa) {
RMat44 * const pMatrix = reinterpret_cast<RMat44 *> (matrixVa);
*pMatrix = RMat44::sIdentity();
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: multiply
Expand Down Expand Up @@ -401,6 +439,42 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_RMat44_postTranslate
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: setAxisX
* Signature: (JFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_setAxisX
(JNIEnv *, jclass, jlong matrixVa, jfloat x , jfloat y, jfloat z) {
RMat44 * const pMatrix = reinterpret_cast<RMat44 *> (matrixVa);
const Vec3 vec(x, y, z);
pMatrix->SetAxisX(vec);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: setAxisY
* Signature: (JFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_setAxisY
(JNIEnv *, jclass, jlong matrixVa, jfloat x , jfloat y, jfloat z) {
RMat44 * const pMatrix = reinterpret_cast<RMat44 *> (matrixVa);
const Vec3 vec(x, y, z);
pMatrix->SetAxisY(vec);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: setAxisZ
* Signature: (JFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_RMat44_setAxisZ
(JNIEnv *, jclass, jlong matrixVa, jfloat x , jfloat y, jfloat z) {
RMat44 * const pMatrix = reinterpret_cast<RMat44 *> (matrixVa);
const Vec3 vec(x, y, z);
pMatrix->SetAxisZ(vec);
}

/*
* Class: com_github_stephengold_joltjni_RMat44
* Method: setElement
Expand Down

0 comments on commit 2afcf89

Please sign in to comment.