capytaine 3.0.0a1__cp313-cp313-macosx_15_0_x86_64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. capytaine/.dylibs/libgcc_s.1.1.dylib +0 -0
  2. capytaine/.dylibs/libgfortran.5.dylib +0 -0
  3. capytaine/.dylibs/libquadmath.0.dylib +0 -0
  4. capytaine/__about__.py +21 -0
  5. capytaine/__init__.py +32 -0
  6. capytaine/bem/__init__.py +0 -0
  7. capytaine/bem/airy_waves.py +111 -0
  8. capytaine/bem/engines.py +321 -0
  9. capytaine/bem/problems_and_results.py +601 -0
  10. capytaine/bem/solver.py +718 -0
  11. capytaine/bodies/__init__.py +4 -0
  12. capytaine/bodies/bodies.py +630 -0
  13. capytaine/bodies/dofs.py +146 -0
  14. capytaine/bodies/hydrostatics.py +540 -0
  15. capytaine/bodies/multibodies.py +216 -0
  16. capytaine/green_functions/Delhommeau_float32.cpython-313-darwin.so +0 -0
  17. capytaine/green_functions/Delhommeau_float64.cpython-313-darwin.so +0 -0
  18. capytaine/green_functions/__init__.py +2 -0
  19. capytaine/green_functions/abstract_green_function.py +64 -0
  20. capytaine/green_functions/delhommeau.py +522 -0
  21. capytaine/green_functions/hams.py +210 -0
  22. capytaine/io/__init__.py +0 -0
  23. capytaine/io/bemio.py +153 -0
  24. capytaine/io/legacy.py +228 -0
  25. capytaine/io/wamit.py +479 -0
  26. capytaine/io/xarray.py +673 -0
  27. capytaine/meshes/__init__.py +2 -0
  28. capytaine/meshes/abstract_meshes.py +375 -0
  29. capytaine/meshes/clean.py +302 -0
  30. capytaine/meshes/clip.py +347 -0
  31. capytaine/meshes/export.py +89 -0
  32. capytaine/meshes/geometry.py +259 -0
  33. capytaine/meshes/io.py +433 -0
  34. capytaine/meshes/meshes.py +826 -0
  35. capytaine/meshes/predefined/__init__.py +6 -0
  36. capytaine/meshes/predefined/cylinders.py +280 -0
  37. capytaine/meshes/predefined/rectangles.py +202 -0
  38. capytaine/meshes/predefined/spheres.py +55 -0
  39. capytaine/meshes/quality.py +159 -0
  40. capytaine/meshes/surface_integrals.py +82 -0
  41. capytaine/meshes/symmetric_meshes.py +641 -0
  42. capytaine/meshes/visualization.py +353 -0
  43. capytaine/post_pro/__init__.py +6 -0
  44. capytaine/post_pro/free_surfaces.py +85 -0
  45. capytaine/post_pro/impedance.py +92 -0
  46. capytaine/post_pro/kochin.py +54 -0
  47. capytaine/post_pro/rao.py +60 -0
  48. capytaine/tools/__init__.py +0 -0
  49. capytaine/tools/block_circulant_matrices.py +275 -0
  50. capytaine/tools/cache_on_disk.py +26 -0
  51. capytaine/tools/deprecation_handling.py +18 -0
  52. capytaine/tools/lists_of_points.py +52 -0
  53. capytaine/tools/memory_monitor.py +45 -0
  54. capytaine/tools/optional_imports.py +27 -0
  55. capytaine/tools/prony_decomposition.py +150 -0
  56. capytaine/tools/symbolic_multiplication.py +161 -0
  57. capytaine/tools/timer.py +90 -0
  58. capytaine/ui/__init__.py +0 -0
  59. capytaine/ui/cli.py +28 -0
  60. capytaine/ui/rich.py +5 -0
  61. capytaine-3.0.0a1.dist-info/LICENSE +674 -0
  62. capytaine-3.0.0a1.dist-info/METADATA +755 -0
  63. capytaine-3.0.0a1.dist-info/RECORD +65 -0
  64. capytaine-3.0.0a1.dist-info/WHEEL +6 -0
  65. capytaine-3.0.0a1.dist-info/entry_points.txt +3 -0
