Dali 3D User Interface Engine
utc-Dali-Vector3.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 #include <sstream>
20 #include <cmath> // isfinite
21 
22 #include <stdlib.h>
24 #include <dali-test-suite-utils.h>
25 
26 using namespace Dali;
27 
29 {
31 }
32 
34 {
36 }
37 
39 {
40  TestApplication application;
41  Vector3 va;
42  DALI_TEST_EQUALS(va.x, 0.0f, 0.001f, TEST_LOCATION);
43  DALI_TEST_EQUALS(va.y, 0.0f, 0.001f, TEST_LOCATION);
44  DALI_TEST_EQUALS(va.z, 0.0f, 0.001f, TEST_LOCATION);
45  END_TEST;
46 }
47 
49 {
50  TestApplication application;
51  Vector3 va(1.f, 1.f, 1.f);
52  DALI_TEST_EQUALS(va.x, 1.0f, 0.001f, TEST_LOCATION);
53  DALI_TEST_EQUALS(va.y, 1.0f, 0.001f, TEST_LOCATION);
54  DALI_TEST_EQUALS(va.z, 1.0f, 0.001f, TEST_LOCATION);
55  END_TEST;
56 }
57 
59 {
60  TestApplication application;
61  float array [] = {1.f, 1.f, 1.f};
62  Vector3 va(array);
63  DALI_TEST_EQUALS(va.x, 1.0f, 0.001f, TEST_LOCATION);
64  DALI_TEST_EQUALS(va.y, 1.0f, 0.001f, TEST_LOCATION);
65  DALI_TEST_EQUALS(va.z, 1.0f, 0.001f, TEST_LOCATION);
66  END_TEST;
67 }
68 
70 {
71  TestApplication application;
72  Vector2 vec2(1.f,1.f);
73  Vector3 va(vec2);
74  DALI_TEST_EQUALS(va.x, 1.0f, 0.001f, TEST_LOCATION);
75  DALI_TEST_EQUALS(va.y, 1.0f, 0.001f, TEST_LOCATION);
76  DALI_TEST_EQUALS(va.z, 0.0f, 0.001f, TEST_LOCATION);
77  END_TEST;
78 }
79 
81 {
82  TestApplication application;
83  Vector4 vec4(1.f, 1.f, 1.f, 1.f);
84  Vector3 va(vec4);
85  DALI_TEST_EQUALS(va.x, 1.0f, 0.001f, TEST_LOCATION);
86  DALI_TEST_EQUALS(va.y, 1.0f, 0.001f, TEST_LOCATION);
87  DALI_TEST_EQUALS(va.z, 1.0f, 0.001f, TEST_LOCATION);
88  END_TEST;
89 }
90 
92 {
93  TestApplication application;
94  Vector3 v0;
95  const float array[] = { 1.0f, 2.0f, 3.0f };
96  v0 = (const float*)array;
97 
98  DALI_TEST_EQUALS(v0.x, 1.0f, 0.001f, TEST_LOCATION);
99  DALI_TEST_EQUALS(v0.y, 2.0f, 0.001f, TEST_LOCATION);
100  DALI_TEST_EQUALS(v0.z, 3.0f, 0.001f, TEST_LOCATION);
101  END_TEST;
102 }
103 
105 {
106  TestApplication application;
107  Vector2 vec2_q(1.0f, 2.0f);
108  Vector3 vec3a;
109  vec3a = vec2_q;
110 
111  DALI_TEST_EQUALS(vec3a.x, 1.0f, 0.001f, TEST_LOCATION);
112  DALI_TEST_EQUALS(vec3a.y, 2.0f, 0.001f, TEST_LOCATION);
113  DALI_TEST_EQUALS(vec3a.z, 0.0f, 0.001f, TEST_LOCATION);
114  END_TEST;
115 }
116 
118 {
119  TestApplication application;
120  Vector4 vec4_q(4.0f, 3.0f, 2.0f, 1.0f);
121  Vector3 vec3b;
122  vec3b = vec4_q;
123 
124  DALI_TEST_EQUALS(vec3b.x, 4.0f, 0.001f, TEST_LOCATION);
125  DALI_TEST_EQUALS(vec3b.y, 3.0f, 0.001f, TEST_LOCATION);
126  DALI_TEST_EQUALS(vec3b.z, 2.0f, 0.001f, TEST_LOCATION);
127  END_TEST;
128 }
129 
131 {
132  TestApplication application;
133  Vector3 v0(1.0f, 2.0f, 3.0f);
134  Vector3 v1(10.0f, 20.0f, 30.0f);
135  Vector3 r0(11.0f, 22.0f, 33.0f);
136 
137  Vector3 v2 = v0+v1;
139 
140  END_TEST;
141 }
142 
144 {
145  TestApplication application;
146  Vector3 v0(1.0f, 2.0f, 3.0f);
147  Vector3 v1(10.0f, 20.0f, 30.0f);
148  Vector3 r0(11.0f, 22.0f, 33.0f);
149 
150  v0 += v1;
152 
153  END_TEST;
154 }
155 
157 {
158  TestApplication application;
159  Vector3 v0(11.0f, 22.0f, 33.0f);
160  Vector3 v1(10.0f, 20.0f, 30.0f);
161  Vector3 r0(1.0f, 2.0f, 3.0f);
162 
163  Vector3 v2 = v0-v1;
165 
166  END_TEST;
167 }
168 
170 {
171  TestApplication application;
172  Vector3 v0(11.0f, 22.0f, 33.0f);
173  Vector3 v1(10.0f, 20.0f, 30.0f);
174  Vector3 r0(1.0f, 2.0f, 3.0f);
175 
176  v0 -= v1;
178  END_TEST;
179 }
180 
182 {
183  TestApplication application;
184  Vector3 v0(2.0f, 3.0f, 4.0f);
185  Vector3 v1(10.0f, 20.0f, 30.0f);
186  Vector3 r0(20.0f, 60.0f, 120.0f);
187 
188  Vector3 v2 = v0 * v1;
190  END_TEST;
191 }
192 
194 {
195  TestApplication application;
196  Vector3 v0(2.0f, 3.0f, 4.0f);
197  Vector3 r0(20.0f, 30.0f, 40.0f);
198  Vector3 v2 = v0 * 10.f;
199  DALI_TEST_EQUALS(v2, r0, 0.001, TEST_LOCATION);
200  END_TEST;
201 }
202 
204 {
205  TestApplication application;
206  Vector3 v0(2.0f, 3.0f, 4.0f);
207  Vector3 v1(10.0f, 20.0f, 30.0f);
208  Vector3 r0(20.0f, 60.0f, 120.0f);
209  v0 *= v1;
210  DALI_TEST_EQUALS(v0, r0, 0.001, TEST_LOCATION);
211  END_TEST;
212 }
213 
215 {
216  TestApplication application;
217  Vector3 v0(2.0f, 3.0f, 4.0f);
218  Vector3 r0(20.0f, 30.0f, 40.0f);
219  v0 *= 10.f;
221  END_TEST;
222 }
223 
225 {
226  TestApplication application;
227  Vector3 vec3(Vector3::YAXIS);
229  Vector3 result(-Vector3::XAXIS);
230  vec3 *= rotation;
231  DALI_TEST_EQUALS( vec3, result, 0.001, TEST_LOCATION );
232  END_TEST;
233 }
234 
236 {
237  TestApplication application;
238  Vector3 v0(1.0f, 1.0f, 1.0f);
239  Vector3 v1(2.0f, 3.0f, 5.0f);
240  Vector3 v2(4.0f, 9.0f, 25.0f);
241 
242  DALI_TEST_EQUALS( v0/v0, v0, TEST_LOCATION);
243  DALI_TEST_EQUALS( v1/v0, v1, TEST_LOCATION);
244  DALI_TEST_EQUALS( v1/v1, v0, TEST_LOCATION);
245  DALI_TEST_EQUALS( v2/v1, v1, TEST_LOCATION);
246 
247  END_TEST;
248 }
249 
251 {
252  TestApplication application;
253  Vector3 v0(3.0f, 6.0f, 9.0f);
254  Vector3 v1(1.0f, 2.0f, 3.0f);
255  Vector3 r(3.0f, 3.0f, 3.0f);
256  Vector3 v2 = v0 / v1;
258  END_TEST;
259 }
260 
262 {
263  TestApplication application;
264  Vector3 v0(3.0f, 6.0f, 9.0f);
265  Vector3 v1(1.0f, 2.0f, 3.0f);
266  Vector3 v2 = v0 / 3.f;
268  END_TEST;
269 }
270 
272 {
273  TestApplication application;
274  Vector3 v0(3.0f, 6.0f, 9.0f);
275  Vector3 v1(1.0f, 1.0f, 1.0f);
276  Vector3 v4(v0);
277  v4 /= v0;
279  END_TEST;
280 }
281 
283 {
284  TestApplication application;
285  Vector3 v0(3.0f, 6.0f, 9.0f);
286  Vector3 v1(1.0f, 2.0f, 3.0f);
287  Vector3 v4(v0);
288  v4 /= 3.f;
290  END_TEST;
291 }
292 
293 
295 {
296  TestApplication application;
297  Vector3 v1(10.0f, 20.0f, 30.f);
298  Vector3 r0(-10.0f, -20.0f, -30.f);
299 
300  Vector3 v2 = -v1;
302  END_TEST;
303 }
304 
306 {
307  TestApplication application;
308  Vector3 v0(1.0f, 2.0f, 3.0f);
309  Vector3 v1(1.0f, 2.0f, 3.0f);
310 
311  DALI_TEST_CHECK(v0 == v1);
312  END_TEST;
313 }
314 
316 {
317  TestApplication application;
318  Vector3 v0(1.0f, 2.0f, 3.0f);
319  Vector3 v1(1.0f, 2.0f, 3.0f);
320  Vector3 v2 = Vector3(0.0f, 2.0f, 3.0f);
321  DALI_TEST_CHECK(v0 != v2);
322 
323  v2 = Vector3(1.0f, 0.0f, 3.0f);
324  DALI_TEST_CHECK(v0 != v2);
325 
326  v2 = Vector3(1.0f, 2.0f, 0.0f);
327  DALI_TEST_CHECK(v0 != v2);
328 
329  v2 = Vector3(11.0f, 22.0f, 33.0f);
330  DALI_TEST_CHECK(v0 != v2);
331  END_TEST;
332 }
333 
335 {
336  TestApplication application;
337  Vector3 testVector(1.0f, 2.0f, 3.0f);
338 
339  // read array subscripts
340  DALI_TEST_EQUALS( testVector[0], 1.0f, TEST_LOCATION );
341  DALI_TEST_EQUALS( testVector[1], 2.0f, TEST_LOCATION );
342  DALI_TEST_EQUALS( testVector[2], 3.0f, TEST_LOCATION );
343 
344  // write array subscripts/read struct memebers
345  testVector[0] = 4.0f;
346  testVector[1] = 5.0f;
347  testVector[2] = 6.0f;
348 
349  DALI_TEST_EQUALS( testVector.x, 4.0f, TEST_LOCATION );
350  DALI_TEST_EQUALS( testVector.y, 5.0f, TEST_LOCATION );
351  DALI_TEST_EQUALS( testVector.z, 6.0f, TEST_LOCATION );
352 
353  // write struct members/read array subscripts
354  testVector.x = 7.0f;
355  testVector.y = 8.0f;
356  testVector.z = 9.0f;
357 
358  DALI_TEST_EQUALS( testVector[0], 7.0f, TEST_LOCATION );
359  DALI_TEST_EQUALS( testVector[1], 8.0f, TEST_LOCATION );
360  DALI_TEST_EQUALS( testVector[2], 9.0f, TEST_LOCATION );
361  END_TEST;
362 }
363 
365 {
366  TestApplication application;
367  Vector3 testVector(1.0f, 2.0f, 3.0f);
368 
369  // write struct members/read array subscripts
370  const Vector3 testVector2(1.0f, 2.0f, 3.0f);
371  const float& x = testVector2[0];
372  const float& y = testVector2[1];
373  const float& z ( testVector2[2] );
374 
375  DALI_TEST_EQUALS( x, 1.0f, TEST_LOCATION );
376  DALI_TEST_EQUALS( y, 2.0f, TEST_LOCATION );
377  DALI_TEST_EQUALS( z, 3.0f, TEST_LOCATION );
378 
379  try
380  {
381  float& w = testVector[4];
382  (void)w; // Suppress unused variable warning
384  }
385  catch (Dali::DaliException& e)
386  {
388  DALI_TEST_ASSERT( e, "index < 3", TEST_LOCATION );
389  }
390 
391  try
392  {
393  const float& w = testVector2[4];
394  (void)w; // Suppress unused variable warning
396  }
397  catch (Dali::DaliException& e)
398  {
400  DALI_TEST_ASSERT(e, "index < 3", TEST_LOCATION);
401  }
402 
403  END_TEST;
404 }
405 
407 {
408  TestApplication application;
414 
415  DALI_TEST_EQUALS(Vector3(1.0f, 0.0f, 0.0f).Dot(Vector3(1.0f, 0.0f, 0.0f)), 1.0f, TEST_LOCATION);
416 
417  for (float x = 0; x<6.0f; x+=1.0f)
418  {
419  Vector3 v0(cosf(x), sinf(x), 0.0f);
420  Vector3 v1(sinf(x), -cosf(x), 0.0f);
421  DALI_TEST_EQUALS(v0.Dot(v1), 0.0f, 0.0001f, TEST_LOCATION);
422  DALI_TEST_EQUALS(v0.Dot(v0), 1.0f, 0.0001f, TEST_LOCATION);
423 
424  v0 = Vector3(cosf(x), 0.0f, sinf(x));
425  v1 = Vector3(sinf(x), 0.0f, -cosf(x));
426  DALI_TEST_EQUALS(v0.Dot(v0), 1.0f, 0.0001f, TEST_LOCATION);
427  }
428 
429  Vector3 v0 = Vector3(12.0f, 7.0f, 9.0f);
430  v0.Normalize();
431 
432  Vector3 v1 = v0 * 2.0f;
433  DALI_TEST_EQUALS(v0.Dot(v1), 2.0f, 0.001f, TEST_LOCATION);
434  END_TEST;
435 }
436 
438 {
439  TestApplication application;
443 
447 
448  Vector3 v0(2.0f, 3.0f, 4.0f);
449  Vector3 v1(10.0f, 20.0f, 30.0f);
450  Vector3 result( (v0.y * v1.z) - (v0.z * v1.y),
451  (v0.z * v1.x) - (v0.x * v1.z),
452  (v0.x * v1.y) - (v0.y * v1.x) );
453 
454 
455  DALI_TEST_EQUALS(v0.Cross(v1), result, 0.001f, TEST_LOCATION);
456  END_TEST;
457 }
458 
460 {
461  TestApplication application;
462  Vector3 v(1.0f, 2.0f, 3.0f);
463  DALI_TEST_EQUALS(v.Length(), sqrtf(v.x*v.x + v.y*v.y + v.z*v.z), 0.001f, TEST_LOCATION);
464 
465  Vector3 v1(0.0f, 0.0f, 0.0f);
466  DALI_TEST_EQUALS(v1.Length(), 0.0f, TEST_LOCATION);
467  END_TEST;
468 }
469 
471 {
472  TestApplication application;
473  Vector3 v(1.0f, 2.0f, 3.0f);
474  DALI_TEST_EQUALS(v.LengthSquared(), v.x*v.x + v.y*v.y + v.z*v.z, 0.001f, TEST_LOCATION);
475 
476  Vector3 v1(0.0f, 0.0f, 0.0f);
477  DALI_TEST_EQUALS(v1.LengthSquared(), 0.0f, TEST_LOCATION);
478  END_TEST;
479 }
480 
482 {
483  TestApplication application;
484  for (float f=0.0f; f<6.0f; f+=1.0f)
485  {
486  Vector3 v(cosf(f)*10.0f, cosf(f+1.0f)*10.0f, cosf(f+2.0f)*10.0f);
487  v.Normalize();
488  DALI_TEST_EQUALS(v.LengthSquared(), 1.0f, 0.001f, TEST_LOCATION);
489  }
490 
491  Vector3 v(0.0f, 0.0f, 0.0f);
492  v.Normalize();
493  DALI_TEST_EQUALS(v.LengthSquared(), 0.0f, 0.00001f, TEST_LOCATION);
494  END_TEST;
495 }
496 
498 {
499  TestApplication application;
500 
501  Vector3 v0(2.0f, 0.8f, 0.0f);
502  Vector3 v1(-1.0f, 2.0f, 10.0f);
503  Vector3 v2(10.0f, 5.0f, 0.0f);
504  Vector3 v3(8.0f, 10.0f, 5.0f);
505  Vector3 v4(4.9f, 5.1f, 10.0f);
506  Vector3 min(1.0f, 4.0f, 1.5f);
507  Vector3 max(9.0f, 6.0f, 8.0f);
508 
509  v0.Clamp( min, max );
510  v1.Clamp( min, max );
511  v2.Clamp( min, max );
512  v3.Clamp( min, max );
513  v4.Clamp( min, max );
514 
515  DALI_TEST_EQUALS( v0, Vector3(2.0f, 4.0f, 1.5f), 0.01f, TEST_LOCATION );
516  DALI_TEST_EQUALS( v1, Vector3(1.0f, 4.0f, 8.0f), 0.01f, TEST_LOCATION );
517  DALI_TEST_EQUALS( v2, Vector3(9.0f, 5.0f, 1.5f), 0.01f, TEST_LOCATION );
518  DALI_TEST_EQUALS( v3, Vector3(8.0f, 6.0f, 5.0f), 0.01f, TEST_LOCATION );
519  DALI_TEST_EQUALS( v4, Vector3(4.9f, 5.1f, 8.0f), 0.01f, TEST_LOCATION );
520  END_TEST;
521 }
522 
524 {
525  TestApplication application;
526  float values[] = {0.0f, 1.0f, 2.0f};
527  Vector3 v0(values);
528 
529  for (int i=0;i<3;++i)
530  {
531  DALI_TEST_EQUALS(v0.AsFloat()[i], values[i], TEST_LOCATION);
532  }
533 
534  END_TEST;
535 }
536 
538 {
539  TestApplication application;
540  float values[] = {0.0f, 1.0f, 2.0f};
541  Vector3 v0(values);
542 
543  const Vector3 v1(values);
544  for (int i=0;i<3;++i)
545  {
546  DALI_TEST_EQUALS(v1.AsFloat()[i], values[i], TEST_LOCATION);
547  }
548  END_TEST;
549 }
550 
552 {
553  TestApplication application;
554  float values[] = {0.0f, 1.0f, 2.0f};
555  const Vector3 v0(values);
556  DALI_TEST_EQUALS(v0.GetVectorXY().x, values[0], TEST_LOCATION);
557  DALI_TEST_EQUALS(v0.GetVectorXY().y, values[1], TEST_LOCATION);
558  END_TEST;
559 }
560 
562 {
563  TestApplication application;
564  float values[] = {0.0f, 1.0f, 2.0f};
565  Vector3 v0(values);
566 
567  DALI_TEST_EQUALS(v0.GetVectorXY().x, values[0], TEST_LOCATION);
568  DALI_TEST_EQUALS(v0.GetVectorXY().y, values[1], TEST_LOCATION);
569  END_TEST;
570 }
571 
573 {
574  TestApplication application;
575  float values[] = {0.0f, 1.0f, 2.0f};
576  const Vector3 v0(values);
577 
578  DALI_TEST_EQUALS(v0.GetVectorYZ().x, values[1], TEST_LOCATION);
579  DALI_TEST_EQUALS(v0.GetVectorYZ().y, values[2], TEST_LOCATION);
580  END_TEST;
581 }
582 
584 {
585  TestApplication application;
586  float values[] = {0.0f, 1.0f, 2.0f};
587  Vector3 v0(values);
588 
589  DALI_TEST_EQUALS(v0.GetVectorYZ().x, values[1], TEST_LOCATION);
590  DALI_TEST_EQUALS(v0.GetVectorYZ().y, values[2], TEST_LOCATION);
591  END_TEST;
592 }
593 
595 {
596  TestApplication application;
597  std::ostringstream oss;
598 
599  Vector3 vector(1, 2, 3);
600 
601  oss << vector;
602 
603  std::string expectedOutput = "[1, 2, 3]";
604 
605  DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
606  END_TEST;
607 }
608 
610 {
611  TestApplication application;
612  Vector3 v0(2.0f, 2.0f, 1.0f);
613  Vector3 v1(1.0f, 1.0f, 2.0f);
614 
615  DALI_TEST_EQUALS(Min(v0, v1), Vector3(1.0f, 1.0f, 1.0f), 0.01f, TEST_LOCATION);
616  END_TEST;
617 }
618 
620 {
621  TestApplication application;
622  Vector3 v0(2.0f, 1.0f, 3.0f);
623  Vector3 v1(1.0f, 2.0f, 3.0f);
624 
625  DALI_TEST_EQUALS(Max(v0, v1), Vector3(2.0f, 2.0f, 3.0f), 0.01f, TEST_LOCATION);
626  END_TEST;
627 }
628 
630 {
631  TestApplication application;
632  Vector3 v0( 2.0f, 1.0f, 0.0f );
633  Vector3 v1( -1.0f, 2.0f, 1.0f );
634 
635  DALI_TEST_EQUALS( Clamp( v0, 0.9f, 1.1f ), Vector3(1.1f, 1.0f, 0.9f), 0.01f, TEST_LOCATION );
636  DALI_TEST_EQUALS( Clamp( v1, 1.0f, 1.0f ), Vector3(1.0f, 1.0f, 1.0f), 0.01f, TEST_LOCATION );
637  END_TEST;
638 }
639 
641 {
642  TestApplication application;
643  Vector3 va = Vector3::ZERO;
644  Vector3 vb = Vector3::ONE;
645  Vector3 vc = Vector3::XAXIS;
646 
647  DALI_TEST_EQUALS(va.x, 0.0f, 0.001f, TEST_LOCATION);
648  DALI_TEST_EQUALS(va.y, 0.0f, 0.001f, TEST_LOCATION);
649  DALI_TEST_EQUALS(va.z, 0.0f, 0.001f, TEST_LOCATION);
650 
651  DALI_TEST_EQUALS(vb.x, 1.0f, 0.001f, TEST_LOCATION);
652  DALI_TEST_EQUALS(vb.y, 1.0f, 0.001f, TEST_LOCATION);
653  DALI_TEST_EQUALS(vb.z, 1.0f, 0.001f, TEST_LOCATION);
654 
655  DALI_TEST_EQUALS(vc.x, 1.0f, 0.001f, TEST_LOCATION);
656  DALI_TEST_EQUALS(vc.y, 0.0f, 0.001f, TEST_LOCATION);
657  DALI_TEST_EQUALS(vc.z, 0.0f, 0.001f, TEST_LOCATION);
658  END_TEST;
659 }
660 
Dali Docs Home
Read more about Dali