Skip to content

Commit

Permalink
Core: Add constexpr constructors to math structs
Browse files Browse the repository at this point in the history
  • Loading branch information
Repiteo committed Oct 21, 2024
1 parent 44fa552 commit 1680605
Show file tree
Hide file tree
Showing 32 changed files with 152 additions and 191 deletions.
4 changes: 2 additions & 2 deletions core/math/aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ struct [[nodiscard]] AABB {

operator String() const;

_FORCE_INLINE_ AABB() {}
inline AABB(const Vector3 &p_pos, const Vector3 &p_size) :
constexpr AABB() {}
constexpr AABB(const Vector3 &p_pos, const Vector3 &p_size) :
position(p_pos),
size(p_size) {
}
Expand Down
27 changes: 14 additions & 13 deletions core/math/audio_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ static const float AUDIO_MIN_PEAK_DB = -200.0f; // linear_to_db(AUDIO_PEAK_OFFSE
struct AudioFrame {
// Left and right samples.
union {
// NOLINTBEGIN(modernize-use-default-member-init)
struct {
float left;
float right;
Expand All @@ -64,6 +65,7 @@ struct AudioFrame {
};
#endif
float levels[2] = { 0.0 };
// NOLINTEND(modernize-use-default-member-init)
};

_ALWAYS_INLINE_ const float &operator[](int p_idx) const {
Expand Down Expand Up @@ -133,14 +135,12 @@ struct AudioFrame {
return res;
}

_ALWAYS_INLINE_ AudioFrame(float p_left, float p_right) {
left = p_left;
right = p_right;
}
_ALWAYS_INLINE_ AudioFrame(const AudioFrame &p_frame) {
left = p_frame.left;
right = p_frame.right;
}
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
constexpr AudioFrame(float p_left, float p_right) :
left(p_left), right(p_right) {}
constexpr AudioFrame(const AudioFrame &p_frame) :
left(p_frame.left), right(p_frame.right) {}
// NOLINTEND(cppcoreguidelines-pro-type-member-init)

_ALWAYS_INLINE_ void operator=(const AudioFrame &p_frame) {
left = p_frame.left;
Expand All @@ -151,11 +151,12 @@ struct AudioFrame {
return Vector2(left, right);
}

_ALWAYS_INLINE_ AudioFrame(const Vector2 &p_v2) {
left = p_v2.x;
right = p_v2.y;
}
_ALWAYS_INLINE_ AudioFrame() {}
// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
constexpr AudioFrame(const Vector2 &p_v2) :
left(p_v2.x), right(p_v2.y) {}
constexpr AudioFrame() :
left(0), right(0) {}
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
};

_ALWAYS_INLINE_ AudioFrame operator*(float p_scalar, const AudioFrame &p_frame) {
Expand Down
20 changes: 13 additions & 7 deletions core/math/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,12 @@ struct [[nodiscard]] Basis {
rows[0].z * p_m[0].y + rows[1].z * p_m[1].y + rows[2].z * p_m[2].y,
rows[0].z * p_m[0].z + rows[1].z * p_m[1].z + rows[2].z * p_m[2].z);
}
Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) {
set(p_xx, p_xy, p_xz, p_yx, p_yy, p_yz, p_zx, p_zy, p_zz);
}
constexpr Basis(real_t p_xx, real_t p_xy, real_t p_xz, real_t p_yx, real_t p_yy, real_t p_yz, real_t p_zx, real_t p_zy, real_t p_zz) :
rows{
{ p_xx, p_xy, p_xz },
{ p_yx, p_yy, p_yz },
{ p_zx, p_zy, p_zz },
} {}

void orthonormalize();
Basis orthonormalized() const;
Expand All @@ -230,11 +233,14 @@ struct [[nodiscard]] Basis {
Basis(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) { set_axis_angle_scale(p_axis, p_angle, p_scale); }
static Basis from_scale(const Vector3 &p_scale);

_FORCE_INLINE_ Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) {
set_columns(p_x_axis, p_y_axis, p_z_axis);
}
constexpr Basis(const Vector3 &p_x_axis, const Vector3 &p_y_axis, const Vector3 &p_z_axis) :
rows{
{ p_x_axis.x, p_y_axis.x, p_z_axis.x },
{ p_x_axis.y, p_y_axis.y, p_z_axis.y },
{ p_x_axis.z, p_y_axis.z, p_z_axis.z },
} {}

_FORCE_INLINE_ Basis() {}
constexpr Basis() {}

private:
// Helper method.
Expand Down
2 changes: 0 additions & 2 deletions core/math/bvh.h
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,6 @@ class BVH_Manager {
return;
}

BOUNDS bb;

typename BVHTREE_CLASS::CullParams params;

params.result_count_overall = 0;
Expand Down
31 changes: 12 additions & 19 deletions core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ class String;

struct [[nodiscard]] Color {
union {
// NOLINTBEGIN(modernize-use-default-member-init)
struct {
float r;
float g;
float b;
float a;
};
float components[4] = { 0, 0, 0, 1.0 };
// NOLINTEND(modernize-use-default-member-init)
};

uint32_t to_rgba32() const;
Expand Down Expand Up @@ -234,39 +236,29 @@ struct [[nodiscard]] Color {
_FORCE_INLINE_ void set_ok_hsl_s(float p_s) { set_ok_hsl(get_ok_hsl_h(), p_s, get_ok_hsl_l(), a); }
_FORCE_INLINE_ void set_ok_hsl_l(float p_l) { set_ok_hsl(get_ok_hsl_h(), get_ok_hsl_s(), p_l, a); }

_FORCE_INLINE_ Color() {}
constexpr Color() :
r(0), g(0), b(0), a(1) {}

/**
* RGBA construct parameters.
* Alpha is not optional as otherwise we can't bind the RGB version for scripting.
*/
_FORCE_INLINE_ Color(float p_r, float p_g, float p_b, float p_a) {
r = p_r;
g = p_g;
b = p_b;
a = p_a;
}
constexpr Color(float p_r, float p_g, float p_b, float p_a) :
r(p_r), g(p_g), b(p_b), a(p_a) {}

/**
* RGB construct parameters.
*/
_FORCE_INLINE_ Color(float p_r, float p_g, float p_b) {
r = p_r;
g = p_g;
b = p_b;
a = 1.0f;
}
constexpr Color(float p_r, float p_g, float p_b) :
r(p_r), g(p_g), b(p_b), a(1) {}

/**
* Construct a Color from another Color, but with the specified alpha value.
*/
_FORCE_INLINE_ Color(const Color &p_c, float p_a) {
r = p_c.r;
g = p_c.g;
b = p_c.b;
a = p_a;
}
constexpr Color(const Color &p_c, float p_a) :
r(p_c.r), g(p_c.g), b(p_c.b), a(p_a) {}

// NOLINTBEGIN(cppcoreguidelines-pro-type-member-init)
Color(const String &p_code) {
if (html_is_valid(p_code)) {
*this = html(p_code);
Expand All @@ -279,6 +271,7 @@ struct [[nodiscard]] Color {
*this = Color(p_code);
a = p_a;
}
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
};

bool Color::operator<(const Color &p_color) const {
Expand Down
9 changes: 3 additions & 6 deletions core/math/face3.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,9 @@ struct [[nodiscard]] Face3 {
_FORCE_INLINE_ bool intersects_aabb2(const AABB &p_aabb) const;
operator String() const;

inline Face3() {}
inline Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) {
vertex[0] = p_v1;
vertex[1] = p_v2;
vertex[2] = p_v3;
}
constexpr Face3() {}
constexpr Face3(const Vector3 &p_v1, const Vector3 &p_v2, const Vector3 &p_v3) :
vertex{ p_v1, p_v2, p_v3 } {}
};

bool Face3::intersects_aabb2(const AABB &p_aabb) const {
Expand Down
8 changes: 4 additions & 4 deletions core/math/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ struct [[nodiscard]] Plane {
_FORCE_INLINE_ bool operator!=(const Plane &p_plane) const;
operator String() const;

_FORCE_INLINE_ Plane() {}
_FORCE_INLINE_ Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
constexpr Plane() {}
constexpr Plane(real_t p_a, real_t p_b, real_t p_c, real_t p_d) :
normal(p_a, p_b, p_c),
d(p_d) {}

_FORCE_INLINE_ Plane(const Vector3 &p_normal, real_t p_d = 0.0);
constexpr Plane(const Vector3 &p_normal, real_t p_d = 0.0);
_FORCE_INLINE_ Plane(const Vector3 &p_normal, const Vector3 &p_point);
_FORCE_INLINE_ Plane(const Vector3 &p_point1, const Vector3 &p_point2, const Vector3 &p_point3, ClockDirection p_dir = CLOCKWISE);
};
Expand All @@ -104,7 +104,7 @@ bool Plane::has_point(const Vector3 &p_point, real_t p_tolerance) const {
return (dist <= p_tolerance);
}

Plane::Plane(const Vector3 &p_normal, real_t p_d) :
constexpr Plane::Plane(const Vector3 &p_normal, real_t p_d) :
normal(p_normal),
d(p_d) {
}
Expand Down
14 changes: 0 additions & 14 deletions core/math/projection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,10 +699,6 @@ void Projection::flip_y() {
}
}

Projection::Projection() {
set_identity();
}

Projection Projection::operator*(const Projection &p_matrix) const {
Projection new_matrix;

Expand Down Expand Up @@ -904,13 +900,6 @@ Projection::operator Transform3D() const {
return tr;
}

Projection::Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) {
columns[0] = p_x;
columns[1] = p_y;
columns[2] = p_z;
columns[3] = p_w;
}

Projection::Projection(const Transform3D &p_transform) {
const Transform3D &tr = p_transform;
real_t *m = &columns[0][0];
Expand All @@ -932,6 +921,3 @@ Projection::Projection(const Transform3D &p_transform) {
m[14] = tr.origin.z;
m[15] = 1.0;
}

Projection::~Projection() {
}
12 changes: 9 additions & 3 deletions core/math/projection.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,16 @@ struct [[nodiscard]] Projection {

real_t get_lod_multiplier() const;

Projection();
Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w);
constexpr Projection() :
columns{
{ 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 },
} {}
constexpr Projection(const Vector4 &p_x, const Vector4 &p_y, const Vector4 &p_z, const Vector4 &p_w) :
columns{ p_x, p_y, p_z, p_w } {}
Projection(const Transform3D &p_transform);
~Projection();
};

Vector3 Projection::xform(const Vector3 &p_vec3) const {
Expand Down
21 changes: 8 additions & 13 deletions core/math/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@

struct [[nodiscard]] Quaternion {
union {
// NOLINTBEGIN(modernize-use-default-member-init)
struct {
real_t x;
real_t y;
real_t z;
real_t w;
};
real_t components[4] = { 0, 0, 0, 1.0 };
// NOLINTEND(modernize-use-default-member-init)
};

_FORCE_INLINE_ real_t &operator[](int p_idx) {
Expand Down Expand Up @@ -115,23 +117,16 @@ struct [[nodiscard]] Quaternion {

operator String() const;

_FORCE_INLINE_ Quaternion() {}
constexpr Quaternion() :
x(0), y(0), z(0), w(1) {}

_FORCE_INLINE_ Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
x(p_x),
y(p_y),
z(p_z),
w(p_w) {
}
constexpr Quaternion(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
x(p_x), y(p_y), z(p_z), w(p_w) {}

Quaternion(const Vector3 &p_axis, real_t p_angle);

Quaternion(const Quaternion &p_q) :
x(p_q.x),
y(p_q.y),
z(p_q.z),
w(p_q.w) {
}
constexpr Quaternion(const Quaternion &p_q) :
x(p_q.x), y(p_q.y), z(p_q.z), w(p_q.w) {}

void operator=(const Quaternion &p_q) {
x = p_q.x;
Expand Down
6 changes: 3 additions & 3 deletions core/math/rect2.h
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,12 @@ struct [[nodiscard]] Rect2 {
operator String() const;
operator Rect2i() const;

Rect2() {}
Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
constexpr Rect2() {}
constexpr Rect2(real_t p_x, real_t p_y, real_t p_width, real_t p_height) :
position(Point2(p_x, p_y)),
size(Size2(p_width, p_height)) {
}
Rect2(const Point2 &p_pos, const Size2 &p_size) :
constexpr Rect2(const Point2 &p_pos, const Size2 &p_size) :
position(p_pos),
size(p_size) {
}
Expand Down
6 changes: 3 additions & 3 deletions core/math/rect2i.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,12 +227,12 @@ struct [[nodiscard]] Rect2i {
operator String() const;
operator Rect2() const;

Rect2i() {}
Rect2i(int p_x, int p_y, int p_width, int p_height) :
constexpr Rect2i() {}
constexpr Rect2i(int p_x, int p_y, int p_width, int p_height) :
position(Point2i(p_x, p_y)),
size(Size2i(p_width, p_height)) {
}
Rect2i(const Point2i &p_pos, const Size2i &p_size) :
constexpr Rect2i(const Point2i &p_pos, const Size2i &p_size) :
position(p_pos),
size(p_size) {
}
Expand Down
31 changes: 14 additions & 17 deletions core/math/transform_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,26 @@ struct [[nodiscard]] Transform2D {

operator String() const;

Transform2D(real_t p_xx, real_t p_xy, real_t p_yx, real_t p_yy, real_t p_ox, real_t p_oy) {
columns[0][0] = p_xx;
columns[0][1] = p_xy;
columns[1][0] = p_yx;
columns[1][1] = p_yy;
columns[2][0] = p_ox;
columns[2][1] = p_oy;
}
constexpr Transform2D(real_t p_xx, real_t p_xy, real_t p_yx, real_t p_yy, real_t p_ox, real_t p_oy) :
columns{
{ p_xx, p_xy },
{ p_yx, p_yy },
{ p_ox, p_oy },
} {}

Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) {
columns[0] = p_x;
columns[1] = p_y;
columns[2] = p_origin;
}
constexpr Transform2D(const Vector2 &p_x, const Vector2 &p_y, const Vector2 &p_origin) :
columns{ p_x, p_y, p_origin } {}

Transform2D(real_t p_rot, const Vector2 &p_pos);

Transform2D(real_t p_rot, const Size2 &p_scale, real_t p_skew, const Vector2 &p_pos);

Transform2D() {
columns[0][0] = 1.0;
columns[1][1] = 1.0;
}
constexpr Transform2D() :
columns{
{ 1, 0 },
{ 0, 1 },
{ 0, 0 },
} {}
};

Vector2 Transform2D::basis_xform(const Vector2 &p_vec) const {
Expand Down
Loading

0 comments on commit 1680605

Please sign in to comment.