2 * Copyright (c) 2016 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>
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>
35 using Internal::PerformanceMonitor;
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);
43 Vector4::Vector4( const Vector2& vec2 )
51 Vector4::Vector4( const Vector3& vec3 )
59 Vector4& Vector4::operator=(const Vector2& vec2 )
68 Vector4& Vector4::operator=(const Vector3& vec3 )
78 bool Vector4::operator==(const Vector4& rhs) const
80 if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
84 if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
88 if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
92 if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
100 float Vector4::Dot(const Vector3 &other) const
102 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
104 return x * other.x + y * other.y + z * other.z;
107 float Vector4::Dot(const Vector4 &other) const
109 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
111 return x * other.x + y * other.y + z * other.z;
114 float Vector4::Dot4(const Vector4 &other) const
116 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
118 return x * other.x + y * other.y + z * other.z + w * other.w;
121 Vector4 Vector4::Cross(const Vector4 &other) const
123 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
125 return Vector4( (y * other.z) - (z * other.y),
126 (z * other.x) - (x * other.z),
127 (x * other.y) - (y * other.x),
131 // Will always return positive square root
132 float Vector4::Length() const
134 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
136 return sqrtf(x*x + y*y + z*z);
139 float Vector4::LengthSquared() const
141 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
143 return x*x + y*y + z*z;
146 void Vector4::Normalize()
148 const float length = Length();
150 // In the case where the length is exactly zero, the vector cannot be normalized.
151 if ( ! EqualsZero( length ) )
153 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
155 const float inverseLength = 1.0f / length;
162 void Vector4::Clamp( const Vector4& min, const Vector4& max )
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 );
170 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
172 return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
175 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
178 result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );