Commit d99fee34 authored by pacome's avatar pacome
Browse files

Add rotate and translate function out of InHull class

parent 36412582
......@@ -818,6 +818,98 @@ class InHull(Shape):
self.points_coordinates = fun()
self.points_operations.append(fun)
#####################
def translate(x, vect):
'''
Translate a point or a list of points by a vector vect
Parameters:
-----------
x: tuple or liste of tuple
the points to translate
vect: list, tuple or numpy array
the translation vector
'''
x = np.asarray(x)
vect = np.asarray(vect)
if len(x.shape) < 2:
# translate a point
return x + np.asarray(vect)
elif len(x.shape) == 2:
return np.repeat([vect], len(x), axis=0) + x
def rotate(x, angle, axis=None, center='self'):
'''Rotate a point or a list of points in 2 or 3d around a center
of an angle or a combination of angles or around an axis
Parameters:
-----------
angle: (tx, ty, tz) or number
tx: number
angle of rotation.
if 3d, tx is either the angle around the x axis or
the angle to rotate around the specified axis
ty: number
if axis is None, the angle of the rotation around y
will be ignored if axis is not None
tz: number (opt, defaults to None)
if axis is None, the angle of the rotation around z
will be ignored if axis is not None
axis: arraylike (opt, defaults to None)
if axis is None, rotation around main axes
if not None, the axis of rotation
center: None, arraylike or 'self'(opt, defaults to None)
the center of the rotation
if None, will be the origin according to the dimension
if self, will be the average of the points
(center of regular polygon for 'inplace' rotation)
'''
x = np.asarray(x)
center_parent = center
if not isinstance(angle, (list, tuple)):
angle = (angle,)
angle = np.asarray(angle)
if len(x.shape) == 2:
npt, ndim = x.shape
elif len(x.shape) == 1:
npt, ndim = 1, x.shape[0]
print(x.shape)
x = x[None, :]
else: raise ValueError
if ndim is 2:
rot_mat = np.array([[np.cos(angle[0]), -np.sin(angle[0])],
[np.sin(angle[0]), np.cos(angle[0])]])
elif ndim is 3:
rot_mat = _rotation_matrix(angle, axis)
else: raise ValueError
if center_parent is None:
center = np.zeros(ndim)
elif center_parent is 'self':
center = np.mean(x, axis=0)
elif isinstance(center_parent, (tuple, list, np.ndarray)):
center = np.asarray(center_parent)
else: raise ValueError
center_mat = np.repeat([center],
len(x), axis=0)
ret = (np.einsum(
'kij, kj -> ki',
np.repeat([rot_mat], len(x), axis=0), x - center_mat)
+ center_mat)
if npt is 1:
return ret[0]
else:
return ret
#################
class Delaunay(InHull):
'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment