Coverage for src/beamme/mesh_creation_functions/nurbs_generic.py: 95%

102 statements  

« 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"""Generic function used to create NURBS meshes.""" 

23 

24import itertools as _itertools 

25 

26import numpy as _np 

27 

28from beamme.core.conf import bme as _bme 

29from beamme.core.geometry_set import GeometryName as _GeometryName 

30from beamme.core.geometry_set import GeometrySetNodes as _GeometrySetNodes 

31from beamme.core.mesh import Mesh as _Mesh 

32from beamme.core.node import ControlPoint as _ControlPoint 

33from beamme.core.nurbs_patch import NURBSSurface as _NURBSSurface 

34from beamme.core.nurbs_patch import NURBSVolume as _NURBSVolume 

35 

36 

37def _check_nurbs_dimension_and_element_type( 

38 nurbs_dimension: int, element_type: type 

39) -> None: 

40 """Check if the element type is compatible with the NURBS dimension. 

41 

42 Args: 

43 nurbs_dimension: The dimension of the NURBS patch (2 for surface, 3 for volume). 

44 element_type: The type of element to be created. 

45 

46 Raises: 

47 ValueError: If the element type is not compatible with the NURBS dimension. 

48 """ 

49 if nurbs_dimension == 2 and not issubclass(element_type, _NURBSSurface): 

50 raise ValueError( 

51 "Error, expected element type to be a NURBSSurface for a NURBS surface!" 

52 ) 

53 elif nurbs_dimension == 3 and not issubclass(element_type, _NURBSVolume): 

54 raise ValueError( 

55 "Error, expected element type to be a NURBSVolume for a NURBS volume!" 

56 ) 

57 

58 

59def add_splinepy_nurbs_to_mesh( 

60 mesh: _Mesh, element_type: type, splinepy_obj, *, material=None 

61) -> _GeometryName: 

62 """Add a splinepy NURBS to the mesh. 

63 

64 Args: 

65 mesh: Mesh that the created NURBS geometry will be added to. 

66 element_type: The type of element to be created. 

67 splinepy_obj (splinepy object): NURBS geometry created using splinepy. 

68 material (Material): Material for this geometry. 

69 

70 Returns: 

71 GeometryName: 

72 Set with the control points that form the topology of the mesh. 

73 

74 For a surface, the following information is stored: 

75 Vertices: 'vertex_u_min_v_min', 'vertex_u_max_v_min', 'vertex_u_min_v_max', 'vertex_u_max_v_max' 

76 Edges: 'line_v_min', 'line_u_max', 'line_v_max', 'line_u_min' 

77 Surface: 'surf' 

78 

79 For a volume, the following information is stored: 

80 Vertices: 'vertex_u_min_v_min_w_min', 'vertex_u_max_v_min_w_min', 'vertex_u_min_v_max_w_min', 'vertex_u_max_v_max_w_min', 

81 'vertex_u_min_v_min_w_max', 'vertex_u_max_v_min_w_max', 'vertex_u_min_v_max_w_max', 'vertex_u_max_v_max_w_max' 

82 Edges: 'line_v_min_w_min', 'line_u_max_w_min', 'line_v_max_w_min', 'line_u_min_w_min', 

83 'line_u_min_v_min', 'line_u_max_v_min', 'line_u_min_v_max', 'line_u_max_v_max' 

84 'line_v_min_w_max', 'line_u_max_w_max', 'line_v_max_w_max', 'line_u_min_w_max' 

85 Surfaces: 'surf_w_min', 'surf_w_max', 'surf_v_min', 'surf_v_max', 'surf_v_max', 'surf_u_min' 

86 Volume: 'vol' 

87 """ 

88 # Make sure that the control points are 3D 

89 nurbs_cp_dim = splinepy_obj.control_points.shape[1] 

90 if not nurbs_cp_dim == 3: 

91 raise ValueError(f"Invalid control point dimension: {nurbs_cp_dim}") 

92 

93 # Make sure the material is in the mesh 

94 mesh.add_material(material) 

95 

96 # Fill control points 

97 control_points = [ 

98 _ControlPoint(coord, weight[0]) 

99 for coord, weight in zip( 

100 _np.asarray(splinepy_obj.control_points), _np.asarray(splinepy_obj.weights) 

101 ) 

102 ] 

103 

104 # Create elements 

105 _check_nurbs_dimension_and_element_type( 

106 len(splinepy_obj.knot_vectors), element_type 

107 ) 

108 

109 element = element_type( 

110 [_np.asarray(knot_vector) for knot_vector in splinepy_obj.knot_vectors], 

111 _np.asarray(splinepy_obj.degrees), 

112 nodes=control_points, 

113 material=material, 

114 ) 

115 

116 # Add element and control points to the mesh 

