Class Quaterniond

java.lang.Object
org.spongepowered.math.imaginary.Quaterniond
All Implemented Interfaces:
Serializable, Comparable<Quaterniond>, Imaginaryd

@Immutable public final class Quaterniond extends Object implements Imaginaryd, Comparable<Quaterniond>, Serializable
Represent a quaternion of the form xi + yj + zk + w.

The x, y, z and w components are stored as doubles. This class is immutable.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Quaterniond
    An immutable identity (0, 0, 0, 1) quaternion.
    static final Quaterniond
    An immutable identity (0, 0, 0, 0) quaternion.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Quaterniond(double x, double y, double z, double w)
    Constructs a new quaternion from the double components.
    Quaterniond(float x, float y, float z, float w)
    Constructs a new quaternion from the float components.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(double x, double y, double z, double w)
    Adds the double components of another quaternion to this one.
    add(float x, float y, float z, float w)
    Adds the float components of another quaternion to this one.
    Adds another quaternion to this one.
    Returns the angles in degrees around the x, y and z axes that correspond to the rotation represented by this quaternion.
    Returns the angles in radians around the x, y and z axes that correspond to the rotation represented by this quaternion.
    Returns the axis of rotation for this quaternion.
    int
     
    Conjugates the quaternion.
    Returns a unit vector representing the direction of this quaternion, which is Vector3d.FORWARD rotated by this quaternion.
    div(double a)
    Divides the components of this quaternion by a double scalar.
    div(double x, double y, double z, double w)
    Divides this quaternions by the double components of another one.
    div(float a)
    Divides the components of this quaternion by a float scalar.
    div(float x, float y, float z, float w)
    Divides this quaternions by the float components of another one.
    Divides this quaternions by another one.
    double
    dot(double x, double y, double z, double w)
    Returns the dot product of this quaternion with the double components of another one.
    double
    dot(float x, float y, float z, float w)
    Returns the dot product of this quaternion with the float components of another one.
    double
    Returns the dot product of this quaternion with another one.
    boolean
    equals(Object other)
     
    from(double x, double y, double z, double w)
    Creates a new quaternion from the double components.
    fromAngleDegAxis(double angle, double x, double y, double z)
    Creates a new quaternion from the rotation double angle in degrees around the axis vector double components.
    fromAngleDegAxis(double angle, Vector3d axis)
    Creates a new quaternion from the rotation double angle in degrees around the axis vector.
    fromAngleDegAxis(float angle, float x, float y, float z)
    Creates a new quaternion from the rotation float angle in degrees around the axis vector float components.
    fromAngleDegAxis(float angle, Vector3d axis)
    Creates a new quaternion from the rotation float angle in degrees around the axis vector.
    fromAngleRadAxis(double angle, double x, double y, double z)
    Creates a new quaternion from the rotation double angle in radians around the axis vector double components.
    fromAngleRadAxis(double angle, Vector3d axis)
    Creates a new quaternion from the rotation double angle in radians around the axis vector.
    fromAngleRadAxis(float angle, float x, float y, float z)
    Creates a new quaternion from the rotation float angle in radians around the axis vector float components.
    fromAngleRadAxis(float angle, Vector3d axis)
    Creates a new quaternion from the rotation float angle in radians around the axis vector.
    fromAxesAnglesDeg(double pitch, double yaw, double roll)
    Creates a new quaternion from the double angles in degrees around the x, y and z axes.
    fromAxesAnglesDeg(float pitch, float yaw, float roll)
    Creates a new quaternion from the float angles in degrees around the x, y and z axes.
    fromAxesAnglesRad(double pitch, double yaw, double roll)
    Creates a new quaternion from the double angles in radians around the x, y and z axes.
    fromAxesAnglesRad(float pitch, float yaw, float roll)
    Creates a new quaternion from the float angles in radians around the x, y and z axes.
    fromImaginary(double x, double y, double z)
    Creates a new quaternion from the double imaginary components.
    fromReal(double w)
    Creates a new quaternion from the double real component.
    Creates a new quaternion from the rotation matrix.
    Creates a new quaternion from the angle-axis rotation defined from the first to the second vector.
    int
     
    Inverts the quaternion.
    double
    Returns the length of this quaternion.
    double
    Returns the square of the length of this quaternion.
    mul(double a)
    Multiplies the components of this quaternion by a double scalar.
    mul(double x, double y, double z, double w)
    Multiplies the double components of another quaternion with this one.
    mul(float a)
    Multiplies the components of this quaternion by a float scalar.
    mul(float x, float y, float z, float w)
    Multiplies the float components of another quaternion with this one.
    Multiplies another quaternion with this one.
    Normalizes this quaternion.
    rotate(double x, double y, double z)
    Rotates the double components of a vector by this quaternion.
    rotate(float x, float y, float z)
    Rotates the float components of a vector by this quaternion.
    Rotates a vector by this quaternion.
    sub(double x, double y, double z, double w)
    Subtracts the double components of another quaternion from this one.
    sub(float x, float y, float z, float w)
    Subtracts the float components of another quaternion from this one.
    Subtracts another quaternion from this one.
    Converts this quaternion to a complex by extracting the rotation around the axis and returning it as a rotation in the plane perpendicular to the rotation axis.
     
     
     
    double
    w()
    Gets the w (real) component of this quaternion.
    double
    x()
    Gets the x (imaginary) component of this quaternion.
    double
    y()
    Gets the y (imaginary) component of this quaternion.
    double
    z()
    Gets the z (imaginary) component of this quaternion.

    Methods inherited from class java.lang.Object

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

    • ZERO

      public static final Quaterniond ZERO
      An immutable identity (0, 0, 0, 0) quaternion.
    • IDENTITY

      public static final Quaterniond IDENTITY
      An immutable identity (0, 0, 0, 1) quaternion.
  • Constructor Details

    • Quaterniond

      public Quaterniond(float x, float y, float z, float w)
      Constructs a new quaternion from the float components.
      Parameters:
      x - The x (imaginary) component
      y - The y (imaginary) component
      z - The z (imaginary) component
      w - The w (real) component
    • Quaterniond

      public Quaterniond(double x, double y, double z, double w)
      Constructs a new quaternion from the double components.
      Parameters:
      x - The x (imaginary) component
      y - The y (imaginary) component
      z - The z (imaginary) component
      w - The w (real) component
  • Method Details

    • x

      public double x()
      Gets the x (imaginary) component of this quaternion.
      Returns:
      The x (imaginary) component
    • y

      public double y()
      Gets the y (imaginary) component of this quaternion.
      Returns:
      The y (imaginary) component
    • z

      public double z()
      Gets the z (imaginary) component of this quaternion.
      Returns:
      The z (imaginary) component
    • w

      public double w()
      Gets the w (real) component of this quaternion.
      Returns:
      The w (real) component
    • add

      public Quaterniond add(Quaterniond q)
      Adds another quaternion to this one.
      Parameters:
      q - The quaternion to add
      Returns:
      A new quaternion, which is the sum of both
    • add

      public Quaterniond add(float x, float y, float z, float w)
      Adds the float components of another quaternion to this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to add
      y - The y (imaginary) component of the quaternion to add
      z - The z (imaginary) component of the quaternion to add
      w - The w (real) component of the quaternion to add
      Returns:
      A new quaternion, which is the sum of both
    • add

      public Quaterniond add(double x, double y, double z, double w)
      Adds the double components of another quaternion to this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to add
      y - The y (imaginary) component of the quaternion to add
      z - The z (imaginary) component of the quaternion to add
      w - The w (real) component of the quaternion to add
      Returns:
      A new quaternion, which is the sum of both
    • sub

      public Quaterniond sub(Quaterniond q)
      Subtracts another quaternion from this one.
      Parameters:
      q - The quaternion to subtract
      Returns:
      A new quaternion, which is the difference of both
    • sub

      public Quaterniond sub(float x, float y, float z, float w)
      Subtracts the float components of another quaternion from this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to subtract
      y - The y (imaginary) component of the quaternion to subtract
      z - The z (imaginary) component of the quaternion to subtract
      w - The w (real) component of the quaternion to subtract
      Returns:
      A new quaternion, which is the difference of both
    • sub

      public Quaterniond sub(double x, double y, double z, double w)
      Subtracts the double components of another quaternion from this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to subtract
      y - The y (imaginary) component of the quaternion to subtract
      z - The z (imaginary) component of the quaternion to subtract
      w - The w (real) component of the quaternion to subtract
      Returns:
      A new quaternion, which is the difference of both
    • mul

      public Quaterniond mul(float a)
      Multiplies the components of this quaternion by a float scalar.
      Parameters:
      a - The multiplication scalar
      Returns:
      A new quaternion, which has each component multiplied by the scalar
    • mul

      public Quaterniond mul(double a)
      Multiplies the components of this quaternion by a double scalar.
      Specified by:
      mul in interface Imaginaryd
      Parameters:
      a - The multiplication scalar
      Returns:
      A new quaternion, which has each component multiplied by the scalar
    • mul

      public Quaterniond mul(Quaterniond q)
      Multiplies another quaternion with this one.
      Parameters:
      q - The quaternion to multiply with
      Returns:
      A new quaternion, which is the product of both
    • mul

      public Quaterniond mul(float x, float y, float z, float w)
      Multiplies the float components of another quaternion with this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to multiply with
      y - The y (imaginary) component of the quaternion to multiply with
      z - The z (imaginary) component of the quaternion to multiply with
      w - The w (real) component of the quaternion to multiply with
      Returns:
      A new quaternion, which is the product of both
    • mul

      public Quaterniond mul(double x, double y, double z, double w)
      Multiplies the double components of another quaternion with this one.
      Parameters:
      x - The x (imaginary) component of the quaternion to multiply with
      y - The y (imaginary) component of the quaternion to multiply with
      z - The z (imaginary) component of the quaternion to multiply with
      w - The w (real) component of the quaternion to multiply with
      Returns:
      A new quaternion, which is the product of both
    • div

      public Quaterniond div(float a)
      Divides the components of this quaternion by a float scalar.
      Parameters:
      a - The division scalar
      Returns:
      A new quaternion, which has each component divided by the scalar
    • div

      public Quaterniond div(double a)
      Divides the components of this quaternion by a double scalar.
      Specified by:
      div in interface Imaginaryd
      Parameters:
      a - The division scalar
      Returns:
      A new quaternion, which has each component divided by the scalar
    • div

      public Quaterniond div(Quaterniond q)
      Divides this quaternions by another one.
      Parameters:
      q - The quaternion to divide with
      Returns:
      The quotient of the two quaternions
    • div

      public Quaterniond div(float x, float y, float z, float w)
      Divides this quaternions by the float components of another one.
      Parameters:
      x - The x (imaginary) component of the quaternion to divide with
      y - The y (imaginary) component of the quaternion to divide with
      z - The z (imaginary) component of the quaternion to divide with
      w - The w (real) component of the quaternion to divide with
      Returns:
      The quotient of the two quaternions
    • div

      public Quaterniond div(double x, double y, double z, double w)
      Divides this quaternions by the double components of another one.
      Parameters:
      x - The x (imaginary) component of the quaternion to divide with
      y - The y (imaginary) component of the quaternion to divide with
      z - The z (imaginary) component of the quaternion to divide with
      w - The w (real) component of the quaternion to divide with
      Returns:
      The quotient of the two quaternions
    • dot

      public double dot(Quaterniond q)
      Returns the dot product of this quaternion with another one.
      Parameters:
      q - The quaternion to calculate the dot product with
      Returns:
      The dot product of the two quaternions
    • dot

      public double dot(float x, float y, float z, float w)
      Returns the dot product of this quaternion with the float components of another one.
      Parameters:
      x - The x (imaginary) component of the quaternion to calculate the dot product with
      y - The y (imaginary) component of the quaternion to calculate the dot product with
      z - The z (imaginary) component of the quaternion to calculate the dot product with
      w - The w (real) component of the quaternion to calculate the dot product with
      Returns:
      The dot product of the two quaternions
    • dot

      public double dot(double x, double y, double z, double w)
      Returns the dot product of this quaternion with the double components of another one.
      Parameters:
      x - The x (imaginary) component of the quaternion to calculate the dot product with
      y - The y (imaginary) component of the quaternion to calculate the dot product with
      z - The z (imaginary) component of the quaternion to calculate the dot product with
      w - The w (real) component of the quaternion to calculate the dot product with
      Returns:
      The dot product of the two quaternions
    • rotate

      public Vector3d rotate(Vector3d v)
      Rotates a vector by this quaternion.
      Parameters:
      v - The vector to rotate
      Returns:
      The rotated vector
    • rotate

      public Vector3d rotate(float x, float y, float z)
      Rotates the float components of a vector by this quaternion.
      Parameters:
      x - The x component of the vector
      y - The y component of the vector
      z - The z component of the vector
      Returns:
      The rotated vector
    • rotate

      public Vector3d rotate(double x, double y, double z)
      Rotates the double components of a vector by this quaternion.
      Parameters:
      x - The x component of the vector
      y - The y component of the vector
      z - The z component of the vector
      Returns:
      The rotated vector
    • direction

      public Vector3d direction()
      Returns a unit vector representing the direction of this quaternion, which is Vector3d.FORWARD rotated by this quaternion.
      Returns:
      The vector representing the direction this quaternion is pointing to
    • axis

      public Vector3d axis()
      Returns the axis of rotation for this quaternion.
      Returns:
      The axis of rotation
    • axesAnglesDeg

      public Vector3d axesAnglesDeg()
      Returns the angles in degrees around the x, y and z axes that correspond to the rotation represented by this quaternion.
      Returns:
      The angle in degrees for each axis, stored in a vector, in the corresponding component
    • axesAnglesRad

      public Vector3d axesAnglesRad()
      Returns the angles in radians around the x, y and z axes that correspond to the rotation represented by this quaternion.
      Returns:
      The angle in radians for each axis, stored in a vector, in the corresponding component
    • conjugate

      public Quaterniond conjugate()
      Conjugates the quaternion.

      Conjugation of a quaternion a is an operation returning quaternion a' such that a' * a = a * a' = |a|<sup>2</sup> where |a|<sup>2</sup> is squared length of a.

      Specified by:
      conjugate in interface Imaginaryd
      Returns:
      the conjugated quaternion
    • invert

      public Quaterniond invert()
      Inverts the quaternion.

      Inversion of a quaternion a returns quaternion a<sup>-1</sup> = a' / |a|<sup>2</sup> where a' is conjugation of a, and |a|<sup>2</sup> is squared length of a.

      For any quaternions a, b, c such that a * b = c, the equations a<sup>-1</sup> * c = b and c * b<sup>-1</sup> = a are true.

      Specified by:
      invert in interface Imaginaryd
      Returns:
      the inverted quaternion
    • lengthSquared

      public double lengthSquared()
      Returns the square of the length of this quaternion.
      Specified by:
      lengthSquared in interface Imaginaryd
      Returns:
      The square of the length
    • length

      public double length()
      Returns the length of this quaternion.
      Specified by:
      length in interface Imaginaryd
      Returns:
      The length
    • normalize

      public Quaterniond normalize()
      Normalizes this quaternion.
      Specified by:
      normalize in interface Imaginaryd
      Returns:
      A new quaternion of unit length
    • toComplex

      public Complexd toComplex()
      Converts this quaternion to a complex by extracting the rotation around the axis and returning it as a rotation in the plane perpendicular to the rotation axis.
      Returns:
      The rotation without the axis as a complex
    • toFloat

      public Quaternionf toFloat()
      Specified by:
      toFloat in interface Imaginaryd
    • toDouble

      public Quaterniond toDouble()
      Specified by:
      toDouble in interface Imaginaryd
    • equals

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

      public int hashCode()
      Overrides:
      hashCode in class Object
    • compareTo

      public int compareTo(Quaterniond q)
      Specified by:
      compareTo in interface Comparable<Quaterniond>
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • fromReal

      public static Quaterniond fromReal(double w)
      Creates a new quaternion from the double real component.

      The ZERO constant is re-used when w is 0.

      Parameters:
      w - The w (real) component
      Returns:
      The quaternion created from the double real component
    • fromImaginary

      public static Quaterniond fromImaginary(double x, double y, double z)
      Creates a new quaternion from the double imaginary components.

      The ZERO constant is re-used when x, y, and z are 0.

      Parameters:
      x - The x (imaginary) component
      y - The y (imaginary) component
      z - The z (imaginary) component
      Returns:
      The quaternion created from the double imaginary components
    • from

      public static Quaterniond from(double x, double y, double z, double w)
      Creates a new quaternion from the double components.

      The ZERO constant is re-used when x, y, z, and w are 0.

      Parameters:
      x - The x (imaginary) component
      y - The y (imaginary) component
      z - The z (imaginary) component
      w - The w (real) component
      Returns:
      The quaternion created from the double components
    • fromAxesAnglesDeg

      public static Quaterniond fromAxesAnglesDeg(float pitch, float yaw, float roll)
      Creates a new quaternion from the float angles in degrees around the x, y and z axes.
      Parameters:
      pitch - The rotation around x
      yaw - The rotation around y
      roll - The rotation around z
      Returns:
      The quaternion defined by the rotations around the axes
    • fromAxesAnglesRad

      public static Quaterniond fromAxesAnglesRad(float pitch, float yaw, float roll)
      Creates a new quaternion from the float angles in radians around the x, y and z axes.
      Parameters:
      pitch - The rotation around x
      yaw - The rotation around y
      roll - The rotation around z
      Returns:
      The quaternion defined by the rotations around the axes
    • fromAxesAnglesDeg

      public static Quaterniond fromAxesAnglesDeg(double pitch, double yaw, double roll)
      Creates a new quaternion from the double angles in degrees around the x, y and z axes.
      Parameters:
      pitch - The rotation around x
      yaw - The rotation around y
      roll - The rotation around z
      Returns:
      The quaternion defined by the rotations around the axes
    • fromAxesAnglesRad

      public static Quaterniond fromAxesAnglesRad(double pitch, double yaw, double roll)
      Creates a new quaternion from the double angles in radians around the x, y and z axes.
      Parameters:
      pitch - The rotation around x
      yaw - The rotation around y
      roll - The rotation around z
      Returns:
      The quaternion defined by the rotations around the axes
    • fromRotationTo

      public static Quaterniond fromRotationTo(Vector3d from, Vector3d to)
      Creates a new quaternion from the angle-axis rotation defined from the first to the second vector.
      Parameters:
      from - The first vector
      to - The second vector
      Returns:
      The quaternion defined by the angle-axis rotation between the vectors
    • fromAngleDegAxis

      public static Quaterniond fromAngleDegAxis(float angle, Vector3d axis)
      Creates a new quaternion from the rotation float angle in degrees around the axis vector.
      Parameters:
      angle - The rotation angle in degrees
      axis - The axis of rotation
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleRadAxis

      public static Quaterniond fromAngleRadAxis(float angle, Vector3d axis)
      Creates a new quaternion from the rotation float angle in radians around the axis vector.
      Parameters:
      angle - The rotation angle in radians
      axis - The axis of rotation
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleDegAxis

      public static Quaterniond fromAngleDegAxis(double angle, Vector3d axis)
      Creates a new quaternion from the rotation double angle in degrees around the axis vector.
      Parameters:
      angle - The rotation angle in degrees
      axis - The axis of rotation
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleRadAxis

      public static Quaterniond fromAngleRadAxis(double angle, Vector3d axis)
      Creates a new quaternion from the rotation double angle in radians around the axis vector.
      Parameters:
      angle - The rotation angle in radians
      axis - The axis of rotation
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleDegAxis

      public static Quaterniond fromAngleDegAxis(float angle, float x, float y, float z)
      Creates a new quaternion from the rotation float angle in degrees around the axis vector float components.
      Parameters:
      angle - The rotation angle in degrees
      x - The x component of the axis vector
      y - The y component of the axis vector
      z - The z component of the axis vector
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleRadAxis

      public static Quaterniond fromAngleRadAxis(float angle, float x, float y, float z)
      Creates a new quaternion from the rotation float angle in radians around the axis vector float components.
      Parameters:
      angle - The rotation angle in radians
      x - The x component of the axis vector
      y - The y component of the axis vector
      z - The z component of the axis vector
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleDegAxis

      public static Quaterniond fromAngleDegAxis(double angle, double x, double y, double z)
      Creates a new quaternion from the rotation double angle in degrees around the axis vector double components.
      Parameters:
      angle - The rotation angle in degrees
      x - The x component of the axis vector
      y - The y component of the axis vector
      z - The z component of the axis vector
      Returns:
      The quaternion defined by the rotation around the axis
    • fromAngleRadAxis

      public static Quaterniond fromAngleRadAxis(double angle, double x, double y, double z)
      Creates a new quaternion from the rotation double angle in radians around the axis vector double components.
      Parameters:
      angle - The rotation angle in radians
      x - The x component of the axis vector
      y - The y component of the axis vector
      z - The z component of the axis vector
      Returns:
      The quaternion defined by the rotation around the axis
    • fromRotationMatrix

      public static Quaterniond fromRotationMatrix(Matrix3d matrix)
      Creates a new quaternion from the rotation matrix. The matrix will be interpreted as a rotation matrix even if it is not.
      Parameters:
      matrix - The rotation matrix
      Returns:
      The quaternion defined by the rotation matrix