Dali 3D User Interface Engine
utc-Dali-Constraints.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 using namespace Dali;
25 
28 {
30 }
31 
33 {
35 }
37 
39 namespace
40 {
41 
43 {
44 public:
45 
46  // Constants
47  static const bool BOOLEAN_VALUE;
48  static const float FLOAT_VALUE;
49  static const int INTEGER_VALUE;
50  static const Vector2 VECTOR2_VALUE;
51  static const Vector3 VECTOR3_VALUE;
52  static const Vector4 VECTOR4_VALUE;
53  static const Matrix3 MATRIX3_VALUE;
54  static const Matrix MATRIX_VALUE;
56 
57  // Construction & Destruction
58  PropertyInputImpl( Property::Type type ) : mType( type ) { }
59  virtual ~PropertyInputImpl() { }
60 
61  // Methods
62  Property::Type GetType() const { return mType; }
63 
64  // Virtual Methods
65  virtual const bool& GetBoolean() const { return BOOLEAN_VALUE; }
66  virtual const float& GetFloat() const { return FLOAT_VALUE; }
67  virtual const int& GetInteger() const { return INTEGER_VALUE; }
68  virtual const Vector2& GetVector2() const { return VECTOR2_VALUE; }
69  virtual const Vector3& GetVector3() const { return VECTOR3_VALUE; }
70  virtual const Vector4& GetVector4() const { return VECTOR4_VALUE; }
71  virtual const Matrix3& GetMatrix3() const { return MATRIX3_VALUE; }
72  virtual const Matrix& GetMatrix() const { return MATRIX_VALUE; }
73  virtual const Quaternion& GetQuaternion() const { return QUATERNION_VALUE; }
74 
75  // Data
77 };
78 
79 const bool PropertyInputImpl::BOOLEAN_VALUE = true;
80 const float PropertyInputImpl::FLOAT_VALUE = 123.0f;
81 const int PropertyInputImpl::INTEGER_VALUE = 456;
82 const Vector2 PropertyInputImpl::VECTOR2_VALUE = Vector2( 10.0f, 20.0f );
83 const Vector3 PropertyInputImpl::VECTOR3_VALUE = Vector3( 30.0f, 40.0f, 50.0f );
84 const Vector4 PropertyInputImpl::VECTOR4_VALUE = Vector4( 60.0f, 70.0f, 80.0f, 90.0f );
85 const Matrix3 PropertyInputImpl::MATRIX3_VALUE ( 1.0f, 2.0f, 3.0f,
86  4.0f, 5.0f, 6.0f,
87  7.0f, 8.0f, 9.0f );
88 const Matrix PropertyInputImpl::MATRIX_VALUE = Matrix::IDENTITY;
89 const Quaternion PropertyInputImpl::QUATERNION_VALUE ( 1.0f, 2.0f, 3.0f, 4.0f );
90 
92 {
93 public:
94 
95  // Construction & Destruction
97  : PropertyInputImpl( Property::VECTOR3 ),
98  mValue( value )
99  {
100  }
101 
103  {
104  }
105 
106  const Vector3& GetVector3() const
107  {
108  return mValue;
109  }
110 
111  // Data
113 };
114 
116 {
117 public:
118 
119  // Construction & Destruction
121  : PropertyInputImpl( Property::ROTATION ),
122  mValue( value )
123  {
124  }
125 
127  {
128  }
129 
130  const Quaternion& GetQuaternion() const
131  {
132  return mValue;
133  }
134 
135  // Data
137 };
138 
139 } // unnamed namespace
141 
143 // EqualToConstraint
146 {
147  PropertyInputContainer inputs;
148  PropertyInputImpl input( Property::FLOAT );
149  inputs.PushBack( &input );
150 
151  float value = 0.0f;
152  DALI_TEST_CHECK( value != PropertyInputImpl::FLOAT_VALUE );
153 
154  EqualToConstraint constraint;
155  constraint( value, inputs );
156 
157  DALI_TEST_EQUALS( value, PropertyInputImpl::FLOAT_VALUE, TEST_LOCATION );
158 
159  END_TEST;
160 }
161 
163 {
164  PropertyInputContainer inputs;
165  PropertyInputImpl input( Property::VECTOR2 );
166  inputs.PushBack( &input );
167 
168  Vector2 value;
169  DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR2_VALUE );
170 
171  EqualToConstraint constraint;
172  constraint( value, inputs );
173 
174  DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR2_VALUE, TEST_LOCATION );
175 
176  END_TEST;
177 }
178 
180 {
181  PropertyInputContainer inputs;
182  PropertyInputImpl input( Property::VECTOR3 );
183  inputs.PushBack( &input );
184 
185  Vector3 value;
186  DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR3_VALUE );
187 
188  EqualToConstraint constraint;
189  constraint( value, inputs );
190 
191  DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE, TEST_LOCATION );
192 
193  END_TEST;
194 }
195 
197 {
198  PropertyInputContainer inputs;
199  PropertyInputImpl input( Property::VECTOR4 );
200  inputs.PushBack( &input );
201 
202  Vector4 value;
203  DALI_TEST_CHECK( value != PropertyInputImpl::VECTOR4_VALUE );
204 
205  EqualToConstraint constraint;
206  constraint( value, inputs );
207 
208  DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR4_VALUE, TEST_LOCATION );
209 
210  END_TEST;
211 }
212 
214 {
215  PropertyInputContainer inputs;
216  PropertyInputImpl input( Property::ROTATION );
217  inputs.PushBack( &input );
218 
219  Quaternion value;
220  DALI_TEST_CHECK( value != PropertyInputImpl::QUATERNION_VALUE );
221 
222  EqualToConstraint constraint;
223  constraint( value, inputs );
224 
225  DALI_TEST_EQUALS( value, PropertyInputImpl::QUATERNION_VALUE, TEST_LOCATION );
226 
227  END_TEST;
228 }
229 
231 {
232  PropertyInputContainer inputs;
233  PropertyInputImpl input( Property::MATRIX3 );
234  inputs.PushBack( &input );
235 
236  Matrix3 value;
237  DALI_TEST_CHECK( value != PropertyInputImpl::MATRIX3_VALUE );
238 
239  EqualToConstraint constraint;
240  constraint( value, inputs );
241 
242  DALI_TEST_EQUALS( value, PropertyInputImpl::MATRIX3_VALUE, 0.1f, TEST_LOCATION);
243 
244  END_TEST;
245 }
246 
248 {
249  PropertyInputContainer inputs;
250  PropertyInputImpl input( Property::MATRIX );
251  inputs.PushBack( &input );
252 
253  Matrix value;
254  DALI_TEST_CHECK( value != PropertyInputImpl::MATRIX_VALUE );
255 
256  EqualToConstraint constraint;
257  constraint( value, inputs );
258 
259  DALI_TEST_EQUALS( value, PropertyInputImpl::MATRIX_VALUE, TEST_LOCATION );
260 
261  END_TEST;
262 }
264 
266 // RelativeToConstraint
269 {
270  PropertyInputContainer inputs;
271  PropertyInputImpl input( Property::VECTOR3 );
272  inputs.PushBack( &input );
273 
274  Vector3 value;
276 
277  const float scale( 4.0f );
278  RelativeToConstraint constraint( scale );
279  constraint( value, inputs );
280 
281  DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE * scale, TEST_LOCATION );
282 
283  END_TEST;
284 }
285 
287 {
288  PropertyInputContainer inputs;
289  PropertyInputImpl input( Property::VECTOR3 );
290  inputs.PushBack( &input );
291 
292  Vector3 value;
294 
295  const Vector3 scale( 4.0f, 5.0f, 6.0f );
296  RelativeToConstraint constraint( scale );
297  constraint( value, inputs );
298 
299  DALI_TEST_EQUALS( value, PropertyInputImpl::VECTOR3_VALUE * scale, TEST_LOCATION );
300 
301  END_TEST;
302 }
304 
306 // RelativeToConstraintFloat
309 {
310  PropertyInputContainer inputs;
311  PropertyInputImpl input( Property::VECTOR3 );
312  inputs.PushBack( &input );
313 
314  const float scale( 4.0f );
315 
316  float value = 0.0f;
317  DALI_TEST_CHECK( value != PropertyInputImpl::FLOAT_VALUE * scale );
318 
319  RelativeToConstraintFloat constraint( scale );
320  constraint( value, inputs );
321 
322  DALI_TEST_EQUALS( value, PropertyInputImpl::FLOAT_VALUE * scale, TEST_LOCATION );
323 
324  END_TEST;
325 }
327 
329 // LookAt
332 {
333  TestApplication application;
334 
335  Actor actor = Actor::New();
337 
338  Vector3 targetPosition;
339  Vector3 cameraPosition;
340  Quaternion targetOrientation;
341 
342  Vector3PropertyInput targetPositionProperty( targetPosition );
343  Vector3PropertyInput cameraPositionProperty( cameraPosition );
344  QuaternionPropertyInput targetOrientationProperty( targetOrientation );
345 
346  PropertyInputContainer inputs;
347  inputs.PushBack( &targetPositionProperty );
348  inputs.PushBack( &cameraPositionProperty );
349  inputs.PushBack( &targetOrientationProperty );
350 
351  Quaternion current;
352 
353  // 180 degrees round y
354  targetPosition = Vector3::ZERO;
355  cameraPosition = Vector3( 0.0f, 0.0f, 1.0f );
356  targetOrientation = Quaternion::IDENTITY;
357  Quaternion lookAtOrientation( Quaternion( Radian( Math::PI ), Vector3::YAXIS ) );
358  LookAt( current, inputs );
359  DALI_TEST_EQUALS( current, lookAtOrientation, TEST_LOCATION );
360 
361  // 180 degrees round y * -45 degrees round x
362  targetPosition = Vector3::ZERO;
363  cameraPosition = Vector3( 0.0f, -1.0f, 1.0f );
364  targetOrientation = Quaternion::IDENTITY;
365  lookAtOrientation = Quaternion( Radian( Math::PI ), Vector3::YAXIS ) * Quaternion( Radian( Math::PI * 0.25f ), -Vector3::XAXIS );
366  LookAt( current, inputs );
367  DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
368 
369  // 180 degrees round y * -45 degrees round x at different points
370  targetPosition = Vector3( 0.0f, 1.0f, -1.0f );
371  cameraPosition = Vector3::ZERO;
372  targetOrientation = Quaternion::IDENTITY;
373  lookAtOrientation = Quaternion( Radian( Math::PI ), Vector3::YAXIS ) * Quaternion( Radian( Math::PI * 0.25f ), -Vector3::XAXIS );
374  LookAt( current, inputs );
375  DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
376 
377  // 225 degrees round y
378  targetPosition = Vector3( -1.0f, 0.0f, 0.0f );
379  cameraPosition = Vector3( 0.0f, 0.0f, 1.0f );
380  targetOrientation = Quaternion::IDENTITY;
381  lookAtOrientation = Quaternion( Radian( Math::PI * 1.25), Vector3::YAXIS );
382  LookAt( current, inputs );
383  DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
384 
385  // 180 degrees round y * -45 degrees round x, Up Vector: 180 degrees
386  targetPosition = Vector3::ZERO;
387  cameraPosition = Vector3( 0.0f, -1.0f, 1.0f );
388  targetOrientation = Quaternion( Radian( Math::PI ), Vector3::ZAXIS );
390  LookAt( current, inputs );
391  DALI_TEST_EQUALS( current, lookAtOrientation, Math::MACHINE_EPSILON_10, TEST_LOCATION );
392 
393  END_TEST;
394 }
396 
398 {
399  PropertyInputImpl input( Property::BOOLEAN );
400  DALI_TEST_CHECK( input.GetExtension() == NULL );
401  END_TEST;
402 }
Dali Docs Home
Read more about Dali