Geogram Version 1.10.1
A programming library of geometric algorithms
Loading...
Searching...
No Matches
mesh_CSG_utils.h
1/*
2 * Copyright (c) 2000-2025 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#ifndef H_GEOGRAM_MESH_MESH_CSG_UTILS_H
41#define H_GEOGRAM_MESH_MESH_CSG_UTILS_H
42
44#include <geogram/mesh/mesh.h>
45#include <string>
46#include <filesystem>
47#include <memory>
48
49namespace GEOCSG {
50
51 using namespace GEO;
52
53 /**********************************************************************/
54 /**** Values, ArgList ****/
55 /**********************************************************************/
56
62 struct Value {
63 enum Type {NONE, NUMBER, BOOLEAN, ARRAY1D, ARRAY2D, STRING, PATH};
64
65 Value();
66 Value(double x);
67 Value(int x);
68 Value(bool x);
69 Value(const std::string& x);
70 Value(const std::filesystem::path& x);
71 std::string to_string() const;
72
73 Type type;
74 bool boolean_val;
75 double number_val;
76 vector<vector<double> > array_val;
77 std::string string_val;
78 };
79
84 class ArgList {
85 public:
86 typedef std::pair<std::string, Value> Arg;
87
92
96 void clear() {
97 args_.clear();
98 }
99
104 index_t size() const {
105 return args_.size();
106 }
107
113 const std::string& ith_arg_name(index_t i) const {
114 geo_debug_assert(i < size());
115 return args_[i].first;
116 }
117
123 const Value& ith_arg_val(index_t i) const {
124 geo_debug_assert(i < size());
125 return args_[i].second;
126 }
127
141 const std::string& name, index_t pos_fallback = NO_INDEX,
142 Value::Type type = Value::NONE
143 ) const;
144
145 void add_arg(const std::string& name, const Value& value);
146
157 const std::string& name, index_t pos_fallback = NO_INDEX
158 ) const;
159
173 double get_arg(
174 const std::string& name, double default_val,
175 index_t pos_fallback = NO_INDEX
176 ) const;
177
192 const std::string& name, int default_val,
193 index_t pos_fallback = NO_INDEX
194 ) const;
195
210 const std::string& name, bool default_val,
211 index_t pos_fallback = NO_INDEX
212 ) const;
213
228 const std::string& name, vec2 default_val,
229 index_t pos_fallback = NO_INDEX
230 ) const;
231
246 const std::string& name, vec3 default_val,
247 index_t pos_fallback = NO_INDEX
248 ) const;
249
264 const std::string& name, vec4 default_val,
265 index_t pos_fallback = NO_INDEX
266 ) const;
267
282 const std::string& name, const mat4& default_val,
283 index_t pos_fallback = NO_INDEX
284 ) const;
285
299 std::string get_arg(
300 const std::string& name, const std::string& default_val,
301 index_t pos_fallback = NO_INDEX
302 ) const;
303
317 std::string get_arg(
318 const std::string& name, const char* default_val,
319 index_t pos_fallback = NO_INDEX
320 ) const {
321 return get_arg(name, std::string(default_val), pos_fallback);
322 }
323
324 protected:
330 static std::string unnamed_arg_name(index_t i) {
331 return "$unnamed_" + String::to_string(i);
332 }
333 private:
334 vector<Arg> args_;
335 index_t nb_unnamed_;
336 };
337
338 /**********************************************************************/
339 /**** General sweeping function ****/
340 /**********************************************************************/
341
345 enum SweepCapping {
346 SWEEP_CAP,
347 SWEEP_POLE,
348 SWEEP_PERIODIC
349 };
350
372 void GEOGRAM_API sweep(
373 std::shared_ptr<Mesh>& M, index_t nv,
374 std::function<vec3(index_t, index_t)> sweep_path,
375 SweepCapping capping = SWEEP_CAP
376 );
377
383 void GEOGRAM_API keep_z0_only(std::shared_ptr<Mesh>& M);
384
385 /**********************************************************************/
386 /**** Call OpenSCAD for help (and cache result) ****/
387 /**********************************************************************/
388
393 void GEOGRAM_API OpenSCAD_cache_invalidate();
394
401 void GEOGRAM_API OpenSCAD_cache_ignore_time();
402
403 std::shared_ptr<Mesh> GEOGRAM_API call_OpenSCAD(
404 const std::filesystem::path& path, const std::string& command,
405 const ArgList& args, bool TWO_D=false
406 );
407
408 std::string GEOGRAM_API load_OpenSCAD(const std::filesystem::path& filename);
409
410 /**********************************************************************/
411 /**** Functions to estimate number of fragments and slices, ****/
412 /**** taken from OpenSCAD ****/
413 /**********************************************************************/
414
418 int GEOGRAM_API get_fragments_from_r_and_twist(
419 double r, double twist, double fn, double fs, double fa
420 );
421
425 inline int get_fragments_from_r(
426 double r, double fn, double fs, double fa
427 ) {
428 return get_fragments_from_r_and_twist(r, 360.0, fn, fs, fa);
429 }
430
434 int GEOGRAM_API get_linear_extrusion_slices(
435 std::shared_ptr<Mesh> M, double height, vec2 scale, double twist,
436 double fn, double fs, double fa
437 );
438
439}
440
441#endif
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:196
A parsed argument list in a .csg file.
ArgList()
Constructs an empty ArgList.
vec4 get_arg(const std::string &name, vec4 default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type vec4.
vec2 get_arg(const std::string &name, vec2 default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type vec2.
double get_arg(const std::string &name, double default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type double.
std::string get_arg(const std::string &name, const std::string &default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type string.
std::string get_arg(const std::string &name, const char *default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type string.
const Value & ith_arg_val(index_t i) const
Gets the value of an argument.
const Value & get_arg_value(const std::string &name, index_t pos_fallback=NO_INDEX) const
Gets an argument value.
int get_arg(const std::string &name, int default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type int.
vec3 get_arg(const std::string &name, vec3 default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type vec3.
mat4 get_arg(const std::string &name, const mat4 &default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type mat4.
bool has_arg(const std::string &name, index_t pos_fallback=NO_INDEX, Value::Type type=Value::NONE) const
Tests whether this arglist has an argument.
static std::string unnamed_arg_name(index_t i)
Gets the internal name used for an unnamed argument.
const std::string & ith_arg_name(index_t i) const
Gets the name of an argument.
void clear()
Removes all the arguments in this arglist.
bool get_arg(const std::string &name, bool default_val, index_t pos_fallback=NO_INDEX) const
Gets an argument of type bool.
index_t size() const
Gets the number of arguments.
Vector with aligned memory allocation.
Definition memory.h:703
index_t size() const
Gets the number of elements.
Definition memory.h:749
Common include file, providing basic definitions. Should be included before anything else by all head...
The class that represents a mesh.
Global Vorpaline namespace.
Definition basic.h:55
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
Definition geometry.h:65
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:340
A parsed value in a .csg file.