-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlane.cpp
81 lines (65 loc) · 1.9 KB
/
Plane.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*----------------------------------------------------------
* COSC363 Ray Tracer
*
* The Plane class
* This is a subclass of Object, and hence implements the
* methods intersect() and normal().
-------------------------------------------------------------*/
#include "Plane.h"
#include <math.h>
/**
* Checks if a point pt is inside the current polygon
* Implement a point inclusion test using
* member variables a, b, c, d.
*/
bool Plane::isInside(glm::vec3 pt)
{
//=== Complete this function ====
glm::vec3 ua = b - a;
glm::vec3 va = pt - a;
glm::vec3 ub = c - b;
glm::vec3 vb = pt - b;
glm::vec3 uc = d - c;
glm::vec3 vc = pt - c;
glm::vec3 ud = a - d;
glm::vec3 vd = pt - d;
glm::vec3 n = normal_internal(pt);
float uavaDotn = glm::dot(glm::cross(ua, va), n);
float ubvbDotn = glm::dot(glm::cross(ub, vb), n);
float ucvcDotn = glm::dot(glm::cross(uc, vc), n);
float udvdDotn = glm::dot(glm::cross(ud, vd), n);
if (uavaDotn > 0 && ubvbDotn > 0 && ucvcDotn > 0 && udvdDotn > 0) {
return true;
} else {
return false;
}
}
/**
* Plane's intersection method. The input is a ray (pos, dir).
*/
float Plane::intersect_internal(glm::vec3 posn, glm::vec3 dir)
{
glm::vec3 n = normal_internal(posn);
glm::vec3 vdif = a - posn;
float vdotn = glm::dot(dir, n);
if(fabs(vdotn) < 1.e-4) return -1;
float t = glm::dot(vdif, n)/vdotn;
if(fabs(t) < 0.0001) return -1;
glm::vec3 q = posn + dir*t;
if(isInside(q)) return t;
else return -1;
}
/**
* Returns the unit normal vector at a given point.
* Compute the plane's normal vector using
* member variables a, b, c, d.
* The parameter pt is a dummy variable and is not used.
*/
glm::vec3 Plane::normal_internal(glm::vec3 pt)
{
glm::vec3 n = glm::vec3(0);
//=== Complete this function ====
n = glm::cross((b - a), (d - a));
n = glm::normalize(n);
return n;
}