From dd67c91abd2e5f549fe35f590f37c5d6c732a100 Mon Sep 17 00:00:00 2001 From: Sven Göthel Date: Thu, 25 Apr 2024 03:27:51 +0200 Subject: Math: Align Frustum/Matrix dependency w/ native jaulib --- .../demos/graph/ui/UIShapeClippingDemo01.java | 2 +- src/jogl/classes/com/jogamp/math/Matrix4f.java | 34 +++++++++------------- src/jogl/classes/com/jogamp/math/Quaternion.java | 24 +++++++-------- src/jogl/classes/com/jogamp/math/geom/Frustum.java | 16 +++++----- .../classes/com/jogamp/math/util/PMVMatrix4f.java | 2 +- .../jogamp/opengl/test/junit/math/Matrix4fb.java | 4 +-- .../test/junit/math/TestQuaternion01NOUI.java | 30 ++++++++++++------- 7 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/demos/com/jogamp/opengl/demos/graph/ui/UIShapeClippingDemo01.java b/src/demos/com/jogamp/opengl/demos/graph/ui/UIShapeClippingDemo01.java index 9df5c1591..69751c458 100644 --- a/src/demos/com/jogamp/opengl/demos/graph/ui/UIShapeClippingDemo01.java +++ b/src/demos/com/jogamp/opengl/demos/graph/ui/UIShapeClippingDemo01.java @@ -161,7 +161,7 @@ public class UIShapeClippingDemo01 { final boolean isOutMv = frustumCbMv.isOutside( shapedMv ); - final Frustum frustumPMv = pmv.getPMv().updateFrustumPlanes(new Frustum()); + final Frustum frustumPMv = new Frustum().setFromMat(pmv.getPMv()); final boolean isOutPMv = frustumPMv.isOutside( shapeBox ); System.err.println("ClipBox "+box); diff --git a/src/jogl/classes/com/jogamp/math/Matrix4f.java b/src/jogl/classes/com/jogamp/math/Matrix4f.java index 06e20ed01..f4d5160cd 100644 --- a/src/jogl/classes/com/jogamp/math/Matrix4f.java +++ b/src/jogl/classes/com/jogamp/math/Matrix4f.java @@ -86,7 +86,7 @@ import com.jogamp.math.geom.Frustum.Plane; * @see com.jogamp.math.util.PMVMatrix4f * @see FloatUtil */ -public class Matrix4f { +public final class Matrix4f { /** * Creates a new identity matrix. @@ -1179,12 +1179,11 @@ public class Matrix4f { * Returns the rotation [m00 .. m22] fields converted to a Quaternion. * @param res resulting Quaternion * @return the resulting Quaternion for chaining. - * @see Quaternion#setFromMatrix(float, float, float, float, float, float, float, float, float) + * @see Quaternion#setFromMat(float, float, float, float, float, float, float, float, float) * @see #setToRotation(Quaternion) */ public final Quaternion getRotation(final Quaternion res) { - res.setFromMatrix(m00, m01, m02, m10, m11, m12, m20, m21, m22); - return res; + return res.setFromMat(m00, m01, m02, m10, m11, m12, m20, m21, m22); } /** @@ -1331,20 +1330,19 @@ public class Matrix4f { /** * Calculate the frustum planes in world coordinates - * using this premultiplied P*MV (column major order) matrix. + * using this column major order matrix, usually a projection (P) or premultiplied P*MV matrix. *

* Frustum plane's normals will point to the inside of the viewing frustum, * as required by the {@link Frustum} class. *

- * @see Frustum#updateFrustumPlanes(Matrix4f) + * @see Frustum#setFromMat(Matrix4f) */ - public Frustum updateFrustumPlanes(final Frustum frustum) { + public Frustum getFrustum(final Frustum frustum) { // Left: a = m41 + m11, b = m42 + m12, c = m43 + m13, d = m44 + m14 - [1..4] column-major // Left: a = m30 + m00, b = m31 + m01, c = m32 + m02, d = m33 + m03 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.LEFT]; - final Vec3f p_n = p.n; - p_n.set( m30 + m00, + p.n.set( m30 + m00, m31 + m01, m32 + m02 ); p.d = m33 + m03; @@ -1354,8 +1352,7 @@ public class Matrix4f { // Right: a = m30 - m00, b = m31 - m01, c = m32 - m02, d = m33 - m03 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.RIGHT]; - final Vec3f p_n = p.n; - p_n.set( m30 - m00, + p.n.set( m30 - m00, m31 - m01, m32 - m02 ); p.d = m33 - m03; @@ -1365,8 +1362,7 @@ public class Matrix4f { // Bottom: a = m30m10, b = m31m11, c = m32m12, d = m33m13 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.BOTTOM]; - final Vec3f p_n = p.n; - p_n.set( m30 + m10, + p.n.set( m30 + m10, m31 + m11, m32 + m12 ); p.d = m33 + m13; @@ -1376,8 +1372,7 @@ public class Matrix4f { // Top: a = m30 - m10, b = m31 - m11, c = m32 - m12, d = m33 - m13 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.TOP]; - final Vec3f p_n = p.n; - p_n.set( m30 - m10, + p.n.set( m30 - m10, m31 - m11, m32 - m12 ); p.d = m33 - m13; @@ -1387,8 +1382,7 @@ public class Matrix4f { // Near: a = m30m20, b = m31m21, c = m32m22, d = m33m23 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.NEAR]; - final Vec3f p_n = p.n; - p_n.set( m30 + m20, + p.n.set( m30 + m20, m31 + m21, m32 + m22 ); p.d = m33 + m23; @@ -1398,8 +1392,7 @@ public class Matrix4f { // Far: a = m30 - m20, b = m31 - m21, c = m32m22, d = m33m23 - [0..3] column-major { final Frustum.Plane p = frustum.getPlanes()[Frustum.FAR]; - final Vec3f p_n = p.n; - p_n.set( m30 - m20, + p.n.set( m30 - m20, m31 - m21, m32 - m22 ); p.d = m33 - m23; @@ -2128,8 +2121,7 @@ public class Matrix4f { if( 0 >= growSize ) { throw new IndexOutOfBoundsException("Out of fixed stack size: "+this); } - final float[] newBuffer = - new float[buffer.length + growSize]; + final float[] newBuffer = new float[buffer.length + Math.max(length, growSize)]; System.arraycopy(buffer, 0, newBuffer, 0, position); buffer = newBuffer; } diff --git a/src/jogl/classes/com/jogamp/math/Quaternion.java b/src/jogl/classes/com/jogamp/math/Quaternion.java index 038745a51..f7a348b20 100644 --- a/src/jogl/classes/com/jogamp/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/math/Quaternion.java @@ -42,7 +42,7 @@ package com.jogamp.math; * See euclideanspace.com-Quaternion *

*/ -public class Quaternion { +public final class Quaternion { private float x, y, z, w; /** @@ -947,11 +947,11 @@ public class Quaternion { *

* * @return this quaternion for chaining. - * @see #setFromMatrix(Matrix4f) + * @see #setFromMat(Matrix4f) */ - public Quaternion setFromMatrix(final float m00, final float m01, final float m02, - final float m10, final float m11, final float m12, - final float m20, final float m21, final float m22) { + public Quaternion setFromMat(final float m00, final float m01, final float m02, + final float m10, final float m11, final float m12, + final float m20, final float m21, final float m22) { // Note: Other implementations uses 'T' w/o '+1f' and compares 'T >= 0' while adding missing 1f in sqrt expr. // However .. this causes setLookAt(..) to fail and actually violates the 'trace definition'. @@ -992,7 +992,7 @@ public class Quaternion { } /** - * Compute the quaternion from a 3x3 column rotation matrix + * Compute the quaternion from a 3x3 column rotation matrix from {@link Matrix4f} instance *

* See Graphics Gems Code,
* MatrixTrace. @@ -1003,9 +1003,9 @@ public class Quaternion { * * @return this quaternion for chaining. * @see Matrix4f#getRotation(Quaternion) - * @see #setFromMatrix(float, float, float, float, float, float, float, float, float) + * @see #setFromMat(float, float, float, float, float, float, float, float, float) */ - public Quaternion setFromMatrix(final Matrix4f m) { + public Quaternion setFromMat(final Matrix4f m) { return m.getRotation(this); } @@ -1021,8 +1021,8 @@ public class Quaternion { * @param matrix float[16] store for the resulting normalized column matrix 4x4 * @return the given matrix store * @see Matrix-FAQ Q54 - * @see #setFromMatrix(Matrix4f) - * @see #setFromMatrix(float, float, float, float, float, float, float, float, float) + * @see #setFromMat(Matrix4f) + * @see #setFromMat(float, float, float, float, float, float, float, float, float) */ public final float[] toMatrix(final float[] matrix) { // pre-multiply scaled-reciprocal-magnitude to reduce multiplications @@ -1086,7 +1086,7 @@ public class Quaternion { * @param matrix store for the resulting normalized column matrix 4x4 * @return the given matrix store * @see Matrix-FAQ Q54 - * @see #setFromMatrix(float, float, float, float, float, float, float, float, float) + * @see #setFromMat(float, float, float, float, float, float, float, float, float) * @see Matrix4f#setToRotation(Quaternion) */ public final Matrix4f toMatrix(final Matrix4f matrix) { @@ -1105,7 +1105,7 @@ public class Quaternion { * @return this quaternion for chaining. */ public final Quaternion setFromAxes(final Vec3f xAxis, final Vec3f yAxis, final Vec3f zAxis) { - return setFromMatrix(xAxis.x(), yAxis.x(), zAxis.x(), + return setFromMat(xAxis.x(), yAxis.x(), zAxis.x(), xAxis.y(), yAxis.y(), zAxis.y(), xAxis.z(), yAxis.z(), zAxis.z()); } diff --git a/src/jogl/classes/com/jogamp/math/geom/Frustum.java b/src/jogl/classes/com/jogamp/math/geom/Frustum.java index 6efc2529c..7d35fab97 100644 --- a/src/jogl/classes/com/jogamp/math/geom/Frustum.java +++ b/src/jogl/classes/com/jogamp/math/geom/Frustum.java @@ -78,7 +78,7 @@ import com.jogamp.math.Vec4f; * *

*/ -public class Frustum { +public final class Frustum { /** * {@link Frustum} description by {@link #fovhv} and {@link #zNear}, {@link #zFar}. */ @@ -345,7 +345,7 @@ public class Frustum { *
  • The given {@link FovDesc} will be transformed * into the given perspective matrix (column major order) first, * see {@link Matrix4f#setToPerspective(FovHVHalves, float, float)}.
  • - *
  • Then the perspective matrix is used to {@link Matrix4f#updateFrustumPlanes(Frustum)} this instance.
  • + *
  • Then the perspective matrix is used to {@link Matrix4f#getFrustum(Frustum)} this instance.
  • * *

    *

    @@ -357,26 +357,26 @@ public class Frustum { * @param fovDesc {@link Frustum} {@link FovDesc} * @return given matrix for chaining * @see Matrix4f#setToPerspective(FovHVHalves, float, float) - * @see Matrix4f#updateFrustumPlanes(Frustum) + * @see Matrix4f#getFrustum(Frustum) * @see Matrix4f#getFrustum(Frustum, FovDesc) */ public Matrix4f updateByFovDesc(final Matrix4f m, final FovDesc fovDesc) { m.setToPerspective(fovDesc.fovhv, fovDesc.zNear, fovDesc.zFar); - m.updateFrustumPlanes(this); + setFromMat(m); return m; } /** * Calculate the frustum planes in world coordinates - * using the passed premultiplied P*MV (column major order) matrix. + * using the passed column major order matrix, usually a projection (P) or premultiplied P*MV matrix. *

    * Frustum plane's normals will point to the inside of the viewing frustum, * as required by this class. *

    - * @see Matrix4f#updateFrustumPlanes(Frustum) + * @see Matrix4f#getFrustum(Frustum) */ - public Frustum updateFrustumPlanes(final Matrix4f pmv) { - return pmv.updateFrustumPlanes(this); + public Frustum setFromMat(final Matrix4f pmv) { + return pmv.getFrustum(this); } /** diff --git a/src/jogl/classes/com/jogamp/math/util/PMVMatrix4f.java b/src/jogl/classes/com/jogamp/math/util/PMVMatrix4f.java index 7d02904be..650289d1f 100644 --- a/src/jogl/classes/com/jogamp/math/util/PMVMatrix4f.java +++ b/src/jogl/classes/com/jogamp/math/util/PMVMatrix4f.java @@ -1259,7 +1259,7 @@ public class PMVMatrix4f { if( null == frustum ) { frustum = new Frustum(); } - getPMv().updateFrustumPlanes(frustum); + frustum.setFromMat(getPMv()); dirtyBits &= ~FRUSTUM; } return frustum; diff --git a/src/test/com/jogamp/opengl/test/junit/math/Matrix4fb.java b/src/test/com/jogamp/opengl/test/junit/math/Matrix4fb.java index 9a7d9f7ff..d76040904 100644 --- a/src/test/com/jogamp/opengl/test/junit/math/Matrix4fb.java +++ b/src/test/com/jogamp/opengl/test/junit/math/Matrix4fb.java @@ -1032,11 +1032,11 @@ public class Matrix4fb { * Returns the rotation [m[0+0*4] .. m[2+2*4]] fields converted to a Quaternion. * @param res resulting Quaternion * @return the resulting Quaternion for chaining. - * @see Quaternion#setFromMatrix(float, float, float, float, float, float, float, float, float) + * @see Quaternion#setFromMat(float, float, float, float, float, float, float, float, float) * @see #setToRotation(Quaternion) */ public final Quaternion getRotation(final Quaternion res) { - res.setFromMatrix(m[0+0*4], m[0+1*4], m[0+2*4], m[1+0*4], m[1+1*4], m[1+2*4], m[2+0*4], m[2+1*4], m[2+2*4]); + res.setFromMat(m[0+0*4], m[0+1*4], m[0+2*4], m[1+0*4], m[1+1*4], m[1+2*4], m[2+0*4], m[2+1*4], m[2+2*4]); return res; } diff --git a/src/test/com/jogamp/opengl/test/junit/math/TestQuaternion01NOUI.java b/src/test/com/jogamp/opengl/test/junit/math/TestQuaternion01NOUI.java index 8e4957d97..f13003175 100644 --- a/src/test/com/jogamp/opengl/test/junit/math/TestQuaternion01NOUI.java +++ b/src/test/com/jogamp/opengl/test/junit/math/TestQuaternion01NOUI.java @@ -309,7 +309,7 @@ public class TestQuaternion01NOUI extends JunitTracer { Assert.assertEquals( 0f, Math.abs( UNIT_Z.dist(vecHas) ), Quaternion.ALLOWED_DEVIANCE ); } mat1.getRotation(quat); - quat.setFromMatrix(mat1); + quat.setFromMat(mat1); quat.rotateVector(UNIT_Y, vecHas); // System.err.println("exp0 "+Arrays.toString(UNIT_Z)); // System.err.println("has0 "+Arrays.toString(vecHas)); @@ -388,7 +388,7 @@ public class TestQuaternion01NOUI extends JunitTracer { // System.err.println("has0 "+Arrays.toString(vecHas)); Assert.assertEquals( 0f, Math.abs( NEG_UNIT_Y.dist(vecHas) ), Quaternion.ALLOWED_DEVIANCE ); } - quat.setFromMatrix(mat1); + quat.setFromMat(mat1); quat.rotateVector(UNIT_Y, vecHas); // System.err.println("exp0 "+Arrays.toString(NEG_UNIT_Y)); // System.err.println("has0 "+Arrays.toString(vecHas)); @@ -423,7 +423,7 @@ public class TestQuaternion01NOUI extends JunitTracer { // System.err.println("has0 "+Arrays.toString(vecHas)); Assert.assertEquals( 0f, Math.abs( NEG_UNIT_X.dist(vecHas) ), Quaternion.ALLOWED_DEVIANCE ); } - quat.setFromMatrix(mat1); + quat.setFromMat(mat1); quat.rotateVector(UNIT_X, vecHas); // System.err.println("exp0 "+Arrays.toString(NEG_UNIT_X)); // System.err.println("has0 "+Arrays.toString(vecHas)); @@ -458,7 +458,7 @@ public class TestQuaternion01NOUI extends JunitTracer { // System.err.println("has0 "+Arrays.toString(vecHas)); Assert.assertEquals( 0f, Math.abs( NEG_UNIT_X.dist(vecHas) ), Quaternion.ALLOWED_DEVIANCE ); } - quat.setFromMatrix(mat1); + quat.setFromMat(mat1); quat.rotateVector(UNIT_X, vecHas); // System.err.println("exp0 "+Arrays.toString(NEG_UNIT_X)); // System.err.println("has0 "+Arrays.toString(vecHas)); @@ -517,7 +517,7 @@ public class TestQuaternion01NOUI extends JunitTracer { final Vec3f eulerHas = new Vec3f(); final Quaternion quat2 = new Quaternion(); - quat2.setFromMatrix(matExp1); + quat2.setFromMat(matExp1); quat2.toEuler(eulerHas); // System.err.println("exp-euler "+Arrays.toString(eulerExp)); // System.err.println("has-euler "+Arrays.toString(eulerHas)); @@ -546,7 +546,7 @@ public class TestQuaternion01NOUI extends JunitTracer { final Vec3f eulerHas = new Vec3f(); final Quaternion quat2 = new Quaternion(); - quat2.setFromMatrix(matExp); + quat2.setFromMat(matExp); quat2.toEuler(eulerHas); // System.err.println("exp-euler "+Arrays.toString(eulerExp)); // System.err.println("has-euler "+Arrays.toString(eulerHas)); @@ -578,7 +578,7 @@ public class TestQuaternion01NOUI extends JunitTracer { final Vec3f eulerHas1 = new Vec3f(); final Quaternion quat2 = new Quaternion(); - quat2.setFromMatrix(matExp); + quat2.setFromMat(matExp); quat2.toEuler(eulerHas1); // Vec3f if( DEBUG ) { System.err.println("PI"); @@ -789,7 +789,7 @@ public class TestQuaternion01NOUI extends JunitTracer { final Quaternion quat1 = new Quaternion().setFromAxes(xAxis, yAxis, zAxis); Assert.assertEquals(quat0, quat1); - final Quaternion quat2 = new Quaternion().setFromMatrix(rotMat); + final Quaternion quat2 = new Quaternion().setFromMat(rotMat); Assert.assertEquals(quat2, quat1); quat1.toAxes(xAxis, yAxis, zAxis, rotMat); @@ -889,12 +889,22 @@ public class TestQuaternion01NOUI extends JunitTracer { Assert.assertEquals(0f, direction.dist( quat.rotateVector(UNIT_Z, vecHas) ), Quaternion.ALLOWED_DEVIANCE); Assert.assertEquals(direction, vecHas); - if( DEBUG ) System.err.println("LookAt #02"); + if( DEBUG ) { + System.err.println("quat0.0 "+quat); + System.err.println("exp0 "+direction+", len "+direction.length()); + System.err.println("has0 "+vecHas+", len "+vecHas.length()); + System.err.println(); + System.err.println("LookAt #02"); + } direction.set(ONE).normalize(); quat.setLookAt(direction, UNIT_Y, xAxis, yAxis, zAxis); - if( DEBUG )System.err.println("quat0 "+quat); + if( DEBUG ) { + System.err.println("direction "+direction); + System.err.println("quat0.0 "+quat); + } quat.rotateVector(UNIT_Z, vecHas); if( DEBUG ) { + System.err.println("quat0.1 "+quat); System.err.println("xAxis "+xAxis+", len "+xAxis.length()); System.err.println("yAxis "+yAxis+", len "+yAxis.length()); System.err.println("zAxis "+zAxis+", len "+zAxis.length()); -- cgit v1.2.3