def closest_point(self, x): th, r = cart2pol(*(x - self.center).T) cpx, cpy = pol2cart(th, self.radius) dist = np.abs(r - self.radius) cp = np.column_stack((cpx, cpy)) + self.center # Maybe we should return np.zeros(dist.shape) instead of just one 0? return cp, dist, 0, {}
def closest_point(self, x): th, r = cart2pol(*(x-self.center).T) cpx, cpy = pol2cart(th, self.radius) dist = np.abs(r - self.radius) cp = np.column_stack((cpx, cpy)) + self.center # Maybe we should return np.zeros(dist.shape) instead of just one 0? return cp, dist, 0, {}
def closestPointToCartesian(self, xx): # TODO: could probably be vectorized x,y = xx - self._center th, r = cart2pol(x, y) x, y = pol2cart(th, self._radius) cp = self._center + a([x,y]) dist = norm(xx - cp, 2) return cp, dist, 0, {}
def closestPointVectorized(self, x, y): th, r = cart2pol(x - self._center[0], y - self._center[1]) cpx, cpy = pol2cart(th, self._radius) #dist = norm(xx - cp, 2) #dist = sqrt( (x-cpx)**2 + (y-cpy)**2 ) sdist = r - self._radius cpx = cpx + self._center[0] cpy = cpy + self._center[1] return cpx, cpy, sdist, 0, {}
def closest_point(self, x): x = np.atleast_2d(x) r = np.sqrt(np.sum((x - self.center)**2, axis=1)) xm = x.copy() r_eq_0 = r == 0 xm[r_eq_0, 0] = self.center[0] + 1 r[r_eq_0] = 1 dim = self.dim cpx = np.zeros(x.shape) bdy = np.zeros(x.shape[0]) below_semicircle = xm[:, -1] < self.center[-1] if dim == 2: left = (xm[:, 0] <= self.center[0]) & below_semicircle cpx[left] = self.center - np.array([self.radius, 0.]) bdy[left] = 1 right = ~left & below_semicircle cpx[right] = self.center + np.array([self.radius, 0.]) bdy[right] = 2 elif dim == 3: # Here it doesn't matter if xm[below_semicircle, *] has # shape (0,) because adding a scalar "works" (ie, xx has # also shape (0,)) xx = xm[below_semicircle, 0] - self.center[0] yy = xm[below_semicircle, 1] - self.center[1] th, _ = cart2pol(xx, yy) xx, yy = pol2cart(th, self.radius) cpx[below_semicircle] = (np.column_stack((xx, yy , np.zeros(xx.shape))) + self.center) bdy[below_semicircle] = 1 else: raise NotImplementedError( 'Dim {0} not implemented'.format(dim) ) rest = ~below_semicircle # This is needed, if xm[rest] is empty (ie shape (0,)) trying # to add an array results in a ValueError: operands could not # be broadcast together if rest.any(): c = (self.radius / r[rest])[:, np.newaxis] cpx[rest] = c * (xm[rest] - self.center) + self.center bdy[rest] = 0 dist = np.sqrt(np.sum((x - cpx)**2, axis=1)) return cpx, dist, bdy, {}
def closestPointToCartesian(self, x): SURF_CEN = self._center SURF_SR = self._radius r = norm(x - SURF_CEN, 2) if (r==0): tx = SURF_CEN[0] + 1.0 r = 1.0 else: tx = x[0] xm = x.copy() xm[0] = tx #dim = len(x) dim = self._dim if (xm[-1] < SURF_CEN[-1]): # "below" semicircle, if (dim == 2): if (xm[0] <= SURF_CEN[0]): # left cpx = SURF_CEN - a([SURF_SR, 0.0]) bdy = 1 else: # right cpx = SURF_CEN + a([SURF_SR, 0.0]) bdy = 2 if (dim == 3): xx = xm[0] - SURF_CEN[0] yy = xm[1] - SURF_CEN[1] th,r = cart2pol(xx,yy) xx,yy = pol2cart(th,SURF_SR) cpx = SURF_CEN + a([xx,yy,0]) bdy = 1 if (dim >= 4): # general case possible? raise NameError('4D and higher not implemented') else: # at or "above" semicircle: same as for whole circle c = SURF_SR / r cpx = c*(xm-SURF_CEN) + SURF_CEN bdy = 0 dist = norm(cpx - x, 2) return cpx, dist, bdy, {}
def closest_point(self, x): x = np.atleast_2d(x) r = np.sqrt(np.sum((x - self.center)**2, axis=1)) xm = x.copy() r_eq_0 = r == 0 xm[r_eq_0, 0] = self.center[0] + 1 r[r_eq_0] = 1 dim = self.dim cpx = np.zeros(x.shape) bdy = np.zeros(x.shape[0]) below_semicircle = xm[:, -1] < self.center[-1] if dim == 2: left = (xm[:, 0] <= self.center[0]) & below_semicircle cpx[left] = self.center - np.array([self.radius, 0.]) bdy[left] = 1 right = ~left & below_semicircle cpx[right] = self.center + np.array([self.radius, 0.]) bdy[right] = 2 elif dim == 3: # Here it doesn't matter if xm[below_semicircle, *] has # shape (0,) because adding a scalar "works" (ie, xx has # also shape (0,)) xx = xm[below_semicircle, 0] - self.center[0] yy = xm[below_semicircle, 1] - self.center[1] th, _ = cart2pol(xx, yy) xx, yy = pol2cart(th, self.radius) cpx[below_semicircle] = (np.column_stack( (xx, yy, np.zeros(xx.shape))) + self.center) bdy[below_semicircle] = 1 else: raise NotImplementedError('Dim {0} not implemented'.format(dim)) rest = ~below_semicircle # This is needed, if xm[rest] is empty (ie shape (0,)) trying # to add an array results in a ValueError: operands could not # be broadcast together if rest.any(): c = (self.radius / r[rest])[:, np.newaxis] cpx[rest] = c * (xm[rest] - self.center) + self.center bdy[rest] = 0 dist = np.sqrt(np.sum((x - cpx)**2, axis=1)) return cpx, dist, bdy, {}
def closest_point_loop(self, x): r = np.linalg.norm(x - self.center, 2) if r==0: tx = self.center[0] + 1.0 r = 1.0 else: tx = x[0] xm = x.copy() xm[0] = tx dim = self.dim if xm[-1] < self.center[-1]: # "below" semicircle, if dim == 2: if xm[0] <= self.center[0]: # left cpx = self.center - np.array([self.radius, 0.0]) bdy = 1 else: # right cpx = self.center + np.array([self.radius, 0.0]) bdy = 2 elif dim == 3: xx = xm[0] - self.center[0] yy = xm[1] - self.center[1] th, r = cart2pol(xx,yy) xx, yy = pol2cart(th, self.radius) cpx = self.center + np.array([xx,yy,0]) bdy = 1 else: # general case possible? raise NotImplementedError( 'Dim {0} not implemented'.format(dim) ) else: # at or "above" semicircle: same as for whole circle c = self.radius / r cpx = c*(xm-self.center) + self.center bdy = 0 dist = np.linalg.norm(cpx - x, 2) return cpx, dist, bdy, {}
def closest_point_loop(self, x): r = np.linalg.norm(x - self.center, 2) if r == 0: tx = self.center[0] + 1.0 r = 1.0 else: tx = x[0] xm = x.copy() xm[0] = tx dim = self.dim if xm[-1] < self.center[-1]: # "below" semicircle, if dim == 2: if xm[0] <= self.center[0]: # left cpx = self.center - np.array([self.radius, 0.0]) bdy = 1 else: # right cpx = self.center + np.array([self.radius, 0.0]) bdy = 2 elif dim == 3: xx = xm[0] - self.center[0] yy = xm[1] - self.center[1] th, r = cart2pol(xx, yy) xx, yy = pol2cart(th, self.radius) cpx = self.center + np.array([xx, yy, 0]) bdy = 1 else: # general case possible? raise NotImplementedError( 'Dim {0} not implemented'.format(dim)) else: # at or "above" semicircle: same as for whole circle c = self.radius / r cpx = c * (xm - self.center) + self.center bdy = 0 dist = np.linalg.norm(cpx - x, 2) return cpx, dist, bdy, {}
def closestPointToCartesian(self, xx): x,y = xx - self.center th,r = cart2pol(x, y) if ((th >= self.angle1) and (th <= self.angle2)): (cpx,cpy) = pol2cart(th, self.radius) bdy = 0 dist = norm( a([x, y]) - a([cpx,cpy]), 2) else: # check the two end points (cpx,cpy) = pol2cart(self.angle1, self.radius) dist = norm( a([x, y]) - a([cpx,cpy]), 2) bdy = 1 (cpx2,cpy2) = pol2cart(self.angle2, self.radius) dist2 = norm( a([x, y]) - a([cpx2,cpy2]), 2) if (dist2 < dist): cpx = cpx2 cpy = cpy2 dist = dist2 bdy = 2 cp = self.center + a([cpx,cpy]) return cp, dist, bdy, {}
def closestPointToCartesian(self, xx): x, y = xx - self.center th, r = cart2pol(x, y) if ((th >= self.angle1) and (th <= self.angle2)): (cpx, cpy) = pol2cart(th, self.radius) bdy = 0 dist = norm(a([x, y]) - a([cpx, cpy]), 2) else: # check the two end points (cpx, cpy) = pol2cart(self.angle1, self.radius) dist = norm(a([x, y]) - a([cpx, cpy]), 2) bdy = 1 (cpx2, cpy2) = pol2cart(self.angle2, self.radius) dist2 = norm(a([x, y]) - a([cpx2, cpy2]), 2) if (dist2 < dist): cpx = cpx2 cpy = cpy2 dist = dist2 bdy = 2 cp = self.center + a([cpx, cpy]) return cp, dist, bdy, {}