Skip to content

Commit 5454c43

Browse files
committed
minor improvement
1 parent 046981b commit 5454c43

File tree

7 files changed

+66
-34
lines changed

7 files changed

+66
-34
lines changed

include/omath/Mat.hpp

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414
namespace omath
1515
{
16-
template<size_t Rows, size_t Columns, class Type = float>
16+
struct MatSize
17+
{
18+
size_t rows, columns;
19+
};
20+
template<size_t Rows = 0, size_t Columns = 0, class Type = float>
1721
class Mat final
1822
{
1923
public:
@@ -56,13 +60,22 @@ namespace omath
5660
}
5761

5862
[[nodiscard]]
59-
static consteval size_t RowCount() noexcept { return Rows; }
63+
static consteval size_t RowCount() noexcept
64+
{
65+
return Rows;
66+
}
6067

6168
[[nodiscard]]
62-
static consteval size_t ColumnsCount() noexcept { return Columns; }
69+
static consteval size_t ColumnsCount() noexcept
70+
{
71+
return Columns;
72+
}
6373

6474
[[nodiscard]]
65-
static consteval std::pair<size_t, size_t> Size() noexcept { return {Rows, Columns}; }
75+
static consteval MatSize Size() noexcept
76+
{
77+
return {Rows, Columns};
78+
}
6679

6780

6881
[[nodiscard]] constexpr const Type &At(const size_t rowIndex, const size_t columnIndex) const
@@ -116,7 +129,7 @@ namespace omath
116129
return result;
117130
}
118131

119-
constexpr Mat &operator*=(const Type& f) noexcept
132+
constexpr Mat &operator*=(const Type &f) noexcept
120133
{
121134
for (size_t i = 0; i < Rows; ++i)
122135
for (size_t j = 0; j < Columns; ++j)
@@ -175,7 +188,7 @@ namespace omath
175188
}
176189

177190
[[nodiscard]]
178-
constexpr Mat<Columns, Rows> Transpose() const noexcept
191+
constexpr Mat<Columns, Rows> Transposed() const noexcept
179192
{
180193
Mat<Columns, Rows> transposed;
181194
for (size_t i = 0; i < Rows; ++i)
@@ -298,6 +311,26 @@ namespace omath
298311
};
299312
}
300313

314+
[[nodiscard]]
315+
constexpr static Mat<4, 1> MatRowFromVector(const Vector3 &vector) noexcept
316+
{
317+
return {{vector.x, vector.y, vector.z, 1}};
318+
}
319+
320+
[[nodiscard]]
321+
constexpr static Mat<1, 4> MatColumnFromVector(const Vector3 &vector) noexcept
322+
{
323+
return
324+
{
325+
{
326+
{vector.x},
327+
{vector.y},
328+
{vector.z},
329+
{1}
330+
}
331+
};
332+
}
333+
301334
private:
302335
std::array<Type, Rows * Columns> m_data;
303336
};

include/omath/Matrix.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#pragma once
2-
#include <vector>
32
#include <memory>
43
#include <string>
54
#include <initializer_list>

include/omath/Vector2.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace omath
1616
float y = 0.f;
1717

1818
// Constructors
19-
constexpr Vector2() : x(0.f), y(0.f) {}
19+
constexpr Vector2() = default;
2020

2121
constexpr Vector2(const float x, const float y) : x(x), y(y) {}
2222

@@ -66,31 +66,31 @@ namespace omath
6666
return *this;
6767
}
6868

69-
constexpr Vector2& operator*=(float fl)
69+
constexpr Vector2& operator*=(const float fl)
7070
{
7171
x *= fl;
7272
y *= fl;
7373

7474
return *this;
7575
}
7676

77-
constexpr Vector2& operator/=(float fl)
77+
constexpr Vector2& operator/=(const float fl)
7878
{
7979
x /= fl;
8080
y /= fl;
8181

8282
return *this;
8383
}
8484

