[dali_2.3.24] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector3.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/quaternion.h>
20 #include <dali/public-api/math/vector3.h>
21
22 // EXTERNAL INCLUDES
23 #include <math.h>
24 #include <ostream>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/render/common/performance-monitor.h>
28 #include <dali/public-api/common/dali-common.h>
29 #include <dali/public-api/math/math-utils.h>
30 #include <dali/public-api/math/vector2.h>
31 #include <dali/public-api/math/vector4.h>
32
33 namespace Dali
34 {
35 using Internal::PerformanceMonitor;
36
37 const Vector3 Vector3::ONE(1.0f, 1.0f, 1.0f);
38 const Vector3 Vector3::XAXIS(1.0f, 0.0f, 0.0f);
39 const Vector3 Vector3::YAXIS(0.0f, 1.0f, 0.0f);
40 const Vector3 Vector3::ZAXIS(0.0f, 0.0f, 1.0f);
41 const Vector3 Vector3::NEGATIVE_XAXIS(-1.0f, 0.0f, 0.0f);
42 const Vector3 Vector3::NEGATIVE_YAXIS(0.0f, -1.0f, 0.0f);
43 const Vector3 Vector3::NEGATIVE_ZAXIS(0.0f, 0.0f, -1.0f);
44 const Vector3 Vector3::ZERO(0.0f, 0.0f, 0.0f);
45
46 Vector3::Vector3(const Vector2& vec2)
47 : x(vec2.x),
48   y(vec2.y),
49   z(0.0f)
50 {
51 }
52
53 Vector3::Vector3(const Vector4& vec4)
54 : x(vec4.x),
55   y(vec4.y),
56   z(vec4.z)
57 {
58 }
59
60 Vector3& Vector3::operator=(const Vector2& rhs)
61 {
62   x = rhs.x;
63   y = rhs.y;
64   z = 0.0f;
65
66   return *this;
67 }
68
69 Vector3& Vector3::operator=(const Vector4& rhs)
70 {
71   x = rhs.x;
72   y = rhs.y;
73   z = rhs.z;
74
75   return *this;
76 }
77
78 Vector3& Vector3::operator*=(const Quaternion& rhs)
79 {
80   // nVidia SDK implementation
81   Vector3 qvec(rhs.mVector.x, rhs.mVector.y, rhs.mVector.z);
82
83   Vector3 uv((qvec.y * z) - (qvec.z * y),
84              (qvec.z * x) - (qvec.x * z),
85              (qvec.x * y) - (qvec.y * x));
86
87   Vector3 uuv((qvec.y * uv.z) - (qvec.z * uv.y),
88               (qvec.z * uv.x) - (qvec.x * uv.z),
89               (qvec.x * uv.y) - (qvec.y * uv.x));
90
91   uv *= rhs.mVector.w;
92
93   x += (uv.x + uuv.x) * 2.0f;
94   y += (uv.y + uuv.y) * 2.0f;
95   z += (uv.z + uuv.z) * 2.0f;
96
97   return *this;
98 }
99
100 bool Vector3::operator==(const Vector3& rhs) const
101 {
102   if(fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
103   {
104     return false;
105   }
106   if(fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
107   {
108     return false;
109   }
110   if(fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z))
111   {
112     return false;
113   }
114
115   return true;
116 }
117
118 float Vector3::Dot(const Vector3& other) const
119 {
120   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
121
122   return x * other.x + y * other.y + z * other.z;
123 }
124
125 Vector3 Vector3::Cross(const Vector3& other) const
126 {
127   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 6);
128
129   return Vector3((y * other.z) - (z * other.y),
130                  (z * other.x) - (x * other.z),
131                  (x * other.y) - (y * other.x));
132 }
133
134 float Vector3::Length() const
135 {
136   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
137
138   return sqrtf(x * x + y * y + z * z);
139 }
140
141 float Vector3::LengthSquared() const
142 {
143   MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
144
145   return x * x + y * y + z * z;
146 }
147
148 void Vector3::Normalize()
149 {
150   float length = Length();
151   if(!EqualsZero(length))
152   {
153     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
154
155     const float inverseLength = 1.0f / length;
156     x *= inverseLength;
157     y *= inverseLength;
158     z *= inverseLength;
159   }
160 }
161
162 void Vector3::Clamp(const Vector3& min, const Vector3& max)
163 {
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 }
168
169 std::ostream& operator<<(std::ostream& o, const Vector3& vector)
170 {
171   return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << "]";
172 }
173
174 Vector3 Clamp(const Vector3& v, const float& min, const float& max)
175 {
176   Vector3 result(v);
177   result.Clamp(Vector3(min, min, min), Vector3(max, max, max));
178
179   return result;
180 }
181
182 } // namespace Dali