Skip to content
Open
Show file tree
Hide file tree
Changes from 8 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
4 changes: 3 additions & 1 deletion .vscode/c_cpp_properties.json

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know why there are changes in this file? Can you also look if this file should even be included I the repo?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a yellow squiggle under the compiler path. The comment is the message that comes up. It seem to only configure the c\c++ extentions in vscode so probably not?

Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
{
"clang-format.executable": "/absolute/path/to/clang-format",
"files.associations": {
"any": "cpp",
"array": "cpp",
Expand Down Expand Up @@ -87,4 +88,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
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"
56 changes: 56 additions & 0 deletions source/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#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 {
bool on = this->isOnLine(that.point);
if (direction == that.direction && on) {
Comment thread
Ycidia marked this conversation as resolved.
Outdated
return true;
} else {
return false;
}
};

bool Line::isOnLine(const Vector &that) const {
bool isOn = true;
try {
this->whereOnLine(that);
} catch (value_error) {
isOn = false;
}
return isOn;
}

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.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
51 changes: 25 additions & 26 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,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;
Expand Down Expand Up @@ -48,45 +46,46 @@ 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) {
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
55 changes: 55 additions & 0 deletions source/plane.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "plane.hpp"

namespace cpp_utils {

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 {
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;
return vecDiff * normal == 0;
};

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 value_error("Line lies in plane or is 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;
}
Comment thread
Ycidia marked this conversation as resolved.
Outdated
}
};

}
28 changes: 28 additions & 0 deletions source/plane.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef PLANE_HPP
#define PLANE_HPP

#include "line.hpp"

namespace cpp_utils {

class Plane {
public:
Vector point, normal;
inline Plane() : point(0, 0, 0), normal(norm, norm, norm) {};
Plane(Vector _point, Vector _normal);

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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the between method do?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tells you whether a line goes between two planes. If the planes of the bounding box are perpendicular to the screen then it will never intersect them. In such a case you would need to know whether the line goes between the planes as to whether the clicking line intersects with the bounding box. At least, that is what I believe.


}

#endif
Loading