GCC Code Coverage Report


Directory: ./
File: voronoi/RVD.cpp
Date: 2026-11-30 06:00:29
Exec Total Coverage
Lines: 448 1082 41.4%
Functions: 107 579 18.5%
Branches: 224 938 23.9%

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 #include <geogram/voronoi/RVD.h>
41 #include <geogram/voronoi/generic_RVD.h>
42 #include <geogram/voronoi/RVD_mesh_builder.h>
43 #include <geogram/voronoi/integration_simplex.h>
44 #include <geogram/voronoi/RVD_callback.h>
45 #include <geogram/mesh/mesh_partition.h>
46 #include <geogram/mesh/mesh_sampling.h>
47 #include <geogram/mesh/mesh_repair.h>
48 #include <geogram/mesh/mesh_AABB.h>
49 #include <geogram/delaunay/delaunay.h>
50 #include <geogram/basic/geometry_nd.h>
51 #include <geogram/basic/process.h>
52 #include <geogram/basic/command_line.h>
53 #include <geogram/basic/argused.h>
54 #include <geogram/basic/algorithm.h>
55 #include <geogram/bibliography/bibliography.h>
56
57 #ifdef GEO_DETERMINISTIC
58 // Deterministic CVT/remeshing accumulates per-simplex contributions in
59 // per-thread-part buffers, then reduces them in a canonical order that does
60 // not depend on the number of threads.
61 #include <algorithm>
62 #include <vector>
63 #include <cmath>
64 #include <utility>
65
66 namespace {
67 using namespace GEO;
68
69 /**
70 * \brief Vector of (target index, contribution) pairs accumulated by a
71 * single thread part, then reduced in a canonical order.
72 */
73 typedef std::vector< std::pair<index_t, double> > DeterministicIndexedAccu;
74
75 /**
76 * \brief Orders contributions by magnitude first, then by sign.
77 * \details Summing contributions in this canonical order (independent of
78 * the order in which threads produced them) makes floating point
79 * reductions reproducible, since floating point addition is not
80 * associative.
81 */
82 inline bool deterministic_value_less(double x, double y) {
83 double ax = std::fabs(x);
84 double ay = std::fabs(y);
85 return (ax != ay) ? (ax < ay) : (std::signbit(x) < std::signbit(y));
86 }
87
88 /**
89 * \brief Scratch buffers used by the deterministic reductions.
90 * \details Owned by the master RVD instance and reused across iterations to
91 * avoid reallocations. The reductions run sequentially on the master
92 * thread, so a single scratch instance per master is enough; nothing is
93 * shared between instances (no global state).
94 */
95 struct DeterministicReduceScratch {
96 std::vector<index_t> offset;
97 std::vector<index_t> cursor;
98 std::vector<double> vals;
99 std::vector<double> group_sum;
100 };
101
102 /**
103 * \brief Groups the per-part (index, value) contributions per index with a
104 * counting sort, sorts and sums each group in canonical order, and passes
105 * each non-empty group's sum to \p sink.
106 * \details Each group is summed in an order that does not depend on how the
107 * contributions were split among parts (hence not on the thread count).
108 * The per-group sort and sum are spread over the available threads. This
109 * avoids a global O(N log N) sort and keeps the accumulation
110 * contention-free, so it scales with the thread count.
111 * \param[in] parts the per-part accumulation buffers
112 * \param[in] range one past the largest index that can appear
113 * \param[in,out] s the scratch buffers to use
114 * \param[in] sink called as sink(index_t i, double group_sum) per group
115 */
116 template <class SINK>
117 inline void deterministic_reduce_buckets(
118 const std::vector<DeterministicIndexedAccu*>& parts, index_t range,
119 DeterministicReduceScratch& s, const SINK& sink
120 ) {
121 size_t nb = 0;
122 for(DeterministicIndexedAccu* p : parts) {
123 nb += p->size();
124 }
125 if(nb == 0) {
126 return;
127 }
128 s.offset.assign(size_t(range) + 1, 0);
129 for(DeterministicIndexedAccu* p : parts) {
130 for(const std::pair<index_t, double>& kv : *p) {
131 ++s.offset[kv.first + 1];
132 }
133 }
134 for(index_t i = 0; i < range; ++i) {
135 s.offset[i + 1] += s.offset[i];
136 }
137 s.vals.resize(nb);
138 s.cursor.assign(s.offset.begin(), s.offset.begin() + range);
139 for(DeterministicIndexedAccu* p : parts) {
140 for(const std::pair<index_t, double>& kv : *p) {
141 s.vals[s.cursor[kv.first]++] = kv.second;
142 }
143 }
144 double* vals = s.vals.data();
145 const index_t* offs = s.offset.data();
146 parallel_for_slice(
147 0, range,
148 [vals, offs, &sink](index_t i0, index_t i1) {
149 for(index_t i = i0; i < i1; ++i) {
150 index_t b = offs[i];
151 index_t e = offs[i + 1];
152 if(e == b) {
153 continue;
154 }
155 if(e - b > 1) {
156 std::sort(vals + b, vals + e, deterministic_value_less);
157 }
158 double acc = 0.0;
159 for(index_t k = b; k < e; ++k) {
160 acc += vals[k];
161 }
162 sink(i, acc);
163 }
164 }
165 );
166 }
167
168 /**
169 * \brief Reduces per-part (index, value) contributions, adding each index's
170 * canonical sum to out[index].
171 */
172 inline void deterministic_reduce_indexed(
173 const std::vector<DeterministicIndexedAccu*>& parts,
174 double* out, index_t range, DeterministicReduceScratch& s
175 ) {
176 deterministic_reduce_buckets(
177 parts, range, s,
178 [out](index_t i, double v) { out[i] += v; }
179 );
180 }
181
182 /**
183 * \brief Reduces per-part (index, value) contributions into a single sum.
184 * \details Each index is summed in canonical order, then the per-index sums
185 * are added in increasing index order, so the result does not depend on
186 * the thread count. Using the index (e.g. the seed vertex) as a grouping
187 * key keeps the reduction load-balanced and parallel, unlike a global sort
188 * of all scalar contributions.
189 */
190 inline double deterministic_reduce_indexed_total(
191 const std::vector<DeterministicIndexedAccu*>& parts, index_t range,
192 DeterministicReduceScratch& s
193 ) {
194 s.group_sum.assign(range, 0.0);
195 double* gs = s.group_sum.data();
196 deterministic_reduce_buckets(
197 parts, range, s,
198 [gs](index_t i, double v) { gs[i] = v; }
199 );
200 double result = 0.0;
201 for(index_t i = 0; i < range; ++i) {
202 result += s.group_sum[i];
203 }
204 return result;
205 }
206
207 /**
208 * \brief Clears a part's accumulator, points its master pointer at it, and
209 * returns the accumulator address (for the reduction's parts list).
210 */
211 inline DeterministicIndexedAccu* deterministic_reset_part(
212 DeterministicIndexedAccu& accu, DeterministicIndexedAccu*& master
213 ) {
214 accu.clear();
215 master = &accu;
216 return &accu;
217 }
218 }
219 #endif
220
221 /*
222 * There are three levels of implementation:
223 * Level 1: RestrictedVoronoiDiagram is the abstract API seen from client code
224 * Level 2: RVD_Nd_Impl<DIM> implements RestrictedVoronoiDiagram
225 * Level 3: RVD_Nd_Impl<DIM>::GenRestrictedVoronoiDiagram is
226 * an instantiation of GEOGen::RestrictedVoronoiDiagram (from generic_RVD.h)
227 *
228 * Warning: there are approx. 1000 lines of boring code ahead.
229 */
230
231 namespace {
232
233 using namespace GEO;
234
235 /**
236 * \brief Generic implementation of RestrictedVoronoiDiagram.
237 * \tparam DIM dimension
238 */
239 template <unsigned int DIM>
240 class RVD_Nd_Impl : public GEO::RestrictedVoronoiDiagram {
241
242 /** \brief This class type */
243 typedef RVD_Nd_Impl<DIM> thisclass;
244
245 /** \brief The base class of this class */
246 typedef RestrictedVoronoiDiagram baseclass;
247
248 public:
249 /** \brief Implementation based on the generic version. */
250 typedef GEOGen::RestrictedVoronoiDiagram<DIM>
251 GenRestrictedVoronoiDiagram;
252
253 /** \brief Representation of points. */
254 typedef vecng<DIM, double> Point;
255
256 /** \brief Representation of vectors. */
257 typedef vecng<DIM, double> Vector;
258
259 /** \brief Represents a point and its symbolic information. */
260 typedef typename GenRestrictedVoronoiDiagram::Vertex Vertex;
261
262 /**
263 * \brief Specifies the computation done by the threads.
264 */
265 enum ThreadMode {
266 MT_NONE, /**< uninitialized */
267 MT_LLOYD, /**< Lloyd iteration */
268 MT_NEWTON, /**< Newton optimization */
269 MT_INT_SMPLX, /**< Newton with integration simplex */
270 MT_POLYG, /**< Polygon callback */
271 MT_POLYH /**< Polyhedron callback */
272 };
273
274 /**
275 * \brief Gets a mesh vertex from its index.
276 * \param[in] v index of the vertex
277 * \return a const reference to a Point
278 */
279 ✗ const Point& mesh_vertex(index_t v) {
280 ✗ return *(const Point*) mesh_->vertices.point_ptr(v);
281 }
282
283
284 /**
285 * \brief Creates a RVD_Nd_Impl.
286 *
287 * \details The dimension is determined by \p mesh->dimension().
288 * \param[in] delaunay the Delaunay triangulation
289 * \param[in] mesh the input mesh
290 * \param[in] R3_embedding gives for each vertex
291 * its mapping in 3D space.
292 * \param[in] R3_embedding_stride gives the stride between
293 * two consecutive vertices in R3_embedding
294 */
295 96 RVD_Nd_Impl(
296 Delaunay* delaunay, Mesh* mesh,
297 const double* R3_embedding, index_t R3_embedding_stride
298 ) :
299 RestrictedVoronoiDiagram(
300 delaunay, mesh, R3_embedding, R3_embedding_stride
301 ),
302
1/2
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
96 RVD_(delaunay, mesh) {
303 96 use_exact_projection_ = false;
304 96 is_slave_ = false;
305 96 master_ = nullptr;
306 96 has_weights_ = false;
307
4/6
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 47 times.
288 if(mesh->vertices.attributes().is_defined("weight")) {
308
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
4 vertex_weight_.bind(mesh->vertices.attributes(), "weight");
309 2 has_weights_ = true;
310 }
311 96 parts_ = nullptr;
312 96 nb_parts_ = 0;
313 96 funcval_ = 0.0;
314 96 simplex_func_ = nullptr;
315 96 polygon_callback_ = nullptr;
316 96 polyhedron_callback_ = nullptr;
317 96 arg_vectors_ = nullptr;
318 96 arg_scalars_ = nullptr;
319 96 thread_mode_ = MT_NONE;
320 96 nb_triangles_ = 0;
321 96 }
322
323 /**
324 * \brief Constructor for parts, used in multithreading mode.
325 */
326 96 RVD_Nd_Impl() :
327 RestrictedVoronoiDiagram(nullptr, nullptr, nullptr, 0),
328
1/2
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
96 RVD_(nullptr, nullptr) {
329 96 use_exact_projection_ = false;
330 96 is_slave_ = true;
331 96 master_ = nullptr;
332 96 mesh_ = nullptr;
333 96 parts_ = nullptr;
334 96 nb_parts_ = 0;
335 96 facets_begin_ = NO_INDEX;
336 96 facets_end_ = NO_INDEX;
337 96 funcval_ = 0.0;
338 96 simplex_func_ = nullptr;
339 96 polygon_callback_ = nullptr;
340 96 polyhedron_callback_ = nullptr;
341 96 arg_vectors_ = nullptr;
342 96 arg_scalars_ = nullptr;
343 96 thread_mode_ = MT_NONE;
344 96 nb_triangles_ = 0;
345 96 }
346
347 96 void set_delaunay(Delaunay* delaunay) override {
348 96 baseclass::set_delaunay(delaunay);
349 96 RVD_.set_delaunay(delaunay);
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
96 for(index_t p = 0; p < nb_parts_; ++p) {
351 ✗ parts_[p].set_delaunay(delaunay);
352 }
353 96 }
354
355 280 void set_check_SR(bool x) override {
356 280 RVD_.set_check_SR(x);
357
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 140 times.
416 for(index_t p = 0; p < nb_parts_; ++p) {
358 136 parts_[p].set_check_SR(x);
359 }
360 280 }
361
362 206 void set_exact_predicates(bool x) override {
363 206 RVD_.set_exact_predicates(x);
364
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 103 times.
238 for(index_t p = 0; p < nb_parts_; ++p) {
365 32 parts_[p].set_exact_predicates(x);
366 }
367 206 }
368
369 ✗ bool exact_predicates() const override {
370 ✗ return RVD_.exact_predicates();
371 }
372
373 /********************************************************************/
374
375 /**
376 * \brief Place holder, "no locking" policy.
377 * \details NoLocks is used by algorithms templated
378 * by locking policy, for the single-threaded instances
379 * that do not need synchronization. The multi-threaded
380 * instances are parameterized by SpinLockArray.
381 */
382 class NoLocks {
383 public:
384 /**
385 * \brief Acquires a spinlock.
386 * \details Does nothing in this version
387 * \param[in] i index of the spinlock to acquire
388 */
389 ✗ void acquire_spinlock(index_t i) {
390 ✗ geo_argused(i);
391 ✗ }
392
393 /**
394 * \brief Releases a spinlock.
395 * \details Does nothing in this version
396 * \param[in] i index of the spinlock to release
397 */
398 ✗ void release_spinlock(index_t i) {
399 ✗ geo_argused(i);
400 ✗ }
401 };
402
403 /******************************************************************/
404
405 /**
406 * \brief Implementation class for surfacic Lloyd relaxation.
407 * \details To be used as a template argument
408 * to RVD::for_each_triangle().
409 * This version ignores the weights.
410 *
411 * Computes for each RVD cell:
412 * - mg[v] (v's Voronoi cell's total area times centroid)
413 * - m[v] (v's total area)
414 * \tparam LOCKS locking policy
415 * (can be one of Process::SpinLockArray, NoLocks)
416 */
417 template <class LOCKS>
418 class ComputeCentroids {
419 public:
420 /**
421 * \brief Constructs a ComputeCentroids.
422 * \param[out] mg where to store the centroids
423 * \param[out] m where to store the masses
424 * \param[in] locks the array of locks
425 * (or NoLocks in single thread mode)
426 */
427 2040 ComputeCentroids(
428 double* mg,
429 double* m,
430 #ifdef GEO_DETERMINISTIC
431 DeterministicIndexedAccu* master_g,
432 DeterministicIndexedAccu* master_m,
433 #endif
434 LOCKS& locks
435 ) :
436 2040 mg_(mg),
437 2040 m_(m),
438 #ifdef GEO_DETERMINISTIC
439 master_g_(master_g),
440 master_m_(master_m),
441 #endif
442 2040 locks_(locks) {
443 2040 }
444
445 /**
446 * \brief The callback called for each integration simplex.
447 * \param[in] v index of current center vertex
448 * \param[in] p1 first vertex of current integration simplex
449 * \param[in] p2 second vertex of current integration simplex
450 * \param[in] p3 third vertex of current integration simplex
451 */
452 23610376 void operator() (
453 index_t v,
454 const double* p1,
455 const double* p2,
456 const double* p3
457 ) const {
458 23610376 double cur_m = Geom::triangle_area(p1, p2, p3, DIM);
459 23610376 double s = cur_m / 3.0;
460 #ifdef GEO_DETERMINISTIC
461 if(master_m_ != nullptr) {
462 master_m_->emplace_back(v, cur_m);
463 for(coord_index_t coord = 0; coord < DIM; coord++) {
464 double val = s * (p1[coord] + p2[coord] + p3[coord]);
465 master_g_->emplace_back(v * DIM + coord, val);
466 }
467 return;
468 }
469 #endif
470 23610376 locks_.acquire_spinlock(v);
471 23610376 m_[v] += cur_m;
472 23610376 double* cur_mg_out = mg_ + v * DIM;
473
2/2
✓ Branch 0 taken 52884744 times.
✓ Branch 1 taken 11805188 times.
129379864 for(coord_index_t coord = 0; coord < DIM; coord++) {
474 105769488 cur_mg_out[coord] +=
475 105769488 s * (p1[coord] + p2[coord] + p3[coord]);
476 }
477 23610376 locks_.release_spinlock(v);
478 23610376 }
479
480 private:
481 double* mg_;
482 double* m_;
483 #ifdef GEO_DETERMINISTIC
484 DeterministicIndexedAccu* master_g_;
485 DeterministicIndexedAccu* master_m_;
486 #endif
487 LOCKS& locks_;
488 };
489
490 /**
491 * \brief Implementation class for surfacic Lloyd relaxation.
492 * \details To be used as a template
493 * argument to RVD::for_each_triangle().
494 * This version takes the weights into account.
495 *
496 * Computes for each RVD cell:
497 * - mg[v] (v's Voronoi cell's total area times centroid)
498 * - m[v] (v's total area)
499 * \tparam LOCKS locking policy
500 * (can be one of Process::SpinLockArray, NoLocks)
501 */
502 template <class LOCKS>
503 class ComputeCentroidsWeighted {
504 public:
505 /**
506 * \brief Constructs a ComputeCentroidsWeighted.
507 * \param[out] mg where to store the centroids
508 * \param[out] m where to store the masses
509 * \param[in] locks the array of locks
510 * (or NoLocks in single thread mode)
511 */
512 320 ComputeCentroidsWeighted(
513 double* mg,
514 double* m,
515 #ifdef GEO_DETERMINISTIC
516 DeterministicIndexedAccu* master_g,
517 DeterministicIndexedAccu* master_m,
518 #endif
519 LOCKS& locks
520 ) :
521 320 mg_(mg),
522 320 m_(m),
523 #ifdef GEO_DETERMINISTIC
524 master_g_(master_g),
525 master_m_(master_m),
526 #endif
527 320 locks_(locks) {
528 320 }
529
530 /**
531 * \brief The callback called for each integration simplex.
532 * \param[in] v index of current center vertex
533 * \param[in] v1 first vertex of current integration simplex
534 * \param[in] v2 second vertex of current integration simplex
535 * \param[in] v3 third vertex of current integration simplex
536 */
537 12509398 void operator() (
538 index_t v,
539 const Vertex& v1,
540 const Vertex& v2,
541 const Vertex& v3
542 ) const {
543 double cur_m;
544 double cur_Vg[DIM];
545
1/2
✓ Branch 7 taken 6254699 times.
✗ Branch 8 not taken.
12509398 Geom::triangle_centroid(
546 v1.point(), v2.point(), v3.point(),
547 v1.weight(), v2.weight(), v3.weight(),
548 cur_Vg, cur_m, DIM
549 );
550 #ifdef GEO_DETERMINISTIC
551 if(master_m_ != nullptr) {
552 master_m_->emplace_back(v, cur_m);
553 for(coord_index_t coord = 0; coord < DIM; coord++) {
554 master_g_->emplace_back(v * DIM + coord, cur_Vg[coord]);
555 }
556 return;
557 }
558 #endif
559
1/2
✓ Branch 1 taken 6254699 times.
✗ Branch 2 not taken.
12509398 locks_.acquire_spinlock(v);
560 12509398 m_[v] += cur_m;
561 12509398 double* cur_mg_out = mg_ + v * DIM;
562
2/2
✓ Branch 0 taken 37528194 times.
✓ Branch 1 taken 6254699 times.
87565786 for(coord_index_t coord = 0; coord < DIM; coord++) {
563 75056388 cur_mg_out[coord] += cur_Vg[coord];
564 }
565
1/2
✓ Branch 1 taken 6254699 times.
✗ Branch 2 not taken.
12509398 locks_.release_spinlock(v);
566 12509398 }
567
568 private:
569 double* mg_;
570 double* m_;
571 #ifdef GEO_DETERMINISTIC
572 DeterministicIndexedAccu* master_g_;
573 DeterministicIndexedAccu* master_m_;
574 #endif
575 LOCKS& locks_;
576 };
577
578 2950 void compute_centroids_on_surface(double* mg, double* m) override {
579 2950 create_threads();
580
2/2
✓ Branch 1 taken 1180 times.
✓ Branch 2 taken 295 times.
2950 if(nb_parts() == 0) {
581
1/2
✓ Branch 0 taken 1180 times.
✗ Branch 1 not taken.
2360 if(master_ != nullptr) {
582 #ifdef GEO_DETERMINISTIC
583 if(has_weights_) {
584 RVD_.for_each_triangle(
585 ComputeCentroidsWeighted<Process::SpinLockArray>(
586 mg, m, master_g_, master_m_,
587 master_->spinlocks_
588 )
589 );
590 } else {
591 RVD_.for_each_triangle(
592 ComputeCentroids<Process::SpinLockArray>(
593 mg, m, master_g_, master_m_,
594 master_->spinlocks_
595 )
596 );
597 }
598 #else
599
2/2
✓ Branch 0 taken 160 times.
✓ Branch 1 taken 1020 times.
2360 if(has_weights_) {
600
1/2
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
320 RVD_.for_each_triangle(
601 320 ComputeCentroidsWeighted<Process::SpinLockArray>(
602 320 mg, m, master_->spinlocks_
603 )
604 );
605 } else {
606
1/2
✓ Branch 1 taken 1020 times.
✗ Branch 2 not taken.
2040 RVD_.for_each_triangle(
607 2040 ComputeCentroids<Process::SpinLockArray>(
608 2040 mg, m, master_->spinlocks_
609 )
610 );
611 }
612 #endif
613 } else {
614 NoLocks nolocks;
615 #ifdef GEO_DETERMINISTIC
616 if(has_weights_) {
617 RVD_.for_each_triangle(
618 ComputeCentroidsWeighted<NoLocks>(
619 mg, m, nullptr, nullptr, nolocks
620 )
621 );
622 } else {
623 RVD_.for_each_triangle(
624 ComputeCentroids<NoLocks>(
625 mg, m, nullptr, nullptr, nolocks
626 )
627 );
628 }
629 #else
630 ✗ if(has_weights_) {
631 ✗ RVD_.for_each_triangle(
632 ✗ ComputeCentroidsWeighted<NoLocks>(
633 mg, m, nolocks
634 )
635 );
636 } else {
637 ✗ RVD_.for_each_triangle(
638 ✗ ComputeCentroids<NoLocks>(mg, m, nolocks)
639 );
640 }
641 #endif
642 }
643 } else {
644 590 thread_mode_ = MT_LLOYD;
645 590 arg_vectors_ = mg;
646 590 arg_scalars_ = m;
647 590 spinlocks_.resize(delaunay_->nb_vertices());
648 #ifdef GEO_DETERMINISTIC
649 std::vector<DeterministicIndexedAccu*> g_parts;
650 std::vector<DeterministicIndexedAccu*> m_parts;
651 for(index_t t = 0; t < nb_parts(); t++) {
652 g_parts.push_back(deterministic_reset_part(
653 part(t).accu_g_, part(t).master_g_
654 ));
655 m_parts.push_back(deterministic_reset_part(
656 part(t).accu_m_, part(t).master_m_
657 ));
658 }
659 #endif
660
1/2
✓ Branch 2 taken 295 times.
✗ Branch 3 not taken.
590 parallel_for(
661 0, nb_parts(),
662 2360 [this](index_t i) { run_thread(i); }
663 );
664 #ifdef GEO_DETERMINISTIC
665 deterministic_reduce_indexed(
666 m_parts, m, delaunay_->nb_vertices(), det_scratch_
667 );
668 deterministic_reduce_indexed(
669 g_parts, mg, index_t(DIM) * delaunay_->nb_vertices(),
670 det_scratch_
671 );
672 #endif
673 }
674 2950 }
675
676 /********************************************************************/
677
678 /**
679 * \brief Implementation class for surfacic Lloyd relaxation.
680 * \details To be used as a template argument
681 * to RVD::for_each_volumetric_integration_simplex().
682 * This version ignores the weights.
683 *
684 * Computes for each RVD cell:
685 * - mg[v] (v's Voronoi cell's total area times centroid)
686 * - m[v] (v's total area)
687 * \tparam LOCKS locking policy
688 * (can be one of Process::SpinLockArray, NoLocks)
689 */
690 template <class LOCKS>
691 class ComputeCentroidsVolumetric {
692 public:
693 /**
694 * \brief Constructs a ComputeCentroidsVolumetric.
695 * \param[out] mg where to store the centroids
696 * \param[out] m where to store the masses
697 * \param[in] delaunay the Delaunay triangulation
698 * \param[in] locks the array of locks
699 * (or NoLocks in single thread mode)
700 */
701 1280 ComputeCentroidsVolumetric(
702 double* mg,
703 double* m,
704 const Delaunay* delaunay,
705 #ifdef GEO_DETERMINISTIC
706 DeterministicIndexedAccu* master_g,
707 DeterministicIndexedAccu* master_m,
708 #endif
709 LOCKS& locks
710 ) :
711 1280 mg_(mg),
712 1280 m_(m),
713 1280 delaunay_(delaunay),
714 #ifdef GEO_DETERMINISTIC
715 master_g_(master_g),
716 master_m_(master_m),
717 #endif
718 1280 locks_(locks) {
719 1280 }
720
721 /**
722 * \brief The callback called for each integration simplex.
723 * \param[in] v index of current center vertex
724 * \param[in] v_adj (unused here) is the index of the Voronoi cell
725 * adjacent to t accros facet (\p v1, \p v2, \p v3) or
726 * NO_INDEX if it does not exists
727 * \param[in] t (unused here) is the index of the current
728 * tetrahedron
729 * \param[in] t_adj (unused here) is the index of the
730 * tetrahedron adjacent to t accros facet (\p v1, \p v2, \p v3)
731 * or NO_INDEX if it does not exists
732 * \param[in] p0 first vertex of current integration simplex
733 * \param[in] p1 second vertex of current integration simplex
734 * \param[in] p2 third vertex of current integration simplex
735 * \param[in] p3 fourth vertex of current integration simplex
736 */
737 11666782 void operator() (
738 index_t v, index_t v_adj,
739 index_t t, index_t t_adj,
740 const double* p0,
741 const double* p1,
742 const double* p2,
743 const double* p3
744 ) const {
745 11666782 geo_argused(v_adj);
746 11666782 geo_argused(t);
747 11666782 geo_argused(t_adj);
748 11666782 double cur_m = Geom::tetra_volume<DIM>(
749 p0, p1, p2, p3
750 );
751 11666782 double s = cur_m / 4.0;
752 #ifdef GEO_DETERMINISTIC
753 if(master_m_ != nullptr) {
754 master_m_->emplace_back(v, cur_m);
755 for(coord_index_t coord = 0; coord < DIM; coord++) {
756 double val = s * (
757 p0[coord] + p1[coord] + p2[coord] + p3[coord]
758 );
759 master_g_->emplace_back(v * DIM + coord, val);
760 }
761 return;
762 }
763 #endif
764 11666782 locks_.acquire_spinlock(v);
765 11666782 m_[v] += cur_m;
766 11666782 double* cur_mg_out = mg_ + v * DIM;
767
2/2
✓ Branch 0 taken 32227042 times.
✓ Branch 1 taken 5833391 times.
76120866 for(coord_index_t coord = 0; coord < DIM; coord++) {
768 64454084 cur_mg_out[coord] += s * (
769 64454084 p0[coord] + p1[coord] + p2[coord] + p3[coord]
770 );
771 }
772 11666782 locks_.release_spinlock(v);
773 11666782 }
774
775 private:
776 double* mg_;
777 double* m_;
778 const Delaunay* delaunay_;
779 #ifdef GEO_DETERMINISTIC
780 DeterministicIndexedAccu* master_g_;
781 DeterministicIndexedAccu* master_m_;
782 #endif
783 LOCKS& locks_;
784 };
785
786 1600 void compute_centroids_in_volume(double* mg, double* m) override {
787 1600 create_threads();
788
2/2
✓ Branch 1 taken 640 times.
✓ Branch 2 taken 160 times.
1600 if(nb_parts() == 0) {
789
1/2
✓ Branch 0 taken 640 times.
✗ Branch 1 not taken.
1280 if(master_ != nullptr) {
790 #ifdef GEO_DETERMINISTIC
791 RVD_.for_each_tetrahedron(
792 ComputeCentroidsVolumetric<Process::SpinLockArray>(
793 mg, m, RVD_.delaunay(), master_g_, master_m_,
794 master_->spinlocks_
795 )
796 );
797 #else
798
1/2
✓ Branch 1 taken 640 times.
✗ Branch 2 not taken.
1280 RVD_.for_each_tetrahedron(
799 2560 ComputeCentroidsVolumetric<Process::SpinLockArray>(
800 1280 mg, m, RVD_.delaunay(), master_->spinlocks_
801 )
802 );
803 #endif
804 } else {
805 NoLocks nolocks;
806 #ifdef GEO_DETERMINISTIC
807 RVD_.for_each_tetrahedron(
808 ComputeCentroidsVolumetric<NoLocks>(
809 mg, m, RVD_.delaunay(), nullptr, nullptr, nolocks
810 )
811 );
812 #else
813 ✗ RVD_.for_each_tetrahedron(
814 ✗ ComputeCentroidsVolumetric<NoLocks>(
815 ✗ mg, m, RVD_.delaunay(), nolocks
816 )
817 );
818 #endif
819 }
820 } else {
821 320 thread_mode_ = MT_LLOYD;
822 320 arg_vectors_ = mg;
823 320 arg_scalars_ = m;
824 320 spinlocks_.resize(delaunay_->nb_vertices());
825 #ifdef GEO_DETERMINISTIC
826 std::vector<DeterministicIndexedAccu*> g_parts;
827 std::vector<DeterministicIndexedAccu*> m_parts;
828 for(index_t t = 0; t < nb_parts(); t++) {
829 g_parts.push_back(deterministic_reset_part(
830 part(t).accu_g_, part(t).master_g_
831 ));
832 m_parts.push_back(deterministic_reset_part(
833 part(t).accu_m_, part(t).master_m_
834 ));
835 }
836 #endif
837
1/2
✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
320 parallel_for(
838 0, nb_parts(),
839 1280 [this](index_t i) { run_thread(i); }
840 );
841 #ifdef GEO_DETERMINISTIC
842 deterministic_reduce_indexed(
843 m_parts, m, delaunay_->nb_vertices(), det_scratch_
844 );
845 deterministic_reduce_indexed(
846 g_parts, mg, index_t(DIM) * delaunay_->nb_vertices(),
847 det_scratch_
848 );
849 #endif
850 }
851 1600 }
852
853 /********************************************************************/
854
855 /**
856 * \brief Implementation class for Newton-based restricted CVT.
857 * \details To be used as a template argument
858 * to RVD::for_each_triangle().
859 * This version ignores the weights.
860 *
861 * Computes for each RVD cell:
862 * - g (gradient)
863 * - f (CVT energy)
864 * \tparam LOCKS locking policy
865 * (can be one of Process::SpinLockArray, NoLocks)
866 */
867 template <class LOCKS>
868 class ComputeCVTFuncGrad {
869 public:
870 /**
871 * \brief Constructs a ComputeCVTFuncGrad.
872 * \param[in] RVD the restricted Voronoi diagram
873 * \param[out] f the computed function value
874 * \param[out] g the computed gradient of f,
875 * allocated by caller, and managed
876 * by caller
877 * \param[in] locks the array of locks
878 * (or NoLocks in single thread mode)
879 */
880 ✗ ComputeCVTFuncGrad(
881 const GenRestrictedVoronoiDiagram& RVD,
882 double& f,
883 double* g,
884 #ifdef GEO_DETERMINISTIC
885 DeterministicIndexedAccu* master_f,
886 DeterministicIndexedAccu* master_g,
887 #endif
888 LOCKS& locks
889 ) :
890 ✗ f_(f),
891 ✗ g_(g),
892 #ifdef GEO_DETERMINISTIC
893 master_f_(master_f),
894 master_g_(master_g),
895 #endif
896 ✗ locks_(locks),
897 ✗ RVD_(RVD) {
898 ✗ }
899
900 /**
901 * \brief The callback called for each integration simplex.
902 * \param[in] v index of current center vertex
903 * \param[in] p1 first vertex of current integration simplex
904 * \param[in] p2 second vertex of current integration simplex
905 * \param[in] p3 third vertex of current integration simplex
906 */
907 ✗ void operator() (
908 index_t v,
909 const double* p1,
910 const double* p2,
911 const double* p3
912 ) const {
913
914 ✗ const double* p0 = RVD_.delaunay()->vertex_ptr(v);
915
916 ✗ double t_area = Geom::triangle_area(p1, p2, p3, DIM);
917
918 ✗ double cur_f = 0.0;
919 ✗ for(index_t c = 0; c < DIM; c++) {
920 ✗ double u0 = p0[c] - p1[c];
921 ✗ double u1 = p0[c] - p2[c];
922 ✗ double u2 = p0[c] - p3[c];
923 ✗ cur_f += u0 * u0;
924 ✗ cur_f += u1 * (u0 + u1);
925 ✗ cur_f += u2 * (u0 + u1 + u2);
926 }
927
928 #ifdef GEO_DETERMINISTIC
929 if(master_f_ != nullptr) {
930 master_f_->emplace_back(v, t_area * cur_f / 6.0);
931 for(index_t c = 0; c < DIM; c++) {
932 double Gc = (1.0 / 3.0) * (p1[c] + p2[c] + p3[c]);
933 double val = (2.0 * t_area) * (p0[c] - Gc);
934 master_g_->emplace_back(v * DIM + c, val);
935 }
936 return;
937 }
938 #endif
939 ✗ f_ += t_area * cur_f / 6.0;
940
941 ✗ locks_.acquire_spinlock(v);
942 ✗ for(index_t c = 0; c < DIM; c++) {
943 ✗ double Gc = (1.0 / 3.0) * (p1[c] + p2[c] + p3[c]);
944 ✗ g_[DIM * v + c] += (2.0 * t_area) * (p0[c] - Gc);
945 }
946 ✗ locks_.release_spinlock(v);
947 ✗ }
948
949 double& f_;
950 double* g_;
951 #ifdef GEO_DETERMINISTIC
952 DeterministicIndexedAccu* master_f_;
953 DeterministicIndexedAccu* master_g_;
954 #endif
955 LOCKS& locks_;
956 const GenRestrictedVoronoiDiagram& RVD_;
957 };
958
959 /**
960 * \brief Implementation class for Newton-based restricted CVT.
961 * \details To be used as a template argument
962 * to RVD::for_each_triangle().
963 * This version takes the weights into account.
964 *
965 * Computes for each RVD cell:
966 * - g (gradient)
967 * - f (CVT energy)
968 * \tparam LOCKS locking policy
969 * (can be one of Process::SpinLockArray, NoLocks)
970 */
971 template <class LOCKS>
972 class ComputeCVTFuncGradWeighted {
973 public:
974 /**
975 * \brief Constructs a ComputeCVTFuncGradWeighted.
976 * \param[in] RVD the restricted Voronoi diagram
977 * \param[out] f the computed function value
978 * \param[out] g the computed gradient of f,
979 * allocated by caller, and managed by caller
980 * \param[in] locks the array of locks
981 * (or NoLocks in single thread mode)
982 */
983 ✗ ComputeCVTFuncGradWeighted(
984 const GenRestrictedVoronoiDiagram& RVD,
985 double& f,
986 double* g,
987 #ifdef GEO_DETERMINISTIC
988 DeterministicIndexedAccu* master_f,
989 DeterministicIndexedAccu* master_g,
990 #endif
991 LOCKS& locks
992 ) :
993 ✗ f_(f),
994 ✗ g_(g),
995 #ifdef GEO_DETERMINISTIC
996 master_f_(master_f),
997 master_g_(master_g),
998 #endif
999 ✗ locks_(locks),
1000 ✗ RVD_(RVD) {
1001 ✗ }
1002
1003 /**
1004 * \brief The callback called for each integration simplex.
1005 * \param[in] v index of current center vertex
1006 * \param[in] v1 first vertex of current integration simplex
1007 * \param[in] v2 second vertex of current integration simplex
1008 * \param[in] v3 third vertex of current integration simplex
1009 */
1010 ✗ void operator() (
1011 index_t v,
1012 const Vertex& v1,
1013 const Vertex& v2,
1014 const Vertex& v3
1015 ) const {
1016
1017 ✗ const double* p0 = RVD_.delaunay()->vertex_ptr(v);
1018
1019 ✗ const double* p1 = v1.point();
1020 ✗ const double* p2 = v2.point();
1021 ✗ const double* p3 = v3.point();
1022
1023 ✗ double t_area = Geom::triangle_area(p1, p2, p3, DIM);
1024
1025 ✗ double Sp = v1.weight() + v2.weight() + v3.weight();
1026 double rho[3], alpha[3];
1027 ✗ rho[0] = v1.weight();
1028 ✗ rho[1] = v2.weight();
1029 ✗ rho[2] = v3.weight();
1030 ✗ alpha[0] = Sp + rho[0];
1031 ✗ alpha[1] = Sp + rho[1];
1032 ✗ alpha[2] = Sp + rho[2];
1033
1034 ✗ double dotprod_00 = 0.0;
1035 ✗ double dotprod_10 = 0.0;
1036 ✗ double dotprod_11 = 0.0;
1037 ✗ double dotprod_20 = 0.0;
1038 ✗ double dotprod_21 = 0.0;
1039 ✗ double dotprod_22 = 0.0;
1040 ✗ for(unsigned int c = 0; c < DIM; c++) {
1041 ✗ double sp0 = p0[c] - p1[c];
1042 ✗ double sp1 = p0[c] - p2[c];
1043 ✗ double sp2 = p0[c] - p3[c];
1044 ✗ dotprod_00 += sp0 * sp0;
1045 ✗ dotprod_10 += sp1 * sp0;
1046 ✗ dotprod_11 += sp1 * sp1;
1047 ✗ dotprod_20 += sp2 * sp0;
1048 ✗ dotprod_21 += sp2 * sp1;
1049 ✗ dotprod_22 += sp2 * sp2;
1050 }
1051
1052 ✗ double cur_f = 0.0;
1053 ✗ cur_f += (alpha[0] + rho[0]) * dotprod_00; // 0 0
1054 ✗ cur_f += (alpha[1] + rho[0]) * dotprod_10; // 1 0
1055 ✗ cur_f += (alpha[1] + rho[1]) * dotprod_11; // 1 1
1056 ✗ cur_f += (alpha[2] + rho[0]) * dotprod_20; // 2 0
1057 ✗ cur_f += (alpha[2] + rho[1]) * dotprod_21; // 2 1
1058 ✗ cur_f += (alpha[2] + rho[2]) * dotprod_22; // 2 2
1059
1060 #ifdef GEO_DETERMINISTIC
1061 if(master_f_ != nullptr) {
1062 master_f_->emplace_back(v, t_area * cur_f / 30.0);
1063 for(index_t c = 0; c < DIM; c++) {
1064 double val = (t_area / 6.0) * (
1065 4.0 * Sp * p0[c] - (
1066 alpha[0] * p1[c] +
1067 alpha[1] * p2[c] +
1068 alpha[2] * p3[c]
1069 )
1070 );
1071 master_g_->emplace_back(v * DIM + c, val);
1072 }
1073 return;
1074 }
1075 #endif
1076 ✗ f_ += t_area * cur_f / 30.0;
1077 ✗ double* g_out = g_ + v * DIM;
1078 ✗ locks_.acquire_spinlock(v);
1079 ✗ for(index_t c = 0; c < DIM; c++) {
1080 ✗ g_out[c] += (t_area / 6.0) * (
1081 ✗ 4.0 * Sp * p0[c] - (
1082 ✗ alpha[0] * p1[c] +
1083 ✗ alpha[1] * p2[c] +
1084 ✗ alpha[2] * p3[c]
1085 )
1086 );
1087 }
1088 ✗ locks_.release_spinlock(v);
1089 ✗ }
1090
1091 double& f_;
1092 double* g_;
1093 #ifdef GEO_DETERMINISTIC
1094 DeterministicIndexedAccu* master_f_;
1095 DeterministicIndexedAccu* master_g_;
1096 #endif
1097 LOCKS& locks_;
1098 const GenRestrictedVoronoiDiagram& RVD_;
1099 };
1100
1101 ✗ void compute_CVT_func_grad_on_surface(double& f, double* g) override {
1102 ✗ create_threads();
1103 ✗ if(nb_parts() == 0) {
1104 ✗ if(master_ != nullptr) {
1105 #ifdef GEO_DETERMINISTIC
1106 if(has_weights_) {
1107 RVD_.for_each_triangle(
1108 ComputeCVTFuncGradWeighted<Process::SpinLockArray>(
1109 RVD_, f, g, master_f_, master_g_,
1110 master_->spinlocks_
1111 )
1112 );
1113 } else {
1114 RVD_.for_each_triangle(
1115 ComputeCVTFuncGrad<Process::SpinLockArray>(
1116 RVD_, f, g, master_f_, master_g_,
1117 master_->spinlocks_
1118 )
1119 );
1120 }
1121 #else
1122 ✗ if(has_weights_) {
1123 ✗ RVD_.for_each_triangle(
1124 ✗ ComputeCVTFuncGradWeighted<Process::SpinLockArray>(
1125 ✗ RVD_, f, g, master_->spinlocks_
1126 )
1127 );
1128 } else {
1129 ✗ RVD_.for_each_triangle(
1130 ✗ ComputeCVTFuncGrad<Process::SpinLockArray>(
1131 ✗ RVD_, f, g, master_->spinlocks_
1132 )
1133 );
1134 }
1135 #endif
1136 } else {
1137 NoLocks nolocks;
1138 #ifdef GEO_DETERMINISTIC
1139 if(has_weights_) {
1140 RVD_.for_each_triangle(
1141 ComputeCVTFuncGradWeighted<NoLocks>(
1142 RVD_, f, g, nullptr, nullptr, nolocks
1143 )
1144 );
1145 } else {
1146 RVD_.for_each_triangle(
1147 ComputeCVTFuncGrad<NoLocks>(
1148 RVD_, f, g, nullptr, nullptr, nolocks
1149 )
1150 );
1151 }
1152 #else
1153 ✗ if(has_weights_) {
1154 ✗ RVD_.for_each_triangle(
1155 ✗ ComputeCVTFuncGradWeighted<NoLocks>(
1156 ✗ RVD_, f, g, nolocks
1157 )
1158 );
1159 } else {
1160 ✗ RVD_.for_each_triangle(
1161 ✗ ComputeCVTFuncGrad<NoLocks>(
1162 ✗ RVD_, f, g, nolocks
1163 )
1164 );
1165 }
1166 #endif
1167 }
1168 } else {
1169 ✗ thread_mode_ = MT_NEWTON;
1170 ✗ arg_vectors_ = g;
1171 ✗ spinlocks_.resize(delaunay_->nb_vertices());
1172 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1173 ✗ part(t).funcval_ = 0.0;
1174 }
1175 #ifdef GEO_DETERMINISTIC
1176 std::vector<DeterministicIndexedAccu*> f_parts;
1177 std::vector<DeterministicIndexedAccu*> g_parts;
1178 for(index_t t = 0; t < nb_parts(); t++) {
1179 f_parts.push_back(deterministic_reset_part(
1180 part(t).accu_f_, part(t).master_f_
1181 ));
1182 g_parts.push_back(deterministic_reset_part(
1183 part(t).accu_g_, part(t).master_g_
1184 ));
1185 }
1186 #endif
1187 ✗ parallel_for(
1188 0, nb_parts(),
1189 ✗ [this](index_t i) { run_thread(i); }
1190 );
1191 #ifdef GEO_DETERMINISTIC
1192 f += deterministic_reduce_indexed_total(
1193 f_parts, delaunay_->nb_vertices(), det_scratch_
1194 );
1195 deterministic_reduce_indexed(
1196 g_parts, g, index_t(DIM) * delaunay_->nb_vertices(),
1197 det_scratch_
1198 );
1199 #else
1200 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1201 ✗ f += part(t).funcval_;
1202 }
1203 #endif
1204 }
1205 ✗ }
1206
1207 /********************************************************************/
1208
1209 /**
1210 * \brief Implementation class for Newton-based restricted CVT
1211 * in volume.
1212 * \details To be used as a template argument
1213 * to RVD::for_each_volumetric_integration_simplex().
1214 * This version ignores the weights.
1215 *
1216 * Computes for each RVD cell:
1217 * - g (gradient)
1218 * - f (CVT energy)
1219 * \tparam LOCKS locking policy
1220 * (can be one of Process::SpinLockArray, NoLocks)
1221 */
1222 template <class LOCKS>
1223 class ComputeCVTFuncGradVolumetric {
1224 public:
1225 /**
1226 * \brief Constructs a ComputeCentroidsFuncGradVolumetric.
1227 * \param[in] RVD the restricted Voronoi diagram
1228 * \param[out] f the computed function value
1229 * \param[out] g the computed gradient of f,
1230 * allocated by caller, and managed
1231 * by caller
1232 * \param[in] locks the array of locks
1233 * (or NoLocks in single thread mode)
1234 */
1235 ✗ ComputeCVTFuncGradVolumetric(
1236 const GenRestrictedVoronoiDiagram& RVD,
1237 double& f,
1238 double* g,
1239 #ifdef GEO_DETERMINISTIC
1240 DeterministicIndexedAccu* master_f,
1241 DeterministicIndexedAccu* master_g,
1242 #endif
1243 LOCKS& locks
1244 ) :
1245 ✗ f_(f),
1246 ✗ g_(g),
1247 #ifdef GEO_DETERMINISTIC
1248 master_f_(master_f),
1249 master_g_(master_g),
1250 #endif
1251 ✗ locks_(locks),
1252 ✗ RVD_(RVD) {
1253 ✗ }
1254
1255 /**
1256 * \brief The callback called for each integration simplex.
1257 * \param[in] v index of current center vertex
1258 * \param[in] v_adj (unused here) is the index of the Voronoi cell
1259 * adjacent to t accros facet (\p v1, \p v2, \p v3) or
1260 * NO_INDEX if it does not exists
1261 * \param[in] t (unused here) is the index of the current
1262 * tetrahedron
1263 * \param[in] t_adj (unused here) is the index of the
1264 * tetrahedron adjacent to t accros facet (\p v1, \p v2, \p v3)
1265 * or NO_INDEX if it does not exists
1266 * \param[in] p1 first vertex of current integration simplex
1267 * \param[in] p2 second vertex of current integration simplex
1268 * \param[in] p3 third vertex of current integration simplex
1269 */
1270 ✗ void operator() (
1271 index_t v,
1272 index_t v_adj,
1273 index_t t,
1274 index_t t_adj,
1275 const double* p1,
1276 const double* p2,
1277 const double* p3
1278 ) const {
1279 ✗ geo_argused(v_adj);
1280 ✗ geo_argused(t);
1281 ✗ geo_argused(t_adj);
1282 ✗ const double* p0 = RVD_.delaunay()->vertex_ptr(v);
1283
1284 ✗ double mi = Geom::tetra_volume<DIM>(p0, p1, p2, p3);
1285
1286 // fi = (mi/10)*(U.U + V.V + W.W + U.V + V.W + W.U)
1287 // where: U = p1-p0 ; V = p2-p0 and W=p3-p0
1288 ✗ double fi = 0.0;
1289 ✗ for(coord_index_t c = 0; c < DIM; ++c) {
1290 ✗ double Uc = p1[c] - p0[c];
1291 ✗ double Vc = p2[c] - p0[c];
1292 ✗ double Wc = p3[c] - p0[c];
1293 ✗ fi += geo_sqr(Uc) + geo_sqr(Vc) + geo_sqr(Wc);
1294 ✗ fi += (Uc * Vc + Vc * Wc + Wc * Uc);
1295 }
1296 ✗ fi *= (mi / 10.0);
1297
1298 #ifdef GEO_DETERMINISTIC
1299 if(master_f_ != nullptr) {
1300 master_f_->emplace_back(v, fi);
1301 // gi = 2*mi(p0 - 1/4(p0 + p1 + p2 + p3))
1302 for(coord_index_t c = 0; c < DIM; ++c) {
1303 double val = 2.0 * mi * (
1304 0.75 * p0[c]
1305 - 0.25 * p1[c] - 0.25 * p2[c] - 0.25 * p3[c]
1306 );
1307 master_g_->emplace_back(v * DIM + c, val);
1308 }
1309 return;
1310 }
1311 #endif
1312 ✗ f_ += fi;
1313
1314 // gi = 2*mi(p0 - 1/4(p0 + p1 + p2 + p3))
1315 ✗ double* g_out = g_ + v * DIM;
1316 ✗ locks_.acquire_spinlock(v);
1317 ✗ for(coord_index_t c = 0; c < DIM; ++c) {
1318 ✗ g_out[c] += 2.0 * mi * (
1319 ✗ 0.75 * p0[c]
1320 ✗ - 0.25 * p1[c] - 0.25 * p2[c] - 0.25 * p3[c]
1321 );
1322 }
1323 ✗ locks_.release_spinlock(v);
1324 ✗ }
1325
1326 double& f_;
1327 double* g_;
1328 #ifdef GEO_DETERMINISTIC
1329 DeterministicIndexedAccu* master_f_;
1330 DeterministicIndexedAccu* master_g_;
1331 #endif
1332 LOCKS& locks_;
1333 const GenRestrictedVoronoiDiagram& RVD_;
1334 };
1335
1336 ✗ void compute_CVT_func_grad_in_volume(double& f, double* g) override {
1337 ✗ create_threads();
1338 ✗ if(nb_parts() == 0) {
1339 ✗ if(master_ != nullptr) {
1340 #ifdef GEO_DETERMINISTIC
1341 RVD_.for_each_volumetric_integration_simplex(
1342 ComputeCVTFuncGradVolumetric<Process::SpinLockArray>(
1343 RVD_, f, g, master_f_, master_g_,
1344 master_->spinlocks_
1345 )
1346 );
1347 #else
1348 ✗ RVD_.for_each_volumetric_integration_simplex(
1349 ✗ ComputeCVTFuncGradVolumetric<Process::SpinLockArray>(
1350 ✗ RVD_, f, g, master_->spinlocks_
1351 )
1352 );
1353 #endif
1354 } else {
1355 NoLocks nolocks;
1356 #ifdef GEO_DETERMINISTIC
1357 RVD_.for_each_volumetric_integration_simplex(
1358 ComputeCVTFuncGradVolumetric<NoLocks>(
1359 RVD_, f, g, nullptr, nullptr, nolocks
1360 )
1361 );
1362 #else
1363 ✗ RVD_.for_each_volumetric_integration_simplex(
1364 ✗ ComputeCVTFuncGradVolumetric<NoLocks>(
1365 ✗ RVD_, f, g, nolocks
1366 )
1367 );
1368 #endif
1369 }
1370 } else {
1371 ✗ thread_mode_ = MT_NEWTON;
1372 ✗ arg_vectors_ = g;
1373 ✗ spinlocks_.resize(delaunay_->nb_vertices());
1374 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1375 ✗ part(t).funcval_ = 0.0;
1376 }
1377 #ifdef GEO_DETERMINISTIC
1378 std::vector<DeterministicIndexedAccu*> f_parts;
1379 std::vector<DeterministicIndexedAccu*> g_parts;
1380 for(index_t t = 0; t < nb_parts(); t++) {
1381 f_parts.push_back(deterministic_reset_part(
1382 part(t).accu_f_, part(t).master_f_
1383 ));
1384 g_parts.push_back(deterministic_reset_part(
1385 part(t).accu_g_, part(t).master_g_
1386 ));
1387 }
1388 #endif
1389 ✗ parallel_for(
1390 0, nb_parts(),
1391 ✗ [this](index_t i) { run_thread(i); }
1392 );
1393 #ifdef GEO_DETERMINISTIC
1394 f += deterministic_reduce_indexed_total(
1395 f_parts, delaunay_->nb_vertices(), det_scratch_
1396 );
1397 deterministic_reduce_indexed(
1398 g_parts, g, index_t(DIM) * delaunay_->nb_vertices(),
1399 det_scratch_
1400 );
1401 #else
1402 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1403 ✗ f += part(t).funcval_;
1404 }
1405 #endif
1406 }
1407 ✗ }
1408
1409 /********************************************************************/
1410
1411 /**
1412 * \brief Implementation class for computing function integrals
1413 * and gradients over integration simplices.
1414 * \details To be used as a template argument
1415 * to RVD::for_each_triangle() and
1416 * RVD::for_each_volumetric_integration_simplex()
1417 */
1418 class ComputeCVTFuncGradIntegrationSimplex {
1419 public:
1420 /**
1421 * \brief Constructs a ComputeCVTFuncGradIntegrationSimplex.
1422 * \param[in] RVD the Restricted Voronoi Diagram
1423 * \param[in] F the IntegrationSimplex
1424 */
1425 ✗ ComputeCVTFuncGradIntegrationSimplex(
1426 const GenRestrictedVoronoiDiagram& RVD,
1427 IntegrationSimplex* F
1428 ) :
1429 ✗ f_(0.0),
1430 ✗ RVD_(RVD),
1431 ✗ simplex_func_(F) {
1432 ✗ simplex_func_->reset_thread_local_storage();
1433 ✗ }
1434
1435 /**
1436 * \brief The callback called for each surfacic integration simplex.
1437 * \param[in] i index of current center vertex
1438 * \param[in] v1 first vertex of current integration simplex
1439 * \param[in] v2 second vertex of current integration simplex
1440 * \param[in] v3 third vertex of current integration simplex
1441 */
1442 ✗ void operator() (
1443 index_t i,
1444 const Vertex& v1,
1445 const Vertex& v2,
1446 const Vertex& v3
1447 ) {
1448 ✗ f_ += simplex_func_->eval(
1449 ✗ i,v1,v2,v3,RVD_.current_facet()
1450 );
1451 ✗ }
1452
1453 /**
1454 * \brief The callback called for each volumetric
1455 * integration simplex.
1456 * \param[in] v index of current center vertex
1457 * \param[in] v_adj index of the Voronoi cell adjacent to t accros
1458 * facet (\p v1, \p v2, \p v3) or NO_INDEX if it does not exists
1459 * \param[in] t index of the current tetrahedron
1460 * \param[in] t_adj index of the tetrahedron adjacent to t accros
1461 * facet (\p v1, \p v2, \p v3) or NO_INDEX if it does not exists
1462 * \param[in] v1 first vertex of current integration simplex
1463 * \param[in] v2 second vertex of current integration simplex
1464 * \param[in] v3 third vertex of current integration simplex
1465 */
1466 ✗ void operator() (
1467 index_t v,
1468 index_t v_adj,
1469 index_t t,
1470 index_t t_adj,
1471 const Vertex& v1,
1472 const Vertex& v2,
1473 const Vertex& v3
1474 ) {
1475 ✗ geo_argused(v_adj);
1476 ✗ geo_argused(t_adj);
1477 ✗ f_ += simplex_func_->eval(v,v1,v2,v3,t,t_adj,v_adj);
1478 ✗ }
1479
1480 /**
1481 * \brief Gets the function value.
1482 * \return The function value accumulated so far.
1483 */
1484 ✗ double f() const {
1485 ✗ return f_;
1486 }
1487
1488 private:
1489 double f_;
1490 const GenRestrictedVoronoiDiagram& RVD_;
1491 IntegrationSimplex* simplex_func_;
1492 };
1493
1494 ✗ void compute_integration_simplex_func_grad(
1495 double& f, double* g, IntegrationSimplex* F
1496 ) override {
1497 ✗ create_threads();
1498 ✗ if(nb_parts() == 0) {
1499 ✗ if(master_ == nullptr) {
1500 ✗ F->set_points_and_gradient(
1501 ✗ delaunay()->dimension(),
1502 delaunay()->nb_vertices(),
1503 delaunay()->vertex_ptr(0),
1504 g
1505 );
1506 }
1507 ✗ ComputeCVTFuncGradIntegrationSimplex C(RVD_,F);
1508 ✗ bool sym = RVD_.symbolic();
1509 ✗ RVD_.set_symbolic(true);
1510 ✗ if(F->volumetric()) {
1511 ✗ RVD_.for_each_volumetric_integration_simplex(
1512 C,
1513 ✗ F->background_mesh_has_varying_attribute(),
1514 false /* Coherent triangles */
1515 );
1516 } else {
1517 ✗ RVD_.for_each_triangle(C);
1518 }
1519 ✗ RVD_.set_symbolic(sym);
1520 ✗ funcval_ = C.f();
1521 ✗ f = C.f();
1522 } else {
1523 ✗ spinlocks_.resize(delaunay_->nb_vertices());
1524 ✗ F->set_points_and_gradient(
1525 ✗ delaunay()->dimension(),
1526 delaunay()->nb_vertices(),
1527 delaunay()->vertex_ptr(0),
1528 g,
1529 &spinlocks_
1530 );
1531 ✗ thread_mode_ = MT_INT_SMPLX;
1532 ✗ arg_vectors_ = g;
1533 ✗ simplex_func_ = F;
1534 ✗ funcval_ = 0.0;
1535 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1536 ✗ part(t).arg_vectors_ = g;
1537 ✗ part(t).simplex_func_ = F;
1538 ✗ part(t).funcval_ = 0.0;
1539 }
1540
1541 ✗ parallel_for(
1542 0, nb_parts(),
1543 ✗ [this](index_t i) { run_thread(i); }
1544 );
1545
1546 ✗ f = 0.0;
1547 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1548 ✗ f += part(t).funcval_;
1549 }
1550 }
1551 ✗ }
1552
1553 /********************************************************************/
1554
1555 /**
1556 * \brief Adapter class used internally to implement for_each_polygon()
1557 * \details Gets the current triangle from the RVD and passes it back
1558 * to the callback. It is needed because GenericRVD::for_each_polygon()
1559 * does not pass the current triangle.
1560 */
1561 // TODO: pass it through all the callbacks, because it is ridiculous:
1562 // we pass it through the first levels, then throw it, then retrieve it
1563 // (see GenRVD)
1564 class PolygonCallbackAction {
1565 public:
1566 /**
1567 * \brief PolygonCallbackAction constructor
1568 * \param[in] RVD a pointer to the restricted Voronoi diagram
1569 * \param[in] callback a pointer to the PolygonCallback
1570 */
1571 ✗ PolygonCallbackAction(
1572 GenRestrictedVoronoiDiagram& RVD,
1573 GEO::RVDPolygonCallback& callback
1574 ) :
1575 ✗ RVD_(RVD),
1576 ✗ callback_(callback) {
1577 ✗ }
1578
1579 /**
1580 * \brief Callback called for each polygon.
1581 * \details Routes the callback to the wrapped user action class.
1582 * \param[in] v index of current Delaunay seed
1583 * \param[in] P intersection between current mesh facet
1584 * and the Voronoi cell of \p v
1585 */
1586 ✗ void operator() (
1587 index_t v,
1588 const GEOGen::Polygon& P
1589 ) const {
1590 ✗ callback_(v, RVD_.current_facet(), P);
1591 ✗ }
1592
1593 protected:
1594 GenRestrictedVoronoiDiagram& RVD_;
1595 GEO::RVDPolygonCallback& callback_;
1596 };
1597
1598
1599 ✗ virtual void compute_with_polygon_callback(
1600 GEO::RVDPolygonCallback& polygon_callback
1601 ) {
1602 ✗ create_threads();
1603 ✗ if(nb_parts() == 0) {
1604 ✗ PolygonCallbackAction action(RVD_,polygon_callback);
1605 ✗ RVD_.for_each_polygon(action);
1606 } else {
1607 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1608 ✗ part(t).RVD_.set_symbolic(RVD_.symbolic());
1609 ✗ part(t).RVD_.set_connected_components_priority(
1610 ✗ RVD_.connected_components_priority()
1611 );
1612 }
1613 ✗ spinlocks_.resize(delaunay_->nb_vertices());
1614 ✗ thread_mode_ = MT_POLYG;
1615 ✗ polygon_callback_ = &polygon_callback;
1616 ✗ polygon_callback_->set_spinlocks(&spinlocks_);
1617 // Note: callback begin()/end() is called in for_each_polygon()
1618 ✗ parallel_for(
1619 0, nb_parts(),
1620 ✗ [this](index_t i) { run_thread(i); }
1621 );
1622 ✗ polygon_callback_->set_spinlocks(nullptr);
1623 }
1624 ✗ }
1625
1626 ✗ virtual void compute_with_polyhedron_callback(
1627 GEO::RVDPolyhedronCallback& polyhedron_callback
1628 ) {
1629 ✗ create_threads();
1630 ✗ if(nb_parts() == 0) {
1631 ✗ RVD_.for_each_polyhedron(polyhedron_callback);
1632 } else {
1633 ✗ for(index_t t = 0; t < nb_parts(); t++) {
1634 ✗ part(t).RVD_.set_symbolic(RVD_.symbolic());
1635 ✗ part(t).RVD_.set_connected_components_priority(
1636 ✗ RVD_.connected_components_priority()
1637 );
1638 }
1639 ✗ spinlocks_.resize(delaunay_->nb_vertices());
1640 ✗ thread_mode_ = MT_POLYH;
1641 ✗ polyhedron_callback_ = &polyhedron_callback;
1642 ✗ polyhedron_callback_->set_spinlocks(&spinlocks_);
1643 // Note: callback begin()/end() is
1644 // called in for_each_polyhedron()
1645 ✗ parallel_for(
1646 0, nb_parts(),
1647 ✗ [this](index_t i) { run_thread(i); }
1648 );
1649 ✗ polyhedron_callback_->set_spinlocks(nullptr);
1650 }
1651 ✗ }
1652
1653 /********************************************************************/
1654
1655 /**
1656 * \brief Implementation class for explicitly constructing
1657 * a surfacic mesh that corresponds to the surfacic
1658 * restricted Voronoi diagram.
1659 * \details To be used as a template argument
1660 * to RVD::for_each_polygon(). The current Vornoi cell is
1661 * reported in facet region.
1662 * \tparam BUILDER a class that implements iterative mesh building,
1663 * e.g., MeshBuilder.
1664 */
1665 template <class BUILDER>
1666 class BuildRVD {
1667 public:
1668 /**
1669 * \brief Constructs a new BuildRVD.
1670 * \param[in] RVD_in the restricted Voronoi diagram
1671 * \param[in] builder the lesh builder
1672 */
1673 72 BuildRVD(
1674 const GenRestrictedVoronoiDiagram& RVD_in,
1675 BUILDER& builder
1676 ) :
1677 72 RVD(RVD_in),
1678 72 builder_(builder),
1679 72 current_facet_(NO_INDEX) {
1680 72 builder_.begin_surface();
1681 72 }
1682
1683 /**
1684 * \brief The destructor
1685 * \details Terminates the current facet
1686 * and the current surface.
1687 */
1688 72 ~BuildRVD() {
1689
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
72 if(current_facet_ != NO_INDEX) {
1690 72 builder_.end_reference_facet();
1691 }
1692 72 builder_.end_surface();
1693 72 }
1694
1695 /**
1696 * \brief The callback called for each restricted Voronoi cell.
1697 * \param[in] v index of current center vertex
1698 * \param[in] P current restricted Voronoi cell
1699 */
1700 420818 void operator() (
1701 index_t v,
1702 const typename GenRestrictedVoronoiDiagram::Polygon& P
1703 ) {
1704 420818 index_t f = RVD.current_facet();
1705
2/2
✓ Branch 0 taken 60502 times.
✓ Branch 1 taken 149907 times.
420818 if(f != current_facet_) {
1706
2/2
✓ Branch 0 taken 60466 times.
✓ Branch 1 taken 36 times.
121004 if(current_facet_ != NO_INDEX) {
1707 120932 builder_.end_reference_facet();
1708 }
1709 121004 current_facet_ = f;
1710 121004 builder_.begin_reference_facet(f);
1711 }
1712 420818 builder_.begin_facet(v);
1713
2/2
✓ Branch 1 taken 841011 times.
✓ Branch 2 taken 210409 times.
2102840 for(index_t i = 0; i < P.nb_vertices(); i++) {
1714 1682022 const Vertex& ve = P.vertex(i);
1715 1682022 builder_.add_vertex_to_facet(ve.point(), ve.sym());
1716 }
1717 420818 builder_.end_facet();
1718 420818 }
1719
1720 private:
1721 const GenRestrictedVoronoiDiagram& RVD;
1722 BUILDER& builder_;
1723 index_t current_facet_;
1724 };
1725
1726 /**
1727 * \brief Implementation class for explicitly constructing
1728 * a volumetric mesh that corresponds to the volumetric
1729 * restricted Voronoi diagram.
1730 * \details To be used as a template argument
1731 * to RVD::for_each_volumetric_integration_simplex().
1732 * The current Voronoi cell is reported in tetrahedron region.
1733 * \note For the moment, vertices are duplicated (will be fixed
1734 * in a future version).
1735 */
1736 class BuildVolumetricRVD {
1737 public:
1738 /**
1739 * Constructs a new BuildVolumetricRVD.
1740 * \param[in] RVD the volumetric restricted Voronoi diagram
1741 * \param[in] dim dimension of the points (can be smaller
1742 * than actual dimension of the RVD).
1743 * \param[out] vertices coordinates of the generated vertices
1744 * \param[out] triangle_vertex_indices generated triangles, as
1745 * vertex indices triplets
1746 * \param[out] tet_vertex_indices generated tetrahedra, as
1747 * vertex indices 4-uples
1748 * \param[out] triangle_regions each generated triangle has
1749 * a region index, that corresponds to the index of the
1750 * Voronoi cell the triangle belongs to
1751 * \param[out] tet_regions each generated tetrahedron has
1752 * a region index, that corresponds to the index of the
1753 * Voronoi cell the tetrahedron belongs to
1754 * \param[in] cell_borders_only if true, only the surfacic
1755 * borders of the volumetric cells are saved in the mesh, else
1756 * volumetric cells are tetrahedralized.
1757 * \pre dim <= delaunay->dimension()
1758 */
1759 ✗ BuildVolumetricRVD(
1760 GenRestrictedVoronoiDiagram& RVD,
1761 coord_index_t dim,
1762 vector<double>& vertices,
1763 vector<index_t>& triangle_vertex_indices,
1764 vector<index_t>& tet_vertex_indices,
1765 vector<index_t>& triangle_regions,
1766 vector<index_t>& tet_regions,
1767 bool cell_borders_only
1768 ) :
1769 ✗ delaunay_(RVD.delaunay()),
1770 ✗ mesh_(RVD.mesh()),
1771 ✗ dim_(dim),
1772 ✗ vertices_(vertices),
1773 ✗ triangle_vertex_indices_(triangle_vertex_indices),
1774 ✗ tet_vertex_indices_(tet_vertex_indices),
1775 ✗ triangle_regions_(triangle_regions),
1776 ✗ tet_regions_(tet_regions),
1777 ✗ cell_borders_only_(cell_borders_only)
1778 {
1779 ✗ vertices_.clear();
1780 ✗ triangle_vertex_indices_.clear();
1781 ✗ tet_vertex_indices_.clear();
1782 ✗ triangle_regions_.clear();
1783 ✗ tet_regions_.clear();
1784
1785 // The first vertices are copied from Delaunay,
1786 // the other ones will be created during the traversal
1787 ✗ nb_vertices_ = delaunay_->nb_vertices();
1788 ✗ vertices_.resize(nb_vertices_ * dim);
1789 ✗ for(index_t v = 0; v < delaunay_->nb_vertices(); ++v) {
1790 ✗ for(coord_index_t c = 0; c < dim; ++c) {
1791 ✗ vertices_[v * dim + c] = delaunay_->vertex_ptr(v)[c];
1792 }
1793 }
1794 ✗ vertex_map_.set_first_vertex_index(nb_vertices_);
1795 ✗ }
1796
1797 /**
1798 * \brief The callback called for each integration simplex.
1799 * \param[in] v index of current center vertex
1800 * \param[in] v_adj (unused here) is the index of the Voronoi cell
1801 * adjacent to t accros facet (\p v1, \p v2, \p v3) or
1802 * NO_INDEX if it does not exists
1803 * \param[in] t (unused here) is the index of the current
1804 * tetrahedron
1805 * \param[in] t_adj (unused here) is the index of the
1806 * tetrahedron adjacent to t accros facet (\p v1, \p v2, \p v3)
1807 * or NO_INDEX if it does not exists
1808 * \param[in] v1 first vertex of current integration simplex
1809 * \param[in] v2 second vertex of current integration simplex
1810 * \param[in] v3 third vertex of current integration simplex
1811 */
1812 ✗ void operator() (
1813 index_t v, index_t v_adj,
1814 index_t t, index_t t_adj,
1815 const Vertex& v1, const Vertex& v2, const Vertex& v3
1816 ) {
1817 ✗ geo_argused(v_adj);
1818 ✗ geo_argused(t);
1819
1820 ✗ if(cell_borders_only_) {
1821 ✗ if(v > v_adj) {
1822 ✗ index_t iv2 = find_or_create_vertex(v, v1);
1823 ✗ index_t iv3 = find_or_create_vertex(v, v2);
1824 ✗ index_t iv4 = find_or_create_vertex(v, v3);
1825 ✗ triangle_vertex_indices_.push_back(iv4);
1826 ✗ triangle_vertex_indices_.push_back(iv3);
1827 ✗ triangle_vertex_indices_.push_back(iv2);
1828 ✗ triangle_regions_.push_back(v);
1829 }
1830 } else {
1831 ✗ index_t iv1 = v;
1832 ✗ index_t iv2 = find_or_create_vertex(v, v1);
1833 ✗ index_t iv3 = find_or_create_vertex(v, v2);
1834 ✗ index_t iv4 = find_or_create_vertex(v, v3);
1835
1836 // Triangle v1,v2,v3 is on border if there is
1837 // no adjacent seed and no adjacent tet.
1838 ✗ if(v_adj == NO_INDEX && t_adj == NO_INDEX) {
1839 ✗ triangle_vertex_indices_.push_back(iv4);
1840 ✗ triangle_vertex_indices_.push_back(iv3);
1841 ✗ triangle_vertex_indices_.push_back(iv2);
1842 ✗ triangle_regions_.push_back(v);
1843 }
1844
1845 ✗ tet_vertex_indices_.push_back(iv1);
1846 ✗ tet_vertex_indices_.push_back(iv2);
1847 ✗ tet_vertex_indices_.push_back(iv3);
1848 ✗ tet_vertex_indices_.push_back(iv4);
1849 ✗ tet_regions_.push_back(v);
1850 }
1851 ✗ }
1852
1853 /**
1854 * \brief The callback called for each tetrahedron
1855 * \param[in] v index of current center vertex
1856 * \param[in] v_adj (unused here) is the index of the Voronoi cell
1857 * adjacent to t accros facet (\p v1, \p v2, \p v3) or
1858 * NO_INDEX if it does not exists
1859 * \param[in] t (unused here) is the index of the current
1860 * tetrahedron
1861 * \param[in] t_adj (unused here) is the index of the
1862 * tetrahedron adjacent to t accros facet (\p v1, \p v2, \p v3)
1863 * or NO_INDEX if it does not exists
1864 * \param[in] v1 first vertex of current tetrahedron
1865 * \param[in] v2 second vertex of current tetrahedron
1866 * \param[in] v3 third vertex of current tetrahedron
1867 * \param[in] v4 fourth vertex of current tetrahedron
1868 */
1869 ✗ void operator() (
1870 index_t v, index_t v_adj,
1871 index_t t, index_t t_adj,
1872 const Vertex& v1, const Vertex& v2,
1873 const Vertex& v3, const Vertex& v4
1874 ) {
1875 ✗ geo_argused(v_adj);
1876 ✗ geo_argused(t);
1877 ✗ geo_argused(t_adj);
1878 ✗ index_t iv1 = vertices_.size() / dim_;
1879 ✗ for(index_t c = 0; c < dim_; ++c) {
1880 ✗ vertices_.push_back(v1.point()[c]);
1881 }
1882 ✗ index_t iv2 = vertices_.size() / dim_;
1883 ✗ for(index_t c = 0; c < dim_; ++c) {
1884 ✗ vertices_.push_back(v2.point()[c]);
1885 }
1886 ✗ index_t iv3 = vertices_.size() / dim_;
1887 ✗ for(index_t c = 0; c < dim_; ++c) {
1888 ✗ vertices_.push_back(v3.point()[c]);
1889 }
1890 ✗ index_t iv4 = vertices_.size() / dim_;
1891 ✗ for(index_t c = 0; c < dim_; ++c) {
1892 ✗ vertices_.push_back(v4.point()[c]);
1893 }
1894 ✗ tet_vertex_indices_.push_back(iv1);
1895 ✗ tet_vertex_indices_.push_back(iv2);
1896 ✗ tet_vertex_indices_.push_back(iv3);
1897 ✗ tet_vertex_indices_.push_back(iv4);
1898 ✗ tet_regions_.push_back(v);
1899 ✗ }
1900
1901 protected:
1902 /**
1903 * \brief Retrieves the index of a vertex given its symbolic
1904 * representation.
1905 * \param[in] center_vertex_id index of current Voronoi seed
1906 * \param[in] v symbolic and geometric representation of the vertex
1907 * \return the index of the vertex
1908 */
1909 ✗ index_t find_or_create_vertex(
1910 index_t center_vertex_id, const Vertex& v
1911 ) {
1912 ✗ index_t result = vertex_map_.find_or_create_vertex(
1913 ✗ center_vertex_id, v.sym()
1914 );
1915 ✗ if(result >= nb_vertices_) {
1916 ✗ geo_assert(result == nb_vertices_);
1917 ✗ nb_vertices_ = result + 1;
1918 ✗ for(coord_index_t c = 0; c < dim_; ++c) {
1919 ✗ vertices_.push_back(v.point()[c]);
1920 }
1921 }
1922 ✗ return result;
1923 }
1924
1925 private:
1926 const Delaunay* delaunay_;
1927 const Mesh* mesh_;
1928 coord_index_t dim_;
1929 vector<double>& vertices_;
1930 vector<index_t>& triangle_vertex_indices_;
1931 vector<index_t>& tet_vertex_indices_;
1932 vector<index_t>& triangle_regions_;
1933 vector<index_t>& tet_regions_;
1934 RVDVertexMap vertex_map_;
1935 index_t nb_vertices_;
1936 bool cell_borders_only_;
1937 };
1938
1939 72 void compute_RVD(
1940 Mesh& M, coord_index_t dim, bool cell_borders_only,
1941 bool integration_simplices
1942 ) override {
1943 72 bool sym = RVD_.symbolic();
1944 72 RVD_.set_symbolic(true);
1945
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
72 if(volumetric_) {
1946 ✗ if(dim == 0) {
1947 ✗ dim = dimension();
1948 }
1949 ✗ vector<double> vertices;
1950 ✗ vector<index_t> triangle_vertices;
1951 ✗ vector<index_t> tet_vertices;
1952 ✗ vector<index_t> triangle_regions;
1953 ✗ vector<index_t> tet_regions;
1954 ✗ if(cell_borders_only) {
1955 ✗ RVD_.for_each_volumetric_integration_simplex(
1956 ✗ BuildVolumetricRVD(
1957 ✗ RVD_, dim,
1958 vertices,
1959 triangle_vertices,
1960 tet_vertices,
1961 triangle_regions,
1962 tet_regions,
1963 cell_borders_only
1964 ),
1965 false, // Do not visit inner tetrahedra.
1966 true // Ensure that polygonal facets are triangulated
1967 // coherently.
1968 );
1969 } else {
1970 ✗ if(integration_simplices) {
1971 ✗ RVD_.for_each_volumetric_integration_simplex(
1972 ✗ BuildVolumetricRVD(
1973 ✗ RVD_, dim,
1974 vertices,
1975 triangle_vertices,
1976 tet_vertices,
1977 triangle_regions,
1978 tet_regions,
1979 cell_borders_only
1980 ),
1981 false, // Do not visit inner tetrahedra.
1982 true // Ensure that polygonal facets are
1983 // triangulated coherently.
1984 );
1985 } else {
1986 ✗ RVD_.for_each_tetrahedron(
1987 ✗ BuildVolumetricRVD(
1988 ✗ RVD_, dim,
1989 vertices,
1990 triangle_vertices,
1991 tet_vertices,
1992 triangle_regions,
1993 tet_regions,
1994 cell_borders_only
1995 )
1996 );
1997 }
1998 }
1999
2000 ✗ M.clear(true); // keep attributes
2001
2002 ✗ M.vertices.assign_points(vertices,dim,true);
2003 ✗ M.facets.assign_triangle_mesh(triangle_vertices, true);
2004 ✗ M.cells.assign_tet_mesh(tet_vertices, true);
2005
2006 // TODO: use Attribute::assign(vector, steal_args)
2007 // when it is there...
2008
2009 ✗ if(M.facets.nb() != 0) {
2010 ✗ Attribute<index_t> facet_region_attr(
2011 ✗ M.facets.attributes(), "region"
2012 );
2013 ✗ for(index_t f=0; f<M.facets.nb(); ++f) {
2014 ✗ facet_region_attr[f] = triangle_regions[f];
2015 }
2016 ✗ }
2017
2018 ✗ if(M.cells.nb() != 0) {
2019 ✗ Attribute<index_t> cell_region_attr(
2020 ✗ M.cells.attributes(), "region"
2021 );
2022 ✗ for(index_t c=0; c<M.cells.nb(); ++c) {
2023 ✗ cell_region_attr[c] = tet_regions[c];
2024 }
2025 ✗ }
2026
2027 ✗ if(cell_borders_only) {
2028 ✗ mesh_repair(M, MESH_REPAIR_TOPOLOGY);
2029 } else {
2030 ✗ M.facets.connect();
2031 }
2032 ✗ } else {
2033
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
72 RVDMeshBuilder builder(
2034 &M, mesh_, delaunay_
2035 );
2036
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 36 times.
72 if(dim != 0) {
2037 ✗ builder.set_dimension(dim);
2038 }
2039
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
72 RVD_.for_each_polygon(
2040
1/2
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
144 BuildRVD<RVDMeshBuilder>(RVD_, builder)
2041 );
2042 72 }
2043 72 RVD_.set_symbolic(sym);
2044
2/4
✓ Branch 1 taken 36 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36 times.
✗ Branch 5 not taken.
72 M.show_stats("RVD");
2045 72 }
2046
2047 /********************************************************************/
2048
2049 ✗ void compute_RVC(
2050 index_t i,
2051 Mesh& M,
2052 Mesh& result,
2053 bool copy_symbolic_info
2054 ) override {
2055 ✗ Mesh* tmp_mesh = mesh_;
2056 ✗ mesh_ = &M;
2057 ✗ RVD_.set_mesh(&M);
2058 ✗ typename GenRestrictedVoronoiDiagram::Polyhedron Cell(dimension());
2059 ✗ Cell.initialize_from_surface_mesh(&M, RVD_.symbolic());
2060 ✗ RVD_.intersect_cell_cell(i, Cell);
2061 ✗ Cell.convert_to_mesh(&result, copy_symbolic_info);
2062 ✗ mesh_ = tmp_mesh;
2063 ✗ RVD_.set_mesh(tmp_mesh);
2064 ✗ }
2065
2066 /********************************************************************/
2067
2068 ✗ void for_each_polygon(
2069 GEO::RVDPolygonCallback& callback,
2070 bool symbolic,
2071 bool connected_comp_priority,
2072 bool parallel
2073 ) override {
2074 ✗ bool sym_backup = RVD_.symbolic();
2075 ✗ RVD_.set_symbolic(symbolic);
2076 ✗ RVD_.set_connected_components_priority(connected_comp_priority);
2077 ✗ callback.begin();
2078 ✗ if(parallel) {
2079 ✗ compute_with_polygon_callback(callback);
2080 } else {
2081 ✗ PolygonCallbackAction action(RVD_,callback);
2082 ✗ RVD_.for_each_polygon(action);
2083 }
2084 ✗ callback.end();
2085 ✗ RVD_.set_symbolic(sym_backup);
2086 ✗ RVD_.set_connected_components_priority(false);
2087 ✗ }
2088
2089 /********************************************************************/
2090
2091 8 void for_each_polyhedron(
2092 GEO::RVDPolyhedronCallback& callback,
2093 bool symbolic,
2094 bool connected_comp_priority,
2095 bool parallel
2096 ) override {
2097 8 bool sym_backup = RVD_.symbolic();
2098 8 RVD_.set_symbolic(symbolic);
2099 8 RVD_.set_connected_components_priority(connected_comp_priority);
2100 8 callback.set_dimension(RVD_.mesh()->vertices.dimension());
2101 8 callback.begin();
2102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
8 if(parallel) {
2103 ✗ compute_with_polyhedron_callback(callback);
2104 } else {
2105 8 RVD_.for_each_polyhedron(callback);
2106 }
2107 8 callback.end();
2108 8 RVD_.set_symbolic(sym_backup);
2109 8 RVD_.set_connected_components_priority(false);
2110 8 }
2111
2112 /********************************************************************/
2113
2114 /**
2115 * \brief Does the actual computation for a specific part
2116 * in multithread mode.
2117 * \param[in] t the index of the part.
2118 * \pre \p t < nb_parts()
2119 */
2120 3640 void run_thread(index_t t) {
2121
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 1820 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
3640 geo_assert(t < nb_parts());
2122 3640 thisclass& T = part(t);
2123
1/7
✓ Branch 0 taken 1820 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
3640 switch(thread_mode_) {
2124 3640 case MT_LLOYD:
2125 {
2126 3640 T.compute_centroids(arg_vectors_, arg_scalars_);
2127 3640 } break;
2128 ✗ case MT_NEWTON:
2129 {
2130 ✗ T.compute_CVT_func_grad(T.funcval_, arg_vectors_);
2131 ✗ } break;
2132 ✗ case MT_INT_SMPLX:
2133 {
2134 ✗ T.compute_integration_simplex_func_grad(
2135 ✗ T.funcval_, arg_vectors_, simplex_func_
2136 );
2137 ✗ } break;
2138 ✗ case MT_POLYG:
2139 {
2140 ✗ T.compute_with_polygon_callback(
2141 ✗ *polygon_callback_
2142 );
2143 ✗ } break;
2144 ✗ case MT_POLYH:
2145 {
2146 ✗ T.compute_with_polyhedron_callback(
2147 ✗ *polyhedron_callback_
2148 );
2149 ✗ } break;
2150 ✗ case MT_NONE:
2151 ✗ geo_assert_not_reached;
2152 }
2153 3640 }
2154
2155 16 bool compute_initial_sampling_on_surface(
2156 double* p, index_t nb_points, bool verbose
2157 ) override {
2158
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
16 geo_assert(mesh_->facets.are_simplices());
2159
2160 // We do that here, since this triggers partitioning,
2161 // that improves data locality. Then data locality is
2162 // inherited by the generated points.
2163 16 create_threads();
2164
2165
4/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
16 if(verbose && facets_begin_ == NO_INDEX && facets_end_ == NO_INDEX) {
2166
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
42 Logger::out("RVD")
2167
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
14 << "Computing initial sampling on surface, using dimension="
2168
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
14 << index_t(dimension_) << std::endl;
2169 }
2170
2171 32 return mesh_generate_random_samples_on_surface<DIM>(
2172 16 *mesh_, p, nb_points, vertex_weight_, facets_begin_, facets_end_
2173 16 );
2174 }
2175
2176 8 bool compute_initial_sampling_in_volume(
2177 double* p, index_t nb_points, bool verbose
2178 ) override {
2179
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
8 geo_assert(mesh_->cells.nb() != 0);
2180
2181 // We do that here, since this triggers partitioning,
2182 // that improves data locality. Then data locality is
2183 // inherited by the generated points.
2184 8 create_threads();
2185
2186
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
8 if(verbose && tets_begin_ == NO_INDEX && tets_end_ == NO_INDEX) {
2187 ✗ Logger::out("RVD")
2188 ✗ << "Computing initial sampling in volume, using dimension="
2189 ✗ << index_t(dimension_) << std::endl;
2190 }
2191
2192 16 return mesh_generate_random_samples_in_volume<DIM>(
2193 8 *mesh_, p, nb_points, vertex_weight_, tets_begin_, tets_end_
2194 8 );
2195 }
2196
2197 /**
2198 * \brief Creates the data structures for fast projection.
2199 * \details It decomposes the surface into triangles, and
2200 * stores the vertices in a KdTree.
2201 */
2202 ✗ void prepare_projection() {
2203 ✗ if(!mesh_vertices_.is_null()) {
2204 ✗ return;
2205 }
2206
2207 // Step 1: get triangles
2208 ✗ for(index_t f = 0; f < mesh_->facets.nb(); f++) {
2209 ✗ index_t i = mesh_->facets.corners_begin(f);
2210 ✗ for(
2211 ✗ index_t j = i + 1;
2212 ✗ j + 1 < mesh_->facets.corners_end(f); j++
2213 ) {
2214 ✗ triangles_.push_back(mesh_->facet_corners.vertex(i));
2215 ✗ triangles_.push_back(mesh_->facet_corners.vertex(j));
2216 ✗ triangles_.push_back(mesh_->facet_corners.vertex(j + 1));
2217 }
2218 }
2219 ✗ nb_triangles_ = index_t(triangles_.size() / 3);
2220
2221 // Step 2: get vertices stars
2222 // Step 2.1: get one-ring neighborhood
2223 ✗ vector<vector<index_t> > stars2(mesh_->vertices.nb());
2224 ✗ for(index_t t = 0; t < nb_triangles_; t++) {
2225 ✗ stars2[triangles_[3 * t]].push_back(t);
2226 ✗ stars2[triangles_[3 * t + 1]].push_back(t);
2227 ✗ stars2[triangles_[3 * t + 2]].push_back(t);
2228 }
2229
2230 // Step 2.2: get two-ring neighborhood
2231 ✗ stars_.resize(mesh_->vertices.nb());
2232 ✗ for(index_t i = 0; i < stars2.size(); i++) {
2233 ✗ vector<index_t> Ni;
2234 ✗ for(index_t j = 0; j < stars2[i].size(); j++) {
2235 ✗ index_t t = stars2[i][j];
2236 ✗ for(index_t iv = 0; iv < 3; iv++) {
2237 ✗ index_t v = triangles_[3 * t + iv];
2238 ✗ if(v != i) {
2239 ✗ Ni.push_back(v);
2240 }
2241 }
2242 }
2243 ✗ sort_unique(Ni);
2244 ✗ for(index_t j = 0; j < Ni.size(); j++) {
2245 ✗ index_t k = Ni[j];
2246 ✗ stars_[i].insert(
2247 ✗ stars_[i].end(), stars2[k].begin(), stars2[k].end()
2248 );
2249 }
2250 ✗ sort_unique(stars_[i]);
2251 }
2252
2253 // Step 3: create search structure
2254 ✗ mesh_vertices_ = Delaunay::create(dimension_, "NN");
2255 ✗ index_t nb_vertices = mesh_->vertices.nb();
2256
2257 // TODO: BUG !! mesh_vertices_ keeps a ref. to mesh_vertices
2258 // that is destroyed when leaving this function.
2259 ✗ vector<double> mesh_vertices(nb_vertices * dimension_);
2260 ✗ for(index_t i = 0; i < nb_vertices; i++) {
2261 ✗ for(index_t coord = 0; coord < dimension_; coord++) {
2262 ✗ mesh_vertices[i * dimension_ + coord] =
2263 ✗ mesh_->vertices.point_ptr(i)[coord];
2264 }
2265 }
2266 ✗ mesh_vertices_->set_vertices(nb_vertices, mesh_vertices.data());
2267 ✗ }
2268
2269 ✗ void project_points_on_surface(
2270 index_t nb_points, double* points, vec3* nearest, bool do_project
2271 ) override {
2272
2273 ✗ prepare_projection();
2274
2275 ✗ if(use_exact_projection_) {
2276 ✗ for(index_t p = 0; p < nb_points; p++) {
2277 ✗ Point P(points + p * dimension_);
2278 ✗ double d2 = Numeric::max_float64();
2279 ✗ for(index_t t = 0; t < nb_triangles_; t++) {
2280 ✗ const Point& p1 = mesh_vertex(triangles_[3 * t]);
2281 ✗ const Point& p2 = mesh_vertex(triangles_[3 * t + 1]);
2282 ✗ const Point& p3 = mesh_vertex(triangles_[3 * t + 2]);
2283
2284 double l1, l2, l3;
2285 ✗ Point nearestP;
2286
2287 ✗ double cur_d2 = Geom::point_triangle_squared_distance(
2288 P, p1, p2, p3, nearestP, l1, l2, l3
2289 );
2290
2291 ✗ if(cur_d2 < d2) {
2292 ✗ d2 = cur_d2;
2293 const vec3& p1_R3 =
2294 ✗ R3_embedding(triangles_[3 * t]);
2295 const vec3& p2_R3 =
2296 ✗ R3_embedding(triangles_[3 * t + 1]);
2297 const vec3& p3_R3 =
2298 ✗ R3_embedding(triangles_[3 * t + 2]);
2299 ✗ nearest[p] = l1 * p1_R3 + l2 * p2_R3 + l3 * p3_R3;
2300 ✗ if(do_project) {
2301 ✗ for(coord_index_t
2302 ✗ coord = 0; coord < dimension_; coord++) {
2303 ✗ (points + p * dimension_)[coord] =
2304 ✗ nearestP[coord];
2305 }
2306 }
2307 }
2308 }
2309 }
2310 ✗ return;
2311 }
2312
2313 // find nearest point on surface in star of nearest vertex
2314 ✗ for(index_t p = 0; p < nb_points; p++) {
2315 ✗ Point P(points + p * dimension_);
2316 ✗ index_t v = mesh_vertices_->nearest_vertex(
2317 ✗ points + p * dimension_
2318 );
2319 ✗ double d2 = Numeric::max_float64();
2320 ✗ nearest[p] = R3_embedding(v);
2321 ✗ for(index_t i = 0; i < stars_[v].size(); i++) {
2322 ✗ index_t t = stars_[v][i];
2323 ✗ const Point& p1 = mesh_vertex(triangles_[3 * t]);
2324 ✗ const Point& p2 = mesh_vertex(triangles_[3 * t + 1]);
2325 ✗ const Point& p3 = mesh_vertex(triangles_[3 * t + 2]);
2326
2327 double l1, l2, l3;
2328 ✗ Point nearestP;
2329 ✗ double cur_d2 = Geom::point_triangle_squared_distance(
2330 P, p1, p2, p3, nearestP, l1, l2, l3
2331 );
2332 ✗ if(cur_d2 < d2) {
2333 ✗ d2 = cur_d2;
2334 ✗ const vec3& p1_R3 = R3_embedding(triangles_[3 * t]);
2335 ✗ const vec3& p2_R3 = R3_embedding(triangles_[3 * t + 1]);
2336 ✗ const vec3& p3_R3 = R3_embedding(triangles_[3 * t + 2]);
2337 ✗ nearest[p] = l1 * p1_R3 + l2 * p2_R3 + l3 * p3_R3;
2338 ✗ if(do_project) {
2339 ✗ for(coord_index_t coord = 0;
2340 ✗ coord < dimension_; coord++
2341 ) {
2342 ✗ (points + p * dimension_)[coord] =
2343 ✗ nearestP[coord];
2344 }
2345 }
2346 }
2347 }
2348 }
2349 }
2350
2351 /********************************************************************/
2352
2353 /**
2354 * \brief Implementation class for computing the restricted Delaunay
2355 * triangulation.
2356 * \details To be used as a template argument
2357 * to RVD::for_each_primal_triangle().
2358 */
2359 class GetPrimalTriangles {
2360 public:
2361 /**
2362 * \brief Creates a new GetPrimalTriangles.
2363 * \param[out] triangles where to store the triangles
2364 */
2365 ✗ GetPrimalTriangles(
2366 vector<index_t>& triangles
2367 ) :
2368 ✗ triangles_(triangles) {
2369 ✗ }
2370
2371 /**
2372 * \brief The callback called for each primal triangle.
2373 * \param[in] v1 index of the first vertex
2374 * \param[in] v2 index of the second vertex
2375 * \param[in] v3 index of the third vertex
2376 */
2377 ✗ void operator() (index_t v1, index_t v2, index_t v3) {
2378 ✗ triangles_.push_back(v1);
2379 ✗ triangles_.push_back(v2);
2380 ✗ triangles_.push_back(v3);
2381 ✗ }
2382
2383 private:
2384 vector<index_t>& triangles_;
2385 };
2386
2387 /**
2388 * \brief Implementation class for computing the restricted Delaunay
2389 * triangulation of the connected components.
2390 *
2391 * \details The difference with GetPrimalTriangles is that when a
2392 * restricted Voronoi cell has multiple connected components,
2393 * more triangles are generated to account for the topology.
2394 * To be used as a template argument to RVD::for_each_polygon().
2395 * The RestrictedVoronoiDiagram needs to be in connected-components
2396 * priority mode.
2397 */
2398 class GetConnectedComponentsPrimalTriangles {
2399 public:
2400 /** Internal representation of the polygons. */
2401 typedef typename GenRestrictedVoronoiDiagram::Polygon Polygon;
2402
2403 /** Internal representation of the vertices. */
2404 typedef typename GenRestrictedVoronoiDiagram::Vertex Vertex;
2405
2406 static constexpr index_t UNINITIALIZED = index_t(-1);
2407 static constexpr index_t MULTI_COMP = index_t(-2);
2408 static constexpr index_t ON_BORDER = index_t(-3);
2409
2410 /**
2411 * \brief Constructs a new GetConnectedComponentsPrimalTriangles.
2412 * \param[in] RVD the restricted Voronoi diagram
2413 * \param[out] triangles where to store the triangles
2414 * \param[out] vertices where to store the vertices
2415 * \param[in] dimension dimension of the restricted Voronoi diagram
2416 * \param[in] mode a combination of constants defined in RDTMode
2417 * \param[in] seed_is_locked specifies for each seed whether it
2418 * can be moved (to RVC centroid or projected on surface). If
2419 * left uninitialized, all the seeds can be moved.
2420 * \param[in] AABB an axis-aligned bounding box tree defined on
2421 * the input surface. It is used if one of (RDT_SELECT_NEAREST,
2422 * RDT_PROJECT_ON_SURFACE) is set in \p mode. If needed and not
2423 * specified, then a new one is created locally.
2424 */
2425 14 GetConnectedComponentsPrimalTriangles(
2426 const GenRestrictedVoronoiDiagram& RVD,
2427 vector<index_t>& triangles,
2428 vector<double>& vertices,
2429 coord_index_t dimension,
2430 RDTMode mode,
2431 const std::vector<bool>& seed_is_locked,
2432 MeshFacetsAABB* AABB = nullptr
2433 ) :
2434 14 RVD_(RVD),
2435 14 dimension_(dimension),
2436 14 triangles_(triangles),
2437 14 vertices_(vertices),
2438 14 m_(0.0),
2439 14 cur_seed_(NO_INDEX),
2440 14 cur_vertex_(0),
2441 14 use_RVC_centroids_((mode & RDT_RVC_CENTROIDS) != 0),
2442 14 select_nearest_((mode & RDT_SELECT_NEAREST) != 0),
2443 14 project_on_surface_((mode & RDT_PROJECT_ON_SURFACE) != 0),
2444 14 seed_is_locked_(seed_is_locked),
2445 14 prefer_seeds_((mode & RDT_PREFER_SEEDS) != 0),
2446 14 AABB_(AABB)
2447 {
2448
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
14 if(prefer_seeds_) {
2449
1/2
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
10 seed_to_vertex_.assign(
2450 RVD.delaunay()->nb_vertices(), UNINITIALIZED
2451 );
2452 }
2453 14 }
2454
2455 /**
2456 * \brief The callback called for each restricted Voronoi cell.
2457 * \param[in] s1 index of current center vertex
2458 * \param[in] P current restricted Voronoi cell
2459 */
2460 358442 void operator() (index_t s1, const Polygon& P) {
2461
2/2
✓ Branch 1 taken 34999 times.
✓ Branch 2 taken 144222 times.
358442 if(RVD_.connected_component_changed()) {
2462
2/2
✓ Branch 0 taken 34992 times.
✓ Branch 1 taken 7 times.
69998 if(cur_seed_ != NO_INDEX) {
2463 69984 end_connected_component();
2464 }
2465 69998 begin_connected_component(s1);
2466 }
2467
2468
4/4
✓ Branch 0 taken 160478 times.
✓ Branch 1 taken 18743 times.
✓ Branch 2 taken 152404 times.
✓ Branch 3 taken 8074 times.
358442 if(prefer_seeds_ && !component_on_border_) {
2469 // NOTE: there is one vertex shift between
2470 // adjacent facet and adjacent seed, there
2471 // must be something wrong in the way the
2472 // combinatorial information is initialized,
2473 // to be checked !!! (should be the i index
2474 // for both)
2475
2/2
✓ Branch 1 taken 595956 times.
✓ Branch 2 taken 152125 times.
1496162 for(index_t i=0; i<P.nb_vertices(); ++i) {
2476 1191912 index_t j = (i+1) % P.nb_vertices();
2477 1191912 if(
2478
6/6
✓ Branch 2 taken 278335 times.
✓ Branch 3 taken 317621 times.
✓ Branch 4 taken 279 times.
✓ Branch 5 taken 278056 times.
✓ Branch 6 taken 279 times.
✓ Branch 7 taken 595677 times.
1748582 P.vertex(i).adjacent_facet() == -1 &&
2479 556670 P.vertex(j).adjacent_seed() == -1
2480 ) {
2481 558 component_on_border_ = true;
2482 558 break;
2483 }
2484 }
2485 }
2486
2487 // Accumulate mass and barycenter
2488 358442 index_t vbase = cur_vertex_ * dimension_;
2489
2/2
✓ Branch 1 taken 357892 times.
✓ Branch 2 taken 179221 times.
1074226 for(index_t i = 1; i + 1 < P.nb_vertices(); ++i) {
2490 715784 double cur_m = Geom::triangle_area(
2491 715784 P.vertex(0).point(),
2492 715784 P.vertex(i).point(),
2493 715784 P.vertex(i + 1).point(), dimension_
2494 );
2495
2/2
✓ Branch 0 taken 1976268 times.
✓ Branch 1 taken 357892 times.
4668320 for(coord_index_t c = 0; c < dimension_; ++c) {
2496 3952536 vertices_[vbase + c] += cur_m / 3.0 * (
2497 3952536 P.vertex(0).point()[c] +
2498 3952536 P.vertex(i).point()[c] +
2499 3952536 P.vertex(i + 1).point()[c]
2500 );
2501 }
2502 715784 m_ += cur_m;
2503 }
2504
2505 // Detect Voronoi vertices and generate
2506 // triangles.
2507 // Note: they can be generated several times,
2508 // since we cannot know in advance whether
2509 // the other instances of the Voronoi vertex
2510 // were finalized or not (i.e. have their
2511 // three vertices ready).
2512 // Duplicate triangles are then filtered-out
2513 // by client code.
2514
2/2
✓ Branch 1 taken 716334 times.
✓ Branch 2 taken 179221 times.
1791110 for(index_t i = 0; i < P.nb_vertices(); ++i) {
2515 1432668 const Vertex& V = P.vertex(i);
2516
2/2
✓ Branch 2 taken 209145 times.
✓ Branch 3 taken 507189 times.
1432668 if(V.sym().nb_bisectors() == 2) {
2517
1/2
✓ Branch 2 taken 209145 times.
✗ Branch 3 not taken.
418290 index_t s2 = V.sym().bisector(0);
2518
1/2
✓ Branch 2 taken 209145 times.
✗ Branch 3 not taken.
418290 index_t s3 = V.sym().bisector(1);
2519
1/2
✓ Branch 2 taken 209145 times.
✗ Branch 3 not taken.
418290 index_t f = V.sym().boundary_facet(0);
2520
2521 418290 index_t v1 = RVD_.current_connected_component();
2522 418290 index_t v2 = index_t(
2523
1/2
✓ Branch 1 taken 209145 times.
✗ Branch 2 not taken.
418290 RVD_.get_facet_seed_connected_component(f,s2)
2524 );
2525 418290 index_t v3 = index_t(
2526
1/2
✓ Branch 1 taken 209145 times.
✗ Branch 2 not taken.
418290 RVD_.get_facet_seed_connected_component(f,s3)
2527 );
2528
2529
4/4
✓ Branch 0 taken 103885 times.
✓ Branch 1 taken 105260 times.
✓ Branch 2 taken 69715 times.
✓ Branch 3 taken 34170 times.
418290 if(v2 != NO_INDEX && v3 != NO_INDEX) {
2530
1/2
✓ Branch 1 taken 69715 times.
✗ Branch 2 not taken.
139430 triangles_.push_back(v1);
2531
1/2
✓ Branch 1 taken 69715 times.
✗ Branch 2 not taken.
139430 triangles_.push_back(v2);
2532
1/2
✓ Branch 1 taken 69715 times.
✗ Branch 2 not taken.
139430 triangles_.push_back(v3);
2533 }
2534 }
2535 }
2536 358442 }
2537
2538 /**
2539 * \brief The destructor
2540 */
2541 14 ~GetConnectedComponentsPrimalTriangles() {
2542
2543 14 bool owns_AABB = false;
2544
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
14 if(select_nearest_ || project_on_surface_) {
2545 ✗ if(AABB_ == nullptr) {
2546 // Construct an axis-aligned bounding box tree,
2547 // do not reorder the mesh (needs to be pre-reordered)
2548 ✗ AABB_ = new MeshFacetsAABB(
2549 ✗ *const_cast<Mesh*>(RVD_.mesh()), AABB_NOREORDER
2550 );
2551 ✗ owns_AABB = true;
2552 }
2553 }
2554
2555
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 if(cur_seed_ != NO_INDEX) {
2556 14 end_connected_component();
2557 }
2558
2559
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
14 if(prefer_seeds_) {
2560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
10 if(select_nearest_) {
2561 ✗ for(index_t s=0; s<seed_to_vertex_.size(); ++s) {
2562 ✗ if(
2563 ✗ seed_to_vertex_[s] != MULTI_COMP &&
2564 ✗ seed_to_vertex_[s] != UNINITIALIZED &&
2565 ✗ seed_to_vertex_[s] != ON_BORDER
2566 ) {
2567 ✗ index_t vbase = seed_to_vertex_[s] * dimension_;
2568
2569 const double* seed_ptr =
2570 ✗ RVD_.delaunay()->vertex_ptr(s);
2571
2572
2573 // At this step, vertex_ptr contains
2574 // the centroid of the connected component
2575 // of the RVC, we now determine whether it
2576 // should be replaced by the
2577 // seed (or by a projection onto the surface).
2578
2579 ✗ const double* vertex_ptr = &(vertices_[vbase]);
2580
2581 // If the seed is nearer to the surface
2582 // than the
2583 // centroid of the connected component of the
2584 // restricted Voronoi cell, then use the seed.
2585
2586 double seed_dist;
2587 ✗ vec3 seed_projection;
2588 double vertex_dist;
2589 ✗ vec3 vertex_projection;
2590
2591 ✗ AABB_->nearest_facet(
2592 ✗ vec3(seed_ptr), seed_projection, seed_dist
2593 );
2594 ✗ AABB_->nearest_facet(
2595 ✗ vec3(vertex_ptr),
2596 vertex_projection, vertex_dist
2597 );
2598
2599 ✗ if(seed_dist < vertex_dist) {
2600 ✗ if(project_on_surface_) {
2601 ✗ for(
2602 ✗ coord_index_t c = 0;
2603 ✗ c < dimension_; ++c
2604 ) {
2605 ✗ vertices_[vbase + c] =
2606 seed_projection[c];
2607 }
2608 } else {
2609 ✗ for(coord_index_t c = 0;
2610 ✗ c < dimension_; ++c
2611 ) {
2612 ✗ vertices_[vbase + c] = seed_ptr[c];
2613 }
2614 }
2615 } else {
2616 ✗ if(project_on_surface_) {
2617 ✗ for(
2618 ✗ coord_index_t c = 0;
2619 ✗ c < dimension_; ++c
2620 ) {
2621 ✗ vertices_[vbase + c] =
2622 vertex_projection[c];
2623 }
2624 }
2625 }
2626 }
2627 }
2628 } else {
2629
2630 // Current mode: prefer seeds and not select nearest
2631 // Replace all points with the seeds (provided that
2632 // they do not correspond to multiple
2633 // connected components).
2634
2635
2/2
✓ Branch 1 taken 25000 times.
✓ Branch 2 taken 5 times.
50010 for(index_t s=0; s<seed_to_vertex_.size(); ++s) {
2636 50000 if(
2637 50000 seed_to_vertex_[s] != MULTI_COMP &&
2638
5/6
✓ Branch 0 taken 25000 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 24999 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 24720 times.
✓ Branch 6 taken 280 times.
99998 seed_to_vertex_[s] != UNINITIALIZED &&
2639
2/2
✓ Branch 1 taken 24720 times.
✓ Branch 2 taken 279 times.
49998 seed_to_vertex_[s] != ON_BORDER
2640 ) {
2641 49440 index_t vbase = seed_to_vertex_[s] * dimension_;
2642
2643 const double* seed_ptr =
2644 49440 RVD_.delaunay()->vertex_ptr(s);
2645
2646
2/2
✓ Branch 0 taken 148320 times.
✓ Branch 1 taken 24720 times.
346080 for(coord_index_t c = 0; c < dimension_; ++c) {
2647 296640 vertices_[vbase + c] = seed_ptr[c];
2648 }
2649 }
2650 }
2651 }
2652 }
2653
2654 14 if(
2655
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
14 (!prefer_seeds_ || !select_nearest_) && project_on_surface_
2656 ) {
2657 ✗ for(index_t v=0; v<vertices_.size()/3; ++v) {
2658 ✗ vec3 p(
2659 ✗ vertices_[3*v], vertices_[3*v+1], vertices_[3*v+2]
2660 );
2661 ✗ vec3 q;
2662 double sq_dist;
2663 ✗ AABB_->nearest_facet(p,q,sq_dist);
2664 ✗ vertices_[3*v ] = q.x;
2665 ✗ vertices_[3*v+1] = q.y;
2666 ✗ vertices_[3*v+2] = q.z;
2667 }
2668 }
2669
2670
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
14 if(owns_AABB) {
2671 ✗ delete AABB_;
2672 ✗ AABB_ = nullptr;
2673 }
2674 14 }
2675
2676 protected:
2677 /**
2678 * \brief Tests whether a given seed is locked.
2679 */
2680 49998 bool seed_is_locked(index_t s) {
2681 return
2682
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 24999 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
49998 seed_is_locked_.size() > 0 &&
2683 49998 seed_is_locked_[s]
2684 ;
2685 }
2686
2687 /**
2688 * \brief Starts a new connected component.
2689 * \param[in] s the seed the connected component
2690 * is associated with.
2691 */
2692 69998 void begin_connected_component(index_t s) {
2693 69998 cur_seed_ = s;
2694
2/2
✓ Branch 0 taken 179994 times.
✓ Branch 1 taken 34999 times.
429986 for(coord_index_t c = 0; c < dimension_; ++c) {
2695
1/2
✓ Branch 1 taken 179994 times.
✗ Branch 2 not taken.
359988 vertices_.push_back(0.0);
2696 }
2697 69998 m_ = 0.0;
2698 69998 component_on_border_ = false;
2699 69998 }
2700
2701 /**
2702 * \brief Terminates the current connected component.
2703 */
2704 69998 void end_connected_component() {
2705
2706 69998 if(
2707 119996 !use_RVC_centroids_ ||
2708
5/6
✓ Branch 0 taken 24999 times.
✓ Branch 1 taken 10000 times.
✓ Branch 3 taken 24999 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 10279 times.
✓ Branch 6 taken 24720 times.
119996 seed_is_locked(cur_seed_) ||
2709
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 24720 times.
49998 component_on_border_
2710 ) {
2711 // Copy seed
2712 20558 index_t vbase = cur_vertex_ * dimension_;
2713 const double* seed_ptr =
2714 20558 RVD_.delaunay()->vertex_ptr(cur_seed_);
2715
2/2
✓ Branch 0 taken 31674 times.
✓ Branch 1 taken 10279 times.
83906 for(coord_index_t c = 0; c < dimension_; ++c) {
2716 63348 vertices_[vbase + c] = seed_ptr[c];
2717 }
2718 } else {
2719 // Use restricted Voronoi
2720 // cell component's centroid.
2721
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24720 times.
49440 double scal = (m_ < 1e-30 ? 0.0 : 1.0 / m_);
2722 49440 index_t vbase = cur_vertex_ * dimension_;
2723
2/2
✓ Branch 0 taken 148320 times.
✓ Branch 1 taken 24720 times.
346080 for(coord_index_t c = 0; c < dimension_; ++c) {
2724 296640 vertices_[vbase + c] *= scal;
2725 }
2726 }
2727
2/2
✓ Branch 0 taken 24999 times.
✓ Branch 1 taken 10000 times.
69998 if(prefer_seeds_) {
2728
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 24720 times.
49998 if(component_on_border_) {
2729 558 seed_to_vertex_[cur_seed_] = ON_BORDER;
2730 }
2731
2/3
✓ Branch 1 taken 24720 times.
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
49998 switch(seed_to_vertex_[cur_seed_]) {
2732 49440 case UNINITIALIZED:
2733 49440 seed_to_vertex_[cur_seed_] = cur_vertex_;
2734 49440 break;
2735 558 case ON_BORDER:
2736 558 break;
2737 ✗ default:
2738 ✗ seed_to_vertex_[cur_seed_] = MULTI_COMP;
2739 ✗ break;
2740 }
2741 }
2742 69998 ++cur_vertex_;
2743 69998 }
2744
2745 private:
2746 const GenRestrictedVoronoiDiagram& RVD_;
2747 coord_index_t dimension_;
2748 vector<index_t>& triangles_;
2749 vector<double>& vertices_;
2750 double m_;
2751 index_t cur_seed_;
2752 index_t cur_vertex_;
2753 bool use_RVC_centroids_;
2754 bool select_nearest_;
2755 bool project_on_surface_;
2756 const std::vector<bool>& seed_is_locked_;
2757 bool prefer_seeds_;
2758 vector<index_t> seed_to_vertex_;
2759 bool component_on_border_;
2760 MeshFacetsAABB* AABB_;
2761 };
2762
2763 /**
2764 * \brief Implementation class for computing the restricted Delaunay
2765 * triangulation in volume mode.
2766 * \details To be used as a template argument
2767 * to RVD::for_each_primal_tetrahedron().
2768 */
2769 class GetPrimalTetrahedra {
2770 public:
2771 /**
2772 * \brief Creates a new GetPrimalTetrahedra.
2773 * \param[out] tetrahedra where to store the tetrahedra
2774 */
2775 ✗ GetPrimalTetrahedra(
2776 vector<index_t>& tetrahedra
2777 ) :
2778 ✗ tetrahedra_(tetrahedra) {
2779 ✗ }
2780
2781 /**
2782 * \brief The callback called for each primal tetrahedron.
2783 * \param[in] v1 index of the first vertex
2784 * \param[in] v2 index of the second vertex
2785 * \param[in] v3 index of the third vertex
2786 * \param[in] v4 index of the fourth vertex
2787 */
2788 ✗ void operator() (index_t v1, index_t v2, index_t v3, index_t v4) {
2789 ✗ tetrahedra_.push_back(v1);
2790 ✗ tetrahedra_.push_back(v2);
2791 ✗ tetrahedra_.push_back(v3);
2792 ✗ tetrahedra_.push_back(v4);
2793 ✗ }
2794
2795 private:
2796 vector<index_t>& tetrahedra_;
2797 };
2798
2799 14 void compute_RDT(
2800 vector<index_t>& simplices,
2801 vector<double>& embedding,
2802 RDTMode mode,
2803 const vector<bool>& seed_is_locked,
2804 MeshFacetsAABB* AABB
2805 ) override {
2806
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
14 if(volumetric_) {
2807 // For the moment, only simple mode is supported
2808 ✗ simplices.clear();
2809 ✗ RVD_.for_each_primal_tetrahedron(
2810 ✗ GetPrimalTetrahedra(simplices)
2811 );
2812 // Reorient the tetrahedra
2813 ✗ index_t nb_tetrahedra = simplices.size() / 4;
2814 ✗ for(index_t t = 0; t < nb_tetrahedra; ++t) {
2815 const double* p1 =
2816 ✗ delaunay()->vertex_ptr(simplices[4 * t]);
2817 const double* p2 =
2818 ✗ delaunay()->vertex_ptr(simplices[4 * t + 1]);
2819 const double*
2820 ✗ p3 = delaunay()->vertex_ptr(simplices[4 * t + 2]);
2821 const double*
2822 ✗ p4 = delaunay()->vertex_ptr(simplices[4 * t + 3]);
2823 ✗ if(PCK::orient_3d(p1, p2, p3, p4) < 0) {
2824 ✗ std::swap(simplices[4 * t], simplices[4 * t + 1]);
2825 }
2826 }
2827 ✗ embedding.clear();
2828 ✗ embedding.reserve(dimension_ * delaunay_->nb_vertices());
2829 ✗ for(index_t i = 0; i < delaunay_->nb_vertices(); i++) {
2830 ✗ for(coord_index_t coord = 0; coord < dimension_; coord++) {
2831 ✗ embedding.push_back(delaunay_->vertex_ptr(i)[coord]);
2832 }
2833 }
2834 } else {
2835
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 if((mode & RDT_MULTINERVE) != 0) {
2836 14 simplices.clear();
2837 14 embedding.clear();
2838 14 bool sym = RVD_.symbolic();
2839 14 RVD_.set_symbolic(true);
2840 14 RVD_.set_connected_components_priority(true);
2841
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
14 RVD_.for_each_polygon(
2842 14 GetConnectedComponentsPrimalTriangles(
2843
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 RVD_, simplices, embedding, RVD_.dimension(),
2844 mode, seed_is_locked, AABB
2845 )
2846 );
2847 14 RVD_.set_symbolic(sym);
2848 14 RVD_.set_connected_components_priority(false);
2849 } else {
2850 // Simple mode: compute RDT, without any post-processing
2851 ✗ simplices.clear();
2852 ✗ RVD_.for_each_primal_triangle(
2853 ✗ GetPrimalTriangles(simplices)
2854 );
2855 ✗ embedding.clear();
2856 ✗ embedding.reserve(dimension_ * delaunay_->nb_vertices());
2857 ✗ for(index_t i = 0; i < delaunay_->nb_vertices(); i++) {
2858 ✗ for(
2859 ✗ coord_index_t coord = 0;
2860 ✗ coord < dimension_; ++coord
2861 ){
2862 ✗ embedding.push_back(
2863 ✗ delaunay_->vertex_ptr(i)[coord]
2864 );
2865 }
2866 }
2867 }
2868 }
2869 14 }
2870
2871 4574 void create_threads() override {
2872 // TODO: check if number of facets is not smaller than
2873 // number of threads
2874 // TODO: create parts even if facets range is specified
2875 // (and subdivide facets range)
2876 4574 if(
2877
2/2
✓ Branch 0 taken 467 times.
✓ Branch 1 taken 1820 times.
4574 is_slave_ ||
2878
2/4
✓ Branch 0 taken 467 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 467 times.
934 facets_begin_ != NO_INDEX || facets_end_ != NO_INDEX
2879 ) {
2880 3640 return;
2881 }
2882 934 index_t nb_parts_in = Process::maximum_concurrent_threads();
2883 #ifdef GEO_DETERMINISTIC
2884 // Always partition (even for 1 thread) so every thread count takes
2885 // the same deterministic reduction path.
2886 const bool use_single_thread_shortcut = false;
2887 #else
2888 934 const bool use_single_thread_shortcut = (nb_parts_in == 1);
2889 #endif
2890
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 455 times.
934 if(nb_parts() != nb_parts_in) {
2891
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
24 if(use_single_thread_shortcut) {
2892 ✗ delete_threads();
2893 } else {
2894 24 vector<index_t> facet_ptr;
2895 24 vector<index_t> tet_ptr;
2896 24 mesh_partition(
2897
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 *mesh_, MESH_PARTITION_HILBERT,
2898 facet_ptr, tet_ptr, nb_parts_in
2899 );
2900
1/2
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
24 delete_threads();
2901
6/20
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 48 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 48 times.
✓ Branch 9 taken 12 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 12 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
120 parts_ = new thisclass[nb_parts_in];
2902 24 nb_parts_ = nb_parts_in;
2903
2/2
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 12 times.
120 for(index_t i = 0; i < nb_parts(); ++i) {
2904
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).mesh_ = mesh_;
2905
2/4
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
96 part(i).set_delaunay(delaunay_);
2906
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).R3_embedding_base_ = R3_embedding_base_;
2907
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).R3_embedding_stride_ = R3_embedding_stride_;
2908
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).has_weights_ = has_weights_;
2909
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).master_ = this;
2910
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
96 part(i).RVD_.set_mesh(mesh_);
2911
4/8
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 48 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 48 times.
✗ Branch 11 not taken.
96 part(i).set_facets_range(
2912 facet_ptr[i], facet_ptr[i + 1]
2913 );
2914
2/4
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
96 part(i).set_exact_predicates(RVD_.exact_predicates());
2915
2/4
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
96 part(i).set_volumetric(volumetric());
2916
2/4
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 48 times.
✗ Branch 6 not taken.
96 part(i).set_check_SR(RVD_.check_SR());
2917 }
2918
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
24 if(mesh_->cells.nb() != 0) {
2919
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 4 times.
40 for(index_t i = 0; i < nb_parts(); ++i) {
2920
4/8
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 16 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
32 part(i).set_tetrahedra_range(
2921 tet_ptr[i], tet_ptr[i + 1]
2922 );
2923 }
2924 }
2925
2/8
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
24 geo_assert(!Process::is_running_threads());
2926 24 }
2927 }
2928 }
2929
2930 176 void set_volumetric(bool x) override {
2931 176 volumetric_ = x;
2932
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
176 for(index_t i = 0; i < nb_parts(); ++i) {
2933 ✗ part(i).set_volumetric(x);
2934 }
2935 176 }
2936
2937 96 void set_facets_range(
2938 index_t facets_begin, index_t facets_end
2939 ) override {
2940 96 RVD_.set_facets_range(facets_begin, facets_end);
2941 96 facets_begin_ = facets_begin;
2942 96 facets_end_ = facets_end;
2943 96 }
2944
2945 32 void set_tetrahedra_range(
2946 index_t tets_begin, index_t tets_end
2947 ) override {
2948 32 RVD_.set_tetrahedra_range(tets_begin, tets_end);
2949 32 tets_begin_ = tets_begin;
2950 32 tets_end_ = tets_end;
2951 32 }
2952
2953 230 void delete_threads() override {
2954
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 12 times.
326 delete[] parts_;
2955 230 parts_ = nullptr;
2956 230 nb_parts_ = 0;
2957 230 }
2958
2959 /**
2960 * \brief Gets the number of parts (or number of threads).
2961 */
2962 15098 index_t nb_parts() const {
2963 15098 return nb_parts_;
2964 }
2965
2966 /**
2967 * \brief Gets a given part from its index.
2968 * \param[in] i index of the part
2969 * \pre \p i < nb_parts()
2970 */
2971 4728 thisclass& part(index_t i) {
2972
1/6
✗ Branch 1 not taken.
✓ Branch 2 taken 2364 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
4728 geo_debug_assert(i < nb_parts());
2973 4728 return parts_[i];
2974 }
2975
2976 /**
2977 * \copydoc RestrictedVoronoiDiagram::point_allocator()
2978 */
2979 ✗ GEOGen::PointAllocator* point_allocator() override {
2980 ✗ return RVD_.point_allocator();
2981 }
2982
2983
2984 protected:
2985
2986 GenRestrictedVoronoiDiagram RVD_;
2987
2988 // For projection
2989 bool use_exact_projection_;
2990 index_t nb_triangles_;
2991 vector<index_t> triangles_;
2992 vector<vector<index_t> > stars_;
2993 Delaunay_var mesh_vertices_;
2994
2995 // One of MT_NONE, MT_LLOYD, MT_NEWTON
2996 ThreadMode thread_mode_;
2997
2998 bool is_slave_;
2999
3000 // Variables for 'master' in multithreading mode
3001 thisclass* parts_;
3002 index_t nb_parts_;
3003 Process::SpinLockArray spinlocks_;
3004
3005 // Newton mode with int. simplex
3006 IntegrationSimplex* simplex_func_;
3007
3008 // PolygonCallback mode.
3009 RVDPolygonCallback* polygon_callback_;
3010
3011 // PolyhedronCallback mode.
3012 RVDPolyhedronCallback* polyhedron_callback_;
3013
3014 // master stores argument for compute_centroids() and
3015 // compute_CVT_func_grad() to pass it to the parts.
3016 double* arg_vectors_;
3017 double* arg_scalars_;
3018
3019 // Variables for 'slaves' in multithreading mode
3020 thisclass* master_;
3021 double funcval_; // Newton mode: function value
3022
3023 #ifdef GEO_DETERMINISTIC
3024 // Master-side accumulators, reduced in canonical order (thread count
3025 // independent).
3026 DeterministicIndexedAccu accu_f_; // Newton mode: function value
3027 DeterministicIndexedAccu accu_g_; // Newton/Lloyd: gradient / centroid
3028 DeterministicIndexedAccu accu_m_; // Lloyd mode: mass
3029
3030 // Where each part pushes its contributions (nullptr: legacy path).
3031 DeterministicIndexedAccu* master_f_ = nullptr;
3032 DeterministicIndexedAccu* master_g_ = nullptr;
3033 DeterministicIndexedAccu* master_m_ = nullptr;
3034
3035 // Master-side scratch buffers for the deterministic reductions.
3036 DeterministicReduceScratch det_scratch_;
3037 #endif
3038
3039 protected:
3040 /**
3041 * \brief Destructor
3042 */
3043 288 ~RVD_Nd_Impl() override {
3044 192 delete_threads();
3045 288 }
3046
3047 private:
3048 /** \brief Forbids construction by copy. */
3049 RVD_Nd_Impl(const thisclass&);
3050
3051 /** \brief Forbids assignment. */
3052 thisclass& operator= (const thisclass&);
3053 };
3054 }
3055
3056 /****************************************************************************/
3057
3058 namespace GEO {
3059
3060 48 RestrictedVoronoiDiagram* RestrictedVoronoiDiagram::create(
3061 Delaunay* delaunay, Mesh* mesh,
3062 const double* R3_embedding, index_t R3_embedding_stride
3063 ) {
3064
3065 48 geo_cite("DBLP:journals/tog/EdelsbrunnerM90");
3066 48 geo_cite("DBLP:conf/compgeom/Shewchuk96");
3067 48 geo_cite("meyer:inria-00344297");
3068 48 geo_cite("DBLP:conf/gmp/YanWLL10");
3069 48 geo_cite("DBLP:journals/cad/YanWLL13");
3070 48 geo_cite("DBLP:journals/cad/Levy16");
3071
3072 48 delaunay->set_stores_neighbors(true);
3073 48 RestrictedVoronoiDiagram* result = nullptr;
3074
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
48 geo_assert(delaunay != nullptr);
3075 48 coord_index_t dim = delaunay->dimension();
3076
4/8
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
48 switch(dim) {
3077 ✗ case 2:
3078 ✗ result = new RVD_Nd_Impl<2>(
3079 delaunay, mesh, R3_embedding, R3_embedding_stride
3080 ✗ );
3081 ✗ break;
3082 40 case 3:
3083
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
40 result = new RVD_Nd_Impl<3>(
3084 delaunay, mesh, R3_embedding, R3_embedding_stride
3085
1/2
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
40 );
3086 40 break;
3087 1 case 4:
3088
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 result = new RVD_Nd_Impl<4>(
3089 delaunay, mesh, R3_embedding, R3_embedding_stride
3090
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 );
3091 1 break;
3092 6 case 6:
3093
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 result = new RVD_Nd_Impl<6>(
3094 delaunay, mesh, R3_embedding, R3_embedding_stride
3095
1/2
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 );
3096 6 break;
3097 1 case 8:
3098
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 result = new RVD_Nd_Impl<8>(
3099 delaunay, mesh, R3_embedding, R3_embedding_stride
3100
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 );
3101 1 break;
3102 ✗ case 20:
3103 ✗ result = new RVD_Nd_Impl<20>(
3104 delaunay, mesh, R3_embedding, R3_embedding_stride
3105 ✗ );
3106 ✗ break;
3107 ✗ case 100:
3108 ✗ result = new RVD_Nd_Impl<100>(
3109 delaunay, mesh, R3_embedding, R3_embedding_stride
3110 ✗ );
3111 ✗ break;
3112 ✗ default:
3113 ✗ geo_assert_not_reached;
3114 }
3115
5/8
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 48 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 35 times.
✓ Branch 12 taken 13 times.
96 if(CmdLine::get_arg("algo:predicates") == "exact") {
3116 35 result->set_exact_predicates(true);
3117 }
3118 48 return result;
3119 }
3120
3121 144 void RestrictedVoronoiDiagram::set_delaunay(Delaunay* delaunay) {
3122 144 delaunay_ = delaunay;
3123
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
144 if(delaunay_ != nullptr) {
3124 96 dimension_ = delaunay->dimension();
3125 } else {
3126 48 dimension_ = 0;
3127 }
3128 144 }
3129
3130 192 RestrictedVoronoiDiagram::~RestrictedVoronoiDiagram() {
3131 192 }
3132
3133 96 RestrictedVoronoiDiagram::RestrictedVoronoiDiagram(
3134 Delaunay* delaunay, Mesh* mesh,
3135 const double* R3_embedding, index_t R3_embedding_stride
3136 96 ) :
3137 96 dimension_(0),
3138 96 mesh_(mesh),
3139 96 R3_embedding_base_(R3_embedding),
3140
1/2
✓ Branch 2 taken 96 times.
✗ Branch 3 not taken.
96 R3_embedding_stride_(R3_embedding_stride) {
3141
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 set_delaunay(delaunay);
3142 96 has_weights_ = false;
3143 96 facets_begin_ = NO_INDEX;
3144 96 facets_end_ = NO_INDEX;
3145 96 tets_begin_ = NO_INDEX;
3146 96 tets_end_ = NO_INDEX;
3147 96 volumetric_ = false;
3148 96 }
3149
3150
3151 ✗ void RestrictedVoronoiDiagram::compute_RDT(
3152 Mesh& RDT,
3153 RDTMode mode,
3154 const vector<bool>& seed_is_locked,
3155 MeshFacetsAABB* AABB
3156 ) {
3157 ✗ vector<index_t> simplices;
3158 ✗ vector<double> embedding;
3159 ✗ compute_RDT(
3160 simplices, embedding,
3161 mode, seed_is_locked,
3162 AABB
3163 );
3164 ✗ if(volumetric()) {
3165 ✗ RDT.cells.assign_tet_mesh(dimension(),embedding,simplices,true);
3166 } else {
3167 ✗ RDT.facets.assign_triangle_mesh(
3168 ✗ dimension(),embedding,simplices,true
3169 );
3170 ✗ if((mode & RDT_DONT_REPAIR) == 0) {
3171 ✗ mesh_repair(RDT); // Needed to reorient triangles
3172 }
3173 }
3174 ✗ }
3175
3176
3177 }
3178