117 mesh.elements.append(element) 

118 mesh.nodes.extend(control_points) 

119 

120 # Create geometry sets that will be returned 

121 return_set = create_geometry_sets(element) 

122 

123 return return_set 

124 

125 

126def add_geomdl_nurbs_to_mesh( 

127 mesh: _Mesh, element_type: type, geomdl_obj, *, material=None 

128) -> _GeometryName: 

129 """Add a geomdl NURBS to the mesh. 

130 

131 Args: 

132 mesh: Mesh that the created NURBS geometry will be added to. 

133 element_type: The type of element to be created. 

134 geomdl_obj (geomdl object): NURBS geometry created using geomdl. 

135 material (Material): Material for this geometry. 

136 

137 Returns: 

138 GeometryName: 

139 Set with the control points that form the topology of the mesh. 

140 

141 For a surface, the following information is stored: 

142 Vertices: 'vertex_u_min_v_min', 'vertex_u_max_v_min', 'vertex_u_min_v_max', 'vertex_u_max_v_max' 

143 Edges: 'line_v_min', 'line_u_max', 'line_v_max', 'line_u_min' 

144 Surface: 'surf' 

145 

146 For a volume, the following information is stored: 

147 Vertices: 'vertex_u_min_v_min_w_min', 'vertex_u_max_v_min_w_min', 'vertex_u_min_v_max_w_min', 'vertex_u_max_v_max_w_min', 

148 'vertex_u_min_v_min_w_max', 'vertex_u_max_v_min_w_max', 'vertex_u_min_v_max_w_max', 'vertex_u_max_v_max_w_max' 

149 Edges: 'line_v_min_w_min', 'line_u_max_w_min', 'line_v_max_w_min', 'line_u_min_w_min', 

150 'line_u_min_v_min', 'line_u_max_v_min', 'line_u_min_v_max', 'line_u_max_v_max' 

151 'line_v_min_w_max', 'line_u_max_w_max', 'line_v_max_w_max', 'line_u_min_w_max' 

152 Surfaces: 'surf_w_min', 'surf_w_max', 'surf_v_min', 'surf_v_max', 'surf_v_max', 'surf_u_min' 

153 Volume: 'vol' 

154 """ 

155 # Make sure the material is in the mesh 

156 mesh.add_material(material) 

157 

158 # Fill control points 

159 control_points = [] 

160 nurbs_dimension = len(geomdl_obj.knotvector) 

161 if nurbs_dimension == 2: 

162 control_points = create_control_points_surface(geomdl_obj) 

163 elif nurbs_dimension == 3: 

164 control_points = create_control_points_volume(geomdl_obj) 

165 else: 

166 raise NotImplementedError( 

167 "Error, not implemented for NURBS with dimension {}!".format( 

168 nurbs_dimension 

169 ) 

170 ) 

171 

172 # Create elements 

173 _check_nurbs_dimension_and_element_type(len(geomdl_obj.knotvector), element_type) 

174 element = element_type( 

175 geomdl_obj.knotvector, 

176 geomdl_obj.degree, 

177 nodes=control_points, 

178 material=material, 

179 ) 

180 

181 # Add element and control points to the mesh 

182 mesh.elements.append(element) 

183 mesh.nodes.extend(control_points) 

184 

185 # Create geometry sets that will be returned 

186 return_set = create_geometry_sets(element) 

187 

188 return return_set 

189 

190 

191def create_control_points_surface(geomdl_obj): 

192 """Creates a list with the ControlPoint objects of a surface created with geomdl.""" 

193 control_points = [] 

194 for dir_v in range(geomdl_obj.ctrlpts_size_v): 

195 for dir_u in range(geomdl_obj.ctrlpts_size_u): 

196 weight = geomdl_obj.ctrlpts2d[dir_u][dir_v][3] 

197 

198 # As the control points are scaled with their weight, divide them to get 

199 # their coordinates 

200 coord = [ 

201 geomdl_obj.ctrlpts2d[dir_u][dir_v][0] / weight, 

202 geomdl_obj.ctrlpts2d[dir_u][dir_v][1] / weight, 

203 geomdl_obj.ctrlpts2d[dir_u][dir_v][2] / weight, 

204 ] 

205 

206 control_points.append(_ControlPoint(coord, weight)) 

207 

208 return control_points 

209 

210 

211def create_control_points_volume(geomdl_obj): 

212 """Creates a list with the ControlPoint objects of a volume created with geomdl.""" 

213 control_points = [] 

214 for dir_w in range(geomdl_obj.ctrlpts_size_w): 

215 for dir_v in range(geomdl_obj.ctrlpts_size_v): 

216 for dir_u in range(geomdl_obj.ctrlpts_size_u): 

217 # Obtain the id of the control point 

