org.opensourcephysics.numerics
Class Quaternion

java.lang.Object
  extended by org.opensourcephysics.numerics.Quaternion
All Implemented Interfaces:
java.lang.Cloneable, MatrixTransformation, Transformation

public class Quaternion
extends java.lang.Object
implements MatrixTransformation

Quaterion models a unit quaternion and implements quaternion arithmetic.


Nested Class Summary
protected static class Quaternion.QuaternionLoader
           
 
Constructor Summary
Quaternion()
          Constructs and initializes a unit quaternion (1,0,0,0).
Quaternion(double[] q)
          Constructs and initializes a quaternion from the array of length 4.
Quaternion(double q0, double q1, double q2, double q3)
          Constructs and initializes quaternion from the specified components.
Quaternion(double q0, Vec3D vector)
          Constructs and initializes quaternion from the specified components.
Quaternion(Quaternion q)
          Constructs and initializes a Quaternion with the same values as the given quaternion.
 
Method Summary
 void add(Quaternion q)
          Adds this quaternion to the given quaternion.
 double angle(Quaternion q)
          Returns the angle in radians between this quaternion and the given quaternion.
 java.lang.Object clone()
          Instaniates a quaterion whose components are identical to this quaterion.
 void conjugate()
          Conjugates this quaternion in place.
static Quaternion createAlignmentTransformation(double[] v1, double[] v2)
          Instantiates a quaternion that aligns the first vector with the second vector.
 double[] direct(double[] p)
          Transforms (rotates) the coordinates of the given point.
 double dot(Quaternion q)
          Returns the dot product of this quaternion and quaternion q.
 double[] getCoordinates()
          Gets the Quaternion coordinates.
 double[] getFlatMatrix(double[] mat)
          Gets the direct homogeneous affine transformation flattened into a 1-d arrray.
static XML.ObjectLoader getLoader()
           
 double[][] getRotationMatrix(double[][] mat)
          Gets the direct rotation matrix of this quaternion rotation.
 double[] inverse(double[] p)
          The inverse transformation (if it exists).
 double magnitude()
          Returns the magnitude of this quaternion.
 double magnitudeSquared()
          Returns the squared magnitude of this vector.
 void multiply(Quaternion q)
          Multiplies this quaternion with the given quaternion.
 void normalize()
          Normalizes this quaternion in place.
 double[] setCoordinates(double[] q)
          Sets the quaternion coordinates from the array of length 4.
 void setCoordinates(double q0, double q1, double q2, double q3)
          Sets the quaternion coordinates.
 double[] setOrigin(double[] origin)
          Sets the origin for this rotation.
 void setOrigin(double ox, double oy, double oz)
          Sets the origin for this rotation.
 void subtract(Quaternion q)
          Subtracts this quaternion from the given quaternion.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Quaternion

public Quaternion(double q0,
                  double q1,
                  double q2,
                  double q3)
Constructs and initializes quaternion from the specified components.

Parameters:
q0 - double
q1 - double
q2 - double
q3 - double

Quaternion

public Quaternion(double q0,
                  Vec3D vector)
Constructs and initializes quaternion from the specified components.

Parameters:
q0 - double
vector - sets with q1:vector.x, q2:vector.y, q3:vector.z

Quaternion

public Quaternion(double[] q)
Constructs and initializes a quaternion from the array of length 4.

Parameters:
q - the array of length 4 containing q0, q1, q2, q3

Quaternion

public Quaternion(Quaternion q)
Constructs and initializes a Quaternion with the same values as the given quaternion.

Parameters:
q - the Vector3d containing the initialization x y z data

Quaternion

public Quaternion()
Constructs and initializes a unit quaternion (1,0,0,0).

Method Detail

createAlignmentTransformation

public static Quaternion createAlignmentTransformation(double[] v1,
                                                       double[] v2)
Instantiates a quaternion that aligns the first vector with the second vector.

Parameters:
v1 - double[]
v2 - double[]
Returns:
Quaternion

setOrigin

public void setOrigin(double ox,
                      double oy,
                      double oz)
Sets the origin for this rotation.

Parameters:
ox - double
oy - double
oz - double

setOrigin

public double[] setOrigin(double[] origin)
Sets the origin for this rotation.

Parameters:
origin - double[] the new origin
Returns:
double[]

getRotationMatrix

public final double[][] getRotationMatrix(double[][] mat)
Gets the direct rotation matrix of this quaternion rotation. Assumes that the quaterion has been normalized. If the mat parameter is null a double[3][3] array is created; otherwise the given array is used.

Parameters:
mat - double[][] optional matrix
Returns:
double[][] the matrix

getFlatMatrix

public final double[] getFlatMatrix(double[] mat)
Gets the direct homogeneous affine transformation flattened into a 1-d arrray. If the mat parameter is null a double[16] array is created; otherwise the given array is used.

Specified by:
getFlatMatrix in interface MatrixTransformation
Parameters:
mat - double[] optional matrix
Returns:
double[] the matrix

getCoordinates

public double[] getCoordinates()
Gets the Quaternion coordinates.

Returns:
double[]

setCoordinates

public final void setCoordinates(double q0,
                                 double q1,
                                 double q2,
                                 double q3)
Sets the quaternion coordinates.

Parameters:
q0 - double
q1 - double
q2 - double
q3 - double

setCoordinates

public final double[] setCoordinates(double[] q)
Sets the quaternion coordinates from the array of length 4.

Parameters:
q - the array of length 4 containing q0, q1, q2, q3

normalize

public final void normalize()
Normalizes this quaternion in place.


conjugate

public final void conjugate()
Conjugates this quaternion in place.


add

public final void add(Quaternion q)
Adds this quaternion to the given quaternion.

Parameters:
q - Quaternion

subtract

public final void subtract(Quaternion q)
Subtracts this quaternion from the given quaternion.

Parameters:
q - Quaternion

multiply

public final void multiply(Quaternion q)
Multiplies this quaternion with the given quaternion.

Parameters:
q - Quaternion

dot

public final double dot(Quaternion q)
Returns the dot product of this quaternion and quaternion q.

Parameters:
q - the other quaternion
Returns:
the dot product of this and q

magnitudeSquared

public final double magnitudeSquared()
Returns the squared magnitude of this vector.

Returns:
the squared magnitude

magnitude

public final double magnitude()
Returns the magnitude of this quaternion.

Returns:
the magnitude

angle

public final double angle(Quaternion q)
Returns the angle in radians between this quaternion and the given quaternion.

Parameters:
q - the other quaternion
Returns:
the angle in radians in the range [0,PI]

clone

public java.lang.Object clone()
Instaniates a quaterion whose components are identical to this quaterion.

Specified by:
clone in interface Transformation
Overrides:
clone in class java.lang.Object
Returns:
Object

direct

public double[] direct(double[] p)
Transforms (rotates) the coordinates of the given point.

Specified by:
direct in interface Transformation
Parameters:
p - double[]
Returns:
double[]

inverse

public double[] inverse(double[] p)
                 throws java.lang.UnsupportedOperationException
Description copied from interface: Transformation
The inverse transformation (if it exists). If the transformation is not invertible, then a call to this method must throw a UnsupportedOperationException exception.

Specified by:
inverse in interface Transformation
Parameters:
p - double[] the coordinates to be transformed (the array's contents will be changed accordingly)
Returns:
double[] the transformed vector (i.e. point)
Throws:
java.lang.UnsupportedOperationException - If the transformation is not invertible

getLoader

public static XML.ObjectLoader getLoader()