Added missing static methods to Quaternion for consistency. Cleaned up handling of src == dest aliasing
This commit is contained in:
parent
09001cb925
commit
8ea2ce415e
|
@ -43,16 +43,16 @@ package org.lwjgl.util.vector;
|
||||||
import java.nio.FloatBuffer;
|
import java.nio.FloatBuffer;
|
||||||
|
|
||||||
public class Quaternion extends Vector implements ReadableVector4f {
|
public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private float x, y, z, w;
|
public float x, y, z, w;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* C'tor
|
* C'tor. The quaternion will be initialized to the identity.
|
||||||
*/
|
*/
|
||||||
public Quaternion() {
|
public Quaternion() {
|
||||||
super();
|
super();
|
||||||
|
setIdentity();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -123,6 +123,27 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set this quaternion to the multiplication identity.
|
||||||
|
* @return this
|
||||||
|
*/
|
||||||
|
public Quaternion setIdentity() {
|
||||||
|
return setIdentity(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the given quaternion to the multiplication identity.
|
||||||
|
* @param q The quaternion
|
||||||
|
* @return q
|
||||||
|
*/
|
||||||
|
public static Quaternion setIdentity(Quaternion q) {
|
||||||
|
q.x = 0;
|
||||||
|
q.y = 0;
|
||||||
|
q.z = 0;
|
||||||
|
q.w = 1;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return the length squared of the quaternion
|
* @return the length squared of the quaternion
|
||||||
*/
|
*/
|
||||||
|
@ -131,24 +152,38 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Normalise this vector and place the result in another vector.
|
* Normalise the source quaternion and place the result in another quaternion.
|
||||||
*
|
*
|
||||||
|
* @param src
|
||||||
|
* The source quaternion
|
||||||
* @param dest
|
* @param dest
|
||||||
* The destination vector, or null if a new vector is to be
|
* The destination quaternion, or null if a new quaternion is to be
|
||||||
* created
|
* created
|
||||||
* @return the normalised vector
|
* @return The normalised quaternion
|
||||||
*/
|
*/
|
||||||
public Quaternion normalise(Quaternion dest) {
|
public static Quaternion normalise(Quaternion src, Quaternion dest) {
|
||||||
float l = length();
|
float inv_l = 1f/src.length();
|
||||||
|
|
||||||
if (dest == null)
|
if (dest == null)
|
||||||
dest = new Quaternion(x / l, y / l, z / l, w / l);
|
dest = new Quaternion();
|
||||||
else
|
|
||||||
dest.set(x / l, y / l, z / l, w / l);
|
dest.set(src.x * inv_l, src.y * inv_l, src.z * inv_l, src.w * inv_l);
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise this quaternion and place the result in another quaternion.
|
||||||
|
*
|
||||||
|
* @param dest
|
||||||
|
* The destination quaternion, or null if a new quaternion is to be
|
||||||
|
* created
|
||||||
|
* @return the normalised quaternion
|
||||||
|
*/
|
||||||
|
public Quaternion normalise(Quaternion dest) {
|
||||||
|
return normalise(this, dest);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The dot product of two quaternions
|
* The dot product of two quaternions
|
||||||
*
|
*
|
||||||
|
@ -171,13 +206,26 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
* quaternion
|
* quaternion
|
||||||
*/
|
*/
|
||||||
public Quaternion negate(Quaternion dest) {
|
public Quaternion negate(Quaternion dest) {
|
||||||
|
return negate(this, dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate the conjugate of this quaternion and put it into the given one
|
||||||
|
*
|
||||||
|
* @param src
|
||||||
|
* The source quaternion
|
||||||
|
* @param dest
|
||||||
|
* The quaternion which should be set to the conjugate of this
|
||||||
|
* quaternion
|
||||||
|
*/
|
||||||
|
public static Quaternion negate(Quaternion src, Quaternion dest) {
|
||||||
if (dest == null)
|
if (dest == null)
|
||||||
dest = new Quaternion();
|
dest = new Quaternion();
|
||||||
|
|
||||||
dest.x = -x;
|
dest.x = -src.x;
|
||||||
dest.y = -y;
|
dest.y = -src.y;
|
||||||
dest.z = -z;
|
dest.z = -src.z;
|
||||||
dest.w = w;
|
dest.w = src.w;
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
@ -186,11 +234,7 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
* Calculate the conjugate of this quaternion
|
* Calculate the conjugate of this quaternion
|
||||||
*/
|
*/
|
||||||
public Vector negate() {
|
public Vector negate() {
|
||||||
x = -x;
|
return negate(this, this);
|
||||||
y = -y;
|
|
||||||
z = -z;
|
|
||||||
|
|
||||||
return this;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
@ -210,11 +254,24 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
* @see org.lwjgl.vector.Vector#scale(float)
|
* @see org.lwjgl.vector.Vector#scale(float)
|
||||||
*/
|
*/
|
||||||
public Vector scale(float scale) {
|
public Vector scale(float scale) {
|
||||||
x *= scale;
|
return scale(scale, this, this);
|
||||||
y *= scale;
|
}
|
||||||
z *= scale;
|
|
||||||
w *= scale;
|
/**
|
||||||
return this;
|
* Scale the source quaternion by scale and put the result in the destination
|
||||||
|
* @param scale The amount to scale by
|
||||||
|
* @param src The source quaternion
|
||||||
|
* @param dest The destination quaternion, or null if a new quaternion is to be created
|
||||||
|
* @return The scaled quaternion
|
||||||
|
*/
|
||||||
|
public static Quaternion scale(float scale, Quaternion src, Quaternion dest) {
|
||||||
|
if (dest == null)
|
||||||
|
dest = new Quaternion();
|
||||||
|
dest.x = src.x * scale;
|
||||||
|
dest.y = src.y * scale;
|
||||||
|
dest.z = src.z * scale;
|
||||||
|
dest.w = src.w * scale;
|
||||||
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
|
@ -314,13 +371,7 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
public static Quaternion mul(Quaternion left, Quaternion right,
|
public static Quaternion mul(Quaternion left, Quaternion right,
|
||||||
Quaternion dest) {
|
Quaternion dest) {
|
||||||
if (dest == null)
|
if (dest == null)
|
||||||
return new Quaternion(left.x * right.w + left.w * right.x + left.y
|
dest = new Quaternion();
|
||||||
* right.z - left.z * right.y, left.y * right.w + left.w
|
|
||||||
* right.y + left.z * right.x - left.x * right.z, left.z
|
|
||||||
* right.w + left.w * right.z + left.x * right.y - left.y
|
|
||||||
* right.x, left.w * right.w - left.x * right.x - left.y
|
|
||||||
* right.y - left.z * right.z);
|
|
||||||
else {
|
|
||||||
dest.set(left.x * right.w + left.w * right.x + left.y * right.z
|
dest.set(left.x * right.w + left.w * right.x + left.y * right.z
|
||||||
- left.z * right.y, left.y * right.w + left.w * right.y
|
- left.z * right.y, left.y * right.w + left.w * right.y
|
||||||
+ left.z * right.x - left.x * right.z, left.z * right.w
|
+ left.z * right.x - left.x * right.z, left.z * right.w
|
||||||
|
@ -329,7 +380,6 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
- left.z * right.z);
|
- left.z * right.z);
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -349,16 +399,7 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
n = (n == 0.0 ? n : 1 / n);
|
n = (n == 0.0 ? n : 1 / n);
|
||||||
// store on stack once for aliasing-safty
|
// store on stack once for aliasing-safty
|
||||||
if (dest == null)
|
if (dest == null)
|
||||||
return new Quaternion((left.x * right.w - left.w * right.x - left.y
|
dest = new Quaternion();
|
||||||
* right.z + left.z * right.y)
|
|
||||||
* n, (left.y * right.w - left.w * right.y - left.z
|
|
||||||
* right.x + left.x * right.z)
|
|
||||||
* n, (left.z * right.w - left.w * right.z - left.x
|
|
||||||
* right.y + left.y * right.x)
|
|
||||||
* n, (left.w * right.w + left.x * right.x + left.y
|
|
||||||
* right.y + left.z * right.z)
|
|
||||||
* n);
|
|
||||||
else {
|
|
||||||
dest
|
dest
|
||||||
.set((left.x * right.w - left.w * right.x - left.y
|
.set((left.x * right.w - left.w * right.x - left.y
|
||||||
* right.z + left.z * right.y)
|
* right.z + left.z * right.y)
|
||||||
|
@ -372,7 +413,6 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
|
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of this quaternion to the equivalent rotation of the
|
* Sets the value of this quaternion to the equivalent rotation of the
|
||||||
|
@ -399,29 +439,58 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
* passed matrix.
|
* passed matrix.
|
||||||
*
|
*
|
||||||
* @param m1
|
* @param m1
|
||||||
* the matrix4f
|
* The matrix
|
||||||
|
* @return this
|
||||||
*/
|
*/
|
||||||
public final void setFromMatrix(Matrix4f m1) {
|
public final Quaternion setFromMatrix(Matrix4f m) {
|
||||||
setFromMat(m1.m00, m1.m01, m1.m02, m1.m10, m1.m11, m1.m12, m1.m20,
|
return setFromMatrix(m, this);
|
||||||
m1.m21, m1.m22);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of the source quaternion using the rotational component of the
|
||||||
|
* passed matrix.
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* The source matrix
|
||||||
|
* @param q
|
||||||
|
* The destination quaternion, or null if a new quaternion is to be created
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public final static Quaternion setFromMatrix(Matrix4f m, Quaternion q) {
|
||||||
|
return q.setFromMat(m.m00, m.m01, m.m02, m.m10, m.m11, m.m12, m.m20,
|
||||||
|
m.m21, m.m22);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the value of this quaternion using the rotational component of the
|
* Sets the value of this quaternion using the rotational component of the
|
||||||
* passed matrix.
|
* passed matrix.
|
||||||
*
|
*
|
||||||
* @param m1
|
* @param m
|
||||||
* the matrix3f
|
* The source matrix
|
||||||
*/
|
*/
|
||||||
public final void setFromMatrix(Matrix3f m1) {
|
public final Quaternion setFromMatrix(Matrix3f m) {
|
||||||
setFromMat(m1.m00, m1.m01, m1.m02, m1.m10, m1.m11, m1.m12, m1.m20,
|
return setFromMatrix(m, this);
|
||||||
m1.m21, m1.m22);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the value of the source quaternion using the rotational component of the
|
||||||
|
* passed matrix.
|
||||||
|
*
|
||||||
|
* @param m
|
||||||
|
* The source matrix
|
||||||
|
* @param q
|
||||||
|
* The destination quaternion, or null if a new quaternion is to be created
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static final Quaternion setFromMatrix(Matrix3f m, Quaternion q) {
|
||||||
|
return q.setFromMat(m.m00, m.m01, m.m02, m.m10, m.m11, m.m12, m.m20,
|
||||||
|
m.m21, m.m22);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Private method to perform the matrix-to-quaternion conversion
|
* Private method to perform the matrix-to-quaternion conversion
|
||||||
*/
|
*/
|
||||||
private void setFromMat(float m00, float m01, float m02, float m10,
|
private Quaternion setFromMat(float m00, float m01, float m02, float m10,
|
||||||
float m11, float m12, float m20, float m21, float m22) {
|
float m11, float m12, float m20, float m21, float m22) {
|
||||||
|
|
||||||
float s;
|
float s;
|
||||||
|
@ -458,6 +527,6 @@ public class Quaternion extends Vector implements ReadableVector4f {
|
||||||
w = (m10 - m01) * s;
|
w = (m10 - m01) * s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue