From 045a4939ba9255b81df92ae9fe712717f855e968 Mon Sep 17 00:00:00 2001 From: Guenther Obermair Date: Tue, 2 Jul 2024 17:23:14 +0200 Subject: [PATCH] Fix: positioning of cs-cps now acc to traj-cps; Fix: auto_refinement routine --- examples/show_swept.py | 132 ++++++++++++++++++++------------------ splinepy/helpme/create.py | 45 +++++++++---- 2 files changed, 100 insertions(+), 77 deletions(-) diff --git a/examples/show_swept.py b/examples/show_swept.py index e4007169e..ba22802a1 100644 --- a/examples/show_swept.py +++ b/examples/show_swept.py @@ -25,63 +25,67 @@ # } # 2D questionmark - # dict_trajectory = { - # "degrees": [3], - # "knot_vectors": [[0.0, 0.0, 0.0, 0.0, 0.2, 0.4, - # 0.6, 0.8, 0.9, 1.0, 1.0, 1.0, 1.0]], - # "control_points": np.array([ - # [0.5, 0], # Startpunkt - # [0.5, 2], - # [1.0, 3], - # [2.0, 4], - # [2.15, 5], - # [1.8, 5.9], - # [1.0, 6.2], - # [-0.25, 6], - # [-0.5, 5],]) - # } - # init trajectory as bspline - - # closed 3D questionmark dict_trajectory = { "degrees": [3], "knot_vectors": [ - [ - 0.0, - 0.0, - 0.0, - 0.0, - 0.1, - 0.2, - 0.3, - 0.4, - 0.5, - 0.6, - 0.8, - 0.9, - 1.0, - 1.0, - 1.0, - 1.0, - ] + [0.0, 0.0, 0.0, 0.0, 0.2, 0.4, 0.6, 0.8, 0.9, 1.0, 1.0, 1.0, 1.0] ], "control_points": np.array( [ - [0.5, 0, 0], - [0.5, 2, 0.3], - [1.0, 3, 0.1], - [2.0, 4, -0.1], - [2.15, 5, -0.2], - [1.8, 5.9, -0.4], - [1.0, 6.2, -0.3], - [-0.25, 6, -0.1], - [-0.5, 5.0, 0.1], - [-2.0, 4.0, 0.2], - [-1, 3.0, 0.1], - [0.5, 0.0, 0.0], + [0.5, 0], # Startpunkt + [0.5, 2], + [1.0, 3], + [2.0, 4], + [2.15, 5], + [1.8, 5.9], + [1.0, 6.2], + [-0.25, 6], + [-0.5, 5], ] ), } + # init trajectory as bspline + + # closed 3D questionmark + # dict_trajectory = { + # "degrees": [3], + # "knot_vectors": [ + # [ + # 0.0, + # 0.0, + # 0.0, + # 0.0, + # 0.1, + # 0.2, + # 0.3, + # 0.4, + # 0.5, + # 0.6, + # 0.8, + # 0.9, + # 1.0, + # 1.0, + # 1.0, + # 1.0, + # ] + # ], + # "control_points": np.array( + # [ + # [0.5, 0, 0], + # [0.5, 2, 0.3], + # [1.0, 3, 0.1], + # [2.0, 4, -0.1], + # [2.15, 5, -0.2], + # [1.8, 5.9, -0.4], + # [1.0, 6.2, -0.3], + # [-0.25, 6, -0.1], + # [-0.5, 5.0, 0.1], + # [-2.0, 4.0, 0.2], + # [-1, 3.0, 0.1], + # [0.5, 0.0, 0.0], + # ] + # ), + # } trajectory = splinepy.BSpline(**dict_trajectory) # alternatively, use helpme to create a trajectory @@ -91,25 +95,25 @@ trajectory.uniform_refine(0, 1) ### CROSS SECTION ### - dict_cross_section = { - "degrees": [3], - "knot_vectors": [[0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0]], - "control_points": np.array( - [ - [0.0, 0.0, 0.0], - [1.0, 2.0, 0.0], - [2.0, 0.0, 0.0], - [3.0, -2.0, 0.0], - [4.0, 0.0, 0.0], - ] - ), - } + # dict_cross_section = { + # "degrees": [3], + # "knot_vectors": [[0.0, 0.0, 0.0, 0.0, 0.5, 1.0, 1.0, 1.0, 1.0]], + # "control_points": np.array( + # [ + # [0.0, 0.0, 0.0], + # [1.0, 2.0, 0.0], + # [2.0, 0.0, 0.0], + # [3.0, -2.0, 0.0], + # [4.0, 0.0, 0.0], + # ] + # ), + # } - # init cross section as bspline - cross_section = splinepy.BSpline(**dict_cross_section) + # # init cross section as bspline + # cross_section = splinepy.BSpline(**dict_cross_section) # alternatively, use helpme to create a cross section - # cross_section = splinepy.helpme.create.surface_circle(0.5).nurbs + cross_section = splinepy.helpme.create.surface_circle(0.5).nurbs # user can define the normal vector of the cross section, in case # the cross section is not planar in the x-y plane (default) diff --git a/splinepy/helpme/create.py b/splinepy/helpme/create.py index ed0fa2639..45b50ec68 100644 --- a/splinepy/helpme/create.py +++ b/splinepy/helpme/create.py @@ -440,7 +440,7 @@ def transformation_matrices(traj, par_value): A.append(_np.linalg.inv(T[i])) # check if the beginning and the end of the B-vector are the same - if not _np.allclose(B_reverse[0], B_reverse[-1]): + if not _np.allclose(B_reverse[0], B_reverse[-1], rtol=1e-3): _log.warning( "Vector calculation is not exact due to the " "trajectory being closed in an uncommon way." @@ -500,20 +500,39 @@ def transformation_matrices(traj, par_value): # insert knots into the trajectory's knot vector insertion_values = _np.unique(insertion_values) - trajectory.uniform_refine_fixed_knots(0, len(insertion_values)) - # add insertion values to the existing parameter values - par_value = _np.concatenate( - (par_value.reshape(-1), _np.asanyarray(insertion_values)) - ) - # sort parameter values - par_value = _np.sort(par_value) + # convert knot vector to list + kv_list = trajectory.knot_vectors[0].numpy() + + # insert knots into the trajectory's knot vector + if any(value in insertion_values for value in kv_list): + add = _np.concatenate((insertion_values, kv_list)) + add = _np.unique(add) + # remove the existing knots + add = add[~_np.isin(add, kv_list)] + # check if there are any knots to insert + if len(add) == 0: + _log.warning("Auto Refinement couldn't insert knots.") + else: + trajectory.insert_knots(0, add) + # give information about the inserted knots + _log.info( + f"Auto Refinement inserted {len(add)} " + "knots into the trajectory." + ) + else: + trajectory.insert_knots(0, insertion_values) + # give information about the inserted knots + _log.info( + f"Auto Refinement inserted {len(insertion_values)} " + "knots into the trajectory." + ) + + # recalculate parameter values + par_value = trajectory.greville_abscissae() par_value = par_value.reshape(-1, 1) ### SWEEPING PROCESS ### - # evaluate trajectory at the parameter values - evals = trajectory.evaluate(par_value) - # evaluate center of cross section and translate to origin cross_para_center = _np.mean(cross_section.parametric_bounds, axis=0) cs_center = cross_section.evaluate( @@ -526,7 +545,7 @@ def transformation_matrices(traj, par_value): # set cross section control points along trajectory swept_spline_cps = [] - for index, eval_point in enumerate(evals): + for index in range(len(par_value)): temp_csp = [] # place every control point of cross section separately for cscp in cross_section.control_points: @@ -535,7 +554,7 @@ def transformation_matrices(traj, par_value): # transform cross section to global coordinates normal_cscp = _np.matmul(A[index], normal_cscp) # translate cross section to trajectory point - normal_cscp += eval_point + normal_cscp += trajectory.control_points[index] # append control point to list temp_csp.append(normal_cscp)