toxi.geom
Class Quaternion

java.lang.Object
  extended by toxi.geom.Quaternion

public class Quaternion
extends java.lang.Object

Quaternion implementation with SLERP based on http://is.gd/2n9s


Field Summary
static float DOT_THRESHOLD
           
 float w
           
 float x
           
 float y
           
 float z
           
 
Constructor Summary
Quaternion()
           
Quaternion(float w, float x, float y, float z)
           
Quaternion(float w, Vec3D v)
           
Quaternion(Quaternion q)
           
 
Method Summary
 Quaternion add(Quaternion q)
           
 Quaternion addSelf(Quaternion q)
           
 Quaternion copy()
           
static Quaternion createFromAxisAngle(Vec3D axis, float angle)
          Creates a Quaternion from a axis and a angle.
static Quaternion createFromEuler(float pitch, float yaw, float roll)
          Creates a Quaternion from Euler angles.
static Quaternion createFromMatrix(Matrix4x4 m)
           
 float dot(Quaternion q)
          Computes the dot product with the given quaternion.
 Quaternion getConjugate()
          Computes this quaternion's conjugate, defined as the same w around the inverted axis.
 Matrix4x4 getMatrix()
          Converts the quat into a 4x4 Matrix.
 Quaternion getNormalized()
          Computes normalized version of this quaternion.
 Quaternion identity()
           
 Quaternion interpolateTo(Quaternion target, float t)
          Spherical interpolation to target quaternion (code ported from GamaSutra)
 Quaternion interpolateTo(Quaternion target, float t, InterpolateStrategy is)
           
 Quaternion interpolateToSelf(Quaternion target, float t)
          Spherical interpolation to target quaternion (code ported from GamaSutra)
 Quaternion interpolateToSelf(Quaternion target, float t, InterpolateStrategy is)
          Uses spherical interpolation to approach the target quaternion.
 float magnitude()
           
 Quaternion multiply(Quaternion q2)
           
 Quaternion normalize()
           
 Quaternion scale(float t)
           
 Quaternion scaleSelf(float t)
           
 Quaternion set(float w, float x, float y, float z)
           
 Quaternion set(float w, Vec3D v)
           
 Quaternion set(Quaternion q)
           
 Quaternion sub(Quaternion q)
           
 Quaternion subSelf(Quaternion q)
           
 float[] toArray()
           
 float[] toAxisAngle()
          Converts the quaternion into a float array consisting of: rotation angle in radians, rotation axis x,y,z
 Matrix4x4 toMatrix4x4()
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DOT_THRESHOLD

public static final float DOT_THRESHOLD
See Also:
Constant Field Values

x

public float x

y

public float y

z

public float z

w

public float w
Constructor Detail

Quaternion

public Quaternion()

Quaternion

public Quaternion(float w,
                  float x,
                  float y,
                  float z)

Quaternion

public Quaternion(float w,
                  Vec3D v)

Quaternion

public Quaternion(Quaternion q)
Method Detail

createFromAxisAngle

public static Quaternion createFromAxisAngle(Vec3D axis,
                                             float angle)
Creates a Quaternion from a axis and a angle.

Parameters:
axis - axis vector (will be normalized)
angle - angle in radians.
Returns:
new quaternion

createFromEuler

public static Quaternion createFromEuler(float pitch,
                                         float yaw,
                                         float roll)
Creates a Quaternion from Euler angles.

Parameters:
pitch - X-angle in radians.
yaw - Y-angle in radians.
roll - Z-angle in radians.
Returns:
new quaternion

createFromMatrix

public static Quaternion createFromMatrix(Matrix4x4 m)

add

public Quaternion add(Quaternion q)

addSelf

public Quaternion addSelf(Quaternion q)

copy

public Quaternion copy()

dot

public float dot(Quaternion q)
Computes the dot product with the given quaternion.

Parameters:
q -
Returns:
dot product

getConjugate

public Quaternion getConjugate()
Computes this quaternion's conjugate, defined as the same w around the inverted axis.

Returns:
new conjugate quaternion

getMatrix

public Matrix4x4 getMatrix()
Converts the quat into a 4x4 Matrix. Assumes the quat is currently normalized (if not, you'll need to call normalize() first). This calculation would be a lot more complicated for non-unit length quaternions Note: The constructor of Matrix4 expects the Matrix in column-major format like expected by OpenGL

Returns:
result matrix

getNormalized

public Quaternion getNormalized()
Computes normalized version of this quaternion.

Returns:
new normalized quaternion

identity

public Quaternion identity()

interpolateTo

public Quaternion interpolateTo(Quaternion target,
                                float t)
Spherical interpolation to target quaternion (code ported from GamaSutra)

Parameters:
target - quaternion
t - interpolation factor (0..1)
Returns:
new interpolated quat

interpolateTo

public Quaternion interpolateTo(Quaternion target,
                                float t,
                                InterpolateStrategy is)
Parameters:
target -
t -
is -
Returns:
interpolated quaternion as new instance

interpolateToSelf

public Quaternion interpolateToSelf(Quaternion target,
                                    float t)
Spherical interpolation to target quaternion (code ported from GamaSutra)

Parameters:
target - quaternion
t - interpolation factor (0..1)
Returns:
new interpolated quat

interpolateToSelf

public Quaternion interpolateToSelf(Quaternion target,
                                    float t,
                                    InterpolateStrategy is)
Uses spherical interpolation to approach the target quaternion. The interpolation factor is manipulated by the chosen InterpolateStrategy first.

Parameters:
target -
t -
is -
Returns:
itself

magnitude

public float magnitude()

multiply

public Quaternion multiply(Quaternion q2)

normalize

public Quaternion normalize()

scale

public Quaternion scale(float t)

scaleSelf

public Quaternion scaleSelf(float t)

set

public Quaternion set(float w,
                      float x,
                      float y,
                      float z)

set

public Quaternion set(float w,
                      Vec3D v)

set

public Quaternion set(Quaternion q)

sub

public Quaternion sub(Quaternion q)

subSelf

public Quaternion subSelf(Quaternion q)

toArray

public float[] toArray()

toAxisAngle

public float[] toAxisAngle()
Converts the quaternion into a float array consisting of: rotation angle in radians, rotation axis x,y,z

Returns:
4-element float array

toMatrix4x4

public Matrix4x4 toMatrix4x4()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object