Skip to content

Commit

Permalink
Add Vector2/3/4i constructors/setters/getters with explicit rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Jan 1, 2020
1 parent 9b44d8f commit ba58982
Show file tree
Hide file tree
Showing 18 changed files with 529 additions and 9 deletions.
57 changes: 57 additions & 0 deletions src/org/joml/Math.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,61 @@ public static double fma(double a, double b, double c) {
//#endif
return a * b + c;
}

public static int roundUsing(float v, int mode) {
switch (mode) {
case RoundingMode.TRUNCATE:
return (int) v;
case RoundingMode.CEILING:
return (int) java.lang.Math.ceil(v);
case RoundingMode.FLOOR:
return (int) java.lang.Math.floor(v);
case RoundingMode.HALF_DOWN:
return roundHalfDown(v);
case RoundingMode.HALF_UP:
return roundHalfUp(v);
case RoundingMode.HALF_EVEN:
return roundHalfEven(v);
default:
throw new UnsupportedOperationException();
}
}
public static int roundUsing(double v, int mode) {
switch (mode) {
case RoundingMode.TRUNCATE:
return (int) v;
case RoundingMode.CEILING:
return (int) java.lang.Math.ceil(v);
case RoundingMode.FLOOR:
return (int) java.lang.Math.floor(v);
case RoundingMode.HALF_DOWN:
return roundHalfDown(v);
case RoundingMode.HALF_UP:
return roundHalfUp(v);
case RoundingMode.HALF_EVEN:
return roundHalfEven(v);
default:
throw new UnsupportedOperationException();
}
}

public static int roundHalfEven(float v) {
return (int) java.lang.Math.rint(v);
}
public static int roundHalfDown(float v) {
return (v > 0) ? (int) java.lang.Math.ceil(v - 0.5d) : (int) java.lang.Math.floor(v + 0.5d);
}
public static int roundHalfUp(float v) {
return (v > 0) ? (int) java.lang.Math.floor(v + 0.5d) : (int) java.lang.Math.ceil(v - 0.5d);
}

public static int roundHalfEven(double v) {
return (int) java.lang.Math.rint(v);
}
public static int roundHalfDown(double v) {
return (v > 0) ? (int) java.lang.Math.ceil(v - 0.5d) : (int) java.lang.Math.floor(v + 0.5d);
}
public static int roundHalfUp(double v) {
return (v > 0) ? (int) java.lang.Math.floor(v + 0.5d) : (int) java.lang.Math.ceil(v - 0.5d);
}
}
60 changes: 60 additions & 0 deletions src/org/joml/RoundingMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* The MIT License
*
* Copyright (c) 2016-2020 JOML
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.joml;

/**
* Rounding modes.
*
* @author Kai Burjack
*/
public class RoundingMode {
private RoundingMode() {}
/**
* Discards the fractional part.
*/
public static final int TRUNCATE = 0;
/**
* Round towards positive infinity.
*/
public static final int CEILING = 1;
/**
* Round towards negative infinity.
*/
public static final int FLOOR = 2;
/**
* Round towards the nearest neighbor. If both neighbors are equidistant, round
* towards the even neighbor.
*/
public static final int HALF_EVEN = 3;
/**
* Round towards the nearest neighbor. If both neighbors are equidistant, round
* down.
*/
public static final int HALF_DOWN = 4;
/**
* Round towards the nearest neighbor. If both neighbors are equidistant, round
* up.
*/
public static final int HALF_UP = 5;
}
4 changes: 4 additions & 0 deletions src/org/joml/Vector2d.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ public double get(int component) throws IllegalArgumentException {
}
}

public Vector2i get(int mode, Vector2i dest) {
return dest.set(this, mode);
}

public Vector2f get(Vector2f dest) {
return dest.set(this);
}
Expand Down
12 changes: 12 additions & 0 deletions src/org/joml/Vector2dc.java
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ public interface Vector2dc {
*/
double get(int component) throws IllegalArgumentException;

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector
* using the given {@link RoundingMode}.
*
* @param mode
* the {@link RoundingMode} to use
* @param dest
* will hold the result
* @return dest
*/
Vector2i get(int mode, Vector2i dest);

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector.
*
Expand Down
4 changes: 4 additions & 0 deletions src/org/joml/Vector2f.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ public float get(int component) throws IllegalArgumentException {
}
}

public Vector2i get(int mode, Vector2i dest) {
return dest.set(this, mode);
}

