29package com.jogamp.opengl.test.junit.math;
31import org.junit.Assert;
33import org.junit.FixMethodOrder;
34import org.junit.runners.MethodSorters;
36import com.jogamp.junit.util.JunitTracer;
37import com.jogamp.math.FloatUtil;
38import com.jogamp.math.Matrix4f;
39import com.jogamp.math.Quaternion;
40import com.jogamp.math.Vec3f;
41import com.jogamp.math.Vec4f;
42import com.jogamp.math.VectorUtil;
44@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 static final boolean DEBUG =
false;
50 static final Vec3f ZERO =
new Vec3f ( 0f, 0f, 0f );
51 static final Vec3f ONE =
new Vec3f ( 1f, 1f, 1f );
52 static final Vec3f NEG_ONE =
new Vec3f ( -1f, -1f, -1f );
53 static final Vec3f UNIT_X =
new Vec3f ( 1f, 0f, 0f );
54 static final Vec3f UNIT_Y =
new Vec3f ( 0f, 1f, 0f );
55 static final Vec3f UNIT_Z =
new Vec3f ( 0f, 0f, 1f );
56 static final Vec3f NEG_UNIT_X =
new Vec3f ( -1f, 0f, 0f );
57 static final Vec3f NEG_UNIT_Y =
new Vec3f ( 0f, -1f, 0f );
58 static final Vec3f NEG_UNIT_Z =
new Vec3f ( 0f, 0f, -1f );
60 static final Vec4f NEG_ONE_v4 =
new Vec4f ( -1f, -1f, -1f, 0f );
61 static final Vec4f ONE_v4 =
new Vec4f ( 1f, 1f, 1f, 0f );
74 Assert.assertEquals(0f, Math.abs(1 - quat2.
magnitude()), MACH_EPSILON);
82 Assert.assertEquals(ZERO, rotVec0);
91 Assert.assertEquals(quat0, quat0Inv.
invert());
97 Assert.assertEquals(quat0, quat0Conj);
104 Assert.assertTrue(35.0f == quat.
dot(3f, 1f, 2f, -2f));
105 Assert.assertTrue(-11.0f == quat.
dot(
new Quaternion(-1f, 1f, -1f, 1f)));
119 Assert.assertEquals(quat2, quat1);
128 Assert.assertEquals(vecOut1, vecOut2);
135 Assert.assertEquals(QUAT_IDENT, quat2);
139 Assert.assertEquals(quat1, quat2);
141 quat1.
set(0, 0, 0, 0);
143 Assert.assertTrue(0.0f == angle);
144 Assert.assertEquals(UNIT_X, vecOut1);
157 Assert.assertEquals(quat, quat2);
160 Assert.assertEquals(quat2, quat);
180 Assert.assertEquals(QUAT_IDENT, quat);
194 Assert.assertEquals(angles0Exp, angles0Has);
198 Assert.assertEquals(quat, quat2);
209 Assert.assertEquals(angles1Exp, angles1Has);
212 Assert.assertEquals(quat, quat2);
223 Assert.assertEquals(angles2Exp, angles2Has);
226 Assert.assertEquals(quat, quat2);
262 quat.
set(0, 0, 0, 0);
264 Assert.assertEquals(mat1, mat2);
271 final float[] mat1_0 =
new float[] {
279 final float[] mat2_0 =
new float[16];
288 final float[] mat2_0 =
new float[16];
294 final float[] mat2_0 =
new float[16];
298 Assert.assertEquals(mat1, mat2);
304 Assert.assertEquals(mat1, mat2);
320 Assert.assertEquals(mat1, mat2);
325 final float[] vecHas_0 =
new float[3];
326 vecHas.
get(vecHas_0);
327 final float[] mat2_0 =
new float[16];
329 final float[] NEG_ONE_0 =
new float[3];
330 NEG_ONE.
get(NEG_ONE_0);
331 final float[] vecOut3_0 =
new float[3];
339 Assert.assertEquals(vecHas, vecOut3);
342 final float[] vecOut3_1 =
new float[3];
343 vecOut3.
get(vecOut3_1);
348 final float[] vecHas_0 =
new float[4];
349 vecHas.
get(vecHas_0);
350 final float[] mat2_0 =
new float[16];
352 final float[] NEG_ONE_v4_0 =
new float[4];
353 NEG_ONE_v4.
get(NEG_ONE_v4_0);
354 final float[] vecOut4_0 =
new float[4];
361 vecOut3.
set(vecOut4);
363 Assert.assertEquals(vecHas, vecOut3);
366 final float[] vecOut4_1 =
new float[4];
367 vecOut4.
get(vecOut4_1);
375 mat1.
load(
new float[] {
385 Assert.assertEquals(mat1, mat2);
399 Assert.assertEquals(mat1, mat2);
403 vecOut3.
set(vecOut4);
410 mat1.
load(
new float[] {
420 Assert.assertEquals(mat1, mat2);
434 Assert.assertEquals(mat1, mat2);
438 vecOut3.
set(vecOut4);
445 mat1.
load(
new float[] {
455 Assert.assertEquals(mat1, mat2);
469 Assert.assertEquals(mat1, mat2);
473 vecOut3.
set(vecOut4);
485 mat1.
load(
new float[] {
493 Assert.assertEquals(vecExp0, vecCol);
497 Assert.assertEquals(vecExp1, vecCol);
501 Assert.assertEquals(vecExp2, vecCol);
516 Assert.assertEquals(matExp1, matHas);
524 Assert.assertEquals(eulerExp, eulerHas);
526 Assert.assertEquals(quat2, quat1);
531 Assert.assertEquals(quat2, quat1);
545 Assert.assertEquals(matExp, matHas);
553 Assert.assertEquals(eulerExp, eulerHas);
555 Assert.assertEquals(quat2, quat1);
560 Assert.assertEquals(quat2, quat1);
566 final float[] eulerExp0 =
new float[3];
567 eulerExp1.
get(eulerExp0);
577 Assert.assertEquals(matExp, matHas);
584 System.err.println(
"PI");
585 System.err.printf(
" double %20.20f%n", Math.PI);
586 System.err.printf(
" float %20.20f%n",
FloatUtil.
PI);
587 System.err.printf(
" diff %20.20f%n", (Math.PI -
FloatUtil.
PI));
588 System.err.println(
"PI/2");
589 System.err.printf(
" double %20.20f%n", Math.PI/2f);
593 System.err.println(
"exp-euler "+eulerExp1);
594 System.err.println(
"has-euler1 "+eulerHas1);
595 System.err.println(
"dif-euler1 "+eulerExp1.
minus(eulerHas1));
598 final float[] eulerHas0 =
new float[3];
599 eulerHas1.
get(eulerHas0);
605 Assert.assertEquals(quat2, quat1);
610 Assert.assertEquals(quat2, quat1);
626 Assert.assertEquals(quatExp1, quatHas);
628 quat1.
set(0, 1, 2, 3);
629 quat2.
set(1, 1, 1, 1);
632 Assert.assertEquals(
new Quaternion(-1, 0, 1, 2), quatHas);
642 Assert.assertEquals(
new Quaternion(1, 2, 4, 6), quat2);
646 Assert.assertEquals(
new Quaternion(2, 4, 8, 12), quat2);
678 Assert.assertEquals(QUAT_IDENT, quat2);
680 Assert.assertEquals(quat0, quat1);
688 Assert.assertEquals(QUAT_IDENT, quat2);
690 Assert.assertEquals(quat0, quat1);
693 quat0.
set(-1f, -2f, -3f, 4f);
694 quat1.
set( 1f, 2f, 3f, 4f);
697 Assert.assertEquals(quat0, quat2);
705 Assert.assertEquals(quat1, quat2);
710 Assert.assertEquals(quat1, quat2);
715 Assert.assertEquals(quat1, quat2);
720 Assert.assertEquals(quat1, quat2);
730 vecRot.
set(1f, 1f, 1f);
734 vecExp.
set(1f, 1f, 1f);
740 Assert.assertEquals(vecExp, vecRot);
744 vecExp.
set(1f, 1f, 1f);
745 vecRot.
set(1f, 1f, 1f);
751 Assert.assertEquals(vecExp, vecRot);
754 vecExp.
set(1f, 1f, 1f);
755 vecRot.
set(1f, 1f, 1f);
761 Assert.assertEquals(vecExp, vecRot);
764 vecExp.
set(1f, 1f, 1f);
765 vecRot.
set(1f, 1f, 1f);
771 Assert.assertEquals(vecExp, vecRot);
775 Assert.assertEquals(quat, worker);
791 Assert.assertEquals(quat0, quat1);
793 Assert.assertEquals(quat2, quat1);
795 quat1.
toAxes(xAxis, yAxis, zAxis, rotMat);
797 Assert.assertEquals(quat0, quat2);
798 Assert.assertEquals(quat1, quat2);
815 if( !vecExp.
equals(vecHas) ) {
816 System.err.println(
"Deviance: "+vecExp+
", "+vecHas+
": "+vecExp.
minus(vecHas)+
", dist "+vecExp.
dist(vecHas));
828 Assert.assertEquals(NEG_UNIT_X, vecHas);
837 if( !UNIT_Y.
equals(vecHas) ) {
838 System.err.println(
"Deviance: "+UNIT_Y+
", "+vecHas+
": "+UNIT_Y.
minus(vecHas)+
", dist "+UNIT_Y.
dist(vecHas));
850 Assert.assertEquals(UNIT_X, vecHas);
855 quatS.
setSlerp(quat1, quat2, 0.25f);
860 Assert.assertEquals(UNIT_X, vecHas);
872 if( !vecExp.
equals(vecHas) ) {
873 System.err.println(
"Deviance: "+vecExp+
", "+vecHas+
": "+vecExp.
minus(vecHas)+
", dist "+vecExp.
dist(vecHas));
886 if( DEBUG ) System.err.println(
"LookAt #01");
887 direction.
set(NEG_UNIT_X);
890 Assert.assertEquals(direction, vecHas);
893 System.err.println(
"quat0.0 "+quat);
894 System.err.println(
"exp0 "+direction+
", len "+direction.
length());
895 System.err.println(
"has0 "+vecHas+
", len "+vecHas.
length());
896 System.err.println();
897 System.err.println(
"LookAt #02");
900 quat.
setLookAt(direction, UNIT_Y, xAxis, yAxis, zAxis);
902 System.err.println(
"direction "+direction);
903 System.err.println(
"quat0.0 "+quat);
907 System.err.println(
"quat0.1 "+quat);
908 System.err.println(
"xAxis "+xAxis+
", len "+xAxis.
length());
909 System.err.println(
"yAxis "+yAxis+
", len "+yAxis.
length());
910 System.err.println(
"zAxis "+zAxis+
", len "+zAxis.
length());
911 System.err.println(
"exp0 "+direction+
", len "+direction.
length());
912 System.err.println(
"has0 "+vecHas+
", len "+vecHas.
length());
916 Assert.assertEquals(direction, vecHas);
918 if( DEBUG )System.err.println(
"LookAt #03");
920 quat.
setLookAt(direction, UNIT_Y, xAxis, yAxis, zAxis);
921 if( DEBUG )System.err.println(
"quat0 "+quat);
924 System.err.println(
"xAxis "+xAxis+
", len "+xAxis.
length());
925 System.err.println(
"yAxis "+yAxis+
", len "+yAxis.
length());
926 System.err.println(
"zAxis "+zAxis+
", len "+zAxis.
length());
927 System.err.println(
"exp0 "+direction+
", len "+direction.
length());
928 System.err.println(
"has0 "+vecHas+
", len "+vecHas.
length());
932 if( !direction.
equals(vecHas) ) {
933 System.err.println(
"Deviance: "+direction+
", "+vecHas+
": "+direction.
minus(vecHas)+
", dist "+direction.
dist(vecHas));
938 public static void main(
final String args[]) {
Basic Float math utility functions.
static float sin(final float a)
static final float QUARTER_PI
The value PI/4, i.e.
static final float PI
The value PI, i.e.
static final float EPSILON
Epsilon for floating point {@value}, as once computed via getMachineEpsilon() on an AMD-64 CPU.
static void multMatrixVec(final float[] m_in, final int m_in_off, final float[] v_in, final int v_in_off, final float[] v_out, final int v_out_off)
static float cos(final float a)
static float[] multMatrixVec3(final float[] m_in, final float[] v_in, final float[] v_out)
Affine 3f-vector transformation by 4x4 matrix.
static final float HALF_PI
The value PI/2, i.e.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
final Matrix4f loadIdentity()
Set this matrix to identity.
final Vec3f mulVec3f(final Vec3f v_in, final Vec3f v_out)
Affine 3f-vector transformation by 4x4 matrix.
Matrix4f setToRotationEuler(final float bankX, final float headingY, final float attitudeZ)
Set this matrix to rotation from the given Euler rotation angles in radians.
float get(final int i)
Gets the ith component, 0 <= i < 16.
final Quaternion getRotation(final Quaternion res)
Returns the rotation [m00 .
final Vec4f mulVec4f(final Vec4f v_in, final Vec4f v_out)
Vec4f getColumn(final int column, final Vec4f v_out)
Get the named column of the given column-major matrix to v_out.
Quaternion implementation supporting Gimbal-Lock free rotations.
final float dot(final float x, final float y, final float z, final float w)
Returns the dot product of this quaternion with the given x,y,z and w components.
final Vec3f rotateVector(final Vec3f vecIn, final Vec3f vecOut)
Quaternion conjugate()
Conjugates this quaternion [-x, -y, -z, w].
final Quaternion invert()
Invert the quaternion If rotational, will produce a the inverse rotation.
final Quaternion setFromNormalVectors(final Vec3f v1, final Vec3f v2, final Vec3f tmpPivotVec)
Initialize this quaternion from two normalized vectors.
final Quaternion scale(final float n)
Scale this quaternion by a constant.
Vec3f toEuler(final Vec3f result)
Transform this quaternion to Euler rotation angles in radians (pitchX, yawY and rollZ).
final Quaternion set(final Quaternion src)
Set all values of this quaternion using the given src.
final Quaternion setFromEuler(final Vec3f angradXYZ)
Initializes this quaternion from the given Euler rotation array angradXYZ in radians.
final Quaternion setSlerp(final Quaternion a, final Quaternion b, final float changeAmnt)
Set this quaternion to a spherical linear interpolation between the given start and end quaternions b...
Quaternion rotateByAngleNormalAxis(final float angle, final float axisX, final float axisY, final float axisZ)
Rotate this quaternion by the given angle and axis.
final Quaternion setFromAngleNormalAxis(final float angle, final Vec3f vector)
final Quaternion setFromAngleAxis(final float angle, final Vec3f vector, final Vec3f tmpV3f)
static final float ALLOWED_DEVIANCE
Quaternion Epsilon, used with equals method to determine if two Quaternions are close enough to be co...
Quaternion rotateByAngleY(final float angle)
Rotate this quaternion around Y axis with the given angle in radians.
Quaternion rotateByAngleZ(final float angle)
Rotate this quaternion around Z axis with the given angle in radians.
final Quaternion setFromAxes(final Vec3f xAxis, final Vec3f yAxis, final Vec3f zAxis)
Initializes this quaternion to represent a rotation formed by the given three orthogonal axes.
final float toAngleAxis(final Vec3f axis)
Transform the rotational quaternion to axis based rotation angles.
final Quaternion subtract(final Quaternion q)
Subtract a quaternion.
Quaternion setFromMat(final float m00, final float m01, final float m02, final float m10, final float m11, final float m12, final float m20, final float m21, final float m22)
Compute the quaternion from a 3x3 column rotation matrix.
final Quaternion setFromVectors(final Vec3f v1, final Vec3f v2, final Vec3f tmpPivotVec, final Vec3f tmpNormalVec)
Initialize this quaternion from two vectors.
final float[] toMatrix(final float[] matrix)
Transform this quaternion to a normalized 4x4 column matrix representing the rotation.
void toAxes(final Vec3f xAxis, final Vec3f yAxis, final Vec3f zAxis, final Matrix4f tmpMat4)
Extracts this quaternion's orthogonal rotation axes.
Quaternion setLookAt(final Vec3f directionIn, final Vec3f upIn, final Vec3f xAxisOut, final Vec3f yAxisOut, final Vec3f zAxisOut)
Set this quaternion to equal the rotation required to point the z-axis at direction and the y-axis to...
final float magnitude()
Return the magnitude of this quaternion, i.e.
final Quaternion mult(final Quaternion q)
Multiply this quaternion by the param quaternion.
final Quaternion setIdentity()
final Quaternion normalize()
Normalize a quaternion required if to be used as a rotational quaternion.
final Quaternion add(final Quaternion q)
Add a quaternion.
Quaternion rotateByAngleX(final float angle)
Rotate this quaternion around X axis with the given angle in radians.
3D Vector based upon three float components.
static final Vec3f UNIT_Y_NEG
boolean equals(final Object o)
Vec3f normalize()
Normalize this vector in place.
boolean isEqual(final Vec3f o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
float dist(final Vec3f o)
Return the distance between this vector and the given one.
float length()
Return the length of this vector, a.k.a the norm or magnitude
float[] get(final float[] xyz)
xyz = this, returns xyz.
Vec3f minus(final Vec3f arg)
Returns this - arg; creates new vector.
static final Vec3f UNIT_Z
Vec3f set(final Vec3f o)
this = o, returns this.
4D Vector based upon four float components.
float[] get(final float[] xyzw)
xyzw = this, returns xyzw.
static float distVec3(final float[] v1, final float[] v2)
Return the distance between the given two points described vector v1 and v2.
void test11FromVectorToVector()
void test15bAxesAndMatrix()
void test23RotationOrder()
void test15aAxesAndMatrix()
void test03InvertAndConj()
void test12FromAndToEulerAngles()
static void main(final String args[])
void test22InvertMultNormalAndConj()
void test13FromEulerAnglesAndRotateVector()
void test02RotateZeroVector()
void test15cAxesAndMatrix()