85-
constexpr Vector2& operator+=(float fl)
85+
constexpr Vector2& operator+=(const float fl)
8686
{
8787
x += fl;
8888
y += fl;
8989

9090
return *this;
9191
}
9292

93-
constexpr Vector2& operator-=(float fl)
93+
constexpr Vector2& operator-=(const float fl)
9494
{
9595
x -= fl;
9696
y -= fl;
@@ -177,12 +177,12 @@ namespace omath
177177
return {x - v.x, y - v.y};
178178
}
179179

180-
[[nodiscard]] constexpr Vector2 operator*(float fl) const
180+
[[nodiscard]] constexpr Vector2 operator*(const float fl) const
181181
{
182182
return {x * fl, y * fl};
183183
}
184184

185-
[[nodiscard]] constexpr Vector2 operator/(float fl) const
185+
[[nodiscard]] constexpr Vector2 operator/(const float fl) const
186186
{
187187
return {x / fl, y / fl};
188188
}

include/omath/Vector3.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace omath
1515
{
1616
public:
1717
float z = 0.f;
18-
constexpr Vector3(float x, float y, float z) : Vector2(x, y), z(z) { }
19-
constexpr Vector3() : Vector2(), z(0.f) {};
18+
constexpr Vector3(const float x, const float y, const float z) : Vector2(x, y), z(z) { }
19+
constexpr Vector3() : Vector2() {};
2020

2121
[[nodiscard]] constexpr bool operator==(const Vector3& src) const
2222
{
@@ -44,7 +44,7 @@ namespace omath
4444
return *this;
4545
}
4646

47-
constexpr Vector3& operator*=(float fl)
47+
constexpr Vector3& operator*=(const float fl)
4848
{
4949
Vector2::operator*=(fl);
5050
z *= fl;
@@ -68,23 +68,23 @@ namespace omath
6868
return *this;
6969
}
7070

71-
constexpr Vector3& operator+=(float fl)
71+
constexpr Vector3& operator+=(const float fl)
7272
{
7373
Vector2::operator+=(fl);
7474
z += fl;
7575

7676
return *this;
7777
}
7878

79-
constexpr Vector3& operator/=(float fl)
79+
constexpr Vector3& operator/=(const float fl)
8080
{
8181
Vector2::operator/=(fl);
8282
z /= fl;
8383

8484
return *this;
8585
}
8686

87-
constexpr Vector3& operator-=(float fl)
87+
constexpr Vector3& operator-=(const float fl)
8888
{
8989
Vector2::operator-=(fl);
9090
z -= fl;
@@ -175,7 +175,7 @@ namespace omath
175175
return {x - v.x, y - v.y, z - v.z};
176176
}
177177

178-
[[nodiscard]] constexpr Vector3 operator*(float fl) const
178+
[[nodiscard]] constexpr Vector3 operator*(const float fl) const
179179
{
180180
return {x * fl, y * fl, z * fl};
181181
}
@@ -206,7 +206,7 @@ namespace omath
206206
}
207207
[[nodiscard]] constexpr float Sum() const
208208
{
209-
return Vector3::Sum2D() + z;
209+
return Sum2D() + z;
210210
}
211211

212212
[[nodiscard]] constexpr float Sum2D() const

include/omath/Vector4.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace omath
1414
public:
1515
float w;
1616

17-
constexpr Vector4(float x, float y, float z, float w) : Vector3(x, y, z), w(w) {}
17+
constexpr Vector4(const float x, const float y, const float z, const float w) : Vector3(x, y, z), w(w) {}
1818
constexpr Vector4() : Vector3(), w(0.f) {};
1919

2020
[[nodiscard]]
@@ -45,7 +45,7 @@ namespace omath
4545
return *this;
4646
}
4747

48-
constexpr Vector4& operator*=(float scalar)
48+
constexpr Vector4& operator*=(const float scalar)
4949
{
5050
Vector3::operator*=(scalar);
5151
w *= scalar;
@@ -61,7 +61,7 @@ namespace omath
6161
return *this;
6262
}
6363

