29package com.jogamp.opengl.test.junit.math;
31import org.junit.Assert;
33import org.junit.FixMethodOrder;
34import org.junit.runners.MethodSorters;
36import com.jogamp.common.os.Platform;
37import com.jogamp.junit.util.JunitTracer;
38import com.jogamp.math.FloatUtil;
39import com.jogamp.math.Matrix4f;
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 final float[] res1 =
new float[16];
47 final float[] res2 =
new float[16];
48 final float[] temp =
new float[16];
50 final float[] identity =
new float[] { 1, 0, 0, 0,
57 invertMatrix(identity, res2, temp);
65 Assert.assertTrue( res3.
invert() );
67 Assert.assertEquals(
new Matrix4f(res1), res3);
68 Assert.assertEquals(
new Matrix4f(), res3);
71 Assert.assertTrue( res4.
invert() );
73 Assert.assertEquals(
new Matrix4fb(res1), res4);
74 Assert.assertEquals(
new Matrix4fb(), res4);
77 private void testImpl(
final float[] matrix) {
78 final float[] inv1_0 =
new float[16];
79 final float[] inv1_1 =
new float[16];
80 final float[] inv1_2 =
new float[16];
81 final float[] inv2_0 =
new float[16];
82 final float[] inv2_1 =
new float[16];
83 final float[] inv2_2 =
new float[16];
84 final float[] temp =
new float[16];
87 invertMatrix(matrix, inv1_0, temp);
88 invertMatrix(inv1_0, inv2_0, temp);
116 Assert.assertTrue( inv1_4a.
invert() );
118 Assert.assertTrue( inv2_4a.
invert() );
129 Assert.assertTrue( inv1_4b.
invert(matrix_m) );
131 Assert.assertTrue( inv2_4b.
invert(inv1_4b) );
147 Assert.assertTrue( inv1_5a.
invert() );
149 Assert.assertTrue( inv2_5a.
invert() );
160 Assert.assertTrue( inv1_5b.
invert(matrix_n) );
162 Assert.assertTrue( inv2_5b.
invert(inv1_5b) );
175 final float[] p =
new float[] { 2.3464675f, 0, 0, 0,
184 final float[] mv =
new float[] {
194 final float[] p =
new float[] {
205 final float[] p1 =
new float[] {
213 final float[] p2 =
new float[]{
216 143, 730, 9770, 5370,
217 71, 386, 5370, 2954 };
221 final float[] res =
new float[16];
222 final float[] temp =
new float[16];
227 final int warmups = 1000;
228 final int loops = 10*1000*1000;
237 for(
int i=0; i<warmups; i++) {
238 invertMatrix(p1, res, temp);
240 long t_0 = Platform.currentTimeMillis();
241 for(
int i=0; i<loops; i++) {
243 invertMatrix(p1, res, temp);
246 invertMatrix(p2, res, temp);
248 tI0 = Platform.currentTimeMillis() - t_0;
251 for(
int i=0; i<warmups; i++) {
255 t_0 = Platform.currentTimeMillis();
256 for(
int i=0; i<loops; i++) {
263 tI2 = Platform.currentTimeMillis() - t_0;
273 for(
int i=0; i<warmups; i++) {
279 t_0 = Platform.currentTimeMillis();
280 for(
int i=0; i<loops; i++) {
286 tI4a = Platform.currentTimeMillis() - t_0;
289 for(
int i=0; i<warmups; i++) {
295 t_0 = Platform.currentTimeMillis();
296 for(
int i=0; i<loops; i++) {
302 tI4b = Platform.currentTimeMillis() - t_0;
309 for(
int i=0; i<warmups; i++) {
315 t_0 = Platform.currentTimeMillis();
316 for(
int i=0; i<loops; i++) {
322 tI5a = Platform.currentTimeMillis() - t_0;
325 for(
int i=0; i<warmups; i++) {
329 t_0 = Platform.currentTimeMillis();
330 for(
int i=0; i<loops; i++) {
336 tI5b = Platform.currentTimeMillis() - t_0;
338 System.err.printf(
"Checkmark %f%n", dr);
339 System.err.printf(
"Summary loops %6d: I0 %6d ms total, %f us/inv%n", loops, tI0, tI0*1e3/loops);
340 System.err.printf(
"Summary loops %6d: I2 %6d ms total, %f us/inv, I2 / I0 %f%%%n", loops, tI2, tI2*1e3/2.0/loops, tI2/(
double)tI0*100.0);
341 System.err.printf(
"Summary loops %6d: I4a %6d ms total, %f us/inv, I4a / I2 %f%%%n", loops, tI4a, tI4a*1e3/2.0/loops, (
double)tI4a/(
double)tI2*100.0);
342 System.err.printf(
"Summary loops %6d: I4b %6d ms total, %f us/inv, I4b / I2 %f%%%n", loops, tI4b, tI4b*1e3/2.0/loops, (
double)tI4b/(
double)tI2*100.0);
343 System.err.printf(
"Summary loops %6d: I5a %6d ms total, %f us/inv, I5a / I2 %f%%%n", loops, tI5a, tI5a*1e3/2.0/loops, (
double)tI5a/(
double)tI2*100.0);
344 System.err.printf(
"Summary loops %6d: I5b %6d ms total, %f us/inv, I5b / I2 %f%%%n", loops, tI5b, tI5b*1e3/2.0/loops, (
double)tI5b/(
double)tI2*100.0);
347 public static float[]
invertMatrix(
final float[] msrc,
final float[] mres,
final float[] temp) {
350 for (i = 0; i < 4; i++) {
352 for (j = 0; j < 4; j++) {
353 temp[i4+j] = msrc[i4+j];
358 for (i = 0; i < 4; i++) {
365 for (j = i + 1; j < 4; j++) {
366 if (Math.abs(temp[j*4+i]) > Math.abs(temp[i4+i])) {
372 final int swap4 = swap*4;
376 for (k = 0; k < 4; k++) {
378 temp[i4+k] = temp[swap4+k];
382 mres[i4+k] = mres[swap4+k];
387 if (temp[i4+i] == 0) {
396 for (k = 0; k < 4; k++) {
400 for (j = 0; j < 4; j++) {
404 for (k = 0; k < 4; k++) {
405 temp[j4+k] -= temp[i4+k] * t;
406 mres[j4+k] -= mres[i4+k]*t;
414 public static void main(
final String args[]) {
Basic Float math utility functions.
static final float INV_DEVIANCE
Inversion Epsilon, used with equals method to determine if two inverted matrices are close enough to ...
static float[] invertMatrix(final float[] msrc, final float[] mres)
Invert the given matrix.
static float[] makeIdentity(final float[] m)
Make matrix an identity matrix.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
boolean isEqual(final Matrix4f o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
boolean invert()
Invert this matrix.
float determinant()
Returns the determinant of this matrix.
float get(final int i)
Gets the ith component, 0 <= i < 16.
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
boolean invert()
Invert this matrix.
float determinant()
Returns the determinant of this matrix.
float get(final int i)
Gets the ith component, 0 <= i < 16.
boolean isEqual(final Matrix4fb o, final float epsilon)
Equals check using a given FloatUtil#EPSILON value and FloatUtil#isEqual(float, float,...
Matrix4fb load(final Matrix4fb src)
Load the values of the given matrix b to this matrix.
static void main(final String args[])
static float[] invertMatrix(final float[] msrc, final float[] mres, final float[] temp)