-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.java
More file actions
78 lines (58 loc) · 2.37 KB
/
Triangle.java
File metadata and controls
78 lines (58 loc) · 2.37 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class Triangle extends Object3d {
public Vertex v1, v2, v3;
public Triangle(Vertex v1, Vertex v2, Vertex v3, Material material) {
super(material);
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
public Vector3 toBarycentricCoordinates(Vector3 point) {
Vector3 edge1 = this.v2.position.subtract(this.v1.position);
Vector3 edge2 = this.v3.position.subtract(this.v1.position);
Vector3 v2 = point.subtract(this.v1.position);
double d00 = edge1.dot(edge1);
double d01 = edge1.dot(edge2);
double d11 = edge2.dot(edge2);
double d20 = v2.dot(edge1);
double d21 = v2.dot(edge2);
double denominator = d00 * d11 - d01 * d01;
double v = (d11 * d20 - d01 * d21) / denominator;
double w = (d00 * d21 - d01 * d20) / denominator;
double u = 1.0 - v - w;
return new Vector3(u, v, w);
}
public Vector3 normal(Vector3 point) {
/*Vector3 barycentricCoordinates = this.toBarycentricCoordinates(point);
Vector3 normal1 = this.v1.normal.multiply(barycentricCoordinates.x);
Vector3 normal2 = this.v2.normal.multiply(barycentricCoordinates.y);
Vector3 normal3 = this.v3.normal.multiply(barycentricCoordinates.z);
return normal1.add(normal2).add(normal3);*/
return this.v1.normal.normalize();
}
public Vector3 intersection(Ray ray) {
// We are using the Moller-Trumbore intersection algorithm
double epsilon = 0.0000001;
Vector3 edge1 = this.v2.position.subtract(this.v1.position);
Vector3 edge2 = this.v3.position.subtract(this.v1.position);
Vector3 h = ray.direction.cross(edge2);
double a = edge1.dot(h);
if (a > -epsilon && a < epsilon)
return null;
double f = 1.0 / a;
Vector3 s = ray.position.subtract(this.v1.position);
double u = f * s.dot(h);
if (u < 0.0 || u > 1.0)
return null;
Vector3 q = s.cross(edge1);
double v = f * ray.direction.dot(q);
if (v < 0.0 || u + v > 1.0)
return null;
double t = f * edge2.dot(q);
if (t > epsilon)
return ray.pointAt(t);
return null;
}
public String toString() {
return String.format("[%s, %s, %s]", this.v1, this.v2, this.v3);
}
}