Package org.opensourcephysics.numerics
Class Quaternion
java.lang.Object
org.opensourcephysics.numerics.Quaternion
- All Implemented Interfaces:
Cloneable
,MatrixTransformation
,Transformation
Quaternion models a unit quaternion and implements quaternion arithmetic.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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 TypeMethodDescriptionfinal void
add
(Quaternion q) Adds this quaternion to the given quaternion.final double
angle
(Quaternion q) Returns the angle in radians between this quaternion and the given quaternion.clone()
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
dot
(Quaternion q) 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.static XML.ObjectLoader
double[]
Returns the origin for this rotationfinal 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.
-
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
- doubleq1
- doubleq2
- doubleq3
- double
-
Quaternion
Constructs and initializes quaternion from the specified components.- Parameters:
q0
- doublevector
- 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
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
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
- doubleoy
- doubleoz
- 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 interfaceMatrixTransformation
- 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
- doubleq1
- doubleq2
- doubleq3
- 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
Adds this quaternion to the given quaternion.- Parameters:
q
- Quaternion
-
subtract
Subtracts this quaternion from the given quaternion.- Parameters:
q
- Quaternion
-
multiply
Multiplies this quaternion with the given quaternion.- Parameters:
q
- Quaternion
-
dot
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
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
Instaniates a quaterion whose components are identical to this quaterion.- Specified by:
clone
in interfaceTransformation
- Overrides:
clone
in classObject
- Returns:
- Object
-
direct
public double[] direct(double[] p) Transforms (rotates) the coordinates of the given point.- Specified by:
direct
in interfaceTransformation
- Parameters:
p
- double[]- Returns:
- double[]
-
inverse
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 interfaceTransformation
- 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
-