public Vector2f get(Vector2f dest) {
return dest.set(this);
}
Expand Down
12 changes: 12 additions & 0 deletions src/org/joml/Vector2fc.java
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,18 @@ public interface Vector2fc {
*/
float get(int component) throws IllegalArgumentException;

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector
* using the given {@link RoundingMode}.
*
* @param mode
* the {@link RoundingMode} to use
* @param dest
* will hold the result
* @return dest
*/
Vector2i get(int mode, Vector2i dest);

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector.
*
Expand Down
62 changes: 61 additions & 1 deletion src/org/joml/Vector2i.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,34 @@ public Vector2i(Vector2ic v) {
y = v.y();
}

/**
* Create a new {@link Vector2i} and initialize its components to the rounded value of
* the given vector.
*
* @param v
* the {@link Vector2fc} to round and copy the values from
* @param mode
* the {@link RoundingMode} to use
*/
public Vector2i(Vector2fc v, int mode) {
x = Math.roundUsing(v.x(), mode);
y = Math.roundUsing(v.y(), mode);
}

/**
* Create a new {@link Vector2i} and initialize its components to the rounded value of
* the given vector.
*
* @param v
* the {@link Vector2dc} to round and copy the values from
* @param mode
* the {@link RoundingMode} to use
*/
public Vector2i(Vector2dc v, int mode) {
x = Math.roundUsing(v.x(), mode);
y = Math.roundUsing(v.y(), mode);
}

//#ifdef __HAS_NIO__
/**
* Create a new {@link Vector2i} and read this vector from the supplied
Expand Down Expand Up @@ -232,7 +260,7 @@ public Vector2i set(Vector2ic v) {
}

/**
* Set this {@link Vector2i} to the values of v.
* Set this {@link Vector2i} to the values of v using {@link RoundingMode#TRUNCATE} rounding.
* <p>
* Note that due to the given vector <code>v</code> storing the components
* in double-precision, there is the possibility to lose precision.
Expand All @@ -245,6 +273,38 @@ public Vector2i set(Vector2dc v) {
return set((int) v.x(), (int) v.y());
}

/**
* Set this {@link Vector2i} to the values of v using the given {@link RoundingMode}.
* <p>
* Note that due to the given vector <code>v</code> storing the components
* in double-precision, there is the possibility to lose precision.
*
* @param v
* the vector to copy from
* @param mode
* the {@link RoundingMode} to use
* @return this
*/
public Vector2i set(Vector2dc v, int mode) {
return set(Math.roundUsing(v.x(), mode), Math.roundUsing(v.y(), mode));
}

/**
* Set this {@link Vector2i} to the values of v using the given {@link RoundingMode}.
* <p>
* Note that due to the given vector <code>v</code> storing the components
* in double-precision, there is the possibility to lose precision.
*
* @param v
* the vector to copy from
* @param mode
* the {@link RoundingMode} to use
* @return this
*/
public Vector2i set(Vector2fc v, int mode) {
return set(Math.roundUsing(v.x(), mode), Math.roundUsing(v.y(), mode));
}

//#ifdef __HAS_NIO__
/**
* Read this vector from the supplied {@link ByteBuffer} at the current
Expand Down
4 changes: 4 additions & 0 deletions src/org/joml/Vector3d.java
Original file line number Diff line number Diff line change
Expand Up @@ -2303,6 +2303,10 @@ public double get(int component) throws IllegalArgumentException {
}
}

public Vector3i get(int mode, Vector3i dest) {
return dest.set(this, mode);
}

public Vector3f get(Vector3f dest) {
return dest.set(this);
}
Expand Down
12 changes: 12 additions & 0 deletions src/org/joml/Vector3dc.java
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,18 @@ public interface Vector3dc {
*/
double get(int component) throws IllegalArgumentException;

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector
* using the given {@link RoundingMode}.
*
* @param mode
* the {@link RoundingMode} to use
* @param dest
* will hold the result
* @return dest
*/
Vector3i get(int mode, Vector3i dest);

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector.
*
Expand Down
4 changes: 4 additions & 0 deletions src/org/joml/Vector3f.java
Original file line number Diff line number Diff line change
Expand Up @@ -1894,6 +1894,10 @@ public float get(int component) throws IllegalArgumentException {
}
}

public Vector3i get(int mode, Vector3i dest) {
return dest.set(this, mode);
}

public Vector3f get(Vector3f dest) {
return dest.set(this);
}
Expand Down
12 changes: 12 additions & 0 deletions src/org/joml/Vector3fc.java
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,18 @@ public interface Vector3fc {
*/
float get(int component) throws IllegalArgumentException;

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector
* using the given {@link RoundingMode}.
*
* @param mode
* the {@link RoundingMode} to use
* @param dest
* will hold the result
* @return dest
*/
Vector3i get(int mode, Vector3i dest);

/**
* Set the components of the given vector <code>dest</code> to those of <code>this</code> vector.
*
Expand Down
Loading

0 comments on commit ba58982

Please sign in to comment.