-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplane.cpp
More file actions
59 lines (52 loc) · 1.79 KB
/
plane.cpp
File metadata and controls
59 lines (52 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "plane.hpp"
namespace cpp_utils {
Plane::Plane(Vector pnt, Vector nor) : point(pnt) {
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;
} 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.");
} 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;
}
}
};
}