Commit 277e7339 authored by asantosnet's avatar asantosnet
Browse files

Merge branch 'clean_shapes'. Only comments changed

parents 871166c0 f44ab1de
......@@ -345,14 +345,51 @@ class Shape(ABC):
return General(func=new_func)
def translate(self, vect):
'''
Translate a Shapes instance
Parameters:
-----------
vect: list, tuple or numpy array
the translation vector
'''
vect = np.asarray(vect)
func = self.geometry
def newfunc(x):
return func(translate(x, -vect))
self.geometry = newfunc
def rotate(self, angle, axis=None, center='self'):
'''3D not tested !!!'''
'''Rotate a Shapes instance in 2 or 3d around a center
of an angle or a combination of angles or around an axis
WARNING: Not tested in 3D
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)
'''
func = self.geometry
try:
angle = -angle
......@@ -364,11 +401,21 @@ class Shape(ABC):
self.geometry = newfunc
def reflect(self, axis):
'''
Parameters:
-----------
axis: array like
2 (3) points that generate the line (plan)
wrt which to reflect in 2d (3d)
'''
func = self.geometry
def newfunc(x):
return func(reflect(x, axis))
self.geometry = newfunc
(),
class PiecewiseFunction(object):
'''
When called apllies self.evaluate_coordinates to a vector of
......@@ -485,6 +532,7 @@ class General(Shape):
def geometry(self, x):
return self.func(x)
class Rectangle(General):
'''
Define a rectangle
......@@ -571,6 +619,7 @@ class Rectangle(General):
else:
return isin
class Ellipsoid(General):
'''
Define a rectangle
......@@ -771,6 +820,7 @@ class InHull(Shape):
axis))
#####################
def box(length, corner=None, center=None):
'''
Returns points of a 2d of 3d box
......@@ -828,6 +878,7 @@ def box(length, corner=None, center=None):
return points
def translate(x, vect):
'''
Translate a point or a list of points by a vector vect
......@@ -849,6 +900,7 @@ def translate(x, 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
......@@ -856,16 +908,16 @@ def rotate(x, angle, axis=None, center='self'):
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
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
......@@ -875,8 +927,10 @@ def rotate(x, angle, axis=None, center='self'):
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)
......@@ -916,6 +970,7 @@ def rotate(x, angle, axis=None, center='self'):
else:
return ret
def reflect(x, axis):
'''returns reflections of point(s) x wrt to axis
......@@ -932,6 +987,7 @@ def reflect(x, axis):
ret: array like (shape of x)
the reflected point(s)
'''
x = np.asarray(x)
if len(x.shape) == 1:
ndim, npt = x.shape[0], -1
......@@ -1043,6 +1099,7 @@ class RegularPolygon(InHull):
self.flat = flat
super().__init__()
def generate_points(self):
'''
Returns a numpy array containing the coordinates of the points
......@@ -1244,16 +1301,16 @@ def __test_plot_rotation_translation():
polyr = RegularPolygon(npoints=4, center=(0, 0))
polyr.translate([-1, -0.2])
polyr.rotate(np.pi/3, center='self')
polyr.rotate(np.pi/4, center='self')
mesh = GridBuilder(meshs=[[get_bbox(poly), 0.1, poly, 0],
[get_bbox(polyt), 0.1, polyt, 1],
[get_bbox(polyr), 0.1, polyr, 2]])
mesh = GridBuilder(meshs=[[get_bbox(poly), 0.01, poly, 0],
[get_bbox(polyt), 0.01, polyt, 1],
[get_bbox(polyr), 0.01, polyr, 2]])
poly_2 = RegularPolygon(npoints=4, center=(0, 0))
poly_2.translate([-1, 1])
poly_2.rotate(np.pi/4, center='self')
mesh2 = GridBuilder(meshs=[[get_bbox(poly_2), 0.1,
mesh2 = GridBuilder(meshs=[[get_bbox(poly_2), 0.01,
poly_2, 0]]).points
plt.scatter(*mesh.points[mesh.point_label == 0].T, c='b',
......@@ -1296,8 +1353,10 @@ def __test_plot_reflection():
plt.axes().set_aspect('equal')
plt.show()
#test_plot_ellipse()
#test_plot_rectangle()
#test_plot_ellipse_circ()
#test_plot_ellipse_circ_class()
#test_plot_rotation_translation()
#__test_plot_ellipse()
#__test_plot_rectangle()
#__test_plot_ellipse_circ()
#__test_plot_ellipse_circ_class()
#__test_plot_rotation_translation()
#__test_reflection()
#__test_plot_reflection()
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