"""Object-level frontage and paint fitting in metres, independent of location.""" import numpy as np,cv2,shapely from scipy.ndimage import median_filter from shapely.geometry import LineString,Point,Polygon from shapely.ops import unary_union def sample_line(p,step=.1): line=LineString(p);t=np.linspace(0,line.length,max(2,int(np.ceil(line.length/step))+1)) return np.array([line.interpolate(x).coords[0][:2] for x in t]) def frontage_envelope(lines,ground_side,maximum_recess=1.0): """Keep the exposed first-floor envelope; retain unoccluded recesses. Input line planes are independently supported by vertical source surfaces. No building back or footprint closure is generated here. """ records=[] for i,line in enumerate(lines): q=np.asarray(line.coords)[:,:2];u=q[-1]-q[0];u/=np.linalg.norm(u);n=np.array([-u[1],u[0]]) points=sample_line(q);side=ground_side(points,n);records.append((q,u,n*side,points)) out=[];suppressed=0 for i,(q,u,n,points) in enumerate(records): hidden=np.zeros(len(points),bool) for j,(other,v,_,_) in enumerate(records): if i==j or abs(u@v)=0)&(t<=length)&(offset>.12)&(offset1 and np.linalg.norm(points[b-1]-points[a])>=1.2: out.append((LineString([points[a],points[b-1]]),[i])) # Merge nearly collinear observed runs. A recess wider than 18 cm remains a # distinct facade; an unobserved gap above 1.5 m is never bridged. changed=True while changed: changed=False for i,(a,idsa) in enumerate(out): pa=np.array(a.coords);u=pa[1]-pa[0];u/=np.linalg.norm(u);n=np.array([-u[1],u[0]]) for j in range(i+1,len(out)): b,idsb=out[j];pb=np.array(b.coords);v=pb[1]-pb[0];v/=np.linalg.norm(v) if abs(u@v).18:continue ranges=sorted([(0,a.length),tuple(sorted((pb-pa[0])@u))]);gap=ranges[1][0]-ranges[0][1] if gap>1.5:continue samples=np.concatenate([sample_line(pa,.2),sample_line(pb,.2)]) fit=cv2.fitLine(samples.astype('float32'),cv2.DIST_HUBER,0,.0001,.0001).ravel();axis=fit[:2];origin=fit[2:];normal=np.array([-axis[1],axis[0]]) if np.max(abs((samples-origin)@normal))>.18:continue t=(samples-origin)@axis;q=origin+np.array([t.min(),t.max()])[:,None]*axis candidate=LineString(q);conflict=False for k,(other,_) in enumerate(out): if k in [i,j]:continue cross=candidate.intersection(other) if cross.geom_type=='Point' and .3.02] if len(pieces)!=1:ambiguous+=1;continue q=pieces[0];centers.append(q.interpolate(.5,normalized=True).coords[0]);widths.append(q.length) if len(centers)<8 or ambiguous>len(centers)*.12:return p.simplify(.045,preserve_topology=True),'branched-observation' points=np.array(centers) offset=(points-o)@n;long=(points-o)@u offset=median_filter(offset,size=3,mode='nearest');points=o+long[:,None]*u+offset[:,None]*n approx=cv2.approxPolyDP(points.astype('float32'),tolerance,False).reshape(-1,2).astype(float) if len(approx)<2:return p,'unfitted' # Width of a sloping/zigzag stroke is its orthogonal width, not its slice width. tangent=np.gradient(points,axis=0);tangent/=np.maximum(np.linalg.norm(tangent,axis=1)[:,None],1e-8) normal_width=np.asarray(widths)*abs(tangent@u) width=float(np.median(normal_width));spread=float(np.quantile(normal_width,.9)-np.quantile(normal_width,.1)) if not .055<=width<=.65 or spread>max(.25,width*1.1):return p.simplify(.045,preserve_topology=True),'variable-width-observation' axis=LineString(approx) if not axis.is_simple:return p,'branched-observation' fitted=axis.buffer(width/2,cap_style=2,join_style=2,mitre_limit=2) # Shape agreement gate prevents regularising away arrowheads / real paint. agreement=p.intersection(fitted).area/max(p.union(fitted).area,1e-9) displacement=p.hausdorff_distance(fitted) if agreement<.58 or displacement>.3:return p.simplify(.045,preserve_topology=True),'rejected-strip-fit' return fitted,'parametric-paint-strip'