2 * Copyright (c) 2015 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 )
67 Vector4& Vector4::operator=(const Vector3& vec3 )
69 // only set x, y and z
77 bool Vector4::operator==(const Vector4& rhs) const
79 if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x) )
83 if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y) )
87 if (fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z) )
91 if (fabsf(w - rhs.w) > GetRangedEpsilon(w, rhs.w) )
99 float Vector4::Dot(const Vector3 &other) const
101 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
103 return x * other.x + y * other.y + z * other.z;
106 float Vector4::Dot(const Vector4 &other) const
108 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
110 return x * other.x + y * other.y + z * other.z;
113 float Vector4::Dot4(const Vector4 &other) const
115 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,4);
117 return x * other.x + y * other.y + z * other.z + w * other.w;
120 Vector4 Vector4::Cross(const Vector4 &other) const
122 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,6);
124 return Vector4( (y * other.z) - (z * other.y),
125 (z * other.x) - (x * other.z),
126 (x * other.y) - (y * other.x),
130 // Will always return positive square root
131 float Vector4::Length() const
133 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
135 return sqrtf(x*x + y*y + z*z);
138 float Vector4::LengthSquared() const
140 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,3);
142 return x*x + y*y + z*z;
145 void Vector4::Normalize()
147 const float length = Length();
149 // In the case where the length is exactly zero, the vector cannot be normalized.
150 if ( ! EqualsZero( length ) )
152 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
154 const float inverseLength = 1.0f / length;
161 void Vector4::Clamp( const Vector4& min, const Vector4& max )
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 );
169 std::ostream& operator<< (std::ostream& o, const Vector4& vector)
171 return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << ", " << vector.w << "]";
174 Vector4 Clamp( const Vector4& v, const float& min, const float& max )
177 result.Clamp( Vector4( min, min, min, min ), Vector4( max, max, max, max) );