def test_edge(self): e = geometry.Edge(DIR_DOWN, 15, 4, 15, 32) self.assertEqual('%s' % e, '#<Edge facing=down y=15 x0=4 x1=32>') self.assertEqual(e.length, 28) self.assertEqual('%s' % e.get_endpoints(), '[#<Point y=15 x=4>, #<Point y=15 x=32>]') self.assertEqual('%s' % e.dim_to_range(), '#<Range p0=4 p1=32>') f = geometry.Edge(DIR_RIGHT, 29, 58, 45, 58) self.assertEqual('%s' % f, '#<Edge facing=right y0=29 y1=45 x=58>') self.assertEqual(f.length, 16) self.assertEqual('%s' % f.get_endpoints(), '[#<Point y=29 x=58>, #<Point y=45 x=58>]') self.assertEqual('%s' % f.dim_to_range(), '#<Range p0=29 p1=45>') self.assertEqual('%s' % e.calc_uncovered(), '#<Edge facing=down y=15 x0=4 x1=32>') self.assertFalse(e.done) e.mark_done(num_range.NumRange(4, 10)) self.assertEqual('%s' % e.calc_uncovered(), '#<Edge facing=down y=15 x0=10 x1=32>') self.assertFalse(e.done) e.mark_done(num_range.NumRange(25, 32)) self.assertEqual('%s' % e.calc_uncovered(), '#<Edge facing=down y=15 x0=10 x1=25>') self.assertFalse(e.done) e.mark_done(num_range.NumRange(10, 25)) self.assertEqual('%s' % e.calc_uncovered(), 'None') self.assertTrue(e.done)
def find_overlapping_vertices(self, rect): overlaps = [] # Top, facing down line = geometry.Edge(DIR_DOWN, rect.top, rect.left, rect.top, rect.right) overlaps += self._find_vertices(line) # Right, facing left line = geometry.Edge(DIR_LEFT, rect.top, rect.right, rect.bot, rect.right) overlaps += self._find_vertices(line) # Bottom, facing up line = geometry.Edge(DIR_UP, rect.bot, rect.left, rect.bot, rect.right) overlaps += self._find_vertices(line) # Left, facing right line = geometry.Edge(DIR_RIGHT, rect.top, rect.left, rect.bot, rect.left) overlaps += self._find_vertices(line) return overlaps
def push_as_far_as_available(self, edge): # Get closet edge dir = edge.facing min_dist = INFINITY answer = None for e in self.edges: if e.facing != opposite_dir(dir): continue if e.not_in_range_of(edge): continue dist = e.distance_from(edge) if dist < 0: continue if dist < min_dist: min_dist = dist answer = e # Collect other rectangles accum = [] for r in self.rects: side = r.get_side(opposite_dir(dir)) dist = edge.distance_from(side) if dist < -7: # rectangles that we're already inside of continue if side.partially_in_range_of(edge): accum.append([dist, side]) # Sort them by distance accum.sort(key=lambda n: n[0]) # Find when we reach coverage coverage = num_range.MultiRange() for dist, other in accum: coverage.add(other.dim_to_range()) if coverage.fully_overlap(edge.dim_to_range()): if dist < min_dist: min_dist = dist answer = other if answer is None: return edge if edge.facing in [DIR_UP, DIR_DOWN]: answer = geometry.Edge(edge.facing, answer.y0, edge.x0, answer.y0, edge.x1) else: answer = geometry.Edge(edge.facing, edge.y0, answer.x0, edge.y1, answer.x0) return answer
def shorted_path(self): # use Dijkstra current_node = self.initial while current_node != self.goal: successor_list = self.get_successor(current_node) successor_min_dist = self.get_min_dist(current_node, successor_list) self.path.append(geometry.Edge(current_node, successor_min_dist)) self.close_list.append(current_node) current_node = successor_min_dist
def side_between(self, first, second, rotate_clockwise): if first.facing != opposite_dir(second.facing): raise errors.AlgorithmError('Side direction mismatch!') if rotate_clockwise: dir = rotate_dir_cw(first.facing) else: dir = rotate_dir_counter_cw(first.facing) if first.facing in DIR_UP: # +---+ # cc | | rc # | | # | | y0, y1 = first.y0, second.y0 if rotate_clockwise: # right-hand side from the top and bottom x0 = x1 = first.x1 else: # left-hand side from the top and bottom x0 = x1 = first.x0 elif first.facing == DIR_RIGHT: x0, x1 = second.x0, first.x0 if rotate_clockwise: # bottom side from the right and left y0 = y1 = first.y1 else: # top side from the right and left y0 = y1 = first.y0 elif first.facing == DIR_DOWN: # | | # | | # rc | | cc # +---+ y0, y1 = second.y0, first.y0 if rotate_clockwise: # left-hand side from the bottom and top x0 = x1 = first.x0 else: # right-hand side from the bottom and top x0 = x1 = first.x1 elif first.facing == DIR_LEFT: x0, x1 = first.x0, second.x0 if rotate_clockwise: # bottom side from the right and left y0 = y1 = first.y0 else: # top side from the right and left y0 = y1 = first.y1 return geometry.Edge(dir, y0, x0, y1, x1)
def add(self, dir, point_y, point_x, next_dir): self.count += 1 if self.count >= 128: raise errors.AlgorithmError('loop detected at y=%s, x=%s' % (point_y, point_x)) # Adjust if dir == DIR_DOWN: point_x += 1 if dir == DIR_LEFT: point_y += 1 if next_dir == DIR_DOWN: point_x += 1 elif next_dir == DIR_LEFT: point_y += 1 # Edge edge = geometry.Edge(rotate_dir_cw(dir), self.curr_y, self.curr_x, point_y, point_x) self.edges.append(edge) # Vertex kind = 'convex' if next_dir == rotate_dir_cw(dir) else 'reflex' self.vertices.append( geometry.Vertex(kind, point_y, point_x, len(self.vertices))) # Border if point_y < self.min_y: self.min_y = point_y if point_y > self.max_y: self.max_y = point_y if point_x < self.min_x: self.min_x = point_x if point_x > self.max_x: self.max_x = point_x # Done? self.curr_y = point_y self.curr_x = point_x if point_y == self.start_y and point_x == self.start_x: first = self.vertices[-1] self.vertices = [first] + self.vertices[:-1] return True
def expand_lower_dim(self, s, dist): if s.facing in [DIR_UP, DIR_DOWN]: return geometry.Edge(s.facing, s.y0, s.x0, s.y0, s.x1 + dist) else: return geometry.Edge(s.facing, s.y0, s.x0, s.y1 + dist, s.x0)
random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0)) point.groupId = 1 Smeshalist.addPoint3D(point) counter = 0 while counter < 1000: counter = counter + 1 point1 = geometry.Point3D(random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0)) point2 = geometry.Point3D(random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0)) edge = geometry.Edge(point1, point2) edge.groupId = 1 Smeshalist.addEdge(edge) counter = 0 while counter < 1000: counter = counter + 1 point1 = geometry.Point3D(random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0)) point2 = geometry.Point3D(random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0)) point3 = geometry.Point3D(random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0), random.uniform(-10.0, 10.0))