License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector4.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/vector4.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/public-api/math/vector2.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/public-api/math/math-utils.h>
29 #include <dali/internal/render/common/performance-monitor.h>
30
31 namespace Dali
32 {
33
34 using Internal::PerformanceMonitor;
35
36 const Vector4 Vector4::ONE(1.0f, 1.0f, 1.0f, 1.0f);
37 const Vector4 Vector4::XAXIS(1.0f, 0.0f, 0.0f, 0.0f);
38 const Vector4 Vector4::YAXIS(0.0f, 1.0f, 0.0f, 0.0f);
39 const Vector4 Vector4::ZAXIS(0.0f, 0.0f, 1.0f, 0.0f);
40 const Vector4 Vector4::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
41
42 Vector4::Vector4( const Vector2& vec2 )
43 : x(vec2.x),
44   y(vec2.y),
45   z(0.0f),
46   w(0.0f)
47 {
48 }
49
50 Vector4::Vector4( const Vector3& vec3 )
51 : x(vec3.x),
52   y(vec3.y),
53   z(vec3.z),
54   w(0.0f)
55 {
56 }
57
58 Vector4& Vector4::operator=(const Vector2& vec2 )
59 {
60   // only set x and y
61   x = vec2.x;
62   y = vec2.y;
63   return *this;
64 }
65
66 Vector4& Vector4::operator=(const Vector3& vec3 )
67 {
68   // only set x, y and z
69   x = vec3.x;
70   y = vec3.y;
71   z = vec3.z;
72   return *this;
73 }
74
75
76 bool Vector4::operator==(const Vector4& rhs) const
77 {
78   if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
79   {
80     return false;
81   }
82   if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
83   {
84     return false;
85   }
86   if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
87   {
88     return false;
89   }
90   if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
91   {
92     return false;
93   }
94
95   return true;
96 }
97
98 float Vector4::Dot(const Vector3 &other) const
99 {
100   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
101
102   return x * other.x + y * other.y + z * other.z;
103 }
104
105 float Vector4::Dot(const Vector4 &other) const
106 {
107   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
108
109   return x * other.x + y * other.y + z * other.z;
110 }
111
112 float Vector4::Dot4(const Vector4 &other) const
113 {
114   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
115
116   return x * other.x + y * other.y + z * other.z + w * other.w;
117 }
118
119 Vector4 Vector4::Cross(const Vector4 &other) const
120 {
121   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
122
123   return Vector4( (y * other.z) - (z * other.y),
124                   (z * other.x) - (x * other.z),
125                   (x * other.y) - (y * other.x),
126                   0.0f);
127 }
128
129 // Will always return positive square root
130 float Vector4::Length() const
131 {
132   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
133
134   return sqrtf(x*x + y*y + z*z);
135 }
136
137 float Vector4::LengthSquared() const
138 {
139   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
140
141   return x*x + y*y + z*z;
142 }
143
144 void Vector4::Normalize()
145 {
146   const float length = Length();
147
148   // In the case where the length is exactly zero, the vector cannot be normalized.
149   if ( ! EqualsZero( length ) )
150   {
151     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
152
153     const float inverseLength = 1.0f / length;
154     x *= inverseLength;
155     y *= inverseLength;
156     z *= inverseLength;
157   }
158 }
159
160 void Vector4::Clamp( const Vector4& min, const Vector4& max )
161 {
162   Dali::ClampInPlace<float>( x, min.x, max.x );
163   Dali::ClampInPlace<float>( y, min.y, max.y );
164   Dali::ClampInPlace<float>( z, min.z, max.z );
165   Dali::ClampInPlace<float>( w, min.w, max.w );
166 }
167
168 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
169 {
170   return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
171 }
172
173 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
174 {
175   Vector4 result( v );
176   result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );
177
178   return result;
179 }
180
181 } // namespace Dali