-
Notifications
You must be signed in to change notification settings - Fork 0
/
Triangle.cpp
96 lines (87 loc) · 2.48 KB
/
Triangle.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "Triangle.h"
#include "Ray.h"
#include "Intersect.h"
#include <iostream>
#include "Vertex.h"
Triangle::Triangle(Vertex::Vertex va, Vertex::Vertex vb, Vertex::Vertex vc) {
v1 = va;
v2 = vb;
v3 = vc;
std::vector<float> normal(3);
normal[0] = (v1.getY()*v2.getZ() - v1.getZ()*v2.getY());
normal[1] = (v1.getX()*v2.getZ() - v1.getZ()*v2.getX());
normal[2] = (v1.getX()*v2.getY() - v1.getY()*v2.getX());
}
Vertex Triangle::getVertex(int n) const{
if (n == 0) {
return v1;
}
else if (n == 1) {
return v2;
}
else if (n == 2){
return v3;
}
}
std::vector<float> Triangle::getNormal() const {
return normal;
}
Intersect Triangle::intersect(Ray r) {
Intersect::Intersect ret = *new Intersect::Intersect();
float a = v1.getX() - v2.getX();
float b = v1.getY() - v2.getY();
float c = v1.getZ() - v2.getZ();
float d = v1.getX() - v3.getX();
float e = v1.getY() - v3.getY();
float f = v1.getZ() - v3.getZ();
float g = r.getDir().at(0);
float h = r.getDir().at(1);
float i = r.getDir().at(2);
float j = v1.getX() - r.getEye().at(0);
float k = v1.getY() - r.getEye().at(1);
float l = v1.getZ() - r.getEye().at(2);
float eiminushf = e*i - h*f;
float gfminusdi = g*f - d*i;
float dhminuseg = d*h - e*g;
float akminusjb = a*k - j*b;
float jcminusal = j*c - a*l;
float blminuskc = b*l - k*c;
float m = a*(eiminushf) + b*(gfminusdi) + c*(dhminuseg);
float beta = (j*(eiminushf) + k*(gfminusdi) + l*(dhminuseg))/m;
if (beta < 0) {return ret;}
float gamma = (i*(akminusjb) + h*(jcminusal) + g*(blminuskc))/m;
if (gamma < 0 || beta+gamma > 1) {return ret;}
float t = (-1)*(f*(akminusjb) + e*(jcminusal) + d*(blminuskc))/m;
std::vector<float> p;
std::vector<float> vec1 = v1.toVec();
std::vector<float> vec2 = v2.toVec();
std::vector<float> vec3 = v3.toVec();
std::vector<float> sub1 = vSub(&vec2, &vec1);
std::vector<float> sub2 = vSub(&vec3, &vec1);
std::vector<float> scaled1 = vScale(&beta, &sub1);
std::vector<float> scaled2 = vScale(&gamma, &sub2);
std::vector<float> added1 = vAdd(&vec1, &scaled1);
p = vAdd(&added1, &scaled2);
ret.setPoint(p);
return ret;
}
int main() {
Vertex a = *new Vertex::Vertex(0,0,0);
Vertex b = *new Vertex::Vertex(1,0,0);
Vertex c = *new Vertex::Vertex(0,1,0);
Triangle t = *new Triangle::Triangle(a, b, c);
std::vector<float> p(3);
p[0] = 0;
p[1] = 0;
p[2] = 0;
std::vector<float> e(3);
e[0] = 0;
e[1] = 0;
e[2] = 3;
Ray::Ray r = *new Ray::Ray(e, p);
Intersect i = t.intersect(r);
if (i.isHit()) {
std::cout<<"OKAY\n";
}
return 0;
}