diff options
author | Petr Skramovsky <[email protected]> | 2013-06-02 21:09:42 +0200 |
---|---|---|
committer | Petr Skramovsky <[email protected]> | 2013-06-05 13:33:56 +0200 |
commit | 0a4defdfe478c3e231f3b1255cba6989aa2c489e (patch) | |
tree | b057a592de650c65d7bc62de7e773fbfdc073020 /src/jogl | |
parent | eaf15f8020bdd97f7d919721c9cc74eb72e6c493 (diff) |
fixed isIdentity method, deprecated isEmpty method (quaternion doesn't have such a property + method do same thing as isIdentity), new setIdentity method, default constructor sets this quaternion to identity, new fromAxis method/costructor
Diffstat (limited to 'src/jogl')
-rw-r--r-- | src/jogl/classes/com/jogamp/opengl/math/Quaternion.java | 52 |
1 files changed, 39 insertions, 13 deletions
diff --git a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java index 96cb2d6f1..d1f3e0336 100644 --- a/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java +++ b/src/jogl/classes/com/jogamp/opengl/math/Quaternion.java @@ -31,7 +31,7 @@ public class Quaternion { protected float x, y, z, w; public Quaternion() { - + setIdentity(); } public Quaternion(float x, float y, float z, float w) { @@ -50,13 +50,32 @@ public class Quaternion { public Quaternion(float[] vector1, float[] vector2) { float theta = FloatUtil.acos(VectorUtil.dot(vector1, vector2)); float[] cross = VectorUtil.cross(vector1, vector2); - cross = VectorUtil.normalize(cross); - - x = FloatUtil.sin(theta / 2) * cross[0]; - y = FloatUtil.sin(theta / 2) * cross[1]; - z = FloatUtil.sin(theta / 2) * cross[2]; - w = FloatUtil.cos(theta / 2); - normalize(); + fromAxis(cross, theta); + } + + /*** + * Constructor to create a rotation based quaternion from axis vector and angle + * @param vector axis vector + * @param angle rotation angle (rads) + * @see #fromAxis(float[], float) + */ + public Quaternion(float[] vector, float angle) { + fromAxis(vector, angle); + } + + /*** + * Initialize this quaternion with given axis vector and rotation angle + * + * @param vector axis vector + * @param angle rotation angle (rads) + */ + public void fromAxis(float[] vector, float angle) { + float sin = FloatUtil.sin(angle *= 0.5f); + float[] nv = VectorUtil.normalize(vector); + x = (nv[0] * sin); + y = (nv[1] * sin); + z = (nv[2] * sin); + w = FloatUtil.cos(angle); } /** @@ -174,8 +193,7 @@ public class Quaternion { public void normalize() { float norme = (float) FloatUtil.sqrt(w * w + x * x + y * y + z * z); if (norme == 0.0f) { - w = 1.0f; - x = y = z = 0.0f; + setIdentity(); } else { float recip = 1.0f / norme; @@ -286,7 +304,9 @@ public class Quaternion { * Check if this quaternion is empty, ie (0,0,0,1) * * @return true if empty, false otherwise + * @deprecated use {@link #isIdentity()} instead */ + @Deprecated public boolean isEmpty() { if (w == 1 && x == 0 && y == 0 && z == 0) return true; @@ -299,9 +319,15 @@ public class Quaternion { * @return true if it is an identity rep., false otherwise */ public boolean isIdentity() { - if (w == 0 && x == 0 && y == 0 && z == 0) - return true; - return false; + return w == 1 && x == 0 && y == 0 && z == 0; + } + + /*** + * Set this quaternion to identity (x=0,y=0,z=0,w=1) + */ + public void setIdentity() { + x = y = z = 0; + w = 1; } /** |