-
Notifications
You must be signed in to change notification settings - Fork 0
/
1705103_Triangle.hpp
116 lines (90 loc) · 3.1 KB
/
1705103_Triangle.hpp
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
#ifndef TRIANGLE
#define TRIANGLE
#include <GL/glut.h>
#include <iostream>
#include <vector>
#include "1705103_Object.hpp"
#include "1705103_Point.hpp"
#include "1705103_Ray.hpp"
#include "1705103_Utils.hpp"
#include "1705103_Vector.hpp"
using namespace std;
class Triangle : public Object {
private:
Point vertices[3];
Vector normal;
double matrix[3][3], a, b, c, detA, beta, gamma, t, x, y, z, dotProduct;
double matrixDeterminant();
public:
Triangle(Point v1, Point v2, Point v3);
void draw();
Ray getNormal(Point &intersectionPoint, Ray &incidentRay);
double intersectionParameter(Ray *ray);
};
Triangle::Triangle(Point v1, Point v2, Point v3) {
vertices[0] = v1;
vertices[1] = v2;
vertices[2] = v3;
Vector vector1 = Point::getVector(vertices[0], vertices[1]);
Vector vector2 = Point::getVector(vertices[0], vertices[2]);
normal = Vector::crossProduct(vector1, vector2);
normal = Vector::normalize(normal);
}
void Triangle::draw() {
const double *color = getColor();
glColor3f(color[0], color[1], color[2]);
glBegin(GL_TRIANGLES);
{
glVertex3f(vertices[0][0], vertices[0][1], vertices[0][2]);
glVertex3f(vertices[1][0], vertices[1][1], vertices[1][2]);
glVertex3f(vertices[2][0], vertices[2][1], vertices[2][2]);
}
glEnd();
}
double Triangle::matrixDeterminant() {
a = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[2][1] * matrix[1][2]);
b = matrix[0][1] * (matrix[2][0] * matrix[1][2] - matrix[1][0] * matrix[2][2]);
c = matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[2][0] * matrix[1][1]);
return a + b + c;
}
double Triangle::intersectionParameter(Ray *ray) {
Vector &rayDirection = ray->getDirection();
Point &rayStart = ray->getStart();
for (int i = 0; i < 3; i++) matrix[i][0] = vertices[0][i] - vertices[1][i];
for (int i = 0; i < 3; i++) matrix[i][1] = vertices[0][i] - vertices[2][i];
for (int i = 0; i < 3; i++) matrix[i][2] = rayDirection[i];
detA = matrixDeterminant();
if (fabs(detA) < EPS) return -1;
x = vertices[0][0] - rayStart[0];
y = vertices[0][1] - rayStart[1];
z = vertices[0][2] - rayStart[2];
matrix[0][0] = x;
matrix[1][0] = y;
matrix[2][0] = z;
beta = matrixDeterminant() / detA;
if (beta <= 0) return -1;
for (int i = 0; i < 3; i++) matrix[i][0] = vertices[0][i] - vertices[1][i];
matrix[0][1] = x;
matrix[1][1] = y;
matrix[2][1] = z;
gamma = matrixDeterminant() / detA;
if (gamma <= 0 || beta + gamma >= 1) return -1;
for (int i = 0; i < 3; i++) matrix[i][1] = vertices[0][i] - vertices[2][i];
matrix[0][2] = x;
matrix[1][2] = y;
matrix[2][2] = z;
t = matrixDeterminant() / detA;
if (t <= 0) return -1;
return t;
}
Ray Triangle::getNormal(Point &intersectionPoint, Ray &incidentRay) {
Ray normalRay;
normalRay.setStart(intersectionPoint);
if (Vector::dotProduct(normal, incidentRay.getDirection()) > 0) {
normalRay.setDirection(-normal);
} else {
normalRay.setDirection(normal);
}
return normalRay;
}
#endif // TRIANGLE