diff --git a/.cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx b/.cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx deleted file mode 100644 index 34b906c..0000000 Binary files a/.cache/clangd/index/Matrix.hpp.B9C1D36F0ACA1714.idx and /dev/null differ diff --git a/.cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx b/.cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx deleted file mode 100644 index f7707e0..0000000 Binary files a/.cache/clangd/index/matrix-tests.cpp.88D512D6040A12A3.idx and /dev/null differ diff --git a/.vscode/settings.json b/.vscode/settings.json index d269529..b25d6b6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -71,7 +71,10 @@ "typeinfo": "cpp", "variant": "cpp", "shared_mutex": "cpp", - "charconv": "cpp" + "charconv": "cpp", + "format": "cpp", + "csignal": "cpp", + "span": "cpp" }, "clangd.enable": false, "C_Cpp.dimInactiveRegions": false diff --git a/CMakeLists.txt b/CMakeLists.txt index 9337e92..437c0c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,21 +37,11 @@ set_target_properties(matrix # Vector3d add_library(vector-3d STATIC - Vector3D.hpp -) - -target_include_directories(vector-3d - PUBLIC - . + Vector3D.cpp ) target_link_libraries(vector-3d PUBLIC vector-3d-intf PRIVATE -) - -set_target_properties(vector-3d - PROPERTIES - LINKER_LANGUAGE CXX ) \ No newline at end of file diff --git a/Vector3D.cpp b/Vector3D.cpp index 9c5182c..350851a 100644 --- a/Vector3D.cpp +++ b/Vector3D.cpp @@ -1,73 +1,99 @@ -#ifdef MATRIX_H_ // since the .cpp file has to be included by the .hpp file this - // will evaluate to true -#include "Vector3D.hpp" +#ifdef VECTOR3D_H_ // since the .cpp file has to be included by the .hpp file this + // will evaluate to true +#include +#include template -constexpr V3D::V3D(const Matrix<1, 3> &other) : x(other[0][0]), y(other[0][1]), z(other[0][2]) {} +V3D::V3D(const Matrix<1, 3> &other) +{ + this->x = other.Get(0, 0); + this->y = other.Get(0, 1); + this->z = other.Get(0, 2); +} template -constexpr V3D::V3D(const Matrix<3, 1> &other) : x(other[0][0]), y(other[1][0]), z(other[2][0]) {} +V3D::V3D(const Matrix<3, 1> &other) +{ + this->x = other.Get(0, 0); + this->y = other.Get(1, 0); + this->z = other.Get(2, 0); +} template -constexpr V3D::V3D(const V3D &other) : x(other.x), - y(other.y), - z(other.z) +V3D::V3D(const V3D &other) : x(other.x), + y(other.y), + z(other.z) { static_assert(std::is_arithmetic::value, "Type must be a number"); } template -constexpr V3D::V3D(Type x = 0, Type y = 0, Type z = 0) : x(x), - y(y), - z(z) +V3D::V3D(Type x, Type y, Type z) : x(x), + y(y), + z(z) { static_assert(std::is_arithmetic::value, "Type must be a number"); } -template -constexpr V3D::V3D(const V3D other) : x(static_cast(other.x)), - y(static_cast(other.y)), - z(static_cast(other.z)) +template +template +V3D::V3D(const V3D &other) { static_assert(std::is_arithmetic::value, "Type must be a number"); static_assert(std::is_arithmetic::value, "OtherType must be a number"); + this->x = static_cast(other.x); + this->y = static_cast(other.y); + this->z = static_cast(other.z); } template -std::array V3D::ToArray() +std::array V3D::ToArray() { return {this->x, this->y, this->z}; } template -V3D &V3D::operator=(const V3D &other) +void V3D::operator=(const V3D &other) { this->x = other.x; this->y = other.y; this->z = other.z; - return *this; } template -V3D &V3D::operator+=(const V3D &other) +V3D V3D::operator+(const V3D &other) { - this->x += other.x; - this->y += other.y; - this->z += other.z; - return *this; + return V3D{this->x + other.x, this->y + other.y, this->z + other.z}; } template -V3D &V3D::operator-=(const V3D &other) +V3D V3D::operator-(const V3D &other) { - this->x -= other.x; - this->y -= other.y; - this->z -= other.z; + return V3D{this->x - other.x, this->y - other.y, this->z - other.z}; +} + +template +V3D V3D::operator*(Type scalar) +{ + return V3D{this->x * scalar, this->y * scalar, this->z * scalar}; +} + +template +V3D &V3D::operator+=(const V3D &other) +{ + *this = *this + other; return *this; } template -V3D &V3D::operator/=(const Type scalar) +V3D &V3D::operator-=(const V3D &other) +{ + *this = *this - other; + return *this; +} + +template +V3D &V3D::operator/=(Type scalar) { if (scalar == 0) { @@ -80,7 +106,7 @@ V3D &V3D::operator/=(const Type scalar) } template -V3D &V3D::operator*=(const Type scalar) +V3D &V3D::operator*=(Type scalar) { this->x *= scalar; this->y *= scalar; @@ -89,15 +115,15 @@ V3D &V3D::operator*=(const Type scalar) } template -bool V3D::operator==(const V3D &other) +bool V3D::operator==(const V3D &other) { return this->x == other.x && this->y == other.y && this->z == other.z; } template -float V3D::magnitude() +float V3D::magnitude() { return std::sqrt(static_cast(this->x * this->x + this->y * this->y + this->z * this->z)); } -#endif // MATRIX_H_ \ No newline at end of file +#endif // VECTOR3D_H_ \ No newline at end of file diff --git a/Vector3D.hpp b/Vector3D.hpp index c35ada0..818a565 100644 --- a/Vector3D.hpp +++ b/Vector3D.hpp @@ -2,43 +2,48 @@ #define VECTOR3D_H_ #include -#include -#include - #include "Matrix.hpp" template class V3D { public: - constexpr V3D(const Matrix<1, 3> &other); - constexpr V3D(const Matrix<3, 1> &other); + V3D(const Matrix<1, 3> &other); + V3D(const Matrix<3, 1> &other); - constexpr V3D(const V3D &other); + V3D(const V3D &other); - constexpr V3D(Type x = 0, Type y = 0, Type z = 0); + V3D(Type x = 0, Type y = 0, Type z = 0); template - constexpr V3D(const V3D other); + V3D(const V3D &other); std::array ToArray(); - V3D &operator=(const V3D &other); + V3D operator+(const V3D &other); - V3D &operator+=(const V3D &other); + V3D operator-(const V3D &other); - V3D &operator-=(const V3D &other); + V3D operator*(Type scalar); - V3D &operator/=(const Type scalar); + void operator=(const V3D &other); - V3D &operator*=(const Type scalar); + V3D &operator+=(const V3D &other); - bool operator==(const V3D &other); + V3D &operator-=(const V3D &other); + + V3D &operator/=(Type scalar); + + V3D &operator*=(Type scalar); + + bool operator==(const V3D &other); float magnitude(); + Type x; Type y; Type z; }; +#include "Vector3D.cpp" #endif // VECTOR3D_H_ \ No newline at end of file diff --git a/unit-tests/CMakeLists.txt b/unit-tests/CMakeLists.txt index fd8684e..b464674 100644 --- a/unit-tests/CMakeLists.txt +++ b/unit-tests/CMakeLists.txt @@ -12,10 +12,20 @@ FetchContent_Declare( FetchContent_MakeAvailable(Catch2) +# matrix tests add_executable(matrix-tests matrix-tests.cpp) target_link_libraries(matrix-tests PRIVATE vector-3d-intf Catch2::Catch2WithMain +) + +# vector tests +add_executable(vector-tests vector-tests.cpp) + +target_link_libraries(vector-tests + PRIVATE + vector-3d-intf + Catch2::Catch2WithMain ) \ No newline at end of file diff --git a/unit-tests/vector-tests.cpp b/unit-tests/vector-tests.cpp new file mode 100644 index 0000000..60c18b8 --- /dev/null +++ b/unit-tests/vector-tests.cpp @@ -0,0 +1,45 @@ +// include the unit test framework first +#include +#include + +// include the module you're going to test next +#include "Vector3D.hpp" +#include "Matrix.hpp" + +// any other libraries +#include +#include +#include + +TEST_CASE("Vector Math", "Vector") +{ + V3D v1{1, 2, 3}; + V3D v2{4, 5, 6}; + V3D v3{}; + + SECTION("Initialization") + { + // list initialization + REQUIRE(v1.x == 1); + REQUIRE(v1.y == 2); + REQUIRE(v1.z == 3); + + // copy initialization + V3D v4{v2}; + REQUIRE(v4.x == 4); + REQUIRE(v4.y == 5); + REQUIRE(v4.z == 6); + + // empty initialization + REQUIRE(v3.x == 0); + REQUIRE(v3.y == 0); + REQUIRE(v3.z == 0); + + // matrix initialization + Matrix<1, 3> mat1{v1.ToArray()}; + V3D v5{mat1}; + REQUIRE(v5.x == v1.x); + REQUIRE(v5.y == v1.y); + REQUIRE(v5.z == v1.z); + } +} \ No newline at end of file