Skip to content

Commit

Permalink
Improve array conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
isteinbrecher committed Feb 14, 2025
1 parent 65af33f commit 512cf63
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 13 deletions.
2 changes: 1 addition & 1 deletion meshpy/cosserat_curve/cosserat_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def get_centerline_positions_and_rotations(
"""

# Get the points that are within the arc length of the given curve.
points_on_arc_length = np.array(points_on_arc_length)
points_on_arc_length = np.asarray(points_on_arc_length)
points_in_bounds = np.logical_and(
points_on_arc_length > self.point_arc_length[0],
points_on_arc_length < self.point_arc_length[-1],
Expand Down
4 changes: 2 additions & 2 deletions meshpy/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def reflect(self, normal_vector, origin=None, flip_beams=False):
"""

# Normalize the normal vector.
normal_vector = np.array(normal_vector / np.linalg.norm(normal_vector))
normal_vector = np.asarray(normal_vector) / np.linalg.norm(normal_vector)

# Get array with all quaternions and positions for the nodes.
pos = get_nodal_coordinates(self.nodes)
Expand Down Expand Up @@ -892,7 +892,7 @@ def display_pyvista(
directors = [
finite_element_nodes.glyph(
geom=arrow,
orient=f"base_vector_{i+1}",
orient=f"base_vector_{i + 1}",
scale="cross_section_radius",
factor=director_radius_scaling_factor,
)
Expand Down
4 changes: 2 additions & 2 deletions meshpy/mesh_creation_functions/beam_fibers_in_rectangle.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def _intersect_line_with_rectangle(
"""

# Convert the input values to np.arrays.
start_line = np.array(start_line)
direction_line = np.array(direction_line)
start_line = np.asarray(start_line)
direction_line = np.asarray(direction_line)

# Set definition for the boundary lines of the rectangle. The director is
# chosen in a way, that the values [0, 1] for the line parameters alpha are
Expand Down
17 changes: 9 additions & 8 deletions meshpy/rotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def __init__(self, *args):
self.q[0] = 1
elif len(args) == 2:
# Set from rotation axis and rotation angle.
axis = np.array(args[0])
axis = np.asarray(args[0])
phi = args[1]
norm = np.linalg.norm(axis)
if norm < mpy.eps_quaternion:
Expand All @@ -95,9 +95,10 @@ def from_quaternion(cls, q, *, normalized=False):
error accumulation.
"""
rotation = object.__new__(cls)
rotation.q = np.array(q, dtype=float)
if not normalized:
rotation.q /= np.linalg.norm(rotation.q)
if normalized:
rotation.q = np.array(q)
else:
rotation.q = np.asarray(q) / np.linalg.norm(q)
if (not rotation.q.ndim == 1) or (not len(rotation.q) == 4):
raise ValueError("Got quaternion array with unexpected dimensions")
return rotation
Expand Down Expand Up @@ -154,7 +155,7 @@ def from_rotation_vector(cls, rotation_vector):
"""Create the object from a rotation vector."""

q = np.zeros(4)
rotation_vector = np.array(rotation_vector)
rotation_vector = np.asarray(rotation_vector)
phi = np.linalg.norm(rotation_vector)
q[0] = np.cos(0.5 * phi)
if phi < mpy.eps_quaternion:
Expand Down Expand Up @@ -199,7 +200,7 @@ def get_rotation_matrix(self):
return R

def get_quaternion(self):
"""Return the quaternion for this rotation, as tuple."""
"""Return the quaternion for this rotation, as numpy array (copy)."""

return np.array(self.q)

Expand Down Expand Up @@ -313,7 +314,7 @@ def __mul__(self, other):
return Rotation.from_quaternion(added_rotation)
elif isinstance(other, (list, np.ndarray)) and len(other) == 3:
# Apply rotation to vector.
return np.dot(self.get_rotation_matrix(), np.array(other))
return np.dot(self.get_rotation_matrix(), np.asarray(other))
raise NotImplementedError("Error, not implemented, does not make sense anyway!")

def __eq__(self, other):
Expand Down Expand Up @@ -490,7 +491,7 @@ def smallest_rotation(q: Rotation, t):

R_old = q.get_rotation_matrix()
g1_old = R_old[:, 0]
g1 = np.array(t) / np.linalg.norm(t)
g1 = np.asarray(t) / np.linalg.norm(t)

# Quaternion components of relative rotation
q_rel = np.zeros(4)
Expand Down

0 comments on commit 512cf63

Please sign in to comment.