GCC Code Coverage Report


Directory: ./
File: mesh/mesh_remesh.cpp
Date: 2026-11-30 05:45:57
Exec Total Coverage
Lines: 161 194 83.0%
Functions: 5 5 100.0%
Branches: 163 316 51.6%

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/mesh/mesh_remesh.h>
41 #include <geogram/mesh/mesh.h>
42 #include <geogram/mesh/mesh_repair.h>
43 #include <geogram/mesh/mesh_geometry.h>
44 #include <geogram/mesh/mesh_halfedges.h>
45 #include <geogram/mesh/mesh_reorder.h>
46 #include <geogram/mesh/mesh_preprocessing.h>
47 #include <geogram/mesh/mesh_io.h>
48 #include <geogram/mesh/mesh_AABB.h>
49 #include <geogram/voronoi/CVT.h>
50 #include <geogram/basic/command_line.h>
51 #include <geogram/basic/stopwatch.h>
52 #include <geogram/basic/progress.h>
53 #include <geogram/bibliography/bibliography.h>
54 #include <geogram/mesh/mesh_io.h>
55 #include <geogram/NL/nl.h>
56
57
58 /****************************************************************************/
59
60 namespace GEO {
61
62 7 void remesh_smooth(
63 Mesh& M_in, Mesh& M_out,
64 index_t nb_points,
65 coord_index_t dim,
66 index_t nb_Lloyd_iter,
67 index_t nb_Newton_iter,
68 index_t Newton_m,
69 bool adjust,
70 double adjust_max_edge_distance,
71 double adjust_border_importance
72
73 ) {
74
75 7 geo_cite("DBLP:journals/cgf/YanLLSW09");
76 7 geo_cite("DBLP:conf/imr/LevyB12");
77
78 if(dim == 0) {
79 dim = coord_index_t(M_in.vertices.dimension());
80 }
81
82 geo_argused(dim);
83
84
2/4
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
14 Stopwatch W("Remesh");
85
86
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 CentroidalVoronoiTesselation CVT(&M_in);
87
88
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if(nb_points == 0) {
89 nb_points = M_in.vertices.nb();
90 }
91
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 CVT.compute_initial_sampling(nb_points, true); // true: for verbose
92
93 try {
94
3/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 7 times.
✗ Branch 8 not taken.
14 ProgressTask progress("Lloyd", 100);
95 CVT.set_progress_logger(&progress);
96 #ifdef GEO_DETERMINISTIC
97 // Safe mode: find the same restricted Voronoi cells regardless of
98 // mesh partitioning (needed for reproducibility).
99 CVT.Lloyd_iterations(nb_Lloyd_iter, true);
100 #else
101
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 CVT.Lloyd_iterations(nb_Lloyd_iter);
102 #endif
103 7 }
104 ✗ catch(const TaskCanceled&) {
105 // TODO_CANCEL
106 ✗ }
107
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if(nb_Newton_iter != 0) {
109 try {
110 ✗ ProgressTask progress("Newton", 100);
111 CVT.set_progress_logger(&progress);
112 ✗ CVT.Newton_iterations(nb_Newton_iter, Newton_m);
113 ✗ }
114 ✗ catch(const TaskCanceled&) {
115 // TODO_CANCEL
116 ✗ }
117 }
118
119
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
7 if(M_in.vertices.dimension() == 6 &&
120
3/6
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
12 CmdLine::get_arg_bool("dbg:save_6d")
121 ) {
122 ✗ Logger::out("Remesh")
123 << "Saving source mesh into mesh6.obj6" << std::endl;
124 ✗ mesh_save(M_in, "mesh6.obj6");
125 ✗ Logger::out("Remesh")
126 << "Saving sampling into points6.txt" << std::endl;
127 ✗ std::ofstream out("points6.txt");
128 out << CVT.delaunay()->nb_vertices() << std::endl;
129 ✗ for(index_t i = 0; i < CVT.delaunay()->nb_vertices(); i++) {
130 ✗ for(coord_index_t c = 0; c < 6; c++) {
131 ✗ out << CVT.delaunay()->vertex_ptr(i)[c] << " ";
132 }
133 out << std::endl;
134 }
135 ✗ }
136
137 // Delete auxiliary storage used for each threads (it uses a lot of RAM,
138 // we need this RAM to create the surface now...)
139
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 CVT.RVD()->delete_threads();
140
141 CVT.set_use_RVC_centroids(
142
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 CmdLine::get_arg_bool("remesh:RVC_centroids")
143 );
144
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 bool multi_nerve = CmdLine::get_arg_bool("remesh:multi_nerve");
145
146
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 Logger::out("Remesh") << "Computing RVD..." << std::endl;
147
148
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 CVT.compute_surface(&M_out, multi_nerve);
149
3/6
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 7 times.
14 if(CmdLine::get_arg_bool("dbg:save_ANN_histo")) {
150 ✗ Logger::out("ANN")
151 << "Saving histogram to ANN_histo.dat" << std::endl;
152 ✗ std::ofstream out("ANN_histo.dat");
153 ✗ CVT.delaunay()->save_histogram(out);
154 ✗ }
155
156
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if(adjust) {
157
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 mesh_adjust_surface(
158 M_out, M_in, adjust_max_edge_distance,
159 false, adjust_border_importance
160 );
161 }
162
163
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
7 if(Geom::mesh_enclosed_volume(M_out) < 0.0) {
164 ✗ M_out.facets.flip();
165 }
166 7 }
167
168 /************************************************************************/
169
170 /**
171 * \brief Gets the middle segment of a quad
172 * \param[in] AABB a reference to a MeshFacetsAABB
173 * \param[in] f a facet
174 * \param[out] q1 , q2 the extremities of the middle segment
175 * \details The quad has vertices p1, p2, p3, p4, on exit
176 * q1 = 1/2(p1+p4) and q2 = 1/2(p2+p3)
177 */
178
1/2
✓ Branch 0 taken 558 times.
✗ Branch 1 not taken.
558 inline void get_quad_middle_segment(
179 const MeshFacetsAABB& AABB, index_t f, vec3& q1, vec3& q2
180 ) {
181
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 558 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
558 geo_assert(AABB.mesh()->facets.nb_vertices(f) == 4);
182 558 vec3 p1 = AABB.mesh()->facets.point(f,0);
183 558 vec3 p2 = AABB.mesh()->facets.point(f,1);
184 558 vec3 p3 = AABB.mesh()->facets.point(f,2);
185 558 vec3 p4 = AABB.mesh()->facets.point(f,3);
186 558 q1 = 0.5*(p1+p4);
187 558 q2 = 0.5*(p2+p3);
188 558 }
189
190 /**
191 * \brief Gets the nearest point on a surface along a ray
192 * \param[in] AABB the facets of the surface
193 * as a MeshFacetsAABB
194 * \param[in] R1 the ray, both directions are tested to find
195 * the nearest point
196 * \param[in] max_dist if the nearest point is further away
197 * than \p max_dist, then the origin of the ray is returned
198 * \param[in] ribbon_mode if set, project on middle segment
199 * of quad
200 */
201 104990 inline vec3 nearest_along_bidirectional_ray(
202 const MeshFacetsAABB& AABB, const Ray& R1,
203 double max_dist, bool ribbon_mode=false
204 ) {
205 104990 vec3 p = R1.origin;
206 104990 vec3 result = p;
207 Ray R2(R1.origin, -R1.direction);
208 MeshFacetsAABB::Intersection I1;
209 MeshFacetsAABB::Intersection I2;
210 104990 bool has_I1 = AABB.ray_nearest_intersection(R1,I1);
211 104990 bool has_I2 = AABB.ray_nearest_intersection(R2,I2);
212
213
2/2
✓ Branch 0 taken 558 times.
✓ Branch 1 taken 104432 times.
104990 if(has_I1 && ribbon_mode) {
214 vec3 q,q1,q2;
215 double l1,l2;
216 558 get_quad_middle_segment(AABB, I1.f, q1, q2);
217 558 Geom::point_segment_squared_distance(I1.p, q1, q2, q, l1, l2);
218 558 I1.p=q;
219 }
220
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104990 times.
104990 if(has_I2 && ribbon_mode) {
222 vec3 q,q1,q2;
223 double l1,l2;
224 ✗ get_quad_middle_segment(AABB, I2.f, q1, q2);
225 ✗ Geom::point_segment_squared_distance(I2.p, q1, q2, q, l1, l2);
226 ✗ I2.p=q;
227 }
228
229
2/2
✓ Branch 0 taken 27307 times.
✓ Branch 1 taken 77683 times.
104990 if(has_I1 && !has_I2) {
230 27307 result = I1.p;
231 }
232
2/2
✓ Branch 0 taken 10665 times.
✓ Branch 1 taken 94325 times.
104990 if(!has_I1 && has_I2) {
233 10665 result = I2.p;
234 }
235
2/2
✓ Branch 0 taken 67018 times.
✓ Branch 1 taken 37972 times.
104990 if(has_I1 && has_I2) {
236
2/2
✓ Branch 0 taken 25351 times.
✓ Branch 1 taken 41667 times.
67018 if(Geom::distance2(p,I1.p) < Geom::distance2(p,I2.p)) {
237 25351 result = I1.p;
238 } else {
239 41667 result = I2.p;
240 }
241 }
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 104990 times.
104990 if(Geom::distance2(result,p) > max_dist*max_dist) {
243 ✗ result = p;
244 }
245 104990 return result;
246 }
247
248
249 /**
250 * \brief Creates a ribbon orthogonal to the border of a
251 * surface mesh.
252 * \param[in] M the input surface mesh
253 * \param[out] ribbon the generated ribbon
254 * \param[in] height the height of the ribbon
255 */
256 2 static void create_ribbon_on_border(
257 const Mesh& M,
258 Mesh& ribbon,
259 double height
260 ) {
261 index_t nb_border_edges=0;
262 2 vector<vec3> Nv(M.vertices.nb(), vec3(0.0, 0.0, 0.0));
263
2/2
✓ Branch 0 taken 65467 times.
✓ Branch 1 taken 2 times.
65469 for(index_t f: M.facets) {
264
2/4
✓ Branch 1 taken 65467 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 65467 times.
✗ Branch 4 not taken.
65467 vec3 N = Geom::mesh_facet_normal(M, f);
265
2/2
✓ Branch 0 taken 196401 times.
✓ Branch 1 taken 65467 times.
261868 for(index_t c1: M.facets.corners(f)) {
266
2/2
✓ Branch 0 taken 1233 times.
✓ Branch 1 taken 195168 times.
196401 if(M.facet_corners.adjacent_facet(c1) == NO_FACET) {
267 index_t c2 = M.facets.next_corner_around_facet(f, c1);
268 index_t v1 = M.facet_corners.vertex(c1);
269 index_t v2 = M.facet_corners.vertex(c2);
270 Nv[v1] += N;
271 Nv[v2] += N;
272 1233 ++nb_border_edges;
273 }
274 }
275 }
276
277
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 ribbon.vertices.set_dimension(3);
278
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 ribbon.vertices.create_vertices(4*nb_border_edges);
279
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 ribbon.facets.create_quads(nb_border_edges);
280
281 index_t cur_border_e = 0;
282
2/2
✓ Branch 0 taken 65467 times.
✓ Branch 1 taken 2 times.
65469 for(index_t f: M.facets) {
283
2/2
✓ Branch 0 taken 196401 times.
✓ Branch 1 taken 65467 times.
261868 for(index_t c1: M.facets.corners(f)) {
284
2/2
✓ Branch 0 taken 1233 times.
✓ Branch 1 taken 195168 times.
196401 if(M.facet_corners.adjacent_facet(c1) == NO_FACET) {
285 index_t c2 = M.facets.next_corner_around_facet(f, c1);
286 index_t v1 = M.facet_corners.vertex(c1);
287 index_t v2 = M.facet_corners.vertex(c2);
288 const vec3& p1 = M.vertices.point(v1);
289 const vec3& p2 = M.vertices.point(v2);
290
291 1233 vec3 U1 = 0.5*height * normalize(Nv[v1]);
292
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1233 times.
1233 vec3 U2 = 0.5*height * normalize(Nv[v2]);
293
294 vec3 q1 = p1 + U1;
295 vec3 q2 = p2 + U2;
296 vec3 q3 = p2 - U2;
297 vec3 q4 = p1 - U1;
298
299 1233 ribbon.vertices.point(4*cur_border_e ) = q1;
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1233 times.
1233 ribbon.vertices.point(4*cur_border_e+1) = q2;
301 1233 ribbon.vertices.point(4*cur_border_e+2) = q3;
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1233 times.
1233 ribbon.vertices.point(4*cur_border_e+3) = q4;
303
304 ribbon.facets.set_vertex(cur_border_e, 0, 4*cur_border_e );
305 ribbon.facets.set_vertex(cur_border_e, 1, 4*cur_border_e+1);
306 ribbon.facets.set_vertex(cur_border_e, 2, 4*cur_border_e+2);
307 ribbon.facets.set_vertex(cur_border_e, 3, 4*cur_border_e+3);
308 1233 ++cur_border_e;
309 }
310 }
311 }
312 2 }
313
314 /************************************************************************/
315
316 7 void GEOGRAM_API mesh_adjust_surface(
317 Mesh& surface,
318 Mesh& reference,
319 double max_edge_distance,
320 bool project_borders,
321 double border_importance
322 ) {
323 // The algorithm:
324 // 1) For each surface vertex v (located at Pv) with normal Nv,
325 // we determine a "target point" Qv as the intersection between
326 // the ray R(Pv,Nv) and the reference surface.
327 // 2) For each facet f with center point Pf, we determine a "target
328 // point" Qf that corresponds to the intersection between the ray
329 // R(Pf, Nf) and the reference surface, where Nf is the sum of
330 // the directions Nv associated with the vertices of the facet.
331 // 3) We optimize for the lambda's in the relations that correspond to
332 // 1) and 2), that is, the sum of the squared distances
333 // || P + lambda N - Q ||^2 for each v, for each f
334 // 4) For each v, Pv = Pv + lambda_v Nv
335 //
336 // For surfaces with borders, there is a subtlety: instead of using
337 // the normal to the surface, we use for Nv a direction tangent to
338 // the surface and normal to the border of the surface, and to find
339 // the reference point Qv, we construct a "ribbon", obtained by
340 // sweeping a segment normal to the reference surface along the
341 // border of the reference surface. The point Qv is obtained by
342 // - compute the intersection I between the ray R(Pv, Nv) and the
343 // ribbon. This intersection is in a quad supported by a segment
344 // [q1,q2] on the border of the reference surface
345 // - Qv is determined as the nearest point to I on [q1,q2]
346 //
347 // For each border edge e=(i,j), the center point Pe=0.5(Pi+Pj), the
348 // direction Ne = 0.5(Ni+Nj), a point Qe is determined (using the same
349 // algorithm as in the previous point) and a least-squares term
350 // || Pe - Qe ||^2 is added to the quantity to be minimized.
351 //
352 // An optional final step (brutally) assigns Pv = Qv for each border
353 // vertex (but in general it gives a worse result on the facets
354 // adjacent to the border, so the option project_borders is deactivated
355 // by default).
356
357 // Geometric search uses larger maximum
358 // distance for points on the border.
359 const double border_distance_factor = 10.0;
360
361 7 MeshFacetsAABB AABB(reference);
362
363 // For each vertex, direction along which the neighbor
364 // on the surface is searched (or the neighbor on the
365 // ribbon if it is a vertex on the border)
366 ✗ vector<vec3> Nv(surface.vertices.nb(), vec3(0.0, 0.0, 0.0));
367
368 // average edge length incident to vertex
369
1/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7 vector<double> Lv(surface.vertices.nb(), 0.0);
370
371 // number of edges incident to a vertex
372
1/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
7 vector<index_t> Cv(surface.vertices.nb(), 0);
373
374 // Compute directions Nv for inner vertices
375 // (use surface normal)
376
2/2
✓ Branch 0 taken 69713 times.
✓ Branch 1 taken 7 times.
69720 for(index_t f: surface.facets) {
377
1/2
✓ Branch 1 taken 69713 times.
✗ Branch 2 not taken.
69713 vec3 n = Geom::mesh_facet_normal(surface, f);
378 index_t d = surface.facets.nb_vertices(f);
379
2/2
✓ Branch 0 taken 209139 times.
✓ Branch 1 taken 69713 times.
278852 for(index_t lv=0;lv<d;++lv) {
380 index_t v1 = surface.facets.vertex(f,lv);
381 139426 index_t v2 = surface.facets.vertex(
382
2/2
✓ Branch 0 taken 139426 times.
✓ Branch 1 taken 69713 times.
209139 f,(lv==d-1)?0:lv+1
383 );
384 Nv[v1] += n;
385 double l = Geom::distance2(
386 surface.vertices.point(v1),
387 surface.vertices.point(v2)
388 );
389 209139 l = ::sqrt(l);
390 209139 Lv[v1] += l;
391 209139 Lv[v2] += l;
392 209139 Cv[v1]++;
393 209139 Cv[v2]++;
394 }
395 }
396
397 // Normalize average incident edge length
398
2/2
✓ Branch 0 taken 34998 times.
✓ Branch 1 taken 7 times.
35005 for(index_t v: surface.vertices) {
399
1/2
✓ Branch 0 taken 34998 times.
✗ Branch 1 not taken.
34998 if(Cv[v] != 0) {
400 34998 Lv[v] /= double(Cv[v]);
401 }
402 }
403
404
405 // Compute Nv for vertices on the border: the used vector
406 // is normal to the border (tangent to the surface), because
407 // we will find nearest neighbor from the "ribbon" mesh.
408 index_t nb_v_on_border = 0;
409 7 vector<bool> v_on_border(surface.vertices.nb(), false);
410
2/2
✓ Branch 0 taken 69713 times.
✓ Branch 1 taken 7 times.
69720 for(index_t f: surface.facets) {
411
2/2
✓ Branch 0 taken 209139 times.
✓ Branch 1 taken 69713 times.
278852 for(index_t c1: surface.facets.corners(f)) {
412
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 208860 times.
209139 if(surface.facet_corners.adjacent_facet(c1) == NO_FACET) {
413 index_t v = surface.facet_corners.vertex(c1);
414 v_on_border[v] = true;
415 279 ++nb_v_on_border;
416 279 Nv[v] = vec3(0.0, 0.0, 0.0);
417 }
418 }
419 }
420
421
2/2
✓ Branch 0 taken 69713 times.
✓ Branch 1 taken 7 times.
69720 for(index_t f: surface.facets) {
422
1/2
✓ Branch 1 taken 69713 times.
✗ Branch 2 not taken.
69713 vec3 N = Geom::mesh_facet_normal(surface, f);
423
2/2
✓ Branch 0 taken 209139 times.
✓ Branch 1 taken 69713 times.
278852 for(index_t c1: surface.facets.corners(f)) {
424
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 208860 times.
209139 if(surface.facet_corners.adjacent_facet(c1) == NO_FACET) {
425 index_t c2 = surface.facets.next_corner_around_facet(f, c1);
426 index_t v1 = surface.facet_corners.vertex(c1);
427 index_t v2 = surface.facet_corners.vertex(c2);
428 const vec3& p1 = surface.vertices.point(v1);
429 const vec3& p2 = surface.vertices.point(v2);
430 vec3 Ne = cross(p2 - p1, N);
431 Nv[v1] += Ne;
432 Nv[v2] += Ne;
433 }
434 }
435 }
436
437 bool reference_has_borders = false;
438
2/2
✓ Branch 0 taken 46802 times.
✓ Branch 1 taken 5 times.
46807 for(index_t c: reference.facet_corners) {
439
2/2
✓ Branch 0 taken 46800 times.
✓ Branch 1 taken 2 times.
46802 if(reference.facet_corners.adjacent_facet(c) == NO_FACET) {
440 reference_has_borders = true;
441 break;
442 }
443 }
444
445 // Create ribbon and AABB for the ribbon if the reference surface
446 // has vertices on the border
447
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 Mesh border_ribbon;
448 MeshFacetsAABB border_ribbon_AABB;
449
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if(nb_v_on_border != 0 && reference_has_borders) {
450
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 create_ribbon_on_border(
451 reference, border_ribbon,
452
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 max_edge_distance*4.0*surface_average_edge_length(surface)
453 );
454
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 border_ribbon_AABB.initialize(border_ribbon);
455 }
456
457 // nearest point along Nv
458 vector<vec3> Qv(surface.vertices.nb());
459
2/2
✓ Branch 0 taken 34998 times.
✓ Branch 1 taken 7 times.
35005 for(index_t v: surface.vertices) {
460
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 34719 times.
34998 vec3 p = surface.vertices.point(v);
461
3/4
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 34719 times.
✓ Branch 2 taken 279 times.
✗ Branch 3 not taken.
34998 if(v_on_border[v] && reference_has_borders) {
462
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 Qv[v] = nearest_along_bidirectional_ray(
463 279 border_ribbon_AABB, Ray(p, Nv[v]),
464
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 border_distance_factor*max_edge_distance*Lv[v],
465 true
466 );
467 } else {
468
1/2
✓ Branch 1 taken 34719 times.
✗ Branch 2 not taken.
34719 Qv[v] = nearest_along_bidirectional_ray(
469 34719 AABB, Ray(p, Nv[v]),max_edge_distance*Lv[v]
470 );
471 }
472 }
473
474
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlNewContext();
475
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE);
476
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlSolverParameteri(
477 NL_NB_VARIABLES, NLint(surface.vertices.nb())
478 );
479
480
481
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlBegin(NL_SYSTEM);
482
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlBegin(NL_MATRIX);
483
484 // For each vertex v, least squares constraint that
485 // makes the vertex "attracted" by Qv[v]
486
2/2
✓ Branch 0 taken 34998 times.
✓ Branch 1 taken 7 times.
35005 for(index_t v: surface.vertices) {
487
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 34719 times.
34998 if(v_on_border[v]) {
488
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 nlRowScaling(border_importance);
489 }
490
491 // p + lambda_v * Nv = q ---> lambda_v * Nv = q - v
492
2/2
✓ Branch 0 taken 104994 times.
✓ Branch 1 taken 34998 times.
139992 for(index_t c=0; c<3; ++c) {
493
1/2
✓ Branch 1 taken 104994 times.
✗ Branch 2 not taken.
104994 nlBegin(NL_ROW);
494
1/2
✓ Branch 1 taken 104994 times.
✗ Branch 2 not taken.
104994 nlCoefficient(v,Nv[v][c]);
495
1/2
✓ Branch 1 taken 104994 times.
✗ Branch 2 not taken.
104994 nlRightHandSide(Qv[v][c] - surface.vertices.point_ptr(v)[c]);
496
1/2
✓ Branch 1 taken 104994 times.
✗ Branch 2 not taken.
104994 nlEnd(NL_ROW);
497 }
498 }
499
500 // For each facet f, least squares constraint that
501 // makes the center of the facet "attracted" by the
502 // nearest point along the averaged directions of
503 // all vertices of the facet
504
2/2
✓ Branch 0 taken 69713 times.
✓ Branch 1 taken 7 times.
69720 for(index_t f: surface.facets) {
505 index_t d = surface.facets.nb_vertices(f);
506
507 vec3 Nf(0.0, 0.0, 0.0);
508 vec3 Pf;
509 double Lf=0.0;
510
511
2/2
✓ Branch 0 taken 209139 times.
✓ Branch 1 taken 69713 times.
278852 for(index_t lv=0; lv<d; ++lv) {
512 index_t v= surface.facets.vertex(f,lv);
513 Nf += Nv[v];
514 Pf += surface.vertices.point(v);
515 209139 Lf += Lv[v];
516 }
517
1/2
✓ Branch 1 taken 69713 times.
✗ Branch 2 not taken.
69713 Pf = (1.0 / double(d))*Pf;
518 69713 Lf = (1.0 / double(d))*Lf;
519
1/2
✓ Branch 1 taken 69713 times.
✗ Branch 2 not taken.
69713 vec3 Qf = nearest_along_bidirectional_ray(
520 69713 AABB, Ray(Pf, Nf), max_edge_distance*Lf
521 );
522
523 // 1/d(Sum Pv + lambda_v Nv) = Qf
524 // --> Pf + 1/d(Sum lambda_v Nv) = Qf
525 // --> Sum (1/d lambda_v Nv) = Qf - Pf
526
2/2
✓ Branch 0 taken 209139 times.
✓ Branch 1 taken 69713 times.
278852 for(index_t c=0; c<3; ++c) {
527
1/2
✓ Branch 1 taken 209139 times.
✗ Branch 2 not taken.
209139 nlBegin(NL_ROW);
528
2/2
✓ Branch 0 taken 627417 times.
✓ Branch 1 taken 209139 times.
836556 for(index_t lv=0; lv<d; ++lv) {
529 index_t v = surface.facets.vertex(f,lv);
530
1/2
✓ Branch 1 taken 627417 times.
✗ Branch 2 not taken.
627417 nlCoefficient(v,Nv[v][c]/double(d));
531 }
532
1/2
✓ Branch 1 taken 209139 times.
✗ Branch 2 not taken.
209139 nlRightHandSide(Qf[c]-Pf[c]);
533
1/2
✓ Branch 1 taken 209139 times.
✗ Branch 2 not taken.
209139 nlEnd(NL_ROW);
534 }
535 }
536
537 // For each edge on the border,
538 // makes the center of the edge attracted by the
539 // nearest point on the ribbon along the averated directions
540 // of the two vertices of the edge
541
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if(nb_v_on_border != 0 && reference_has_borders) {
542
2/2
✓ Branch 0 taken 19713 times.
✓ Branch 1 taken 2 times.
19715 for(index_t f: surface.facets) {
543
2/2
✓ Branch 0 taken 59139 times.
✓ Branch 1 taken 19713 times.
78852 for(index_t c1: surface.facets.corners(f)) {
544
2/2
✓ Branch 0 taken 279 times.
✓ Branch 1 taken 58860 times.
59139 if(surface.facet_corners.adjacent_facet(c1) == NO_FACET) {
545 index_t c2 = surface.facets.next_corner_around_facet(
546 f, c1
547 );
548 index_t v1 = surface.facet_corners.vertex(c1);
549 index_t v2 = surface.facet_corners.vertex(c2);
550 const vec3& p1 = surface.vertices.point(v1);
551 const vec3& p2 = surface.vertices.point(v2);
552 vec3 p = 0.5*(p1+p2);
553 vec3 N = 0.5*(Nv[v1] + Nv[v2]);
554 279 vec3 q = nearest_along_bidirectional_ray(
555 ✗ border_ribbon_AABB, Ray(p, N),
556 279 border_distance_factor*max_edge_distance*
557
1/2
✓ Branch 1 taken 279 times.
✗ Branch 2 not taken.
279 0.5*(Lv[v1]+Lv[v2]),
558 true
559 );
560
561 // 1/2(p1 + p2 + lambda_1 N1 + lambda_2 N2) = q
562 // --> p + 1/2(lambda_1 N1 + lambda_2 N2) = q
563 // --> (1/2 lambda_1 N1 + 1/2 lambda_2 N2) = q - p
564
2/2
✓ Branch 0 taken 837 times.
✓ Branch 1 taken 279 times.
1116 for(index_t c=0; c<3; ++c) {
565
1/2
✓ Branch 1 taken 837 times.
✗ Branch 2 not taken.
837 nlRowScaling(0.5*border_importance);
566
1/2
✓ Branch 1 taken 837 times.
✗ Branch 2 not taken.
837 nlBegin(NL_ROW);
567
1/2
✓ Branch 1 taken 837 times.
✗ Branch 2 not taken.
837 nlCoefficient(v1,0.5*N[c]);
568 nlCoefficient(v2,0.5*N[c]);
569
1/2
✓ Branch 1 taken 837 times.
✗ Branch 2 not taken.
837 nlRightHandSide(q[c]-p[c]);
570
1/2
✓ Branch 1 taken 837 times.
✗ Branch 2 not taken.
837 nlEnd(NL_ROW);
571 }
572
573 }
574 }
575 }
576 }
577
578
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlEnd(NL_MATRIX);
579
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlEnd(NL_SYSTEM);
580
581
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 nlSolve();
582
583 // Displace each vertex v along Nv[v] by
584 // the solution of the least squares problem
585 // at v
586
2/2
✓ Branch 0 taken 34998 times.
✓ Branch 1 taken 7 times.
35005 for(index_t v: surface.vertices) {
587 vec3& p = surface.vertices.point(v);
588 p += nlGetVariable(v)*Nv[v];
589 }
590
591 // (Brutally) project border vertices
592
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
7 if(project_borders && nb_v_on_border != 0 && reference_has_borders) {
593 ✗ for(index_t v: surface.vertices) {
594 ✗ if(v_on_border[v]) {
595 ✗ vec3 p = surface.vertices.point(v);
596 ✗ vec3 q = nearest_along_bidirectional_ray(
597 ✗ border_ribbon_AABB, Ray(p, Nv[v]),
598 ✗ border_distance_factor*max_edge_distance*0.5*(Lv[v]),
599 true
600 );
601 ✗ surface.vertices.point(v) = q;
602 }
603 }
604 }
605
606
2/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 nlDeleteContext(nlGetCurrent());
607 14 }
608 }
609