def grow(self,plans,length = None,side = None,force = False): side = self.grow_side(plans,side) if side is None:return False gleng = self.grow_length(plans,length,side) if gleng is None:return False self.face_away(side) v1,v2 = side[0] c1 = v2.copy() c2 = v1.copy() c3,c4 = dpr.extrude_edge(c1,c2,gleng,side[1]['normal']) newcorners = [c1,c2,c3,c4] x,y,z = dpv.center_of_mass(newcorners) xpj = dpv.project_coords(newcorners,dpv.x()) ypj = dpv.project_coords(newcorners,dpv.y()) l,w = xpj.y-xpj.x,ypj.y-ypj.x margs = ((),{'x':x,'y':y,'l':l,'w':w,'shafted':False}) self.create_bbox(margs) if not self.verify_growth(plans,margs):return False splan = self.should_shaft(plans,margs) cpairs = [(c2,c3),(c3,c4),(c4,c1)] iwargs = [] ewargs = [[cp, {'h':4.0,'w':0.5,'walltype':'exterior','room':margs}] for cp in cpairs] self.switch_walltype(plans,side) if self.resolve_walls(plans,ewargs,iwargs,margs): rps,eps,ips,sps = plans rps.append(margs) eps.extend(ewargs) ips.extend(iwargs) if splan:sps.append(splan) return True else:return False
def create_bbox(self, roomplan): x, y = roomplan[1]['x'], roomplan[1]['y'] l, w = roomplan[1]['l'] - 0.01, roomplan[1]['w'] - 0.01 cns = dpr.square(l, w, dpv.vector(x, y, 0)) xpj = dpv.project_coords(cns, dpv.x()) ypj = dpv.project_coords(cns, dpv.y()) zpj = dpv.project_coords(cns, dpv.z()) bb = dbb.bbox(xpj, ypj, zpj) roomplan[1]['bbox'] = bb
def create_bbox(self,roomplan): x,y = roomplan[1]['x'],roomplan[1]['y'] l,w = roomplan[1]['l']-0.01,roomplan[1]['w']-0.01 cns = dpr.square(l,w,dpv.vector(x,y,0)) xpj = dpv.project_coords(cns,dpv.x()) ypj = dpv.project_coords(cns,dpv.y()) zpj = dpv.project_coords(cns,dpv.z()) bb = dbb.bbox(xpj,ypj,zpj) roomplan[1]['bbox'] = bb
def valid_pair(py1,py2): eb1,eb2 = py1[0],py2[0] for x in range(len(eb2)): one,two = eb2[x-1],eb2[x] if one.near(two): print('invalid!!!') pdb.set_trace() ebn1 = dpr.polygon_normal(eb1) pj1 = dpv.project_coords(list(eb1),ebn1) pj2 = dpv.project_coords(list(eb2),ebn1) if not (dpr.isnear(pj2.x,pj2.y) and dpr.isnear(pj1.x,pj2.x)):return if ebn1.near(dpv.nz()):prot = dpq.q_from_av(dpr.PI,dpv.x()) elif not ebn1.near(dpv.z() ):prot = dpq.q_from_uu(ebn1,dpv.z()) else: prot = dpq.zero() return prot
def valid_pair(py1, py2): eb1, eb2 = py1[0], py2[0] for x in range(len(eb2)): one, two = eb2[x - 1], eb2[x] if one.near(two): print('invalid!!!') pdb.set_trace() ebn1 = dpr.polygon_normal(eb1) pj1 = dpv.project_coords(list(eb1), ebn1) pj2 = dpv.project_coords(list(eb2), ebn1) if not (dpr.isnear(pj2.x, pj2.y) and dpr.isnear(pj1.x, pj2.x)): return if ebn1.near(dpv.nz()): prot = dpq.q_from_av(dpr.PI, dpv.x()) elif not ebn1.near(dpv.z()): prot = dpq.q_from_uu(ebn1, dpv.z()) else: prot = dpq.zero() return prot
def grow(self, plans, length=None, side=None, force=False): side = self.grow_side(plans, side) if side is None: return False gleng = self.grow_length(plans, length, side) if gleng is None: return False self.face_away(side) v1, v2 = side[0] c1 = v2.copy() c2 = v1.copy() c3, c4 = dpr.extrude_edge(c1, c2, gleng, side[1]['normal']) newcorners = [c1, c2, c3, c4] x, y, z = dpv.center_of_mass(newcorners) xpj = dpv.project_coords(newcorners, dpv.x()) ypj = dpv.project_coords(newcorners, dpv.y()) l, w = xpj.y - xpj.x, ypj.y - ypj.x margs = ((), {'x': x, 'y': y, 'l': l, 'w': w, 'shafted': False}) self.create_bbox(margs) if not self.verify_growth(plans, margs): return False splan = self.should_shaft(plans, margs) cpairs = [(c2, c3), (c3, c4), (c4, c1)] iwargs = [] ewargs = [[ cp, { 'h': 4.0, 'w': 0.5, 'walltype': 'exterior', 'room': margs } ] for cp in cpairs] self.switch_walltype(plans, side) if self.resolve_walls(plans, ewargs, iwargs, margs): rps, eps, ips, sps = plans rps.append(margs) eps.extend(ewargs) ips.extend(iwargs) if splan: sps.append(splan) return True else: return False
def segment_split_polygon(s1,s2,py,plot = False): eb,ibs = py ltn = dpv.v1_v2(s1,s2).normalize() pyprj = dpv.project_coords(list(eb),ltn) midpt = dpv.midpoint(s1,s2) mpprj = dpv.dot(midpt,ltn) offst = mpprj-((pyprj.x+pyprj.y)/2.0) l1del = ltn.copy().scale_u(offst+100) l2del = ltn.copy().scale_u(offst-100) l1far = midpt.copy().translate(l1del) l2far = midpt.copy().translate(l2del) pysegs = polygon_segments(py) lnsegs = [(l1far,l2far)] #pysegs = polygon_segments(py)+[(s1,s2)] ebn = dpr.polygon_normal(eb) prot = dpr.q_to_xy(ebn) dpr.rotate_segments(pysegs,prot) dpr.rotate_coords(list(lnsegs[0]),prot) isects = intersections(pysegs+lnsegs) '''# print('truly time to line split!!!',len(isects)) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges([l1far,l2far],ax,lw = 4.0) for p in isects:plot_point(p,ax) plt.show() '''# if len(isects) == 0: prot.flip() dpr.rotate_segments(pysegs,prot) #dpr.rotate_coords(list(lnsegs[0]),prot) return pysegs = break_segments(pysegs,isects) lnsegs = break_segments(lnsegs,isects) # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE lnsegs = segments_inpolygon(lnsegs,py) leftof = segments_leftofline( pysegs,l1far,l2far) rightof = segments_rightofline(pysegs,l1far,l2far) if len(leftof) == 0 or len(rightof) == 0: #print('maybe??') prot.flip() dpr.rotate_segments(pysegs,prot) #dpr.rotate_coords(list(lnsegs[0]),prot) return if len(leftof) < 3 or len(rightof) < 3: print('HERE IT IS',len(pysegs),s1,s2) ax = plot_axes() #for p in pysegs:plot_edges(p,ax) #for s in leftof:plot_edges(s,ax,lw = 3.0) #for s in rightof:plot_edges(s,ax,lw = 6.0) for s in lnsegs:plot_edges(s,ax,lw = 6.0) #plot_edges([l1far,l2far],ax,lw = 4.0) for p in isects:plot_point(p,ax) plt.show() pdb.set_trace() leftloops = construct_loops( leftof+lnsegs,plot) rightloops = construct_loops(rightof+lnsegs,plot) newpolys = [] prot.flip() for seg in leftloops: newpolys.append(dpr.rotate_polygon((seg,()),prot)) for seg in rightloops: newpolys.append(dpr.rotate_polygon((seg,()),prot)) #dpr.rotate_coords(list(lnsegs[0]),prot) '''# print('MADE NEW POLYSSSS',len(newpolys)) ax = plot_axes() for ll in newpolys:plot_polygon_full(ll,ax) plt.show() '''# return newpolys
def line_intersects_polygon_at(l1,l2,py,intins): eb,ibs = py ltn = dpv.v1_v2(l1,l2).normalize() pyprj = dpv.project_coords(list(eb),ltn) midpt = dpv.midpoint(l1,l2) mpprj = dpv.dot(midpt,ltn) offst = mpprj-((pyprj.x+pyprj.y)/2.0) l1del = ltn.copy().scale_u(offst+100) l2del = ltn.copy().scale_u(offst-100) l1far = midpt.copy().translate(l1del) l2far = midpt.copy().translate(l2del) pysegs = polygon_segments(py) lnsegs = [(l1far,l2far)] prot = prot_to_xy(py) '''# print('before you rotate!!',prot.__str__()) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) #for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN') '''# dpr.rotate_segments(pysegs,prot) dpr.rotate_coords(list(lnsegs[0]),prot) isects = intersections(pysegs+lnsegs) #isects = [x for x in isects if dpr.orient3d(eb[0],eb[1],eb[2],x) == 0] if len(isects) == 1: print('while you were rotated!!') ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN.5',len(isects)) if len(isects) == 0: prot.flip() dpr.rotate_segments(pysegs,prot) dpr.rotate_coords(list(lnsegs[0]),prot) dpv.rotate_coords(isects,prot) return else: #print('actuallyinsssssisects',isects) actuallyintins = dpr.inconcave_xy(dpv.midpoint(*isects),eb) '''# print('wattttch',actuallyintins) ax = plot_axes() plot_polygon(list(eb),ax) plot_edges(isects,ax,lw = 4.0) plt.show() '''# prot.flip() dpr.rotate_segments(pysegs,prot) dpr.rotate_coords(list(lnsegs[0]),prot) dpv.rotate_coords(isects,prot) if intins and not actuallyintins:return if len(isects) == 1: print('after you rotate!!') ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN2',len(isects)) #ebn = dpr.polygon_normal(eb) #pyj = dpv.project_coords(list(eb),ebn) #isects2 = [x for x in isects if dpr.isnear(dpv.dot(x,ebn),pyj.x)] #isects2 = [x for x in isects if dpr.orient3d(eb[0],eb[1],eb[2],x) == 0] #if intins and not dpr.inconcave_xy(dpv.midpoint(*isects),eb):return #intins = dpr.inconcave_xy(dpv.midpoint(*isects),eb) #if not intins:return '''# print('breakin some shit!!',intins) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() '''# return isects
def segment_split_polygon(s1, s2, py, plot=False): eb, ibs = py ltn = dpv.v1_v2(s1, s2).normalize() pyprj = dpv.project_coords(list(eb), ltn) midpt = dpv.midpoint(s1, s2) mpprj = dpv.dot(midpt, ltn) offst = mpprj - ((pyprj.x + pyprj.y) / 2.0) l1del = ltn.copy().scale_u(offst + 100) l2del = ltn.copy().scale_u(offst - 100) l1far = midpt.copy().translate(l1del) l2far = midpt.copy().translate(l2del) pysegs = polygon_segments(py) lnsegs = [(l1far, l2far)] #pysegs = polygon_segments(py)+[(s1,s2)] ebn = dpr.polygon_normal(eb) prot = dpr.q_to_xy(ebn) dpr.rotate_segments(pysegs, prot) dpr.rotate_coords(list(lnsegs[0]), prot) isects = intersections(pysegs + lnsegs) '''# print('truly time to line split!!!',len(isects)) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges([l1far,l2far],ax,lw = 4.0) for p in isects:plot_point(p,ax) plt.show() '''# if len(isects) == 0: prot.flip() dpr.rotate_segments(pysegs, prot) #dpr.rotate_coords(list(lnsegs[0]),prot) return pysegs = break_segments(pysegs, isects) lnsegs = break_segments(lnsegs, isects) # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE # THIS MISSES TANGENTIAL INTERSECTION WITH THE LINE lnsegs = segments_inpolygon(lnsegs, py) leftof = segments_leftofline(pysegs, l1far, l2far) rightof = segments_rightofline(pysegs, l1far, l2far) if len(leftof) == 0 or len(rightof) == 0: #print('maybe??') prot.flip() dpr.rotate_segments(pysegs, prot) #dpr.rotate_coords(list(lnsegs[0]),prot) return if len(leftof) < 3 or len(rightof) < 3: print('HERE IT IS', len(pysegs), s1, s2) ax = plot_axes() #for p in pysegs:plot_edges(p,ax) #for s in leftof:plot_edges(s,ax,lw = 3.0) #for s in rightof:plot_edges(s,ax,lw = 6.0) for s in lnsegs: plot_edges(s, ax, lw=6.0) #plot_edges([l1far,l2far],ax,lw = 4.0) for p in isects: plot_point(p, ax) plt.show() pdb.set_trace() leftloops = construct_loops(leftof + lnsegs, plot) rightloops = construct_loops(rightof + lnsegs, plot) newpolys = [] prot.flip() for seg in leftloops: newpolys.append(dpr.rotate_polygon((seg, ()), prot)) for seg in rightloops: newpolys.append(dpr.rotate_polygon((seg, ()), prot)) #dpr.rotate_coords(list(lnsegs[0]),prot) '''# print('MADE NEW POLYSSSS',len(newpolys)) ax = plot_axes() for ll in newpolys:plot_polygon_full(ll,ax) plt.show() '''# return newpolys
def line_intersects_polygon_at(l1, l2, py, intins): eb, ibs = py ltn = dpv.v1_v2(l1, l2).normalize() pyprj = dpv.project_coords(list(eb), ltn) midpt = dpv.midpoint(l1, l2) mpprj = dpv.dot(midpt, ltn) offst = mpprj - ((pyprj.x + pyprj.y) / 2.0) l1del = ltn.copy().scale_u(offst + 100) l2del = ltn.copy().scale_u(offst - 100) l1far = midpt.copy().translate(l1del) l2far = midpt.copy().translate(l2del) pysegs = polygon_segments(py) lnsegs = [(l1far, l2far)] prot = prot_to_xy(py) '''# print('before you rotate!!',prot.__str__()) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) #for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN') '''# dpr.rotate_segments(pysegs, prot) dpr.rotate_coords(list(lnsegs[0]), prot) isects = intersections(pysegs + lnsegs) #isects = [x for x in isects if dpr.orient3d(eb[0],eb[1],eb[2],x) == 0] if len(isects) == 1: print('while you were rotated!!') ax = plot_axes() for p in pysegs: plot_edges(p, ax) plot_edges(lnsegs[-1], ax, lw=5.0) for p in isects: plot_point(p, ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN.5', len(isects)) if len(isects) == 0: prot.flip() dpr.rotate_segments(pysegs, prot) dpr.rotate_coords(list(lnsegs[0]), prot) dpv.rotate_coords(isects, prot) return else: #print('actuallyinsssssisects',isects) actuallyintins = dpr.inconcave_xy(dpv.midpoint(*isects), eb) '''# print('wattttch',actuallyintins) ax = plot_axes() plot_polygon(list(eb),ax) plot_edges(isects,ax,lw = 4.0) plt.show() '''# prot.flip() dpr.rotate_segments(pysegs, prot) dpr.rotate_coords(list(lnsegs[0]), prot) dpv.rotate_coords(isects, prot) if intins and not actuallyintins: return if len(isects) == 1: print('after you rotate!!') ax = plot_axes() for p in pysegs: plot_edges(p, ax) plot_edges(lnsegs[-1], ax, lw=5.0) for p in isects: plot_point(p, ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() print('AMEN2', len(isects)) #ebn = dpr.polygon_normal(eb) #pyj = dpv.project_coords(list(eb),ebn) #isects2 = [x for x in isects if dpr.isnear(dpv.dot(x,ebn),pyj.x)] #isects2 = [x for x in isects if dpr.orient3d(eb[0],eb[1],eb[2],x) == 0] #if intins and not dpr.inconcave_xy(dpv.midpoint(*isects),eb):return #intins = dpr.inconcave_xy(dpv.midpoint(*isects),eb) #if not intins:return '''# print('breakin some shit!!',intins) ax = plot_axes() for p in pysegs:plot_edges(p,ax) plot_edges(lnsegs[-1],ax,lw = 5.0) for p in isects:plot_point(p,ax) #for p in isects2:plot_point(p,ax,marker = 's') plt.show() '''# return isects
def break_polygon(py,p2s,subop = 'union'): breakers = [x for x in range(len(p2s))] broken = [dpr.copy_polygon(py)] pieces = [] while broken: piece = broken.pop(0) eb1,ibs1 = piece ebn1 = dpr.polygon_normal(eb1) brokeone = False for x in breakers: breaker = p2s[x] eb2,ibs2 = breaker ebn2 = dpr.polygon_normal(eb2) pj1 = dpv.project_coords(list(eb1),ebn1) pj2 = dpv.project_coords(list(eb2),ebn1) if dpr.isnear(pj2.x,pj2.y): if not dpr.isnear(pj1.x,pj2.x): print('disjoint polygons!') else: if subop == 'union': pu = dtl.polygon_union(piece,breaker) elif subop == 'intersection': pu = dtl.polygon_intersection(piece,breaker) elif subop == 'difference': pu = dtl.polygon_difference(piece,breaker) else:print('unknown subop!',subop) print('known subop!',subop) print('coplanar polygons!',pu) if not pu is None: ax = dtl.plot_axes() #dtl.plot_polygon_full(piece,ax) #dtl.plot_polygon_full(breaker,ax) dtl.plot_polygon(list(pu[0]),ax,lw = 5.0) dtl.plot_polygon(list(pu[1][0]),ax,lw = 1.0) plt.show() #broken.append(pu) pieces.append(pu) print("breaker will continue to intersect this guy...") brokeone = True break else: print('skew polygons') plintersect = dtl.planes_intersection(ebn1,eb1[0],ebn2,eb2[0]) if not plintersect is None: pli1,pli2 = plintersect intins = subop == 'difference' #intins = dpr.inconcave_xy(dpv.midpoint(pli1,pli2),eb2) #print('inis',subop,intins) breakersect = dtl.line_intersects_polygon_at( pli1,pli2,breaker,intins) if not breakersect is None: if not len(breakersect) == 2: print('breakersect is confusing!!') ax = dtl.plot_axes() dtl.plot_polygon_full(breaker,ax) dtl.plot_line(pli1,pli2,25,ax,lw = 4.0) for x in range(len(breakersect)): dtl.plot_point(breakersect[x],ax) plt.show() pdb.set_trace() b1,b2 = breakersect lsp = dtl.segment_split_polygon(b1,b2,piece) if not lsp is None: '''# print('lsssspppp',len(lsp)) ax = dtl.plot_axes() dtl.plot_line(b1,b2,25,ax,lw = 8.0) for ls in lsp: ax = dtl.plot_polygon_full(ls,ax,lw = 1.0) ax = dtl.plot_polygon_full(breaker,ax,lw = 3.0) plt.show() '''# broken.extend(lsp) brokeone = True #pdb.set_trace() break if not brokeone: pieces.append(piece) print('nice piece',len(pieces)) '''# if len(pieces) > 1: print('broke some shit??') ax = dtl.plot_axes() for p in pieces:dtl.plot_polygon_full(p,ax) plt.show() '''# return pieces
def _aaabbb(self): xproj = dpv.project_coords(self.pcoords,dpv.xhat) yproj = dpv.project_coords(self.pcoords,dpv.yhat) zproj = dpv.project_coords(self.pcoords,dpv.zhat) bb = dbb.bbox(xproj,yproj,zproj) return bb
def triangle_cover(boundary,side_length): side_length = float(side_length) perp_side_length = math.sqrt(3)*side_length/2.0 bproj_x = dpv.project_coords(boundary,dpv.xhat) bproj_y = dpv.project_coords(boundary,dpv.yhat) xrng = bproj_x.y - bproj_x.x yrng = bproj_y.y - bproj_y.x total_offset = dpv.vector( bproj_x.x - side_length, bproj_y.x - perp_side_length,0) xtcnt = int(xrng/side_length+0.5) + 3 ytcnt = int(yrng/perp_side_length+0.5) + 3 broffset = dpv.vector(side_length/2.0,perp_side_length,0) corners = [] xs = [x*side_length for x in range(xtcnt)] pts = [] seedrow = [dpv.vector(x,0,0).translate(total_offset) for x in xs] rowleng = len(seedrow) pts.extend(seedrow) bottomrow = [x for x in range(rowleng)] pts.extend([b.copy().translate(broffset) for b in seedrow]) toprow = [x+rowleng for x in range(rowleng)] def next_rows(bottom,top,even): sign = -1 if even else 1 broffset.translate_x(sign*side_length) newpts = [pts[t].copy().translate(broffset) for t in top] pcnt = len(pts) pts.extend(newpts) newrng = range(pcnt,len(pts)) newtop = [x for x in newrng] newbottom = [x for x in top] return newbottom,newtop for rdx in range(ytcnt): even = rdx % 2 == 0 for vdx in range(1,len(bottomrow)): c1 = bottomrow[vdx-1] c2 = bottomrow[vdx] c3 = toprow[vdx-1] c4 = toprow[vdx] if even: corners.append((c1,c2,c3)) corners.append((c3,c2,c4)) else: corners.append((c1,c2,c4)) corners.append((c3,c1,c4)) if not rdx == ytcnt - 1: bottomrow,toprow = next_rows(bottomrow,toprow,even) extras = [] keeppts = [] for cnx in range(len(corners)): ptri = [pts[x] for x in corners[cnx]] ins = [dpv.inside(c,boundary) for c in ptri] if not ins.count(True) > 0:extras.append(cnx) else: for x in corners[cnx]: if not x in keeppts: keeppts.append(x) extras = sorted(extras) extras.reverse() for x in extras:corners.pop(x) extrapts = [x for x in range(len(pts)) if not x in keeppts] extrapts = sorted(extrapts) extrapts.reverse() # THIS MESSES UP THE INDICES OF CORNERS!!! #for x in extrapts:pts.pop(x) #plot_points(pts+boundary) return pts,corners