GCC Code Coverage Report


Directory: ./
File: examples/geogram/boolean_operations/main.cpp
Date: 2026-09-07 02:36:43
Exec Total Coverage
Lines: 0 75 0.0%
Functions: 0 2 0.0%
Branches: 0 226 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/mesh/mesh.h>
46 #include <geogram/mesh/mesh_io.h>
47 #include <geogram/mesh/mesh_repair.h>
48 #include <geogram/mesh/mesh_fill_holes.h>
49 #include <geogram/mesh/mesh_surface_intersection.h>
50 #include <geogram/mesh/mesh_geometry.h>
51 #include <geogram/mesh/mesh_remesh.h>
52
53 #include <algorithm>
54
55 namespace {
56 using namespace GEO;
57
58 /**
59 * \brief Pre/Post-processes a mesh.
60 * \details Triangulates the facets, collapses the small edges
61 * and removes self-intersections.
62 */
63 void fix_mesh_for_boolean_ops(Mesh& M) {
64 mesh_repair(
65 M,
66 MeshRepairMode(
67 MESH_REPAIR_COLOCATE | MESH_REPAIR_DUP_F
68 ),
69 1e-3*surface_average_edge_length(M)
70 );
71 tessellate_facets(M,3);
72 mesh_remove_intersections(M);
73 }
74 }
75
76 int main(int argc, char** argv) {
77 using namespace GEO;
78
79 // Needs to be called once.
80 GEO::initialize(GEO::GEOGRAM_INSTALL_ALL);
81
82 try {
83
84 std::vector<std::string> filenames;
85
86 CmdLine::import_arg_group("standard");
87 CmdLine::import_arg_group("algo");
88 CmdLine::declare_arg("pre", false, "pre-process input meshes");
89 CmdLine::declare_arg("post", false, "post-process output mesh");
90 CmdLine::declare_arg(
91 "operation", "union", "one of union,intersection,difference"
92 );
93 CmdLine::declare_arg(
94 "simplify_coplanar_facets",true,"simplify coplanar facets"
95 );
96 CmdLine::declare_arg(
97 "detect_intersecting_neighbors",true,
98 "test also neighboring triangles for intersection"
99 );
100 CmdLine::declare_arg("verbose", false, "display log messages");
101
102 if(
103 !CmdLine::parse(
104 argc, argv, filenames, "meshA meshB <outputfile|none>"
105 )
106 ) {
107 return 1;
108 }
109
110
111 std::string A_filename = filenames[0];
112 std::string B_filename = filenames[1];
113
114 std::string output_filename =
115 filenames.size() >= 3 ? filenames[2] : std::string("out.obj");
116
117 Logger::div("Data I/O");
118
119 Logger::out("I/O") << "Output = " << output_filename << std::endl;
120
121 MeshBooleanOperationFlags flags=MESH_BOOL_OPS_DEFAULT;
122 if(!CmdLine::get_arg_bool("simplify_coplanar_facets")) {
123 flags = MeshBooleanOperationFlags(flags | MESH_BOOL_OPS_NO_SIMPLIFY);
124 }
125 if(CmdLine::get_arg_bool("verbose")) {
126 flags = MeshBooleanOperationFlags(flags | MESH_BOOL_OPS_VERBOSE);
127 }
128 if(!CmdLine::get_arg_bool("detect_intersecting_neighbors")) {
129 flags = MeshBooleanOperationFlags(
130 flags | MESH_BOOL_OPS_NO_CHECK_NEIGHBORS
131 );
132 }
133
134
135
136 Mesh A;
137 Mesh B;
138
139 if(!mesh_load(A_filename,A)) {
140 return 1;
141 }
142
143 if(!mesh_load(B_filename,B)) {
144 return 1;
145 }
146
147 Mesh result;
148
149 if(CmdLine::get_arg_bool("pre")) {
150 Logger::div("Pre-processing");
151 fix_mesh_for_boolean_ops(A);
152 fix_mesh_for_boolean_ops(B);
153 }
154
155 {
156 Stopwatch Wboolean("Booleans");
157 Logger::div("Boolean operation");
158 std::string op = CmdLine::get_arg("operation");
159 if(op == "union") {
160 mesh_union(result, A, B, flags);
161 } else if(op == "intersection") {
162 mesh_intersection(result, A, B, flags);
163 } else if(op == "difference") {
164 mesh_difference(result, A, B, flags);
165 } else {
166 Logger::err("Boolean") << op << ": invalid operation"
167 << std::endl;
168 return 1;
169 }
170 }
171
172 if(CmdLine::get_arg_bool("post")) {
173 Logger::div("Post-processing");
174 fix_mesh_for_boolean_ops(result);
175 }
176
177 Logger::div("Data I/O");
178
179 if(output_filename != "none") {
180 mesh_save(result, output_filename);
181 }
182
183
184 }
185 catch(const std::exception& e) {
186 std::cerr << "Received an exception: " << e.what() << std::endl;
187 return 1;
188 }
189
190 Logger::out("") << "Everything OK, Returning status 0" << std::endl;
191 return 0;
192 }
193