UTC tests; PropertyValue, Vector2/3/4, Matrix
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-Matrix.cpp
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 #include <sstream>
20
21 #include <stdlib.h>
22 #include <dali/public-api/dali-core.h>
23 #include <dali-test-suite-utils.h>
24
25 using namespace Dali;
26
27
28 void utc_dali_matrix_startup(void)
29 {
30   test_return_value = TET_UNDEF;
31 }
32
33 void utc_dali_matrix_cleanup(void)
34 {
35   test_return_value = TET_PASS;
36 }
37
38
39 int UtcDaliMatrixConstructor01P(void)
40 {
41   Matrix m2(false);
42
43   bool initialised = true;
44   {
45     float* els = m2.AsFloat();
46     for(size_t idx=0; idx<16; ++idx, ++els)
47     {
48       if(*els != 0.0f)
49         initialised = false;
50     }
51   }
52
53   DALI_TEST_EQUALS(initialised, false, TEST_LOCATION);
54
55   END_TEST;
56 }
57
58 int UtcDaliMatrixConstructor02P(void)
59 {
60   float r[] = { 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f};
61   Matrix m(r);
62
63   float* els = m.AsFloat();
64   float* init = r;
65   bool initialised = true;
66   for(size_t idx=0; idx<16; ++idx, ++els, ++init)
67   {
68     if(*els != *init)
69       initialised = false;
70   }
71   DALI_TEST_EQUALS(initialised, true, TEST_LOCATION);
72
73   END_TEST;
74 }
75
76 int UtcDaliMatrixConstructor03P(void)
77 {
78   float r[] = { 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f};
79
80   Matrix ma(r);
81   Matrix mb(ma);
82
83   float* els = ma.AsFloat();
84   float* init = mb.AsFloat();
85   bool initialised = true;
86   for(size_t idx=0; idx<16; ++idx, ++els, ++init)
87   {
88     if(*els != *init)
89       initialised = false;
90   }
91   DALI_TEST_EQUALS(initialised, true, TEST_LOCATION);
92
93   END_TEST;
94 }
95
96 int UtcDaliMatrixConstructor04P(void)
97 {
98   Quaternion q(Quaternion::IDENTITY);
99   Matrix m(q);
100   DALI_TEST_EQUALS(Matrix(Matrix::IDENTITY), m, 0.001, TEST_LOCATION);
101   END_TEST;
102 }
103
104 int UtcDaliMatrixAssignP(void)
105 {
106   Matrix a(Matrix::IDENTITY);
107   Matrix b = a;
108   DALI_TEST_EQUALS(a, b, 0.001, TEST_LOCATION);
109   END_TEST;
110 }
111
112 int UtcDaliMatrixSetIdentityP(void)
113 {
114   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
115                   4.0f,  5.0f,  6.0f,  7.0f,
116                   8.0f,  9.0f, 10.0f, 11.0f,
117                   12.0f, 13.0f, 14.0f, 15.0f };
118   Matrix m(els);
119   m.SetIdentity();
120
121   DALI_TEST_EQUALS(m, Matrix::IDENTITY, 0.001f, TEST_LOCATION);
122   END_TEST;
123 }
124
125 int UtcDaliMatrixSetIdentityAndScaleP(void)
126 {
127   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
128                   4.0f,  5.0f,  6.0f,  7.0f,
129                   8.0f,  9.0f, 10.0f, 11.0f,
130                   12.0f, 13.0f, 14.0f, 15.0f };
131   Matrix m(els);
132   m.SetIdentityAndScale(Vector3(4.0f, 4.0f, 4.0f));
133
134   float els2[] = { 4.0f, 0.0f, 0.0f, 0.0f,
135                    0.0f, 4.0f, 0.0f, 0.0f,
136                    0.0f, 0.0f, 4.0f, 0.0f,
137                    0.0f, 0.0f, 0.0f, 1.0f };
138   Matrix r(els2);
139
140   DALI_TEST_EQUALS(m, r, 0.001f, TEST_LOCATION);
141   END_TEST;
142 }
143
144 int UtcDaliMatrixInvertTransform01P(void)
145 {
146   for (int i=0;i<1000;++i)
147   {
148     float f = i;
149     Vector3 axis(cosf(f*0.001f), cosf(f*0.02f), cosf(f*0.03f));
150     axis.Normalize();
151     Vector3 center(f, cosf(f) * 100.0f, cosf(f*0.5f) * 50.0f);
152
153     Matrix m0;
154     m0.SetIdentity();
155     m0.SetTransformComponents( Vector3::ONE, Quaternion(Radian(1.0f), axis), center );
156
157     Matrix m1;
158     m0.InvertTransform(m1);
159
160     Matrix m2( false );
161     Matrix::Multiply( m2, m0, m1 );
162
163     DALI_TEST_EQUALS(m2, Matrix::IDENTITY, 0.001f, TEST_LOCATION);
164   }
165   END_TEST;
166 }
167
168 int UtcDaliMatrixInvertTransform02P(void)
169 {
170   std::string exceptionString( "EqualsZero( mMatrix[3] ) && EqualsZero( mMatrix[7] ) && EqualsZero( mMatrix[11] ) && Equals( mMatrix[15], 1.0f" );
171   try
172   {
173     float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
174                     4.0f,  5.0f,  6.0f,  7.0f,
175                     8.0f,  9.0f, 10.0f, 11.0f,
176                     12.0f, 13.0f, 14.0f, 15.0f };
177     Matrix m(els);
178
179     Matrix it;
180     m.InvertTransform(it);
181     tet_result(TET_FAIL);
182   }
183   catch (Dali::DaliException& e)
184   {
185     DALI_TEST_PRINT_ASSERT( e );
186     DALI_TEST_ASSERT( e, exceptionString, TEST_LOCATION );
187   }
188
189   try
190   {
191     float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
192                     4.0f,  5.0f,  6.0f,  7.0f,
193                     8.0f,  9.0f, 10.0f, 11.0f,
194                     12.0f, 13.0f, 14.0f, 15.0f };
195     Matrix m(els);
196
197     Matrix it;
198     m.InvertTransform(it);
199     tet_result(TET_FAIL);
200   }
201   catch (Dali::DaliException& e)
202   {
203     DALI_TEST_PRINT_ASSERT( e );
204     DALI_TEST_ASSERT( e, exceptionString, TEST_LOCATION );
205   }
206
207   try
208   {
209     float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
210                     4.0f,  5.0f,  6.0f,  7.0f,
211                     8.0f,  9.0f, 10.0f, 11.0f,
212                     12.0f, 13.0f, 14.0f, 15.0f };
213     Matrix m(els);
214
215     Matrix it;
216     m.InvertTransform(it);
217     tet_result(TET_FAIL);
218   }
219   catch (Dali::DaliException& e)
220   {
221     DALI_TEST_PRINT_ASSERT( e );
222     DALI_TEST_ASSERT( e, exceptionString, TEST_LOCATION );
223   }
224
225   try
226   {
227     float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
228                     4.0f,  5.0f,  6.0f,  7.0f,
229                     8.0f,  9.0f, 10.0f, 11.0f,
230                     12.0f, 13.0f, 14.0f, 15.0f };
231     Matrix m(els);
232
233     Matrix it;
234     m.InvertTransform(it);
235     tet_result(TET_FAIL);
236   }
237   catch (Dali::DaliException& e)
238   {
239     DALI_TEST_PRINT_ASSERT( e );
240     DALI_TEST_ASSERT( e, exceptionString, TEST_LOCATION );
241   }
242   END_TEST;
243 }
244
245 int UtcDaliMatrixInvert01P(void)
246 {
247   // We're going to invert a whole load of different matrices to make sure we don't
248   // fail on particular orientations.
249   for (int i=0;i<1000;++i)
250   {
251     float f = i;
252     Vector3 axis(cosf(f*0.001f), cosf(f*0.02f), cosf(f*0.03f));
253     axis.Normalize();
254     Vector3 center(f, cosf(f) * 100.0f, cosf(f*0.5f) * 50.0f);
255
256     Matrix m0;
257     m0.SetIdentity();
258     m0.SetTransformComponents( Vector3::ONE, Quaternion(Radian(1.0f), axis), center );
259
260     Matrix m1(m0);
261     m1.Invert();
262
263     Matrix m2( false );
264     Matrix::Multiply( m2, m0, m1 );
265
266     DALI_TEST_EQUALS(m2, Matrix::IDENTITY, 0.001f, TEST_LOCATION);
267
268     m1.Invert();    // doube invert - should be back to m0
269
270     DALI_TEST_EQUALS(m0, m1, 0.001f, TEST_LOCATION);
271   }
272   END_TEST;
273 }
274
275 int UtcDaliMatrixInvert02P(void)
276 {
277   Matrix m1 = Matrix::IDENTITY;
278   m1.SetXAxis(Vector3(0.0f, 0.0f, 0.0f));
279   DALI_TEST_EQUALS(m1.Invert(), false, TEST_LOCATION);
280   END_TEST;
281 }
282
283 int UtcDaliMatrixTransposeP(void)
284 {
285   float floats[] =
286   {   0.0f,  1.0f,  2.0f,  3.0f,
287       4.0f,  5.0f,  6.0f,  7.0f,
288       8.0f,  9.0f, 10.0f,  11.0f,
289      12.0f, 13.0f, 14.0f,  15.0f
290   };
291
292   Matrix m(floats);
293   m.Transpose();
294
295   bool success = true;
296
297   for (int x=0;x<4;++x)
298   {
299     for (int y=0;y<4;++y)
300     {
301       success &= (m.AsFloat()[x+y*4] == floats[x*4+y]);
302     }
303   }
304
305   DALI_TEST_CHECK(success);
306   END_TEST;
307 }
308
309 int UtcDaliMatrixGetXAxisP(void)
310 {
311   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
312                   4.0f,  5.0f,  6.0f,  7.0f,
313                   8.0f,  9.0f, 10.0f, 11.0f,
314                   12.0f, 13.0f, 14.0f, 15.0f };
315   Matrix m(els);
316
317   DALI_TEST_CHECK(m.GetXAxis() == Vector3(0.0f,  1.0f,  2.0f));
318   END_TEST;
319 }
320
321 int UtcDaliMatrixGetYAxisP(void)
322 {
323   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
324                   4.0f,  5.0f,  6.0f,  7.0f,
325                   8.0f,  9.0f, 10.0f, 11.0f,
326                   12.0f, 13.0f, 14.0f, 15.0f };
327   Matrix m(els);
328
329   DALI_TEST_CHECK(m.GetYAxis() == Vector3(4.0f,  5.0f,  6.0f));
330   END_TEST;
331 }
332
333 int UtcDaliMatrixGetZAxisP(void)
334 {
335   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
336                   4.0f,  5.0f,  6.0f,  7.0f,
337                   8.0f,  9.0f, 10.0f, 11.0f,
338                   12.0f, 13.0f, 14.0f, 15.0f };
339   Matrix m(els);
340
341   DALI_TEST_CHECK(m.GetZAxis() == Vector3(8.0f,  9.0f, 10.0f));
342   END_TEST;
343 }
344
345 int UtcDaliMatrixSetXAxisP(void)
346 {
347   Matrix m;
348   Vector3 v(2.0f, 3.0f, 4.0f);
349   m.SetXAxis(v);
350
351   DALI_TEST_CHECK(m.GetXAxis() == v);
352   END_TEST;
353 }
354
355 int UtcDaliMatrixSetYAxisP(void)
356 {
357   Matrix m;
358   Vector3 v(2.0f, 3.0f, 4.0f);
359   m.SetYAxis(v);
360
361   DALI_TEST_CHECK(m.GetYAxis() == v);
362   END_TEST;
363 }
364
365 int UtcDaliMatrixSetZAxisP(void)
366 {
367   Matrix m;
368   Vector3 v(2.0f, 3.0f, 4.0f);
369   m.SetZAxis(v);
370
371   DALI_TEST_CHECK(m.GetZAxis() == v);
372   END_TEST;
373 }
374
375 int UtcDaliMatrixGetTranslationP(void)
376 {
377   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
378                   4.0f,  5.0f,  6.0f,  7.0f,
379                   8.0f,  9.0f, 10.0f, 11.0f,
380                   12.0f, 13.0f, 14.0f, 15.0f };
381   Matrix m(els);
382
383   DALI_TEST_EQUALS(m.GetTranslation(), Vector4(12.0f, 13.0f, 14.0f, 15.0f), TEST_LOCATION);
384   END_TEST;
385 }
386
387 int UtcDaliMatrixGetTranslation3P(void)
388 {
389   float els[] = { 0.0f,  1.0f,  2.0f,  3.0f,
390                   4.0f,  5.0f,  6.0f,  7.0f,
391                   8.0f,  9.0f, 10.0f, 11.0f,
392                   12.0f, 13.0f, 14.0f, 15.0f };
393   Matrix m(els);
394
395   DALI_TEST_EQUALS(m.GetTranslation3(), Vector3(12.0f, 13.0f, 14.0f), TEST_LOCATION);
396   END_TEST;
397 }
398
399 int UtcDaliMatrixSetTranslationP(void)
400 {
401   Matrix m;
402   Vector4 v(2.0f, 3.0f, 4.0f, 5.0f);
403   m.SetTranslation(v);
404
405   DALI_TEST_CHECK(m.GetTranslation() == v);
406   END_TEST;
407 }
408
409 int UtcDaliMatrixSetTranslation3P(void)
410 {
411   Matrix m;
412   Vector3 v(2.0f, 3.0f, 4.0f);
413   m.SetTranslation(v);
414
415   DALI_TEST_CHECK(m.GetTranslation3() == v);
416   END_TEST;
417 }
418
419 int UtcDaliMatrixOrthoNormalize0P(void)
420 {
421   // OrthoNormalise fixes floating point errors from matrix rotations
422   Matrix m;
423   m.SetIdentity();
424
425   for (int i=0;i<1000;++i)
426   {
427     float f = i;
428     Vector3 axis(cosf(f*0.001f), cosf(f*0.02f), cosf(f*0.03f));
429     axis.Normalize();
430
431     m.SetTransformComponents( Vector3::ONE, Quaternion(Radian(1.0f), axis), Vector3::ZERO );
432     m.OrthoNormalize();
433   }
434
435   bool success = true;
436   success &= fabsf(m.GetXAxis().Dot(m.GetYAxis())) < 0.001f;
437   success &= fabsf(m.GetYAxis().Dot(m.GetXAxis())) < 0.001f;
438   success &= fabsf(m.GetZAxis().Dot(m.GetYAxis())) < 0.001f;
439
440   success &= fabsf(m.GetXAxis().Length() - 1.0f) < 0.001f;
441   success &= fabsf(m.GetYAxis().Length() - 1.0f) < 0.001f;
442   success &= fabsf(m.GetZAxis().Length() - 1.0f) < 0.001f;
443
444   DALI_TEST_CHECK(success);
445   END_TEST;
446 }
447
448 int UtcDaliMatrixOrthoNormalize1P(void)
449 {
450   // OrthoNormalize is not flipping the axes and is maintaining the translation
451   for (int i=0;i<1000;++i)
452   {
453     float f = i;
454     Vector3 axis(cosf(f*0.001f), cosf(f*0.02f), cosf(f*0.03f));
455     axis.Normalize();
456     Vector3 center(10.0f, 15.0f, 5.0f);
457
458     Matrix m0;
459     m0.SetIdentity();
460     m0.SetTransformComponents( Vector3::ONE, Quaternion(Radian(1.0f), axis), center );
461
462     Matrix m1(m0);
463     m1.OrthoNormalize();
464
465     DALI_TEST_EQUALS(m0.GetXAxis(), m1.GetXAxis(), 0.001f, TEST_LOCATION);
466     DALI_TEST_EQUALS(m0.GetYAxis(), m1.GetYAxis(), 0.001f, TEST_LOCATION);
467     DALI_TEST_EQUALS(m0.GetZAxis(), m1.GetZAxis(), 0.001f, TEST_LOCATION);
468     DALI_TEST_EQUALS(m0.GetTranslation(), m1.GetTranslation(), 0.001f, TEST_LOCATION);
469   }
470   END_TEST;
471 }
472
473 int UtcDaliMatrixConstAsFloatP(void)
474 {
475   float r[] = { 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f};
476   const Matrix m(r);
477
478   const float* els = m.AsFloat();
479   const float* init = r;
480   bool initialised = true;
481   for(size_t idx=0; idx<16; ++idx, ++els, ++init)
482   {
483     if(*els != *init)
484       initialised = false;
485   }
486   DALI_TEST_EQUALS(initialised, true, TEST_LOCATION);
487
488
489   END_TEST;
490 }
491
492 int UtcDaliMatrixAsFloatP(void)
493 {
494   float r[] = { 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 2.0f, 3.0f, 4.0f};
495   Matrix m(r);
496
497   float* els = m.AsFloat();
498   float* init = r;
499   bool initialised = true;
500   for(size_t idx=0; idx<16; ++idx, ++els, ++init)
501   {
502     if(*els != *init)
503       initialised = false;
504   }
505   DALI_TEST_EQUALS(initialised, true, TEST_LOCATION);
506
507
508   END_TEST;
509 }
510
511 int UtcDaliMatrixMultiplyP(void)
512 {
513   Matrix m1 = Matrix::IDENTITY;
514
515   float els[] = { 1.0f, 0.0f,    0.0f,   0.0f,
516                   0.0f, 0.707f, 0.707f, 0.0f,
517                   0.0f, -0.707f,  0.707f, 0.0f,
518                   0.0f, 0.0f,    0.0f,   1.0f };
519   Matrix result(els);
520
521   Quaternion q(Radian(Degree(45.0f)), Vector3::XAXIS);
522   Matrix m2(false);
523   Matrix::Multiply(m2, m1, q);
524
525   DALI_TEST_EQUALS(m2, result, 0.01f, TEST_LOCATION);
526   END_TEST;
527 }
528
529 int UtcDaliMatrixOperatorMultiply01P(void)
530 {
531   Vector4 v1(2.0f, 5.0f, 4.0f, 0.0f);
532
533   float els[] = {2.0f, 0.0f, 0.0f, 0.0f,
534                  0.0f, 3.0f, 0.0f, 0.0f,
535                  0.0f, 0.0f, 4.0f, 0.0f,
536                  0.0f, 0.0f, 0.0f, 1.0f };
537   Matrix m1(els);
538
539   Vector4 v2 = m1 * v1;
540   Vector4 r1(4.0f, 15.0f, 16.0f, 0.0f);
541   DALI_TEST_EQUALS(v2, r1, 0.01f, TEST_LOCATION);
542   END_TEST;
543 }
544
545 int UtcDaliMatrixOperatorMultiply02P(void)
546 {
547   TestApplication application;
548
549   Vector3 position ( 30.f, 40.f, 50.f);
550
551   Matrix m1(false);
552   m1.SetIdentity();
553   m1.SetTranslation(-position);
554
555   Vector4 positionV4(position);
556   positionV4.w=1.0f;
557   Vector4 output = m1 * positionV4;
558
559   output.w = 0.0f;
560   DALI_TEST_EQUALS(output, Vector4::ZERO, 0.01f, TEST_LOCATION);
561   END_TEST;
562 }
563
564 int UtcDaliMatrixOperatorEqualsP(void)
565 {
566   Matrix m1 = Matrix::IDENTITY;
567
568   float els[] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f};
569   Matrix r2(els);
570   DALI_TEST_EQUALS(m1 == r2, true, TEST_LOCATION);
571
572   float *f = m1.AsFloat();
573   for(size_t i=0; i<16; i++)
574   {
575     f[15-i] = 1.2f;
576     DALI_TEST_EQUALS(m1 == r2, false, TEST_LOCATION);
577   }
578   END_TEST;
579 }
580
581 int UtcDaliMatrixOperatorNotEqualsP(void)
582 {
583   Matrix m1 = Matrix::IDENTITY;
584   float els[] = {2.0f, 0.0f, 0.0f, 0.0f,
585                  0.0f, 3.0f, 0.0f, 0.0f,
586                  0.0f, 0.0f, 4.0f, 0.0f,
587                  0.0f, 0.0f, 0.0f, 1.0f };
588   Matrix r1(els);
589
590   DALI_TEST_CHECK(m1 != r1);
591   DALI_TEST_CHECK(!(m1 != m1));
592   END_TEST;
593 }
594
595 int UtcDaliMatrixSetTransformComponents01P(void)
596 {
597   // Create an arbitrary vector
598   for( float x=-1.0f; x<=1.0f; x+=0.1f )
599   {
600     for( float y=-1.0f; y<1.0f; y+=0.1f )
601     {
602       for( float z=-1.0f; z<1.0f; z+=0.1f )
603       {
604         Vector3 vForward(x, y, z);
605         vForward.Normalize();
606
607         for( float angle = 5.0f; angle <= 360.0f; angle += 15.0f)
608         {
609           Quaternion rotation1(Radian(Degree(angle)), vForward);
610
611           Matrix m1(rotation1);
612           Matrix result1(false);
613           Vector3 vForward3(vForward.x, vForward.y, vForward.z);
614           result1.SetTransformComponents( Vector3::ONE, Quaternion(Radian(Degree(angle)), vForward3), Vector3::ZERO );
615
616           DALI_TEST_EQUALS(m1, result1, 0.001, TEST_LOCATION);
617
618           Matrix m2(false);
619           m2.SetTransformComponents(vForward, Quaternion::IDENTITY, Vector3::ZERO);
620
621           Matrix result2a(Matrix::IDENTITY);
622           result2a.SetXAxis(result2a.GetXAxis() * vForward[0]);
623           result2a.SetYAxis(result2a.GetYAxis() * vForward[1]);
624           result2a.SetZAxis(result2a.GetZAxis() * vForward[2]);
625
626           DALI_TEST_EQUALS(m2, result2a, 0.001, TEST_LOCATION);
627
628           Matrix m3(false);
629           m3.SetTransformComponents(vForward, rotation1, Vector3::ZERO);
630
631           Matrix result3(Matrix::IDENTITY);
632           result3.SetXAxis(result3.GetXAxis() * vForward[0]);
633           result3.SetYAxis(result3.GetYAxis() * vForward[1]);
634           result3.SetZAxis(result3.GetZAxis() * vForward[2]);
635
636           Matrix::Multiply(result3, result3, m1);
637           DALI_TEST_EQUALS(m3, result3, 0.001, TEST_LOCATION);
638         }
639       }
640     }
641   }
642   END_TEST;
643 }
644
645
646 int UtcDaliMatrixSetInverseTransformComponent01P(void)
647 {
648   // Create an arbitrary vector
649   for( float x=-1.0f; x<=1.0f; x+=0.1f )
650   {
651     for( float y=-1.0f; y<1.0f; y+=0.1f )
652     {
653       for( float z=-1.0f; z<1.0f; z+=0.1f )
654       {
655         Vector3 vForward(x, y, z);
656         vForward.Normalize();
657
658         for( float angle = 5.0f; angle <= 360.0f; angle += 15.0f)
659         {
660           Quaternion rotation1(Radian(Degree(angle)), vForward);
661           Vector3 scale1(2.0f, 3.0f, 4.0f);
662           Vector3 position1(1.0f, 2.0f, 3.0f);
663
664           Matrix m1(false);
665           m1.SetTransformComponents(scale1, rotation1, position1);
666
667           Matrix m2(false);
668           m2.SetInverseTransformComponents(scale1, rotation1, position1);
669
670           Matrix result;
671           Matrix::Multiply(result, m1, m2);
672
673           DALI_TEST_EQUALS(result, Matrix::IDENTITY, 0.001, TEST_LOCATION);
674         }
675       }
676     }
677   }
678   END_TEST;
679 }
680
681 int UtcDaliMatrixSetInverseTransformComponent02P(void)
682 {
683   // Create an arbitrary vector
684   for( float x=-1.0f; x<=1.0f; x+=0.1f )
685   {
686     for( float y=-1.0f; y<1.0f; y+=0.1f )
687     {
688       for( float z=-1.0f; z<1.0f; z+=0.1f )
689       {
690         Vector3 vForward(x, y, z);
691         vForward.Normalize();
692
693         for( float angle = 5.0f; angle <= 360.0f; angle += 15.0f)
694         {
695           Quaternion rotation1(Radian(Degree(angle)), vForward);
696           Matrix rotationMatrix(rotation1);   // TEST RELIES ON THIS METHOD WORKING!!!
697
698           Vector3 position1(5.0f, -6.0f, 7.0f);
699
700           Matrix m1(false);
701           m1.SetTransformComponents( Vector3::ONE, rotation1, position1 );
702
703           Matrix m2(false);
704           m2.SetInverseTransformComponents( rotationMatrix.GetXAxis(),
705                                             rotationMatrix.GetYAxis(),
706                                             rotationMatrix.GetZAxis(),
707                                             position1 );
708
709           Matrix result;
710           Matrix::Multiply(result, m1, m2);
711
712           DALI_TEST_EQUALS(result, Matrix::IDENTITY, 0.001, TEST_LOCATION);
713         }
714       }
715     }
716   }
717   END_TEST;
718 }
719
720 int UtcDaliMatrixGetTransformComponents01P(void)
721 {
722   Matrix m2(Matrix::IDENTITY.AsFloat());
723   Vector3 pos2;
724   Vector3 scale2;
725   Quaternion q2;
726   m2.GetTransformComponents(pos2, q2, scale2);
727   DALI_TEST_EQUALS(Vector3(0.0f, 0.0f, 0.0f), pos2, 0.001, TEST_LOCATION);
728   DALI_TEST_EQUALS(Vector3(1.0f, 1.0f, 1.0f), scale2, 0.001, TEST_LOCATION);
729   DALI_TEST_EQUALS(Quaternion(), q2, 0.001, TEST_LOCATION);
730   END_TEST;
731 }
732
733
734 int UtcDaliMatrixGetTransformComponents02P(void)
735 {
736   // Create an arbitrary vector
737   for( float x=-1.0f; x<=1.0f; x+=0.1f )
738   {
739     for( float y=-1.0f; y<1.0f; y+=0.1f )
740     {
741       for( float z=-1.0f; z<1.0f; z+=0.1f )
742       {
743         Vector3 vForward(x, y, z);
744         vForward.Normalize();
745
746         for( float angle = 5.0f; angle <= 360.0f; angle += 15.0f)
747         {
748           Quaternion rotation1(Radian(Degree(angle)), vForward);
749           Vector3 scale1(2.0f, 3.0f, 4.0f);
750           Vector3 position1(1.0f, 2.0f, 3.0f);
751
752           Matrix m1(false);
753           m1.SetTransformComponents(scale1, rotation1, position1);
754
755           Vector3 position2;
756           Quaternion rotation2;
757           Vector3 scale2;
758           m1.GetTransformComponents(position2, rotation2, scale2);
759
760           DALI_TEST_EQUALS(position1, position2, 0.001, TEST_LOCATION);
761           DALI_TEST_EQUALS(scale1, scale2, 0.001, TEST_LOCATION);
762           DALI_TEST_EQUALS(rotation1, rotation2, 0.001, TEST_LOCATION);
763         }
764       }
765     }
766   }
767   END_TEST;
768 }
769
770
771 int UtcDaliMatrixOStreamOperator(void)
772 {
773   std::ostringstream oss;
774
775   Matrix matrix;
776   matrix.SetIdentity();
777
778   oss << matrix;
779
780   std::string expectedOutput = "[ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1] ]";
781
782   DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
783   END_TEST;
784 }