[Tizen] Temporary fix, Will be removed later, Fix Emul crash issue
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector3.cpp
1 /*
2  * Copyright (c) 2015 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 <ostream>
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 // Temporary fix, Will be removed later, Fix Emul crash issue
101 #ifndef _ARCH_ARM_
102 #undef fabsf
103 float fabsf(float value)
104 {
105   if (value < 0) return -value;
106   return value;
107 }
108 #endif
109
110 bool Vector3::operator==(const Vector3& rhs) const
111 {
112   if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
113   {
114     return false;
115   }
116   if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
117   {
118     return false;
119   }
120   if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
121   {
122     return false;
123   }
124
125   return true;
126 }
127
128 float Vector3::Dot(const Vector3 &other) const
129 {
130   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
131
132   return x * other.x + y * other.y + z * other.z;
133 }
134
135 Vector3 Vector3::Cross(const Vector3 &other) const
136 {
137   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
138
139   return Vector3( (y * other.z) - (z * other.y),
140                   (z * other.x) - (x * other.z),
141                   (x * other.y) - (y * other.x));
142 }
143
144 float Vector3::Length() const
145 {
146   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
147
148   return sqrtf(x*x + y*y + z*z);
149 }
150
151 float Vector3::LengthSquared() const
152 {
153   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
154
155   return x*x + y*y + z*z;
156 }
157
158 void Vector3::Normalize()
159 {
160   float length = Length();
161   if( ! EqualsZero(length) )
162   {
163     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
164
165     const float inverseLength = 1.0f / length;
166     x *= inverseLength;
167     y *= inverseLength;
168     z *= inverseLength;
169   }
170 }
171
172 void Vector3::Clamp( const Vector3& min, const Vector3& max )
173 {
174   Dali::ClampInPlace<float>( x, min.x, max.x );
175   Dali::ClampInPlace<float>( y, min.y, max.y );
176   Dali::ClampInPlace<float>( z, min.z, max.z );
177 }
178
179 std::ostream& operator<< (std::ostream& o, const Vector3& vector)
180 {
181   return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << "]";
182 }
183
184 Vector3 Clamp( const Vector3& v, const float& min, const float& max )
185 {
186   Vector3 result( v );
187   result.Clamp( Vector3( min, min, min ) , Vector3( max, max, max ) );
188
189   return result;
190 }
191
192 } // namespace Dali