| 1 | /**************************************************************************/ |
| 2 | /* vector3.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "vector3.h" |
| 32 | |
| 33 | #include "core/math/basis.h" |
| 34 | #include "core/math/vector2.h" |
| 35 | #include "core/math/vector3i.h" |
| 36 | #include "core/string/ustring.h" |
| 37 | |
| 38 | void Vector3::rotate(const Vector3 &p_axis, real_t p_angle) { |
| 39 | *this = Basis(p_axis, p_angle).xform(*this); |
| 40 | } |
| 41 | |
| 42 | Vector3 Vector3::rotated(const Vector3 &p_axis, real_t p_angle) const { |
| 43 | Vector3 r = *this; |
| 44 | r.rotate(p_axis, p_angle); |
| 45 | return r; |
| 46 | } |
| 47 | |
| 48 | void Vector3::snap(const Vector3 &p_step) { |
| 49 | x = Math::snapped(x, p_step.x); |
| 50 | y = Math::snapped(y, p_step.y); |
| 51 | z = Math::snapped(z, p_step.z); |
| 52 | } |
| 53 | |
| 54 | Vector3 Vector3::snapped(const Vector3 &p_step) const { |
| 55 | Vector3 v = *this; |
| 56 | v.snap(p_step); |
| 57 | return v; |
| 58 | } |
| 59 | |
| 60 | void Vector3::snapf(real_t p_step) { |
| 61 | x = Math::snapped(x, p_step); |
| 62 | y = Math::snapped(y, p_step); |
| 63 | z = Math::snapped(z, p_step); |
| 64 | } |
| 65 | |
| 66 | Vector3 Vector3::snappedf(real_t p_step) const { |
| 67 | Vector3 v = *this; |
| 68 | v.snapf(p_step); |
| 69 | return v; |
| 70 | } |
| 71 | |
| 72 | Vector3 Vector3::limit_length(real_t p_len) const { |
| 73 | const real_t l = length(); |
| 74 | Vector3 v = *this; |
| 75 | if (l > 0 && p_len < l) { |
| 76 | v /= l; |
| 77 | v *= p_len; |
| 78 | } |
| 79 | |
| 80 | return v; |
| 81 | } |
| 82 | |
| 83 | Vector3 Vector3::move_toward(const Vector3 &p_to, real_t p_delta) const { |
| 84 | Vector3 v = *this; |
| 85 | Vector3 vd = p_to - v; |
| 86 | real_t len = vd.length(); |
| 87 | return len <= p_delta || len < (real_t)CMP_EPSILON ? p_to : v + vd / len * p_delta; |
| 88 | } |
| 89 | |
| 90 | Vector2 Vector3::octahedron_encode() const { |
| 91 | Vector3 n = *this; |
| 92 | n /= Math::abs(n.x) + Math::abs(n.y) + Math::abs(n.z); |
| 93 | Vector2 o; |
| 94 | if (n.z >= 0.0f) { |
| 95 | o.x = n.x; |
| 96 | o.y = n.y; |
| 97 | } else { |
| 98 | o.x = (1.0f - Math::abs(n.y)) * (n.x >= 0.0f ? 1.0f : -1.0f); |
| 99 | o.y = (1.0f - Math::abs(n.x)) * (n.y >= 0.0f ? 1.0f : -1.0f); |
| 100 | } |
| 101 | o.x = o.x * 0.5f + 0.5f; |
| 102 | o.y = o.y * 0.5f + 0.5f; |
| 103 | return o; |
| 104 | } |
| 105 | |
| 106 | Vector3 Vector3::octahedron_decode(const Vector2 &p_oct) { |
| 107 | Vector2 f(p_oct.x * 2.0f - 1.0f, p_oct.y * 2.0f - 1.0f); |
| 108 | Vector3 n(f.x, f.y, 1.0f - Math::abs(f.x) - Math::abs(f.y)); |
| 109 | const real_t t = CLAMP(-n.z, 0.0f, 1.0f); |
| 110 | n.x += n.x >= 0 ? -t : t; |
| 111 | n.y += n.y >= 0 ? -t : t; |
| 112 | return n.normalized(); |
| 113 | } |
| 114 | |
| 115 | Vector2 Vector3::octahedron_tangent_encode(float p_sign) const { |
| 116 | const real_t bias = 1.0f / (real_t)32767.0f; |
| 117 | Vector2 res = octahedron_encode(); |
| 118 | res.y = MAX(res.y, bias); |
| 119 | res.y = res.y * 0.5f + 0.5f; |
| 120 | res.y = p_sign >= 0.0f ? res.y : 1 - res.y; |
| 121 | return res; |
| 122 | } |
| 123 | |
| 124 | Vector3 Vector3::octahedron_tangent_decode(const Vector2 &p_oct, float *r_sign) { |
| 125 | Vector2 oct_compressed = p_oct; |
| 126 | oct_compressed.y = oct_compressed.y * 2 - 1; |
| 127 | *r_sign = oct_compressed.y >= 0.0f ? 1.0f : -1.0f; |
| 128 | oct_compressed.y = Math::abs(oct_compressed.y); |
| 129 | Vector3 res = Vector3::octahedron_decode(oct_compressed); |
| 130 | return res; |
| 131 | } |
| 132 | |
| 133 | Basis Vector3::outer(const Vector3 &p_with) const { |
| 134 | Basis basis; |
| 135 | basis.rows[0] = Vector3(x * p_with.x, x * p_with.y, x * p_with.z); |
| 136 | basis.rows[1] = Vector3(y * p_with.x, y * p_with.y, y * p_with.z); |
| 137 | basis.rows[2] = Vector3(z * p_with.x, z * p_with.y, z * p_with.z); |
| 138 | return basis; |
| 139 | } |
| 140 | |
| 141 | bool Vector3::is_equal_approx(const Vector3 &p_v) const { |
| 142 | return Math::is_equal_approx(x, p_v.x) && Math::is_equal_approx(y, p_v.y) && Math::is_equal_approx(z, p_v.z); |
| 143 | } |
| 144 | |
| 145 | bool Vector3::is_same(const Vector3 &p_v) const { |
| 146 | return Math::is_same(x, p_v.x) && Math::is_same(y, p_v.y) && Math::is_same(z, p_v.z); |
| 147 | } |
| 148 | |
| 149 | bool Vector3::is_zero_approx() const { |
| 150 | return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z); |
| 151 | } |
| 152 | |
| 153 | bool Vector3::is_finite() const { |
| 154 | return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z); |
| 155 | } |
| 156 | |
| 157 | Vector3::operator String() const { |
| 158 | return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ")"; |
| 159 | } |
| 160 | |
| 161 | Vector3::operator Vector3i() const { |
| 162 | return Vector3i(x, y, z); |
| 163 | } |