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