2 * Copyright (c) 2020 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/quaternion.h>
20 #include <dali/public-api/math/vector3.h>
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>
35 using Internal::PerformanceMonitor;
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);
46 Vector3::Vector3(const Vector2& vec2)
53 Vector3::Vector3(const Vector4& vec4)
60 Vector3& Vector3::operator=(const Vector2& rhs)
69 Vector3& Vector3::operator=(const Vector4& rhs)
78 Vector3& Vector3::operator*=(const Quaternion& rhs)
80 // nVidia SDK implementation
81 Vector3 qvec(rhs.mVector.x, rhs.mVector.y, rhs.mVector.z);
83 Vector3 uv((qvec.y * z) - (qvec.z * y),
84 (qvec.z * x) - (qvec.x * z),
85 (qvec.x * y) - (qvec.y * x));
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));
93 x += (uv.x + uuv.x) * 2.0f;
94 y += (uv.y + uuv.y) * 2.0f;
95 z += (uv.z + uuv.z) * 2.0f;
100 bool Vector3::operator==(const Vector3& rhs) const
102 if(fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
106 if(fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
110 if(fabsf(z - rhs.z) > GetRangedEpsilon(z, rhs.z))
118 float Vector3::Dot(const Vector3& other) const
120 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
122 return x * other.x + y * other.y + z * other.z;
125 Vector3 Vector3::Cross(const Vector3& other) const
127 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 6);
129 return Vector3((y * other.z) - (z * other.y),
130 (z * other.x) - (x * other.z),
131 (x * other.y) - (y * other.x));
134 float Vector3::Length() const
136 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
138 return sqrtf(x * x + y * y + z * z);
141 float Vector3::LengthSquared() const
143 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
145 return x * x + y * y + z * z;
148 void Vector3::Normalize()
150 float length = Length();
151 if(!EqualsZero(length))
153 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 3);
155 const float inverseLength = 1.0f / length;
162 void Vector3::Clamp(const Vector3& min, const Vector3& 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);
169 std::ostream& operator<<(std::ostream& o, const Vector3& vector)
171 return o << "[" << vector.x << ", " << vector.y << ", " << vector.z << "]";
174 Vector3 Clamp(const Vector3& v, const float& min, const float& max)
177 result.Clamp(Vector3(min, min, min), Vector3(max, max, max));