Dali 3D User Interface Engine
utc-Dali-Vector4.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 
21 #include <stdlib.h>
23 #include <dali-test-suite-utils.h>
24 
25 using namespace Dali;
26 
28 {
30 }
31 
33 {
35 }
36 
38 {
39  Vector4 v;
44  END_TEST;
45 }
46 
48 {
49  Vector4 v(1.0f, 2.0f, 3.0f, 4.f);
54  END_TEST;
55 }
56 
58 {
59  float f [] = {1.0f, 2.0f, 3.0f, 4.f};
60  Vector4 v(f);
65  END_TEST;
66 }
67 
69 {
70  Vector2 vec2(1.f, 2.f);
71  Vector4 v(vec2);
76  END_TEST;
77 }
78 
80 {
81  Vector3 vec3(1.f, 2.f, 3.f);
82  Vector4 v(vec3);
87  END_TEST;
88 }
89 
91 {
92  Vector4 v0;
93  const float array[] = { 1.0f, 2.0f, 3.0f, 4.f };
94  v0 = (const float*)array;
95 
96  DALI_TEST_EQUALS(v0.x, 1.0f, 0.001f, TEST_LOCATION);
97  DALI_TEST_EQUALS(v0.y, 2.0f, 0.001f, TEST_LOCATION);
98  DALI_TEST_EQUALS(v0.z, 3.0f, 0.001f, TEST_LOCATION);
99  DALI_TEST_EQUALS(v0.w, 4.0f, 0.001f, TEST_LOCATION);
100  END_TEST;
101 }
102 
104 {
105  Vector2 vec2(1.f, 2.f);
106  Vector4 v0;
107  v0 = vec2;
108  DALI_TEST_EQUALS(v0.x, 1.0f, 0.001f, TEST_LOCATION);
109  DALI_TEST_EQUALS(v0.y, 2.0f, 0.001f, TEST_LOCATION);
110  DALI_TEST_EQUALS(v0.z, 0.0f, 0.001f, TEST_LOCATION);
111  DALI_TEST_EQUALS(v0.z, 0.0f, 0.001f, TEST_LOCATION);
112  END_TEST;
113 }
114 
116 {
117  Vector3 vec3(1.f, 2.f, 3.f);
118  Vector4 v0;
119  v0 = vec3;
120  DALI_TEST_EQUALS(v0.x, 1.0f, 0.001f, TEST_LOCATION);
121  DALI_TEST_EQUALS(v0.y, 2.0f, 0.001f, TEST_LOCATION);
122  DALI_TEST_EQUALS(v0.z, 3.0f, 0.001f, TEST_LOCATION);
123  DALI_TEST_EQUALS(v0.w, 0.0f, 0.001f, TEST_LOCATION);
124  END_TEST;
125 }
126 
128 {
129  Vector4 v0(1.0f, 2.0f, 3.0f, 4.0f);
130  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
131  Vector4 r0(11.0f, 22.0f, 33.0f, 44.0f);
132 
133  Vector4 v2 = v0+v1;
135 
136  END_TEST;
137 }
138 
140 {
141  Vector4 v0(1.0f, 2.0f, 3.0f, 4.0f);
142  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
143  Vector4 r0(11.0f, 22.0f, 33.0f, 44.0f);
144 
145  v0 += v1;
147  END_TEST;
148 }
149 
151 {
152  Vector4 v0(11.0f, 22.0f, 33.0f, 44.0f);
153  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
154  Vector4 r0(1.0f, 2.0f, 3.0f, 4.0f);
155 
156  Vector4 v2 = v0-v1;
158  END_TEST;
159 }
160 
162 {
163  Vector4 v0(11.0f, 22.0f, 33.0f, 44.0f);
164  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
165  Vector4 r0(1.0f, 2.0f, 3.0f, 4.0f);
166 
167  v0 -= v1;
169  END_TEST;
170 }
171 
173 {
174  Vector4 v0(2.0f, 3.0f, 4.0f, 5.0f);
175  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
176  Vector4 r0(20.0f, 60.0f, 120.0f, 200.0f);
177 
178  Vector4 v2 = v0 * v1;
180 
181  END_TEST;
182 }
183 
185 {
186  Vector4 v0(2.0f, 4.0f, 8.0f, 16.0f);
187  const Vector4 r0(20.0f, 40.0f, 80.0f, 160.0f);
188  Vector4 v2 = v0 * 10.0f;
190  END_TEST;
191 }
192 
194 {
195  Vector4 v0(2.0f, 3.0f, 4.0f, 5.0f);
196  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
197  Vector4 r0(20.0f, 60.0f, 120.0f, 200.0f);
198 
199  v0 *= v1;
201  END_TEST;
202 }
203 
205 {
206  Vector4 v0(2.0f, 4.0f, 8.0f, 16.0f);
207  const Vector4 r0(20.0f, 40.0f, 80.0f, 160.0f);
208  Vector4 v2(r0);
209  v0 *= 10.0f;
211  END_TEST;
212 }
213 
215 {
216  Vector4 v0(1.0f, 1.0f, 1.0f, 1.0f);
217  Vector4 v1(2.0f, 3.0f, 5.0f, 7.0f);
218  Vector4 v2(4.0f, 9.0f, 25.0f, 49.0f);
219 
220  DALI_TEST_EQUALS( v0/v0, v0, TEST_LOCATION);
221  DALI_TEST_EQUALS( v1/v0, v1, TEST_LOCATION);
222  DALI_TEST_EQUALS( v1/v1, v0, TEST_LOCATION);
223  DALI_TEST_EQUALS( v2/v1, v1, TEST_LOCATION);
224 
225 
226  END_TEST;
227 }
228 
230 {
231  Vector4 v0(2.0f, 4.0f, 8.0f, 16.0f);
232  const Vector4 r0(20.0f, 40.0f, 80.0f, 160.0f);
233  const Vector4 r1(10.0f, 20.0f, 40.0f, 80.0f);
234  const Vector4 r2( 1.0f, 2.0f, 4.0f, 8.0f);
235  const Vector4 r3(2.0f, 4.0f, 8.0f, 16.0f);
236 
237  Vector4 v2 = r0 / 10.0f;
239 
240  END_TEST;
241 }
242 
244 {
245  Vector4 v0(1.0f, 1.0f, 1.0f, 1.0f);
246  Vector4 v1(2.0f, 3.0f, 5.0f, 7.0f);
247  Vector4 v2(4.0f, 9.0f, 25.0f, 49.0f);
248 
249  Vector4 v4(v0);
250  v4 /= v0;
252 
253  Vector4 v5(v1);
254  v5 /= v0;
256 
257  Vector4 v6(v1);
258  v6 /= v6;
260 
261  v2 /= v1;
263 
264  END_TEST;
265 }
266 
268 {
269  Vector4 v0(2.0f, 4.0f, 8.0f, 16.0f);
270  const Vector4 r0(20.0f, 40.0f, 80.0f, 160.0f);
271  const Vector4 r1(10.0f, 20.0f, 40.0f, 80.0f);
272  const Vector4 r2( 1.0f, 2.0f, 4.0f, 8.0f);
273  const Vector4 r3(2.0f, 4.0f, 8.0f, 16.0f);
274 
275  Vector4 v2(r0);
276  v2 /= 10.0f;
278 
279  END_TEST;
280 }
281 
283 {
284  TestApplication application;
285  Vector4 v1(10.0f, 20.0f, 30.f, 40.f);
286  Vector4 r0(-10.0f, -20.0f, -30.f, -40.f);
287 
288  Vector4 v2 = -v1;
290  END_TEST;
291 }
292 
294 {
295  Vector4 v0(1.0f, 2.0f, 3.0f, 4.0f);
296  Vector4 v1(1.0f, 2.0f, 3.0f, 4.0f);
297 
298  DALI_TEST_CHECK(v0 == v1);
299  END_TEST;
300 }
301 
303 {
304  Vector4 v0(1.0f, 2.0f, 3.0f, 4.0f);
305  Vector4 v1(1.0f, 2.0f, 3.0f, 4.0f);
306 
307  Vector4 v2 = Vector4(0.0f, 2.0f, 3.0f, 4.0f);
308  DALI_TEST_CHECK(v0 != v2);
309 
310  v2 = Vector4(1.0f, 0.0f, 3.0f, 4.0f);
311  DALI_TEST_CHECK(v0 != v2);
312 
313  v2 = Vector4(1.0f, 2.0f, 0.0f, 4.0f);
314  DALI_TEST_CHECK(v0 != v2);
315 
316  v2 = Vector4(1.0f, 2.0f, 3.0f, 0.0f);
317  DALI_TEST_CHECK(v0 != v2);
318  END_TEST;
319 }
320 
322 {
323  Vector4 testVector(1.0f, 2.0f, 3.0f, 4.0f);
324 
325  // read array subscripts
326  DALI_TEST_EQUALS( testVector[0], 1.0f, TEST_LOCATION );
327  DALI_TEST_EQUALS( testVector[1], 2.0f, TEST_LOCATION );
328  DALI_TEST_EQUALS( testVector[2], 3.0f, TEST_LOCATION );
329  DALI_TEST_EQUALS( testVector[3], 4.0f, TEST_LOCATION );
330 
331  // write array subscripts/read struct memebers
332  testVector[0] = 5.0f;
333  testVector[1] = 6.0f;
334  testVector[2] = 7.0f;
335  testVector[3] = 8.0f;
336 
337  DALI_TEST_EQUALS( testVector.x, 5.0f, TEST_LOCATION );
338  DALI_TEST_EQUALS( testVector.y, 6.0f, TEST_LOCATION );
339  DALI_TEST_EQUALS( testVector.z, 7.0f, TEST_LOCATION );
340  DALI_TEST_EQUALS( testVector.w, 8.0f, TEST_LOCATION );
341 
342  // write struct members/read array subscripts
343  testVector.x = 9.0f;
344  testVector.y = 10.0f;
345  testVector.z = 11.0f;
346  testVector.w = 12.0f;
347 
348  DALI_TEST_EQUALS( testVector[0], 9.0f, TEST_LOCATION );
349  DALI_TEST_EQUALS( testVector[1], 10.0f, TEST_LOCATION );
350  DALI_TEST_EQUALS( testVector[2], 11.0f, TEST_LOCATION );
351  DALI_TEST_EQUALS( testVector[3], 12.0f, TEST_LOCATION );
352 
353  END_TEST;
354 }
355 
356 
358 {
359  Vector4 testVector(1.0f, 2.0f, 3.0f, 4.0f);
360 
361  // write struct members/read array subscripts
362  const Vector4 testVector2(1.0f, 2.0f, 3.0f, 4.0f);
363  const float& x = testVector2[0];
364  const float& y = testVector2[1];
365  const float& z ( testVector2[2] );
366  const float& w ( testVector2[3] );
367 
368  DALI_TEST_EQUALS( x, 1.0f, TEST_LOCATION );
369  DALI_TEST_EQUALS( y, 2.0f, TEST_LOCATION );
370  DALI_TEST_EQUALS( z, 3.0f, TEST_LOCATION );
371  DALI_TEST_EQUALS( w, 4.0f, TEST_LOCATION );
372 
373  try
374  {
375  float& w = testVector[4];
376  (void)w; // Suppress unused variable warning
378  }
379  catch (Dali::DaliException& e)
380  {
382  DALI_TEST_ASSERT( e, "index < 4", TEST_LOCATION );
383  }
384 
385  try
386  {
387  const float& w = testVector2[4];
388  (void)w; // Suppress unused variable warning
390  }
391  catch (Dali::DaliException& e)
392  {
394  DALI_TEST_ASSERT( e, "index < 4", TEST_LOCATION );
395  }
396  END_TEST;
397 }
398 
400 {
406 
407  DALI_TEST_EQUALS(Vector4(1.0f, 0.0f, 0.0f, 1.0f).Dot(Vector4(1.0f, 0.0f, 0.0f, 1.0f)), 1.0f, TEST_LOCATION);
408 
409  // Test v0 . v0 and v0 . v1 (v1 is always 90 degrees out of phase with v0)
410  for (float x = 0; x<6.0f; x+=1.0f)
411  {
412  // vectors rotating in the XY plane.
413  Vector4 v0(cosf(x), sinf(x), 0.0f, 1.0f);
414  Vector4 v1(sinf(x), -cosf(x), 0.0f, 1.0f);
415  DALI_TEST_EQUALS(v0.Dot(v1), 0.0f, 0.0001f, TEST_LOCATION);
416  DALI_TEST_EQUALS(v0.Dot(v0), 1.0f, 0.0001f, TEST_LOCATION);
417 
418  // vectors rotating in the XZ plane.
419  v0 = Vector4(cosf(x), 0.0f, sinf(x), 0.0f);
420  v1 = Vector4(sinf(x), 0.0f, -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 
425  Vector4 v0 = Vector4(12.0f, 7.0f, 9.0f, 14.0f);
426  v0.Normalize();
427 
428  Vector4 v1 = v0 * 2.0f;
429  DALI_TEST_EQUALS(v0.Dot(v1), 2.0f, 0.001f, TEST_LOCATION);
430  END_TEST;
431 }
432 
434 {
440 
441  DALI_TEST_EQUALS(Vector4(1.0f, 0.0f, 0.0f, 1.0f).Dot(Vector3(1.0f, 0.0f, 0.0f)), 1.0f, TEST_LOCATION);
442 
443  // Test v0 . v0b and v0 . v1 (v1 is always 90 degrees out of phase with v0)
444  for (float x = 0; x<6.0f; x+=1.0f)
445  {
446  // vectors rotating in the XY plane.
447  Vector4 v0(cosf(x), sinf(x), 0.0f, 1.0f);
448  Vector3 v0b(cosf(x), sinf(x), 0.0f);
449  Vector3 v1(sinf(x), -cosf(x), 0.0f);
450  DALI_TEST_EQUALS(v0.Dot(v1), 0.0f, 0.0001f, TEST_LOCATION);
451  DALI_TEST_EQUALS(v0.Dot(v0b), 1.0f, 0.0001f, TEST_LOCATION);
452 
453  // vectors rotating in the XZ plane.
454  v0 = Vector4(cosf(x), 0.0f, sinf(x), 0.0f);
455  v0b = Vector3(cosf(x), 0.0f, sinf(x));
456  v1 = Vector3(sinf(x), 0.0f, -cosf(x));
457  DALI_TEST_EQUALS(v0.Dot(v1), 0.0f, 0.0001f, TEST_LOCATION);
458  DALI_TEST_EQUALS(v0.Dot(v0b), 1.0f, 0.0001f, TEST_LOCATION);
459  }
460 
461  Vector4 v0 = Vector4(12.0f, 7.0f, 9.0f, 14.0f);
462  v0.Normalize();
463 
464  Vector3 v1(v0 * 2.0f);
465  DALI_TEST_EQUALS(v0.Dot(v1), 2.0f, 0.001f, TEST_LOCATION);
466  END_TEST;
467 }
468 
470 {
474 
478 
479  DALI_TEST_EQUALS(Vector4(1.0f, 0.0f, 0.0f, 1.0f).Dot4(Vector4(1.0f, 0.0f, 0.0f, 1.0f)), 2.0f, TEST_LOCATION);
480 
481  for (float x = 0; x<6.0f; x+=1.0f)
482  {
483  Vector4 v0(cosf(x), sinf(x), 0.0f, 1.0f);
484  Vector4 v1(sinf(x), -cosf(x), 0.0f, 1.0f);
485  DALI_TEST_EQUALS(v0.Dot4(v1), 1.0f, 0.0001f, TEST_LOCATION);
486  DALI_TEST_EQUALS(v0.Dot4(v0), 2.0f, 0.0001f, TEST_LOCATION);
487 
488  v0 = Vector4(cosf(x), 0.0f, sinf(x), 0.0f);
489  v1 = Vector4(sinf(x), 0.0f, -cosf(x), 0.0f);
490  DALI_TEST_EQUALS(v0.Dot4(v1), 0.0f, 0.0001f, TEST_LOCATION);
491  DALI_TEST_EQUALS(v0.Dot4(v0), 1.0f, 0.0001f, TEST_LOCATION);
492  }
493 
494  Vector4 v0(12.0f, 7.0f, 9.0f, 3.0f);
495  v0.Normalize();
496 
497  Vector4 v1 = v0 * 2.0f;
498  DALI_TEST_EQUALS(v0.Dot4(v1), 2.0f + 3.0f*6.0f, 0.001f, TEST_LOCATION);
499  END_TEST;
500 }
501 
503 {
507 
511 
512  Vector4 v0(2.0f, 3.0f, 4.0f, 5.0f);
513  Vector4 v1(10.0f, 20.0f, 30.0f, 40.0f);
514  Vector4 result( (v0.y * v1.z) - (v0.z * v1.y),
515  (v0.z * v1.x) - (v0.x * v1.z),
516  (v0.x * v1.y) - (v0.y * v1.x),
517  0.0f);
518 
519  DALI_TEST_EQUALS(v0.Cross(v1), result, 0.001f, TEST_LOCATION);
520  END_TEST;
521 }
522 
524 {
525  Vector4 v(1.0f, 2.0f, 3.0f, 4.0f);
526  DALI_TEST_EQUALS(v.Length(), sqrtf(v.x*v.x + v.y*v.y + v.z*v.z), 0.001f, TEST_LOCATION);
527 
528  Vector4 v1(0.0f, 0.0f, 0.0f, 0.0f);
529  DALI_TEST_EQUALS(v1.Length(), 0.0f, TEST_LOCATION);
530  END_TEST;
531 }
532 
534 {
535  Vector4 v(1.0f, 2.0f, 3.0f, 4.0f);
536  DALI_TEST_EQUALS(v.LengthSquared(), v.x*v.x + v.y*v.y + v.z*v.z, 0.001f, TEST_LOCATION);
537 
538  Vector4 v1(0.0f, 0.0f, 0.0f, 0.0f);
539  DALI_TEST_EQUALS(v1.LengthSquared(), 0.0f, TEST_LOCATION);
540  END_TEST;
541 }
542 
544 {
545  for (float f=0.0f; f<6.0f; f+=1.0f)
546  {
547  Vector4 v(cosf(f)*10.0f, cosf(f+1.0f)*10.0f, cosf(f+2.0f)*10.0f, 1.0f);
548  v.Normalize();
549  DALI_TEST_EQUALS(v.LengthSquared(), 1.0f, 0.001f, TEST_LOCATION);
550  }
551 
552  Vector4 v(0.0f, 0.0f, 0.0f, 1.0f);
553  v.Normalize();
554  DALI_TEST_EQUALS(v.LengthSquared(), 0.0f, 0.00001f, TEST_LOCATION);
555  END_TEST;
556 }
557 
559 {
560  tet_infoline("Testing Dali::Vector4::Clamp( const Vector4& v, const Vector4& min, const Vector4& max )");
561 
562  Vector4 v0(2.0f, 0.8f, 0.0f, 5.0f);
563  Vector4 v1(-1.0f, 2.0f, 10.0f, -10.0f);
564  Vector4 v2(10.0f, 5.0f, 0.0f, 10.0f);
565  Vector4 v3(8.0f, 10.0f, 5.0f, -20.0f);
566  Vector4 v4(4.9f, 5.1f, 10.0f, 0.0f);
567 
568  Vector4 min(1.0f, -2.0f, -8.0f, -16.0f);
569  Vector4 max(2.0f, 4.0f, 4.0f, -8.0f);
570 
571  v0.Clamp( min, max );
572  v1.Clamp( min, max );
573  v2.Clamp( min, max );
574  v3.Clamp( min, max );
575  v4.Clamp( min, max );
576 
577  DALI_TEST_EQUALS( v0, Vector4( 2.0f, 0.8f, 0.0f, -8.0f), 0.01f, TEST_LOCATION );
578  DALI_TEST_EQUALS( v1, Vector4( 1.0f, 2.0f, 4.0f, -10.0f), 0.01f, TEST_LOCATION );
579  DALI_TEST_EQUALS( v2, Vector4( 2.0f, 4.0f, 0.0f, -8.0f), 0.01f, TEST_LOCATION );
580  DALI_TEST_EQUALS( v3, Vector4( 2.0f, 4.0f, 4.0f, -16.0f), 0.01f, TEST_LOCATION );
581  DALI_TEST_EQUALS( v4, Vector4( 2.0f, 4.0f, 4.0f, -8.0f), 0.01f, TEST_LOCATION );
582  END_TEST;
583 }
584 
586 {
587  float values[] = {0.0f, 1.0f, 2.0f, 3.0f};
588  Vector4 v0(values);
589 
590  for (int i=0;i<4;++i)
591  {
592  DALI_TEST_EQUALS(v0.AsFloat()[i], values[i], TEST_LOCATION);
593  }
594 
595  END_TEST;
596 }
597 
599 {
600  float values[] = {0.0f, 1.0f, 2.0f, 3.0f};
601  Vector4 v0(values);
602 
603  const Vector4 v1(values);
604  for (int i=0;i<4;++i)
605  {
606  DALI_TEST_EQUALS(v1.AsFloat()[i], values[i], TEST_LOCATION);
607  }
608  END_TEST;
609 }
610 
612 {
613  std::ostringstream oss;
614 
615  Vector4 vector(1.0f, 2.0f, 3.0f, 4.0f);
616 
617  oss << vector;
618 
619  std::string expectedOutput = "[1, 2, 3, 4]";
620 
621  DALI_TEST_EQUALS( oss.str(), expectedOutput, TEST_LOCATION);
622  END_TEST;
623 }
624 
626 {
627  Vector4 v0(2.0f, 2.0f, 1.0f, 1.0f);
628  Vector4 v1(1.0f, 1.0f, 2.0f, 2.0f);
629 
630  DALI_TEST_EQUALS(Max(v0, v1), Vector4(2.0f, 2.0f, 2.0f, 2.0f), 0.01f, TEST_LOCATION);
631  END_TEST;
632 }
633 
635 {
636  Vector4 v0(2.0f, 2.0f, 1.0f, 1.0f);
637  Vector4 v1(1.0f, 1.0f, 2.0f, 2.0f);
638 
639  DALI_TEST_EQUALS(Min(v0, v1), Vector4(1.0f, 1.0f, 1.0f, 1.0f), 0.01f, TEST_LOCATION);
640  END_TEST;
641 }
642 
644 {
645  tet_infoline("Testing Dali::Vector4::Clamp()");
646 
647  Vector4 v0(2.0f, 2.0f, -2.0f, -2.0f);
648  DALI_TEST_EQUALS(Clamp(v0, -1.0f, 1.0f), Vector4(1.0f, 1.0f, -1.0f, -1.0f), 0.01f, TEST_LOCATION);
649 
650  Vector4 v1(1.0f, 0.0f, 0.0f, -1.0f);
651  DALI_TEST_EQUALS(Clamp(v1, -1.0f, 1.0f), v1, 0.01f, TEST_LOCATION);
652 
653  END_TEST;
654 }
655 
657 {
658  float f[] = {2.0f, 3.0f, 4.0f, 5.0f};
659  Vector4 v0(f);
660  Vector4 v1(f[0], f[1], f[2], f[3]);
661  Vector4 v2(v0);
662 
665  DALI_TEST_CHECK(v0 == v1);
666  END_TEST;
667 }
Dali Docs Home
Read more about Dali