Merge "Addition of build flag "-Wl,-Bsymbolic-functions" for launching performance...
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector3.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 // CLASS HEADER
19 #include <dali/public-api/math/vector3.h>
20 #include <dali/public-api/math/quaternion.h>
21
22 // EXTERNAL INCLUDES
23 #include <math.h>
24 #include <iostream>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/math/vector4.h>
30 #include <dali/public-api/math/math-utils.h>
31 #include <dali/internal/render/common/performance-monitor.h>
32
33 namespace Dali
34 {
35 using Internal::PerformanceMonitor;
36
37 const Vector3 Vector3::ONE(1.0f, 1.0f, 1.0f);
38 const Vector3 Vector3::XAXIS(1.0f, 0.0f, 0.0f);
39 const Vector3 Vector3::YAXIS(0.0f, 1.0f, 0.0f);
40 const Vector3 Vector3::ZAXIS(0.0f, 0.0f, 1.0f);
41 const Vector3 Vector3::NEGATIVE_XAXIS(-1.0f, 0.0f, 0.0f);
42 const Vector3 Vector3::NEGATIVE_YAXIS(0.0f, -1.0f, 0.0f);
43 const Vector3 Vector3::NEGATIVE_ZAXIS(0.0f, 0.0f, -1.0f);
44 const Vector3 Vector3::ZERO(0.0f, 0.0f, 0.0f);
45
46 Vector3::Vector3( const Vector2& vec2 )
47 : x(vec2.x),
48   y(vec2.y),
49   z(0.0f)
50 {
51 }
52
53 Vector3::Vector3( const Vector4& vec4 )
54 : x(vec4.x),
55   y(vec4.y),
56   z(vec4.z)
57 {
58 }
59
60 Vector3& Vector3::operator=(const Vector2& rhs)
61 {
62   x = rhs.x;
63   y = rhs.y;
64   z = 0.0f;
65
66   return *this;
67 }
68
69 Vector3& Vector3::operator=(const Vector4& rhs)
70 {
71   x = rhs.x;
72   y = rhs.y;
73   z = rhs.z;
74
75   return *this;
76 }
77
78 Vector3& Vector3::operator*=(const Quaternion& rhs)
79 {
80   // nVidia SDK implementation
81   Vector3 qvec(rhs.mVector.x, rhs.mVector.y, rhs.mVector.z);
82
83   Vector3 uv( (qvec.y * z) - (qvec.z * y),
84               (qvec.z * x) - (qvec.x * z),
85               (qvec.x * y) - (qvec.y * x) );
86
87   Vector3 uuv( (qvec.y * uv.z) - (qvec.z * uv.y),
88                (qvec.z * uv.x) - (qvec.x * uv.z),
89                (qvec.x * uv.y) - (qvec.y * uv.x) );
90
91   uv *= rhs.mVector.w;
92
93   x += (uv.x + uuv.x) * 2.0f;
94   y += (uv.y + uuv.y) * 2.0f;
95   z += (uv.z + uuv.z) * 2.0f;
96
97   return *this;
98 }
99
100 bool Vector3::operator==(const Vector3& rhs) const
101 {
102   if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
103   {
104     return false;
105   }
106   if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
107   {
108     return false;
109   }
110   if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
111   {
112     return false;
113   }
114
115   return true;
116 }
117
118 float Vector3::Dot(const Vector3 &other) const
119 {
120   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
121
122   return x * other.x + y * other.y + z * other.z;
123 }
124
125 Vector3 Vector3::Cross(const Vector3 &other) const
126 {
127   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
128
129   return Vector3( (y * other.z) - (z * other.y),
130                   (z * other.x) - (x * other.z),
131                   (x * other.y) - (y * other.x));
132 }
133
134 float Vector3::Length() const
135 {
136   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
137
138   return sqrtf(x*x + y*y + z*z);
139 }
140
141 float Vector3::LengthSquared() const
142 {
143   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
144
145   return x*x + y*y + z*z;
146 }
147
148 void Vector3::Normalize()
149 {
150   float length = Length();
151   if( ! EqualsZero(length) )
152   {
153     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
154
155     const float inverseLength = 1.0f / length;
156     x *= inverseLength;
157     y *= inverseLength;
158     z *= inverseLength;
159   }
160 }
161
162 void Vector3::Clamp( const Vector3& min, const Vector3& max )
163 {
164   Dali::ClampInPlace<float>( x, min.x, max.x );
165   Dali::ClampInPlace<float>( y, min.y, max.y );
166   Dali::ClampInPlace<float>( z, min.z, max.z );
167 }
168
169 std::ostream& operator<< (std::ostream& o, const Vector3& vector)
170 {
171   return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << "]";
172 }
173
174 Vector3 Clamp( const Vector3& v, const float& min, const float& max )
175 {
176   Vector3 result( v );
177   result.Clamp( Vector3( min, min, min ) , Vector3( max, max, max ) );
178
179   return result;
180 }
181
182 Vector3 FitKeepAspectRatio( const Vector3& target, const Vector3& source )
183 {
184   float scale = 0.0f;
185
186   if ( fabsf(source.x) > 0.0f )
187   {
188     scale = target.x / source.x;
189   }
190
191   if ( fabsf(source.y) > 0.0f )
192   {
193     if ( scale > Math::MACHINE_EPSILON_1 )
194     {
195       scale =  std::min( scale, target.y / source.y );
196     }
197     else
198     {
199       scale = target.y / source.y;
200     }
201   }
202
203   if ( fabsf(source.z) > 0.0f )
204   {
205     if ( scale > Math::MACHINE_EPSILON_1 )
206     {
207       scale =  std::min( scale, target.z / source.z );
208     }
209     else
210     {
211       scale = target.z / source.z;
212     }
213   }
214
215   if ( scale < Math::MACHINE_EPSILON_1 )
216   {
217     scale = 1.0f;
218   }
219
220   return Vector3( scale, scale, scale );
221 }
222
223 Vector3 FillKeepAspectRatio( const Vector3& target, const Vector3& source )
224 {
225   float scale = 0.0f;
226
227   if ( fabsf(source.x) > 0.0f )
228   {
229     scale = target.x / source.x;
230   }
231
232   if ( fabsf(source.y) > 0.0f )
233   {
234     if ( scale > Math::MACHINE_EPSILON_1 )
235     {
236       scale =  std::max( scale, target.y / source.y );
237     }
238     else
239     {
240       scale = target.y / source.y;
241     }
242   }
243
244   if ( fabsf(source.z) > 0.0f )
245   {
246     if ( scale > Math::MACHINE_EPSILON_1 )
247     {
248       scale =  std::max( scale, target.z / source.z );
249     }
250     else
251     {
252       scale = target.z / source.z;
253     }
254   }
255
256   if ( scale < Math::MACHINE_EPSILON_1 )
257   {
258     scale = 1.0f;
259   }
260
261   return Vector3( scale, scale, scale );
262 }
263
264 Vector3 FillXYKeepAspectRatio( const Vector3& target, const Vector3& source )
265 {
266   float scale = 0.0f;
267
268   if ( fabsf(source.x) > 0.0f )
269   {
270     scale = target.x / source.x;
271   }
272
273   if ( fabsf(source.y) > 0.0f )
274   {
275     if ( scale > Math::MACHINE_EPSILON_1 )
276     {
277       scale =  std::max( scale, target.y / source.y );
278     }
279     else
280     {
281       scale = target.y / source.y;
282     }
283   }
284
285   if ( scale < Math::MACHINE_EPSILON_1 )
286   {
287     scale = 1.0f;
288   }
289
290   return Vector3( scale, scale, scale );
291 }
292
293 Vector3 ShrinkInsideKeepAspectRatio( const Vector3& target, const Vector3& source )
294 {
295   // calculate source size vs target size to see if we need to shrink
296   float widthScale = 1.0f;
297   if( target.width < source.width )
298   {
299     // not enough width, width needs to shrink
300     widthScale = target.width / source.width;
301   }
302   float heightScale = 1.0f;
303   if( target.height < source.height )
304   {
305     // not enough height, height needs to shrink
306     heightScale = target.height / source.height;
307   }
308   float depthScale = 1.0f;
309   if( target.depth < source.depth )
310   {
311     // not enough depth, depth needs to shrink
312     depthScale = target.depth / source.depth;
313   }
314   // use smaller of the scales
315   float scale = std::min( std::min( widthScale, heightScale ), depthScale );
316
317   // check if we need to scale
318   if( scale < 1.0f )
319   {
320     // scale natural size to fit inside
321     return Vector3( scale, scale, scale );
322   }
323   // there is enough space so use source size
324   return Vector3::ONE;
325 }
326
327 } // namespace Dali