-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plane.cpp
49 lines (42 loc) · 1.25 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
/*----------------------------------------------------------
* COSC363 Ray Tracer
*
* The Plane class
* This is a subclass of Object, and hence implements the
* methods intersect() and normal().
-------------------------------------------------------------*/
#include "Plane.h"
#include "Vector.h"
#include <math.h>
//Function to test if an input point is within the quad.
bool Plane::isInside(Vector q)
{
Vector n = normal(q);
Vector ua = b-a, ub = c-b, uc = d-c, ud = a-d;
Vector va = q-a, vb = q-b, vc = q-c, vd = q-d;
if (ua.cross(va).dot(n) > 0 && ub.cross(vb).dot(n) > 0 && uc.cross(vc).dot(n) > 0 && ud.cross(vd).dot(n) > 0) {
return true;
}
return false;
}
//Function to compute the paramter t at the point of intersection.
float Plane::intersect(Vector pos, Vector dir)
{
Vector n = normal(pos);
Vector vdif = a-pos;
float vdotn = dir.dot(n);
if(fabs(vdotn) < 1.e-4) return -1;
float t = vdif.dot(n)/vdotn;
if(fabs(t) < 0.0001) return -1;
Vector q = pos + dir*t;
if(isInside(q)) return t;
else return -1;
}
// Function to compute the unit normal vector
// Remember to output a normalised vector!
Vector Plane::normal(Vector pos)
{
Vector n = (b-a).cross(c-a);
n.normalise();
return n;
}