-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.h
103 lines (80 loc) · 2.74 KB
/
Triangle.h
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef Triangle_H
#define Triangle_H
#include "math.h"
#include "Object.h"
#include "Vect.h"
#include "Color.h"
#include "Ray.h"
class Triangle : public Object {
Vect A, B, C;
Vect normal;
double distance;
Color color;
public:
Triangle ();
Triangle (Vect, Vect, Vect, Color);
// method functions
Vect getTriangleNormal () {
Vect CA (C.getVectX() - A.getVectX(), C.getVectY() - A.getVectY(), C.getVectZ() - A.getVectZ());
Vect BA (B.getVectX() - A.getVectX(), B.getVectY() - A.getVectY(), B.getVectZ() - A.getVectZ());
normal = CA.crossProduct(BA).normalize();
return normal;
}
double getTriangleDistance () {
normal = getTriangleNormal();
distance = normal.dotProduct(A);
return distance;
}
virtual Color getColor () { return color; }
virtual Vect getNormalAt(Vect point) {
normal = getTriangleNormal();
return normal;
}
virtual double findIntersection(Ray ray) {
Vect ray_direction = ray.getRayDirection();
Vect ray_origin = ray.getRayOrigin();
normal = getTriangleNormal();
distance = getTriangleDistance();
double a = ray_direction.dotProduct(normal);
if (a == 0) {
// ray is parallel to the Triangle
return -1;
}
else {
double b = normal.dotProduct(ray.getRayOrigin().vectAdd(normal.vectMult(distance).negative()));
double distance2plane = -1*b/a;
double Qx = ray_direction.vectMult(distance2plane).getVectX() + ray_origin.getVectX();
double Qy = ray_direction.vectMult(distance2plane).getVectY() + ray_origin.getVectY();
double Qz = ray_direction.vectMult(distance2plane).getVectZ() + ray_origin.getVectZ();
Vect Q (Qx, Qy, Qz);
Vect CA (C.getVectX() - A.getVectX(), C.getVectY() - A.getVectY(), C.getVectZ() - A.getVectZ());
Vect QA (Q.getVectX() - A.getVectX(), Q.getVectY() - A.getVectY(), Q.getVectZ() - A.getVectZ());
double test1 = (CA.crossProduct(QA)).dotProduct(normal);
Vect BC (B.getVectX() - C.getVectX(), B.getVectY() - C.getVectY(), B.getVectZ() - C.getVectZ());
Vect QC (Q.getVectX() - C.getVectX(), Q.getVectY() - C.getVectY(), Q.getVectZ() - C.getVectZ());
double test2 = (BC.crossProduct(QC)).dotProduct(normal);
Vect AB (A.getVectX() - B.getVectX(), A.getVectY() - B.getVectY(), A.getVectZ() - B.getVectZ());
Vect QB (Q.getVectX() - B.getVectX(), Q.getVectY() - B.getVectY(), Q.getVectZ() - B.getVectZ());
double test3 = (AB.crossProduct(QB)).dotProduct(normal);
if ((test1 >= 0) && (test2 >= 0) && (test3 >= 0)){
return -1*b/a;
}
else {
return -1;
}
}
}
};
Triangle::Triangle () {
A = Vect (1, 0, 0);
B = Vect (0, 1, 0);
C = Vect (0, 0, 1);
color = Color(0.5,0.5,0.5, 0);
}
Triangle::Triangle (Vect a, Vect b, Vect c, Color colorValue) {
A = a;
B = b;
C = c;
color = colorValue;
}
#endif