From 41b3a1cee877c2e3089b1929f86863f872b97409 Mon Sep 17 00:00:00 2001 From: stephengold Date: Sun, 23 Feb 2025 21:42:34 -0800 Subject: [PATCH] RVec3Arg: add 8 methods --- .../com/github/stephengold/joltjni/RVec3.java | 117 ++++++++++++++++++ .../joltjni/readonly/RVec3Arg.java | 69 +++++++++++ 2 files changed, 186 insertions(+) diff --git a/src/main/java/com/github/stephengold/joltjni/RVec3.java b/src/main/java/com/github/stephengold/joltjni/RVec3.java index f3706a15..b2130edb 100644 --- a/src/main/java/com/github/stephengold/joltjni/RVec3.java +++ b/src/main/java/com/github/stephengold/joltjni/RVec3.java @@ -318,6 +318,43 @@ public RVec3 cross(RVec3Arg rightFactor) { return result; } + /** + * Return the dot product with the argument. Both vectors are unaffected. + * + * @param factor the vector to dot with the current one (not null, + * unaffected) + * @return the dot product + */ + @Override + public double dot(RVec3Arg factor) { + double result = xx * factor.xx() + yy * factor.yy() + zz * factor.zz(); + return result; + } + + /** + * Return the specified component in double precision. The vector is + * unaffected. + * + * @param index 0, 1, or 2 + * @return the X component if index=0, the Y component if index=1, or the Z + * component if index=2 + * @throws IllegalArgumentException if index is not 0, 1, or 2 + */ + @Override + public double get(int index) { + switch (index) { + case 0: + return xx; + case 1: + return yy; + case 2: + return zz; + default: + throw new IllegalArgumentException( + "index must be either 0, 1 or 2"); + } + } + /** * Return the first (X) component at positional precision. The vector is * unaffected. @@ -372,6 +409,32 @@ public Object getZ() { return result; } + /** + * Test whether the vector contains infinities or NaNs. The vector is + * unaffected. + * + * @return {@code false} if one or more infinities or NaNs, otherwise + * {@code true} + */ + @Override + public boolean isFinite() { + boolean result = Double.isFinite(xx) + && Double.isFinite(yy) && Double.isFinite(zz); + return result; + } + + /** + * Test whether the vector contains NaNs. The vector is unaffected. + * + * @return {@code true} if one or more NaNs, otherwise {@code false} + */ + @Override + public boolean isNan() { + boolean result + = Double.isNaN(xx) || Double.isNaN(yy) || Double.isNaN(zz); + return result; + } + /** * Test whether the squared length is within 10^-12 (single-precision) or * 10^-24 (double-precision) of zero. The vector is unaffected. @@ -402,6 +465,37 @@ public boolean isNearZero(double tolerance) { } } + /** + * Test whether the vector is normalized to within a tolerance of 10^-6 + * (single precision) or 10^-12 (double precision). The vector is + * unaffected. + * + * @return {@code true} if normalized, otherwise {@code false} + */ + @Override + public boolean isNormalized() { + double tolerance = Jolt.isDoublePrecision() ? 1e-12 : 1e-6; + boolean result = isNormalized(tolerance); + return result; + } + + /** + * Test whether the vector is normalized to within the specified tolerance. + * The vector is unaffected. + * + * @param tolerance the desired tolerance (≥0, default=1e-6 or 1e-12) + * @return {@code true} if normalized, otherwise {@code false} + */ + @Override + public boolean isNormalized(double tolerance) { + double lengthSq = lengthSq(); + if (Math.abs(lengthSq - 1.) <= tolerance) { + return true; + } else { + return false; + } + } + /** * Return the length. The vector is unaffected. * @@ -426,6 +520,29 @@ public double lengthSq() { return result; } + /** + * Generate a unit vector with the same direction. The current vector is + * unaffected. + * + * @return a new vector + */ + @Override + public RVec3 normalized() { + RVec3 result = Op.star(1. / length(), this); + return result; + } + + /** + * Generate the component-wise reciprocal. The current vector is unaffected. + * + * @return a new vector + */ + @Override + public RVec3 reciprocal() { + RVec3 result = new RVec3(1. / xx, 1. / yy, 1. / 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 046295f1..e2e4188b 100644 --- a/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java +++ b/src/main/java/com/github/stephengold/joltjni/readonly/RVec3Arg.java @@ -42,6 +42,26 @@ public interface RVec3Arg { */ RVec3 cross(RVec3Arg rightFactor); + /** + * Return the dot product with the argument. Both vectors are unaffected. + * + * @param factor the vector to dot with the current one (not null, + * unaffected) + * @return the dot product + */ + double dot(RVec3Arg factor); + + /** + * Return the specified component in double precision. The vector is + * unaffected. + * + * @param index 0, 1, or 2 + * @return the X component if index=0, the Y component if index=1, or the Z + * component if index=2 + * @throws IllegalArgumentException if index is not 0, 1, or 2 + */ + double get(int index); + /** * Return the first (X) component in positional precision. The vector is * unaffected. @@ -66,6 +86,22 @@ public interface RVec3Arg { */ Object getZ(); + /** + * Test whether the vector contains infinities or NaNs. The vector is + * unaffected. + * + * @return {@code false} if one or more infinities or NaNs, otherwise + * {@code true} + */ + boolean isFinite(); + + /** + * Test whether the vector contains NaNs. The vector is unaffected. + * + * @return {@code true} if one or more NaNs, otherwise {@code false} + */ + boolean isNan(); + /** * Test whether the squared length is within 10^-12 (single-precision) or * 10^-24 (double-precision) of zero. The vector is unaffected. @@ -83,6 +119,24 @@ public interface RVec3Arg { */ boolean isNearZero(double tolerance); + /** + * Test whether the vector is normalized to within a tolerance of 10^-6 + * (single precision) or 10^-12 (double precision). The vector is + * unaffected. + * + * @return {@code true} if normalized, otherwise {@code false} + */ + boolean isNormalized(); + + /** + * Test whether the vector is normalized to within the specified tolerance. + * The vector is unaffected. + * + * @param tolerance the desired tolerance (≥0, default=1e-6 or 1e-12) + * @return {@code true} if normalized, otherwise {@code false} + */ + boolean isNormalized(double tolerance); + /** * Return the length. The vector is unaffected. * @@ -97,6 +151,21 @@ public interface RVec3Arg { */ double lengthSq(); + /** + * Generate a unit vector with the same direction. The current vector is + * unaffected. + * + * @return a new vector + */ + RVec3 normalized(); + + /** + * Generate the component-wise reciprocal. The current vector is unaffected. + * + * @return a new vector + */ + RVec3 reciprocal(); + /** * Copy the components to an array. The vector is unaffected. *