def pypoly(data,kind="solid",mapmulti=None): assert data.count("\t")==0 xpos=1 ypos=data.count("\n") chs=dict() for c in data: if c=='\t': raise Exception("tab not allowed") if c=='\n': xpos=1 ypos-=1 else: if mapmulti: cs=mapmulti.get(c,[c]) else: cs=[c] for c in cs: if c!=' ': chs.setdefault(c,[]).append((xpos,ypos)) xpos+=1 lines=[] for c,poss in chs.items(): endpoints=[] for pos in poss: cnt=0 for aroundx in [-1,0,1]: for aroundy in [-1,0,1]: if aroundx==0 and aroundy==0: continue if (pos[0]+aroundx,pos[1]+aroundy) in poss: cnt+=1 if cnt==1 or cnt==0: endpoints.append(Vertex(*pos)) if len(endpoints)==1: endpoints.append(endpoints[0]) print len(endpoints) assert(len(endpoints)==2) lines.append((c,Line(endpoints[0],endpoints[1]))) lines.sort() #print "C,poss: ",lines outv=[] last=None for (n1,line1),(n2,line2) in izip(lines,lines[1:]+lines[:1]): if not ((line1.get_v2()-line2.get_v1()).taxilength()==1 or (line1.get_v2()-line2.get_v2()).taxilength()==1): line1=Line(line1.get_v2(),line1.get_v1()) if (line1.get_v2()-line2.get_v1()).taxilength()==1: outline=Line(line1.get_v1(),line2.get_v1()) elif (line1.get_v2()-line2.get_v2()).taxilength()==1: outline=Line(line1.get_v1(),line2.get_v2()) else: raise Exception("Unexpected error - bad input-data?") #print "Line: %s:%s"%(n1,outline) outv.append(outline.get_v1()) poly=Polygon(vvector(outv)) #if not poly.is_ccw(): # raise Exception("shouldn't be cw") if kind=="hole": poly.set_kind(Polygon.HOLE) return poly
def dump_cells(bo): for cell in list(bo.dbg_step5_get_cells()): sqs=[] for edge in list(cell.dbg_get_edges()): line=Line(edge.get_v1(),edge.get_v2()) sqs.append(visualize.Line( line.get_v1().get_x(), line.get_v1().get_y(), line.get_v2().get_x(), line.get_v2().get_y(), (255,0,0))) #print "Cell cover:",list(cell.get_shapes()) #print "Cell type:",cell.get_classification() draw_things(sqs)
def vistest_merge_shapes(): for x in xrange(10): x=5 poly_a=Polygon(vvector([ Vertex(0+x,0),Vertex(2+x,0), Vertex(2+x,2),Vertex(0+x,2)])) poly_b=Polygon(vvector([ Vertex(1,1),Vertex(3,1), Vertex(3,4),Vertex(1,4)])) shape_a=Shape("shape_a",poly_a) shape_b=Shape("shape_b",poly_b) # OOOO # OOOO # OOOOOO # OOOO # OOOO bo=BooleanOp() #print "Shape_a,shape_b: ",shape_a,shape_b bo.step1_add_lines(shape_a,shape_b) bo.step2_intersect_lines() splitset=set([NondirLine(x.get_v1(),x.get_v2()) for x in bo.dbg_step2_get_split_lines()]) #print "Split result: %s"%("\n".join(str(l) for l in splitset),) assert not NondirLine(Vertex(0,2),Vertex(2,2)) in splitset bo.step3_create_edges() edges=list(bo.dbg_step3_and_4_get_edges()) nondiredges=list(NondirLine(x.get_v1(),x.get_v2()) for x in bo.dbg_step3_and_4_get_edges()) sqs=[] cnt=0 #print "Edges: %s"%(edges,) for edge in edges: line=Line(edge.get_v1(),edge.get_v2()) r=(25*cnt)%128+128 g=(128*cnt)%128+128 b=(64*cnt)%128+128 sqs.append(visualize.Line( line.get_v1().get_x(), line.get_v1().get_y(), line.get_v2().get_x(), line.get_v2().get_y(), (r,g,b))) sqs.append(visualize.Square( line.get_v1().get_x()-0.1, line.get_v1().get_y()-0.1, line.get_v1().get_x()+0.1, line.get_v1().get_y()+0.1, (r,g,b))) cnt+=1 draw_things(sqs) assert not (NondirLine(Vertex(0,2),Vertex(2,2)) in nondiredges) bo.step4_eliminate_deadends() bo.step5_create_cells() bo.step6_determine_cell_cover() bas=BooleanOrStrategy() bo.step7_classify_cells(bas) for cell in list(bo.dbg_step5_get_cells()): sqs=[] for edge in list(cell.dbg_get_edges()): line=Line(edge.get_v1(),edge.get_v2()) sqs.append(visualize.Line( line.get_v1().get_x(), line.get_v1().get_y(), line.get_v2().get_x(), line.get_v2().get_y(), (255,0,0))) #print "Cell cover:",list(cell.get_shapes()) #print "Cell type:",cell.get_classification() draw_things(sqs) bo.step8_merge_cells() bo.step9_calc_result() shape=bo.step9_get_result() polys=list(shape.get_polys()) plines=set() for poly in polys: #print "Poly: %s %s"%(poly.get_kind_str(),list(poly.get_lines())) plines=plines.union(frozenset(poly.get_lines())) sqs=[] edges=set(bo.dbg_step3_and_4_get_edges()) for line in plines: sqs.append(visualize.Line( line.get_v1().get_x(), line.get_v1().get_y(), line.get_v2().get_x(), line.get_v2().get_y(), (0,200,0))) draw_things(sqs)