-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.h
134 lines (103 loc) · 2.96 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef _TRIANGLE_H
#define _TRIANGLE_H
#include "math.h"
#include "Object.h"
#include "Vect.h"
#include "Color.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 parrallel to Triangle
return -1;
}
else
{
double b = normal.dotProduct(ray.getRayOrigin().vectAdd(normal.vectMult(distance).negative()));
double distance2plane = -1*b/a;
// Q is point of intersection
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);
// [CAxQA] * b >= 0
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);
// [BCxQC] * b >= 0
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);
// [ABxQB] * b >= 0
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) ) {
// inside triangle
return -1*b/a;
}
else{
// outside triangle
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 pointA, Vect pointB, Vect pointC, Color colorValue){
A = pointA;
B = pointB;
C = pointC;
color = colorValue;
}
#endif