Class Quaternion

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

public class Quaternion extends Object implements MatrixTransformation
Quaternion models a unit quaternion and implements quaternion arithmetic.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    protected static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    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.
    Constructs and initializes a Quaternion with the same values as the given quaternion.
  • Method Summary

    Modifier and Type
    Method
    Description
    final void
    Adds this quaternion to the given quaternion.
    final double
    Returns the angle in radians between this quaternion and the given quaternion.
    Instaniates a quaterion whose components are identical to this quaterion.
    final void
    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.
    final double
    Returns the dot product of this quaternion and quaternion q.
    double[]
    Gets the Quaternion coordinates.
    final double[]
    getFlatMatrix(double[] mat)
    Gets the direct homogeneous affine transformation flattened into a 1-d array.
     
    double[]
    Returns the origin for this rotation
    final double[][]
    getRotationMatrix(double[][] mat)
    Gets the direct rotation matrix of this quaternion rotation.
    double[]
    inverse(double[] p)
    The inverse transformation (if it exists).
    final double
    Returns the magnitude of this quaternion.
    final double
    Returns the squared magnitude of this vector.
    final void
    Multiplies this quaternion with the given quaternion.
    final void
    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.
    final void
    Subtracts this quaternion from the given quaternion.

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • q0

      protected double q0
    • q1

      protected double q1
    • q2

      protected double q2
    • q3

      protected double q3
    • ox

      protected double ox
    • oy

      protected double oy
    • oz

      protected double oz
  • Constructor Details

    • 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 Details

    • 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[]
    • getOrigin

      public double[] getOrigin()
      Returns the origin for this rotation
      Returns:
    • getRotationMatrix

      public final double[][] getRotationMatrix(double[][] mat)
      Gets the direct rotation matrix of this quaternion rotation. Assumes that the quaternion 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 array. 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 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 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 Object clone()
      Instaniates a quaterion whose components are identical to this quaterion.
      Specified by:
      clone in interface Transformation
      Overrides:
      clone in class 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 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:
      UnsupportedOperationException - If the transformation is not invertible
    • getLoader

      public static XML.ObjectLoader getLoader()