def split_edge(self,u,v,newp = None): def replace_edge(loop,which,new1,new2): loop.pop(which) loop.insert(which,new2) loop.insert(which,new1) def add_edge_lookup(poly,new): if not new in self.eg_poly_lookup: self.eg_poly_lookup[new] = [] self.eg_poly_lookup[new].append(poly) if newp is None: newp = dpv.midpoint(*self.points.get_points(u,v)) edex = self.delete_edge(u,v) w = self.points.add_point(newp) ne1dex = self.add_edge(u,w) ne2dex = self.add_edge(w,v) if edex in self.eg_poly_lookup: poly = self.eg_poly_lookup[edex] for pygnx in poly: pygn = self.polygons[pygnx] eb,ibs = pygn if edex in eb: epdex = eb.index(edex) replace_edge(eb,epdex,ne1dex,ne2dex) else: for ib in ibs: if edex in ib: ipdex = ib.index(edex) replace_edge(ib,ipdex,ne1dex,ne2dex) break add_edge_lookup(pygnx,ne1dex) add_edge_lookup(pygnx,ne2dex) return (u,w),(w,v)
def _endpoints(self,v1,v2): self.v1 = v1.copy() self.v2 = v2.copy() self.l = dpv.distance(v1,v2) self.center = dpv.midpoint(v1,v2) self.tangent = dpv.v1_v2(v1,v2).normalize() self.normal = self.tangent.copy() self.normal.rotate_z(dpr.rad(90)).normalize()
def split_edge(self,ex): e = self.mesh.edges[ex] nds = self.mesh.mask(0,None,e,None) mp = dpv.midpoint(*self.geom.points.get_points(nds[0].px,nds[1].px)) mx = self.mesh.add_node(self.geom.points.add_point(mp)) e1 = self.mesh.add_edge(nds[0].index,mx) e2 = self.mesh.add_edge(nds[1].index,mx) self.mesh.replace_edge(e.index,e1,e2)
def inflate(convex,radius): enorms = dpv.edge_normals_xy(convex) for cdx in range(len(convex)): lead = enorms[cdx] rear = enorms[cdx-1] norm = dpv.midpoint(lead,rear).normalize() convex[cdx].translate(norm.scale_u(radius)) return convex
def search(self,ps,ps0,p1,p2,w): p = dpv.midpoint(p1,p2) pfnd = p.nearest(ps0) if not pfnd == -1:return pfnd z = self.search_offset(p,p1,p2,w) pcnt = len(ps) ps.append((p,z,w)) ps0.append(p) return pcnt
def _door_gap(self,dx,fh): wh,ch = self.h,fh dw,dh,dz = 1.5,dpr.clamp(0.8*(wh-fh-ch),2,3),fh dpts = self._gap(dx,dw) if dpts is None:return dargs = ((),{'z':dz,'w':dw,'h':dh,'gap':dpts,'wall':self}) dpos = dpv.midpoint(*dpts) newdoor = pd.door(*dargs[0],**dargs[1]).translate(dpos) self.portals.append(newdoor)
def _window_gap(self,dx,fh): wh,winh = self.h,0.5*self.h dw,dh,dz = 0.75*winh,winh,max([(wh-winh)/2.0,fh+0.5]) wpts = self._gap(dx,dw) if wpts is None:return wargs = ((),{'z':dz,'w':dw,'h':dh,'gap':wpts,'wall':self}) wpos = dpv.midpoint(*wpts) newwindow = pw.window(*wargs[0],**wargs[1]).translate(wpos) self.portals.append(newwindow)
def face_away(self, side): rp = side[1]['room'] intpt = dpv.vector(rp[1]['x'], rp[1]['y'], 0.0) midpt = dpv.midpoint(*side[0]) tangt = dpv.v1_v2(*side[0]).normalize() norml = tangt.copy().rotate_z(dpr.rad(90)).normalize() tstpt = midpt.copy().translate(norml) side[1]['normal'] = norml if dpv.distance(intpt, midpt) > dpv.distance(intpt, tstpt): side[1]['normal'].flip()
def face_away(self,side): rp = side[1]['room'] intpt = dpv.vector(rp[1]['x'],rp[1]['y'],0.0) midpt = dpv.midpoint(*side[0]) tangt = dpv.v1_v2(*side[0]).normalize() norml = tangt.copy().rotate_z(dpr.rad(90)).normalize() tstpt = midpt.copy().translate(norml) side[1]['normal'] = norml if dpv.distance(intpt,midpt) > dpv.distance(intpt,tstpt): side[1]['normal'].flip()
def plot_edges(points,ax = None,lw = 1.0,center = False): if ax is None:ax = plot_axes() #pts = [p.to_tuple() for p in points] pts = [p.__iter__() for p in points] xs,ys,zs = zip(*pts) ax.plot(xs,ys,zs,marker = '+',lw = lw) if center: centers = [dpv.midpoint(points[x-1],points[x]) for x in range(1,len(points))] plot_points(centers,ax) return ax
def grow_length(self, plans, length, side): if length is None: l, w = self.bldg.l, self.bldg.w corners = dpr.square(l, w) sidept = dpv.midpoint(*side[0]) bdist = dpv.distance_to_border_xy(sidept, corners) length = rm.choice([8, 12, 16, 20, 24, 28, 32]) if bdist < 8: return elif length > bdist: length = bdist return length
def grow_length(self,plans,length,side): if length is None: l,w = self.bldg.l,self.bldg.w corners = dpr.square(l,w) sidept = dpv.midpoint(*side[0]) bdist = dpv.distance_to_border_xy(sidept,corners) length = rm.choice([8,12,16,20,24,28,32]) if bdist < 8:return elif length > bdist:length = bdist return length
def segments_outpolygon(segments,py): eb,ibs = py outpoly = [] for x in range(len(segments)): i1,i2 = segments[x] eisect = False for ex in range(len(eb)): ep1,ep2 = eb[ex-1],eb[ex] isect = segments_intersect_at(i1,i2,ep1,ep2) if not isect is None:eisect = True if not eisect and not dpr.inconcave_xy(dpv.midpoint(i1,i2),eb): outpoly.append((i1,i2)) return outpoly
def plot_edges(points, ax=None, lw=1.0, center=False): if ax is None: ax = plot_axes() #pts = [p.to_tuple() for p in points] pts = [p.__iter__() for p in points] xs, ys, zs = zip(*pts) ax.plot(xs, ys, zs, marker='+', lw=lw) if center: centers = [ dpv.midpoint(points[x - 1], points[x]) for x in range(1, len(points)) ] plot_points(centers, ax) return ax
def segments_outpolygon(segments, py): eb, ibs = py outpoly = [] for x in range(len(segments)): i1, i2 = segments[x] eisect = False for ex in range(len(eb)): ep1, ep2 = eb[ex - 1], eb[ex] isect = segments_intersect_at(i1, i2, ep1, ep2) if not isect is None: eisect = True if not eisect and not dpr.inconcave_xy(dpv.midpoint(i1, i2), eb): outpoly.append((i1, i2)) return outpoly
def dice_edges(verts,dices = 3): for di in range(dices): newpts = [] vcnt = len(verts) for tdx in range(vcnt): p1 = verts[tdx-1] p2 = verts[tdx] mpt = dpv.midpoint(p1,p2) newpts.append(p1) newpts.append(mpt) newpts.append(newpts.pop(0)) newpts.append(newpts.pop(0)) verts = newpts return verts
def locally_delaunay_edge(self,u,v): v1,v2 = self.points.get_points(u,v) cc = dpv.midpoint(v1,v2) cr = dpv.distance(cc,v1) polyxs = self.eg_poly_lookup[self.eg_lookup[(u,v)]] polyps = [self.get_polygon_points(px) for px in polyxs] for polyp in polyps: ebps,ibs = polyp for ep in ebps: if ep.near(v1) or ep.near(v2):continue if dpr.inside_circle(ep,cc,cr):return False for ibps in ibs: for ip in ibps: if ip.near(v1) or ip.near(v2):continue if dpr.inside_circle(ip,cc,cr):return False return True
def _extrude(self,loop,curve,control,ctrl = None,ns = None,us = None,m = None): nfstart = len(self.faces) tangents = dpv.edge_tangents(curve) tangents.append(tangents[-1].copy()) tangloop = [l.copy() for l in loop] tangloop = dpr.orient_loop(tangloop,tangents[0],control) tangloop = dpv.translate_coords(tangloop,curve[0]) tailloop = dpr.project_coords_plane_along( tangloop,curve[0],tangents[0],tangents[0]) n = len(curve) for step in range(1,n): c0,c1 = curve[step-1],curve[step] t0,t1 = tangents[step-1],tangents[step] halft = dpv.midpoint(t0,t1).normalize() n = halft tangloop = [l.copy() for l in loop] tangloop = dpr.orient_loop(tangloop,t0,control) tangloop = dpv.translate_coords(tangloop,c1) tiploop = dpr.project_coords_plane_along(tangloop,c1,n,t0) self._bridge(tiploop,tailloop,ns = ns,us = us,m = m) tailloop = [p.copy() for p in tiploop] nfend = len(self.faces) return range(nfstart,nfend)
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 esplit(lk,u,v): k = (u,v) if not k in lk: pts.append(dpv.midpoint(pts[u],pts[v])) lk[k] = len(pts)-1 return lk[k]
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 esplit(lk, u, v): k = (u, v) if not k in lk: pts.append(dpv.midpoint(pts[u], pts[v])) lk[k] = len(pts) - 1 return lk[k]
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