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