| 1 | /**************************************************************************/ |
| 2 | /* vector4.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 "vector4.h" |
| 32 | |
| 33 | #include "core/math/math_funcs.h" |
| 34 | #include "core/math/vector4i.h" |
| 35 | #include "core/string/ustring.h" |
| 36 | |
| 37 | Vector4::Axis Vector4::min_axis_index() const { |
| 38 | uint32_t min_index = 0; |
| 39 | real_t min_value = x; |
| 40 | for (uint32_t i = 1; i < 4; i++) { |
| 41 | if (operator[](i) <= min_value) { |
| 42 | min_index = i; |
| 43 | min_value = operator[](i); |
| 44 | } |
| 45 | } |
| 46 | return Vector4::Axis(min_index); |
| 47 | } |
| 48 | |
| 49 | Vector4::Axis Vector4::max_axis_index() const { |
| 50 | uint32_t max_index = 0; |
| 51 | real_t max_value = x; |
| 52 | for (uint32_t i = 1; i < 4; i++) { |
| 53 | if (operator[](i) > max_value) { |
| 54 | max_index = i; |
| 55 | max_value = operator[](i); |
| 56 | } |
| 57 | } |
| 58 | return Vector4::Axis(max_index); |
| 59 | } |
| 60 | |
| 61 | bool Vector4::is_equal_approx(const Vector4 &p_vec4) const { |
| 62 | return Math::is_equal_approx(x, p_vec4.x) && Math::is_equal_approx(y, p_vec4.y) && Math::is_equal_approx(z, p_vec4.z) && Math::is_equal_approx(w, p_vec4.w); |
| 63 | } |
| 64 | |
| 65 | bool Vector4::is_same(const Vector4 &p_vec4) const { |
| 66 | return Math::is_same(x, p_vec4.x) && Math::is_same(y, p_vec4.y) && Math::is_same(z, p_vec4.z) && Math::is_same(w, p_vec4.w); |
| 67 | } |
| 68 | |
| 69 | bool Vector4::is_zero_approx() const { |
| 70 | return Math::is_zero_approx(x) && Math::is_zero_approx(y) && Math::is_zero_approx(z) && Math::is_zero_approx(w); |
| 71 | } |
| 72 | |
| 73 | bool Vector4::is_finite() const { |
| 74 | return Math::is_finite(x) && Math::is_finite(y) && Math::is_finite(z) && Math::is_finite(w); |
| 75 | } |
| 76 | |
| 77 | real_t Vector4::length() const { |
| 78 | return Math::sqrt(length_squared()); |
| 79 | } |
| 80 | |
| 81 | void Vector4::normalize() { |
| 82 | real_t lengthsq = length_squared(); |
| 83 | if (lengthsq == 0) { |
| 84 | x = y = z = w = 0; |
| 85 | } else { |
| 86 | real_t length = Math::sqrt(lengthsq); |
| 87 | x /= length; |
| 88 | y /= length; |
| 89 | z /= length; |
| 90 | w /= length; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | Vector4 Vector4::normalized() const { |
| 95 | Vector4 v = *this; |
| 96 | v.normalize(); |
| 97 | return v; |
| 98 | } |
| 99 | |
| 100 | bool Vector4::is_normalized() const { |
| 101 | return Math::is_equal_approx(length_squared(), (real_t)1, (real_t)UNIT_EPSILON); |
| 102 | } |
| 103 | |
| 104 | real_t Vector4::distance_to(const Vector4 &p_to) const { |
| 105 | return (p_to - *this).length(); |
| 106 | } |
| 107 | |
| 108 | real_t Vector4::distance_squared_to(const Vector4 &p_to) const { |
| 109 | return (p_to - *this).length_squared(); |
| 110 | } |
| 111 | |
| 112 | Vector4 Vector4::direction_to(const Vector4 &p_to) const { |
| 113 | Vector4 ret(p_to.x - x, p_to.y - y, p_to.z - z, p_to.w - w); |
| 114 | ret.normalize(); |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | Vector4 Vector4::abs() const { |
| 119 | return Vector4(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w)); |
| 120 | } |
| 121 | |
| 122 | Vector4 Vector4::sign() const { |
| 123 | return Vector4(SIGN(x), SIGN(y), SIGN(z), SIGN(w)); |
| 124 | } |
| 125 | |
| 126 | Vector4 Vector4::floor() const { |
| 127 | return Vector4(Math::floor(x), Math::floor(y), Math::floor(z), Math::floor(w)); |
| 128 | } |
| 129 | |
| 130 | Vector4 Vector4::ceil() const { |
| 131 | return Vector4(Math::ceil(x), Math::ceil(y), Math::ceil(z), Math::ceil(w)); |
| 132 | } |
| 133 | |
| 134 | Vector4 Vector4::round() const { |
| 135 | return Vector4(Math::round(x), Math::round(y), Math::round(z), Math::round(w)); |
| 136 | } |
| 137 | |
| 138 | Vector4 Vector4::lerp(const Vector4 &p_to, real_t p_weight) const { |
| 139 | Vector4 res = *this; |
| 140 | res.x = Math::lerp(res.x, p_to.x, p_weight); |
| 141 | res.y = Math::lerp(res.y, p_to.y, p_weight); |
| 142 | res.z = Math::lerp(res.z, p_to.z, p_weight); |
| 143 | res.w = Math::lerp(res.w, p_to.w, p_weight); |
| 144 | return res; |
| 145 | } |
| 146 | |
| 147 | Vector4 Vector4::cubic_interpolate(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight) const { |
| 148 | Vector4 res = *this; |
| 149 | res.x = Math::cubic_interpolate(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight); |
| 150 | res.y = Math::cubic_interpolate(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight); |
| 151 | res.z = Math::cubic_interpolate(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight); |
| 152 | res.w = Math::cubic_interpolate(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight); |
| 153 | return res; |
| 154 | } |
| 155 | |
| 156 | Vector4 Vector4::cubic_interpolate_in_time(const Vector4 &p_b, const Vector4 &p_pre_a, const Vector4 &p_post_b, real_t p_weight, real_t p_b_t, real_t p_pre_a_t, real_t p_post_b_t) const { |
| 157 | Vector4 res = *this; |
| 158 | res.x = Math::cubic_interpolate_in_time(res.x, p_b.x, p_pre_a.x, p_post_b.x, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
| 159 | res.y = Math::cubic_interpolate_in_time(res.y, p_b.y, p_pre_a.y, p_post_b.y, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
| 160 | res.z = Math::cubic_interpolate_in_time(res.z, p_b.z, p_pre_a.z, p_post_b.z, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
| 161 | res.w = Math::cubic_interpolate_in_time(res.w, p_b.w, p_pre_a.w, p_post_b.w, p_weight, p_b_t, p_pre_a_t, p_post_b_t); |
| 162 | return res; |
| 163 | } |
| 164 | |
| 165 | Vector4 Vector4::posmod(real_t p_mod) const { |
| 166 | return Vector4(Math::fposmod(x, p_mod), Math::fposmod(y, p_mod), Math::fposmod(z, p_mod), Math::fposmod(w, p_mod)); |
| 167 | } |
| 168 | |
| 169 | Vector4 Vector4::posmodv(const Vector4 &p_modv) const { |
| 170 | return Vector4(Math::fposmod(x, p_modv.x), Math::fposmod(y, p_modv.y), Math::fposmod(z, p_modv.z), Math::fposmod(w, p_modv.w)); |
| 171 | } |
| 172 | |
| 173 | void Vector4::snap(const Vector4 &p_step) { |
| 174 | x = Math::snapped(x, p_step.x); |
| 175 | y = Math::snapped(y, p_step.y); |
| 176 | z = Math::snapped(z, p_step.z); |
| 177 | w = Math::snapped(w, p_step.w); |
| 178 | } |
| 179 | |
| 180 | void Vector4::snapf(real_t p_step) { |
| 181 | x = Math::snapped(x, p_step); |
| 182 | y = Math::snapped(y, p_step); |
| 183 | z = Math::snapped(z, p_step); |
| 184 | w = Math::snapped(w, p_step); |
| 185 | } |
| 186 | |
| 187 | Vector4 Vector4::snapped(const Vector4 &p_step) const { |
| 188 | Vector4 v = *this; |
| 189 | v.snap(p_step); |
| 190 | return v; |
| 191 | } |
| 192 | |
| 193 | Vector4 Vector4::snappedf(real_t p_step) const { |
| 194 | Vector4 v = *this; |
| 195 | v.snapf(p_step); |
| 196 | return v; |
| 197 | } |
| 198 | |
| 199 | Vector4 Vector4::inverse() const { |
| 200 | return Vector4(1.0f / x, 1.0f / y, 1.0f / z, 1.0f / w); |
| 201 | } |
| 202 | |
| 203 | Vector4 Vector4::clamp(const Vector4 &p_min, const Vector4 &p_max) const { |
| 204 | return Vector4( |
| 205 | CLAMP(x, p_min.x, p_max.x), |
| 206 | CLAMP(y, p_min.y, p_max.y), |
| 207 | CLAMP(z, p_min.z, p_max.z), |
| 208 | CLAMP(w, p_min.w, p_max.w)); |
| 209 | } |
| 210 | |
| 211 | Vector4 Vector4::clampf(real_t p_min, real_t p_max) const { |
| 212 | return Vector4( |
| 213 | CLAMP(x, p_min, p_max), |
| 214 | CLAMP(y, p_min, p_max), |
| 215 | CLAMP(z, p_min, p_max), |
| 216 | CLAMP(w, p_min, p_max)); |
| 217 | } |
| 218 | |
| 219 | Vector4::operator String() const { |
| 220 | return "(" + String::num_real(x, true) + ", " + String::num_real(y, true) + ", " + String::num_real(z, true) + ", " + String::num_real(w, true) + ")"; |
| 221 | } |
| 222 | |
| 223 | static_assert(sizeof(Vector4) == 4 * sizeof(real_t)); |
| 224 | |
| 225 | Vector4::operator Vector4i() const { |
| 226 | return Vector4i(x, y, z, w); |
| 227 | } |