2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/math/vector4.h>
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>
34 using Internal::PerformanceMonitor;
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);
42 Vector4::Vector4( const Vector2& vec2 )
50 Vector4::Vector4( const Vector3& vec3 )
58 Vector4& Vector4::operator=(const Vector2& vec2 )
66 Vector4& Vector4::operator=(const Vector3& vec3 )
68 // only set x, y and z
76 bool Vector4::operator==(const Vector4& rhs) const
78 if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
82 if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
86 if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
90 if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
98 float Vector4::Dot(const Vector3 &other) const
100 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
102 return x * other.x + y * other.y + z * other.z;
105 float Vector4::Dot(const Vector4 &other) const
107 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
109 return x * other.x + y * other.y + z * other.z;
112 float Vector4::Dot4(const Vector4 &other) const
114 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
116 return x * other.x + y * other.y + z * other.z + w * other.w;
119 Vector4 Vector4::Cross(const Vector4 &other) const
121 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
123 return Vector4( (y * other.z) - (z * other.y),
124 (z * other.x) - (x * other.z),
125 (x * other.y) - (y * other.x),
129 // Will always return positive square root
130 float Vector4::Length() const
132 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
134 return sqrtf(x*x + y*y + z*z);
137 float Vector4::LengthSquared() const
139 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
141 return x*x + y*y + z*z;
144 void Vector4::Normalize()
146 const float length = Length();
148 // In the case where the length is exactly zero, the vector cannot be normalized.
149 if ( ! EqualsZero( length ) )
151 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
153 const float inverseLength = 1.0f / length;
160 void Vector4::Clamp( const Vector4& min, const Vector4& max )
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 );
168 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
170 return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
173 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
176 result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );