Skip to content

Commit 59aa458

Browse files
RepiteoProfessorOctopus
authored andcommitted
Merge pull request godotengine#109744 from Repiteo/core/math-constants-semantic
Core: Integrate semantic constants in math structs
1 parent b617884 commit 59aa458

16 files changed

+198
-129
lines changed

core/math/basis.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
#include "core/math/vector3.h"
3535

3636
struct [[nodiscard]] Basis {
37+
static const Basis FLIP_X;
38+
static const Basis FLIP_Y;
39+
static const Basis FLIP_Z;
40+
3741
Vector3 rows[3] = {
3842
Vector3(1, 0, 0),
3943
Vector3(0, 1, 0),
@@ -224,7 +228,7 @@ struct [[nodiscard]] Basis {
224228

225229
operator Quaternion() const { return get_quaternion(); }
226230

227-
static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
231+
static Basis looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
228232

229233
Basis(const Quaternion &p_quaternion) { set_quaternion(p_quaternion); }
230234
Basis(const Quaternion &p_quaternion, const Vector3 &p_scale) { set_quaternion_scale(p_quaternion, p_scale); }
@@ -247,6 +251,10 @@ struct [[nodiscard]] Basis {
247251
void _set_diagonal(const Vector3 &p_diag);
248252
};
249253

254+
inline constexpr Basis Basis::FLIP_X = { { -1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
255+
inline constexpr Basis Basis::FLIP_Y = { { 1, 0, 0 }, { 0, -1, 0 }, { 0, 0, 1 } };
256+
inline constexpr Basis Basis::FLIP_Z = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, -1 } };
257+
250258
constexpr bool Basis::operator==(const Basis &p_matrix) const {
251259
for (int i = 0; i < 3; i++) {
252260
for (int j = 0; j < 3; j++) {

core/math/plane.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
class Variant;
3636

3737
struct [[nodiscard]] Plane {
38+
static const Plane PLANE_YZ;
39+
static const Plane PLANE_XZ;
40+
static const Plane PLANE_XY;
41+
3842
Vector3 normal;
3943
real_t d = 0;
4044

@@ -90,6 +94,10 @@ struct [[nodiscard]] Plane {
9094
_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
9195
};
9296

97+
inline constexpr Plane Plane::PLANE_YZ = { 1, 0, 0, 0 };
98+
inline constexpr Plane Plane::PLANE_XZ = { 0, 1, 0, 0 };
99+
inline constexpr Plane Plane::PLANE_XY = { 0, 0, 1, 0 };
100+
93101
bool Plane::is_point_over(const Vector3 &p_point) const {
94102
return (normal.dot(p_point) > d);
95103
}

core/math/transform_2d.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ struct [[nodiscard]] Transform2D {
5252
// WARNING: Be aware that unlike 3D code, 2D code uses a left-handed coordinate system:
5353
// Y-axis points down, and angle is measure from +X to +Y in a clockwise-fashion.
5454

55+
static const Transform2D FLIP_X;
56+
static const Transform2D FLIP_Y;
57+
5558
Vector2 columns[3] = {
5659
{ 1, 0 },
5760
{ 0, 1 },
@@ -149,6 +152,9 @@ struct [[nodiscard]] Transform2D {
149152
Transform2D() = default;
150153
};
151154

155+
inline constexpr Transform2D Transform2D::FLIP_X = { { -1, 0 }, { 0, 1 }, { 0, 0 } };
156+
inline constexpr Transform2D Transform2D::FLIP_Y = { { 1, 0 }, { 0, -1 }, { 0, 0 } };
157+
152158
constexpr bool Transform2D::operator==(const Transform2D &p_transform) const {
153159
for (int i = 0; i < 3; i++) {
154160
if (columns[i] != p_transform.columns[i]) {

core/math/transform_3d.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include "core/templates/vector.h"
3737

3838
struct [[nodiscard]] Transform3D {
39+
static const Transform3D FLIP_X;
40+
static const Transform3D FLIP_Y;
41+
static const Transform3D FLIP_Z;
42+
3943
Basis basis;
4044
Vector3 origin;
4145

@@ -51,8 +55,8 @@ struct [[nodiscard]] Transform3D {
5155
void rotate(const Vector3 &p_axis, real_t p_angle);
5256
void rotate_basis(const Vector3 &p_axis, real_t p_angle);
5357

54-
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false);
55-
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3(0, 1, 0), bool p_use_model_front = false) const;
58+
void set_look_at(const Vector3 &p_eye, const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false);
59+
Transform3D looking_at(const Vector3 &p_target, const Vector3 &p_up = Vector3::UP, bool p_use_model_front = false) const;
5660

5761
void scale(const Vector3 &p_scale);
5862
Transform3D scaled(const Vector3 &p_scale) const;
@@ -136,6 +140,10 @@ struct [[nodiscard]] Transform3D {
136140
origin(p_ox, p_oy, p_oz) {}
137141
};
138142

143+
inline constexpr Transform3D Transform3D::FLIP_X = { Basis::FLIP_X };
144+
inline constexpr Transform3D Transform3D::FLIP_Y = { Basis::FLIP_Y };
145+
inline constexpr Transform3D Transform3D::FLIP_Z = { Basis::FLIP_Z };
146+
139147
constexpr bool Transform3D::operator==(const Transform3D &p_transform) const {
140148
return (basis == p_transform.basis && origin == p_transform.origin);
141149
}

core/math/vector2.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ class String;
3737
struct Vector2i;
3838

3939
struct [[nodiscard]] Vector2 {
40-
static const int AXIS_COUNT = 2;
40+
static const Vector2 LEFT;
41+
static const Vector2 RIGHT;
42+
static const Vector2 UP;
43+
static const Vector2 DOWN;
44+
45+
static constexpr int AXIS_COUNT = 2;
4146

4247
enum Axis {
4348
AXIS_X,
@@ -193,6 +198,11 @@ struct [[nodiscard]] Vector2 {
193198
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
194199
};
195200

201+
inline constexpr Vector2 Vector2::LEFT = { -1, 0 };
202+
inline constexpr Vector2 Vector2::RIGHT = { 1, 0 };
203+
inline constexpr Vector2 Vector2::UP = { 0, -1 };
204+
inline constexpr Vector2 Vector2::DOWN = { 0, 1 };
205+
196206
_FORCE_INLINE_ Vector2 Vector2::plane_project(real_t p_d, const Vector2 &p_vec) const {
197207
return p_vec - *this * (dot(p_vec) - p_d);
198208
}

core/math/vector2i.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ class String;
3737
struct Vector2;
3838

3939
struct [[nodiscard]] Vector2i {
40-
static const int AXIS_COUNT = 2;
40+
static const Vector2i LEFT;
41+
static const Vector2i RIGHT;
42+
static const Vector2i UP;
43+
static const Vector2i DOWN;
44+
45+
static constexpr int AXIS_COUNT = 2;
4146

4247
enum Axis {
4348
AXIS_X,
@@ -150,6 +155,11 @@ struct [[nodiscard]] Vector2i {
150155
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
151156
};
152157

158+
inline constexpr Vector2i Vector2i::LEFT = { -1, 0 };
159+
inline constexpr Vector2i Vector2i::RIGHT = { 1, 0 };
160+
inline constexpr Vector2i Vector2i::UP = { 0, -1 };
161+
inline constexpr Vector2i Vector2i::DOWN = { 0, 1 };
162+
153163
constexpr Vector2i Vector2i::operator+(const Vector2i &p_v) const {
154164
return Vector2i(x + p_v.x, y + p_v.y);
155165
}

core/math/vector3.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,20 @@ struct Vector2;
3939
struct Vector3i;
4040

4141
struct [[nodiscard]] Vector3 {
42-
static const int AXIS_COUNT = 3;
42+
static const Vector3 LEFT;
43+
static const Vector3 RIGHT;
44+
static const Vector3 UP;
45+
static const Vector3 DOWN;
46+
static const Vector3 FORWARD;
47+
static const Vector3 BACK;
48+
static const Vector3 MODEL_LEFT;
49+
static const Vector3 MODEL_RIGHT;
50+
static const Vector3 MODEL_TOP;
51+
static const Vector3 MODEL_BOTTOM;
52+
static const Vector3 MODEL_FRONT;
53+
static const Vector3 MODEL_REAR;
54+
55+
static constexpr int AXIS_COUNT = 3;
4356

4457
enum Axis {
4558
AXIS_X,
@@ -207,6 +220,19 @@ struct [[nodiscard]] Vector3 {
207220
x(p_x), y(p_y), z(p_z) {}
208221
};
209222

223+
inline constexpr Vector3 Vector3::LEFT = { -1, 0, 0 };
224+
inline constexpr Vector3 Vector3::RIGHT = { 1, 0, 0 };
225+
inline constexpr Vector3 Vector3::UP = { 0, 1, 0 };
226+
inline constexpr Vector3 Vector3::DOWN = { 0, -1, 0 };
227+
inline constexpr Vector3 Vector3::FORWARD = { 0, 0, -1 };
228+
inline constexpr Vector3 Vector3::BACK = { 0, 0, 1 };
229+
inline constexpr Vector3 Vector3::MODEL_LEFT = { 1, 0, 0 };
230+
inline constexpr Vector3 Vector3::MODEL_RIGHT = { -1, 0, 0 };
231+
inline constexpr Vector3 Vector3::MODEL_TOP = { 0, 1, 0 };
232+
inline constexpr Vector3 Vector3::MODEL_BOTTOM = { 0, -1, 0 };
233+
inline constexpr Vector3 Vector3::MODEL_FRONT = { 0, 0, 1 };
234+
inline constexpr Vector3 Vector3::MODEL_REAR = { 0, 0, -1 };
235+
210236
Vector3 Vector3::cross(const Vector3 &p_with) const {
211237
Vector3 ret(
212238
(y * p_with.z) - (z * p_with.y),
@@ -346,7 +372,7 @@ Vector3 Vector3::get_any_perpendicular() const {
346372
// since it could be a different vector depending on the prior branching code Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z).
347373
// However, it would be reasonable to use any of the axes of the basis, as it is simpler to calculate.
348374
ERR_FAIL_COND_V_MSG(is_zero_approx(), Vector3(0, 0, 0), "The Vector3 must not be zero.");
349-
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3(1, 0, 0) : Vector3(0, 1, 0)).normalized();
375+
return cross((Math::abs(x) <= Math::abs(y) && Math::abs(x) <= Math::abs(z)) ? Vector3::RIGHT : Vector3::UP).normalized();
350376
}
351377

352378
/* Operators */

core/math/vector3i.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ class String;
3737
struct Vector3;
3838

3939
struct [[nodiscard]] Vector3i {
40-
static const int AXIS_COUNT = 3;
40+
static const Vector3i LEFT;
41+
static const Vector3i RIGHT;
42+
static const Vector3i UP;
43+
static const Vector3i DOWN;
44+
static const Vector3i FORWARD;
45+
static const Vector3i BACK;
46+
47+
static constexpr int AXIS_COUNT = 3;
4148

4249
enum Axis {
4350
AXIS_X,
@@ -139,6 +146,13 @@ struct [[nodiscard]] Vector3i {
139146
x(p_x), y(p_y), z(p_z) {}
140147
};
141148

149+
inline constexpr Vector3i Vector3i::LEFT = { -1, 0, 0 };
150+
inline constexpr Vector3i Vector3i::RIGHT = { 1, 0, 0 };
151+
inline constexpr Vector3i Vector3i::UP = { 0, 1, 0 };
152+
inline constexpr Vector3i Vector3i::DOWN = { 0, -1, 0 };
153+
inline constexpr Vector3i Vector3i::FORWARD = { 0, 0, -1 };
154+
inline constexpr Vector3i Vector3i::BACK = { 0, 0, 1 };
155+
142156
int64_t Vector3i::length_squared() const {
143157
return x * (int64_t)x + y * (int64_t)y + z * (int64_t)z;
144158
}

core/math/vector4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class String;
3838
struct Vector4i;
3939

4040
struct [[nodiscard]] Vector4 {
41-
static const int AXIS_COUNT = 4;
41+
static constexpr int AXIS_COUNT = 4;
4242

4343
enum Axis {
4444
AXIS_X,

core/math/vector4i.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class String;
3737
struct Vector4;
3838

3939
struct [[nodiscard]] Vector4i {
40-
static const int AXIS_COUNT = 4;
40+
static constexpr int AXIS_COUNT = 4;
4141

4242
enum Axis {
4343
AXIS_X,

0 commit comments

Comments
 (0)