64-
constexpr Vector4& operator/=(float scalar)
64+
constexpr Vector4& operator/=(const float scalar)
6565
{
6666
Vector3::operator/=(scalar);
6767
w /= scalar;
@@ -95,7 +95,7 @@ namespace omath
9595

9696
return *this;
9797
}
98-
constexpr Vector4& Clamp(float min, float max)
98+
constexpr Vector4& Clamp(const float min, const float max)
9999
{
100100
x = std::clamp(x, min, max);
101101
y = std::clamp(y, min, max);
@@ -123,7 +123,7 @@ namespace omath
123123
}
124124

125125
[[nodiscard]]
126-
constexpr Vector4 operator*(float scalar) const
126+
constexpr Vector4 operator*(const float scalar) const
127127
{
128128
return {x * scalar, y * scalar, z * scalar, w * scalar};
129129
}
@@ -135,7 +135,7 @@ namespace omath
135135
}
136136

137137
[[nodiscard]]
138-
constexpr Vector4 operator/(float scalar) const
138+
constexpr Vector4 operator/(const float scalar) const
139139
{
140140
return {x / scalar, y / scalar, z / scalar, w / scalar};
141141
}

source/projection/Camera.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ namespace omath::projection
2828
const auto right = Vector3::RightVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
2929
const auto up = Vector3::UpVector(m_viewAngles.x, m_viewAngles.y, m_viewAngles.z);
3030

31-
return Mat<4, 4>::TranslationMat(-m_origin) * Mat<4, 4>::OrientationMat(forward, right, up);
31+
return Mat<>::TranslationMat(-m_origin) * Mat<>::OrientationMat(forward, right, up);
3232
}
3333

3434
std::expected<Vector3, Error> Camera::WorldToScreen(const Vector3& worldPosition) const
3535
{
36-
const auto posVecAsMatrix = Mat<1, 4>({{worldPosition.x, worldPosition.y, worldPosition.z, 1.f}});
36+
const auto posVecAsMatrix = Mat<>::MatColumnFromVector(worldPosition);
3737

3838

39-
const auto projectionMatrix = Mat<4, 4>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
40-
m_nearPlaneDistance, m_farPlaneDistance, 1.335f);
39+
const auto projectionMatrix = Mat<>::ProjectionMat(m_fieldOfView, m_viewPort.AspectRatio(),
40+
m_nearPlaneDistance, m_farPlaneDistance, m_lensZoom);
4141

4242
Mat<1, 4> projected = posVecAsMatrix * (GetViewMatrix() * projectionMatrix);
4343

tests/UnitTestMat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ TEST_F(UnitTestMat, Operator_Division_Scalar)
8888
// Test matrix functions
8989
TEST_F(UnitTestMat, Transpose)
9090
{
91-
Mat<2, 2> m3 = m2.Transpose();
91+
Mat<2, 2> m3 = m2.Transposed();
9292
EXPECT_FLOAT_EQ(m3.At(0, 0), m2.At(0, 0));
9393
EXPECT_FLOAT_EQ(m3.At(0, 1), m2.At(1, 0));
9494
EXPECT_FLOAT_EQ(m3.At(1, 0), m2.At(0, 1));
@@ -215,7 +215,7 @@ TEST(UnitTestMatStandalone, Minor_3x3)
215215
TEST(UnitTestMatStandalone, Transpose_NonSquare)
216216
{
217217
constexpr Mat<2, 3> m{{1.0f, 2.0f, 3.0f}, {4.0f, 5.0f, 6.0f}};
218-
auto transposed = m.Transpose();
218+
auto transposed = m.Transposed();
219219
EXPECT_EQ(transposed.RowCount(), 3);
220220
EXPECT_EQ(transposed.ColumnsCount(), 2);
221221
EXPECT_FLOAT_EQ(transposed.At(0, 0), 1.0f);

0 commit comments

Comments
 (0)