 ### Add extrude and inflate functions in shapes

parent 277e7339
 ... ... @@ -879,6 +879,114 @@ def box(length, corner=None, center=None): return points def extrude(x, newcoord, force1d=False): '''return x with value newcoord in an added dimension Parameters: ----------- x: array like (npt, ndim) points or list of points in 1d or 2d if 1d: use force1d=True newcoord: array like value of array of the values of the expected new coordinate force1d: bool (defaults to False) to make the difference between one point in 2d and a list of 1d points, force1d needs to be put to True in the 1d case Returns: -------- newx: array like (npt * len(newcoord), ndim + 1) the new extruded array ''' newcoord = np.asarray(newcoord) x = np.asarray(x) if len(x.shape) <= 1: if force1d: ndim = -1 npt = len(x) else: ndim, npt = x.shape, 1 x = x[None, :] else: npt, ndim = x.shape if ndim is -1: newx = np.tile(x, len(newcoord)).T else: newx = np.tile(x, (len(newcoord), 1)) newcoord = np.repeat(newcoord, npt, axis=0) newx = np.c_[newx, newcoord] return newx def inflate(x, value, direction='all', center='self'): '''add or substract value to list of points x depending depending if x[i] < or > center[i] for chosen i Notes: ------ This is usefull for example if ones wants to create increase slightly the size of a polygon (polyhedron) Parameters: ----------- x: 2d array the list of points to inflate value: number, list the value of which to inflate x in the chosen direction if it is a number, will inflate in all directions in direction param direction: list (opt, defaults to 'all') the direction in which to inflate x if all: will infalte all directions otherwhise should match shape of x center: list of coordinates (opt, defaults to 'self) the reference wrt to which to inflate. x[: i] < center[i] -> newx[i] = x[i] - value[i] if 'self', center will be mean(x) Returns: -------- newx: 2d array the list of inflated coordinates ''' x = np.asarray(x) if len(x.shape) == 2: npt, ndim = x.shape else: assert False, 'x should be a 2d array' assert (ndim == 2 or ndim == 3) if direction is 'all': direction = np.arange(ndim) try: assert len(value) == len(direction) except TypeError: tmpvalue = np.zeros(ndim) tmpvalue[direction] = np.ones(len(direction)) * value value = tmpvalue if center is 'self': center = np.mean(x, axis=0) newx = x.copy() for i in direction: tmp = ((x[:, i] + value[i]) * (x[:, i] > center[i]) + (x[:, i] - value[i]) * (x[:, i] < center[i]) + (x[:, i]) * (x[:, i] == center[i])) newx[:, i] = tmp return newx def translate(x, vect): ''' Translate a point or a list of points by a vector vect ... ...
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!