Dali 3D User Interface Engine
utc-Dali-Matrix3.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include <iostream>
19 
20 #include <stdlib.h>
22 #include <dali-test-suite-utils.h>
23 
24 
25 using namespace Dali;
26 
28 {
30 }
31 
33 {
35 }
36 
37 namespace
38 {
39 
40 Matrix3 m1(
41  -18.6f, 1.88e-09f, -6.85e-09f,
42  0.0f,13.2f, 13.2f,
43  -1.36e-08f,13.2f, -13.2f);
44 
45 Matrix3 m2(
46  -18.6f,6.91e-06f, 6.76e-06f,
47  8.04e-09f,13.2f, 13.2f,
48  3.01e-06f,13.2f, -13.2f);
49 
50 Matrix3 m3(
51  6.24f,-12.4f, -12.4f,
52  -17.6f,-4.46f, -4.37f,
53  -0.0641f,13.2f, -13.2f);
54 
55 Matrix3 m4(
56  -16.3f,6.42f, 6.38f,
57  9.05f,11.6f, 11.4f,
58  -0.0371f,13.1f, -13.3f);
59 
60 Matrix3 m5(
61  -2.43f,13.2f, 12.9f,
62  18.5f,1.92f, 1.51f,
63  -0.257f,13.0f, -13.4f);
64 
65 Matrix3 m6(
66  -2.43f, -13.2f, -200.9f,
67  18.5f, 1.92f, 1.51f,
68  0.257f, 13.0f, 13.4f);
69 
70 
71 Matrix3 i1(
72  -0.05, -0.00, 0.00,
73  -0.00, 0.04, 0.04,
74  0.00, 0.04, -0.04);
75 
76 Matrix3 i2(
77  -0.05, 0.00, -0.00,
78  0.00, 0.04, 0.04,
79  0.00, 0.04, -0.04);
80 
81 Matrix3 i3(
82  0.02, -0.05, -0.00,
83  -0.04, -0.01, 0.04,
84  -0.04, -0.01, -0.04);
85 
86 Matrix3 i4(
87  -0.05, 0.03, -0.00,
88  0.02, 0.03, 0.04,
89  0.02, 0.03, -0.04);
90 
91 Matrix3 i5(
92  -0.01, 0.05, -0.00,
93  0.04, 0.01, 0.04,
94  0.04, 0.00, -0.04);
95 
96 
97 
98 Matrix3 t1(
99  -18.6f, 0.0f, -1.36e-08f,
100  1.88e-09f,13.2f, 13.2f,
101  -6.85e-09f,13.2f, -13.2f);
102 
103 Matrix3 t2(
104  -18.6f,8.04e-09f, 3.01e-06f,
105  6.91e-06f,13.2f, 13.2f,
106  6.76e-06f,13.2f, -13.2f);
107 
108 Matrix3 t3(
109  6.24f,-17.6f, -0.0641f,
110  -12.4f,-4.46f, 13.2f,
111  -12.4f, -4.37f, -13.2f);
112 
113 Matrix3 t4(
114  -16.3f,9.05f, -0.0371f,
115  6.42f, 11.6f, 13.1f,
116  6.38f,11.4f, -13.3f);
117 
118 Matrix3 t5(
119  -2.43f,18.5f, -0.257f,
120  13.2f, 1.92f, 13.0f,
121  12.9f, 1.51f, -13.4f);
122 
123 
124 
125 Matrix3* matrices[5] = { &m1, &m2, &m3, &m4, &m5 };
126 Matrix3* inverseMatrices[5] = { &i1, &i2, &i3, &i4, &i5 };
127 Matrix3* transposeMatrices[5] = { &t1, &t2, &t3, &t4, &t5 };
128 
129 } // anonymous namespace
130 
132 {
133  float els0[] = { 0.0f, 1.0f, 2.0f, 3.0f,
134  4.0f, 5.0f, 6.0f, 7.0f,
135  8.0f, 9.0f, 10.0f, 11.0f,
136  12.0f, 13.0f, 14.0f, 15.0f};
137  Matrix m0(els0);
138  Matrix3 m1(0.0f, 1.0f, 2.0f,
139  4.0f, 5.0f, 6.0f,
140  8.0f, 9.0f, 10.0f);
141 
142  Matrix3 m2(m0);
143 
144  DALI_TEST_EQUALS(m1, m2, 0.001, TEST_LOCATION);
145  END_TEST;
146 }
147 
149 {
150  float els0[] = { 0.0f, 1.0f, 2.0f, 3.0f,
151  4.0f, 5.0f, 6.0f, 7.0f,
152  8.0f, 9.0f, 10.0f, 11.0f,
153  12.0f, 13.0f, 14.0f, 15.0f};
154  Matrix m0(els0);
155 
156  Matrix3 m1(0.0f, 1.0f, 2.0f,
157  4.0f, 5.0f, 6.0f,
158  8.0f, 9.0f, 10.0f);
159 
160  Matrix3 m2;
161  m2 = m0;
162  m2 = m2; // Test branch
163 
164  DALI_TEST_EQUALS(m1, m2, 0.001, TEST_LOCATION);
165  END_TEST;
166 }
167 
168 
170 {
171  Matrix3 m0(0.0f, 1.0f, 2.0f,
172  4.0f, 5.0f, 6.0f,
173  8.0f, 9.0f, 10.0f);
174 
175  Matrix3 m1(0.0f, 1.0f, 2.0f,
176  4.0f, 5.0f, 6.0f,
177  8.0f, 9.0f, 10.0f);
178 
179  Matrix3 m2;
180  m2 = m0;
181 
182  DALI_TEST_EQUALS(m1, m2, 0.001, TEST_LOCATION);
183  END_TEST;
184 }
185 
186 
187 
188 // AsFloat
190 {
191  float values[] = {0.0f, 1.0f, 2.0f,
192  4.0f, 5.0f, 6.0f,
193  8.0f, 9.0f, 10.0f };
194 
195  Matrix3 m1(values[0], values[1], values[2], values[3],values[4], values[5], values[6], values[7],values[8]);
196 
197  for (int i=0;i<9;++i)
198  {
199  DALI_TEST_EQUALS(m1.AsFloat()[i], values[i], TEST_LOCATION);
200  }
201  END_TEST;
202 }
203 
204 
205 // Invert works
207 {
208  // We're going to invert a whole load of different matrices to make sure we don't
209  // fail on particular orientations.
210  for (int i=0;i<5;++i)
211  {
212  Matrix3 m = *matrices[i];
213  Matrix3 inverseResult1 = *inverseMatrices[i];
214 
215  // Convert to Mat4, perform inverse, and convert back to Mat3
216  float* mf = m.AsFloat();
217  float els[] = { mf[0], mf[1], mf[2], 0.0f,
218  mf[3], mf[4], mf[5], 0.0f,
219  mf[6], mf[7], mf[8], 0.0f,
220  0.0f, 0.0f, 0.0f, 1.0f };
221  Matrix mat4(els);
222  mat4.Invert();
223  Matrix3 inverseResult2 = mat4;
224 
225  Matrix3 mInv = m;
226  mInv.Invert();
227 
228  DALI_TEST_EQUALS(mInv, inverseResult1, 0.01f, TEST_LOCATION);
229  DALI_TEST_EQUALS(mInv, inverseResult2, 0.01f, TEST_LOCATION);
230 
231  Matrix3 m2 = mInv;
232  m2.Invert(); // double invert - should be back to m
233 
234  DALI_TEST_EQUALS(m, m2, 0.01f, TEST_LOCATION);
235  }
236  END_TEST;
237 }
238 
240 {
241  for (int i=0;i<5;++i)
242  {
243  Matrix3 m0 = *matrices[i];
244  Matrix3 trans = *transposeMatrices[i];
245 
246  Matrix3 m1 = m0;
247  m1.Transpose();
248 
249  DALI_TEST_EQUALS(m1, trans, 0.001f, TEST_LOCATION);
250 
251  Matrix3 m2 = m1;
252  m2.Transpose();
253 
254  DALI_TEST_EQUALS(m0, m2, 0.001f, TEST_LOCATION);
255  }
256  END_TEST;
257 }
258 
259 // SetIdentity
261 {
262  Matrix3 m( 0.0f, 1.0f, 2.0f,
263  4.0f, 5.0f, 6.0f,
264  8.0f, 9.0f, 10.0f);
265  m.SetIdentity();
266 
268  END_TEST;
269 }
270 
271 
273 {
274  Matrix3 m1( 0.0f, 1.0f, 2.0f,
275  4.0f, 5.0f, 6.0f,
276  8.0f, 9.0f, 10.0f);
277 
278  Matrix3 m2( 0.0f, 3.0f, 6.0f,
279  12.0f, 15.0f, 18.0f,
280  24.0f, 27.0f, 30.0f);
281 
282  m1.Scale(3.0f);
283 
284  DALI_TEST_EQUALS(m1, m2, 0.001, TEST_LOCATION);
285  END_TEST;
286 }
287 
289 {
290  Matrix3 m1( 0.0f, 1.0f, -2.0f,
291  3.0f, -4.0f, 5.0f,
292  -6.0f, 7.0f, 8.0f);
293 
294  DALI_TEST_EQUALS(Matrix3::IDENTITY.Magnitude(), 1.0f, 0.001f, TEST_LOCATION);
295  DALI_TEST_EQUALS(m1.Magnitude(), 12.0f, 0.001f, TEST_LOCATION);
296  END_TEST;
297 }
298 
299 
300 
302 {
303  Matrix3* matrices[6] = { &m1, &m2, &m3, &m4, &m5, &m6 };
304 
305 
306  for (int i=0;i<6;++i)
307  {
308  Matrix3 m0 = *matrices[i];
309 
310  Matrix3 m1 = m0;
311  m1.Invert();
312  m1.Transpose();
313  m1.Scale(3.0f/(m1.Magnitude()));
314 
315  Matrix3 m2 = m0;
317 
318  DALI_TEST_EQUALS(m1, m2, 0.001f, TEST_LOCATION);
319  }
320  END_TEST;
321 }
322 
324 {
325  std::ostringstream oss;
326 
327  Matrix3 matrix( 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f );
328 
329  oss << matrix;
330 
331  std::string expectedOutput = "[ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]";
332 
333  DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
334  END_TEST;
335 }
336 
338 {
339  Matrix3 m1( 0.0f, 3.0f, 6.0f,
340  12.0f, 15.0f, 18.0f,
341  24.0f, 27.0f, 30.0f);
342 
343  Matrix3 m2( 0.0f, 1.0f, 0.0f,
344  -1.0f, 0.0f, 0.0f,
345  0.0f, 0.0f, 1.0f);
346 
347  Matrix3 m3( -3.0f, 0.0f, 6.0f,
348  -15.0f, 12.0f, 18.0f,
349  -27.0f, 24.0f, 30.0f);
350 
351  Matrix3 result;
352  Matrix3::Multiply(result, m1, m2);
353 
354  DALI_TEST_EQUALS(m3, result, 0.01f, TEST_LOCATION);
355  END_TEST;
356 }
357 
359 {
360  Matrix3 m1( 0.0f, 3.0f, 6.0f,
361  12.0f, 15.0f, 18.0f,
362  24.0f, 27.0f, 30.0f);
363 
364  Matrix3 m2( 0.0f, 3.0f, 6.0f,
365  12.0f, 15.0f, 18.0f,
366  24.0f, 27.0f, 30.0f);
367 
368  DALI_TEST_CHECK(m1 == m2);
369  END_TEST;
370 }
371 
373 {
374  Matrix3 m1( 1.0f, 0.0f, 0.0f,
375  0.0f, 1.0f, 0.0f,
376  0.0f, 0.0f, 1.0f);
377 
378  Matrix3 m2( 0.0f, 3.0f, 6.0f,
379  12.0f, 15.0f, 18.0f,
380  24.0f, 27.0f, 30.0f);
381 
382  DALI_TEST_CHECK(m1 != m2);
383  END_TEST;
384 }
Dali Docs Home
Read more about Dali