1 module doml.tests.matrix_3x2d_test;
2 
3 import std.stdio;
4 import doml.tests.dunit_tests;
5 import Math = doml.math;
6 import doml.matrix_3x2d;
7 import doml.vector_2d;
8 
9 /*
10  * The MIT License
11  *
12  * Copyright (c) 2017-2021 JOML.
13  #$%^ Translated by jordan4ibanez
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this software and associated documentation files (the "Software"), to deal
17  * in the Software without restriction, including without limitation the rights
18  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19  * copies of the Software, and to permit persons to whom the Software is
20  * furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31  * THE SOFTWARE.
32  */
33 
34 /**
35  * Tests for the {@link Matrix3x2d} class.
36  * 
37  * @author Kai Burjack
38  */
39 unittest {
40 
41     writeln("\nBEGINNING MATRIX3X2D TESTS\n");
42 
43     
44     // testInvert
45     {
46         Matrix3x2d m = Matrix3x2d(1, 2, 4, 5, -0.5, -2.0);
47         Vector2d v = Vector2d(4, 0.5);
48         m.transformPosition(v);
49         Vector2d v2 = Vector2d(v);
50         m.invert();
51         m.transformPosition(v2);
52         assertVector2dEquals(Vector2d(4, 0.5), v2, MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
53     }
54 
55     // testView
56     {
57         Matrix3x2d m = Matrix3x2d().view(-4, 0.5, -2, 3);
58         Vector2d v = Vector2d(-4, -2);
59         m.transformPosition(v);
60         assertVector2dEquals(Vector2d(-1, -1), v, MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
61         v.set(0.5f, 3);
62         m.transformPosition(v);
63         assertVector2dEquals(Vector2d(1, 1), v, MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
64     }
65 
66     // testUnproject
67     {
68 
69         // Another test where the blank value was inline as an object
70         // This fixes it
71         Vector2d blank = Vector2d();
72         Matrix3x2d m = Matrix3x2d().view(-3, 2, -4, 1);
73         assertVector2dEquals(Vector2d(-3, -4), m.unproject(0, 0, [0, 0, 800, 600], blank), MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
74         // Needs to zero out as the next function was using the same thing
75         blank.zero();
76         assertVector2dEquals(Vector2d(2, 1), m.unproject(800, 600, [0, 0, 800, 600], blank), MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
77     }
78 
79     // testTestPoint
80     {
81         Matrix3x2d m = Matrix3x2d().view(-4, 2, -3, 10);
82         assertTrue(m.testPoint(0, 0));
83         assertTrue(m.testPoint(-4, -2.9));
84         assertFalse(m.testPoint(-4.01, -2.9));
85         assertFalse(m.testPoint(-3.9, -3.01));
86         assertTrue(m.testPoint(0, 9.99));
87         assertFalse(m.testPoint(0, 10.01));
88 
89         // rotated
90         m.setView(-2, 2, -2, 2).rotate(Math.toRadians(45));
91         double[] area = m.viewArea(new double[4]);
92         assertTrue(m.testPoint(area[0], 0));
93         assertFalse(m.testPoint(area[0]-0.01, 0));
94         assertTrue(m.testPoint(area[2]-0.1, 0));
95         assertFalse(m.testPoint(area[2]+0.01, 0));
96     }
97 
98     writeln("PASSED!");
99 
100 }