Coverage for src/beamme/core/nurbs_patch.py: 99%
69 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-28 15:20 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-28 15:20 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2026 BeamMe Authors
4#
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
14#
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21# THE SOFTWARE.
22"""This module implements NURBS patches for the mesh."""
24from abc import abstractmethod as _abstractmethod
25from collections.abc import Iterator as _Iterator
27import numpy as _np
28import pyvista as _pv
30from beamme.core.conf import bme as _bme
31from beamme.core.element import Element as _Element
34class NURBSPatch(_Element):
35 """A base class for a NURBS patch."""
37 # Generic VTK cell type for NURBS elements - this will not show the correct topology in vtk.
38 vtk_cell_type = _pv.CellType.POLYGON
40 # Type of this element.
41 element_type = _bme.element_type.nurbs
43 def __init__(self, knot_vectors, polynomial_orders, material=None, nodes=None):
44 super().__init__(nodes=nodes, material=material)
46 # Knot vectors
47 self.knot_vectors = knot_vectors
49 # Polynomial degrees
50 self.polynomial_orders = polynomial_orders
52 def get_nurbs_dimension(self) -> int:
53 """Determine the number of dimensions of the NURBS structure.
55 Returns:
56 Number of dimensions of the NURBS object.
57 """
58 n_knots = len(self.knot_vectors)
59 n_polynomial = len(self.polynomial_orders)
60 if not n_knots == n_polynomial:
61 raise ValueError(
62 "The variables n_knots and polynomial_orders should have "
63 f"the same length. Got {n_knots} and {n_polynomial}"
64 )
65 return n_knots
67 def get_number_of_control_points_per_dir(self) -> list[int]:
68 """Determine the number of control points in each parameter direction of the
69 patch.
71 Returns:
72 List of control points per direction.
73 """
74 n_dim = len(self.knot_vectors)
75 n_cp_per_dim = []
76 for i_dim in range(n_dim):
77 knot_vector_size = len(self.knot_vectors[i_dim])
78 polynomial_order = self.polynomial_orders[i_dim]
79 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1)
80 return n_cp_per_dim
82 def get_non_empty_knot_span_indices(self) -> list[list[int]]:
83 """Determine the indices of the non-empty knot spans in each parameter
84 direction.
86 Returns:
87 List of lists with the indices of the non-empty knot spans in
88 each parameter direction.
89 """
90 non_empty_knot_spans_indices: list[list[int]] = [
91 [] for _ in range(self.get_nurbs_dimension())
92 ]
94 for i_dir in range(len(self.knot_vectors)):
95 for i_knot in range(len(self.knot_vectors[i_dir]) - 1):
96 if (
97 abs(
98 self.knot_vectors[i_dir][i_knot]
99 - self.knot_vectors[i_dir][i_knot + 1]
100 )
101 > _bme.eps_knot_vector
102 ):
103 non_empty_knot_spans_indices[i_dir].append(i_knot)
104 return non_empty_knot_spans_indices
106 def get_number_of_elements(self) -> int:
107 """Determine the number of elements in this patch by checking the amount of
108 nonzero knot spans in the knot vector.
110 Returns:
111 Number of elements for this patch.
112 """
113 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices()
114 num_elements_dir = [len(indices) for indices in non_empty_knot_spans_indices]
115 total_num_elements = _np.prod(num_elements_dir)
116 return total_num_elements
118 @_abstractmethod
119 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
120 """Return a tuple with the knot spans for this patch."""
122 @_abstractmethod
123 def get_ids_ctrlpts(self, *args) -> list[int]:
124 """Compute the global indices of the control points that influence the element
125 defined by the given knot span."""
128class NURBSSurface(NURBSPatch):
129 """A patch of a NURBS surface."""
131 def __init__(self, *args, **kwargs):
132 super().__init__(*args, **kwargs)
134 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
135 """Return a tuple with the knot spans for this patch."""
136 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices()
137 return (
138 (u, v)
139 for v in non_empty_knot_spans_indices[1]
140 for u in non_empty_knot_spans_indices[0]
141 )
143 def get_ids_ctrlpts(self, knot_span_u: int, knot_span_v: int) -> list[int]:
144 """Compute the global indices of the control points that influence the element
145 defined by the given knot span."""
146 p, q = self.polynomial_orders
147 ctrlpts_size_u = len(self.knot_vectors[0]) - p - 1
148 id_u = knot_span_u - p
149 id_v = knot_span_v - q
151 return [
152 ctrlpts_size_u * (id_v + j) + id_u + i
153 for j in range(q + 1)
154 for i in range(p + 1)
155 ]
158class NURBSVolume(NURBSPatch):
159 """A patch of a NURBS volume."""
161 def __init__(self, *args, **kwargs):
162 super().__init__(*args, **kwargs)
164 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
165 """Return a tuple with the knot spans for this patch."""
166 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices()
167 return (
168 (u, v, w)
169 for w in non_empty_knot_spans_indices[2]
170 for v in non_empty_knot_spans_indices[1]
171 for u in non_empty_knot_spans_indices[0]
172 )
174 def get_ids_ctrlpts(
175 self, knot_span_u: int, knot_span_v: int, knot_span_w: int
176 ) -> list[int]:
177 """Compute the global indices of the control points that influence the element
178 defined by the given knot span."""
179 p, q, r = self.polynomial_orders
180 id_u = knot_span_u - p
181 id_v = knot_span_v - q
182 id_w = knot_span_w - r
183 size_u = len(self.knot_vectors[0]) - p - 1
184 size_v = len(self.knot_vectors[1]) - q - 1
186 return [
187 size_u * size_v * (id_w + k) + size_u * (id_v + j) + id_u + i
188 for k in range(r + 1)
189 for j in range(q + 1)
190 for i in range(p + 1)
191 ]