Repository navigation
Expand file tree
/
Copy pathtriangle.cpp
More file actions
58 lines (46 loc) · 1.44 KB
/
Copy pathtriangle.cpp
File metadata and controls
58 lines (46 loc) · 1.44 KB
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
#include "triangle.h"
#include "intersection.h"
#include "utilities.h" // For EPSILON and equal()
#include <cmath>
Triangle::Triangle(RayPoint p1, RayPoint p2, RayPoint p3,
const Material &material)
: Shape(material), p1(p1), p2(p2), p3(p3), e1(p2 - p1), e2(p3 - p1),
normal((p3 - p1).cross(p2 - p1).normalize()) {
e1 = p2 - p1;
e2 = p3 - p1;
normal = e2.cross(e1).normalize();
}
RayVector Triangle::local_normal_at(const RayPoint &,
const Intersection *) const {
return normal;
}
BoundingBox Triangle::bounds_of() const {
BoundingBox box;
box.add(p1);
box.add(p2);
box.add(p3);
return box;
}
std::vector<Intersection>
Triangle::local_intersects(const Ray &local_ray) const {
std::vector<Intersection> intersections;
RayVector dir_cross_e2 = local_ray.direction.cross(e2);
double det = e1.dot(dir_cross_e2);
if (std::abs(det) < EPSILON) {
return intersections;
}
double f = 1.0 / det;
RayVector p1_to_origin = local_ray.origin - p1;
double u = f * p1_to_origin.dot(dir_cross_e2);
if (u < 0 || u > 1) {
return intersections;
}
RayVector origin_cross_e1 = p1_to_origin.cross(e1);
double v = f * local_ray.direction.dot(origin_cross_e1);
if (v < 0 || (u + v) > 1) {
return intersections;
}
double t = f * e2.dot(origin_cross_e1);
intersections.push_back(Intersection(t, const_cast<Triangle *>(this), u, v));
return intersections;
}