2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/public-api/math/vector4.h>
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/public-api/math/vector2.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/public-api/math/math-utils.h>
28 #include <dali/internal/render/common/performance-monitor.h>
33 using Internal::PerformanceMonitor;
35 const Vector4 Vector4::ONE(1.0f, 1.0f, 1.0f, 1.0f);
36 const Vector4 Vector4::XAXIS(1.0f, 0.0f, 0.0f, 0.0f);
37 const Vector4 Vector4::YAXIS(0.0f, 1.0f, 0.0f, 0.0f);
38 const Vector4 Vector4::ZAXIS(0.0f, 0.0f, 1.0f, 0.0f);
39 const Vector4 Vector4::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
41 Vector4::Vector4( const Vector2& vec2 )
49 Vector4::Vector4( const Vector3& vec3 )
57 Vector4& Vector4::operator=(const Vector2& vec2 )
65 Vector4& Vector4::operator=(const Vector3& vec3 )
67 // only set x, y and z
75 bool Vector4::operator==(const Vector4& rhs) const
77 if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
81 if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
85 if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
89 if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
97 float Vector4::Dot(const Vector3 &other) const
99 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
101 return x * other.x + y * other.y + z * other.z;
104 float Vector4::Dot(const Vector4 &other) const
106 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
108 return x * other.x + y * other.y + z * other.z;
111 float Vector4::Dot4(const Vector4 &other) const
113 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
115 return x * other.x + y * other.y + z * other.z + w * other.w;
118 Vector4 Vector4::Cross(const Vector4 &other) const
120 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
122 return Vector4( (y * other.z) - (z * other.y),
123 (z * other.x) - (x * other.z),
124 (x * other.y) - (y * other.x),
128 // Will always return positive square root
129 float Vector4::Length() const
131 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
133 return sqrtf(x*x + y*y + z*z);
136 float Vector4::LengthSquared() const
138 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
140 return x*x + y*y + z*z;
143 void Vector4::Normalize()
145 const float length = Length();
147 // In the case where the length is exactly zero, the vector cannot be normalized.
148 if ( ! EqualsZero( length ) )
150 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
152 const float inverseLength = 1.0f / length;
159 void Vector4::Clamp( const Vector4& min, const Vector4& max )
161 Dali::ClampInPlace<float>( x, min.x, max.x );
162 Dali::ClampInPlace<float>( y, min.y, max.y );
163 Dali::ClampInPlace<float>( z, min.z, max.z );
164 Dali::ClampInPlace<float>( w, min.w, max.w );
167 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
169 return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
172 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
175 result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );