Skip to content

Commit 6ed09e3

Browse files
committed
Add scalar constructor to initalize all components in Vector types.
1 parent 149a4b4 commit 6ed09e3

File tree

12 files changed

+72
-6
lines changed

12 files changed

+72
-6
lines changed

core/math/vector2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ struct [[nodiscard]] Vector2 {
195195
x(0), y(0) {}
196196
constexpr Vector2(real_t p_x, real_t p_y) :
197197
x(p_x), y(p_y) {}
198+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
199+
constexpr explicit Vector2(V p_v) :
200+
x(p_v), y(p_v) {}
198201
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
199202
};
200203

core/math/vector2i.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ struct [[nodiscard]] Vector2i {
152152
x(0), y(0) {}
153153
constexpr Vector2i(int32_t p_x, int32_t p_y) :
154154
x(p_x), y(p_y) {}
155+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
156+
constexpr explicit Vector2i(V p_v) :
157+
x(p_v), y(p_v) {}
155158
// NOLINTEND(cppcoreguidelines-pro-type-member-init)
156159
};
157160

core/math/vector3.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ struct [[nodiscard]] Vector3 {
206206
x(0), y(0), z(0) {}
207207
constexpr Vector3(real_t p_x, real_t p_y, real_t p_z) :
208208
x(p_x), y(p_y), z(p_z) {}
209+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
210+
constexpr explicit Vector3(V p_v) :
211+
x(p_v), y(p_v), z(p_v) {}
209212
};
210213

211214
inline constexpr Vector3 Vector3::LEFT = { -1, 0, 0 };

core/math/vector3i.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ struct [[nodiscard]] Vector3i {
144144
x(0), y(0), z(0) {}
145145
constexpr Vector3i(int32_t p_x, int32_t p_y, int32_t p_z) :
146146
x(p_x), y(p_y), z(p_z) {}
147+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
148+
constexpr explicit Vector3i(V p_v) :
149+
x(p_v), y(p_v), z(p_v) {}
147150
};
148151

149152
inline constexpr Vector3i Vector3i::LEFT = { -1, 0, 0 };

core/math/vector4.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ struct [[nodiscard]] Vector4 {
150150
x(0), y(0), z(0), w(0) {}
151151
constexpr Vector4(real_t p_x, real_t p_y, real_t p_z, real_t p_w) :
152152
x(p_x), y(p_y), z(p_z), w(p_w) {}
153+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
154+
constexpr explicit Vector4(V p_v) :
155+
x(p_v), y(p_v), z(p_v), w(p_v) {}
153156
};
154157

155158
real_t Vector4::dot(const Vector4 &p_vec4) const {

core/math/vector4i.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ struct [[nodiscard]] Vector4i {
140140
Vector4i(const Vector4 &p_vec4);
141141
constexpr Vector4i(int32_t p_x, int32_t p_y, int32_t p_z, int32_t p_w) :
142142
x(p_x), y(p_y), z(p_z), w(p_w) {}
143+
template <typename V, std::enable_if_t<std::is_arithmetic_v<V>, int> = 0>
144+
constexpr explicit Vector4i(V p_v) :
145+
x(p_v), y(p_v), z(p_v), w(p_v) {}
143146
};
144147

145148
int64_t Vector4i::length_squared() const {

tests/core/math/test_vector2.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ namespace TestVector2 {
3838

3939
TEST_CASE("[Vector2] Constructor methods") {
4040
constexpr Vector2 vector_empty = Vector2();
41+
constexpr Vector2 vector_from_int_zero = Vector2(0);
42+
constexpr Vector2 vector_from_float_zero = Vector2(0.0f);
4143
constexpr Vector2 vector_zero = Vector2(0.0, 0.0);
4244
static_assert(
4345
vector_empty == vector_zero,
44-
"Vector2 Constructor with no inputs should return a zero Vector2.");
46+
"Default constructor Vector2() should create a zero Vector2.");
47+
static_assert(
48+
vector_from_int_zero == vector_zero,
49+
"Vector2(int scalar) should create a zero Vector2 when scalar is 0.");
50+
static_assert(
51+
vector_from_float_zero == vector_zero,
52+
"Vector2(float scalar) should create a zero Vector2 when scalar is 0.0f.");
4553
}
4654

4755
TEST_CASE("[Vector2] Angle methods") {

tests/core/math/test_vector2i.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,18 @@ namespace TestVector2i {
3838

3939
TEST_CASE("[Vector2i] Constructor methods") {
4040
constexpr Vector2i vector_empty = Vector2i();
41+
constexpr Vector2i vector_from_int_zero = Vector2i(0);
42+
constexpr Vector2i vector_from_float_half = Vector2i(0.5f);
4143
constexpr Vector2i vector_zero = Vector2i(0, 0);
4244
static_assert(
4345
vector_empty == vector_zero,
44-
"Vector2i Constructor with no inputs should return a zero Vector2i.");
46+
"Default constructor Vector2i() should create a zero Vector2i.");
47+
static_assert(
48+
vector_from_int_zero == vector_zero,
49+
"Vector2i(int scalar) should create a zero Vector2i when scalar is 0.");
50+
static_assert(
51+
vector_from_float_half == vector_zero,
52+
"Vector2i(float scalar) should create a zero Vector2i when scalar < 1 (truncated to 0).");
4553
}
4654

4755
TEST_CASE("[Vector2i] Axis methods") {

tests/core/math/test_vector3.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ namespace TestVector3 {
3737

3838
TEST_CASE("[Vector3] Constructor methods") {
3939
constexpr Vector3 vector_empty = Vector3();
40+
constexpr Vector3 vector_from_int_zero = Vector3(0);
41+
constexpr Vector3 vector_from_float_zero = Vector3(0.0f);
4042
constexpr Vector3 vector_zero = Vector3(0.0, 0.0, 0.0);
4143
static_assert(
4244
vector_empty == vector_zero,
43-
"Vector3 Constructor with no inputs should return a zero Vector3.");
45+
"Default constructor Vector3() should create a zero Vector3.");
46+
static_assert(
47+
vector_from_int_zero == vector_zero,
48+
"Vector3(int scalar) should create a zero Vector3 when scalar is 0.");
49+
static_assert(
50+
vector_from_float_zero == vector_zero,
51+
"Vector3(float scalar) should create a zero Vector3 when scalar is 0.0f.");
4452
}
4553

4654
TEST_CASE("[Vector3] Angle methods") {

tests/core/math/test_vector3i.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,18 @@ namespace TestVector3i {
3737

3838
TEST_CASE("[Vector3i] Constructor methods") {
3939
constexpr Vector3i vector_empty = Vector3i();
40+
constexpr Vector3i vector_from_int_zero = Vector3i(0);
41+
constexpr Vector3i vector_from_float_half = Vector3i(0.5f);
4042
constexpr Vector3i vector_zero = Vector3i(0, 0, 0);
4143
static_assert(
4244
vector_empty == vector_zero,
43-
"Vector3i Constructor with no inputs should return a zero Vector3i.");
45+
"Default constructor Vector3i() should create a zero Vector3i.");
46+
static_assert(
47+
vector_from_int_zero == vector_zero,
48+
"Vector3i(int scalar) should create a zero Vector3i when scalar is 0.");
49+
static_assert(
50+
vector_from_float_half == vector_zero,
51+
"Vector3i(float scalar) should create a zero Vector3i when scalar < 1 (truncated to 0).");
4452
}
4553

4654
TEST_CASE("[Vector3i] Axis methods") {

0 commit comments

Comments
 (0)