218 cp_id = ( 

219 dir_v 

220 + geomdl_obj.ctrlpts_size_v * dir_u 

221 + geomdl_obj.ctrlpts_size_u * geomdl_obj.ctrlpts_size_v * dir_w 

222 ) 

223 

224 weight = geomdl_obj.ctrlptsw[cp_id][3] 

225 

226 # As the control points are scaled with their weight, divide them to get 

227 # their coordinates 

228 coord = [ 

229 geomdl_obj.ctrlptsw[cp_id][0] / weight, 

230 geomdl_obj.ctrlptsw[cp_id][1] / weight, 

231 geomdl_obj.ctrlptsw[cp_id][2] / weight, 

232 ] 

233 

234 control_points.append(_ControlPoint(coord, weight)) 

235 

236 return control_points 

237 

238 

239def create_geometry_sets(element: _NURBSSurface | _NURBSVolume) -> _GeometryName: 

240 """Create the geometry sets for NURBS patches of all dimensions. 

241 

242 Args: 

243 element: The NURBS patch for which the geometry sets should be created. 

244 

245 Returns: 

246 The geometry set container for the given NURBS patch. 

247 """ 

248 # Create return set 

249 return_set = _GeometryName() 

250 

251 # Get general data needed for the set creation 

252 num_cps_uvw = element.get_number_of_control_points_per_dir() 

253 nurbs_dimension = len(element.knot_vectors) 

254 n_cp = _np.prod(num_cps_uvw) 

255 axes = ["u", "v", "w"][:nurbs_dimension] 

256 name_map = {0: "min", -1: "max", 1: "min_next", -2: "max_next"} 

257 

258 # This is a tensor array that contains the CP indices 

259 cp_indices_dim = _np.arange(n_cp, dtype=int).reshape(*num_cps_uvw[::-1]).transpose() 

260 

261 if nurbs_dimension >= 0: 

262 # Add point sets 

263 directions = [0, -1] 

264 for corner in _itertools.product(*([directions] * nurbs_dimension)): 

265 name = "vertex_" + "_".join( 

266 f"{axis}_{name_map[coord]}" for axis, coord in zip(axes, corner) 

267 ) 

268 index = cp_indices_dim[corner] 

269 return_set[name] = _GeometrySetNodes( 

270 _bme.geo.point, nodes=element.nodes[index] 

271 ) 

272 

273 if nurbs_dimension > 0: 

274 # Add edge sets 

275 

276 if nurbs_dimension == 2: 

277 directions = [0, 1, -2, -1] 

278 elif nurbs_dimension == 3: 

279 directions = [0, -1] 

280 else: 

281 raise ValueError("NURBS dimension 1 not implemented") 

282 

283 # Iterate over each axis (the axis that varies — the "edge" direction) 

284 for edge_axis in range(nurbs_dimension): 

285 # The other axes will be fixed 

286 fixed_axes = [i for i in range(nurbs_dimension) if i != edge_axis] 

287 for fixed_dir in _itertools.product(*([directions] * len(fixed_axes))): 

288 # Build slicing tuple for indexing cp_indices_dim 

289 slicer: list[slice | int] = [slice(None)] * nurbs_dimension 

290 name_parts = [] 

291 for axis_idx, dir_val in zip(fixed_axes, fixed_dir): 

292 slicer[axis_idx] = dir_val 

293 name_parts.append(f"{axes[axis_idx]}_{name_map[dir_val]}") 

294 name = "line_" + "_".join(name_parts) 

295 

296 # Get node indices along the edge 

297 edge_indices = cp_indices_dim[tuple(slicer)].flatten() 

298 return_set[name] = _GeometrySetNodes( 

299 _bme.geo.line, nodes=[element.nodes[i] for i in edge_indices] 

300 ) 

301 

302 if nurbs_dimension == 2: 

303 # Add surface sets for surface NURBS 

304 return_set["surf"] = _GeometrySetNodes(_bme.geo.surface, element.nodes) 

305 

306 if nurbs_dimension == 3: 

307 # Add surface sets for volume NURBS 

308 for fixed_axis in range(nurbs_dimension): 

309 for dir_val in directions: 

310 # Build slice and name 

311 slicer = [slice(None)] * nurbs_dimension 

312 slicer[fixed_axis] = dir_val 

313 surface_name = f"surf_{axes[fixed_axis]}_{name_map[dir_val]}" 

314 

315 surface_indices = cp_indices_dim[tuple(slicer)].flatten() 

316 return_set[surface_name] = _GeometrySetNodes( 

317 _bme.geo.surface, 

318 nodes=[element.nodes[i] for i in surface_indices], 

319 ) 

320 

321 # Add volume sets 

322 return_set["vol"] = _GeometrySetNodes(_bme.geo.volume, element.nodes) 

323 

324 return return_set