Debian Codegraph @

core/math/vector4i.h

raw symbols SCIP
1 /**************************************************************************/
2 /* vector4i.h */
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 #pragma once
32
33 #include "core/error/error_macros.h"
34 #include "core/math/math_funcs.h"
35 #include "core/templates/hashfuncs.h"
36
37 class String;
38 struct Vector4;
39
40 struct [[nodiscard]] Vector4i {
41 static constexpr int AXIS_COUNT = 4;
42
43 enum Axis {
44 AXIS_X,
45 AXIS_Y,
46 AXIS_Z,
47 AXIS_W,
48 };
49
50 union {
51 // NOLINTBEGIN(modernize-use-default-member-init)
52 struct {
53 int32_t x;
54 int32_t y;
55 int32_t z;
56 int32_t w;
57 };
58
59 int32_t coord[4] = { 0 };
60 // NOLINTEND(modernize-use-default-member-init)
61 };
62
63 _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
64 DEV_ASSERT((unsigned int)p_axis < 4);
65 return coord[p_axis];
66 }
67
68 _FORCE_INLINE_ int32_t &operator[](int p_axis) {
69 DEV_ASSERT((unsigned int)p_axis < 4);
70 return coord[p_axis];
71 }
72
73 Vector4i::Axis min_axis_index() const;
74 Vector4i::Axis max_axis_index() const;
75
76 Vector4i min(const Vector4i &p_vector4i) const {
77 return Vector4i(MIN(x, p_vector4i.x), MIN(y, p_vector4i.y), MIN(z, p_vector4i.z), MIN(w, p_vector4i.w));
78 }
79
80 Vector4i mini(int32_t p_scalar) const {
81 return Vector4i(MIN(x, p_scalar), MIN(y, p_scalar), MIN(z, p_scalar), MIN(w, p_scalar));
82 }
83
84 Vector4i max(const Vector4i &p_vector4i) const {
85 return Vector4i(MAX(x, p_vector4i.x), MAX(y, p_vector4i.y), MAX(z, p_vector4i.z), MAX(w, p_vector4i.w));
86 }
87
88 Vector4i maxi(int32_t p_scalar) const {
89 return Vector4i(MAX(x, p_scalar), MAX(y, p_scalar), MAX(z, p_scalar), MAX(w, p_scalar));
90 }
91
92 _FORCE_INLINE_ int64_t length_squared() const;
93 _FORCE_INLINE_ double length() const;
94
95 _FORCE_INLINE_ void zero();
96
97 _FORCE_INLINE_ double distance_to(const Vector4i &p_to) const;
98 _FORCE_INLINE_ int64_t distance_squared_to(const Vector4i &p_to) const;
99
100 _FORCE_INLINE_ Vector4i abs() const;
101 _FORCE_INLINE_ Vector4i sign() const;
102 Vector4i clamp(const Vector4i &p_min, const Vector4i &p_max) const;
103 Vector4i clampi(int32_t p_min, int32_t p_max) const;
104 Vector4i snapped(const Vector4i &p_step) const;
105 Vector4i snappedi(int32_t p_step) const;
106
107 /* Operators */
108
109 constexpr Vector4i &operator+=(const Vector4i &p_v);
110 constexpr Vector4i operator+(const Vector4i &p_v) const;
111 constexpr Vector4i &operator-=(const Vector4i &p_v);
112 constexpr Vector4i operator-(const Vector4i &p_v) const;
113 constexpr Vector4i &operator*=(const Vector4i &p_v);
114 constexpr Vector4i operator*(const Vector4i &p_v) const;
115 constexpr Vector4i &operator/=(const Vector4i &p_v);
116 constexpr Vector4i operator/(const Vector4i &p_v) const;
117 constexpr Vector4i &operator%=(const Vector4i &p_v);
118 constexpr Vector4i operator%(const Vector4i &p_v) const;
119
120 constexpr Vector4i &operator*=(int32_t p_scalar);
121 constexpr Vector4i operator*(int32_t p_scalar) const;
122 constexpr Vector4i &operator/=(int32_t p_scalar);
123 constexpr Vector4i operator/(int32_t p_scalar) const;
124 constexpr Vector4i &operator%=(int32_t p_scalar);
125 constexpr Vector4i operator%(int32_t p_scalar) const;
126
127 constexpr Vector4i operator-() const;
128
129 constexpr bool operator==(const Vector4i &p_v) const;
130 constexpr bool operator!=(const Vector4i &p_v) const;
131 constexpr bool operator<(const Vector4i &p_v) const;
132 constexpr bool operator<=(const Vector4i &p_v) const;
133 constexpr bool operator>(const Vector4i &p_v) const;
134 constexpr bool operator>=(const Vector4i &p_v) const;
135
136 explicit operator String() const;
137 operator Vector4() const;
138
139 uint32_t hash() const {
140 uint32_t h = hash_murmur3_one_32(uint32_t(x));
141 h = hash_murmur3_one_32(uint32_t(y), h);
142 h = hash_murmur3_one_32(uint32_t(z), h);
143 h = hash_murmur3_one_32(uint32_t(w), h);
144 return hash_fmix32(h);
145 }
146
147 constexpr Vector4i() :
148 x(0), y(0), z(0), w(0) {}
149 Vector4i(const Vector4 &p_vec4);
150 constexpr Vector4i(int32_t p_x, int32_t p_y, int32_t p_z, int32_t p_w) :
151 x(p_x), y(p_y), z(p_z), w(p_w) {}
152 };
153
154 int64_t Vector4i::length_squared() const {
155 return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z + w * (int64_t)w;
156 }
157
158 double Vector4i::length() const {
159 return Math::sqrt((double)length_squared());
160 }
161
162 double Vector4i::distance_to(const Vector4i &p_to) const {
163 return (p_to - *this).length();
164 }
165
166 int64_t Vector4i::distance_squared_to(const Vector4i &p_to) const {
167 return (p_to - *this).length_squared();
168 }
169
170 Vector4i Vector4i::abs() const {
171 return Vector4i(Math::abs(x), Math::abs(y), Math::abs(z), Math::abs(w));
172 }
173
174 Vector4i Vector4i::sign() const {
175 return Vector4i(SIGN(x), SIGN(y), SIGN(z), SIGN(w));
176 }
177
178 /* Operators */
179
180 constexpr Vector4i &Vector4i::operator+=(const Vector4i &p_v) {
181 x += p_v.x;
182 y += p_v.y;
183 z += p_v.z;
184 w += p_v.w;
185 return *this;
186 }
187
188 constexpr Vector4i Vector4i::operator+(const Vector4i &p_v) const {
189 return Vector4i(x + p_v.x, y + p_v.y, z + p_v.z, w + p_v.w);
190 }
191
192 constexpr Vector4i &Vector4i::operator-=(const Vector4i &p_v) {
193 x -= p_v.x;
194 y -= p_v.y;
195 z -= p_v.z;
196 w -= p_v.w;
197 return *this;
198 }
199
200 constexpr Vector4i Vector4i::operator-(const Vector4i &p_v) const {
201 return Vector4i(x - p_v.x, y - p_v.y, z - p_v.z, w - p_v.w);
202 }
203
204 constexpr Vector4i &Vector4i::operator*=(const Vector4i &p_v) {
205 x *= p_v.x;
206 y *= p_v.y;
207 z *= p_v.z;
208 w *= p_v.w;
209 return *this;
210 }
211
212 constexpr Vector4i Vector4i::operator*(const Vector4i &p_v) const {
213 return Vector4i(x * p_v.x, y * p_v.y, z * p_v.z, w * p_v.w);
214 }
215
216 constexpr Vector4i &Vector4i::operator/=(const Vector4i &p_v) {
217 x /= p_v.x;
218 y /= p_v.y;
219 z /= p_v.z;
220 w /= p_v.w;
221 return *this;
222 }
223
224 constexpr Vector4i Vector4i::operator/(const Vector4i &p_v) const {
225 return Vector4i(x / p_v.x, y / p_v.y, z / p_v.z, w / p_v.w);
226 }
227
228 constexpr Vector4i &Vector4i::operator%=(const Vector4i &p_v) {
229 x %= p_v.x;
230 y %= p_v.y;
231 z %= p_v.z;
232 w %= p_v.w;
233 return *this;
234 }
235
236 constexpr Vector4i Vector4i::operator%(const Vector4i &p_v) const {
237 return Vector4i(x % p_v.x, y % p_v.y, z % p_v.z, w % p_v.w);
238 }
239
240 constexpr Vector4i &Vector4i::operator*=(int32_t p_scalar) {
241 x *= p_scalar;
242 y *= p_scalar;
243 z *= p_scalar;
244 w *= p_scalar;
245 return *this;
246 }
247
248 constexpr Vector4i Vector4i::operator*(int32_t p_scalar) const {
249 return Vector4i(x * p_scalar, y * p_scalar, z * p_scalar, w * p_scalar);
250 }
251
252 // Multiplication operators required to workaround issues with LLVM using implicit conversion.
253
254 constexpr Vector4i operator*(int32_t p_scalar, const Vector4i &p_vector) {
255 return p_vector * p_scalar;
256 }
257
258 constexpr Vector4i operator*(int64_t p_scalar, const Vector4i &p_vector) {
259 return p_vector * p_scalar;
260 }
261
262 constexpr Vector4i operator*(float p_scalar, const Vector4i &p_vector) {
263 return p_vector * p_scalar;
264 }
265
266 constexpr Vector4i operator*(double p_scalar, const Vector4i &p_vector) {
267 return p_vector * p_scalar;
268 }
269
270 constexpr Vector4i &Vector4i::operator/=(int32_t p_scalar) {
271 x /= p_scalar;
272 y /= p_scalar;
273 z /= p_scalar;
274 w /= p_scalar;
275 return *this;
276 }
277
278 constexpr Vector4i Vector4i::operator/(int32_t p_scalar) const {
279 return Vector4i(x / p_scalar, y / p_scalar, z / p_scalar, w / p_scalar);
280 }
281
282 constexpr Vector4i &Vector4i::operator%=(int32_t p_scalar) {
283 x %= p_scalar;
284 y %= p_scalar;
285 z %= p_scalar;
286 w %= p_scalar;
287 return *this;
288 }
289
290 constexpr Vector4i Vector4i::operator%(int32_t p_scalar) const {
291 return Vector4i(x % p_scalar, y % p_scalar, z % p_scalar, w % p_scalar);
292 }
293
294 constexpr Vector4i Vector4i::operator-() const {
295 return Vector4i(-x, -y, -z, -w);
296 }
297
298 constexpr bool Vector4i::operator==(const Vector4i &p_v) const {
299 return (x == p_v.x && y == p_v.y && z == p_v.z && w == p_v.w);
300 }
301
302 constexpr bool Vector4i::operator!=(const Vector4i &p_v) const {
303 return (x != p_v.x || y != p_v.y || z != p_v.z || w != p_v.w);
304 }
305
306 constexpr bool Vector4i::operator<(const Vector4i &p_v) const {
307 if (x == p_v.x) {
308 if (y == p_v.y) {
309 if (z == p_v.z) {
310 return w < p_v.w;
311 } else {
312 return z < p_v.z;
313 }
314 } else {
315 return y < p_v.y;
316 }
317 } else {
318 return x < p_v.x;
319 }
320 }
321
322 constexpr bool Vector4i::operator>(const Vector4i &p_v) const {
323 if (x == p_v.x) {
324 if (y == p_v.y) {
325 if (z == p_v.z) {
326 return w > p_v.w;
327 } else {
328 return z > p_v.z;
329 }
330 } else {
331 return y > p_v.y;
332 }
333 } else {
334 return x > p_v.x;
335 }
336 }
337
338 constexpr bool Vector4i::operator<=(const Vector4i &p_v) const {
339 if (x == p_v.x) {
340 if (y == p_v.y) {
341 if (z == p_v.z) {
342 return w <= p_v.w;
343 } else {
344 return z < p_v.z;
345 }
346 } else {
347 return y < p_v.y;
348 }
349 } else {
350 return x < p_v.x;
351 }
352 }
353
354 constexpr bool Vector4i::operator>=(const Vector4i &p_v) const {
355 if (x == p_v.x) {
356 if (y == p_v.y) {
357 if (z == p_v.z) {
358 return w >= p_v.w;
359 } else {
360 return z > p_v.z;
361 }
362 } else {
363 return y > p_v.y;
364 }
365 } else {
366 return x > p_v.x;
367 }
368 }
369
370 void Vector4i::zero() {
371 x = y = z = w = 0;
372 }
373
374 template <>
375 struct is_zero_constructible<Vector4i> : std::true_type {};