Skip to content

Commit b291e03

Browse files
committed
fix: use global verbose flag instead of per function variable
to set verbose mode on loop_cgal.set_verbose(True)
1 parent c48394a commit b291e03

11 files changed

Lines changed: 167 additions & 117 deletions

File tree

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,18 @@ include_directories(${CMAKE_SOURCE_DIR}/src)
2626
find_package(pybind11 REQUIRED)
2727

2828
# Add the Python module
29-
add_library(loop_cgal MODULE
29+
add_library(_loop_cgal MODULE
3030
loop_cgal/bindings.cpp
3131
src/clip.cpp
3232
src/mesh.cpp
3333
src/meshutils.cpp
34+
src/globals.cpp
3435

3536
)
36-
target_link_libraries(loop_cgal PRIVATE pybind11::module CGAL::CGAL)
37-
target_include_directories(loop_cgal PRIVATE ${CMAKE_SOURCE_DIR}/src)
38-
set_target_properties(loop_cgal PROPERTIES PREFIX "" SUFFIX ".so")
37+
target_link_libraries(_loop_cgal PRIVATE pybind11::module CGAL::CGAL)
38+
target_include_directories(_loop_cgal PRIVATE ${CMAKE_SOURCE_DIR}/src)
39+
set_target_properties(_loop_cgal PROPERTIES PREFIX "" SUFFIX ".so")
3940
# Install the Python module to the correct location
40-
install(TARGETS loop_cgal
41+
install(TARGETS _loop_cgal
4142
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/loop_cgal
4243
)

examples/clip_v2.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
import numpy as np
33
import pyvista as pv
44
from LoopStructural.datatypes import BoundingBox
5+
loop_cgal.set_verbose(True)
6+
print(loop_cgal.verbose) # Should print True
57
def test():
68
bb = BoundingBox(np.zeros(3), np.ones(3))
79
grid = bb.structured_grid().vtk()
8-
print(grid)
910
grid["scalars"] = grid.points[:,0]
1011
surface = grid.contour([.5])
1112
surface_1_tri = surface.faces.reshape(-1, 4)[:, 1:].copy()
@@ -18,12 +19,9 @@ def test():
1819
# surface_cgal.remesh(target_edge_length=0.02, verbose=True,protect_constraints=True, relax_constraints=False, number_of_iterations=1,split_long_edges=False)
1920
surface_cgal_2 = loop_cgal.TriMesh(surface_2)
2021
# surface_cgal_2.remesh(target_edge_length=0.02, verbose=True,protect_constraints=True, relax_constraints=False, number_of_iterations=1,split_long_edges=False)
21-
print("clipping 1")
22-
surface_cgal.cut_with_surface(surface_cgal_2,verbose=True, preserve_intersection=False, preserve_intersection_clipper=False)
22+
surface_cgal.cut_with_surface(surface_cgal_2, preserve_intersection=False, preserve_intersection_clipper=False)
2323

24-
print("reverse face orientation")
2524
surface_cgal_2.reverse_face_orientation()
26-
print("clipping 3")
2725
surface_cgal_3 = loop_cgal.TriMesh(surface)
2826
# surface_cgal_3.remesh(target_edge_length=0.02, verbose=True,protect_constraints=True, relax_constraints=False, number_of_iterations=1,split_long_edges=False)
2927
surface_cgal_3.cut_with_surface(surface_cgal_2)

loop_cgal/__init__.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
import numpy as np
66
import pyvista as pv
77

8-
from .loop_cgal import NumpyMesh, NumpyPlane, clip_plane, clip_surface, corefine_mesh
9-
from .loop_cgal import TriMesh as _TriMesh
10-
8+
from ._loop_cgal import NumpyMesh, NumpyPlane, clip_plane, clip_surface, corefine_mesh
9+
from ._loop_cgal import TriMesh as _TriMesh
10+
from ._loop_cgal import verbose
11+
from ._loop_cgal import set_verbose as set_verbose
1112
class TriMesh(_TriMesh):
1213
"""
1314
A class for handling triangular meshes using CGAL.
@@ -21,7 +22,7 @@ def __init__(self, surface: pv.PolyData):
2122

2223
def to_pyvista(self, area_threshold: float = 1e-6, # this is the area threshold for the faces, if the area is smaller than this it will be removed
2324
duplicate_vertex_threshold: float = 1e-4, # this is the threshold for duplicate vertices
24-
verbose: bool = False) -> pv.PolyData:
25+
) -> pv.PolyData:
2526
"""
2627
Convert the TriMesh to a pyvista PolyData object.
2728
@@ -30,7 +31,7 @@ def to_pyvista(self, area_threshold: float = 1e-6, # this is the area threshold
3031
pyvista.PolyData
3132
The converted PolyData object.
3233
"""
33-
np_mesh = self.save(area_threshold, duplicate_vertex_threshold, verbose)
34+
np_mesh = self.save(area_threshold, duplicate_vertex_threshold)
3435
vertices = np.array(np_mesh.vertices).copy()
3536
triangles = np.array(np_mesh.triangles).copy()
3637
return pv.PolyData.from_regular_faces(vertices, triangles)
@@ -47,7 +48,6 @@ def clip_pyvista_polydata_with_plane(
4748
area_threshold: float = 0.0001,
4849
protect_constraints: bool = False,
4950
relax_constraints: bool = True,
50-
verbose: bool = False,
5151
) -> pv.PolyData:
5252
"""
5353
Clip a pyvista PolyData object with a plane using the CGAL library.
@@ -72,8 +72,7 @@ def clip_pyvista_polydata_with_plane(
7272
The threshold for merging duplicate vertices, by default 0.001
7373
area_threshold : float, optional
7474
The area threshold for removing small faces, by default 0.0001
75-
verbose : bool, optional
76-
Whether to print verbose output, by default False
75+
7776
Returns
7877
-------
7978
pyvista.PolyData
@@ -98,7 +97,6 @@ def clip_pyvista_polydata_with_plane(
9897
area_threshold=area_threshold,
9998
protect_constraints=protect_constraints,
10099
relax_constraints=relax_constraints,
101-
verbose=verbose,
102100
)
103101
return pv.PolyData.from_regular_faces(mesh.vertices, mesh.triangles)
104102

@@ -114,7 +112,6 @@ def clip_pyvista_polydata(
114112
area_threshold: float = 0.0001,
115113
protect_constraints: bool = False,
116114
relax_constraints: bool = True,
117-
verbose: bool = False,
118115
) -> pv.PolyData:
119116
"""
120117
Clip two pyvista PolyData objects using the CGAL library.
@@ -150,7 +147,6 @@ def clip_pyvista_polydata(
150147
area_threshold=area_threshold,
151148
protect_constraints=protect_constraints,
152149
relax_constraints=relax_constraints,
153-
verbose=verbose,
154150
)
155151
return pv.PolyData.from_regular_faces(mesh.vertices, mesh.triangles)
156152

@@ -164,7 +160,6 @@ def corefine_pyvista_polydata(
164160
number_of_iterations: int = 10,
165161
protect_constraints: bool = True,
166162
relax_constraints: bool = True,
167-
verbose: bool = False,
168163
) -> Tuple[pv.PolyData, pv.PolyData]:
169164
"""
170165
Corefine two pyvista PolyData objects using the CGAL library.
@@ -199,7 +194,6 @@ def corefine_pyvista_polydata(
199194
number_of_iterations=number_of_iterations,
200195
relax_constraints=relax_constraints,
201196
protect_constraints=protect_constraints,
202-
verbose=verbose,
203197
)
204198
return (
205199
pv.PolyData.from_regular_faces(tm1.vertices, tm1.triangles),

loop_cgal/bindings.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
#include "clip.h" // Include the API implementation
55
#include "mesh.h"
66
#include "numpymesh.h"
7-
7+
#include "globals.h" // Include the global verbose flag
88
namespace py = pybind11;
99

10-
PYBIND11_MODULE(loop_cgal, m)
10+
PYBIND11_MODULE(_loop_cgal, m)
1111
{
12-
12+
m.attr("verbose") = &LoopCGAL::verbose; // Expose the global verbose flag
13+
m.def("set_verbose", &LoopCGAL::set_verbose, "Set the verbose flag");
1314
m.def("clip_surface", &clip_surface, py::arg("tm"), py::arg("clipper"),
1415
py::arg("target_edge_length") = 10.0,
1516
py::arg("remesh_before_clipping") = true,
@@ -49,18 +50,15 @@ PYBIND11_MODULE(loop_cgal, m)
4950
.def(py::init<const pybind11::array_t<double> &, const pybind11::array_t<int> &>(),
5051
py::arg("vertices"), py::arg("triangles"))
5152
.def("cut_with_surface", &TriMesh::cutWithSurface, py::arg("surface"),
52-
py::arg("verbose") = false,
5353
py::arg("preserve_intersection") = false,
5454
py::arg("preserve_intersection_clipper") = false)
5555
.def("remesh", &TriMesh::remesh, py::arg("split_long_edges") = true,
56-
py::arg("verbose") = false,
5756
py::arg("target_edge_length") = 10.0,
5857
py::arg("number_of_iterations") = 3,
5958
py::arg("protect_constraints") = true,
6059
py::arg("relax_constraints") = false)
6160
.def("save", &TriMesh::save, py::arg("area_threshold") = 1e-6,
62-
py::arg("duplicate_vertex_threshold") = 1e-6,
63-
py::arg("verbose") = false)
61+
py::arg("duplicate_vertex_threshold") = 1e-6)
6462
.def("reverse_face_orientation", &TriMesh::reverseFaceOrientation,
6563
"Reverse the face orientation of the mesh.")
6664
.def("add_fixed_edges", &TriMesh::add_fixed_edges,

src/clip.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ NumpyMesh clip_plane(NumpyMesh tm, NumpyPlane clipper,
271271
// store the result in a numpymesh object for sending back to Python
272272

273273
NumpyMesh result =
274-
export_mesh(_tm, area_threshold, duplicate_vertex_threshold, verbose);
274+
export_mesh(_tm, area_threshold, duplicate_vertex_threshold);
275275
if (verbose) {
276276
std::cout << "Exported clipped mesh with " << result.vertices.shape(0)
277277
<< " vertices and " << result.triangles.shape(0) << " triangles."
@@ -299,7 +299,9 @@ NumpyMesh clip_surface(NumpyMesh tm, NumpyMesh clipper,
299299
if (!CGAL::is_valid_polygon_mesh(_tm, verbose)) {
300300
std::cerr << "tm is invalid!" << std::endl;
301301
if (verbose)
302+
{
302303
CGAL::is_valid_polygon_mesh(_tm, true);
304+
}
303305
}
304306
if (!CGAL::is_valid_polygon_mesh(_clipper, verbose)) {
305307
std::cerr << "clipper is invalid!" << std::endl;
@@ -386,7 +388,9 @@ NumpyMesh clip_surface(NumpyMesh tm, NumpyMesh clipper,
386388
}
387389
} else {
388390
if (verbose)
391+
{
389392
std::cout << "Meshes do not intersect. Returning tm." << std::endl;
393+
}
390394
}
391395
if (verbose) {
392396
std::cout << "Clipping done." << std::endl;
@@ -395,7 +399,7 @@ NumpyMesh clip_surface(NumpyMesh tm, NumpyMesh clipper,
395399
// store the result in a numpymesh object for sending back to Python
396400

397401
NumpyMesh result =
398-
export_mesh(_tm, area_threshold, duplicate_vertex_threshold, verbose);
402+
export_mesh(_tm, area_threshold, duplicate_vertex_threshold);
399403
if (verbose) {
400404
std::cout << "Exported clipped mesh with " << result.vertices.shape(0)
401405
<< " vertices and " << result.triangles.shape(0) << " triangles."
@@ -437,10 +441,11 @@ corefine_mesh(NumpyMesh tm1, NumpyMesh tm2, double target_edge_length,
437441
}
438442
}
439443
if (verbose)
444+
{
440445
std::cout << "Found " << tm_1_shared_edges.size()
441446
<< " shared edges in tm1 and " << tm_2_shared_edges.size()
442447
<< " shared edges in tm2." << std::endl;
443-
448+
}
444449
// std::set<TriangleMesh::Edge_index> constrained_edges;
445450

446451
std::set<TriangleMesh::Edge_index> boundary_edges =
@@ -470,9 +475,10 @@ corefine_mesh(NumpyMesh tm1, NumpyMesh tm2, double target_edge_length,
470475
.protect_constraints(protect_constraints));
471476

472477
if (verbose)
478+
{
473479
std::cout << "Corefinement done." << std::endl;
474-
480+
}
475481
return {
476-
export_mesh(_tm1, area_threshold, duplicate_vertex_threshold, verbose),
477-
export_mesh(_tm2, area_threshold, duplicate_vertex_threshold, verbose)};
482+
export_mesh(_tm1, area_threshold, duplicate_vertex_threshold),
483+
export_mesh(_tm2, area_threshold, duplicate_vertex_threshold)};
478484
}

src/globals.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "globals.h"
2+
#include <iostream>
3+
4+
namespace LoopCGAL
5+
{
6+
bool verbose = false; // Definition of the verbose flag
7+
8+
void set_verbose(bool value)
9+
{
10+
verbose = value;
11+
std::cout << "Verbose flag set to: " << verbose << std::endl;
12+
}
13+
}

src/globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace LoopCGAL
55
{
66
extern bool verbose; // Declaration of the module-wide verbose flag
7+
void set_verbose(bool value); // Declaration of the set_verbose function
78
}
89

910
#endif // GLOBALS_H

0 commit comments

Comments
 (0)