diff options
author | neothemachine <[email protected]> | 2012-12-05 17:03:16 +0100 |
---|---|---|
committer | neothemachine <[email protected]> | 2012-12-05 17:03:16 +0100 |
commit | 9dd02f103042cb8a196f8a3ed2278da443e345bf (patch) | |
tree | 422449f0c62ff9518316ce5d4219bb2b12f0ed15 /ardor3d-math/src/test | |
parent | 2b26b12fd794de0f03a064a10024a3d9f5583756 (diff) |
move all files from trunk to root folder
Diffstat (limited to 'ardor3d-math/src/test')
20 files changed, 5675 insertions, 0 deletions
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestColorRGBA.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestColorRGBA.java new file mode 100644 index 0000000..84e2c7d --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestColorRGBA.java @@ -0,0 +1,387 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestColorRGBA {
+
+ @Test
+ public void test() {
+ final ColorRGBA clr1 = new ColorRGBA();
+ assertTrue(1f == clr1.getRed());
+ assertTrue(1f == clr1.getGreen());
+ assertTrue(1f == clr1.getBlue());
+ assertTrue(1f == clr1.getAlpha());
+ }
+
+ @Test
+ public void testGetSet() {
+ final ColorRGBA clr1 = new ColorRGBA();
+ clr1.setRed(0f);
+ assertTrue(clr1.getRed() == 0.0f);
+ clr1.setRed(Float.POSITIVE_INFINITY);
+ assertTrue(clr1.getRed() == Float.POSITIVE_INFINITY);
+ clr1.setRed(Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getRed() == Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getValue(0) == Float.NEGATIVE_INFINITY);
+
+ clr1.setGreen(0);
+ assertTrue(clr1.getGreen() == 0.0);
+ clr1.setGreen(Float.POSITIVE_INFINITY);
+ assertTrue(clr1.getGreen() == Float.POSITIVE_INFINITY);
+ clr1.setGreen(Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getGreen() == Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getValue(1) == Float.NEGATIVE_INFINITY);
+
+ clr1.setBlue(0);
+ assertTrue(clr1.getBlue() == 0.0);
+ clr1.setBlue(Float.POSITIVE_INFINITY);
+ assertTrue(clr1.getBlue() == Float.POSITIVE_INFINITY);
+ clr1.setBlue(Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getBlue() == Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getValue(2) == Float.NEGATIVE_INFINITY);
+
+ clr1.setAlpha(0);
+ assertTrue(clr1.getAlpha() == 0.0);
+ clr1.setAlpha(Float.POSITIVE_INFINITY);
+ assertTrue(clr1.getAlpha() == Float.POSITIVE_INFINITY);
+ clr1.setAlpha(Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getAlpha() == Float.NEGATIVE_INFINITY);
+ assertTrue(clr1.getValue(3) == Float.NEGATIVE_INFINITY);
+
+ clr1.set((float) Math.PI, (float) Math.PI, (float) Math.PI, (float) Math.PI);
+ assertTrue(clr1.getRed() == (float) Math.PI);
+ assertTrue(clr1.getGreen() == (float) Math.PI);
+ assertTrue(clr1.getBlue() == (float) Math.PI);
+ assertTrue(clr1.getAlpha() == (float) Math.PI);
+
+ final ColorRGBA clr2 = new ColorRGBA(ColorRGBA.BLACK);
+ clr2.set(clr1);
+ assertEquals(clr1, clr2);
+
+ clr1.setValue(0, 1);
+ clr1.setValue(1, 1);
+ clr1.setValue(2, 1);
+ clr1.setValue(3, 1);
+ assertEquals(ColorRGBA.WHITE, clr1);
+
+ clr1.zero();
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+
+ // catch a few expected exceptions
+ try {
+ clr2.getValue(4);
+ fail("getValue(4) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ clr2.getValue(-1);
+ fail("getValue(-1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ clr2.setValue(-1, 0);
+ fail("setValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ clr2.setValue(4, 0);
+ fail("setValue(4, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered vec2
+ assertEquals(new ColorRGBA((float) Math.PI, (float) Math.PI, (float) Math.PI, (float) Math.PI), clr2);
+ }
+
+ @Test
+ public void testToArray() {
+ final ColorRGBA clr1 = new ColorRGBA(ColorRGBA.DARK_GRAY);
+
+ final float[] farray = clr1.toArray(null);
+ final float[] farray2 = clr1.toArray(new float[4]);
+ assertNotNull(farray);
+ assertNotNull(farray2);
+ assertTrue(farray.length == 4);
+ assertTrue(farray[0] == .2f);
+ assertTrue(farray[1] == .2f);
+ assertTrue(farray[2] == .2f);
+ assertTrue(farray[3] == 1f);
+
+ try {
+ clr1.toArray(new float[1]);
+ fail("toFloatArray(d[1]) should have thrown ArrayIndexOutOfBoundsException.");
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ }
+ }
+
+ @Test
+ public void testClamp() {
+ final ColorRGBA clr1 = new ColorRGBA(-1, -1, -1, -1);
+ final ColorRGBA clr2 = clr1.clamp(new ColorRGBA());
+ final ColorRGBA clr3 = clr1.clamp(null);
+ assertNotNull(clr2);
+ assertNotNull(clr3);
+ assertTrue(clr2.getRed() == 0);
+ assertTrue(clr2.getGreen() == 0);
+ assertTrue(clr2.getBlue() == 0);
+ assertTrue(clr2.getAlpha() == 0);
+
+ clr1.set(2, .5f, 1, 0);
+ clr1.clamp(clr2);
+ assertTrue(clr2.getRed() == 1);
+ assertTrue(clr2.getGreen() == .5f);
+ assertTrue(clr2.getBlue() == 1);
+ assertTrue(clr2.getAlpha() == 0);
+
+ clr1.set(2, 2, 2, 2);
+ clr1.clampLocal();
+ assertTrue(clr1.getRed() == 1);
+ assertTrue(clr1.getGreen() == 1);
+ assertTrue(clr1.getBlue() == 1);
+ assertTrue(clr1.getAlpha() == 1);
+
+ clr1.set(0.5f, 0.5f, 0.5f, 0.5f);
+ assertEquals(clr1, clr1.clamp(null));
+ }
+
+ @Test
+ public void testRandomColor() {
+ final ColorRGBA clr1 = new ColorRGBA(0, 0, 0, 0);
+ MathUtils.setRandomSeed(0);
+ ColorRGBA.randomColor(clr1);
+ assertEquals(new ColorRGBA(0.73096776f, 0.831441f, 0.24053639f, 1.0f), clr1);
+ final ColorRGBA clr2 = ColorRGBA.randomColor(null);
+ assertEquals(new ColorRGBA(0.6063452f, 0.6374174f, 0.30905056f, 1.0f), clr2);
+ }
+
+ @Test
+ public void testIntColor() {
+ assertTrue(ColorRGBA.BLACK.asIntARGB() == -16777216);
+ assertTrue(ColorRGBA.BLACK.asIntRGBA() == 255);
+ assertTrue(ColorRGBA.RED.asIntARGB() == -65536);
+ assertTrue(ColorRGBA.RED.asIntRGBA() == -16776961);
+
+ assertEquals(ColorRGBA.BLACK, new ColorRGBA().fromIntARGB(-16777216));
+ assertEquals(ColorRGBA.BLACK, new ColorRGBA().fromIntRGBA(255));
+ assertEquals(ColorRGBA.RED, new ColorRGBA().fromIntARGB(-65536));
+ assertEquals(ColorRGBA.RED, new ColorRGBA().fromIntRGBA(-16776961));
+ }
+
+ @Test
+ public void testHexColor() {
+ assertEquals("#00000000", ColorRGBA.BLACK_NO_ALPHA.asHexRRGGBBAA());
+ assertEquals("#412819ff", ColorRGBA.BROWN.asHexRRGGBBAA());
+ assertEquals("#fb8200ff", ColorRGBA.ORANGE.asHexRRGGBBAA());
+
+ assertEquals(ColorRGBA.BROWN, ColorRGBA.parseColor("#412819ff", new ColorRGBA()));
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, ColorRGBA.parseColor("#00", null));
+ assertEquals(ColorRGBA.WHITE, ColorRGBA.parseColor("#F", null));
+ assertEquals(ColorRGBA.BLACK, ColorRGBA.parseColor("#0F", null));
+ assertEquals(ColorRGBA.BLUE, ColorRGBA.parseColor("#00F", null));
+ assertEquals(ColorRGBA.YELLOW, ColorRGBA.parseColor("#FF0F", null));
+ assertEquals(ColorRGBA.MAGENTA, ColorRGBA.parseColor("#FF00FF", null));
+ assertEquals(ColorRGBA.CYAN, ColorRGBA.parseColor("#00FFFFFF", null));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testHexFail1() {
+ ColorRGBA.parseColor("000", null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testHexFail2() {
+ ColorRGBA.parseColor("#000000000000000000000", null);
+ }
+
+ @Test
+ public void testClone() {
+ final ColorRGBA clr1 = new ColorRGBA();
+ final ColorRGBA clr2 = clr1.clone();
+ assertEquals(clr1, clr2);
+ assertNotSame(clr1, clr2);
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final ColorRGBA clr1 = new ColorRGBA(1, 0, 0, 1);
+ final ColorRGBA clr2 = new ColorRGBA(1, 0, 0, 1);
+ final ColorRGBA clr3 = new ColorRGBA(1, 1, 1, 1);
+
+ assertTrue(clr1.hashCode() == clr2.hashCode());
+ assertTrue(clr1.hashCode() != clr3.hashCode());
+ }
+
+ @Test
+ public void testAdd() {
+ final ColorRGBA clr1 = new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA);
+ final ColorRGBA clr2 = new ColorRGBA(ColorRGBA.WHITE);
+
+ clr1.addLocal(1, 2, 3, 4);
+ assertEquals(new ColorRGBA(1, 2, 3, 4), clr1);
+ clr1.addLocal(-1, -2, -3, -4);
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+
+ clr1.zero();
+ clr1.addLocal(clr2);
+ assertEquals(ColorRGBA.WHITE, clr1);
+
+ clr1.zero();
+ final ColorRGBA clr3 = clr1.add(clr2, new ColorRGBA());
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+ assertEquals(ColorRGBA.WHITE, clr3);
+
+ final ColorRGBA clr4 = clr1.add(0, 0, 0, 1, null);
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+ assertEquals(ColorRGBA.BLACK, clr4);
+ }
+
+ @Test
+ public void testSubtract() {
+ final ColorRGBA clr1 = new ColorRGBA(ColorRGBA.BLACK_NO_ALPHA);
+ final ColorRGBA clr2 = new ColorRGBA(ColorRGBA.WHITE);
+
+ clr1.subtractLocal(1, 2, 3, 4);
+ assertEquals(new ColorRGBA(-1, -2, -3, -4), clr1);
+ clr1.subtractLocal(-1, -2, -3, -4);
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+
+ clr1.zero();
+ clr1.subtractLocal(clr2);
+ assertEquals(new ColorRGBA(-1, -1, -1, -1), clr1);
+
+ clr1.zero();
+ final ColorRGBA clr3 = clr1.subtract(clr2, new ColorRGBA());
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+ assertEquals(new ColorRGBA(-1, -1, -1, -1), clr3);
+
+ final ColorRGBA clr4 = clr1.subtract(0, 0, 0, 1, null);
+ assertEquals(ColorRGBA.BLACK_NO_ALPHA, clr1);
+ assertEquals(new ColorRGBA(0, 0, 0, -1), clr4);
+ }
+
+ @Test
+ public void testMultiply() {
+ final ColorRGBA clr1 = new ColorRGBA(1, -1, 2, -2);
+ final ColorRGBA clr2 = clr1.multiply(2.0f, null);
+ final ColorRGBA clr2B = clr1.multiply(2.0f, new ColorRGBA());
+ assertEquals(new ColorRGBA(2.0f, -2.0f, 4.0f, -4.0f), clr2);
+ assertEquals(new ColorRGBA(2.0f, -2.0f, 4.0f, -4.0f), clr2B);
+
+ clr2.multiplyLocal(0.5f);
+ assertEquals(new ColorRGBA(1.0f, -1.0f, 2.0f, -2.0f), clr2);
+
+ final ColorRGBA clr3 = clr1.multiply(clr2, null);
+ final ColorRGBA clr3B = clr1.multiply(clr2, new ColorRGBA());
+ assertEquals(new ColorRGBA(1, 1, 4, 4), clr3);
+ assertEquals(new ColorRGBA(1, 1, 4, 4), clr3B);
+
+ clr1.multiplyLocal(clr2);
+ assertEquals(new ColorRGBA(1, 1, 4, 4), clr1);
+ }
+
+ @Test
+ public void testDivide() {
+ final ColorRGBA clr1 = new ColorRGBA(1, -1, 2, -2);
+ final ColorRGBA clr2 = clr1.divide(2.0f, null);
+ final ColorRGBA clr2B = clr1.divide(2.0f, new ColorRGBA());
+ assertEquals(new ColorRGBA(0.5f, -0.5f, 1.0f, -1.0f), clr2);
+ assertEquals(new ColorRGBA(0.5f, -0.5f, 1.0f, -1.0f), clr2B);
+
+ clr2.divideLocal(0.5f);
+ assertEquals(new ColorRGBA(1.0f, -1.0f, 2.0f, -2.0f), clr2);
+
+ final ColorRGBA clr3 = clr1.divide(clr2, null);
+ final ColorRGBA clr3B = clr1.divide(clr2, new ColorRGBA());
+ assertEquals(ColorRGBA.WHITE, clr3);
+ assertEquals(ColorRGBA.WHITE, clr3B);
+
+ clr1.divideLocal(clr2);
+ assertEquals(ColorRGBA.WHITE, clr1);
+ }
+
+ @Test
+ public void testLerp() {
+ final ColorRGBA clr1 = new ColorRGBA(8, 3, -2, 2);
+ final ColorRGBA clr2 = new ColorRGBA(2, 1, 0, -2);
+ assertEquals(new ColorRGBA(5, 2, -1, 0), clr1.lerp(clr2, 0.5f, null));
+ assertEquals(new ColorRGBA(5, 2, -1, 0), clr1.lerp(clr2, 0.5f, new ColorRGBA()));
+ assertEquals(new ColorRGBA(5, 2, -1, 0), ColorRGBA.lerp(clr1, clr2, 0.5f, null));
+ assertEquals(new ColorRGBA(5, 2, -1, 0), ColorRGBA.lerp(clr1, clr2, 0.5f, new ColorRGBA()));
+
+ clr1.set(14, 5, 4, 2);
+ clr1.lerpLocal(clr2, 0.25f);
+ assertEquals(new ColorRGBA(11, 4, 3, 1), clr1);
+
+ clr1.set(15, 7, 6, 8);
+ final ColorRGBA clr3 = new ColorRGBA(-1, -1, -1, -1);
+ clr3.lerpLocal(clr1, clr2, 0.5f);
+ assertEquals(new ColorRGBA(8.5f, 4.0f, 3.0f, 3.0f), clr3);
+
+ // coverage
+ assertEquals(clr1.lerp(clr1, .25f, null), clr1);
+ assertEquals(clr2.lerpLocal(clr2, .25f), clr2);
+ assertEquals(clr2.lerpLocal(clr2, clr2, .25f), clr2);
+ assertEquals(ColorRGBA.lerp(clr1, clr1, .25f, null), clr1);
+ }
+
+ @Test
+ public void testValid() {
+ final ColorRGBA clr1 = new ColorRGBA(0, 0, 0, 0);
+ final ColorRGBA clr2A = new ColorRGBA(Float.POSITIVE_INFINITY, 0, 0, 0);
+ final ColorRGBA clr2B = new ColorRGBA(0, Float.NEGATIVE_INFINITY, 0, 0);
+ final ColorRGBA clr2C = new ColorRGBA(0, 0, Float.POSITIVE_INFINITY, 0);
+ final ColorRGBA clr2D = new ColorRGBA(0, 0, 0, Float.POSITIVE_INFINITY);
+ final ColorRGBA clr3A = new ColorRGBA(Float.NaN, 0, 0, 0);
+ final ColorRGBA clr3B = new ColorRGBA(0, Float.NaN, 0, 0);
+ final ColorRGBA clr3C = new ColorRGBA(0, 0, Float.NaN, 0);
+ final ColorRGBA clr3D = new ColorRGBA(0, 0, 0, Float.NaN);
+
+ assertTrue(ColorRGBA.isValid(clr1));
+ assertFalse(ColorRGBA.isValid(clr2A));
+ assertFalse(ColorRGBA.isValid(clr2B));
+ assertFalse(ColorRGBA.isValid(clr2C));
+ assertFalse(ColorRGBA.isValid(clr2D));
+ assertFalse(ColorRGBA.isValid(clr3A));
+ assertFalse(ColorRGBA.isValid(clr3B));
+ assertFalse(ColorRGBA.isValid(clr3C));
+ assertFalse(ColorRGBA.isValid(clr3D));
+
+ clr3C.zero();
+ assertTrue(ColorRGBA.isValid(clr3C));
+
+ assertFalse(ColorRGBA.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(clr1, clr1);
+ assertFalse(clr1.equals(null));
+ assertFalse(clr1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final ColorRGBA clr6 = ColorRGBA.fetchTempInstance();
+ clr6.set(clr1);
+ assertEquals(clr1, clr6);
+ assertNotSame(clr1, clr6);
+ ColorRGBA.releaseTempInstance(clr6);
+
+ // cover more of equals
+ clr1.set(0, 1, 2, 3);
+ assertFalse(clr1.equals(new ColorRGBA(4, 4, 4, 4)));
+ assertFalse(clr1.equals(new ColorRGBA(0, 4, 4, 4)));
+ assertFalse(clr1.equals(new ColorRGBA(0, 1, 4, 4)));
+ assertFalse(clr1.equals(new ColorRGBA(0, 1, 2, 4)));
+ assertTrue(clr1.equals(new ColorRGBA(0, 1, 2, 3)));
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestFastMath.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestFastMath.java new file mode 100644 index 0000000..93c4bc1 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestFastMath.java @@ -0,0 +1,71 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestFastMath {
+
+ @Test
+ public void testSin() {
+ final double angle = MathUtils.DEG_TO_RAD * 45;
+ assertTrue(Math.abs(FastMath.sin(angle) - Math.sin(angle)) <= FastMath.EPSILON_SIN);
+ }
+
+ @Test
+ public void testCos() {
+ double angle = MathUtils.DEG_TO_RAD * 45;
+ assertTrue(Math.abs(FastMath.cos(angle) - Math.cos(angle)) <= FastMath.EPSILON_COS);
+ angle = MathUtils.DEG_TO_RAD * 135;
+ assertTrue(Math.abs(FastMath.cos(angle) - Math.cos(angle)) <= FastMath.EPSILON_COS);
+ }
+
+ @Test
+ public void testTan() {
+ final double angle = MathUtils.DEG_TO_RAD * 45;
+ assertTrue(Math.abs(FastMath.tan(angle) - Math.tan(angle)) <= FastMath.EPSILON_SIN2COS2);
+ }
+
+ @Test
+ public void testAsin() {
+ final double val = 0.5;
+ assertTrue(Math.abs(FastMath.asin(val) - Math.asin(val)) <= FastMath.EPSILON_ASIN);
+ }
+
+ @Test
+ public void testAcos() {
+ final double val = 0.5;
+ assertTrue(Math.abs(FastMath.acos(val) - Math.acos(val)) <= FastMath.EPSILON_ACOS);
+ }
+
+ @Test
+ public void testAtan() {
+ double val = 1;
+ assertTrue(Math.abs(FastMath.atan(val) - Math.atan(val)) <= FastMath.EPSILON_ATAN);
+ val = 0.5;
+ assertTrue(Math.abs(FastMath.atan(val) - Math.atan(val)) <= FastMath.EPSILON_ATAN);
+ }
+
+ @Test
+ public void testInverseSqrt() {
+ final double val = 173;
+ assertTrue(Math.abs(FastMath.inverseSqrt(val) - 1.0 / Math.sqrt(val)) <= FastMath.EPSILON_SQRT);
+ }
+
+ @Test
+ public void testSqrt() {
+ final double val = 173;
+ assertTrue(Math.abs(FastMath.sqrt(val) - Math.sqrt(val)) <= FastMath.EPSILON_SQRT);
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestLine3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestLine3.java new file mode 100644 index 0000000..54fb4de --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestLine3.java @@ -0,0 +1,105 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestLine3 {
+
+ @Test
+ public void testGetSet() {
+ final Line3 line1 = new Line3();
+ assertEquals(Vector3.ZERO, line1.getOrigin());
+ assertEquals(Vector3.UNIT_Z, line1.getDirection());
+
+ line1.setOrigin(Vector3.NEG_ONE);
+ line1.setDirection(Vector3.UNIT_X);
+ assertEquals(Vector3.NEG_ONE, line1.getOrigin());
+ assertEquals(Vector3.UNIT_X, line1.getDirection());
+
+ final Line3 line2 = new Line3(line1);
+ assertEquals(Vector3.NEG_ONE, line2.getOrigin());
+ assertEquals(Vector3.UNIT_X, line2.getDirection());
+
+ final Line3 line3 = new Line3(Vector3.ONE, Vector3.UNIT_Y);
+ assertEquals(Vector3.ONE, line3.getOrigin());
+ assertEquals(Vector3.UNIT_Y, line3.getDirection());
+ }
+
+ @Test
+ public void testEquals() {
+ // couple of equals validity tests
+ final Line3 line1 = new Line3();
+ assertEquals(line1, line1);
+ assertFalse(line1.equals(null));
+ assertFalse(line1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Line3 line2 = Line3.fetchTempInstance();
+ line2.set(line1);
+ assertEquals(line1, line2);
+ assertNotSame(line1, line2);
+ Line3.releaseTempInstance(line2);
+
+ // cover more of equals
+ assertFalse(line1.equals(new Line3(Vector3.ZERO, Vector3.UNIT_X)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Line3 line1 = new Line3(Vector3.ZERO, Vector3.UNIT_Y);
+ final Line3 line2 = new Line3(Vector3.ZERO, Vector3.UNIT_Y);
+ final Line3 line3 = new Line3(Vector3.ZERO, Vector3.UNIT_Z);
+
+ assertTrue(line1.hashCode() == line2.hashCode());
+ assertTrue(line1.hashCode() != line3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final Line3 line1 = new Line3();
+ final Line3 line2 = line1.clone();
+ assertEquals(line1, line2);
+ assertNotSame(line1, line2);
+ }
+
+ @Test
+ public void testValid() {
+ final Line3 line1 = new Line3();
+ final Line3 line2 = new Line3(new Vector3(Double.NaN, 0, 0), Vector3.UNIT_Z);
+ final Line3 line3 = new Line3(Vector3.ZERO, new Vector3(Double.NaN, 0, 0));
+
+ assertTrue(Line3.isValid(line1));
+ assertFalse(Line3.isValid(line2));
+ assertFalse(Line3.isValid(line3));
+
+ line2.setOrigin(Vector3.ZERO);
+ assertTrue(Line3.isValid(line2));
+
+ assertFalse(Line3.isValid(null));
+ }
+
+ @Test
+ public void testDistance() {
+ final Line3 line1 = new Line3(Vector3.ZERO, Vector3.UNIT_Z);
+ final Vector3 store = new Vector3();
+ assertTrue(0.0 == line1.distanceSquared(new Vector3(0, 0, 5), store));
+ assertTrue(16.0 == line1.distanceSquared(new Vector3(0, 4, 1), store));
+ assertEquals(Vector3.UNIT_Z, store);
+ assertTrue(9.0 == line1.distanceSquared(new Vector3(0, -3, -1), store));
+ assertEquals(Vector3.NEG_UNIT_Z, store);
+ assertTrue(1.0 == line1.distanceSquared(Vector3.NEG_UNIT_X, null));
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestLineSegment3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestLineSegment3.java new file mode 100644 index 0000000..9b3d9c8 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestLineSegment3.java @@ -0,0 +1,140 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestLineSegment3 {
+
+ @Test
+ public void testGetSet() {
+ final LineSegment3 seg1 = new LineSegment3();
+ assertEquals(Vector3.ZERO, seg1.getOrigin());
+ assertEquals(Vector3.UNIT_Z, seg1.getDirection());
+ assertTrue(seg1.getExtent() == 0.5);
+
+ seg1.setOrigin(Vector3.NEG_ONE);
+ seg1.setDirection(Vector3.UNIT_X);
+ seg1.setExtent(42.0);
+ assertEquals(Vector3.NEG_ONE, seg1.getOrigin());
+ assertEquals(Vector3.UNIT_X, seg1.getDirection());
+ assertTrue(seg1.getExtent() == 42.0);
+
+ final LineSegment3 seg2 = new LineSegment3(seg1);
+ assertEquals(Vector3.NEG_ONE, seg2.getOrigin());
+ assertEquals(Vector3.UNIT_X, seg2.getDirection());
+ assertTrue(seg2.getExtent() == 42.0);
+
+ final LineSegment3 seg3 = new LineSegment3(Vector3.ONE, Vector3.UNIT_Y, 2.5);
+ assertEquals(Vector3.ONE, seg3.getOrigin());
+ assertEquals(Vector3.UNIT_Y, seg3.getDirection());
+ assertTrue(seg3.getExtent() == 2.5);
+
+ final LineSegment3 seg4 = new LineSegment3(new Vector3(9, 2, 2), new Vector3(5, 2, 2));
+ assertEquals(new Vector3(7, 2, 2), seg4.getOrigin());
+ assertEquals(Vector3.NEG_UNIT_X, seg4.getDirection());
+ assertTrue(seg4.getExtent() == 2);
+
+ assertEquals(new Vector3(9, 2, 2), seg4.getNegativeEnd(null));
+ assertEquals(new Vector3(1, -1.5, 1), seg3.getNegativeEnd(new Vector3()));
+ assertEquals(new Vector3(5, 2, 2), seg4.getPositiveEnd(null));
+ assertEquals(new Vector3(1, 3.5, 1), seg3.getPositiveEnd(new Vector3()));
+
+ assertFalse(seg3.equals(new LineSegment3(Vector3.ONE, Vector3.ONE, 42)));
+ assertFalse(seg3.equals(new LineSegment3(Vector3.ONE, Vector3.UNIT_Y, 42)));
+ }
+
+ @Test
+ public void testEquals() {
+ // couple of equals validity tests
+ final LineSegment3 seg1 = new LineSegment3();
+ assertEquals(seg1, seg1);
+ assertFalse(seg1.equals(null));
+ assertFalse(seg1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final LineSegment3 seg2 = LineSegment3.fetchTempInstance();
+ seg2.set(seg1);
+ assertEquals(seg1, seg2);
+ assertNotSame(seg1, seg2);
+ LineSegment3.releaseTempInstance(seg2);
+
+ // cover more of equals
+ assertFalse(seg1.equals(new LineSegment3(Vector3.ZERO, Vector3.UNIT_X, 2)));
+ assertFalse(seg1.equals(new LineSegment3(Vector3.ZERO, Vector3.UNIT_Z, 2)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final LineSegment3 seg1 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Y, 2);
+ final LineSegment3 seg2 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Y, 2);
+ final LineSegment3 seg3 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Y, 4);
+
+ assertTrue(seg1.hashCode() == seg2.hashCode());
+ assertTrue(seg1.hashCode() != seg3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final LineSegment3 seg1 = new LineSegment3();
+ final LineSegment3 seg2 = seg1.clone();
+ assertEquals(seg1, seg2);
+ assertNotSame(seg1, seg2);
+ }
+
+ @Test
+ public void testRandom() {
+ MathUtils.setRandomSeed(0);
+ final LineSegment3 seg1 = new LineSegment3();
+ final Vector3 store = seg1.random(null);
+ assertEquals(new Vector3(0.0, 0.0, 0.23096778737665702), store);
+
+ seg1.random(store);
+ assertEquals(new Vector3(0.0, 0.0, -0.25946358432851413), store);
+ }
+
+ @Test
+ public void testValid() {
+ final LineSegment3 seg1 = new LineSegment3();
+ final LineSegment3 seg2 = new LineSegment3(new Vector3(Double.NaN, 0, 0), Vector3.UNIT_Z, 0.5);
+ final LineSegment3 seg3 = new LineSegment3(Vector3.ZERO, new Vector3(Double.NaN, 0, 0), 0.5);
+ final LineSegment3 seg4 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Z, Double.NaN);
+ final LineSegment3 seg5 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Z, Double.POSITIVE_INFINITY);
+
+ assertTrue(LineSegment3.isValid(seg1));
+ assertFalse(LineSegment3.isValid(seg2));
+ assertFalse(LineSegment3.isValid(seg3));
+ assertFalse(LineSegment3.isValid(seg4));
+ assertFalse(LineSegment3.isValid(seg5));
+
+ seg5.setExtent(1);
+ assertTrue(LineSegment3.isValid(seg5));
+
+ assertFalse(LineSegment3.isValid(null));
+ }
+
+ @Test
+ public void testDistance() {
+ final LineSegment3 seg1 = new LineSegment3(Vector3.ZERO, Vector3.UNIT_Z, 1.0);
+ final Vector3 store = new Vector3();
+ assertTrue(16.0 == seg1.distanceSquared(new Vector3(0, 0, 5), store));
+ assertEquals(Vector3.UNIT_Z, store);
+ assertTrue(9.0 == seg1.distanceSquared(new Vector3(0, 0, -4), store));
+ assertEquals(Vector3.NEG_UNIT_Z, store);
+ assertTrue(4.0 == seg1.distanceSquared(new Vector3(2, 0, 0), store));
+ assertEquals(Vector3.ZERO, store);
+ assertTrue(1.0 == seg1.distanceSquared(Vector3.NEG_UNIT_X, null));
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestMathExceptions.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestMathExceptions.java new file mode 100644 index 0000000..74b3c06 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestMathExceptions.java @@ -0,0 +1,41 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestMathExceptions {
+ @Test
+ public void testInvalidTransformException() {
+ final InvalidTransformException ex1 = new InvalidTransformException();
+ final InvalidTransformException ex2 = new InvalidTransformException("ABC");
+ final Exception a = new Exception();
+ final InvalidTransformException ex3 = new InvalidTransformException(a);
+ final InvalidTransformException ex4 = new InvalidTransformException("DEF", a);
+
+ assertNotNull(ex1);
+ assertEquals("ABC", ex2.getMessage());
+ assertEquals(a, ex3.getCause());
+ assertEquals("DEF", ex4.getMessage());
+ assertEquals(a, ex4.getCause());
+ }
+
+ @Test
+ public void testTransformException() {
+ final TransformException ex1 = new TransformException();
+ final TransformException ex2 = new TransformException("ABC");
+
+ assertNotNull(ex1);
+ assertEquals("ABC", ex2.getMessage());
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix3.java new file mode 100644 index 0000000..6684c30 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix3.java @@ -0,0 +1,808 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+
+import org.junit.Test;
+
+public class TestMatrix3 {
+
+ @Test
+ public void testGetSet() {
+ final Matrix3 mat3A = new Matrix3();
+ assertEquals(Matrix3.IDENTITY, mat3A);
+
+ mat3A.setM00(0.0);
+ mat3A.setM01(0.1);
+ mat3A.setM02(0.2);
+ mat3A.setM10(1.0);
+ mat3A.setM11(1.1);
+ mat3A.setM12(1.2);
+ mat3A.setM20(2.0);
+ mat3A.setM21(2.1);
+ mat3A.setM22(2.2);
+
+ assertTrue(0.0 == mat3A.getM00());
+ assertTrue(0.1 == mat3A.getM01());
+ assertTrue(0.2 == mat3A.getM02());
+ assertTrue(1.0 == mat3A.getM10());
+ assertTrue(1.1 == mat3A.getM11());
+ assertTrue(1.2 == mat3A.getM12());
+ assertTrue(2.0 == mat3A.getM20());
+ assertTrue(2.1 == mat3A.getM21());
+ assertTrue(2.2 == mat3A.getM22());
+
+ final Matrix3 mat3B = new Matrix3(mat3A);
+ assertTrue(0.0 == mat3B.getM00());
+ assertTrue(0.1 == mat3B.getM01());
+ assertTrue(0.2 == mat3B.getM02());
+ assertTrue(1.0 == mat3B.getM10());
+ assertTrue(1.1 == mat3B.getM11());
+ assertTrue(1.2 == mat3B.getM12());
+ assertTrue(2.0 == mat3B.getM20());
+ assertTrue(2.1 == mat3B.getM21());
+ assertTrue(2.2 == mat3B.getM22());
+
+ final Matrix3 mat3C = new Matrix3(0.0, 0.1, 0.2, 1.0, 1.1, 1.2, 2.0, 2.1, 2.2);
+ assertTrue(0.0 == mat3C.getM00());
+ assertTrue(0.1 == mat3C.getM01());
+ assertTrue(0.2 == mat3C.getM02());
+ assertTrue(1.0 == mat3C.getM10());
+ assertTrue(1.1 == mat3C.getM11());
+ assertTrue(1.2 == mat3C.getM12());
+ assertTrue(2.0 == mat3C.getM20());
+ assertTrue(2.1 == mat3C.getM21());
+ assertTrue(2.2 == mat3C.getM22());
+
+ mat3C.setIdentity();
+ assertTrue(mat3C.isIdentity());
+
+ for (int x = 0; x < 3; x++) {
+ for (int y = 0; y < 3; y++) {
+ final double value = (10 * x + y) / 10.;
+ mat3C.setValue(x, y, value);
+ assertTrue(value == mat3C.getValue(x, y));
+ }
+ }
+
+ mat3C.setIdentity();
+ mat3C.set(0.0, 0.1, 0.2, 2.0, 2.1, 2.2, 4.0, 4.1, 4.2);
+ for (int x = 0; x < 3; x++) {
+ for (int y = 0; y < 3; y++) {
+ assertTrue((20 * x + y) / 10.f == mat3C.getValuef(x, y));
+ }
+ }
+
+ final Matrix3 store = new Matrix3(mat3C);
+ // catch a few expected exceptions
+ try {
+ mat3C.getValue(-1, 0);
+ fail("getValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.getValue(0, 3);
+ fail("getValue(0, 3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.getValue(1, -1);
+ fail("getValue(1, -1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.getValue(2, 3);
+ fail("getValue(2, 3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.getValue(3, 0);
+ fail("getValue(3, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+
+ try {
+ mat3C.setValue(-1, 0, 0);
+ fail("setValue(-1, 0, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.setValue(0, -1, 0);
+ fail("setValue(0, -1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.setValue(1, 3, 0);
+ fail("setValue(1, 3, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.setValue(2, -1, 0);
+ fail("setValue(2, -1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3C.setValue(3, 0, 0);
+ fail("setValue(3, 0, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered mat3C
+ assertEquals(store, mat3C);
+ }
+
+ @Test
+ public void testColumns() {
+ final Matrix3 mat3A = new Matrix3();
+ mat3A.setColumn(0, new Vector3(0, 3, 6));
+ mat3A.setColumn(1, new Vector3(1, 4, 7));
+ mat3A.setColumn(2, new Vector3(2, 5, 8));
+ assertEquals(new Vector3(0, 3, 6), mat3A.getColumn(0, new Vector3()));
+ assertEquals(new Vector3(1, 4, 7), mat3A.getColumn(1, null));
+ assertEquals(new Vector3(2, 5, 8), mat3A.getColumn(2, null));
+ try {
+ mat3A.getColumn(-1, null);
+ fail("getColumn(-1, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.getColumn(3, null);
+ fail("getColumn(3, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.setColumn(-1, new Vector3());
+ fail("setColumn(-1, Vector3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.setColumn(4, new Vector3());
+ fail("setColumn(4, Vector3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+
+ mat3A.fromAxes(new Vector3(1, 2, 3), new Vector3(4, 5, 6), new Vector3(7, 8, 9));
+ mat3A.setColumn(0, new Vector3(1, 2, 3));
+ mat3A.setColumn(1, new Vector3(4, 5, 6));
+ mat3A.setColumn(2, new Vector3(7, 8, 9));
+ }
+
+ @Test
+ public void testRows() {
+ final Matrix3 mat3A = new Matrix3();
+ mat3A.setRow(0, new Vector3(0, 1, 2));
+ mat3A.setRow(1, new Vector3(3, 4, 5));
+ mat3A.setRow(2, new Vector3(6, 7, 8));
+ assertEquals(new Vector3(0, 1, 2), mat3A.getRow(0, new Vector3()));
+ assertEquals(new Vector3(3, 4, 5), mat3A.getRow(1, null));
+ assertEquals(new Vector3(6, 7, 8), mat3A.getRow(2, null));
+ try {
+ mat3A.getRow(-1, null);
+ fail("getRow(-1, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.getRow(3, null);
+ fail("getRow(3, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.setRow(-1, new Vector3());
+ fail("setRow(-1, Vector3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat3A.setRow(3, new Vector3());
+ fail("setRow(3, Vector3]) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ }
+
+ @Test
+ public void testSetRotation() {
+ final Matrix3 mat3A = new Matrix3();
+ // rotate identity 90 degrees around Y
+ final double a = MathUtils.HALF_PI;
+ final Quaternion quaternion = new Quaternion();
+ quaternion.fromAngleAxis(a, Vector3.UNIT_Y);
+ mat3A.set(quaternion);
+
+ assertEquals(new Matrix3( //
+ Math.cos(a), 0, Math.sin(a), //
+ 0, 1, 0, //
+ -Math.sin(a), 0, Math.cos(a)), mat3A);
+ }
+
+ @Test
+ public void testFromBuffer() {
+ final FloatBuffer fb = FloatBuffer.allocate(9);
+ fb.put(new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
+ fb.flip();
+ // row major
+ final Matrix3 mat3A = new Matrix3().fromFloatBuffer(fb);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(1 == mat3A.getM01());
+ assertTrue(2 == mat3A.getM02());
+ assertTrue(3 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(5 == mat3A.getM12());
+ assertTrue(6 == mat3A.getM20());
+ assertTrue(7 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+
+ // column major
+ fb.rewind();
+ mat3A.setIdentity();
+ mat3A.fromFloatBuffer(fb, false);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(3 == mat3A.getM01());
+ assertTrue(6 == mat3A.getM02());
+ assertTrue(1 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(7 == mat3A.getM12());
+ assertTrue(2 == mat3A.getM20());
+ assertTrue(5 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+
+ final DoubleBuffer db = DoubleBuffer.allocate(16);
+ db.put(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
+ db.flip();
+ // row major
+ mat3A.setIdentity();
+ mat3A.fromDoubleBuffer(db);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(1 == mat3A.getM01());
+ assertTrue(2 == mat3A.getM02());
+ assertTrue(3 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(5 == mat3A.getM12());
+ assertTrue(6 == mat3A.getM20());
+ assertTrue(7 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+
+ // column major
+ db.rewind();
+ mat3A.setIdentity();
+ mat3A.fromDoubleBuffer(db, false);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(3 == mat3A.getM01());
+ assertTrue(6 == mat3A.getM02());
+ assertTrue(1 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(7 == mat3A.getM12());
+ assertTrue(2 == mat3A.getM20());
+ assertTrue(5 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+ }
+
+ @Test
+ public void testToBuffer() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
+ final double[] colmajor = { 0, 3, 6, 1, 4, 7, 2, 5, 8 };
+
+ final Matrix3 mat3A = new Matrix3().fromArray(values);
+
+ // row major
+ final FloatBuffer fb = mat3A.toFloatBuffer(FloatBuffer.allocate(9));
+ fb.flip();
+ for (int i = 0; i < 9; i++) {
+ assertTrue(values[i] == fb.get());
+ }
+
+ // column major
+ fb.rewind();
+ mat3A.toFloatBuffer(fb, false);
+ fb.flip();
+ for (int i = 0; i < 9; i++) {
+ assertTrue(colmajor[i] == fb.get());
+ }
+
+ // row major
+ final DoubleBuffer db = mat3A.toDoubleBuffer(DoubleBuffer.allocate(9));
+ db.flip();
+ for (int i = 0; i < 9; i++) {
+ assertTrue(values[i] == db.get());
+ }
+
+ // column major
+ db.rewind();
+ mat3A.toDoubleBuffer(db, false);
+ db.flip();
+ for (int i = 0; i < 9; i++) {
+ assertTrue(colmajor[i] == db.get());
+ }
+ }
+
+ @Test
+ public void testFromArray() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
+ final Matrix3 mat3A = new Matrix3();
+
+ // row major
+ mat3A.fromArray(values);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(1 == mat3A.getM01());
+ assertTrue(2 == mat3A.getM02());
+ assertTrue(3 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(5 == mat3A.getM12());
+ assertTrue(6 == mat3A.getM20());
+ assertTrue(7 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+
+ // column major
+ mat3A.setIdentity();
+ mat3A.fromArray(values, false);
+ assertTrue(0 == mat3A.getM00());
+ assertTrue(3 == mat3A.getM01());
+ assertTrue(6 == mat3A.getM02());
+ assertTrue(1 == mat3A.getM10());
+ assertTrue(4 == mat3A.getM11());
+ assertTrue(7 == mat3A.getM12());
+ assertTrue(2 == mat3A.getM20());
+ assertTrue(5 == mat3A.getM21());
+ assertTrue(8 == mat3A.getM22());
+ }
+
+ @Test
+ public void testToArray() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
+ final Matrix3 mat3A = new Matrix3().fromArray(values);
+
+ // row major
+ final double[] dbls1 = mat3A.toArray(null);
+ for (int i = 0; i < 9; i++) {
+ assertTrue(values[i] == dbls1[i]);
+ }
+
+ // column major
+ final double[] colmajor = { 0, 3, 6, 1, 4, 7, 2, 5, 8 };
+ mat3A.toArray(dbls1, false);
+ for (int i = 0; i < 9; i++) {
+ assertTrue(colmajor[i] == dbls1[i]);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBadArray() {
+ final Matrix3 mat3A = new Matrix3();
+ mat3A.toArray(new double[4]);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBadAnglesArray() {
+ final Matrix3 mat3A = new Matrix3();
+ mat3A.toAngles(new double[2]);
+ }
+
+ @Test
+ public void testAngleAxis() {
+ final Matrix3 mat3A = new Matrix3();
+ // rotate identity 90 degrees around X
+ final double angle = MathUtils.HALF_PI;
+ mat3A.fromAngleAxis(MathUtils.HALF_PI, Vector3.UNIT_X);
+ assertEquals(new Matrix3( //
+ 1, 0, 0, //
+ 0, Math.cos(angle), -Math.sin(angle), //
+ 0, Math.sin(angle), Math.cos(angle)), mat3A);
+ }
+
+ @Test
+ public void testRotations() {
+ final Vector3 rotated = new Vector3(1, 1, 1);
+ final Vector3 expected = new Vector3(1, 1, 1);
+
+ // rotated
+ final Matrix3 mat3A = new Matrix3().fromAngles(MathUtils.HALF_PI, MathUtils.QUARTER_PI, MathUtils.PI);
+ mat3A.applyPost(rotated, rotated);
+
+ // expected - post
+ final Matrix3 worker = new Matrix3().fromAngleAxis(MathUtils.HALF_PI, Vector3.UNIT_X);
+ worker.applyPost(expected, expected);
+ worker.fromAngleAxis(MathUtils.PI, Vector3.UNIT_Z);
+ worker.applyPost(expected, expected);
+ worker.fromAngleAxis(MathUtils.QUARTER_PI, Vector3.UNIT_Y);
+ worker.applyPost(expected, expected);
+
+ // test how close it came out
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Try a new way with new angles...
+ final Matrix3 mat3B = new Matrix3().fromAngles(MathUtils.QUARTER_PI, MathUtils.PI, MathUtils.HALF_PI);
+ rotated.set(1, 1, 1);
+ mat3B.applyPost(rotated, rotated);
+
+ // expected
+ expected.set(1, 1, 1);
+ worker.setIdentity();
+ // put together matrix, then apply to vector, so YZX
+ worker.applyRotationY(MathUtils.PI);
+ worker.applyRotationZ(MathUtils.HALF_PI);
+ worker.applyRotationX(MathUtils.QUARTER_PI);
+ worker.applyPost(expected, expected);
+
+ // test how close it came out
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // test axis rotation methods against general purpose
+ // X AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationX(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 1, 0, 0).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Y AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationY(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 1, 0).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Z AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationZ(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 0, 1).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // test toAngles - not necessarily the same values as fromAngles, but should be same resulting Matrix.
+ mat3A.fromAngles(MathUtils.HALF_PI, MathUtils.QUARTER_PI, MathUtils.PI);
+ final double[] angles = mat3A.toAngles(null);
+ worker.fromAngles(angles[0], angles[1], angles[2]);
+ assertEquals(mat3A, worker);
+
+ mat3A.fromAngles(MathUtils.HALF_PI, MathUtils.QUARTER_PI, MathUtils.HALF_PI);
+ mat3A.toAngles(angles);
+ worker.fromAngles(angles[0], angles[1], angles[2]);
+ assertEquals(mat3A, worker);
+
+ mat3A.fromAngles(MathUtils.HALF_PI, MathUtils.QUARTER_PI, -MathUtils.HALF_PI);
+ mat3A.toAngles(angles);
+ worker.fromAngles(angles[0], angles[1], angles[2]);
+ assertEquals(mat3A, worker);
+ }
+
+ @Test
+ public void testMultiplyDiagonal() {
+ final Matrix3 mat3A = new Matrix3();
+ Matrix3 result = mat3A.multiplyDiagonalPost(new Vector3(2, 4, 6), null);
+ assertEquals(new Matrix3( //
+ 2, 0, 0, //
+ 0, 4, 0, //
+ 0, 0, 6), result);
+ mat3A.multiplyDiagonalPre(new Vector3(-2, -4, -6), result);
+ assertEquals(new Matrix3( //
+ -2, 0, 0, //
+ 0, -4, 0, //
+ 0, 0, -6), result);
+
+ final double a = MathUtils.HALF_PI;
+ mat3A.applyRotationY(a);
+ mat3A.multiplyDiagonalPost(new Vector3(2, 4, 6), result);
+ assertEquals(new Matrix3( //
+ 2 * Math.cos(a), 4 * 0, 6 * Math.sin(a), //
+ 2 * 0, 4 * 1, 6 * 0, //
+ 2 * -Math.sin(a), 4 * 0, 6 * Math.cos(a)), result);
+ result = mat3A.multiplyDiagonalPre(new Vector3(-2, -4, -6), null);
+ assertEquals(new Matrix3( //
+ -2 * Math.cos(a), -2 * 0, -2 * Math.sin(a), //
+ -4 * 0, -4 * 1, -4 * 0, //
+ -6 * -Math.sin(a), -6 * 0, -6 * Math.cos(a)), result);
+ }
+
+ @Test
+ public void testMultiply() {
+ final Matrix3 mat3A = new Matrix3( //
+ 0.01, 0.1, 0.2, //
+ 1.0, 1.1, 1.2, //
+ 2.0, 2.1, 2.2);
+ mat3A.multiplyLocal(2);
+ assertEquals(new Matrix3( //
+ 0.02, 0.2, 0.4, //
+ 2.0, 2.2, 2.4, //
+ 4.0, 4.2, 4.4), mat3A);
+
+ final Matrix3 mat3B = new Matrix3( //
+ 0.5, 1, 2, //
+ 4, 5, 6, //
+ 8, 9, 10);
+ final Matrix3 result = mat3A.multiply(mat3B, null);
+ assertTrue(0.02 * 0.5 + 0.2 * 4 + 0.4 * 8 == result.getM00());
+ assertTrue(0.02 * 1 + 0.2 * 5 + 0.4 * 9 == result.getM01());
+ assertTrue(0.02 * 2 + 0.2 * 6 + 0.4 * 10 == result.getM02());
+ assertTrue(2.0 * 0.5 + 2.2 * 4 + 2.4 * 8 == result.getM10());
+ assertTrue(2.0 * 1 + 2.2 * 5 + 2.4 * 9 == result.getM11());
+ assertTrue(2.0 * 2 + 2.2 * 6 + 2.4 * 10 == result.getM12());
+ assertTrue(4.0 * 0.5 + 4.2 * 4 + 4.4 * 8 == result.getM20());
+ assertTrue(4.0 * 1 + 4.2 * 5 + 4.4 * 9 == result.getM21());
+ assertTrue(4.0 * 2 + 4.2 * 6 + 4.4 * 10 == result.getM22());
+ mat3A.multiplyLocal(mat3B);
+ assertTrue(0.02 * 0.5 + 0.2 * 4 + 0.4 * 8 == mat3A.getM00());
+ assertTrue(0.02 * 1 + 0.2 * 5 + 0.4 * 9 == mat3A.getM01());
+ assertTrue(0.02 * 2 + 0.2 * 6 + 0.4 * 10 == mat3A.getM02());
+ assertTrue(2.0 * 0.5 + 2.2 * 4 + 2.4 * 8 == mat3A.getM10());
+ assertTrue(2.0 * 1 + 2.2 * 5 + 2.4 * 9 == mat3A.getM11());
+ assertTrue(2.0 * 2 + 2.2 * 6 + 2.4 * 10 == mat3A.getM12());
+ assertTrue(4.0 * 0.5 + 4.2 * 4 + 4.4 * 8 == mat3A.getM20());
+ assertTrue(4.0 * 1 + 4.2 * 5 + 4.4 * 9 == mat3A.getM21());
+ assertTrue(4.0 * 2 + 4.2 * 6 + 4.4 * 10 == mat3A.getM22());
+ }
+
+ @Test
+ public void testAddSubtract() {
+ final Matrix3 mat3A = new Matrix3( //
+ 0.0, 0.1, 0.2, //
+ 1.0, 1.1, 1.2, //
+ 2.0, 2.1, 2.2);
+
+ final Matrix3 result1 = mat3A.add(new Matrix3(//
+ 1, 2, 3,//
+ 5, 6, 7, //
+ 9, 10, 11), null);
+ assertEquals(new Matrix3( //
+ 1.0, 2.1, 3.2, //
+ 6.0, 7.1, 8.2, //
+ 11.0, 12.1, 13.2), result1);
+
+ final Matrix3 result2 = result1.subtract(new Matrix3(//
+ 1, 2, 3, //
+ 5, 6, 7, //
+ 9, 10, 11), null);
+ assertEquals(mat3A, result2);
+ result2.addLocal(Matrix3.IDENTITY);
+ assertEquals(new Matrix3( //
+ 1.0, 0.1, 0.2, //
+ 1.0, 2.1, 1.2, //
+ 2.0, 2.1, 3.2), result2);
+
+ result1.subtractLocal(Matrix3.IDENTITY);
+ assertEquals(new Matrix3( //
+ 0.0, 2.1, 3.2, //
+ 6.0, 6.1, 8.2, //
+ 11.0, 12.1, 12.2), result1);
+ }
+
+ @Test
+ public void testScale() {
+ final Matrix3 mat3A = new Matrix3( //
+ 0.01, 0.1, 0.2, //
+ 1.0, 1.1, 1.2, //
+ 2.0, 2.1, 2.2);
+ final Matrix3 result = mat3A.scale(new Vector3(-1, 2, 3), null);
+ assertEquals(new Matrix3( //
+ 0.01 * -1, 0.1 * 2, 0.2 * 3, //
+ 1.0 * -1, 1.1 * 2, 1.2 * 3, //
+ 2.0 * -1, 2.1 * 2, 2.2 * 3), result);
+
+ result.scaleLocal(new Vector3(-1, 0.5, 1 / 3.));
+ assertEquals(mat3A, result);
+ }
+
+ @Test
+ public void testTranspose() {
+ final Matrix3 mat3A = new Matrix3( //
+ 0.01, 0.1, 0.2, //
+ 1.0, 1.1, 1.2, //
+ 2.0, 2.1, 2.2);
+ final Matrix3 result = mat3A.transpose(null);
+ assertEquals(new Matrix3( //
+ 0.01, 1.0, 2.0, //
+ 0.1, 1.1, 2.1, //
+ 0.2, 1.2, 2.2), result);
+ assertEquals(new Matrix3( //
+ 0.01, 0.1, 0.2, //
+ 1.0, 1.1, 1.2, //
+ 2.0, 2.1, 2.2), result.transposeLocal());
+ // coverage
+ final Matrix3 result2 = result.transposeLocal().transpose(new Matrix3());
+ assertEquals(mat3A, result2);
+ }
+
+ @Test
+ public void testInvert() {
+ final Matrix3 mat3A = new Matrix3().applyRotationX(MathUtils.QUARTER_PI);
+ final Matrix3 inverted = mat3A.invert(null);
+ assertEquals(Matrix3.IDENTITY, mat3A.multiply(inverted, null));
+ assertEquals(mat3A, inverted.invertLocal());
+ }
+
+ @Test(expected = ArithmeticException.class)
+ public void testBadInvert() {
+ final Matrix3 mat3A = new Matrix3(0, 0, 0, 0, 0, 0, 0, 0, 0);
+ mat3A.invertLocal();
+ }
+
+ @Test
+ public void testAdjugate() {
+ final double //
+ a = -3, b = 2, c = -5, //
+ d = -1, e = 0, f = -2, //
+ g = 3, h = -4, i = 1;
+
+ final Matrix3 mat3A = new Matrix3( //
+ a, b, c, //
+ d, e, f, //
+ g, h, i);
+
+ final Matrix3 testValue = new Matrix3( //
+ e * i - h * f, -(b * i - h * c), b * f - e * c, //
+ -(d * i - g * f), a * i - g * c, -(a * f - d * c),//
+ d * h - g * e, -(a * h - g * b), a * e - d * b);
+
+ assertEquals(testValue, mat3A.adjugate(null));
+ assertEquals(testValue, mat3A.adjugateLocal());
+ }
+
+ @Test
+ public void testDeterminant() {
+ {
+ final double //
+ a = -3, b = 2, c = -5, //
+ d = -1, e = 0, f = -2, //
+ g = 3, h = -4, i = 1;
+
+ final Matrix3 mat3A = new Matrix3( //
+ a, b, c, //
+ d, e, f, //
+ g, h, i);
+ final double determinant = a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h;
+ assertTrue(determinant == mat3A.determinant());
+ }
+
+ {
+ final double //
+ a = -1, b = 2, c = -3, //
+ d = 4, e = -5, f = 6, //
+ g = -7, h = 8, i = -9;
+
+ final Matrix3 mat3A = new Matrix3( //
+ a, b, c, //
+ d, e, f, //
+ g, h, i);
+ final double determinant = a * e * i + b * f * g + c * d * h - c * e * g - b * d * i - a * f * h;
+ assertTrue(determinant == mat3A.determinant());
+ }
+ }
+
+ @Test
+ public void testClone() {
+ final Matrix3 mat1 = new Matrix3();
+ final Matrix3 mat2 = mat1.clone();
+ assertEquals(mat1, mat2);
+ assertNotSame(mat1, mat2);
+ }
+
+ @Test
+ public void testValid() {
+ final Matrix3 mat3 = new Matrix3();
+ assertTrue(Matrix3.isValid(mat3));
+ for (int i = 0; i < 9; i++) {
+ mat3.setIdentity();
+ mat3.setValue(i / 3, i % 3, Double.NaN);
+ assertFalse(Matrix3.isValid(mat3));
+ mat3.setIdentity();
+ mat3.setValue(i / 3, i % 3, Double.POSITIVE_INFINITY);
+ assertFalse(Matrix3.isValid(mat3));
+ }
+
+ mat3.setIdentity();
+ assertTrue(Matrix3.isValid(mat3));
+
+ assertFalse(Matrix3.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(mat3, mat3);
+ assertTrue(mat3.strictEquals(mat3));
+ assertFalse(mat3.equals(null));
+ assertFalse(mat3.strictEquals(null));
+ assertFalse(mat3.equals(new Vector2()));
+ assertFalse(mat3.strictEquals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Matrix3 matTemp = Matrix3.fetchTempInstance();
+ matTemp.set(mat3);
+ assertEquals(mat3, matTemp);
+ assertNotSame(mat3, matTemp);
+ Matrix3.releaseTempInstance(matTemp);
+
+ // cover more of equals
+ mat3.set(0, 1, 2, 3, 4, 5, 6, 7, 8);
+ final Matrix3 comp = new Matrix3(-1, -1, -1, -1, -1, -1, -1, -1, -1);
+ assertFalse(mat3.equals(comp));
+ assertFalse(mat3.strictEquals(comp));
+ for (int i = 0; i < 8; i++) {
+ comp.setValue(i / 3, i % 3, i);
+ assertFalse(mat3.equals(comp));
+ assertFalse(mat3.strictEquals(comp));
+ }
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Matrix3 mat1 = new Matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ final Matrix3 mat2 = new Matrix3(1, 2, 3, 4, 5, 6, 7, 8, 9);
+ final Matrix3 mat3 = new Matrix3(1, 2, 3, 4, 5, 6, 7, 8, 0);
+
+ assertTrue(mat1.hashCode() == mat2.hashCode());
+ assertTrue(mat1.hashCode() != mat3.hashCode());
+ }
+
+ @Test
+ public void testOrthonormal() {
+ final Matrix3 mat3 = new Matrix3();
+ assertTrue(mat3.isOrthonormal());
+ // just rotation
+ mat3.applyRotationX(MathUtils.QUARTER_PI);
+ assertTrue(mat3.isOrthonormal());
+ // non-uniform scale
+ mat3.setIdentity();
+ mat3.scaleLocal(new Vector3(1, 2, 3));
+ assertFalse(mat3.isOrthonormal());
+ // non-uniform scale + rotation
+ mat3.setIdentity();
+ mat3.scaleLocal(new Vector3(1, 2, 3));
+ mat3.applyRotationX(MathUtils.QUARTER_PI);
+ assertFalse(mat3.isOrthonormal());
+ }
+
+ @Test
+ public void testApplyVector3() {
+ final Matrix3 mat3 = new Matrix3().applyRotationX(MathUtils.HALF_PI);
+ final Vector3 vec3 = new Vector3(0, 1, 0);
+ final Vector3 result = mat3.applyPost(vec3, null);
+ assertTrue(Math.abs(new Vector3(0, 0, 1).distance(result)) <= MathUtils.EPSILON);
+ vec3.set(0, 1, 1);
+ mat3.applyPost(vec3, result);
+ assertTrue(Math.abs(new Vector3(0, -1, 1).distance(result)) <= MathUtils.EPSILON);
+
+ vec3.set(0, 1, 1);
+ mat3.applyPre(vec3, result);
+ assertTrue(Math.abs(new Vector3(0, 1, -1).distance(result)) <= MathUtils.EPSILON);
+
+ vec3.set(1, 1, 1);
+ assertTrue(Math.abs(new Vector3(1, 1, -1).distance(mat3.applyPre(vec3, null))) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testStartEnd() {
+ final Matrix3 mat3 = new Matrix3();
+ mat3.fromStartEndLocal(Vector3.UNIT_X, Vector3.UNIT_Y); // should be a 90 degree turn around Z
+ assertEquals(new Vector3(-1, 1, 1), mat3.applyPost(new Vector3(1, 1, 1), null));
+
+ // coverage
+ mat3.fromStartEndLocal(new Vector3(1, 0, 0), new Vector3(1 + Double.MIN_VALUE, 0, 0));
+ assertTrue(mat3.applyPost(Vector3.ONE, null).distance(Vector3.ONE) < MathUtils.ZERO_TOLERANCE);
+ mat3.fromStartEndLocal(new Vector3(0, 1, 0), new Vector3(0, 1 + Double.MIN_VALUE, 0));
+ assertTrue(mat3.applyPost(Vector3.ONE, null).distance(Vector3.ONE) < MathUtils.ZERO_TOLERANCE);
+ mat3.fromStartEndLocal(new Vector3(0, 0, 1), new Vector3(0, 0, 1 + Double.MIN_VALUE));
+ assertTrue(mat3.applyPost(Vector3.ONE, null).distance(Vector3.ONE) < MathUtils.ZERO_TOLERANCE);
+ }
+
+ @Test
+ public void testLookAt() {
+ final Vector3 direction = new Vector3(-1, 0, 0);
+ final Matrix3 mat3 = new Matrix3().lookAt(direction, Vector3.UNIT_Y);
+ assertEquals(direction, mat3.applyPost(Vector3.UNIT_Z, null));
+
+ direction.set(1, 1, 1).normalizeLocal();
+ mat3.lookAt(direction, Vector3.UNIT_Y);
+ assertEquals(direction, mat3.applyPost(Vector3.UNIT_Z, null));
+
+ direction.set(-1, 2, -1).normalizeLocal();
+ mat3.lookAt(direction, Vector3.UNIT_Y);
+ assertEquals(direction, mat3.applyPost(Vector3.UNIT_Z, new Vector3()));
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix4.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix4.java new file mode 100644 index 0000000..9774434 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestMatrix4.java @@ -0,0 +1,935 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+
+import org.junit.Test;
+
+public class TestMatrix4 {
+
+ @Test
+ public void testGetSet() {
+ final Matrix4 mat4A = new Matrix4();
+ assertEquals(Matrix4.IDENTITY, mat4A);
+
+ mat4A.setM00(0.0);
+ mat4A.setM01(0.1);
+ mat4A.setM02(0.2);
+ mat4A.setM03(0.3);
+ mat4A.setM10(1.0);
+ mat4A.setM11(1.1);
+ mat4A.setM12(1.2);
+ mat4A.setM13(1.3);
+ mat4A.setM20(2.0);
+ mat4A.setM21(2.1);
+ mat4A.setM22(2.2);
+ mat4A.setM23(2.3);
+ mat4A.setM30(3.0);
+ mat4A.setM31(3.1);
+ mat4A.setM32(3.2);
+ mat4A.setM33(3.3);
+
+ assertTrue(0.0 == mat4A.getM00());
+ assertTrue(0.1 == mat4A.getM01());
+ assertTrue(0.2 == mat4A.getM02());
+ assertTrue(0.3 == mat4A.getM03());
+ assertTrue(1.0 == mat4A.getM10());
+ assertTrue(1.1 == mat4A.getM11());
+ assertTrue(1.2 == mat4A.getM12());
+ assertTrue(1.3 == mat4A.getM13());
+ assertTrue(2.0 == mat4A.getM20());
+ assertTrue(2.1 == mat4A.getM21());
+ assertTrue(2.2 == mat4A.getM22());
+ assertTrue(2.3 == mat4A.getM23());
+ assertTrue(3.0 == mat4A.getM30());
+ assertTrue(3.1 == mat4A.getM31());
+ assertTrue(3.2 == mat4A.getM32());
+ assertTrue(3.3 == mat4A.getM33());
+
+ final Matrix4 mat4B = new Matrix4(mat4A);
+ assertTrue(0.0 == mat4B.getM00());
+ assertTrue(0.1 == mat4B.getM01());
+ assertTrue(0.2 == mat4B.getM02());
+ assertTrue(0.3 == mat4B.getM03());
+ assertTrue(1.0 == mat4B.getM10());
+ assertTrue(1.1 == mat4B.getM11());
+ assertTrue(1.2 == mat4B.getM12());
+ assertTrue(1.3 == mat4B.getM13());
+ assertTrue(2.0 == mat4B.getM20());
+ assertTrue(2.1 == mat4B.getM21());
+ assertTrue(2.2 == mat4B.getM22());
+ assertTrue(2.3 == mat4B.getM23());
+ assertTrue(3.0 == mat4B.getM30());
+ assertTrue(3.1 == mat4B.getM31());
+ assertTrue(3.2 == mat4B.getM32());
+ assertTrue(3.3 == mat4B.getM33());
+
+ final Matrix4 mat4C = new Matrix4(0.0, 0.1, 0.2, 0.3, 1.0, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2, 2.3, 3.0, 3.1, 3.2,
+ 3.3);
+ assertTrue(0.0 == mat4C.getM00());
+ assertTrue(0.1 == mat4C.getM01());
+ assertTrue(0.2 == mat4C.getM02());
+ assertTrue(0.3 == mat4C.getM03());
+ assertTrue(1.0 == mat4C.getM10());
+ assertTrue(1.1 == mat4C.getM11());
+ assertTrue(1.2 == mat4C.getM12());
+ assertTrue(1.3 == mat4C.getM13());
+ assertTrue(2.0 == mat4C.getM20());
+ assertTrue(2.1 == mat4C.getM21());
+ assertTrue(2.2 == mat4C.getM22());
+ assertTrue(2.3 == mat4C.getM23());
+ assertTrue(3.0 == mat4C.getM30());
+ assertTrue(3.1 == mat4C.getM31());
+ assertTrue(3.2 == mat4C.getM32());
+ assertTrue(3.3 == mat4C.getM33());
+
+ mat4C.setIdentity();
+ assertTrue(mat4C.isIdentity());
+
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ final double value = (10 * x + y) / 10.;
+ mat4C.setValue(x, y, value);
+ assertTrue(value == mat4C.getValue(x, y));
+ }
+ }
+
+ mat4C.setIdentity();
+ mat4C.set(0.0, 0.1, 0.2, 0.3, 2.0, 2.1, 2.2, 2.3, 4.0, 4.1, 4.2, 4.3, 6.0, 6.1, 6.2, 6.3);
+ for (int x = 0; x < 4; x++) {
+ for (int y = 0; y < 4; y++) {
+ assertTrue((20 * x + y) / 10.f == mat4C.getValuef(x, y));
+ }
+ }
+
+ final Matrix4 store = new Matrix4(mat4C);
+ // catch a few expected exceptions
+ try {
+ mat4C.getValue(-1, 0);
+ fail("getValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.getValue(0, 4);
+ fail("getValue(0, 4) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.getValue(1, -1);
+ fail("getValue(1, -1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.getValue(2, 4);
+ fail("getValue(2, 4) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.getValue(3, -1);
+ fail("getValue(3, -1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.getValue(4, 0);
+ fail("getValue(4, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+
+ try {
+ mat4C.setValue(-1, 0, 0);
+ fail("setValue(-1, 0, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.setValue(0, -1, 0);
+ fail("setValue(0, -1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.setValue(1, 4, 0);
+ fail("setValue(1, 4, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.setValue(2, -1, 0);
+ fail("setValue(2, -1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.setValue(3, 4, 0);
+ fail("setValue(3, 4, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4C.setValue(4, 0, 0);
+ fail("setValue(4, 0, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered mat4C
+ assertEquals(store, mat4C);
+ }
+
+ @Test
+ public void testColumns() {
+ final Matrix4 mat4A = new Matrix4();
+ mat4A.setColumn(0, new Vector4(0, 4, 8, 12));
+ mat4A.setColumn(1, new Vector4(1, 5, 9, 13));
+ mat4A.setColumn(2, new Vector4(2, 6, 10, 14));
+ mat4A.setColumn(3, new Vector4(3, 7, 11, 15));
+ assertEquals(new Vector4(0, 4, 8, 12), mat4A.getColumn(0, new Vector4()));
+ assertEquals(new Vector4(1, 5, 9, 13), mat4A.getColumn(1, null));
+ assertEquals(new Vector4(2, 6, 10, 14), mat4A.getColumn(2, null));
+ assertEquals(new Vector4(3, 7, 11, 15), mat4A.getColumn(3, null));
+ try {
+ mat4A.getColumn(-1, null);
+ fail("getColumn(-1, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.getColumn(4, null);
+ fail("getColumn(4, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.setColumn(-1, new Vector4());
+ fail("setColumn(-1, double[]) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.setColumn(4, new Vector4());
+ fail("setColumn(4, double[]) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ }
+
+ @Test
+ public void testRows() {
+ final Matrix4 mat4A = new Matrix4();
+ mat4A.setRow(0, new Vector4(0, 1, 2, 3));
+ mat4A.setRow(1, new Vector4(4, 5, 6, 7));
+ mat4A.setRow(2, new Vector4(8, 9, 10, 11));
+ mat4A.setRow(3, new Vector4(12, 13, 14, 15));
+ assertEquals(new Vector4(0, 1, 2, 3), mat4A.getRow(0, new Vector4()));
+ assertEquals(new Vector4(4, 5, 6, 7), mat4A.getRow(1, null));
+ assertEquals(new Vector4(8, 9, 10, 11), mat4A.getRow(2, null));
+ assertEquals(new Vector4(12, 13, 14, 15), mat4A.getRow(3, null));
+ try {
+ mat4A.getRow(-1, null);
+ fail("getRow(-1, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.getRow(4, null);
+ fail("getRow(4, null) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.setRow(-1, new Vector4());
+ fail("setRow(-1, double[]) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ mat4A.setRow(4, new Vector4());
+ fail("setRow(4, double[]) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ }
+
+ @Test
+ public void testSetRotation() {
+ final Matrix4 mat4A = new Matrix4(0.0, 0.1, 0.2, 0.3, 1.0, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2, 2.3, 3.0, 3.1, 3.2,
+ 3.3);
+ mat4A.set(Matrix3.IDENTITY);
+ assertTrue(1.0 == mat4A.getM00());
+ assertTrue(0.0 == mat4A.getM01());
+ assertTrue(0.0 == mat4A.getM02());
+ assertTrue(0.3 == mat4A.getM03());
+ assertTrue(0.0 == mat4A.getM10());
+ assertTrue(1.0 == mat4A.getM11());
+ assertTrue(0.0 == mat4A.getM12());
+ assertTrue(1.3 == mat4A.getM13());
+ assertTrue(0.0 == mat4A.getM20());
+ assertTrue(0.0 == mat4A.getM21());
+ assertTrue(1.0 == mat4A.getM22());
+ assertTrue(2.3 == mat4A.getM23());
+ assertTrue(3.0 == mat4A.getM30());
+ assertTrue(3.1 == mat4A.getM31());
+ assertTrue(3.2 == mat4A.getM32());
+ assertTrue(3.3 == mat4A.getM33());
+
+ mat4A.setIdentity();
+ // rotate identity 90 degrees around Y
+ final double a = MathUtils.HALF_PI;
+ final Quaternion quaternion = new Quaternion();
+ quaternion.fromAngleAxis(a, Vector3.UNIT_Y);
+ mat4A.set(quaternion);
+
+ assertEquals(new Matrix4( //
+ Math.cos(a), 0, Math.sin(a), 0, //
+ 0, 1, 0, 0, //
+ -Math.sin(a), 0, Math.cos(a), 0, //
+ 0, 0, 0, 1), mat4A);
+ }
+
+ @Test
+ public void testFromBuffer() {
+ final FloatBuffer fb = FloatBuffer.allocate(16);
+ fb.put(new float[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
+ fb.flip();
+ // row major
+ final Matrix4 mat4A = new Matrix4().fromFloatBuffer(fb);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(1 == mat4A.getM01());
+ assertTrue(2 == mat4A.getM02());
+ assertTrue(3 == mat4A.getM03());
+ assertTrue(4 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(6 == mat4A.getM12());
+ assertTrue(7 == mat4A.getM13());
+ assertTrue(8 == mat4A.getM20());
+ assertTrue(9 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(11 == mat4A.getM23());
+ assertTrue(12 == mat4A.getM30());
+ assertTrue(13 == mat4A.getM31());
+ assertTrue(14 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+
+ // column major
+ fb.rewind();
+ mat4A.setIdentity();
+ mat4A.fromFloatBuffer(fb, false);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(4 == mat4A.getM01());
+ assertTrue(8 == mat4A.getM02());
+ assertTrue(12 == mat4A.getM03());
+ assertTrue(1 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(9 == mat4A.getM12());
+ assertTrue(13 == mat4A.getM13());
+ assertTrue(2 == mat4A.getM20());
+ assertTrue(6 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(14 == mat4A.getM23());
+ assertTrue(3 == mat4A.getM30());
+ assertTrue(7 == mat4A.getM31());
+ assertTrue(11 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+
+ final DoubleBuffer db = DoubleBuffer.allocate(16);
+ db.put(new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 });
+ db.flip();
+ // row major
+ mat4A.setIdentity();
+ mat4A.fromDoubleBuffer(db);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(1 == mat4A.getM01());
+ assertTrue(2 == mat4A.getM02());
+ assertTrue(3 == mat4A.getM03());
+ assertTrue(4 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(6 == mat4A.getM12());
+ assertTrue(7 == mat4A.getM13());
+ assertTrue(8 == mat4A.getM20());
+ assertTrue(9 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(11 == mat4A.getM23());
+ assertTrue(12 == mat4A.getM30());
+ assertTrue(13 == mat4A.getM31());
+ assertTrue(14 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+
+ // column major
+ db.rewind();
+ mat4A.setIdentity();
+ mat4A.fromDoubleBuffer(db, false);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(4 == mat4A.getM01());
+ assertTrue(8 == mat4A.getM02());
+ assertTrue(12 == mat4A.getM03());
+ assertTrue(1 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(9 == mat4A.getM12());
+ assertTrue(13 == mat4A.getM13());
+ assertTrue(2 == mat4A.getM20());
+ assertTrue(6 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(14 == mat4A.getM23());
+ assertTrue(3 == mat4A.getM30());
+ assertTrue(7 == mat4A.getM31());
+ assertTrue(11 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+ }
+
+ @Test
+ public void testToBuffer() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
+ final double[] colmajor = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
+
+ final Matrix4 mat4A = new Matrix4().fromArray(values);
+
+ // row major
+ final FloatBuffer fb = mat4A.toFloatBuffer(FloatBuffer.allocate(16));
+ fb.flip();
+ for (int i = 0; i < 16; i++) {
+ assertTrue(values[i] == fb.get());
+ }
+
+ // column major
+ fb.rewind();
+ mat4A.toFloatBuffer(fb, false);
+ fb.flip();
+ for (int i = 0; i < 16; i++) {
+ assertTrue(colmajor[i] == fb.get());
+ }
+
+ // row major
+ final DoubleBuffer db = mat4A.toDoubleBuffer(DoubleBuffer.allocate(16));
+ db.flip();
+ for (int i = 0; i < 16; i++) {
+ assertTrue(values[i] == db.get());
+ }
+
+ // column major
+ db.rewind();
+ mat4A.toDoubleBuffer(db, false);
+ db.flip();
+ for (int i = 0; i < 16; i++) {
+ assertTrue(colmajor[i] == db.get());
+ }
+ }
+
+ @Test
+ public void testFromArray() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
+ final Matrix4 mat4A = new Matrix4();
+
+ // row major
+ mat4A.setIdentity();
+ mat4A.fromArray(values);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(1 == mat4A.getM01());
+ assertTrue(2 == mat4A.getM02());
+ assertTrue(3 == mat4A.getM03());
+ assertTrue(4 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(6 == mat4A.getM12());
+ assertTrue(7 == mat4A.getM13());
+ assertTrue(8 == mat4A.getM20());
+ assertTrue(9 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(11 == mat4A.getM23());
+ assertTrue(12 == mat4A.getM30());
+ assertTrue(13 == mat4A.getM31());
+ assertTrue(14 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+
+ // column major
+ mat4A.setIdentity();
+ mat4A.fromArray(values, false);
+ assertTrue(0 == mat4A.getM00());
+ assertTrue(4 == mat4A.getM01());
+ assertTrue(8 == mat4A.getM02());
+ assertTrue(12 == mat4A.getM03());
+ assertTrue(1 == mat4A.getM10());
+ assertTrue(5 == mat4A.getM11());
+ assertTrue(9 == mat4A.getM12());
+ assertTrue(13 == mat4A.getM13());
+ assertTrue(2 == mat4A.getM20());
+ assertTrue(6 == mat4A.getM21());
+ assertTrue(10 == mat4A.getM22());
+ assertTrue(14 == mat4A.getM23());
+ assertTrue(3 == mat4A.getM30());
+ assertTrue(7 == mat4A.getM31());
+ assertTrue(11 == mat4A.getM32());
+ assertTrue(15 == mat4A.getM33());
+ }
+
+ @Test
+ public void testToArray() {
+ final double[] values = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
+ final Matrix4 mat4A = new Matrix4().fromArray(values);
+
+ // row major
+ final double[] dbls1 = mat4A.toArray(null);
+ for (int i = 0; i < 16; i++) {
+ assertTrue(values[i] == dbls1[i]);
+ }
+
+ // column major
+ final double[] colmajor = { 0, 4, 8, 12, 1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15 };
+ mat4A.toArray(dbls1, false);
+ for (int i = 0; i < 16; i++) {
+ assertTrue(colmajor[i] == dbls1[i]);
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBadArray() {
+ final Matrix4 mat4A = new Matrix4();
+ mat4A.toArray(new double[9]);
+ }
+
+ @Test
+ public void testAngleAxis() {
+ final Matrix4 mat4A = new Matrix4();
+ // rotate identity 90 degrees around X
+ final double angle = MathUtils.HALF_PI;
+ mat4A.fromAngleAxis(MathUtils.HALF_PI, Vector3.UNIT_X);
+ assertEquals(new Matrix4( //
+ 1, 0, 0, 0, //
+ 0, Math.cos(angle), -Math.sin(angle), 0, //
+ 0, Math.sin(angle), Math.cos(angle), 0, //
+ 0, 0, 0, 1), mat4A);
+ }
+
+ @Test
+ public void testRotations() {
+ final Vector4 rotated = new Vector4();
+ final Vector4 expected = new Vector4();
+ final Matrix4 worker = new Matrix4();
+
+ // test axis rotation methods against general purpose
+ // X AXIS
+ expected.set(1, 1, 1, 1);
+ rotated.set(1, 1, 1, 1);
+ worker.setIdentity().applyRotationX(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 1, 0, 0).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Y AXIS
+ expected.set(1, 1, 1, 1);
+ rotated.set(1, 1, 1, 1);
+ worker.setIdentity().applyRotationY(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 1, 0).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Z AXIS
+ expected.set(1, 1, 1, 1);
+ rotated.set(1, 1, 1, 1);
+ worker.setIdentity().applyRotationZ(MathUtils.QUARTER_PI).applyPost(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 0, 1).applyPost(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testTranslation() {
+ final Matrix4 src = new Matrix4();
+ src.applyRotation(MathUtils.QUARTER_PI, 1, 0, 0);
+
+ final Matrix4 trans = new Matrix4();
+ trans.setColumn(3, new Vector4(1, 2, 3, 1));
+ final Matrix4 transThenRotate = trans.multiply(src, null);
+ final Matrix4 rotateThenTrans = src.multiply(trans, null);
+
+ final Matrix4 pre1 = new Matrix4(src).applyTranslationPre(1, 2, 3);
+ final Matrix4 post1 = new Matrix4(src).applyTranslationPost(1, 2, 3);
+
+ assertEquals(transThenRotate, pre1);
+ assertEquals(rotateThenTrans, post1);
+ }
+
+ @Test
+ public void testMultiplyDiagonal() {
+ final Matrix4 mat4A = new Matrix4();
+ Matrix4 result = mat4A.multiplyDiagonalPost(new Vector4(2, 4, 6, 8), null);
+ assertEquals(new Matrix4( //
+ 2, 0, 0, 0, //
+ 0, 4, 0, 0, //
+ 0, 0, 6, 0, //
+ 0, 0, 0, 8), result);
+ mat4A.multiplyDiagonalPre(new Vector4(-2, -4, -6, -8), result);
+ assertEquals(new Matrix4( //
+ -2, 0, 0, 0, //
+ 0, -4, 0, 0, //
+ 0, 0, -6, 0, //
+ 0, 0, 0, -8), result);
+
+ final double a = MathUtils.HALF_PI;
+ mat4A.applyRotationY(a);
+ mat4A.multiplyDiagonalPost(new Vector4(2, 4, 6, 8), result);
+ assertEquals(new Matrix4( //
+ 2 * Math.cos(a), 4 * 0, 6 * Math.sin(a), 8 * 0, //
+ 2 * 0, 4 * 1, 6 * 0, 8 * 0, //
+ 2 * -Math.sin(a), 4 * 0, 6 * Math.cos(a), 8 * 0, //
+ 2 * 0, 4 * 0, 6 * 0, 8 * 1), result);
+ result = mat4A.multiplyDiagonalPre(new Vector4(-2, -4, -6, -8), null);
+ assertEquals(new Matrix4( //
+ -2 * Math.cos(a), -2 * 0, -2 * Math.sin(a), -2 * 0, //
+ -4 * 0, -4 * 1, -4 * 0, -4 * 0, //
+ -6 * -Math.sin(a), -6 * 0, -6 * Math.cos(a), -6 * 0, //
+ -8 * 0, -8 * 0, -8 * 0, -8 * 1), result);
+ }
+
+ @Test
+ public void testMultiply() {
+ final Matrix4 mat4A = new Matrix4( //
+ 0.01, 0.1, 0.2, 0.3, //
+ 1.0, 1.1, 1.2, 1.3, //
+ 2.0, 2.1, 2.2, 2.3, //
+ 3.0, 3.1, 3.2, 3.3);
+ mat4A.multiplyLocal(2);
+ assertEquals(new Matrix4( //
+ 0.02, 0.2, 0.4, 0.6, //
+ 2.0, 2.2, 2.4, 2.6, //
+ 4.0, 4.2, 4.4, 4.6, //
+ 6.0, 6.2, 6.4, 6.6), mat4A);
+
+ final Matrix4 mat4B = new Matrix4( //
+ 0.5, 1, 2, 3, //
+ 4, 5, 6, 7, //
+ 8, 9, 10, 11, //
+ 12, 13, 14, 15);
+ final Matrix4 result = mat4A.multiply(mat4B, null);
+ assertTrue(0.02 * 0.5 + 0.2 * 4 + 0.4 * 8 + 0.6 * 12 == result.getM00());
+ assertTrue(0.02 * 1 + 0.2 * 5 + 0.4 * 9 + 0.6 * 13 == result.getM01());
+ assertTrue(0.02 * 2 + 0.2 * 6 + 0.4 * 10 + 0.6 * 14 == result.getM02());
+ assertTrue(0.02 * 3 + 0.2 * 7 + 0.4 * 11 + 0.6 * 15 == result.getM03());
+ assertTrue(2.0 * 0.5 + 2.2 * 4 + 2.4 * 8 + 2.6 * 12 == result.getM10());
+ assertTrue(2.0 * 1 + 2.2 * 5 + 2.4 * 9 + 2.6 * 13 == result.getM11());
+ assertTrue(2.0 * 2 + 2.2 * 6 + 2.4 * 10 + 2.6 * 14 == result.getM12());
+ assertTrue(2.0 * 3 + 2.2 * 7 + 2.4 * 11 + 2.6 * 15 == result.getM13());
+ assertTrue(4.0 * 0.5 + 4.2 * 4 + 4.4 * 8 + 4.6 * 12 == result.getM20());
+ assertTrue(4.0 * 1 + 4.2 * 5 + 4.4 * 9 + 4.6 * 13 == result.getM21());
+ assertTrue(4.0 * 2 + 4.2 * 6 + 4.4 * 10 + 4.6 * 14 == result.getM22());
+ assertTrue(4.0 * 3 + 4.2 * 7 + 4.4 * 11 + 4.6 * 15 == result.getM23());
+ assertTrue(6.0 * 0.5 + 6.2 * 4 + 6.4 * 8 + 6.6 * 12 == result.getM30());
+ assertTrue(6.0 * 1 + 6.2 * 5 + 6.4 * 9 + 6.6 * 13 == result.getM31());
+ assertTrue(6.0 * 2 + 6.2 * 6 + 6.4 * 10 + 6.6 * 14 == result.getM32());
+ assertTrue(6.0 * 3 + 6.2 * 7 + 6.4 * 11 + 6.6 * 15 == result.getM33());
+ mat4A.multiplyLocal(mat4B);
+ assertTrue(0.02 * 0.5 + 0.2 * 4 + 0.4 * 8 + 0.6 * 12 == mat4A.getM00());
+ assertTrue(0.02 * 1 + 0.2 * 5 + 0.4 * 9 + 0.6 * 13 == mat4A.getM01());
+ assertTrue(0.02 * 2 + 0.2 * 6 + 0.4 * 10 + 0.6 * 14 == mat4A.getM02());
+ assertTrue(0.02 * 3 + 0.2 * 7 + 0.4 * 11 + 0.6 * 15 == mat4A.getM03());
+ assertTrue(2.0 * 0.5 + 2.2 * 4 + 2.4 * 8 + 2.6 * 12 == mat4A.getM10());
+ assertTrue(2.0 * 1 + 2.2 * 5 + 2.4 * 9 + 2.6 * 13 == mat4A.getM11());
+ assertTrue(2.0 * 2 + 2.2 * 6 + 2.4 * 10 + 2.6 * 14 == mat4A.getM12());
+ assertTrue(2.0 * 3 + 2.2 * 7 + 2.4 * 11 + 2.6 * 15 == mat4A.getM13());
+ assertTrue(4.0 * 0.5 + 4.2 * 4 + 4.4 * 8 + 4.6 * 12 == mat4A.getM20());
+ assertTrue(4.0 * 1 + 4.2 * 5 + 4.4 * 9 + 4.6 * 13 == mat4A.getM21());
+ assertTrue(4.0 * 2 + 4.2 * 6 + 4.4 * 10 + 4.6 * 14 == mat4A.getM22());
+ assertTrue(4.0 * 3 + 4.2 * 7 + 4.4 * 11 + 4.6 * 15 == mat4A.getM23());
+ assertTrue(6.0 * 0.5 + 6.2 * 4 + 6.4 * 8 + 6.6 * 12 == mat4A.getM30());
+ assertTrue(6.0 * 1 + 6.2 * 5 + 6.4 * 9 + 6.6 * 13 == mat4A.getM31());
+ assertTrue(6.0 * 2 + 6.2 * 6 + 6.4 * 10 + 6.6 * 14 == mat4A.getM32());
+ assertTrue(6.0 * 3 + 6.2 * 7 + 6.4 * 11 + 6.6 * 15 == mat4A.getM33());
+ }
+
+ @Test
+ public void testAddSubtract() {
+ final Matrix4 mat4A = new Matrix4( //
+ 0.0, 0.1, 0.2, 0.3, //
+ 1.0, 1.1, 1.2, 1.3, //
+ 2.0, 2.1, 2.2, 2.3, //
+ 3.0, 3.1, 3.2, 3.3);
+
+ final Matrix4 result1 = mat4A.add(new Matrix4(//
+ 1, 2, 3, 4,//
+ 5, 6, 7, 8,//
+ 9, 10, 11, 12,//
+ 13, 14, 15, 16), null);
+ assertEquals(new Matrix4( //
+ 1.0, 2.1, 3.2, 4.3, //
+ 6.0, 7.1, 8.2, 9.3, //
+ 11.0, 12.1, 13.2, 14.3, //
+ 16.0, 17.1, 18.2, 19.3), result1);
+
+ final Matrix4 result2 = result1.subtract(new Matrix4(//
+ 1, 2, 3, 4,//
+ 5, 6, 7, 8,//
+ 9, 10, 11, 12,//
+ 13, 14, 15, 16), null);
+ assertEquals(mat4A, result2);
+ result2.addLocal(Matrix4.IDENTITY);
+ assertEquals(new Matrix4( //
+ 1.0, 0.1, 0.2, 0.3, //
+ 1.0, 2.1, 1.2, 1.3, //
+ 2.0, 2.1, 3.2, 2.3, //
+ 3.0, 3.1, 3.2, 4.3), result2);
+
+ result1.subtractLocal(Matrix4.IDENTITY);
+ assertEquals(new Matrix4( //
+ 0.0, 2.1, 3.2, 4.3, //
+ 6.0, 6.1, 8.2, 9.3, //
+ 11.0, 12.1, 12.2, 14.3, //
+ 16.0, 17.1, 18.2, 18.3), result1);
+ }
+
+ @Test
+ public void testScale() {
+ final Matrix4 mat4A = new Matrix4( //
+ 0.01, 0.1, 0.2, 0.3, //
+ 1.0, 1.1, 1.2, 1.3, //
+ 2.0, 2.1, 2.2, 2.3, //
+ 3.0, 3.1, 3.2, 3.3);
+ final Matrix4 result = mat4A.scale(new Vector4(-1, 2, 3, 4), null);
+ assertEquals(new Matrix4( //
+ 0.01 * -1, 0.1 * 2, 0.2 * 3, 0.3 * 4, //
+ 1.0 * -1, 1.1 * 2, 1.2 * 3, 1.3 * 4, //
+ 2.0 * -1, 2.1 * 2, 2.2 * 3, 2.3 * 4, //
+ 3.0 * -1, 3.1 * 2, 3.2 * 3, 3.3 * 4), result);
+
+ result.scaleLocal(new Vector4(-1, 0.5, 1 / 3., .25));
+ assertEquals(mat4A, result);
+ }
+
+ @Test
+ public void testTranspose() {
+ final Matrix4 mat4A = new Matrix4( //
+ 0.01, 0.1, 0.2, 0.3, //
+ 1.0, 1.1, 1.2, 1.3, //
+ 2.0, 2.1, 2.2, 2.3, //
+ 3.0, 3.1, 3.2, 3.3);
+ final Matrix4 result = mat4A.transpose(null);
+ assertEquals(new Matrix4( //
+ 0.01, 1.0, 2.0, 3.0, //
+ 0.1, 1.1, 2.1, 3.1, //
+ 0.2, 1.2, 2.2, 3.2, //
+ 0.3, 1.3, 2.3, 3.3), result);
+ assertEquals(new Matrix4( //
+ 0.01, 0.1, 0.2, 0.3, //
+ 1.0, 1.1, 1.2, 1.3, //
+ 2.0, 2.1, 2.2, 2.3, //
+ 3.0, 3.1, 3.2, 3.3), result.transposeLocal());
+ // coverage
+ final Matrix4 result2 = result.transposeLocal().transpose(new Matrix4());
+ assertEquals(mat4A, result2);
+ }
+
+ @Test
+ public void testInvert() {
+ final Matrix4 mat4A = new Matrix4().applyRotationX(MathUtils.QUARTER_PI).applyTranslationPost(1, 2, 3);
+ final Matrix4 inverted = mat4A.invert(null);
+ assertEquals(Matrix4.IDENTITY, mat4A.multiply(inverted, null));
+ assertEquals(mat4A, inverted.invertLocal());
+ }
+
+ @Test(expected = ArithmeticException.class)
+ public void testBadInvert() {
+ final Matrix4 mat4A = new Matrix4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+ mat4A.invertLocal();
+ }
+
+ @Test
+ public void testAdjugate() {
+ final double //
+ a = -3, b = 2, c = -5, d = 2, //
+ e = -1, f = 0, g = -2, h = 3, //
+ i = 1, j = -3, k = -4, l = 0, //
+ m = 4, n = 2, o = 3, p = 1;
+
+ final Matrix4 mat4A = new Matrix4( //
+ a, b, c, d,//
+ e, f, g, h, //
+ i, j, k, l, //
+ m, n, o, p);
+
+ // prepare sections
+ final Matrix3 m00 = new Matrix3(f, g, h, j, k, l, n, o, p);
+ final Matrix3 m01 = new Matrix3(b, c, d, j, k, l, n, o, p);
+ final Matrix3 m02 = new Matrix3(b, c, d, f, g, h, n, o, p);
+ final Matrix3 m03 = new Matrix3(b, c, d, f, g, h, j, k, l);
+ final Matrix3 m10 = new Matrix3(e, g, h, i, k, l, m, o, p);
+ final Matrix3 m11 = new Matrix3(a, c, d, i, k, l, m, o, p);
+ final Matrix3 m12 = new Matrix3(a, c, d, e, g, h, m, o, p);
+ final Matrix3 m13 = new Matrix3(a, c, d, e, g, h, i, k, l);
+ final Matrix3 m20 = new Matrix3(e, f, h, i, j, l, m, n, p);
+ final Matrix3 m21 = new Matrix3(a, b, d, i, j, l, m, n, p);
+ final Matrix3 m22 = new Matrix3(a, b, d, e, f, h, m, n, p);
+ final Matrix3 m23 = new Matrix3(a, b, d, e, f, h, i, j, l);
+ final Matrix3 m30 = new Matrix3(e, f, g, i, j, k, m, n, o);
+ final Matrix3 m31 = new Matrix3(a, b, c, i, j, k, m, n, o);
+ final Matrix3 m32 = new Matrix3(a, b, c, e, f, g, m, n, o);
+ final Matrix3 m33 = new Matrix3(a, b, c, e, f, g, i, j, k);
+
+ // generate adjugate
+ final Matrix4 testValue = new Matrix4( //
+ m00.determinant(), -m01.determinant(), m02.determinant(), -m03.determinant(), //
+ -m10.determinant(), m11.determinant(), -m12.determinant(), m13.determinant(), //
+ m20.determinant(), -m21.determinant(), m22.determinant(), -m23.determinant(), //
+ -m30.determinant(), m31.determinant(), -m32.determinant(), m33.determinant());
+
+ assertEquals(testValue, mat4A.adjugate(null));
+ assertEquals(testValue, mat4A.adjugateLocal());
+ }
+
+ @Test
+ public void testDeterminant() {
+ {
+ final double //
+ a = -3, b = 2, c = -5, d = 2, //
+ e = -1, f = 0, g = -2, h = 3, //
+ i = 1, j = -3, k = -4, l = 0, //
+ m = 4, n = 2, o = 3, p = 1;
+
+ final Matrix4 mat4A = new Matrix4( //
+ a, b, c, d,//
+ e, f, g, h, //
+ i, j, k, l, //
+ m, n, o, p);
+
+ // prepare sections
+ final double m00 = new Matrix3(f, g, h, j, k, l, n, o, p).determinant();
+ final double m01 = new Matrix3(e, g, h, i, k, l, m, o, p).determinant();
+ final double m02 = new Matrix3(e, f, h, i, j, l, m, n, p).determinant();
+ final double m03 = new Matrix3(e, f, g, i, j, k, m, n, o).determinant();
+ final double determinant = a * m00 - b * m01 + c * m02 - d * m03;
+
+ assertTrue(Math.abs(determinant - mat4A.determinant()) <= MathUtils.EPSILON);
+ }
+
+ {
+ final double //
+ a = -1.2, b = 4, c = -2.5, d = 1.7, //
+ e = 2, f = -3, g = -2, h = 3.2, //
+ i = 3.1, j = -1, k = 2, l = 1.15, //
+ m = 1, n = 2, o = 3.14, p = 1.4;
+
+ final Matrix4 mat4A = new Matrix4( //
+ a, b, c, d,//
+ e, f, g, h, //
+ i, j, k, l, //
+ m, n, o, p);
+
+ // prepare sections
+ final double m00 = new Matrix3(f, g, h, j, k, l, n, o, p).determinant();
+ final double m01 = new Matrix3(e, g, h, i, k, l, m, o, p).determinant();
+ final double m02 = new Matrix3(e, f, h, i, j, l, m, n, p).determinant();
+ final double m03 = new Matrix3(e, f, g, i, j, k, m, n, o).determinant();
+ final double determinant = a * m00 - b * m01 + c * m02 - d * m03;
+
+ assertTrue(Math.abs(determinant - mat4A.determinant()) <= MathUtils.EPSILON);
+ }
+ }
+
+ @Test
+ public void testClone() {
+ final Matrix4 mat1 = new Matrix4();
+ final Matrix4 mat2 = mat1.clone();
+ assertEquals(mat1, mat2);
+ assertNotSame(mat1, mat2);
+ }
+
+ @Test
+ public void testValid() {
+ final Matrix4 mat4 = new Matrix4();
+ assertTrue(Matrix4.isValid(mat4));
+ for (int i = 0; i < 16; i++) {
+ mat4.setIdentity();
+ mat4.setValue(i / 4, i % 4, Double.NaN);
+ assertFalse(Matrix4.isValid(mat4));
+ mat4.setIdentity();
+ mat4.setValue(i / 4, i % 4, Double.POSITIVE_INFINITY);
+ assertFalse(Matrix4.isValid(mat4));
+ }
+
+ mat4.setIdentity();
+ assertTrue(Matrix4.isValid(mat4));
+
+ assertFalse(Matrix4.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(mat4, mat4);
+ assertTrue(mat4.strictEquals(mat4));
+ assertFalse(mat4.equals(null));
+ assertFalse(mat4.strictEquals(null));
+ assertFalse(mat4.equals(new Vector2()));
+ assertFalse(mat4.strictEquals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Matrix4 matTemp = Matrix4.fetchTempInstance();
+ matTemp.set(mat4);
+ assertEquals(mat4, matTemp);
+ assertNotSame(mat4, matTemp);
+ Matrix4.releaseTempInstance(matTemp);
+
+ // cover more of equals
+ mat4.set(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
+ final Matrix4 comp = new Matrix4(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
+ assertFalse(mat4.equals(comp));
+ assertFalse(mat4.strictEquals(comp));
+ for (int i = 0; i < 15; i++) {
+ comp.setValue(i / 4, i % 4, i);
+ assertFalse(mat4.equals(comp));
+ assertFalse(mat4.strictEquals(comp));
+ }
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Matrix4 mat1 = new Matrix4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
+ final Matrix4 mat2 = new Matrix4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
+ final Matrix4 mat3 = new Matrix4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0);
+
+ assertTrue(mat1.hashCode() == mat2.hashCode());
+ assertTrue(mat1.hashCode() != mat3.hashCode());
+ }
+
+ @Test
+ public void testOrthonormal() {
+ final Matrix4 mat4 = new Matrix4();
+ assertTrue(mat4.isOrthonormal());
+ // just rotation
+ mat4.set(new Matrix3().applyRotationX(MathUtils.QUARTER_PI));
+ assertTrue(mat4.isOrthonormal());
+ // non-uniform scale
+ mat4.set(new Matrix3().scaleLocal(new Vector3(1, 2, 3)).applyRotationX(MathUtils.QUARTER_PI));
+ assertFalse(mat4.isOrthonormal());
+ // uniform scale
+ mat4.set(new Matrix3().scaleLocal(new Vector3(2, 2, 2)).applyRotationX(MathUtils.QUARTER_PI));
+ assertFalse(mat4.isOrthonormal());
+ // uniform scale 1
+ mat4.set(new Matrix3().scaleLocal(new Vector3(1, 1, 1)).applyRotationX(MathUtils.QUARTER_PI));
+ assertTrue(mat4.isOrthonormal());
+ }
+
+ @Test
+ public void testApplyVector4() {
+ final Matrix4 mat4 = new Matrix4().applyRotationX(MathUtils.HALF_PI);
+ final Vector4 vec4 = new Vector4(0, 1, 0, 1);
+ final Vector4 result = mat4.applyPost(vec4, null);
+ assertTrue(Math.abs(new Vector4(0, 0, 1, 1).distance(result)) <= MathUtils.EPSILON);
+ vec4.set(0, 1, 1, 1);
+ mat4.applyPost(vec4, result);
+ assertTrue(Math.abs(new Vector4(0, -1, 1, 1).distance(result)) <= MathUtils.EPSILON);
+
+ vec4.set(0, 1, 1, 1);
+ mat4.applyPre(vec4, result);
+ assertTrue(Math.abs(new Vector4(0, 1, -1, 1).distance(result)) <= MathUtils.EPSILON);
+
+ vec4.set(1, 1, 1, 1);
+ assertTrue(Math.abs(new Vector4(1, 1, -1, 1).distance(mat4.applyPre(vec4, null))) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testApplyVector3() {
+ final Matrix4 mat4 = new Matrix4().applyRotationX(MathUtils.HALF_PI).applyTranslationPre(1, 2, 3);
+ final Vector3 vec3 = new Vector3(0, 1, 0);
+ final Vector3 result = mat4.applyPostPoint(vec3, null);
+ assertTrue(Math.abs(new Vector3(1, 2, 4).distance(result)) <= MathUtils.EPSILON);
+ vec3.set(0, 1, 1);
+ mat4.applyPostPoint(vec3, result);
+ assertTrue(Math.abs(new Vector3(1, 1, 4).distance(result)) <= MathUtils.EPSILON);
+
+ vec3.set(0, 1, 1);
+ mat4.applyPostVector(vec3, result);
+ assertTrue(Math.abs(new Vector3(0, -1, 1).distance(result)) <= MathUtils.EPSILON);
+
+ vec3.set(1, 1, 1);
+ assertTrue(Math.abs(new Vector3(1, -1, 1).distance(mat4.applyPostVector(vec3, null))) <= MathUtils.EPSILON);
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestObjectPool.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestObjectPool.java new file mode 100644 index 0000000..5f06c34 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestObjectPool.java @@ -0,0 +1,34 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import org.junit.Test;
+
+public class TestObjectPool {
+
+ @Test(expected = RuntimeException.class)
+ public void testPoolReleaseNullError() {
+ Vector2.releaseTempInstance(null);
+ }
+
+ @Test(expected = RuntimeException.class)
+ public void testPoolBadClass() {
+ ObjectPool.create(Poolable.class, 10).fetch();
+ }
+
+ @Test
+ public void testPoolSize() {
+ final ObjectPool<Vector2> pool = ObjectPool.create(Vector2.class, 10);
+ for (int i = 0; i < 11; i++) {
+ pool.release(new Vector2());
+ }
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestPlane.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestPlane.java new file mode 100644 index 0000000..1b6b164 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestPlane.java @@ -0,0 +1,136 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import com.ardor3d.math.type.ReadOnlyPlane.Side;
+
+public class TestPlane {
+
+ @Test
+ public void testGetSet() {
+ final Plane plane = new Plane();
+ assertEquals(Vector3.UNIT_Y, plane.getNormal());
+ assertTrue(plane.getConstant() == 0.0);
+
+ plane.setNormal(Vector3.UNIT_X);
+ plane.setConstant(1.0);
+ assertEquals(Vector3.UNIT_X, plane.getNormal());
+ assertTrue(plane.getConstant() == 1.0);
+
+ final Plane plane2 = new Plane(plane);
+ assertEquals(Vector3.UNIT_X, plane2.getNormal());
+ assertTrue(plane.getConstant() == 1.0);
+
+ final Plane plane3 = new Plane(Vector3.NEG_UNIT_Z, 2.5);
+ assertEquals(Vector3.NEG_UNIT_Z, plane3.getNormal());
+ assertTrue(plane3.getConstant() == 2.5);
+
+ final Plane plane4 = new Plane().setPlanePoints(new Vector3(1, 1, 1), new Vector3(2, 1, 1),
+ new Vector3(2, 2, 1));
+ assertEquals(Vector3.UNIT_Z, plane4.getNormal());
+ assertTrue(plane4.getConstant() == 1.0);
+ }
+
+ @Test
+ public void testEquals() {
+ // couple of equals validity tests
+ final Plane plane1 = new Plane();
+ assertEquals(plane1, plane1);
+ assertFalse(plane1.equals(null));
+ assertFalse(plane1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Plane plane2 = Plane.fetchTempInstance();
+ plane2.set(plane1);
+ assertEquals(plane1, plane2);
+ assertNotSame(plane1, plane2);
+ Plane.releaseTempInstance(plane2);
+
+ // cover more of equals
+ assertFalse(plane1.equals(new Plane(Vector3.UNIT_X, 0)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Plane plane1 = new Plane(Vector3.UNIT_Y, 2);
+ final Plane plane2 = new Plane(Vector3.UNIT_Y, 2);
+ final Plane plane3 = new Plane(Vector3.UNIT_Z, 2);
+
+ assertTrue(plane1.hashCode() == plane2.hashCode());
+ assertTrue(plane1.hashCode() != plane3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final Plane plane1 = new Plane();
+ final Plane plane2 = plane1.clone();
+ assertEquals(plane1, plane2);
+ assertNotSame(plane1, plane2);
+ }
+
+ @Test
+ public void testValid() {
+ final Plane plane1 = new Plane();
+ final Plane plane2 = new Plane(new Vector3(Double.NaN, 0, 0), 0.5);
+ final Plane plane3 = new Plane(Vector3.UNIT_X, Double.NaN);
+ final Plane plane4 = new Plane(Vector3.UNIT_X, Double.POSITIVE_INFINITY);
+
+ assertTrue(Plane.isValid(plane1));
+ assertFalse(Plane.isValid(plane2));
+ assertFalse(Plane.isValid(plane3));
+ assertFalse(Plane.isValid(plane4));
+
+ plane4.setConstant(1);
+ assertTrue(Plane.isValid(plane4));
+
+ assertFalse(Plane.isValid(null));
+ }
+
+ @Test
+ public void testDistance() {
+ final Plane plane1 = new Plane(Vector3.UNIT_Y, 1.0);
+ final Vector3 point = new Vector3(0, 5, 0);
+ assertTrue(4.0 == plane1.pseudoDistance(point));
+ assertEquals(Side.Outside, plane1.whichSide(point));
+
+ point.set(0, -4, 0);
+ assertTrue(-5.0 == plane1.pseudoDistance(point));
+ assertEquals(Side.Inside, plane1.whichSide(point));
+
+ point.set(1, 1, 1);
+ assertTrue(0.0 == plane1.pseudoDistance(point));
+ assertEquals(Side.Neither, plane1.whichSide(point));
+ }
+
+ @Test
+ public void testReflect() {
+ final Plane plane1 = new Plane(Vector3.UNIT_X, 5.0);
+ assertEquals(new Vector3(), plane1.reflectVector(new Vector3(), new Vector3()));
+ assertEquals(new Vector3(-1, 0, 0), plane1.reflectVector(new Vector3(1, 0, 0), null));
+ assertEquals(new Vector3(-1, 1, 1).normalizeLocal(),
+ plane1.reflectVector(new Vector3(1, 1, 1).normalizeLocal(), null));
+ assertEquals(new Vector3(-3, 2, -1).normalizeLocal(),
+ plane1.reflectVector(new Vector3(3, 2, -1).normalizeLocal(), null));
+
+ final Plane plane2 = new Plane(Vector3.UNIT_Z, 1.0);
+ assertEquals(new Vector3(), plane2.reflectVector(new Vector3(), new Vector3()));
+ assertEquals(new Vector3(0, 0, -1), plane2.reflectVector(new Vector3(0, 0, 1), null));
+ assertEquals(new Vector3(1, 1, -1).normalizeLocal(),
+ plane2.reflectVector(new Vector3(1, 1, 1).normalizeLocal(), null));
+ assertEquals(new Vector3(3, 2, 1).normalizeLocal(),
+ plane2.reflectVector(new Vector3(3, 2, -1).normalizeLocal(), null));
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestQuaternion.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestQuaternion.java new file mode 100644 index 0000000..f7ee4f4 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestQuaternion.java @@ -0,0 +1,554 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import com.ardor3d.math.type.ReadOnlyVector3;
+
+public class TestQuaternion {
+
+ @Test
+ public void testGetSet() {
+ final Quaternion quat1 = new Quaternion();
+ assertEquals(Quaternion.IDENTITY, quat1);
+ assertTrue(quat1.isIdentity());
+
+ quat1.setX(1);
+ assertTrue(quat1.getX() == 1.0);
+ quat1.setX(Double.POSITIVE_INFINITY);
+ assertTrue(quat1.getX() == Double.POSITIVE_INFINITY);
+ quat1.setX(Double.NEGATIVE_INFINITY);
+ assertTrue(quat1.getX() == Double.NEGATIVE_INFINITY);
+
+ quat1.setY(1);
+ assertTrue(quat1.getY() == 1.0);
+ quat1.setY(Double.POSITIVE_INFINITY);
+ assertTrue(quat1.getY() == Double.POSITIVE_INFINITY);
+ quat1.setY(Double.NEGATIVE_INFINITY);
+ assertTrue(quat1.getY() == Double.NEGATIVE_INFINITY);
+
+ quat1.setZ(1);
+ assertTrue(quat1.getZ() == 1.0);
+ quat1.setZ(Double.POSITIVE_INFINITY);
+ assertTrue(quat1.getZ() == Double.POSITIVE_INFINITY);
+ quat1.setZ(Double.NEGATIVE_INFINITY);
+ assertTrue(quat1.getZ() == Double.NEGATIVE_INFINITY);
+
+ quat1.setW(1);
+ assertTrue(quat1.getW() == 1.0);
+ quat1.setW(Double.POSITIVE_INFINITY);
+ assertTrue(quat1.getW() == Double.POSITIVE_INFINITY);
+ quat1.setW(Double.NEGATIVE_INFINITY);
+ assertTrue(quat1.getW() == Double.NEGATIVE_INFINITY);
+
+ quat1.set(Math.PI, Math.PI, Math.PI, Math.PI);
+ assertTrue(quat1.getXf() == (float) Math.PI);
+ assertTrue(quat1.getYf() == (float) Math.PI);
+ assertTrue(quat1.getZf() == (float) Math.PI);
+ assertTrue(quat1.getWf() == (float) Math.PI);
+
+ final Quaternion quat2 = new Quaternion();
+ quat2.set(quat1);
+ assertEquals(quat1, quat2);
+ }
+
+ @Test
+ public void testToArray() {
+ final Quaternion quat1 = new Quaternion();
+ quat1.set(Math.PI, Double.MAX_VALUE, 42, -1);
+ final double[] array = quat1.toArray(null);
+ final double[] array2 = quat1.toArray(new double[4]);
+ assertNotNull(array);
+ assertTrue(array.length == 4);
+ assertTrue(array[0] == Math.PI);
+ assertTrue(array[1] == Double.MAX_VALUE);
+ assertTrue(array[2] == 42);
+ assertTrue(array[3] == -1);
+ assertNotNull(array2);
+ assertTrue(array2.length == 4);
+ assertTrue(array2[0] == Math.PI);
+ assertTrue(array2[1] == Double.MAX_VALUE);
+ assertTrue(array2[2] == 42);
+ assertTrue(array2[3] == -1);
+ }
+
+ @Test(expected = ArrayIndexOutOfBoundsException.class)
+ public void testBadArray() {
+ final Quaternion quat = new Quaternion();
+ quat.toArray(new double[2]);
+ }
+
+ @Test(expected = ArrayIndexOutOfBoundsException.class)
+ public void testBadAxesArray() {
+ final Quaternion quat = new Quaternion();
+ quat.toAxes(new Vector3[2]);
+ }
+
+ @Test(expected = ArrayIndexOutOfBoundsException.class)
+ public void testBadEuler1() {
+ new Quaternion().fromEulerAngles(new double[2]);
+ }
+
+ @Test(expected = ArrayIndexOutOfBoundsException.class)
+ public void testBadEuler2() {
+ final Quaternion quat = new Quaternion();
+ quat.toEulerAngles(new double[2]);
+ }
+
+ @Test
+ public void testEulerAngles() {
+ final Quaternion quat = new Quaternion().fromEulerAngles(new double[] { MathUtils.HALF_PI, 0, 0 });
+ assertTrue(1.0 == quat.magnitude());
+ assertTrue(Math.abs(Vector3.NEG_UNIT_Z.distance(quat.apply(Vector3.UNIT_X, null))) <= MathUtils.EPSILON);
+
+ quat.fromEulerAngles(0, -MathUtils.HALF_PI, 0);
+ assertTrue(1.0 == quat.magnitude());
+ assertTrue(Math.abs(Vector3.NEG_UNIT_Y.distance(quat.apply(Vector3.UNIT_X, null))) <= MathUtils.EPSILON);
+
+ quat.fromEulerAngles(0, 0, MathUtils.HALF_PI);
+ assertTrue(1.0 == quat.magnitude());
+ assertTrue(Math.abs(Vector3.UNIT_Z.distance(quat.apply(Vector3.UNIT_Y, null))) <= MathUtils.EPSILON);
+
+ quat.fromEulerAngles(0, MathUtils.HALF_PI, 0);
+ double[] angles = quat.toEulerAngles(null);
+ final Quaternion quat2 = new Quaternion().fromEulerAngles(angles);
+ assertEquals(quat, quat2);
+ quat.fromEulerAngles(0, -MathUtils.HALF_PI, 0);
+ angles = quat.toEulerAngles(null);
+ quat2.fromEulerAngles(angles);
+ assertEquals(quat, quat2);
+ quat.fromEulerAngles(0, 0, MathUtils.HALF_PI);
+ angles = quat.toEulerAngles(null);
+ quat2.fromEulerAngles(angles);
+ assertEquals(quat, quat2);
+ }
+
+ @Test
+ public void testMatrix3() {
+ double a = MathUtils.HALF_PI;
+ final Quaternion quat = new Quaternion();
+ quat.fromRotationMatrix( //
+ 1, 0, 0, //
+ 0, Math.cos(a), -Math.sin(a), //
+ 0, Math.sin(a), Math.cos(a));
+
+ assertTrue(Math.abs(Vector3.UNIT_Z.distance(quat.apply(Vector3.UNIT_Y, null))) <= MathUtils.EPSILON);
+ final Matrix3 mat = quat.toRotationMatrix((Matrix3) null);
+ assertTrue(Math.abs(quat.apply(Vector3.NEG_ONE, null).distance(mat.applyPost(Vector3.NEG_ONE, null))) <= MathUtils.EPSILON);
+
+ a = MathUtils.PI;
+ quat.fromRotationMatrix( //
+ 1, 0, 0, //
+ 0, Math.cos(a), -Math.sin(a), //
+ 0, Math.sin(a), Math.cos(a));
+
+ assertTrue(Math.abs(Vector3.NEG_UNIT_Y.distance(quat.apply(Vector3.UNIT_Y, null))) <= MathUtils.EPSILON);
+ quat.toRotationMatrix(mat);
+ assertTrue(Math.abs(quat.apply(Vector3.ONE, null).distance(mat.applyPost(Vector3.ONE, null))) <= MathUtils.EPSILON);
+
+ quat.set(0, 0, 0, 0);
+ assertEquals(Matrix3.IDENTITY, quat.toRotationMatrix((Matrix3) null));
+
+ a = MathUtils.PI;
+ quat.fromRotationMatrix( //
+ Math.cos(a), 0, Math.sin(a), //
+ 0, 1, 0, //
+ -Math.sin(a), 0, Math.cos(a));
+
+ assertTrue(Math.abs(Vector3.NEG_UNIT_X.distance(quat.apply(Vector3.UNIT_X, null))) <= MathUtils.EPSILON);
+ final Matrix4 mat4 = quat.toRotationMatrix((Matrix4) null);
+ assertTrue(Math.abs(quat.apply(Vector3.NEG_ONE, null).distance(mat4.applyPostVector(Vector3.NEG_ONE, null))) <= MathUtils.EPSILON);
+
+ a = MathUtils.PI;
+ quat.fromRotationMatrix(new Matrix3(//
+ Math.cos(a), -Math.sin(a), 0, //
+ Math.sin(a), Math.cos(a), 0, //
+ 0, 0, 1));
+
+ assertTrue(Math.abs(Vector3.NEG_UNIT_X.distance(quat.apply(Vector3.UNIT_X, null))) <= MathUtils.EPSILON);
+ quat.toRotationMatrix(mat4);
+ assertTrue(Math.abs(quat.apply(Vector3.ONE, null).distance(mat4.applyPostVector(Vector3.ONE, null))) <= MathUtils.EPSILON);
+
+ quat.set(0, 0, 0, 0);
+ assertEquals(Matrix4.IDENTITY, quat.toRotationMatrix((Matrix4) null));
+ }
+
+ @Test
+ public void testRotations() {
+ final double a = MathUtils.QUARTER_PI;
+ final Quaternion quat = new Quaternion().fromRotationMatrix(new Matrix3(//
+ Math.cos(a), -Math.sin(a), 0, //
+ Math.sin(a), Math.cos(a), 0, //
+ 0, 0, 1));
+ final Vector3 column = quat.getRotationColumn(0, null);
+ assertTrue(Math.abs(new Vector3(Math.cos(a), Math.sin(a), 0).distance(column)) <= MathUtils.EPSILON);
+ quat.getRotationColumn(1, column);
+ assertTrue(Math.abs(new Vector3(-Math.sin(a), Math.sin(a), 0).distance(column)) <= MathUtils.EPSILON);
+ quat.getRotationColumn(2, column);
+ assertTrue(Math.abs(new Vector3(0, 0, 1).distance(column)) <= MathUtils.EPSILON);
+
+ quat.set(0, 0, 0, 0);
+ assertEquals(Vector3.UNIT_X, quat.getRotationColumn(0, null));
+
+ // Try a new way with new angles...
+ quat.fromEulerAngles(MathUtils.QUARTER_PI, MathUtils.PI, MathUtils.HALF_PI);
+ final Vector3 rotated = new Vector3(1, 1, 1);
+ quat.apply(rotated, rotated);
+
+ // expected
+ final Vector3 expected = new Vector3(1, 1, 1);
+ final Quaternion worker = new Quaternion();
+ // put together matrix, then apply to vector, so YZX
+ worker.applyRotationY(MathUtils.QUARTER_PI);
+ worker.applyRotationZ(MathUtils.PI);
+ worker.applyRotationX(MathUtils.HALF_PI);
+ worker.apply(expected, expected);
+
+ // test how close it came out
+ assertTrue(rotated.distance(expected) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // test axis rotation methods against general purpose
+ // X AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationX(MathUtils.QUARTER_PI).apply(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 1, 0, 0).apply(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Y AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationY(MathUtils.QUARTER_PI).apply(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 1, 0).apply(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ // Z AXIS
+ expected.set(1, 1, 1);
+ rotated.set(1, 1, 1);
+ worker.setIdentity().applyRotationZ(MathUtils.QUARTER_PI).apply(expected, expected);
+ worker.setIdentity().applyRotation(MathUtils.QUARTER_PI, 0, 0, 1).apply(rotated, rotated);
+ assertTrue(rotated.distance(expected) <= MathUtils.EPSILON);
+
+ quat.set(worker);
+ worker.applyRotation(0, 0, 0, 0);
+ assertEquals(quat, worker);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBadRotationColumn1() {
+ new Quaternion().getRotationColumn(-1, null);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testBadRotationColumn2() {
+ new Quaternion().getRotationColumn(4, null);
+ }
+
+ @Test
+ public void testAngleAxis() {
+ final Quaternion quat = new Quaternion().fromAngleAxis(MathUtils.HALF_PI, new Vector3(2, 0, 0));
+ final Quaternion quat2 = new Quaternion().fromAngleNormalAxis(MathUtils.HALF_PI, new Vector3(1, 0, 0));
+
+ assertEquals(quat2, quat);
+ assertTrue(1 - quat.magnitude() <= MathUtils.EPSILON);
+
+ assertEquals(quat.apply(Vector3.ONE, null), quat2.apply(Vector3.ONE, null));
+ assertTrue(Math.abs(new Vector3(0, -1, 0).distance(quat.apply(new Vector3(0, 0, 1), null))) <= MathUtils.EPSILON);
+
+ assertEquals(Quaternion.IDENTITY,
+ new Quaternion(1, 2, 3, 4).fromAngleAxis(MathUtils.HALF_PI, new Vector3(0, 0, 0)));
+
+ final Vector3 axisStore = new Vector3();
+ double angle = quat.toAngleAxis(axisStore);
+ assertEquals(quat, new Quaternion().fromAngleAxis(angle, axisStore));
+
+ quat.set(0, 0, 0, 0);
+ angle = quat.toAngleAxis(axisStore);
+ assertTrue(0.0 == angle);
+ assertEquals(Vector3.UNIT_X, axisStore);
+ }
+
+ @Test
+ public void testFromVectorToVector() {
+ final Quaternion quat = new Quaternion().fromVectorToVector(Vector3.UNIT_Z, Vector3.UNIT_X);
+ assertEquals(new Quaternion().fromAngleAxis(MathUtils.HALF_PI, Vector3.UNIT_Y), quat);
+
+ quat.fromVectorToVector(Vector3.UNIT_Z, Vector3.NEG_UNIT_Z);
+ assertTrue(Math.abs(new Vector3(0, 0, -1).distance(quat.apply(new Vector3(0, 0, 1), null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat.fromVectorToVector(Vector3.UNIT_X, Vector3.NEG_UNIT_X);
+ assertTrue(Math.abs(new Vector3(-1, 0, 0).distance(quat.apply(new Vector3(1, 0, 0), null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat.fromVectorToVector(Vector3.UNIT_Y, Vector3.NEG_UNIT_Y);
+ assertTrue(Math.abs(new Vector3(0, -1, 0).distance(quat.apply(new Vector3(0, 1, 0), null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat.fromVectorToVector(Vector3.ONE, Vector3.NEG_ONE);
+ assertTrue(Math.abs(new Vector3(-1, -1, -1).distance(quat.apply(new Vector3(1, 1, 1), null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat.fromVectorToVector(Vector3.ZERO, Vector3.ZERO);
+ assertEquals(Quaternion.IDENTITY, quat);
+ }
+
+ @Test
+ public void testNormalize() {
+ final Quaternion quat = new Quaternion(0, 1, 2, 3);
+ final Quaternion quat2 = quat.normalize(null);
+ assertEquals(quat2, quat.normalizeLocal());
+ assertTrue(Math.abs(1 - quat.magnitude()) <= MathUtils.EPSILON);
+ assertTrue(Math.abs(1 - quat2.magnitude()) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testApplyToZero() {
+ assertEquals(Vector3.ZERO, new Quaternion().apply(new Vector3(0, 0, 0), null));
+ }
+
+ @Test
+ public void testInvert() {
+ final Quaternion quat1 = new Quaternion(0, 1, 2, 3);
+ final Quaternion quat2 = quat1.invert(null);
+ assertEquals(Quaternion.IDENTITY, quat1.multiply(quat2, null));
+ assertEquals(quat1, quat2.invert(new Quaternion()));
+ assertEquals(quat1, quat2.invertLocal());
+
+ // normalized version
+ quat1.fromAngleAxis(MathUtils.QUARTER_PI, Vector3.UNIT_Y);
+ quat1.invert(quat2);
+ assertEquals(Quaternion.IDENTITY, quat1.multiply(quat2, null));
+ assertEquals(quat1, quat2.invert(new Quaternion()));
+ assertEquals(quat1, quat2.invertLocal());
+
+ // conjugate check
+ assertEquals(new Quaternion(-1, -2, -3, 4), new Quaternion(1, 2, 3, 4).conjugate(null));
+ }
+
+ @Test
+ public void testAddSubtract() {
+ final Quaternion quat1 = new Quaternion(0, 1, 2, 3);
+ final Quaternion quat2 = new Quaternion(1, 1, 1, 1);
+ assertEquals(new Quaternion(1, 2, 3, 4), quat1.add(quat2, null));
+ assertEquals(new Quaternion(1, 2, 3, 4), quat1.add(quat2, new Quaternion()));
+ assertEquals(new Quaternion(1, 2, 3, 4), quat1.addLocal(quat2));
+
+ quat1.set(0, 1, 2, 3);
+ quat2.set(1, 1, 1, 1);
+ assertEquals(new Quaternion(-1, 0, 1, 2), quat1.subtract(quat2, null));
+ assertEquals(new Quaternion(-1, 0, 1, 2), quat1.subtract(quat2, new Quaternion()));
+ assertEquals(new Quaternion(-1, 0, 1, 2), quat1.subtractLocal(quat2));
+ }
+
+ @Test
+ public void testMultiply() {
+ final Quaternion quat1 = new Quaternion(0.5, 1, 2, 3);
+ final Quaternion quat2 = new Quaternion();
+ assertEquals(new Quaternion(1, 2, 4, 6), quat1.multiply(2, null));
+ assertEquals(new Quaternion(2, 4, 8, 12), quat1.multiply(4, quat2));
+ assertEquals(new Quaternion(1, 2, 4, 6), quat1.multiplyLocal(2));
+
+ quat1.fromAngleNormalAxis(MathUtils.QUARTER_PI, Vector3.UNIT_Y);
+ quat1.multiply(quat1, quat2);
+
+ final ReadOnlyVector3 vec = Vector3.UNIT_Z;
+ assertTrue(Math.abs(Vector3.UNIT_X.distance(quat2.apply(vec, null))) <= Quaternion.ALLOWED_DEVIANCE);
+ quat1.multiplyLocal(quat1.getX(), quat1.getY(), quat1.getZ(), quat1.getW());
+ assertTrue(Math.abs(Vector3.UNIT_X.distance(quat1.apply(vec, null))) <= Quaternion.ALLOWED_DEVIANCE);
+ quat2.fromAngleNormalAxis(MathUtils.HALF_PI, Vector3.UNIT_Y);
+ quat1.multiplyLocal(quat2);
+ assertTrue(Math.abs(Vector3.NEG_UNIT_Z.distance(quat1.apply(vec, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat1.multiplyLocal(new Matrix3().applyRotationY(MathUtils.HALF_PI));
+ assertTrue(Math.abs(Vector3.NEG_UNIT_X.distance(quat1.apply(vec, null))) <= Quaternion.ALLOWED_DEVIANCE);
+ }
+
+ @Test
+ public void testAxes() {
+ final Matrix3 rot = new Matrix3().applyRotationX(MathUtils.QUARTER_PI).applyRotationY(MathUtils.HALF_PI);
+ final Quaternion quat1 = new Quaternion().fromAxes(rot.getColumn(0, null), rot.getColumn(1, null),
+ rot.getColumn(2, null));
+ final Quaternion quat2 = new Quaternion().fromRotationMatrix(rot);
+ assertEquals(quat2, quat1);
+
+ final Vector3[] axes = quat1.toAxes(new Vector3[3]);
+ quat1.fromAxes(axes[0], axes[1], axes[2]);
+ assertEquals(quat2, quat1);
+ }
+
+ @Test
+ public void testSlerp() {
+ final Quaternion quat = new Quaternion();
+ final Quaternion quat2 = new Quaternion().applyRotationY(MathUtils.HALF_PI);
+ final Quaternion store = quat.slerp(quat2, .5, null);
+ assertTrue(Math.abs(new Vector3(Math.sin(MathUtils.QUARTER_PI), 0, Math.sin(MathUtils.QUARTER_PI))
+ .distance(store.apply(Vector3.UNIT_Z, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // delta == 100%
+ quat2.setIdentity().applyRotationZ(MathUtils.PI);
+ quat.slerp(quat2, 1.0, store);
+ assertTrue(Math.abs(new Vector3(-1, 0, 0).distance(store.apply(Vector3.UNIT_X, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat2.setIdentity().applyRotationZ(MathUtils.PI);
+ quat.slerp(quat2, .5, store);
+ assertTrue(Math.abs(new Vector3(0, 1, 0).distance(store.apply(Vector3.UNIT_X, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // delta == 0%
+ quat2.setIdentity().applyRotationZ(MathUtils.PI);
+ quat.slerp(quat2, 0, store);
+ assertTrue(Math.abs(new Vector3(1, 0, 0).distance(store.apply(Vector3.UNIT_X, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // a==b
+ quat2.setIdentity();
+ quat.slerp(quat2, 0.25, store);
+ assertTrue(Math.abs(new Vector3(1, 0, 0).distance(store.apply(Vector3.UNIT_X, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // negative dot product
+ quat.setIdentity().applyRotationX(-2 * MathUtils.HALF_PI);
+ quat2.setIdentity().applyRotationX(MathUtils.HALF_PI);
+ quat.slerp(quat2, 0.5, store);
+ assertTrue(Math.abs(new Vector3(0, -Math.sin(MathUtils.QUARTER_PI), Math.sin(MathUtils.QUARTER_PI))
+ .distance(store.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // LOCAL
+ // delta == 100%
+ quat2.setIdentity().applyRotationX(MathUtils.PI);
+ quat.slerpLocal(quat2, 1.0);
+ assertTrue(Math.abs(new Vector3(0, -1, 0).distance(quat.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ quat.setIdentity();
+ quat.slerpLocal(quat2, .5);
+ assertTrue(Math.abs(new Vector3(0, 0, 1).distance(quat.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // delta == 0%
+ quat.setIdentity();
+ quat.slerpLocal(quat2, 0);
+ assertTrue(Math.abs(new Vector3(0, 1, 0).distance(quat.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // a==b
+ quat.setIdentity();
+ quat2.setIdentity();
+ quat.slerpLocal(quat2, 0.25);
+ assertTrue(Math.abs(new Vector3(0, 1, 0).distance(quat.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ // negative dot product
+ quat.setIdentity().applyRotationX(-2 * MathUtils.HALF_PI);
+ quat2.setIdentity().applyRotationX(MathUtils.HALF_PI);
+ quat.slerpLocal(quat2, 0.5);
+ assertTrue(Math.abs(new Vector3(0, -Math.sin(MathUtils.QUARTER_PI), Math.sin(MathUtils.QUARTER_PI))
+ .distance(quat.apply(Vector3.UNIT_Y, null))) <= Quaternion.ALLOWED_DEVIANCE);
+ }
+
+ @Test
+ public void testLookAt() {
+ final Vector3 direction = new Vector3(-1, 0, 0);
+ final Quaternion quat = new Quaternion().lookAt(direction, Vector3.UNIT_Y);
+ assertTrue(Math.abs(direction.distance(quat.apply(Vector3.UNIT_Z, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ direction.set(1, 1, 1).normalizeLocal();
+ quat.lookAt(direction, Vector3.UNIT_Y);
+ assertTrue(Math.abs(direction.distance(quat.apply(Vector3.UNIT_Z, null))) <= Quaternion.ALLOWED_DEVIANCE);
+
+ direction.set(-1, 2, -1).normalizeLocal();
+ quat.lookAt(direction, Vector3.UNIT_Y);
+ assertTrue(Math.abs(direction.distance(quat.apply(Vector3.UNIT_Z, null))) <= Quaternion.ALLOWED_DEVIANCE);
+ }
+
+ @Test
+ public void testDot() {
+ final Quaternion quat = new Quaternion(7, 2, 5, -1);
+ assertTrue(35.0 == quat.dot(3, 1, 2, -2));
+
+ assertTrue(-11.0 == quat.dot(new Quaternion(-1, 1, -1, 1)));
+ }
+
+ @Test
+ public void testClone() {
+ final Quaternion quat1 = new Quaternion();
+ final Quaternion quat2 = quat1.clone();
+ assertEquals(quat1, quat2);
+ assertNotSame(quat1, quat2);
+ }
+
+ @Test
+ public void testValid() {
+ final Quaternion quat = new Quaternion();
+ assertTrue(Quaternion.isValid(quat));
+
+ quat.set(Double.NaN, 0, 0, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, Double.NaN, 0, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, 0, Double.NaN, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, 0, 0, Double.NaN);
+ assertFalse(Quaternion.isValid(quat));
+
+ quat.set(Double.NEGATIVE_INFINITY, 0, 0, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, Double.NEGATIVE_INFINITY, 0, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, 0, Double.NEGATIVE_INFINITY, 0);
+ assertFalse(Quaternion.isValid(quat));
+ quat.set(0, 0, 0, Double.NEGATIVE_INFINITY);
+ assertFalse(Quaternion.isValid(quat));
+
+ quat.setIdentity();
+ assertTrue(Quaternion.isValid(quat));
+
+ assertFalse(Quaternion.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(quat, quat);
+ assertTrue(quat.strictEquals(quat));
+ assertFalse(quat.equals(null));
+ assertFalse(quat.strictEquals(null));
+ assertFalse(quat.equals(new Vector2()));
+ assertFalse(quat.strictEquals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Quaternion quatTemp = Quaternion.fetchTempInstance();
+ quatTemp.set(quat);
+ assertEquals(quat, quatTemp);
+ assertNotSame(quat, quatTemp);
+ Quaternion.releaseTempInstance(quatTemp);
+
+ // cover more of equals
+ quat.set(0, 1, 2, 3);
+ final Quaternion comp = new Quaternion(-1, -1, -1, -1);
+ assertFalse(quat.equals(comp));
+ assertFalse(quat.strictEquals(comp));
+ comp.setX(0);
+ assertFalse(quat.equals(comp));
+ assertFalse(quat.strictEquals(comp));
+ comp.setY(1);
+ assertFalse(quat.equals(comp));
+ assertFalse(quat.strictEquals(comp));
+ comp.setZ(2);
+ assertFalse(quat.equals(comp));
+ assertFalse(quat.strictEquals(comp));
+ comp.setW(3);
+ assertEquals(quat, comp);
+ assertTrue(quat.strictEquals(comp));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Quaternion quat1 = new Quaternion(1, 2, 3, 4);
+ final Quaternion quat2 = new Quaternion(1, 2, 3, 4);
+ final Quaternion quat3 = new Quaternion(1, 2, 3, 0);
+
+ assertTrue(quat1.hashCode() == quat2.hashCode());
+ assertTrue(quat1.hashCode() != quat3.hashCode());
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestRay3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestRay3.java new file mode 100644 index 0000000..f662700 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestRay3.java @@ -0,0 +1,230 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import com.ardor3d.math.type.ReadOnlyRay3;
+
+public class TestRay3 {
+ @Test
+ public void testData() {
+ final Ray3 ray = new Ray3();
+ assertEquals(Vector3.UNIT_Z, ray.getDirection());
+ assertEquals(Vector3.ZERO, ray.getOrigin());
+
+ ray.setDirection(Vector3.NEG_UNIT_X);
+ assertEquals(Vector3.NEG_UNIT_X, ray.getDirection());
+ ray.setOrigin(Vector3.ONE);
+ assertEquals(Vector3.ONE, ray.getOrigin());
+
+ final Ray3 ray2 = new Ray3(ray);
+ assertEquals(Vector3.NEG_UNIT_X, ray2.getDirection());
+ assertEquals(Vector3.ONE, ray2.getOrigin());
+
+ ray.set(new Ray3());
+ assertEquals(Vector3.UNIT_Z, ray.getDirection());
+ assertEquals(Vector3.ZERO, ray.getOrigin());
+ }
+
+ @Test
+ public void testValid() {
+ final Ray3 ray1 = new Ray3(new Vector3(0, 0, 0), new Vector3(0, 0, 1));
+ final Ray3 ray2 = new Ray3(new Vector3(Double.POSITIVE_INFINITY, 0, 0), new Vector3(0, 0, 1));
+ final Ray3 ray3 = new Ray3(new Vector3(0, 0, 0), new Vector3(Double.POSITIVE_INFINITY, 0, 1));
+
+ assertTrue(Ray3.isValid(ray1));
+ assertFalse(Ray3.isValid(ray2));
+ assertFalse(Ray3.isValid(ray3));
+
+ assertFalse(Ray3.isValid(null));
+
+ // couple if equals validity tests
+ assertEquals(ray1, ray1);
+ assertFalse(ray1.equals(null));
+ assertFalse(ray1.equals(new Vector3()));
+
+ // throw in a couple pool accesses for coverage
+ final Ray3 ray4 = Ray3.fetchTempInstance();
+ ray4.set(ray1);
+ assertEquals(ray1, ray4);
+ assertNotSame(ray1, ray4);
+ Ray3.releaseTempInstance(ray4);
+
+ // cover more of equals
+ assertFalse(ray1.equals(new Ray3(Vector3.ZERO, Vector3.NEG_UNIT_X)));
+ }
+
+ @Test
+ public void testClone() {
+ final Ray3 ray1 = new Ray3();
+ final Ray3 ray2 = ray1.clone();
+ assertEquals(ray1, ray2);
+ assertNotSame(ray1, ray2);
+ }
+
+ @Test
+ public void testDistance() {
+ final Ray3 ray1 = new Ray3();
+ assertTrue(25.0 == ray1.distanceSquared(new Vector3(0, 5, 3), null));
+
+ final Vector3 store = new Vector3();
+ assertTrue(9.0 == ray1.distanceSquared(new Vector3(0, 3, 3), store));
+ assertEquals(new Vector3(0, 0, 3), store);
+ assertTrue(18.0 == ray1.distanceSquared(new Vector3(0, 3, -3), store));
+ assertEquals(new Vector3(0, 0, 0), store);
+ }
+
+ @Test
+ public void testIntersectsTriangle() {
+ final Vector3 v0 = new Vector3(-1, -1, -1);
+ final Vector3 v1 = new Vector3(+1, -1, -1);
+ final Vector3 v2 = new Vector3(+1, +1, -1);
+
+ final Vector3 intersectionPoint = new Vector3();
+
+ // inside triangle
+ Ray3 pickRay = new Ray3(new Vector3(0.5, -0.5, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // horizontal edge
+ pickRay = new Ray3(new Vector3(0, -1, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // diagonal edge
+ pickRay = new Ray3(new Vector3(0, 0, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // vertical edge
+ pickRay = new Ray3(new Vector3(+1, 0, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // v0
+ pickRay = new Ray3(new Vector3(-1, -1, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // v1
+ pickRay = new Ray3(new Vector3(+1, -1, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // v2
+ pickRay = new Ray3(new Vector3(1, 1, 3), new Vector3(0, 0, -1));
+ assertTrue(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // outside horizontal edge
+ pickRay = new Ray3(new Vector3(0, -1.1, 3), new Vector3(0, 0, -1));
+ assertFalse(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // outside diagonal edge
+ pickRay = new Ray3(new Vector3(-0.1, 0.1, 3), new Vector3(0, 0, -1));
+ assertFalse(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // outside vertical edge
+ pickRay = new Ray3(new Vector3(+1.1, 0, 3), new Vector3(0, 0, -1));
+ assertFalse(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // inside triangle but ray pointing other way
+ pickRay = new Ray3(new Vector3(-0.5, -0.5, 3), new Vector3(0, 0, +1));
+ assertFalse(pickRay.intersectsTriangle(v0, v1, v2, intersectionPoint));
+
+ // test distance
+ pickRay = new Ray3(new Vector3(0.5, -0.5, 3), new Vector3(0, 0, -1));
+ assertTrue(4.0 == pickRay.getDistanceToPrimitive(new Vector3[] { v0, v1, v2 }));
+
+ // test intersect planar
+ assertTrue(pickRay.intersectsTrianglePlanar(v0, v1, v2, intersectionPoint));
+
+ }
+
+ @Test
+ public void testIntersectsPlane() {
+ final Vector3 intersectionPoint = new Vector3();
+
+ Plane plane = new Plane(new Vector3(0, 1, 0), 2);
+
+ Ray3 pickRay = new Ray3(new Vector3(0, 3, 0), new Vector3(0, 0, 1));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, 3, 0), new Vector3(0, 1, 0));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, 2, 0), new Vector3(0, 1, 0));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, 1, 0), new Vector3(0, 1, 0));
+ assertTrue(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, 0, 0), new Vector3(1, 0, 0));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -3, 0), new Vector3(0, 0, 1));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, 3, 0), new Vector3(0, -1, 0));
+ assertTrue(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -3, 0), new Vector3(1, 1, 1));
+ assertTrue(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -3, 0), new Vector3(-1, -1, -1));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ plane = new Plane(new Vector3(1, 1, 1), -2);
+
+ pickRay = new Ray3(new Vector3(0, 0, 0), new Vector3(1, -1, 1));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -1, 0), new Vector3(0, 1, 0));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -2, 0), new Vector3(0, 1, 0));
+ assertFalse(pickRay.intersectsPlane(plane, intersectionPoint));
+
+ pickRay = new Ray3(new Vector3(0, -3, 0), new Vector3(0, 1, 0));
+ assertTrue(pickRay.intersectsPlane(plane, null));
+ }
+
+ @Test
+ public void testIntersectsQuad() {
+ final Vector3 v0 = new Vector3(0, 0, 0);
+ final Vector3 v1 = new Vector3(5, 0, 0);
+ final Vector3 v2 = new Vector3(5, 5, 0);
+ final Vector3 v3 = new Vector3(0, 5, 0);
+
+ Vector3 intersectionPoint = null;
+
+ // inside quad
+ final ReadOnlyRay3 pickRayA = new Ray3(new Vector3(2, 2, 10), new Vector3(0, 0, -1));
+ final ReadOnlyRay3 pickRayB = new Ray3(new Vector3(2, 4, 10), new Vector3(0, 0, -1));
+ assertTrue(pickRayA.intersectsQuad(v0, v1, v2, v3, intersectionPoint));
+ assertTrue(pickRayB.intersectsQuad(v0, v1, v2, v3, intersectionPoint));
+
+ // inside quad
+ final Ray3 pickRay2 = new Ray3(new Vector3(-1, 0, 10), new Vector3(0, 0, -1));
+ assertFalse(pickRay2.intersectsQuad(v0, v1, v2, v3, intersectionPoint));
+
+ // test distance
+ assertTrue(10.0 == pickRayA.getDistanceToPrimitive(new Vector3[] { v0, v1, v2, v3 }));
+ assertTrue(Double.POSITIVE_INFINITY == pickRay2.getDistanceToPrimitive(new Vector3[] { v0, v1, v2, v3 }));
+
+ // test unsupported pick
+ assertFalse(pickRay2.intersects(new Vector3[] { v0, v1 }, null));
+
+ // test intersect planar
+ assertFalse(new Ray3(new Vector3(0, 0, -1), Vector3.UNIT_Y).intersectsQuadPlanar(v0, v1, v2, v3,
+ intersectionPoint));
+ intersectionPoint = new Vector3();
+ assertTrue(pickRayA.intersectsQuadPlanar(v0, v1, v2, v3, intersectionPoint));
+ assertTrue(pickRayB.intersectsQuadPlanar(v0, v1, v2, v3, intersectionPoint));
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle2.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle2.java new file mode 100644 index 0000000..74cb987 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle2.java @@ -0,0 +1,120 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestRectangle2 {
+ @Test
+ public void testGetSet() {
+ final Rectangle2 rect1 = new Rectangle2();
+ assertTrue(0 == rect1.getX());
+ assertTrue(0 == rect1.getY());
+ assertTrue(0 == rect1.getWidth());
+ assertTrue(0 == rect1.getHeight());
+
+ final Rectangle2 rect2 = new Rectangle2(2, 3, 4, 5);
+ assertTrue(2 == rect2.getX());
+ assertTrue(3 == rect2.getY());
+ assertTrue(4 == rect2.getWidth());
+ assertTrue(5 == rect2.getHeight());
+
+ rect1.set(rect2);
+ assertTrue(2 == rect1.getX());
+ assertTrue(3 == rect1.getY());
+ assertTrue(4 == rect1.getWidth());
+ assertTrue(5 == rect1.getHeight());
+
+ final Rectangle2 rect3 = new Rectangle2(rect1);
+ assertTrue(2 == rect3.getX());
+ assertTrue(3 == rect3.getY());
+ assertTrue(4 == rect3.getWidth());
+ assertTrue(5 == rect3.getHeight());
+
+ rect1.set(5, 0, 10, 15);
+ assertTrue(5 == rect1.getX());
+ assertTrue(0 == rect1.getY());
+ assertTrue(10 == rect1.getWidth());
+ assertTrue(15 == rect1.getHeight());
+
+ rect1.setX(4);
+ assertTrue(4 == rect1.getX());
+ rect1.setY(42);
+ assertTrue(42 == rect1.getY());
+ rect1.setWidth(50);
+ assertTrue(50 == rect1.getWidth());
+ rect1.setHeight(100);
+ assertTrue(100 == rect1.getHeight());
+ }
+
+ @Test
+ public void testClone() {
+ final Rectangle2 rect1 = new Rectangle2();
+ final Rectangle2 rect2 = rect1.clone();
+ assertEquals(rect1, rect2);
+ assertNotSame(rect1, rect2);
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Rectangle2 rect1 = new Rectangle2(1, 2, 0, 0);
+ final Rectangle2 rect2 = new Rectangle2(1, 2, 0, 0);
+ final Rectangle2 rect3 = new Rectangle2(2, 2, 0, 0);
+
+ assertTrue(rect1.hashCode() == rect2.hashCode());
+ assertTrue(rect1.hashCode() != rect3.hashCode());
+ }
+
+ @Test
+ public void testEquals() {
+ final Rectangle2 rect1 = new Rectangle2(1, 2, 3, 4);
+ final Rectangle2 rect2 = new Rectangle2(1, 0, 0, 0);
+ final Rectangle2 rect3 = new Rectangle2(1, 2, 0, 0);
+ final Rectangle2 rect4 = new Rectangle2(1, 2, 3, 0);
+ final Rectangle2 rect5 = new Rectangle2(1, 2, 3, 4);
+
+ // couple of equals validity tests
+ assertEquals(rect1, rect1);
+ assertFalse(rect1.equals(null));
+ assertFalse(rect1.equals(new Vector2()));
+
+ assertFalse(rect1.equals(rect2));
+ assertFalse(rect1.equals(rect3));
+ assertFalse(rect1.equals(rect4));
+ assertTrue(rect1.equals(rect5));
+ }
+
+ @Test
+ public void testIntersect() {
+ final Rectangle2 rect1 = new Rectangle2(0, 0, 10, 10);
+ final Rectangle2 rect2 = new Rectangle2(5, 5, 10, 10);
+
+ Rectangle2 intersection = rect1.intersect(rect2, Rectangle2.fetchTempInstance());
+ assertTrue(5 == intersection.getX());
+ assertTrue(5 == intersection.getY());
+ assertTrue(5 == intersection.getWidth());
+ assertTrue(5 == intersection.getHeight());
+ Rectangle2.releaseTempInstance(intersection);
+ assertNotNull(rect1.intersect(rect2, null));
+
+ intersection = Rectangle2.intersect(rect1, rect2, Rectangle2.fetchTempInstance());
+ assertTrue(5 == intersection.getX());
+ assertTrue(5 == intersection.getY());
+ assertTrue(5 == intersection.getWidth());
+ assertTrue(5 == intersection.getHeight());
+ Rectangle2.releaseTempInstance(intersection);
+ assertNotNull(Rectangle2.intersect(rect1, rect2, null));
+
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle3.java new file mode 100644 index 0000000..0a98953 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestRectangle3.java @@ -0,0 +1,102 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestRectangle3 {
+
+ @Test
+ public void testGetSet() {
+ final Rectangle3 rect = new Rectangle3();
+ assertEquals(Vector3.ZERO, rect.getA());
+ assertEquals(Vector3.ZERO, rect.getB());
+ assertEquals(Vector3.ZERO, rect.getC());
+
+ rect.setA(Vector3.ONE);
+ rect.setB(Vector3.UNIT_X);
+ rect.setC(Vector3.UNIT_Z);
+ assertEquals(Vector3.ONE, rect.getA());
+ assertEquals(Vector3.UNIT_X, rect.getB());
+ assertEquals(Vector3.UNIT_Z, rect.getC());
+
+ final Rectangle3 rect2 = new Rectangle3(rect);
+ assertEquals(Vector3.ONE, rect2.getA());
+ assertEquals(Vector3.UNIT_X, rect2.getB());
+ assertEquals(Vector3.UNIT_Z, rect2.getC());
+
+ final Rectangle3 rect3 = new Rectangle3(Vector3.NEG_ONE, Vector3.UNIT_Z, Vector3.NEG_UNIT_Y);
+ assertEquals(Vector3.NEG_ONE, rect3.getA());
+ assertEquals(Vector3.UNIT_Z, rect3.getB());
+ assertEquals(Vector3.NEG_UNIT_Y, rect3.getC());
+ }
+
+ @Test
+ public void testEquals() {
+ // couple of equals validity tests
+ final Rectangle3 rect1 = new Rectangle3();
+ assertEquals(rect1, rect1);
+ assertFalse(rect1.equals(null));
+ assertFalse(rect1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Rectangle3 rect2 = Rectangle3.fetchTempInstance();
+ rect2.set(rect1);
+ assertEquals(rect1, rect2);
+ assertNotSame(rect1, rect2);
+ Rectangle3.releaseTempInstance(rect2);
+
+ // cover more of equals
+ assertTrue(rect1.equals(new Rectangle3(Vector3.ZERO, Vector3.ZERO, Vector3.ZERO)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.ZERO, Vector3.ZERO, Vector3.UNIT_X)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_X)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.ZERO, Vector3.UNIT_X, Vector3.ZERO)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.UNIT_X, Vector3.ZERO, Vector3.ZERO)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.UNIT_X, Vector3.ZERO, Vector3.UNIT_X)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.UNIT_X, Vector3.UNIT_X, Vector3.ZERO)));
+ assertFalse(rect1.equals(new Rectangle3(Vector3.UNIT_X, Vector3.UNIT_X, Vector3.UNIT_X)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Rectangle3 rect1 = new Rectangle3(Vector3.ZERO, Vector3.UNIT_Y, Vector3.UNIT_X);
+ final Rectangle3 rect2 = new Rectangle3(Vector3.ZERO, Vector3.UNIT_Y, Vector3.UNIT_X);
+ final Rectangle3 rect3 = new Rectangle3(Vector3.ZERO, Vector3.UNIT_Y, Vector3.UNIT_Z);
+
+ assertTrue(rect1.hashCode() == rect2.hashCode());
+ assertTrue(rect1.hashCode() != rect3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final Rectangle3 rect1 = new Rectangle3();
+ final Rectangle3 rect2 = rect1.clone();
+ assertEquals(rect1, rect2);
+ assertNotSame(rect1, rect2);
+ }
+
+ @Test
+ public void testRandom() {
+ MathUtils.setRandomSeed(0);
+ final Rectangle3 rect1 = new Rectangle3();
+ final Vector3 store = rect1.random(null);
+ assertEquals(new Vector3(0.0, 0.0, 0.0), store);
+
+ rect1.setA(new Vector3(1, 0, 0));
+ rect1.setB(new Vector3(1, 1, 0));
+ rect1.setC(new Vector3(0, 1, 0));
+ rect1.random(store);
+ assertEquals(new Vector3(0.39365482330322266, 0.8468815684318542, 0.0), store);
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestRing.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestRing.java new file mode 100644 index 0000000..b6120ab --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestRing.java @@ -0,0 +1,113 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestRing {
+
+ @Test
+ public void testGetSet() {
+ final Ring ring = new Ring();
+ assertEquals(Vector3.ZERO, ring.getCenter());
+ assertEquals(Vector3.UNIT_Y, ring.getUp());
+ assertTrue(ring.getInnerRadius() == 0.0);
+ assertTrue(ring.getOuterRadius() == 1.0);
+
+ ring.setCenter(Vector3.ONE);
+ ring.setUp(Vector3.UNIT_X);
+ ring.setInnerRadius(1.5);
+ ring.setOuterRadius(3.0);
+ assertEquals(Vector3.ONE, ring.getCenter());
+ assertEquals(Vector3.UNIT_X, ring.getUp());
+ assertTrue(ring.getInnerRadius() == 1.5);
+ assertTrue(ring.getOuterRadius() == 3.0);
+
+ final Ring ring2 = new Ring(ring);
+ assertEquals(Vector3.ONE, ring2.getCenter());
+ assertEquals(Vector3.UNIT_X, ring2.getUp());
+ assertTrue(ring2.getInnerRadius() == 1.5);
+ assertTrue(ring2.getOuterRadius() == 3.0);
+
+ final Ring ring3 = new Ring(Vector3.NEG_ONE, Vector3.UNIT_Z, 12.0, 42.0);
+ assertEquals(Vector3.NEG_ONE, ring3.getCenter());
+ assertEquals(Vector3.UNIT_Z, ring3.getUp());
+ assertTrue(ring3.getInnerRadius() == 12.0);
+ assertTrue(ring3.getOuterRadius() == 42.0);
+ }
+
+ @Test
+ public void testValid() {
+ assertTrue(Ring.isValid(new Ring()));
+ assertFalse(Ring.isValid(null));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, Double.NaN)));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, Double.POSITIVE_INFINITY)));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, Vector3.UNIT_Y, Double.NaN, 1.0)));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, Vector3.UNIT_Y, Double.NEGATIVE_INFINITY, 1.0)));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, new Vector3(Double.NaN, 0, 0), 0.0, 1.0)));
+ assertFalse(Ring.isValid(new Ring(Vector3.ZERO, new Vector3(Double.POSITIVE_INFINITY, 0, 0), 0.0, 1.0)));
+ assertFalse(Ring.isValid(new Ring(new Vector3(Double.NaN, 0, 0), Vector3.UNIT_Y, 0.0, 1.0)));
+ assertFalse(Ring.isValid(new Ring(new Vector3(Double.NEGATIVE_INFINITY, 0, 0), Vector3.UNIT_Y, 0.0, 1.0)));
+
+ // couple of equals validity tests
+ final Ring ring1 = new Ring();
+ assertEquals(ring1, ring1);
+ assertFalse(ring1.equals(null));
+ assertFalse(ring1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Ring ring2 = Ring.fetchTempInstance();
+ ring2.set(ring1);
+ assertEquals(ring1, ring2);
+ assertNotSame(ring1, ring2);
+ Ring.releaseTempInstance(ring2);
+
+ // cover more of equals
+ assertFalse(ring1.equals(new Ring(Vector3.UNIT_X, Vector3.UNIT_X, -1.0, 2.0)));
+ assertFalse(ring1.equals(new Ring(Vector3.UNIT_X, Vector3.UNIT_X, -1.0, 1.0)));
+ assertFalse(ring1.equals(new Ring(Vector3.UNIT_X, Vector3.UNIT_X, 0.0, 1.0)));
+ assertFalse(ring1.equals(new Ring(Vector3.UNIT_X, Vector3.UNIT_Y, 0.0, 1.0)));
+ assertTrue(ring1.equals(new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, 1.0)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Ring ring1 = new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, 2.0);
+ final Ring ring2 = new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, 2.0);
+ final Ring ring3 = new Ring(Vector3.ZERO, Vector3.UNIT_Y, 0.0, 3.0);
+
+ assertTrue(ring1.hashCode() == ring2.hashCode());
+ assertTrue(ring1.hashCode() != ring3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final Ring ring1 = new Ring();
+ final Ring ring2 = ring1.clone();
+ assertEquals(ring1, ring2);
+ assertNotSame(ring1, ring2);
+ }
+
+ @Test
+ public void testRandom() {
+ MathUtils.setRandomSeed(0);
+ final Ring ring1 = new Ring();
+ final Vector3 store = ring1.random(null);
+ assertEquals(new Vector3(0.7454530390475868, 0.0, -0.4186496466746111), store);
+
+ ring1.setUp(Vector3.UNIT_X);
+ ring1.random(store);
+ assertEquals(new Vector3(0.0, 0.30386186434027496, -0.3849731927481824), store);
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestTransform.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestTransform.java new file mode 100644 index 0000000..310e70f --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestTransform.java @@ -0,0 +1,424 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import java.nio.DoubleBuffer;
+import java.nio.FloatBuffer;
+
+import org.junit.Test;
+
+public class TestTransform {
+
+ @Test
+ public void testGetSet() {
+ final Transform trans = new Transform();
+ assertEquals(Transform.IDENTITY, trans);
+
+ final Transform immutable = new Transform(new Matrix3().applyRotationX(MathUtils.QUARTER_PI), new Vector3(0,
+ -1, -2), new Vector3(1, 2, 3), true, true, true);
+ assertTrue(true == immutable.isIdentity());
+ assertTrue(true == immutable.isRotationMatrix());
+ assertTrue(true == immutable.isUniformScale());
+ assertEquals(new Matrix3().applyRotationX(MathUtils.QUARTER_PI), immutable.getMatrix());
+ assertEquals(new Vector3(0, -1, -2), immutable.getScale());
+ assertEquals(new Vector3(1, 2, 3), immutable.getTranslation());
+
+ final Transform trans2 = new Transform(immutable);
+ assertEquals(immutable, trans2);
+ trans2.updateFlags(false);
+
+ trans.set(immutable);
+ assertEquals(Transform.IDENTITY, trans); // because of shortcut flags.
+
+ trans.set(trans2);
+ assertEquals(trans2, trans);
+
+ trans.setIdentity();
+ assertEquals(Transform.IDENTITY, trans);
+
+ final double a = MathUtils.QUARTER_PI;
+ trans.setRotation(new Quaternion().fromAngleAxis(a, Vector3.UNIT_Y));
+
+ assertEquals(new Matrix3( //
+ Math.cos(a), 0, Math.sin(a), //
+ 0, 1, 0, //
+ -Math.sin(a), 0, Math.cos(a)), trans.getMatrix());
+
+ trans2.setRotation(new Matrix3().fromAngleAxis(a, Vector3.UNIT_Y));
+ assertEquals(trans.getMatrix(), trans2.getMatrix());
+
+ trans.setScale(1.0);
+ assertEquals(Vector3.ONE, trans.getScale());
+
+ trans.setScale(new Vector3(1, 2, 3));
+ assertEquals(new Vector3(1, 2, 3), trans.getScale());
+
+ trans.setScale(-1, 5, -3);
+ assertEquals(new Vector3(-1, 5, -3), trans.getScale());
+
+ trans.setTranslation(new Vector3(10, 20, 30));
+ assertEquals(new Vector3(10, 20, 30), trans.getTranslation());
+
+ trans.setTranslation(-10, 50, -30);
+ assertEquals(new Vector3(-10, 50, -30), trans.getTranslation());
+
+ trans.setIdentity();
+ trans.setRotation(new Matrix3().fromAngleAxis(a, Vector3.UNIT_Y));
+ trans.setScale(2, 3, 4);
+ trans.setTranslation(5, 10, 15);
+
+ final Matrix4 mat4 = trans.getHomogeneousMatrix(null);
+ assertEquals(new Matrix4( //
+ 2 * Math.cos(a), 2 * 0, 2 * Math.sin(a), 5, //
+ 3 * 0, 3 * 1, 3 * 0, 10, //
+ 4 * -Math.sin(a), 4 * 0, 4 * Math.cos(a), 15, //
+ 0, 0, 0, 1), mat4);
+
+ trans2.fromHomogeneousMatrix(mat4);
+ trans2.getHomogeneousMatrix(mat4);
+ assertEquals(new Matrix4( //
+ 2 * Math.cos(a), 2 * 0, 2 * Math.sin(a), 5, //
+ 3 * 0, 3 * 1, 3 * 0, 10, //
+ 4 * -Math.sin(a), 4 * 0, 4 * Math.cos(a), 15, //
+ 0, 0, 0, 1), mat4);
+
+ trans.setIdentity();
+ trans.setRotation(new Matrix3(0, 1, 2, 3, 4, 5, 6, 7, 8));
+ trans.setTranslation(10, 11, 12);
+ trans.getHomogeneousMatrix(mat4);
+ assertEquals(new Matrix4( //
+ 0, 1, 2, 10, //
+ 3, 4, 5, 11, //
+ 6, 7, 8, 12, //
+ 0, 0, 0, 1), mat4);
+
+ }
+
+ @Test(expected = TransformException.class)
+ public void testFailScale1A() {
+ final Transform trans = new Transform(new Matrix3(), new Vector3(), new Vector3(), false, false, false);
+ trans.setScale(Vector3.ONE);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testFailScale1B() {
+ final Transform trans = new Transform();
+ trans.setScale(Vector3.ZERO);
+ }
+
+ @Test(expected = TransformException.class)
+ public void testFailScale2A() {
+ final Transform trans = new Transform(new Matrix3(), new Vector3(), new Vector3(), false, false, false);
+ trans.setScale(1, 1, 1);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testFailScale2B() {
+ final Transform trans = new Transform();
+ trans.setScale(0, 0, 0);
+ }
+
+ @Test(expected = TransformException.class)
+ public void testFailScale3A() {
+ final Transform trans = new Transform(new Matrix3(), new Vector3(), new Vector3(), false, false, false);
+ trans.setScale(1);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testFailScale3B() {
+ final Transform trans = new Transform();
+ trans.setScale(0);
+ }
+
+ @Test
+ public void testTranslate() {
+ final Transform trans = new Transform();
+ trans.translate(1, 3, 5);
+ assertEquals(new Vector3(1, 3, 5), trans.getTranslation());
+ trans.translate(trans.getTranslation().negate(null));
+ assertEquals(Vector3.ZERO, trans.getTranslation());
+
+ trans.translate(new Vector3(1, 3, 5));
+ assertEquals(new Vector3(1, 3, 5), trans.getTranslation());
+ trans.translate(-1, -3, -5);
+ assertEquals(Vector3.ZERO, trans.getTranslation());
+ }
+
+ @Test
+ public void testApplyVector3() {
+ final Transform trans = new Transform().setRotation(new Matrix3().applyRotationX(MathUtils.HALF_PI)).translate(
+ 1, 2, 3);
+ final Vector3 vec3 = new Vector3(0, 1, 0);
+
+ final Vector3 result = trans.applyForward(vec3, null);
+ assertTrue(Math.abs(new Vector3(1, 2, 4).distance(result)) <= MathUtils.EPSILON);
+ trans.applyForward(vec3, result);
+ assertTrue(Math.abs(new Vector3(1, 2, 4).distance(result)) <= MathUtils.EPSILON);
+ trans.applyForward(vec3);
+ assertTrue(Math.abs(new Vector3(1, 2, 4).distance(vec3)) <= MathUtils.EPSILON);
+
+ vec3.set(0, 1, 1);
+ final Vector3 result2 = trans.applyForwardVector(vec3, null);
+ assertTrue(Math.abs(new Vector3(0, -1, 1).distance(result2)) <= MathUtils.EPSILON);
+ trans.applyForwardVector(vec3, result2);
+ assertTrue(Math.abs(new Vector3(0, -1, 1).distance(result2)) <= MathUtils.EPSILON);
+ trans.applyForwardVector(vec3);
+ assertTrue(Math.abs(new Vector3(0, -1, 1).distance(vec3)) <= MathUtils.EPSILON);
+
+ vec3.set(0, 1, 0);
+ final Vector3 result3 = trans.applyInverse(vec3, null);
+ assertTrue(Math.abs(new Vector3(-1, -3, 1).distance(result3)) <= MathUtils.EPSILON);
+ trans.applyInverse(vec3, result3);
+ assertTrue(Math.abs(new Vector3(-1, -3, 1).distance(result3)) <= MathUtils.EPSILON);
+ trans.applyInverse(vec3);
+ assertTrue(Math.abs(new Vector3(-1, -3, 1).distance(vec3)) <= MathUtils.EPSILON);
+
+ vec3.set(0, 1, 1);
+ final Vector3 result4 = trans.applyInverseVector(vec3, null);
+ assertTrue(Math.abs(new Vector3(0, 1, -1).distance(result4)) <= MathUtils.EPSILON);
+ trans.applyInverseVector(vec3, result4);
+ assertTrue(Math.abs(new Vector3(0, 1, -1).distance(result4)) <= MathUtils.EPSILON);
+ trans.applyInverseVector(vec3);
+ assertTrue(Math.abs(new Vector3(0, 1, -1).distance(vec3)) <= MathUtils.EPSILON);
+
+ trans.setRotation(new Matrix3().applyRotationY(MathUtils.PI)).translate(2, 3, -1);
+
+ vec3.set(1, 2, 3).normalizeLocal();
+ final Vector3 orig = new Vector3(vec3);
+ trans.applyForward(vec3);
+ trans.applyInverse(vec3);
+ assertTrue(Math.abs(orig.distance(vec3)) <= 10 * MathUtils.EPSILON); // accumulated error
+
+ vec3.set(orig);
+ trans.applyForwardVector(vec3);
+ trans.applyInverseVector(vec3);
+ assertTrue(Math.abs(orig.distance(vec3)) <= 10 * MathUtils.EPSILON); // accumulated error
+
+ vec3.set(orig);
+ trans.setIdentity();
+ trans.applyForward(vec3);
+ assertEquals(orig, vec3);
+ trans.applyForwardVector(vec3);
+ assertEquals(orig, vec3);
+ trans.applyInverse(vec3);
+ assertEquals(orig, vec3);
+ trans.applyInverseVector(vec3);
+ assertEquals(orig, vec3);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testApplyFail1() {
+ final Transform trans = new Transform();
+ trans.applyForward(null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testApplyFail2() {
+ final Transform trans = new Transform();
+ trans.applyForwardVector(null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testApplyFail3() {
+ final Transform trans = new Transform();
+ trans.applyInverse(null);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testApplyFail4() {
+ final Transform trans = new Transform();
+ trans.applyInverseVector(null);
+ }
+
+ @Test
+ public void testMultiply() {
+ final Transform trans1 = new Transform();
+ final Transform trans2 = new Transform();
+ assertEquals(Transform.IDENTITY, trans1.multiply(trans2, null));
+
+ trans1.setTranslation(1, 2, 3);
+ final Transform trans3 = trans1.multiply(trans2, null);
+ assertEquals(trans1, trans3);
+
+ trans2.setTranslation(-1, -2, -3);
+ trans1.multiply(trans2, trans3);
+ assertEquals(Transform.IDENTITY, trans3);
+ assertTrue(trans3.isRotationMatrix());
+ assertTrue(trans3.isIdentity());
+ assertTrue(trans3.isUniformScale());
+
+ trans2.setScale(1, 2, 1);
+ trans1.multiply(trans2, trans3);
+ assertEquals(new Transform().setScale(1, 2, 1), trans3);
+ assertTrue(trans3.isRotationMatrix());
+ assertFalse(trans3.isIdentity());
+ assertFalse(trans3.isUniformScale());
+
+ trans1.setScale(1, 2, 1);
+ trans1.multiply(trans2, trans3);
+ assertEquals(new Transform().setRotation(new Matrix3(1, 0, 0, 0, 4, 0, 0, 0, 1)).setTranslation(0, -2, 0),
+ trans3);
+ assertFalse(trans3.isRotationMatrix());
+ assertFalse(trans3.isIdentity());
+ assertFalse(trans3.isUniformScale());
+ }
+
+ @Test
+ public void testInvert() {
+ final Transform trans1 = new Transform();
+ trans1.setRotation(new Matrix3().applyRotationZ(3 * MathUtils.QUARTER_PI));
+ final Transform trans2 = trans1.invert(null);
+ assertEquals(Transform.IDENTITY, trans1.multiply(trans2, null));
+
+ trans1.setIdentity().invert(trans1);
+ assertEquals(Transform.IDENTITY, trans1);
+ }
+
+ @Test
+ public void testClone() {
+ final Transform trans1 = new Transform();
+ final Transform trans2 = trans1.clone();
+ assertEquals(trans1, trans2);
+ assertNotSame(trans1, trans2);
+ }
+
+ @Test
+ public void testValid() {
+ final Transform trans = new Transform();
+ assertTrue(Transform.isValid(trans));
+ trans.setIdentity();
+ trans.setRotation(new Matrix3(Double.NaN, 0, 0, 0, 0, 0, 0, 0, 0));
+ assertFalse(Transform.isValid(trans));
+ trans.setIdentity();
+ trans.setScale(Double.NaN, 0, 0);
+ assertFalse(Transform.isValid(trans));
+ trans.setScale(Double.NaN);
+ assertFalse(Transform.isValid(trans));
+ trans.setIdentity();
+ trans.setTranslation(Double.NaN, 0, 0);
+ assertFalse(Transform.isValid(trans));
+
+ trans.setIdentity();
+ assertTrue(Transform.isValid(trans));
+
+ assertFalse(Transform.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(trans, trans);
+ assertTrue(trans.strictEquals(trans));
+ assertFalse(trans.equals(null));
+ assertFalse(trans.strictEquals(null));
+ assertFalse(trans.equals(new Vector2()));
+ assertFalse(trans.strictEquals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Transform transTemp = Transform.fetchTempInstance();
+ transTemp.set(trans);
+ assertEquals(trans, transTemp);
+ assertNotSame(trans, transTemp);
+ Transform.releaseTempInstance(transTemp);
+
+ // cover more of equals
+ trans.setScale(1, 2, 3);
+ trans.setRotation(new Matrix3(0, 1, 2, 3, 4, 5, 6, 7, 8));
+ trans.setTranslation(1, 2, 3);
+ final Transform comp = new Transform();
+ final Matrix3 mat3 = new Matrix3(-1, -1, -1, -1, -1, -1, -1, -1, -1);
+ comp.setScale(-1, -1, -1);
+ comp.setRotation(mat3);
+ comp.setTranslation(-1, -1, -1);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ for (int i = 0; i < 8; i++) {
+ mat3.setValue(i / 3, i % 3, i);
+ comp.setRotation(mat3);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ }
+ // test translation
+ trans.setRotation(Matrix3.IDENTITY);
+ comp.setRotation(Matrix3.IDENTITY);
+ comp.setTranslation(1, -1, -1);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ comp.setTranslation(1, 2, -1);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ comp.setTranslation(1, 2, 3);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+
+ // test scale
+ comp.setScale(1, -1, -1);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ comp.setScale(1, 2, -1);
+ assertFalse(trans.equals(comp));
+ assertFalse(trans.strictEquals(comp));
+ comp.setScale(1, 2, 3);
+ assertTrue(trans.equals(comp));
+ assertTrue(trans.strictEquals(comp));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Transform trans1 = new Transform().setTranslation(1, 2, 3);
+ final Transform trans2 = new Transform().setTranslation(1, 2, 3);
+ final Transform trans3 = new Transform().setTranslation(1, 2, 0);
+
+ assertTrue(trans1.hashCode() == trans2.hashCode());
+ assertTrue(trans1.hashCode() != trans3.hashCode());
+ }
+
+ @Test
+ public void testGLApplyMatrix() {
+ final Transform trans = new Transform();
+
+ // non-rotational
+ trans.setRotation(new Matrix3(0, 1, 2, 3, 4, 5, 6, 7, 8));
+ trans.setTranslation(10, 11, 12);
+ final DoubleBuffer db = DoubleBuffer.allocate(16);
+ trans.getGLApplyMatrix(db);
+ for (final double val : new double[] { 0, 3, 6, 0, 1, 4, 7, 0, 2, 5, 8, 0, 10, 11, 12, 1 }) {
+ assertTrue(val == db.get());
+ }
+ final FloatBuffer fb = FloatBuffer.allocate(16);
+ trans.getGLApplyMatrix(fb);
+ for (final float val : new float[] { 0, 3, 6, 0, 1, 4, 7, 0, 2, 5, 8, 0, 10, 11, 12, 1 }) {
+ assertTrue(val == fb.get());
+ }
+
+ // rotational
+ final double a = MathUtils.QUARTER_PI;
+ trans.setRotation(new Matrix3().applyRotationY(a));
+ trans.setTranslation(10, 11, 12);
+ trans.setScale(2, 3, 4);
+ db.rewind();
+ trans.getGLApplyMatrix(db);
+ for (final double val : new double[] { 2 * Math.cos(a), 2 * 0, 2 * -Math.sin(a), 0, //
+ 3 * 0, 3 * 1, 3 * 0, 0, //
+ 4 * Math.sin(a), 4 * 0, 4 * Math.cos(a), 0, //
+ 10, 11, 12, 1 }) {
+ assertTrue(val == db.get());
+ }
+ fb.rewind();
+ trans.getGLApplyMatrix(fb);
+ for (final float val : new float[] { (float) (2 * Math.cos(a)), 2 * 0, (float) (2 * -Math.sin(a)), 0, //
+ 3 * 0, 3 * 1, 3 * 0, 0, //
+ (float) (4 * Math.sin(a)), 4 * 0, (float) (4 * Math.cos(a)), 0, //
+ 10, 11, 12, 1 }) {
+ assertTrue(val == fb.get());
+ }
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestTriangle.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestTriangle.java new file mode 100644 index 0000000..b59bc37 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestTriangle.java @@ -0,0 +1,166 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestTriangle {
+
+ @Test
+ public void testGetSet() {
+ final Triangle tri1 = new Triangle();
+ assertEquals(Vector3.ZERO, tri1.getA());
+ assertEquals(Vector3.ZERO, tri1.getB());
+ assertEquals(Vector3.ZERO, tri1.getC());
+ assertTrue(tri1.getIndex() == 0);
+
+ tri1.setA(Vector3.NEG_ONE);
+ tri1.setB(Vector3.UNIT_X);
+ tri1.setC(Vector3.UNIT_Z);
+ tri1.setIndex(1);
+ assertEquals(Vector3.NEG_ONE, tri1.getA());
+ assertEquals(Vector3.UNIT_X, tri1.getB());
+ assertEquals(Vector3.UNIT_Z, tri1.getC());
+ assertTrue(tri1.getIndex() == 1);
+
+ final Triangle tri2 = new Triangle(tri1);
+ assertEquals(Vector3.NEG_ONE, tri2.getA());
+ assertEquals(Vector3.UNIT_X, tri2.getB());
+ assertEquals(Vector3.UNIT_Z, tri2.getC());
+ assertTrue(tri2.getIndex() == 1);
+
+ final Triangle tri3 = new Triangle(Vector3.ONE, Vector3.UNIT_Y, Vector3.NEG_ONE);
+ assertEquals(Vector3.ONE, tri3.getA());
+ assertEquals(Vector3.UNIT_Y, tri3.getB());
+ assertEquals(Vector3.NEG_ONE, tri3.getC());
+ assertTrue(tri3.getIndex() == 0);
+
+ final Triangle tri4 = new Triangle(Vector3.ONE, Vector3.UNIT_Y, Vector3.NEG_ONE, 42);
+ assertEquals(Vector3.ONE, tri4.getA());
+ assertEquals(Vector3.UNIT_Y, tri4.getB());
+ assertEquals(Vector3.NEG_ONE, tri4.getC());
+ assertTrue(tri4.getIndex() == 42);
+
+ tri2.set(0, Vector3.UNIT_X);
+ tri2.set(1, Vector3.UNIT_Y);
+ tri2.set(2, Vector3.UNIT_Z);
+
+ // catch a few expected exceptions
+ try {
+ tri2.get(3);
+ fail("get(3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ tri2.get(-1);
+ fail("get(-1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ tri2.set(-1, Vector3.ZERO);
+ fail("set(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ tri2.set(3, Vector3.ZERO);
+ fail("set(3, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+
+ // shouldn't have changed
+ assertEquals(Vector3.UNIT_X, tri2.get(0));
+ assertEquals(Vector3.UNIT_Y, tri2.get(1));
+ assertEquals(Vector3.UNIT_Z, tri2.get(2));
+ }
+
+ @Test
+ public void testEquals() {
+ // couple of equals validity tests
+ final Triangle tri1 = new Triangle();
+ assertEquals(tri1, tri1);
+ assertFalse(tri1.equals(null));
+ assertFalse(tri1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Triangle tri2 = Triangle.fetchTempInstance();
+ tri2.set(tri1);
+ assertEquals(tri1, tri2);
+ assertNotSame(tri1, tri2);
+ Triangle.releaseTempInstance(tri2);
+
+ // cover more of equals
+ assertTrue(tri1.equals(new Triangle(Vector3.ZERO, Vector3.ZERO, Vector3.ZERO)));
+ assertFalse(tri1.equals(new Triangle(Vector3.ZERO, Vector3.ZERO, Vector3.UNIT_X)));
+ assertFalse(tri1.equals(new Triangle(Vector3.ZERO, Vector3.UNIT_X, Vector3.UNIT_X)));
+ assertFalse(tri1.equals(new Triangle(Vector3.ZERO, Vector3.UNIT_X, Vector3.ZERO)));
+ assertFalse(tri1.equals(new Triangle(Vector3.UNIT_X, Vector3.ZERO, Vector3.ZERO)));
+ assertFalse(tri1.equals(new Triangle(Vector3.UNIT_X, Vector3.ZERO, Vector3.UNIT_X)));
+ assertFalse(tri1.equals(new Triangle(Vector3.UNIT_X, Vector3.UNIT_X, Vector3.ZERO)));
+ assertFalse(tri1.equals(new Triangle(Vector3.UNIT_X, Vector3.UNIT_X, Vector3.UNIT_X)));
+ }
+
+ @Test
+ public void testValid() {
+ final Triangle vec1 = new Triangle();
+ final Triangle vec2 = new Triangle(new Vector3(0, 0, Double.NaN), Vector3.ZERO, Vector3.ZERO);
+ final Triangle vec3 = new Triangle(Vector3.ZERO, new Vector3(0, 0, Double.NaN), Vector3.ZERO);
+ final Triangle vec4 = new Triangle(Vector3.ZERO, Vector3.ZERO, new Vector3(0, 0, Double.NaN));
+
+ assertTrue(Triangle.isValid(vec1));
+ assertFalse(Triangle.isValid(vec2));
+ assertFalse(Triangle.isValid(vec3));
+ assertFalse(Triangle.isValid(vec4));
+
+ vec4.setC(Vector3.ZERO);
+ assertTrue(Triangle.isValid(vec4));
+
+ assertFalse(Triangle.isValid(null));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Triangle tri1 = new Triangle(Vector3.ZERO, Vector3.UNIT_Y, Vector3.NEG_ONE, 1);
+ final Triangle tri2 = new Triangle(Vector3.ZERO, Vector3.UNIT_Y, Vector3.NEG_ONE, 1);
+ final Triangle tri3 = new Triangle(Vector3.ZERO, Vector3.UNIT_Z, Vector3.NEG_ONE, 2);
+
+ assertTrue(tri1.hashCode() == tri2.hashCode());
+ assertTrue(tri1.hashCode() != tri3.hashCode());
+ }
+
+ @Test
+ public void testClone() {
+ final Triangle tri1 = new Triangle();
+ final Triangle tri2 = tri1.clone();
+ assertEquals(tri1, tri2);
+ assertNotSame(tri1, tri2);
+ }
+
+ @Test
+ public void testCenter() {
+ final Triangle tri1 = new Triangle(Vector3.ZERO, Vector3.UNIT_Y, Vector3.UNIT_X, 0);
+ assertEquals(new Vector3(1 / 3., 1 / 3., 0), tri1.getCenter()); // dirty
+ assertEquals(new Vector3(1 / 3., 1 / 3., 0), tri1.getCenter()); // clean
+ tri1.setA(Vector3.ONE);
+ assertEquals(new Vector3(2 / 3., 2 / 3., 1 / 3.), tri1.getCenter()); // dirty, but with existing center
+ }
+
+ @Test
+ public void testNormal() {
+ final Triangle tri1 = new Triangle(Vector3.ZERO, Vector3.UNIT_Y, Vector3.UNIT_X, 0);
+ assertEquals(new Vector3(0, 0, -1), tri1.getNormal()); // dirty
+ assertEquals(new Vector3(0, 0, -1), tri1.getNormal()); // clean
+ tri1.setB(Vector3.UNIT_Z);
+ assertEquals(new Vector3(0, 1, 0), tri1.getNormal()); // dirty, but with existing normal
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestValidatingTransform.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestValidatingTransform.java new file mode 100644 index 0000000..7dab55d --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestValidatingTransform.java @@ -0,0 +1,151 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestValidatingTransform {
+
+ @Test
+ public void testConstructor() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ assertEquals(Transform.IDENTITY, vt1);
+
+ vt1.translate(0, 1, 2);
+ vt1.setRotation(new Matrix3().fromAngleAxis(Math.PI, Vector3.UNIT_X));
+
+ final ValidatingTransform vt2 = new ValidatingTransform(vt1);
+ assertEquals(vt1, vt2);
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void failConstructor() {
+ final Transform bad = new Transform();
+ bad.translate(Double.NaN, 1, 2);
+ new ValidatingTransform(Transform.IDENTITY); // good
+ new ValidatingTransform(bad); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetRotationReadOnlyMatrix3() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ final Matrix3 rotation = new Matrix3();
+ vt1.setRotation(rotation); // good
+ rotation.setM00(Double.NaN);
+ vt1.setRotation(rotation); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetRotationReadOnlyQuaternion() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ final Quaternion rotation = new Quaternion();
+ vt1.setRotation(rotation); // good
+ rotation.setX(Double.NaN);
+ vt1.setRotation(rotation); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetTranslationReadOnlyVector3() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setTranslation(new Vector3(0, 0, 1)); // good
+ vt1.setTranslation(new Vector3(0, 0, Double.NaN)); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetTranslationDoubleDoubleDouble() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setTranslation(0, 0, 1); // good
+ vt1.setTranslation(0, 0, Double.NaN); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetScaleReadOnlyVector3() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setScale(new Vector3(1, 1, 1)); // good
+ vt1.setScale(new Vector3(1, 1, Double.NaN)); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetScaleDoubleDoubleDouble() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setScale(0, 0, 1); // good
+ vt1.setScale(0, 0, Double.NaN); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSetScaleDouble() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setScale(1); // good
+ vt1.setScale(Double.NaN); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testSet() {
+ final Transform bad = new Transform();
+ bad.translate(Double.NaN, 1, 2);
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.set(Transform.IDENTITY); // good
+ vt1.set(bad); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testTranslateDoubleDoubleDouble() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.translate(1, 2, 3); // good
+ vt1.translate(0, 0, Double.NaN); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testTranslateReadOnlyVector3() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.translate(new Vector3(1, 2, 3)); // good
+ vt1.translate(new Vector3(0, 0, Double.NaN)); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testMultiply() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.multiply(Transform.IDENTITY, null); // good
+ final Transform bad = new Transform();
+ bad.translate(Double.NaN, 1, 2);
+ vt1.multiply(bad, null); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testInvert() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ vt1.setScale(2);
+ vt1.invert(null); // good
+ // a little chicanery to get around other checks.
+ ((Vector3) vt1.getScale()).setX(0);
+ vt1.invert(null); // bad
+ }
+
+ @Test(expected = InvalidTransformException.class)
+ public void testFromHomogeneousMatrix() {
+ final ValidatingTransform vt1 = new ValidatingTransform();
+ final Matrix4 matrix = new Matrix4();
+ vt1.fromHomogeneousMatrix(matrix); // good
+ matrix.setM00(Double.NaN);
+ vt1.fromHomogeneousMatrix(matrix); // bad
+ }
+
+ @Test
+ public void testClone() {
+ final ValidatingTransform trans1 = new ValidatingTransform();
+ final ValidatingTransform trans2 = trans1.clone();
+ assertEquals(trans1, trans2);
+ assertNotSame(trans1, trans2);
+ }
+
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestVector2.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector2.java new file mode 100644 index 0000000..6ba3b7c --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector2.java @@ -0,0 +1,376 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestVector2 {
+
+ @Test
+ public void testAdd() {
+ final Vector2 vec1 = new Vector2();
+ final Vector2 vec2 = new Vector2(Vector2.ONE);
+
+ vec1.addLocal(1, 2);
+ assertEquals(new Vector2(1, 2), vec1);
+ vec1.addLocal(-1, -2);
+ assertEquals(Vector2.ZERO, vec1);
+
+ vec1.zero();
+ vec1.addLocal(vec2);
+ assertEquals(Vector2.ONE, vec1);
+
+ vec1.zero();
+ final Vector2 vec3 = vec1.add(vec2, new Vector2());
+ assertEquals(Vector2.ZERO, vec1);
+ assertEquals(Vector2.ONE, vec3);
+
+ final Vector2 vec4 = vec1.add(1, 0, null);
+ assertEquals(Vector2.ZERO, vec1);
+ assertEquals(Vector2.UNIT_X, vec4);
+ }
+
+ @Test
+ public void testSubtract() {
+ final Vector2 vec1 = new Vector2();
+ final Vector2 vec2 = new Vector2(Vector2.ONE);
+
+ vec1.subtractLocal(1, 2);
+ assertEquals(new Vector2(-1, -2), vec1);
+ vec1.subtractLocal(-1, -2);
+ assertEquals(Vector2.ZERO, vec1);
+
+ vec1.zero();
+ vec1.subtractLocal(vec2);
+ assertEquals(Vector2.NEG_ONE, vec1);
+
+ vec1.zero();
+ final Vector2 vec3 = vec1.subtract(vec2, new Vector2());
+ assertEquals(Vector2.ZERO, vec1);
+ assertEquals(Vector2.NEG_ONE, vec3);
+
+ final Vector2 vec4 = vec1.subtract(1, 0, null);
+ assertEquals(Vector2.ZERO, vec1);
+ assertEquals(Vector2.NEG_UNIT_X, vec4);
+ }
+
+ @Test
+ public void testGetSet() {
+ final Vector2 vec1 = new Vector2();
+ vec1.setX(0);
+ assertTrue(vec1.getX() == 0.0);
+ vec1.setX(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.POSITIVE_INFINITY);
+ vec1.setX(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(0) == Double.NEGATIVE_INFINITY);
+
+ vec1.setY(0);
+ assertTrue(vec1.getY() == 0.0);
+ vec1.setY(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.POSITIVE_INFINITY);
+ vec1.setY(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(1) == Double.NEGATIVE_INFINITY);
+
+ vec1.set(Math.PI, Math.PI);
+ assertTrue(vec1.getXf() == (float) Math.PI);
+ assertTrue(vec1.getYf() == (float) Math.PI);
+
+ final Vector2 vec2 = new Vector2();
+ vec2.set(vec1);
+ assertEquals(vec1, vec2);
+
+ vec1.setValue(0, 0);
+ vec1.setValue(1, 0);
+ assertEquals(Vector2.ZERO, vec1);
+
+ // catch a few expected exceptions
+ try {
+ vec2.getValue(2);
+ fail("getValue(2) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.getValue(-1);
+ fail("getValue(-1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(-1, 0);
+ fail("setValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(2, 0);
+ fail("setValue(2, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered vec2
+ assertEquals(new Vector2(Math.PI, Math.PI), vec2);
+ }
+
+ @Test
+ public void testPolarAngle() {
+ final Vector2 vec1 = new Vector2();
+ assertTrue(0.0 == vec1.getPolarAngle());
+
+ vec1.set(1.0, 0.0); // 0
+ assertTrue(Math.abs(0 - vec1.getPolarAngle()) <= MathUtils.EPSILON);
+
+ vec1.set(0.0, 1.0); // -HALF_PI
+ assertTrue(Math.abs(-MathUtils.HALF_PI - vec1.getPolarAngle()) <= MathUtils.EPSILON);
+
+ vec1.set(-1.0, 0.0); // -PI
+ assertTrue(Math.abs(-MathUtils.PI - vec1.getPolarAngle()) <= MathUtils.EPSILON);
+
+ vec1.set(0, -1.0); // HALF_PI
+ assertTrue(Math.abs(MathUtils.HALF_PI - vec1.getPolarAngle()) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testToArray() {
+ final Vector2 vec1 = new Vector2();
+ vec1.set(Math.PI, Double.MAX_VALUE);
+ final double[] array = vec1.toArray(null);
+ final double[] array2 = vec1.toArray(new double[2]);
+ assertNotNull(array);
+ assertTrue(array.length == 2);
+ assertTrue(array[0] == Math.PI);
+ assertTrue(array[1] == Double.MAX_VALUE);
+ assertNotNull(array2);
+ assertTrue(array2.length == 2);
+ assertTrue(array2[0] == Math.PI);
+ assertTrue(array2[1] == Double.MAX_VALUE);
+
+ try {
+ vec1.toArray(new double[1]);
+ fail("toArray(d[1]) should have thrown ArrayIndexOutOfBoundsException.");
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ }
+ }
+
+ @Test
+ public void testMultiply() {
+ final Vector2 vec1 = new Vector2(1, -1);
+ final Vector2 vec2 = vec1.multiply(2.0, null);
+ final Vector2 vec2B = vec1.multiply(2.0, new Vector2());
+ assertEquals(new Vector2(2.0, -2.0), vec2);
+ assertEquals(new Vector2(2.0, -2.0), vec2B);
+
+ vec2.multiplyLocal(0.5);
+ assertEquals(new Vector2(1.0, -1.0), vec2);
+
+ final Vector2 vec3 = vec1.multiply(vec2, null);
+ final Vector2 vec3B = vec1.multiply(vec2, new Vector2());
+ assertEquals(Vector2.ONE, vec3);
+ assertEquals(Vector2.ONE, vec3B);
+
+ final Vector2 vec4 = vec1.multiply(2, 3, null);
+ final Vector2 vec4B = vec1.multiply(2, 3, new Vector2());
+ assertEquals(new Vector2(2, -3), vec4);
+ assertEquals(new Vector2(2, -3), vec4B);
+
+ vec1.multiplyLocal(0.5, 0.5);
+ assertEquals(new Vector2(0.5, -0.5), vec1);
+
+ vec1.multiplyLocal(vec2);
+ assertEquals(new Vector2(0.5, 0.5), vec1);
+ }
+
+ @Test
+ public void testDivide() {
+ final Vector2 vec1 = new Vector2(1, -1);
+ final Vector2 vec2 = vec1.divide(2.0, null);
+ final Vector2 vec2B = vec1.divide(2.0, new Vector2());
+ assertEquals(new Vector2(0.5, -0.5), vec2);
+ assertEquals(new Vector2(0.5, -0.5), vec2B);
+
+ vec2.divideLocal(0.5);
+ assertEquals(new Vector2(1.0, -1.0), vec2);
+
+ final Vector2 vec3 = vec1.divide(vec2, null);
+ final Vector2 vec3B = vec1.divide(vec2, new Vector2());
+ assertEquals(Vector2.ONE, vec3);
+ assertEquals(Vector2.ONE, vec3B);
+
+ final Vector2 vec4 = vec1.divide(2, 3, null);
+ final Vector2 vec4B = vec1.divide(2, 3, new Vector2());
+ assertEquals(new Vector2(1 / 2., -1 / 3.), vec4);
+ assertEquals(new Vector2(1 / 2., -1 / 3.), vec4B);
+
+ vec1.divideLocal(0.5, 0.5);
+ assertEquals(new Vector2(2, -2), vec1);
+
+ vec1.divideLocal(vec2);
+ assertEquals(new Vector2(2, 2), vec1);
+ }
+
+ @Test
+ public void testScaleAdd() {
+ final Vector2 vec1 = new Vector2(1, 1);
+ final Vector2 vec2 = vec1.scaleAdd(2.0, new Vector2(1, 2), null);
+ final Vector2 vec2B = vec1.scaleAdd(2.0, new Vector2(1, 2), new Vector2());
+ assertEquals(new Vector2(3.0, 4.0), vec2);
+ assertEquals(new Vector2(3.0, 4.0), vec2B);
+
+ vec1.scaleAddLocal(2.0, new Vector2(1, 2));
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNegate() {
+ final Vector2 vec1 = new Vector2(2, 1);
+ final Vector2 vec2 = vec1.negate(null);
+ assertEquals(new Vector2(-2, -1), vec2);
+
+ vec1.negateLocal();
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNormalize() {
+ final Vector2 vec1 = new Vector2(2, 1);
+ assertTrue(vec1.length() == Math.sqrt(5));
+
+ final Vector2 vec2 = vec1.normalize(null);
+ final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1);
+ assertEquals(new Vector2(2 * invLength, 1 * invLength), vec2);
+
+ vec1.normalizeLocal();
+ assertEquals(new Vector2(2 * invLength, 1 * invLength), vec1);
+
+ vec1.zero();
+ vec1.normalize(vec2);
+ assertEquals(vec1, vec2);
+
+ // ensure no exception thrown
+ vec1.normalizeLocal();
+ vec1.normalize(null);
+ }
+
+ @Test
+ public void testDistance() {
+ final Vector2 vec1 = new Vector2(0, 0);
+ assertTrue(3.0 == vec1.distance(0, 3));
+ assertTrue(4.0 == vec1.distance(4, 0));
+
+ final Vector2 vec2 = new Vector2(1, 1);
+ assertTrue(Math.sqrt(2) == vec1.distance(vec2));
+ }
+
+ @Test
+ public void testLerp() {
+ final Vector2 vec1 = new Vector2(8, 3);
+ final Vector2 vec2 = new Vector2(2, 1);
+ assertEquals(new Vector2(5, 2), vec1.lerp(vec2, 0.5, null));
+ assertEquals(new Vector2(5, 2), vec1.lerp(vec2, 0.5, new Vector2()));
+ assertEquals(new Vector2(5, 2), Vector2.lerp(vec1, vec2, 0.5, null));
+ assertEquals(new Vector2(5, 2), Vector2.lerp(vec1, vec2, 0.5, new Vector2()));
+
+ vec1.set(14, 5);
+ vec1.lerpLocal(vec2, 0.25);
+ assertEquals(new Vector2(11, 4), vec1);
+
+ vec1.set(15, 7);
+ final Vector2 vec3 = new Vector2(-1, -1);
+ vec3.lerpLocal(vec1, vec2, 0.5);
+ assertEquals(new Vector2(8.5, 4.0), vec3);
+ }
+
+ @Test
+ public void testRotate() {
+ final Vector2 vec1 = new Vector2(1, 0);
+ final Vector2 vec2 = vec1.rotateAroundOrigin(MathUtils.HALF_PI, true, null);
+ final Vector2 vec2B = vec1.rotateAroundOrigin(MathUtils.HALF_PI, false, new Vector2());
+ assertEquals(new Vector2(0, -1), vec2);
+ assertEquals(new Vector2(0, 1), vec2B);
+ vec2.rotateAroundOriginLocal(MathUtils.HALF_PI, false);
+ assertEquals(new Vector2(1, 0), vec2);
+ vec2.rotateAroundOriginLocal(MathUtils.PI, true);
+ assertTrue(Math.abs(vec2.getX() - -1) <= MathUtils.EPSILON);
+ assertTrue(Math.abs(vec2.getY() - 0) <= MathUtils.EPSILON);
+ }
+
+ @Test
+ public void testAngle() {
+ final Vector2 vec1 = new Vector2(1, 0);
+ assertTrue(MathUtils.HALF_PI == vec1.angleBetween(new Vector2(0, 1)));
+ assertTrue(-MathUtils.HALF_PI == vec1.angleBetween(new Vector2(0, -1)));
+
+ assertTrue(MathUtils.HALF_PI == vec1.smallestAngleBetween(new Vector2(0, -1)));
+ }
+
+ @Test
+ public void testDot() {
+ final Vector2 vec1 = new Vector2(7, 2);
+ assertTrue(23.0 == vec1.dot(3, 1));
+
+ assertTrue(-5.0 == vec1.dot(new Vector2(-1, 1)));
+ }
+
+ @Test
+ public void testClone() {
+ final Vector2 vec1 = new Vector2(0, 0);
+ final Vector2 vec2 = vec1.clone();
+ assertEquals(vec1, vec2);
+ assertNotSame(vec1, vec2);
+ }
+
+ @Test
+ public void testValid() {
+ final Vector2 vec1 = new Vector2(0, 0);
+ final Vector2 vec2 = new Vector2(Double.POSITIVE_INFINITY, 0);
+ final Vector2 vec3 = new Vector2(0, Double.NEGATIVE_INFINITY);
+ final Vector2 vec4 = new Vector2(Double.NaN, 0);
+ final Vector2 vec5 = new Vector2(0, Double.NaN);
+
+ assertTrue(Vector2.isValid(vec1));
+ assertFalse(Vector2.isValid(vec2));
+ assertFalse(Vector2.isValid(vec3));
+ assertFalse(Vector2.isValid(vec4));
+ assertFalse(Vector2.isValid(vec5));
+
+ vec5.zero();
+ assertTrue(Vector2.isValid(vec5));
+
+ assertFalse(Vector2.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(vec1, vec1);
+ assertFalse(vec1.equals(null));
+ assertFalse(vec1.equals(new Vector3()));
+
+ // throw in a couple pool accesses for coverage
+ final Vector2 vec6 = Vector2.fetchTempInstance();
+ vec6.set(vec1);
+ assertEquals(vec1, vec6);
+ assertNotSame(vec1, vec6);
+ Vector2.releaseTempInstance(vec6);
+
+ // cover more of equals
+ vec1.set(0, 1);
+ assertFalse(vec1.equals(new Vector2(0, 2)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Vector2 vec1 = new Vector2(1, 2);
+ final Vector2 vec2 = new Vector2(1, 2);
+ final Vector2 vec3 = new Vector2(2, 2);
+
+ assertTrue(vec1.hashCode() == vec2.hashCode());
+ assertTrue(vec1.hashCode() != vec3.hashCode());
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestVector3.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector3.java new file mode 100644 index 0000000..8bf4bfa --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector3.java @@ -0,0 +1,405 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestVector3 {
+
+ @Test
+ public void testAdd() {
+ final Vector3 vec1 = new Vector3();
+ final Vector3 vec2 = new Vector3(Vector3.ONE);
+
+ vec1.addLocal(1, 2, 3);
+ assertEquals(new Vector3(1, 2, 3), vec1);
+ vec1.addLocal(-1, -2, -3);
+ assertEquals(Vector3.ZERO, vec1);
+
+ vec1.zero();
+ vec1.addLocal(vec2);
+ assertEquals(Vector3.ONE, vec1);
+
+ vec1.zero();
+ final Vector3 vec3 = vec1.add(vec2, new Vector3());
+ assertEquals(Vector3.ZERO, vec1);
+ assertEquals(Vector3.ONE, vec3);
+
+ final Vector3 vec4 = vec1.add(1, 0, 0, null);
+ assertEquals(Vector3.ZERO, vec1);
+ assertEquals(Vector3.UNIT_X, vec4);
+ }
+
+ @Test
+ public void testSubtract() {
+ final Vector3 vec1 = new Vector3();
+ final Vector3 vec2 = new Vector3(Vector3.ONE);
+
+ vec1.subtractLocal(1, 2, 3);
+ assertEquals(new Vector3(-1, -2, -3), vec1);
+ vec1.subtractLocal(-1, -2, -3);
+ assertEquals(Vector3.ZERO, vec1);
+
+ vec1.zero();
+ vec1.subtractLocal(vec2);
+ assertEquals(Vector3.NEG_ONE, vec1);
+
+ vec1.zero();
+ final Vector3 vec3 = vec1.subtract(vec2, new Vector3());
+ assertEquals(Vector3.ZERO, vec1);
+ assertEquals(Vector3.NEG_ONE, vec3);
+
+ final Vector3 vec4 = vec1.subtract(1, 0, 0, null);
+ assertEquals(Vector3.ZERO, vec1);
+ assertEquals(Vector3.NEG_UNIT_X, vec4);
+ }
+
+ @Test
+ public void testGetSet() {
+ final Vector3 vec1 = new Vector3();
+ vec1.setX(0);
+ assertTrue(vec1.getX() == 0.0);
+ vec1.setX(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.POSITIVE_INFINITY);
+ vec1.setX(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(0) == Double.NEGATIVE_INFINITY);
+
+ vec1.setY(0);
+ assertTrue(vec1.getY() == 0.0);
+ vec1.setY(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.POSITIVE_INFINITY);
+ vec1.setY(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(1) == Double.NEGATIVE_INFINITY);
+
+ vec1.setZ(0);
+ assertTrue(vec1.getZ() == 0.0);
+ vec1.setZ(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getZ() == Double.POSITIVE_INFINITY);
+ vec1.setZ(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getZ() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(2) == Double.NEGATIVE_INFINITY);
+
+ vec1.set(Math.PI, Math.PI, Math.PI);
+ assertTrue(vec1.getXf() == (float) Math.PI);
+ assertTrue(vec1.getYf() == (float) Math.PI);
+ assertTrue(vec1.getZf() == (float) Math.PI);
+
+ final Vector3 vec2 = new Vector3();
+ vec2.set(vec1);
+ assertEquals(vec1, vec2);
+
+ vec1.setValue(0, 0);
+ vec1.setValue(1, 0);
+ vec1.setValue(2, 0);
+ assertEquals(Vector3.ZERO, vec1);
+
+ // catch a few expected exceptions
+ try {
+ vec2.getValue(3);
+ fail("getValue(3) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.getValue(-1);
+ fail("getValue(-1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(-1, 0);
+ fail("setValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(3, 0);
+ fail("setValue(3, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered vec2
+ assertEquals(new Vector3(Math.PI, Math.PI, Math.PI), vec2);
+ }
+
+ @Test
+ public void testToArray() {
+ final Vector3 vec1 = new Vector3();
+ vec1.set(Math.PI, Double.MAX_VALUE, 42);
+ final double[] array = vec1.toArray(null);
+ final double[] array2 = vec1.toArray(new double[3]);
+ assertNotNull(array);
+ assertTrue(array.length == 3);
+ assertTrue(array[0] == Math.PI);
+ assertTrue(array[1] == Double.MAX_VALUE);
+ assertTrue(array[2] == 42);
+ assertNotNull(array2);
+ assertTrue(array2.length == 3);
+ assertTrue(array2[0] == Math.PI);
+ assertTrue(array2[1] == Double.MAX_VALUE);
+ assertTrue(array2[2] == 42);
+
+ try {
+ vec1.toArray(new double[1]);
+ fail("toArray(d[1]) should have thrown ArrayIndexOutOfBoundsException.");
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ }
+
+ final float[] farray = vec1.toFloatArray(null);
+ final float[] farray2 = vec1.toFloatArray(new float[3]);
+ assertNotNull(farray);
+ assertTrue(farray.length == 3);
+ assertTrue(farray[0] == (float) Math.PI);
+ assertTrue(farray[1] == (float) Double.MAX_VALUE);
+ assertTrue(farray[2] == 42f);
+ assertNotNull(farray2);
+ assertTrue(farray2.length == 3);
+ assertTrue(farray2[0] == (float) Math.PI);
+ assertTrue(farray2[1] == (float) Double.MAX_VALUE);
+ assertTrue(farray2[2] == 42f);
+
+ try {
+ vec1.toFloatArray(new float[1]);
+ fail("toFloatArray(d[1]) should have thrown ArrayIndexOutOfBoundsException.");
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ }
+ }
+
+ @Test
+ public void testMultiply() {
+ final Vector3 vec1 = new Vector3(1, -1, 2);
+ final Vector3 vec2 = vec1.multiply(2.0, null);
+ final Vector3 vec2B = vec1.multiply(2.0, new Vector3());
+ assertEquals(new Vector3(2.0, -2.0, 4.0), vec2);
+ assertEquals(new Vector3(2.0, -2.0, 4.0), vec2B);
+
+ vec2.multiplyLocal(0.5);
+ assertEquals(new Vector3(1.0, -1.0, 2.0), vec2);
+
+ final Vector3 vec3 = vec1.multiply(vec2, null);
+ final Vector3 vec3B = vec1.multiply(vec2, new Vector3());
+ assertEquals(new Vector3(1, 1, 4), vec3);
+ assertEquals(new Vector3(1, 1, 4), vec3B);
+
+ final Vector3 vec4 = vec1.multiply(2, 3, 2, null);
+ final Vector3 vec4B = vec1.multiply(2, 3, 2, new Vector3());
+ assertEquals(new Vector3(2, -3, 4), vec4);
+ assertEquals(new Vector3(2, -3, 4), vec4B);
+
+ vec1.multiplyLocal(0.5, 0.5, 0.5);
+ assertEquals(new Vector3(0.5, -0.5, 1.0), vec1);
+
+ vec1.multiplyLocal(vec2);
+ assertEquals(new Vector3(0.5, 0.5, 2.0), vec1);
+ }
+
+ @Test
+ public void testDivide() {
+ final Vector3 vec1 = new Vector3(1, -1, 2);
+ final Vector3 vec2 = vec1.divide(2.0, null);
+ final Vector3 vec2B = vec1.divide(2.0, new Vector3());
+ assertEquals(new Vector3(0.5, -0.5, 1.0), vec2);
+ assertEquals(new Vector3(0.5, -0.5, 1.0), vec2B);
+
+ vec2.divideLocal(0.5);
+ assertEquals(new Vector3(1.0, -1.0, 2.0), vec2);
+
+ final Vector3 vec3 = vec1.divide(vec2, null);
+ final Vector3 vec3B = vec1.divide(vec2, new Vector3());
+ assertEquals(Vector3.ONE, vec3);
+ assertEquals(Vector3.ONE, vec3B);
+
+ final Vector3 vec4 = vec1.divide(2, 3, 4, null);
+ final Vector3 vec4B = vec1.divide(2, 3, 4, new Vector3());
+ assertEquals(new Vector3(0.5, -1 / 3., 0.5), vec4);
+ assertEquals(new Vector3(0.5, -1 / 3., 0.5), vec4B);
+
+ vec1.divideLocal(0.5, 0.5, 0.5);
+ assertEquals(new Vector3(2, -2, 4), vec1);
+
+ vec1.divideLocal(vec2);
+ assertEquals(new Vector3(2, 2, 2), vec1);
+ }
+
+ @Test
+ public void testScaleAdd() {
+ final Vector3 vec1 = new Vector3(1, 1, 1);
+ final Vector3 vec2 = vec1.scaleAdd(2.0, new Vector3(1, 2, 3), null);
+ final Vector3 vec2B = vec1.scaleAdd(2.0, new Vector3(1, 2, 3), new Vector3());
+ assertEquals(new Vector3(3.0, 4.0, 5.0), vec2);
+ assertEquals(new Vector3(3.0, 4.0, 5.0), vec2B);
+
+ vec1.scaleAddLocal(2.0, new Vector3(1, 2, 3));
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNegate() {
+ final Vector3 vec1 = new Vector3(3, 2, -1);
+ final Vector3 vec2 = vec1.negate(null);
+ assertEquals(new Vector3(-3, -2, 1), vec2);
+
+ vec1.negateLocal();
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNormalize() {
+ final Vector3 vec1 = new Vector3(2, 1, 3);
+ assertTrue(vec1.length() == Math.sqrt(14));
+
+ final Vector3 vec2 = vec1.normalize(null);
+ final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1 + 3 * 3);
+ assertEquals(new Vector3(2 * invLength, 1 * invLength, 3 * invLength), vec2);
+
+ vec1.normalizeLocal();
+ assertEquals(new Vector3(2 * invLength, 1 * invLength, 3 * invLength), vec1);
+
+ vec1.zero();
+ vec1.normalize(vec2);
+ assertEquals(vec1, vec2);
+
+ // ensure no exception thrown
+ vec1.normalizeLocal();
+ vec1.normalize(null);
+ }
+
+ @Test
+ public void testDistance() {
+ final Vector3 vec1 = new Vector3(0, 0, 0);
+ assertTrue(4.0 == vec1.distance(4, 0, 0));
+ assertTrue(3.0 == vec1.distance(0, 3, 0));
+ assertTrue(2.0 == vec1.distance(0, 0, 2));
+
+ final Vector3 vec2 = new Vector3(1, 1, 1);
+ assertTrue(Math.sqrt(3) == vec1.distance(vec2));
+ }
+
+ @Test
+ public void testLerp() {
+ final Vector3 vec1 = new Vector3(8, 3, -2);
+ final Vector3 vec2 = new Vector3(2, 1, 0);
+ assertEquals(new Vector3(5, 2, -1), vec1.lerp(vec2, 0.5, null));
+ assertEquals(new Vector3(5, 2, -1), vec1.lerp(vec2, 0.5, new Vector3()));
+ assertEquals(new Vector3(5, 2, -1), Vector3.lerp(vec1, vec2, 0.5, null));
+ assertEquals(new Vector3(5, 2, -1), Vector3.lerp(vec1, vec2, 0.5, new Vector3()));
+
+ vec1.set(14, 5, 4);
+ vec1.lerpLocal(vec2, 0.25);
+ assertEquals(new Vector3(11, 4, 3), vec1);
+
+ vec1.set(15, 7, 6);
+ final Vector3 vec3 = new Vector3(-1, -1, -1);
+ vec3.lerpLocal(vec1, vec2, 0.5);
+ assertEquals(new Vector3(8.5, 4.0, 3.0), vec3);
+
+ // coverage
+ assertEquals(vec1.lerp(vec1, .25, null), vec1);
+ assertEquals(vec2.lerpLocal(vec2, .25), vec2);
+ assertEquals(vec2.lerpLocal(vec2, vec2, .25), vec2);
+ assertEquals(Vector3.lerp(vec1, vec1, .25, null), vec1);
+ }
+
+ @Test
+ public void testCross() {
+ final Vector3 vec1 = new Vector3(1, 0, 0);
+ final Vector3 vec2 = new Vector3(0, 1, 0);
+ assertEquals(Vector3.UNIT_Z, vec1.cross(vec2, null));
+ assertEquals(Vector3.UNIT_Z, vec1.cross(vec2, new Vector3()));
+
+ assertEquals(Vector3.UNIT_Z, vec1.cross(0, 1, 0, null));
+ assertEquals(Vector3.UNIT_Z, vec1.cross(0, 1, 0, new Vector3()));
+
+ vec1.crossLocal(vec2);
+ assertEquals(Vector3.UNIT_Z, vec1);
+ vec2.crossLocal(1, 0, 0);
+ assertEquals(Vector3.NEG_UNIT_Z, vec2);
+ }
+
+ @Test
+ public void testAngle() {
+ final Vector3 vec1 = new Vector3(1, 0, 0);
+
+ assertTrue(MathUtils.HALF_PI == vec1.smallestAngleBetween(new Vector3(0, -1, 0)));
+ }
+
+ @Test
+ public void testDot() {
+ final Vector3 vec1 = new Vector3(7, 2, 5);
+ assertTrue(33.0 == vec1.dot(3, 1, 2));
+
+ assertTrue(-10.0 == vec1.dot(new Vector3(-1, 1, -1)));
+ }
+
+ @Test
+ public void testClone() {
+ final Vector3 vec1 = new Vector3(0, 0, 0);
+ final Vector3 vec2 = vec1.clone();
+ assertEquals(vec1, vec2);
+ assertNotSame(vec1, vec2);
+ }
+
+ @Test
+ public void testValid() {
+ final Vector3 vec1 = new Vector3(0, 0, 0);
+ final Vector3 vec2A = new Vector3(Double.POSITIVE_INFINITY, 0, 0);
+ final Vector3 vec2B = new Vector3(0, Double.NEGATIVE_INFINITY, 0);
+ final Vector3 vec2C = new Vector3(0, 0, Double.POSITIVE_INFINITY);
+ final Vector3 vec3A = new Vector3(Double.NaN, 0, 0);
+ final Vector3 vec3B = new Vector3(0, Double.NaN, 0);
+ final Vector3 vec3C = new Vector3(0, 0, Double.NaN);
+
+ assertTrue(Vector3.isValid(vec1));
+ assertFalse(Vector3.isValid(vec2A));
+ assertFalse(Vector3.isValid(vec2B));
+ assertFalse(Vector3.isValid(vec2C));
+ assertFalse(Vector3.isValid(vec3A));
+ assertFalse(Vector3.isValid(vec3B));
+ assertFalse(Vector3.isValid(vec3C));
+
+ assertFalse(Vector3.isInfinite(vec1));
+ assertTrue(Vector3.isInfinite(vec2A));
+
+ vec3C.zero();
+ assertTrue(Vector3.isValid(vec3C));
+
+ assertFalse(Vector3.isValid(null));
+ assertFalse(Vector3.isInfinite(null));
+
+ // couple of equals validity tests
+ assertEquals(vec1, vec1);
+ assertFalse(vec1.equals(null));
+ assertFalse(vec1.equals(new Vector4()));
+
+ // throw in a couple pool accesses for coverage
+ final Vector3 vec6 = Vector3.fetchTempInstance();
+ vec6.set(vec1);
+ assertEquals(vec1, vec6);
+ assertNotSame(vec1, vec6);
+ Vector3.releaseTempInstance(vec6);
+
+ // cover more of equals
+ vec1.set(0, 1, 2);
+ assertFalse(vec1.equals(new Vector3(0, 2, 3)));
+ assertFalse(vec1.equals(new Vector3(0, 1, 3)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Vector3 vec1 = new Vector3(1, 2, 3);
+ final Vector3 vec2 = new Vector3(1, 2, 3);
+ final Vector3 vec3 = new Vector3(2, 2, 2);
+
+ assertTrue(vec1.hashCode() == vec2.hashCode());
+ assertTrue(vec1.hashCode() != vec3.hashCode());
+ }
+}
diff --git a/ardor3d-math/src/test/java/com/ardor3d/math/TestVector4.java b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector4.java new file mode 100644 index 0000000..21c1b42 --- /dev/null +++ b/ardor3d-math/src/test/java/com/ardor3d/math/TestVector4.java @@ -0,0 +1,377 @@ +/**
+ * Copyright (c) 2008-2012 Ardor Labs, Inc.
+ *
+ * This file is part of Ardor3D.
+ *
+ * Ardor3D is free software: you can redistribute it and/or modify it
+ * under the terms of its license which may be found in the accompanying
+ * LICENSE file or at <http://www.ardor3d.com/LICENSE>.
+ */
+
+package com.ardor3d.math;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestVector4 {
+
+ @Test
+ public void testAdd() {
+ final Vector4 vec1 = new Vector4();
+ final Vector4 vec2 = new Vector4(Vector4.ONE);
+
+ vec1.addLocal(1, 2, 3, 4);
+ assertEquals(new Vector4(1, 2, 3, 4), vec1);
+ vec1.addLocal(-1, -2, -3, -4);
+ assertEquals(Vector4.ZERO, vec1);
+
+ vec1.zero();
+ vec1.addLocal(vec2);
+ assertEquals(Vector4.ONE, vec1);
+
+ vec1.zero();
+ final Vector4 vec3 = vec1.add(vec2, new Vector4());
+ assertEquals(Vector4.ZERO, vec1);
+ assertEquals(Vector4.ONE, vec3);
+
+ final Vector4 vec4 = vec1.add(0, 0, 0, 1, null);
+ assertEquals(Vector4.ZERO, vec1);
+ assertEquals(Vector4.UNIT_W, vec4);
+ }
+
+ @Test
+ public void testSubtract() {
+ final Vector4 vec1 = new Vector4();
+ final Vector4 vec2 = new Vector4(Vector4.ONE);
+
+ vec1.subtractLocal(1, 2, 3, 4);
+ assertEquals(new Vector4(-1, -2, -3, -4), vec1);
+ vec1.subtractLocal(-1, -2, -3, -4);
+ assertEquals(Vector4.ZERO, vec1);
+
+ vec1.zero();
+ vec1.subtractLocal(vec2);
+ assertEquals(Vector4.NEG_ONE, vec1);
+
+ vec1.zero();
+ final Vector4 vec3 = vec1.subtract(vec2, new Vector4());
+ assertEquals(Vector4.ZERO, vec1);
+ assertEquals(Vector4.NEG_ONE, vec3);
+
+ final Vector4 vec4 = vec1.subtract(0, 0, 0, 1, null);
+ assertEquals(Vector4.ZERO, vec1);
+ assertEquals(Vector4.NEG_UNIT_W, vec4);
+ }
+
+ @Test
+ public void testGetSet() {
+ final Vector4 vec1 = new Vector4();
+ vec1.setX(0);
+ assertTrue(vec1.getX() == 0.0);
+ vec1.setX(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.POSITIVE_INFINITY);
+ vec1.setX(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getX() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(0) == Double.NEGATIVE_INFINITY);
+
+ vec1.setY(0);
+ assertTrue(vec1.getY() == 0.0);
+ vec1.setY(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.POSITIVE_INFINITY);
+ vec1.setY(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getY() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(1) == Double.NEGATIVE_INFINITY);
+
+ vec1.setZ(0);
+ assertTrue(vec1.getZ() == 0.0);
+ vec1.setZ(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getZ() == Double.POSITIVE_INFINITY);
+ vec1.setZ(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getZ() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(2) == Double.NEGATIVE_INFINITY);
+
+ vec1.setW(0);
+ assertTrue(vec1.getW() == 0.0);
+ vec1.setW(Double.POSITIVE_INFINITY);
+ assertTrue(vec1.getW() == Double.POSITIVE_INFINITY);
+ vec1.setW(Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getW() == Double.NEGATIVE_INFINITY);
+ assertTrue(vec1.getValue(3) == Double.NEGATIVE_INFINITY);
+
+ vec1.set(Math.PI, Math.PI, Math.PI, Math.PI);
+ assertTrue(vec1.getXf() == (float) Math.PI);
+ assertTrue(vec1.getYf() == (float) Math.PI);
+ assertTrue(vec1.getZf() == (float) Math.PI);
+ assertTrue(vec1.getWf() == (float) Math.PI);
+
+ final Vector4 vec2 = new Vector4();
+ vec2.set(vec1);
+ assertEquals(vec1, vec2);
+
+ vec1.setValue(0, 0);
+ vec1.setValue(1, 0);
+ vec1.setValue(2, 0);
+ vec1.setValue(3, 0);
+ assertEquals(Vector4.ZERO, vec1);
+
+ // catch a few expected exceptions
+ try {
+ vec2.getValue(4);
+ fail("getValue(4) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.getValue(-1);
+ fail("getValue(-1) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(-1, 0);
+ fail("setValue(-1, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ try {
+ vec2.setValue(4, 0);
+ fail("setValue(4, 0) should have thrown IllegalArgumentException.");
+ } catch (final IllegalArgumentException e) {
+ }
+ // above exceptions shouldn't have altered vec2
+ assertEquals(new Vector4(Math.PI, Math.PI, Math.PI, Math.PI), vec2);
+ }
+
+ @Test
+ public void testToArray() {
+ final Vector4 vec1 = new Vector4();
+ vec1.set(Math.PI, Double.MAX_VALUE, 42, -1);
+ final double[] array = vec1.toArray(null);
+ final double[] array2 = vec1.toArray(new double[4]);
+ assertNotNull(array);
+ assertTrue(array.length == 4);
+ assertTrue(array[0] == Math.PI);
+ assertTrue(array[1] == Double.MAX_VALUE);
+ assertTrue(array[2] == 42);
+ assertTrue(array[3] == -1);
+ assertNotNull(array2);
+ assertTrue(array2.length == 4);
+ assertTrue(array2[0] == Math.PI);
+ assertTrue(array2[1] == Double.MAX_VALUE);
+ assertTrue(array2[2] == 42);
+ assertTrue(array2[3] == -1);
+
+ try {
+ vec1.toArray(new double[1]);
+ fail("toArray(d[1]) should have thrown ArrayIndexOutOfBoundsException.");
+ } catch (final ArrayIndexOutOfBoundsException e) {
+ }
+ }
+
+ @Test
+ public void testMultiply() {
+ final Vector4 vec1 = new Vector4(1, -1, 2, -2);
+ final Vector4 vec2 = vec1.multiply(2.0, null);
+ final Vector4 vec2B = vec1.multiply(2.0, new Vector4());
+ assertEquals(new Vector4(2.0, -2.0, 4.0, -4.0), vec2);
+ assertEquals(new Vector4(2.0, -2.0, 4.0, -4.0), vec2B);
+
+ vec2.multiplyLocal(0.5);
+ assertEquals(new Vector4(1.0, -1.0, 2.0, -2.0), vec2);
+
+ final Vector4 vec3 = vec1.multiply(vec2, null);
+ final Vector4 vec3B = vec1.multiply(vec2, new Vector4());
+ assertEquals(new Vector4(1, 1, 4, 4), vec3);
+ assertEquals(new Vector4(1, 1, 4, 4), vec3B);
+
+ final Vector4 vec4 = vec1.multiply(2, 3, 2, 3, null);
+ final Vector4 vec4B = vec1.multiply(2, 3, 2, 3, new Vector4());
+ assertEquals(new Vector4(2, -3, 4, -6), vec4);
+ assertEquals(new Vector4(2, -3, 4, -6), vec4B);
+
+ vec1.multiplyLocal(0.5, 0.5, 0.5, 0.5);
+ assertEquals(new Vector4(0.5, -0.5, 1.0, -1.0), vec1);
+
+ vec1.multiplyLocal(vec2);
+ assertEquals(new Vector4(0.5, 0.5, 2.0, 2.0), vec1);
+ }
+
+ @Test
+ public void testDivide() {
+ final Vector4 vec1 = new Vector4(1, -1, 2, -2);
+ final Vector4 vec2 = vec1.divide(2.0, null);
+ final Vector4 vec2B = vec1.divide(2.0, new Vector4());
+ assertEquals(new Vector4(0.5, -0.5, 1.0, -1.0), vec2);
+ assertEquals(new Vector4(0.5, -0.5, 1.0, -1.0), vec2B);
+
+ vec2.divideLocal(0.5);
+ assertEquals(new Vector4(1.0, -1.0, 2.0, -2.0), vec2);
+
+ final Vector4 vec3 = vec1.divide(vec2, null);
+ final Vector4 vec3B = vec1.divide(vec2, new Vector4());
+ assertEquals(Vector4.ONE, vec3);
+ assertEquals(Vector4.ONE, vec3B);
+
+ final Vector4 vec4 = vec1.divide(2, 3, 4, 5, null);
+ final Vector4 vec4B = vec1.divide(2, 3, 4, 5, new Vector4());
+ assertEquals(new Vector4(0.5, -1 / 3., 0.5, -0.4), vec4);
+ assertEquals(new Vector4(0.5, -1 / 3., 0.5, -0.4), vec4B);
+
+ vec1.divideLocal(0.5, 0.5, 0.5, 0.5);
+ assertEquals(new Vector4(2, -2, 4, -4), vec1);
+
+ vec1.divideLocal(vec2);
+ assertEquals(new Vector4(2, 2, 2, 2), vec1);
+ }
+
+ @Test
+ public void testScaleAdd() {
+ final Vector4 vec1 = new Vector4(1, 1, 1, 1);
+ final Vector4 vec2 = vec1.scaleAdd(2.0, new Vector4(1, 2, 3, 4), null);
+ final Vector4 vec2B = vec1.scaleAdd(2.0, new Vector4(1, 2, 3, 4), new Vector4());
+ assertEquals(new Vector4(3.0, 4.0, 5.0, 6.0), vec2);
+ assertEquals(new Vector4(3.0, 4.0, 5.0, 6.0), vec2B);
+
+ vec1.scaleAddLocal(2.0, new Vector4(1, 2, 3, 4));
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNegate() {
+ final Vector4 vec1 = new Vector4(3, 2, -1, 1);
+ final Vector4 vec2 = vec1.negate(null);
+ assertEquals(new Vector4(-3, -2, 1, -1), vec2);
+
+ vec1.negateLocal();
+ assertEquals(vec2, vec1);
+ }
+
+ @Test
+ public void testNormalize() {
+ final Vector4 vec1 = new Vector4(2, 1, 3, -1);
+ assertTrue(vec1.length() == Math.sqrt(15));
+
+ final Vector4 vec2 = vec1.normalize(null);
+ final double invLength = MathUtils.inverseSqrt(2 * 2 + 1 * 1 + 3 * 3 + -1 * -1);
+ assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec2);
+
+ vec1.normalizeLocal();
+ assertEquals(new Vector4(2 * invLength, 1 * invLength, 3 * invLength, -1 * invLength), vec1);
+
+ vec1.zero();
+ vec1.normalize(vec2);
+ assertEquals(vec1, vec2);
+
+ // ensure no exception thrown
+ vec1.normalizeLocal();
+ vec1.normalize(null);
+ }
+
+ @Test
+ public void testDistance() {
+ final Vector4 vec1 = new Vector4(0, 0, 0, 0);
+ assertTrue(4.0 == vec1.distance(4, 0, 0, 0));
+ assertTrue(3.0 == vec1.distance(0, 3, 0, 0));
+ assertTrue(2.0 == vec1.distance(0, 0, 2, 0));
+ assertTrue(1.0 == vec1.distance(0, 0, 0, 1));
+
+ final Vector4 vec2 = new Vector4(1, 1, 1, 1);
+ assertTrue(Math.sqrt(4) == vec1.distance(vec2));
+ }
+
+ @Test
+ public void testLerp() {
+ final Vector4 vec1 = new Vector4(8, 3, -2, 2);
+ final Vector4 vec2 = new Vector4(2, 1, 0, -2);
+ assertEquals(new Vector4(5, 2, -1, 0), vec1.lerp(vec2, 0.5, null));
+ assertEquals(new Vector4(5, 2, -1, 0), vec1.lerp(vec2, 0.5, new Vector4()));
+ assertEquals(new Vector4(5, 2, -1, 0), Vector4.lerp(vec1, vec2, 0.5, null));
+ assertEquals(new Vector4(5, 2, -1, 0), Vector4.lerp(vec1, vec2, 0.5, new Vector4()));
+
+ vec1.set(14, 5, 4, 2);
+ vec1.lerpLocal(vec2, 0.25);
+ assertEquals(new Vector4(11, 4, 3, 1), vec1);
+
+ vec1.set(15, 7, 6, 8);
+ final Vector4 vec3 = new Vector4(-1, -1, -1, -1);
+ vec3.lerpLocal(vec1, vec2, 0.5);
+ assertEquals(new Vector4(8.5, 4.0, 3.0, 3.0), vec3);
+
+ // coverage
+ assertEquals(vec1.lerp(vec1, .25, null), vec1);
+ assertEquals(vec2.lerpLocal(vec2, .25), vec2);
+ assertEquals(vec2.lerpLocal(vec2, vec2, .25), vec2);
+ assertEquals(Vector4.lerp(vec1, vec1, .25, null), vec1);
+ }
+
+ @Test
+ public void testDot() {
+ final Vector4 vec1 = new Vector4(7, 2, 5, -1);
+ assertTrue(35.0 == vec1.dot(3, 1, 2, -2));
+
+ assertTrue(-11.0 == vec1.dot(new Vector4(-1, 1, -1, 1)));
+ }
+
+ @Test
+ public void testClone() {
+ final Vector4 vec1 = new Vector4(0, 0, 0, 0);
+ final Vector4 vec2 = vec1.clone();
+ assertEquals(vec1, vec2);
+ assertNotSame(vec1, vec2);
+ }
+
+ @Test
+ public void testValid() {
+ final Vector4 vec1 = new Vector4(0, 0, 0, 0);
+ final Vector4 vec2A = new Vector4(Double.POSITIVE_INFINITY, 0, 0, 0);
+ final Vector4 vec2B = new Vector4(0, Double.NEGATIVE_INFINITY, 0, 0);
+ final Vector4 vec2C = new Vector4(0, 0, Double.POSITIVE_INFINITY, 0);
+ final Vector4 vec2D = new Vector4(0, 0, 0, Double.POSITIVE_INFINITY);
+ final Vector4 vec3A = new Vector4(Double.NaN, 0, 0, 0);
+ final Vector4 vec3B = new Vector4(0, Double.NaN, 0, 0);
+ final Vector4 vec3C = new Vector4(0, 0, Double.NaN, 0);
+ final Vector4 vec3D = new Vector4(0, 0, 0, Double.NaN);
+
+ assertTrue(Vector4.isValid(vec1));
+ assertFalse(Vector4.isValid(vec2A));
+ assertFalse(Vector4.isValid(vec2B));
+ assertFalse(Vector4.isValid(vec2C));
+ assertFalse(Vector4.isValid(vec2D));
+ assertFalse(Vector4.isValid(vec3A));
+ assertFalse(Vector4.isValid(vec3B));
+ assertFalse(Vector4.isValid(vec3C));
+ assertFalse(Vector4.isValid(vec3D));
+
+ vec3C.zero();
+ assertTrue(Vector4.isValid(vec3C));
+
+ assertFalse(Vector4.isValid(null));
+
+ // couple of equals validity tests
+ assertEquals(vec1, vec1);
+ assertFalse(vec1.equals(null));
+ assertFalse(vec1.equals(new Vector2()));
+
+ // throw in a couple pool accesses for coverage
+ final Vector4 vec6 = Vector4.fetchTempInstance();
+ vec6.set(vec1);
+ assertEquals(vec1, vec6);
+ assertNotSame(vec1, vec6);
+ Vector4.releaseTempInstance(vec6);
+
+ // cover more of equals
+ vec1.set(0, 1, 2, 3);
+ assertFalse(vec1.equals(new Vector4(0, 4, 4, 4)));
+ assertFalse(vec1.equals(new Vector4(0, 1, 4, 4)));
+ assertFalse(vec1.equals(new Vector4(0, 1, 2, 4)));
+ }
+
+ @Test
+ public void testSimpleHash() {
+ // Just a simple sanity check.
+ final Vector4 vec1 = new Vector4(1, 2, 3, 4);
+ final Vector4 vec2 = new Vector4(1, 2, 3, 4);
+ final Vector4 vec3 = new Vector4(2, 2, 2, 2);
+
+ assertTrue(vec1.hashCode() == vec2.hashCode());
+ assertTrue(vec1.hashCode() != vec3.hashCode());
+ }
+}
|