GCC Code Coverage Report


Directory: ./
File: delaunay/parallel_delaunay_3d.cpp
Date: 2026-11-30 05:45:57
Exec Total Coverage
Lines: 608 663 91.7%
Functions: 29 31 93.5%
Branches: 497 674 73.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifdef GEOGRAM_WITH_PDEL
41
42 #include <geogram/delaunay/parallel_delaunay_3d.h>
43 #include <geogram/delaunay/cavity.h>
44 #include <geogram/mesh/mesh_reorder.h>
45 #include <geogram/numerics/predicates.h>
46 #include <geogram/basic/geometry.h>
47 #include <geogram/basic/stopwatch.h>
48 #include <geogram/basic/command_line.h>
49 #include <geogram/basic/permutation.h>
50 #include <geogram/bibliography/bibliography.h>
51
52 #include <mutex>
53 #include <condition_variable>
54
55 // ParallelDelaunayThread class, declared locally, has
56 // no out-of-line virtual functions. It is not a
57 // problem since they are only visible from this translation
58 // unit, but clang will complain.
59 #ifdef GEO_COMPILER_CLANG
60 #pragma GCC diagnostic ignored "-Wweak-vtables"
61 #endif
62
63 namespace {
64 using namespace GEO;
65
66 /**
67 * \brief Generates a random integer.
68 * \return a random integer between 0 and \p choices - 1
69 * \param [in] choices_in number of possible choices for the
70 * random variable (maximum value + 1)
71 * \details The function is thread-safe, and uses one seed
72 * per thread.
73 */
74 14 index_t thread_safe_random(index_t choices_in) {
75 #ifdef GARGANTUA
76 typedef Numeric::int64 Int;
77 #else
78 typedef long int Int;
79 #endif
80 14 signed_index_t choices = signed_index_t(choices_in);
81 static thread_local Int randomseed = 1l ;
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (choices >= 714025l) {
83 ✗ Int newrandom = (randomseed * 1366l + 150889l) % 714025l;
84 ✗ randomseed = (newrandom * 1366l + 150889l) % 714025l;
85 ✗ newrandom = newrandom * (choices / 714025l) + randomseed;
86 ✗ if (newrandom >= choices) {
87 ✗ return index_t(newrandom - choices);
88 } else {
89 ✗ return index_t(newrandom);
90 }
91 } else {
92 14 randomseed = (randomseed * 1366l + 150889l) % 714025l;
93 14 return index_t(randomseed % choices);
94 }
95 }
96
97 /**
98 * \brief Generates a random integer between 0 and 3.
99 * \return a random integer between 0 and 3
100 * \details The function is thread-safe, and uses one seed
101 * per thread.
102 */
103 index_t thread_safe_random_4() {
104 static thread_local long int randomseed = 1l ;
105 14983 randomseed = (randomseed * 1366l + 150889l) % 714025l;
106 14983 return index_t(randomseed % 4);
107 }
108 }
109
110 namespace GEO {
111
112 /**
113 * \brief One of the threads of the multi-threaded
114 * 3d Delaunay implementation.
115 */
116 class Delaunay3dThread : public GEO::Thread {
117 public:
118
119 /**
120 * \brief Symbolic value for cell_status_[t] that
121 * indicates that no thread owns t.
122 */
123 static constexpr index_t NO_THREAD = CellStatusArray::FREE_CELL;
124
125 /**
126 * \brief Creates a new Delaunay3dThread.
127 * \details Each Delaunay3dThread has an affected working
128 * zone, i.e. a range of tetrahedra indices in which the
129 * thread is allowed to create tetrahedra.
130 * \param[in] master a pointer to the ParallelDelaunay3d
131 * this thread belongs to
132 * \param[in] pool_begin first tetrahedron index of
133 * the working zone of this Delaunay3dThread
134 * \param[in] pool_end one position past the last tetrahedron
135 * index of the working zone of this Delaunay3dThread
136 */
137 24 Delaunay3dThread(
138 ParallelDelaunay3d* master,
139 index_t pool_begin,
140 index_t pool_end
141 24 ) :
142 24 master_(master),
143 24 cell_to_v_store_(master_->cell_to_v_store_),
144 24 cell_to_cell_store_(master_->cell_to_cell_store_),
145 24 cell_next_(master_->cell_next_),
146 24 cell_status_(master_->cell_status_)
147 {
148
149 // max_used_t_ is initialized to 1 so that
150 // computing modulos does not trigger FPEs
151 // at the beginning.
152 24 max_used_t_ = 1;
153
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
24 max_t_ = master_->cell_next_.size();
154
155
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 nb_vertices_ = master_->nb_vertices();
156 24 vertices_ = master_->vertex_ptr(0);
157 24 weighted_ = master_->weighted_;
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 heights_ = weighted_ ? master_->heights_.data() : nullptr;
159 24 dimension_ = master_->dimension();
160 24 vertex_stride_ = dimension_;
161 24 reorder_ = master_->reorder_.data();
162
163 // Initialize free list in memory pool
164 24 first_free_ = pool_begin;
165
2/2
✓ Branch 0 taken 98382 times.
✓ Branch 1 taken 24 times.
98406 for(index_t t=pool_begin; t<pool_end-1; ++t) {
166 98382 cell_next_[t] = t+1;
167 }
168 24 cell_next_[pool_end-1] = END_OF_LIST;
169 24 nb_free_ = pool_end - pool_begin;
170 24 memory_overflow_ = false;
171
172 24 work_begin_ = NO_INDEX;
173 24 work_end_ = NO_INDEX;
174 24 finished_ = false;
175 24 b_hint_ = NO_TETRAHEDRON;
176 24 e_hint_ = NO_TETRAHEDRON;
177 24 direction_ = true;
178
179 #ifdef GEO_DEBUG
180 nb_acquired_tets_ = 0;
181 #endif
182 24 interfering_thread_ = NO_THREAD;
183
184 24 nb_rollbacks_ = 0;
185 24 nb_failed_locate_ = 0;
186
187 24 nb_tets_to_create_ = 0;
188 24 t_boundary_ = NO_TETRAHEDRON;
189 24 f_boundary_ = NO_INDEX;
190
191 24 v1_ = NO_INDEX;
192 24 v2_ = NO_INDEX;
193 24 v3_ = NO_INDEX;
194 24 v4_ = NO_INDEX;
195 24 }
196
197 /**
198 * \brief Copies some variables from another thread.
199 * \param[in] rhs the thread from which variables should
200 * be copied
201 * \details copies v1_, v2_, v3_, v4_ (indices of the vertices
202 * of the first created tetrahedron), max_used_t_ (maximum
203 * used tetrahedron index) and max_t_ (maximum valid tetrahedron
204 * index).
205 */
206 void initialize_from(const Delaunay3dThread* rhs) {
207 6 max_used_t_ = rhs->max_used_t_;
208 6 max_t_ = rhs->max_t_;
209 6 v1_ = rhs->v1_;
210 6 v2_ = rhs->v2_;
211 6 v3_ = rhs->v3_;
212 6 v4_ = rhs->v4_;
213 3 }
214
215 /**
216 * \brief Gets the number of rollbacks.
217 * \return the number of rollbacks
218 * \details rollbacks occur whenever a point
219 * could not be inserted, due to interferences
220 * from other threads
221 */
222 index_t nb_rollbacks() const {
223
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 return nb_rollbacks_;
224 }
225
226 /**
227 * \brief Gets the number of failed locate() calls.
228 * \return the number of failed locate() calls
229 * \details locate() can fail when it cannot acquire
230 * the tetrahedra that are traversed, due to
231 * interferences from another thread.
232 */
233 index_t nb_failed_locate() const {
234
1/2
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
16 return nb_failed_locate_;
235 }
236
237 /**
238 * \brief Sets the point index sequence that
239 * should be processed by this thread.
240 * \param[in] b index of the first point to insert
241 * \param[in] e one position past the index of the
242 * last point to insert
243 */
244 void set_work(index_t b, index_t e) {
245 geo_debug_assert(work_size() == 0);
246 10 work_begin_ = b;
247 // e is one position past the last point index
248 // to insert.
249 10 work_end_ = e-1;
250 }
251
252 /**
253 * \brief Gets the number of remaining points to
254 * be inserted.
255 * \return the number of points to be inserted by
256 * this thread
257 */
258 index_t work_size() const {
259
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if(work_begin_ == NO_INDEX && work_end_ == NO_INDEX) {
260 return 0;
261 }
262 geo_debug_assert(work_begin_ != NO_INDEX);
263 geo_debug_assert(work_end_ != NO_INDEX);
264 4 return std::max(work_end_ - work_begin_ + 1, index_t(0));
265 }
266
267 /**
268 * \brief Gets the number of threads.
269 * \return the number of threads created by
270 * the master ParallelDelaunay3d of this thread.
271 */
272 index_t nb_threads() const {
273 return index_t(master_->threads_.size());
274 }
275
276 /**
277 * \brief Gets a thread by index
278 * \pre t < nb_threads()
279 * \param[in] t index of the thread
280 * \return a poiner to the \p t th thread
281 */
282 Delaunay3dThread* thread(index_t t) {
283 2 return static_cast<Delaunay3dThread*>(master_->threads_[t].get());
284 }
285
286
287 /**
288 * \brief Inserts the point sequence allocated to
289 * this thread.
290 * \details The point sequence was previously defined
291 * by set_work().
292 */
293 11 void run() override {
294
295 11 finished_ = false;
296
297
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 if(work_begin_ == NO_INDEX || work_end_ == NO_INDEX) {
298 return ;
299 }
300
301 11 memory_overflow_ = false;
302
303 // Current hint associated with b
304 11 b_hint_ = NO_TETRAHEDRON;
305
306 // Current hint associated with e
307 11 e_hint_ = NO_TETRAHEDRON;
308
309 // If true, insert in b->e order,
310 // else insert in e->b order
311 11 direction_ = true;
312
313
4/4
✓ Branch 0 taken 14065 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 14064 times.
✓ Branch 3 taken 1 times.
14075 while(work_end_ >= work_begin_ && !memory_overflow_) {
314
2/2
✓ Branch 0 taken 1261 times.
✓ Branch 1 taken 12803 times.
14064 index_t v = direction_ ? work_begin_ : work_end_ ;
315
2/2
✓ Branch 0 taken 12803 times.
✓ Branch 1 taken 1261 times.
14064 index_t& hint = direction_ ? b_hint_ : e_hint_;
316
317 // Try to insert v and update hint
318 14064 bool success = insert(reorder_[v],hint);
319
320 // Notify all threads that are waiting for
321 // this thread to release some tetrahedra.
322 send_event();
323
324
2/2
✓ Branch 0 taken 14058 times.
✓ Branch 1 taken 6 times.
14064 if(success) {
325
2/2
✓ Branch 0 taken 12798 times.
✓ Branch 1 taken 1260 times.
14058 if(direction_) {
326 12798 ++work_begin_;
327 } else {
328 1260 --work_end_;
329 }
330 } else {
331 6 ++nb_rollbacks_;
332
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if(interfering_thread_ != NO_THREAD) {
333
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if(id() < interfering_thread_) {
334 // If this thread has a higher priority than
335 // the one that interfered, wait for the
336 // interfering thread to release the tets that
337 // it holds (then the loop will retry to insert
338 // the same vertex).
339 2 wait_for_event(interfering_thread_);
340 } else {
341 // If this thread has a lower priority than
342 // the interfering thread, try inserting
343 // from the other end of the points sequence.
344 3 direction_ = !direction_;
345 }
346 }
347 }
348 }
349 11 finished_ = true;
350
351 // Fix by Hiep Vu: wake up threads that potentially missed
352 // the previous wake ups.
353 11 mutex_.lock();
354 send_event();
355 mutex_.unlock();
356 }
357
358 /**
359 * \brief Symbolic constant for uninitialized hint.
360 * \details Locate functions can be accelerated by
361 * specifying a hint. This constant indicates that
362 * no hint is given.
363 */
364 static constexpr index_t NO_TETRAHEDRON = NO_INDEX;
365
366 /**
367 * \brief Symbolic value for a vertex of a
368 * tetrahedron that indicates a virtual tetrahedron.
369 * \details The three other vertices then correspond to a
370 * facet on the convex hull of the points.
371 */
372 static constexpr index_t VERTEX_AT_INFINITY = NO_INDEX;
373
374
375 /**
376 * \brief Maximum valid index for a tetrahedron.
377 * \details This includes not only real tetrahedra,
378 * but also the virtual ones on the border, the conflict
379 * list and the free list.
380 * \return the maximum valid index for a tetrahedron
381 */
382 index_t max_t() const {
383 16 return max_t_;
384 }
385
386 /**
387 * \brief Tests whether a given tetrahedron
388 * is a finite one.
389 * \details Infinite tetrahedra are the ones
390 * that are incident to the infinite vertex
391 * (index -1)
392 * \param[in] t the index of the tetrahedron
393 * \retval true if \p t is finite
394 * \retval false otherwise
395 */
396 104645 bool tet_is_finite(index_t t) const {
397 return
398
2/2
✓ Branch 0 taken 104642 times.
✓ Branch 1 taken 3 times.
104645 cell_to_v_store_[4 * t] != NO_INDEX &&
399
2/2
✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 100172 times.
104642 cell_to_v_store_[4 * t + 1] != NO_INDEX &&
400
4/4
✓ Branch 0 taken 104642 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2332 times.
✓ Branch 3 taken 97840 times.
204817 cell_to_v_store_[4 * t + 2] != NO_INDEX &&
401
2/2
✓ Branch 0 taken 3756 times.
✓ Branch 1 taken 94084 times.
97840 cell_to_v_store_[4 * t + 3] != NO_INDEX ;
402 }
403
404 /**
405 * \brief Tests whether a tetrahedron is
406 * a real one.
407 * \details Real tetrahedra are incident to
408 * four user-specified vertices (there are also
409 * virtual tetrahedra that are incident to the
410 * vertex at infinity, with index -1)
411 * \param[in] t index of the tetrahedron
412 * \retval true if tetrahedron \p t is a real one
413 * \retval false otherwise
414 */
415 bool tet_is_real(index_t t) const {
416
3/4
✓ Branch 0 taken 65507 times.
✓ Branch 1 taken 30533 times.
✓ Branch 2 taken 14983 times.
✗ Branch 3 not taken.
111023 return !tet_is_free(t) && tet_is_finite(t);
417 }
418
419 /**
420 * \brief Tests whether a tetrahedron is
421 * in the free list.
422 * \details Deleted tetrahedra are recycled
423 * in a free list.
424 * \param[in] t index of the tetrahedron
425 * \retval true if tetrahedron \p t is in
426 * the free list
427 * \retval false otherwise
428 */
429 bool tet_is_free(index_t t) const {
430 return tet_is_in_list(t);
431 }
432
433 /**
434 * \brief Tests whether a tetrahedron is contained
435 * by a given linked list.
436 * \details Used for debugging purposes.
437 * \param[in] t the tetrahedron
438 * \param[in] first the first element of the list
439 * \retval true if \p t is contained in the list starting
440 * at \p first
441 * \retval false otherwise
442 */
443 bool tet_is_in_list(index_t t, index_t first) const {
444 for(
445 index_t cur = first; cur != END_OF_LIST;
446 cur = tet_next(cur)
447 ) {
448 if(cur == t) {
449 return true;
450 }
451 }
452 return false;
453 }
454
455
456 /**
457 * \brief Finds in the pointset a set of four non-coplanar
458 * points and creates a tetrahedron that connects them.
459 * \details This function is used to initiate the incremental
460 * Delaunay construction, it should be called only once.
461 * \retval the index of the created tetrahedron
462 * \retval NO_TETRAHEDRON if all points were coplanar
463 */
464 6 index_t create_first_tetrahedron() {
465 index_t iv0,iv1,iv2,iv3;
466 6 if(nb_vertices() < 4) {
467 return NO_TETRAHEDRON;
468 }
469
470 iv0 = 0;
471
472 iv1 = 1;
473 while(
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 iv1 < nb_vertices() &&
475 6 PCK::points_are_identical_3d(
476 vertex_ptr(iv0), vertex_ptr(iv1)
477 )
478 ) {
479 ✗ ++iv1;
480 }
481 6 if(iv1 == nb_vertices()) {
482 return NO_TETRAHEDRON;
483 }
484
485 6 iv2 = iv1 + 1;
486 6 while(
487
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 iv2 < nb_vertices() &&
488 6 PCK::points_are_colinear_3d(
489 vertex_ptr(iv0), vertex_ptr(iv1), vertex_ptr(iv2)
490 )
491 ) {
492 ✗ ++iv2;
493 }
494 6 if(iv2 == nb_vertices()) {
495 return NO_TETRAHEDRON;
496 }
497
498 6 iv3 = iv2 + 1;
499 Sign s = ZERO;
500 6 while(
501
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 31 times.
37 iv3 < nb_vertices() &&
502 37 (s = PCK::orient_3d(
503 vertex_ptr(iv0), vertex_ptr(iv1),
504 vertex_ptr(iv2), vertex_ptr(iv3)
505 )) == ZERO
506 ) {
507 31 ++iv3;
508 }
509
510 6 if(iv3 == nb_vertices()) {
511 return NO_TETRAHEDRON;
512 }
513
514 geo_debug_assert(s != ZERO);
515
516
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if(s == NEGATIVE) {
517 std::swap(iv2, iv3);
518 }
519
520 // Create the first tetrahedron
521 6 index_t t0 = new_tetrahedron(iv0, iv1, iv2, iv3);
522
523 // Create the first four virtual tetrahedra surrounding it
524 index_t t[4];
525
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for(index_t f = 0; f < 4; ++f) {
526 // In reverse order since it is an adjacent tetrahedron
527 index_t v1 = tet_vertex(t0, tet_facet_vertex(f,2));
528 index_t v2 = tet_vertex(t0, tet_facet_vertex(f,1));
529 index_t v3 = tet_vertex(t0, tet_facet_vertex(f,0));
530 24 t[f] = new_tetrahedron(VERTEX_AT_INFINITY, v1, v2, v3);
531 }
532
533 // Connect the virtual tetrahedra to the real one
534
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for(index_t f=0; f<4; ++f) {
535 24 set_tet_adjacent(t[f], 0, t0);
536 set_tet_adjacent(t0, f, t[f]);
537 }
538
539 // Interconnect the four virtual tetrahedra along their common
540 // faces
541
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for(index_t f = 0; f < 4; ++f) {
542 // In reverse order since it is an adjacent tetrahedron
543 index_t lv1 = tet_facet_vertex(f,2);
544 index_t lv2 = tet_facet_vertex(f,1);
545 index_t lv3 = tet_facet_vertex(f,0);
546 24 set_tet_adjacent(t[f], 1, t[lv1]);
547 24 set_tet_adjacent(t[f], 2, t[lv2]);
548 24 set_tet_adjacent(t[f], 3, t[lv3]);
549 }
550
551 6 v1_ = iv0;
552 6 v2_ = iv1;
553 6 v3_ = iv2;
554 6 v4_ = iv3;
555
556 6 release_tets();
557
558 return t0;
559 }
560
561
562 /**
563 * \brief Creates a star of tetrahedra filling the conflict
564 * zone.
565 * \param[in] v the index of the point to be inserted
566 * \details This function is used when the Cavity computed
567 * when traversing the conflict zone is OK, that is to say
568 * when its array sizes were not exceeded.
569 * \return the index of one the newly created tetrahedron
570 */
571 14003 index_t stellate_cavity(index_t v) {
572 index_t new_tet = NO_INDEX;
573
574
2/2
✓ Branch 0 taken 364842 times.
✓ Branch 1 taken 14003 times.
378845 for(index_t f=0; f<cavity_.nb_facets(); ++f) {
575 index_t old_tet = cavity_.facet_tet(f);
576 index_t lf = cavity_.facet_facet(f);
577 index_t t_neigh = tet_adjacent(old_tet, lf);
578 index_t v1 = cavity_.facet_vertex(f,0);
579 index_t v2 = cavity_.facet_vertex(f,1);
580 index_t v3 = cavity_.facet_vertex(f,2);
581 364842 new_tet = new_tetrahedron(v, v1, v2, v3);
582 set_tet_adjacent(new_tet, 0, t_neigh);
583 set_tet_adjacent(
584 t_neigh, find_tet_adjacent(t_neigh,old_tet), new_tet
585 );
586 cavity_.set_facet_tet(f, new_tet);
587 }
588
589
2/2
✓ Branch 0 taken 364842 times.
✓ Branch 1 taken 14003 times.
378845 for(index_t f=0; f<cavity_.nb_facets(); ++f) {
590 new_tet = cavity_.facet_tet(f);
591 index_t neigh1, neigh2, neigh3;
592 364842 cavity_.get_facet_neighbor_tets(f, neigh1, neigh2, neigh3);
593 364842 set_tet_adjacent(new_tet, 1, neigh1);
594 364842 set_tet_adjacent(new_tet, 2, neigh2);
595 364842 set_tet_adjacent(new_tet, 3, neigh3);
596 }
597
598 14003 return new_tet;
599 }
600
601 /**
602 * \brief Inserts a point in the triangulation.
603 * \param[in] v the index of the point to be inserted
604 * \param[in,out] hint the index of a tetrahedron as near as
605 * possible to \p v, or NO_TETRAHEDRON if unspecified. On
606 * exit, the index of one of the tetrahedra incident to
607 * point \p v
608 * \retval true if insertion was successful
609 * \retval false otherwise
610 */
611 14064 bool insert(index_t v, index_t& hint) {
612
613 // If v is one of the vertices of the
614 // first tetrahedron, nothing to do.
615 14064 if(
616
2/2
✓ Branch 0 taken 14058 times.
✓ Branch 1 taken 6 times.
14064 v == v1_ ||
617
2/2
✓ Branch 0 taken 14052 times.
✓ Branch 1 taken 6 times.
14058 v == v2_ ||
618
2/2
✓ Branch 0 taken 14046 times.
✓ Branch 1 taken 6 times.
14052 v == v3_ ||
619
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14040 times.
14046 v == v4_
620 ) {
621 return true;
622 }
623
624 Sign orient[4];
625 14040 index_t t = locate(vertex_ptr(v),hint,orient);
626
627 // locate() may fail due to tets already owned by
628 // other threads.
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14040 times.
14040 if(t == NO_TETRAHEDRON) {
630 ✗ ++nb_failed_locate_;
631 geo_debug_assert(nb_acquired_tets_ == 0);
632 ✗ return false;
633 }
634
635 // At this point, t is a valid tetrahedron,
636 // and this thread acquired a lock on it.
637
638 // Test whether the point already exists in
639 // the triangulation. The point already exists
640 // if it's located on three faces of the
641 // tetrahedron returned by locate().
642 14040 int nb_zero =
643 14040 (orient[0] == ZERO) +
644 14040 (orient[1] == ZERO) +
645 14040 (orient[2] == ZERO) +
646 14040 (orient[3] == ZERO) ;
647
648
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14040 times.
14040 if(nb_zero >= 3) {
649 release_tet(t);
650 ✗ return true;
651 }
652
653 geo_debug_assert(nb_acquired_tets_ == 1);
654 geo_debug_assert(weighted_ || tet_is_in_conflict(t,vertex_ptr(v)));
655
656 14040 index_t t_bndry = NO_TETRAHEDRON;
657 14040 index_t f_bndry = NO_INDEX;
658
659 cavity_.clear();
660
661 14040 bool ok = find_conflict_zone(v,t,t_bndry,f_bndry);
662
663 // When in multithreading mode, we cannot allocate memory
664 // dynamically and we use a fixed pool. If the fixed pool
665 // is full, then we exit the thread (and the missing points
666 // are inserted after, in sequential mode).
667 if(
668
4/4
✓ Branch 0 taken 2848 times.
✓ Branch 1 taken 11192 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2847 times.
16888 nb_tets_to_create_ > nb_free_ &&
669 2848 Process::is_running_threads()
670 ) {
671 1 memory_overflow_ = true;
672 ok = false;
673 }
674
675
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14034 times.
14039 if(!ok) {
676 // At this point, this thread did not successfully
677 // acquire all the tets in the conflict zone, so
678 // we need to rollback.
679 6 release_tets();
680 geo_debug_assert(nb_acquired_tets_ == 0);
681 6 return false;
682 }
683
684 // The conflict list can be empty if
685 // the triangulation is weighted and v is not visible
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14034 times.
14034 if(tets_to_delete_.size() == 0) {
687 ✗ release_tets();
688 geo_debug_assert(nb_acquired_tets_ == 0);
689 ✗ return true;
690 }
691
692
693 geo_debug_assert(
694 nb_acquired_tets_ ==
695 tets_to_delete_.size() + tets_to_release_.size()
696 );
697
698 #ifdef GEO_DEBUG
699 // Sanity check: make sure this threads owns all the tets
700 // in conflict and their neighbors.
701 for(index_t i=0; i<tets_to_delete_.size(); ++i) {
702 index_t tdel = tets_to_delete_[i];
703 geo_debug_assert(owns_tet(tdel));
704 for(index_t lf=0; lf<4; ++lf) {
705 geo_debug_assert(tet_adjacent(tdel,lf) != NO_INDEX);
706 geo_debug_assert(owns_tet(tet_adjacent(tdel,lf)));
707 }
708 }
709 #endif
710 geo_debug_assert(owns_tet(t_bndry));
711 geo_debug_assert(owns_tet(tet_adjacent(t_bndry,f_bndry)));
712 geo_debug_assert(
713 !tet_is_marked_as_conflict(tet_adjacent(t_bndry,f_bndry))
714 );
715
716 // At this point, this threads owns all the tets in conflict and
717 // their neighbors, therefore no other thread can interfere, and
718 // we can update the triangulation.
719
720 index_t new_tet = NO_INDEX;
721
2/2
✓ Branch 0 taken 14003 times.
✓ Branch 1 taken 31 times.
14034 if(cavity_.OK()) {
722 14003 new_tet = stellate_cavity(v);
723 } else {
724 31 new_tet = stellate_conflict_zone_iterative(v,t_bndry,f_bndry);
725 }
726
727
728 // Recycle the tetrahedra of the conflict zone.
729
2/2
✓ Branch 0 taken 269306 times.
✓ Branch 1 taken 14034 times.
297374 for(index_t i=0; i<tets_to_delete_.size()-1; ++i) {
730 269306 cell_next_[tets_to_delete_[i]] = tets_to_delete_[i+1];
731 }
732 14034 cell_next_[tets_to_delete_[tets_to_delete_.size()-1]] =
733 14034 first_free_;
734 14034 first_free_ = tets_to_delete_[0];
735 14034 nb_free_ += nb_tets_in_conflict();
736
737 // For debugging purposes.
738 #ifdef GEO_DEBUG
739 for(index_t i=0; i<tets_to_delete_.size(); ++i) {
740 index_t tdel = tets_to_delete_[i];
741 set_tet_vertex(tdel,0,VERTEX_OF_DELETED_TET);
742 set_tet_vertex(tdel,1,VERTEX_OF_DELETED_TET);
743 set_tet_vertex(tdel,2,VERTEX_OF_DELETED_TET);
744 set_tet_vertex(tdel,3,VERTEX_OF_DELETED_TET);
745 }
746 #endif
747
748 // Return one of the newly created tets
749 14034 hint=new_tet;
750
751 14034 release_tets();
752
753 geo_debug_assert(nb_acquired_tets_ == 0);
754 return true;
755 }
756
757 /**
758 * \brief Determines the list of tetrahedra in conflict
759 * with a given point.
760 * \param[in] v the index of the point to be inserted
761 * \param[in] t the index of a tetrahedron that contains
762 * \p p, as returned by locate()
763 * \param[out] t_bndry a tetrahedron adjacent to the
764 * boundary of the conflict zone
765 * \param[out] f_bndry the facet along which t_bndry is
766 * adjacent to the boundary of the conflict zone
767 * The other tetrahedra are linked, and can be traversed
768 * from \p first by using tet_next() until \p last or END_OF_LIST
769 * is reached.
770 * The conflict zone can be empty under two circumstances:
771 * - the vertex \p v already exists in the triangulation
772 * - the triangulation is weighted and \p v is not visible
773 * in either cases, both \p first and \p last contain END_OF_LIST
774 * \retval true if all the tetrahedra of the conflict zone and their
775 * neighbors could be acquired by this thread
776 * \retval false otherwise
777 */
778 14040 bool find_conflict_zone(
779 index_t v, index_t t,
780 index_t& t_bndry, index_t& f_bndry
781 ) {
782 14040 nb_tets_to_create_ = 0;
783
784 geo_debug_assert(t != NO_TETRAHEDRON);
785 geo_debug_assert(owns_tet(t));
786
787 // Pointer to the coordinates of the point to be inserted
788 const double* p = vertex_ptr(v);
789
790 // Weighted triangulations can have dangling
791 // vertices. Such vertices p are characterized by
792 // the fact that p is not in conflict with the
793 // tetrahedron returned by locate().
794 ✗ if(weighted_ && !tet_is_in_conflict(t,p)) {
795 release_tet(t);
796 ✗ return true;
797 }
798
799 14040 mark_tet_as_conflict(t);
800
801 // Sanity check: the vertex to be inserted should
802 // not correspond to one of the vertices of t.
803 geo_debug_assert(v != tet_vertex(t,0));
804 geo_debug_assert(v != tet_vertex(t,1));
805 geo_debug_assert(v != tet_vertex(t,2));
806 geo_debug_assert(v != tet_vertex(t,3));
807
808 // Note: points on edges and on facets are
809 // handled by the way tet_is_in_conflict()
810 // is implemented, that naturally inserts
811 // the correct tetrahedra in the conflict list.
812
813 // Determine the conflict list by greedy propagation from t.
814 14040 bool result = find_conflict_zone_iterative(p,t);
815 14040 t_bndry = t_boundary_;
816 14040 f_bndry = f_boundary_;
817 14040 return result;
818 }
819
820
821 /**
822 * \brief This function is used to implement find_conflict_zone.
823 * \details This function detects the neighbors of \p t that are
824 * in the conflict zone and calls itself recursively on them.
825 * \param[in] p the point to be inserted
826 * \param[in] t_in index of a tetrahedron in the conflict zone
827 * \pre The tetrahedron \p t was alredy marked as
828 * conflict (tet_is_in_list(t))
829 */
830 14040 bool find_conflict_zone_iterative(
831 const double* p, index_t t_in
832 ) {
833 geo_debug_assert(owns_tet(t_in));
834
2/2
✓ Branch 0 taken 14031 times.
✓ Branch 1 taken 9 times.
14040 S_.push_back(t_in);
835
836
2/2
✓ Branch 0 taken 283565 times.
✓ Branch 1 taken 14035 times.
297600 while(S_.size() != 0) {
837 283565 index_t t = *(S_.rbegin());
838 S_.pop_back();
839
840 geo_debug_assert(owns_tet(t));
841
842
2/2
✓ Branch 0 taken 1134253 times.
✓ Branch 1 taken 283560 times.
1417813 for(index_t lf = 0; lf < 4; ++lf) {
843 1134253 index_t t2 = tet_adjacent(t, lf);
844
845 // If t2 is already owned by current thread, then
846 // its status was previously determined.
847
2/2
✓ Branch 0 taken 543284 times.
✓ Branch 1 taken 590969 times.
1134253 if(owns_tet(t2)) {
848 geo_debug_assert(
849 tet_is_marked_as_conflict(t2) ==
850 tet_is_in_conflict(t2,p)
851 );
852
853 // If t2 is not in conflict list, then t has a facet
854 // on the border of the conflict zone, and there is
855 // a tet to create.
856 543284 if(!tet_is_marked_as_conflict(t2)) {
857 51798 ++nb_tets_to_create_;
858 51798 cavity_.new_facet(
859 t, lf,
860 tet_vertex(t, tet_facet_vertex(lf,0)),
861 tet_vertex(t, tet_facet_vertex(lf,1)),
862 tet_vertex(t, tet_facet_vertex(lf,2))
863 );
864 }
865 812857 continue;
866 }
867
868 590969 if(!acquire_tet(t2)) {
869 5 S_.resize(0);
870 5 return false;
871 }
872
873 geo_debug_assert(owns_tet(t2));
874
875
2/2
✓ Branch 1 taken 321391 times.
✓ Branch 2 taken 269573 times.
590964 if(!tet_is_in_conflict(t2,p)) {
876 321391 mark_tet_as_neighbor(t2);
877 // If t2 is not in conflict list, then t has a facet
878 // on the border of the conflict zone, and there is
879 // a tet to create.
880 321391 ++nb_tets_to_create_;
881 } else {
882 269573 mark_tet_as_conflict(t2);
883 geo_debug_assert(owns_tet(t2));
884 S_.push_back(t2);
885 269573 continue;
886 }
887
888 // At this point, t is in conflict
889 // and t2 is not in conflict.
890 // We keep a reference to a tet on the boundary
891 321391 t_boundary_ = t;
892 321391 f_boundary_ = lf;
893 321391 ++nb_tets_to_create_;
894 321391 cavity_.new_facet(
895 t, lf,
896 tet_vertex(t, tet_facet_vertex(lf,0)),
897 tet_vertex(t, tet_facet_vertex(lf,1)),
898 tet_vertex(t, tet_facet_vertex(lf,2))
899 );
900 geo_debug_assert(tet_adjacent(t,lf) == t2);
901 geo_debug_assert(owns_tet(t));
902 geo_debug_assert(owns_tet(t2));
903 }
904 }
905 return true;
906 }
907
908 /**
909 * \brief Gets the lifted coordinate of a point by its 3d coordinates.
910 * \param[in] p a pointer to the coordinates of one of the vertices
911 * of the triangulation.
912 * \return the lifted coordinate of \p p
913 */
914 double lifted_coordinate(const double* p) const {
915 // Compute the index of the point from its address
916 index_t pindex = index_t(
917 ✗ (p - vertex_ptr(0)) / int(vertex_stride_)
918 ✗ );
919 ✗ return heights_[pindex];
920 }
921
922
923 /**
924 * \brief Tests whether a given tetrahedron is in conflict with
925 * a given 3d point.
926 * \details A real tetrahedron is in conflict with a point whenever
927 * the point is contained by its circumscribed sphere, and a
928 * virtual tetrahedron is in conflict with a point whenever the
929 * tetrahedron formed by its real face and with the point has
930 * positive orientation.
931 * \param[in] t the index of the tetrahedron
932 * \param[in] p a pointer to the coordinates of the point
933 * \retval true if point \p p is in conflict with tetrahedron \p t
934 * \retval false otherwise
935 */
936 1080958 bool tet_is_in_conflict(index_t t, const double* p) const {
937
938 // Lookup tetrahedron vertices
939 const double* pv[4];
940
2/2
✓ Branch 0 taken 4323832 times.
✓ Branch 1 taken 1080958 times.
5404790 for(index_t i=0; i<4; ++i) {
941 index_t v = tet_vertex(t,i);
942
2/2
✓ Branch 0 taken 4107945 times.
✓ Branch 1 taken 215887 times.
8431777 pv[i] = (v == NO_INDEX) ? nullptr : vertex_ptr(v);
943 }
944
945 // Check for virtual tetrahedra (then in_sphere()
946 // is replaced with orient3d())
947
2/2
✓ Branch 0 taken 4094373 times.
✓ Branch 1 taken 865071 times.
4959444 for(index_t lf = 0; lf < 4; ++lf) {
948
949
2/2
✓ Branch 0 taken 215887 times.
✓ Branch 1 taken 3878486 times.
4094373 if(pv[lf] == nullptr) {
950
951 // Facet of a virtual tetrahedron opposite to
952 // infinite vertex corresponds to
953 // the triangle on the convex hull of the points.
954 // Orientation is obtained by replacing vertex lf
955 // with p.
956 215887 pv[lf] = p;
957 215887 Sign sign = PCK::orient_3d(pv[0],pv[1],pv[2],pv[3]);
958
959
2/2
✓ Branch 0 taken 198263 times.
✓ Branch 1 taken 17624 times.
215887 if(sign > 0) {
960 return true;
961 }
962
963
2/2
✓ Branch 0 taken 42009 times.
✓ Branch 1 taken 156254 times.
198263 if(sign < 0) {
964 return false;
965 }
966
967 // If sign is zero, we check the real tetrahedron
968 // adjacent to the facet on the convex hull.
969 geo_debug_assert(tet_adjacent(t, lf) != NO_INDEX);
970 index_t t2 = tet_adjacent(t, lf);
971 geo_debug_assert(!tet_is_virtual(t2));
972
973 // If t2 was already visited by this thread, then
974 // it is in conflict if it is already marked.
975
2/2
✓ Branch 0 taken 1888 times.
✓ Branch 1 taken 40121 times.
42009 if(owns_tet(t2)) {
976 1888 return tet_is_marked_as_conflict(t2);
977 }
978
979 // If t2 was not already visited, then we need to
980 // switch to the in_circum_circle_3d() predicate.
981
982 40121 const double* q0 = pv[(lf+1)%4];
983 40121 const double* q1 = pv[(lf+2)%4];
984 40121 const double* q2 = pv[(lf+3)%4];
985
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40121 times.
40121 if(weighted_) {
987 return (
988 ✗ PCK::in_circle_3dlifted_SOS(
989 q0, q1, q2, p,
990 lifted_coordinate(q0),
991 lifted_coordinate(q1),
992 lifted_coordinate(q2),
993 lifted_coordinate(p)
994 ) > 0
995 ✗ );
996 } else {
997 return (
998 40121 PCK::in_circle_3d_SOS(
999 q0,q1,q2,p
1000 ) > 0
1001 40121 );
1002 }
1003 }
1004 }
1005
1006 // If the tetrahedron is a finite one, it is in conflict
1007 // if its circumscribed sphere contains the point (this is
1008 // the standard case).
1009
1010
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 865071 times.
865071 if(weighted_) {
1011 ✗ double h0 = heights_[finite_tet_vertex(t, 0)];
1012 ✗ double h1 = heights_[finite_tet_vertex(t, 1)];
1013 ✗ double h2 = heights_[finite_tet_vertex(t, 2)];
1014 ✗ double h3 = heights_[finite_tet_vertex(t, 3)];
1015 double h = lifted_coordinate(p);
1016 ✗ return (PCK::orient_3dlifted_SOS(
1017 pv[0],pv[1],pv[2],pv[3],p,h0,h1,h2,h3,h
1018 ✗ ) > 0) ;
1019 }
1020
1021 865071 return (PCK::in_sphere_3d_SOS(pv[0], pv[1], pv[2], pv[3], p) > 0);
1022 }
1023
1024
1025 /**
1026 * \brief Finds the tetrahedron that contains a point.
1027 * \details The tetrahedron is acquired by this thread. If the
1028 * tetrahedron could not be acquired, then NO_TETRAHEDRON is returned.
1029 * If the point is on a face, edge or vertex,
1030 * the function returns one of the tetrahedra incident
1031 * to that face, edge or vertex.
1032 * \param[in] p a pointer to the coordinates of the point
1033 * \param[out] orient a pointer to an array of four Sign%s
1034 * or nullptr. If non-nullptr, returns the orientation with respect
1035 * to the four facets of the tetrahedron that contains \p p.
1036 * \retval the index of a tetrahedron that contains \p p.
1037 * If the point is outside the convex hull of
1038 * the inserted so-far points, then the returned tetrahedron
1039 * is a virtual one (first vertex is the "vertex at infinity"
1040 * of index -1)
1041 * \retval NO_TETRAHEDRON if the tetrahedron could not be
1042 * acquired by this thread, or if the virtual tetrahedra
1043 * were previously removed
1044 */
1045 14040 index_t locate(
1046 const double* p, index_t hint = NO_TETRAHEDRON,
1047 Sign* orient = nullptr
1048 ) {
1049 // Try improving the hint by using the
1050 // inexact locate function. This gains
1051 // (a little bit) performance (a few
1052 // percent in total Delaunay computation
1053 // time), but it is better than nothing...
1054 // Note: there is a maximum number of tets
1055 // traversed by locate_inexact() (2500)
1056 // since there exists configurations in which
1057 // locate_inexact() loops forever !
1058
1059 {
1060 14040 index_t new_hint = locate_inexact(p, hint, 2500);
1061
1062
1/2
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
14040 if(new_hint == NO_TETRAHEDRON) {
1063 return NO_TETRAHEDRON;
1064 }
1065
1066 hint = new_hint;
1067 }
1068
1069 // If no hint specified, find a tetrahedron randomly
1070
1071 if(hint != NO_TETRAHEDRON) {
1072
1/2
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
14040 if(tet_is_free(hint)) {
1073 hint = NO_TETRAHEDRON;
1074 } else {
1075
1/2
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
14040 if( !owns_tet(hint) && !acquire_tet(hint) ) {
1076 hint = NO_TETRAHEDRON;
1077 }
1078
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14040 times.
14040 if((hint != NO_TETRAHEDRON) && tet_is_free(hint)) {
1079 release_tet(hint);
1080 hint = NO_TETRAHEDRON;
1081 }
1082 }
1083 }
1084
1085 do {
1086
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14040 times.
14040 if(hint == NO_TETRAHEDRON) {
1087 ✗ hint = thread_safe_random(max_used_t_);
1088 }
1089 if(
1090
2/4
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14040 times.
14040 tet_is_free(hint) ||
1091 (!owns_tet(hint) && !acquire_tet(hint))
1092 ) {
1093 ✗ if(owns_tet(hint)) {
1094 release_tet(hint);
1095 }
1096 hint = NO_TETRAHEDRON;
1097 } else {
1098
2/2
✓ Branch 0 taken 50503 times.
✓ Branch 1 taken 8565 times.
59068 for(index_t f=0; f<4; ++f) {
1099
2/2
✓ Branch 0 taken 5475 times.
✓ Branch 1 taken 45028 times.
50503 if(tet_vertex(hint,f) == VERTEX_AT_INFINITY) {
1100 index_t new_hint = tet_adjacent(hint,f);
1101 if(
1102 5475 tet_is_free(new_hint) ||
1103 !acquire_tet(new_hint)
1104 ) {
1105 new_hint = NO_TETRAHEDRON;
1106 }
1107 release_tet(hint);
1108 hint = new_hint;
1109 5475 break;
1110 }
1111 }
1112 }
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14040 times.
14040 } while(hint == NO_TETRAHEDRON) ;
1114
1115 index_t t = hint;
1116 index_t t_pred = NO_TETRAHEDRON;
1117 Sign orient_local[4];
1118
1/2
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
14040 if(orient == nullptr) {
1119 orient = orient_local;
1120 }
1121
1122
1123 14040 still_walking:
1124 {
1125
2/2
✓ Branch 0 taken 943 times.
✓ Branch 1 taken 14040 times.
14983 if(t_pred != NO_TETRAHEDRON) {
1126 release_tet(t_pred);
1127 }
1128
1129
1/2
✓ Branch 0 taken 14983 times.
✗ Branch 1 not taken.
14983 if(tet_is_free(t)) {
1130 5475 return NO_TETRAHEDRON;
1131 }
1132
1133
2/2
✓ Branch 0 taken 943 times.
✓ Branch 1 taken 14040 times.
14983 if(!owns_tet(t) && !acquire_tet(t)) {
1134 return NO_TETRAHEDRON;
1135 }
1136
1137
1138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14983 times.
14983 if(!tet_is_real(t)) {
1139 release_tet(t);
1140 ✗ return NO_TETRAHEDRON;
1141 }
1142
1143 const double* pv[4];
1144 14983 pv[0] = vertex_ptr(finite_tet_vertex(t,0));
1145 14983 pv[1] = vertex_ptr(finite_tet_vertex(t,1));
1146 14983 pv[2] = vertex_ptr(finite_tet_vertex(t,2));
1147 14983 pv[3] = vertex_ptr(finite_tet_vertex(t,3));
1148
1149 // Start from a random facet
1150 index_t f0 = thread_safe_random_4();
1151
2/2
✓ Branch 0 taken 47142 times.
✓ Branch 1 taken 8565 times.
55707 for(index_t df = 0; df < 4; ++df) {
1152 47142 index_t f = (f0 + df) % 4;
1153
1154 index_t t_next = tet_adjacent(t,f);
1155
1156 // If the opposite tet is -1, then it means that
1157 // we are trying to locate() (e.g. called from
1158 // nearest_vertex) within a tetrahedralization
1159 // from which the infinite tets were removed.
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 47142 times.
47142 if(t_next == NO_INDEX) {
1161 release_tet(t);
1162 ✗ return NO_TETRAHEDRON;
1163 }
1164
1165 // If the candidate next tetrahedron is the
1166 // one we came from, then we know already that
1167 // the orientation is positive, thus we examine
1168 // the next candidate (or exit the loop if they
1169 // are exhausted).
1170
2/2
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 46735 times.
47142 if(t_next == t_pred) {
1171 407 orient[f] = POSITIVE ;
1172 407 continue ;
1173 }
1174
1175 // To test the orientation of p w.r.t. the facet f of
1176 // t, we replace vertex number f with p in t (same
1177 // convention as in CGAL).
1178 // This is equivalent to tet_facet_point_orient3d(t,f,p)
1179 // (but less costly, saves a couple of lookups)
1180 46735 const double* pv_bkp = pv[f];
1181 46735 pv[f] = p;
1182 46735 orient[f] = PCK::orient_3d(pv[0], pv[1], pv[2], pv[3]);
1183
1184 // If the orientation is not negative, then we cannot
1185 // walk towards t_next, and examine the next candidate
1186 // (or exit the loop if they are exhausted).
1187
2/2
✓ Branch 0 taken 40317 times.
✓ Branch 1 taken 6418 times.
46735 if(orient[f] != NEGATIVE) {
1188 40317 pv[f] = pv_bkp;
1189 40317 continue;
1190 }
1191
1192 // If the opposite tet is a virtual tet, then
1193 // the point has a positive orientation relative
1194 // to the facet on the border of the convex hull,
1195 // thus t_next is a tet in conflict and we are
1196 // done.
1197
2/2
✓ Branch 0 taken 5475 times.
✓ Branch 1 taken 943 times.
6418 if(tet_is_virtual(t_next)) {
1198 release_tet(t);
1199 if(!acquire_tet(t_next)) {
1200 return NO_TETRAHEDRON;
1201 }
1202
2/2
✓ Branch 0 taken 21900 times.
✓ Branch 1 taken 5475 times.
27375 for(index_t lf = 0; lf < 4; ++lf) {
1203 21900 orient[lf] = POSITIVE;
1204 }
1205 return t_next;
1206 }
1207
1208 // If we reach this point, then t_next is a valid
1209 // successor, thus we are still walking.
1210 t_pred = t;
1211 t = t_next;
1212 943 goto still_walking;
1213 }
1214 }
1215
1216 // If we reach this point, we did not find a valid successor
1217 // for walking (a face for which p has negative orientation),
1218 // thus we reached the tet for which p has all positive
1219 // face orientations (i.e. the tet that contains p).
1220
1221 #ifdef GEO_DEBUG
1222 geo_debug_assert(tet_is_real(t));
1223
1224 const double* pv[4];
1225 Sign signs[4];
1226 pv[0] = vertex_ptr(finite_tet_vertex(t,0));
1227 pv[1] = vertex_ptr(finite_tet_vertex(t,1));
1228 pv[2] = vertex_ptr(finite_tet_vertex(t,2));
1229 pv[3] = vertex_ptr(finite_tet_vertex(t,3));
1230 for(index_t f=0; f<4; ++f) {
1231 const double* pv_bkp = pv[f];
1232 pv[f] = p;
1233 signs[f] = PCK::orient_3d(pv[0], pv[1], pv[2], pv[3]);
1234 geo_debug_assert(signs[f] >= 0);
1235 pv[f] = pv_bkp;
1236 }
1237 #endif
1238
1239 8565 return t;
1240 }
1241
1242
1243 protected:
1244
1245 /**
1246 * \brief Tests whether a tetrahedron was marked as conflict.
1247 * \pre owns_tet(t)
1248 * \param[in] t the index of the tetrahedron to be tested
1249 * \retval true if \p t was marked as conflict
1250 * \retval false otherwise
1251 */
1252 bool tet_is_marked_as_conflict(index_t t) const {
1253 geo_debug_assert(owns_tet(t));
1254
4/4
✓ Branch 0 taken 15336 times.
✓ Branch 1 taken 12186 times.
✓ Branch 2 taken 51798 times.
✓ Branch 3 taken 491486 times.
572694 return cell_status_.cell_is_marked_as_conflict(t);
1255 }
1256
1257
1258 /**
1259 * \brief Gets the number of tetrahedra in conflict.
1260 * \return the number of tetrahedra in conflict,
1261 * specified by mark_tet_as_conflict()
1262 */
1263 index_t nb_tets_in_conflict() const {
1264 return tets_to_delete_.size();
1265 }
1266
1267 /**
1268 * \brief Marks a tetrahedron as conflict.
1269 * \details The index of the tetrahedron is also
1270 * stored it in the list of conflict tetrahedra.
1271 * \param[in] t index of the tetrahedron to mark
1272 * \pre owns_tet(t)
1273 */
1274 283613 void mark_tet_as_conflict(index_t t) {
1275 geo_debug_assert(owns_tet(t));
1276
2/2
✓ Branch 0 taken 283538 times.
✓ Branch 1 taken 75 times.
283613 tets_to_delete_.push_back(t);
1277 283613 cell_status_.mark_cell_as_conflict(t);
1278 geo_debug_assert(owns_tet(t));
1279 geo_debug_assert(tet_is_marked_as_conflict(t));
1280 283613 }
1281
1282 /**
1283 * \brief Marks a tetrahedron as neighbor of the conflict zone.
1284 * \details The index of the tetrahedron is also
1285 * stored it in the list of tetrahedra to release.
1286 * \param[in] t index of the tetrahedron to mark
1287 * \pre owns_tet(t)
1288 */
1289 void mark_tet_as_neighbor(index_t t) {
1290 // Note: nothing to change in cell_status_[t]
1291 // since LSB=0 means neigbhor tet.
1292
2/2
✓ Branch 0 taken 321370 times.
✓ Branch 1 taken 21 times.
321391 tets_to_release_.push_back(t);
1293 }
1294
1295 /**
1296 * \brief Acquires a lock on a tetrahedron and keep
1297 * it in the list of acquired tetrahedra.
1298 * \param[in] t index of the tetrahedron to acquire
1299 */
1300 372996 void acquire_and_mark_tet_as_created(index_t t) {
1301 // The tet was created in this thread's tet pool,
1302 // therefore there is no need to use sync
1303 // primitives to acquire a lock on it.
1304 geo_debug_assert(cell_status_.cell_thread(t) == NO_THREAD);
1305
2/2
✓ Branch 0 taken 372934 times.
✓ Branch 1 taken 62 times.
372996 cell_status_.set_cell_status(
1306 t, CellStatusArray::thread_index_t(id())
1307 );
1308 #ifdef GEO_DEBUG
1309 ++nb_acquired_tets_;
1310 #endif
1311
2/2
✓ Branch 0 taken 372934 times.
✓ Branch 1 taken 62 times.
372996 tets_to_release_.push_back(t);
1312 372996 }
1313
1314
1315 /**
1316 * \brief Releases all the tetrahedron locks that were
1317 * acquired using mark_tet_as_neighbor(),
1318 * acquire_and_mark_tet_as_created() and mark_as_conflict().
1319 */
1320 14046 void release_tets() {
1321
2/2
✓ Branch 0 taken 694387 times.
✓ Branch 1 taken 14046 times.
722479 for(index_t i=0; i<tets_to_release_.size(); ++i) {
1322 694387 release_tet(tets_to_release_[i]);
1323 }
1324 14046 tets_to_release_.resize(0);
1325
2/2
✓ Branch 0 taken 283613 times.
✓ Branch 1 taken 14046 times.
311705 for(index_t i=0; i<tets_to_delete_.size(); ++i) {
1326 283613 release_tet(tets_to_delete_[i]);
1327 }
1328 14046 tets_to_delete_.resize(0);
1329 14046 }
1330
1331 /**
1332 * \brief Atomically acquires a lock on a tetrahedron.
1333 * \details When the lock could not be acquired, interfering_thread_
1334 * contains the id of the thread that owns the lock.
1335 * \param[in] t the index of the tetrahedron to acquire
1336 * \retval true if the lock was successfully acquired
1337 * \retval false otherwise
1338 */
1339 bool acquire_tet(index_t t) {
1340 geo_debug_assert(t < max_t());
1341 geo_debug_assert(!owns_tet(t));
1342
1343 616902 interfering_thread_ = cell_status_.acquire_cell(
1344 t, CellStatusArray::thread_index_t(id())
1345 );
1346
1347
6/12
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5475 times.
✓ Branch 6 taken 943 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 5475 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5 times.
✓ Branch 11 taken 590964 times.
616902 if(interfering_thread_ == NO_THREAD) {
1348 geo_debug_assert(t == first_free_ || !tet_is_in_list(t));
1349 #ifdef GEO_DEBUG
1350 ++nb_acquired_tets_;
1351 #endif
1352 return true;
1353 }
1354 return false;
1355 }
1356
1357 /**
1358 * \brief Releases a lock on a tetrahedron, making it
1359 * available to the other threads.
1360 */
1361 void release_tet(index_t t) {
1362 geo_debug_assert(t < max_t());
1363 geo_debug_assert(owns_tet(t));
1364 #ifdef GEO_DEBUG
1365 --nb_acquired_tets_;
1366 #endif
1367 989893 cell_status_.release_cell(t);
1368 943 }
1369
1370
1371 /**
1372 * \brief Tests whether this thread owns a tetrahedron.
1373 * \param[in] t index of the tetrahedron
1374 * \retval true if this thread owns t
1375 * \retval false otherwise
1376 */
1377 bool owns_tet(index_t t) const {
1378 geo_debug_assert(t < max_t());
1379 return (
1380
8/12
✓ Branch 0 taken 14040 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14040 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 943 times.
✓ Branch 7 taken 14040 times.
✓ Branch 8 taken 1888 times.
✓ Branch 9 taken 40121 times.
✓ Branch 10 taken 543284 times.
✓ Branch 11 taken 590969 times.
1219325 cell_status_.cell_thread(t) ==
1381 CellStatusArray::thread_index_t(id())
1382 );
1383 }
1384
1385 /**
1386 * \brief Finds the tetrahedron that (approximately)
1387 * contains a point using inexact predicates.
1388 * \details The result of this function can be used as a hint
1389 * for locate(). It accelerates locate as compared to calling
1390 * it directly. This technique is referred to as "structural
1391 * filtering".
1392 * \param[in] p a pointer to the coordinates of the point
1393 * \param[in] max_iter maximum number of traversed tets
1394 * \return the index of a tetrahedron that (approximately)
1395 * contains \p p.
1396 * If the point is outside the convex hull of
1397 * the inserted so-far points, then the returned tetrahedron
1398 * is a virtual one (first vertex is the "vertex at infinity"
1399 * of index -1) or NO_TETRAHEDRON if the virtual tetrahedra
1400 * were previously removed.
1401 */
1402 14040 index_t locate_inexact(
1403 const double* p, index_t hint, index_t max_iter
1404 ) const {
1405 // If no hint was specified, or the specified hint refers to a
1406 // tetrahedron that another thread freed/recycled in the meanwhile,
1407 // find a tetrahedron randomly.
1408
3/4
✓ Branch 0 taken 14027 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 14027 times.
✗ Branch 3 not taken.
14040 if(hint != NO_TETRAHEDRON && tet_is_free(hint)) {
1409 hint = NO_TETRAHEDRON;
1410 }
1411
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 14040 times.
14054 while(hint == NO_TETRAHEDRON) {
1412 14 hint = thread_safe_random(max_used_t_);
1413
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
14 if(tet_is_free(hint) || tet_thread(hint) != NO_THREAD) {
1414 hint = NO_TETRAHEDRON;
1415 }
1416 }
1417
1418 // Always start from a real tet. If the tet is virtual,
1419 // find its real neighbor (always opposite to the
1420 // infinite vertex)
1421
2/2
✓ Branch 0 taken 3695 times.
✓ Branch 1 taken 10345 times.
14040 if(tet_is_virtual(hint)) {
1422
1/2
✓ Branch 0 taken 10708 times.
✗ Branch 1 not taken.
10708 for(index_t lf = 0; lf < 4; ++lf) {
1423
2/2
✓ Branch 0 taken 3695 times.
✓ Branch 1 taken 7013 times.
10708 if(tet_vertex(hint, lf) == VERTEX_AT_INFINITY) {
1424 hint = tet_adjacent(hint, lf);
1425
1426 // Yes, this can happen if the tetrahedron was
1427 // modified by another thread in the meanwhile.
1428
1/2
✓ Branch 0 taken 3695 times.
✗ Branch 1 not taken.
3695 if(hint == NO_TETRAHEDRON) {
1429 return NO_TETRAHEDRON;
1430 }
1431
1432 break;
1433 }
1434 }
1435 }
1436
1437 index_t t = hint;
1438 index_t t_pred = NO_TETRAHEDRON;
1439
1440 14040 still_walking:
1441 {
1442
1443 // Lookup the vertices of the current tetrahedron.
1444 const double* pv[4];
1445
2/2
✓ Branch 0 taken 451744 times.
✓ Branch 1 taken 112936 times.
564680 for(index_t lv=0; lv<4; ++lv) {
1446 index_t iv = tet_vertex(t,lv);
1447
1448
1449 // Since we did not acquire any lock,
1450 // it is possible that another threads made
1451 // this tetrahedron virtual (iv == NO_INDEX) or
1452 // deleted this tetrahedron (iv == VERTEX_OF_DELETED_TET)
1453 // (in both cases we exit immediately).
1454
1/2
✓ Branch 0 taken 451744 times.
✗ Branch 1 not taken.
451744 if(iv == NO_INDEX || iv == VERTEX_OF_DELETED_TET) {
1455 5475 return NO_TETRAHEDRON;
1456 }
1457 451744 pv[lv] = vertex_ptr(iv);
1458 }
1459
1460
2/2
✓ Branch 0 taken 276078 times.
✓ Branch 1 taken 8565 times.
284643 for(index_t f = 0; f < 4; ++f) {
1461
1462 index_t t_next = tet_adjacent(t,f);
1463
1464 // If the opposite tet is -1, then it means that
1465 // we are trying to locate() (e.g. called from
1466 // nearest_vertex) within a tetrahedralization
1467 // from which the infinite tets were removed.
1468
1/2
✓ Branch 0 taken 276078 times.
✗ Branch 1 not taken.
276078 if(t_next == NO_INDEX) {
1469 return NO_TETRAHEDRON;
1470 }
1471
1472 // If the candidate next tetrahedron is the
1473 // one we came from, then we know already that
1474 // the orientation is positive, thus we examine
1475 // the next candidate (or exit the loop if they
1476 // are exhausted).
1477
2/2
✓ Branch 0 taken 47633 times.
✓ Branch 1 taken 228445 times.
276078 if(t_next == t_pred) {
1478 47633 continue ;
1479 }
1480
1481 // To test the orientation of p w.r.t. the facet f of
1482 // t, we replace vertex number f with p in t (same
1483 // convention as in CGAL).
1484 228445 const double* pv_bkp = pv[f];
1485 228445 pv[f] = p;
1486 228445 Sign ori = PCK::orient_3d_inexact(
1487 pv[0], pv[1], pv[2], pv[3]
1488 );
1489
1490 // If the orientation is not negative, then we cannot
1491 // walk towards t_next, and examine the next candidate
1492 // (or exit the loop if they are exhausted).
1493
2/2
✓ Branch 0 taken 124074 times.
✓ Branch 1 taken 104371 times.
228445 if(ori != NEGATIVE) {
1494 124074 pv[f] = pv_bkp;
1495 124074 continue;
1496 }
1497
1498 // If the opposite tet is a virtual tet, then
1499 // the point has a positive orientation relative
1500 // to the facet on the border of the convex hull,
1501 // thus t_next is a tet in conflict and we are
1502 // done.
1503
2/2
✓ Branch 0 taken 98896 times.
✓ Branch 1 taken 5475 times.
104371 if(tet_is_virtual(t_next)) {
1504 return t_next;
1505 }
1506
1507 // If we reach this point, then t_next is a valid
1508 // successor, thus we are still walking.
1509 t_pred = t;
1510 t = t_next;
1511
1/2
✓ Branch 0 taken 98896 times.
✗ Branch 1 not taken.
98896 if(--max_iter != 0) {
1512 98896 goto still_walking;
1513 }
1514 }
1515 }
1516
1517 // If we reach this point, we did not find a valid successor
1518 // for walking (a face for which p has negative orientation),
1519 // thus we reached the tet for which p has all positive
1520 // face orientations (i.e. the tet that contains p).
1521
1522 8565 return t;
1523 }
1524
1525
1526 /**
1527 * \brief Tests whether a tetrahedron is
1528 * a virtual one.
1529 * \details Virtual tetrahedra are tetrahedra
1530 * incident to the vertex at infinity.
1531 * \param[in] t index of the tetrahedron
1532 * \retval true if tetrahedron \p t is virtual
1533 * \retval false otherwise
1534 */
1535 124829 bool tet_is_virtual(index_t t) const {
1536 return
1537
3/4
✓ Branch 0 taken 124829 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 124810 times.
✓ Branch 3 taken 19 times.
124829 !tet_is_free(t) && (
1538
2/2
✓ Branch 0 taken 124810 times.
✓ Branch 1 taken 19 times.
124829 cell_to_v_store_[4 * t] == VERTEX_AT_INFINITY ||
1539
2/2
✓ Branch 0 taken 6155 times.
✓ Branch 1 taken 118655 times.
124810 cell_to_v_store_[4 * t + 1] == VERTEX_AT_INFINITY ||
1540
2/2
✓ Branch 0 taken 2968 times.
✓ Branch 1 taken 115687 times.
118655 cell_to_v_store_[4 * t + 2] == VERTEX_AT_INFINITY ||
1541
2/2
✓ Branch 0 taken 5503 times.
✓ Branch 1 taken 110184 times.
115687 cell_to_v_store_[4 * t + 3] == VERTEX_AT_INFINITY) ;
1542 }
1543
1544
1545 /**
1546 * \brief Returns the local index of a vertex by
1547 * facet and by local vertex index in the facet.
1548 * \details
1549 * tet facet vertex is such that the tetrahedron
1550 * formed with:
1551 * - vertex lv
1552 * - tet_facet_vertex(lv,0)
1553 * - tet_facet_vertex(lv,1)
1554 * - tet_facet_vertex(lv,2)
1555 * has the same orientation as the original tetrahedron for
1556 * any vertex lv.
1557 * \param[in] f local facet index, in (0,1,2,3)
1558 * \param[in] v local vertex index, in (0,1,2)
1559 * \return the local tetrahedron vertex index of
1560 * vertex \p v in facet \p f
1561 */
1562 static index_t tet_facet_vertex(index_t f, index_t v) {
1563 geo_debug_assert(f < 4);
1564 geo_debug_assert(v < 3);
1565 430789 return index_t(tet_facet_vertex_[f][v]);
1566 }
1567
1568 /**
1569 * \brief Gets the index of a vertex of a tetrahedron
1570 * \param[in] t index of the tetrahedron
1571 * \param[in] lv local vertex (0,1,2 or 3) index in \p t
1572 * \return the global index of the \p lv%th vertex of tetrahedron \p t
1573 * or -1 if the vertex is at infinity
1574 */
1575 index_t tet_vertex(index_t t, index_t lv) const {
1576 geo_debug_assert(t < max_t());
1577 geo_debug_assert(lv < 4);
1578
11/12
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 8428 times.
✓ Branch 2 taken 8798 times.
✓ Branch 3 taken 10386 times.
✓ Branch 9 taken 3695 times.
✓ Branch 10 taken 7013 times.
✓ Branch 11 taken 451744 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 5475 times.
✓ Branch 14 taken 45028 times.
✓ Branch 15 taken 4107945 times.
✓ Branch 16 taken 215887 times.
5317370 return cell_to_v_store_[4 * t + lv];
1579 }
1580
1581 /**
1582 * \brief Finds the index of the vertex in a tetrahedron.
1583 * \param[in] t the tetrahedron
1584 * \param[in] v the vertex
1585 * \return iv such that tet_vertex(t,v)==iv
1586 * \pre \p t is incident to \p v
1587 */
1588 index_t find_tet_vertex(index_t t, index_t v) const {
1589 geo_debug_assert(t < max_t());
1590 // Find local index of v in tetrahedron t vertices.
1591 const index_t* T = &(cell_to_v_store_[4 * t]);
1592 return find_4(T,v);
1593 }
1594
1595
1596 /**
1597 * \brief Gets the index of a vertex of a tetrahedron
1598 * \param[in] t index of the tetrahedron
1599 * \param[in] lv local vertex (0,1,2 or 3) index in \p t
1600 * \return the global index of the \p lv%th vertex of tetrahedron \p t
1601 * \pre Vertex \p lv of tetrahedron \p t is not at infinity
1602 */
1603 index_t finite_tet_vertex(index_t t, index_t lv) const {
1604 geo_debug_assert(t < max_t());
1605 geo_debug_assert(lv < 4);
1606 geo_debug_assert(cell_to_v_store_[4 * t + lv] != NO_INDEX);
1607 14983 return cell_to_v_store_[4 * t + lv];
1608 }
1609
1610 /**
1611 * \brief Sets a tetrahedron-to-vertex adjacency.
1612 * \param[in] t index of the tetrahedron
1613 * \param[in] lv local vertex index (0,1,2 or 3) in \p t
1614 * \param[in] v global index of the vertex
1615 */
1616 void set_tet_vertex(index_t t, index_t lv, index_t v) {
1617 geo_debug_assert(t < max_t());
1618 geo_debug_assert(lv < 4);
1619 geo_debug_assert(owns_tet(t));
1620
2/2
✓ Branch 0 taken 6187 times.
✓ Branch 1 taken 1937 times.
8124 cell_to_v_store_[4 * t + lv] = v;
1621 }
1622
1623 /**
1624 * \brief Gets the index of a tetrahedron adjacent to another one.
1625 * \param[in] t index of the tetrahedron
1626 * \param[in] lf local facet (0,1,2 or 3) index in \p t
1627 * \return the tetrahedron adjacent to \p t accorss facet \p lf
1628 */
1629 index_t tet_adjacent(index_t t, index_t lf) const {
1630 geo_debug_assert(t < max_t());
1631 geo_debug_assert(lf < 4);
1632
13/18
✗ Branch 0 not taken.
✓ Branch 1 taken 9160 times.
✓ Branch 2 taken 9160 times.
✓ Branch 3 taken 27480 times.
✓ Branch 4 taken 12217 times.
✓ Branch 5 taken 12186 times.
✓ Branch 6 taken 3695 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 276078 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 5475 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 47142 times.
✓ Branch 14 taken 1888 times.
✓ Branch 15 taken 40121 times.
✓ Branch 16 taken 543284 times.
✓ Branch 17 taken 590969 times.
1943697 index_t result = cell_to_cell_store_[4 * t + lf];
1633 return result;
1634 }
1635
1636 /**
1637 * \brief Sets a tetrahedron-to-tetrahedron adjacency.
1638 * \param[in] t1 index of the first tetrahedron
1639 * \param[in] lf1 local facet index (0,1,2 or 3) in t1
1640 * \param[in] t2 index of the tetrahedron
1641 * adjacent to \p t1 accros \p lf1
1642 */
1643 void set_tet_adjacent(index_t t1, index_t lf1, index_t t2) {
1644 geo_debug_assert(t1 < max_t());
1645 geo_debug_assert(t2 < max_t());
1646 geo_debug_assert(lf1 < 4);
1647 geo_debug_assert(owns_tet(t1));
1648 geo_debug_assert(owns_tet(t2));
1649
2/2
✓ Branch 0 taken 262247 times.
✓ Branch 1 taken 102595 times.
729732 cell_to_cell_store_[4 * t1 + lf1] = t2;
1650 12186 }
1651
1652 /**
1653 * \brief Finds the index of the facet accros which t1 is
1654 * adjacent to t2.
1655 * \param[in] t1 first tetrahedron
1656 * \param[in] t2 second tetrahedron
1657 * \return f such that tet_adjacent(t1,f)==t2
1658 * \pre \p t1 and \p t2 are adjacent
1659 */
1660 index_t find_tet_adjacent(index_t t1, index_t t2) const {
1661 geo_debug_assert(t1 < max_t());
1662 geo_debug_assert(t2 < max_t());
1663 geo_debug_assert(t1 != t2);
1664
1665 // Find local index of t2 in tetrahedron t1 adajcent tets.
1666 const index_t* T = &(cell_to_cell_store_[4 * t1]);
1667 index_t result = find_4(T,t2);
1668
1669 // Sanity check: make sure that t1 is adjacent to t2
1670 // only once!
1671 geo_debug_assert(tet_adjacent(t1,(result+1)%4) != t2);
1672 geo_debug_assert(tet_adjacent(t1,(result+2)%4) != t2);
1673 geo_debug_assert(tet_adjacent(t1,(result+3)%4) != t2);
1674 return result;
1675 }
1676
1677
1678 /**
1679 * Gets the local facet index incident to an
1680 * oriented halfedge.
1681 * \param[in] t index of the tetrahedron
1682 * \param[in] v1 global index of the first extremity
1683 * \param[in] v2 global index of the second extremity
1684 * \return the local index of the facet incident to
1685 * the oriented edge \p v1, \p v2.
1686 */
1687 15336 index_t get_facet_by_halfedge(
1688 index_t t, index_t v1, index_t v2
1689 ) const {
1690 geo_debug_assert(t < max_t());
1691 geo_debug_assert(v1 != v2);
1692 // Find local index of v1 and v2 in tetrahedron t
1693
2/2
✓ Branch 0 taken 11481 times.
✓ Branch 1 taken 3855 times.
15336 const index_t* T = &(cell_to_v_store_[4 * t]);
1694 index_t lv1 = find_4(T,v1);
1695 index_t lv2 = find_4(T,v2);
1696 geo_debug_assert(lv1 != lv2);
1697 15336 return index_t(halfedge_facet_[lv1][lv2]);
1698 }
1699
1700
1701 /**
1702 * Gets the local facet indices incident to an
1703 * oriented halfedge.
1704 * \param[in] t index of the tetrahedron
1705 * \param[in] v1 global index of the first extremity
1706 * \param[in] v2 global index of the second extremity
1707 * \param[out] f12 the local index of the facet
1708 * indicent to the halfedge [v1,v2]
1709 * \param[out] f21 the local index of the facet
1710 * indicent to the halfedge [v2,v1]
1711 */
1712 12186 void get_facets_by_halfedge(
1713 index_t t, index_t v1, index_t v2,
1714 index_t& f12, index_t& f21
1715 ) const {
1716 geo_debug_assert(t < max_t());
1717 geo_debug_assert(v1 != v2);
1718
1719 // Find local index of v1 and v2 in tetrahedron t
1720 // The following expression is 10% faster than using
1721 // if() statements (multiply by boolean result of test).
1722 // Thank to Laurent Alonso for this idea.
1723
2/2
✓ Branch 0 taken 8945 times.
✓ Branch 1 taken 3241 times.
12186 const index_t* T = &(cell_to_v_store_[4 * t]);
1724
1725 12186 index_t lv1 =
1726
4/4
✓ Branch 0 taken 8945 times.
✓ Branch 1 taken 3241 times.
✓ Branch 2 taken 9082 times.
✓ Branch 3 taken 3104 times.
21131 index_t((T[1] == v1) | ((T[2] == v1) * 2) | ((T[3] == v1) * 3));
1727
1728 12186 index_t lv2 =
1729
4/4
✓ Branch 0 taken 9298 times.
✓ Branch 1 taken 2888 times.
✓ Branch 2 taken 9577 times.
✓ Branch 3 taken 2609 times.
21484 index_t((T[1] == v2) | ((T[2] == v2) * 2) | ((T[3] == v2) * 3));
1730
1731 geo_debug_assert(lv1 != 0 || T[0] == v1);
1732 geo_debug_assert(lv2 != 0 || T[0] == v2);
1733 geo_debug_assert(lv1 != NO_INDEX);
1734 geo_debug_assert(lv2 != NO_INDEX);
1735 geo_debug_assert(lv1 != lv2);
1736
1737 12186 f12 = index_t(halfedge_facet_[lv1][lv2]);
1738 12186 f21 = index_t(halfedge_facet_[lv2][lv1]);
1739 12186 }
1740
1741 /**
1742 * \brief Symbolic value of the cell_next_ field
1743 * that indicates the end of list in a linked
1744 * list of tetrahedra.
1745 */
1746 static constexpr index_t END_OF_LIST = NO_INDEX;
1747
1748
1749 /**
1750 * \brief Symbolic value of the cell_next_ field
1751 * for a tetrahedron that is not in a list.
1752 */
1753 static constexpr index_t NOT_IN_LIST = index_t(-2);
1754
1755 /**
1756 * \brief Symbolic value for t2v_[] indicating a deleted tetrahedron.
1757 */
1758 static constexpr index_t VERTEX_OF_DELETED_TET = index_t(-2);
1759
1760 /**
1761 * \brief Gets the number of vertices.
1762 * \return the number of vertices in this Delaunay
1763 */
1764 index_t nb_vertices() const {
1765
7/14
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 37 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 6 times.
67 return nb_vertices_;
1766 }
1767
1768 /**
1769 * \brief Gets a pointer to a vertex by its global index.
1770 * \param[in] i global index of the vertex
1771 * \return a pointer to vertex \p i
1772 */
1773 const double* vertex_ptr(index_t i) const {
1774 geo_debug_assert(i < nb_vertices());
1775
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 14040 times.
4602801 return vertices_ + vertex_stride_ * i;
1776 }
1777
1778 /**
1779 * \brief Tests whether a tetrahedron belongs to a linked
1780 * list.
1781 * \details Tetrahedra can be linked, it is used to manage
1782 * both the free list that recycles deleted tetrahedra,
1783 * the conflict region and the list of newly created
1784 * tetrahedra.
1785 * \param[in] t the index of the tetrahedron
1786 * \retval true if tetrahedron \p t belongs to a linked list
1787 * \retval false otherwise
1788 */
1789 bool tet_is_in_list(index_t t) const {
1790 geo_debug_assert(t < max_t());
1791
22/30
✓ Branch 0 taken 20186 times.
✓ Branch 1 taken 24149 times.
✓ Branch 2 taken 65507 times.
✓ Branch 3 taken 30533 times.
✓ Branch 4 taken 2290 times.
✓ Branch 5 taken 2506 times.
✓ Branch 6 taken 2506 times.
✓ Branch 7 taken 2290 times.
✓ Branch 8 taken 2290 times.
✓ Branch 9 taken 2506 times.
✓ Branch 10 taken 9320 times.
✓ Branch 11 taken 240 times.
✓ Branch 12 taken 124829 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 14027 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 13 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 14040 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✓ Branch 21 taken 14040 times.
✓ Branch 22 taken 14040 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 5475 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 14983 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 14983 times.
✗ Branch 29 not taken.
360568 return (cell_next_[t] != NOT_IN_LIST);
1792 }
1793
1794 /**
1795 * \brief Gets the index of a successor of a tetrahedron.
1796 * \details Tetrahedra can be linked, it is used to manage
1797 * both the free list that recycles deleted tetrahedra.
1798 * \param[in] t the index of the tetrahedron
1799 * \retval END_OF_LIST if the end of the list is reached
1800 * \retval the index of the successor of
1801 * tetrahedron \t otherwise
1802 * \pre tet_is_in_list(t)
1803 */
1804 index_t tet_next(index_t t) const {
1805 geo_debug_assert(t < max_t());
1806 geo_debug_assert(tet_is_in_list(t));
1807 372996 return cell_next_[t];
1808 }
1809
1810
1811 index_t tet_thread(index_t t) const {
1812 geo_debug_assert(t < max_t());
1813
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 return cell_status_.cell_thread(t);
1814 }
1815
1816 /**
1817 * \brief Adds a tetrahedron to a linked list.
1818 * \details Tetrahedra can be linked, it is used to manage
1819 * the free list that recycles deleted tetrahedra.
1820 * \param[in] t the index of the tetrahedron
1821 * \param[in,out] first first item of the list or END_OF_LIST if
1822 * the list is empty
1823 * \param[in,out] last last item of the list or END_OF_LIST if
1824 * the list is empty
1825 */
1826 void add_tet_to_list(index_t t, index_t& first, index_t& last) {
1827 geo_debug_assert(t < max_t());
1828 geo_debug_assert(!tet_is_in_list(t));
1829 geo_debug_assert(owns_tet(t));
1830 if(last == END_OF_LIST) {
1831 geo_debug_assert(first == END_OF_LIST);
1832 first = last = t;
1833 cell_next_[t] = END_OF_LIST;
1834 } else {
1835 cell_next_[t] = first;
1836 first = t;
1837 }
1838 }
1839
1840 /**
1841 * \brief Removes a tetrahedron from the linked list it
1842 * belongs to.
1843 * \details Tetrahedra can be linked, it is used to manage
1844 * the free list that recycles deleted tetrahedra.
1845 * \param[in] t the index of the tetrahedron
1846 */
1847 void remove_tet_from_list(index_t t) {
1848 geo_debug_assert(t < max_t());
1849 geo_debug_assert(tet_is_in_list(t));
1850 geo_debug_assert(owns_tet(t));
1851 372996 cell_next_[t] = NOT_IN_LIST;
1852 }
1853
1854
1855 /**
1856 * \brief Creates a new tetrahedron.
1857 * \details Uses either a tetrahedron recycled
1858 * from the free list, or creates a new one by
1859 * expanding the two indices arrays.
1860 * \return the index of the newly created tetrahedron
1861 */
1862 372996 index_t new_tetrahedron() {
1863
1864 // If the memory pool is full, then we expand it.
1865 // This cannot be done when running multiple threads.
1866
2/2
✓ Branch 0 taken 21783 times.
✓ Branch 1 taken 351213 times.
372996 if(first_free_ == END_OF_LIST) {
1867 geo_debug_assert(!Process::is_running_threads());
1868
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21783 times.
21783 master_->cell_to_v_store_.resize(
1869 master_->cell_to_v_store_.size() + 4, NO_INDEX
1870 );
1871
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 21783 times.
21783 master_->cell_to_cell_store_.resize(
1872 master_->cell_to_cell_store_.size() + 4, NO_INDEX
1873 );
1874
2/2
✓ Branch 0 taken 21777 times.
✓ Branch 1 taken 6 times.
21783 master_->cell_next_.push_back(END_OF_LIST);
1875 21783 master_->cell_status_.grow();
1876 21783 ++nb_free_;
1877 21783 ++max_t_;
1878 21783 first_free_ = master_->cell_status_.size() - 1;
1879 }
1880
1881 372996 acquire_and_mark_tet_as_created(first_free_);
1882 372996 index_t result = first_free_;
1883
1884 372996 first_free_ = tet_next(first_free_);
1885 remove_tet_from_list(result);
1886
1887 372996 cell_to_cell_store_[4 * result] = NO_INDEX;
1888 372996 cell_to_cell_store_[4 * result + 1] = NO_INDEX;
1889 372996 cell_to_cell_store_[4 * result + 2] = NO_INDEX;
1890 372996 cell_to_cell_store_[4 * result + 3] = NO_INDEX;
1891
1892 372996 max_used_t_ = std::max(max_used_t_, result);
1893
1894 372996 --nb_free_;
1895 372996 return result;
1896 }
1897
1898 /**
1899 * \brief Creates a new tetrahedron.
1900 * \details Sets the vertices. Adjacent tetrahedra index are
1901 * left uninitialized. Uses either a tetrahedron recycled
1902 * from the free list, or creates a new one by
1903 * expanding the two indices arrays.
1904 * \param[in] v1 index of the first vertex
1905 * \param[in] v2 index of the second vertex
1906 * \param[in] v3 index of the third vertex
1907 * \param[in] v4 index of the fourth vertex
1908 * \return the index of the newly created tetrahedron
1909 */
1910 372996 index_t new_tetrahedron(index_t v1, index_t v2, index_t v3, index_t v4) {
1911 372996 index_t result = new_tetrahedron();
1912 372996 cell_to_v_store_[4 * result] = v1;
1913 372996 cell_to_v_store_[4 * result + 1] = v2;
1914 372996 cell_to_v_store_[4 * result + 2] = v3;
1915 372996 cell_to_v_store_[4 * result + 3] = v4;
1916 372996 return result;
1917 }
1918
1919 /**
1920 * \brief Finds the index of an integer in an array of four integers.
1921 * \param[in] T a const pointer to an array of four integers
1922 * \param[in] v the integer to retrieve in \p T
1923 * \return the index (0,1,2 or 3) of \p v in \p T
1924 * \pre The four entries of \p T are different and one of them is
1925 * equal to \p v.
1926 */
1927 static index_t find_4(const index_t* T, index_t v) {
1928 // The following expression is 10% faster than using
1929 // if() statements. This uses the C++ norm, that
1930 // ensures that the 'true' boolean value converted to
1931 // an int is always 1. With most compilers, this avoids
1932 // generating branching instructions.
1933 // Thank to Laurent Alonso for this idea.
1934 // Note: Laurent also has this version:
1935 // (T[0] != v)+(T[2]==v)+2*(T[3]==v)
1936 // that avoids a *3 multiply, but it is not faster in
1937 // practice.
1938 415824 index_t result = index_t(
1939
18/18
✓ Branch 0 taken 8317 times.
✓ Branch 1 taken 3869 times.
✓ Branch 2 taken 6187 times.
✓ Branch 3 taken 1937 times.
✓ Branch 4 taken 5856 times.
✓ Branch 5 taken 2268 times.
✓ Branch 6 taken 11481 times.
✓ Branch 7 taken 3855 times.
✓ Branch 8 taken 11666 times.
✓ Branch 9 taken 3670 times.
✓ Branch 10 taken 11257 times.
✓ Branch 11 taken 4079 times.
✓ Branch 12 taken 12299 times.
✓ Branch 13 taken 3037 times.
✓ Branch 14 taken 262247 times.
✓ Branch 15 taken 102595 times.
✓ Branch 16 taken 267079 times.
✓ Branch 17 taken 97763 times.
976261 (T[1] == v) | ((T[2] == v) * 2) | ((T[3] == v) * 3)
1940 );
1941 // Sanity check, important if it was T[0], not explicitly
1942 // tested (detects input that does not meet the precondition).
1943 geo_debug_assert(T[result] == v);
1944 return result;
1945 }
1946
1947 /**
1948 * \brief Wakes up all the threads that are waiting for
1949 * this thread.
1950 */
1951 void send_event() {
1952 14075 cond_.notify_all();
1953 }
1954
1955 /**
1956 * \brief Waits for a thread.
1957 * \details Sleeps until thread \p t calls send_event().
1958 * \param[in] t index of the thread
1959 * \pre t < nb_threads()
1960 */
1961 2 void wait_for_event(index_t t) {
1962 // Fixed by Hiep Vu: enlarged critical section (contains
1963 // now the test (!thrd->finished)
1964 Delaunay3dThread* thrd = thread(t);
1965 // RAII: ctor locks, dtor unlocks
1966 2 std::unique_lock<std::mutex> L(thrd->mutex_);
1967
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if(!thrd->finished_) {
1968
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 thrd->cond_.wait(L);
1969 }
1970 2 }
1971
1972 /****** iterative stellate_conflict_zone *****************/
1973
1974 /**
1975 * \brief Used to represent the stack in the
1976 * (de-recursified) stellate_conflict_zone_iterative()
1977 * function.
1978 */
1979 class StellateConflictStack {
1980 public:
1981
1982 /**
1983 * \brief Pushes a new frame onto the stack.
1984 * \details This also creates the local variables (they are
1985 * left uninitialized).
1986 * \param[in] t1 index of a tetrahedron on the border of
1987 * the conflict zone
1988 * \param[in] t1fbord index of the facet of \p t1 that is
1989 * on the border of the conflict zone
1990 * \param[in] t1fprev index of the facet of \p t1 that we
1991 * come from, or NO_INDEX if \p t1 is the first tetrahedron
1992 */
1993 void push(index_t t1, index_t t1fbord, index_t t1fprev) {
1994 8124 store_.resize(store_.size()+1);
1995 8124 top().t1 = t1;
1996 8124 top().t1fbord = Numeric::uint8(t1fbord);
1997 8124 top().t1fprev = Numeric::uint8(t1fprev);
1998 31 }
1999
2000 /**
2001 * \brief Saves local variables into the current stack frame.
2002 * \param[in] new_t the index of the newly created tetrahedron
2003 * \param[in] t1ft2 the facet of t1 that is adjacent to t2
2004 * \param[in] t2ft1 the facet of t2 that is adjacent to t1
2005 */
2006 void save_locals(index_t new_t, index_t t1ft2, index_t t2ft1) {
2007 geo_debug_assert(!empty());
2008 8093 top().new_t = new_t;
2009 8093 top().t1ft2 = Numeric::uint8(t1ft2);
2010 8093 top().t2ft1 = Numeric::uint8(t2ft1);
2011 }
2012
2013 /**
2014 * \brief Gets the parameters from the current stack frame.
2015 * \param[out] t1 index of a tetrahedron on the border of
2016 * the conflict zone
2017 * \param[out] t1fbord index of the facet of \p t1 that is
2018 * on the border of the conflict zone
2019 * \param[out] t1fprev index of the facet of \p t1 that we
2020 * come from, or NO_INDEX if \p t1 is the first tetrahedron
2021 */
2022 void get_parameters(
2023 index_t& t1, index_t& t1fbord, index_t& t1fprev
2024 ) const {
2025 geo_debug_assert(!empty());
2026 16217 t1 = top().t1;
2027 16217 t1fbord = index_t(top().t1fbord);
2028 8124 t1fprev = index_t(top().t1fprev);
2029 }
2030
2031
2032 /**
2033 * \brief Gets the local variables from the current stack frame.
2034 * \param[out] new_t the index of the newly created tetrahedron
2035 * \param[out] t1ft2 the facet of t1 that is adjacent to t2
2036 * \param[out] t2ft1 the facet of t2 that is adjacent to t1
2037 */
2038 void get_locals(
2039 index_t& new_t, index_t& t1ft2, index_t& t2ft1
2040 ) const {
2041 geo_debug_assert(!empty());
2042 8093 new_t = top().new_t;
2043 8093 t1ft2 = index_t(top().t1ft2);
2044 8093 t2ft1 = index_t(top().t2ft1);
2045 }
2046
2047 /**
2048 * \brief Pops a stack frame.
2049 */
2050 void pop() {
2051 geo_debug_assert(!empty());
2052 store_.pop_back();
2053 }
2054
2055 /**
2056 * \brief Tests whether the stack is empty.
2057 * \retval true if the stack is empty
2058 * \retval false otherwise
2059 */
2060 bool empty() const {
2061 return store_.empty();
2062 }
2063
2064 private:
2065
2066 /**
2067 * \brief The parameters and local
2068 * variables stored in a stack frame.
2069 */
2070 struct Frame {
2071 // Parameters
2072 index_t t1;
2073 index_t new_t;
2074 Numeric::uint8 t1fbord ;
2075
2076 // Local variables
2077 Numeric::uint8 t1fprev ;
2078 Numeric::uint8 t1ft2 ;
2079 Numeric::uint8 t2ft1 ;
2080 };
2081
2082 /**
2083 * \brief Gets the top of the stack.
2084 * \return a modifiable reference to the Frame on
2085 * the top of the stack
2086 * \pre !empty()
2087 */
2088 Frame& top() {
2089 geo_debug_assert(!empty());
2090 return *store_.rbegin();
2091 }
2092
2093 /**
2094 * \brief Gets the top of the stack.
2095 * \return a const reference to the Frame on
2096 * the top of the stack
2097 * \pre !empty()
2098 */
2099 const Frame& top() const {
2100 geo_debug_assert(!empty());
2101 return *store_.rbegin();
2102 }
2103
2104 std::vector<Frame> store_;
2105 };
2106
2107 /**
2108 * \brief Creates a star of tetrahedra filling the conflict
2109 * zone.
2110 * \details For each tetrahedron facet on the border of the
2111 * conflict zone, a new tetrahedron is created, resting on
2112 * the facet and incident to vertex \p v. The function is
2113 * called recursively until the entire conflict zone is filled.
2114 * \param[in] v the index of the point to be inserted
2115 * \param[in] t1 index of a tetrahedron on the border
2116 * of the conflict zone.
2117 * \param[in] t1fbord index of the facet along which \p t_bndry
2118 * is incident to the border of the conflict zone
2119 * \param[in] t1fprev the facet of \p t_bndry connected to the
2120 * tetrahedron that \p t_bndry was reached from, or NO_INDEX
2121 * if it is the first tetrahedron.
2122 * \return the index of one the newly created tetrahedron
2123 */
2124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 31 times.
31 index_t stellate_conflict_zone_iterative(
2125 index_t v, index_t t1, index_t t1fbord,
2126 index_t t1fprev = NO_INDEX
2127 ) {
2128 // This function is de-recursified because some degenerate
2129 // inputs can cause stack overflow (system stack is limited to
2130 // a few megs). For instance, it can happen when a large number
2131 // of points are on the same sphere exactly.
2132
2133 // To de-recursify, it uses class StellateConflictStack
2134 // that emulates system's stack for storing functions's
2135 // parameters and local variables in all the nested stack
2136 // frames.
2137
2138 S2_.push(t1, t1fbord, t1fprev);
2139
2140 index_t new_t; // the newly created tetrahedron.
2141
2142 index_t t1ft2; // traverses the 4 facets of t1.
2143
2144 index_t t2; // the tetrahedron on the border of
2145 // the conflict zone that shares an
2146 // edge with t1 along t1ft2.
2147
2148 index_t t2fbord; // the facet of t2 on the border of
2149 // the conflict zone.
2150
2151 index_t t2ft1; // the facet of t2 that is incident to t1.
2152
2153 8124 entry_point:
2154 S2_.get_parameters(t1, t1fbord, t1fprev);
2155
2156
2157 geo_debug_assert(owns_tet(t1));
2158 geo_debug_assert(tet_adjacent(t1,t1fbord) != NO_INDEX);
2159 geo_debug_assert(owns_tet(tet_adjacent(t1,t1fbord)));
2160 geo_debug_assert(tet_is_marked_as_conflict(t1));
2161 geo_debug_assert(
2162 !tet_is_marked_as_conflict(tet_adjacent(t1,t1fbord))
2163 );
2164
2165 // Create new tetrahedron with same vertices as t_bndry
2166 8124 new_t = new_tetrahedron(
2167 tet_vertex(t1,0),
2168 tet_vertex(t1,1),
2169 tet_vertex(t1,2),
2170 tet_vertex(t1,3)
2171 );
2172
2173 // Replace in new_t the vertex opposite to t1fbord with v
2174 set_tet_vertex(new_t, t1fbord, v);
2175
2176 // Connect new_t with t1's neighbor accros t1fbord
2177 {
2178 index_t tbord = tet_adjacent(t1,t1fbord);
2179 set_tet_adjacent(new_t, t1fbord, tbord);
2180 set_tet_adjacent(tbord, find_tet_adjacent(tbord,t1), new_t);
2181 }
2182
2183 // Lookup new_t's neighbors accros its three other
2184 // facets and connect them
2185
2/2
✓ Branch 0 taken 32496 times.
✓ Branch 1 taken 8124 times.
40620 for(t1ft2=0; t1ft2<4; ++t1ft2) {
2186
2187
4/4
✓ Branch 0 taken 24403 times.
✓ Branch 1 taken 8093 times.
✓ Branch 2 taken 12217 times.
✓ Branch 3 taken 12186 times.
32496 if(t1ft2 == t1fprev || tet_adjacent(new_t,t1ft2) != NO_INDEX) {
2188 20310 continue;
2189 }
2190
2191 // Get t1's neighbor along the border of the conflict zone
2192
2/2
✓ Branch 1 taken 8093 times.
✓ Branch 2 taken 4093 times.
12186 if(!get_neighbor_along_conflict_zone_border(
2193 t1,t1fbord,t1ft2, t2,t2fbord,t2ft1
2194 )) {
2195 // If t1's neighbor is not a new tetrahedron,
2196 // create a new tetrahedron through a recursive call.
2197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8093 times.
8093 S2_.save_locals(new_t, t1ft2, t2ft1);
2198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8093 times.
8093 S2_.push(t2, t2fbord, t2ft1);
2199 8093 goto entry_point;
2200
2201 return_point:
2202 // This is the return value of the called function.
2203 index_t result = new_t;
2204 S2_.pop();
2205
2206 // Special case: we were in the outermost frame,
2207 // then we (truly) return from the function.
2208 8124 if(S2_.empty()) {
2209 31 return result;
2210 }
2211
2212 S2_.get_parameters(t1, t1fbord, t1fprev);
2213 S2_.get_locals(new_t, t1ft2, t2ft1);
2214 8093 t2 = result;
2215 }
2216
2217 12186 set_tet_adjacent(t2, t2ft1, new_t);
2218 set_tet_adjacent(new_t, t1ft2, t2);
2219 }
2220
2221 // Except for the initial call (see "Special case" above),
2222 // the nested calls all come from the same location,
2223 // thus there is only one possible return point
2224 // (no need to push any return address).
2225 8124 goto return_point;
2226 }
2227
2228 /**
2229 * \brief Finds the neighbor of a tetrahedron on the border of the
2230 * conflict zone.
2231 * \details This function is used by stellate_conflict_zone_iterative()
2232 * \param[in] t1 a tetrahedron on the border of the conflict zone
2233 * \param[in] t1fborder the local facet index of \p t1 along which it
2234 * is on the border of the conflict zone
2235 * \param[in] t1ft2 the local facet index of \p t1 that will be
2236 * traversed
2237 * \param[out] t2 a tetrahedron on the border of the conflict zone,
2238 * with an edge common to facets \p t1fborder and \p t1ft2 of
2239 * tetrahedron \p t1
2240 * \param[out] t2fborder the local facet index of \p t2 along which it
2241 * is on the border of the conflict zone
2242 * \param[out] t2ft1 the local index of the facet of \p t2 that has a
2243 * common edge with facets \p t1fborder and \p t1ft2 of tetrahedron
2244 * \p t1
2245 * \retval true if \p t2 is a newly created tetrahedron
2246 * \retval false if \p t2 is an old tetrahedron in conflict
2247 */
2248 12186 bool get_neighbor_along_conflict_zone_border(
2249 index_t t1,
2250 index_t t1fborder,
2251 index_t t1ft2,
2252 index_t& t2,
2253 index_t& t2fborder,
2254 index_t& t2ft1
2255 ) const {
2256
2257 // Note: this function is a bit long for an inline function,
2258 // but I observed a (modest) performance gain doing so.
2259
2260 // Find two vertices that are both on facets new_f and f1
2261 // (the edge around which we are turning)
2262 // This uses duality as follows:
2263 // Primal form (not used here):
2264 // halfedge_facet_[v1][v2] returns a facet that is incident
2265 // to both v1 and v2.
2266 // Dual form (used here):
2267 // halfedge_facet_[f1][f2] returns a vertex that both
2268 // f1 and f2 are incident to.
2269 index_t ev1 =
2270 12186 tet_vertex(t1, index_t(halfedge_facet_[t1ft2][t1fborder]));
2271 index_t ev2 =
2272 12186 tet_vertex(t1, index_t(halfedge_facet_[t1fborder][t1ft2]));
2273
2274 // Turn around edge [ev1,ev2] inside the conflict zone
2275 // until we reach again the boundary of the conflict zone.
2276 // Traversing inside the conflict zone is faster (as compared
2277 // to outside) since it traverses a smaller number of tets.
2278 index_t cur_t = t1;
2279 index_t cur_f = t1ft2;
2280 index_t next_t = tet_adjacent(cur_t,cur_f);
2281
2/2
✓ Branch 0 taken 15336 times.
✓ Branch 1 taken 12186 times.
27522 while(tet_is_marked_as_conflict(next_t)) {
2282 geo_debug_assert(next_t != t1);
2283 cur_t = next_t;
2284 15336 cur_f = get_facet_by_halfedge(cur_t,ev1,ev2);
2285 next_t = tet_adjacent(cur_t, cur_f);
2286 }
2287
2288 // At this point, cur_t is in conflict zone and
2289 // next_t is outside the conflict zone.
2290 index_t f12,f21;
2291 12186 get_facets_by_halfedge(next_t, ev1, ev2, f12, f21);
2292
2/2
✓ Branch 0 taken 8842 times.
✓ Branch 1 taken 3344 times.
12186 t2 = tet_adjacent(next_t,f21);
2293
2/2
✓ Branch 0 taken 8842 times.
✓ Branch 1 taken 3344 times.
12186 index_t v_neigh_opposite = tet_vertex(next_t,f12);
2294 12186 t2ft1 = find_tet_vertex(t2, v_neigh_opposite);
2295 12186 t2fborder = cur_f;
2296
2297 // Test whether the found neighboring tet was created
2298 // (then return true) or is an old tet in conflict
2299 // (then return false).
2300 12186 return(t2 != cur_t);
2301 }
2302
2303 /**
2304 * \brief Used by the (de-recursified)
2305 * stellate_conflict_zone_iterative() function.
2306 */
2307 StellateConflictStack S2_;
2308
2309 /*************************** debugging ************************/
2310
2311 /**
2312 * \brief For debugging purposes, displays a tetrahedron adjacency.
2313 * \param[in] t index of the tetrahedron to display.
2314 * \param[in] lf local index (0,1,2 or 3) of the tetrahedron
2315 * facet adjacenty to display.
2316 */
2317
2/2
✓ Branch 0 taken 9560 times.
✓ Branch 1 taken 9624 times.
19184 void show_tet_adjacent(index_t t, index_t lf) const {
2318 index_t adj = tet_adjacent(t, lf);
2319
2/2
✓ Branch 0 taken 9560 times.
✓ Branch 1 taken 9624 times.
19184 if(adj != NO_INDEX) {
2320
2/2
✓ Branch 0 taken 9320 times.
✓ Branch 1 taken 240 times.
18880 std::cerr << (tet_is_in_list(adj) ? '*' : ' ');
2321 }
2322 std::cerr << adj;
2323 19184 std::cerr << ' ';
2324 19184 }
2325
2326
2327 /**
2328 * \brief For debugging purposes, displays a tetrahedron.
2329 * \param[in] t index of the tetrahedron to display.
2330 */
2331 4796 void show_tet(index_t t) const {
2332 std::cerr << "tet"
2333
2/2
✓ Branch 0 taken 2290 times.
✓ Branch 1 taken 2506 times.
7086 << (tet_is_in_list(t) ? '*' : ' ')
2334 << t
2335 << ", v=["
2336 << tet_vertex(t, 0)
2337 4796 << ' '
2338 << tet_vertex(t, 1)
2339 4796 << ' '
2340 << tet_vertex(t, 2)
2341 4796 << ' '
2342 << tet_vertex(t, 3)
2343 4796 << "] adj=[";
2344 4796 show_tet_adjacent(t, 0);
2345 4796 show_tet_adjacent(t, 1);
2346 4796 show_tet_adjacent(t, 2);
2347 4796 show_tet_adjacent(t, 3);
2348 4796 std::cerr << "] ";
2349
2350
2/2
✓ Branch 0 taken 19184 times.
✓ Branch 1 taken 4796 times.
23980 for(index_t f = 0; f < 4; ++f) {
2351 38368 std::cerr << 'f' << f << ':';
2352
2/2
✓ Branch 0 taken 57552 times.
✓ Branch 1 taken 19184 times.
76736 for(index_t v = 0; v < 3; ++v) {
2353 std::cerr << tet_vertex(t, tet_facet_vertex(f,v))
2354 57552 << ',';
2355 }
2356 19184 std::cerr << ' ';
2357 }
2358 std::cerr << std::endl;
2359 4796 }
2360
2361 public:
2362
2363 /**
2364 * \brief For debugging purposes, tests some combinatorial properties.
2365 */
2366 4 void check_combinatorics(bool verbose) const {
2367
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(verbose) {
2368 std::cerr << std::endl;
2369 }
2370 bool ok = true;
2371 4 std::vector<bool> v_has_tet(nb_vertices(), false);
2372
2/2
✓ Branch 0 taken 4796 times.
✓ Branch 1 taken 4 times.
4800 for(index_t t = 0; t < max_t(); ++t) {
2373
2/2
✓ Branch 0 taken 2506 times.
✓ Branch 1 taken 2290 times.
4796 if(tet_is_free(t)) {
2374
1/2
✓ Branch 0 taken 2506 times.
✗ Branch 1 not taken.
2506 if(verbose) {
2375
1/2
✓ Branch 1 taken 2506 times.
✗ Branch 2 not taken.
2506 std::cerr << "-Deleted tet: ";
2376
1/2
✓ Branch 1 taken 2506 times.
✗ Branch 2 not taken.
2506 show_tet(t);
2377 }
2378 } else {
2379
1/2
✓ Branch 0 taken 2290 times.
✗ Branch 1 not taken.
2290 if(verbose) {
2380
1/2
✓ Branch 1 taken 2290 times.
✗ Branch 2 not taken.
2290 std::cerr << "Checking tet: ";
2381
1/2
✓ Branch 1 taken 2290 times.
✗ Branch 2 not taken.
2290 show_tet(t);
2382 }
2383
2/2
✓ Branch 0 taken 9160 times.
✓ Branch 1 taken 2290 times.
11450 for(index_t lf = 0; lf < 4; ++lf) {
2384
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9160 times.
9160 if(tet_adjacent(t, lf) == NO_INDEX) {
2385 std::cerr << lf << ":Missing adjacent tet"
2386 << std::endl;
2387 ok = false;
2388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9160 times.
9160 } else if(tet_adjacent(t, lf) == t) {
2389 std::cerr << lf << ":Tet is adjacent to itself"
2390 << std::endl;
2391 ok = false;
2392 } else {
2393 index_t t2 = tet_adjacent(t, lf);
2394 bool found = false;
2395
2/2
✓ Branch 0 taken 36640 times.
✓ Branch 1 taken 9160 times.
45800 for(index_t lf2 = 0; lf2 < 4; ++lf2) {
2396
2/2
✓ Branch 0 taken 9160 times.
✓ Branch 1 taken 27480 times.
36640 if(tet_adjacent(t2, lf2) == t) {
2397 found = true;
2398 }
2399 }
2400
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9160 times.
9160 if(!found) {
2401 std::cerr
2402 << lf
2403 << ":Adjacent link is not bidirectional"
2404 << std::endl;
2405 ok = false;
2406 }
2407 }
2408 }
2409 index_t nb_infinite = 0;
2410
2/2
✓ Branch 0 taken 9160 times.
✓ Branch 1 taken 2290 times.
11450 for(index_t lv = 0; lv < 4; ++lv) {
2411
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 8428 times.
9160 if(tet_vertex(t, lv) == NO_INDEX) {
2412 732 ++nb_infinite;
2413 }
2414 }
2415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2290 times.
2290 if(nb_infinite > 1) {
2416 ok = false;
2417 std::cerr << "More than one infinite vertex"
2418 << std::endl;
2419 }
2420 }
2421
2/2
✓ Branch 0 taken 19184 times.
✓ Branch 1 taken 4796 times.
23980 for(index_t lv = 0; lv < 4; ++lv) {
2422 index_t v = tet_vertex(t, lv);
2423
2/2
✓ Branch 0 taken 8798 times.
✓ Branch 1 taken 10386 times.
19184 if(v != NO_INDEX && v != VERTEX_OF_DELETED_TET) {
2424 v_has_tet[v] = true;
2425 }
2426 }
2427 }
2428
2/2
✓ Branch 0 taken 458 times.
✓ Branch 1 taken 4 times.
462 for(index_t v = 0; v < nb_vertices(); ++v) {
2429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 458 times.
458 if(!v_has_tet[v]) {
2430 ✗ if(verbose) {
2431 std::cerr << "Vertex " << v
2432 << " is isolated (duplicated ?)" << std::endl;
2433 }
2434 }
2435 }
2436
1/8
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
4 geo_assert(ok);
2437
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(verbose) {
2438 std::cerr << std::endl;
2439 }
2440 std::cerr << std::endl << "Delaunay Combi OK" << std::endl;
2441 4 }
2442
2443
2444 /**
2445 * \brief For debugging purposes, test some geometrical properties.
2446 */
2447 4 void check_geometry(bool verbose) const {
2448 bool ok = true;
2449
2/2
✓ Branch 0 taken 4796 times.
✓ Branch 1 taken 4 times.
4800 for(index_t t = 0; t < max_t(); ++t) {
2450
2/2
✓ Branch 0 taken 2290 times.
✓ Branch 1 taken 2506 times.
4796 if(!tet_is_free(t)) {
2451 index_t v0 = tet_vertex(t, 0);
2452 index_t v1 = tet_vertex(t, 1);
2453 index_t v2 = tet_vertex(t, 2);
2454 index_t v3 = tet_vertex(t, 3);
2455
2/2
✓ Branch 0 taken 498422 times.
✓ Branch 1 taken 2290 times.
500712 for(index_t v = 0; v < nb_vertices(); ++v) {
2456
4/4
✓ Branch 0 taken 494160 times.
✓ Branch 1 taken 4262 times.
✓ Branch 2 taken 4166 times.
✓ Branch 3 taken 489994 times.
498422 if(v == v0 || v == v1 || v == v2 || v == v3) {
2457 8428 continue;
2458 }
2459
1/2
✓ Branch 1 taken 489994 times.
✗ Branch 2 not taken.
489994 if(tet_is_in_conflict(t, vertex_ptr(v))) {
2460 ok = false;
2461 ✗ if(verbose) {
2462 std::cerr << "Tet " << t <<
2463 " is in conflict with vertex " << v
2464 << std::endl;
2465
2466 ✗ std::cerr << " offending tet: ";
2467 ✗ show_tet(t);
2468 }
2469 }
2470 }
2471 }
2472 }
2473
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
4 geo_assert(ok);
2474 std::cerr << std::endl << "Delaunay Geo OK" << std::endl;
2475 4 }
2476
2477 private:
2478 ParallelDelaunay3d* master_;
2479 index_t nb_vertices_;
2480 const double* vertices_;
2481 const double* heights_;
2482 index_t* reorder_;
2483 index_t dimension_;
2484 index_t vertex_stride_;
2485 bool weighted_;
2486 index_t max_t_;
2487 index_t max_used_t_;
2488
2489 vector<index_t>& cell_to_v_store_;
2490 vector<index_t>& cell_to_cell_store_;
2491 vector<index_t>& cell_next_;
2492 CellStatusArray& cell_status_;
2493
2494 index_t first_free_;
2495 index_t nb_free_;
2496 bool memory_overflow_;
2497
2498 index_t v1_,v2_,v3_,v4_; // The first four vertices
2499
2500 vector<index_t> S_;
2501 index_t nb_tets_to_create_;
2502 index_t t_boundary_; // index of a tet,facet on the bndry
2503 index_t f_boundary_; // of the conflict zone.
2504
2505 bool direction_;
2506 index_t work_begin_;
2507 index_t work_end_;
2508 index_t b_hint_;
2509 index_t e_hint_;
2510 bool finished_;
2511
2512 // Whenever acquire_tet() is unsuccessful, contains
2513 // the index of the thread that was interfering
2514 // (shifted to the left by 1 !!)
2515 CellStatusArray::thread_index_t interfering_thread_;
2516
2517 #ifdef GEO_DEBUG
2518 index_t nb_acquired_tets_;
2519 #endif
2520
2521 vector<index_t> tets_to_delete_;
2522 vector<index_t> tets_to_release_;
2523
2524 index_t nb_rollbacks_;
2525 index_t nb_failed_locate_;
2526
2527 std::condition_variable cond_;
2528 std::mutex mutex_;
2529
2530 /**
2531 * \brief Gives the indexing of tetrahedron facet
2532 * vertices.
2533 * \details tet_facet_vertex[lf][lv] gives the
2534 * local vertex index (in 0,1,2,3) from a
2535 * local facet index lf (in 0,1,2,3) and a
2536 * local vertex index within the facet (in 0,1,2).
2537 */
2538 static char tet_facet_vertex_[4][3];
2539
2540 /**
2541 * \brief Gives a local facet index by
2542 * halfedge extremities local indices.
2543 */
2544 static char halfedge_facet_[4][4];
2545
2546 /**
2547 * \brief Stores the triangles on the boundary
2548 * of the cavity, for faster generation of the
2549 * new tetrahedra.
2550 */
2551 Cavity cavity_;
2552 };
2553
2554
2555 char Delaunay3dThread::halfedge_facet_[4][4] = {
2556 {4, 2, 3, 1},
2557 {3, 4, 0, 2},
2558 {1, 3, 4, 0},
2559 {2, 0, 1, 4}
2560 };
2561
2562 // tet facet vertex is such that the tetrahedron
2563 // formed with:
2564 // vertex lv
2565 // tet_facet_vertex[lv][0]
2566 // tet_facet_vertex[lv][1]
2567 // tet_facet_vertex[lv][2]
2568 // has the same orientation as the original tetrahedron for
2569 // any vertex lv.
2570
2571 char Delaunay3dThread::tet_facet_vertex_[4][3] = {
2572 {1, 2, 3},
2573 {0, 3, 2},
2574 {3, 0, 1},
2575 {1, 0, 2}
2576 };
2577
2578
2579 /*************************************************************************/
2580
2581 6 ParallelDelaunay3d::ParallelDelaunay3d(
2582 coord_index_t dimension
2583
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 ) : Delaunay(dimension) {
2584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if(dimension != 3 && dimension != 4) {
2585 ✗ throw InvalidDimension(dimension, "Delaunay3d", "3 or 4");
2586 }
2587
2588
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2589 "DBLP:journals/cj/Bowyer81",
2590 "One of the two initial references to the algorithm, "
2591 "discovered independently and simultaneously by Bowyer and Watson."
2592 );
2593
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2594 "journals/cj/Watson81",
2595 "One of the two initial references to the algorithm, "
2596 "discovered independently and simultaneously by Bowyer and Watson."
2597 );
2598
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2599 "DBLP:conf/compgeom/AmentaCR03",
2600 "Using spatial sorting has a dramatic impact on the performances."
2601 );
2602
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2603 "DBLP:journals/comgeo/FunkeMN05",
2604 "Initializing \\verb|locate()| with a non-exact version "
2605 " (structural filtering) gains (a bit of) performance."
2606 );
2607
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2608 "DBLP:journals/comgeo/BoissonnatDPTY02",
2609 "The idea of traversing the cavity from inside "
2610 " used in GEOGRAM is inspired by the implementation of "
2611 " \\verb|Delaunay_triangulation_3| in CGAL."
2612 );
2613
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2614 "DBLP:conf/imr/Si06",
2615 "The triangulation data structure used in GEOGRAM is inspired "
2616 "by Tetgen."
2617 );
2618
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 geo_cite_with_info(
2619 "DBLP:journals/ijfcs/DevillersPT02",
2620 "Analysis of the different versions of the line walk algorithm "
2621 " used by \\verb|locate()|."
2622 );
2623
2624 6 weighted_ = (dimension == 4);
2625 // In weighted mode, vertices are 4d but combinatorics is 3d.
2626
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if(weighted_) {
2627 ✗ cell_size_ = 4;
2628 ✗ cell_v_stride_ = 4;
2629 ✗ cell_neigh_stride_ = 4;
2630 }
2631
3/6
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
6 debug_mode_ = CmdLine::get_arg_bool("dbg:delaunay");
2632
3/6
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
6 verbose_debug_mode_ = CmdLine::get_arg_bool("dbg:delaunay_verbose");
2633
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 debug_mode_ = (debug_mode_ || verbose_debug_mode_);
2634
3/8
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
6 benchmark_mode_ = CmdLine::get_arg_bool("dbg:delaunay_benchmark");
2635 6 }
2636
2637 6 void ParallelDelaunay3d::set_vertices(
2638 index_t nb_vertices, const double* vertices
2639 ) {
2640 Stopwatch* W = nullptr ;
2641
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2642
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
8 W = new Stopwatch("DelInternal");
2643 }
2644
2645
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if(weighted_) {
2646 ✗ heights_.resize(nb_vertices);
2647 ✗ for(index_t i = 0; i < nb_vertices; ++i) {
2648 // Client code uses 4d embedding with ti = sqrt(W - wi)
2649 // where W = max(wi)
2650 // We recompute the standard "shifted" lifting on
2651 // the paraboloid from it.
2652 // (we use wi - W, everything is shifted by W, but
2653 // we do not care since the power diagram is invariant
2654 // by a translation of all weights).
2655 ✗ double w = -geo_sqr(vertices[4 * i + 3]);
2656 ✗ heights_[i] = -w +
2657 ✗ geo_sqr(vertices[4 * i]) +
2658 ✗ geo_sqr(vertices[4 * i + 1]) +
2659 ✗ geo_sqr(vertices[4 * i + 2]);
2660 }
2661 }
2662 6 Delaunay::set_vertices(nb_vertices, vertices);
2663
2664 6 index_t expected_tetra = nb_vertices * 7;
2665
2666 // Allocate the tetrahedra
2667 6 cell_to_v_store_.assign(expected_tetra * 4,NO_INDEX);
2668 6 cell_to_cell_store_.assign(expected_tetra * 4,NO_INDEX);
2669 6 cell_next_.assign(expected_tetra,NO_INDEX);
2670 6 cell_status_.resize(expected_tetra);
2671
2672 // Reorder the points
2673
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if(do_reorder_) {
2674 6 compute_BRIO_order(
2675 6 nb_vertices, vertex_ptr(0), reorder_,
2676 3, dimension(),
2677 64, 0.125,
2678 &levels_
2679 );
2680 } else {
2681 ✗ reorder_.resize(nb_vertices);
2682 ✗ for(index_t i = 0; i < nb_vertices; ++i) {
2683 ✗ reorder_[i] = i;
2684 }
2685 geo_debug_assert(levels_[0] == 0);
2686 geo_debug_assert(levels_[levels_.size()-1] == nb_vertices);
2687 }
2688
2689 double sorting_time = 0;
2690
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2691 4 sorting_time = W->elapsed_time();
2692
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Logger::out("DelInternal1") << "BRIO sorting:"
2693 << sorting_time
2694 << std::endl;
2695 }
2696
2697 // Create the threads
2698 // The maximum number of threads is limited by the number of bits used by
2699 // cell_status_ (see delaunay_sync.h)
2700 index_t nb_threads = std::min(
2701 6 Process::maximum_concurrent_threads(),
2702 CellStatusArray::MAX_THREADS
2703 );
2704 6 index_t pool_size = expected_tetra / nb_threads;
2705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (pool_size == 0) {
2706 // There are more threads than expected_tetra
2707 pool_size = 1;
2708 nb_threads = expected_tetra;
2709 }
2710 index_t pool_begin = 0;
2711 6 threads_.clear();
2712
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
30 for(index_t t=0; t<nb_threads; ++t) {
2713 18 index_t pool_end =
2714
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 (t == nb_threads - 1) ? expected_tetra : pool_begin + pool_size;
2715 threads_.push_back(
2716 48 new Delaunay3dThread(this, pool_begin, pool_end)
2717 );
2718 pool_begin = pool_end;
2719 }
2720
2721
2722 // Create first tetrahedron and triangulate first set of points
2723 // in sequential mode.
2724
2725
2726 index_t lvl = 1;
2727
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
12 while(lvl < (levels_.size() - 1) && levels_[lvl] < 1000) {
2728 6 ++lvl;
2729 }
2730
2731
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2732
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Logger::out("PDEL")
2733
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 << "Using " << levels_.size()-1 << " levels" << std::endl;
2734
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Logger::out("PDEL")
2735
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 << "Levels 0 - " << lvl-1
2736 << ": bootstraping with first levels in sequential mode"
2737 << std::endl;
2738 }
2739 Delaunay3dThread* thread0 =
2740 static_cast<Delaunay3dThread*>(threads_[0].get());
2741 6 thread0->create_first_tetrahedron();
2742 6 thread0->set_work(levels_[0], levels_[lvl]);
2743 6 thread0->run();
2744
2745 index_t first_lvl = lvl;
2746 index_t nb_sequential_points = 0;
2747
2748 // Insert points in all BRIO levels
2749
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
13 for(; lvl<levels_.size()-1; ++lvl) {
2750
2751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(benchmark_mode_) {
2752 ✗ Logger::out("PDEL") << "Level "
2753 << lvl << " : start" << std::endl;
2754 }
2755
2756 1 index_t lvl_b = levels_[lvl];
2757 1 index_t lvl_e = levels_[lvl+1];
2758
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 index_t work_size = (lvl_e - lvl_b)/index_t(threads_.size());
2759
2760 // Initialize threads
2761 index_t b = lvl_b;
2762
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for(index_t t=0; t<threads_.size(); ++t) {
2763
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 index_t e = t == threads_.size()-1 ? lvl_e : b+work_size;
2764 Delaunay3dThread* thread =
2765 static_cast<Delaunay3dThread*>(threads_[t].get());
2766
2767 // Copy the indices of the first created tetrahedron
2768 // and the maximum valid tetrahedron index max_t_
2769
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if(lvl == first_lvl && t!=0) {
2770 thread->initialize_from(thread0);
2771 }
2772 thread->set_work(b,e);
2773 b = e;
2774 }
2775 1 Process::run_threads(threads_);
2776
2777 // Drain overflowed work before the next level replaces its range.
2778 // Outside Process::run_threads(), the memory pool can grow.
2779 index_t nb_level_sequential_points = 0;
2780
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
10 for(index_t t=0; t<threads_.size(); ++t) {
2781 Delaunay3dThread* thread =
2782 static_cast<Delaunay3dThread*>(threads_[t].get());
2783 index_t remaining = thread->work_size();
2784
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if(remaining == 0) {
2785 3 continue;
2786 }
2787 1 nb_level_sequential_points += remaining;
2788
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if(t != 0) {
2789 thread->initialize_from(thread0);
2790 }
2791 1 thread->run();
2792 geo_debug_assert(thread->work_size() == 0);
2793
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(t != 0) {
2794 thread0->initialize_from(thread);
2795 }
2796 }
2797 1 nb_sequential_points += nb_level_sequential_points;
2798
2799 // All workers need the grown storage bound before parallel work
2800 // resumes, including workers whose own ranges were completed.
2801
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if(nb_level_sequential_points != 0) {
2802
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for(index_t t=1; t<threads_.size(); ++t) {
2803 Delaunay3dThread* thread =
2804 static_cast<Delaunay3dThread*>(threads_[t].get());
2805 thread->initialize_from(thread0);
2806 }
2807 }
2808 }
2809
2810
2811
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2812 index_t tot_rollbacks = 0 ;
2813 index_t tot_failed_locate = 0 ;
2814
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
20 for(index_t t=0; t<threads_.size(); ++t) {
2815 Delaunay3dThread* thread =
2816 static_cast<Delaunay3dThread*>(threads_[t].get());
2817
1/2
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 Logger::out("PDEL")
2818 << "thread " << std::setw(3) << t << " : "
2819 << std::setw(3)
2820 << thread->nb_rollbacks() << " rollbacks "
2821 << std::setw(3)
2822 << thread->nb_failed_locate() << " restarted locate"
2823 << std::endl;
2824 16 tot_rollbacks += thread->nb_rollbacks();
2825 16 tot_failed_locate += thread->nb_failed_locate();
2826 }
2827
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 Logger::out("PDEL") << "------------------" << std::endl;
2828
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Logger::out("PDEL") << "total: "
2829 << tot_rollbacks << " rollbacks "
2830 << tot_failed_locate << " restarted locate"
2831 << std::endl;
2832 }
2833
2834
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2835
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if(nb_sequential_points != 0) {
2836 ✗ Logger::out("PDEL") << "Local thread memory overflow occurred:"
2837 << std::endl;
2838 ✗ Logger::out("PDEL") << nb_sequential_points
2839 << " points inserted in sequential mode"
2840 << std::endl;
2841 } else {
2842
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Logger::out("PDEL")
2843 << "All the points were inserted in parallel mode"
2844 << std::endl;
2845 }
2846 }
2847
2848
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2849
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 Logger::out("DelInternal2") << "Core insertion algo:"
2850
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 << W->elapsed_time() - sorting_time
2851 << std::endl;
2852 }
2853
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 delete W;
2854
2855
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(debug_mode_) {
2856
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
36 for(index_t i=0; i<threads_.size(); ++i) {
2857 std::cerr << i << " : " <<
2858 static_cast<Delaunay3dThread*>(threads_[i].get())
2859 ->max_t() << std::endl;
2860 }
2861
2862 4 thread0->check_combinatorics(verbose_debug_mode_);
2863 4 thread0->check_geometry(verbose_debug_mode_);
2864 }
2865
2866
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2867
2/4
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
8 W = new Stopwatch("DelCompress");
2868 }
2869
2870 // Compress cell_to_v_store_ and cell_to_cell_store_
2871 // (remove free and virtual tetrahedra).
2872 // Since cell_next_ is not used at this point,
2873 // we reuse it for storing the conversion array that
2874 // maps old tet indices to new tet indices
2875 // Note: tet_is_real() uses the previous value of
2876 // cell_next(), but we are processing indices
2877 // in increasing order and since old2new[t] is always
2878 // smaller or equal to t, we never overwrite a value
2879 // before needing it.
2880
2881 vector<index_t>& old2new = cell_next_;
2882 index_t nb_tets = 0;
2883 index_t nb_tets_to_delete = 0;
2884
2885 {
2886
2/2
✓ Branch 0 taken 120189 times.
✓ Branch 1 taken 6 times.
120195 for(index_t t = 0; t < thread0->max_t(); ++t) {
2887 if(
2888
6/6
✓ Branch 0 taken 44335 times.
✓ Branch 1 taken 75854 times.
✓ Branch 2 taken 20186 times.
✓ Branch 3 taken 24149 times.
✓ Branch 4 taken 8708 times.
✓ Branch 5 taken 56799 times.
185696 (keep_infinite_ && !thread0->tet_is_free(t)) ||
2889 thread0->tet_is_real(t)
2890 ) {
2891
2/2
✓ Branch 0 taken 80509 times.
✓ Branch 1 taken 439 times.
80948 if(t != nb_tets) {
2892 Memory::copy(
2893 &cell_to_v_store_[nb_tets * 4],
2894 &cell_to_v_store_[t * 4],
2895 4 * sizeof(index_t)
2896 );
2897 Memory::copy(
2898 &cell_to_cell_store_[nb_tets * 4],
2899 &cell_to_cell_store_[t * 4],
2900 4 * sizeof(index_t)
2901 );
2902 }
2903 80948 old2new[t] = nb_tets;
2904 80948 ++nb_tets;
2905 } else {
2906 39241 old2new[t] = NO_INDEX;
2907 39241 ++nb_tets_to_delete;
2908 }
2909 }
2910
2911 6 cell_to_v_store_.resize(4 * nb_tets);
2912 6 cell_to_cell_store_.resize(4 * nb_tets);
2913
2/2
✓ Branch 0 taken 323792 times.
✓ Branch 1 taken 6 times.
323798 for(index_t i = 0; i < 4 * nb_tets; ++i) {
2914 323792 index_t t = cell_to_cell_store_[i];
2915 geo_debug_assert(t != NO_INDEX);
2916 323792 t = old2new[t];
2917 // Note: t can be equal to -1 when a real tet is
2918 // adjacent to a virtual one (and this is how the
2919 // rest of Vorpaline expects to see tets on the
2920 // border).
2921 geo_debug_assert(!(keep_infinite_ && t == NO_INDEX));
2922 323792 cell_to_cell_store_[i] = t;
2923 }
2924 }
2925
2926 // In "keep_infinite" mode, we reorder the cells in such
2927 // a way that finite cells have indices [0..nb_finite_cells_-1]
2928 // and infinite cells have indices [nb_finite_cells_ .. nb_cells_-1]
2929
2930
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if(keep_infinite_) {
2931 3 nb_finite_cells_ = 0;
2932 index_t finite_ptr = 0;
2933 3 index_t infinite_ptr = nb_tets - 1;
2934 for(;;) {
2935
2/2
✓ Branch 0 taken 20700 times.
✓ Branch 1 taken 1602 times.
22302 while(thread0->tet_is_finite(finite_ptr)) {
2936 20700 old2new[finite_ptr] = finite_ptr;
2937 20700 ++finite_ptr;
2938 20700 ++nb_finite_cells_;
2939 }
2940
2/2
✓ Branch 0 taken 251 times.
✓ Branch 1 taken 1602 times.
1853 while(!thread0->tet_is_finite(infinite_ptr)) {
2941 251 old2new[infinite_ptr] = infinite_ptr;
2942 251 --infinite_ptr;
2943 }
2944
2/2
✓ Branch 0 taken 1599 times.
✓ Branch 1 taken 3 times.
1602 if(finite_ptr > infinite_ptr) {
2945 break;
2946 }
2947 1599 old2new[finite_ptr] = infinite_ptr;
2948 1599 old2new[infinite_ptr] = finite_ptr;
2949 1599 ++nb_finite_cells_;
2950
2/2
✓ Branch 0 taken 6396 times.
✓ Branch 1 taken 1599 times.
7995 for(index_t lf=0; lf<4; ++lf) {
2951 6396 std::swap(
2952 6396 cell_to_cell_store_[4*finite_ptr + lf],
2953 6396 cell_to_cell_store_[4*infinite_ptr + lf]
2954 );
2955 }
2956
2/2
✓ Branch 0 taken 6396 times.
✓ Branch 1 taken 1599 times.
7995 for(index_t lv=0; lv<4; ++lv) {
2957 6396 std::swap(
2958 6396 cell_to_v_store_[4*finite_ptr + lv],
2959 6396 cell_to_v_store_[4*infinite_ptr + lv]
2960 );
2961 }
2962 1599 ++finite_ptr;
2963 1599 --infinite_ptr;
2964 1599 }
2965
2/2
✓ Branch 0 taken 96596 times.
✓ Branch 1 taken 3 times.
96599 for(index_t i = 0; i < 4 * nb_tets; ++i) {
2966 96596 index_t t = cell_to_cell_store_[i];
2967 geo_debug_assert(t != NO_INDEX);
2968 96596 t = old2new[t];
2969 geo_debug_assert(t != NO_INDEX);
2970 96596 cell_to_cell_store_[i] = t;
2971 }
2972 }
2973
2974
2975
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if(benchmark_mode_) {
2976
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if(keep_infinite_) {
2977
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Logger::out("DelCompress")
2978 << "Removed " << nb_tets_to_delete
2979 << " tets (free list)" << std::endl;
2980 } else {
2981
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Logger::out("DelCompress")
2982 << "Removed " << nb_tets_to_delete
2983 << " tets (free list and infinite)" << std::endl;
2984 }
2985 }
2986
2987
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 delete W;
2988
2989 6 set_arrays(
2990 nb_tets,
2991 cell_to_v_store_.data(),
2992 cell_to_cell_store_.data()
2993 );
2994 6 }
2995
2996 ✗ index_t ParallelDelaunay3d::nearest_vertex(const double* p) const {
2997 // TODO
2998 ✗ return Delaunay::nearest_vertex(p);
2999 }
3000
3001 ✗ void ParallelDelaunay3d::set_BRIO_levels(const vector<index_t>& levels) {
3002 levels_ = levels;
3003 ✗ }
3004
3005 }
3006
3007 #endif
3008