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