40#ifndef H_GEO_MESH_CSG_BUILDER_H
41#define H_GEO_MESH_CSG_BUILDER_H
44#include <geogram/mesh/mesh_CSG_utils.h>
68 static constexpr double DEFAULT_FA = 12.0;
71 static constexpr double DEFAULT_FS = 2.0;
74 static constexpr double DEFAULT_FN = 0.0;
104 fn_ = std::max(fn, 0.0);
114 fs_ = std::max(fs,0.01);
124 fa_ = std::max(fa,0.01);
145 detailed_verbose_ = x;
146 if(detailed_verbose_ && !verbose_) {
168 file_path_.push_back(path);
177 file_path_.push_back(std::filesystem::current_path());
187 file_path_.push_back(path);
195 file_path_.pop_back();
200 virtual void add_object(
201 const std::string&
object,
const ArgList& args
204 virtual void begin_instruction();
206 virtual void end_instruction(
207 const std::string& instruction,
const ArgList& args
211 bool is_object(
const std::string&
id)
const {
212 return (object_funcs_.find(
id) != object_funcs_.end());
215 bool is_instruction(
const std::string&
id)
const {
216 return (instruction_funcs_.find(
id) != instruction_funcs_.end());
223 virtual void add_square(
const ArgList& args);
224 virtual void add_circle(
const ArgList& args);
225 virtual void add_cube(
const ArgList& args);
226 virtual void add_sphere(
const ArgList& args);
227 virtual void add_cylinder(
const ArgList& args);
228 virtual void add_polyhedron(
const ArgList& args);
229 virtual void add_polygon(
const ArgList& args);
230 virtual void add_import(
const ArgList& args);
231 virtual void add_surface(
const ArgList& args);
232 virtual void add_text(
const ArgList& args);
236 virtual void eval_multmatrix(
const ArgList& args);
237 virtual void eval_resize(
const ArgList& args);
238 virtual void eval_union(
const ArgList& args);
239 virtual void eval_intersection(
const ArgList& args);
240 virtual void eval_difference(
const ArgList& args);
241 virtual void eval_group(
const ArgList& args);
242 virtual void eval_color(
const ArgList& args);
243 virtual void eval_hull(
const ArgList& args);
244 virtual void eval_linear_extrude(
const ArgList& args);
245 virtual void eval_rotate_extrude(
const ArgList& args);
246 virtual void eval_projection(
const ArgList& args);
247 virtual void eval_minkowski(
const ArgList& args);
248 virtual void eval_render(
const ArgList& args);
252 [[noreturn]]
void error(
const char* str) {
253 throw(std::logic_error(str));
256 [[noreturn]]
void error(
const std::string& str) {
257 throw(std::logic_error(str.c_str()));
264 std::vector<std::filesystem::path> file_path_;
267 bool detailed_verbose_;
269 typedef std::function<void(
const ArgList& args)> csg_builder_func;
270 std::map<std::string, csg_builder_func> object_funcs_;
271 std::map<std::string, csg_builder_func> instruction_funcs_;
281 typedef std::vector<std::shared_ptr<Mesh>>
CSGScope;
295 virtual std::shared_ptr<Mesh> square(
296 vec2 size =
vec2(1.0,1.0),
bool center=
true
305 virtual std::shared_ptr<Mesh> cube(
306 vec3 size =
vec3(1.0, 1.0, 1.0),
bool center=
true
309 virtual std::shared_ptr<Mesh> sphere(
double r=1.0);
311 virtual std::shared_ptr<Mesh> cylinder(
312 double h=1.0,
double r1=1.0,
double r2=1.0,
bool center=
true
315 virtual std::shared_ptr<Mesh>
import(
316 const std::filesystem::path& filename,
const std::string& layer=
"",
321 virtual std::shared_ptr<Mesh> surface(
322 const std::filesystem::path& filename,
bool center,
bool invert
325 virtual std::shared_ptr<Mesh> text(
326 const std::string& text,
328 const std::string& font =
"",
329 const std::string& halign =
"left",
330 const std::string& valign =
"baseline",
331 double spacing = 1.0,
332 const std::string& direction =
"ltr",
333 const std::string& language =
"en",
334 const std::string& script =
"latin"
422 const CSGScope& scope,
double angle = 360.0
447 void add_object(
const std::string&
object,
const ArgList& args)
override;
448 void begin_instruction()
override;
449 void end_instruction(
450 const std::string& instruction,
const ArgList& args
455 void add_square(
const ArgList& args)
override;
456 void add_circle(
const ArgList& args)
override;
457 void add_cube(
const ArgList& args)
override;
458 void add_sphere(
const ArgList& args)
override;
459 void add_cylinder(
const ArgList& args)
override;
460 void add_polyhedron(
const ArgList& args)
override;
461 void add_polygon(
const ArgList& args)
override;
462 void add_import(
const ArgList& args)
override;
463 void add_surface(
const ArgList& args)
override;
464 void add_text(
const ArgList& args)
override;
469 void eval_multmatrix(
const ArgList& args)
override;
470 void eval_resize(
const ArgList& args)
override;
471 void eval_union(
const ArgList& args)
override;
472 void eval_intersection(
const ArgList& args)
override;
473 void eval_difference(
const ArgList& args)
override;
474 void eval_group(
const ArgList& args)
override;
475 void eval_color(
const ArgList& args)
override;
476 void eval_hull(
const ArgList& args)
override;
477 void eval_linear_extrude(
const ArgList& args)
override;
478 void eval_rotate_extrude(
const ArgList& args)
override;
479 void eval_projection(
const ArgList& args)
override;
480 void eval_minkowski(
const ArgList& args)
override;
481 void eval_render(
const ArgList& args)
override;
501 detect_intersecting_neighbors_ = x;
514 simplify_coplanar_facets_ = x;
515 coplanar_angle_tolerance_ = angle_tolerance;
559 const std::shared_ptr<Mesh>& mesh
562 return std::make_pair(
vec3(result.xyz_min),
vec3(result.xyz_max));
567 std::shared_ptr<Mesh> surface_with_OpenSCAD(
568 const std::filesystem::path& filename,
bool center,
bool invert
571 std::shared_ptr<Mesh> text_with_OpenSCAD(
572 const std::string& text,
574 const std::string& font =
"",
575 const std::string& halign =
"left",
576 const std::string& valign =
"baseline",
577 double spacing = 1.0,
578 const std::string& direction =
"ltr",
579 const std::string& language =
"en",
580 const std::string& script =
"latin"
600 return *(file_path_.rbegin());
609 const std::filesystem::path& filename,
const std::string& layer=
"",
629 std::shared_ptr<Mesh>& mesh,
const std::string& boolean_expr
656 std::shared_ptr<Mesh>& mesh,
const std::string& boolean_expr
683 return scope_stack_.top();
687 scope_stack_.emplace();
698 bool detect_intersecting_neighbors_;
700 bool simplify_coplanar_facets_;
701 double coplanar_angle_tolerance_;
704 std::shared_ptr<Mesh> empty_mesh_;
705 std::shared_ptr<Mesh> result_;
706 std::stack<CSGScope> scope_stack_;
708 friend class CSGCompiler;
#define geo_assert(x)
Verifies that a condition is met.
#define geo_debug_assert(x)
Verifies that a condition is met.
A parsed argument list in a .csg file.
Base class for implementing CSG objects and instructions.
AbstractCSGBuilder()
AbstractCSGBuilder constructor.
void set_detailed_verbose(bool x)
Displays (even more) additional information.
void reset_defaults()
Resets defaults value for fn, fs, fa.
void reset_file_path()
Resets the file path to its default value, with only the current directory ".".
bool verbose() const
Tests wheter verbose mode is set.
void set_verbose(bool x)
Displays (lots of) additional information.
void set_fa(double fa)
Sets the minimum angle for a fragment.
void push_file_path(const std::filesystem::path &path)
Adds a path to the file path.
void set_fn(double fn)
Sets the number of fragments.
void pop_file_path()
Removes the latest pushed file path.
virtual ~AbstractCSGBuilder()
AbstractCSGBuilder destructor.
void set_fs(double fs)
Sets the minimum size for a fragment.
void add_file_path(const std::filesystem::path &path)
Adds a path to the file path.
Axis-aligned bounding box.
Implements CSG objects and instructions.
void set_delaunay(bool x)
If set, compute constrained Delaunay triangulation in the intersected triangles. If there are interse...
virtual std::shared_ptr< Mesh > rotate_extrude(const CSGScope &scope, double angle=360.0)
Computes a 3D extrusion from a 2D shape.
std::shared_ptr< Mesh > import_with_openSCAD(const std::filesystem::path &filename, const std::string &layer="", index_t timestamp=0)
For the file formats that are not supported by geogram, get help from OpenSCAD to convert them.
virtual void do_CSG(std::shared_ptr< Mesh > &mesh, const std::string &boolean_expr)
Apply a CSG operation to a mesh.
virtual std::shared_ptr< Mesh > intersection(const CSGScope &scope)
Computes the intersection between two or more meshes.
virtual std::shared_ptr< Mesh > projection(const CSGScope &scope, bool cut)
Creates a 2D mesh from 3D mesh.
bool find_file(std::filesystem::path &filename)
Finds a file in the path.
virtual std::shared_ptr< Mesh > union_instr(const CSGScope &scope)
Computes the union of two or more meshes.
virtual void triangulate(std::shared_ptr< Mesh > &mesh)
Triangulates a 2D mesh.
void set_fast_union(bool x)
Sets fast union mode.
static Box3d get_bbox(const std::shared_ptr< Mesh > &mesh)
Computes the bounding box of a mesh.
virtual std::shared_ptr< Mesh > linear_extrude(const CSGScope &scope, double height=1.0, bool center=false, vec2 scale=vec2(1.0, 1.0), index_t slices=0, double twist=0.0)
Computes a 3D extrusion from a 2D shape.
virtual std::shared_ptr< Mesh > hull(const CSGScope &scope)
Computes the convex hull of several meshes.
void set_detect_intersecting_neighbors(bool x)
detect and compute intersections between facets that share a facet or an edge. Set to false if input ...
void set_simplify_coplanar_facets(bool x, double angle_tolerance=0.0)
Specifies whether coplanar facets should be simplified.
virtual std::shared_ptr< Mesh > minkowski(const CSGScope &scope)
Computes the Minkowski sum of meshes.
void set_noop(bool x)
Sets noop mode.
void keep_z0_only(std::shared_ptr< Mesh > &M)
keeps only triangles and vertices embedded in the z=0 plane, and makes the mesh 2D.
virtual std::shared_ptr< Mesh > circle(double r=1.0, index_t nu=0)
virtual std::shared_ptr< Mesh > append(const CSGScope &scope)
Appends all meshes in scope into a unique mesh, without testing for intersections.
virtual void finalize_mesh(std::shared_ptr< Mesh > &mesh)
Derived classes may override this function and compute some cached information, e....
virtual std::shared_ptr< Mesh > color(vec4 color, const CSGScope &scope)
Groups several meshes into a single one and sets their color.
virtual std::shared_ptr< Mesh > group(const CSGScope &scope)
synonym for union.
virtual std::shared_ptr< Mesh > multmatrix(const mat4 &M, const CSGScope &scope)
Groups several meshes into a single one and transforms them.
static std::pair< vec3, vec3 > get_bbox_bounds(const std::shared_ptr< Mesh > &mesh)
Computes the bounding box of a mesh.
virtual void triangulate(std::shared_ptr< Mesh > &mesh, const std::string &boolean_expr)
Triangulates a 2D mesh.
virtual std::shared_ptr< Mesh > difference(const CSGScope &scope)
Computes the intersection between two meshes.
const std::filesystem::path & current_path()
Gets the current path.
Common include file, providing basic definitions. Should be included before anything else by all head...
Global Vorpaline namespace.
void get_bbox(const Mesh &M, double *xyzmin, double *xyzmax)
Gets the bounding box of a mesh.
std::vector< std::shared_ptr< Mesh > > CSGScope
A Scope corresponds to a set of primitive between curly braces in OpenSCAD, arguments of an operation...
geo_index_t index_t
The type for storing and manipulating indices.
A parsed value in a .csg file.