Skip to content

Commit

Permalink
Add Quaternion.equals(delta) (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
pollend committed Aug 17, 2020
1 parent a8f0274 commit 9867ebf
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/org/joml/Quaterniond.java
Original file line number Diff line number Diff line change
Expand Up @@ -2940,4 +2940,34 @@ public Quaterniond conjugateBy(Quaterniondc q, Quaterniond dest) {
public boolean isFinite() {
return Math.isFinite(x) && Math.isFinite(y) && Math.isFinite(z) && Math.isFinite(w);
}

public boolean equals(Quaterniondc q, double delta) {
if (this == q)
return true;
if (q == null)
return false;
if (!(q instanceof Quaterniondc))
return false;
if (!Runtime.equals(x, q.x(), delta))
return false;
if (!Runtime.equals(y, q.y(), delta))
return false;
if (!Runtime.equals(z, q.z(), delta))
return false;
if (!Runtime.equals(w, q.w(), delta))
return false;
return true;
}

public boolean equals(double x, double y, double z, double w) {
if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(x))
return false;
if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(y))
return false;
if (Double.doubleToLongBits(this.z) != Double.doubleToLongBits(z))
return false;
if (Double.doubleToLongBits(this.w) != Double.doubleToLongBits(w))
return false;
return true;
}
}
31 changes: 31 additions & 0 deletions src/org/joml/Quaterniondc.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.joml;

import java.util.*;
/**
* Interface to a read-only view of a quaternion of double-precision floats.
*
Expand Down Expand Up @@ -1916,4 +1917,34 @@ public interface Quaterniondc {
*/
boolean isFinite();

/**
Compare the quaternion components of <code>this</code> quaternion with the given quaternion using the given <code>delta</code>
* and return whether all of them are equal within a maximum difference of <code>delta</code>.
* <p>
* Please note that this method is not used by any data structure such as {@link ArrayList} {@link HashSet} or {@link HashMap}
* and their operations, such as {@link ArrayList#contains(Object)} or {@link HashSet#remove(Object)}, since those
* data structures only use the {@link Object#equals(Object)} and {@link Object#hashCode()} methods.
*
* @param q
* the other quaternion
* @param delta
* the allowed maximum difference
* @return <code>true</code> whether all of the quaternion components are equal; <code>false</code> otherwise
*/
boolean equals(Quaterniondc q, double delta);

/**
*
* @param x
* the x component to compare to
* @param y
* the y component to compare to
* @param z
* the z component to compare to
* @param w
* the w component to compare to
* @return <code>true</code> if all the quaternion components are equal
*/
boolean equals(double x, double y, double z, double w);

}
30 changes: 30 additions & 0 deletions src/org/joml/Quaternionf.java
Original file line number Diff line number Diff line change
Expand Up @@ -3025,4 +3025,34 @@ public Quaternionf conjugateBy(Quaternionfc q, Quaternionf dest) {
public boolean isFinite() {
return Math.isFinite(x) && Math.isFinite(y) && Math.isFinite(z) && Math.isFinite(w);
}

public boolean equals(Quaternionfc q, float delta) {
if (this == q)
return true;
if (q == null)
return false;
if (!(q instanceof Quaternionfc))
return false;
if (!Runtime.equals(x, q.x(), delta))
return false;
if (!Runtime.equals(y, q.y(), delta))
return false;
if (!Runtime.equals(z, q.z(), delta))
return false;
if (!Runtime.equals(w, q.w(), delta))
return false;
return true;
}

public boolean equals(float x, float y, float z, float w) {
if (Float.floatToIntBits(this.x) != Float.floatToIntBits(x))
return false;
if (Float.floatToIntBits(this.y) != Float.floatToIntBits(y))
return false;
if (Float.floatToIntBits(this.z) != Float.floatToIntBits(z))
return false;
if (Float.floatToIntBits(this.w) != Float.floatToIntBits(w))
return false;
return true;
}
}
30 changes: 30 additions & 0 deletions src/org/joml/Quaternionfc.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
//#endif
import java.util.*;

/**
* Interface to a read-only view of a quaternion of single-precision floats.
Expand Down Expand Up @@ -1930,4 +1931,33 @@ public interface Quaternionfc {
*/
boolean isFinite();

/**
Compare the quaternion components of <code>this</code> quaternion with the given quaternion using the given <code>delta</code>
* and return whether all of them are equal within a maximum difference of <code>delta</code>.
* <p>
* Please note that this method is not used by any data structure such as {@link ArrayList} {@link HashSet} or {@link HashMap}
* and their operations, such as {@link ArrayList#contains(Object)} or {@link HashSet#remove(Object)}, since those
* data structures only use the {@link Object#equals(Object)} and {@link Object#hashCode()} methods.
*
* @param q
* the other quaternion
* @param delta
* the allowed maximum difference
* @return <code>true</code> whether all of the quaternion components are equal; <code>false</code> otherwise
*/
boolean equals(Quaternionfc q, float delta);

/**
*
* @param x
* the x component to compare to
* @param y
* the y component to compare to
* @param z
* the z component to compare to
* @param w
* the w component to compare to
* @return <code>true</code> if all the quaternion components are equal
*/
boolean equals(float x, float y, float z, float w);
}

0 comments on commit 9867ebf

Please sign in to comment.