Get world scale more faster
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector2.cpp
1 /*
2  * Copyright (c) 2020 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/vector2.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <ostream>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/render/common/performance-monitor.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/math-utils.h>
29 #include <dali/public-api/math/uint-16-pair.h>
30 #include <dali/public-api/math/vector3.h>
31
32 namespace Dali
33 {
34 const Vector2 Vector2::ONE(1.0f, 1.0f);
35 const Vector2 Vector2::XAXIS(1.0f, 0.0f);
36 const Vector2 Vector2::YAXIS(0.0f, 1.0f);
37 const Vector2 Vector2::NEGATIVE_XAXIS(-1.0f, 0.0f);
38 const Vector2 Vector2::NEGATIVE_YAXIS(0.0f, -1.0f);
39 const Vector2 Vector2::ZERO(0.0f, 0.0f);
40
41 Vector2::Vector2(const Vector3& vec3)
42 : x(vec3.x),
43   y(vec3.y)
44 {
45 }
46
47 Vector2::Vector2(const Vector4& vec4)
48 : x(vec4.x),
49   y(vec4.y)
50 {
51 }
52
53 Vector2::Vector2(const Uint16Pair& pair)
54 : width(pair.GetWidth()),
55   height(pair.GetHeight())
56 {
57 }
58
59 Vector2& Vector2::operator=(const Vector3& rhs)
60 {
61   x = rhs.x;
62   y = rhs.y;
63
64   return *this;
65 }
66
67 Vector2& Vector2::operator=(const Vector4& rhs)
68 {
69   x = rhs.x;
70   y = rhs.y;
71
72   return *this;
73 }
74
75 bool Vector2::operator==(const Vector2& rhs) const
76 {
77   if(fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
78   {
79     return false;
80   }
81   if(fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
82   {
83     return false;
84   }
85
86   return true;
87 }
88
89 float Vector2::Length() const
90 {
91   return sqrtf(LengthSquared());
92 }
93
94 float Vector2::LengthSquared() const
95 {
96   return (x * x) + (y * y);
97 }
98
99 void Vector2::Normalize()
100 {
101   float length = Length();
102   if(!EqualsZero(length))
103   {
104     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 2);
105
106     const float inverseLength = 1.0f / length;
107     x *= inverseLength;
108     y *= inverseLength;
109   }
110 }
111
112 void Vector2::Clamp(const Vector2& min, const Vector2& max)
113 {
114   Dali::ClampInPlace<float>(x, min.x, max.x);
115   Dali::ClampInPlace<float>(y, min.y, max.y);
116 }
117
118 std::ostream& operator<<(std::ostream& o, const Vector2& vector)
119 {
120   return o << "[" << vector.x << ", " << vector.y << "]";
121 }
122
123 Vector2 Clamp(const Vector2& v, const float& min, const float& max)
124 {
125   Vector2 result(v);
126   result.Clamp(Vector2(min, min), Vector2(max, max));
127
128   return result;
129 }
130
131 } // namespace Dali