Debian Codegraph @

core/math/vector2i.h

raw symbols SCIP
1 /**************************************************************************/
2 /* vector2i.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 Vector2;
39
40 struct [[nodiscard]] Vector2i {
41 static const Vector2i LEFT;
42 static const Vector2i RIGHT;
43 static const Vector2i UP;
44 static const Vector2i DOWN;
45
46 static constexpr int AXIS_COUNT = 2;
47
48 enum Axis {
49 AXIS_X,
50 AXIS_Y,
51 };
52
53 union {
54 // NOLINTBEGIN(modernize-use-default-member-init)
55 struct {
56 int32_t x;
57 int32_t y;
58 };
59
60 struct {
61 int32_t width;
62 int32_t height;
63 };
64
65 int32_t coord[2] = { 0 };
66 // NOLINTEND(modernize-use-default-member-init)
67 };
68
69 _FORCE_INLINE_ int32_t &operator[](int p_axis) {
70 DEV_ASSERT((unsigned int)p_axis < 2);
71 return coord[p_axis];
72 }
73 _FORCE_INLINE_ const int32_t &operator[](int p_axis) const {
74 DEV_ASSERT((unsigned int)p_axis < 2);
75 return coord[p_axis];
76 }
77
78 _FORCE_INLINE_ Vector2i::Axis min_axis_index() const {
79 return x < y ? Vector2i::AXIS_X : Vector2i::AXIS_Y;
80 }
81
82 _FORCE_INLINE_ Vector2i::Axis max_axis_index() const {
83 return x < y ? Vector2i::AXIS_Y : Vector2i::AXIS_X;
84 }
85
86 Vector2i min(const Vector2i &p_vector2i) const {
87 return Vector2i(MIN(x, p_vector2i.x), MIN(y, p_vector2i.y));
88 }
89
90 Vector2i mini(int32_t p_scalar) const {
91 return Vector2i(MIN(x, p_scalar), MIN(y, p_scalar));
92 }
93
94 Vector2i max(const Vector2i &p_vector2i) const {
95 return Vector2i(MAX(x, p_vector2i.x), MAX(y, p_vector2i.y));
96 }
97
98 Vector2i maxi(int32_t p_scalar) const {
99 return Vector2i(MAX(x, p_scalar), MAX(y, p_scalar));
100 }
101
102 double distance_to(const Vector2i &p_to) const {
103 return (p_to - *this).length();
104 }
105
106 int64_t distance_squared_to(const Vector2i &p_to) const {
107 return (p_to - *this).length_squared();
108 }
109
110 constexpr Vector2i operator+(const Vector2i &p_v) const;
111 constexpr void operator+=(const Vector2i &p_v);
112 constexpr Vector2i operator-(const Vector2i &p_v) const;
113 constexpr void operator-=(const Vector2i &p_v);
114 constexpr Vector2i operator*(const Vector2i &p_v1) const;
115
116 constexpr Vector2i operator*(int32_t p_rvalue) const;
117 constexpr void operator*=(int32_t p_rvalue);
118
119 constexpr Vector2i operator/(const Vector2i &p_v1) const;
120 constexpr Vector2i operator/(int32_t p_rvalue) const;
121 constexpr void operator/=(int32_t p_rvalue);
122
123 constexpr Vector2i operator%(const Vector2i &p_v1) const;
124 constexpr Vector2i operator%(int32_t p_rvalue) const;
125 constexpr void operator%=(int32_t p_rvalue);
126
127 constexpr Vector2i operator-() const;
128 constexpr bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
129 constexpr bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }
130
131 constexpr bool operator<=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y <= p_vec2.y) : (x < p_vec2.x); }
132 constexpr bool operator>=(const Vector2i &p_vec2) const { return x == p_vec2.x ? (y >= p_vec2.y) : (x > p_vec2.x); }
133
134 constexpr bool operator==(const Vector2i &p_vec2) const;
135 constexpr bool operator!=(const Vector2i &p_vec2) const;
136
137 int64_t length_squared() const;
138 double length() const;
139
140 real_t aspect() const { return width / (real_t)height; }
141 Vector2i sign() const { return Vector2i(SIGN(x), SIGN(y)); }
142 Vector2i abs() const { return Vector2i(Math::abs(x), Math::abs(y)); }
143 Vector2i clamp(const Vector2i &p_min, const Vector2i &p_max) const;
144 Vector2i clampi(int32_t p_min, int32_t p_max) const;
145 Vector2i snapped(const Vector2i &p_step) const;
146 Vector2i snappedi(int32_t p_step) const;
147
148 explicit operator String() const;
149 operator Vector2() const;
150
151 uint32_t hash() const {
152 uint32_t h = hash_murmur3_one_32(uint32_t(x));
153 h = hash_murmur3_one_32(uint32_t(y), h);
154 return hash_fmix32(h);
155 }
156
157 // NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
158 constexpr Vector2i() :
159 x(0), y(0) {}
160 constexpr Vector2i(int32_t p_x, int32_t p_y) :
161 x(p_x), y(p_y) {}
162 // NOLINTEND(cppcoreguidelines-pro-type-member-init)
163 };
164
165 inline constexpr Vector2i Vector2i::LEFT = { -1, 0 };
166 inline constexpr Vector2i Vector2i::RIGHT = { 1, 0 };
167 inline constexpr Vector2i Vector2i::UP = { 0, -1 };
168 inline constexpr Vector2i Vector2i::DOWN = { 0, 1 };
169
170 constexpr Vector2i Vector2i::operator+(const Vector2i &p_v) const {
171 return Vector2i(x + p_v.x, y + p_v.y);
172 }
173
174 constexpr void Vector2i::operator+=(const Vector2i &p_v) {
175 x += p_v.x;
176 y += p_v.y;
177 }
178
179 constexpr Vector2i Vector2i::operator-(const Vector2i &p_v) const {
180 return Vector2i(x - p_v.x, y - p_v.y);
181 }
182
183 constexpr void Vector2i::operator-=(const Vector2i &p_v) {
184 x -= p_v.x;
185 y -= p_v.y;
186 }
187
188 constexpr Vector2i Vector2i::operator*(const Vector2i &p_v1) const {
189 return Vector2i(x * p_v1.x, y * p_v1.y);
190 }
191
192 constexpr Vector2i Vector2i::operator*(int32_t p_rvalue) const {
193 return Vector2i(x * p_rvalue, y * p_rvalue);
194 }
195
196 constexpr void Vector2i::operator*=(int32_t p_rvalue) {
197 x *= p_rvalue;
198 y *= p_rvalue;
199 }
200
201 constexpr Vector2i Vector2i::operator/(const Vector2i &p_v1) const {
202 return Vector2i(x / p_v1.x, y / p_v1.y);
203 }
204
205 constexpr Vector2i Vector2i::operator/(int32_t p_rvalue) const {
206 return Vector2i(x / p_rvalue, y / p_rvalue);
207 }
208
209 constexpr void Vector2i::operator/=(int32_t p_rvalue) {
210 x /= p_rvalue;
211 y /= p_rvalue;
212 }
213
214 constexpr Vector2i Vector2i::operator%(const Vector2i &p_v1) const {
215 return Vector2i(x % p_v1.x, y % p_v1.y);
216 }
217
218 constexpr Vector2i Vector2i::operator%(int32_t p_rvalue) const {
219 return Vector2i(x % p_rvalue, y % p_rvalue);
220 }
221
222 constexpr void Vector2i::operator%=(int32_t p_rvalue) {
223 x %= p_rvalue;
224 y %= p_rvalue;
225 }
226
227 constexpr Vector2i Vector2i::operator-() const {
228 return Vector2i(-x, -y);
229 }
230
231 constexpr bool Vector2i::operator==(const Vector2i &p_vec2) const {
232 return x == p_vec2.x && y == p_vec2.y;
233 }
234
235 constexpr bool Vector2i::operator!=(const Vector2i &p_vec2) const {
236 return x != p_vec2.x || y != p_vec2.y;
237 }
238
239 // Multiplication operators required to workaround issues with LLVM using implicit conversion.
240
241 constexpr Vector2i operator*(int32_t p_scalar, const Vector2i &p_vector) {
242 return p_vector * p_scalar;
243 }
244
245 constexpr Vector2i operator*(int64_t p_scalar, const Vector2i &p_vector) {
246 return p_vector * p_scalar;
247 }
248
249 constexpr Vector2i operator*(float p_scalar, const Vector2i &p_vector) {
250 return p_vector * p_scalar;
251 }
252
253 constexpr Vector2i operator*(double p_scalar, const Vector2i &p_vector) {
254 return p_vector * p_scalar;
255 }
256
257 typedef Vector2i Size2i;
258 typedef Vector2i Point2i;
259
260 template <>
261 struct is_zero_constructible<Vector2i> : std::true_type {};