-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalkMesh.cpp
More file actions
325 lines (307 loc) · 9.37 KB
/
WalkMesh.cpp
File metadata and controls
325 lines (307 loc) · 9.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "WalkMesh.hpp"
#include "read_chunk.hpp"
#include <glm/glm.hpp>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <cstddef>
WalkMesh::WalkMesh(std::string const &filename){
std::ifstream file(filename, std::ios::binary);
read_chunk(file, "vtc0", &vertices);
read_chunk(file, "nml0", &normals);
read_chunk(file, "ply0", &triangles);
//construct next_vertex map
for (auto& triangle : triangles) {
next_vertex[glm::uvec2(triangle.x, triangle.y)] = triangle.z;
next_vertex[glm::uvec2(triangle.y, triangle.z)] = triangle.x;
next_vertex[glm::uvec2(triangle.z, triangle.x)] = triangle.y;
}
}
//source https://gist.github.com/joshuashaffer/99d58e4ccbd37ca5d96e
//source https://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistPointTriangleExact.h
void WalkMesh::closestpt2triangle(std::vector<glm::vec3> &trianglePoints, glm::vec3 const &position, glm::vec3 &closestPoint) const{
glm::vec3 E0 = trianglePoints[1] - trianglePoints[0];
glm::vec3 E1 = trianglePoints[2] - trianglePoints[0];
glm::vec3 v0 = trianglePoints[0] - position;
float a = glm::dot(E0, E0);
float b = glm::dot(E0, E1);
float c = glm::dot(E1, E1);
float d = glm::dot(E0, v0);
float e = glm::dot(E1, v0);
float det = a * c - b * b;
float s = b * e - c * d;
float t = b * d - a * e;
// std::cout << "s: " << s << "t: " << t << "det: " << det << std::endl;
if (s + t <= det)
{
if (s < 0)
{
if (t < 0) // region 4
{
if (d < 0)
{
t = 0;
if (-d >= a) // V1
{
s = 1;
}
else // E01
{
s = -d / a;
}
}
else
{
s = 0;
if (e >= 0) // V0
{
t = 0;
}
else if (-e >= c) // V2
{
t = 1;
}
else // E20
{
t = -e / c;
}
}
}
else // region 3
{
s = 0;
if (e >= 0) // V0
{
t = 0;
}
else if (-e >= c) // V2
{
t = 1;
}
else // E20
{
t = -e / c;
}
}
}
else if (t < 0) // region 5
{
t = 0;
if (d >= 0) // V0
{
s = 0;
}
else if (-d >= a) // V1
{
s = 1;
}
else // E01
{
s = -d / a;
}
}
else // region 0, interior
{
float invDet = 1 / det;
s *= invDet;
t *= invDet;
}
}
else
{
float tmp0, tmp1, numer, denom;
if (s < 0) // region 2
{
tmp0 = b + d;
tmp1 = c + e;
if (tmp1 > tmp0)
{
numer = tmp1 - tmp0;
denom = a - ((float)2)*b + c;
if (numer >= denom) // V1
{
s = 1;
t = 0;
}
else // E12
{
s = numer / denom;
t = 1 - s;
}
}
else
{
s = 0;
if (tmp1 <= 0) // V2
{
t = 1;
}
else if (e >= 0) // V0
{
t = 0;
}
else // E20
{
t = -e / c;
}
}
}
else if (t < 0) // region 6
{
tmp0 = b + e;
tmp1 = a + d;
if (tmp1 > tmp0)
{
numer = tmp1 - tmp0;
denom = a - ((float)2)*b + c;
if (numer >= denom) // V2
{
t = 1;
s = 0;
}
else // E12
{
t = numer / denom;
s = 1 - t;
}
}
else
{
t = 0;
if (tmp1 <= 0) // V1
{
s = 1;
}
else if (d >= 0) // V0
{
s = 0;
}
else // E01
{
s = -d / a;
}
}
}
else // region 1
{
numer = c + e - b - d;
if (numer <= 0) // V2
{
s = 0;
t = 1;
}
else
{
denom = a - ((float)2)*b + c;
if (numer >= denom) // V1
{
s = 1;
t = 0;
}
else // 12
{
s = numer / denom;
t = 1 - s;
}
}
}
}
closestPoint = trianglePoints[0] + s * E0 + t * E1;
// std::cout << "the return value of closest point to triangle" << closestPoint[0] << closestPoint[1] << closestPoint[2] << std::endl;
}
//source https://gamedev.stackexchange.com/questions/23743/whats-the-most-efficient-way-to-find-barycentric-coordinates
void WalkMesh::barycentric(glm::vec3 p, glm::vec3 a, glm::vec3 b, glm::vec3 c, float &u, float &v, float &w) const{
glm::vec3 v0 = b - a, v1 = c - a, v2 = p - a;
float d00 = glm::dot(v0, v0);
float d01 = glm::dot(v0, v1);
float d11 = glm::dot(v1, v1);
float d20 = glm::dot(v2, v0);
float d21 = glm::dot(v2, v1);
float denom = d00 * d11 - d01 * d01;
v = (d11 * d20 - d01 * d21) / denom;
w = (d00 * d21 - d01 * d20) / denom;
u = 1.0f - v - w;
}
WalkMesh::WalkPoint WalkMesh::start(glm::vec3 const &world_point) const {
WalkPoint closest;
//TODO: iterate through triangles
//TODO: for each triangle, find closest point on triangle to world_point
//TODO: if point is closest, closest.triangle gets the current triangle, closest.weights gets the barycentric coordinates
// std::cout << "the length of triangles: " << triangles.size() << std::endl;
std::vector< glm::vec3 > trianglePoints;
trianglePoints.push_back(glm::vec3(0.0f));
trianglePoints.push_back(glm::vec3(0.0f));
trianglePoints.push_back(glm::vec3(0.0f));
int counter = 0;
float dis;
float min = std::numeric_limits<float>::max();
for(auto& triangle : triangles){
trianglePoints[0] = vertices[triangle[0]];
trianglePoints[1] = vertices[triangle[1]];
trianglePoints[2] = vertices[triangle[2]];
glm::vec3 closestPoint;
WalkMesh::closestpt2triangle(trianglePoints, world_point, closestPoint);
dis = glm::distance(closestPoint, world_point);
// std::cout << min << " " << dis /<< std::endl;
if(dis < min){
counter++;
min = dis;
closest.triangle = triangle;
float u = 0.f;
float v = 0.f;
float w = 0.f;
WalkMesh::barycentric(closestPoint, vertices[triangle[0]], vertices[triangle[1]],
vertices[triangle[2]], u, v, w);
// std::cout << u << " " << v << " " << w << std::endl;
closest.weights = glm::vec3(u, v, w);
}
}
// std::cout << "DEBUG " << closest.triangle[0] << std::endl;
// std::cout << counter << std::endl;
return closest;
}
void WalkMesh::walk(WalkPoint &wp, glm::vec3 const &step) const {
//TODO: project step to barycentric coordinates to get weights_step
glm::vec3 post_point = world_point(wp) + glm::vec3(1.0);
glm::vec3 weights_step;
float u = 0.f;
float v = 0.f;
float w = 0.f;
WalkMesh::barycentric(post_point, vertices[wp.triangle[0]], vertices[wp.triangle[1]],
vertices[wp.triangle[2]], u, v, w);
weights_step = glm::vec3(u, v, w) + wp.weights;
// std::cout << u << " " << v << " " << w << std::endl;
//TODO: when does wp.weights + t * weights_step cross a triangle edge?
// float t = 1.0f;
// t = weights_step[0] + weights_step[1] + weights_step[2];
if (u >= 0 && v >= 0 && w >=0) { //if a triangle edge is not crossed
//TODO: wp.weights gets moved by weights_step, nothing else needs to be d1.
wp.weights = glm::vec3(u, v, w);
} else { //if a triangle edge is crossed
// WalkPoint new_wp;
//find which edge has been crossed
//reference eric1221bday
float reduced_coeff = 0.f;
glm::uvec2 crossed_edge;
if (u < 0) {
//cross point 1 and 2
reduced_coeff = wp.weights.x / -weights_step.x;
crossed_edge = glm::uvec2(wp.triangle[2], wp.triangle[1]);
} else if (v < 0) {
reduced_coeff = wp.weights.y / -weights_step.y;
crossed_edge = glm::uvec2(wp.triangle[0], wp.triangle[2]);
} else {
//w < 0, where the edge fromed by point 0 and point 1 has been crossed
reduced_coeff = wp.weights.z / -weights_step.z;
crossed_edge = glm::uvec2(wp.triangle[1], wp.triangle[0]);
}
//find the next triangle
if (next_vertex.find(crossed_edge) != next_vertex.end()) {
std::cout << "find edge" << std::endl;
// auto third_vertex = next_vertex.at(crossed_edge);
}
}
}