def __init__(self): super().__init__() cut_region = CutRegion() cut_region.GenerateRegularPolygon(4, 2.0) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(1.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 0.0)) sub_region.polygon.vertex_list.append( Vector(2.5, math.sqrt(3.0) * 3.0 / 2.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) # This shape has reflection symmetry, but no rotational symmetry. sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 0.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 8.0)) sub_region.polygon.vertex_list.append(Vector(0.0, 8.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(0.0, 8.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, 8.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, 0.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(2.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(2.0, 9.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 9.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) '''sub_region = SubRegion()
def __init__(self): super().__init__() # It's amazing how hard or easy 2 overlapping squares is. # It depends on _how_ the squares overlap! sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(3.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(3.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(0.0, 3.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(2.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(5.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(5.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(2.0, 4.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(3.0, -math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(1.0, math.sqrt(3.0))) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(1.0, math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(-3.0, math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(-1.0, -math.sqrt(3.0))) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) # A reflection symmetry of this rectangle won't be accessible. Hmmmm... sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-3.0, math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(-3.0, -math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(3.0, -math.sqrt(3.0))) sub_region.polygon.vertex_list.append(Vector(3.0, math.sqrt(3.0))) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def __init__(self): super().__init__() radius = 7.0 x = radius * math.cos(math.pi / 3.0) y = radius * math.sin(math.pi / 3.0) cut_region = CutRegion() cut_region.GenerateRegularPolygon(6, radius) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(radius, 0.0)) sub_region.polygon.vertex_list.append(Vector(x, y)) sub_region.polygon.vertex_list.append(Vector(x, -y)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(radius, 0.0)) sub_region.polygon.vertex_list.append(Vector(x, y)) sub_region.polygon.vertex_list.append(Vector(-x, y)) sub_region.polygon.vertex_list.append(Vector(-radius, 0.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(0.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, 0.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) cut_region = CutRegion() cut_region.region = Region() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(0.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 0.0)) cut_region.region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(2.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(2.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, -2.0)) cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, -1.0)) hole = Polygon() hole.vertex_list.append(Vector(0.0, 0.0)) hole.vertex_list.append(Vector(0.0, 1.0)) hole.vertex_list.append(Vector(-1.0, 1.0)) hole.vertex_list.append(Vector(-1.0, 0.0)) sub_region.hole_list.append(hole) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) transform = AffineTransform() transform.Rotation(Vector(0.0, 0.0), math.pi / 4.0) for cut_region in self.cut_region_list: cut_region.Transform(transform)
def __init__(self): super().__init__() cut_region = CutRegion() cut_region.GenerateRegularPolygon(8, 1.0) self.cut_region_list.append(cut_region) cut_region = CutRegion() cut_region.GenerateRegularPolygon(8, 2.0) self.cut_region_list.append(cut_region) cut_region = CutRegion() cut_region.GenerateRegularPolygon(8, 3.0) self.cut_region_list.append(cut_region) cut_region = CutRegion() cut_region.GenerateRegularPolygon(8, 4.0) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(4.0 * math.cos(math.pi / 4.0), 4.0 * math.sin(math.pi / 4))) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) transform = AffineTransform() transform.Rotation(Vector(0.0, 0.0), math.pi / 2.0 - math.pi / 8.0) for cut_region in self.cut_region_list: cut_region.Transform(transform)
def __init__(self): super().__init__() # TODO: This shape will have reflection symmetries that the interface can't access. Fix it. How? cut_region = CutRegion() cut_region.region = Region() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(3.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(8.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, 8.0)) cut_region.region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-3.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 8.0)) sub_region.polygon.vertex_list.append(Vector(-8.0, 3.0)) cut_region.region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(-8.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -8.0)) cut_region.region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, -8.0)) sub_region.polygon.vertex_list.append(Vector(8.0, -3.0)) cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-4.5, -4.5)) sub_region.polygon.vertex_list.append(Vector(1.5, -1.5)) sub_region.polygon.vertex_list.append(Vector(4.5, 4.5)) sub_region.polygon.vertex_list.append(Vector(-1.5, 1.5)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(4.5, -4.5)) sub_region.polygon.vertex_list.append(Vector(1.5, 1.5)) sub_region.polygon.vertex_list.append(Vector(-4.5, 4.5)) sub_region.polygon.vertex_list.append(Vector(-1.5, -1.5)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def GenerateRectangle(self, width, height): sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-width / 2.0, -height / 2.0)) sub_region.polygon.vertex_list.append(Vector(width / 2.0, -height / 2.0)) sub_region.polygon.vertex_list.append(Vector(width / 2.0, height / 2.0)) sub_region.polygon.vertex_list.append(Vector(-width / 2.0, height / 2.0)) self.region = Region() self.region.sub_region_list.append(sub_region)
def __init__(self): super().__init__() # This shape is almost a swashtica (I'm trying to avoid that for obvious reason) and # therefore only has rotational symmetry. sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(2.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(2.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(4.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(2.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(2.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-3.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(3.0, 0.0)) hole = Polygon() hole.vertex_list.append(Vector(-1.0, -1.0)) hole.vertex_list.append(Vector(-1.0, -2.0)) hole.vertex_list.append(Vector(1.0, -2.0)) hole.vertex_list.append(Vector(1.0, -1.0)) sub_region.hole_list.append(hole) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def Transform(self, object): from math2d_polygon import Polygon from math2d_region import Region, SubRegion from math2d_aa_rect import AxisAlignedRectangle if isinstance(object, Vector): return self.linear_transform.Transform(object) + self.translation elif isinstance(object, AffineTransform): transform = AffineTransform() transform.linear_transform.x_axis = self.linear_transform.Transform( object.linear_transform.x_axis) transform.linear_transform.y_axis = self.linear_transform.Transform( object.linear_transform.y_axis) transform.translation = self.Transform(object.translation) return transform elif isinstance(object, LineSegment): return LineSegment(self.Transform(object.point_a), self.Transform(object.point_b)) elif isinstance(object, Polygon): polygon = Polygon() for vertex in object.vertex_list: polygon.vertex_list.append(self.Transform(vertex)) return polygon elif isinstance(object, Region): region = Region() for sub_region in object.sub_region_list: region.sub_region_list.append(self.Transform(sub_region)) return region elif isinstance(object, SubRegion): sub_region = SubRegion() sub_region.polygon = self.Transform(object.polygon) for hole in object.hole_list: sub_region.hole_list.append(self.Transform(hole)) return sub_region elif isinstance(object, AxisAlignedRectangle): return AxisAlignedRectangle( min_point=self.Transform(object.min_point), max_point=self.Transform(object.max_point))
def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) transform = AffineTransform() transform.Translation(Vector(2.0, 0.0)) cut_region.Transform(transform) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) transform = AffineTransform() transform.Translation(Vector(-2.0, 0.0)) cut_region.Transform(transform) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) transform = AffineTransform() transform.Translation(Vector(2.0, -2.0)) cut_region.Transform(transform) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, -1.0)) sub_region.polygon.vertex_list.append(Vector(1.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 1.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) transform = AffineTransform() transform.Translation(Vector(-2.0, 2.0)) cut_region.Transform(transform) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-2.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(2.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(2.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 2.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 2.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(2.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 0.0)) sub_region.polygon.vertex_list.append(Vector(-2.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-4.0, -2.0)) sub_region.polygon.vertex_list.append(Vector(2.0, -2.0)) cut_region = CutRegion() cut_region.region = Region() cut_region.region.sub_region_list.append(sub_region) self.cut_region_list.append(cut_region)
def ApplyCuts(self, region): # Turn all the cuts into bi-directional borders. Discard cuts that don't cut anything. region.Tessellate() while True: for i, edge in enumerate(self.edge_list): if edge[2] == PlanarGraphEdgeLabel.CUT: break else: break del self.edge_list[i] edge_segment = self.EdgeSegment(edge) if region.ContainsPoint(edge_segment.Lerp(0.5)): self.edge_list.append( (edge[0], edge[1], PlanarGraphEdgeLabel.REGION_BORDER)) self.edge_list.append( (edge[1], edge[0], PlanarGraphEdgeLabel.REGION_BORDER)) # Now go read-off all the perimeter and hole polygons. from math2d_polygon import Polygon perimeter_list = [] hole_list = [] while True: for edge in self.edge_list: if edge[2] == PlanarGraphEdgeLabel.REGION_BORDER: break else: break polygon = Polygon() cycle_list, cycle_found = self.FindCycleContainingEdge(edge, True) polygon.vertex_list = [ self.vertex_list[edge[0]] for edge in cycle_list ] if polygon.IsWoundCCW(): polygon.Tessellate() perimeter_list.append(polygon) else: cycle_list, cycle_found = self.FindCycleContainingEdge( edge, False) polygon.vertex_list = [ self.vertex_list[edge[0]] for edge in cycle_list ] if polygon.IsWoundCW(): polygon.ReverseWinding() hole_list.append(polygon) else: raise Exception('Failed to process cycle containing edge.') for edge in cycle_list: i = self.FindEdge(edge, False, False) del self.edge_list[i] # Finally, merry all the holes to the appropriate perimeters. # TODO: This is a bit tricky. A hole may lie inside a perimeter, but that doesn't mean it belongs to that perimeter, # because it may lie inside a yet smaller perimeter that is contained in a hole of the other perimeter. Make sure # we assign holes to containing perimeters of the smallest area. I think this may fix the problem. from math2d_region import Region, SubRegion region = Region() for perimeter in perimeter_list: sub_region = SubRegion(perimeter) new_hole_list = [] for hole in hole_list: # I believe we need only test a single point on the hole. if perimeter.ContainsPoint( hole.vertex_list[0] ) and not perimeter.ContainsPointOnBorder(hole.vertex_list[0]): sub_region.hole_list.append(hole) else: new_hole_list.append(hole) hole_list = new_hole_list region.sub_region_list.append(sub_region) if len(hole_list) > 0: raise Exception('Failed to marry %d holes to perimeters.' % len(hole_list)) return region
def GenerateRegularPolygon(self, sides, radius): sub_region = SubRegion() sub_region.polygon.MakeRegularPolygon(sides, radius) self.region = Region() self.region.sub_region_list.append(sub_region)
def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-5.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(-5.0, 3.0)) hole = Polygon() hole.vertex_list.append(Vector(-4.0, -3.0)) hole.vertex_list.append(Vector(3.0, -3.0)) hole.vertex_list.append(Vector(3.0, -2.0)) hole.vertex_list.append(Vector(-3.0, -2.0)) hole.vertex_list.append(Vector(-3.0, 1.0)) hole.vertex_list.append(Vector(3.0, 1.0)) hole.vertex_list.append(Vector(3.0, 2.0)) hole.vertex_list.append(Vector(-4.0, 2.0)) sub_region.hole_list.append(hole) hole = Polygon() hole.vertex_list.append(Vector(-2.0, -1.0)) hole.vertex_list.append(Vector(0.0, -1.0)) hole.vertex_list.append(Vector(0.0, 0.0)) hole.vertex_list.append(Vector(-2.0, 0.0)) sub_region.hole_list.append(hole) hole = Polygon() hole.vertex_list.append(Vector(1.0, -1.0)) hole.vertex_list.append(Vector(3.0, -1.0)) hole.vertex_list.append(Vector(3.0, 0.0)) hole.vertex_list.append(Vector(1.0, 0.0)) sub_region.hole_list.append(hole) self.region = Region() self.region.sub_region_list.append(sub_region) self.cut_region = Region() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(5.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(5.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 4.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-6.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-6.0, 4.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-6.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(-6.0, -3.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.5, 0.5)) sub_region.polygon.vertex_list.append(Vector(0.5, -1.5)) sub_region.polygon.vertex_list.append(Vector(3.5, -1.5)) sub_region.polygon.vertex_list.append(Vector(3.5, 0.5)) self.cut_region.sub_region_list.append(sub_region) self.result_region = None self.polygon_list = []
class TestCaseA(TestCase): def __init__(self): super().__init__() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-5.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, -4.0)) sub_region.polygon.vertex_list.append(Vector(4.0, 3.0)) sub_region.polygon.vertex_list.append(Vector(-5.0, 3.0)) hole = Polygon() hole.vertex_list.append(Vector(-4.0, -3.0)) hole.vertex_list.append(Vector(3.0, -3.0)) hole.vertex_list.append(Vector(3.0, -2.0)) hole.vertex_list.append(Vector(-3.0, -2.0)) hole.vertex_list.append(Vector(-3.0, 1.0)) hole.vertex_list.append(Vector(3.0, 1.0)) hole.vertex_list.append(Vector(3.0, 2.0)) hole.vertex_list.append(Vector(-4.0, 2.0)) sub_region.hole_list.append(hole) hole = Polygon() hole.vertex_list.append(Vector(-2.0, -1.0)) hole.vertex_list.append(Vector(0.0, -1.0)) hole.vertex_list.append(Vector(0.0, 0.0)) hole.vertex_list.append(Vector(-2.0, 0.0)) sub_region.hole_list.append(hole) hole = Polygon() hole.vertex_list.append(Vector(1.0, -1.0)) hole.vertex_list.append(Vector(3.0, -1.0)) hole.vertex_list.append(Vector(3.0, 0.0)) hole.vertex_list.append(Vector(1.0, 0.0)) sub_region.hole_list.append(hole) self.region = Region() self.region.sub_region_list.append(sub_region) self.cut_region = Region() sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-1.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(5.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(5.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-1.0, 4.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-6.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 1.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, 4.0)) sub_region.polygon.vertex_list.append(Vector(-6.0, 4.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(-6.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -5.0)) sub_region.polygon.vertex_list.append(Vector(-3.0, -3.0)) sub_region.polygon.vertex_list.append(Vector(-6.0, -3.0)) self.cut_region.sub_region_list.append(sub_region) sub_region = SubRegion() sub_region.polygon.vertex_list.append(Vector(0.5, 0.5)) sub_region.polygon.vertex_list.append(Vector(0.5, -1.5)) sub_region.polygon.vertex_list.append(Vector(3.5, -1.5)) sub_region.polygon.vertex_list.append(Vector(3.5, 0.5)) self.cut_region.sub_region_list.append(sub_region) self.result_region = None self.polygon_list = [] def Render(self): if self.result_region is None: glColor3f(1.0, 1.0, 1.0) self.region.Render() glColor3f(1.0, 0.0, 0.0) self.cut_region.Render() else: glColor3f(0.0, 1.0, 0.0) self.result_region.Render() for polygon in self.polygon_list: glColor3f(random.uniform(0.0, 1.0), random.uniform(0.0, 1.0), random.uniform(0.0, 1.0)) polygon.mesh.Render() def Click(self): self.result_region = self.region.CutAgainst(self.cut_region) self.polygon_list = self.result_region.TessellatePolygons()