Skip to content

Commit

Permalink
Add: auto_refinement variable
Browse files Browse the repository at this point in the history
  • Loading branch information
OberGue committed Jul 2, 2024
1 parent cc32216 commit b37f158
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
1 change: 1 addition & 0 deletions examples/show_swept.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
trajectory=trajectory,
cross_section=cross_section,
cross_section_normal=cs_nv,
auto_refinement=True,
)

### VISUALIZATION ###
Expand Down
91 changes: 51 additions & 40 deletions splinepy/helpme/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
-------
Expand All @@ -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:
Expand All @@ -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 ###
Expand Down Expand Up @@ -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 ###

Expand Down

0 comments on commit b37f158

Please sign in to comment.