From bc36b1799ef0af3ca764f2a1c84959164e97d65b Mon Sep 17 00:00:00 2001 From: Ycidia Date: Sun, 17 Aug 2025 20:05:35 +0100 Subject: [PATCH 01/15] Protection --- .vscode/c_cpp_properties.json | 4 +- CMakeLists.txt | 6 ++ source/cpp_utils.hpp | 2 + source/line.cpp | 38 ++++++++++++ source/line.hpp | 26 +++++++++ source/matrix.cpp | 38 ++++++------ source/matrix.hpp | 48 ++++++++-------- source/plane.cpp | 60 +++++++++++++++++++ source/plane.hpp | 29 ++++++++++ source/quaternion.cpp | 12 ++-- source/quaternion.hpp | 73 ++++++++++++----------- source/vector.cpp | 94 +++++++++++++++--------------- source/vector.hpp | 52 +++++++++-------- test/line_unittest.cpp | 73 +++++++++++++++++++++++ test/matrix_unittest.cpp | 1 - test/plane_unittest.cpp | 105 ++++++++++++++++++++++++++++++++++ test/vector_unittest.cpp | 1 - 17 files changed, 499 insertions(+), 163 deletions(-) create mode 100644 source/line.cpp create mode 100644 source/line.hpp create mode 100644 source/plane.cpp create mode 100644 source/plane.hpp create mode 100644 test/line_unittest.cpp create mode 100644 test/plane_unittest.cpp diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 47a0d5d..4588867 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -11,10 +11,12 @@ "_UNICODE" ], "compilerPath": "C:/msys64/mingw64/bin/g++.exe", + // Cannot find: C:/msys64/mingw64/bin/g++.exe + // Full path of the compiler being used, e.g. /usr/bin/gcc, to enable more accurate IntelliSense. "cStandard": "c17", "cppStandard": "gnu++17", "intelliSenseMode": "windows-gcc-x64" } ], "version": 4 -} \ No newline at end of file +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d9c0b2..3c0c947 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,12 @@ add_executable( test/quaternion_unittest.cpp source/quaternion.hpp source/quaternion.cpp + test/line_unittest.cpp + source/line.hpp + source/line.cpp + test/plane_unittest.cpp + source/plane.hpp + source/plane.cpp ) target_link_libraries( diff --git a/source/cpp_utils.hpp b/source/cpp_utils.hpp index ab6ee46..f93694f 100644 --- a/source/cpp_utils.hpp +++ b/source/cpp_utils.hpp @@ -1,3 +1,5 @@ #include "vector.hpp" #include "quaternion.hpp" #include "matrix.hpp" +#include "line.hpp" +#include "plane.hpp" diff --git a/source/line.cpp b/source/line.cpp new file mode 100644 index 0000000..f86b864 --- /dev/null +++ b/source/line.cpp @@ -0,0 +1,38 @@ +#include "line.hpp" + +namespace cpp_utils { + + Line::Line(Vector setPoint, Vector setDirection) : point(setPoint) { + double magnitude = setDirection.mod(); + Vector normDir = (1 / magnitude) * setDirection; + if(normDir.x < 0) { + direction = -normDir; + } else { + direction = normDir; + } + }; + + bool Line::operator==(const Line &that) const { + if(direction == that.direction && this->isOnLine(that.point).first) { + return true; + } else { + return false; + } + }; + + std::pair Line::isOnLine(const Vector &that) const { + Vector vecDiff = that - point; + double scalarX, scalarY, scalarZ; + + if(point.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / point.x;} + if(point.y == 0) {scalarY = vecDiff.y;} else {scalarY = vecDiff.y / point.y;} + if(point.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / point.z;} + + if(scalarX == scalarY && scalarX == scalarZ) { + return std::pair (true, scalarX); + } else { + return std::pair (false, 0); + } + }; + +} diff --git a/source/line.hpp b/source/line.hpp new file mode 100644 index 0000000..d316b26 --- /dev/null +++ b/source/line.hpp @@ -0,0 +1,26 @@ +#ifndef LINE_HPP +#define LINE_HPP + +#include "vector.hpp" + +const double norm = (1 / sqrt(3)); + +namespace cpp_utils { + + class Line { + public: + Vector point, direction; + inline Line() : point(0, 0, 0), direction(norm, norm, norm) {}; + Line(Vector setPoint, Vector setDirection); + + bool operator==(const Line &that) const; + inline bool operator!=(const Line &that) const { + return true != (*this == that); + }; + + std::pair isOnLine(const Vector &that) const; + }; + +} + +#endif diff --git a/source/matrix.cpp b/source/matrix.cpp index ce7fd0d..6eace9e 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -1,5 +1,3 @@ -#include -#include #include "matrix.hpp" namespace cpp_utils { @@ -16,9 +14,9 @@ namespace cpp_utils { double Matrix::det() const { return xx*(yy*zz-yz*zy)-xy*(yx*zz-yz*zx)+xz*(yx*zy-yy*zx); - } + }; - bool Matrix::operator==(const Matrix & that) const { + bool Matrix::operator==(const Matrix &that) const { double test_precision = std::fmax(precision, that.precision); if ((xx < (that.xx - test_precision)) || (xx > (that.xx + test_precision))) { return false; @@ -48,37 +46,37 @@ namespace cpp_utils { return false; } return true; - } + }; - Matrix Matrix::operator+(const Matrix & that) const { + Matrix Matrix::operator+(const Matrix &that) const { return Matrix(this->xx + that.xx, this->xy + that.xy, this->xz + that.xz, this->yx + that.yx, this->yy + that.yy, this->yz + that.yz, this->zx + that.zx, this->zy + that.zy, this->zz + that.zz); - } + }; - Matrix Matrix::operator-(const Matrix & that) const { + Matrix Matrix::operator-(const Matrix &that) const { return Matrix(this->xx - that.xx, this->xy - that.xy, this->xz - that.xz, this->yx - that.yx, this->yy - that.yy, this->yz - that.yz, this->zx - that.zx, this->zy - that.zy, this->zz - that.zz); - } + }; - Matrix Matrix::operator*(const double & that) const { + Matrix Matrix::operator*(const double &that) const { return Matrix(xx * that, xy * that, xz * that, yx * that, yy * that, yz * that, zx * that, zy * that, zz * that); - } + }; - Matrix operator*(double that, const Matrix & those) { + Matrix operator*(const double that, const Matrix &those) { return those * that; - } + }; - Matrix Matrix::operator*(const Matrix & that) const { + Matrix Matrix::operator*(const Matrix &that) const { return Matrix((xx * that.xx) + (xy * that.yx) + (xz * that.zx), (xx * that.xy) + (xy * that.yy) + (xz * that.zy), (xx * that.xz) + (xy * that.yz) + (xz * that.zz), (yx * that.xx) + (yy * that.yx) + (yz * that.zx), (yx * that.xy) + (yy * that.yy) + (yz * that.zy), (yx * that.xz) + (yy * that.yz) + (yz * that.zz), (zx * that.xx) + (zy * that.yx) + (zz * that.zx), (zx * that.xy) + (zy * that.yy) + (zz * that.zy), (zx * that.xz) + (zy * that.yz) + (zz * that.zz)); - } + }; - Vector Matrix::operator*(const Vector & that) const { + Vector Matrix::operator*(const Vector &that) const { return Vector((xx * that.x) + (xy * that.y) + (xz * that.z), (yx * that.x) + (yy * that.y) + (yz * that.z), (zx * that.x) + (zy * that.y) + (zz * that.z)); - } + }; - Vector operator*(Vector that, const Matrix & those) { + Vector operator*(const Vector that, const Matrix &those) { return Vector((that.x * those.xx) + (that.y * those.yx) + (that.z * those.zx), (that.x * those.xy) + (that.y * those.yy) + (that.z * those.zy), (that.x * those.xz) + (that.y * those.yz) + (that.z * those.zz)); - } + }; Matrix Matrix::inverse() const { if(this->det() == 0) { @@ -87,6 +85,6 @@ namespace cpp_utils { return (1/det()) * Matrix( ((yy * zz) - (zy * yz)), -((xy * zz) - (zy * xz)), ((xy * yz) - (yy * xz)), -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), ((yx * zy) - (zx * yy)), -((xx * zy) - (zx * xy)), ((xx * yy) - (yx * xy))); - } + }; } diff --git a/source/matrix.hpp b/source/matrix.hpp index eafb6b5..bb4cab7 100644 --- a/source/matrix.hpp +++ b/source/matrix.hpp @@ -6,32 +6,32 @@ namespace cpp_utils { -class Matrix{ - public: - double xx, xy, xz, yx, yy, yz, zx, zy, zz; - inline Matrix() : xx(1), xy(0), xz(0), yx(0), yy(1), yz(0), zx(0), zy(0), zz(1) {}; - Matrix(double xx, double xy, double xz, double yx, double yy, double yz, double zx, double zy, double zz); - Matrix(double xx, double yy, double zz); - double det() const; + class Matrix { + public: + double xx, xy, xz, yx, yy, yz, zx, zy, zz; + inline Matrix() : xx(1), xy(0), xz(0), yx(0), yy(1), yz(0), zx(0), zy(0), zz(1) {}; + Matrix(double xx, double xy, double xz, double yx, double yy, double yz, double zx, double zy, double zz); + Matrix(double xx, double yy, double zz); + double det() const; - bool operator==(const Matrix & that) const; - inline bool operator!=(const Matrix & that) const { - return true != (*this == that); - }; - Matrix operator+(const Matrix & that) const; - Matrix operator-(const Matrix & that) const; - Matrix operator*(const double & that) const; - inline Matrix operator/(const double & that) const {return *this * (1/that);}; - inline Matrix operator-() {return *this * -1;}; - Matrix operator*(const Matrix & that) const; - Vector operator*(const Vector & that) const; - Matrix inverse() const; - private: - double precision = 0.0001; -}; + bool operator==(const Matrix &that) const; + inline bool operator!=(const Matrix &that) const { + return true != (*this == that); + }; + Matrix operator+(const Matrix &that) const; + Matrix operator-(const Matrix &that) const; + Matrix operator*(const double &that) const; + inline Matrix operator/(const double & that) const {return *this * (1/that);}; + inline Matrix operator-() {return *this * -1;}; + Matrix operator*(const Matrix &that) const; + Vector operator*(const Vector &that) const; + Matrix inverse() const; + private: + double precision = 0.0001; + }; -Vector operator*(Vector that, const Matrix & those); -Matrix operator*(double that, const Matrix & those); + Vector operator*(const Vector that, const Matrix &those); + Matrix operator*(const double that, const Matrix &those); } diff --git a/source/plane.cpp b/source/plane.cpp new file mode 100644 index 0000000..156785b --- /dev/null +++ b/source/plane.cpp @@ -0,0 +1,60 @@ +#include "plane.hpp" + +namespace cpp_utils { + + Plane::Plane(Vector setPoint, Vector setNormal) : point(setPoint) { + double magnitude = setNormal.mod(); + Vector normNor = (1 / magnitude) * setNormal; + if(normNor.x < 0) { + normal = -normNor; + } else { + normal = normNor; + } + }; + + bool Plane::operator==(const Plane &that) const { + if(normal == that.normal && this->isOnPlane(that.point)) { + return true; + } else { + return false; + } + }; + + bool Plane::isOnPlane(const Vector &that) const { + Vector vecDiff = that - point; + if(vecDiff * normal == 0) { + return true; + } else { + return false; + } + }; + + Vector Plane::intersection(const Line &that) const { + double cosAngle = that.direction * normal; + Vector vecDiff = that.point - point; + double dotProd = vecDiff * normal; + if(cosAngle == 0) { + throw std::overflow_error("Line lies in plane or it parallel to plane."); + } else if(dotProd == 0) { + return that.point; + } else { + double scalar = -dotProd / cosAngle; + return that.point + (scalar * that.direction); + } + }; + + bool between(const Line line, const Plane &plane1, const Plane &plane2) { + if(plane1.normal != plane2.normal || line.direction * plane1.normal != 0) { + return false; + } else { + double abovePlane1 = (line.point - plane1.point) * plane1.normal; + double abovePlane2 = (line.point - plane2.point) * plane2.normal; + if(abovePlane1 * abovePlane2 < 0) { + return true; + } else { + return false; + } + } + }; + +} diff --git a/source/plane.hpp b/source/plane.hpp new file mode 100644 index 0000000..190a4fe --- /dev/null +++ b/source/plane.hpp @@ -0,0 +1,29 @@ +#ifndef PLANE_HPP +#define PLANE_HPP + +#include +#include "line.hpp" + +namespace cpp_utils { + + class Plane { + public: + Vector point, normal; + inline Plane() : point(0, 0, 0), normal(norm, norm, norm) {}; + Plane(Vector setPoint, Vector setNormal); + + bool operator==(const Plane &that) const; + inline bool operator!=(const Plane &that) const { + return true != (*this == that); + }; + + bool isOnPlane(const Vector &that) const; + + Vector intersection(const Line &that) const; + }; + + bool between(const Line line, const Plane &plane1, const Plane &plane2); + +} + +#endif diff --git a/source/quaternion.cpp b/source/quaternion.cpp index 01945ea..4aceed2 100644 --- a/source/quaternion.cpp +++ b/source/quaternion.cpp @@ -1,6 +1,4 @@ #include "quaternion.hpp" -#include -#include namespace cpp_utils { @@ -9,7 +7,7 @@ namespace cpp_utils { x(axis.x / axis.mod() * sin(angle / 2)), y(axis.y / axis.mod() * sin(angle / 2)), z(axis.z / axis.mod() * sin(angle / 2)) - {}; + {}; double Quaternion::mod() const { return sqrt((w * w) + (x * x) + (y * y) + (z * z)); @@ -17,9 +15,9 @@ namespace cpp_utils { Quaternion Quaternion::conj() const { return {w, -x, -y, -z}; - } + }; - Quaternion Quaternion::operator*(const double & that) const { + Quaternion Quaternion::operator*(const double &that) const { return Quaternion(w * that, x * that, y * that, z * that); }; @@ -43,7 +41,7 @@ namespace cpp_utils { ); }; - Quaternion Quaternion::operator*(const Vector & vec) const { + Quaternion Quaternion::operator*(const Vector &vec) const { return *this * Quaternion(0.0, vec.x, vec.y, vec.z); }; @@ -51,4 +49,4 @@ namespace cpp_utils { return Quaternion(0.0, vec.x, vec.y, vec.z) * quat; }; -} \ No newline at end of file +} diff --git a/source/quaternion.hpp b/source/quaternion.hpp index 4bed08f..bb9da50 100644 --- a/source/quaternion.hpp +++ b/source/quaternion.hpp @@ -7,43 +7,42 @@ namespace cpp_utils { -class Quaternion { - public: - double w, x, y, z; - inline Quaternion() : w(0), x(0), y(0), z(0) {}; - inline Quaternion(double w, double x, double y, double z): - w(w), x(x), y(y), z(z) {}; - Quaternion(double angle, Vector axis); - double mod() const; - Quaternion conj() const; - - bool operator==(const Quaternion & that) const; - inline bool operator!=(const Quaternion & that) const { - return true != (*this == that); - }; - Quaternion operator*(const double & that) const; - inline Quaternion operator/(const double & that) const { - return *this * (1/that); - }; - inline Quaternion operator-() {return *this * -1;}; - Quaternion operator*(const Quaternion & that) const; - Quaternion operator*(const Vector & vec) const; - private: - double relPrecision = 0.0001; - -}; - -Quaternion operator*(const Vector vec, const Quaternion quat); - -inline Vector rotate(Vector vec, Quaternion quat) { - Quaternion result = quat.conj() * vec * quat / quat.mod(); - return {result.x, result.y, result.z}; -}; - -inline std::ostream &operator<<(std::ostream &os, Quaternion const &quat) { - return os << "Quat(" << quat.w << ", " << quat.x << ", " << quat.y << ", "<< quat.z << ")\n"; -}; + class Quaternion { + public: + double w, x, y, z; + inline Quaternion() : w(0), x(0), y(0), z(0) {}; + inline Quaternion(double w, double x, double y, double z): + w(w), x(x), y(y), z(z) {}; + Quaternion(double angle, Vector axis); + double mod() const; + Quaternion conj() const; + + bool operator==(const Quaternion &that) const; + inline bool operator!=(const Quaternion &that) const { + return true != (*this == that); + }; + Quaternion operator*(const double &that) const; + inline Quaternion operator/(const double &that) const { + return *this * (1/that); + }; + inline Quaternion operator-() {return *this * -1;}; + Quaternion operator*(const Quaternion &that) const; + Quaternion operator*(const Vector &vec) const; + private: + double relPrecision = 0.0001; + }; + + Quaternion operator*(const Vector vec, const Quaternion quat); + + inline Vector rotate(Vector vec, Quaternion quat) { + Quaternion result = quat.conj() * vec * quat / quat.mod(); + return {result.x, result.y, result.z}; + }; + + inline std::ostream &operator<<(std::ostream &os, Quaternion const &quat) { + return os << "Quat(" << quat.w << ", " << quat.x << ", " << quat.y << ", "<< quat.z << ")\n"; + }; } -#endif \ No newline at end of file +#endif diff --git a/source/vector.cpp b/source/vector.cpp index 0939796..9f85817 100644 --- a/source/vector.cpp +++ b/source/vector.cpp @@ -1,53 +1,53 @@ - -#include -#include - #include "vector.hpp" namespace cpp_utils { -Vector::Vector(double x, double y, double z) : x(x), y(y), z(z) {}; - -double Vector::mod() const { - return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); -} - -bool Vector::operator==(const Vector & that) const { - double test_precision = std::fmax(precision, that.precision); - if ((x < (that.x - test_precision)) || (x > (that.x + test_precision))) { - return false; - } - if ((y < (that.y - test_precision)) || (y > (that.y + test_precision))) { - return false; - } - if ((z < (that.z - test_precision)) || (z > (that.z + test_precision))) { - return false; - } - return true; -} - -Vector Vector::operator+(const Vector & that) const { - return Vector(this->x + that.x, this->y + that.y, this->z + that.z); -} + Vector::Vector(double x, double y, double z) : x(x), y(y), z(z) {}; + + double Vector::mod() const { + return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); + }; + + bool Vector::operator==(const Vector &that) const { + double test_precision = std::fmax(precision, that.precision); + if ((x < (that.x - test_precision)) || (x > (that.x + test_precision))) { + return false; + } + if ((y < (that.y - test_precision)) || (y > (that.y + test_precision))) { + return false; + } + if ((z < (that.z - test_precision)) || (z > (that.z + test_precision))) { + return false; + } + return true; + }; + + Vector Vector::operator+(const Vector &that) const { + return Vector(this->x + that.x, this->y + that.y, this->z + that.z); + }; + + Vector Vector::operator-(const Vector &that) const { + return Vector(this->x - that.x, this->y - that.y, this->z - that.z); + }; + + Vector Vector::operator*(const double &that) const { + return Vector(x * that, y * that, z * that); + }; + + Vector operator*(const double that, const Vector &those) { + return those * that; + }; + + double Vector::operator*(const Vector &that) const { + return (x * that.x) + (y * that.y) + (z * that.z); + }; + + Vector Vector::operator^(const Vector &that) const { + return Vector( + (y * that.z) - (z * that.y), + (z * that.x) - (x * that.z), + (x * that.y) - (y * that.x) + ); + }; -Vector Vector::operator-(const Vector & that) const { - return Vector(this->x - that.x, this->y - that.y, this->z - that.z); } - -Vector Vector::operator*(const double & that) const { - return Vector(x * that, y * that, z * that); -} - -double Vector::operator*(const Vector & that) const { - return (x * that.x) + (y * that.y) + (z * that.z); -} - -Vector Vector::operator^(const Vector & that) const { - return Vector( - (y * that.z) - (z * that.y), - (z * that.x) - (x * that.z), - (x * that.y) - (y * that.x) - ); -} - -} \ No newline at end of file diff --git a/source/vector.hpp b/source/vector.hpp index e67e5a6..1ec8591 100644 --- a/source/vector.hpp +++ b/source/vector.hpp @@ -5,31 +5,33 @@ namespace cpp_utils { -class Vector{ - public: - double x, y, z; - inline Vector() : x(0), y(0), z(0) {}; - Vector(double x, double y, double z); - double mod() const; - - bool operator==(const Vector & that) const; - inline bool operator!=(const Vector & that) const { - return true != (*this == that); - }; - Vector operator+(const Vector & that) const; - Vector operator-(const Vector & that) const; - Vector operator*(const double & that) const; - inline Vector operator/(const double & that) const {return *this * (1/that);}; - inline Vector operator-() {return *this * -1;}; - double operator*(const Vector & that) const; - Vector operator^(const Vector & that) const; - protected: - double precision = 0.0001; -}; - -inline std::ostream &operator<<(std::ostream &os, Vector const &vec) { - return os << "Vec(" << vec.x << ", " << vec.y << ", "<< vec.z << ")\n"; -}; + class Vector { + public: + double x, y, z; + inline Vector() : x(0), y(0), z(0) {}; + Vector(double x, double y, double z); + double mod() const; + + bool operator==(const Vector &that) const; + inline bool operator!=(const Vector &that) const { + return true != (*this == that); + }; + Vector operator+(const Vector &that) const; + Vector operator-(const Vector &that) const; + Vector operator*(const double &that) const; + inline Vector operator/(const double &that) const {return *this * (1/that);}; + inline Vector operator-() {return *this * -1;}; + double operator*(const Vector &that) const; + Vector operator^(const Vector &that) const; + protected: + double precision = 0.0001; + }; + + Vector operator*(const double that, const Vector &those); + + inline std::ostream &operator<<(std::ostream &os, Vector const &vec) { + return os << "Vec(" << vec.x << ", " << vec.y << ", "<< vec.z << ")\n"; + }; } diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp new file mode 100644 index 0000000..08396f9 --- /dev/null +++ b/test/line_unittest.cpp @@ -0,0 +1,73 @@ +#include "gtest/gtest.h" +#include "../source/line.hpp" + +TEST(LineTest, DefaultInitTest) { + cpp_utils::Vector point(0, 0, 0); + cpp_utils::Vector direction(norm, norm, norm); + cpp_utils::Line line; + EXPECT_TRUE(line.point == point); + EXPECT_TRUE(line.direction == direction); +} + +TEST(LineTest, InitTest) { + cpp_utils::Vector point(1, 2, 3); + cpp_utils::Vector direction(4, 5, 6); + cpp_utils::Line line(point, direction); + EXPECT_TRUE(line.point == point); + EXPECT_TRUE(line.direction == (1 / direction.mod()) * direction); +} + +TEST(LineTest, EqualityTest) { + cpp_utils::Vector vec0; + cpp_utils::Vector vec1(-1, -1, -1); + cpp_utils::Vector vec2(1, 0, 1); + cpp_utils::Line line0; + cpp_utils::Line line1(vec1, vec1); + cpp_utils::Line line2(vec0, vec2); + cpp_utils::Line line3(vec2, vec1); + + EXPECT_TRUE(line0 == line0); + EXPECT_TRUE(line0 == line1); + EXPECT_FALSE(line0 == line2); + EXPECT_FALSE(line1 == line2); + EXPECT_FALSE(line0 == line3); + EXPECT_FALSE(line1 == line3); + EXPECT_FALSE(line2 == line3); +} + +TEST(LineTest, NotEqualityTest) { + cpp_utils::Vector vec0; + cpp_utils::Vector vec1(-1, -1, -1); + cpp_utils::Vector vec2(1, 0, 1); + cpp_utils::Line line0; + cpp_utils::Line line1(vec1, vec1); + cpp_utils::Line line2(vec0, vec2); + cpp_utils::Line line3(vec2, vec1); + + EXPECT_FALSE(line0 != line0); + EXPECT_FALSE(line0 != line1); + EXPECT_TRUE(line0 != line2); + EXPECT_TRUE(line1 != line2); + EXPECT_TRUE(line0 != line3); + EXPECT_TRUE(line1 != line3); + EXPECT_TRUE(line2 != line3); +} + +TEST(LineTest, IsOnTest) { + cpp_utils::Vector point0(0, 1, 1); + cpp_utils::Vector point1(1, 1, 1); + cpp_utils::Vector point2(1, 2, 2); + cpp_utils::Line line(point0, point1); + + std::pair isOnLine0 = line.isOnLine(point0); + std::pair isOnLine1 = line.isOnLine(point1); + std::pair isOnLine2 = line.isOnLine(point2); + + EXPECT_TRUE(isOnLine0.first); + EXPECT_FALSE(isOnLine1.first); + EXPECT_TRUE(isOnLine2.first); + + EXPECT_EQ(isOnLine0.second, 0); + EXPECT_EQ(isOnLine1.second, 0); + EXPECT_EQ(isOnLine2.second, 1); +} diff --git a/test/matrix_unittest.cpp b/test/matrix_unittest.cpp index 22c6416..fe4d5b0 100644 --- a/test/matrix_unittest.cpp +++ b/test/matrix_unittest.cpp @@ -1,5 +1,4 @@ #include "gtest/gtest.h" -#include #include "../source/matrix.hpp" TEST(MatrixTest, DefaultInitTest) { diff --git a/test/plane_unittest.cpp b/test/plane_unittest.cpp new file mode 100644 index 0000000..264fc07 --- /dev/null +++ b/test/plane_unittest.cpp @@ -0,0 +1,105 @@ +#include "gtest/gtest.h" +#include "../source/plane.hpp" + +TEST(PlaneTest, DefaultInitTest) { + cpp_utils::Vector point(0, 0, 0); + cpp_utils::Vector normal(norm, norm, norm); + cpp_utils::Plane plane; + EXPECT_TRUE(plane.point == point); + EXPECT_TRUE(plane.normal == normal); +} + +TEST(PlaneTest, InitTest) { + cpp_utils::Vector point(1, 2, 3); + cpp_utils::Vector normal(4, 5, 6); + cpp_utils::Plane plane(point, normal); + EXPECT_TRUE(plane.point == point); + EXPECT_TRUE(plane.normal == (1 / normal.mod()) * normal); +} + +TEST(PlaneTest, EqualityTest) { + cpp_utils::Vector vec0; + cpp_utils::Vector vec1(-1, -1, -1); + cpp_utils::Vector vec2(1, 0, -1); + cpp_utils::Plane plane0; + cpp_utils::Plane plane1(vec1, vec1); + cpp_utils::Plane plane2(vec0, vec2); + cpp_utils::Plane plane3(vec2, vec1); + + EXPECT_TRUE(plane0 == plane0); + EXPECT_FALSE(plane0 == plane1); + EXPECT_FALSE(plane0 == plane2); + EXPECT_TRUE(plane0 == plane3); + EXPECT_FALSE(plane1 == plane2); + EXPECT_FALSE(plane2 == plane3); +} + +TEST(PlaneTest, NotEqualityTest) { + cpp_utils::Vector vec0; + cpp_utils::Vector vec1(-1, -1, -1); + cpp_utils::Vector vec2(1, 0, -1); + cpp_utils::Plane plane0; + cpp_utils::Plane plane1(vec1, vec1); + cpp_utils::Plane plane2(vec0, vec2); + cpp_utils::Plane plane3(vec2, vec1); + + EXPECT_FALSE(plane0 != plane0); + EXPECT_TRUE(plane0 != plane1); + EXPECT_TRUE(plane0 != plane2); + EXPECT_FALSE(plane0 != plane3); + EXPECT_TRUE(plane1 != plane2); + EXPECT_TRUE(plane2 != plane3); +} + +TEST(PlaneTest, IsOnTest) { + cpp_utils::Vector point0(0, 1, 1); + cpp_utils::Vector point1(1, 1, 1); + cpp_utils::Vector point2(1, 0, 1); + cpp_utils::Plane plane(point0, point1); + + EXPECT_TRUE(plane.isOnPlane(point0)); + EXPECT_FALSE(plane.isOnPlane(point1)); + EXPECT_TRUE(plane.isOnPlane(point2)); +} + +TEST(PlaneTest, IntersectionTest) { + cpp_utils::Vector point0; + cpp_utils::Vector point1(1, -1, 1); + cpp_utils::Vector point2(1, 0, -1); + cpp_utils::Line line1(point0, point1); + cpp_utils::Line line2(point1, point1); + cpp_utils::Line line3(point0, point2); + cpp_utils::Line line4(point1, point2); + cpp_utils::Plane plane; + + EXPECT_EQ(plane.intersection(line1), point0); + EXPECT_EQ(plane.intersection(line2), point0); + EXPECT_THROW(plane.intersection(line3), std::exception); + EXPECT_THROW(plane.intersection(line4), std::exception); +} + +TEST(PlaneTest, BetweenTest) { + cpp_utils::Vector point0; + cpp_utils::Vector point1(1, 1, 1); + cpp_utils::Vector point2(1, 0, -1); + cpp_utils::Vector point3(0.5, 0.5, 0.5); + cpp_utils::Vector point4(-0.5, -0.5, -0.5); + cpp_utils::Vector point5(1.5, 1.5, 1.5); + + cpp_utils::Line line0; + cpp_utils::Line line1(point2, point2); + cpp_utils::Line line2(point3, point2); + cpp_utils::Line line3(point4, point2); + cpp_utils::Line line4(point5, point2); + + cpp_utils::Plane plane0; + cpp_utils::Plane plane1(point1, point1); + cpp_utils::Plane plane2(point0, point2); + + EXPECT_FALSE(cpp_utils::between(line1, plane0, plane0)); + EXPECT_TRUE(cpp_utils::between(line2, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line3, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line4, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line0, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line0, plane0, plane2)); +} diff --git a/test/vector_unittest.cpp b/test/vector_unittest.cpp index d96cd6f..7b105d3 100644 --- a/test/vector_unittest.cpp +++ b/test/vector_unittest.cpp @@ -1,5 +1,4 @@ #include "gtest/gtest.h" -#include #include "../source/vector.hpp" TEST(VectorTest, DefaultInitTest) { From 2dde29738e2ef3362f29974e5f5f8f3630eded7c Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 19:06:51 +0100 Subject: [PATCH 02/15] First Corrections --- source/line.cpp | 51 ++++++++--- source/line.hpp | 11 ++- source/matrix.cpp | 15 ++-- source/matrix.hpp | 1 - source/plane.cpp | 21 +++-- source/plane.hpp | 3 +- source/vector.cpp | 4 + source/vector.hpp | 1 + test/line_unittest.cpp | 183 +++++++++++++++++++++++++++++++--------- test/plane_unittest.cpp | 118 ++++++++++++++++++++------ 10 files changed, 301 insertions(+), 107 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index f86b864..55c4918 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -2,36 +2,59 @@ namespace cpp_utils { - Line::Line(Vector setPoint, Vector setDirection) : point(setPoint) { - double magnitude = setDirection.mod(); - Vector normDir = (1 / magnitude) * setDirection; - if(normDir.x < 0) { + Line::Line(Vector pnt, Vector dir) : point(pnt) { + Vector normDir = dir.normalise(); + if ((normDir.x < 0) || (normDir.x == 0 && normDir.y < 0) || (normDir.x == 0 && normDir.y == 0 && normDir.z < 0)) { direction = -normDir; } else { direction = normDir; } }; - bool Line::operator==(const Line &that) const { - if(direction == that.direction && this->isOnLine(that.point).first) { + bool Line::operator==(const Line &that) { + bool on = this->isOnLine(that.point); + if (direction == that.direction && on) { return true; } else { return false; } }; - std::pair Line::isOnLine(const Vector &that) const { + bool Line::isOnLine(const Vector &that) { Vector vecDiff = that - point; double scalarX, scalarY, scalarZ; + if ((direction.x == 0 && vecDiff.x != 0) || + (direction.y == 0 && vecDiff.y != 0) || + (direction.z == 0 && vecDiff.z != 0)) { + return false; + } else { + if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;} + if (direction.y == 0) {scalarY = vecDiff.z;} else {scalarY = vecDiff.y / direction.y;} + if (direction.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / direction.z;} + if ((scalarX == scalarY && scalarX == scalarZ) || + (scalarY == 0 && scalarX == scalarZ && direction.y == 0) || + (scalarZ == 0 && scalarX == scalarY && direction.z == 0) || + (scalarY == 0 && scalarZ == 0 && direction.y == 0 && direction.z == 0)) { + scalar = scalarX; + return true; + } else if ((scalarX == 0 && scalarY == scalarZ && direction.x == 0) || + (scalarX == 0 && scalarZ == 0 && direction.x == 0 && direction.z == 0)) { + scalar = scalarY; + return true; + } else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) { + scalar = scalarZ; + return true; + } else { + return false; + } + }; + } - if(point.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / point.x;} - if(point.y == 0) {scalarY = vecDiff.y;} else {scalarY = vecDiff.y / point.y;} - if(point.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / point.z;} - - if(scalarX == scalarY && scalarX == scalarZ) { - return std::pair (true, scalarX); + double Line::whereOnLine(const Vector &that) { + if (this->isOnLine(that)) { + return scalar; } else { - return std::pair (false, 0); + throw std::overflow_error("Point not on line."); } }; diff --git a/source/line.hpp b/source/line.hpp index d316b26..4d88839 100644 --- a/source/line.hpp +++ b/source/line.hpp @@ -9,16 +9,19 @@ namespace cpp_utils { class Line { public: + double scalar; Vector point, direction; inline Line() : point(0, 0, 0), direction(norm, norm, norm) {}; - Line(Vector setPoint, Vector setDirection); + Line(Vector pnt, Vector dir); - bool operator==(const Line &that) const; - inline bool operator!=(const Line &that) const { + bool operator==(const Line &that); + inline bool operator!=(const Line &that) { return true != (*this == that); }; - std::pair isOnLine(const Vector &that) const; + bool isOnLine(const Vector &that); + + double whereOnLine(const Vector &that); }; } diff --git a/source/matrix.cpp b/source/matrix.cpp index 6eace9e..ba941d5 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -79,12 +79,13 @@ namespace cpp_utils { }; Matrix Matrix::inverse() const { - if(this->det() == 0) { + if (this->det() == 0) { throw std::overflow_error("No inverse due to zero value of determinant."); - } - return (1/det()) * Matrix( ((yy * zz) - (zy * yz)), -((xy * zz) - (zy * xz)), ((xy * yz) - (yy * xz)), - -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), - ((yx * zy) - (zx * yy)), -((xx * zy) - (zx * xy)), ((xx * yy) - (yx * xy))); - }; - + } else { + return (1/det()) * Matrix( ((yy * zz) - (zy * yz)), -((xy * zz) - (zy * xz)), ((xy * yz) - (yy * xz)), + -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), + ((yx * zy) - (zx * yy)), -((xx * zy) - (zx * xy)), ((xx * yy) - (yx * xy))); + }; + } + } diff --git a/source/matrix.hpp b/source/matrix.hpp index bb4cab7..d3a27d7 100644 --- a/source/matrix.hpp +++ b/source/matrix.hpp @@ -1,7 +1,6 @@ #ifndef MATRIX_HPP #define MATRIX_HPP -#include #include "vector.hpp" namespace cpp_utils { diff --git a/source/plane.cpp b/source/plane.cpp index 156785b..5993432 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -2,10 +2,9 @@ namespace cpp_utils { - Plane::Plane(Vector setPoint, Vector setNormal) : point(setPoint) { - double magnitude = setNormal.mod(); - Vector normNor = (1 / magnitude) * setNormal; - if(normNor.x < 0) { + Plane::Plane(Vector pnt, Vector nor) : point(pnt) { + Vector normNor = nor.normalise(); + if ((normNor.x < 0) || (normNor.x == 0 && normNor.y < 0) || (normNor.x == 0 && normNor.y == 0 && normNor.z < 0)) { normal = -normNor; } else { normal = normNor; @@ -13,7 +12,7 @@ namespace cpp_utils { }; bool Plane::operator==(const Plane &that) const { - if(normal == that.normal && this->isOnPlane(that.point)) { + if (normal == that.normal && this->isOnPlane(that.point)) { return true; } else { return false; @@ -22,7 +21,7 @@ namespace cpp_utils { bool Plane::isOnPlane(const Vector &that) const { Vector vecDiff = that - point; - if(vecDiff * normal == 0) { + if (vecDiff * normal == 0) { return true; } else { return false; @@ -33,9 +32,9 @@ namespace cpp_utils { double cosAngle = that.direction * normal; Vector vecDiff = that.point - point; double dotProd = vecDiff * normal; - if(cosAngle == 0) { - throw std::overflow_error("Line lies in plane or it parallel to plane."); - } else if(dotProd == 0) { + if (cosAngle == 0) { + throw std::overflow_error("Line lies in plane or is parallel to plane."); + } else if (dotProd == 0) { return that.point; } else { double scalar = -dotProd / cosAngle; @@ -44,12 +43,12 @@ namespace cpp_utils { }; bool between(const Line line, const Plane &plane1, const Plane &plane2) { - if(plane1.normal != plane2.normal || line.direction * plane1.normal != 0) { + if (plane1.normal != plane2.normal || line.direction * plane1.normal != 0) { return false; } else { double abovePlane1 = (line.point - plane1.point) * plane1.normal; double abovePlane2 = (line.point - plane2.point) * plane2.normal; - if(abovePlane1 * abovePlane2 < 0) { + if (abovePlane1 * abovePlane2 < 0) { return true; } else { return false; diff --git a/source/plane.hpp b/source/plane.hpp index 190a4fe..538fc90 100644 --- a/source/plane.hpp +++ b/source/plane.hpp @@ -1,7 +1,6 @@ #ifndef PLANE_HPP #define PLANE_HPP -#include #include "line.hpp" namespace cpp_utils { @@ -10,7 +9,7 @@ namespace cpp_utils { public: Vector point, normal; inline Plane() : point(0, 0, 0), normal(norm, norm, norm) {}; - Plane(Vector setPoint, Vector setNormal); + Plane(Vector pnt, Vector nor); bool operator==(const Plane &that) const; inline bool operator!=(const Plane &that) const { diff --git a/source/vector.cpp b/source/vector.cpp index 9f85817..b464c02 100644 --- a/source/vector.cpp +++ b/source/vector.cpp @@ -8,6 +8,10 @@ namespace cpp_utils { return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2)); }; + Vector Vector::normalise() const { + return (1 / this->mod()) * *this; + }; + bool Vector::operator==(const Vector &that) const { double test_precision = std::fmax(precision, that.precision); if ((x < (that.x - test_precision)) || (x > (that.x + test_precision))) { diff --git a/source/vector.hpp b/source/vector.hpp index 1ec8591..a0bbf18 100644 --- a/source/vector.hpp +++ b/source/vector.hpp @@ -11,6 +11,7 @@ namespace cpp_utils { inline Vector() : x(0), y(0), z(0) {}; Vector(double x, double y, double z); double mod() const; + Vector normalise() const; bool operator==(const Vector &that) const; inline bool operator!=(const Vector &that) const { diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index 08396f9..037e473 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -14,60 +14,161 @@ TEST(LineTest, InitTest) { cpp_utils::Vector direction(4, 5, 6); cpp_utils::Line line(point, direction); EXPECT_TRUE(line.point == point); - EXPECT_TRUE(line.direction == (1 / direction.mod()) * direction); + EXPECT_TRUE(line.direction == direction.normalise()); } TEST(LineTest, EqualityTest) { - cpp_utils::Vector vec0; - cpp_utils::Vector vec1(-1, -1, -1); - cpp_utils::Vector vec2(1, 0, 1); - cpp_utils::Line line0; - cpp_utils::Line line1(vec1, vec1); - cpp_utils::Line line2(vec0, vec2); - cpp_utils::Line line3(vec2, vec1); + cpp_utils::Vector vec00; + cpp_utils::Vector vec01(1, 0, 0); + cpp_utils::Vector vec02(0, 1, 0); + cpp_utils::Vector vec03(0, 0, 1); + cpp_utils::Vector vec04(-1, 0, 0); + cpp_utils::Vector vec05(0, -1, 0); + cpp_utils::Vector vec06(0, 0, -1); + cpp_utils::Vector vec07(1, 1, 1); + cpp_utils::Vector vec08(-1, 1, 1); + cpp_utils::Vector vec09(1, -1, 1); + cpp_utils::Vector vec10(1, 1, -1); + cpp_utils::Vector vec11(1, -1, -1); + cpp_utils::Vector vec12(-1, 1, -1); + cpp_utils::Vector vec13(-1, -1, 1); + cpp_utils::Vector vec14(-1, -1, -1); + cpp_utils::Line line00; + cpp_utils::Line line01(vec04, vec01); + cpp_utils::Line line02(vec05, vec02); + cpp_utils::Line line03(vec06, vec03); + cpp_utils::Line line04(vec01, vec04); + cpp_utils::Line line05(vec02, vec05); + cpp_utils::Line line06(vec03, vec06); + cpp_utils::Line line07(vec14, vec07); + cpp_utils::Line line08(vec11, vec08); + cpp_utils::Line line09(vec12, vec09); + cpp_utils::Line line10(vec13, vec10); + cpp_utils::Line line11(vec08, vec11); + cpp_utils::Line line12(vec09, vec12); + cpp_utils::Line line13(vec10, vec13); + cpp_utils::Line line14(vec07, vec14); - EXPECT_TRUE(line0 == line0); - EXPECT_TRUE(line0 == line1); - EXPECT_FALSE(line0 == line2); - EXPECT_FALSE(line1 == line2); - EXPECT_FALSE(line0 == line3); - EXPECT_FALSE(line1 == line3); - EXPECT_FALSE(line2 == line3); + EXPECT_TRUE(line00 == line00); + EXPECT_FALSE(line00 == line01); + EXPECT_TRUE(line01 == line04); + EXPECT_FALSE(line00 == line02); + EXPECT_TRUE(line02 == line05); + EXPECT_FALSE(line00 == line03); + EXPECT_TRUE(line03 == line06); + EXPECT_TRUE(line00 == line07); + EXPECT_TRUE(line07 == line14); + EXPECT_FALSE(line00 == line08); + EXPECT_TRUE(line08 == line11); + EXPECT_FALSE(line00 == line09); + EXPECT_TRUE(line09 == line12); + EXPECT_FALSE(line00 == line10); + EXPECT_TRUE(line10 == line13); } TEST(LineTest, NotEqualityTest) { - cpp_utils::Vector vec0; - cpp_utils::Vector vec1(-1, -1, -1); - cpp_utils::Vector vec2(1, 0, 1); - cpp_utils::Line line0; - cpp_utils::Line line1(vec1, vec1); - cpp_utils::Line line2(vec0, vec2); - cpp_utils::Line line3(vec2, vec1); + cpp_utils::Vector vec00; + cpp_utils::Vector vec01(1, 0, 0); + cpp_utils::Vector vec02(0, 1, 0); + cpp_utils::Vector vec03(0, 0, 1); + cpp_utils::Vector vec04(-1, 0, 0); + cpp_utils::Vector vec05(0, -1, 0); + cpp_utils::Vector vec06(0, 0, -1); + cpp_utils::Vector vec07(1, 1, 1); + cpp_utils::Vector vec08(-1, 1, 1); + cpp_utils::Vector vec09(1, -1, 1); + cpp_utils::Vector vec10(1, 1, -1); + cpp_utils::Vector vec11(1, -1, -1); + cpp_utils::Vector vec12(-1, 1, -1); + cpp_utils::Vector vec13(-1, -1, 1); + cpp_utils::Vector vec14(-1, -1, -1); + cpp_utils::Line line00; + cpp_utils::Line line01(vec04, vec01); + cpp_utils::Line line02(vec05, vec02); + cpp_utils::Line line03(vec06, vec03); + cpp_utils::Line line04(vec01, vec04); + cpp_utils::Line line05(vec02, vec05); + cpp_utils::Line line06(vec03, vec06); + cpp_utils::Line line07(vec14, vec07); + cpp_utils::Line line08(vec11, vec08); + cpp_utils::Line line09(vec12, vec09); + cpp_utils::Line line10(vec13, vec10); + cpp_utils::Line line11(vec08, vec11); + cpp_utils::Line line12(vec09, vec12); + cpp_utils::Line line13(vec10, vec13); + cpp_utils::Line line14(vec07, vec14); - EXPECT_FALSE(line0 != line0); - EXPECT_FALSE(line0 != line1); - EXPECT_TRUE(line0 != line2); - EXPECT_TRUE(line1 != line2); - EXPECT_TRUE(line0 != line3); - EXPECT_TRUE(line1 != line3); - EXPECT_TRUE(line2 != line3); + EXPECT_FALSE(line00 != line00); + EXPECT_TRUE(line00 != line01); + EXPECT_FALSE(line01 != line04); + EXPECT_TRUE(line00 != line02); + EXPECT_FALSE(line02 != line05); + EXPECT_TRUE(line00 != line03); + EXPECT_FALSE(line03 != line06); + EXPECT_FALSE(line00 != line07); + EXPECT_FALSE(line07 != line14); + EXPECT_TRUE(line00 != line08); + EXPECT_FALSE(line08 != line11); + EXPECT_TRUE(line00 != line09); + EXPECT_FALSE(line09 != line12); + EXPECT_TRUE(line00 != line10); + EXPECT_FALSE(line10 != line13); } TEST(LineTest, IsOnTest) { cpp_utils::Vector point0(0, 1, 1); - cpp_utils::Vector point1(1, 1, 1); - cpp_utils::Vector point2(1, 2, 2); - cpp_utils::Line line(point0, point1); + cpp_utils::Vector point1(-1, 1, -1); + cpp_utils::Vector point2(-1, 2, 0); + cpp_utils::Line line0(point0, point1); + + cpp_utils::Vector point3(1, 0, 1); + cpp_utils::Vector point4(-1, -1, 1); + cpp_utils::Vector point5(2, 1, 0); + cpp_utils::Line line1(point3, point4); + + cpp_utils::Vector point6(1, 1, 0); + cpp_utils::Vector point7(1, -1, -1); + cpp_utils::Vector point8(0, 2, 1); + cpp_utils::Line line2(point6, point7); - std::pair isOnLine0 = line.isOnLine(point0); - std::pair isOnLine1 = line.isOnLine(point1); - std::pair isOnLine2 = line.isOnLine(point2); + EXPECT_TRUE(line0.isOnLine(point0)); + EXPECT_FALSE(line0.isOnLine(point1)); + EXPECT_TRUE(line0.isOnLine(point2)); - EXPECT_TRUE(isOnLine0.first); - EXPECT_FALSE(isOnLine1.first); - EXPECT_TRUE(isOnLine2.first); + EXPECT_TRUE(line1.isOnLine(point3)); + EXPECT_FALSE(line1.isOnLine(point4)); + EXPECT_TRUE(line1.isOnLine(point5)); - EXPECT_EQ(isOnLine0.second, 0); - EXPECT_EQ(isOnLine1.second, 0); - EXPECT_EQ(isOnLine2.second, 1); + EXPECT_TRUE(line2.isOnLine(point6)); + EXPECT_FALSE(line2.isOnLine(point7)); + EXPECT_TRUE(line2.isOnLine(point8)); +} + +TEST(LineTest, whereOnTest) { + cpp_utils::Vector point0(0, 1, 1); + cpp_utils::Vector point1(-1, 1, -1); + cpp_utils::Vector point2(-1, 2, 0); + cpp_utils::Line line0(point0, point1); + + cpp_utils::Vector point3(1, 0, 1); + cpp_utils::Vector point4(-1, -1, 1); + cpp_utils::Vector point5(2, 1, 0); + cpp_utils::Line line1(point3, point4); + + cpp_utils::Vector point6(1, 1, 0); + cpp_utils::Vector point7(1, -1, -1); + cpp_utils::Vector point8(0, 2, 1); + cpp_utils::Line line2(point6, point7); + + EXPECT_EQ(line0.whereOnLine(point0), 0); + EXPECT_THROW(line0.whereOnLine(point1), std::exception); + EXPECT_EQ(line0.whereOnLine(point2), - 1 / norm); + + EXPECT_EQ(line1.whereOnLine(point3), 0); + EXPECT_THROW(line1.whereOnLine(point4), std::exception); + EXPECT_EQ(line1.whereOnLine(point5), 1 / norm); + + EXPECT_EQ(line2.whereOnLine(point6), 0); + EXPECT_THROW(line2.whereOnLine(point7), std::exception); + EXPECT_EQ(line2.whereOnLine(point8), - 1 / norm); } diff --git a/test/plane_unittest.cpp b/test/plane_unittest.cpp index 264fc07..ce3aded 100644 --- a/test/plane_unittest.cpp +++ b/test/plane_unittest.cpp @@ -14,41 +14,105 @@ TEST(PlaneTest, InitTest) { cpp_utils::Vector normal(4, 5, 6); cpp_utils::Plane plane(point, normal); EXPECT_TRUE(plane.point == point); - EXPECT_TRUE(plane.normal == (1 / normal.mod()) * normal); + EXPECT_TRUE(plane.normal == normal.normalise()); } TEST(PlaneTest, EqualityTest) { - cpp_utils::Vector vec0; - cpp_utils::Vector vec1(-1, -1, -1); - cpp_utils::Vector vec2(1, 0, -1); - cpp_utils::Plane plane0; - cpp_utils::Plane plane1(vec1, vec1); - cpp_utils::Plane plane2(vec0, vec2); - cpp_utils::Plane plane3(vec2, vec1); + cpp_utils::Vector vec00; + cpp_utils::Vector vec01(1, 0, 0); + cpp_utils::Vector vec02(0, 1, 0); + cpp_utils::Vector vec03(0, 0, 1); + cpp_utils::Vector vec04(-1, 0, 0); + cpp_utils::Vector vec05(0, -1, 0); + cpp_utils::Vector vec06(0, 0, -1); + cpp_utils::Vector vec07(1, 1, 1); + cpp_utils::Vector vec08(1, 0, -1); + cpp_utils::Vector vec09(-1, 1, 0); + cpp_utils::Vector vec10(0, -1, 1); + cpp_utils::Vector vec11(-1, 0, 1); + cpp_utils::Vector vec12(1, -1, 0); + cpp_utils::Vector vec13(0, 1, -1); + cpp_utils::Vector vec14(-1, -1, -1); + cpp_utils::Plane plane00; + cpp_utils::Plane plane01(vec06, vec01); + cpp_utils::Plane plane02(vec04, vec02); + cpp_utils::Plane plane03(vec05, vec03); + cpp_utils::Plane plane04(vec02, vec04); + cpp_utils::Plane plane05(vec03, vec05); + cpp_utils::Plane plane06(vec01, vec06); + cpp_utils::Plane plane07(vec00, vec07); + cpp_utils::Plane plane08(vec02, vec08); + cpp_utils::Plane plane09(vec03, vec09); + cpp_utils::Plane plane10(vec01, vec10); + cpp_utils::Plane plane11(vec05, vec11); + cpp_utils::Plane plane12(vec06, vec12); + cpp_utils::Plane plane13(vec04, vec13); + cpp_utils::Plane plane14(vec00, vec14); - EXPECT_TRUE(plane0 == plane0); - EXPECT_FALSE(plane0 == plane1); - EXPECT_FALSE(plane0 == plane2); - EXPECT_TRUE(plane0 == plane3); - EXPECT_FALSE(plane1 == plane2); - EXPECT_FALSE(plane2 == plane3); + EXPECT_TRUE(plane00 == plane00); + EXPECT_FALSE(plane00 == plane01); + EXPECT_TRUE(plane01 == plane04); + EXPECT_FALSE(plane00 == plane02); + EXPECT_TRUE(plane02 == plane05); + EXPECT_FALSE(plane00 == plane03); + EXPECT_TRUE(plane03 == plane06); + EXPECT_TRUE(plane00 == plane07); + EXPECT_TRUE(plane07 == plane14); + EXPECT_FALSE(plane00 == plane08); + EXPECT_TRUE(plane08 == plane11); + EXPECT_FALSE(plane00 == plane09); + EXPECT_TRUE(plane09 == plane12); + EXPECT_FALSE(plane00 == plane10); + EXPECT_TRUE(plane10 == plane13); } TEST(PlaneTest, NotEqualityTest) { - cpp_utils::Vector vec0; - cpp_utils::Vector vec1(-1, -1, -1); - cpp_utils::Vector vec2(1, 0, -1); - cpp_utils::Plane plane0; - cpp_utils::Plane plane1(vec1, vec1); - cpp_utils::Plane plane2(vec0, vec2); - cpp_utils::Plane plane3(vec2, vec1); + cpp_utils::Vector vec00; + cpp_utils::Vector vec01(1, 0, 0); + cpp_utils::Vector vec02(0, 1, 0); + cpp_utils::Vector vec03(0, 0, 1); + cpp_utils::Vector vec04(-1, 0, 0); + cpp_utils::Vector vec05(0, -1, 0); + cpp_utils::Vector vec06(0, 0, -1); + cpp_utils::Vector vec07(1, 1, 1); + cpp_utils::Vector vec08(1, 0, -1); + cpp_utils::Vector vec09(-1, 1, 0); + cpp_utils::Vector vec10(0, -1, 1); + cpp_utils::Vector vec11(-1, 0, 1); + cpp_utils::Vector vec12(1, -1, 0); + cpp_utils::Vector vec13(0, 1, -1); + cpp_utils::Vector vec14(-1, -1, -1); + cpp_utils::Plane plane00; + cpp_utils::Plane plane01(vec06, vec01); + cpp_utils::Plane plane02(vec04, vec02); + cpp_utils::Plane plane03(vec05, vec03); + cpp_utils::Plane plane04(vec02, vec04); + cpp_utils::Plane plane05(vec03, vec05); + cpp_utils::Plane plane06(vec01, vec06); + cpp_utils::Plane plane07(vec00, vec07); + cpp_utils::Plane plane08(vec02, vec08); + cpp_utils::Plane plane09(vec03, vec09); + cpp_utils::Plane plane10(vec01, vec10); + cpp_utils::Plane plane11(vec05, vec11); + cpp_utils::Plane plane12(vec06, vec12); + cpp_utils::Plane plane13(vec04, vec13); + cpp_utils::Plane plane14(vec00, vec14); - EXPECT_FALSE(plane0 != plane0); - EXPECT_TRUE(plane0 != plane1); - EXPECT_TRUE(plane0 != plane2); - EXPECT_FALSE(plane0 != plane3); - EXPECT_TRUE(plane1 != plane2); - EXPECT_TRUE(plane2 != plane3); + EXPECT_FALSE(plane00 != plane00); + EXPECT_TRUE(plane00 != plane01); + EXPECT_FALSE(plane01 != plane04); + EXPECT_TRUE(plane00 != plane02); + EXPECT_FALSE(plane02 != plane05); + EXPECT_TRUE(plane00 != plane03); + EXPECT_FALSE(plane03 != plane06); + EXPECT_FALSE(plane00 != plane07); + EXPECT_FALSE(plane07 != plane14); + EXPECT_TRUE(plane00 != plane08); + EXPECT_FALSE(plane08 != plane11); + EXPECT_TRUE(plane00 != plane09); + EXPECT_FALSE(plane09 != plane12); + EXPECT_TRUE(plane00 != plane10); + EXPECT_FALSE(plane10 != plane13); } TEST(PlaneTest, IsOnTest) { From 9f8ba4d3555f62f73d0f9df802f07d5d72090ec6 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 21:07:33 +0100 Subject: [PATCH 03/15] Catch Block --- source/line.cpp | 57 ++++++++++++++++++++++--------------------------- source/line.hpp | 9 ++++---- 2 files changed, 30 insertions(+), 36 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index 55c4918..3cfca57 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -11,7 +11,7 @@ namespace cpp_utils { } }; - bool Line::operator==(const Line &that) { + bool Line::operator==(const Line &that) const { bool on = this->isOnLine(that.point); if (direction == that.direction && on) { return true; @@ -20,39 +20,34 @@ namespace cpp_utils { } }; - bool Line::isOnLine(const Vector &that) { + bool Line::isOnLine(const Vector &that) const { + bool isOn = true; + try { + this->whereOnLine(that); + } catch (std::overflow_error) { + isOn = false; + } + return isOn; + } + + double Line::whereOnLine(const Vector &that) const { Vector vecDiff = that - point; double scalarX, scalarY, scalarZ; - if ((direction.x == 0 && vecDiff.x != 0) || - (direction.y == 0 && vecDiff.y != 0) || - (direction.z == 0 && vecDiff.z != 0)) { - return false; - } else { - if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;} - if (direction.y == 0) {scalarY = vecDiff.z;} else {scalarY = vecDiff.y / direction.y;} - if (direction.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / direction.z;} - if ((scalarX == scalarY && scalarX == scalarZ) || - (scalarY == 0 && scalarX == scalarZ && direction.y == 0) || - (scalarZ == 0 && scalarX == scalarY && direction.z == 0) || - (scalarY == 0 && scalarZ == 0 && direction.y == 0 && direction.z == 0)) { - scalar = scalarX; - return true; - } else if ((scalarX == 0 && scalarY == scalarZ && direction.x == 0) || - (scalarX == 0 && scalarZ == 0 && direction.x == 0 && direction.z == 0)) { - scalar = scalarY; - return true; - } else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) { - scalar = scalarZ; - return true; - } else { - return false; - } - }; - } - double Line::whereOnLine(const Vector &that) { - if (this->isOnLine(that)) { - return scalar; + if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;} + if (direction.y == 0) {scalarY = vecDiff.z;} else {scalarY = vecDiff.y / direction.y;} + if (direction.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / direction.z;} + + if ((scalarX == scalarY && scalarX == scalarZ) || + (scalarY == 0 && scalarX == scalarZ && direction.y == 0) || + (scalarZ == 0 && scalarX == scalarY && direction.z == 0) || + (scalarY == 0 && scalarZ == 0 && direction.y == 0 && direction.z == 0)) { + return scalarX; + } else if ((scalarX == 0 && scalarY == scalarZ && direction.x == 0) || + (scalarX == 0 && scalarZ == 0 && direction.x == 0 && direction.z == 0)) { + return scalarY; + } else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) { + return scalarZ; } else { throw std::overflow_error("Point not on line."); } diff --git a/source/line.hpp b/source/line.hpp index 4d88839..69f4e21 100644 --- a/source/line.hpp +++ b/source/line.hpp @@ -9,19 +9,18 @@ namespace cpp_utils { class Line { public: - double scalar; Vector point, direction; inline Line() : point(0, 0, 0), direction(norm, norm, norm) {}; Line(Vector pnt, Vector dir); - bool operator==(const Line &that); - inline bool operator!=(const Line &that) { + bool operator==(const Line &that) const; + inline bool operator!=(const Line &that) const { return true != (*this == that); }; - bool isOnLine(const Vector &that); + bool isOnLine(const Vector &that) const; - double whereOnLine(const Vector &that); + double whereOnLine(const Vector &that) const; }; } From e4d75baab61bd70b1ab765664536365532eb1a40 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 21:09:17 +0100 Subject: [PATCH 04/15] WhereOnTest --- test/line_unittest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index 037e473..4f56059 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -144,7 +144,7 @@ TEST(LineTest, IsOnTest) { EXPECT_TRUE(line2.isOnLine(point8)); } -TEST(LineTest, whereOnTest) { +TEST(LineTest, WhereOnTest) { cpp_utils::Vector point0(0, 1, 1); cpp_utils::Vector point1(-1, 1, -1); cpp_utils::Vector point2(-1, 2, 0); From 468f3226cc5c8b131679ac88a731570ad8694f05 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 21:34:16 +0100 Subject: [PATCH 05/15] Second Fix --- source/line.cpp | 4 ++-- source/matrix.cpp | 2 +- source/plane.cpp | 8 ++------ 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index 3cfca57..6f7622f 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -24,7 +24,7 @@ namespace cpp_utils { bool isOn = true; try { this->whereOnLine(that); - } catch (std::overflow_error) { + } catch (std::logic_error) { isOn = false; } return isOn; @@ -49,7 +49,7 @@ namespace cpp_utils { } else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) { return scalarZ; } else { - throw std::overflow_error("Point not on line."); + throw std::logic_error("Point not on line."); } }; diff --git a/source/matrix.cpp b/source/matrix.cpp index ba941d5..0b338d3 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -80,7 +80,7 @@ namespace cpp_utils { Matrix Matrix::inverse() const { if (this->det() == 0) { - throw std::overflow_error("No inverse due to zero value of determinant."); + throw std::logic_error("No inverse due to zero value of determinant."); } else { return (1/det()) * Matrix( ((yy * zz) - (zy * yz)), -((xy * zz) - (zy * xz)), ((xy * yz) - (yy * xz)), -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), diff --git a/source/plane.cpp b/source/plane.cpp index 5993432..3dc180e 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -21,11 +21,7 @@ namespace cpp_utils { bool Plane::isOnPlane(const Vector &that) const { Vector vecDiff = that - point; - if (vecDiff * normal == 0) { - return true; - } else { - return false; - } + return vecDiff * normal == 0; }; Vector Plane::intersection(const Line &that) const { @@ -33,7 +29,7 @@ namespace cpp_utils { Vector vecDiff = that.point - point; double dotProd = vecDiff * normal; if (cosAngle == 0) { - throw std::overflow_error("Line lies in plane or is parallel to plane."); + throw std::logic_error("Line lies in plane or is parallel to plane."); } else if (dotProd == 0) { return that.point; } else { From 9deee68a282ddd3037cd112dfc2678b5dbc07506 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 23:31:30 +0100 Subject: [PATCH 06/15] Derived Error --- .vscode/settings.json | 5 +++-- exception/value.hpp | 14 ++++++++++++++ source/line.cpp | 4 ++-- source/matrix.cpp | 2 +- source/plane.cpp | 2 +- source/vector.hpp | 1 + test/line_unittest.cpp | 6 +++--- test/matrix_unittest.cpp | 4 ++-- test/plane_unittest.cpp | 4 ++-- 9 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 exception/value.hpp diff --git a/.vscode/settings.json b/.vscode/settings.json index 5bb4b0e..c06eed5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ -{ +{ + "clang-format.executable": "/absolute/path/to/clang-format", "files.associations": { "any": "cpp", "array": "cpp", @@ -87,4 +88,4 @@ "xtree": "cpp", "xutility": "cpp" } -} \ No newline at end of file +} diff --git a/exception/value.hpp b/exception/value.hpp new file mode 100644 index 0000000..62a4eec --- /dev/null +++ b/exception/value.hpp @@ -0,0 +1,14 @@ +#ifndef VALUE_HPP +#define VALUE_HPP + +#include + +class value_error : public std::exception { + public: + value_error(const char* msg) : message(msg) {}; + const char* what() const noexcept {return message.c_str();} + private: + std::string message; +}; + +#endif diff --git a/source/line.cpp b/source/line.cpp index 6f7622f..801d58a 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -24,7 +24,7 @@ namespace cpp_utils { bool isOn = true; try { this->whereOnLine(that); - } catch (std::logic_error) { + } catch (value_error) { isOn = false; } return isOn; @@ -49,7 +49,7 @@ namespace cpp_utils { } else if ((scalarX == 0 && scalarY == 0 && direction.x == 0 && direction.y == 0)) { return scalarZ; } else { - throw std::logic_error("Point not on line."); + throw value_error("Point not on line."); } }; diff --git a/source/matrix.cpp b/source/matrix.cpp index 0b338d3..7215610 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -80,7 +80,7 @@ namespace cpp_utils { Matrix Matrix::inverse() const { if (this->det() == 0) { - throw std::logic_error("No inverse due to zero value of determinant."); + throw value_error("No inverse due to zero value of determinant."); } else { return (1/det()) * Matrix( ((yy * zz) - (zy * yz)), -((xy * zz) - (zy * xz)), ((xy * yz) - (yy * xz)), -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), diff --git a/source/plane.cpp b/source/plane.cpp index 3dc180e..bd46685 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -29,7 +29,7 @@ namespace cpp_utils { Vector vecDiff = that.point - point; double dotProd = vecDiff * normal; if (cosAngle == 0) { - throw std::logic_error("Line lies in plane or is parallel to plane."); + throw value_error("Line lies in plane or is parallel to plane."); } else if (dotProd == 0) { return that.point; } else { diff --git a/source/vector.hpp b/source/vector.hpp index a0bbf18..dc3cf09 100644 --- a/source/vector.hpp +++ b/source/vector.hpp @@ -2,6 +2,7 @@ #define VECTOR_HPP #include +#include "..\exception\value.hpp" namespace cpp_utils { diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index 4f56059..f653630 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -161,14 +161,14 @@ TEST(LineTest, WhereOnTest) { cpp_utils::Line line2(point6, point7); EXPECT_EQ(line0.whereOnLine(point0), 0); - EXPECT_THROW(line0.whereOnLine(point1), std::exception); + EXPECT_THROW(line0.whereOnLine(point1), value_error); EXPECT_EQ(line0.whereOnLine(point2), - 1 / norm); EXPECT_EQ(line1.whereOnLine(point3), 0); - EXPECT_THROW(line1.whereOnLine(point4), std::exception); + EXPECT_THROW(line1.whereOnLine(point4), value_error); EXPECT_EQ(line1.whereOnLine(point5), 1 / norm); EXPECT_EQ(line2.whereOnLine(point6), 0); - EXPECT_THROW(line2.whereOnLine(point7), std::exception); + EXPECT_THROW(line2.whereOnLine(point7), value_error); EXPECT_EQ(line2.whereOnLine(point8), - 1 / norm); } diff --git a/test/matrix_unittest.cpp b/test/matrix_unittest.cpp index fe4d5b0..bc37aac 100644 --- a/test/matrix_unittest.cpp +++ b/test/matrix_unittest.cpp @@ -146,8 +146,8 @@ TEST(MatrixTest, InverseTest) { cpp_utils::Matrix Mat4(0, 0, 1, 0, 1, 0, -1, 0, 0); cpp_utils::Matrix Mat5(0, 0, 1, 0, 1, 0, 1, 0, 0); - EXPECT_THROW(Mat1.inverse(), std::exception); - EXPECT_THROW(Mat2.inverse(), std::exception); + EXPECT_THROW(Mat1.inverse(), value_error); + EXPECT_THROW(Mat2.inverse(), value_error); EXPECT_TRUE(Mat3.inverse() == Mat4); EXPECT_TRUE(Mat4.inverse() == Mat3); EXPECT_TRUE(Mat5.inverse() == Mat5); diff --git a/test/plane_unittest.cpp b/test/plane_unittest.cpp index ce3aded..3beba7a 100644 --- a/test/plane_unittest.cpp +++ b/test/plane_unittest.cpp @@ -138,8 +138,8 @@ TEST(PlaneTest, IntersectionTest) { EXPECT_EQ(plane.intersection(line1), point0); EXPECT_EQ(plane.intersection(line2), point0); - EXPECT_THROW(plane.intersection(line3), std::exception); - EXPECT_THROW(plane.intersection(line4), std::exception); + EXPECT_THROW(plane.intersection(line3), value_error); + EXPECT_THROW(plane.intersection(line4), value_error); } TEST(PlaneTest, BetweenTest) { From ba96749da9147361c818c7894a002508e9f8dc27 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 23:44:00 +0100 Subject: [PATCH 07/15] _names --- source/line.cpp | 4 ++-- source/line.hpp | 2 +- source/plane.cpp | 4 ++-- source/plane.hpp | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index 801d58a..5e5fb25 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -2,8 +2,8 @@ namespace cpp_utils { - Line::Line(Vector pnt, Vector dir) : point(pnt) { - Vector normDir = dir.normalise(); + Line::Line(Vector _point, Vector _direction) : point(_point) { + Vector normDir = _direction.normalise(); if ((normDir.x < 0) || (normDir.x == 0 && normDir.y < 0) || (normDir.x == 0 && normDir.y == 0 && normDir.z < 0)) { direction = -normDir; } else { diff --git a/source/line.hpp b/source/line.hpp index 69f4e21..47a4d21 100644 --- a/source/line.hpp +++ b/source/line.hpp @@ -11,7 +11,7 @@ namespace cpp_utils { public: Vector point, direction; inline Line() : point(0, 0, 0), direction(norm, norm, norm) {}; - Line(Vector pnt, Vector dir); + Line(Vector _point, Vector _direction); bool operator==(const Line &that) const; inline bool operator!=(const Line &that) const { diff --git a/source/plane.cpp b/source/plane.cpp index bd46685..9d0b173 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -2,8 +2,8 @@ namespace cpp_utils { - Plane::Plane(Vector pnt, Vector nor) : point(pnt) { - Vector normNor = nor.normalise(); + Plane::Plane(Vector _point, Vector _normal) : point(_point) { + Vector normNor = _normal.normalise(); if ((normNor.x < 0) || (normNor.x == 0 && normNor.y < 0) || (normNor.x == 0 && normNor.y == 0 && normNor.z < 0)) { normal = -normNor; } else { diff --git a/source/plane.hpp b/source/plane.hpp index 538fc90..06b8348 100644 --- a/source/plane.hpp +++ b/source/plane.hpp @@ -9,7 +9,7 @@ namespace cpp_utils { public: Vector point, normal; inline Plane() : point(0, 0, 0), normal(norm, norm, norm) {}; - Plane(Vector pnt, Vector nor); + Plane(Vector _point, Vector _normal); bool operator==(const Plane &that) const; inline bool operator!=(const Plane &that) const { From d8da90a762673147483882563ea53b29bc23d0b9 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Wed, 20 Aug 2025 23:58:44 +0100 Subject: [PATCH 08/15] exception --- exception/exception.hpp | 1 + source/vector.hpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 exception/exception.hpp diff --git a/exception/exception.hpp b/exception/exception.hpp new file mode 100644 index 0000000..5cbb0c9 --- /dev/null +++ b/exception/exception.hpp @@ -0,0 +1 @@ +#include "value.hpp" diff --git a/source/vector.hpp b/source/vector.hpp index dc3cf09..8130aed 100644 --- a/source/vector.hpp +++ b/source/vector.hpp @@ -2,7 +2,7 @@ #define VECTOR_HPP #include -#include "..\exception\value.hpp" +#include "..\exception\exception.hpp" namespace cpp_utils { From 921c3d3b226a2da2dd40068ece154c8fd32638eb Mon Sep 17 00:00:00 2001 From: Ycidia Date: Thu, 21 Aug 2025 15:08:48 +0100 Subject: [PATCH 09/15] if statements removed and readablility of unittests --- source/line.cpp | 11 +- source/matrix.cpp | 69 ++++---- source/plane.cpp | 12 +- source/vector.cpp | 15 +- test/line_unittest.cpp | 308 ++++++++++++++++------------------- test/matrix_unittest.cpp | 290 ++++++++++++++++----------------- test/plane_unittest.cpp | 278 ++++++++++++++----------------- test/quaternion_unittest.cpp | 242 +++++++++++++-------------- test/vector_unittest.cpp | 209 ++++++++++++------------ 9 files changed, 673 insertions(+), 761 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index 5e5fb25..ac40271 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -12,12 +12,7 @@ namespace cpp_utils { }; bool Line::operator==(const Line &that) const { - bool on = this->isOnLine(that.point); - if (direction == that.direction && on) { - return true; - } else { - return false; - } + return direction == that.direction && this->isOnLine(that.point); }; bool Line::isOnLine(const Vector &that) const { @@ -28,7 +23,7 @@ namespace cpp_utils { isOn = false; } return isOn; - } + }; double Line::whereOnLine(const Vector &that) const { Vector vecDiff = that - point; @@ -52,5 +47,5 @@ namespace cpp_utils { throw value_error("Point not on line."); } }; - + } diff --git a/source/matrix.cpp b/source/matrix.cpp index 7215610..4452a7c 100644 --- a/source/matrix.cpp +++ b/source/matrix.cpp @@ -17,43 +17,28 @@ namespace cpp_utils { }; bool Matrix::operator==(const Matrix &that) const { - double test_precision = std::fmax(precision, that.precision); - if ((xx < (that.xx - test_precision)) || (xx > (that.xx + test_precision))) { - return false; - } - if ((xy < (that.xy - test_precision)) || (xy > (that.xy + test_precision))) { - return false; - } - if ((xz < (that.xz - test_precision)) || (xz > (that.xz + test_precision))) { - return false; - } - if ((yx < (that.yx - test_precision)) || (yx > (that.yx + test_precision))) { - return false; - } - if ((yy < (that.yy - test_precision)) || (yy > (that.yy + test_precision))) { - return false; - } - if ((yz < (that.yz - test_precision)) || (yz > (that.yz + test_precision))) { - return false; - } - if ((zx < (that.zx - test_precision)) || (zx > (that.zx + test_precision))) { - return false; - } - if ((zy < (that.zy - test_precision)) || (zy > (that.zy + test_precision))) { - return false; - } - if ((zz < (that.zz - test_precision)) || (zz > (that.zz + test_precision))) { - return false; - } - return true; + double testPrecision = std::fmax(precision, that.precision); + return !(std::abs(xx - that.xx) > testPrecision || + std::abs(xy - that.xy) > testPrecision || + std::abs(xz - that.xz) > testPrecision || + std::abs(yx - that.yx) > testPrecision || + std::abs(yy - that.yy) > testPrecision || + std::abs(yz - that.yz) > testPrecision || + std::abs(zx - that.zx) > testPrecision || + std::abs(zy - that.zy) > testPrecision || + std::abs(zz - that.zz) > testPrecision); }; Matrix Matrix::operator+(const Matrix &that) const { - return Matrix(this->xx + that.xx, this->xy + that.xy, this->xz + that.xz, this->yx + that.yx, this->yy + that.yy, this->yz + that.yz, this->zx + that.zx, this->zy + that.zy, this->zz + that.zz); + return Matrix(this->xx + that.xx, this->xy + that.xy, this->xz + that.xz, + this->yx + that.yx, this->yy + that.yy, this->yz + that.yz, + this->zx + that.zx, this->zy + that.zy, this->zz + that.zz); }; Matrix Matrix::operator-(const Matrix &that) const { - return Matrix(this->xx - that.xx, this->xy - that.xy, this->xz - that.xz, this->yx - that.yx, this->yy - that.yy, this->yz - that.yz, this->zx - that.zx, this->zy - that.zy, this->zz - that.zz); + return Matrix(this->xx - that.xx, this->xy - that.xy, this->xz - that.xz, + this->yx - that.yx, this->yy - that.yy, this->yz - that.yz, + this->zx - that.zx, this->zy - that.zy, this->zz - that.zz); }; Matrix Matrix::operator*(const double &that) const { @@ -65,17 +50,27 @@ namespace cpp_utils { }; Matrix Matrix::operator*(const Matrix &that) const { - return Matrix((xx * that.xx) + (xy * that.yx) + (xz * that.zx), (xx * that.xy) + (xy * that.yy) + (xz * that.zy), (xx * that.xz) + (xy * that.yz) + (xz * that.zz), - (yx * that.xx) + (yy * that.yx) + (yz * that.zx), (yx * that.xy) + (yy * that.yy) + (yz * that.zy), (yx * that.xz) + (yy * that.yz) + (yz * that.zz), - (zx * that.xx) + (zy * that.yx) + (zz * that.zx), (zx * that.xy) + (zy * that.yy) + (zz * that.zy), (zx * that.xz) + (zy * that.yz) + (zz * that.zz)); + return Matrix((xx * that.xx) + (xy * that.yx) + (xz * that.zx), + (xx * that.xy) + (xy * that.yy) + (xz * that.zy), + (xx * that.xz) + (xy * that.yz) + (xz * that.zz), + (yx * that.xx) + (yy * that.yx) + (yz * that.zx), + (yx * that.xy) + (yy * that.yy) + (yz * that.zy), + (yx * that.xz) + (yy * that.yz) + (yz * that.zz), + (zx * that.xx) + (zy * that.yx) + (zz * that.zx), + (zx * that.xy) + (zy * that.yy) + (zz * that.zy), + (zx * that.xz) + (zy * that.yz) + (zz * that.zz)); }; Vector Matrix::operator*(const Vector &that) const { - return Vector((xx * that.x) + (xy * that.y) + (xz * that.z), (yx * that.x) + (yy * that.y) + (yz * that.z), (zx * that.x) + (zy * that.y) + (zz * that.z)); + return Vector((xx * that.x) + (xy * that.y) + (xz * that.z), + (yx * that.x) + (yy * that.y) + (yz * that.z), + (zx * that.x) + (zy * that.y) + (zz * that.z)); }; Vector operator*(const Vector that, const Matrix &those) { - return Vector((that.x * those.xx) + (that.y * those.yx) + (that.z * those.zx), (that.x * those.xy) + (that.y * those.yy) + (that.z * those.zy), (that.x * those.xz) + (that.y * those.yz) + (that.z * those.zz)); + return Vector((that.x * those.xx) + (that.y * those.yx) + (that.z * those.zx), + (that.x * those.xy) + (that.y * those.yy) + (that.z * those.zy), + (that.x * those.xz) + (that.y * those.yz) + (that.z * those.zz)); }; Matrix Matrix::inverse() const { @@ -86,6 +81,6 @@ namespace cpp_utils { -((yx * zz) - (zx * yz)), ((xx * zz) - (zx * xz)), -((xx * yz) - (yx * xz)), ((yx * zy) - (zx * yy)), -((xx * zy) - (zx * xy)), ((xx * yy) - (yx * xy))); }; - } + }; } diff --git a/source/plane.cpp b/source/plane.cpp index 9d0b173..1043480 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -12,11 +12,7 @@ namespace cpp_utils { }; bool Plane::operator==(const Plane &that) const { - if (normal == that.normal && this->isOnPlane(that.point)) { - return true; - } else { - return false; - } + return normal == that.normal && this->isOnPlane(that.point); }; bool Plane::isOnPlane(const Vector &that) const { @@ -44,11 +40,7 @@ namespace cpp_utils { } else { double abovePlane1 = (line.point - plane1.point) * plane1.normal; double abovePlane2 = (line.point - plane2.point) * plane2.normal; - if (abovePlane1 * abovePlane2 < 0) { - return true; - } else { - return false; - } + return abovePlane1 * abovePlane2 < 0; } }; diff --git a/source/vector.cpp b/source/vector.cpp index b464c02..ad2bcb5 100644 --- a/source/vector.cpp +++ b/source/vector.cpp @@ -13,17 +13,10 @@ namespace cpp_utils { }; bool Vector::operator==(const Vector &that) const { - double test_precision = std::fmax(precision, that.precision); - if ((x < (that.x - test_precision)) || (x > (that.x + test_precision))) { - return false; - } - if ((y < (that.y - test_precision)) || (y > (that.y + test_precision))) { - return false; - } - if ((z < (that.z - test_precision)) || (z > (that.z + test_precision))) { - return false; - } - return true; + double testPrecision = std::fmax(precision, that.precision); + return !(std::abs(x - that.x) > testPrecision || + std::abs(y - that.y) > testPrecision || + std::abs(z - that.z) > testPrecision); }; Vector Vector::operator+(const Vector &that) const { diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index f653630..56dbf10 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -1,174 +1,152 @@ #include "gtest/gtest.h" #include "../source/line.hpp" -TEST(LineTest, DefaultInitTest) { - cpp_utils::Vector point(0, 0, 0); - cpp_utils::Vector direction(norm, norm, norm); - cpp_utils::Line line; - EXPECT_TRUE(line.point == point); - EXPECT_TRUE(line.direction == direction); -} +namespace cpp_utils { -TEST(LineTest, InitTest) { - cpp_utils::Vector point(1, 2, 3); - cpp_utils::Vector direction(4, 5, 6); - cpp_utils::Line line(point, direction); - EXPECT_TRUE(line.point == point); - EXPECT_TRUE(line.direction == direction.normalise()); -} + TEST(LineTest, DefaultInitTest) { + Vector point(0, 0, 0); + Vector direction(norm, norm, norm); + Line line; + EXPECT_TRUE(line.point == point); + EXPECT_TRUE(line.direction == direction); + } -TEST(LineTest, EqualityTest) { - cpp_utils::Vector vec00; - cpp_utils::Vector vec01(1, 0, 0); - cpp_utils::Vector vec02(0, 1, 0); - cpp_utils::Vector vec03(0, 0, 1); - cpp_utils::Vector vec04(-1, 0, 0); - cpp_utils::Vector vec05(0, -1, 0); - cpp_utils::Vector vec06(0, 0, -1); - cpp_utils::Vector vec07(1, 1, 1); - cpp_utils::Vector vec08(-1, 1, 1); - cpp_utils::Vector vec09(1, -1, 1); - cpp_utils::Vector vec10(1, 1, -1); - cpp_utils::Vector vec11(1, -1, -1); - cpp_utils::Vector vec12(-1, 1, -1); - cpp_utils::Vector vec13(-1, -1, 1); - cpp_utils::Vector vec14(-1, -1, -1); - cpp_utils::Line line00; - cpp_utils::Line line01(vec04, vec01); - cpp_utils::Line line02(vec05, vec02); - cpp_utils::Line line03(vec06, vec03); - cpp_utils::Line line04(vec01, vec04); - cpp_utils::Line line05(vec02, vec05); - cpp_utils::Line line06(vec03, vec06); - cpp_utils::Line line07(vec14, vec07); - cpp_utils::Line line08(vec11, vec08); - cpp_utils::Line line09(vec12, vec09); - cpp_utils::Line line10(vec13, vec10); - cpp_utils::Line line11(vec08, vec11); - cpp_utils::Line line12(vec09, vec12); - cpp_utils::Line line13(vec10, vec13); - cpp_utils::Line line14(vec07, vec14); - - EXPECT_TRUE(line00 == line00); - EXPECT_FALSE(line00 == line01); - EXPECT_TRUE(line01 == line04); - EXPECT_FALSE(line00 == line02); - EXPECT_TRUE(line02 == line05); - EXPECT_FALSE(line00 == line03); - EXPECT_TRUE(line03 == line06); - EXPECT_TRUE(line00 == line07); - EXPECT_TRUE(line07 == line14); - EXPECT_FALSE(line00 == line08); - EXPECT_TRUE(line08 == line11); - EXPECT_FALSE(line00 == line09); - EXPECT_TRUE(line09 == line12); - EXPECT_FALSE(line00 == line10); - EXPECT_TRUE(line10 == line13); -} + TEST(LineTest, InitTest) { + Vector point(1, 2, 3); + Vector direction(4, 5, 6); + Line line(point, direction); + EXPECT_TRUE(line.point == point); + EXPECT_TRUE(line.direction == direction.normalise()); + } -TEST(LineTest, NotEqualityTest) { - cpp_utils::Vector vec00; - cpp_utils::Vector vec01(1, 0, 0); - cpp_utils::Vector vec02(0, 1, 0); - cpp_utils::Vector vec03(0, 0, 1); - cpp_utils::Vector vec04(-1, 0, 0); - cpp_utils::Vector vec05(0, -1, 0); - cpp_utils::Vector vec06(0, 0, -1); - cpp_utils::Vector vec07(1, 1, 1); - cpp_utils::Vector vec08(-1, 1, 1); - cpp_utils::Vector vec09(1, -1, 1); - cpp_utils::Vector vec10(1, 1, -1); - cpp_utils::Vector vec11(1, -1, -1); - cpp_utils::Vector vec12(-1, 1, -1); - cpp_utils::Vector vec13(-1, -1, 1); - cpp_utils::Vector vec14(-1, -1, -1); - cpp_utils::Line line00; - cpp_utils::Line line01(vec04, vec01); - cpp_utils::Line line02(vec05, vec02); - cpp_utils::Line line03(vec06, vec03); - cpp_utils::Line line04(vec01, vec04); - cpp_utils::Line line05(vec02, vec05); - cpp_utils::Line line06(vec03, vec06); - cpp_utils::Line line07(vec14, vec07); - cpp_utils::Line line08(vec11, vec08); - cpp_utils::Line line09(vec12, vec09); - cpp_utils::Line line10(vec13, vec10); - cpp_utils::Line line11(vec08, vec11); - cpp_utils::Line line12(vec09, vec12); - cpp_utils::Line line13(vec10, vec13); - cpp_utils::Line line14(vec07, vec14); - - EXPECT_FALSE(line00 != line00); - EXPECT_TRUE(line00 != line01); - EXPECT_FALSE(line01 != line04); - EXPECT_TRUE(line00 != line02); - EXPECT_FALSE(line02 != line05); - EXPECT_TRUE(line00 != line03); - EXPECT_FALSE(line03 != line06); - EXPECT_FALSE(line00 != line07); - EXPECT_FALSE(line07 != line14); - EXPECT_TRUE(line00 != line08); - EXPECT_FALSE(line08 != line11); - EXPECT_TRUE(line00 != line09); - EXPECT_FALSE(line09 != line12); - EXPECT_TRUE(line00 != line10); - EXPECT_FALSE(line10 != line13); -} + TEST(LineTest, EqualityTest) { -TEST(LineTest, IsOnTest) { - cpp_utils::Vector point0(0, 1, 1); - cpp_utils::Vector point1(-1, 1, -1); - cpp_utils::Vector point2(-1, 2, 0); - cpp_utils::Line line0(point0, point1); - - cpp_utils::Vector point3(1, 0, 1); - cpp_utils::Vector point4(-1, -1, 1); - cpp_utils::Vector point5(2, 1, 0); - cpp_utils::Line line1(point3, point4); - - cpp_utils::Vector point6(1, 1, 0); - cpp_utils::Vector point7(1, -1, -1); - cpp_utils::Vector point8(0, 2, 1); - cpp_utils::Line line2(point6, point7); - - EXPECT_TRUE(line0.isOnLine(point0)); - EXPECT_FALSE(line0.isOnLine(point1)); - EXPECT_TRUE(line0.isOnLine(point2)); - - EXPECT_TRUE(line1.isOnLine(point3)); - EXPECT_FALSE(line1.isOnLine(point4)); - EXPECT_TRUE(line1.isOnLine(point5)); - - EXPECT_TRUE(line2.isOnLine(point6)); - EXPECT_FALSE(line2.isOnLine(point7)); - EXPECT_TRUE(line2.isOnLine(point8)); -} + Vector vec00, + vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), + vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), + vec07(1, 1, 1), + vec08(-1, 1, 1), vec09(1, -1, 1), vec10(1, 1, -1), + vec11(1, -1, -1), vec12(-1, 1, -1), vec13(-1, -1, 1), + vec14(-1, -1, -1); + + Line line00, + line01(vec04, vec01), line02(vec05, vec02), line03(vec06, vec03), + line04(vec01, vec04), line05(vec02, vec05), line06(vec03, vec06), + line07(vec14, vec07), + line08(vec11, vec08), line09(vec12, vec09), line10(vec13, vec10), + line11(vec08, vec11), line12(vec09, vec12), line13(vec10, vec13), + line14(vec07, vec14); + + EXPECT_TRUE(line00 == line00); + EXPECT_FALSE(line00 == line01); + EXPECT_TRUE(line01 == line04); + EXPECT_FALSE(line00 == line02); + EXPECT_TRUE(line02 == line05); + EXPECT_FALSE(line00 == line03); + EXPECT_TRUE(line03 == line06); + EXPECT_TRUE(line00 == line07); + EXPECT_TRUE(line07 == line14); + EXPECT_FALSE(line00 == line08); + EXPECT_TRUE(line08 == line11); + EXPECT_FALSE(line00 == line09); + EXPECT_TRUE(line09 == line12); + EXPECT_FALSE(line00 == line10); + EXPECT_TRUE(line10 == line13); + } + + TEST(LineTest, NotEqualityTest) { + + Vector vec00, + vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), + vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), + vec07(1, 1, 1), + vec08(-1, 1, 1), vec09(1, -1, 1), vec10(1, 1, -1), + vec11(1, -1, -1), vec12(-1, 1, -1), vec13(-1, -1, 1), + vec14(-1, -1, -1); + + Line line00, + line01(vec04, vec01), line02(vec05, vec02), line03(vec06, vec03), + line04(vec01, vec04), line05(vec02, vec05), line06(vec03, vec06), + line07(vec14, vec07), + line08(vec11, vec08), line09(vec12, vec09), line10(vec13, vec10), + line11(vec08, vec11), line12(vec09, vec12), line13(vec10, vec13), + line14(vec07, vec14); + + EXPECT_FALSE(line00 != line00); + EXPECT_TRUE(line00 != line01); + EXPECT_FALSE(line01 != line04); + EXPECT_TRUE(line00 != line02); + EXPECT_FALSE(line02 != line05); + EXPECT_TRUE(line00 != line03); + EXPECT_FALSE(line03 != line06); + EXPECT_FALSE(line00 != line07); + EXPECT_FALSE(line07 != line14); + EXPECT_TRUE(line00 != line08); + EXPECT_FALSE(line08 != line11); + EXPECT_TRUE(line00 != line09); + EXPECT_FALSE(line09 != line12); + EXPECT_TRUE(line00 != line10); + EXPECT_FALSE(line10 != line13); + } + + TEST(LineTest, IsOnTest) { + + Vector point0(0, 1, 1); + Vector point1(-1, 1, -1); + Vector point2(-1, 2, 0); + Line line0(point0, point1); + + Vector point3(1, 0, 1); + Vector point4(-1, -1, 1); + Vector point5(2, 1, 0); + Line line1(point3, point4); + + Vector point6(1, 1, 0); + Vector point7(1, -1, -1); + Vector point8(0, 2, 1); + Line line2(point6, point7); + + EXPECT_TRUE(line0.isOnLine(point0)); + EXPECT_FALSE(line0.isOnLine(point1)); + EXPECT_TRUE(line0.isOnLine(point2)); + + EXPECT_TRUE(line1.isOnLine(point3)); + EXPECT_FALSE(line1.isOnLine(point4)); + EXPECT_TRUE(line1.isOnLine(point5)); + + EXPECT_TRUE(line2.isOnLine(point6)); + EXPECT_FALSE(line2.isOnLine(point7)); + EXPECT_TRUE(line2.isOnLine(point8)); + } + + TEST(LineTest, WhereOnTest) { + + Vector point0(0, 1, 1); + Vector point1(-1, 1, -1); + Vector point2(-1, 2, 0); + Line line0(point0, point1); + + Vector point3(1, 0, 1); + Vector point4(-1, -1, 1); + Vector point5(2, 1, 0); + Line line1(point3, point4); + + Vector point6(1, 1, 0); + Vector point7(1, -1, -1); + Vector point8(0, 2, 1); + Line line2(point6, point7); + + EXPECT_EQ(line0.whereOnLine(point0), 0); + EXPECT_THROW(line0.whereOnLine(point1), value_error); + EXPECT_EQ(line0.whereOnLine(point2), - 1 / norm); + + EXPECT_EQ(line1.whereOnLine(point3), 0); + EXPECT_THROW(line1.whereOnLine(point4), value_error); + EXPECT_EQ(line1.whereOnLine(point5), 1 / norm); + + EXPECT_EQ(line2.whereOnLine(point6), 0); + EXPECT_THROW(line2.whereOnLine(point7), value_error); + EXPECT_EQ(line2.whereOnLine(point8), - 1 / norm); + } -TEST(LineTest, WhereOnTest) { - cpp_utils::Vector point0(0, 1, 1); - cpp_utils::Vector point1(-1, 1, -1); - cpp_utils::Vector point2(-1, 2, 0); - cpp_utils::Line line0(point0, point1); - - cpp_utils::Vector point3(1, 0, 1); - cpp_utils::Vector point4(-1, -1, 1); - cpp_utils::Vector point5(2, 1, 0); - cpp_utils::Line line1(point3, point4); - - cpp_utils::Vector point6(1, 1, 0); - cpp_utils::Vector point7(1, -1, -1); - cpp_utils::Vector point8(0, 2, 1); - cpp_utils::Line line2(point6, point7); - - EXPECT_EQ(line0.whereOnLine(point0), 0); - EXPECT_THROW(line0.whereOnLine(point1), value_error); - EXPECT_EQ(line0.whereOnLine(point2), - 1 / norm); - - EXPECT_EQ(line1.whereOnLine(point3), 0); - EXPECT_THROW(line1.whereOnLine(point4), value_error); - EXPECT_EQ(line1.whereOnLine(point5), 1 / norm); - - EXPECT_EQ(line2.whereOnLine(point6), 0); - EXPECT_THROW(line2.whereOnLine(point7), value_error); - EXPECT_EQ(line2.whereOnLine(point8), - 1 / norm); } diff --git a/test/matrix_unittest.cpp b/test/matrix_unittest.cpp index bc37aac..03d728e 100644 --- a/test/matrix_unittest.cpp +++ b/test/matrix_unittest.cpp @@ -1,154 +1,146 @@ #include "gtest/gtest.h" #include "../source/matrix.hpp" -TEST(MatrixTest, DefaultInitTest) { - cpp_utils::Matrix iMat; - EXPECT_EQ(iMat.xx, 1); - EXPECT_EQ(iMat.xy, 0); - EXPECT_EQ(iMat.xz, 0); - EXPECT_EQ(iMat.yx, 0); - EXPECT_EQ(iMat.yy, 1); - EXPECT_EQ(iMat.yz, 0); - EXPECT_EQ(iMat.zx, 0); - EXPECT_EQ(iMat.zy, 0); - EXPECT_EQ(iMat.zz, 1); -} - -TEST(MatrixTest, InitTest) { - cpp_utils::Matrix Mat1(0, 1, 2, 3, 4, 5, 6, 7, 8); - EXPECT_EQ(Mat1.xx, 0); - EXPECT_EQ(Mat1.xy, 1); - EXPECT_EQ(Mat1.xz, 2); - EXPECT_EQ(Mat1.yx, 3); - EXPECT_EQ(Mat1.yy, 4); - EXPECT_EQ(Mat1.yz, 5); - EXPECT_EQ(Mat1.zx, 6); - EXPECT_EQ(Mat1.zy, 7); - EXPECT_EQ(Mat1.zz, 8); -} - -TEST(MatrixTest, DiagonalInitTest) { - cpp_utils::Matrix Mat1(1, 2, 3); - EXPECT_EQ(Mat1.xx, 1); - EXPECT_EQ(Mat1.xy, 0); - EXPECT_EQ(Mat1.xz, 0); - EXPECT_EQ(Mat1.yx, 0); - EXPECT_EQ(Mat1.yy, 2); - EXPECT_EQ(Mat1.yz, 0); - EXPECT_EQ(Mat1.zx, 0); - EXPECT_EQ(Mat1.zy, 0); - EXPECT_EQ(Mat1.zz, 3); -} - -TEST(MatrixTest, DetTest) { - cpp_utils::Matrix iMat; - cpp_utils::Matrix Mat1(2, 0, 0, 0, 2, 0, 0, 0, 2); - cpp_utils::Matrix Mat2(0, 1, 2, 3, 4, 5, 6, 7, 8); - - EXPECT_NEAR(iMat.det(), 1, 0.0001); - EXPECT_NEAR(Mat1.det(), 8, 0.0001); - EXPECT_NEAR(Mat2.det(), 0, 0.0001); -} - -TEST(MatrixTest, EqualityTest) { - cpp_utils::Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1); - cpp_utils::Matrix Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); - - EXPECT_TRUE(Mat1 == Mat1); - EXPECT_FALSE(Mat1 == Mat2); -} - -TEST(MatrixTest, NotEqualityTest) { - cpp_utils::Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1); - cpp_utils::Matrix Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); - - EXPECT_FALSE(Mat1 != Mat1); - EXPECT_TRUE(Mat1 != Mat2); -} - -TEST(MatrixTest, AdditionTest) { - cpp_utils::Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1); - cpp_utils::Matrix Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); - - cpp_utils::Matrix Exp1(1, 1, 1, 1, 1, 1, 1, 1, 1); - EXPECT_EQ(Mat1 + Mat2, Exp1); -} - -TEST(MatrixTest, SubtractionTest) { - cpp_utils::Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1); - cpp_utils::Matrix Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); - - cpp_utils::Matrix Exp1(1, -1, 1, -1, 1, -1, 1, -1, 1); - EXPECT_EQ(Mat1 - Mat2, Exp1); - EXPECT_EQ(Mat2 - Mat1, -Exp1); -} - -TEST(MatrixTest, NegativeTest) { - cpp_utils::Matrix Mat1(0, -1, 2, -3, 4, -5, 6, -7, 8); - cpp_utils::Matrix Mat2(0, 1, -2, 3, -4, 5, -6, 7, -8); - - EXPECT_EQ(-Mat1, Mat2); -} - -TEST(MatrixTest, MultiplicationTest) { - cpp_utils::Matrix Mat1(0, 1, 2, 3, 4, -1, -2, -3, -4); - - cpp_utils::Matrix Exp1(0, -2, -4, -6, -8, 2, 4, 6, 8); - EXPECT_EQ(Mat1 * int(-2), Exp1) << "Failed to multiply by negative int"; - EXPECT_EQ(Mat1 * float(-2), Exp1) << "Failed to multiply by negative float"; - EXPECT_EQ(int(-2) * Mat1, Exp1) << "Failed to multiply by negative int"; - EXPECT_EQ(float(-2) * Mat1, Exp1) << "Failed to multiply by negative float"; - - cpp_utils::Matrix Exp2(0.0, 0.5, 1.0, 1.5, 2.0, -0.5, -1.0, -1.5, -2.0); - cpp_utils::Matrix res = Mat1 * float(0.5); - EXPECT_EQ(Mat1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; - EXPECT_EQ(float(0.5) * Mat1, Exp2) << "Failed to multiply by decimal value"; -} - -TEST(MatrixTest, DivisionTest) { - cpp_utils::Matrix Mat1(0, 2, 4, 6, 8, -2, -4, -6, -8); - - cpp_utils::Matrix Exp1(0, 1, 2, 3, 4, -1, -2, -3, -4); - EXPECT_EQ(Mat1 / float(2), Exp1); - EXPECT_EQ(Mat1 / int(2), Exp1); -} - -TEST(MatrixTest, ZeroDivisionTest) { - cpp_utils::Matrix Mat1; - std::invalid_argument Excep("Division by zero is not possible"); -} - -TEST(MatrixTest, MatrixMultiplicationTest) { - cpp_utils::Matrix Mat1(1, 1, 0, 0, 1, 0, 0, 0, 0); - cpp_utils::Matrix Mat2(0, 1, 2, 3, 4, 5, 6, 7, 8); - cpp_utils::Matrix Mat3(3, 5, 7, 3, 4, 5, 0, 0, 0); - - EXPECT_TRUE(Mat1 * Mat2 == Mat3); - EXPECT_FALSE(Mat2 * Mat1 == Mat3); -} - -TEST(MatrixTest, VectorMultiplicationTest) { - cpp_utils::Matrix Mat1(1, 1, 0, 0, 1, 0, 0, 0, 0); - cpp_utils::Vector Vec1(0, 1, 2); - cpp_utils::Vector Vec2(1, 0, 2); - cpp_utils::Vector Vec3(1, 1, 0); - - EXPECT_TRUE(Mat1 * Vec1 == Vec3); - EXPECT_FALSE(Mat1 * Vec3 == Vec1); - EXPECT_TRUE(Vec2 * Mat1 == Vec3); - EXPECT_FALSE(Vec3 * Mat1 == Vec2); -} +namespace cpp_utils { + + TEST(MatrixTest, DefaultInitTest) { + Matrix iMat; + EXPECT_EQ(iMat.xx, 1); + EXPECT_EQ(iMat.xy, 0); + EXPECT_EQ(iMat.xz, 0); + EXPECT_EQ(iMat.yx, 0); + EXPECT_EQ(iMat.yy, 1); + EXPECT_EQ(iMat.yz, 0); + EXPECT_EQ(iMat.zx, 0); + EXPECT_EQ(iMat.zy, 0); + EXPECT_EQ(iMat.zz, 1); + } + + TEST(MatrixTest, InitTest) { + Matrix Mat1(0, 1, 2, 3, 4, 5, 6, 7, 8); + EXPECT_EQ(Mat1.xx, 0); + EXPECT_EQ(Mat1.xy, 1); + EXPECT_EQ(Mat1.xz, 2); + EXPECT_EQ(Mat1.yx, 3); + EXPECT_EQ(Mat1.yy, 4); + EXPECT_EQ(Mat1.yz, 5); + EXPECT_EQ(Mat1.zx, 6); + EXPECT_EQ(Mat1.zy, 7); + EXPECT_EQ(Mat1.zz, 8); + } + + TEST(MatrixTest, DiagonalInitTest) { + Matrix Mat1(1, 2, 3); + EXPECT_EQ(Mat1.xx, 1); + EXPECT_EQ(Mat1.xy, 0); + EXPECT_EQ(Mat1.xz, 0); + EXPECT_EQ(Mat1.yx, 0); + EXPECT_EQ(Mat1.yy, 2); + EXPECT_EQ(Mat1.yz, 0); + EXPECT_EQ(Mat1.zx, 0); + EXPECT_EQ(Mat1.zy, 0); + EXPECT_EQ(Mat1.zz, 3); + } + + TEST(MatrixTest, DetTest) { + Matrix iMat, Mat1(2, 0, 0, 0, 2, 0, 0, 0, 2), Mat2(0, 1, 2, 3, 4, 5, 6, 7, 8); + + EXPECT_NEAR(iMat.det(), 1, 0.0001); + EXPECT_NEAR(Mat1.det(), 8, 0.0001); + EXPECT_NEAR(Mat2.det(), 0, 0.0001); + } + + TEST(MatrixTest, EqualityTest) { + Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1), Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); + + EXPECT_TRUE(Mat1 == Mat1); + EXPECT_FALSE(Mat1 == Mat2); + } + + TEST(MatrixTest, NotEqualityTest) { + Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1), Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); + + EXPECT_FALSE(Mat1 != Mat1); + EXPECT_TRUE(Mat1 != Mat2); + } + + TEST(MatrixTest, AdditionTest) { + Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1), Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); + + Matrix Exp1(1, 1, 1, 1, 1, 1, 1, 1, 1); + EXPECT_EQ(Mat1 + Mat2, Exp1); + } + + TEST(MatrixTest, SubtractionTest) { + Matrix Mat1(1, 0, 1, 0, 1, 0, 1, 0, 1), Mat2(0, 1, 0, 1, 0, 1, 0, 1, 0); + + Matrix Exp1(1, -1, 1, -1, 1, -1, 1, -1, 1); + EXPECT_EQ(Mat1 - Mat2, Exp1); + EXPECT_EQ(Mat2 - Mat1, -Exp1); + } + + TEST(MatrixTest, NegativeTest) { + Matrix Mat1(0, -1, 2, -3, 4, -5, 6, -7, 8), Mat2(0, 1, -2, 3, -4, 5, -6, 7, -8); + + EXPECT_EQ(-Mat1, Mat2); + } + + TEST(MatrixTest, MultiplicationTest) { + Matrix Mat1(0, 1, 2, 3, 4, -1, -2, -3, -4); + + Matrix Exp1(0, -2, -4, -6, -8, 2, 4, 6, 8); + EXPECT_EQ(Mat1 * int(-2), Exp1) << "Failed to multiply by negative int"; + EXPECT_EQ(Mat1 * float(-2), Exp1) << "Failed to multiply by negative float"; + EXPECT_EQ(int(-2) * Mat1, Exp1) << "Failed to multiply by negative int"; + EXPECT_EQ(float(-2) * Mat1, Exp1) << "Failed to multiply by negative float"; + + Matrix Exp2(0.0, 0.5, 1.0, 1.5, 2.0, -0.5, -1.0, -1.5, -2.0); + EXPECT_EQ(Mat1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; + EXPECT_EQ(float(0.5) * Mat1, Exp2) << "Failed to multiply by decimal value"; + } + + TEST(MatrixTest, DivisionTest) { + Matrix Mat1(0, 2, 4, 6, 8, -2, -4, -6, -8); + + Matrix Exp1(0, 1, 2, 3, 4, -1, -2, -3, -4); + EXPECT_EQ(Mat1 / float(2), Exp1); + EXPECT_EQ(Mat1 / int(2), Exp1); + } + + TEST(MatrixTest, ZeroDivisionTest) { + Matrix Mat1; + std::invalid_argument Excep("Division by zero is not possible"); + } + + TEST(MatrixTest, MatrixMultiplicationTest) { + Matrix Mat1(1, 1, 0, 0, 1, 0, 0, 0, 0), Mat2(0, 1, 2, 3, 4, 5, 6, 7, 8), Mat3(3, 5, 7, 3, 4, 5, 0, 0, 0); + + EXPECT_TRUE(Mat1 * Mat2 == Mat3); + EXPECT_FALSE(Mat2 * Mat1 == Mat3); + } + + TEST(MatrixTest, VectorMultiplicationTest) { + Matrix Mat1(1, 1, 0, 0, 1, 0, 0, 0, 0); + Vector Vec1(0, 1, 2), Vec2(1, 0, 2), Vec3(1, 1, 0); + + EXPECT_TRUE(Mat1 * Vec1 == Vec3); + EXPECT_FALSE(Mat1 * Vec3 == Vec1); + EXPECT_TRUE(Vec2 * Mat1 == Vec3); + EXPECT_FALSE(Vec3 * Mat1 == Vec2); + } + + TEST(MatrixTest, InverseTest) { + Matrix Mat1(0, 1, 2, 3, 4, 5, 6, 7, 8), + Mat2(0, 0, 0, 0, 0, 0, 0, 0, 0), + Mat3(0, 0, -1, 0, 1, 0, 1, 0, 0), + Mat4(0, 0, 1, 0, 1, 0, -1, 0, 0), + Mat5(0, 0, 1, 0, 1, 0, 1, 0, 0); + + EXPECT_THROW(Mat1.inverse(), value_error); + EXPECT_THROW(Mat2.inverse(), value_error); + EXPECT_TRUE(Mat3.inverse() == Mat4); + EXPECT_TRUE(Mat4.inverse() == Mat3); + EXPECT_TRUE(Mat5.inverse() == Mat5); + } -TEST(MatrixTest, InverseTest) { - cpp_utils::Matrix Mat1(0, 1, 2, 3, 4, 5, 6, 7, 8); - cpp_utils::Matrix Mat2(0, 0, 0, 0, 0, 0, 0, 0, 0); - cpp_utils::Matrix Mat3(0, 0, -1, 0, 1, 0, 1, 0, 0); - cpp_utils::Matrix Mat4(0, 0, 1, 0, 1, 0, -1, 0, 0); - cpp_utils::Matrix Mat5(0, 0, 1, 0, 1, 0, 1, 0, 0); - - EXPECT_THROW(Mat1.inverse(), value_error); - EXPECT_THROW(Mat2.inverse(), value_error); - EXPECT_TRUE(Mat3.inverse() == Mat4); - EXPECT_TRUE(Mat4.inverse() == Mat3); - EXPECT_TRUE(Mat5.inverse() == Mat5); } diff --git a/test/plane_unittest.cpp b/test/plane_unittest.cpp index 3beba7a..4df07bf 100644 --- a/test/plane_unittest.cpp +++ b/test/plane_unittest.cpp @@ -1,169 +1,131 @@ #include "gtest/gtest.h" #include "../source/plane.hpp" -TEST(PlaneTest, DefaultInitTest) { - cpp_utils::Vector point(0, 0, 0); - cpp_utils::Vector normal(norm, norm, norm); - cpp_utils::Plane plane; - EXPECT_TRUE(plane.point == point); - EXPECT_TRUE(plane.normal == normal); -} +namespace cpp_utils { -TEST(PlaneTest, InitTest) { - cpp_utils::Vector point(1, 2, 3); - cpp_utils::Vector normal(4, 5, 6); - cpp_utils::Plane plane(point, normal); - EXPECT_TRUE(plane.point == point); - EXPECT_TRUE(plane.normal == normal.normalise()); -} + TEST(PlaneTest, DefaultInitTest) { + Vector point(0, 0, 0), normal(norm, norm, norm); + Plane plane; + EXPECT_TRUE(plane.point == point); + EXPECT_TRUE(plane.normal == normal); + } -TEST(PlaneTest, EqualityTest) { - cpp_utils::Vector vec00; - cpp_utils::Vector vec01(1, 0, 0); - cpp_utils::Vector vec02(0, 1, 0); - cpp_utils::Vector vec03(0, 0, 1); - cpp_utils::Vector vec04(-1, 0, 0); - cpp_utils::Vector vec05(0, -1, 0); - cpp_utils::Vector vec06(0, 0, -1); - cpp_utils::Vector vec07(1, 1, 1); - cpp_utils::Vector vec08(1, 0, -1); - cpp_utils::Vector vec09(-1, 1, 0); - cpp_utils::Vector vec10(0, -1, 1); - cpp_utils::Vector vec11(-1, 0, 1); - cpp_utils::Vector vec12(1, -1, 0); - cpp_utils::Vector vec13(0, 1, -1); - cpp_utils::Vector vec14(-1, -1, -1); - cpp_utils::Plane plane00; - cpp_utils::Plane plane01(vec06, vec01); - cpp_utils::Plane plane02(vec04, vec02); - cpp_utils::Plane plane03(vec05, vec03); - cpp_utils::Plane plane04(vec02, vec04); - cpp_utils::Plane plane05(vec03, vec05); - cpp_utils::Plane plane06(vec01, vec06); - cpp_utils::Plane plane07(vec00, vec07); - cpp_utils::Plane plane08(vec02, vec08); - cpp_utils::Plane plane09(vec03, vec09); - cpp_utils::Plane plane10(vec01, vec10); - cpp_utils::Plane plane11(vec05, vec11); - cpp_utils::Plane plane12(vec06, vec12); - cpp_utils::Plane plane13(vec04, vec13); - cpp_utils::Plane plane14(vec00, vec14); - - EXPECT_TRUE(plane00 == plane00); - EXPECT_FALSE(plane00 == plane01); - EXPECT_TRUE(plane01 == plane04); - EXPECT_FALSE(plane00 == plane02); - EXPECT_TRUE(plane02 == plane05); - EXPECT_FALSE(plane00 == plane03); - EXPECT_TRUE(plane03 == plane06); - EXPECT_TRUE(plane00 == plane07); - EXPECT_TRUE(plane07 == plane14); - EXPECT_FALSE(plane00 == plane08); - EXPECT_TRUE(plane08 == plane11); - EXPECT_FALSE(plane00 == plane09); - EXPECT_TRUE(plane09 == plane12); - EXPECT_FALSE(plane00 == plane10); - EXPECT_TRUE(plane10 == plane13); -} + TEST(PlaneTest, InitTest) { + Vector point(1, 2, 3), normal(4, 5, 6); + Plane plane(point, normal); + EXPECT_TRUE(plane.point == point); + EXPECT_TRUE(plane.normal == normal.normalise()); + } -TEST(PlaneTest, NotEqualityTest) { - cpp_utils::Vector vec00; - cpp_utils::Vector vec01(1, 0, 0); - cpp_utils::Vector vec02(0, 1, 0); - cpp_utils::Vector vec03(0, 0, 1); - cpp_utils::Vector vec04(-1, 0, 0); - cpp_utils::Vector vec05(0, -1, 0); - cpp_utils::Vector vec06(0, 0, -1); - cpp_utils::Vector vec07(1, 1, 1); - cpp_utils::Vector vec08(1, 0, -1); - cpp_utils::Vector vec09(-1, 1, 0); - cpp_utils::Vector vec10(0, -1, 1); - cpp_utils::Vector vec11(-1, 0, 1); - cpp_utils::Vector vec12(1, -1, 0); - cpp_utils::Vector vec13(0, 1, -1); - cpp_utils::Vector vec14(-1, -1, -1); - cpp_utils::Plane plane00; - cpp_utils::Plane plane01(vec06, vec01); - cpp_utils::Plane plane02(vec04, vec02); - cpp_utils::Plane plane03(vec05, vec03); - cpp_utils::Plane plane04(vec02, vec04); - cpp_utils::Plane plane05(vec03, vec05); - cpp_utils::Plane plane06(vec01, vec06); - cpp_utils::Plane plane07(vec00, vec07); - cpp_utils::Plane plane08(vec02, vec08); - cpp_utils::Plane plane09(vec03, vec09); - cpp_utils::Plane plane10(vec01, vec10); - cpp_utils::Plane plane11(vec05, vec11); - cpp_utils::Plane plane12(vec06, vec12); - cpp_utils::Plane plane13(vec04, vec13); - cpp_utils::Plane plane14(vec00, vec14); - - EXPECT_FALSE(plane00 != plane00); - EXPECT_TRUE(plane00 != plane01); - EXPECT_FALSE(plane01 != plane04); - EXPECT_TRUE(plane00 != plane02); - EXPECT_FALSE(plane02 != plane05); - EXPECT_TRUE(plane00 != plane03); - EXPECT_FALSE(plane03 != plane06); - EXPECT_FALSE(plane00 != plane07); - EXPECT_FALSE(plane07 != plane14); - EXPECT_TRUE(plane00 != plane08); - EXPECT_FALSE(plane08 != plane11); - EXPECT_TRUE(plane00 != plane09); - EXPECT_FALSE(plane09 != plane12); - EXPECT_TRUE(plane00 != plane10); - EXPECT_FALSE(plane10 != plane13); -} + TEST(PlaneTest, EqualityTest) { -TEST(PlaneTest, IsOnTest) { - cpp_utils::Vector point0(0, 1, 1); - cpp_utils::Vector point1(1, 1, 1); - cpp_utils::Vector point2(1, 0, 1); - cpp_utils::Plane plane(point0, point1); + Vector vec00, + vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), + vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), + vec07(1, 1, 1), + vec08(1, 0, -1), vec09(-1, 1, 0), vec10(0, -1, 1), + vec11(-1, 0, 1), vec12(1, -1, 0), vec13(0, 1, -1), + vec14(-1, -1, -1); - EXPECT_TRUE(plane.isOnPlane(point0)); - EXPECT_FALSE(plane.isOnPlane(point1)); - EXPECT_TRUE(plane.isOnPlane(point2)); -} + Plane plane00, + plane01(vec06, vec01), plane02(vec04, vec02), plane03(vec05, vec03), + plane04(vec02, vec04), plane05(vec03, vec05), plane06(vec01, vec06), + plane07(vec00, vec07), + plane08(vec02, vec08), plane09(vec03, vec09), plane10(vec01, vec10), + plane11(vec05, vec11), plane12(vec06, vec12), plane13(vec04, vec13), + plane14(vec00, vec14); -TEST(PlaneTest, IntersectionTest) { - cpp_utils::Vector point0; - cpp_utils::Vector point1(1, -1, 1); - cpp_utils::Vector point2(1, 0, -1); - cpp_utils::Line line1(point0, point1); - cpp_utils::Line line2(point1, point1); - cpp_utils::Line line3(point0, point2); - cpp_utils::Line line4(point1, point2); - cpp_utils::Plane plane; - - EXPECT_EQ(plane.intersection(line1), point0); - EXPECT_EQ(plane.intersection(line2), point0); - EXPECT_THROW(plane.intersection(line3), value_error); - EXPECT_THROW(plane.intersection(line4), value_error); -} + EXPECT_TRUE(plane00 == plane00); + EXPECT_FALSE(plane00 == plane01); + EXPECT_TRUE(plane01 == plane04); + EXPECT_FALSE(plane00 == plane02); + EXPECT_TRUE(plane02 == plane05); + EXPECT_FALSE(plane00 == plane03); + EXPECT_TRUE(plane03 == plane06); + EXPECT_TRUE(plane00 == plane07); + EXPECT_TRUE(plane07 == plane14); + EXPECT_FALSE(plane00 == plane08); + EXPECT_TRUE(plane08 == plane11); + EXPECT_FALSE(plane00 == plane09); + EXPECT_TRUE(plane09 == plane12); + EXPECT_FALSE(plane00 == plane10); + EXPECT_TRUE(plane10 == plane13); + } + + TEST(PlaneTest, NotEqualityTest) { + + Vector vec00, + vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), + vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), + vec07(1, 1, 1), + vec08(1, 0, -1), vec09(-1, 1, 0), vec10(0, -1, 1), + vec11(-1, 0, 1), vec12(1, -1, 0), vec13(0, 1, -1), + vec14(-1, -1, -1); + + Plane plane00, + plane01(vec06, vec01), plane02(vec04, vec02), plane03(vec05, vec03), + plane04(vec02, vec04), plane05(vec03, vec05), plane06(vec01, vec06), + plane07(vec00, vec07), + plane08(vec02, vec08), plane09(vec03, vec09), plane10(vec01, vec10), + plane11(vec05, vec11), plane12(vec06, vec12), plane13(vec04, vec13), + plane14(vec00, vec14); + + EXPECT_FALSE(plane00 != plane00); + EXPECT_TRUE(plane00 != plane01); + EXPECT_FALSE(plane01 != plane04); + EXPECT_TRUE(plane00 != plane02); + EXPECT_FALSE(plane02 != plane05); + EXPECT_TRUE(plane00 != plane03); + EXPECT_FALSE(plane03 != plane06); + EXPECT_FALSE(plane00 != plane07); + EXPECT_FALSE(plane07 != plane14); + EXPECT_TRUE(plane00 != plane08); + EXPECT_FALSE(plane08 != plane11); + EXPECT_TRUE(plane00 != plane09); + EXPECT_FALSE(plane09 != plane12); + EXPECT_TRUE(plane00 != plane10); + EXPECT_FALSE(plane10 != plane13); + } + + TEST(PlaneTest, IsOnTest) { + Vector point0(0, 1, 1), point1(1, 1, 1), point2(1, 0, 1); + Plane plane(point0, point1); + + EXPECT_TRUE(plane.isOnPlane(point0)); + EXPECT_FALSE(plane.isOnPlane(point1)); + EXPECT_TRUE(plane.isOnPlane(point2)); + } + + TEST(PlaneTest, IntersectionTest) { + Vector point0, point1(1, -1, 1), point2(1, 0, -1); + Line line1(point0, point1), line2(point1, point1), + line3(point0, point2), line4(point1, point2); + Plane plane; + + EXPECT_EQ(plane.intersection(line1), point0); + EXPECT_EQ(plane.intersection(line2), point0); + EXPECT_THROW(plane.intersection(line3), value_error); + EXPECT_THROW(plane.intersection(line4), value_error); + } + + TEST(PlaneTest, BetweenTest) { + Vector point0, + point1(1, 1, 1), + point2(1, 0, -1), + point3(0.5, 0.5, 0.5), + point4(-0.5, -0.5, -0.5), + point5(1.5, 1.5, 1.5); + + Line line0, line1(point2, point2), line2(point3, point2), line3(point4, point2), line4(point5, point2); + + Plane plane0, plane1(point1, point1), plane2(point0, point2); + + EXPECT_FALSE(cpp_utils::between(line1, plane0, plane0)); + EXPECT_TRUE(cpp_utils::between(line2, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line3, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line4, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line0, plane0, plane1)); + EXPECT_FALSE(cpp_utils::between(line0, plane0, plane2)); + } -TEST(PlaneTest, BetweenTest) { - cpp_utils::Vector point0; - cpp_utils::Vector point1(1, 1, 1); - cpp_utils::Vector point2(1, 0, -1); - cpp_utils::Vector point3(0.5, 0.5, 0.5); - cpp_utils::Vector point4(-0.5, -0.5, -0.5); - cpp_utils::Vector point5(1.5, 1.5, 1.5); - - cpp_utils::Line line0; - cpp_utils::Line line1(point2, point2); - cpp_utils::Line line2(point3, point2); - cpp_utils::Line line3(point4, point2); - cpp_utils::Line line4(point5, point2); - - cpp_utils::Plane plane0; - cpp_utils::Plane plane1(point1, point1); - cpp_utils::Plane plane2(point0, point2); - - EXPECT_FALSE(cpp_utils::between(line1, plane0, plane0)); - EXPECT_TRUE(cpp_utils::between(line2, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line3, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line4, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line0, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line0, plane0, plane2)); } diff --git a/test/quaternion_unittest.cpp b/test/quaternion_unittest.cpp index 36286be..2b8bc32 100644 --- a/test/quaternion_unittest.cpp +++ b/test/quaternion_unittest.cpp @@ -2,165 +2,165 @@ #include "../source/quaternion.hpp" #include "testing.hpp" -namespace cpp_utils { - double pi = 3.1415926535; -TEST(QuaternionTest, DefaultInitTest) { - Quaternion quat; - EXPECT_EQ(quat.w, 0); - EXPECT_EQ(quat.x, 0); - EXPECT_EQ(quat.y, 0); - EXPECT_EQ(quat.z, 0); -} +namespace cpp_utils { -TEST(QuaternionTest, InitTest) { - Quaternion quat(1, 2, 3, 4); - EXPECT_EQ(quat.w, 1); - EXPECT_EQ(quat.x, 2); - EXPECT_EQ(quat.y, 3); - EXPECT_EQ(quat.z, 4); -} + TEST(QuaternionTest, DefaultInitTest) { + Quaternion quat; + EXPECT_EQ(quat.w, 0); + EXPECT_EQ(quat.x, 0); + EXPECT_EQ(quat.y, 0); + EXPECT_EQ(quat.z, 0); + } -TEST(QuaternionTest, InitTestAngleVec) { - Quaternion quat(pi/2, {1, 0, 0}); + TEST(QuaternionTest, InitTest) { + Quaternion quat(1, 2, 3, 4); + EXPECT_EQ(quat.w, 1); + EXPECT_EQ(quat.x, 2); + EXPECT_EQ(quat.y, 3); + EXPECT_EQ(quat.z, 4); + } - EXPECT_EQ(quat, Quaternion(cos(pi/4), sin(pi/4), 0, 0)); -} + TEST(QuaternionTest, InitTestAngleVec) { + Quaternion quat(pi/2, {1, 0, 0}); -TEST(QuaternionTest, ModTest) { - Quaternion quat1(1, 1, 1, 1); - EXPECT_NEAR(quat1.mod(), sqrt(4), 0.0001); -} + EXPECT_EQ(quat, Quaternion(cos(pi/4), sin(pi/4), 0, 0)); + } -TEST(QuaternionTest, EqualityTest) { - Quaternion quat1(1, 0, 1, 1); - Quaternion quat2(0, 1, 0, 1); + TEST(QuaternionTest, ModTest) { + Quaternion quat1(1, 1, 1, 1); + EXPECT_NEAR(quat1.mod(), sqrt(4), 0.0001); + } - EXPECT_TRUE(quat1 == quat1); - EXPECT_FALSE(quat1 == quat2); -} + TEST(QuaternionTest, EqualityTest) { + Quaternion quat1(1, 0, 1, 1); + Quaternion quat2(0, 1, 0, 1); -TEST(QuaternionTest, NotEqualityTest) { - Quaternion quat1(1, 0, 1, 1); - Quaternion quat2(0, 1, 0, 1); + EXPECT_TRUE(quat1 == quat1); + EXPECT_FALSE(quat1 == quat2); + } - EXPECT_FALSE(quat1 != quat1); - EXPECT_TRUE(quat1 != quat2); -} + TEST(QuaternionTest, NotEqualityTest) { + Quaternion quat1(1, 0, 1, 1); + Quaternion quat2(0, 1, 0, 1); -TEST(QuaternionTest, NegativeTest) { - Quaternion quat1(1, -2, 3, 4); - Quaternion quat2(-1, 2, -3, -4); + EXPECT_FALSE(quat1 != quat1); + EXPECT_TRUE(quat1 != quat2); + } - EXPECT_EQ(-quat1, quat2); -} + TEST(QuaternionTest, NegativeTest) { + Quaternion quat1(1, -2, 3, 4); + Quaternion quat2(-1, 2, -3, -4); -TEST(QuaternionTest, MultiplicationTest) { - Quaternion quat1(1, 2, -1, 3); + EXPECT_EQ(-quat1, quat2); + } - Quaternion Exp1(-2, -4, 2, -6); - EXPECT_EQ(quat1 * int(-2), Exp1) << "Failed to multiply by negative int"; - EXPECT_EQ(quat1 * float(-2), Exp1) << "Failed to multiply by negative float"; + TEST(QuaternionTest, MultiplicationTest) { + Quaternion quat1(1, 2, -1, 3); - Quaternion Exp2(0.5, 1.0, -0.5, 1.5); - Quaternion res = quat1 * float(0.5); - EXPECT_EQ( quat1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; -} + Quaternion Exp1(-2, -4, 2, -6); + EXPECT_EQ(quat1 * int(-2), Exp1) << "Failed to multiply by negative int"; + EXPECT_EQ(quat1 * float(-2), Exp1) << "Failed to multiply by negative float"; -TEST(QuaternionTest, DivisionTest) { - Quaternion quat1(2, 4, -2, 3); + Quaternion Exp2(0.5, 1.0, -0.5, 1.5); + Quaternion res = quat1 * float(0.5); + EXPECT_EQ( quat1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; + } - Quaternion Exp1(1, 2, -1, 1.5); - EXPECT_EQ(quat1 / float(2), Exp1); - EXPECT_EQ(quat1 / int(2), Exp1); -} + TEST(QuaternionTest, DivisionTest) { + Quaternion quat1(2, 4, -2, 3); -TEST(QuaternionTest, AssignmentTest) { - Quaternion quat1 = {1, {2, 3, 4}}; - Quaternion quat2; - quat2 = quat1; + Quaternion Exp1(1, 2, -1, 1.5); + EXPECT_EQ(quat1 / float(2), Exp1); + EXPECT_EQ(quat1 / int(2), Exp1); + } - EXPECT_TRUE(quat1 == quat2); -} + TEST(QuaternionTest, AssignmentTest) { + Quaternion quat1 = {1, {2, 3, 4}}; + Quaternion quat2; + quat2 = quat1; -TEST(QuaternionTest, productTest) { - Quaternion quat1 = {1, 0, 0, 0}; - Quaternion quat2 = {0, 2, 3, -6}; + EXPECT_TRUE(quat1 == quat2); + } - EXPECT_EQ(quat1 * quat2, quat2); - EXPECT_EQ(quat2 * quat1, quat2); + TEST(QuaternionTest, productTest) { + Quaternion quat1 = {1, 0, 0, 0}; + Quaternion quat2 = {0, 2, 3, -6}; - Quaternion quat3 = {1, 2, 4, 7}; - Quaternion quat4 = {6, 2, 3, -6}; - Quaternion exp4 = {32, -31, 53, 34}; + EXPECT_EQ(quat1 * quat2, quat2); + EXPECT_EQ(quat2 * quat1, quat2); - EXPECT_EQ(quat3 * quat4, exp4); + Quaternion quat3 = {1, 2, 4, 7}; + Quaternion quat4 = {6, 2, 3, -6}; + Quaternion exp4 = {32, -31, 53, 34}; - Quaternion quat5 = {1, 0, 1, 0}; - Quaternion quat6 = {0, 2, 3, -6}; - Quaternion exp6 = {3, -4, 3, -8}; + EXPECT_EQ(quat3 * quat4, exp4); - EXPECT_EQ(quat5 * quat6, exp6); -} + Quaternion quat5 = {1, 0, 1, 0}; + Quaternion quat6 = {0, 2, 3, -6}; + Quaternion exp6 = {3, -4, 3, -8}; -TEST(QuaternionTest, VectorMultiplication1) { - Quaternion quat = {1, 0, 0, 0}; - Vector vec = {2, 3, 4}; - Quaternion exp = {0, 2, 3, 4}; + EXPECT_EQ(quat5 * quat6, exp6); + } - EXPECT_EQ(exp, quat.conj() * vec * quat); -} + TEST(QuaternionTest, VectorMultiplication1) { + Quaternion quat = {1, 0, 0, 0}; + Vector vec = {2, 3, 4}; + Quaternion exp = {0, 2, 3, 4}; -TEST(QuaternionTest, VectorMultiplication2) { - Quaternion quat = {cos(pi / 4), 0, 0, sin(pi / 4)}; - Vector vec = {1, 0, 0}; - Quaternion exp = {0, 0, -1, 0}; + EXPECT_EQ(exp, quat.conj() * vec * quat); + } - EXPECT_EQ(exp, quat.conj() * vec * quat); -} + TEST(QuaternionTest, VectorMultiplication2) { + Quaternion quat = {cos(pi / 4), 0, 0, sin(pi / 4)}; + Vector vec = {1, 0, 0}; + Quaternion exp = {0, 0, -1, 0}; -TEST(QuaternionTest, VectorMultiplication3) { - Quaternion quat = {cos(pi / 4), sin(pi / 4), 0, 0}; - Vector vec = {0, 1, -3}; - Quaternion exp = {0, 0, -3, -1}; + EXPECT_EQ(exp, quat.conj() * vec * quat); + } - EXPECT_EQ(exp, quat.conj() * vec * quat); -} + TEST(QuaternionTest, VectorMultiplication3) { + Quaternion quat = {cos(pi / 4), sin(pi / 4), 0, 0}; + Vector vec = {0, 1, -3}; + Quaternion exp = {0, 0, -3, -1}; -TEST(QuaternionTest, VectorMultiplication4) { - Quaternion quat = {cos(pi / 4), 0, sin(pi / 4), 0}; - Vector vec = {1, 0, 2}; - Quaternion exp = {0, -2, 0, 1}; + EXPECT_EQ(exp, quat.conj() * vec * quat); + } - EXPECT_EQ(exp, quat.conj() * vec * quat); -}; + TEST(QuaternionTest, VectorMultiplication4) { + Quaternion quat = {cos(pi / 4), 0, sin(pi / 4), 0}; + Vector vec = {1, 0, 2}; + Quaternion exp = {0, -2, 0, 1}; + EXPECT_EQ(exp, quat.conj() * vec * quat); + }; -TEST(QuaternionTest, VectorRotation1) { - Vector vec = {1, 0, 0}; - Quaternion quat = {cos(pi/4), 0, 0, sin(pi/4)}; - Vector exp = {0, -1, 0}; - EXPECT_EQ(exp, rotate(vec, quat)); -} + TEST(QuaternionTest, VectorRotation1) { + Vector vec = {1, 0, 0}; + Quaternion quat = {cos(pi/4), 0, 0, sin(pi/4)}; + Vector exp = {0, -1, 0}; -TEST(QuaternionTest, VectorRotation2) { - Vector vec = {1, 0, 0}; - Quaternion quat = {pi/2, {0, 0, 1}}; - Vector exp = {0, -1, 0}; + EXPECT_EQ(exp, rotate(vec, quat)); + } - EXPECT_EQ(exp, rotate(vec, quat)); -} + TEST(QuaternionTest, VectorRotation2) { + Vector vec = {1, 0, 0}; + Quaternion quat = {pi/2, {0, 0, 1}}; + Vector exp = {0, -1, 0}; -TEST(QuaternionTest, VectorRotation3) { - Vector vec = {1, 0, 0}; - Quaternion quat1 = {pi/2, {1, 0, 0}}; - Quaternion quat2 = {pi/2.6, {1, 0, 0}}; - Vector exp = {1, 0, 0}; + EXPECT_EQ(exp, rotate(vec, quat)); + } - EXPECT_EQ(exp, rotate(vec, quat1)); - EXPECT_EQ(exp, rotate(vec, quat2)); -}; + TEST(QuaternionTest, VectorRotation3) { + Vector vec = {1, 0, 0}; + Quaternion quat1 = {pi/2, {1, 0, 0}}; + Quaternion quat2 = {pi/2.6, {1, 0, 0}}; + Vector exp = {1, 0, 0}; -} \ No newline at end of file + EXPECT_EQ(exp, rotate(vec, quat1)); + EXPECT_EQ(exp, rotate(vec, quat2)); + }; + +} diff --git a/test/vector_unittest.cpp b/test/vector_unittest.cpp index 7b105d3..d8e944c 100644 --- a/test/vector_unittest.cpp +++ b/test/vector_unittest.cpp @@ -1,107 +1,112 @@ #include "gtest/gtest.h" #include "../source/vector.hpp" -TEST(VectorTest, DefaultInitTest) { - cpp_utils::Vector NullVec; - EXPECT_EQ(NullVec.x, 0); - EXPECT_EQ(NullVec.y, 0); - EXPECT_EQ(NullVec.z, 0); -} - -TEST(VectorTest, InitTest) { - cpp_utils::Vector Vec1(1, 2, 3); - EXPECT_EQ(Vec1.x, 1); - EXPECT_EQ(Vec1.y, 2); - EXPECT_EQ(Vec1.z, 3); -} - -TEST(VectorTest, ModTest) { - cpp_utils::Vector Vec1(1, 1, 1); - EXPECT_NEAR(Vec1.mod(), sqrt(3), 0.0001); -} - -TEST(VectorTest, EqualityTest) { - cpp_utils::Vector Vec1(1, 0, 1); - cpp_utils::Vector Vec2(0, 1, 0); - - EXPECT_TRUE(Vec1 == Vec1); - EXPECT_FALSE(Vec1 == Vec2); -} - -TEST(VectorTest, NotEqualityTest) { - cpp_utils::Vector Vec1(1, 0, 1); - cpp_utils::Vector Vec2(0, 1, 0); - - EXPECT_FALSE(Vec1 != Vec1); - EXPECT_TRUE(Vec1 != Vec2); -} - -TEST(VectorTest, AdditionTest) { - cpp_utils::Vector Vec1(1, 0, 1); - cpp_utils::Vector Vec2(0, 1, 0); - - cpp_utils::Vector ExpVec(1, 1, 1); - - EXPECT_EQ(Vec1 + Vec2, ExpVec); -} - -TEST(VectorTest, SubtractionTest) { - cpp_utils::Vector Vec1(1, 0, 1); - cpp_utils::Vector Vec2(0, 1, 1); - - cpp_utils::Vector ExpVec(1, -1, 0); - EXPECT_EQ(Vec1 - Vec2, ExpVec); - EXPECT_EQ(Vec2 - Vec1, -ExpVec); -} - -TEST(VectorTest, NegativeTest) { - cpp_utils::Vector Vec1(1, -2, 3); - cpp_utils::Vector Vec2(-1, 2, -3); - - EXPECT_EQ(-Vec1, Vec2); -} - -TEST(VectorTest, MultiplicationTest) { - cpp_utils::Vector Vec1(1, 2, -1); - - cpp_utils::Vector Exp1(-2, -4, 2); - EXPECT_EQ(Vec1 * int(-2), Exp1) << "Failed to multiply by negative int"; - EXPECT_EQ(Vec1 * float(-2), Exp1) << "Failed to multiply by negative float"; - - cpp_utils::Vector Exp2(0.5, 1.0, -0.5); - cpp_utils::Vector res = Vec1 * float(0.5); - EXPECT_EQ( Vec1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; -} - -TEST(VectorTest, DivisionTest) { - cpp_utils::Vector Vec1(2, 4, -2); - - cpp_utils::Vector Exp1(1, 2, -1); - EXPECT_EQ(Vec1 / float(2), Exp1); - EXPECT_EQ(Vec1 / int(2), Exp1); -} - -TEST(VectorTest, ZeroDivisionTest) { - cpp_utils::Vector Vec1; - std::invalid_argument Excep("Division by zero is not possible"); - -} - -TEST(VectorTest, DotProductTest) { - cpp_utils::Vector Vec1(1, 0, 0); - cpp_utils::Vector Vec2(0, 1, 2); - cpp_utils::Vector Vec3(2, 0, 3); - - EXPECT_NEAR(Vec1 * Vec2, 0, 0.0001); - EXPECT_NEAR(Vec1 * Vec3, 2, 0.0001); - EXPECT_NEAR(Vec2 * Vec3, 6, 0.0001); -} - -TEST(VectorTest, CrossProductTest) { - cpp_utils::Vector Vecx(1, 0, 0); - cpp_utils::Vector Vecy(0, 1, 0); - cpp_utils::Vector Vecz(0, 0, 1); +namespace cpp_utils { + + TEST(VectorTest, DefaultInitTest) { + Vector NullVec; + EXPECT_EQ(NullVec.x, 0); + EXPECT_EQ(NullVec.y, 0); + EXPECT_EQ(NullVec.z, 0); + } + + TEST(VectorTest, InitTest) { + Vector Vec1(1, 2, 3); + EXPECT_EQ(Vec1.x, 1); + EXPECT_EQ(Vec1.y, 2); + EXPECT_EQ(Vec1.z, 3); + } + + TEST(VectorTest, ModTest) { + Vector Vec1(1, 1, 1); + EXPECT_NEAR(Vec1.mod(), sqrt(3), 0.0001); + } + + TEST(VectorTest, EqualityTest) { + Vector Vec1(1, 0, 1); + Vector Vec2(0, 1, 0); + + EXPECT_TRUE(Vec1 == Vec1); + EXPECT_FALSE(Vec1 == Vec2); + } + + TEST(VectorTest, NotEqualityTest) { + Vector Vec1(1, 0, 1); + Vector Vec2(0, 1, 0); + + EXPECT_FALSE(Vec1 != Vec1); + EXPECT_TRUE(Vec1 != Vec2); + } + + TEST(VectorTest, AdditionTest) { + Vector Vec1(1, 0, 1); + Vector Vec2(0, 1, 0); + + Vector ExpVec(1, 1, 1); + EXPECT_EQ(Vec1 + Vec2, ExpVec); + } + + TEST(VectorTest, SubtractionTest) { + Vector Vec1(1, 0, 1); + Vector Vec2(0, 1, 1); + + Vector ExpVec(1, -1, 0); + EXPECT_EQ(Vec1 - Vec2, ExpVec); + EXPECT_EQ(Vec2 - Vec1, -ExpVec); + } + + TEST(VectorTest, NegativeTest) { + Vector Vec1(1, -2, 3); + Vector Vec2(-1, 2, -3); + + EXPECT_EQ(-Vec1, Vec2); + } + + TEST(VectorTest, MultiplicationTest) { + Vector Vec1(1, 2, -1); + + Vector Exp1(-2, -4, 2); + EXPECT_EQ(Vec1 * int(-2), Exp1) << "Failed to multiply by negative int"; + EXPECT_EQ(int(-2) * Vec1, Exp1) << "Failed to multiply by negative int"; + EXPECT_EQ(Vec1 * float(-2), Exp1) << "Failed to multiply by negative float"; + EXPECT_EQ(float(-2) * Vec1, Exp1) << "Failed to multiply by negative float"; + + Vector Exp2(0.5, 1.0, -0.5); + EXPECT_EQ(Vec1 * float(0.5), Exp2) << "Failed to multiply by decimal value"; + EXPECT_EQ(float(0.5) * Vec1, Exp2) << "Failed to multiply by decimal value"; + } + + TEST(VectorTest, DivisionTest) { + Vector Vec1(2, 4, -2); + + Vector Exp1(1, 2, -1); + EXPECT_EQ(Vec1 / float(2), Exp1); + EXPECT_EQ(Vec1 / int(2), Exp1); + } + + TEST(VectorTest, ZeroDivisionTest) { + Vector Vec1; + std::invalid_argument Excep("Division by zero is not possible"); + + } + + TEST(VectorTest, DotProductTest) { + Vector Vec1(1, 0, 0); + Vector Vec2(0, 1, 2); + Vector Vec3(2, 0, 3); + + EXPECT_NEAR(Vec1 * Vec2, 0, 0.0001); + EXPECT_NEAR(Vec1 * Vec3, 2, 0.0001); + EXPECT_NEAR(Vec2 * Vec3, 6, 0.0001); + } + + TEST(VectorTest, CrossProductTest) { + Vector Vecx(1, 0, 0); + Vector Vecy(0, 1, 0); + Vector Vecz(0, 0, 1); + + EXPECT_EQ(Vecx ^ Vecy, Vecz); + EXPECT_EQ(Vecy ^ Vecx, -Vecz); + } - EXPECT_EQ(Vecx ^ Vecy, Vecz); - EXPECT_EQ(Vecy ^ Vecx, -Vecz); } From 392297ef5b06cabe7962b913dd356425c35f8af9 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Fri, 22 Aug 2025 11:37:29 +0100 Subject: [PATCH 10/15] isBetween --- source/plane.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/plane.cpp b/source/plane.cpp index 1043480..b75de94 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -34,7 +34,7 @@ namespace cpp_utils { } }; - bool between(const Line line, const Plane &plane1, const Plane &plane2) { + bool isBetween(const Line line, const Plane &plane1, const Plane &plane2) { if (plane1.normal != plane2.normal || line.direction * plane1.normal != 0) { return false; } else { From 869dbcef9729760e15d49bc076c45a1a13c5de1c Mon Sep 17 00:00:00 2001 From: Ycidia Date: Fri, 22 Aug 2025 17:14:51 +0100 Subject: [PATCH 11/15] Basic docstrings for methods. --- source/line.cpp | 6 ++++++ source/plane.cpp | 11 ++++++++++- source/plane.hpp | 2 +- test/plane_unittest.cpp | 14 +++++++------- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/source/line.cpp b/source/line.cpp index ac40271..e4137c3 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -15,6 +15,9 @@ namespace cpp_utils { return direction == that.direction && this->isOnLine(that.point); }; + /** + * Determines whether a point lies on the line. + */ bool Line::isOnLine(const Vector &that) const { bool isOn = true; try { @@ -25,6 +28,9 @@ namespace cpp_utils { return isOn; }; + /** + * Determines where on the line a point lies. + */ double Line::whereOnLine(const Vector &that) const { Vector vecDiff = that - point; double scalarX, scalarY, scalarZ; diff --git a/source/plane.cpp b/source/plane.cpp index b75de94..11c5308 100644 --- a/source/plane.cpp +++ b/source/plane.cpp @@ -15,11 +15,17 @@ namespace cpp_utils { return normal == that.normal && this->isOnPlane(that.point); }; + /** + * Determines whether a point lines on a plane. + */ bool Plane::isOnPlane(const Vector &that) const { Vector vecDiff = that - point; return vecDiff * normal == 0; }; + /** + * Finds the point of intersection for a given line with the plane. + */ Vector Plane::intersection(const Line &that) const { double cosAngle = that.direction * normal; Vector vecDiff = that.point - point; @@ -33,7 +39,10 @@ namespace cpp_utils { return that.point + (scalar * that.direction); } }; - + + /** + * Determines whether a line lies stricly between twos planes. + */ bool isBetween(const Line line, const Plane &plane1, const Plane &plane2) { if (plane1.normal != plane2.normal || line.direction * plane1.normal != 0) { return false; diff --git a/source/plane.hpp b/source/plane.hpp index 06b8348..3b33f51 100644 --- a/source/plane.hpp +++ b/source/plane.hpp @@ -21,7 +21,7 @@ namespace cpp_utils { Vector intersection(const Line &that) const; }; - bool between(const Line line, const Plane &plane1, const Plane &plane2); + bool isBetween(const Line line, const Plane &plane1, const Plane &plane2); } diff --git a/test/plane_unittest.cpp b/test/plane_unittest.cpp index 4df07bf..4f1daa9 100644 --- a/test/plane_unittest.cpp +++ b/test/plane_unittest.cpp @@ -108,7 +108,7 @@ namespace cpp_utils { EXPECT_THROW(plane.intersection(line4), value_error); } - TEST(PlaneTest, BetweenTest) { + TEST(PlaneTest, IsBetweenTest) { Vector point0, point1(1, 1, 1), point2(1, 0, -1), @@ -120,12 +120,12 @@ namespace cpp_utils { Plane plane0, plane1(point1, point1), plane2(point0, point2); - EXPECT_FALSE(cpp_utils::between(line1, plane0, plane0)); - EXPECT_TRUE(cpp_utils::between(line2, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line3, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line4, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line0, plane0, plane1)); - EXPECT_FALSE(cpp_utils::between(line0, plane0, plane2)); + EXPECT_FALSE(cpp_utils::isBetween(line1, plane0, plane0)); + EXPECT_TRUE(cpp_utils::isBetween(line2, plane0, plane1)); + EXPECT_FALSE(cpp_utils::isBetween(line3, plane0, plane1)); + EXPECT_FALSE(cpp_utils::isBetween(line4, plane0, plane1)); + EXPECT_FALSE(cpp_utils::isBetween(line0, plane0, plane1)); + EXPECT_FALSE(cpp_utils::isBetween(line0, plane0, plane2)); } } From 86cc66735543b615f47794c43fc4f870e18bdae2 Mon Sep 17 00:00:00 2001 From: scicode711 Date: Sat, 23 Aug 2025 13:32:57 +0100 Subject: [PATCH 12/15] Add some more test cases revealing a bug --- .gitignore | 1 + .vscode/c_cpp_properties.json | 7 ++- DevGuide/utils/runtests.bat | 3 ++ test/line_unittest.cpp | 81 ++++++++++++++++++++--------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index d710913..0d46faf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ testingBuild CMakeCache.txt dependencies CMakeFiles +build \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 4588867..f4f3e12 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -11,12 +11,11 @@ "_UNICODE" ], "compilerPath": "C:/msys64/mingw64/bin/g++.exe", - // Cannot find: C:/msys64/mingw64/bin/g++.exe - // Full path of the compiler being used, e.g. /usr/bin/gcc, to enable more accurate IntelliSense. "cStandard": "c17", "cppStandard": "gnu++17", - "intelliSenseMode": "windows-gcc-x64" + "intelliSenseMode": "windows-gcc-x64", + "configurationProvider": "ms-vscode.cmake-tools" } ], "version": 4 -} +} \ No newline at end of file diff --git a/DevGuide/utils/runtests.bat b/DevGuide/utils/runtests.bat index a02398a..53607de 100644 --- a/DevGuide/utils/runtests.bat +++ b/DevGuide/utils/runtests.bat @@ -1 +1,4 @@ +set -x + +cmake --build testingBuild .\testingBuild\Debug\testBuild.exe \ No newline at end of file diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index 56dbf10..17c4879 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -19,39 +19,48 @@ namespace cpp_utils { EXPECT_TRUE(line.direction == direction.normalise()); } - TEST(LineTest, EqualityTest) { + TEST(LineTest, BasicEqualityTest) { + EXPECT_TRUE(Line() == Line()); + EXPECT_FALSE(Line() == Line({1, 0, 0}, {0, 3, 6})); + } - Vector vec00, - vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), - vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), - vec07(1, 1, 1), - vec08(-1, 1, 1), vec09(1, -1, 1), vec10(1, 1, -1), - vec11(1, -1, -1), vec12(-1, 1, -1), vec13(-1, -1, 1), - vec14(-1, -1, -1); + TEST(LineTest, EqualityTest_DirectionChanges) { + // Same Line but direction reversal + Line line01({0, 0, 0}, {1, 0, 0}); + Line line02({0, 0, 0}, {-1, 0, 0}); + EXPECT_TRUE(line01 == line02); - Line line00, - line01(vec04, vec01), line02(vec05, vec02), line03(vec06, vec03), - line04(vec01, vec04), line05(vec02, vec05), line06(vec03, vec06), - line07(vec14, vec07), - line08(vec11, vec08), line09(vec12, vec09), line10(vec13, vec10), - line11(vec08, vec11), line12(vec09, vec12), line13(vec10, vec13), - line14(vec07, vec14); + Line line02a({0, 0, 0}, {0, 2, 2}); + Line line02b({0, 0, 0}, {0, -2, -2}); + EXPECT_TRUE(line02a == line02b); - EXPECT_TRUE(line00 == line00); - EXPECT_FALSE(line00 == line01); - EXPECT_TRUE(line01 == line04); - EXPECT_FALSE(line00 == line02); - EXPECT_TRUE(line02 == line05); - EXPECT_FALSE(line00 == line03); - EXPECT_TRUE(line03 == line06); - EXPECT_TRUE(line00 == line07); - EXPECT_TRUE(line07 == line14); - EXPECT_FALSE(line00 == line08); - EXPECT_TRUE(line08 == line11); - EXPECT_FALSE(line00 == line09); - EXPECT_TRUE(line09 == line12); - EXPECT_FALSE(line00 == line10); - EXPECT_TRUE(line10 == line13); + Line line02c({0, 0, 0}, {0, -2, 2}); + Line line02d({0, 0, 0}, {0, 2, -2}); + EXPECT_TRUE(line02c == line02d); + + // same point different direction + Line line07({1, 0, 0}, {1, 0, 0}); + Line line08({1, 0, 0}, {0, 1, 0}); + EXPECT_FALSE(line07 == line08); + } + + TEST(LineTest, EqualityTest_PointChanges) { + // Different points - same direction - different line + Line line03({1, 0, 0}, {1, 0, 0}); + Line line04({0, 1, 0}, {1, 0, 0}); + EXPECT_FALSE(line03 == line04); + + // Different points but the same line + Line line05({1, 0, 0}, {1, 0, 0}); + Line line06({0, 0, 0}, {1, 0, 0}); + EXPECT_TRUE(line05 == line06); + } + + TEST(LineTest, EqualityTest_Composite) { + // different point - reversed direction - same line + Line line09({0, 0, 0}, {0, 1, 1}); + Line line10({0, 2, 2}, {0, -1, -1}); + EXPECT_TRUE(line09 == line10); } TEST(LineTest, NotEqualityTest) { @@ -90,17 +99,19 @@ namespace cpp_utils { } TEST(LineTest, IsOnTest) { + // This is causing the other test failure + EXPECT_FALSE(Line({1, 0, 0}, {1, 0, 0}).isOnLine({0, 1, 0})); Vector point0(0, 1, 1); Vector point1(-1, 1, -1); Vector point2(-1, 2, 0); Line line0(point0, point1); - + Vector point3(1, 0, 1); Vector point4(-1, -1, 1); Vector point5(2, 1, 0); Line line1(point3, point4); - + Vector point6(1, 1, 0); Vector point7(1, -1, -1); Vector point8(0, 2, 1); @@ -120,17 +131,17 @@ namespace cpp_utils { } TEST(LineTest, WhereOnTest) { - + Vector point0(0, 1, 1); Vector point1(-1, 1, -1); Vector point2(-1, 2, 0); Line line0(point0, point1); - + Vector point3(1, 0, 1); Vector point4(-1, -1, 1); Vector point5(2, 1, 0); Line line1(point3, point4); - + Vector point6(1, 1, 0); Vector point7(1, -1, -1); Vector point8(0, 2, 1); From 49ccf19e77c07d6e0c5fe7892280e35549441ffc Mon Sep 17 00:00:00 2001 From: Ycidia Date: Sat, 23 Aug 2025 14:34:15 +0100 Subject: [PATCH 13/15] z to y --- .vscode/settings.json | 3 +-- DevGuide/utils/runBuild.bat | 3 --- source/line.cpp | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) delete mode 100644 DevGuide/utils/runBuild.bat diff --git a/.vscode/settings.json b/.vscode/settings.json index c06eed5..cfe40bd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,4 @@ -{ - "clang-format.executable": "/absolute/path/to/clang-format", +{ "files.associations": { "any": "cpp", "array": "cpp", diff --git a/DevGuide/utils/runBuild.bat b/DevGuide/utils/runBuild.bat deleted file mode 100644 index 67ee9d5..0000000 --- a/DevGuide/utils/runBuild.bat +++ /dev/null @@ -1,3 +0,0 @@ -set -x - -cmake --build testingBuild \ No newline at end of file diff --git a/source/line.cpp b/source/line.cpp index e4137c3..7cb3bc7 100644 --- a/source/line.cpp +++ b/source/line.cpp @@ -34,9 +34,9 @@ namespace cpp_utils { double Line::whereOnLine(const Vector &that) const { Vector vecDiff = that - point; double scalarX, scalarY, scalarZ; - + if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;} - if (direction.y == 0) {scalarY = vecDiff.z;} else {scalarY = vecDiff.y / direction.y;} + if (direction.y == 0) {scalarY = vecDiff.y;} else {scalarY = vecDiff.y / direction.y;} if (direction.z == 0) {scalarZ = vecDiff.z;} else {scalarZ = vecDiff.z / direction.z;} if ((scalarX == scalarY && scalarX == scalarZ) || From 7397c10dd82db7abe5e0372118845cbf703a40b3 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Sat, 23 Aug 2025 16:43:15 +0100 Subject: [PATCH 14/15] More robust IsOnTest --- test/line_unittest.cpp | 233 +++++++++++++++++++++++++++++++---------- 1 file changed, 176 insertions(+), 57 deletions(-) diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index 17c4879..fceec21 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -63,71 +63,190 @@ namespace cpp_utils { EXPECT_TRUE(line09 == line10); } - TEST(LineTest, NotEqualityTest) { - - Vector vec00, - vec01(1, 0, 0), vec02(0, 1, 0), vec03(0, 0, 1), - vec04(-1, 0, 0), vec05(0, -1, 0), vec06(0, 0, -1), - vec07(1, 1, 1), - vec08(-1, 1, 1), vec09(1, -1, 1), vec10(1, 1, -1), - vec11(1, -1, -1), vec12(-1, 1, -1), vec13(-1, -1, 1), - vec14(-1, -1, -1); - - Line line00, - line01(vec04, vec01), line02(vec05, vec02), line03(vec06, vec03), - line04(vec01, vec04), line05(vec02, vec05), line06(vec03, vec06), - line07(vec14, vec07), - line08(vec11, vec08), line09(vec12, vec09), line10(vec13, vec10), - line11(vec08, vec11), line12(vec09, vec12), line13(vec10, vec13), - line14(vec07, vec14); - - EXPECT_FALSE(line00 != line00); - EXPECT_TRUE(line00 != line01); - EXPECT_FALSE(line01 != line04); - EXPECT_TRUE(line00 != line02); - EXPECT_FALSE(line02 != line05); - EXPECT_TRUE(line00 != line03); - EXPECT_FALSE(line03 != line06); - EXPECT_FALSE(line00 != line07); - EXPECT_FALSE(line07 != line14); - EXPECT_TRUE(line00 != line08); - EXPECT_FALSE(line08 != line11); - EXPECT_TRUE(line00 != line09); - EXPECT_FALSE(line09 != line12); - EXPECT_TRUE(line00 != line10); - EXPECT_FALSE(line10 != line13); + TEST(LineTest, BasicNotEqualityTest) { + EXPECT_FALSE(Line() != Line()); + EXPECT_TRUE(Line() != Line({1, 0, 0}, {0, 3, 6})); } - TEST(LineTest, IsOnTest) { - // This is causing the other test failure - EXPECT_FALSE(Line({1, 0, 0}, {1, 0, 0}).isOnLine({0, 1, 0})); + TEST(LineTest, NotEqualityTest_DirectionChanges) { + // Same Line but direction reversal + Line line01({0, 0, 0}, {1, 0, 0}); + Line line02({0, 0, 0}, {-1, 0, 0}); + EXPECT_FALSE(line01 != line02); - Vector point0(0, 1, 1); - Vector point1(-1, 1, -1); - Vector point2(-1, 2, 0); - Line line0(point0, point1); + Line line02a({0, 0, 0}, {0, 2, 2}); + Line line02b({0, 0, 0}, {0, -2, -2}); + EXPECT_FALSE(line02a != line02b); - Vector point3(1, 0, 1); - Vector point4(-1, -1, 1); - Vector point5(2, 1, 0); - Line line1(point3, point4); + Line line02c({0, 0, 0}, {0, -2, 2}); + Line line02d({0, 0, 0}, {0, 2, -2}); + EXPECT_FALSE(line02c != line02d); - Vector point6(1, 1, 0); - Vector point7(1, -1, -1); - Vector point8(0, 2, 1); - Line line2(point6, point7); + // same point different direction + Line line07({1, 0, 0}, {1, 0, 0}); + Line line08({1, 0, 0}, {0, 1, 0}); + EXPECT_TRUE(line07 != line08); + } + + TEST(LineTest, NotEqualityTest_PointChanges) { + // Different points - same direction - different line + Line line03({1, 0, 0}, {1, 0, 0}); + Line line04({0, 1, 0}, {1, 0, 0}); + EXPECT_TRUE(line03 != line04); + + // Different points but the same line + Line line05({1, 0, 0}, {1, 0, 0}); + Line line06({0, 0, 0}, {1, 0, 0}); + EXPECT_FALSE(line05 != line06); + } + + TEST(LineTest, NotEqualityTest_Composite) { + // different point - reversed direction - same line + Line line09({0, 0, 0}, {0, 1, 1}); + Line line10({0, 2, 2}, {0, -1, -1}); + EXPECT_FALSE(line09 != line10); + } + + TEST(LineTest, IsOnTest_PointChanges) { + // Direction {1, -1, 0} + Line line0({1, 0, 0}, {1, -1, 0}), + line1({0, 1, 0}, {1, -1, 0}), + line2({0, 0, 1}, {1, -1, 0}); + + EXPECT_TRUE(line0.isOnLine({1, 0, 0})); + EXPECT_TRUE(line0.isOnLine({2, -1, 0})); + EXPECT_TRUE(line0.isOnLine({0, 1, 0})); + EXPECT_FALSE(line0.isOnLine({1, -1, 0})); + EXPECT_FALSE(line0.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line1.isOnLine({0, 1, 0})); + EXPECT_TRUE(line1.isOnLine({1, 0, 0})); + EXPECT_TRUE(line1.isOnLine({-1, 2, 0})); + EXPECT_FALSE(line1.isOnLine({1, -1, 0})); + EXPECT_FALSE(line1.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line2.isOnLine({0, 0, 1})); + EXPECT_TRUE(line2.isOnLine({1, -1, 1})); + EXPECT_TRUE(line2.isOnLine({-1, 1, 1})); + EXPECT_FALSE(line2.isOnLine({1, -1, 0})); + EXPECT_FALSE(line2.isOnLine({0, 0, 0})); + + // Direction {0, 1, -1} + Line line3({1, 0, 0}, {0, 1, -1}), + line4({0, 1, 0}, {0, 1, -1}), + line5({0, 0, 1}, {0, 1, -1}); + + EXPECT_TRUE(line3.isOnLine({1, 0, 0})); + EXPECT_TRUE(line3.isOnLine({1, 1, -1})); + EXPECT_TRUE(line3.isOnLine({1, -1, 1})); + EXPECT_FALSE(line3.isOnLine({0, 1, -1})); + EXPECT_FALSE(line3.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line4.isOnLine({0, 1, 0})); + EXPECT_TRUE(line4.isOnLine({0, 2, -1})); + EXPECT_TRUE(line4.isOnLine({0, 0, 1})); + EXPECT_FALSE(line4.isOnLine({0, 1, -1})); + EXPECT_FALSE(line4.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line5.isOnLine({0, 0, 1})); + EXPECT_TRUE(line5.isOnLine({0, 1, 0})); + EXPECT_TRUE(line5.isOnLine({0, -1, 2})); + EXPECT_FALSE(line5.isOnLine({0, 1, -1})); + EXPECT_FALSE(line5.isOnLine({0, 0, 0})); + + // Direction {-1, 0, 1} + Line line6({1, 0, 0}, {-1, 0, 1}), + line7({0, 1, 0}, {-1, 0, 1}), + line8({0, 0, 1}, {-1, 0, 1}); + + EXPECT_TRUE(line6.isOnLine({1, 0, 0})); + EXPECT_TRUE(line6.isOnLine({0, 0, 1})); + EXPECT_TRUE(line6.isOnLine({2, 0, -1})); + EXPECT_FALSE(line6.isOnLine({-1, 0, 1})); + EXPECT_FALSE(line6.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line7.isOnLine({0, 1, 0})); + EXPECT_TRUE(line7.isOnLine({-1, 1, 1})); + EXPECT_TRUE(line7.isOnLine({1, 1, -1})); + EXPECT_FALSE(line7.isOnLine({-1, 0, 1})); + EXPECT_FALSE(line7.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line8.isOnLine({0, 0, 1})); + EXPECT_TRUE(line8.isOnLine({-1, 0, 2})); + EXPECT_TRUE(line8.isOnLine({1, 0, 0})); + EXPECT_FALSE(line8.isOnLine({-1, 0, 1})); + EXPECT_FALSE(line8.isOnLine({0, 0, 0})); + } + + TEST(LineTest, IsOnTest_DirectionChanges) { + // Point {1, -1, 0} + Line line0({1, -1, 0}, {1, 0, 0}), + line1({1, -1, 0}, {0, 1, 0}), + line2({1, -1, 0}, {0, 0, 1}); + + EXPECT_TRUE(line0.isOnLine({1, -1, 0})); + EXPECT_TRUE(line0.isOnLine({2, -1, 0})); + EXPECT_TRUE(line0.isOnLine({0, -1, 0})); + EXPECT_FALSE(line0.isOnLine({1, 0, 0})); + EXPECT_FALSE(line0.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line1.isOnLine({1, -1, 0})); + EXPECT_TRUE(line1.isOnLine({1, 0, 0})); + EXPECT_TRUE(line1.isOnLine({1, -2, 0})); + EXPECT_FALSE(line1.isOnLine({0, 1, 0})); + EXPECT_FALSE(line1.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line2.isOnLine({1, -1, 0})); + EXPECT_TRUE(line2.isOnLine({1, -1, 1})); + EXPECT_TRUE(line2.isOnLine({1, -1, -1})); + EXPECT_FALSE(line2.isOnLine({0, 0, 1})); + EXPECT_FALSE(line2.isOnLine({0, 0, 0})); + + // Point {0, 1, -1} + Line line3({0, 1, -1}, {1, 0, 0}), + line4({0, 1, -1}, {0, 1, 0}), + line5({0, 1, -1}, {0, 0, 1}); + + EXPECT_TRUE(line3.isOnLine({0, 1, -1})); + EXPECT_TRUE(line3.isOnLine({1, 1, -1})); + EXPECT_TRUE(line3.isOnLine({-1, 1, -1})); + EXPECT_FALSE(line3.isOnLine({1, 0, 0})); + EXPECT_FALSE(line3.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line4.isOnLine({0, 1, -1})); + EXPECT_TRUE(line4.isOnLine({0, 2, -1})); + EXPECT_TRUE(line4.isOnLine({0, 0, -1})); + EXPECT_FALSE(line4.isOnLine({0, 1, 0})); + EXPECT_FALSE(line4.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line5.isOnLine({0, 1, -1})); + EXPECT_TRUE(line5.isOnLine({0, 1, 0})); + EXPECT_TRUE(line5.isOnLine({0, 1, -2})); + EXPECT_FALSE(line5.isOnLine({0, 0, 1})); + EXPECT_FALSE(line5.isOnLine({0, 0, 0})); + + // Point {-1, 0, 1} + Line line6({-1, 0, 1}, {1, 0, 0}), + line7({-1, 0, 1}, {0, 1, 0}), + line8({-1, 0, 1}, {0, 0, 1}); - EXPECT_TRUE(line0.isOnLine(point0)); - EXPECT_FALSE(line0.isOnLine(point1)); - EXPECT_TRUE(line0.isOnLine(point2)); + EXPECT_TRUE(line6.isOnLine({-1, 0, 1})); + EXPECT_TRUE(line6.isOnLine({0, 0, 1})); + EXPECT_TRUE(line6.isOnLine({-2, 0, 1})); + EXPECT_FALSE(line6.isOnLine({1, 0, 0})); + EXPECT_FALSE(line6.isOnLine({0, 0, 0})); - EXPECT_TRUE(line1.isOnLine(point3)); - EXPECT_FALSE(line1.isOnLine(point4)); - EXPECT_TRUE(line1.isOnLine(point5)); + EXPECT_TRUE(line7.isOnLine({-1, 0, 1})); + EXPECT_TRUE(line7.isOnLine({-1, 1, 1})); + EXPECT_TRUE(line7.isOnLine({-1, -1, 1})); + EXPECT_FALSE(line7.isOnLine({0, 1, 0})); + EXPECT_FALSE(line7.isOnLine({0, 0, 0})); - EXPECT_TRUE(line2.isOnLine(point6)); - EXPECT_FALSE(line2.isOnLine(point7)); - EXPECT_TRUE(line2.isOnLine(point8)); + EXPECT_TRUE(line8.isOnLine({-1, 0, 1})); + EXPECT_TRUE(line8.isOnLine({-1, 0, 2})); + EXPECT_TRUE(line8.isOnLine({-1, 0, 0})); + EXPECT_FALSE(line8.isOnLine({0, 0, 1})); + EXPECT_FALSE(line8.isOnLine({0, 0, 0})); } TEST(LineTest, WhereOnTest) { From 45bed6e6c33744b842f6c2003565b177f3a360b4 Mon Sep 17 00:00:00 2001 From: Ycidia Date: Sat, 23 Aug 2025 20:39:22 +0100 Subject: [PATCH 15/15] Further tests --- test/line_unittest.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/test/line_unittest.cpp b/test/line_unittest.cpp index fceec21..9615a3a 100644 --- a/test/line_unittest.cpp +++ b/test/line_unittest.cpp @@ -107,6 +107,83 @@ namespace cpp_utils { EXPECT_FALSE(line09 != line10); } + TEST(LineTest, IsOnTest_UnitChecks) { + // Direction {1, 0, 0} + Line line0({1, 0, 0}, {1, 0, 0}), + line1({0, 1, 0}, {1, 0, 0}), + line2({0, 0, 1}, {1, 0, 0}); + + EXPECT_TRUE(line0.isOnLine({1, 0, 0})); + EXPECT_TRUE(line0.isOnLine({2, 0, 0})); + EXPECT_TRUE(line0.isOnLine({0, 0, 0})); + EXPECT_FALSE(line0.isOnLine({0, 1, 0})); + EXPECT_FALSE(line0.isOnLine({0, 0, 1})); + + EXPECT_TRUE(line1.isOnLine({0, 1, 0})); + EXPECT_TRUE(line1.isOnLine({1, 1, 0})); + EXPECT_TRUE(line1.isOnLine({-1, 1, 0})); + EXPECT_FALSE(line1.isOnLine({0, 0, 1})); + EXPECT_FALSE(line1.isOnLine({1, 0, 0})); + EXPECT_FALSE(line1.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line2.isOnLine({0, 0, 1})); + EXPECT_TRUE(line2.isOnLine({1, 0, 1})); + EXPECT_TRUE(line2.isOnLine({-1, 0, 1})); + EXPECT_FALSE(line2.isOnLine({1, 0, 0})); + EXPECT_FALSE(line2.isOnLine({0, 1, 0})); + EXPECT_FALSE(line2.isOnLine({0, 0, 0})); + + // Direction {0, 1, 0} + Line line3({1, 0, 0}, {0, 1, 0}), + line4({0, 1, 0}, {0, 1, 0}), + line5({0, 0, 1}, {0, 1, 0}); + + EXPECT_TRUE(line3.isOnLine({1, 0, 0})); + EXPECT_TRUE(line3.isOnLine({1, 1, 0})); + EXPECT_TRUE(line3.isOnLine({1, -1, 0})); + EXPECT_FALSE(line3.isOnLine({0, 1, 0})); + EXPECT_FALSE(line3.isOnLine({0, 0, 1})); + EXPECT_FALSE(line3.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line4.isOnLine({0, 1, 0})); + EXPECT_TRUE(line4.isOnLine({0, 2, 0})); + EXPECT_TRUE(line4.isOnLine({0, 0, 0})); + EXPECT_FALSE(line4.isOnLine({0, 0, 1})); + EXPECT_FALSE(line4.isOnLine({1, 0, 0})); + + EXPECT_TRUE(line5.isOnLine({0, 0, 1})); + EXPECT_TRUE(line5.isOnLine({0, 1, 1})); + EXPECT_TRUE(line5.isOnLine({0, -1, 1})); + EXPECT_FALSE(line5.isOnLine({1, 0, 0})); + EXPECT_FALSE(line5.isOnLine({0, 1, 0})); + EXPECT_FALSE(line5.isOnLine({0, 0, 0})); + + // Direction {0, 0, 1} + Line line6({1, 0, 0}, {0, 0, 1}), + line7({0, 1, 0}, {0, 0, 1}), + line8({0, 0, 1}, {0, 0, 1}); + + EXPECT_TRUE(line6.isOnLine({1, 0, 0})); + EXPECT_TRUE(line6.isOnLine({1, 0, 1})); + EXPECT_TRUE(line6.isOnLine({1, 0, -1})); + EXPECT_FALSE(line6.isOnLine({0, 1, 0})); + EXPECT_FALSE(line6.isOnLine({0, 0, 1})); + EXPECT_FALSE(line6.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line7.isOnLine({0, 1, 0})); + EXPECT_TRUE(line7.isOnLine({0, 1, 1})); + EXPECT_TRUE(line7.isOnLine({0, 1, -1})); + EXPECT_FALSE(line7.isOnLine({0, 0, 1})); + EXPECT_FALSE(line7.isOnLine({1, 0, 0})); + EXPECT_FALSE(line7.isOnLine({0, 0, 0})); + + EXPECT_TRUE(line8.isOnLine({0, 0, 1})); + EXPECT_TRUE(line8.isOnLine({0, 0, 2})); + EXPECT_TRUE(line8.isOnLine({0, 0, 0})); + EXPECT_FALSE(line8.isOnLine({1, 0, 0})); + EXPECT_FALSE(line8.isOnLine({0, 1, 0})); + } + TEST(LineTest, IsOnTest_PointChanges) { // Direction {1, -1, 0} Line line0({1, 0, 0}, {1, -1, 0}),