diff options
author | Sven Gothel <[email protected]> | 2023-09-03 08:31:24 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2023-09-03 08:31:24 +0200 |
commit | 47233ea7014e34adca8a5c1d2323a57e9de1c7fa (patch) | |
tree | 8911ce16673237d6c6c568a5e4a68874c4ccb877 | |
parent | e6087583dc06c823f54bb6206af2acb0e2119b4c (diff) |
FloatUtil.isEqual(..): Rename raw {isEqual->isEqualRaw}(a,b) varianr w/o EPSILON; Add isEqual(a,b) w/ default EPSILON; Use it where applicable
Also add isEqual2(a,b) w/o corner cases (NaN, Inf) used for comparison in Graph Outline, OutlineShape and later GraphUI Shape.
11 files changed, 121 insertions, 59 deletions
diff --git a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java index 1a1bd94dd..644f428cd 100644 --- a/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java +++ b/src/jogl/classes/com/jogamp/graph/curve/OutlineShape.java @@ -1060,7 +1060,7 @@ public final class OutlineShape implements Comparable<OutlineShape> { public final int compareTo(final OutlineShape other) { final float thisSize = getBounds().getSize(); final float otherSize = other.getBounds().getSize(); - if( FloatUtil.isEqual(thisSize, otherSize, FloatUtil.EPSILON) ) { + if( FloatUtil.isEqual2(thisSize, otherSize) ) { return 0; } else if( thisSize < otherSize ){ return -1; diff --git a/src/jogl/classes/com/jogamp/graph/geom/Outline.java b/src/jogl/classes/com/jogamp/graph/geom/Outline.java index a0999baa1..654bd2636 100644 --- a/src/jogl/classes/com/jogamp/graph/geom/Outline.java +++ b/src/jogl/classes/com/jogamp/graph/geom/Outline.java @@ -318,7 +318,7 @@ public class Outline implements Comparable<Outline> { public final int compareTo(final Outline other) { final float thisSize = getBounds().getSize(); final float otherSize = other.getBounds().getSize(); - if( FloatUtil.isEqual(thisSize, otherSize, FloatUtil.EPSILON) ) { + if( FloatUtil.isEqual2(thisSize, otherSize) ) { return 0; } else if(thisSize < otherSize){ return -1; diff --git a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java index 004562767..34a581681 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/FloatUtil.java @@ -1422,7 +1422,7 @@ public final class FloatUtil { * </p> * @see #isEqual(float, float, float) */ - public static boolean isEqual(final float a, final float b) { + public static boolean isEqualRaw(final float a, final float b) { // Values are equal (Inf, Nan .. ) return Float.floatToIntBits(a) == Float.floatToIntBits(b); } @@ -1449,6 +1449,38 @@ public final class FloatUtil { } /** + * Return true if both values are equal, i.e. their absolute delta < {@link #EPSILON}. + * <p> + * Implementation considers following corner cases: + * <ul> + * <li>NaN == NaN</li> + * <li>+Inf == +Inf</li> + * <li>-Inf == -Inf</li> + * </ul> + * </p> + * @see #EPSILON + */ + public static boolean isEqual(final float a, final float b) { + if ( Math.abs(a - b) < EPSILON ) { + return true; + } else { + // Values are equal (Inf, Nan .. ) + return Float.floatToIntBits(a) == Float.floatToIntBits(b); + } + } + + /** + * Return true if both values are equal, i.e. their absolute delta < {@link #EPSILON}. + * <p> + * Implementation does not consider corner cases like {@link #isEqual(float, float, float)}. + * </p> + * @see #EPSILON + */ + public static boolean isEqual2(final float a, final float b) { + return Math.abs(a - b) < EPSILON; + } + + /** * Return true if both values are equal w/o regarding an epsilon. * <p> * Implementation considers following corner cases: diff --git a/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java b/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java index 77971b72d..5d1d8e968 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Matrix4f.java @@ -1125,13 +1125,13 @@ public class Matrix4f { public final Matrix4f setToRotation(final Quaternion q) { // pre-multiply scaled-reciprocal-magnitude to reduce multiplications final float norm = q.magnitudeSquared(); - if ( FloatUtil.isZero(norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isZero(norm) ) { // identity matrix -> srecip = 0f loadIdentity(); return this; } final float srecip; - if ( FloatUtil.isEqual(1f, norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isEqual(1f, norm) ) { srecip = 2f; } else { srecip = 2.0f / norm; diff --git a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java index 9548a160a..be09d03af 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java @@ -90,10 +90,10 @@ public class Quaternion { */ public final float magnitude() { final float magnitudeSQ = magnitudeSquared(); - if ( FloatUtil.isZero(magnitudeSQ, FloatUtil.EPSILON) ) { + if ( FloatUtil.isZero(magnitudeSQ) ) { return 0f; } - if ( FloatUtil.isEqual(1f, magnitudeSQ, FloatUtil.EPSILON) ) { + if ( FloatUtil.isEqual(1f, magnitudeSQ) ) { return 1f; } return FloatUtil.sqrt(magnitudeSQ); @@ -149,13 +149,13 @@ public class Quaternion { * Returns <code>true</code> if this quaternion has identity. * <p> * Implementation uses {@link FloatUtil#EPSILON epsilon} to compare - * {@link #w() W} {@link FloatUtil#isEqual(float, float, float) against 1f} and + * {@link #w() W} {@link FloatUtil#isEqual(float, float) against 1f} and * {@link #x() X}, {@link #y() Y} and {@link #z() Z} - * {@link FloatUtil#isZero(float, float) against zero}. + * {@link FloatUtil#isZero(float) against zero}. * </p> */ public final boolean isIdentity() { - return FloatUtil.isEqual(1f, w, FloatUtil.EPSILON) && VectorUtil.isZero(x, y, z, FloatUtil.EPSILON); + return FloatUtil.isEqual(1f, w) && VectorUtil.isZero(x, y, z); // return w == 1f && x == 0f && y == 0f && z == 0f; } @@ -217,7 +217,7 @@ public class Quaternion { */ public final Quaternion invert() { final float magnitudeSQ = magnitudeSquared(); - if ( FloatUtil.isEqual(1.0f, magnitudeSQ, FloatUtil.EPSILON) ) { + if ( FloatUtil.isEqual(1.0f, magnitudeSQ) ) { conjugate(); } else { final float invmsq = 1f/magnitudeSQ; @@ -1025,12 +1025,12 @@ public class Quaternion { public final float[] toMatrix(final float[] matrix) { // pre-multiply scaled-reciprocal-magnitude to reduce multiplications final float norm = magnitudeSquared(); - if ( FloatUtil.isZero(norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isZero(norm) ) { // identity matrix -> srecip = 0f return FloatUtil.makeIdentity(matrix); } final float srecip; - if ( FloatUtil.isEqual(1f, norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isEqual(1f, norm) ) { srecip = 2f; } else { srecip = 2.0f / norm; diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java index d733aca5d..f504b960b 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec2f.java @@ -349,7 +349,7 @@ public final class Vec2f { } /** - * Equals check using {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. + * Equals check using {@link FloatUtil#EPSILON} in {@link FloatUtil#isEqual(float, float)}. * <p> * Implementation considers following corner cases: * <ul> @@ -361,13 +361,18 @@ public final class Vec2f { * @return true if all components differ less than {@link FloatUtil#EPSILON}, otherwise false. */ public boolean isEqual(final Vec2f o) { - return isEqual(o, FloatUtil.EPSILON); + if( this == o ) { + return true; + } else { + return FloatUtil.isEqual(x, o.x) && + FloatUtil.isEqual(y, o.y); + } } @Override public boolean equals(final Object o) { if( o instanceof Vec2f ) { - return isEqual((Vec2f)o, FloatUtil.EPSILON); + return isEqual((Vec2f)o); } else { return false; } diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java index 34f13adbe..7012edd74 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec3f.java @@ -367,7 +367,7 @@ public final class Vec3f { } /** - * Equals check using {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. + * Equals check using {@link FloatUtil#EPSILON} in {@link FloatUtil#isEqual(float, float)}. * <p> * Implementation considers following corner cases: * <ul> @@ -379,13 +379,19 @@ public final class Vec3f { * @return true if all components differ less than {@link FloatUtil#EPSILON}, otherwise false. */ public boolean isEqual(final Vec3f o) { - return isEqual(o, FloatUtil.EPSILON); + if( this == o ) { + return true; + } else { + return FloatUtil.isEqual(x, o.x) && + FloatUtil.isEqual(y, o.y) && + FloatUtil.isEqual(z, o.z); + } } @Override public boolean equals(final Object o) { if( o instanceof Vec3f ) { - return isEqual((Vec3f)o, FloatUtil.EPSILON); + return isEqual((Vec3f)o); } else { return false; } diff --git a/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java b/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java index a5276c7f1..914450bf1 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Vec4f.java @@ -356,7 +356,7 @@ public final class Vec4f { } /** - * Equals check using {@link FloatUtil#EPSILON} value and {@link FloatUtil#isEqual(float, float, float)}. + * Equals check using {@link FloatUtil#EPSILON} in {@link FloatUtil#isEqual(float, float)}. * <p> * Implementation considers following corner cases: * <ul> @@ -368,13 +368,20 @@ public final class Vec4f { * @return true if all components differ less than {@link FloatUtil#EPSILON}, otherwise false. */ public boolean isEqual(final Vec4f o) { - return isEqual(o, FloatUtil.EPSILON); + if( this == o ) { + return true; + } else { + return FloatUtil.isEqual(x, o.x) && + FloatUtil.isEqual(y, o.y) && + FloatUtil.isEqual(z, o.z) && + FloatUtil.isEqual(w, o.w); + } } @Override public boolean equals(final Object o) { if( o instanceof Vec4f ) { - return isEqual((Vec4f)o, FloatUtil.EPSILON); + return isEqual((Vec4f)o); } else { return false; } diff --git a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java index d3b2c3cfd..e38501c73 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java +++ b/src/jogl/classes/com/jogamp/opengl/math/VectorUtil.java @@ -52,6 +52,18 @@ public final class VectorUtil { } /** + * Return true if all three vector components are zero, i.e. it's their absolute value < {@link FloatUtil#EPSILON}. + * <p> + * Implementation uses {@link FloatUtil#isZero(float)}, see API doc for details. + * </p> + */ + public static boolean isZero(final float x, final float y, final float z) { + return FloatUtil.isZero(x) && + FloatUtil.isZero(y) && + FloatUtil.isZero(z) ; + } + + /** * Return the squared distance between the given two points described vector v1 and v2. * <p> * When comparing the relative distance between two points it is usually sufficient to compare the squared diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/math/Matrix4fb.java b/src/test/com/jogamp/opengl/test/junit/jogl/math/Matrix4fb.java index 83ecf7380..feb9b6f46 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/math/Matrix4fb.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/math/Matrix4fb.java @@ -977,13 +977,13 @@ public class Matrix4fb { public final Matrix4fb setToRotation(final Quaternion q) { // pre-multiply scaled-reciprocal-magnitude to reduce multiplications final float norm = q.magnitudeSquared(); - if ( FloatUtil.isZero(norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isZero(norm) ) { // identity matrix -> srecip = 0f loadIdentity(); return this; } final float srecip; - if ( FloatUtil.isEqual(1f, norm, FloatUtil.EPSILON) ) { + if ( FloatUtil.isEqual(1f, norm) ) { srecip = 2f; } else { srecip = 2.0f / norm; diff --git a/src/test/com/jogamp/opengl/test/junit/jogl/math/TestFloatUtil01NOUI.java b/src/test/com/jogamp/opengl/test/junit/jogl/math/TestFloatUtil01NOUI.java index 909b94a90..1c2b32fcb 100644 --- a/src/test/com/jogamp/opengl/test/junit/jogl/math/TestFloatUtil01NOUI.java +++ b/src/test/com/jogamp/opengl/test/junit/jogl/math/TestFloatUtil01NOUI.java @@ -72,7 +72,7 @@ public class TestFloatUtil01NOUI extends JunitTracer { }
private void dumpTestNE(final int tstNum, final int exp, final float a, final float b) {
final float delta = a-b;
- final boolean equal = FloatUtil.isEqual(a, b);
+ final boolean equal = FloatUtil.isEqualRaw(a, b);
final int comp = FloatUtil.compare(a, b);
final String msg = ( exp != comp ) ? "**** mismatch ****" : " OK";
System.err.println("Print.NE."+tstNum+": a: "+a+", b: "+b+" -> d "+delta+", exp "+exp+", equal "+equal+", comp "+comp+" - "+msg);
@@ -136,7 +136,7 @@ public class TestFloatUtil01NOUI extends JunitTracer { dumpTestNE(i++, 0, 0f, 0f+Float.MIN_VALUE);
}
private void testEqualsNoEpsilon(final int tstNum, final boolean exp, final float a, final float b) {
- final boolean equal = FloatUtil.isEqual(a, b);
+ final boolean equal = FloatUtil.isEqualRaw(a, b);
final int comp = FloatUtil.compare(a, b);
final float delta = a-b;
System.err.println("Equal.NE."+tstNum+": a: "+a+", b: "+b+" -> d "+delta+", exp "+exp+", equal "+equal+", comp "+comp);
@@ -223,7 +223,7 @@ public class TestFloatUtil01NOUI extends JunitTracer { dumpTestNE(i++, 0, 0f, 0f+Float.MIN_VALUE);
}
private void testCompareNoEpsilon(final int tstNum, final int exp, final float a, final float b) {
- final boolean equal = FloatUtil.isEqual(a, b);
+ final boolean equal = FloatUtil.isEqualRaw(a, b);
final int comp = FloatUtil.compare(a, b);
final float delta = a-b;
System.err.println("Comp.NE."+tstNum+": a: "+a+", b: "+b+" -> d "+delta+", equal "+equal+", comp: exp "+exp+" has "+comp);
@@ -238,42 +238,42 @@ public class TestFloatUtil01NOUI extends JunitTracer { public void test05bCompareWithMachEpsilon() {
test05CompareWithEpsilon(50, MACH_EPSILON);
}
- private void test05CompareWithEpsilon(int i, final float EPSILON) {
+ private void test05CompareWithEpsilon(int i, final float epsilon) {
System.err.println();
- testCompareWithEpsilon(i++, 0, 0f, 0f, EPSILON);
- testCompareWithEpsilon(i++, 0, 1f, 1f-EPSILON/2f, EPSILON);
- testCompareWithEpsilon(i++, 0, 1f, 1f+EPSILON/2f, EPSILON);
- testCompareWithEpsilon(i++, 0, 1f, 1f-Float.MIN_VALUE, EPSILON);
- testCompareWithEpsilon(i++, 0, 1f, 1f+Float.MIN_VALUE, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.MAX_VALUE, Float.MAX_VALUE, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.MIN_VALUE, Float.MIN_VALUE, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.MIN_NORMAL, Float.MIN_NORMAL, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, EPSILON);
- testCompareWithEpsilon(i++, 0, Float.NaN, Float.NaN, EPSILON);
-
- testCompareWithEpsilon(i++, 1, 1f, 0f, EPSILON);
- testCompareWithEpsilon(i++, -1, 0f, 1f, EPSILON);
- testCompareWithEpsilon(i++, 1, 0f, -1f, EPSILON);
- testCompareWithEpsilon(i++, -1, -1f, 0f, EPSILON);
-
- testCompareWithEpsilon(i++, 1, Float.MAX_VALUE, Float.MIN_VALUE, EPSILON);
- testCompareWithEpsilon(i++, -1, Float.MIN_VALUE, Float.MAX_VALUE, EPSILON);
- testCompareWithEpsilon(i++, 1, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, EPSILON);
- testCompareWithEpsilon(i++, -1, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, EPSILON);
-
- testCompareWithEpsilon(i++, -1, 0f,Float.NaN, EPSILON);
- testCompareWithEpsilon(i++, 1, Float.NaN, 0f, EPSILON);
-
- testCompareWithEpsilon(i++, 0, -0f, 0f, EPSILON);
- testCompareWithEpsilon(i++, 0, 0f, -0f, EPSILON);
+ testCompareWithEpsilon(i++, 0, 0f, 0f, epsilon);
+ testCompareWithEpsilon(i++, 0, 1f, 1f-epsilon/2f, epsilon);
+ testCompareWithEpsilon(i++, 0, 1f, 1f+epsilon/2f, epsilon);
+ testCompareWithEpsilon(i++, 0, 1f, 1f-Float.MIN_VALUE, epsilon);
+ testCompareWithEpsilon(i++, 0, 1f, 1f+Float.MIN_VALUE, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.MAX_VALUE, Float.MAX_VALUE, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.MIN_VALUE, Float.MIN_VALUE, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.MIN_NORMAL, Float.MIN_NORMAL, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.POSITIVE_INFINITY, Float.POSITIVE_INFINITY, epsilon);
+ testCompareWithEpsilon(i++, 0, Float.NaN, Float.NaN, epsilon);
+
+ testCompareWithEpsilon(i++, 1, 1f, 0f, epsilon);
+ testCompareWithEpsilon(i++, -1, 0f, 1f, epsilon);
+ testCompareWithEpsilon(i++, 1, 0f, -1f, epsilon);
+ testCompareWithEpsilon(i++, -1, -1f, 0f, epsilon);
+
+ testCompareWithEpsilon(i++, 1, Float.MAX_VALUE, Float.MIN_VALUE, epsilon);
+ testCompareWithEpsilon(i++, -1, Float.MIN_VALUE, Float.MAX_VALUE, epsilon);
+ testCompareWithEpsilon(i++, 1, Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, epsilon);
+ testCompareWithEpsilon(i++, -1, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, epsilon);
+
+ testCompareWithEpsilon(i++, -1, 0f,Float.NaN, epsilon);
+ testCompareWithEpsilon(i++, 1, Float.NaN, 0f, epsilon);
+
+ testCompareWithEpsilon(i++, 0, -0f, 0f, epsilon);
+ testCompareWithEpsilon(i++, 0, 0f, -0f, epsilon);
}
- private void testCompareWithEpsilon(final int tstNum, final int exp, final float a, final float b, final float EPSILON) {
- final boolean equal = FloatUtil.isEqual(a, b, EPSILON);
- final int comp = FloatUtil.compare(a, b, EPSILON);
+ private void testCompareWithEpsilon(final int tstNum, final int exp, final float a, final float b, final float epsilon) {
+ final boolean equal = FloatUtil.isEqual(a, b, epsilon);
+ final int comp = FloatUtil.compare(a, b, epsilon);
final float delta = a-b;
System.err.println("Comp.WE."+tstNum+": a: "+a+", b: "+b+" -> d "+delta+", equal "+equal+", comp: exp "+exp+" has "+comp);
- Assert.assertEquals("Compare failed a: "+a+", b: "+b+" within "+EPSILON, exp, comp);
+ Assert.assertEquals("Compare failed a: "+a+", b: "+b+" within "+epsilon, exp, comp);
}
|