-
Notifications
You must be signed in to change notification settings - Fork 0
Line and Planes #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Line and Planes #15
Changes from 8 commits
bc36b17
2dde297
9f8ba4d
e4d75ba
468f322
9deee68
ba96749
d8da90a
921c3d3
392297e
869dbce
86cc667
49ccf19
7397c10
45bed6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| #include "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 |
| 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" |
| 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) { | ||
|
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."); | ||
| } | ||
| }; | ||
|
|
||
| } | ||
| 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 |
| 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; | ||
| } | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| } | ||
| }; | ||
|
|
||
| } | ||
| 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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does the between method do?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?