Skip to content

Commit 70313f5

Browse files
authored
Merge pull request #29 from orange-cpp/u/orange-cpp/template-vectors
Added template Vectors 2d 3d 4d
2 parents 9ba3bc7 + 978656b commit 70313f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+223
-256
lines changed

CMakeLists.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ option(OMATH_BUILD_AS_SHARED_LIBRARY "Build Omath as .so or .dll" OFF)
1313
if (OMATH_BUILD_AS_SHARED_LIBRARY)
1414
add_library(omath SHARED source/Vector3.cpp)
1515
else()
16-
add_library(omath STATIC source/Vector3.cpp
17-
include/omath/engines/OpenGL/Constants.hpp
18-
include/omath/engines/OpenGL/Formulas.hpp
19-
include/omath/engines/OpenGL/Camera.hpp)
16+
add_library(omath STATIC source/Matrix.cpp)
2017
endif()
2118

2219
set_target_properties(omath PROPERTIES

include/omath/Color.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ namespace omath
1818
};
1919

2020

21-
class Color final : public Vector4
21+
class Color final : public Vector4<float>
2222
{
2323
public:
24-
constexpr Color(float r, float g, float b, float a) : Vector4(r,g,b,a)
24+
constexpr Color(const float r, const float g, const float b, const float a) : Vector4(r,g,b,a)
2525
{
2626
Clamp(0.f, 1.f);
2727
}

include/omath/Mat.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,21 +333,21 @@ namespace omath
333333

334334
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
335335
[[nodiscard]]
336-
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3& vector) noexcept
336+
constexpr static Mat<1, 4, Type, St> MatRowFromVector(const Vector3<Type>& vector) noexcept
337337
{
338338
return {{vector.x, vector.y, vector.z, 1}};
339339
}
340340

341341
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
342342
[[nodiscard]]
343-
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3& vector) noexcept
343+
constexpr static Mat<4, 1, Type, St> MatColumnFromVector(const Vector3<Type>& vector) noexcept
344344
{
345345
return {{vector.x}, {vector.y}, {vector.z}, {1}};
346346
}
347347

