-
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 1 commit
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,38 @@ | ||
| #include "line.hpp" | ||
|
|
||
| namespace cpp_utils { | ||
|
|
||
| Line::Line(Vector setPoint, Vector setDirection) : point(setPoint) { | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| double magnitude = setDirection.mod(); | ||
| Vector normDir = (1 / magnitude) * setDirection; | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| if(normDir.x < 0) { | ||
|
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) { | ||
|
Ycidia marked this conversation as resolved.
Outdated
|
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| }; | ||
|
|
||
| std::pair<bool,double> Line::isOnLine(const Vector &that) const { | ||
|
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); | ||
| } | ||
| }; | ||
|
|
||
| } | ||
| 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 |
| 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; | ||
|
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; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } | ||
| 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); | ||
|
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?