Package util

Class Quaternion

java.lang.Object
util.Quaternion

public class Quaternion extends Object
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
    The real component (x0).
    protected double
    The 1st imaginary component (x1).
    protected double
    The 2nd imaginary component (x2).
    protected double
    The 3rd imaginary component (x3).
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an identity quaternion: all components are zero except x0, which is set to 1.
    Quaternion(double[] angles)
    Creates a quaternion from Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
    Quaternion(double w, double x, double y, double z)
    Creates a quaternion with the specified components.
     
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    fromDegreeAngles(double[] angles)
    Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
    fromDegreeAngles(double xAngle, double yAngle, double zAngle)
    Sets the quaternion from three angles in degree
    fromRadiansAngles(double[] angles)
    Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
    fromRadiansAngles(double xAngle, double yAngle, double zAngle)
    Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
    Sets the quaternion from a rotation matrix with the specified elements.
    double
    Returns the w (real) component.
    double
    Returns the x component.
    double
    Returns the y component.
    double
    Returns the z component.
    int
     
    boolean
    Compares with the identity quaternion, without distinguishing -0 from 0.
    void
    Sets all components to zero except w, which is set to 1.
    double
    Returns the norm, defined as the dot product of the quaternion with itself.
    set(double w, double x, double y, double z)
    Sets all 4 components to specified values.
    Copies all 4 components from another quaternion.
    slerp(Quaternion q1, Quaternion q2, float t)
    Interpolates between the specified quaternions and stores the result in the current instance.
    Subtracts the argument and returns difference as a new instance.
    double[]
    toDegreesAngles(double[] angles)
    Converts to equivalent Tait-Bryan angles in degree
    double[]
    toRadiansAngles(double[] angles)
    Converts to equivalent Tait-Bryan angles, to be applied in x-z-y intrinsic order or y-z'-x" extrinsic order, for instance by fromRadiansAngles(double[]) (double[])}.
    Converts to an equivalent rotation matrix.
    Helper function to print the values of the quaternion

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • w

      protected double w
      The real component (x0).
    • x

      protected double x
      The 1st imaginary component (x1).
    • y

      protected double y
      The 2nd imaginary component (x2).
    • z

      protected double z
      The 3rd imaginary component (x3).
  • Constructor Details

    • Quaternion

      public Quaternion()
      Creates an identity quaternion: all components are zero except x0, which is set to 1.
    • Quaternion

      public Quaternion(double w, double x, double y, double z)
      Creates a quaternion with the specified components.
      Parameters:
      w - the x0 component
      x - the x1 component
      y - the x2 component
      z - the x3 component
    • Quaternion

      public Quaternion(double[] angles)
      Creates a quaternion from Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
      Parameters:
      angles - an array of Tait-Bryan angles (in degree, exactly 3 elements, the X angle in angles[0], the Y angle in angles[1], and the Z angle in angles[2], not null, unaffected)
    • Quaternion

      public Quaternion(Matrix3D matrix)
  • Method Details

    • getW

      public double getW()
      Returns the w (real) component.
      Returns:
      the value of the w component
    • getX

      public double getX()
      Returns the x component.
      Returns:
      the value of the x component
    • getY

      public double getY()
      Returns the y component.
      Returns:
      the value of the y component
    • getZ

      public double getZ()
      Returns the z component.
      Returns:
      the value of the z component
    • set

      public Quaternion set(double w, double x, double y, double z)
      Sets all 4 components to specified values.
      Parameters:
      w - the w (real) component
      x - the x component
      y - the y component
      z - the z component
      Returns:
      the (modified) current instance
    • set

      public Quaternion set(Quaternion q)
      Copies all 4 components from another quaternion.
      Parameters:
      q - the quaternion to copy (not null, unaffected)
      Returns:
      the (modified) current instance
    • makeIdentityQuaternion

      public void makeIdentityQuaternion()
      Sets all components to zero except w, which is set to 1.
    • isIdentity

      public boolean isIdentity()
      Compares with the identity quaternion, without distinguishing -0 from 0. The current instance is unaffected.
      Returns:
      true if the current quaternion equals the identity, otherwise false
    • fromRadiansAngles

      public Quaternion fromRadiansAngles(double[] angles)
      Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
      Parameters:
      angles - an array of Tait-Bryan angles (in radians, exactly 3 elements, the X angle in angles[0], the Y angle in angles[1], and the Z angle in angles[2], not null, unaffected)
      Returns:
      the (modified) current instance (for chaining)
      Throws:
      IllegalArgumentException - if angles.length != 3
    • fromDegreeAngles

      public Quaternion fromDegreeAngles(double[] angles)
      Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
      Parameters:
      angles - an array of Tait-Bryan angles (in degree, exactly 3 elements, the X angle in angles[0], the Y angle in angles[1], and the Z angle in angles[2], not null, unaffected)
      Returns:
      the (modified) current instance (for chaining)
      Throws:
      IllegalArgumentException - if angles.length != 3
    • fromRadiansAngles

      public Quaternion fromRadiansAngles(double xAngle, double yAngle, double zAngle)
      Sets the quaternion from the specified Tait-Bryan angles, applying the rotations in x-z-y extrinsic order or y-z'-x" intrinsic order.
      Parameters:
      xAngle - the X angle (in radians)
      yAngle - the Y angle (in radians)
      zAngle - the Z angle (in radians)
      Returns:
      the (modified) current instance (for chaining)
      See Also:
    • fromDegreeAngles

      public Quaternion fromDegreeAngles(double xAngle, double yAngle, double zAngle)
      Sets the quaternion from three angles in degree
      Parameters:
      xAngle - the X angle (in degree)
      yAngle - the Y angle (in degree)
      zAngle - the Z angle (in degree)
      Returns:
      the (modified) current instance (for chaining)
    • toRadiansAngles

      public double[] toRadiansAngles(double[] angles)
      Converts to equivalent Tait-Bryan angles, to be applied in x-z-y intrinsic order or y-z'-x" extrinsic order, for instance by fromRadiansAngles(double[]) (double[])}. The current instance is unaffected.
      Parameters:
      angles - storage for the result, or null for a new float[3]
      Returns:
      an array of 3 angles (in radians, either angles or a new float[3], the X angle in angles[0], the Y angle in angles[1], and the Z angle in angles[2])
      Throws:
      IllegalArgumentException - if angles.length != 3
      See Also:
    • toDegreesAngles

      public double[] toDegreesAngles(double[] angles)
      Converts to equivalent Tait-Bryan angles in degree
      Parameters:
      angles - storage for the result, or null for a new float[3]
      Returns:
      an array of 3 angles (in degree)
    • fromRotationMatrix

      public Quaternion fromRotationMatrix(Matrix3D matrix)
      Sets the quaternion from a rotation matrix with the specified elements.
      Parameters:
      matrix - a Matrix3D object
      Returns:
      the (modified) current instance (for chaining)
    • toRotationMatrix

      public Matrix3D toRotationMatrix()
      Converts to an equivalent rotation matrix. The current instance is unaffected.

      Note: the result is created from a normalized version..

      Returns:
      result, configured as a 3x3 rotation matrix
    • norm

      public double norm()
      Returns the norm, defined as the dot product of the quaternion with itself. The current instance is unaffected.
      Returns:
      the sum of the squared components (not negative)
    • subtract

      public Quaternion subtract(Quaternion q)
      Subtracts the argument and returns difference as a new instance. The current instance is unaffected.
      Parameters:
      q - the quaternion to subtract (not null, unaffected)
      Returns:
      a new Quaternion
    • slerp

      public Quaternion slerp(Quaternion q1, Quaternion q2, float t)
      Interpolates between the specified quaternions and stores the result in the current instance.
      Parameters:
      q1 - the desired value when interp=0 (not null, unaffected)
      q2 - the desired value when interp=1 (not null, may be modified)
      t - the fractional change amount
      Returns:
      the (modified) current instance (for chaining)
    • toString

      public String toString()
      Helper function to print the values of the quaternion
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object