def __array_wrap__(self, out_arr, context=None): # Handle scalars so as not to break ndimage. # See http://stackoverflow.com/a/794812/1221924 print("__array_wrap__") # still have no sorted out why this happens for ndarrays, but not # sub-classes. However, this seems like the correct behavior as it # will not silently clobber subclasses -> scalars if out_arr.ndim == 0: # explicitly clobber subclass -> scalar return out_arr[()] return ndarray.__array_wrap__(self, out_arr, context)
def __array_wrap__(self, out_arr, context=None): # Handle scalars so as not to break ndimage. # See http://stackoverflow.com/a/794812/1221924 if out_arr.ndim == 0: return out_arr[()] return ndarray.__array_wrap__(self, out_arr, context)
def __array_wrap__(self, obj): if obj.shape == (): return obj[()] else: return ndarray.__array_wrap__(self, obj)
class Point(ooarray, baseGeometryObject): __array_priority__ = 100 pointCounter = array(0) weight = None def __init__(self, *args, **kw): ooarray.__init__(self) #, *args, **kw) baseGeometryObject.__init__(self, *args, **kw) def __new__(self, *args, **kw): self.pointCounter += 1 self._num = asscalar(self.pointCounter) self.specifier = '.' self.color = 'k' Args = args[:-1] if type(args[-1]) == str else args r = asarray(Args[0]).view(self) if len(Args) == 1 and type( Args[0]) in (ooarray, Point, ndarray, list, tuple) else ooarray(Args).view(self) r.name = args[-1] if type(args[-1]) == str else kw[ 'name'] if 'name' in kw else 'unnamed point ' + str(self._num) r._id = oofun._id oofun._id += 1 return r #obj = ooarray(asarray(args[:-1] if type(args[-1]) == str else args).tolist()).view(self) #return obj # def __array_finalize__(self, obj): # if obj is None: return __call__ = lambda self, *args, **kw: \ self._name(args[0]) \ if len(args) == 1 and type(args[0]) == str \ else Point(ooarray.__call__(self, *args, **kw) if self.size is not 1 or not isinstance(self.view(ndarray)[0], oofun)\ else asscalar(self)(*args, **kw), weight = self.__dict__.get('weight', None)) _spaceDimension = lambda self: self.size if self.size is not 1 or not isinstance( self.view(ndarray).flatten()[0], oofun) else asscalar(self).size __getitem__ = lambda self, ind: self.view(ndarray).flatten()[ ind] if self.size is not 1 else asscalar(self)[ind] # def __get__(self, ind): # raise 0 __getslice__ = lambda self, ind1, ind2: self.view(ndarray)[ ind1:ind2] if self.size is not 1 else asscalar(self)[ind1:ind2] def _name(self, newName): self.name = newName return self __array_wrap__ = lambda self, out_arr, context=None: ndarray.__array_wrap__( self, out_arr, context) __radd__ = __add__ = lambda self, other: Point(ooarray.__add__( self, other)) __rmul__ = __mul__ = lambda self, other: Point(ooarray.__mul__( self, other)) __div__ = lambda self, other: Point(ooarray.__div__(self, other)) __rdiv__ = lambda self, other: Point(ooarray.__div__(other, self)) def __str__(self): return str(self.view(ndarray).flatten()) def distance(self, *args, **kw): assert len(kw) == 0 and len(args) == 1 other = args[0] if isinstance(other, (list, tuple)): other = Point(other) if isinstance(other, (Line, Plane)): return self.distance(self.projection(other)) elif isinstance(other, ndarray): return sqrt(sum((self - other)**2), attachConstraints=False) def projection(self, obj): assert isinstance( obj, baseGeometryObject), 'incorrect object for projection' if isinstance(obj, Line): # TODO: remove 0.0 for future Python 3.X versions tmp = dot(self - obj.basePoint, obj.direction) if isinstance(tmp, oofun): tmp.size = 1 tmp1 = obj.direction * tmp / (0.0 + sum(obj.direction**2)) tmp2 = obj.basePoint if type(tmp1) == ooarray: # hence tmp2 is array tmp2 = tmp2.view(ooarray) return Point(tmp1 + tmp2) elif isinstance(obj, Plane): d1, d2 = obj.directions bp = obj.basePoint a = sum(d1**2) b = d = dot(d1, d2) e = sum(d2**2) c = dot(self - bp, d1) f = dot(self - bp, d2) delta = a * e - b * d + 0.0 # TODO: remove 0.0 when Python 2.x will be deprecated alpha = (c * e - b * f) / delta beta = (a * f - c * d) / delta return Point(bp + alpha * d1 + beta * d2) else: raise SpaceFuncsException( 'not implemented for this geometry object yet') def rotate(self, P, angle): ''' Usage: newPoint = originPoint.rotate(BasePoint, angle) (angle in radians) currently the method is implemented for 2-dimensional space only for other spaces you can use FuncDesigner.dot(RotationMatrix ,Point - BasePoint) + BasePoint (implementing it with more convenient API is in Future Plans) if BasePoint is (0,0), you can just use prevPoint.rotate(0, angle) ''' Tmp = (self - P) if P is not 0 else self x, y = Tmp[0], Tmp[1] sin_theta, cos_theta = sin(angle), cos(angle) x2, y2 = x * cos_theta - y * sin_theta, x * sin_theta + y * cos_theta return Point(x2 + P[0], y2 + P[1]) if P is not 0 else Point(x2, y2) def symmetry(self, other): if not isinstance(other, (Point, Line, Plane)): raise SpaceFuncsException( 'method Point.symmetry(object) is implemented for Point|Line|Plane objects only' ) if type(other) == Point: return 2 * other - self else: proj = self.projection(other) return 2 * proj - self perpendicular = lambda self, obj: \ _perpendicularToLine(self, obj) if isinstance(obj, Line) \ else _perpendicularToPlane(self, obj) if isinstance(obj, Plane)\ else SF_error('perpendicular from Point to the object is not implemented yet') def plot(self, *args, **kw): if not pylabInstalled: raise SpaceFuncsException( 'to plot you should have matplotlib installed') # TODO: use something else instead, e.g. scatter if self.size != 2: raise SpaceFuncsException('only 2-D plot is implemented for now') if not self.name.startswith('unnamed'): pylab.plot([self[0]], [self[1]], self.specifier, color=self.color) pylab.text(self[0], self[1], self.name)
def __array_wrap__(self, out_arr, context=None): return ndarray.__array_wrap__(self, out_arr, context)