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