| 1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
| 2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
| 3 | #include "Math/BsVector3.h" |
| 4 | #include "Math/BsVector4.h" |
| 5 | #include "Math/BsMath.h" |
| 6 | |
| 7 | namespace bs |
| 8 | { |
| 9 | Vector3::Vector3(const Vector4& vec) |
| 10 | :x(vec.x), y(vec.y), z(vec.z) |
| 11 | { |
| 12 | |
| 13 | } |
| 14 | |
| 15 | Radian Vector3::angleBetween(const Vector3& dest) const |
| 16 | { |
| 17 | float lenProduct = length() * dest.length(); |
| 18 | |
| 19 | // Divide by zero check |
| 20 | if (lenProduct < 1e-6f) |
| 21 | lenProduct = 1e-6f; |
| 22 | |
| 23 | float f = dot(dest) / lenProduct; |
| 24 | |
| 25 | f = Math::clamp(f, -1.0f, 1.0f); |
| 26 | return Math::acos(f); |
| 27 | } |
| 28 | |
| 29 | Vector3 Vector3::normalize(const Vector3& val) |
| 30 | { |
| 31 | float len = Math::sqrt(val.x * val.x + val.y * val.y + val.z * val.z); |
| 32 | |
| 33 | // Will also work for zero-sized vectors, but will change nothing |
| 34 | if (len > 1e-08) |
| 35 | { |
| 36 | float invLen = 1.0f / len; |
| 37 | |
| 38 | Vector3 normalizedVec; |
| 39 | normalizedVec.x = val.x * invLen; |
| 40 | normalizedVec.y = val.y * invLen; |
| 41 | normalizedVec.z = val.z * invLen; |
| 42 | |
| 43 | return normalizedVec; |
| 44 | } |
| 45 | else |
| 46 | return val; |
| 47 | } |
| 48 | |
| 49 | bool Vector3::isNaN() const |
| 50 | { |
| 51 | return Math::isNaN(x) || Math::isNaN(y) || Math::isNaN(z); |
| 52 | } |
| 53 | |
| 54 | const Vector3 Vector3::ZERO{BS_ZERO()}; |
| 55 | const Vector3 Vector3::ONE(1, 1, 1); |
| 56 | const Vector3 Vector3::INF = |
| 57 | Vector3(std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity(), std::numeric_limits<float>::infinity()); |
| 58 | |
| 59 | const Vector3 Vector3::UNIT_X(1, 0, 0); |
| 60 | const Vector3 Vector3::UNIT_Y(0, 1, 0); |
| 61 | const Vector3 Vector3::UNIT_Z(0, 0, 1); |
| 62 | } |
| 63 | |