|
| 1 | +package processing.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import org.junit.Before; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +public class PMatrix2DTest { |
| 9 | + |
| 10 | + private PMatrix2D m; |
| 11 | + |
| 12 | + @Before |
| 13 | + public void setUp() { |
| 14 | + m = new PMatrix2D(); |
| 15 | + } |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testIdentity() { |
| 19 | + assertTrue("New matrix should be identity", m.isIdentity()); |
| 20 | + float[] arr = m.get(null); |
| 21 | + assertEquals(1, arr[0], 0.0001f); // m00 |
| 22 | + assertEquals(0, arr[1], 0.0001f); // m01 |
| 23 | + assertEquals(0, arr[2], 0.0001f); // m02 |
| 24 | + assertEquals(0, arr[3], 0.0001f); // m10 |
| 25 | + assertEquals(1, arr[4], 0.0001f); // m11 |
| 26 | + assertEquals(0, arr[5], 0.0001f); // m12 |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testTranslate() { |
| 31 | + m.translate(10, 20); |
| 32 | + assertEquals(10, m.m02, 0.0001f); |
| 33 | + assertEquals(20, m.m12, 0.0001f); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void testRotate() { |
| 38 | + m.rotate(PConstants.HALF_PI); |
| 39 | + assertEquals(0, m.m00, 0.0001f); |
| 40 | + assertEquals(-1, m.m01, 0.0001f); |
| 41 | + assertEquals(1, m.m10, 0.0001f); |
| 42 | + assertEquals(0, m.m11, 0.0001f); |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + @Test |
| 47 | + public void testScale() { |
| 48 | + m.scale(2, 3); |
| 49 | + assertEquals(2, m.m00, 0.0001f); |
| 50 | + assertEquals(3, m.m11, 0.0001f); |
| 51 | + assertEquals(0, m.m02, 0.0001f); |
| 52 | + assertEquals(0, m.m12, 0.0001f); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void testShear() { |
| 57 | + float shearAngle = 0.2f; |
| 58 | + m.shearX(shearAngle); |
| 59 | + assertEquals(0, m.m01, 0.0001f); |
| 60 | + assertEquals((float)Math.tan(shearAngle), m.m10, 0.0001f); |
| 61 | + assertEquals(1, m.m02, 0.0001f); |
| 62 | + |
| 63 | + m.reset(); |
| 64 | + |
| 65 | + m.shearY(shearAngle); |
| 66 | + assertEquals(0, m.m01, 0.0001f); |
| 67 | + assertEquals(0, m.m10, 0.0001f); |
| 68 | + assertEquals((float)Math.tan(shearAngle), m.m11, 0.0001f); |
| 69 | + assertEquals(1, m.m02, 0.0001f); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testApply() { |
| 74 | + PMatrix2D m2 = new PMatrix2D(1, 2, 3, 4, 5, 6); |
| 75 | + m.apply(m2); |
| 76 | + assertEquals(m2.m00, m.m00, 0.0001f); |
| 77 | + assertEquals(m2.m01, m.m01, 0.0001f); |
| 78 | + assertEquals(m2.m02, m.m02, 0.0001f); |
| 79 | + assertEquals(m2.m10, m.m10, 0.0001f); |
| 80 | + assertEquals(m2.m11, m.m11, 0.0001f); |
| 81 | + assertEquals(m2.m12, m.m12, 0.0001f); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testPreApply() { |
| 86 | + PMatrix2D m1 = new PMatrix2D(1, 2, 3, 4, 5, 6); |
| 87 | + m.reset(); // identity matrix |
| 88 | + m.preApply(m1); |
| 89 | + assertEquals(m1.m00, m.m00, 0.0001f); |
| 90 | + assertEquals(m1.m01, m.m01, 0.0001f); |
| 91 | + assertEquals(m1.m02, m.m02, 0.0001f); |
| 92 | + assertEquals(m1.m10, m.m10, 0.0001f); |
| 93 | + assertEquals(m1.m11, m.m11, 0.0001f); |
| 94 | + assertEquals(m1.m12, m.m12, 0.0001f); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testMultPVector() { |
| 99 | + PVector src = new PVector(1, 2, 0); |
| 100 | + PVector result = m.mult(src, null); |
| 101 | + assertEquals(src.x, result.x, 0.0001f); |
| 102 | + assertEquals(src.y, result.y, 0.0001f); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testMultArray() { |
| 107 | + float[] vec = { 1, 2 }; |
| 108 | + float[] out = m.mult(vec, null); |
| 109 | + assertEquals(1, out[0], 0.0001f); |
| 110 | + assertEquals(2, out[1], 0.0001f); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testMultXandY() { |
| 115 | + float x = 10, y = 20; |
| 116 | + float xOut = m.multX(x, y); |
| 117 | + float yOut = m.multY(x, y); |
| 118 | + assertEquals(x, xOut, 0.0001f); |
| 119 | + assertEquals(y, yOut, 0.0001f); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testInvertAndDeterminant() { |
| 124 | + m.set(2, 0, 5, 1, 3, 7); |
| 125 | + float det = m.determinant(); |
| 126 | + assertEquals(6, det, 0.0001f); |
| 127 | + |
| 128 | + boolean invertible = m.invert(); |
| 129 | + assertTrue("Matrix should be invertible", invertible); |
| 130 | + |
| 131 | + PMatrix2D identity = new PMatrix2D(2, 0, 5, 1, 3, 7); |
| 132 | + identity.apply(m); |
| 133 | + |
| 134 | + assertEquals(1, identity.m00, 0.001f); |
| 135 | + assertEquals(0, identity.m01, 0.001f); |
| 136 | + assertEquals(0, identity.m10, 0.001f); |
| 137 | + assertEquals(1, identity.m11, 0.001f); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + public void testIdentityWarped() { |
| 142 | + assertTrue(m.isIdentity()); |
| 143 | + assertFalse(m.isWarped()); |
| 144 | + |
| 145 | + m.translate(10, 20); |
| 146 | + assertFalse(m.isIdentity()); |
| 147 | + } |
| 148 | + |
| 149 | + @Test(expected = IllegalArgumentException.class) |
| 150 | + public void testTranslate3DThrows() { |
| 151 | + m.translate(1, 2, 3); |
| 152 | + } |
| 153 | + |
| 154 | + @Test(expected = IllegalArgumentException.class) |
| 155 | + public void testRotateXThrows() { |
| 156 | + m.rotateX(1); |
| 157 | + } |
| 158 | + |
| 159 | + @Test(expected = IllegalArgumentException.class) |
| 160 | + public void testRotateYThrows() { |
| 161 | + m.rotateY(1); |
| 162 | + } |
| 163 | + |
| 164 | + @Test(expected = IllegalArgumentException.class) |
| 165 | + public void testScale3DThrows() { |
| 166 | + m.scale(1, 2, 3); |
| 167 | + } |
| 168 | + |
| 169 | + @Test(expected = IllegalArgumentException.class) |
| 170 | + public void testApplyPMatrix3DThrows() { |
| 171 | + PMatrix3D m3d = new PMatrix3D(1, 0, 0, 0, |
| 172 | + 0, 1, 0, 0, |
| 173 | + 0, 0, 1, 0, |
| 174 | + 0, 0, 0, 1); |
| 175 | + m.apply(m3d); |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + public void testGetArray() { |
| 180 | + m.set(new float[]{1, 2, 0, 0, 1, 0}); |
| 181 | + float[] arr = m.get(null); |
| 182 | + assertEquals(1, arr[0], 0.0001f); |
| 183 | + assertEquals(2, arr[1], 0.0001f); |
| 184 | + assertEquals(0, arr[2], 0.0001f); |
| 185 | + } |
| 186 | +} |
0 commit comments