Skip to content

Silence logs and light refactoring #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@ include_directories(${CMAKE_SOURCE_DIR}/src)
find_package(pybind11 REQUIRED)

# Add the Python module
add_library(loop_cgal MODULE
add_library(_loop_cgal MODULE
loop_cgal/bindings.cpp
src/clip.cpp
src/mesh.cpp
src/meshutils.cpp
src/globals.cpp

)
target_link_libraries(loop_cgal PRIVATE pybind11::module CGAL::CGAL)
target_include_directories(loop_cgal PRIVATE ${CMAKE_SOURCE_DIR}/src)
set_target_properties(loop_cgal PROPERTIES PREFIX "" SUFFIX ".so")
target_link_libraries(_loop_cgal PRIVATE pybind11::module CGAL::CGAL)
target_include_directories(_loop_cgal PRIVATE ${CMAKE_SOURCE_DIR}/src)
set_target_properties(_loop_cgal PROPERTIES PREFIX "" SUFFIX ".so")
# Install the Python module to the correct location
install(TARGETS loop_cgal
install(TARGETS _loop_cgal
LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/loop_cgal
)
8 changes: 3 additions & 5 deletions examples/clip_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import numpy as np
import pyvista as pv
from LoopStructural.datatypes import BoundingBox
loop_cgal.set_verbose(True)
print(loop_cgal.verbose) # Should print True
def test():
bb = BoundingBox(np.zeros(3), np.ones(3))
grid = bb.structured_grid().vtk()
print(grid)
grid["scalars"] = grid.points[:,0]
surface = grid.contour([.5])
surface_1_tri = surface.faces.reshape(-1, 4)[:, 1:].copy()
Expand All @@ -18,12 +19,9 @@ def test():
# surface_cgal.remesh(target_edge_length=0.02, verbose=True,protect_constraints=True, relax_constraints=False, number_of_iterations=1,split_long_edges=False)
surface_cgal_2 = loop_cgal.TriMesh(surface_2)
# 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)
print("clipping 1")
surface_cgal.cut_with_surface(surface_cgal_2,verbose=True, preserve_intersection=False, preserve_intersection_clipper=False)
surface_cgal.cut_with_surface(surface_cgal_2, preserve_intersection=False, preserve_intersection_clipper=False)

print("reverse face orientation")
surface_cgal_2.reverse_face_orientation()
print("clipping 3")
surface_cgal_3 = loop_cgal.TriMesh(surface)
# 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)
surface_cgal_3.cut_with_surface(surface_cgal_2)
Expand Down
30 changes: 13 additions & 17 deletions loop_cgal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from .loop_cgal import clip_surface, NumpyMesh, NumpyPlane, clip_plane, corefine_mesh
from .loop_cgal import TriMesh as _TriMesh
import pyvista as pv
import numpy as np
from __future__ import annotations

from typing import Tuple

import numpy as np
import pyvista as pv

from ._loop_cgal import NumpyMesh, NumpyPlane, clip_plane, clip_surface, corefine_mesh
from ._loop_cgal import TriMesh as _TriMesh
from ._loop_cgal import verbose
from ._loop_cgal import set_verbose as set_verbose
class TriMesh(_TriMesh):
"""
A class for handling triangular meshes using CGAL.
Expand All @@ -17,7 +22,7 @@ def __init__(self, surface: pv.PolyData):

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
duplicate_vertex_threshold: float = 1e-4, # this is the threshold for duplicate vertices
verbose: bool = False) -> pv.PolyData:
) -> pv.PolyData:
"""
Convert the TriMesh to a pyvista PolyData object.

Expand All @@ -26,7 +31,7 @@ def to_pyvista(self, area_threshold: float = 1e-6, # this is the area threshold
pyvista.PolyData
The converted PolyData object.
"""
np_mesh = self.save(area_threshold, duplicate_vertex_threshold, verbose)
np_mesh = self.save(area_threshold, duplicate_vertex_threshold)
vertices = np.array(np_mesh.vertices).copy()
triangles = np.array(np_mesh.triangles).copy()
return pv.PolyData.from_regular_faces(vertices, triangles)
Expand All @@ -43,7 +48,6 @@ def clip_pyvista_polydata_with_plane(
area_threshold: float = 0.0001,
protect_constraints: bool = False,
relax_constraints: bool = True,
verbose: bool = False,
) -> pv.PolyData:
"""
Clip a pyvista PolyData object with a plane using the CGAL library.
Expand All @@ -68,8 +72,7 @@ def clip_pyvista_polydata_with_plane(
The threshold for merging duplicate vertices, by default 0.001
area_threshold : float, optional
The area threshold for removing small faces, by default 0.0001
verbose : bool, optional
Whether to print verbose output, by default False

Returns
-------
pyvista.PolyData
Expand All @@ -94,7 +97,6 @@ def clip_pyvista_polydata_with_plane(
area_threshold=area_threshold,
protect_constraints=protect_constraints,
relax_constraints=relax_constraints,
verbose=verbose,
)
return pv.PolyData.from_regular_faces(mesh.vertices, mesh.triangles)

Expand All @@ -110,7 +112,6 @@ def clip_pyvista_polydata(
area_threshold: float = 0.0001,
protect_constraints: bool = False,
relax_constraints: bool = True,
verbose: bool = False,
) -> pv.PolyData:
"""
Clip two pyvista PolyData objects using the CGAL library.
Expand Down Expand Up @@ -146,11 +147,8 @@ def clip_pyvista_polydata(
area_threshold=area_threshold,
protect_constraints=protect_constraints,
relax_constraints=relax_constraints,
verbose=verbose,
)
out = pv.PolyData.from_regular_faces(mesh.vertices, mesh.triangles)

return out
return pv.PolyData.from_regular_faces(mesh.vertices, mesh.triangles)


def corefine_pyvista_polydata(
Expand All @@ -162,7 +160,6 @@ def corefine_pyvista_polydata(
number_of_iterations: int = 10,
protect_constraints: bool = True,
relax_constraints: bool = True,
verbose: bool = False,
) -> Tuple[pv.PolyData, pv.PolyData]:
"""
Corefine two pyvista PolyData objects using the CGAL library.
Expand Down Expand Up @@ -197,7 +194,6 @@ def corefine_pyvista_polydata(
number_of_iterations=number_of_iterations,
relax_constraints=relax_constraints,
protect_constraints=protect_constraints,
verbose=verbose,
)
return (
pv.PolyData.from_regular_faces(tm1.vertices, tm1.triangles),
Expand Down
12 changes: 5 additions & 7 deletions loop_cgal/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
#include "clip.h" // Include the API implementation
#include "mesh.h"
#include "numpymesh.h"

#include "globals.h" // Include the global verbose flag
namespace py = pybind11;

PYBIND11_MODULE(loop_cgal, m)
PYBIND11_MODULE(_loop_cgal, m)
{

m.attr("verbose") = &LoopCGAL::verbose; // Expose the global verbose flag
m.def("set_verbose", &LoopCGAL::set_verbose, "Set the verbose flag");
m.def("clip_surface", &clip_surface, py::arg("tm"), py::arg("clipper"),
py::arg("target_edge_length") = 10.0,
py::arg("remesh_before_clipping") = true,
Expand Down Expand Up @@ -49,18 +50,15 @@ PYBIND11_MODULE(loop_cgal, m)
.def(py::init<const pybind11::array_t<double> &, const pybind11::array_t<int> &>(),
py::arg("vertices"), py::arg("triangles"))
.def("cut_with_surface", &TriMesh::cutWithSurface, py::arg("surface"),
py::arg("verbose") = false,
py::arg("preserve_intersection") = false,
py::arg("preserve_intersection_clipper") = false)
.def("remesh", &TriMesh::remesh, py::arg("split_long_edges") = true,
py::arg("verbose") = false,
py::arg("target_edge_length") = 10.0,
py::arg("number_of_iterations") = 3,
py::arg("protect_constraints") = true,
py::arg("relax_constraints") = false)
.def("save", &TriMesh::save, py::arg("area_threshold") = 1e-6,
py::arg("duplicate_vertex_threshold") = 1e-6,
py::arg("verbose") = false)
py::arg("duplicate_vertex_threshold") = 1e-6)
.def("reverse_face_orientation", &TriMesh::reverseFaceOrientation,
"Reverse the face orientation of the mesh.")
.def("add_fixed_edges", &TriMesh::add_fixed_edges,
Expand Down
Loading