1 module doml.tests.matrix_4x3d_test;
2 
3 
4 import doml.tests.dunit_tests;
5 import doml.matrix_4x3d;
6 import doml.vector_3d;
7 import Math = doml.math;
8 import std.stdio;
9 
10 /*
11  * The MIT License
12  *
13  * Copyright (c) 2016-2021 JOML.
14  %$#%#$% Translated by jordan4ibanez
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a copy
17  * of this software and associated documentation files (the "Software"), to deal
18  * in the Software without restriction, including without limitation the rights
19  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20  * copies of the Software, and to permit persons to whom the Software is
21  * furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be included in
24  * all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32  * THE SOFTWARE.
33  */
34 /**
35  * Tests for the {@link Matrix4x3d} class.
36  * 
37  * @author Kai Burjack
38  */
39 unittest {
40 
41     writeln("\nBEGINNING MATRIX4X3D TESTING\n");
42 
43     // testLookAt
44     {
45         Matrix4x3d m1, m2;
46         m1 = Matrix4x3d().lookAt(0, 2, 3, 0, 0, 0, 0, 1, 0);
47         m2 = Matrix4x3d().translate(0, 0, -cast(double) Math.sqrt(2 * 2 + 3 * 3)).rotateX(
48                 cast(double) Math.atan2(2, 3));
49         assertMatrix4x3dEquals(m1, m2, 1E-5f);
50         m1 = Matrix4x3d().lookAt(3, 2, 0, 0, 0, 0, 0, 1, 0);
51         m2 = Matrix4x3d().translate(0, 0, -cast(double) Math.sqrt(2 * 2 + 3 * 3))
52                 .rotateX(cast(double) Math.atan2(2, 3)).rotateY(cast(double) Math.toRadians(-90));
53         assertMatrix4x3dEquals(m1, m2, 1E-4f);
54     }
55 
56     // testPositiveXRotateY
57     {
58         Vector3d dir = Vector3d();
59         Matrix4x3d m = Matrix4x3d()
60                 .rotateY(cast(double) Math.toRadians(90));
61         m.positiveX(dir);
62         assertVector3dEquals(Vector3d(0, 0, 1), dir, 1E-7f);
63     }
64 
65     // testPositiveYRotateX
66     {
67         Vector3d dir = Vector3d();
68         Matrix4x3d m = Matrix4x3d()
69                 .rotateX(cast(double) Math.toRadians(90));
70         m.positiveY(dir);
71         assertVector3dEquals(Vector3d(0, 0, -1), dir, 1E-7f);
72     }
73 
74     // testPositiveZRotateX
75     {
76         Vector3d dir = Vector3d();
77         Matrix4x3d m = Matrix4x3d()
78                 .rotateX(cast(double) Math.toRadians(90));
79         m.positiveZ(dir);
80         assertVector3dEquals(Vector3d(0, 1, 0), dir, 1E-7f);
81     }
82 
83     // testPositiveXRotateXY
84     {
85         Vector3d dir = Vector3d();
86         Matrix4x3d m = Matrix4x3d()
87                 .rotateY(cast(double) Math.toRadians(90)).rotateX(cast(double) Math.toRadians(45));
88         m.positiveX(dir);
89         assertVector3dEquals(Vector3d(0, 1, 1).normalize(), dir, 1E-7f);
90     }
91 
92     // testPositiveXYZLookAt
93     {
94         Vector3d dir = Vector3d();
95         Matrix4x3d m = Matrix4x3d()
96                 .lookAt(0, 0, 0, -1, 0, 0, 0, 1, 0);
97         m.positiveX(dir);
98         assertVector3dEquals(Vector3d(0, 0, -1).normalize(), dir, 1E-7f);
99         m.positiveY(dir);
100         assertVector3dEquals(Vector3d(0, 1, 0).normalize(), dir, 1E-7f);
101         m.positiveZ(dir);
102         assertVector3dEquals(Vector3d(1, 0, 0).normalize(), dir, 1E-7f);
103     }
104 
105     // testPositiveXYZSameAsInvert
106     {
107         Vector3d dir = Vector3d();
108         Vector3d dir2 = Vector3d();
109         Matrix4x3d m = Matrix4x3d().rotateXYZ(0.12f, 1.25f, -2.56f);
110         Matrix4x3d inv = Matrix4x3d(m).invert();
111         m.positiveX(dir);
112         inv.transformDirection(dir2.set(1, 0, 0));
113         assertVector3dEquals(dir2, dir, 1E-6f);
114         m.positiveY(dir);
115         inv.transformDirection(dir2.set(0, 1, 0));
116         assertVector3dEquals(dir2, dir, 1E-6f);
117         m.positiveZ(dir);
118         inv.transformDirection(dir2.set(0, 0, 1));
119         assertVector3dEquals(dir2, dir, 1E-6f);
120     }
121 
122     // testNormal
123     {
124         Matrix4x3d r = Matrix4x3d().rotateY(cast(double) Math.PI / 2);
125         Matrix4x3d s = Matrix4x3d(r).scale(0.2f);
126         Matrix4x3d n = Matrix4x3d();
127         s.normal(n);
128         n.normalize3x3();
129         assertMatrix4x3dEquals(r, n, 1E-8f);
130     }
131 
132     // testInvert
133     {
134         Matrix4x3d invm = Matrix4x3d();
135         Matrix4x3d m = Matrix4x3d();
136         m.rotateX(1.2f).rotateY(0.2f).rotateZ(0.1f).translate(1, 2, 3).invert(invm);
137         Vector3d orig = Vector3d(4, -6, 8);
138         Vector3d v = Vector3d();
139         Vector3d w = Vector3d();
140         m.transformPosition(orig, v);
141         invm.transformPosition(v, w);
142         assertVector3dEquals(orig, w, 1E-6f);
143         invm.invert();
144         assertMatrix4x3dEquals(m, invm, 1E-6f);
145     }
146 
147     // testRotateXYZ
148     {
149         Matrix4x3d m = Matrix4x3d().rotateX(0.12f).rotateY(0.0623f).rotateZ(0.95f);
150         Matrix4x3d n = Matrix4x3d().rotateXYZ(0.12f, 0.0623f, 0.95f);
151         assertMatrix4x3dEquals(m, n, 1E-6f);
152     }
153 
154     // testRotateZYX
155     {
156         Matrix4x3d m = Matrix4x3d().rotateZ(1.12f).rotateY(0.0623f).rotateX(0.95f);
157         Matrix4x3d n = Matrix4x3d().rotateZYX(1.12f, 0.0623f, 0.95f);
158         assertMatrix4x3dEquals(m, n, 1E-6f);
159     }
160 
161     // testRotateYXZ
162     {
163         Matrix4x3d m = Matrix4x3d().rotateY(1.12f).rotateX(0.0623f).rotateZ(0.95f);
164         Matrix4x3d n = Matrix4x3d().rotateYXZ(1.12f, 0.0623f, 0.95f);
165         assertMatrix4x3dEquals(m, n, 1E-6f);
166     }
167 
168     // testRotateAffineXYZ
169     {
170         Matrix4x3d m = Matrix4x3d().rotateX(0.12f).rotateY(0.0623f).rotateZ(0.95f);
171         Matrix4x3d n = Matrix4x3d().rotateXYZ(0.12f, 0.0623f, 0.95f);
172         assertMatrix4x3dEquals(m, n, 1E-6f);
173     }
174 
175     // testRotateAffineZYX
176     {
177         Matrix4x3d m = Matrix4x3d().rotateZ(1.12f).rotateY(0.0623f).rotateX(0.95f);
178         Matrix4x3d n = Matrix4x3d().rotateZYX(1.12f, 0.0623f, 0.95f);
179         assertMatrix4x3dEquals(m, n, 1E-6f);
180     }
181 
182     // testRotateAffineYXZ
183     {
184         Matrix4x3d m = Matrix4x3d().rotateY(1.12f).rotateX(0.0623f).rotateZ(0.95f);
185         Matrix4x3d n = Matrix4x3d().rotateYXZ(1.12f, 0.0623f, 0.95f);
186         assertMatrix4x3dEquals(m, n, 1E-6f);
187     }
188 
189     // testRotationXYZ
190     {
191         Matrix4x3d m = Matrix4x3d().rotationX(0.32f).rotateY(0.5623f).rotateZ(0.95f);
192         Matrix4x3d n = Matrix4x3d().rotationXYZ(0.32f, 0.5623f, 0.95f);
193         assertMatrix4x3dEquals(m, n, 1E-6f);
194     }
195 
196     // testRotationZYX
197     {
198         Matrix4x3d m = Matrix4x3d().rotationZ(0.12f).rotateY(0.0623f).rotateX(0.95f);
199         Matrix4x3d n = Matrix4x3d().rotationZYX(0.12f, 0.0623f, 0.95f);
200         assertMatrix4x3dEquals(m, n, 1E-6f);
201     }
202 
203     // testRotationYXZ
204     {
205         Matrix4x3d m = Matrix4x3d().rotationY(0.12f).rotateX(0.0623f).rotateZ(0.95f);
206         Matrix4x3d n = Matrix4x3d().rotationYXZ(0.12f, 0.0623f, 0.95f);
207         assertMatrix4x3dEquals(m, n, 1E-6f);
208     }
209 
210     writeln("PASSED!");
211 
212 }