-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSegment.cpp
64 lines (52 loc) · 1.66 KB
/
Segment.cpp
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
60
61
62
63
64
#include "Segment.h"
#include "Plane.h"
Segment::Segment()
{
}
Segment::Segment(const Vector3 &p1, const Vector3 &p2)
{
this->p1 = p1;
this->p2 = p2;
}
bool Segment::IsPartOfPlane(Plane *plane) const
{
Vector3 v1 = plane->point - p1;
Vector3 v2 = plane->point - p2;
Vector3 perpend = Vector3::Cross(v1,v2);
return std::abs( Vector3::Dot(perpend, plane->normal) ) == 1.0f;
}
bool Segment::IsPointInLeft(const Vector3 &p)
{
return GetOrientation(p) > 0;
}
int Segment::GetOrientation(const Vector3 &p)
{
float crossProduct = ((p2.x - p1.x) * (p.y - p1.y) - (p2.y - p1.y) * (p.x - p1.x));
return crossProduct == 0 ? 0 : ( crossProduct > 0 ? 1 : -1 ) ;
}
bool Segment::IsPointOver(const Vector3 &p)
{
return GetOrientation(p) == 0 && InsideBoundingRect(p);
}
bool Segment::IsPointInRight(const Vector3 &p)
{
return GetOrientation(p) < 0;
}
// Given (p,q,r) colinear points => returns if r is between(p,q).
inline bool Segment::InsideBoundingRect(const Vector3 &p)
{
return p.x >= std::min(p1.x, p2.x) && p.x <= std::max(p1.x, p2.x) &&
p.y >= std::min(p1.y, p2.y) && p.y <= std::max(p1.y, p2.y);
}
bool Segment::GetIntersection(const Plane &plane,
Vector3 *intersectionPoint) const
{
float distp1 = Vector3::Dot(plane.normal, (p1 - plane.point));
float distp2 = Vector3::Dot(plane.normal, (p2 - plane.point));
if (distp1 * distp2 > 0) return false; // There is no intersection
Vector3 x = (p2-p1).Normalized();
float dot = Vector3::Dot(plane.normal, x);
if (dot == 0) return false; // Segment parallel to plane
*intersectionPoint = p2 - x * (distp2 / dot);
return true;
}