From b37f1582739525084a827cf2e8f296bb79b0dc77 Mon Sep 17 00:00:00 2001 From: Guenther Obermair Date: Tue, 2 Jul 2024 13:36:38 +0200 Subject: [PATCH] Add: auto_refinement variable --- examples/show_swept.py | 1 + splinepy/helpme/create.py | 91 ++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 40 deletions(-) diff --git a/examples/show_swept.py b/examples/show_swept.py index 9e3a82e50..e4007169e 100644 --- a/examples/show_swept.py +++ b/examples/show_swept.py @@ -120,6 +120,7 @@ trajectory=trajectory, cross_section=cross_section, cross_section_normal=cs_nv, + auto_refinement=True, ) ### VISUALIZATION ### diff --git a/splinepy/helpme/create.py b/splinepy/helpme/create.py index 2f90750c5..ed0fa2639 100644 --- a/splinepy/helpme/create.py +++ b/splinepy/helpme/create.py @@ -302,6 +302,7 @@ def swept( cross_section, trajectory, cross_section_normal=None, + auto_refinement=False, ): """ Sweeps a cross-section along a trajectory. The cross-section @@ -320,6 +321,9 @@ def swept( cross_section_normal : np.array Normal vector of the cross-section Default is [0, 0, 1] + auto_refinement : bool + If True, the trajectory will be refined at points of + highest curvature. Default is False. Returns ------- @@ -340,6 +344,8 @@ def swept( raise NotImplementedError("Trajectory must be 1D") if not len(cross_section_normal) == 3: raise ValueError("Cross section normal must be 3D") + if not isinstance(auto_refinement, bool): + raise ValueError("auto_refinement must be a boolean") # setting default value for cross_section_normal if cross_section_normal is None: @@ -349,6 +355,10 @@ def swept( trajectory = trajectory.create.embedded(3) cross_section = cross_section.create.embedded(3) + # initialize parameter values + par_value = trajectory.greville_abscissae() + par_value = par_value.reshape(-1, 1) + def transformation_matrices(traj, par_value): ### TRANSFORMATION MATRICES ### @@ -454,49 +464,50 @@ def transformation_matrices(traj, par_value): ### REFINEMENT OF TRAJECTORY ### - par_value = trajectory.greville_abscissae() - par_value = par_value.reshape(-1, 1) + if auto_refinement: + ## inserts knots in trajectory area with highest curvature # - ### insert knots in trajectory area with highest curvature ### - curv = [] - for i in par_value: - # calculate curvature of trajectory at parametric value i - curv.append(round(_np.linalg.norm(trajectory.derivative([i], [2])), 2)) - # evaluate the par_values-vector indices of the maximum curvature points - max_curv = max(curv) - max_indices = [i for i, x in enumerate(curv) if x == max_curv] - # prepare matrix for the insertion - insertion_values = [] - # compute the new insertion values - par_values = par_value.ravel() - - for maxi in max_indices: - if maxi == 0: - insertion_values.append( - (par_values[maxi] + par_values[maxi + 1]) / 2 - ) - elif maxi == len(par_values) - 1: - insertion_values.append( - (par_values[maxi] + par_values[maxi - 1]) / 2 - ) - else: - insertion_values.append( - (par_values[maxi] + par_values[maxi - 1]) / 2 - ) - insertion_values.append( - (par_values[maxi] + par_values[maxi + 1]) / 2 + curv = [] + for i in par_value: + # calculate curvature of trajectory at parametric value i + curv.append( + round(_np.linalg.norm(trajectory.derivative([i], [2])), 2) ) + # evaluate the par_values-vector indices of the maximum curvature points + max_curv = max(curv) + max_indices = [i for i, x in enumerate(curv) if x == max_curv] + # prepare matrix for the insertion + insertion_values = [] + # compute the new insertion values + par_values = par_value.ravel() + + for maxi in max_indices: + if maxi == 0: + insertion_values.append( + (par_values[maxi] + par_values[maxi + 1]) / 2 + ) + elif maxi == len(par_values) - 1: + insertion_values.append( + (par_values[maxi] + par_values[maxi - 1]) / 2 + ) + else: + insertion_values.append( + (par_values[maxi] + par_values[maxi - 1]) / 2 + ) + insertion_values.append( + (par_values[maxi] + par_values[maxi + 1]) / 2 + ) - # 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) - par_value = par_value.reshape(-1, 1) + # 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) + par_value = par_value.reshape(-1, 1) ### SWEEPING PROCESS ###