public class Quaternion extends Object
All matrix operation provided are in column-major order,
as specified in the OpenGL fixed function pipeline, i.e. compatibility profile.
See FloatUtil
.
See Matrix-FAQ
Modifier and Type | Field and Description |
---|---|
static float |
ALLOWED_DEVIANCE
Quaternion Epsilon, used with equals method to determine if two Quaternions are close enough to be considered equal.
|
Constructor and Description |
---|
Quaternion() |
Quaternion(float x,
float y,
float z,
float w) |
Quaternion(Quaternion q) |
Modifier and Type | Method and Description |
---|---|
Quaternion |
add(Quaternion q)
Add a quaternion
|
Quaternion |
conjugate()
Conjugates this quaternion
[-x, -y, -z, w] . |
float[] |
copyMatrixColumn(int index,
float[] result,
int resultOffset) |
float |
dot(float x,
float y,
float z,
float w)
Returns the dot product of this quaternion with the given x,y,z and w components.
|
float |
dot(Quaternion quat)
Returns the dot product of this quaternion with the given quaternion
|
boolean |
equals(Object o) |
float |
getW() |
float |
getX() |
float |
getY() |
float |
getZ() |
int |
hashCode() |
Quaternion |
invert()
Invert the quaternion If rotational, will produce a the inverse rotation
|
boolean |
isIdentity()
Returns
true if this quaternion has identity. |
boolean |
isRotationMatrix3f(float[] m)
Check if the the 3x3 matrix (param) is in fact an affine rotational
matrix
|
float |
magnitude()
Return the magnitude of this quaternion, i.e.
|
float |
magnitudeSquared()
See
magnitude() for special handling of epsilon ,
which is not applied here. |
Quaternion |
mult(Quaternion q)
Multiply this quaternion by the param quaternion
|
Quaternion |
normalize()
Normalize a quaternion required if to be used as a rotational quaternion.
|
Quaternion |
rotateByAngleNormalAxis(float angle,
float axisX,
float axisY,
float axisZ)
Rotate this quaternion by the given angle and axis.
|
Quaternion |
rotateByAngleX(float angle)
Rotate this quaternion around X axis with the given angle in radians
|
Quaternion |
rotateByAngleY(float angle)
Rotate this quaternion around Y axis with the given angle in radians
|
Quaternion |
rotateByAngleZ(float angle)
Rotate this quaternion around Z axis with the given angle in radians
|
Quaternion |
rotateByEuler(float[] angradXYZ)
Rotates this quaternion from the given Euler rotation array
angradXYZ in radians. |
Quaternion |
rotateByEuler(float bankX,
float headingY,
float attitudeZ)
Rotates this quaternion from the given Euler rotation angles in radians.
|
float[] |
rotateVector(float[] vecOut,
int vecOutOffset,
float[] vecIn,
int vecInOffset)
Rotate the given vector by this quaternion
|
Quaternion |
scale(float n)
Scale this quaternion by a constant
|
Quaternion |
set(float x,
float y,
float z,
float w)
Set all values of this quaternion using the given components.
|
Quaternion |
set(Quaternion src)
Set all values of this quaternion using the given src.
|
Quaternion |
setFromAngleAxis(float angle,
float[] vector,
float[] tmpV3f)
Initialize this quaternion with given non-normalized axis vector and rotation angle
|
Quaternion |
setFromAngleNormalAxis(float angle,
float[] vector)
Initialize this quaternion with given normalized axis vector and rotation angle
|
Quaternion |
setFromAxes(float[] xAxis,
float[] yAxis,
float[] zAxis)
Initializes this quaternion to represent a rotation formed by the given three orthogonal axes.
|
Quaternion |
setFromEuler(float[] angradXYZ)
Initializes this quaternion from the given Euler rotation array
angradXYZ in radians. |
Quaternion |
setFromEuler(float bankX,
float headingY,
float attitudeZ)
Initializes this quaternion from the given Euler rotation angles in radians.
|
Quaternion |
setFromMatrix(float[] m,
int m_off)
Initializes this quaternion from a 4x4 column rotation matrix
|
Quaternion |
setFromMatrix(float m00,
float m01,
float m02,
float m10,
float m11,
float m12,
float m20,
float m21,
float m22)
Compute the quaternion from a 3x3 column rotation matrix
|
Quaternion |
setFromNormalVectors(float[] v1,
float[] v2,
float[] tmpPivotVec)
Initialize this quaternion from two normalized vectors
|
Quaternion |
setFromVectors(float[] v1,
float[] v2,
float[] tmpPivotVec,
float[] tmpNormalVec)
Initialize this quaternion from two vectors
|
Quaternion |
setIdentity()
Set this quaternion to identity (x=0,y=0,z=0,w=1)
|
Quaternion |
setLookAt(float[] directionIn,
float[] upIn,
float[] xAxisOut,
float[] yAxisOut,
float[] zAxisOut)
Set this quaternion to equal the rotation required
to point the z-axis at direction and the y-axis to up.
|
Quaternion |
setSlerp(Quaternion a,
Quaternion b,
float changeAmnt)
Set this quaternion to a spherical linear interpolation
between the given start and end quaternions by the given change amount.
|
void |
setW(float w) |
void |
setX(float x) |
void |
setY(float y) |
void |
setZ(float z) |
Quaternion |
subtract(Quaternion q)
Subtract a quaternion
|
float |
toAngleAxis(float[] axis)
Transform the rotational quaternion to axis based rotation angles
|
void |
toAxes(float[] xAxis,
float[] yAxis,
float[] zAxis,
float[] tmpMat4)
Extracts this quaternion's orthogonal rotation axes.
|
float[] |
toEuler(float[] result)
Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).
|
float[] |
toMatrix(float[] matrix,
int mat_offset)
Transform this quaternion to a normalized 4x4 column matrix representing the rotation.
|
String |
toString() |
public static final float ALLOWED_DEVIANCE
Using 9.999999974752427E-7f, which is ~10 times FloatUtil.EPSILON
.
public Quaternion()
public Quaternion(Quaternion q)
public Quaternion(float x, float y, float z, float w)
public final float magnitudeSquared()
magnitude()
for special handling of epsilon
,
which is not applied here.public final float magnitude()
magnitude()
)
A magnitude of zero shall equal identity
,
as performed by normalize()
.
Implementation Details:
magnitudeSquared()
is is zero
using epsilon
magnitudeSquared()
is equals 1f
using epsilon
public final float getW()
public final void setW(float w)
public final float getX()
public final void setX(float x)
public final float getY()
public final void setY(float y)
public final float getZ()
public final void setZ(float z)
public final float dot(float x, float y, float z, float w)
public final float dot(Quaternion quat)
public final boolean isIdentity()
true
if this quaternion has identity.
Implementation uses epsilon
to compare
W
against 1f
and
X
, Y
and Z
against zero
.
public final Quaternion setIdentity()
public final Quaternion normalize()
Implementation Details:
setIdentity()
if magnitude()
is is zero
using epsilon
public Quaternion conjugate()
[-x, -y, -z, w]
.public final Quaternion invert()
Implementation Details:
conjugates
if magnitudeSquared()
is is equals 1f
using epsilon
public final Quaternion set(Quaternion src)
public final Quaternion set(float x, float y, float z, float w)
public final Quaternion add(Quaternion q)
q
- quaternionpublic final Quaternion subtract(Quaternion q)
q
- quaternionpublic final Quaternion mult(Quaternion q)
q
- a quaternion to multiply withpublic final Quaternion scale(float n)
n
- a float constantpublic Quaternion rotateByAngleNormalAxis(float angle, float axisX, float axisY, float axisZ)
The axis must be a normalized vector.
A rotational quaternion is made from the given angle and axis.
angle
- in radiansaxisX
- x-coord of rotation axisaxisY
- y-coord of rotation axisaxisZ
- z-coord of rotation axispublic Quaternion rotateByAngleX(float angle)
angle
- in radianspublic Quaternion rotateByAngleY(float angle)
angle
- in radianspublic Quaternion rotateByAngleZ(float angle)
angle
- in radianspublic final Quaternion rotateByEuler(float[] angradXYZ)
angradXYZ
in radians.
The angradXYZ
array is laid out in natural order:
rotateByEuler(float, float, float)
.angradXYZ
- euler angel array in radiansrotateByEuler(float, float, float)
public final Quaternion rotateByEuler(float bankX, float headingY, float attitudeZ)
The rotations are applied in the given order and using chained rotation per axis:
rotateByAngleY(float)
rotateByAngleZ(float)
rotateByAngleX(float)
Implementation Details:
is zero
using epsilon
normalize()
edbankX
- the Euler pitch angle in radians. (rotation about the X axis)headingY
- the Euler yaw angle in radians. (rotation about the Y axis)attitudeZ
- the Euler roll angle in radians. (rotation about the Z axis)rotateByAngleY(float)
,
rotateByAngleZ(float)
,
rotateByAngleX(float)
,
setFromEuler(float, float, float)
public final float[] rotateVector(float[] vecOut, int vecOutOffset, float[] vecIn, int vecInOffset)
vecOut
- result float[3] storage for rotated vector, maybe equal to vecIn for in-place rotationvecOutOffset
- offset in result storagevecIn
- float[3] vector to be rotatedvecInOffset
- offset in vecInpublic final Quaternion setSlerp(Quaternion a, Quaternion b, float changeAmnt)
Note: Method does not normalize this quaternion!
a
- start quaternionb
- end quaternionchangeAmnt
- float between 0 and 1 representing interpolation.public Quaternion setLookAt(float[] directionIn, float[] upIn, float[] xAxisOut, float[] yAxisOut, float[] zAxisOut)
Implementation generates a 3x3 matrix
and is equal with ProjectFloat's lookAt(..).
normalize()
eddirectionIn
- where to look atupIn
- a vector indicating the local up direction.xAxisOut
- vector storing the orthogonal x-axis of the coordinate system.yAxisOut
- vector storing the orthogonal y-axis of the coordinate system.zAxisOut
- vector storing the orthogonal z-axis of the coordinate system.public final Quaternion setFromVectors(float[] v1, float[] v2, float[] tmpPivotVec, float[] tmpNormalVec)
q = (s,v) = (v1•v2 , v1 × v2), angle = angle(v1, v2) = v1•v2 axis = normal(v1 x v2)
Implementation Details:
setIdentity()
if square vector-length is is zero
using epsilon
v1
- not normalizedv2
- not normalizedtmpPivotVec
- float[3] temp storage for cross producttmpNormalVec
- float[3] temp storage to normalize vectorpublic final Quaternion setFromNormalVectors(float[] v1, float[] v2, float[] tmpPivotVec)
q = (s,v) = (v1•v2 , v1 × v2), angle = angle(v1, v2) = v1•v2 axis = v1 x v2
Implementation Details:
setIdentity()
if square vector-length is is zero
using epsilon
v1
- normalizedv2
- normalizedtmpPivotVec
- float[3] temp storage for cross productpublic final Quaternion setFromAngleAxis(float angle, float[] vector, float[] tmpV3f)
Implementation Details:
setIdentity()
if axis is is zero
using epsilon
angle
- rotation angle (rads)vector
- axis vector not normalizedtmpV3f
- float[3] temp storage to normalize vectortoAngleAxis(float[])
public final Quaternion setFromAngleNormalAxis(float angle, float[] vector)
Implementation Details:
setIdentity()
if axis is is zero
using epsilon
angle
- rotation angle (rads)vector
- axis vector normalizedtoAngleAxis(float[])
public final float toAngleAxis(float[] axis)
axis
- float[3] storage for computed axissetFromAngleAxis(float, float[], float[])
public final Quaternion setFromEuler(float[] angradXYZ)
angradXYZ
in radians.
The angradXYZ
array is laid out in natural order:
setFromEuler(float, float, float)
.angradXYZ
- euler angel array in radianssetFromEuler(float, float, float)
public final Quaternion setFromEuler(float bankX, float headingY, float attitudeZ)
The rotations are applied in the given order:
Implementation Details:
setIdentity()
if all angles are is zero
using epsilon
normalize()
edbankX
- the Euler pitch angle in radians. (rotation about the X axis)headingY
- the Euler yaw angle in radians. (rotation about the Y axis)attitudeZ
- the Euler roll angle in radians. (rotation about the Z axis)toEuler(float[])
public float[] toEuler(float[] result)
result
- the float[] array storing the computed angles.setFromEuler(float, float, float)
public final Quaternion setFromMatrix(float[] m, int m_off)
See Graphics Gems Code,
MatrixTrace.
Buggy Matrix-FAQ Q55
m
- 4x4 column matrixtoMatrix(float[], int)
public Quaternion setFromMatrix(float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22)
See Graphics Gems Code,
MatrixTrace.
Buggy Matrix-FAQ Q55
toMatrix(float[], int)
public final float[] toMatrix(float[] matrix, int mat_offset)
Implementation Details:
magnitudeSquared()
is is zero
using epsilon
matrix
- float[16] store for the resulting normalized column matrix 4x4mat_offset
- setFromMatrix(float[], int)
public float[] copyMatrixColumn(int index, float[] result, int resultOffset)
index
- the 3x3 rotation matrix column to retrieve from this quaternion (normalized). Must be between 0 and 2.result
- the vector object to store the result in.public final Quaternion setFromAxes(float[] xAxis, float[] yAxis, float[] zAxis)
No validation whether the axes are orthogonal is performed.
xAxis
- vector representing the orthogonal x-axis of the coordinate system.yAxis
- vector representing the orthogonal y-axis of the coordinate system.zAxis
- vector representing the orthogonal z-axis of the coordinate system.public void toAxes(float[] xAxis, float[] yAxis, float[] zAxis, float[] tmpMat4)
xAxis
- vector representing the orthogonal x-axis of the coordinate system.yAxis
- vector representing the orthogonal y-axis of the coordinate system.zAxis
- vector representing the orthogonal z-axis of the coordinate system.tmpMat4
- temporary float[4] matrix, used to transform this quaternion to a matrix.public final boolean isRotationMatrix3f(float[] m)
m
- 3x3 column matrixpublic boolean equals(Object o)
Copyright 2010 JogAmp Community.