Coverage for src/beamme/core/nurbs_patch.py: 99%
68 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-08 11:03 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-08 11:03 +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 typing 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 def __init__(self, knot_vectors, polynomial_orders, material=None, nodes=None):
41 super().__init__(nodes=nodes, material=material)
43 # Knot vectors
44 self.knot_vectors = knot_vectors
46 # Polynomial degrees
47 self.polynomial_orders = polynomial_orders
49 def get_nurbs_dimension(self) -> int:
50 """Determine the number of dimensions of the NURBS structure.
52 Returns:
53 Number of dimensions of the NURBS object.
54 """
55 n_knots = len(self.knot_vectors)
56 n_polynomial = len(self.polynomial_orders)
57 if not n_knots == n_polynomial:
58 raise ValueError(
59 "The variables n_knots and polynomial_orders should have "
60 f"the same length. Got {n_knots} and {n_polynomial}"
61 )
62 return n_knots
64 def get_number_of_control_points_per_dir(self) -> list[int]:
65 """Determine the number of control points in each parameter direction
66 of the patch.
68 Returns:
69 List of control points per direction.
70 """
71 n_dim = len(self.knot_vectors)
72 n_cp_per_dim = []
73 for i_dim in range(n_dim):
74 knot_vector_size = len(self.knot_vectors[i_dim])
75 polynomial_order = self.polynomial_orders[i_dim]
76 n_cp_per_dim.append(knot_vector_size - polynomial_order - 1)
77 return n_cp_per_dim
79 def get_non_empty_knot_span_indices(self) -> list[list[int]]:
80 """Determine the indices of the non-empty knot spans in each parameter
81 direction.
83 Returns:
84 List of lists with the indices of the non-empty knot spans in
85 each parameter direction.
86 """
88 non_empty_knot_spans_indices: list[list[int]] = [
89 [] for _ in range(self.get_nurbs_dimension())
90 ]
92 for i_dir in range(len(self.knot_vectors)):
93 for i_knot in range(len(self.knot_vectors[i_dir]) - 1):
94 if (
95 abs(
96 self.knot_vectors[i_dir][i_knot]
97 - self.knot_vectors[i_dir][i_knot + 1]
98 )
99 > _bme.eps_knot_vector
100 ):
101 non_empty_knot_spans_indices[i_dir].append(i_knot)
102 return non_empty_knot_spans_indices
104 def get_number_of_elements(self) -> int:
105 """Determine the number of elements in this patch by checking the
106 amount of nonzero knot spans in the knot vector.
108 Returns:
109 Number of elements for this patch.
110 """
112 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices()
113 num_elements_dir = [len(indices) for indices in non_empty_knot_spans_indices]
114 total_num_elements = _np.prod(num_elements_dir)
115 return total_num_elements
117 @_abstractmethod
118 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
119 """Return a tuple with the knot spans for this patch."""
121 @_abstractmethod
122 def get_ids_ctrlpts(self, *args) -> list[int]:
123 """Compute the global indices of the control points that influence the
124 element defined by the given knot span."""
127class NURBSSurface(NURBSPatch):
128 """A patch of a NURBS surface."""
130 def __init__(self, *args, **kwargs):
131 super().__init__(*args, **kwargs)
133 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
134 """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
145 element defined by the given knot span."""
147 p, q = self.polynomial_orders
148 ctrlpts_size_u = len(self.knot_vectors[0]) - p - 1
149 id_u = knot_span_u - p
150 id_v = knot_span_v - q
152 return [
153 ctrlpts_size_u * (id_v + j) + id_u + i
154 for j in range(q + 1)
155 for i in range(p + 1)
156 ]
159class NURBSVolume(NURBSPatch):
160 """A patch of a NURBS volume."""
162 def __init__(self, *args, **kwargs):
163 super().__init__(*args, **kwargs)
165 def get_knot_span_iterator(self) -> _Iterator[tuple[int, ...]]:
166 """Return a tuple with the knot spans for this patch."""
168 non_empty_knot_spans_indices = self.get_non_empty_knot_span_indices()
169 return (
170 (u, v, w)
171 for w in non_empty_knot_spans_indices[2]
172 for v in non_empty_knot_spans_indices[1]
173 for u in non_empty_knot_spans_indices[0]
174 )
176 def get_ids_ctrlpts(
177 self, knot_span_u: int, knot_span_v: int, knot_span_w: int
178 ) -> list[int]:
179 """Compute the global indices of the control points that influence the
180 element defined by the given knot span."""
182 p, q, r = self.polynomial_orders
183 id_u = knot_span_u - p
184 id_v = knot_span_v - q
185 id_w = knot_span_w - r
186 size_u = len(self.knot_vectors[0]) - p - 1
187 size_v = len(self.knot_vectors[1]) - q - 1
189 return [
190 size_u * size_v * (id_w + k) + size_u * (id_v + j) + id_u + i
191 for k in range(r + 1)
192 for j in range(q + 1)
193 for i in range(p + 1)
194 ]