Allow reading Vector2/3/4 values from Property::Value with type Vector2/3/4, matching...
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector4.cpp
1 /*
2  * Copyright (c) 2016 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 <ostream>
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   x = vec2.x;
62   y = vec2.y;
63   z = 0.0f;
64   w = 0.0f;
65   return *this;
66 }
67
68 Vector4& Vector4::operator=(const Vector3& vec3 )
69 {
70   x = vec3.x;
71   y = vec3.y;
72   z = vec3.z;
73   w = 0.0f;
74   return *this;
75 }
76
77
78 bool Vector4::operator==(const Vector4& rhs) const
79 {
80   if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
81   {
82     return false;
83   }
84   if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
85   {
86     return false;
87   }
88   if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
89   {
90     return false;
91   }
92   if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
93   {
94     return false;
95   }
96
97   return true;
98 }
99
100 float Vector4::Dot(const Vector3 &other) const
101 {
102   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
103
104   return x * other.x + y * other.y + z * other.z;
105 }
106
107 float Vector4::Dot(const Vector4 &other) const
108 {
109   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
110
111   return x * other.x + y * other.y + z * other.z;
112 }
113
114 float Vector4::Dot4(const Vector4 &other) const
115 {
116   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
117
118   return x * other.x + y * other.y + z * other.z + w * other.w;
119 }
120
121 Vector4 Vector4::Cross(const Vector4 &other) const
122 {
123   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
124
125   return Vector4( (y * other.z) - (z * other.y),
126                   (z * other.x) - (x * other.z),
127                   (x * other.y) - (y * other.x),
128                   0.0f);
129 }
130
131 // Will always return positive square root
132 float Vector4::Length() const
133 {
134   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
135
136   return sqrtf(x*x + y*y + z*z);
137 }
138
139 float Vector4::LengthSquared() const
140 {
141   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
142
143   return x*x + y*y + z*z;
144 }
145
146 void Vector4::Normalize()
147 {
148   const float length = Length();
149
150   // In the case where the length is exactly zero, the vector cannot be normalized.
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 Vector4::Clamp( const Vector4& min, const Vector4& 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   Dali::ClampInPlace<float>( w, min.w, max.w );
168 }
169
170 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
171 {
172   return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
173 }
174
175 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
176 {
177   Vector4 result( v );
178   result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );
179
180   return result;
181 }
182
183 } // namespace Dali