public class Quat extends Object implements Linkable, Rotation
Rotation
is a 4 element unit quaternion
represented by single precision floating point x,y,z,w coordinates. This class API aims
to conform that of the great libQGLViewer
Quaternion.Modifier and Type | Field and Description |
---|---|
float[] |
quat
The x, y, z, and w coordinates of the Quat represented as a public array.
|
Constructor and Description |
---|
Quat()
Constructs and initializes a Quat to (0.0,0.0,0.0,1.0), i.e., an identity rotation.
|
Quat(float[] q)
Convenience constructor that simply calls
this(q, true) |
Quat(float[] q,
boolean normalize)
Constructs and initializes a Quat from the array of length 4.
|
Quat(float roll,
float pitch,
float yaw)
Constructs a Quat from the given Euler angles.
|
Quat(float x,
float y,
float z,
float w)
Default constructor for Quat(float x, float y, float z, float w, boolean normalize),
with
normalize=true . |
Quat(float x,
float y,
float z,
float w,
boolean normalize)
Constructs and initializes a Quat from the specified xyzw coordinates.
|
Quat(Mat glMat)
Constructs a Quat from a (supposedly correct) 3x3 rotation matrix given in the upper
left 3x3 sub-matrix of the Mat.
|
Quat(Quat q1,
boolean normalize)
Copy constructor.
|
Quat(Vec axis,
float angle)
|
Quat(Vec from,
Vec to)
Constructs a Quat that will rotate from the
from direction to the to
direction. |
Quat(Vec X,
Vec Y,
Vec Z)
Constructs a Quat from the three rotated vectors of an orthogonal basis.
|
Modifier and Type | Method and Description |
---|---|
float |
angle()
Returns the
angle (in radians) of the rotation represented by the Quat. |
Vec |
axis()
Returns the normalized axis direction of the rotation represented by the Quat.
|
void |
compose(Rotation q)
Compose the rotation
|
static Rotation |
compose(Rotation q1,
Rotation q2) |
void |
conjugate()
Sets the value of this Quat to the conjugate of itself.
|
void |
conjugate(Quat q1)
Sets the value of this Quat to the conjugate of Quat q1.
|
static float |
dot(Quat a,
Quat b)
Returns the "dot" product of
a and b : |
float |
dotProduct(Quat b)
Returns the "dot" product of this Quat and
b : |
boolean |
equals(Object obj) |
Vec |
eulerAngles()
Converts this Quat to Euler rotation angles
roll , pitch and
yaw in radians. |
Quat |
exp()
Returns the exponential of the Quat.
|
void |
fromAxisAngle(float x,
float y,
float z,
float angle)
Same as
fromAxisAngle(new Vec(x,y,z), angle) . |
void |
fromAxisAngle(Vec axis,
float angle)
|
void |
fromEulerAngles(float roll,
float pitch,
float yaw)
Converts Euler rotation angles
roll , pitch and yaw ,
respectively defined to the x, y and z axes, to this Quat. |
void |
fromEulerAngles(Vec angles)
Convenience function that simply calls
fromEulerAngles(angles.vec[0], angles.vec[1], angles.vec[2]) . |
void |
fromMatrix(Mat glMatrix)
Set the Quat from a (supposedly correct) 3x3 rotation matrix given in the upper left
3x3 sub-matrix of the Mat.
|
void |
fromRotatedBasis(Vec X,
Vec Y,
Vec Z)
Sets the Quat from the three rotated vectors of an orthogonal basis.
|
void |
fromTaitBryan(float roll,
float pitch,
float yaw)
Same as
fromEulerAngles(float, float, float) . |
void |
fromTaitBryan(Vec angles)
Same as
fromEulerAngles(Vec) . |
void |
fromTo(float x1,
float y1,
float z1,
float x2,
float y2,
float z2)
Same as
fromTo(new Vec(x1,y1,z1), new Vec(x2,y2,z2)) . |
void |
fromTo(Vec from,
Vec to)
Sets the Quat as a rotation from the
from direction to the to
direction. |
Quat |
get()
Returns a deep copy of the object.
|
float[] |
get(float[] target)
Gets data as an array
|
int |
hashCode() |
Quat |
inverse()
Returns the inverse Quat (inverse rotation).
|
Mat |
inverseMatrix()
Returns the associated inverse rotation Mat.
|
Vec |
inverseRotate(Vec v)
Returns the image of
v by the Quat inverse() rotation. |
void |
invert()
Sets the value of this Quat to the inverse of itself.
|
void |
invert(Quat q1)
Sets the value of this Quat to the Quat inverse of
q1 . |
void |
link(float[] src)
Share the src array data.
|
static Quat |
lnDif(Quat a,
Quat b)
Simply returns {@code log(a.
|
Quat |
log()
Returns the logarithm of the Quat.
|
Mat |
matrix()
Returns the Mat which represents the rotation matrix associated with the Quat.
|
void |
multiply(Quat q1)
Sets the value of this Quat to the Quat product of itself and
q1 , (i.e.,
this = this * q1 ). |
static Quat |
multiply(Quat q1,
Quat q2)
Returns the Quat which is product of quaternions
q1 and q2 . |
static Vec |
multiply(Quat q1,
Vec v)
Returns the image of
v by the rotation q1 . |
Vec |
multiply(Vec v)
Returns the image of
v by the rotation of this vector. |
void |
multiplyInverse(Quat q1)
Multiplies this Quat by the inverse of Quat
q1 and places the value into this
Quat (i.e., this = this * q^-1 ). |
static Quat |
multiplyInverse(Quat q1,
Quat q2)
Returns the product of Quat
q1 by the inverse of Quat q2 (i.e.,
q1 * q2^-1 ). |
void |
negate()
Negates all the coefficients of the Quat.
|
float |
normalize()
Normalizes the value of this Quat in place and return its
norm . |
void |
print()
Prints orientation data.
|
static Quat |
randomQuat()
Returns a random unit Quat.
|
void |
reset()
Sets data to default value.
|
Vec |
rotate(Vec v)
Returns the image of
v by the Quat rotation. |
void |
set(float[] source)
Sets representation from the array data
|
void |
set(Linkable q)
Links array data representation with the source
|
void |
set(Quat q1)
Convenience function that simply calls
set(q1, true); |
void |
set(Quat q1,
boolean normalize)
Set this Quat from quaternion
q1 . |
void |
setW(float w)
Sets the Quat w component
|
void |
setX(float x)
Sets the Quat x component
|
void |
setY(float y)
Sets the Quat y component
|
void |
setZ(float z)
Sets the Quat z component
|
static Quat |
slerp(Quat a,
Quat b,
float t)
Wrapper function that simply calls
slerp(a, b, t, true) . |
static Quat |
slerp(Quat a,
Quat b,
float t,
boolean allowFlip)
Returns the slerp interpolation of quaternions
a and b , at time
t . |
static Quat |
squad(Quat a,
Quat tgA,
Quat tgB,
Quat b,
float t)
Returns the slerp interpolation of the two quaternions
a and b , at
time t , using tangents tgA and tgB . |
static Quat |
squadTangent(Quat before,
Quat center,
Quat after)
Returns a tangent Quat for
center , defined by before and
after quaternions. |
static float |
squaredNorm(Quat q)
Utility function that returns the squared norm of the Quat.
|
Vec |
taitBryanAngles()
Same as
eulerAngles() . |
void |
unLink()
Stop sharing data with the source
|
float |
w() |
float |
x() |
float |
y() |
float |
z() |
public float[] quat
public Quat()
public Quat(float x, float y, float z, float w)
normalize=true
.public Quat(float x, float y, float z, float w, boolean normalize)
x
- the x coordinatey
- the y coordinatez
- the z coordinatew
- the w scalar componentnormalize
- tells whether or not the constructed Quat should be normalized.public Quat(float[] q)
this(q, true)
public Quat(float[] q, boolean normalize)
q
- the array of length 4 containing xyzw in orderpublic Quat(Quat q1, boolean normalize)
q1
- the Quat containing the initialization x y z w datapublic Quat(Vec axis, float angle)
axis
(non null) and angle
(in radians).axis
- the Vec representing the axisangle
- the angle in radiansfromAxisAngle(Vec, float)
public Quat(Vec from, Vec to)
from
direction to the to
direction.from
- the first Vecto
- the second VecfromTo(Vec, Vec)
public Quat(float roll, float pitch, float yaw)
roll
- Rotation angle in radians around the x-Axispitch
- Rotation angle in radians around the y-Axisyaw
- Rotation angle in radians around the z-AxisfromEulerAngles(float, float, float)
public Quat(Mat glMat)
glMat
- fromMatrix(Mat)
public Quat(Vec X, Vec Y, Vec Z)
X
- 1st Orthogonal VecY
- 2nd Orthogonal VecZ
- 3rd Orthogonal VecfromRotatedBasis(Vec, Vec, Vec)
public Quat get()
Copyable
Typical implementation should simple look like: return new Object(this)
.
public void reset()
Linkable
public void link(float[] src)
Linkable
public void unLink()
Linkable
public float[] get(float[] target)
Linkable
public void set(float[] source)
Linkable
public float x()
public float y()
public float z()
public float w()
public void setX(float x)
public void setY(float y)
public void setZ(float z)
public void setW(float w)
public void set(Linkable q)
Linkable
public void set(Quat q1)
set(q1, true);
set(Quat, boolean)
public void set(Quat q1, boolean normalize)
public final void conjugate()
public final void conjugate(Quat q1)
q1
- the source vectorpublic final void negate()
public final float dotProduct(Quat b)
b
:
this.quat[0] * b.x + this.quat[1] * b.y + this.quat[2] * b.z + this.quat[3] * b.quat[3]
b
- the Quatpublic static final float dot(Quat a, Quat b)
a
and b
:
a.x * b.x + a.y * b.y + a.z * b.z + a.quat[3] * b.quat[3]
a
- the first Quatb
- the second Quatpublic final void compose(Rotation q)
Rotation
public final void multiply(Quat q1)
q1
, (i.e.,
this = this * q1
).q1
- the other Quatpublic static final Quat multiply(Quat q1, Quat q2)
q1
and q2
.q1
- the first Quatq2
- the second Quatpublic final Vec multiply(Vec v)
v
by the rotation of this vector. Same as
this.rotate(v).
v
- the Vecrotate(Vec)
,
inverseRotate(Vec)
public static final Vec multiply(Quat q1, Vec v)
v
by the rotation q1
. Same as
q1.rotate(v).
q1
- the Quatv
- the Vecrotate(Vec)
,
inverseRotate(Vec)
public final void multiplyInverse(Quat q1)
q1
and places the value into this
Quat (i.e., this = this * q^-1
). The value of the argument Quat is preserved.q1
- the other Quatpublic static final Quat multiplyInverse(Quat q1, Quat q2)
q1
by the inverse of Quat q2
(i.e.,
q1 * q2^-1
). The value of both argument quaternions is preserved.q1
- the first Quatq2
- the second Quatpublic final Quat inverse()
public final void invert()
inverse()
public final void invert(Quat q1)
q1
.q1
- the Quat to be invertedpublic final float normalize()
norm
.public final Vec inverseRotate(Vec v)
v
by the Quat inverse()
rotation.
rotate(Vec)
performs an inverse transformation.
inverseRotate
in interface Rotation
v
- the Vecpublic void fromAxisAngle(Vec axis, float angle)
axis
and angle
(in
radians).
The axis
does not need to be normalized. A null axis
will result in
an identity Quat.
axis
- the Vec representing the axisangle
- the angle in radianspublic void fromAxisAngle(float x, float y, float z, float angle)
fromAxisAngle(new Vec(x,y,z), angle)
.fromAxisAngle(Vec, float)
public void fromTaitBryan(Vec angles)
fromEulerAngles(Vec)
.public void fromTaitBryan(float roll, float pitch, float yaw)
fromEulerAngles(float, float, float)
.public void fromEulerAngles(Vec angles)
fromEulerAngles(angles.vec[0], angles.vec[1], angles.vec[2])
.public void fromEulerAngles(float roll, float pitch, float yaw)
roll
, pitch
and yaw
,
respectively defined to the x, y and z axes, to this Quat. In the convention used
here these angles represent a composition of extrinsic rotations (rotations about the
reference frame axes), which is also known as taitBryanAngles()
(See
http://en.wikipedia.org/wiki/Euler_angles and
http://en.wikipedia.org/wiki/Tait-Bryan_angles). eulerAngles()
performs the
inverse operation.
Each rotation angle is converted to an axis-angle pair, with the axis corresponding to one of the Euclidean axes. The axis-angle pairs are converted to quaternions and multiplied together. The order of the rotations is: y,z,x which follows the convention found here: http://www.euclideanspace.com/maths/geometry/rotations/euler/index.htm.
eulerAngles()
public Vec taitBryanAngles()
eulerAngles()
.public Vec eulerAngles()
roll
, pitch
and
yaw
in radians. fromEulerAngles(float, float, float)
performs the
inverse operation. The code was adapted from:
http://www.euclideanspace.com/maths/geometry/rotations/conversions/
quaternionToEuler/index.htm.
Attention: This method assumes that this Quat is normalized.
fromEulerAngles(roll, pitch, yaw)
) is: y,z,x.fromEulerAngles(float, float, float)
public void fromTo(Vec from, Vec to)
from
direction to the to
direction.
Attention: this rotation is not uniquely defined. The selected axis is usually
orthogonal to from
and to
, minimizing the rotation angle. This method
is robust and can handle small or almost identical vectors.
fromTo
in interface Rotation
fromAxisAngle(Vec, float)
public void fromTo(float x1, float y1, float z1, float x2, float y2, float z2)
fromTo(new Vec(x1,y1,z1), new Vec(x2,y2,z2))
.fromTo(Vec, Vec)
public final void fromMatrix(Mat glMatrix)
fromMatrix
in interface Rotation
fromRotatedBasis(Vec, Vec, Vec)
public final void fromRotatedBasis(Vec X, Vec Y, Vec Z)
The three vectors do not have to be normalized but must be orthogonal and direct
(i,e., X^Y=k*Z, with k>0
).
fromRotatedBasis
in interface Rotation
X
- the first VecY
- the second VecZ
- the third VecfromRotatedBasis(Vec, Vec, Vec)
,
Quat(Vec, Vec)
public final Vec axis()
The result is null for an identity Quat.
angle()
public final float angle()
angle
(in radians) of the rotation represented by the Quat.
This value is always in the range [0-pi]
. Larger rotational angles are
obtained by inverting the axis()
direction.
public final Mat matrix()
public final Mat inverseMatrix()
matrix()
of the
inverse()
.
Attention: The result is only valid until the next call to
inverseMatrix()
. Use it immediately (as in
applyMatrix(q.inverseMatrix())
).
inverseMatrix
in interface Rotation
public static final Quat randomQuat()
You can create a randomly directed unit vector using:
Vec randomDir = new Vec(1.0f, 0.0f, 0.0f);
randomDir = Quat.multiply(Quat.randomQuat(),
randomDir);
public static final Quat slerp(Quat a, Quat b, float t)
slerp(a, b, t, true)
.
See slerp(Quat, Quat, float, boolean)
for details.
public static final Quat slerp(Quat a, Quat b, float t, boolean allowFlip)
a
and b
, at time
t
.
t
should range in [0,1]
. Result is a
when t=0
and
b
when t=1
.
When allowFlip
is true (default) the slerp interpolation will always use the
"shortest path" between the quaternions' orientations, by "flipping" the source Quat
if needed (see negate()
).
a
- the first Quatb
- the second Quatt
- the t interpolation parameterallowFlip
- tells whether or not the interpolation allows axis flippublic static final Quat squad(Quat a, Quat tgA, Quat tgB, Quat b, float t)
a
and b
, at
time t
, using tangents tgA
and tgB
.
The resulting Quat is "between" a
and b
(result is a
when
t=0
and b
for t=1
).
Use squadTangent(Quat, Quat, Quat)
to define the Quat tangents tgA
and tgB
.
a
- the first QuattgA
- the first tangent QuattgB
- the second tangent Quatb
- the second Quatt
- the t interpolation parameterpublic static final Quat lnDif(Quat a, Quat b)
log(a. inverse() * b)
.
Useful for squadTangent(Quat, Quat, Quat)
.
a
- the first Quatb
- the second Quatpublic static final Quat squadTangent(Quat before, Quat center, Quat after)
center
, defined by before
and
after
quaternions.before
- the first Quatcenter
- the second Quatafter
- the third Quatpublic static float squaredNorm(Quat q)
Processing Library proscene by Jean Pierre Charalambos. (c) 2014-2017 National University of Colombia