348348
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
349349
[[nodiscard]]
350-
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3& diff) noexcept
350+
constexpr Mat<4, 4, Type, St> MatTranslation(const Vector3<Type>& diff) noexcept
351351
{
352352
return
353353
{
@@ -399,8 +399,8 @@ namespace omath
399399

400400
template<class Type = float, MatStoreType St = MatStoreType::ROW_MAJOR>
401401
[[nodiscard]]
402-
static Mat<4, 4, Type, St> MatCameraView(const Vector3& forward, const Vector3& right, const Vector3& up,
403-
const Vector3& cameraOrigin) noexcept
402+
static Mat<4, 4, Type, St> MatCameraView(const Vector3<Type>& forward, const Vector3<Type>& right,
403+
const Vector3<Type>& up, const Vector3<Type>& cameraOrigin) noexcept
404404
{
405405
return Mat<4, 4, Type, St>
406406
{

include/omath/Matrix.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
#include <initializer_list>
33
#include <memory>
44
#include <string>
5+
#include "Vector3.hpp"
56

67
namespace omath
78
{
8-
class Vector3;
99

1010
class Matrix final
1111
{
@@ -19,10 +19,10 @@ namespace omath
1919
static Matrix ToScreenMatrix(float screenWidth, float screenHeight);
2020

2121
[[nodiscard]]
22-
static Matrix TranslationMatrix(const Vector3& diff);
22+
static Matrix TranslationMatrix(const Vector3<float>& diff);
2323

2424
[[nodiscard]]
25-
static Matrix OrientationMatrix(const Vector3& forward, const Vector3& right, const Vector3& up);
25+
static Matrix OrientationMatrix(const Vector3<float>& forward, const Vector3<float>& right, const Vector3<float>& up);
2626

2727
[[nodiscard]]
2828
static Matrix ProjectionMatrix(float fieldOfView, float aspectRatio, float near, float far);

include/omath/Triangle.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ namespace omath
1616
{
1717
}
1818

19-
Vector3 m_vertex1;
20-
Vector3 m_vertex2;
21-
Vector3 m_vertex3;
19+
Vector3<float> m_vertex1;
20+
Vector3<float> m_vertex2;
21+
Vector3<float> m_vertex3;
2222

2323
[[nodiscard]]
24-
constexpr Vector3 CalculateNormal() const
24+
constexpr Vector3<float> CalculateNormal() const
2525
{
2626
const auto b = SideBVector();
2727
const auto a = SideAVector();
@@ -41,7 +41,7 @@ namespace omath
4141
}
4242

4343
[[nodiscard]]
44-
constexpr Vector3 SideAVector() const
44+
constexpr Vector3<float> SideAVector() const
4545
{
4646
return m_vertex1 - m_vertex2;
4747
}
@@ -61,12 +61,12 @@ namespace omath
6161
return std::abs(sideA*sideA + sideB*sideB - hypot*hypot) <= 0.0001f;
6262
}
6363
[[nodiscard]]
64-
constexpr Vector3 SideBVector() const
64+
constexpr Vector3<float> SideBVector() const
6565
{
6666
return m_vertex3 - m_vertex2;
6767
}
6868
[[nodiscard]]
69-
constexpr Vector3 MidPoint() const
69+
constexpr Vector3<float> MidPoint() const
7070
{
7171
return (m_vertex1 + m_vertex2 + m_vertex3) / 3;
7272
}

include/omath/Vector2.hpp

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@
88

99
namespace omath
1010
{
11+
12+
template<class Type> requires std::is_arithmetic_v<Type>
1113
class Vector2
1214
{
1315
public:
14-
float x = 0.f;
15-
float y = 0.f;
16+
Type x = static_cast<Type>(0);
17+
Type y = static_cast<Type>(0);
1618

1719
// Constructors
1820
constexpr Vector2() = default;
1921

20-
constexpr Vector2(const float x, const float y) : x(x), y(y) {}
22+
constexpr Vector2(const Type& x, const Type& y) : x(x), y(y) {}
2123

2224
// Equality operators
2325
[[nodiscard]]
@@ -65,31 +67,31 @@ namespace omath
6567
return *this;
6668
}
6769

68-
constexpr Vector2& operator*=(const float fl)
70+
constexpr Vector2& operator*=(const Type& fl)
6971
{
7072
x *= fl;
7173
y *= fl;
7274

7375
return *this;
7476
}
7577

76-
constexpr Vector2& operator/=(const float fl)
78+
constexpr Vector2& operator/=(const Type& fl)
7779
{
7880
x /= fl;
7981
y /= fl;
8082

8183
return *this;
8284
}
8385

84-
constexpr Vector2& operator+=(const float fl)
86+
constexpr Vector2& operator+=(const Type& fl)
8587
{
8688
x += fl;
8789
y += fl;
8890

8991
return *this;
9092
}
9193

92-
constexpr Vector2& operator-=(const float fl)
94+
constexpr Vector2& operator-=(const Type& fl)
9395
{
9496
x -= fl;
9597
y -= fl;
@@ -98,45 +100,45 @@ namespace omath
98100
}
99101

100102
// Basic vector operations
101-
[[nodiscard]] float DistTo(const Vector2& vOther) const
103+
[[nodiscard]] Type DistTo(const Vector2& vOther) const
102104
{
103105
return std::sqrt(DistToSqr(vOther));
104106
}
105107

106-
[[nodiscard]] constexpr float DistToSqr(const Vector2& vOther) const
108+
[[nodiscard]] constexpr Type DistToSqr(const Vector2& vOther) const
107109
{
108110
return (x - vOther.x) * (x - vOther.x) + (y - vOther.y) * (y - vOther.y);
109111
}
110112

111-
[[nodiscard]] constexpr float Dot(const Vector2& vOther) const
113+
[[nodiscard]] constexpr Type Dot(const Vector2& vOther) const
112114
{
113115
return x * vOther.x + y * vOther.y;
114116
}
115117

116118
#ifndef _MSC_VER
117-
[[nodiscard]] constexpr float Length() const
119+
[[nodiscard]] constexpr Type& Length() const
118120
{
119121
return std::hypot(x, y);
120122
}
121123

122124
[[nodiscard]] constexpr Vector2 Normalized() const
123125
{
124-
const float len = Length();
126+
const Type len = Length();
125127
return len > 0.f ? *this / len : *this;
126128
}
127129
#else
128-
[[nodiscard]] float Length() const
130+
[[nodiscard]] Type Length() const
129131
{
130132
return std::hypot(x, y);
131133
}
132134

133135
[[nodiscard]] Vector2 Normalized() const
134136
{
135-
const float len = Length();
137+
const Type len = Length();
136138
return len > 0.f ? *this / len : *this;
137139
}
138140
#endif
139-
[[nodiscard]] constexpr float LengthSqr() const
141+
[[nodiscard]] constexpr Type LengthSqr() const
140142
{
141143
return x * x + y * y;
142144
}
@@ -187,13 +189,13 @@ namespace omath
187189
}
188190

189191
// Sum of elements
190-
[[nodiscard]] constexpr float Sum() const
192+
[[nodiscard]] constexpr Type Sum() const
191193
{
192194
return x + y;
193195
}
194196

195197
[[nodiscard]]
196-
constexpr std::tuple<float, float> AsTuple() const
198+
constexpr std::tuple<Type, Type> AsTuple() const
197199
{
198200
return std::make_tuple(x, y);
199201
}

0 commit comments

Comments
 (0)