Skip to content

Commit

Permalink
RVec3Arg: add 8 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 24, 2025
1 parent 04f8ed3 commit 12bde6e
Show file tree
Hide file tree
Showing 2 changed files with 186 additions and 13 deletions.
130 changes: 117 additions & 13 deletions src/main/java/com/github/stephengold/joltjni/RVec3.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,19 +134,6 @@ public void loadZero() {
this.zz = 0.;
}

/**
* Test whether all 3 components are finite.
*
* @return {@code true} if all are finite, otherwise {@code false}
*/
public boolean isFinite() {
if (Double.isFinite(xx) && Double.isFinite(yy) && Double.isFinite(zz)) {
return true;
} else {
return false;
}
}

/**
* Create a unit vector along the 1st (X) principal axis.
*
Expand Down Expand Up @@ -318,6 +305,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.
Expand Down Expand Up @@ -372,6 +396,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.
Expand Down Expand Up @@ -402,6 +452,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.
*
Expand All @@ -426,6 +507,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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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 (&ge;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.
*
Expand All @@ -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.
*
Expand Down

0 comments on commit 12bde6e

Please sign in to comment.