Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions jme3-core/src/main/java/com/jme3/math/Vector2f.java
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,28 @@ public Vector2f normalizeLocal() {
return divideLocal(1);
}

/**
* Computes the absolute value of each component and returns the result as a
* new instance. The current instance is unaffected.
*
* @return a new Vector2f with non-negative components
*/
public Vector2f abs() {
return new Vector2f(Math.abs(x), Math.abs(y));
}

/**
* Computes the absolute value of each component and returns the (modified)
* current instance.
*
* @return the (modified) current instance (for chaining)
*/
public Vector2f absLocal() {
x = Math.abs(x);
y = Math.abs(y);
return this;
}

/**
* Returns the unsigned angle between the current instance and the argument,
* provided both vectors have length=1. If {@code otherVector} is null, Pi/2
Expand Down
23 changes: 23 additions & 0 deletions jme3-core/src/main/java/com/jme3/math/Vector3f.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,29 @@ public Vector3f normalizeLocal() {
return this;
}

/**
* Computes the absolute value of each component and returns the result as a
* new instance. The current instance is unaffected.
*
* @return a new Vector3f with non-negative components
*/
public Vector3f abs() {
return new Vector3f(Math.abs(x), Math.abs(y), Math.abs(z));
}

/**
* Computes the absolute value of each component and returns the (modified)
* current instance.
*
* @return the (modified) current instance (for chaining)
*/
public Vector3f absLocal() {
x = Math.abs(x);
y = Math.abs(y);
z = Math.abs(z);
return this;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The six new public methods have no test coverage. jme3-core/src/test/java/com/jme3/math/Vector3fTest.java exercises essentially every other public method of Vector3f (including maxLocal, minLocal, negate), so abs()/absLocal() should get matching tests there. Worth asserting the edge cases the existing suite cares about: negative components, -0.0f (→ +0.0f), and NaN propagation. Vector2f/Vector4f have no test classes, so covering the Vector3f variants is the minimum.


/**
* Compares this vector component-wise with the argument (keeping the most
* positive value for each component) and returns the (modified) current
Expand Down
24 changes: 24 additions & 0 deletions jme3-core/src/main/java/com/jme3/math/Vector4f.java
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,30 @@ public Vector4f normalizeLocal() {
return this;
}

/**
* Computes the absolute value of each component and returns the result as a
* new instance. The current instance is unaffected.
*
* @return a new Vector4f with non-negative components
*/
public Vector4f abs() {
return new Vector4f(Math.abs(x), Math.abs(y), Math.abs(z), Math.abs(w));
}

/**
* Computes the absolute value of each component and returns the (modified)
* current instance.
*
* @return the (modified) current instance (for chaining)
*/
public Vector4f absLocal() {
x = Math.abs(x);
y = Math.abs(y);
z = Math.abs(z);
w = Math.abs(w);
return this;
}

/**
* <code>maxLocal</code> computes the maximum value for each
* component in this and <code>other</code> vector. The result is stored
Expand Down