Debian Codegraph @

tools/physicalModeling/mesh2faust/vega/libraries/wildMagic/geometryQuery/verticesQuery.cpp

raw symbols SCIP
1 /*
2 This code is based on code from the Geometric Tools library,
3 which is licensed under a boost license.
4 Such usage is permitted by the boost license; for details,
5 please see the boost license below.
6 */
7
8 // Geometric Tools, LLC
9 // Copyright (c) 1998-2014
10 // Distributed under the Boost Software License, Version 1.0.
11 // http://www.boost.org/LICENSE_1_0.txt
12 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
13
14 /*************************************************************************
15 * *
16 * We release our improvements to the wildMagic code under our standard *
17 * Vega FEM license, as follows: *
18 * *
19 * Vega FEM Simulation Library Version 3.1 *
20 * *
21 * "improvements to the wildMagic library" , Copyright (C) 2016 USC *
22 * All rights reserved. *
23 * *
24 * Code author: Yijing Li *
25 * http://www.jernejbarbic.com/code *
26 * *
27 * Funding: National Science Foundation *
28 * *
29 * This library is free software; you can redistribute it and/or *
30 * modify it under the terms of the BSD-style license that is *
31 * included with this library in the file LICENSE.txt *
32 * *
33 * This library is distributed in the hope that it will be useful, *
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *
36 * LICENSE.TXT for more details. *
37 * *
38 *************************************************************************/
39
40 #include "verticesQuery.h"
41 #include <cassert>
42 #include <cstring>
43 #include <iostream>
44 #include <cmath>
45 using namespace std;
46
47 VerticesQuery::VerticesQuery (int numVertices, const Vec3d* v)
48 {
49 assert(numVertices > 0 && v);
50 vertices.resize(numVertices);
51 memcpy(vertices.data(), v, sizeof(Vec3d) * numVertices);
52 }
53
54
55 VerticesQuery::~VerticesQuery ()
56 {
57 }
58
59 void VerticesQuery::addVertex(const Vec3d & v)
60 {
61 vertices.push_back(v);
62 }
63
64 int VerticesQuery::toPlane (int i, int v0, int v1, int v2) const
65 {
66 return toPlane(vertices[i], v0, v1, v2);
67 }
68
69 int VerticesQuery::toPlane (const Vec3d& test, int v0, int v1, int v2) const
70 {
71 bool positive = sort(v0, v1, v2);
72
73 const Vec3d & vec0 = vertices[v0];
74 const Vec3d & vec1 = vertices[v1];
75 const Vec3d & vec2 = vertices[v2];
76
77 double x0 = test[0] - vec0[0];
78 double y0 = test[1] - vec0[1];
79 double z0 = test[2] - vec0[2];
80 double x1 = vec1[0] - vec0[0];
81 double y1 = vec1[1] - vec0[1];
82 double z1 = vec1[2] - vec0[2];
83 double x2 = vec2[0] - vec0[0];
84 double y2 = vec2[1] - vec0[1];
85 double z2 = vec2[2] - vec0[2];
86
87 double det = det3(x0,y0,z0,x1,y1,z1,x2,y2,z2);
88 if (!positive)
89 det = -det;
90
91 return (det > (double)0 ? +1 : (det < (double)0 ? -1 : 0));
92 }
93 //----------------------------------------------------------------------------
94
95 int VerticesQuery::toTetrahedron (int i, int v0, int v1, int v2,
96 int v3) const
97 {
98 return toTetrahedron(vertices[i], v0, v1, v2, v3);
99 }
100 //----------------------------------------------------------------------------
101
102 int VerticesQuery::toTetrahedron (const Vec3d& test, int v0, int v1, int v2, int v3) const
103 {
104 int sign0 = toPlane(test, v1, v2, v3);
105 if (sign0 > 0)
106 return +1;
107
108 int sign1 = toPlane(test, v0, v2, v3);
109 if (sign1 < 0)
110 return +1;
111
112 int sign2 = toPlane(test, v0, v1, v3);
113 if (sign2 > 0)
114 return +1;
115
116 int sign3 = toPlane(test, v0, v1, v2);
117 if (sign3 < 0)
118 return +1;
119
120 return ((sign0 && sign1 && sign2 && sign3) ? -1 : 0);
121 }
122
123 int VerticesQuery::toCircumsphere (int i, int v0, int v1, int v2, int v3) const
124 {
125 return toCircumsphere(vertices[i], v0, v1, v2, v3);
126 }
127
128 int VerticesQuery::toCircumsphere (const Vec3d& test, int v0, int v1, int v2, int v3) const
129 {
130 bool positive = sort(v0, v1, v2, v3);
131
132 const Vec3d& vec0 = vertices[v0];
133 const Vec3d& vec1 = vertices[v1];
134 const Vec3d& vec2 = vertices[v2];
135 const Vec3d& vec3 = vertices[v3];
136
137 double s0x = vec0[0] + test[0];
138 double d0x = vec0[0] - test[0];
139 double s0y = vec0[1] + test[1];
140 double d0y = vec0[1] - test[1];
141 double s0z = vec0[2] + test[2];
142 double d0z = vec0[2] - test[2];
143 double s1x = vec1[0] + test[0];
144 double d1x = vec1[0] - test[0];
145 double s1y = vec1[1] + test[1];
146 double d1y = vec1[1] - test[1];
147 double s1z = vec1[2] + test[2];
148 double d1z = vec1[2] - test[2];
149 double s2x = vec2[0] + test[0];
150 double d2x = vec2[0] - test[0];
151 double s2y = vec2[1] + test[1];
152 double d2y = vec2[1] - test[1];
153 double s2z = vec2[2] + test[2];
154 double d2z = vec2[2] - test[2];
155 double s3x = vec3[0] + test[0];
156 double d3x = vec3[0] - test[0];
157 double s3y = vec3[1] + test[1];
158 double d3y = vec3[1] - test[1];
159 double s3z = vec3[2] + test[2];
160 double d3z = vec3[2] - test[2];
161 double w0 = s0x*d0x + s0y*d0y + s0z*d0z;
162 double w1 = s1x*d1x + s1y*d1y + s1z*d1z;
163 double w2 = s2x*d2x + s2y*d2y + s2z*d2z;
164 double w3 = s3x*d3x + s3y*d3y + s3z*d3z;
165
166 double det = det4(d0x, d0y, d0z, w0, d1x, d1y, d1z, w1, d2x, d2y, d2z, w2,
167 d3x,d3y,d3z,w3);
168 if (!positive)
169 det = -det;
170 return (det > 0 ? 1 : (det < 0 ? -1 : 0));
171 }
172
173
174 int VerticesQuery::toCircumsphere (int i, int v0, int v1, int v2) const
175 {
176 return toCircumsphere(vertices[i], v0,v1,v2);
177 }
178
179 // https://en.wikipedia.org/wiki/Circumscribed_circle
180 // circumcenter of triangle(p0,p1,p2):
181 // O = (|p2-p1|^2 dot(p0-p1,p0-p2) p0 + |p0-p2|^2 dot(p1-p0,p1-p2) p1 + |p1-p0|^2 dot(p2-p0,p2-p1) p2 ) / (2 |cross(p1-p0, p2-p1)|^2)
182 // radius = |p0-p1| |p1-p2| |p2-p0| / (2 |cross(p1-p0, p2-p1) )
183 // p1-p0 = d0, p2-p1 = d1, p0-p2 = d2
184
185 // So, S = cross(d0, d1)
186 // O = - (|d1|^2 dot(d0,d2) p0 + |d2|^2 dot(d0,d1) p1 + |d0|^2 dot(d2, d1) p2 ) / ( 2 |S|^2)
187 // radius = sqrt( d0^2 d1^2 d2^2 / 4 S^2 )
188 int VerticesQuery::toCircumsphere (const Vec3d & test, int v0, int v1, int v2) const
189 {
190 const Vec3d & p0 = vertices[v0];
191 const Vec3d & p1 = vertices[v1];
192 const Vec3d & p2 = vertices[v2];
193 sort(v0,v1,v2);
194 Vec3d d0 = p1 - p0, d1 = p2 - p1, d2 = p0 - p2;
195 Vec3d S = cross(d0, d1);
196 double S2 = len2(S);
197 double d02 = len2(d0), d12 = len2(d1), d22 = len2(d2);
198 Vec3d center( (d12 * ::dot(d0,d2)) * p0 + (d22 * ::dot(d0,d1)) * p1 + (d02 * ::dot(d2,d1)) * p2 );
199 center /= -(2 * S2);
200 double r2 = d02 * d12 * d22 / (4*S2);
201 //assert(abs(len2(center - p0) - r2) < 1e-6);
202 //assert(abs(len2(center - p1) - r2) < 1e-6);
203 //assert(abs(len2(center - p2) - r2) < 1e-6);
204
205 double dist2 = len2(test - center);
206 return dist2 > r2 ? +1 : (dist2 < r2 ? -1 : 0);
207 }
208
209 double VerticesQuery::dot (double x0, double y0, double z0, double x1, double y1,
210 double z1)
211 {
212 return x0*x1 + y0*y1 + z0*z1;
213 }
214 //----------------------------------------------------------------------------
215
216 double VerticesQuery::det3 (double x0, double y0, double z0, double x1, double y1,
217 double z1, double x2, double y2, double z2)
218 {
219 double c00 = y1*z2 - y2*z1;
220 double c01 = y2*z0 - y0*z2;
221 double c02 = y0*z1 - y1*z0;
222 return x0*c00 + x1*c01 + x2*c02;
223 }
224 //----------------------------------------------------------------------------
225
226 double VerticesQuery::det4 (double x0, double y0, double z0, double w0, double x1,
227 double y1, double z1, double w1, double x2, double y2, double z2, double w2,
228 double x3, double y3, double z3, double w3)
229 {
230 double a0 = x0*y1 - x1*y0;
231 double a1 = x0*y2 - x2*y0;
232 double a2 = x0*y3 - x3*y0;
233 double a3 = x1*y2 - x2*y1;
234 double a4 = x1*y3 - x3*y1;
235 double a5 = x2*y3 - x3*y2;
236 double b0 = z0*w1 - z1*w0;
237 double b1 = z0*w2 - z2*w0;
238 double b2 = z0*w3 - z3*w0;
239 double b3 = z1*w2 - z2*w1;
240 double b4 = z1*w3 - z3*w1;
241 double b5 = z2*w3 - z3*w2;
242 return a0*b5 - a1*b4 + a2*b3 + a3*b2 - a4*b1 + a5*b0;
243 }
244
245
246 bool VerticesQuery::sort (int& v0, int& v1)
247 {
248 int j0, j1;
249 bool positive;
250
251 if (v0 < v1)
252 {
253 j0 = 0; j1 = 1; positive = true;
254 }
255 else
256 {
257 j0 = 1; j1 = 0; positive = false;
258 }
259
260 int value[2] = { v0, v1 };
261 v0 = value[j0];
262 v1 = value[j1];
263 return positive;
264 }
265 //----------------------------------------------------------------------------
266 bool VerticesQuery::sort (int& v0, int& v1, int& v2)
267 {
268 int j0, j1, j2;
269 bool positive;
270
271 if (v0 < v1)
272 {
273 if (v2 < v0)
274 {
275 j0 = 2; j1 = 0; j2 = 1; positive = true;
276 }
277 else if (v2 < v1)
278 {
279 j0 = 0; j1 = 2; j2 = 1; positive = false;
280 }
281 else
282 {
283 j0 = 0; j1 = 1; j2 = 2; positive = true;
284 }
285 }
286 else
287 {
288 if (v2 < v1)
289 {
290 j0 = 2; j1 = 1; j2 = 0; positive = false;
291 }
292 else if (v2 < v0)
293 {
294 j0 = 1; j1 = 2; j2 = 0; positive = true;
295 }
296 else
297 {
298 j0 = 1; j1 = 0; j2 = 2; positive = false;
299 }
300 }
301
302 int value[3] = { v0, v1, v2 };
303 v0 = value[j0];
304 v1 = value[j1];
305 v2 = value[j2];
306 return positive;
307 }
308 //----------------------------------------------------------------------------
309 bool VerticesQuery::sort (int& v0, int& v1, int& v2, int& v3)
310 {
311 int j0, j1, j2, j3;
312 bool positive;
313
314 if (v0 < v1)
315 {
316 if (v2 < v3)
317 {
318 if (v1 < v2)
319 {
320 j0 = 0; j1 = 1; j2 = 2; j3 = 3; positive = true;
321 }
322 else if (v3 < v0)
323 {
324 j0 = 2; j1 = 3; j2 = 0; j3 = 1; positive = true;
325 }
326 else if (v2 < v0)
327 {
328 if (v3 < v1)
329 {
330 j0 = 2; j1 = 0; j2 = 3; j3 = 1; positive = false;
331 }
332 else
333 {
334 j0 = 2; j1 = 0; j2 = 1; j3 = 3; positive = true;
335 }
336 }
337 else
338 {
339 if (v3 < v1)
340 {
341 j0 = 0; j1 = 2; j2 = 3; j3 = 1; positive = true;
342 }
343 else
344 {
345 j0 = 0; j1 = 2; j2 = 1; j3 = 3; positive = false;
346 }
347 }
348 }
349 else
350 {
351 if (v1 < v3)
352 {
353 j0 = 0; j1 = 1; j2 = 3; j3 = 2; positive = false;
354 }
355 else if (v2 < v0)
356 {
357 j0 = 3; j1 = 2; j2 = 0; j3 = 1; positive = false;
358 }
359 else if (v3 < v0)
360 {
361 if (v2 < v1)
362 {
363 j0 = 3; j1 = 0; j2 = 2; j3 = 1; positive = true;
364 }
365 else
366 {
367 j0 = 3; j1 = 0; j2 = 1; j3 = 2; positive = false;
368 }
369 }
370 else
371 {
372 if (v2 < v1)
373 {
374 j0 = 0; j1 = 3; j2 = 2; j3 = 1; positive = false;
375 }
376 else
377 {
378 j0 = 0; j1 = 3; j2 = 1; j3 = 2; positive = true;
379 }
380 }
381 }
382 }
383 else
384 {
385 if (v2 < v3)
386 {
387 if (v0 < v2)
388 {
389 j0 = 1; j1 = 0; j2 = 2; j3 = 3; positive = false;
390 }
391 else if (v3 < v1)
392 {
393 j0 = 2; j1 = 3; j2 = 1; j3 = 0; positive = false;
394 }
395 else if (v2 < v1)
396 {
397 if (v3 < v0)
398 {
399 j0 = 2; j1 = 1; j2 = 3; j3 = 0; positive = true;
400 }
401 else
402 {
403 j0 = 2; j1 = 1; j2 = 0; j3 = 3; positive = false;
404 }
405 }
406 else
407 {
408 if (v3 < v0)
409 {
410 j0 = 1; j1 = 2; j2 = 3; j3 = 0; positive = false;
411 }
412 else
413 {
414 j0 = 1; j1 = 2; j2 = 0; j3 = 3; positive = true;
415 }
416 }
417 }
418 else
419 {
420 if (v0 < v3)
421 {
422 j0 = 1; j1 = 0; j2 = 3; j3 = 2; positive = true;
423 }
424 else if (v2 < v1)
425 {
426 j0 = 3; j1 = 2; j2 = 1; j3 = 0; positive = true;
427 }
428 else if (v3 < v1)
429 {
430 if (v2 < v0)
431 {
432 j0 = 3; j1 = 1; j2 = 2; j3 = 0; positive = false;
433 }
434 else
435 {
436 j0 = 3; j1 = 1; j2 = 0; j3 = 2; positive = true;
437 }
438 }
439 else
440 {
441 if (v2 < v0)
442 {
443 j0 = 1; j1 = 3; j2 = 2; j3 = 0; positive = true;
444 }
445 else
446 {
447 j0 = 1; j1 = 3; j2 = 0; j3 = 2; positive = false;
448 }
449 }
450 }
451 }
452
453 int value[4] = { v0, v1, v2, v3 };
454 v0 = value[j0];
455 v1 = value[j1];
456 v2 = value[j2];
457 v3 = value[j3];
458 return positive;
459 }
460
461
462 void VerticesQuery::getInformation(double epsilon, Information & info)
463 {
464 assert(epsilon >= 0 && vertices.size() > 0);
465 info.extremeCCW = false;
466
467 // Compute the axis-aligned bounding box for the vertices. Keep track
468 // of the indices in the vertices for the current min and max.
469 int indexMin[3] = {0,0,0}, indexMax[3] = {0,0,0};
470 vertices[0].convertToArray(info.min);
471 vertices[0].convertToArray(info.max);
472
473 for(size_t i = 1; i < vertices.size(); ++i)
474 {
475 for(int j = 0; j < 3; ++j)
476 {
477 if (vertices[i][j] < info.min[j])
478 {
479 info.min[j] = vertices[i][j];
480 indexMin[j] = i;
481 }
482 else if (vertices[i][j] > info.max[j])
483 {
484 info.max[j] = vertices[i][j];
485 indexMax[j] = i;
486 }
487 }
488 }
489
490 // Determine the maximum range for the bounding box.
491 info.maxRange = info.max[0] - info.min[0];
492 info.extreme[0] = indexMin[0];
493 info.extreme[1] = indexMax[0];
494 double range = info.max[1] - info.min[1];
495 if (range > info.maxRange)
496 {
497 info.maxRange = range;
498 info.extreme[0] = indexMin[1];
499 info.extreme[1] = indexMax[1];
500 }
501 range = info.max[2] - info.min[2];
502 if (range > info.maxRange)
503 {
504 info.maxRange = range;
505 info.extreme[0] = indexMin[2];
506 info.extreme[1] = indexMax[2];
507 }
508
509 // The origin is either the point of minimum x-value, point of
510 // minimum y-value, or point of minimum z-value.
511 info.origin = vertices[info.extreme[0]];
512
513 // Test whether the point set is (nearly) a point.
514 if (info.maxRange < epsilon)
515 {
516 info.dimension = 0;
517 for(int j = 0; j < 3; ++j)
518 {
519 info.extreme[j + 1] = info.extreme[0];
520 info.direction[j] = Vec3d(0.);
521 }
522 return;
523 }
524
525 // Test whether the point set is (nearly) a line segment.
526 info.direction[0] = vertices[info.extreme[1]] - info.origin;
527 info.direction[0].normalize();
528 double maxDistance = 0.;
529 info.extreme[2] = info.extreme[0];
530 for(size_t i = 0; i < vertices.size(); ++i)
531 {
532 Vec3d diff = vertices[i] - info.origin;
533 double dotProduct = ::dot(info.direction[0], diff);
534 Vec3d proj = diff - dotProduct * info.direction[0];
535 double distance = len(proj);
536 if (distance > maxDistance)
537 {
538 maxDistance = distance;
539 info.extreme[2] = i;
540 }
541 }
542
543 if (maxDistance < epsilon*info.maxRange)
544 {
545 info.dimension = 1;
546 info.extreme[2] = info.extreme[1];
547 info.extreme[3] = info.extreme[1];
548 return;
549 }
550
551 // Test whether the point set is (nearly) a planar polygon.
552 info.direction[1] = vertices[info.extreme[2]] - info.origin;
553 info.direction[1] -= ::dot(info.direction[0], info.direction[1]) * info.direction[0];
554 info.direction[1].normalize();
555 info.direction[2] = cross(info.direction[0], info.direction[1]);
556 maxDistance = 0;
557 double maxSign = 0;
558 info.extreme[3] = info.extreme[0];
559 for(size_t i = 0; i < vertices.size(); ++i)
560 {
561 Vec3d diff = vertices[i] - info.origin;
562 double distance = ::dot(info.direction[2],diff);
563 int sign = distance >= 0 ? 1 : -1;
564 distance = fabs(distance);
565 if (distance > maxDistance)
566 {
567 maxDistance = distance;
568 maxSign = sign;
569 info.extreme[3] = i;
570 }
571 }
572
573 if (maxDistance < epsilon*info.maxRange)
574 {
575 info.dimension = 2;
576 info.extreme[3] = info.extreme[2];
577 return;
578 }
579
580 info.dimension = 3;
581 assert(epsilon == 0 || maxSign != 0);
582 info.extremeCCW = (maxSign >= 0);
583 }
584
585