Skip to content

Commit

Permalink
Add tests for Quaternion.getEulerAnglesXYZ()
Browse files Browse the repository at this point in the history
  • Loading branch information
httpdigest committed Dec 11, 2021
1 parent 9261702 commit 9d1de33
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.joml</groupId>
<artifactId>joml</artifactId>
<version>1.10.3</version>
<version>1.10.4-SNAPSHOT</version>
<name>JOML</name>
<description>Java OpenGL Math Library</description>
<inceptionYear>2015</inceptionYear>
Expand Down
22 changes: 22 additions & 0 deletions test/org/joml/test/QuaternionDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import junit.framework.TestCase;

import org.joml.*;
import org.joml.Math;

/**
* Test class for {@link Quaterniond}.
Expand Down Expand Up @@ -113,4 +114,25 @@ public static void testFromAxisAngle() {
assertEquals(fromRad1, fromRad2);
assertEquals(fromRad2, fromDeg);
}

public static void testGetEulerAnglesXYZ() {
Random rnd = new Random(1L);
int failure = 0;
int N = 3000000;
for (int i = 0; i < N; i++) {
double x = (rnd.nextFloat() * 2.0 - 1.0) * Math.PI;
double y = (rnd.nextFloat() * 2.0 - 1.0) * Math.PI;
double z = (rnd.nextFloat() * 2.0 - 1.0) * Math.PI;
Quaterniond p = new Quaterniond().rotateXYZ(x, y, z);
Vector3d a = p.getEulerAnglesXYZ(new Vector3d());
Quaterniond q = new Quaterniond().rotateX(a.x).rotateY(a.y).rotateZ(a.z);
Vector3d v = new Vector3d(rnd.nextFloat()*2-1, rnd.nextFloat()*2-1, rnd.nextFloat()*2-1);
Vector3d t1 = p.transform(v, new Vector3d());
Vector3d t2 = q.transform(v, new Vector3d());
if (!t1.equals(t2, 1E-10f))
failure++;
}
if ((float)failure / N > 0.0001f) // <- allow for a failure rate of 0.01%
throw new AssertionError();
}
}
21 changes: 21 additions & 0 deletions test/org/joml/test/QuaternionfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import junit.framework.TestCase;

import org.joml.*;
import org.joml.Math;

/**
* Test class for {@link Quaternionf}.
Expand Down Expand Up @@ -173,4 +174,24 @@ public static void testConjugateBy() {
TestUtil.assertQuaternionfEquals(r, r2, 1E-6f);
}

public static void testGetEulerAnglesXYZ() {
Random rnd = new Random(1L);
int failure = 0;
int N = 3000000;
for (int i = 0; i < N; i++) {
float x = (rnd.nextFloat() * 2f - 1f) * (float) Math.PI;
float y = (rnd.nextFloat() * 2f - 1f) * (float) Math.PI;
float z = (rnd.nextFloat() * 2f - 1f) * (float) Math.PI;
Quaternionf p = new Quaternionf().rotateXYZ(x, y, z);
Vector3f a = p.getEulerAnglesXYZ(new Vector3f());
Quaternionf q = new Quaternionf().rotateX(a.x).rotateY(a.y).rotateZ(a.z);
Vector3f v = new Vector3f(rnd.nextFloat()*2-1, rnd.nextFloat()*2-1, rnd.nextFloat()*2-1);
Vector3f t1 = p.transform(v, new Vector3f());
Vector3f t2 = q.transform(v, new Vector3f());
if (!t1.equals(t2, 1E-3f))
failure++;
}
if ((float)failure / N > 0.0001f) // <- allow for a failure rate of 0.01%
throw new AssertionError();
}
}

0 comments on commit 9d1de33

Please sign in to comment.