Skip to content

Commit

Permalink
Vec3: add 6 public in-place methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Feb 20, 2025
1 parent a7b45c8 commit 928da7b
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/Vec3.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ public Vec3(Vec3Arg vec) {
// *************************************************************************
// new methods exposed

/**
* Add the specified offsets.
*
* @param xOffset the amount to add to the X component
* @param yOffset the amount to add to the Y component
* @param zOffset the amount to add to the Z component
*/
public void addInPlace(float xOffset, float yOffset, float zOffset) {
this.x += xOffset;
this.y += yOffset;
this.z += zOffset;
}

/**
* Change the current vector to a unit vector with the same direction.
*/
public void normalizeInPlace() {
float invLength = 1f / length();
this.x *= invLength;
this.y *= invLength;
this.z *= invLength;
}

/**
* Return the bitwise AND of the specified vectors.
*
Expand Down Expand Up @@ -196,6 +219,19 @@ public static Vec3 sAxisZ() {
return result;
}

/**
* Separately scale each component.
*
* @param xScale the scale factor to apply to the X component
* @param yScale the scale factor to apply to the Y component
* @param zScale the scale factor to apply to the Z component
*/
public void scaleInPlace(float xScale, float yScale, float zScale) {
this.x *= xScale;
this.y *= yScale;
this.z *= zScale;
}

/**
* Set all 3 components to specified values.
*
Expand All @@ -221,6 +257,17 @@ public void set(float[] array) {
this.z = array[2];
}

/**
* Set all 3 components from the argument.
*
* @param source the vector to copy (not null, unaffected)
*/
public void set(Vec3Arg source) {
this.x = source.getX();
this.y = source.getY();
this.z = source.getZ();
}

/**
* Alter the first (X) component.
*
Expand Down Expand Up @@ -403,6 +450,21 @@ public static Vec3 sSelect(Vec3Arg notSet, Vec3Arg set, UVec4Arg control) {
return result;
}

/**
* Replace any -0 elements with +0, in preparation for hashing.
*/
public void standardizeInPlace() {
if (Float.compare(x, -0f) == 0) {
this.x = 0f;
}
if (Float.compare(y, -0f) == 0) {
this.y = 0f;
}
if (Float.compare(z, -0f) == 0) {
this.z = 0f;
}
}

/**
* Return the component-wise sum of the specified vectors.
*
Expand Down

0 comments on commit 928da7b

Please sign in to comment.