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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ testingBuild
CMakeCache.txt
dependencies
CMakeFiles
build
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"compilerPath": "C:/msys64/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "windows-gcc-x64"
"intelliSenseMode": "windows-gcc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
Expand Down
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
"files.associations": {
"any": "cpp",
"array": "cpp",
Expand Down Expand Up @@ -87,4 +87,4 @@
"xtree": "cpp",
"xutility": "cpp"
}
}
}
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions DevGuide/utils/runBuild.bat

This file was deleted.

3 changes: 3 additions & 0 deletions DevGuide/utils/runtests.bat
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
set -x

cmake --build testingBuild
.\testingBuild\Debug\testBuild.exe
1 change: 1 addition & 0 deletions exception/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "value.hpp"
14 changes: 14 additions & 0 deletions exception/value.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef VALUE_HPP
#define VALUE_HPP

#include<stdexcept>

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
2 changes: 2 additions & 0 deletions source/cpp_utils.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "vector.hpp"
#include "quaternion.hpp"
#include "matrix.hpp"
#include "line.hpp"
#include "plane.hpp"
57 changes: 57 additions & 0 deletions source/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "line.hpp"

namespace cpp_utils {

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 {
direction = normDir;
}
};

bool Line::operator==(const Line &that) const {
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 {
this->whereOnLine(that);
} catch (value_error) {
isOn = false;
}
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;

if (direction.x == 0) {scalarX = vecDiff.x;} else {scalarX = vecDiff.x / direction.x;}
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) ||
(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 value_error("Point not on line.");
}
};

}
28 changes: 28 additions & 0 deletions source/line.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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 _point, Vector _direction);

bool operator==(const Line &that) const;
inline bool operator!=(const Line &that) const {
return true != (*this == that);
};

bool isOnLine(const Vector &that) const;

double whereOnLine(const Vector &that) const;
};

}

#endif
120 changes: 57 additions & 63 deletions source/matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include <math.h>
#include <cmath>
#include "matrix.hpp"

namespace cpp_utils {
Expand All @@ -16,77 +14,73 @@ 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 {
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;
}
bool Matrix::operator==(const Matrix &that) const {
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);
}
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 {
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);
};

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 {
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));
}
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 {
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 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) {
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));
}
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) {
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)));
}

if (this->det() == 0) {
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)),
((yx * zy) - (zx * yy)), -((xx * zy) - (zx * xy)), ((xx * yy) - (yx * xy)));
};
};

}
49 changes: 24 additions & 25 deletions source/matrix.hpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
#ifndef MATRIX_HPP
#define MATRIX_HPP

#include <stdexcept>
#include "vector.hpp"

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);

}

Expand Down
Loading