-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprintIntersections.cpp
More file actions
342 lines (278 loc) · 15.8 KB
/
printIntersections.cpp
File metadata and controls
342 lines (278 loc) · 15.8 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "printIntersections.h"
/**
* Print the triangles, the intersection points and the intersection edges on the GUI
* @param t00
* @param t01
* @param t02
* @param t10
* @param t11
* @param t12
* @param intersections - the intersections result struct
* @param printableIntersections - the struct containing the drawable objects and the GUI
*/
void printTrianglesIntersections(const double * t00, const double * t01, const double * t02, const double * t10, const double * t11, const double * t12, intersectionResult &intersections, printableIntersections &printableIntersections){
std::array<const double*, 3> t0 = {t00, t01, t02};
std::array<const double*, 3> t1 = {t10, t11, t12};
int n = intersections.getIntersectionPointsSize();
//calculate the intersection points
std::vector<std::vector<double>> explicitIntersectionPoints;
for(int i = 0; i < n; i ++){
switch (intersections.intersectionsPoints[i * 3]){
case VA_ON_VB:{
int index = intersections.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0[index][0], t0[index][1], t0[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VA_ON_EB:{
int index = intersections.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0[index][0], t0[index][1], t0[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VB_ON_EA:{
int index = intersections.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t1[index][0], t1[index][1], t1[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;
}
case VA_ON_TB:{
int index = intersections.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0[index][0], t0[index][1], t0[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VB_ON_TA:{
int index = intersections.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t1[index][0], t1[index][1], t1[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case EA_CROSS_EB:{
int p0 = intersections.intersectionsPoints[i * 3 + 1];
int p1 = intersections.intersectionsPoints[i * 3 + 2];
cinolib::vec3d v00 = cinolib::vec3d(t0[p0][0], t0[p0][1], t0[p0][2]);
cinolib::vec3d v01 = cinolib::vec3d(t0[(p0+1)%3][0], t0[(p0+1)%3][1], t0[(p0+1)%3][2]);
cinolib::vec3d v10 = cinolib::vec3d(t1[p1][0], t1[p1][1], t1[p1][2]);
cinolib::vec3d v11 = cinolib::vec3d(t1[(p1+1)%3][0], t1[(p1+1)%3][1], t1[(p1+1)%3][2]);
cinolib::vec3d intersectionPoint = cinolib::segment_intersection(v00, v01, v10, v11);
std::vector<double> tmp = {intersectionPoint[0], intersectionPoint[1], intersectionPoint[2]};
explicitIntersectionPoints.push_back(tmp);
break;
}
case EA_CROSS_TB:{
int e_index = intersections.intersectionsPoints[i * 3 + 1];
double v0[3] = {t0[e_index][0], t0[e_index][1], t0[e_index][2]};
double v1[3] = {t0[(e_index+1)%3][0], t0[(e_index+1)%3][1], t0[(e_index+1)%3][2]};
// plane vectors
std::array<double, 3> vec1 = {t1[1][0] - t1[0][0], t1[1][1] - t1[0][1], t1[1][2] - t1[0][2]};
std::array<double, 3> vec2 = {t1[2][0] - t1[0][0], t1[2][1] - t1[0][1], t1[2][2] - t1[0][2]};
// plane normal
std::array<double, 3> normal = {
vec1[1] * vec2[2] - vec1[2] * vec2[1],
vec1[2] * vec2[0] - vec1[0] * vec2[2],
vec1[0] * vec2[1] - vec1[1] * vec2[0]
};
// plane d
double d = -(normal[0] * t1[0][0] + normal[1] * t1[0][1] + normal[2] * t1[0][2]);
// segment direction
std::array<double, 3> dir = { v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]};
// calculate t param
double num = -(normal[0] * v0[0] + normal[1] * v0[1] + normal[2] * v0[2] + d);
double den = normal[0] * dir[0] + normal[1] * dir[1] + normal[2] * dir[2];
double t = num / den;
// calculate intersection point
std::vector<double> p = {
v0[0] + t * dir[0],
v0[1] + t * dir[1],
v0[2] + t * dir[2]
};
explicitIntersectionPoints.push_back(p);
break;}
case EB_CROSS_TA: {
int e_index = intersections.intersectionsPoints[i * 3 + 1];
double v0[3] = {t1[e_index][0], t1[e_index][1], t1[e_index][2]};
double v1[3] = {t1[(e_index+1)%3][0], t1[(e_index+1)%3][1], t1[(e_index+1)%3][2]};
// plane vectors
std::array<double, 3> tmp1 = {t0[1][0] - t0[0][0], t0[1][1] - t0[0][1], t0[1][2] - t0[0][2]};
std::array<double, 3> tmp2 = {t0[2][0] - t0[0][0], t0[2][1] - t0[0][1], t0[2][2] - t0[0][2]};
// plane normal
std::array<double, 3> normal = {
tmp1[1] * tmp2[2] - tmp1[2] * tmp2[1],
tmp1[2] * tmp2[0] - tmp1[0] * tmp2[2],
tmp1[0] * tmp2[1] - tmp1[1] * tmp2[0]
};
// d plane
double d = -(normal[0] * t0[0][0] + normal[1] * t0[0][1] + normal[2] * t0[0][2]);
// segment direction
std::array<double, 3> dir = { v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]};
// calculate t param
double num = -(normal[0] * v0[0] + normal[1] * v0[1] + normal[2] * v0[2] + d);
double den = normal[0] * dir[0] + normal[1] * dir[1] + normal[2] * dir[2];
double t = num / den;
//intersection point
std::vector<double> p = {
v0[0] + t * dir[0],
v0[1] + t * dir[1],
v0[2] + t * dir[2]
};
explicitIntersectionPoints.push_back(p);
break;
}
}
}
//print the triangles on the GUI
for(int i = 0; i < 3 ; i++){
cinolib::Marker m0,m1;
m0.pos_3d = cinolib::vec3d(t0[i][0], t0[i][1], t0[i][2]);
m0.text = "t0" + std::to_string(i);
printableIntersections.markers->push_back(m0);
m1.pos_3d = cinolib::vec3d(t1[i][0], t1[i][1], t1[i][2]);
m1.text = "t1" + std::to_string(i);
printableIntersections.markers->push_back(m1);
}
printableIntersections.gui->push(printableIntersections.triangles);
//print the intersection points
n = intersections.getIntersectionPointsSize();
for(int i = 0; i < n; i++){
cinolib::Marker m;
m.pos_3d = cinolib::vec3d(explicitIntersectionPoints[i][0], explicitIntersectionPoints[i][1], explicitIntersectionPoints[i][2]);
m.text = "p" + std::to_string(i);
m.color = cinolib::Color::RED();
printableIntersections.markers->push_back(m);
}
printableIntersections.gui->markers = *printableIntersections.markers;
//print the intersection edges
n = (int)intersections.intersectionsEdges.size()/2;
for(int e =0; e < n; e++){
int v0_index = intersections.intersectionsEdges[e * 2];
int v1_index = intersections.intersectionsEdges[e * 2 + 1];
cinolib::vec3d v0 = cinolib::vec3d(explicitIntersectionPoints[v0_index][0], explicitIntersectionPoints[v0_index][1], explicitIntersectionPoints[v0_index][2]);
cinolib::vec3d v1 = cinolib::vec3d(explicitIntersectionPoints[v1_index][0], explicitIntersectionPoints[v1_index][1], explicitIntersectionPoints[v1_index][2]);
printableIntersections.segments->push_seg(v0, v1);
}
printableIntersections.gui->push(printableIntersections.segments);
}
void printTrianglesIntersections(const std::vector<double> &t0, const std::vector<double> &t1, intersectionResult &intersections, printableIntersections &printableIntersections){
printTrianglesIntersections(t0.data(), t0.data() + 3, t0.data() + 6, t1.data(), t1.data() + 3, t1.data() + 6, intersections, printableIntersections);
}
void drawIntersections(cinolib::DrawableTrimesh<> &m ,cinolib::DrawableSegmentSoup &s ,cinolib::GLcanvas &gui, uint t0, uint t1, intersectionResult &intersection ){
std::vector<cinolib::vec3d> t0_verts = m.poly_verts(t0);
std::vector<cinolib::vec3d> t1_verts = m.poly_verts(t1);
int n = intersection.getIntersectionPointsSize();
//calculate the intersection points
std::vector<std::vector<double>> explicitIntersectionPoints;
for(int i = 0; i < n; i ++){
switch (intersection.intersectionsPoints[i * 3]){
case VA_ON_VB:{
int index = intersection.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0_verts[index][0], t0_verts[index][1], t0_verts[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VA_ON_EB:{
int index = intersection.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0_verts[index][0], t0_verts[index][1], t0_verts[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VB_ON_EA:{
int index = intersection.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t1_verts[index][0], t1_verts[index][1], t1_verts[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;
}
case VA_ON_TB:{
int index = intersection.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t0_verts[index][0], t0_verts[index][1], t0_verts[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case VB_ON_TA:{
int index = intersection.intersectionsPoints[i * 3 + 1];
std::vector<double> tmp = {t1_verts[index][0], t1_verts[index][1], t1_verts[index][2]};
explicitIntersectionPoints.push_back(tmp);
break;}
case EA_CROSS_EB:{
int p0 = intersection.intersectionsPoints[i * 3 + 1];
int p1 = intersection.intersectionsPoints[i * 3 + 2];
cinolib::vec3d v00 = cinolib::vec3d(t0_verts[p0][0], t0_verts[p0][1], t0_verts[p0][2]);
cinolib::vec3d v01 = cinolib::vec3d(t0_verts[(p0+1)%3][0], t0_verts[(p0+1)%3][1], t0_verts[(p0+1)%3][2]);
cinolib::vec3d v10 = cinolib::vec3d(t1_verts[p1][0], t1_verts[p1][1], t1_verts[p1][2]);
cinolib::vec3d v11 = cinolib::vec3d(t1_verts[(p1+1)%3][0], t1_verts[(p1+1)%3][1], t1_verts[(p1+1)%3][2]);
cinolib::vec3d intersectionPoint = cinolib::segment_intersection(v00, v01, v10, v11);
std::vector<double> tmp = {intersectionPoint[0], intersectionPoint[1], intersectionPoint[2]};
explicitIntersectionPoints.push_back(tmp);
break;
}
case EA_CROSS_TB:{
int e_index = intersection.intersectionsPoints[i * 3 + 1];
double v0[3] = {t0_verts[e_index][0], t0_verts[e_index][1], t0_verts[e_index][2]};
double v1[3] = {t0_verts[(e_index+1)%3][0], t0_verts[(e_index+1)%3][1], t0_verts[(e_index+1)%3][2]};
// plane vectors
std::array<double, 3> vec1 = {t1_verts[1][0] - t1_verts[0][0], t1_verts[1][1] - t1_verts[0][1], t1_verts[1][2] - t1_verts[0][2]};
std::array<double, 3> vec2 = {t1_verts[2][0] - t1_verts[0][0], t1_verts[2][1] - t1_verts[0][1], t1_verts[2][2] - t1_verts[0][2]};
// plane normal
std::array<double, 3> normal = {
vec1[1] * vec2[2] - vec1[2] * vec2[1],
vec1[2] * vec2[0] - vec1[0] * vec2[2],
vec1[0] * vec2[1] - vec1[1] * vec2[0]
};
// plane d
double d = -(normal[0] * t1_verts[0][0] + normal[1] * t1_verts[0][1] + normal[2] * t1_verts[0][2]);
// segment direction
std::array<double, 3> dir = { v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]};
// calculate t param
double num = -(normal[0] * v0[0] + normal[1] * v0[1] + normal[2] * v0[2] + d);
double den = normal[0] * dir[0] + normal[1] * dir[1] + normal[2] * dir[2];
double t = num / den;
// calculate intersection point
std::vector<double> p = {
v0[0] + t * dir[0],
v0[1] + t * dir[1],
v0[2] + t * dir[2]
};
explicitIntersectionPoints.push_back(p);
break;}
case EB_CROSS_TA: {
int e_index = intersection.intersectionsPoints[i * 3 + 1];
double v0[3] = {t1_verts[e_index][0], t1_verts[e_index][1], t1_verts[e_index][2]};
double v1[3] = {t1_verts[(e_index+1)%3][0], t1_verts[(e_index+1)%3][1], t1_verts[(e_index+1)%3][2]};
// plane vectors
std::array<double, 3> tmp1 = {t0_verts[1][0] - t0_verts[0][0], t0_verts[1][1] - t0_verts[0][1], t0_verts[1][2] - t0_verts[0][2]};
std::array<double, 3> tmp2 = {t0_verts[2][0] - t0_verts[0][0], t0_verts[2][1] - t0_verts[0][1], t0_verts[2][2] - t0_verts[0][2]};
// plane normal
std::array<double, 3> normal = {
tmp1[1] * tmp2[2] - tmp1[2] * tmp2[1],
tmp1[2] * tmp2[0] - tmp1[0] * tmp2[2],
tmp1[0] * tmp2[1] - tmp1[1] * tmp2[0]
};
// d plane
double d = -(normal[0] * t0_verts[0][0] + normal[1] * t0_verts[0][1] + normal[2] * t0_verts[0][2]);
// segment direction
std::array<double, 3> dir = { v1[0] - v0[0], v1[1] - v0[1], v1[2] - v0[2]};
// calculate t param
double num = -(normal[0] * v0[0] + normal[1] * v0[1] + normal[2] * v0[2] + d);
double den = normal[0] * dir[0] + normal[1] * dir[1] + normal[2] * dir[2];
double t = num / den;
//intersection point
std::vector<double> p = {
v0[0] + t * dir[0],
v0[1] + t * dir[1],
v0[2] + t * dir[2]
};
explicitIntersectionPoints.push_back(p);
break;
}
}
}
n = intersection.getIntersectionPointsSize();
for(int i = 0; i < n; i++){
cinolib::Marker marker;
marker.pos_3d = cinolib::vec3d(explicitIntersectionPoints[i][0], explicitIntersectionPoints[i][1], explicitIntersectionPoints[i][2]);;
marker.color = cinolib::Color::RED();
gui.markers.push_back(marker);
}
//print the intersection edges
n = (int)intersection.intersectionsEdges.size()/2;
for(int e =0; e < n; e++){
int v0_index = intersection.intersectionsEdges[e * 2];
int v1_index = intersection.intersectionsEdges[e * 2 + 1];
cinolib::vec3d v0 = cinolib::vec3d(explicitIntersectionPoints[v0_index][0], explicitIntersectionPoints[v0_index][1], explicitIntersectionPoints[v0_index][2]);
cinolib::vec3d v1 = cinolib::vec3d(explicitIntersectionPoints[v1_index][0], explicitIntersectionPoints[v1_index][1], explicitIntersectionPoints[v1_index][2]);
s.push_seg(v0, v1);
}
}