From d9c9d3dae2a7027e82ad8721f5284a8a450e93b6 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Sun, 20 Mar 2022 11:17:21 +0100 Subject: [PATCH] [DEBUG] correct the Matrix4f invertion --- .classpath | 28 ++++++------ .project | 11 +++++ src/org/atriasoft/etk/math/Matrix4f.java | 41 +++++++++++++----- test/src/test/atriasoft/etk/TestMatrix4.java | 45 ++++++++++++++++++++ 4 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 test/src/test/atriasoft/etk/TestMatrix4.java diff --git a/.classpath b/.classpath index 61e0048..b6d8b74 100644 --- a/.classpath +++ b/.classpath @@ -5,31 +5,27 @@ - + - - - - - - - - - - - - - - - + + + + + + + + + + + diff --git a/.project b/.project index 9f45611..07cff41 100644 --- a/.project +++ b/.project @@ -21,4 +21,15 @@ org.eclipse.jdt.core.javanature net.sf.eclipsecs.core.CheckstyleNature + + + 1646149232198 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/src/org/atriasoft/etk/math/Matrix4f.java b/src/org/atriasoft/etk/math/Matrix4f.java index 66e2a0a..d820f3c 100644 --- a/src/org/atriasoft/etk/math/Matrix4f.java +++ b/src/org/atriasoft/etk/math/Matrix4f.java @@ -166,7 +166,7 @@ public record Matrix4f( final float cosVal = (float) Math.cos(angleRad); final float sinVal = (float) Math.sin(angleRad); final float invVal = 1.0f - cosVal; - // set rotation : + // set rotation : a1 = normal.x() * normal.x() * invVal + cosVal; b1 = normal.x() * normal.y() * invVal - normal.z() * sinVal; c1 = normal.x() * normal.z() * invVal + normal.y() * sinVal; @@ -458,22 +458,27 @@ public record Matrix4f( return this; } float a1 = coFactorRaw0Col0() / det; - float b1 = coFactorRaw0Col1() / det; + float b1 = -coFactorRaw0Col1() / det; float c1 = coFactorRaw0Col2() / det; - float d1 = coFactorRaw0Col3() / det; - float a2 = coFactorRaw1Col0() / det; + float d1 = -coFactorRaw0Col3() / det; + + float a2 = -coFactorRaw1Col0() / det; float b2 = coFactorRaw1Col1() / det; - float c2 = coFactorRaw1Col2() / det; + float c2 = -coFactorRaw1Col2() / det; float d2 = coFactorRaw1Col3() / det; + float a3 = coFactorRaw2Col0() / det; - float b3 = coFactorRaw2Col1() / det; + float b3 = -coFactorRaw2Col1() / det; float c3 = coFactorRaw2Col2() / det; - float d3 = coFactorRaw2Col3() / det; - float a4 = coFactorRaw3Col0() / det; + float d3 = -coFactorRaw2Col3() / det; + + float a4 = -coFactorRaw3Col0() / det; float b4 = coFactorRaw3Col1() / det; - float c4 = coFactorRaw3Col2() / det; + float c4 = -coFactorRaw3Col2() / det; float d4 = coFactorRaw3Col3() / det; + return new Matrix4f(a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3, a4, b4, c4, d4); + //return new Matrix4f(a1, b1, c1, a4, a2, b2, c2, b4, a3, b3, c3, c4, d1, d2, d3, d4); } /** @@ -533,8 +538,22 @@ public record Matrix4f( */ @CheckReturnValue public Vector3f multiply(final Vector3f point) { - return new Vector3f(this.a1 * point.x() + this.b1 * point.y() + this.c1 * point.z() + this.d1, this.a2 * point.x() + this.b2 * point.y() + this.c2 * point.z() + this.d2, - this.a3 * point.x() + this.b3 * point.y() + this.c3 * point.z() + this.d3); + return new Vector3f(this.a1 * point.x() + this.b1 * point.y() + this.c1 * point.z() + this.d1, // X + this.a2 * point.x() + this.b2 * point.y() + this.c2 * point.z() + this.d2, // Y + this.a3 * point.x() + this.b3 * point.y() + this.c3 * point.z() + this.d3); // Z + } + + /** + * Operator* apply matrix on a vector + * @param point Point value to apply the matrix + * @return New vector containing the value + */ + @CheckReturnValue + public Vector4f multiply(final Vector4f point) { + return new Vector4f(this.a1 * point.x() + this.b1 * point.y() + this.c1 * point.z() + this.d1 * point.w(), // X + this.a2 * point.x() + this.b2 * point.y() + this.c2 * point.z() + this.d2 * point.w(), // Y + this.a3 * point.x() + this.b3 * point.y() + this.c3 * point.z() + this.d3 * point.w(), // Z + this.a4 * point.x() + this.b4 * point.y() + this.c4 * point.z() + this.d4 * point.w()); // W } /** diff --git a/test/src/test/atriasoft/etk/TestMatrix4.java b/test/src/test/atriasoft/etk/TestMatrix4.java new file mode 100644 index 0000000..b2b2072 --- /dev/null +++ b/test/src/test/atriasoft/etk/TestMatrix4.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * Contributors: + * Edouard DUPIN - initial API and implementation + ******************************************************************************/ +package test.atriasoft.etk; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.atriasoft.etk.math.Matrix4f; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; + +public class TestMatrix4 { + + @Test + @Order(4) + public void invert() { + Matrix4f tmp = new Matrix4f(1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, -1, 1, 1, 1); + Matrix4f tmpInvert = tmp.invert(); + assertEquals(tmpInvert.a1(), 0.25f); + assertEquals(tmpInvert.b1(), 0.25f); + assertEquals(tmpInvert.c1(), 0.25f); + assertEquals(tmpInvert.d1(), -0.25f); + + assertEquals(tmpInvert.a2(), 0.25f); + assertEquals(tmpInvert.b2(), 0.25f); + assertEquals(tmpInvert.c2(), -0.25f); + assertEquals(tmpInvert.d2(), 0.25f); + + assertEquals(tmpInvert.a3(), 0.25f); + assertEquals(tmpInvert.b3(), -0.25f); + assertEquals(tmpInvert.c3(), 0.25f); + assertEquals(tmpInvert.d3(), 0.25f); + + assertEquals(tmpInvert.a4(), -0.25f); + assertEquals(tmpInvert.b4(), 0.25f); + assertEquals(tmpInvert.c4(), 0.25f); + assertEquals(tmpInvert.d4(), 0.25f); + } + +}