@@ -0,0 +1,347 @@
1
+ # Copyright 2025 Mews Labs
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from __future__ import annotations
16
+ from typing import List, Set, Tuple
17
+
18
+ import numpy as np
19
+
20
+
21
+ def _get_intersection(
22
+ v1: np.ndarray,
23
+ v2: np.ndarray,
24
+ normal: np.ndarray,
25
+ origin: np.ndarray,
26
+ tol: float = 1e-8,
27
+ ) -> np.ndarray:
28
+ """Intersect a line segment with a plane.
29
+
30
+ Parameters
31
+ ----------
32
+ v1, v2 : numpy.ndarray
33
+ Endpoints of the segment expressed as 3D vectors.
34
+ normal : numpy.ndarray
35
+ Plane normal; does not need to be unit length.
36
+ origin : numpy.ndarray
37
+ Point lying on the plane.
38
+ tol : float, default=1e-8
39
+ Tolerance used to detect parallel segments and clamp the intersection
40
+ parameter to the segment bounds.
41
+
42
+ Returns
43
+ -------
44
+ numpy.ndarray
45
+ Intersection point located on the (possibly clamped) segment.
46
+
47
+ Raises
48
+ ------
49
+ ValueError
50
+ If the segment is parallel to the plane or the intersection lies
51
+ outside the tolerated segment range.
52
+ """
53
+ v1 = np.asarray(v1, dtype=float)
54
+ v2 = np.asarray(v2, dtype=float)
55
+ n = np.asarray(normal, dtype=float)
56
+ o = np.asarray(origin, dtype=float)
57
+
58
+ u = v2 - v1
59
+ denom = float(np.dot(n, u))
60
+ if abs(denom) < tol:
61
+ raise ValueError("Segment is parallel to the plane (no unique intersection).")
62
+
63
+ t = float(np.dot(n, (o - v1)) / denom)
64
+
65
+ if t < -tol or t > 1.0 + tol:
66
+ raise ValueError(f"Intersection t={t:.6g} lies outside the segment [0,1].")
67
+
68
+ t = max(0.0, min(1.0, t))
69
+ return v1 + t * u
70
+
71
+
72
+ def compute_aspect_ratio(tri_pts: np.ndarray) -> float:
73
+ """Compute the aspect ratio of a triangle.
74
+
75
+ The aspect ratio is defined as the longest edge length divided by the
76
+ altitude relative to that edge.
77
+
78
+ Parameters
79
+ ----------
80
+ tri_pts : numpy.ndarray
81
+ Triangle vertices arranged in a ``(3, 3)`` array.
82
+
83
+ Returns
84
+ -------
85
+ float
86
+ Aspect ratio ``L / h`` (values greater than or equal to 1).
87
+
88
+ Raises
89
+ ------
90
+ ValueError
91
+ If the triangle is degenerate and its area approaches zero.
92
+ """
93
+ tri_pts = np.asarray(tri_pts, dtype=float)
94
+ if tri_pts.shape != (3, 3):
95
+ raise ValueError("tri_pts must have shape (3,3).")
96
+
97
+ edges = np.array(
98
+ [
99
+ np.linalg.norm(tri_pts[1] - tri_pts[0]),
100
+ np.linalg.norm(tri_pts[2] - tri_pts[1]),
101
+ np.linalg.norm(tri_pts[0] - tri_pts[2]),
102
+ ],
103
+ dtype=float,
104
+ )
105
+ L = float(np.max(edges))
106
+ i = int(np.argmax(edges))
107
+
108
+ A, B = tri_pts[i], tri_pts[(i + 1) % 3]
109
+ C = tri_pts[(i + 2) % 3]
110
+ area = 0.5 * np.linalg.norm(np.cross(B - A, C - A))
111
+ if area <= 0.0:
112
+ raise ValueError("Degenerate triangle: zero area.")
113
+ h = 2.0 * area / L
114
+ return L / h
115
+
116
+
117
+ def _signed_distances(
118
+ verts: list[list[float]],
119
+ face: list[int],
120
+ normal: np.ndarray,
121
+ origin: np.ndarray,
122
+ ) -> np.ndarray:
123
+ """Evaluate signed distances of face vertices to a clipping plane.
124
+
125
+ Parameters
126
+ ----------
127
+ verts : list of list of float
128
+ All mesh vertices.
129
+ face : list of int
130
+ Indices of the vertices forming the face.
131
+ normal : numpy.ndarray
132
+ Plane normal vector.
133
+ origin : numpy.ndarray
134
+ Point belonging to the plane.
135
+
136
+ Returns
137
+ -------
138
+ numpy.ndarray
139
+ Signed distances for the vertices belonging to ``face``.
140
+ """
141
+ pts = np.asarray([verts[i] for i in face], dtype=float)
142
+ return (origin - pts) @ normal
143
+
144
+
145
+ def _compute_keep_sets(
146
+ verts: list[list[float]],
147
+ face: list[int],
148
+ normal: np.ndarray,
149
+ origin: np.ndarray,
150
+ tol: float,
151
+ ) -> tuple[list[int], set[int], np.ndarray]:
152
+ """Split vertices of a face between kept and discarded sets.
153
+
154
+ Parameters
155
+ ----------
156
+ verts : list of list of float
157
+ All mesh vertices.
158
+ face : list of int
159
+ Indices forming the face currently being clipped.
160
+ normal : numpy.ndarray
161
+ Plane normal vector defining the clipping plane.
162
+ origin : numpy.ndarray
163
+ Point on the clipping plane.
164
+ tol : float
165
+ Tolerance used to consider vertices inside the kept half-space.
166
+
167
+ Returns
168
+ -------
169
+ list of int
170
+ Indices of vertices that remain after clipping.
171
+ set of int
172
+ Indices of vertices removed by the clipping plane.
173
+ numpy.ndarray
174
+ Signed distance of each face vertex to the plane.
175
+ """
176
+ s = _signed_distances(verts, face, normal, origin)
177
+ mask = s >= -tol
178
+ keep = [face[i] for i, m in enumerate(mask) if m]
179
+ unkeep = set(face) - set(keep)
180
+ return keep, unkeep, s
181
+
182
+
183
+ def _compute_edge_intersections(
184
+ verts: list[list[float]],
185
+ face: list[int],
186
+ keep: list[int],
187
+ normal: np.ndarray,
188
+ origin: np.ndarray,
189
+ tol: float,
190
+ ) -> dict[tuple[int, int], int]:
191
+ """Intersect face edges with the clipping plane.
192
+
193
+ Parameters
194
+ ----------
195
+ verts : list of list of float
196
+ Mutable list of mesh vertices; intersection points are appended here.
197
+ face : list of int
198
+ Indices of the vertices forming the face being processed.
199
+ keep : list of int
200
+ Vertices that remain inside the kept half-space.
201
+ normal : numpy.ndarray
202
+ Plane normal vector.
203
+ origin : numpy.ndarray
204
+ Point belonging to the plane.
205
+ tol : float
206
+ Tolerance for plane intersection checks.
207
+
208
+ Returns
209
+ -------
210
+ dict[tuple[int, int], int]
211
+ Mapping from directed edges to newly created vertex indices.
212
+ """
213
+ keep_set = set(keep)
214
+ edges = list(zip(face, face[1:] + face[:1]))
215
+ cut_edges = [(i, j) for (i, j) in edges if (i in keep_set) ^ (j in keep_set)]
216
+
217
+ edge_inters: dict[tuple[int, int], int] = {}
218
+ for i, j in cut_edges:
219
+ ip = _get_intersection(
220
+ np.array(verts[i]), np.array(verts[j]), normal, origin, tol
221
+ )
222
+ idx = len(verts)
223
+ verts.append(ip.tolist())
224
+ edge_inters[(i, j)] = idx
225
+ return edge_inters
226
+
227
+
228
+ def _build_clipped_boundary(
229
+ face: list[int],
230
+ keep: list[int],
231
+ edge_inters: dict[tuple[int, int], int],
232
+ ) -> list[int]:
233
+ """Assemble the boundary indices for a clipped polygon.
234
+
235
+ Parameters
236
+ ----------
237
+ face : list of int
238
+ Original face indices.
239
+ keep : list of int
240
+ Vertices that remain after clipping.
241
+ edge_inters : dict[tuple[int, int], int]
242
+ Mapping from edges to newly created intersection vertices.
243
+
244
+ Returns
245
+ -------
246
+ list of int
247
+ Ordered vertex indices describing the clipped boundary.
248
+ """
249
+ keep_set = set(keep)
250
+ boundary: list[int] = []
251
+ for i, j in zip(face, face[1:] + face[:1]):
252
+ if i in keep_set:
253
+ boundary.append(i)
254
+ if (i, j) in edge_inters:
255
+ boundary.append(edge_inters[(i, j)])
256
+ return boundary
257
+
258
+
259
+ def clip_faces(
260
+ vertices: np.ndarray,
261
+ faces: list[list[int]],
262
+ normal: np.ndarray,
263
+ origin: np.ndarray,
264
+ tol: float = 1e-8,
265
+ ) -> Tuple[np.ndarray, List[List[int]], np.ndarray]:
266
+ """Clip faces of a mesh against a plane.
267
+
268
+ The kept half-space is defined by ``(v - origin) · normal >= -tol``.
269
+
270
+ Parameters
271
+ ----------
272
+ vertices : numpy.ndarray
273
+ Input vertex positions of shape ``(n, 3)``.
274
+ faces : list of list of int
275
+ Face connectivity; triangles and quads are supported.
276
+ normal : numpy.ndarray
277
+ Normal vector of the clipping plane.
278
+ origin : numpy.ndarray
279
+ Point located on the plane.
280
+ tol : float, default=1e-8
281
+ Tolerance for classifying vertices relative to the plane.
282
+
283
+ Returns
284
+ -------
285
+ np.ndarray of floats of shape (new_nb_vertices, 3)
286
+ The pruned vertex array
287
+ list of list of int
288
+ The list of of length new_nb_faces with clipped faces
289
+ np.ndarray of ints of shape (new_nb_faces,)
290
+ For each new face, the index of the face it comes from in the input.
291
+ """
292
+ normal = np.asarray(normal, dtype=float)
293
+ origin = np.asarray(origin, dtype=float)
294
+
295
+ verts: List[List[float]] = vertices.astype(float).tolist()
296
+ new_faces: List[Tuple[int, List[int]]] = []
297
+ # A new face is a tuple storing the index of the parent face in the
298
+ # original mesh and a list of vertices
299
+ dropped_vs: Set[int] = set()
300
+
301
+ for i_face, face in enumerate(faces):
302
+ keep, unkeep, _ = _compute_keep_sets(verts, face, normal, origin, tol)
303
+ dropped_vs.update(unkeep)
304
+
305
+ if len(keep) == 0:
306
+ continue # fully outside
307
+ if len(keep) == len(face):
308
+ # Face fully inside → keep original (quad or triangle unchanged)
309
+ new_faces.append((i_face, list(face)))
310
+ continue
311
+
312
+ edge_inters = _compute_edge_intersections(
313
+ verts, face, keep, normal, origin, tol
314
+ )
315
+ boundary = _build_clipped_boundary(face, keep, edge_inters)
316
+
317
+ if len(boundary) == 3:
318
+ new_faces.append((i_face, boundary))
319
+ elif len(boundary) == 4:
320
+ # clipped quad → 2 triangles
321
+ new_faces.append((i_face, [boundary[0], boundary[1], boundary[2]]))
322
+ new_faces.append((i_face, [boundary[0], boundary[2], boundary[3]]))
323
+ elif len(boundary) == 5:
324
+ # pentagon → 1 triangle + 1 quad
325
+ tri = [boundary[0], boundary[1], boundary[2]]
326
+ quad = [boundary[0], boundary[2], boundary[3], boundary[4]]
327
+ new_faces.append((i_face, tri))
328
+ new_faces.append((i_face, quad))
329
+ else:
330
+ # fallback: fan triangulation
331
+ for k in range(1, len(boundary) - 1):
332
+ new_faces.append((i_face, [boundary[0], boundary[k], boundary[k + 1]]))
333
+
334
+ if not new_faces:
335
+ return np.empty((0, 3), dtype=float), [], np.empty((0,), dtype=int)
336
+
337
+ used = {idx for (_, f) in new_faces for idx in f}
338
+ dropped_vs -= used
339
+ keep_vs = [i for i in range(len(verts)) if i not in dropped_vs]
340
+ remap = {old: new for new, old in enumerate(keep_vs)}
341
+
342
+ pruned_verts = np.asarray([verts[i] for i in keep_vs], dtype=float)
343
+ pruned_faces = [[remap[i] for i in face] for (_, face) in new_faces]
344
+
345
+ parent_of_face = np.array([i_parent_face for (i_parent_face, _) in new_faces])
346
+
347
+ return pruned_verts, pruned_faces, parent_of_face
@@ -0,0 +1,89 @@
1
+ import numpy as np
2
+ import xarray as xr
3
+
4
+ from capytaine.tools.optional_imports import import_optional_dependency
5
+
6
+
7
+ def export_mesh(mesh, format: str):
8
+ format = format.lower()
9
+ if format == "pyvista":
10
+ return export_to_pyvista(mesh)
11
+ elif format == "xarray":
12
+ return export_to_xarray(mesh)
13
+ elif format == "meshio":
14
+ return export_to_meshio(mesh)
15
+ elif format == "trimesh":
16
+ return export_to_trimesh(mesh)
17
+ else:
18
+ raise ValueError(f"Unrecognized output format: {format}")
19
+
20
+
21
+ def export_to_pyvista(mesh):
22
+ """
23
+ Build a PyVista UnstructuredGrid from a list of faces (triangles or quads).
24
+ """
25
+ pv = import_optional_dependency("pyvista")
26
+
27
+ # flatten into the VTK cell‐array format: [n0, i0, i1, ..., in-1, n1, j0, j1, ...]
28
+ flat_cells = []
29
+ cell_types = []
30
+ for face in mesh._faces:
31
+ n = len(face)
32
+ flat_cells.append(n)
33
+ flat_cells.extend(face)
34
+ if n == 3:
35
+ cell_types.append(pv.CellType.TRIANGLE)
36
+ elif n == 4:
37
+ cell_types.append(pv.CellType.QUAD)
38
+ else:
39
+ # if you ever have ngons, you can map them as POLYGON:
40
+ cell_types.append(pv.CellType.POLYGON)
41
+
42
+ cells_array = np.array(flat_cells, dtype=np.int64)
43
+ cell_types = np.array(cell_types, dtype=np.uint8)
44
+
45
+ return pv.UnstructuredGrid(cells_array, cell_types, mesh.vertices.astype(np.float32))
46
+
47
+
48
+ def export_to_xarray(mesh):
49
+ return xr.Dataset(
50
+ {
51
+ "mesh_vertices": (
52
+ ["face", "vertices_of_face", "space_coordinate"],
53
+ mesh.as_array_of_faces()
54
+ )
55
+ },
56
+ coords={
57
+ "space_coordinate": ["x", "y", "z"],
58
+ })
59
+
60
+
61
+ def export_to_meshio(mesh):
62
+ meshio = import_optional_dependency("meshio")
63
+
64
+ quads = [f for f in mesh._faces if len(f) == 4]
65
+ tris = [f for f in mesh._faces if len(f) == 3]
66
+
67
+ cells = []
68
+ if quads:
69
+ cells.append(meshio.CellBlock("quad", np.array(quads, dtype=np.int32)))
70
+ if tris:
71
+ cells.append(meshio.CellBlock("triangle", np.array(tris, dtype=np.int32)))
72
+
73
+ return meshio.Mesh(points=mesh.vertices, cells=cells)
74
+
75
+
76
+ def export_to_trimesh(mesh):
77
+ trimesh = import_optional_dependency("trimesh")
78
+ triangle_faces = []
79
+ for face in mesh._faces:
80
+ if len(face) == 4 and face[3] != face[2]:
81
+ triangle_faces.append([face[0], face[1], face[2]])
82
+ triangle_faces.append([face[0], face[2], face[3]])
83
+ else:
84
+ triangle_faces.append(face[:3])
85
+ return trimesh.Trimesh(
86
+ vertices=mesh.vertices,
87
+ faces=np.array(triangle_faces),
88
+ process=False
89
+ )
@@ -0,0 +1,259 @@
1
+ # Copyright 2025 Mews Labs
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from typing import List
16
+ from functools import reduce
17
+ from itertools import chain
18
+
19
+ import numpy as np
20
+ from numpy.typing import NDArray
21
+
22
+
23
+ def get_vertices_face(face, vertices):
24
+ if len(face) == 4 and face[2] != face[3]:
25
+ return (
26
+ vertices[face[0]],
27
+ vertices[face[1]],
28
+ vertices[face[2]],
29
+ vertices[face[3]],
30
+ )
31
+ else:
32
+ return vertices[face[0]], vertices[face[1]], vertices[face[2]]
33
+
34
+
35
+ def compute_faces_normals(vertices, faces):
36
+ normals = []
37
+ for face in faces:
38
+ if len(face) == 4 and face[2] != face[3]:
39
+ normal = _quad_normal(vertices, face[0], face[1], face[2], face[3])
40
+ else:
41
+ normal = _triangle_normal(vertices, face[0], face[1], face[2])
42
+ normals.append(normal)
43
+ return np.array(normals)
44
+
45
+
46
+ def compute_faces_areas(vertices, faces):
47
+ areas = []
48
+ for face in faces:
49
+ verts = get_vertices_face(face, vertices)
50
+ if len(verts) == 4:
51
+ a, b, c, d = verts
52
+ area1 = 0.5 * np.linalg.norm(np.cross(b - a, c - a))
53
+ area2 = 0.5 * np.linalg.norm(np.cross(c - a, d - a))
54
+ areas.append(area1 + area2)
55
+ else:
56
+ a, b, c = verts
57
+ areas.append(0.5 * np.linalg.norm(np.cross(b - a, c - a)))
58
+ return np.array(areas)
59
+
60
+
61
+ def compute_faces_centers(vertices, faces):
62
+ centers = []
63
+ for face in faces:
64
+ verts = get_vertices_face(face, vertices)
65
+ if len(verts) == 4:
66
+ a, b, c, d = verts
67
+ area1 = 0.5 * np.linalg.norm(np.cross(b - a, c - a))
68
+ area2 = 0.5 * np.linalg.norm(np.cross(c - a, d - a))
69
+ c1 = (a + b + c) / 3
70
+ c2 = (a + c + d) / 3
71
+ center = (c1 * area1 + c2 * area2) / (area1 + area2)
72
+ else:
73
+ a, b, c = verts
74
+ center = (a + b + c) / 3
75
+ centers.append(center)
76
+ return np.array(centers)
77
+
78
+
79
+ def compute_faces_radii(vertices, faces):
80
+ centers = compute_faces_centers(vertices, faces)
81
+ distances = []
82
+ for face, center in zip(faces, centers):
83
+ d = compute_distance_between_points(vertices[face[0]], center)
84
+ distances.append(d)
85
+ return np.array(distances)
86
+
87
+
88
+ def compute_gauss_legendre_2_quadrature(vertices, faces):
89
+ # Parameters of Gauss-Legendre 2 quadrature scheme
90
+ local_points = np.array([(+1/np.sqrt(3), +1/np.sqrt(3)),
91
+ (+1/np.sqrt(3), -1/np.sqrt(3)),
92
+ (-1/np.sqrt(3), +1/np.sqrt(3)),
93
+ (-1/np.sqrt(3), -1/np.sqrt(3))])
94
+ local_weights = np.array([1/4, 1/4, 1/4, 1/4])
95
+
96
+ # Application to mesh
97
+ faces = vertices[faces[:, :], :]
98
+ nb_faces = faces.shape[0]
99
+ nb_quad_points = len(local_weights)
100
+ points = np.empty((nb_faces, nb_quad_points, 3))
101
+ weights = np.empty((nb_faces, nb_quad_points))
102
+ for i_face in range(nb_faces):
103
+ for k_quad in range(nb_quad_points):
104
+ xk, yk = local_points[k_quad, :]
105
+ points[i_face, k_quad, :] = (
106
+ (1+xk)*(1+yk) * faces[i_face, 0, :]
107
+ + (1+xk)*(1-yk) * faces[i_face, 1, :]
108
+ + (1-xk)*(1-yk) * faces[i_face, 2, :]
109
+ + (1-xk)*(1+yk) * faces[i_face, 3, :]
110
+ )/4
111
+ dxidx = ((1+yk)*faces[i_face, 0, :] + (1-yk)*faces[i_face, 1, :]
112
+ - (1-yk)*faces[i_face, 2, :] - (1+yk)*faces[i_face, 3, :])/4
113
+ dxidy = ((1+xk)*faces[i_face, 0, :] - (1+xk)*faces[i_face, 1, :]
114
+ - (1-xk)*faces[i_face, 2, :] + (1-xk)*faces[i_face, 3, :])/4
115
+ detJ = np.linalg.norm(np.cross(dxidx, dxidy))
116
+ weights[i_face, k_quad] = local_weights[k_quad] * 4 * detJ
117
+
118
+ return points, weights
119
+
120
+
121
+ def _triangle_normal(vertices, v0_idx, v1_idx, v2_idx):
122
+ """
123
+ Compute normal vector of a triangle face.
124
+
125
+ Parameters
126
+ ----------
127
+ vertices : ndarray
128
+ Vertex coordinate array.
129
+ v0_idx, v1_idx, v2_idx : int
130
+ Indices of triangle vertices.
131
+
132
+ Returns
133
+ -------
134
+ np.ndarray
135
+ Normalized normal vector (3,)
136
+ """
137
+ v0, v1, v2 = vertices[v0_idx], vertices[v1_idx], vertices[v2_idx]
138
+ normal = np.cross(v1 - v0, v2 - v0)
139
+ return normal / np.linalg.norm(normal)
140
+
141
+
142
+ def _quad_normal(vertices, v0_idx, v1_idx, v2_idx, v3_idx):
143
+ """
144
+ Compute normal vector of a quadrilateral face via diagonals.
145
+
146
+ Parameters
147
+ ----------
148
+ vertices : ndarray
149
+ Vertex coordinate array.
150
+ v0_idx, v1_idx, v2_idx, v3_idx : int
151
+ Indices of quad vertices.
152
+
153
+ Returns
154
+ -------
155
+ np.ndarray
156
+ Normalized normal vector (3,)
157
+ """
158
+ v0, v1, v2, v3 = (
159
+ vertices[v0_idx],
160
+ vertices[v1_idx],
161
+ vertices[v2_idx],
162
+ vertices[v3_idx],
163
+ )
164
+ ac = v2 - v0
165
+ bd = v3 - v1
166
+ normal = np.cross(ac, bd)
167
+ return normal / np.linalg.norm(normal)
168
+
169
+
170
+ def compute_distance_between_points(a, b):
171
+ """
172
+ Compute Euclidean distance between two points in n-dimensional space.
173
+
174
+ Parameters
175
+ ----------
176
+ a, b : array_like
177
+ Coordinate arrays (length 3 or more).
178
+
179
+ Returns
180
+ -------
181
+ float
182
+ Euclidean distance.
183
+ """
184
+ a = np.asarray(a)
185
+ b = np.asarray(b)
186
+ return np.linalg.norm(b - a)
187
+
188
+ def faces_in_group(faces: NDArray[np.integer], group: NDArray[np.integer]) -> NDArray[np.bool_]:
189
+ """Identification of faces with vertices within group.
190
+
191
+ Parameters
192
+ ----------
193
+ faces : NDArray[np.integer]
194
+ Mesh faces. Expecting a numpy array of shape N_faces x N_vertices_per_face.
195
+ group : NDArray[np.integer]
196
+ Group of connected vertices
197
+
198
+ Returns
199
+ -------
200
+ NDArray[np.bool]
201
+ Mask of faces containing vertices from the group
202
+ """
203
+ return np.any(np.isin(faces, group), axis=1)
204
+
205
+ def clustering(faces: NDArray[np.integer]) -> List[NDArray[np.integer]]:
206
+ """Clustering of vertices per connected faces.
207
+
208
+ Parameters
209
+ ----------
210
+ faces : NDArray[np.integer]
211
+ Mesh faces. Expecting a numpy array of shape N_faces x N_vertices_per_face.
212
+
213
+ Returns
214
+ -------
215
+ list[NDArray[np.integer]]
216
+ Groups of connected vertices.
217
+ """
218
+ vert_groups: list[NDArray[np.integer]] = []
219
+ mask = np.ones(faces.shape[0], dtype=bool)
220
+ while np.any(mask):
221
+ # Consider faces whose vertices are not already identified in a group.
222
+ # Start new group by considering first face
223
+ remaining_faces = faces[mask]
224
+ group = remaining_faces[0]
225
+ rem_mask = np.ones(remaining_faces.shape[0], dtype=bool)
226
+ # Iterative update of vertices group. Output final result to frozenset
227
+ while not np.allclose(new:=faces_in_group(remaining_faces, group), rem_mask):
228
+ group = np.unique(remaining_faces[new])
229
+ rem_mask = new
230
+ else:
231
+ group = np.unique(remaining_faces[new])
232
+ vert_groups.append(group)
233
+ # Identify faces that have no vertices in current groups
234
+ mask = ~reduce(np.logical_or, [faces_in_group(faces, group) for group in vert_groups])
235
+ return vert_groups
236
+
237
+
238
+ def connected_components(mesh):
239
+ """Returns a list of meshes that each corresponds to the a connected component in the original mesh.
240
+ Assumes the mesh is mostly conformal without duplicate vertices.
241
+ """
242
+ # Get connected vertices
243
+ vertices_components = clustering(mesh.faces)
244
+ # Verification
245
+ if sum(len(group) for group in vertices_components) != len(set(chain.from_iterable(vertices_components))):
246
+ raise ValueError("Error in connected components clustering. Some elements are duplicated")
247
+ # The components are found. The rest is just about retrieving the faces in each components.
248
+ faces_components = [np.argwhere(faces_in_group(mesh.faces, group)) for group in vertices_components]
249
+ components = [mesh.extract_faces(f) for f in faces_components]
250
+ return components
251
+
252
+
253
+ def connected_components_of_waterline(mesh, z=0.0):
254
+ if np.any(mesh.vertices[:, 2] > z + 1e-8):
255
+ mesh = mesh.immersed_part(free_surface=z)
256
+ fs_vertices_indices = np.where(np.isclose(mesh.vertices[:, 2], z))[0]
257
+ fs_faces_indices = np.where(np.any(np.isin(mesh.faces, fs_vertices_indices), axis=1))[0]
258
+ crown_mesh = mesh.extract_faces(fs_faces_indices)
259
+ return connected_components(crown_mesh)