-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTriangle.hpp
186 lines (136 loc) · 4.76 KB
/
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#pragma once
#include "Plane.hpp"
#include "Shape.hpp"
#include "Mat4.hpp"
struct CudaTriangleAttributes : Shape {
Vec3 normals[3];
CUDA_CALLABLE Vec3 calculateNormal(float u, float v) const {
return normals[0] * (1.0 - u - v) + normals[1] * u + normals[2] * v;
}
};
struct CudaTriangle {
float data[0];
CUDA_CALLABLE Vec3 getVertex(int vertex) const {
return Vec3(
data[vertex * 3 + 0],
data[vertex * 3 + 1],
data[vertex * 3 + 2]
);
}
CUDA_CALLABLE void setVertex(int vertex, Vec3 v) {
data[vertex * 3 + 0] = v.x;
data[vertex * 3 + 1] = v.y;
data[vertex * 3 + 2] = v.z;
}
CUDA_CALLABLE Plane getPlane() const {
return Plane(data[9], data[10], data[11], data[12]);
}
void setPlane(Plane p) {
data[9] = p.normal.x;
data[10] = p.normal.y;
data[11] = p.normal.z;
data[12] = p.d;
}
CUDA_CALLABLE int calculateRayIntersections(const Ray& ray, CudaTriangleIntersection* intersectDest) {
Vec3 p[3] = {
getVertex(0),
getVertex(1),
getVertex(2)
};
Vec3 u = (p[1] - p[0]);
Vec3 v = (p[2] - p[0]);
Plane plane = getPlane();
if(!plane.calculateRayIntersection(ray, intersectDest->pos))
return 0;
if(ray.dir.neg().dot(plane.normal) < 0)
return 0;
Vec3 w = (intersectDest->pos - p[0]);
float uv = u.dot(v);
float uu = u.dot(u);
float vv = v.dot(v);
float wv = w.dot(v);
float wu = w.dot(u);
float d = uv * uv - uu * vv;
if(d == 0)
return 0;
float s1 = (uv * wv - vv * wu) / d;
float t1 = (uv * wu - uu * wv) / d;
if(s1 < 0 || s1 > 1.0 || t1 < 0 || (s1 + t1) > 1.0)
return 0;
intersectDest->triangleStart = data;
intersectDest->distanceFromRayStartSquared = (intersectDest->pos - ray.v[0]).lengthSquared();
intersectDest->intersectionS = s1;
intersectDest->intersectionT = t1;
return 1;
}
};
struct Triangle : Shape {
Vec3 p[3];
Plane plane;
Vec3 normals[3];
CUDA_CALLABLE Triangle() { }
CUDA_CALLABLE Triangle(Vec3 p0, Vec3 p1, Vec3 p2) {
p[0] = p0;
p[1] = p1;
p[2] = p2;
updatePlaneEquation();
normals[0] = plane.normal;
normals[1] = plane.normal;
normals[2] = plane.normal;
}
void updatePlaneEquation() {
Vec3 u = (p[1] - p[0]).normalize();
Vec3 v = (p[2] - p[0]).normalize();
plane = Plane(p[0], u, v);
}
void flipUpsideDown() {
for(int i = 0; i < 3; ++i) {
p[i].y = -p[i].y;
normals[i].y = -normals[i].y;
}
}
void transform(Mat4 mat) {
for(int i = 0; i < 3; ++i) {
p[i] = mat.rotateVec3(p[i]);
normals[i] = mat.rotateVec3Normal(normals[i]);
}
updatePlaneEquation();
}
CUDA_CALLABLE int calculateRayIntersections(const Ray& ray, Intersection<Triangle>* intersectDest) const {
Vec3 u = (p[1] - p[0]);
Vec3 v = (p[2] - p[0]);
if(!plane.calculateRayIntersection(ray, intersectDest->pos))
return 0;
//cout << intersectDest.toString() << endl;
if(ray.dir.neg().dot(plane.normal) < 0)
return 0;
Vec3 w = (intersectDest->pos - p[0]);
float uv = u.dot(v);
float uu = u.dot(u);
float vv = v.dot(v);
float wv = w.dot(v);
float wu = w.dot(u);
float d = uv * uv - uu * vv;
if(d == 0)
return 0;
float s1 = (uv * wv - vv * wu) / d;
float t1 = (uv * wu - uu * wv) / d;
if(s1 < 0 || s1 > 1.0 || t1 < 0 || (s1 + t1) > 1.0)
return 0;
intersectDest->normal = calculateNormal(s1, t1); //calculateNormalAtPoint(intersectDest->pos);
intersectDest->shape = this;
intersectDest->distanceFromRayStartSquared = (intersectDest->pos - ray.v[0]).lengthSquared();
return 1;
}
CUDA_CALLABLE void setNormals(Vec3 n0, Vec3 n1, Vec3 n2) {
normals[0] = n0;
normals[1] = n1;
normals[2] = n2;
}
CUDA_CALLABLE Vec3 calculateNormal(float u, float v) const {
return normals[0] * (1.0 - u - v) + normals[1] * u + normals[2] * v;
}
CUDA_CALLABLE Vec3 calculateNormalAtPoint(Vec3& point) const {
return plane.normal;
}
};