Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions VectorMatrixLib/Constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// MIT License
// Copyright(c) 2021 - 2026 Olivier BARRY
//
// This file is part of the C++ Aventura Project
//
// VectorMatrix Math Library
//

#ifndef CONSTANTS_H
#define CONSTANTS_H

namespace vectormatrix
{
// Sizes of the vectors and matrices
constexpr int SIZE_2 = 2;
constexpr int SIZE_3 = 3;
constexpr int SIZE_4 = 4;

// Indices of the axes
constexpr int X_AXIS = 0;
constexpr int Y_AXIS = 1;
constexpr int Z_AXIS = 2;

// Tolerance used to compare floats (same value as Aventura Constants.EPSILON)
constexpr float EPSILON = 1.0E-4f;
}

#endif // CONSTANTS_H
54 changes: 54 additions & 0 deletions VectorMatrixLib/Exceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// MIT License
// Copyright(c) 2021 - 2026 Olivier BARRY
//
// This file is part of the C++ Aventura Project
//
// VectorMatrix Math Library
//

#ifndef EXCEPTIONS_H
#define EXCEPTIONS_H

#include <stdexcept>
#include <string>

namespace vectormatrix
{
// Base class of all the exceptions of the library (same hierarchy as Aventura)
class Vector3DException : public std::runtime_error
{
public:
explicit Vector3DException(const std::string& message) : std::runtime_error(message) {}
};

// An index of a vector or matrix element is out of bound
class IndexOutOfBoundException : public Vector3DException
{
public:
explicit IndexOutOfBoundException(const std::string& message) : Vector3DException(message) {}
};

// An array used to set a vector has a wrong size
class VectorArrayWrongSizeException : public Vector3DException
{
public:
explicit VectorArrayWrongSizeException(const std::string& message) : Vector3DException(message) {}
};

// An array used to set a matrix has a wrong size
class MatrixArrayWrongSizeException : public Vector3DException
{
public:
explicit MatrixArrayWrongSizeException(const std::string& message) : Vector3DException(message) {}
};

// A matrix is singular and cannot be inverted
class NotInvertibleMatrixException : public Vector3DException
{
public:
explicit NotInvertibleMatrixException(const std::string& message = "Matrix is not invertible") : Vector3DException(message) {}
};
}

#endif // EXCEPTIONS_H
29 changes: 29 additions & 0 deletions VectorMatrixLib/MathTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// MIT License
// Copyright(c) 2021 - 2026 Olivier BARRY
//
// This file is part of the C++ Aventura Project
//
// VectorMatrix Math Library
//

#ifndef MATHTOOLS_H
#define MATHTOOLS_H

#include <cmath>
#include "Constants.h"

namespace vectormatrix
{
class MathTools
{
public:
// true if a and b are equal within EPSILON tolerance
static bool equals(float a, float b)
{
return std::fabs(a - b) <= EPSILON;
}
};
}

#endif // MATHTOOLS_H
14 changes: 14 additions & 0 deletions VectorMatrixLib/Matrix3f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include "Matrix3f.h"
#include "Matrix4f.h"
#include "MathTools.h"

namespace vectormatrix
{
Expand Down Expand Up @@ -114,6 +115,19 @@ namespace vectormatrix
return v * *this;
}

bool Matrix3f::operator==(const Matrix3f& m) const
{
for (int i = 0; i < SIZE3; i++)
for (int j = 0; j < SIZE3; j++)
if (!MathTools::equals(array_[i][j], m.array_[i][j])) return false;
return true;
}

bool Matrix3f::operator!=(const Matrix3f& m) const
{
return !(*this == m);
}

