-
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 2 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 |
|---|---|---|
| @@ -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,61 @@ | ||
| #include "line.hpp" | ||
|
|
||
| namespace cpp_utils { | ||
|
|
||
| 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) { | ||
| 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) { | ||
| 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; | ||
| } else { | ||
| throw std::overflow_error("Point not on line."); | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| } | ||
| }; | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #ifndef LINE_HPP | ||
| #define LINE_HPP | ||
|
|
||
| #include "vector.hpp" | ||
|
|
||
| const double norm = (1 / sqrt(3)); | ||
|
|
||
| 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) { | ||
| return true != (*this == that); | ||
| }; | ||
|
|
||
| bool isOnLine(const Vector &that); | ||
|
|
||
| double whereOnLine(const Vector &that); | ||
| }; | ||
|
|
||
| } | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| #include "plane.hpp" | ||
|
|
||
| namespace cpp_utils { | ||
|
|
||
| Plane::Plane(Vector pnt, Vector nor) : point(pnt) { | ||
|
sci-code-711 marked this conversation as resolved.
Outdated
|
||
| 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; | ||
| } | ||
| }; | ||
|
|
||
| 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; | ||
|
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 is parallel to plane."); | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| } 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 pnt, Vector nor); | ||
|
|
||
| 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?