1 module doml.tests.matrix_3d_test;
2 
3 import std.stdio;
4 import Math = doml.math;
5 import doml.tests.dunit_tests;
6 import doml.matrix_3d;
7 import doml.vector_3d;
8 
9 /*
10  * The MIT License
11  *
12  * Copyright (c) 2020-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  *
36  * @author mhameed
37  */
38 unittest {
39 
40     writeln("\nBEGINNING TEST OF MATRIX3D\n");
41 
42     /**
43      * Test of setRow method, of class Matrix3d.
44      */
45     {
46         int row = 0;
47         double x = 0.0;
48         double y = 1.0;
49         double z = 2.0;
50 
51         Matrix3d instance = Matrix3d();
52         Matrix3d result = instance.setRow(row, x, y, z);
53 
54         Vector3d inRow = Vector3d(x, y, z);
55         Vector3d outRow = Vector3d();
56         
57         result.getRow(row, outRow);
58         assertEquals(inRow, outRow);
59     }
60 
61     // testGet
62     {
63         Matrix3d m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9);
64         for (int c = 0; c < 3; c++)
65             for (int r = 0; r < 3; r++)
66                 assertEquals(c*3+r+1, m.get(c, r), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
67     }
68 
69     // testSet
70     {
71         assertMatrix3dEquals(Matrix3d().zero().set(0, 0, 3), Matrix3d(3, 0, 0, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
72         assertMatrix3dEquals(Matrix3d().zero().set(0, 1, 3), Matrix3d(0, 3, 0, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
73         assertMatrix3dEquals(Matrix3d().zero().set(0, 2, 3), Matrix3d(0, 0, 3, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
74         assertMatrix3dEquals(Matrix3d().zero().set(1, 0, 3), Matrix3d(0, 0, 0, 3, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
75         assertMatrix3dEquals(Matrix3d().zero().set(1, 1, 3), Matrix3d(0, 0, 0, 0, 3, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
76         assertMatrix3dEquals(Matrix3d().zero().set(1, 2, 3), Matrix3d(0, 0, 0, 0, 0, 3, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
77         assertMatrix3dEquals(Matrix3d().zero().set(2, 0, 3), Matrix3d(0, 0, 0, 0, 0, 0, 3, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
78         assertMatrix3dEquals(Matrix3d().zero().set(2, 1, 3), Matrix3d(0, 0, 0, 0, 0, 0, 0, 3, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
79         assertMatrix3dEquals(Matrix3d().zero().set(2, 2, 3), Matrix3d(0, 0, 0, 0, 0, 0, 0, 0, 3), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
80     }
81 
82 
83 
84     // From here below was Matrix3d
85 
86     /**
87     *
88     * @author mhameed
89     */
90 
91 
92     /**
93      * Test of setRow method, of class Matrix3d.
94      */
95      // testSetRow_4args
96     {
97         int row = 0;
98         double x = 0.0;
99         double y = 1.0;
100         double z = 2.0;
101         Matrix3d instance = Matrix3d();
102         Vector3d inRow = Vector3d(x, y, z);
103         Vector3d outRow = Vector3d();
104         Matrix3d result = instance.setRow(row, x, y, z);
105         result.getRow(row, outRow);
106         assertEquals(inRow, outRow);
107     }
108 
109     // testMatrix3dTranspose
110     {
111         double m00 = 1, m01 = 2, m02 = 3;
112         double m10 = 5, m11 = 6, m12 = 7;
113         double m20 = 9, m21 = 10, m22 = 11;
114 
115         Matrix3d m = Matrix3d(m00, m01, m02, m10, m11, m12, m20, m21, m22);
116         Matrix3d expect = Matrix3d(m00, m10, m20, m01, m11, m21, m02, m12, m22);
117         assertMatrix3dEquals(Matrix3d(m).transpose(), expect, 1E-5);
118         // This test originally constructed a new object using Java's weak memory management
119         // Fixed by creating a temporary structure here, uses default constructor
120         Matrix3d testUnit = Matrix3d();
121         assertMatrix3dEquals(Matrix3d(m).transpose(testUnit/*new Matrix3d()*/), expect, 1E-5);
122     } // *Poof* testUnit is gone
123 
124     // testInvert
125     {
126         Matrix3d invm = Matrix3d();
127         Matrix3d m = Matrix3d();
128 
129         m.rotateXYZ(0.23, 1.523, -0.7234).invert(invm);
130 
131         Vector3d orig = Vector3d(4, -6, 8);
132 
133         Vector3d v = Vector3d();
134         Vector3d w = Vector3d();
135 
136         m.transform(orig, v);
137         invm.transform(v, w);
138 
139         // Precision was too low for this test, raised up
140         assertVector3dEquals(orig, w, MANY_OPS_AROUND_ZERO_PRECISION_DOUBLE);
141 
142         invm.invert();
143         assertMatrix3dEquals(m, invm, 1E-3);
144     }
145 
146     // testGet
147     {
148         Matrix3d m = Matrix3d(1, 2, 3, 4, 5, 6, 7, 8, 9);
149         for (int c = 0; c < 3; c++)
150             for (int r = 0; r < 3; r++)
151                 assertEquals(c*3+r+1, m.get(c, r), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
152     }
153 
154     // testSet
155     {
156         assertMatrix3dEquals(Matrix3d().zero().set(0, 0, 3), Matrix3d(3, 0, 0, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
157         assertMatrix3dEquals(Matrix3d().zero().set(0, 1, 3), Matrix3d(0, 3, 0, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
158         assertMatrix3dEquals(Matrix3d().zero().set(0, 2, 3), Matrix3d(0, 0, 3, 0, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
159         assertMatrix3dEquals(Matrix3d().zero().set(1, 0, 3), Matrix3d(0, 0, 0, 3, 0, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
160         assertMatrix3dEquals(Matrix3d().zero().set(1, 1, 3), Matrix3d(0, 0, 0, 0, 3, 0, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
161         assertMatrix3dEquals(Matrix3d().zero().set(1, 2, 3), Matrix3d(0, 0, 0, 0, 0, 3, 0, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
162         assertMatrix3dEquals(Matrix3d().zero().set(2, 0, 3), Matrix3d(0, 0, 0, 0, 0, 0, 3, 0, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
163         assertMatrix3dEquals(Matrix3d().zero().set(2, 1, 3), Matrix3d(0, 0, 0, 0, 0, 0, 0, 3, 0), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
164         assertMatrix3dEquals(Matrix3d().zero().set(2, 2, 3), Matrix3d(0, 0, 0, 0, 0, 0, 0, 0, 3), STANDARD_AROUND_ZERO_PRECISION_DOUBLE);
165     }
166 
167     writeln("PASSED!");
168 }