Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
}
}
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
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"
38 changes: 38 additions & 0 deletions source/line.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "line.hpp"

namespace cpp_utils {

Line::Line(Vector setPoint, Vector setDirection) : point(setPoint) {
Comment thread
Ycidia marked this conversation as resolved.
Outdated
double magnitude = setDirection.mod();
Vector normDir = (1 / magnitude) * setDirection;
Comment thread
Ycidia marked this conversation as resolved.
Outdated
if(normDir.x < 0) {
Comment thread
Ycidia marked this conversation as resolved.
Outdated
direction = -normDir;
} else {
direction = normDir;
}
};

bool Line::operator==(const Line &that) const {
if(direction == that.direction && this->isOnLine(that.point).first) {
Comment thread
Ycidia marked this conversation as resolved.
Outdated
return true;
} else {
return false;
}
};

std::pair<bool,double> Line::isOnLine(const Vector &that) const {
Comment thread
Ycidia marked this conversation as resolved.
Outdated
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<bool,double> (true, scalarX);
} else {
return std::pair<bool,double> (false, 0);
}
};

}
26 changes: 26 additions & 0 deletions source/line.hpp
Original file line number Diff line number Diff line change
@@ -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<bool,double> isOnLine(const Vector &that) const;
};

}

#endif
38 changes: 18 additions & 20 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,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) {
Expand All @@ -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)));
}
};

}
48 changes: 24 additions & 24 deletions source/matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}

Expand Down
60 changes: 60 additions & 0 deletions source/plane.cpp
Original file line number Diff line number Diff line change
@@ -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;
Comment thread
Ycidia marked this conversation as resolved.
Outdated
} 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;
}
}
};

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

#include <stdexcept>
#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);

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
12 changes: 5 additions & 7 deletions source/quaternion.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include "quaternion.hpp"
#include <math.h>
#include <cmath>

namespace cpp_utils {

Expand All @@ -9,17 +7,17 @@ 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));
};

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

Expand All @@ -43,12 +41,12 @@ 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);
};

Quaternion operator*(const Vector vec, const Quaternion quat) {
return Quaternion(0.0, vec.x, vec.y, vec.z) * quat;
};

}
}
Loading