def doCompute(self, drawer, landmark, figure, **args): (lowerLeft, lowerRight), (width, height) = math2d.boundingBox(landmark) scale = pow(pow(width, 2) + pow(height, 2), 0.5) scaleFactor = scale * 0.1 polygon = math2d.boxToPolygon( (lowerLeft - scaleFactor, lowerRight - scaleFactor), (width + 2 * scaleFactor, height + 2 * scaleFactor)) out = {} figureSteps = list( math2d.stepAlongLine(figure, math2d.length(figure) / 100.0)) out["endPointsInLandmarkBoundingBox"] = 0 for p in figureSteps[90:]: if math2d.isInteriorPoint(polygon, p): out["endPointsInLandmarkBoundingBox"] += 1 drawer.drawPoint("endPointsInLandmarkBoundingBox", p) drawer.drawLine("endPointsInLandmarkBoundingBox", math2d.polygonToLine(polygon)) out["startPointsInLandmarkBoundingBox"] = 0 for p in figureSteps[:10]: if math2d.isInteriorPoint(polygon, p): out["startPointsInLandmarkBoundingBox"] += 1 drawer.drawPoint("startPointsInLandmarkBoundingBox", p) drawer.drawLine("startPointsInLandmarkBoundingBox", math2d.polygonToLine(polygon)) return out
def get_topological_map(self): print "clusters", self.clusters stmap, stmap_cnt, stmap_locs = self.clusters.get_topological_map() #stmap_locs[45.0] = na.array([72.5, 84.4]) #stmap_locs[63.0] = na.array([66.64, 110]) up_idx = float(int(max(stmap.keys()) + 1)) #[45.0, 16.0, 46.0, 2.0] for node in stmap.keys(): stmap[up_idx] = list(stmap[node]) + [up_idx] stmap_locs[up_idx] = na.append(stmap_locs[node], 4.0) for elt in stmap[up_idx]: stmap[elt] = na.append(stmap[elt], up_idx) up_idx += 1 tmap = dict(stmap) tmap_locs = {} for key, loc in stmap_locs.iteritems(): if self.boundingBox == None or math2d.isInteriorPoint( self.boundingBox, loc[0:2]): if len(loc) == 2: tmap_locs[key] = na.append(loc, 1) else: tmap_locs[key] = loc else: del tmap[key] for key1, lst in tmap.iteritems(): tmap[key1] = [x for x in lst if x != key] return tmap, None, tmap_locs
def doCompute(self, drawer, landmark, figure, **args): figureSteps = list( math2d.stepAlongLine(figure, math2d.length(figure) / 100.0)) points = [ math2d.closestPointOnPolygon(landmark, p) for p in figureSteps ] interiorPoints = math2d.interiorPoints(landmark, figureSteps) landmarkCentroid = math2d.centroid(landmark) distances = [math2d.squaredist(p, (0, 0)) for p in points] bborigin, (width, height) = math2d.boundingBox(na.append(landmark, figure, 0)) scale = pow(pow(width, 2) + pow(height, 2), 0.5) out = {} i, dist = math2d.argMin(distances) drawer.distanceFeature(out, "minimumDistanceToLandmark", "Closest point on landmark", "Figure End", figureSteps[i], points[i], scale) for p in interiorPoints: drawer.drawPoint("numInteriorPoints", p) if len(interiorPoints) == 0: drawer.drawText("numInteriorPoints", landmarkCentroid, "No interior points.") #if out["numInteriorPoints"] > 0: #out["numInteriorPoints"] = 1 out["numInteriorPoints"] = len(interiorPoints) if math2d.isInteriorPoint(landmark, figureSteps[i]): out["minimumDistanceToLandmark"] = 0 if math2d.isInteriorPoint(landmark, figure[-1]): out["distFigureEndToLandmark"] = 0 differences = [d1 - d2 for d1, d2 in zip(distances, distances[1:])] differences = [d / d if d != 0 else 0 for d in differences] if len(differences) == 0: out["averageDistanceDifference"] = 0 else: out["averageDistanceDifference"] = math2d.mean(differences) for p1, p2 in zip(points, points[1:]): drawer.drawLine("averageDistanceDifference", [p1, p2]) return out
def compute(self, situation, offset): a1 = situation.agent(self.figure) a2 = situation.agent(self.landmark) geom = a2.geometry(offset) if math2d.isInteriorPoint(a1.location(offset)): return True else: return False
def doCompute(self, drawer, landmark, figure, **args): out = {} bborigin, (width, height) = math2d.boundingBox(na.append(landmark, figure, 0)) scale = pow(pow(width, 2) + pow(height, 2), 0.5) landmarkCentroid = math2d.centroid(landmark) drawer.distanceFeature(out, "distFigureEndToLandmarkCentroid", "Landmark center of mass", "Figure", landmarkCentroid, figure[-1], scale) drawer.distanceFeature(out, "distFigureStartToLandmarkCentroid", "Landmark center of mass", "Figure", landmarkCentroid, figure[0], scale) if math2d.isInteriorPoint(landmark, figure[0]): drawer.distanceFeature(out, "distFigureStartToLandmark", "Closest point on landmark", "Figure Start", figure[0], figure[0], scale) else: cp = math2d.closestPointOnPolygon(landmark, figure[0]) drawer.distanceFeature(out, "distFigureStartToLandmark", "Closest point on landmark", "Figure Start", figure[0], cp, scale) if math2d.isInteriorPoint(landmark, figure[-1]): drawer.distanceFeature(out, "distFigureEndToLandmark", "Closest point on landmark", "Figure End", figure[-1], figure[-1], scale) else: cp = math2d.closestPointOnPolygon(landmark, figure[-1]) drawer.distanceFeature(out, "distFigureEndToLandmark", "Closest point on landmark", "Figure End", cp, figure[-1], scale) return out
def get_topological_map(self): print "clusters", self._clusters # nodes manually annotated for the hsp d2 map. nodes = [(2.22, 6.32), (1.19, 4.16), (1.14, 1.68), (4.28, 3.18), (5.55, 4.75), (5.8, 7.45), (10.1, 3.67), (7.58, 5.86), (9.36, 2.17), (7.53, 0.602), (10.8, 5.19), (11.9, 3.38), (6.69, 3.71)] stmap_locs = {} for i, node in enumerate(nodes): i = float(i) stmap_locs[i] = node stmap = {} for skey, sloc in stmap_locs.iteritems(): for ekey, eloc in stmap_locs.iteritems(): X, Y = self._clusters.skel.compute_path(sloc, eloc) length = math2d.length(na.transpose((X, Y))) if length < 10: stmap.setdefault(skey, []) stmap[skey].append(ekey) up_idx = float(int(max(stmap.keys()) + 1)) for node in stmap.keys(): stmap[up_idx] = list(stmap[node]) + [up_idx] stmap_locs[up_idx] = na.append(stmap_locs[node], 6.0) for elt in stmap[up_idx]: stmap[elt] = na.append(stmap[elt], up_idx) up_idx += 1 tmap = dict(stmap) tmap_locs = {} for key, loc in stmap_locs.iteritems(): if self.boundingBox == None or math2d.isInteriorPoint( self.boundingBox, loc[0:2]): if len(loc) == 2: tmap_locs[key] = na.append(loc, 4.5) else: tmap_locs[key] = loc else: del tmap[key] for key1, lst in tmap.iteritems(): tmap[key1] = [x for x in lst if x != key] return tmap, None, tmap_locs
def get_topological_map(self): print "clusters", self._clusters # nodes manually annotated for the hsp d2 map. # (361, 314), # camp charlie, gator can't make turn nodes = [(564, 497), (435, 396), (417, 359), (591, 319), (445, 237), (253, 253)] stmap_locs = {} for i, node in enumerate(nodes): i = float(i) stmap_locs[i] = node stmap = {} for skey, sloc in stmap_locs.iteritems(): for ekey, eloc in stmap_locs.iteritems(): X, Y = self._clusters.skel.compute_path(sloc, eloc) #length = math2d.length(na.transpose((X, Y))) #if length < 10: stmap.setdefault(skey, []) stmap[skey].append(ekey) up_idx = float(int(max(stmap.keys()) + 1)) for node in stmap.keys(): stmap[up_idx] = list(stmap[node]) + [up_idx] stmap_locs[up_idx] = na.append(stmap_locs[node], 6.0) for elt in stmap[up_idx]: stmap[elt] = na.append(stmap[elt], up_idx) up_idx += 1 tmap = dict(stmap) tmap_locs = {} for key, loc in stmap_locs.iteritems(): if self.boundingBox == None or math2d.isInteriorPoint( self.boundingBox, loc[0:2]): if len(loc) == 2: tmap_locs[key] = na.append(loc, 4.5) else: tmap_locs[key] = loc else: del tmap[key] for key1, lst in tmap.iteritems(): tmap[key1] = [x for x in lst if x != key] return tmap, None, tmap_locs
def recenter(self, location): x, y = location xstart, xend, ystart, yend = self.axes.axis() bbox = [(xstart, ystart), (xend, ystart), (xend, yend), (xstart, yend)] width = xend - xstart height = yend - ystart print 'axis', self.axes.axis() if not math2d.isInteriorPoint(bbox, (x, y)): print "invoking recentering" xstart = x - width / 2 xend = x + width / 2 ystart = y - height / 2 yend = y + height / 2 self.axes.axis([xstart, xend, ystart, yend]) self.background = None return True else: return False
def computeAxes2(landmarkGeom, figureGeom): if math2d.length(figureGeom) == 0: raise InsaneExample("Degenerate figure.") # three algorithms # 1. connect endpoints and extend # 2. fit line with regression # 3. get a line with my fancy optimization (minimize area and distance) #intersectPoints = math2d.intersectLines(math2d.polygonToLine(landmarkGeom), # figureGeom) #m, b = math2d.fitLine(figureGeom) #intersectPoints = math2d.intersectPolygonAnalytic(landmarkGeom, m, b) endPointSegment = [figureGeom[0], figureGeom[-1]] if not math2d.isVertical(endPointSegment) and not math2d.isDegenerate( endPointSegment): m, b = math2d.lineEquation(endPointSegment) intersectPoints = math2d.intersectPolygonAnalytic(landmarkGeom, m, b) else: gYs = [p[1] for p in landmarkGeom] mungedFigAxis = [(figureGeom[0][0], min(gYs) - 1), (figureGeom[0][0], max(gYs) + 1)] intersectPoints = math2d.intersectLines( math2d.polygonToLine(landmarkGeom), mungedFigAxis) #m, b = math2d.fitLine(figureGeom) #intersectPoints = math2d.intersectPolygonAnalytic(landmarkGeom, m, b) if len(intersectPoints) == 0: if math2d.isInteriorPoint(landmarkGeom, figureGeom[0]): minor = [ math2d.closestPointOnPolygon(landmarkGeom, figureGeom[0]), math2d.closestPointOnPolygon(landmarkGeom, figureGeom[-1]) ] else: return None, None elif (len(intersectPoints) == 1 or (intersectPoints[0] == intersectPoints[1])): if math2d.isInteriorPoint(landmarkGeom, figureGeom[0]): minorStart = math2d.closestPointOnPolygon(landmarkGeom, figureGeom[0]) else: minorStart = intersectPoints[0] minorEnd = math2d.closestPointOnPolygon(landmarkGeom, figureGeom[-1]) minor = [minorStart, minorEnd] else: # ignoring figure after first two intersect points minor = [intersectPoints[0], intersectPoints[1]] D = tklib_get_distance(na.transpose(intersectPoints), [0, 0]) i_st = na.argmin(D) i_end = na.argmax(D) minor = [intersectPoints[i_st], intersectPoints[i_end]] try: major = math2d.perpendicular(minor) intersectPoint = math2d.intersectSegment(major, minor) except: print "minor", minor print "figure", figureGeom raise if intersectPoint == None: # if math2d.sorta_eq(math2d.length(figureGeom), 0): # degenerate case - start and end at the same point. # can happen for zero length tracks. # But other tracks too - still happens when you test zero length. raise InsaneExample("No axes" + ` [major, minor, intersectPoint] `) else: return major, minor