GCC Code Coverage Report


Directory: ./
File: examples/geogram/opennl_mesh_smooth/main.cpp
Date: 2026-09-07 02:37:58
Exec Total Coverage
Lines: 0 107 0.0%
Functions: 0 2 0.0%
Branches: 0 258 0.0%

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/basic/common.h>
41 #include <geogram/basic/logger.h>
42 #include <geogram/basic/command_line.h>
43 #include <geogram/basic/command_line_args.h>
44 #include <geogram/basic/stopwatch.h>
45 #include <geogram/basic/file_system.h>
46 #include <geogram/mesh/mesh.h>
47 #include <geogram/mesh/mesh_subdivision.h>
48 #include <geogram/mesh/mesh_io.h>
49 #include <geogram/NL/nl.h>
50
51 namespace {
52 using namespace GEO;
53
54
55 void mesh_smooth(Mesh* M, NLenum solver = NL_SOLVER_DEFAULT) {
56
57 // Chain corners around vertices
58 vector<index_t> v2c(M->vertices.nb(), index_t(-1));
59 vector<index_t> next_c_around_v(M->facet_corners.nb(), index_t(-1));
60 vector<index_t> c2f(M->facet_corners.nb(), index_t(-1));
61 for(index_t f=0; f<M->facets.nb(); ++f) {
62 for(index_t c=M->facets.corners_begin(f);
63 c<M->facets.corners_end(f); ++c
64 ) {
65 index_t v = M->facet_corners.vertex(c);
66 next_c_around_v[c] = v2c[v];
67 v2c[v] = c;
68 c2f[c] = f;
69 }
70 }
71
72 nlNewContext();
73
74 if(solver == NL_SUPERLU_EXT || solver == NL_PERM_SUPERLU_EXT) {
75 if(nlInitExtension("SUPERLU")) {
76 nlSolverParameteri(NL_SOLVER, NLint(solver));
77 } else {
78 Logger::err("NL") << "Could not init SUPERLU extension";
79 }
80 } else if(solver == NL_CHOLMOD_EXT) {
81 if(nlInitExtension("CHOLMOD")) {
82 nlSolverParameteri(NL_SOLVER, NLint(solver));
83 } else {
84 Logger::err("NL") << "Could not init CHOLMOD extension";
85 }
86 }
87
88 nlSolverParameteri(NL_LEAST_SQUARES, NL_TRUE);
89 nlSolverParameteri(NL_NB_VARIABLES, NLint(M->vertices.nb()));
90 nlSolverParameteri(NL_NB_SYSTEMS, NLint(M->vertices.dimension()));
91 nlEnable(NL_NORMALIZE_ROWS);
92 nlEnable(NL_VARIABLES_BUFFER);
93
94 Attribute<bool> v_is_locked(M->vertices.attributes(), "selection");
95
96 nlBegin(NL_SYSTEM);
97
98 for(index_t coord=0; coord<M->vertices.dimension(); ++coord) {
99 // Bind directly the variables buffer to the coordinates in
100 // the mesh, to avoid copying data.
101 nlBindBuffer(
102 NL_VARIABLES_BUFFER, NLuint(coord),
103 M->vertices.point_ptr(0) + coord,
104 NLuint(sizeof(double)*M->vertices.dimension())
105 );
106 }
107
108 for(index_t v=0; v<M->vertices.nb(); ++v) {
109 if(v_is_locked[v]) {
110 nlLockVariable(v);
111 }
112 }
113
114 nlBegin(NL_MATRIX);
115 for(index_t v=0; v<M->vertices.nb(); ++v) {
116 nlBegin(NL_ROW);
117 index_t count = 0;
118 for(
119 index_t c = v2c[v];
120 c != index_t(-1); c = next_c_around_v[c]
121 ) {
122 index_t f = c2f[c];
123 index_t c2 = M->facets.next_corner_around_facet(f,c);
124 index_t w = M->facet_corners.vertex(c2);
125 nlCoefficient(w, 1.0);
126 ++count;
127 }
128 nlCoefficient(v, -double(count));
129 nlEnd(NL_ROW);
130 }
131 nlEnd(NL_MATRIX);
132 nlEnd(NL_SYSTEM);
133
134 Logger::div("Solve");
135 nlSolve();
136
137 nlDeleteContext(nlGetCurrent());
138 }
139 }
140
141 int main(int argc, char** argv) {
142 using namespace GEO;
143
144 GEO::initialize(GEO::GEOGRAM_INSTALL_ALL);
145
146 try {
147 Stopwatch Wtot("Total time");
148
149 std::vector<std::string> filenames;
150
151 CmdLine::import_arg_group("standard");
152 CmdLine::import_arg_group("algo");
153 CmdLine::declare_arg(
154 "solver", "NL_SOLVER_DEFAULT", "solver"
155 );
156 CmdLine::declare_arg(
157 "nb_subdivide", 2, "number of times mesh is subdivided"
158 );
159
160 if(
161 !CmdLine::parse(
162 argc, argv, filenames, "inmesh <outmesh>"
163 )
164 ) {
165 return 1;
166 }
167
168
169 if(filenames.size() != 2) {
170 Logger::out("Smooth") << "Generating output to out.geogram"
171 << std::endl;
172 filenames.push_back("out.geogram");
173 }
174
175 Logger::div("Data I/O");
176
177 Mesh M;
178
179 MeshIOFlags flags;
180 flags.reset_element(MESH_CELLS);
181 flags.set_attributes(MESH_ALL_ATTRIBUTES);
182 if(!mesh_load(filenames[0], M, flags)) {
183 return 1;
184 }
185
186 {
187 Attribute<bool> is_locked(M.vertices.attributes(), "selection");
188 for(index_t v=0; v<M.vertices.nb(); ++v) {
189 is_locked[v] = true;
190 }
191 for(index_t i=0; i<CmdLine::get_arg_uint("nb_subdivide"); ++i) {
192 mesh_split_triangles(M);
193 }
194 }
195
196 NLenum solver = NL_SOLVER_DEFAULT;
197 std::string solver_string = CmdLine::get_arg("solver");
198 if(solver_string == "NL_CG") {
199 solver = NL_CG;
200 } else if(solver_string == "NL_SUPERLU_EXT") {
201 solver = NL_SUPERLU_EXT;
202 } else if(solver_string == "NL_PERM_SUPERLU_EXT") {
203 solver = NL_PERM_SUPERLU_EXT;
204 } else if(solver_string == "NL_SYMMETRIC_SUPERLU_EXT") {
205 solver = NL_SYMMETRIC_SUPERLU_EXT;
206 } else if(solver_string == "NL_CHOLMOD_EXT") {
207 solver = NL_CHOLMOD_EXT;
208 }
209
210 mesh_smooth(&M, solver);
211
212 if(!mesh_save(M, filenames[1], flags)) {
213 return 1;
214 }
215
216 }
217 catch(const std::exception& e) {
218 std::cerr << "Received an exception: " << e.what() << std::endl;
219 return 1;
220 }
221
222 Logger::out("") << "Everything OK, Returning status 0" << std::endl;
223 return 0;
224 }
225