float Matrix3f::get(int x, int y) const
{
return array_[x][y];
Expand Down
3 changes: 3 additions & 0 deletions VectorMatrixLib/Matrix3f.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace vectormatrix
Matrix3f& operator*=(float a);
// W = A.V
Vector3f operator*(const Vector3f& v) const;
// Equality within EPSILON tolerance
bool operator==(const Matrix3f& m) const;
bool operator!=(const Matrix3f& m) const;

// Getters and Setters
float get(int x, int y) const;
Expand Down
14 changes: 14 additions & 0 deletions VectorMatrixLib/Matrix4f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include "Matrix3f.h"
#include "Matrix4f.h"
#include "MathTools.h"

namespace vectormatrix
{
Expand Down Expand Up @@ -114,6 +115,19 @@ namespace vectormatrix
return v * *this;
}

bool Matrix4f::operator==(const Matrix4f& m) const
{
for (int i = 0; i < SIZE4; i++)
for (int j = 0; j < SIZE4; j++)
if (!MathTools::equals(array_[i][j], m.array_[i][j])) return false;
return true;
}

bool Matrix4f::operator!=(const Matrix4f& m) const
{
return !(*this == m);
}

float Matrix4f::get(int x, int y) const
{
return array_[x][y];
Expand Down
3 changes: 3 additions & 0 deletions VectorMatrixLib/Matrix4f.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ namespace vectormatrix
Matrix4f& operator*=(float a);
// W = A.V
Vector4f operator*(const Vector4f& v) const;
// Equality within EPSILON tolerance
bool operator==(const Matrix4f& m) const;
bool operator!=(const Matrix4f& m) const;

// Getters and Setters
float get(int x, int y) const;
Expand Down
17 changes: 15 additions & 2 deletions VectorMatrixLib/Vector3f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

#include <iostream>
#include <cmath>
#include <string>
#include "Vector3f.h"
#include "Exceptions.h"
#include "MathTools.h"
#include "Vector4f.h"
#include "Matrix3f.h"

Expand Down Expand Up @@ -108,6 +111,16 @@ namespace vectormatrix
return x * v.x + y * v.y + z * v.z;
}

bool Vector3f::operator==(const Vector3f& v) const
{
return MathTools::equals(x, v.x) && MathTools::equals(y, v.y) && MathTools::equals(z, v.z);
}

bool Vector3f::operator!=(const Vector3f& v) const
{
return !(*this == v);
}

float Vector3f::get(int i) const
{
switch (i) {
Expand All @@ -118,7 +131,7 @@ namespace vectormatrix
case 2:
return z;
default:
return NAN;
throw IndexOutOfBoundException("Index out of bound while getting coordinate (" + std::to_string(i) + ") of Vector3f");
}
}

Expand Down Expand Up @@ -150,7 +163,7 @@ namespace vectormatrix
z = a;
break;
default:
break;
throw IndexOutOfBoundException("Index out of bound while setting coordinate (" + std::to_string(i) + ") of Vector3f");
}
}

Expand Down
5 changes: 4 additions & 1 deletion VectorMatrixLib/Vector3f.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ namespace vectormatrix
Vector3f& operator/=(float a);
// dot operator cannot use * operator as same signature than operator*(const Vector3f& v) except return type but that is not sufficient
float dot(const Vector3f& v) const;
// Equality within EPSILON tolerance
bool operator==(const Vector3f& v) const;
bool operator!=(const Vector3f& v) const;

// getter and setter
// getter and setter (get and set throw IndexOutOfBoundException if i is out of bound)
float get(int i) const;
float getX() const;
float getY() const;
Expand Down
17 changes: 15 additions & 2 deletions VectorMatrixLib/Vector4f.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

#include <iostream>
#include <cmath>
#include <string>
#include "Vector4f.h"
#include "Exceptions.h"
#include "MathTools.h"
#include "Vector3f.h"
#include "Matrix4f.h"

Expand Down Expand Up @@ -117,6 +120,16 @@ namespace vectormatrix
return x * v.x + y * v.y + z * v.z + w * v.w;
}

bool Vector4f::operator==(const Vector4f& v) const
{
return MathTools::equals(x, v.x) && MathTools::equals(y, v.y) && MathTools::equals(z, v.z) && MathTools::equals(w, v.w);
}

bool Vector4f::operator!=(const Vector4f& v) const
{
return !(*this == v);
}

float Vector4f::get(int i) const
{
switch (i) {
Expand All @@ -129,7 +142,7 @@ namespace vectormatrix
case 3:
return w;
default:
return NAN;
throw IndexOutOfBoundException("Index out of bound while getting coordinate (" + std::to_string(i) + ") of Vector4f");
}
}

Expand Down Expand Up @@ -169,7 +182,7 @@ namespace vectormatrix
w = a;
break;
default:
break;
throw IndexOutOfBoundException("Index out of bound while setting coordinate (" + std::to_string(i) + ") of Vector4f");
}
}

Expand Down
5 changes: 4 additions & 1 deletion VectorMatrixLib/Vector4f.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ namespace vectormatrix
Vector4f& operator/=(float a);
// dot operator cannot use * operator as same signature than operator*(const Vector4f& v) except return type but that is not sufficient
float dot(const Vector4f& v) const;
// Equality within EPSILON tolerance
bool operator==(const Vector4f& v) const;
bool operator!=(const Vector4f& v) const;

// getter and setter
// getter and setter (get and set throw IndexOutOfBoundException if i is out of bound)
float get(int i) const;
float getX() const;
float getY() const;
Expand Down
3 changes: 3 additions & 0 deletions VectorMatrixLib/VectorMatrixLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@
<ClInclude Include="Matrix4f.h" />
<ClInclude Include="Vector3f.h" />
<ClInclude Include="Vector4f.h" />
<ClInclude Include="Constants.h" />
<ClInclude Include="Exceptions.h" />
<ClInclude Include="MathTools.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Matrix4f.cpp" />
Expand Down
9 changes: 9 additions & 0 deletions VectorMatrixLib/VectorMatrixLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
<ClInclude Include="Matrix4f.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Constants.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Exceptions.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MathTools.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Vector3f.cpp">
Expand Down
Loading
Loading