From fe6604f6b5d5e61e719a0842a3b24131e9654838 Mon Sep 17 00:00:00 2001 From: stephengold Date: Thu, 5 Dec 2024 16:15:21 -0800 Subject: [PATCH] RVec3Arg: add 3 methods --- .../com/github/stephengold/joltjni/RVec3.java | 41 +++++++++++++++++++ .../joltjni/readonly/RVec3Arg.java | 24 +++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/main/java/com/github/stephengold/joltjni/RVec3.java b/src/main/java/com/github/stephengold/joltjni/RVec3.java index 9d5bfb32..0907e594 100644 --- a/src/main/java/com/github/stephengold/joltjni/RVec3.java +++ b/src/main/java/com/github/stephengold/joltjni/RVec3.java @@ -272,6 +272,47 @@ public Object getZ() { return result; } + /** + * Test whether the squared length is within 1e-12 (single-precision) or + * 1e-24 (double-precision) of zero. The vector is unaffected. + * + * @return {@code true} if nearly zero, otherwise {@code false} + */ + @Override + public boolean isNearZero() { + double tolerance = Jolt.isDoublePrecision() ? 1e-24 : 1e-12; + boolean result = isNearZero(tolerance); + return result; + } + + /** + * Test whether the squared length is within the specified tolerance of + * zero. The vector is unaffected. + * + * @param tolerance the desired tolerance (≥0) + * @return {@code true} if nearly zero, otherwise {@code false} + */ + @Override + public boolean isNearZero(double tolerance) { + double lengthSq = lengthSq(); + if (lengthSq <= tolerance) { + return true; + } else { + return false; + } + } + + /** + * Return the squared length. The vector is unaffected. + * + * @return the squared length + */ + @Override + public double lengthSq() { + double result = xx * xx + yy * yy + zz * zz; + return result; + } + /** * Copy the components to an array. The vector is unaffected. * diff --git a/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java b/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java index 0da120bf..9fffc85b 100644 --- a/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java +++ b/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java @@ -67,6 +67,30 @@ public interface RVec3Arg { */ Object getZ(); + /** + * Test whether the squared length is within 1e-12 (single-precision) or + * 1e-24 (double-precision) of zero. The vector is unaffected. + * + * @return {@code true} if nearly zero, otherwise {@code false} + */ + boolean isNearZero(); + + /** + * Test whether the squared length is within the specified tolerance of + * zero. The vector is unaffected. + * + * @param tolerance the desired tolerance (&gel0) + * @return {@code true} if nearly zero, otherwise {@code false} + */ + boolean isNearZero(double tolerance); + + /** + * Return the squared length. The vector is unaffected. + * + * @return the squared length + */ + double lengthSq(); + /** * Copy the components to an array. The vector is unaffected. *