rm-graphical-computing-node 1.0.27 → 1.0.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.mjs +1 -1
  2. package/package.json +1 -1
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- var xi=Object.create;var ar=Object.defineProperty;var yi=Object.getOwnPropertyDescriptor;var bi=Object.getOwnPropertyNames;var wi=Object.getPrototypeOf,zi=Object.prototype.hasOwnProperty;var Fe=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var Mi=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of bi(e))!zi.call(t,r)&&r!==n&&ar(t,r,{get:()=>e[r],enumerable:!(o=yi(e,r))||o.enumerable});return t};var Ei=(t,e,n)=>(n=t!=null?xi(wi(t)):{},Mi(e||!t||!t.__esModule?ar(n,"default",{value:t,enumerable:!0}):n,t));var Lr=Fe((Ro,Do)=>{(function(t,e){typeof Ro=="object"&&typeof Do<"u"?Do.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self,t.RBush=e())})(Ro,function(){"use strict";function t(b,w,z,M,P){e(b,w,z||0,M||b.length-1,P||o)}function e(b,w,z,M,P){for(;M>z;){if(M-z>600){var E=M-z+1,S=w-z+1,I=Math.log(E),W=.5*Math.exp(2*I/3),v=.5*Math.sqrt(I*W*(E-W)/E)*(S-E/2<0?-1:1),T=Math.max(z,Math.floor(w-S*W/E+v)),R=Math.min(M,Math.floor(w+(E-S)*W/E+v));e(b,w,T,R,P)}var D=b[w],H=z,V=M;for(n(b,z,w),P(b[M],D)>0&&n(b,z,M);H<V;){for(n(b,H,V),H++,V--;P(b[H],D)<0;)H++;for(;P(b[V],D)>0;)V--}P(b[z],D)===0?n(b,z,V):(V++,n(b,V,M)),V<=w&&(z=V+1),w<=V&&(M=V-1)}}function n(b,w,z){var M=b[w];b[w]=b[z],b[z]=M}function o(b,w){return b<w?-1:b>w?1:0}var r=function(w){w===void 0&&(w=9),this._maxEntries=Math.max(4,w),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()};r.prototype.all=function(){return this._all(this.data,[])},r.prototype.search=function(w){var z=this.data,M=[];if(!y(w,z))return M;for(var P=this.toBBox,E=[];z;){for(var S=0;S<z.children.length;S++){var I=z.children[S],W=z.leaf?P(I):I;y(w,W)&&(z.leaf?M.push(I):x(w,W)?this._all(I,M):E.push(I))}z=E.pop()}return M},r.prototype.collides=function(w){var z=this.data;if(!y(w,z))return!1;for(var M=[];z;){for(var P=0;P<z.children.length;P++){var E=z.children[P],S=z.leaf?this.toBBox(E):E;if(y(w,S)){if(z.leaf||x(w,S))return!0;M.push(E)}}z=M.pop()}return!1},r.prototype.load=function(w){if(!(w&&w.length))return this;if(w.length<this._minEntries){for(var z=0;z<w.length;z++)this.insert(w[z]);return this}var M=this._build(w.slice(),0,w.length-1,0);if(!this.data.children.length)this.data=M;else if(this.data.height===M.height)this._splitRoot(this.data,M);else{if(this.data.height<M.height){var P=this.data;this.data=M,M=P}this._insert(M,this.data.height-M.height-1,!0)}return this},r.prototype.insert=function(w){return w&&this._insert(w,this.data.height-1),this},r.prototype.clear=function(){return this.data=m([]),this},r.prototype.remove=function(w,z){if(!w)return this;for(var M=this.data,P=this.toBBox(w),E=[],S=[],I,W,v;M||E.length;){if(M||(M=E.pop(),W=E[E.length-1],I=S.pop(),v=!0),M.leaf){var T=s(w,M.children,z);if(T!==-1)return M.children.splice(T,1),E.push(M),this._condense(E),this}!v&&!M.leaf&&x(M,P)?(E.push(M),S.push(I),I=0,W=M,M=M.children[0]):W?(I++,M=W.children[I],v=!1):M=null}return this},r.prototype.toBBox=function(w){return w},r.prototype.compareMinX=function(w,z){return w.minX-z.minX},r.prototype.compareMinY=function(w,z){return w.minY-z.minY},r.prototype.toJSON=function(){return this.data},r.prototype.fromJSON=function(w){return this.data=w,this},r.prototype._all=function(w,z){for(var M=[];w;)w.leaf?z.push.apply(z,w.children):M.push.apply(M,w.children),w=M.pop();return z},r.prototype._build=function(w,z,M,P){var E=M-z+1,S=this._maxEntries,I;if(E<=S)return I=m(w.slice(z,M+1)),i(I,this.toBBox),I;P||(P=Math.ceil(Math.log(E)/Math.log(S)),S=Math.ceil(E/Math.pow(S,P-1))),I=m([]),I.leaf=!1,I.height=P;var W=Math.ceil(E/S),v=W*Math.ceil(Math.sqrt(S));p(w,z,M,v,this.compareMinX);for(var T=z;T<=M;T+=v){var R=Math.min(T+v-1,M);p(w,T,R,W,this.compareMinY);for(var D=T;D<=R;D+=W){var H=Math.min(D+W-1,R);I.children.push(this._build(w,D,H,P-1))}}return i(I,this.toBBox),I},r.prototype._chooseSubtree=function(w,z,M,P){for(;P.push(z),!(z.leaf||P.length-1===M);){for(var E=1/0,S=1/0,I=void 0,W=0;W<z.children.length;W++){var v=z.children[W],T=c(v),R=h(w,v)-T;R<S?(S=R,E=T<E?T:E,I=v):R===S&&T<E&&(E=T,I=v)}z=I||z.children[0]}return z},r.prototype._insert=function(w,z,M){var P=M?w:this.toBBox(w),E=[],S=this._chooseSubtree(P,this.data,z,E);for(S.children.push(w),u(S,P);z>=0&&E[z].children.length>this._maxEntries;)this._split(E,z),z--;this._adjustParentBBoxes(P,E,z)},r.prototype._split=function(w,z){var M=w[z],P=M.children.length,E=this._minEntries;this._chooseSplitAxis(M,E,P);var S=this._chooseSplitIndex(M,E,P),I=m(M.children.splice(S,M.children.length-S));I.height=M.height,I.leaf=M.leaf,i(M,this.toBBox),i(I,this.toBBox),z?w[z-1].children.push(I):this._splitRoot(M,I)},r.prototype._splitRoot=function(w,z){this.data=m([w,z]),this.data.height=w.height+1,this.data.leaf=!1,i(this.data,this.toBBox)},r.prototype._chooseSplitIndex=function(w,z,M){for(var P,E=1/0,S=1/0,I=z;I<=M-z;I++){var W=a(w,0,I,this.toBBox),v=a(w,I,M,this.toBBox),T=g(W,v),R=c(W)+c(v);T<E?(E=T,P=I,S=R<S?R:S):T===E&&R<S&&(S=R,P=I)}return P||M-z},r.prototype._chooseSplitAxis=function(w,z,M){var P=w.leaf?this.compareMinX:d,E=w.leaf?this.compareMinY:f,S=this._allDistMargin(w,z,M,P),I=this._allDistMargin(w,z,M,E);S<I&&w.children.sort(P)},r.prototype._allDistMargin=function(w,z,M,P){w.children.sort(P);for(var E=this.toBBox,S=a(w,0,z,E),I=a(w,M-z,M,E),W=l(S)+l(I),v=z;v<M-z;v++){var T=w.children[v];u(S,w.leaf?E(T):T),W+=l(S)}for(var R=M-z-1;R>=z;R--){var D=w.children[R];u(I,w.leaf?E(D):D),W+=l(I)}return W},r.prototype._adjustParentBBoxes=function(w,z,M){for(var P=M;P>=0;P--)u(z[P],w)},r.prototype._condense=function(w){for(var z=w.length-1,M=void 0;z>=0;z--)w[z].children.length===0?z>0?(M=w[z-1].children,M.splice(M.indexOf(w[z]),1)):this.clear():i(w[z],this.toBBox)};function s(b,w,z){if(!z)return w.indexOf(b);for(var M=0;M<w.length;M++)if(z(b,w[M]))return M;return-1}function i(b,w){a(b,0,b.children.length,w,b)}function a(b,w,z,M,P){P||(P=m(null)),P.minX=1/0,P.minY=1/0,P.maxX=-1/0,P.maxY=-1/0;for(var E=w;E<z;E++){var S=b.children[E];u(P,b.leaf?M(S):S)}return P}function u(b,w){return b.minX=Math.min(b.minX,w.minX),b.minY=Math.min(b.minY,w.minY),b.maxX=Math.max(b.maxX,w.maxX),b.maxY=Math.max(b.maxY,w.maxY),b}function d(b,w){return b.minX-w.minX}function f(b,w){return b.minY-w.minY}function c(b){return(b.maxX-b.minX)*(b.maxY-b.minY)}function l(b){return b.maxX-b.minX+(b.maxY-b.minY)}function h(b,w){return(Math.max(w.maxX,b.maxX)-Math.min(w.minX,b.minX))*(Math.max(w.maxY,b.maxY)-Math.min(w.minY,b.minY))}function g(b,w){var z=Math.max(b.minX,w.minX),M=Math.max(b.minY,w.minY),P=Math.min(b.maxX,w.maxX),E=Math.min(b.maxY,w.maxY);return Math.max(0,P-z)*Math.max(0,E-M)}function x(b,w){return b.minX<=w.minX&&b.minY<=w.minY&&w.maxX<=b.maxX&&w.maxY<=b.maxY}function y(b,w){return w.minX<=b.maxX&&w.minY<=b.maxY&&w.maxX>=b.minX&&w.maxY>=b.minY}function m(b){return{children:b,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function p(b,w,z,M,P){for(var E=[w,z];E.length;)if(z=E.pop(),w=E.pop(),!(z-w<=M)){var S=w+Math.ceil((z-w)/M/2)*M;t(b,S,w,z,P),E.push(w,S,S,z)}}return r})});var kr=Fe((vo,Vo)=>{(function(t,e){typeof vo=="object"&&typeof Vo<"u"?Vo.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self,t.TinyQueue=e())})(vo,function(){"use strict";var t=function(o,r){if(o===void 0&&(o=[]),r===void 0&&(r=e),this.data=o,this.length=this.data.length,this.compare=r,this.length>0)for(var s=(this.length>>1)-1;s>=0;s--)this._down(s)};t.prototype.push=function(o){this.data.push(o),this.length++,this._up(this.length-1)},t.prototype.pop=function(){if(this.length!==0){var o=this.data[0],r=this.data.pop();return this.length--,this.length>0&&(this.data[0]=r,this._down(0)),o}},t.prototype.peek=function(){return this.data[0]},t.prototype._up=function(o){for(var r=this,s=r.data,i=r.compare,a=s[o];o>0;){var u=o-1>>1,d=s[u];if(i(a,d)>=0)break;s[o]=d,o=u}s[o]=a},t.prototype._down=function(o){for(var r=this,s=r.data,i=r.compare,a=this.length>>1,u=s[o];o<a;){var d=(o<<1)+1,f=s[d],c=d+1;if(c<this.length&&i(s[c],f)<0&&(d=c,f=s[c]),i(f,u)>=0)break;s[o]=f,o=d}s[o]=u};function e(n,o){return n<o?-1:n>o?1:0}return t})});var Fr=Fe((rh,Or)=>{Or.exports=function(e,n,o,r){var s=e[0],i=e[1],a=!1;o===void 0&&(o=0),r===void 0&&(r=n.length);for(var u=(r-o)/2,d=0,f=u-1;d<u;f=d++){var c=n[o+d*2+0],l=n[o+d*2+1],h=n[o+f*2+0],g=n[o+f*2+1],x=l>i!=g>i&&s<(h-c)*(i-l)/(g-l)+c;x&&(a=!a)}return a}});var qr=Fe((sh,Nr)=>{Nr.exports=function(e,n,o,r){var s=e[0],i=e[1],a=!1;o===void 0&&(o=0),r===void 0&&(r=n.length);for(var u=r-o,d=0,f=u-1;d<u;f=d++){var c=n[d+o][0],l=n[d+o][1],h=n[f+o][0],g=n[f+o][1],x=l>i!=g>i&&s<(h-c)*(i-l)/(g-l)+c;x&&(a=!a)}return a}});var Xr=Fe((ih,Gn)=>{var jr=Fr(),$r=qr();Gn.exports=function(e,n,o,r){return n.length>0&&Array.isArray(n[0])?$r(e,n,o,r):jr(e,n,o,r)};Gn.exports.nested=$r;Gn.exports.flat=jr});var Gr=Fe((Yn,Zr)=>{(function(t,e){typeof Yn=="object"&&typeof Zr<"u"?e(Yn):typeof define=="function"&&define.amd?define(["exports"],e):e((t=t||self).predicates={})})(Yn,function(t){"use strict";let n=33306690738754706e-32;function o(h,g,x,y,m){let p,b,w,z,M=g[0],P=y[0],E=0,S=0;P>M==P>-M?(p=M,M=g[++E]):(p=P,P=y[++S]);let I=0;if(E<h&&S<x)for(P>M==P>-M?(w=p-((b=M+p)-M),M=g[++E]):(w=p-((b=P+p)-P),P=y[++S]),p=b,w!==0&&(m[I++]=w);E<h&&S<x;)P>M==P>-M?(w=p-((b=p+M)-(z=b-p))+(M-z),M=g[++E]):(w=p-((b=p+P)-(z=b-p))+(P-z),P=y[++S]),p=b,w!==0&&(m[I++]=w);for(;E<h;)w=p-((b=p+M)-(z=b-p))+(M-z),M=g[++E],p=b,w!==0&&(m[I++]=w);for(;S<x;)w=p-((b=p+P)-(z=b-p))+(P-z),P=y[++S],p=b,w!==0&&(m[I++]=w);return p===0&&I!==0||(m[I++]=p),I}function r(h){return new Float64Array(h)}let s=33306690738754716e-32,i=22204460492503146e-32,a=11093356479670487e-47,u=r(4),d=r(8),f=r(12),c=r(16),l=r(4);t.orient2d=function(h,g,x,y,m,p){let b=(g-p)*(x-m),w=(h-m)*(y-p),z=b-w;if(b===0||w===0||b>0!=w>0)return z;let M=Math.abs(b+w);return Math.abs(z)>=s*M?z:-(function(P,E,S,I,W,v,T){let R,D,H,V,C,_,A,L,k,F,B,O,$,G,Q,j,Z,U,it=P-W,ht=S-W,St=E-v,ot=I-v;C=(Q=(L=it-(A=(_=134217729*it)-(_-it)))*(F=ot-(k=(_=134217729*ot)-(_-ot)))-((G=it*ot)-A*k-L*k-A*F))-(B=Q-(Z=(L=St-(A=(_=134217729*St)-(_-St)))*(F=ht-(k=(_=134217729*ht)-(_-ht)))-((j=St*ht)-A*k-L*k-A*F))),u[0]=Q-(B+C)+(C-Z),C=($=G-((O=G+B)-(C=O-G))+(B-C))-(B=$-j),u[1]=$-(B+C)+(C-j),C=(U=O+B)-O,u[2]=O-(U-C)+(B-C),u[3]=U;let Mt=(function(Tt,nt){let ft=nt[0];for(let rt=1;rt<Tt;rt++)ft+=nt[rt];return ft})(4,u),xt=i*T;if(Mt>=xt||-Mt>=xt||(R=P-(it+(C=P-it))+(C-W),H=S-(ht+(C=S-ht))+(C-W),D=E-(St+(C=E-St))+(C-v),V=I-(ot+(C=I-ot))+(C-v),R===0&&D===0&&H===0&&V===0)||(xt=a*T+n*Math.abs(Mt),(Mt+=it*V+ot*R-(St*H+ht*D))>=xt||-Mt>=xt))return Mt;C=(Q=(L=R-(A=(_=134217729*R)-(_-R)))*(F=ot-(k=(_=134217729*ot)-(_-ot)))-((G=R*ot)-A*k-L*k-A*F))-(B=Q-(Z=(L=D-(A=(_=134217729*D)-(_-D)))*(F=ht-(k=(_=134217729*ht)-(_-ht)))-((j=D*ht)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-Z),C=($=G-((O=G+B)-(C=O-G))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let vt=o(4,u,4,l,d);C=(Q=(L=it-(A=(_=134217729*it)-(_-it)))*(F=V-(k=(_=134217729*V)-(_-V)))-((G=it*V)-A*k-L*k-A*F))-(B=Q-(Z=(L=St-(A=(_=134217729*St)-(_-St)))*(F=H-(k=(_=134217729*H)-(_-H)))-((j=St*H)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-Z),C=($=G-((O=G+B)-(C=O-G))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let Pt=o(vt,d,4,l,f);C=(Q=(L=R-(A=(_=134217729*R)-(_-R)))*(F=V-(k=(_=134217729*V)-(_-V)))-((G=R*V)-A*k-L*k-A*F))-(B=Q-(Z=(L=D-(A=(_=134217729*D)-(_-D)))*(F=H-(k=(_=134217729*H)-(_-H)))-((j=D*H)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-Z),C=($=G-((O=G+B)-(C=O-G))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let zt=o(Pt,f,4,l,c);return c[zt-1]})(h,g,x,y,m,p,M)},t.orient2dfast=function(h,g,x,y,m,p){return(g-p)*(x-m)-(h-m)*(y-p)},Object.defineProperty(t,"__esModule",{value:!0})})});var ts=Fe((lh,Wo)=>{"use strict";var Yr=Lr(),Kn=kr(),bl=Xr(),wl=Gr().orient2d;Kn.default&&(Kn=Kn.default);Wo.exports=Jr;Wo.exports.default=Jr;function Jr(t,e,n){e=Math.max(0,e===void 0?2:e),n=n||0;var o=Tl(t),r=new Yr(16);r.toBBox=function(p){return{minX:p[0],minY:p[1],maxX:p[0],maxY:p[1]}},r.compareMinX=function(p,b){return p[0]-b[0]},r.compareMinY=function(p,b){return p[1]-b[1]},r.load(t);for(var s=[],i=0,a;i<o.length;i++){var u=o[i];r.remove(u),a=Qr(u,a),s.push(a)}var d=new Yr(16);for(i=0;i<s.length;i++)d.insert(Io(s[i]));for(var f=e*e,c=n*n;s.length;){var l=s.shift(),h=l.p,g=l.next.p,x=Co(h,g);if(!(x<c)){var y=x/f;u=zl(r,l.prev.p,h,g,l.next.next.p,y,d),u&&Math.min(Co(u,h),Co(u,g))<=y&&(s.push(l),s.push(Qr(u,l)),r.remove(u),d.remove(l),d.insert(Io(l)),d.insert(Io(l.next)))}}l=a;var m=[];do m.push(l.p),l=l.next;while(l!==a);return m.push(l.p),m}function zl(t,e,n,o,r,s,i){for(var a=new Kn([],Ml),u=t.data;u;){for(var d=0;d<u.children.length;d++){var f=u.children[d],c=u.leaf?Ho(f,n,o):El(n,o,f);c>s||a.push({node:f,dist:c})}for(;a.length&&!a.peek().node.children;){var l=a.pop(),h=l.node,g=Ho(h,e,n),x=Ho(h,o,r);if(l.dist<g&&l.dist<x&&Kr(n,h,i)&&Kr(o,h,i))return h}u=a.pop(),u&&(u=u.node)}return null}function Ml(t,e){return t.dist-e.dist}function El(t,e,n){if(Ur(t,n)||Ur(e,n))return 0;var o=Un(t[0],t[1],e[0],e[1],n.minX,n.minY,n.maxX,n.minY);if(o===0)return 0;var r=Un(t[0],t[1],e[0],e[1],n.minX,n.minY,n.minX,n.maxY);if(r===0)return 0;var s=Un(t[0],t[1],e[0],e[1],n.maxX,n.minY,n.maxX,n.maxY);if(s===0)return 0;var i=Un(t[0],t[1],e[0],e[1],n.minX,n.maxY,n.maxX,n.maxY);return i===0?0:Math.min(o,r,s,i)}function Ur(t,e){return t[0]>=e.minX&&t[0]<=e.maxX&&t[1]>=e.minY&&t[1]<=e.maxY}function Kr(t,e,n){for(var o=Math.min(t[0],e[0]),r=Math.min(t[1],e[1]),s=Math.max(t[0],e[0]),i=Math.max(t[1],e[1]),a=n.search({minX:o,minY:r,maxX:s,maxY:i}),u=0;u<a.length;u++)if(Pl(a[u].p,a[u].next.p,t,e))return!1;return!0}function un(t,e,n){return wl(t[0],t[1],e[0],e[1],n[0],n[1])}function Pl(t,e,n,o){return t!==o&&e!==n&&un(t,e,n)>0!=un(t,e,o)>0&&un(n,o,t)>0!=un(n,o,e)>0}function Io(t){var e=t.p,n=t.next.p;return t.minX=Math.min(e[0],n[0]),t.minY=Math.min(e[1],n[1]),t.maxX=Math.max(e[0],n[0]),t.maxY=Math.max(e[1],n[1]),t}function Tl(t){for(var e=t[0],n=t[0],o=t[0],r=t[0],s=0;s<t.length;s++){var i=t[s];i[0]<e[0]&&(e=i),i[0]>o[0]&&(o=i),i[1]<n[1]&&(n=i),i[1]>r[1]&&(r=i)}var a=[e,n,o,r],u=a.slice();for(s=0;s<t.length;s++)bl(t[s],a)||u.push(t[s]);return Rl(u)}function Qr(t,e){var n={p:t,prev:null,next:null,minX:0,minY:0,maxX:0,maxY:0};return e?(n.next=e.next,n.prev=e,e.next.prev=n,e.next=n):(n.prev=n,n.next=n),n}function Co(t,e){var n=t[0]-e[0],o=t[1]-e[1];return n*n+o*o}function Ho(t,e,n){var o=e[0],r=e[1],s=n[0]-o,i=n[1]-r;if(s!==0||i!==0){var a=((t[0]-o)*s+(t[1]-r)*i)/(s*s+i*i);a>1?(o=n[0],r=n[1]):a>0&&(o+=s*a,r+=i*a)}return s=t[0]-o,i=t[1]-r,s*s+i*i}function Un(t,e,n,o,r,s,i,a){var u=n-t,d=o-e,f=i-r,c=a-s,l=t-r,h=e-s,g=u*u+d*d,x=u*f+d*c,y=f*f+c*c,m=u*l+d*h,p=f*l+c*h,b=g*y-x*x,w,z,M,P,E=b,S=b;b===0?(z=0,E=1,P=p,S=y):(z=x*p-y*m,P=g*p-x*m,z<0?(z=0,P=p,S=y):z>E&&(z=E,P=p+x,S=y)),P<0?(P=0,-m<0?z=0:-m>g?z=E:(z=-m,E=g)):P>S&&(P=S,-m+x<0?z=0:-m+x>g?z=E:(z=-m+x,E=g)),w=z===0?0:z/E,M=P===0?0:P/S;var I=(1-w)*t+w*n,W=(1-w)*e+w*o,v=(1-M)*r+M*i,T=(1-M)*s+M*a,R=v-I,D=T-W;return R*R+D*D}function Sl(t,e){return t[0]===e[0]?t[1]-e[1]:t[0]-e[0]}function Rl(t){t.sort(Sl);for(var e=[],n=0;n<t.length;n++){for(;e.length>=2&&un(e[e.length-2],e[e.length-1],t[n])<=0;)e.pop();e.push(t[n])}for(var o=[],r=t.length-1;r>=0;r--){for(;o.length>=2&&un(o[o.length-2],o[o.length-1],t[r])<=0;)o.pop();o.push(t[r])}return o.pop(),e.pop(),e.concat(o)}});import*as qe from"three";import*as Ne from"three";var _t=class t{constructor(e=0,n=0,o=0){this.x=e,this.y=n,this.z=o}set(e,n,o){return this.x=e,this.y=n,this.z=o,this}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this}clone(){return new t(this.x,this.y,this.z)}add(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this}sub(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this}subVectors(e,n){return this.x=e.x-n.x,this.y=e.y-n.y,this.z=e.z-n.z,this}multiplyScalar(e){return this.x*=e,this.y*=e,this.z*=e,this}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}crossVectors(e,n){let o=e.x,r=e.y,s=e.z,i=n.x,a=n.y,u=n.z;return this.x=r*u-s*a,this.y=s*i-o*u,this.z=o*a-r*i,this}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z}length(){return Math.sqrt(this.lengthSq())}normalize(){let e=this.length();return e>0&&(this.x/=e,this.y/=e,this.z/=e),this}distanceTo(e){return Math.sqrt(this.distanceToSquared(e))}distanceToSquared(e){let n=this.x-e.x,o=this.y-e.y,r=this.z-e.z;return n*n+o*o+r*r}toArray(){return[this.x,this.y,this.z]}fromArray(e,n=0){return this.x=e[n],this.y=e[n+1],this.z=e[n+2],this}applyAxisAngle(e,n){let o=this.x,r=this.y,s=this.z,i=e.x,a=e.y,u=e.z,d=Math.cos(n),f=Math.sin(n),c=i*o+a*r+u*s;return this.x=i*c*(1-d)+o*d+(-u*r+a*s)*f,this.y=a*c*(1-d)+r*d+(u*o-i*s)*f,this.z=u*c*(1-d)+s*d+(-a*o+i*r)*f,this}},An=class{constructor(e=new _t(1,0,0),n=0){this.normal=e,this.constant=n}set(e,n){return this.normal.copy(e),this.constant=n,this}setFromNormalAndCoplanarPoint(e,n){return this.normal.copy(e),this.constant=-n.dot(this.normal),this}setFromCoplanarPoints(e,n,o){let r=new _t().subVectors(n,e),s=new _t().subVectors(o,e),i=new _t().crossVectors(r,s).normalize();return this.normal.copy(i),this.constant=-e.dot(this.normal),this}distanceToPoint(e){return this.normal.dot(e)+this.constant}projectPoint(e,n=new _t){let o=this.distanceToPoint(e);return n.copy(e),n.x-=this.normal.x*o,n.y-=this.normal.y*o,n.z-=this.normal.z*o,n}},_n=()=>typeof performance<"u"&&performance.now?performance.now():Date.now(),yo=class{constructor(){this.results=null,this.clusterResults=null}initLimits1(e,n){let o,r;return e<.3?o=5:e<.5?o=8:e<.8?o=10:e<1?o=15:e<2?o=30:e<3?o=35:e<4?o=40:e<5?o=45:e<6?o=50:e<7?o=60:e<8.5?o=70:e<10?o=80:e<15?o=100:e<20?o=120:e<25?o=140:e<30?o=160:e<35?o=180:e<40?o=200:e<45?o=230:e<50?o=250:o=350,n<1?r=30:n<2?r=40:n<2.5?r=45:n<3?r=60:n<3.6?r=70:r=80,{horizontalSubdivisions:o,verticalSubdivisions:r}}initLimits2(e,n){let s=Math.round(Math.max(5,Math.min(350,e/.08))),i=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:s,verticalSubdivisions:i}}initLimits3(e,n){let o;e<2?o=.06:e<10?o=.1:o=.15;let r=Math.round(Math.max(5,Math.min(350,e/o))),s=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:r,verticalSubdivisions:s}}initLimits4(e,n){let o=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(e)))),r=Math.round(Math.max(30,Math.min(80,20*Math.sqrt(n))));return{horizontalSubdivisions:o,verticalSubdivisions:r}}createSubdividedWallPlane(e,n,o,r=null){let s=[];if(!e||!e.start||!e.end||isNaN(e.rooftopPz))return s;let i=new _t(e.start.x,e.start.y,e.start.z),a=new _t(e.end.x,e.end.y,e.end.z),u=new _t(e.end.x,e.end.y,e.rooftopPz),d=new _t(e.start.x,e.start.y,e.rooftopPz),f=[i,a,u,d];for(let P=0;P<f.length;P++)if(!this.isValidVector3(f[P]))return console.error(`\u5899\u4F53\u57FA\u7840\u70B9${P}\u65E0\u6548:`,f[P]),s;let c=i.distanceTo(a),l=Math.abs(e.rooftopPz-e.start.z);if(c===0||l===0)return s;let h=c/o,g=l/n,x=new _t().subVectors(a,i).normalize(),y=i.x,m=i.y,p=i.z,b=x.x,w=x.y,z=x.z,M=(P,E)=>new _t(y+b*P,m+w*P,p+z*P+E);for(let P=0;P<n;P++){let E=P*g,S=(P+1)*g;for(let I=0;I<o;I++){let W=I*h,v=(I+1)*h;s.push({points:[M(W,E),M(v,E),M(v,S),M(W,S)],userData:{type:"wallSegment",row:P,col:I,width:h,height:g}})}}return s}analyzePointDistribution(e,n,o=.01,r={}){let s=r.projectPlane||null;this.results={totalSegments:0,segmentsWithPoints:0,segmentsWithoutPoints:0,missingSegments:[],existSegments:[],pointsInSegments:new Map,segmentsByPoint:new Map,segmentDetails:new Map,tolerance:o,analysisTime:null};let i=_n(),a=[],u=new Map,d=1/0,f=-1/0,c=1/0,l=-1/0;for(let W=0;W<e.length;W++){let v=e[W];if(v.userData&&v.userData.type==="wallSegment"){let T=this.getWorldVerticesNew(v.points);if(T.length<4)continue;let R=new _t().add(T[0]).add(T[1]).add(T[2]).add(T[3]).multiplyScalar(.25);v.userData.center=R,v.vertices=T;let D=v.userData.row,H=v.userData.col,V=`row${D}_col${H}`;a.push(v),u.set(V,v),D<d&&(d=D),D>f&&(f=D),H<c&&(c=H),H>l&&(l=H)}}if(this.results.totalSegments=a.length,a.length===0)return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results;let h=`row${d}_col${c}`,g=u.get(h)||a[0];if(!g||!g.vertices||g.vertices.length<4)return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results;let x=g.vertices[0],y=g.vertices[1],m=g.vertices[3],p=new _t().subVectors(y,x).normalize(),b=new _t().subVectors(m,x).normalize(),w=g.userData.width,z=g.userData.height,M=f-d+1,P=l-c+1,E=new An;E.setFromCoplanarPoints(x,y,m);let S=new _t,I=new _t;for(let W=0;W<n.length;W+=3){let v=new _t(n[W],n[W+1],n[W+2]),T=s?s.projectPoint(v,I):v;if(Math.abs(E.distanceToPoint(T))>o)continue;S.subVectors(T,x);let D=S.dot(p),H=S.dot(b),V=Math.ceil((D-o)/w-1),C=Math.floor((D+o)/w),_=Math.ceil((H-o)/z-1),A=Math.floor((H+o)/z);if(C<0||V>P-1||A<0||_>M-1)continue;let L=Math.max(0,V),k=Math.min(P-1,C),F=Math.max(0,_),B=Math.min(M-1,A);for(let O=F;O<=B;O++){let $=d+O;for(let G=L;G<=k;G++){let Q=c+G,j=`row${$}_col${Q}`;u.has(j)&&(this.results.pointsInSegments.has(j)||this.results.pointsInSegments.set(j,[W]))}}}for(let W of a){let v=`row${W.userData.row}_col${W.userData.col}`,T=this.results.pointsInSegments.has(v)?this.results.pointsInSegments.get(v).length:0,R=T>=1,D={row:W.userData.row,col:W.userData.col,width:W.userData.width,height:W.userData.height,segmentKey:v,vertices:W.vertices,center:W.userData.center,mesh:W,pointCount:T,hasPointsButNotEnough:T>0&&T<1};R?(this.results.segmentsWithPoints++,this.results.existSegments.push(D)):(this.results.segmentsWithoutPoints++,this.results.missingSegments.push(D))}return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results}isPointInWallSegmentOptimized(e,n,o,r,s,i,a,u,d,f,c){if(Math.abs(o.distanceToPoint(e))>c)return!1;let h=new _t().subVectors(e,n),g=Math.abs(h.dot(s)),x=Math.abs(h.dot(i));if(g>d&&x>f)return!1;let y=new _t().subVectors(e,r),m=y.dot(s),p=y.dot(i),b=m>=-c&&m<=a+c,w=p>=-c&&p<=u+c;return b&&w}getWorldVerticesNew(e){return e}highlightEmptySegments(e,n=null,o=null,r=1){if(!this.results)return console.warn("\u8BF7\u5148\u8FD0\u884C\u5206\u6790\u51FD\u6570"),null;let s=new Map,i=new Map;for(let d of this.results.missingSegments)s.set(d.segmentKey,d),i.has(d.row)||i.set(d.row,new Map),i.get(d.row).set(d.col,{segmentKey:d.segmentKey,isEmpty:!0,visited:!1});for(let d of e)if(d.userData&&d.userData.type==="wallSegment"){let f=`row${d.userData.row}_col${d.userData.col}`,c=s.has(f);i.has(d.userData.row)||i.set(d.userData.row,new Map),i.get(d.userData.row).has(d.userData.col)||i.get(d.userData.row).set(d.userData.col,{segmentKey:f,isEmpty:c,visited:!1})}let a=[],u=[[0,1],[1,0],[0,-1],[-1,0]];for(let[d,f]of s){let c=i.get(f.row)?.get(f.col);if(c&&!c.visited){let l=[],h=[[f.row,f.col]],g=0;for(c.visited=!0;g<h.length;){let x=h[g++],y=x[0],m=x[1],p=`row${y}_col${m}`;s.has(p)&&l.push(s.get(p));for(let b of u){let w=y+b[0],z=m+b[1];if(i.has(w)&&i.get(w).has(z)){let M=i.get(w).get(z);!M.visited&&M.isEmpty&&(M.visited=!0,h.push([w,z]))}}}l.length>=r&&a.push(l)}}return this.clusterResults={clusters:a.sort((d,f)=>f.length-d.length),totalClusters:a.length},this.clusterResults}getClusterInfo(){if(!this.clusterResults)return null;let e=this.clusterResults.clusters.map(n=>n.length);return{totalClusters:this.clusterResults.totalClusters,clusterSizes:this.clusterResults.clusters.map((n,o)=>({clusterIndex:o,size:n.length,segments:n.map(r=>r.segmentKey)})),largestCluster:e.length?Math.max(...e):0,smallestCluster:e.length?Math.min(...e):0}}printAnalysisResults(){if(!this.results){console.warn("\u6CA1\u6709\u53EF\u7528\u7684\u5206\u6790\u7ED3\u679C");return}}isValidVector3(e){return e&&!isNaN(e.x)&&!isNaN(e.y)&&!isNaN(e.z)&&isFinite(e.x)&&isFinite(e.y)&&isFinite(e.z)}},cr=(t,e)=>{let n={},o=[],r=[];if(!t||t.length===0)return n.originalVertices=o,n.allCenterPoints=r,n;if(e=="xyz")for(let s of t){r.push({x:s.center.x,y:s.center.y,z:s.center.z});let i=[];for(let a=0;a<s.vertices.length;a++)i.push({x:s.vertices[a].x,y:s.vertices[a].y,z:s.vertices[a].z});o.push(i)}else if(e=="three")for(let s of t){r.push(new Ne.Vector3(s.center.x,s.center.y,s.center.z));let i=[];for(let a=0;a<s.vertices.length;a++)i.push(new Ne.Vector3(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z));o.push(i)}else if(e=="array")for(let s of t){r.push(s.center.x),r.push(s.center.y),r.push(s.center.z);let i=[];for(let a=0;a<s.vertices.length;a++)i.push(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z);o.push(i)}return n.originalVertices=o,n.allCenterPoints=r,n},Pi=(t,e)=>{let n=new _t().copy(t.start).add(t.end).multiplyScalar(.5),o=new _t().subVectors(new _t(n.x,n.y,n.z+1),n).normalize(),r=new _t(t.direction.x,t.direction.y,0);r.applyAxisAngle(o,Math.PI/2);let s=new _t().copy(n).add(r.clone().multiplyScalar(t.length)),i=new _t().copy(n).sub(r.clone().multiplyScalar(t.length));if(n.z=t.start.z,s.z=t.start.z,i.z=t.start.z,e=="three")return{start:new Ne.Vector3(s.x,s.y,s.z),end:new Ne.Vector3(i.x,i.y,i.z),center:new Ne.Vector3(n.x,n.y,n.z),direction:new Ne.Vector3(r.x,r.y,r.z)};if(e=="xyz")return{start:{x:s.x,y:s.y,z:s.z},end:{x:i.x,y:i.y,z:i.z},center:{x:n.x,y:n.y,z:n.z},direction:{x:r.x,y:r.y,z:r.z}};if(e=="array")return{start:[s.x,s.y,s.z],end:[i.x,i.y,i.z],center:[n.x,n.y,n.z],direction:[r.x,r.y,r.z]}},ur=async(t,e=!0,n="three")=>{for(let o=0;o<t.length;o++){let r=t[o];await bo(r,e,n)}},bo=(t,e=!0,n="three")=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&e)return;let o=new An().setFromCoplanarPoints(new _t(t.start.x,t.start.y,t.start.z),new _t(t.end.x,t.end.y,t.end.z),new _t(t.start.x,t.start.y,t.rooftopPz)),r=new _t(t.start.x,t.start.y,t.start.z).distanceTo(new _t(t.end.x,t.end.y,t.end.z)),s=Math.abs(t.rooftopPz-t.start.z),i=new yo,{horizontalSubdivisions:a,verticalSubdivisions:u}=i.initLimits4(r,s),d=i.createSubdividedWallPlane(t,u,a,null);i.analyzePointDistribution(d,t.originalPoints,.01,{projectPlane:o});let f=i.highlightEmptySegments(d,null,null),c=[];if(f&&f.clusters)for(let g of f.clusters){let x=cr(g,n);x&&c.push(x)}let h=Pi(t,n).direction;if(t.checkResults=c,t.verticalDirection=h,t.saveExistSegments){let g=[],x=cr(i.results.existSegments,n);x&&g.push(x),t.existSegments=g}};var Dt=class t{constructor(e=0,n=0,o=0){this.x=e,this.y=n,this.z=o}set(e,n,o){return this.x=e,this.y=n,this.z=o,this}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this}clone(){return new t(this.x,this.y,this.z)}add(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this}sub(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this}subVectors(e,n){return this.x=e.x-n.x,this.y=e.y-n.y,this.z=e.z-n.z,this}multiplyScalar(e){return this.x*=e,this.y*=e,this.z*=e,this}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}crossVectors(e,n){let o=e.x,r=e.y,s=e.z,i=n.x,a=n.y,u=n.z;return this.x=r*u-s*a,this.y=s*i-o*u,this.z=o*a-r*i,this}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z}length(){return Math.sqrt(this.lengthSq())}normalize(){let e=this.length();return e>0&&(this.x/=e,this.y/=e,this.z/=e),this}distanceTo(e){return Math.sqrt(this.distanceToSquared(e))}distanceToSquared(e){let n=this.x-e.x,o=this.y-e.y,r=this.z-e.z;return n*n+o*o+r*r}toArray(){return[this.x,this.y,this.z]}fromArray(e,n=0){return this.x=e[n],this.y=e[n+1],this.z=e[n+2],this}applyAxisAngle(e,n){let o=this.x,r=this.y,s=this.z,i=e.x,a=e.y,u=e.z,d=Math.cos(n),f=Math.sin(n),c=i*o+a*r+u*s;return this.x=i*c*(1-d)+o*d+(-u*r+a*s)*f,this.y=a*c*(1-d)+r*d+(u*o-i*s)*f,this.z=u*c*(1-d)+s*d+(-a*o+i*r)*f,this}},bn=class{constructor(e=new Dt(1,0,0),n=0){this.normal=e,this.constant=n}set(e,n){return this.normal.copy(e),this.constant=n,this}setFromNormalAndCoplanarPoint(e,n){return this.normal.copy(e),this.constant=-n.dot(this.normal),this}setFromCoplanarPoints(e,n,o){let r=new Dt().subVectors(n,e),s=new Dt().subVectors(o,e),i=new Dt().crossVectors(r,s).normalize();return this.normal.copy(i),this.constant=-e.dot(this.normal),this}distanceToPoint(e){return this.normal.dot(e)+this.constant}projectPoint(e,n=new Dt){let o=this.distanceToPoint(e);return n.copy(e),n.x-=this.normal.x*o,n.y-=this.normal.y*o,n.z-=this.normal.z*o,n}},Bn=()=>typeof performance<"u"&&performance.now?performance.now():Date.now(),Ln=class{constructor(){this.results=null,this.clusterResults=null}initLimits1(e,n){let o,r;return e<.3?o=5:e<.5?o=8:e<.8?o=10:e<1?o=15:e<2?o=30:e<3?o=35:e<4?o=40:e<5?o=45:e<6?o=50:e<7?o=60:e<8.5?o=70:e<10?o=80:e<15?o=100:e<20?o=120:e<25?o=140:e<30?o=160:e<35?o=180:e<40?o=200:e<45?o=230:e<50?o=250:o=350,n<1?r=30:n<2?r=40:n<2.5?r=45:n<3?r=60:n<3.6?r=70:r=80,{horizontalSubdivisions:o,verticalSubdivisions:r}}initLimits2(e,n){let s=Math.round(Math.max(5,Math.min(350,e/.08))),i=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:s,verticalSubdivisions:i}}initLimits3(e,n){let o;e<2?o=.06:e<10?o=.1:o=.15;let r=Math.round(Math.max(5,Math.min(350,e/o))),s=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:r,verticalSubdivisions:s}}initLimits4(e,n){let o=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(e)))),r=Math.round(Math.max(30,Math.min(80,20*Math.sqrt(n))));return{horizontalSubdivisions:o,verticalSubdivisions:r}}createSubdividedWallPlane(e,n,o,r=null){let s=[];if(!e||!e.start||!e.end||isNaN(e.rooftopPz))return s;let i=new Dt(e.start.x,e.start.y,e.start.z),a=new Dt(e.end.x,e.end.y,e.end.z),u=new Dt(e.end.x,e.end.y,e.rooftopPz),d=new Dt(e.start.x,e.start.y,e.rooftopPz),f=[i,a,u,d];for(let P=0;P<f.length;P++)if(!this.isValidVector3(f[P]))return console.error(`\u5899\u4F53\u57FA\u7840\u70B9${P}\u65E0\u6548:`,f[P]),s;let c=i.distanceTo(a),l=Math.abs(e.rooftopPz-e.start.z);if(c===0||l===0)return s;let h=c/o,g=l/n,x=new Dt().subVectors(a,i).normalize(),y=i.x,m=i.y,p=i.z,b=x.x,w=x.y,z=x.z,M=(P,E)=>new Dt(y+b*P,m+w*P,p+z*P+E);for(let P=0;P<n;P++){let E=P*g,S=(P+1)*g;for(let I=0;I<o;I++){let W=I*h,v=(I+1)*h;s.push({points:[M(W,E),M(v,E),M(v,S),M(W,S)],userData:{type:"wallSegment",row:P,col:I,width:h,height:g}})}}return s}analyzePointDistribution(e,n,o=.01,r={}){let s=r.projectPlane||null;this.results={totalSegments:0,segmentsWithPoints:0,segmentsWithoutPoints:0,missingSegments:[],existSegments:[],pointsInSegments:new Map,segmentsByPoint:new Map,segmentDetails:new Map,tolerance:o,analysisTime:null};let i=Bn(),a=[],u=new Map,d=1/0,f=-1/0,c=1/0,l=-1/0;for(let W=0;W<e.length;W++){let v=e[W];if(v.userData&&v.userData.type==="wallSegment"){let T=this.getWorldVerticesNew(v.points);if(T.length<4)continue;let R=new Dt().add(T[0]).add(T[1]).add(T[2]).add(T[3]).multiplyScalar(.25);v.userData.center=R,v.vertices=T;let D=v.userData.row,H=v.userData.col,V=`row${D}_col${H}`;a.push(v),u.set(V,v),D<d&&(d=D),D>f&&(f=D),H<c&&(c=H),H>l&&(l=H)}}if(this.results.totalSegments=a.length,a.length===0)return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results;let h=`row${d}_col${c}`,g=u.get(h)||a[0];if(!g||!g.vertices||g.vertices.length<4)return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results;let x=g.vertices[0],y=g.vertices[1],m=g.vertices[3],p=new Dt().subVectors(y,x).normalize(),b=new Dt().subVectors(m,x).normalize(),w=g.userData.width,z=g.userData.height,M=f-d+1,P=l-c+1,E=new bn;E.setFromCoplanarPoints(x,y,m);let S=new Dt,I=new Dt;for(let W=0;W<n.length;W++){let v=n[W],T=s?s.projectPoint(v,I):v;if(Math.abs(E.distanceToPoint(T))>o)continue;S.subVectors(T,x);let D=S.dot(p),H=S.dot(b),V=Math.ceil((D-o)/w-1),C=Math.floor((D+o)/w),_=Math.ceil((H-o)/z-1),A=Math.floor((H+o)/z);if(C<0||V>P-1||A<0||_>M-1)continue;let L=Math.max(0,V),k=Math.min(P-1,C),F=Math.max(0,_),B=Math.min(M-1,A);for(let O=F;O<=B;O++){let $=d+O;for(let G=L;G<=k;G++){let Q=c+G,j=`row${$}_col${Q}`;u.has(j)&&(this.results.pointsInSegments.has(j)||this.results.pointsInSegments.set(j,[W]))}}}for(let W of a){let v=`row${W.userData.row}_col${W.userData.col}`,T=this.results.pointsInSegments.has(v)?this.results.pointsInSegments.get(v).length:0,R=T>=1,D={row:W.userData.row,col:W.userData.col,width:W.userData.width,height:W.userData.height,segmentKey:v,vertices:W.vertices,center:W.userData.center,mesh:W,pointCount:T,hasPointsButNotEnough:T>0&&T<1};R?(this.results.segmentsWithPoints++,this.results.existSegments.push(D)):(this.results.segmentsWithoutPoints++,this.results.missingSegments.push(D))}return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results}isPointInWallSegmentOptimized(e,n,o,r,s,i,a,u,d,f,c){if(Math.abs(o.distanceToPoint(e))>c)return!1;let h=new Dt().subVectors(e,n),g=Math.abs(h.dot(s)),x=Math.abs(h.dot(i));if(g>d&&x>f)return!1;let y=new Dt().subVectors(e,r),m=y.dot(s),p=y.dot(i),b=m>=-c&&m<=a+c,w=p>=-c&&p<=u+c;return b&&w}getWorldVerticesNew(e){return e}highlightEmptySegments(e,n=null,o=null,r=1){if(!this.results)return console.warn("\u8BF7\u5148\u8FD0\u884C\u5206\u6790\u51FD\u6570"),null;let s=new Map,i=new Map;for(let d of this.results.missingSegments)s.set(d.segmentKey,d),i.has(d.row)||i.set(d.row,new Map),i.get(d.row).set(d.col,{segmentKey:d.segmentKey,isEmpty:!0,visited:!1});for(let d of e)if(d.userData&&d.userData.type==="wallSegment"){let f=`row${d.userData.row}_col${d.userData.col}`,c=s.has(f);i.has(d.userData.row)||i.set(d.userData.row,new Map),i.get(d.userData.row).has(d.userData.col)||i.get(d.userData.row).set(d.userData.col,{segmentKey:f,isEmpty:c,visited:!1})}let a=[],u=[[0,1],[1,0],[0,-1],[-1,0]];for(let[d,f]of s){let c=i.get(f.row)?.get(f.col);if(c&&!c.visited){let l=[],h=[[f.row,f.col]],g=0;for(c.visited=!0;g<h.length;){let x=h[g++],y=x[0],m=x[1],p=`row${y}_col${m}`;s.has(p)&&l.push(s.get(p));for(let b of u){let w=y+b[0],z=m+b[1];if(i.has(w)&&i.get(w).has(z)){let M=i.get(w).get(z);!M.visited&&M.isEmpty&&(M.visited=!0,h.push([w,z]))}}}l.length>=r&&a.push(l)}}return this.clusterResults={clusters:a.sort((d,f)=>f.length-d.length),totalClusters:a.length},this.clusterResults}getClusterInfo(){if(!this.clusterResults)return null;let e=this.clusterResults.clusters.map(n=>n.length);return{totalClusters:this.clusterResults.totalClusters,clusterSizes:this.clusterResults.clusters.map((n,o)=>({clusterIndex:o,size:n.length,segments:n.map(r=>r.segmentKey)})),largestCluster:e.length?Math.max(...e):0,smallestCluster:e.length?Math.min(...e):0}}printAnalysisResults(){if(!this.results){console.warn("\u6CA1\u6709\u53EF\u7528\u7684\u5206\u6790\u7ED3\u679C");return}}isValidVector3(e){return e&&!isNaN(e.x)&&!isNaN(e.y)&&!isNaN(e.z)&&isFinite(e.x)&&isFinite(e.y)&&isFinite(e.z)}},fr=(t,e)=>{let n={},o=[],r=[];if(!t||t.length===0)return n.originalVertices=o,n.allCenterPoints=r,n;if(e=="xyz")for(let s of t){r.push({x:s.center.x,y:s.center.y,z:s.center.z});let i=[];for(let a=0;a<s.vertices.length;a++)i.push({x:s.vertices[a].x,y:s.vertices[a].y,z:s.vertices[a].z});o.push(i)}else if(e=="three")for(let s of t){r.push(new qe.Vector3(s.center.x,s.center.y,s.center.z));let i=[];for(let a=0;a<s.vertices.length;a++)i.push(new qe.Vector3(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z));o.push(i)}else if(e=="array")for(let s of t){r.push(s.center.x),r.push(s.center.y),r.push(s.center.z);let i=[];for(let a=0;a<s.vertices.length;a++)i.push(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z);o.push(i)}return n.originalVertices=o,n.allCenterPoints=r,n},Ti=(t,e)=>{let n=new Dt().copy(t.start).add(t.end).multiplyScalar(.5),o=new Dt().subVectors(new Dt(n.x,n.y,n.z+1),n).normalize(),r=new Dt(t.direction.x,t.direction.y,0);r.applyAxisAngle(o,Math.PI/2);let s=new Dt().copy(n).add(r.clone().multiplyScalar(t.length)),i=new Dt().copy(n).sub(r.clone().multiplyScalar(t.length));if(n.z=t.start.z,s.z=t.start.z,i.z=t.start.z,e=="three")return{start:new qe.Vector3(s.x,s.y,s.z),end:new qe.Vector3(i.x,i.y,i.z),center:new qe.Vector3(n.x,n.y,n.z),direction:new qe.Vector3(r.x,r.y,r.z)};if(e=="xyz")return{start:{x:s.x,y:s.y,z:s.z},end:{x:i.x,y:i.y,z:i.z},center:{x:n.x,y:n.y,z:n.z},direction:{x:r.x,y:r.y,z:r.z}};if(e=="array")return{start:[s.x,s.y,s.z],end:[i.x,i.y,i.z],center:[n.x,n.y,n.z],direction:[r.x,r.y,r.z]}},Si=(t,e=!0,n="three",o="xyz")=>{o=="xyz"?dr(t,e,n):o=="array"&&bo(t,e,n)},Ri=(t,e=!0,n="three",o="xyz")=>{o=="xyz"?Di(t,e,n):o=="array"&&ur(t,e,n)},Di=(t,e=!0,n="three")=>{for(let o=0;o<t.length;o++){let r=t[o];dr(r,e,n)}},dr=(t,e=!0,n="three")=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&e)return;let o=new bn().setFromCoplanarPoints(new Dt(t.start.x,t.start.y,t.start.z),new Dt(t.end.x,t.end.y,t.end.z),new Dt(t.start.x,t.start.y,t.rooftopPz)),r=new Dt(t.start.x,t.start.y,t.start.z).distanceTo(new Dt(t.end.x,t.end.y,t.end.z)),s=Math.abs(t.rooftopPz-t.start.z),i=new Ln,{horizontalSubdivisions:a,verticalSubdivisions:u}=i.initLimits4(r,s),d=i.createSubdividedWallPlane(t,u,a,null);i.analyzePointDistribution(d,t.originalPoints,.01,{projectPlane:o});let f=i.highlightEmptySegments(d,null,null),c=[];if(f&&f.clusters)for(let g of f.clusters){let x=fr(g,n);x&&c.push(x)}let h=Ti(t,n).direction;if(t.checkResults=c,t.verticalDirection=h,t.saveExistSegments){let g=[],x=fr(i.results.existSegments,n);x&&g.push(x),t.existSegments=g}},vi=t=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&limit)return;let e=new bn().setFromCoplanarPoints(new Dt(t.start.x,t.start.y,t.start.z),new Dt(t.end.x,t.end.y,t.end.z),new Dt(t.start.x,t.start.y,t.rooftopPz)),n=new Dt(t.start.x,t.start.y,t.start.z).distanceTo(new Dt(t.end.x,t.end.y,t.end.z)),o=Math.abs(t.rooftopPz-t.start.z),r=new Ln,{horizontalSubdivisions:s,verticalSubdivisions:i}=r.initLimits4(n,o),a=r.createSubdividedWallPlane(t,i,s,null);return r.analyzePointDistribution(a,t.originalPoints,.01,{projectPlane:e}),r.highlightEmptySegments(a,null,null)};import*as N from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";import*as Y from"three";import{CSS2DObject as vc}from"three/examples/jsm/renderers/CSS2DRenderer.js";import"three/examples/jsm/utils/BufferGeometryUtils.js";import Me from"path";import{fileURLToPath as Ci}from"url";import kn from"path";import{fileURLToPath as Vi}from"url";import{Vector3 as lc}from"three";var Ii=Vi(import.meta.url),wo=kn.dirname(Ii),ac=kn.resolve(wo,"./data/lines.meta.json"),cc=kn.resolve(wo,"./data/lines.bin"),uc=kn.resolve(wo,"./data/checks.bin");var Hi=Ci(import.meta.url),ve=Me.dirname(Hi),gc=Me.resolve(ve,"./data/lines.tmp.bin"),xc=Me.resolve(ve,"./data/checks.tmp.bin"),yc=Me.resolve(ve,"./data/lines.new_points.tmp.bin"),bc=Me.resolve(ve,"./data/lines.new_points.bin"),wc=Me.resolve(ve,"./data/lines.meta.json"),zc=Me.resolve(ve,"./data/lines.bin"),Mc=Me.resolve(ve,"./data/checks.bin"),Ec=Me.resolve(ve,"./data/lines.exist_segments.bin"),Pc=Me.resolve(ve,"./data/lines.exist_segments.tmp.bin");import{Vector3 as Wi}from"three";var On=class{points=[];_tempArr=[0,0,0];constructor(e){}initPoints(e){this.points=e}getPointByIndex(e,n){let o=e*3;return n.x=this.points[o],n.y=this.points[o+1],n.z=this.points[o+2],n}getPointArrByIndex(e){let n=e*3;return[this.points[n],this.points[n+1],this.points[n+2]]}getPoint2ArrByIndex(e){let n=e*3;return[this.points[n],this.points[n+1]]}getIndexsByIndex(e,n){n.length=0,n.push(e*3),n.push(e*3+1),n.push(e*3+2)}pointsToVector3Arr(e){let n=[];for(let o=0;o<e.length/3;o++)n.push(new Wi(e[o*3],e[o*3+1],e[o*3+2]));return n}vector3ArrToPoints(e){let n=[];for(let o=0;o<e.length;o++)n.push(e[o].x,e[o].y,e[o].z);return n}vector3ArrToFloat32Array(e){return new Float32Array(e.flatMap(o=>[o.x,o.y,o.z]))}clsoe(){this.points=[]}};var mr=(t,e,n)=>{let o=new Y.LineBasicMaterial({color:"#0011ff"}),r=[];r.push(t),r.push(e);let s=new Y.BufferGeometry;s.setFromPoints(r),s.rotateX(-Math.PI/2),n.add(new Y.Line(s,o))},Ee=(t,e,n,o,r=1e-8)=>{let s=e.x-t.x,i=e.y-t.y,a=o.x-n.x,u=o.y-n.y,d=s*u-i*a;if(Math.abs(d)<r)return null;let f=((n.x-t.x)*u-(n.y-t.y)*a)/d,c=((n.x-t.x)*i-(n.y-t.y)*s)/d;return f<-r||f>1+r||c<-r||c>1+r?null:{point:new Y.Vector3(t.x+f*s,t.y+f*i,t.z)}},pr=(t,e,n={})=>{let{fontSize:o=36,fontFamily:r="sans-serif",backgroundColor:s="rgba(0,0,0,0)",textColor:i="#ffffff",padding:a=10,maxWidth:u=256}=n,f=document.createElement("canvas").getContext("2d");f.font=`bold ${o}px ${r}`;let l=f.measureText(t).width,h=Math.min(l+a*2,u),g=o+a*2,x=document.createElement("canvas");x.width=h,x.height=g;let y=x.getContext("2d");y.fillStyle=s,y.fillRect(0,0,h,g),y.fillStyle=i,y.font=`bold ${o}px ${r}`,y.textAlign="center",y.textBaseline="middle",l>u-a*2?_i(y,t,h/2,g/2,u,o):y.fillText(t,h/2,g/2);let m=new Y.CanvasTexture(x),p=new Y.SpriteMaterial({map:m,depthTest:!1,transparent:!0}),b=new Y.Sprite(p);b.position.set(e.x,e.y,e.z);let w=.005;return b.scale.set(h*w,g*w,1),b};function _i(t,e,n,o,r,s){let i=e.split(""),a="",u=s*1.2,d=o;for(let f=0;f<i.length;f++){let c=a+i[f];t.measureText(c).width>r&&f>0?(t.fillText(a,n,d),a=i[f],d+=u):a=c}t.fillText(a,n,d)}var $t=(t,e,n=.01)=>{let o=Math.sqrt(t.x*t.x+t.y*t.y+t.z*t.z),r=Math.sqrt(e.x*e.x+e.y*e.y+e.z*e.z);if(o<1e-10||r<1e-10)return{parallel:!1,sameDirection:null,angle:NaN};let s={x:t.x/o,y:t.y/o,z:t.z/o},i={x:e.x/r,y:e.y/r,z:e.z/r},a=s.x*i.x+s.y*i.y+s.z*i.z;a=Math.max(-1,Math.min(1,a));let u=Math.acos(Math.abs(a)),d=u<=n;return{parallel:d,sameDirection:d?a>0:null,angle:u}};var gr=(t,e,n,o)=>{if(e<1e-4||n<1e-4)return{};let r=t,s=[];if(o&&o.length&&(s=o.map(M=>M.clone())),!r||r.length<4)return{};let i=new Y.Vector3().subVectors(r[1],r[0]).normalize(),a=new Y.Vector3().subVectors(r[3],r[0]).normalize(),u=i.clone().cross(a).normalize(),d=1/0,f=-1/0,c=1/0,l=-1/0;for(let M of r){let P=M.dot(i),E=M.dot(a);P<d&&(d=P),P>f&&(f=P),E<c&&(c=E),E>l&&(l=E)}let h=r[0].dot(u),g=i.clone().multiplyScalar(d).add(a.clone().multiplyScalar(c)).add(u.clone().multiplyScalar(h)),x=i.clone().multiplyScalar(f).add(a.clone().multiplyScalar(c)).add(u.clone().multiplyScalar(h)),y=i.clone().multiplyScalar(f).add(a.clone().multiplyScalar(l)).add(u.clone().multiplyScalar(h)),m=i.clone().multiplyScalar(d).add(a.clone().multiplyScalar(l)).add(u.clone().multiplyScalar(h)),p=[g,x,y,m],b=$t(new Y.Vector3().subVectors(g,x),new Y.Vector3(0,0,1));b&&b.parallel&&(p=[g,m,y,x]);let w=0;if(o.length>0){let M=new Y.Vector3().subVectors(p[1],p[0]).normalize(),P=M.clone().negate(),E=new Y.Vector3().subVectors(p[3],p[0]).normalize(),S=E.clone().negate(),I=p[0].distanceTo(p[1]),W=p[0].distanceTo(p[3]),v=I/e,T=W/n,R=.001;for(let D=0;D<v;D++){let H=p[0],V=0;for(let C=0;C<T;C++){let _=H.clone().addScaledVector(M,e),A=_.clone().addScaledVector(E,n),L=H.clone().addScaledVector(E,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=L}if(V>=T/2)break;w+=V,p[0]=p[0].addScaledVector(M,e),p[3]=p[3].addScaledVector(M,e)}I=p[0].distanceTo(p[1]),v=I/e;for(let D=0;D<v;D++){let H=p[1],V=0;for(let C=0;C<T;C++){let _=H.clone().addScaledVector(P,e),A=_.clone().addScaledVector(E,n),L=H.clone().addScaledVector(E,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=L}if(V>=T/2)break;w+=V,p[1]=p[1].addScaledVector(P,e),p[2]=p[2].addScaledVector(P,e)}I=p[0].distanceTo(p[1]),v=I/e;for(let D=0;D<T;D++){let H=p[3],V=0;for(let C=0;C<v;C++){let _=H.clone().addScaledVector(M,e),A=_.clone().addScaledVector(S,n),L=H.clone().addScaledVector(S,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=_}if(V>=v/2)break;w+=V,p[2]=p[2].addScaledVector(S,n),p[3]=p[3].addScaledVector(S,n)}}let z=p[0].distanceTo(p[1])*p[0].distanceTo(p[3]);return{facePoints:p,boxArea:z,totalInPlaneNum:w}},Xt=(t,e,n,o,r=Y.MathUtils.degToRad(8))=>{let s=new Y.Vector3,i=new Y.Vector3;s.subVectors(new Y.Vector3(e.x,e.y,0),new Y.Vector3(t.x,t.y,0)).normalize(),i.subVectors(new Y.Vector3(o.x,o.y,0),new Y.Vector3(n.x,n.y,0)).normalize();let a=s.dot(i),u=Math.acos(Math.min(Math.abs(a),1)),d=1-Math.min(u/r,1),f=a>0?"same":"opposite";return{parallelism:d,angle:u,angleDeg:Y.MathUtils.radToDeg(u),isParallel:u<r,direction:f,vectors:{v1:s,v2:i}}},ie=(t,e,n,o)=>{let r=new Y.Vector3().subVectors(e,t),s=new Y.Vector3().subVectors(o,n),i=r.length(),a=s.length();if(i===0||a===0)return{rate:NaN,angle:NaN,isPerpendicular:!1,isParallel:!1};r.normalize(),s.normalize();let u=Math.abs(r.dot(s)),d=Math.min(1,Math.max(0,u)),f=Math.acos(d),c=Y.MathUtils.radToDeg(f),l=1-d,h=1e-6;return{rate:l,percent:`${(l*100).toFixed(2)}%`,angle:c,isPerpendicular:Math.abs(c-90)<.01,isParallel:c<.01}},At=(t,e,n=!1)=>new Y.Vector3((t.x+e.x)/2,(t.y+e.y)/2,n?0:(t.z+e.z)/2),be=(t,e,n,o,r={})=>{let{parallelAngleMax:s=Y.MathUtils.degToRad(10),collinearAngleMax:i=Y.MathUtils.degToRad(7),distanceThreshold:a=2,overlapThreshold:u=.01}=r,d=new Y.Vector3().subVectors(e,t),f=new Y.Vector3().subVectors(o,n),c=d.clone().normalize(),l=f.clone().normalize(),h=Y.MathUtils.clamp(c.dot(l),-1,1),g=Math.acos(Math.abs(h)),x=Y.MathUtils.radToDeg(g);if(g>s)return{type:"not_parallel",angleDeg:x,avgPerpendicularDistance:null,maxPerpendicularDistance:null,gap:null,overlap:null,closestDistance:null};h<0&&l.negate();let y=new Y.Vector3().addVectors(c,l).normalize(),m=new Y.Vector3().addVectors(t,e).multiplyScalar(.5),p=hr(n,m,c),b=hr(o,m,c),w=(p+b)/2,z=Math.max(p,b),M=t.dot(y),P=e.dot(y),E=n.dot(y),S=o.dot(y),I=Math.min(M,P),W=Math.max(M,P),v=Math.min(E,S),T=Math.max(E,S),R=W-I,D=T-v,H=Math.min(R,D),V=Math.min(W,T)-Math.max(I,v),C=V<0?-V:0,_=V>0?V:0,A=H>0?_/H:0,L=Ai(t,e,n,o),k=g<=i&&w<a,F;return k?F=A>.5?"collinear_overlap":"collinear_gap":g<=s&&(F="parallel_offset"),{type:F,angleDeg:x,avgPerpendicularDistance:w,maxPerpendicularDistance:z,gap:C,overlap:_,closestDistance:L}};function hr(t,e,n){let o=new Y.Vector3().subVectors(t,e),r=o.dot(n),s=n.clone().multiplyScalar(r);return new Y.Vector3().subVectors(o,s).length()}function Ai(t,e,n,o){let r=new Y.Vector3().subVectors(e,t),s=new Y.Vector3().subVectors(o,n),i=new Y.Vector3().subVectors(n,t),a=r.dot(r),u=r.dot(s),d=s.dot(s),f=r.dot(i),c=s.dot(i),l=a*d-u*u,h,g;l<1e-10?(h=0,g=d!==0?c/d:0):(h=(u*c-d*f)/l,g=(a*c-u*f)/l),h=Y.MathUtils.clamp(h,0,1),g=d!==0?Y.MathUtils.clamp((u*h+c)/d,0,1):0,h=a!==0?Y.MathUtils.clamp((u*g-f)/a,0,1):0;let x=t.clone().add(r.clone().multiplyScalar(h)),y=n.clone().add(s.clone().multiplyScalar(g));return x.distanceTo(y)}var Ve=t=>{let e=999999,n=-999999;for(let o=0;o<t.length;o++)t[o].z>n&&(n=t[o].z),t[o].z<e&&(e=t[o].z);return{minZ:e,maxZ:n}},wn=(t,e,n)=>{let o=new Y.Plane,r=new Y.Vector3(t.x,t.y,t.z),s=new Y.Vector3(e.x,e.y,e.z),i=new Y.Vector3(e.x,e.y,e.z+5);o.setFromCoplanarPoints(r,s,i);let a=[],u=[];n.forEach(y=>{let m=new Y.Vector3;o.projectPoint(y,m),u.push(m.clone()),m.z=r.z,a.push(m)});let d=new Y.Vector3,f=new Y.Vector3;o.projectPoint(r,d),o.projectPoint(s,f);let c=new Y.Line3(d,f),l=[],h=[],g=[],x=new Y.Vector3;return a.forEach((y,m)=>{c.closestPointToPoint(y,!0,x).distanceTo(y)<.001?(l.push(n[m]),h.push(u[m])):g.push(y)}),{newOriginalPoints:l,newProjectPoints:g,newOnLinePoints:h}},xr=(t,e,n)=>{function o(r,s,i,a,u,d,f,c,l){let h=new Y.Line3(r,s),g=new Map,x=new Y.Vector3,y=r.clone(),m=s.clone(),p=0;for(;;){let b=new Map,w=0;for(let z=0;z<l.checkResults.length;z++)if(!(l.checkResults[z].allCenterPoints.length/c*100<1))for(let P=0;P<l.checkResults[z].allCenterPoints.length;P++){if(g.has(z)&&g.get(z).removePtsIndex.includes(P))continue;let E=l.checkResults[z].allCenterPoints[P];E=new Y.Vector3(E.x,E.y,E.z);let S=h.closestPointToPoint(E,!0,x),I=E.distanceTo(S);Math.abs(I-u)<.01&&(w++,b.has(z)?b.get(z).removePtsIndex.push(P):b.set(z,{index:z,removePtsIndex:[P]}))}if(p==f){r=y,s=m;break}if(w<d/20*19||w==0){b.clear(),f-p<5&&(r=y,s=m,g.clear());break}else{p++;for(let[z,M]of b)g.has(z)?g.get(z).removePtsIndex.push(...M.removePtsIndex):g.set(z,M);b.clear(),r=r.addScaledVector(i,a),s=s.addScaledVector(i,a),h.set(r,s)}}if(g.size>0&&p!=f)for(let[b,w]of g){let z=[],M=[];for(let P=0;P<l.checkResults[b].allCenterPoints.length;P++)w.removePtsIndex.includes(P)||(z.push(l.checkResults[b].allCenterPoints[P]),M.push(l.checkResults[b].originalVertices[P]));l.checkResults[b].allCenterPoints=z,l.checkResults[b].originalVertices=M}}for(let r=0;r<t.length;r++){let s=t[r];if(s.length<.5)continue;let i=-1,a=-1,u=e.readLineById(s.id);u.checkResults=e.getLineCheckReultsById(s.id),u.originalPoints=e.getOriginalPointsById(s.id),s.originalPoints=u.originalPoints,s.checkResults=u.checkResults,s.checkResults.allCenterPoints=[],u=null;for(let D=0;D<s.checkResults.length;D++)for(let H=0;H<s.checkResults[D].originalVertices.length;H++){let V=Fn(s.checkResults[D].originalVertices[H]);s.checkResults.allCenterPoints.push(V)}if(!s.checkResults||s.checkResults.length<=0)continue;if(s.checkResults[0].originalVertices&&s.checkResults[0].originalVertices.length>0){let D=s.checkResults[0].originalVertices[0],[H,V,C,_]=D;H=new Y.Vector3(H.x,H.y,H.z),V=new Y.Vector3(V.x,V.y,V.z),C=new Y.Vector3(C.x,C.y,C.z),_=new Y.Vector3(_.x,_.y,_.z),i=H.distanceTo(V),a=H.distanceTo(_);let A=new Y.Vector3().subVectors(H,V).normalize(),L=$t(A,new Y.Vector3(0,0,1));L&&L.parallel&&([a,i]=[i,a])}if(i<0||a<0)continue;let d=new Y.Vector3(s.start.x,s.start.y,s.start.z),f=new Y.Vector3(s.end.x,s.end.y,s.end.z),c=d.distanceTo(f),l=s.rooftopPz-s.start.z,h=Math.ceil(c/i),g=Math.ceil(l/a),x=d.clone(),y=f.clone(),m=y.clone().add(new Y.Vector3(0,0,l)),p=x.clone().add(new Y.Vector3(0,0,l)),b=new Y.Vector3().subVectors(y,x).normalize(),w=b.clone().negate(),M=new Y.Vector3().subVectors(m,x).normalize().clone().negate(),P=i/2,E=a/2,S=c*l,I=i*a,W=S/I;o(x,p,b,i,P,g,h,W,s),o(y,m,w,i,P,g,h,W,s),s.start=x,s.end=y;let{newOriginalPoints:v}=wn(x,y,s.originalPoints);s.originalPoints=v,n.stageReplaceCheckResults(s.id,s.checkResults);let R=new On().vector3ArrToFloat32Array(s.originalPoints);n.stageReplacePoints(s.id,R),s.checkResults=null,s.originalPoints=null}n.commit()},Zt=(t,e)=>new Y.Vector3(t.x,t.y,0).distanceTo(new Y.Vector3(e.x,e.y,0)),zo=(t,e,n=1e-6)=>{let o=new Y.Vector3;return t.closestPointToPoint(e,!0,o),o.distanceTo(e)<n},yr=(t,e,n,o=1e-6)=>{let r=new Y.Line3(new Y.Vector3(t.x,t.y,t.z),new Y.Vector3(e.x,e.y,e.z)),s=new Y.Vector3;return r.closestPointToPoint(new Y.Vector3(n.x,n.y,n.z),!0,s),s.distanceTo(new Y.Vector3(n.x,n.y,n.z))<o},We=t=>{let e=0,n=[];for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o].distanceTo(t[r]);s>e&&(e=s,n=[t[o],t[r]])}return n},ct=(t,e,n=.03)=>Math.abs(t.x-e.x)<n&&Math.abs(t.y-e.y)<n&&Math.abs(t.z-e.z)<n;var br=()=>{let t=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0"),e=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0"),n=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0");return`#${t}${e}${n}`};var Mo=(t,e=.001)=>{let n=new Map,o=[];for(let r of t){let s=Math.round(r.x/e),i=Math.round(r.y/e),a=Math.round(r.z/e),u=`${s},${i},${a}`;n.has(u)||(n.set(u,!0),o.push(r))}return o},wr=t=>{let e=t.center,n=t.halfSize,r=t.rotation.elements,s=new Y.Vector3(r[0],r[1],r[2]).multiplyScalar(n.x),i=new Y.Vector3(r[3],r[4],r[5]).multiplyScalar(n.y),a=new Y.Vector3(r[6],r[7],r[8]).multiplyScalar(n.z);function u(f,c,l){return e.clone().addScaledVector(s,f).addScaledVector(i,c).addScaledVector(a,l)}return[u(-1,-1,-1),u(1,-1,-1),u(1,1,-1),u(-1,1,-1),u(-1,-1,1),u(1,-1,1),u(1,1,1),u(-1,1,1)]},Eo=(t,e)=>{let n=t[0],o=t[1],r=t[2],s=t[3],i=new Y.Vector3().subVectors(o,n),a=new Y.Vector3().subVectors(s,n),u=i.length(),d=a.length();i.normalize(),a.normalize();let f=new Y.Vector3(0,0,1),l=new Y.Vector3().add(n).add(o).add(r).add(s).multiplyScalar(.25).clone().addScaledVector(f,e/2),h=new Y.BoxGeometry(u,d,e),g=new Y.Matrix4;return g.makeBasis(i,a,f),g.setPosition(l),h.applyMatrix4(g),h.computeVertexNormals(),h},Fn=t=>{if(!t||t.length===0)return null;let e=new Y.Vector3(0,0,0);for(let n of t)Array.isArray(n)?(e.x+=n[0],e.y+=n[1],e.z+=n[2]):(e.x+=n.x,e.y+=n.y,e.z+=n.z);return e.divideScalar(t.length),e},zr=t=>{if(!t||t.length<3)return 0;let e=Array.isArray(t[0]),n=0;for(let o=0;o<t.length;o++){let r=t[o],s=t[(o+1)%t.length];e?n+=r[0]*s[1]-s[0]*r[1]:n+=r.x*s.y-s.x*r.y}return Math.abs(n)/2};import*as Gt from"three";var Bi=(t,e)=>{console.log("lines",t);for(let n=0;n<t.length;n++){let o=t[n],r=[];o.originalPoints.forEach(b=>{let w=new Gt.Vector3(b.x,b.y,0),z=!1;for(let M=0;M<r.length;M++)if(ct(w,r[M],.01)){z=!0;break}z||r.push(w)});let s=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(o.length)))),i=1,a=new Gt.Vector3(o.start.x,o.start.y,o.start.z),u=new Gt.Vector3(o.end.x,o.end.y,o.end.z),d=new Gt.Vector3().subVectors(u,a).normalize(),f=d.clone().cross(new Gt.Vector3(0,0,1)).normalize(),c=f.clone().negate(),l=[],h=[],g=o.length/s;console.log(g);for(let b=0;b<s;b++){let w=[];w.push(a.clone().addScaledVector(f,i)),w.push(w[0].clone().addScaledVector(d,g)),w.push(w[1].clone().addScaledVector(c,i*2)),w.push(a.clone().addScaledVector(c,g));let z=[];for(let M=0;M<r.length;M++)l.includes(M)||ki(r[M],w)&&(l.push(M),z.push(r[M]));z.length>0&&h.push(Oi(z)),a=a.addScaledVector(d,g)}let x=new Float32Array(r.length*3);r.forEach((b,w)=>{x[w*3]=b.x,x[w*3+1]=b.y,x[w*3+2]=b.z});let y=new Gt.BufferGeometry;y.setAttribute("position",new Gt.BufferAttribute(x,3)),y.rotateX(-Math.PI/2),e.add(new Gt.Points(y,new Gt.PointsMaterial({color:br(),size:.02})));let[m,p]=ji(h,{plane:"xy",trimRatio:0});mr(m,p,e)}};function Mr(t,e="xy"){if(e==="xy")return{x:t.x,y:t.y};if(e==="xz")return{x:t.x,y:t.z};if(e==="yz")return{x:t.y,y:t.z};throw new Error(`Unsupported plane: ${e}`)}function Li(t,e,n){let o=e.x-t.x,r=e.y-t.y,s=n.x-t.x,i=n.y-t.y;return o*i-r*s}function ki(t,e,n="xy"){if(!e||e.length!==4)throw new Error("rectPoints must contain 4 points");let o=Mr(t,n),r=e.map(a=>Mr(a,n)),s=!1,i=!1;for(let a=0;a<4;a++){let u=r[a],d=r[(a+1)%4],f=Li(u,d,o);if(f>1e-9&&(s=!0),f<-1e-9&&(i=!0),s&&i)return!1}return!0}function Oi(t){if(!t||t.length===0)return null;let e=new Gt.Vector3(0,0,0);for(let n of t)Array.isArray(n)?(e.x+=n[0],e.y+=n[1],e.z+=n[2]):(e.x+=n.x,e.y+=n.y,e.z+=n.z);return e.divideScalar(t.length),e}function Fi(t){return t instanceof Gt.Vector3?t.clone():Array.isArray(t)?new Gt.Vector3(t[0],t[1],t[2]):new Gt.Vector3(t.x,t.y,t.z)}function Ni(t,e="xy"){let n=Fi(t);if(e==="xy")return{x:n.x,y:n.y,up:n.z};if(e==="xz")return{x:n.x,y:n.z,up:n.y};if(e==="yz")return{x:n.y,y:n.z,up:n.x};throw new Error(`Unsupported plane: ${e}`)}function Er(t,e,n,o="xy"){if(o==="xy")return new Gt.Vector3(t,e,n);if(o==="xz")return new Gt.Vector3(t,n,e);if(o==="yz")return new Gt.Vector3(n,t,e);throw new Error(`Unsupported plane: ${o}`)}function qi(t,e,n){if(Math.abs(e)<1e-12)return t>=n?{x:1,y:0}:{x:0,y:1};let r=t+n,s=t*n-e*e,i=Math.sqrt(Math.max(0,r*r*.25-s)),a=r*.5+i,u=e,d=a-t,f=Math.hypot(u,d);return f<1e-12?{x:1,y:0}:{x:u/f,y:d/f}}function ji(t,e={}){let{plane:n="xy",trimRatio:o=0}=e;if(!t||t.length<2)return null;let r=t.map(V=>Ni(V,n)),s=0,i=0,a=0;for(let V of r)s+=V.x,i+=V.y,a+=V.up;s/=r.length,i/=r.length,a/=r.length;let u=0,d=0,f=0;for(let V of r){let C=V.x-s,_=V.y-i;u+=C*C,d+=C*_,f+=_*_}u/=r.length,d/=r.length,f/=r.length;let c=qi(u,d,f),l={x:-c.y,y:c.x},h=[];for(let V of r){let C=V.x-s,_=V.y-i,A=C*c.x+_*c.y,L=C*l.x+_*l.y;h.push({s:A,t:L,up:V.up})}h.sort((V,C)=>V.s-C.s);let g=h.length,x=Math.floor(g*o),y=Math.ceil(g*(1-o))-1;x=Math.max(0,Math.min(g-1,x)),y=Math.max(0,Math.min(g-1,y));let m=h[x],p=h[y],b=(()=>{let V=h.map(_=>_.t).sort((_,A)=>_-A),C=Math.floor(V.length/2);return V.length%2===0?(V[C-1]+V[C])*.5:V[C]})(),w=(()=>{let V=h.map(_=>_.up).sort((_,A)=>_-A),C=Math.floor(V.length/2);return V.length%2===0?(V[C-1]+V[C])*.5:V[C]})(),z=m.s,M=p.s,P=b,E=b,S=w,I=w,W=s+c.x*z+l.x*P,v=i+c.y*z+l.y*P,T=s+c.x*M+l.x*E,R=i+c.y*M+l.y*E,D=Er(W,v,S,n),H=Er(T,R,I,n);return[D,H]}var $i=async(t,e,n,o,r)=>{if(xr(t,o,r),t.length<=0||e.length<=0)return t;n||(n=[]);for(let m of t){let p=[],b=o.readLineById(m.id);if(b.checkResults=o.getLineCheckReultsById(m.id),m.checkResults=b.checkResults,!(!m.checkResults||m.checkResults.length<=0)){for(let w=0;w<m.checkResults.length;w++){let S=function(R){for(;E[R]!==R;)E[R]=E[E[R]],R=E[R];return R},z=m.checkResults[w];if(!z.originalVertices||z.originalVertices.length==0)continue;let M=[];z.originalVertices.forEach(R=>{let D=[];R.forEach(H=>{D.push(new N.Vector3(H.x,H.y,H.z))}),M.push(D)});let P=M.length,E=Array.from({length:P},(R,D)=>D);for(let R=0;R<P;R++)for(let D=R+1;D<P;D++){let H=M[R],V=M[D];if(H.some(_=>V.some(A=>_.equals(A)))){let _=S(R),A=S(D);_!==A&&(E[_]=A)}}let I=new Map,W=-1,v=-1;for(let R=0;R<P;R++){let D=S(R);I.has(D)||I.set(D,{points:[],area:0,index:-1,gridWidth:-1,gridHeight:-1,centerPts:[],facePoints:[],boxArea:-1,totalInPlaneNum:-1});let H=I.get(D),[V,C,_,A]=M[R],L=new N.Vector3().add(V).add(C).add(_).add(A).multiplyScalar(.25);if(H.points.push(...M[R]),H.area+=V.distanceTo(C)*V.distanceTo(A),H.index=w,H.centerPts.push(L),W==-1||v==-1){let k=new N.Vector3().subVectors(V,C).normalize();v=V.distanceTo(A),W=V.distanceTo(C);let F=$t(k,new N.Vector3(0,0,1));F&&F.parallel&&([v,W]=[W,v])}H.gridWidth=W,H.gridHeight=v}let T=I.values();for(let R of I.values()){let{facePoints:D,boxArea:H,totalInPlaneNum:V}=gr(R.points,R.gridWidth,R.gridHeight,R.centerPts);R.centerPts=null,R.facePoints=D,R.boxArea=H,R.totalInPlaneNum=V}p.push(...T)}m.checkResults=null,m.mergeCheckRegion=p}}let s=[];for(let m=0;m<n.length;m++){let p=n[m];if(p.isFindBeam=!1,p.category=="door"){let b=[];p.coordinatesByArea.coordinates.forEach(E=>{b.push(new N.Vector3(E.x,E.y,E.z))}),b[0].equals(b[b.length-1])&&b.pop();let w=b[0].distanceTo(b[1]),z=b[0].distanceTo(b[3]);z>w&&([w,z]=[z,w],b=[b[0],b[3],b[2],b[1]]);let M=At(b[0],b[3],!0),P=At(b[1],b[2],!0);s.push({doorStartPt:M,doorEndPt:P,boxPoints:b,minZ:p.coordinatesByArea.heightData.minZ,maxZ:p.coordinatesByArea.heightData.maxZ,index:m,isFind:!1,inWall:!1})}}for(let m=0;m<s.length;m++){let p=s[m];for(let b=0;b<t.length;b++){if(t[b].length<.5)continue;let w=[],z=At(t[b].start,t[b].end,!0);w.push(z),w.push(At(z,t[b].start,!0)),w.push(At(z,t[b].end,!0));let M=!1,P=p.boxPoints.length;for(let E=0;E<w.length;E++){let S=w[E].x,I=w[E].y;for(let W=0,v=P-1;W<P;v=W++){let T=p.boxPoints[W].x,R=p.boxPoints[W].y,D=p.boxPoints[v].x,H=p.boxPoints[v].y;R>I!=H>I&&S<(D-T)*(I-R)/(H-R)+T&&(M=!M)}if(M)break}if(M){let E=new N.Vector3(t[b].start.x,t[b].start.y,0),S=new N.Vector3(t[b].end.x,t[b].end.y,0),I=p.doorStartPt.distanceTo(p.doorEndPt),W=E.distanceTo(S),v=Xt(p.doorStartPt,p.doorEndPt,E,S);if(Math.abs(W-I)<.5&&v&&v.angleDeg<15){p.doorStartPt=new N.Vector3(t[b].start.x,t[b].start.y,0),p.doorEndPt=new N.Vector3(t[b].end.x,t[b].end.y,0);break}else if(Math.abs(W-I)>1.3&&v&&v.angleDeg<15){let T=be(p.doorStartPt,p.doorEndPt,E,S,{parallelAngleMax:N.MathUtils.degToRad(15)}),R=p.doorStartPt.distanceTo(E),D=p.doorStartPt.distanceTo(S),H=p.doorEndPt.distanceTo(E),V=p.doorEndPt.distanceTo(S),C=R<.2||D<.2||H<.2||V<.2;if(T&&T.type=="collinear_overlap"&&T.maxPerpendicularDistance<.1&&!C){p.inWall=!0;break}else{let _=new N.Line3(E,S),A=_.closestPointToPoint(p.doorStartPt,!0,new N.Vector3),L=_.closestPointToPoint(p.doorEndPt,!0,new N.Vector3),k=A.distanceTo(p.doorStartPt),F=L.distanceTo(p.doorEndPt);if(k<.1||F<.1){p.inWall=!0;break}}}}else continue}}let i=0,a=[],u=[],d=[],f=[],c=.15;for(let m=0;m<t.length;m++){let p=t[m];if(p.noDetection)continue;let b=o.readLineById(p.id);if(b.originalPoints=o.getOriginalPointsById(p.id),b.checkResults=o.getLineCheckReultsById(p.id),p.originalPoints=b.originalPoints,p.checkResults=b.checkResults,b=null,p.doorAndBeamData=[],p.doorAndBeamIdToCheckResults=[],!p.mergeCheckRegion||p.mergeCheckRegion.length==0)continue;p.completePointAreaPercentage=-1;let w=new N.Box3;w.setFromPoints(p.originalPoints);let z=w.max.z,M=w.min.z,P=new N.Vector3(p.start.x,p.start.y,p.start.z),E=new N.Vector3(p.end.x,p.end.y,p.end.z),S=p.rooftopPz-p.start.z,I=0;for(let v of p.mergeCheckRegion){let T=v.facePoints,R=v.boxArea,D=v.totalInPlaneNum;if(!T||T.length<=0)continue;let H=T[0].distanceTo(T[1]),V=T[0].distanceTo(T[3]),C=v.gridHeight*(V/v.gridHeight/3*2)+v.gridHeight/2,_=new N.Vector3(T[0].x,T[0].y,T[0].z+C),A=new N.Vector3(T[1].x,T[1].y,T[1].z+C),L=new N.Line3(_,A),k=[],F=new N.Vector3;v.points.forEach(j=>{L.closestPointToPoint(j,!0,F).distanceTo(j)<v.gridHeight+.01&&k.push(j)});let B=[],O=[];for(;;){let j=[];for(let Z=0;Z<k.length;Z++)O.includes(Z)||j.length==0&&(j.push(k[Z]),O.push(Z));for(let Z=0;Z<k.length;Z++)if(!O.includes(Z)){for(let U=0;U<j.length;U++)if(k[Z].distanceTo(j[U])<v.gridWidth+.01){j.push(k[Z]),O.push(Z),Z=-1;break}}if(j.length==0)break;B.push(j)}let $=[];for(let j=0;j<B.length;j++){let Z={lineSt:new N.Vector3,lineEd:new N.Vector3,facePoints:[]},U=We(B[j]);Array.isArray(U)&&U.length==2&&(Z.lineSt=U[0],Z.lineEd=U[1],Z.facePoints.push(new N.Vector3(U[0].x,U[0].y,T[0].z)),Z.facePoints.push(new N.Vector3(U[1].x,U[1].y,T[0].z)),Z.facePoints.push(new N.Vector3(U[1].x,U[1].y,T[2].z)),Z.facePoints.push(new N.Vector3(U[0].x,U[0].y,T[2].z)),$.push(Z))}I+=v.area;let G=0;if(D!=0&&(G=v.gridHeight*v.gridWidth*D),(v.area-G)/R*100>70&&$.length==1||$.length>1)for(let j=0;j<$.length;j++){let Z=$[j],U=Z.facePoints[0].distanceTo(Z.facePoints[1]),it=Z.facePoints[0].distanceTo(Z.facePoints[3]),ht=U/p.length*100,St=p.rooftopPz-Z.facePoints[2].z;if(it<1.5||z<Z.facePoints[2].z||St>S/3*2)continue;let ot=!1,Mt=!1,xt=!1,vt=new N.Vector3(0,0,0),Pt=new N.Vector3(0,0,0),zt=!1,Tt=[],nt=Z.facePoints[2].clone(),ft=Z.facePoints[3].clone(),rt=Z.facePoints[0].clone(),tt=Z.facePoints[1].clone(),le=!1,yt=-1;if(Math.abs(Z.facePoints[0].z-p.start.z)<.25){let Jt=!1;for(let st=0;st<e.length;st++){let Ct=st+1;if(Ct>=e.length)continue;let Nt=new N.Vector3(e[st].x,e[st].y,e[st].z),lt=new N.Vector3(e[Ct].x,e[Ct].y,e[Ct].z);if(Ee(new N.Vector3(Nt.x,Nt.y,0),new N.Vector3(lt.x,lt.y,0),new N.Vector3(Z.facePoints[0].x,Z.facePoints[0].y,0),new N.Vector3(Z.facePoints[1].x,Z.facePoints[1].y,0),.1)!=null){Jt=!0;break}}let{newOriginalPoints:me}=wn(rt,tt,p.originalPoints),Rt=[],ee=[];if(me.forEach(st=>{st.z>=nt.z?Rt.push(st):st.z<=rt.z&&ee.push(st)}),Rt.length<20)continue;let et=p.rooftopPz-nt.z,pe=nt.clone();pe.z+=et;let nn=nt.distanceTo(ft),Le=new N.Vector3().subVectors(ft,nt).normalize(),gn=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(nn)))),ke=nn/gn,ge=[],on=[];for(let st=0;st<gn;st++)ge.push(new N.Vector3(nt.x,nt.y,nt.z+et/5).addScaledVector(Le,ke*st)),ge.push(new N.Vector3(pe.x,pe.y,pe.z-et/5).addScaledVector(Le,ke*st));let Oe=ge.length;for(let st=0;st<Rt.length;st++)for(let Ct=0;Ct<ge.length;Ct++)if(new N.Vector3(Rt[st].x,Rt[st].y,Rt[st].z).distanceTo(ge[Ct])<.05){on.push(ge[Ct]),ge.splice(Ct,1);break}if(on.length/Oe<.5)continue;if(U>.17&&p.length>.5){let st=Zt(P,rt),Ct=Zt(P,tt),Nt=Zt(E,rt),lt=Zt(E,tt),X=.15,Et=10;if(st<X||Ct<X)if(Rt.length>10){let wt=new N.Vector3(p.start.x,p.start.y,0),ut=new N.Vector3(p.end.x,p.end.y,0),pt=new N.Vector3().subVectors(wt,ut).normalize(),kt=new N.Vector3,gt=1/0,Ft=-1,bt=!1,dt=!1;for(let J=0;J<t.length;J++){if(J==m)continue;let mt=new N.Vector3(t[J].start.x,t[J].start.y,0),at=new N.Vector3(t[J].end.x,t[J].end.y,0),Vt=Ee(wt,ut,mt,at,.1),Ht=ie(wt,ut,mt,at);if(Vt!=null){if(Ht&&Ht.angle>85){let Wt=Vt.point.distanceTo(wt),Bt=Vt.point.distanceTo(ut);if(Wt<.3){dt=!0,gt=1/0,Ft=-1;break}}continue}if(Ht&&Ht.angle>85){let Bt=new N.Line3(mt,at).closestPointToPoint(wt,!0,kt),Ot=Xt(wt,ut,Bt,wt);if(!Ot||Ot.direction!="same")continue;let re=wt.clone().addScaledVector(pt,Et),Kt=ut.clone().addScaledVector(pt.clone().negate(),Et),se=new N.Vector3().subVectors(at,mt).normalize(),te=mt.clone().addScaledVector(se.clone().negate(),.03),Re=at.clone().addScaledVector(se,.03),ae=Ee(re,Kt,te,Re);if(ae==null)continue;let ne=wt.distanceTo(ae.point);ne<gt&&(gt=ne,Ft=J)}else if(Ht&&Ht.angle<5){let Wt=new N.Vector3(rt.x,rt.y,0),Bt=new N.Vector3(tt.x,tt.y,0),Ot=Zt(Wt,mt),re=Zt(Wt,at),Kt=Zt(Bt,mt),se=Zt(Bt,at),te=Xt(wt,ut,mt,Wt),Re=!1,ae=o.readLineById(t[J].id);if(ae.checkResults=o.getLineCheckReultsById(t[J].id),t[J].checkResults=ae.checkResults,ae=null,t[J].checkResults){for(let ne=0;ne<t[J].checkResults.length;ne++){let yn=[];for(let ye=0;ye<t[J].checkResults[ne].originalVertices.length;ye++){let De=Fn(t[J].checkResults[ne].originalVertices[ye]);De.z=0,yn.push(De)}let xe=We(yn);if(xe.length==2){let ye=Zt(Wt,xe[0]),De=Zt(Wt,xe[1]),go=Zt(Bt,xe[0]),xo=Zt(Bt,xe[1]);if(ye<.2||De<.2||go<.2||xo<.2){Re=!0;break}}}t[J].checkResults=null}if(te&&te.direction=="same"&&!Re&&(Ot<.1||re<.1||Kt<.1||se<.1)){bt=!0,gt=1/0,Ft=-1;break}}}if(gt!=1/0&&gt<2&&(p.length-U>.2||p.length-U<.02)&&gt<U*2)t[Ft].length>.25&&U+gt>.5&&(le=!0,st<X?(ft=ft.addScaledVector(pt,gt),rt=rt.addScaledVector(pt,gt)):(nt=nt.addScaledVector(pt,gt),tt=tt.addScaledVector(pt,gt)));else{if(!bt&&!dt&&p.length-U>.1)continue;if(!bt&&!dt&&p.length-U<.1&&p.length<1.8)continue}}else continue;if(Nt<X||lt<X)if(Rt.length>10){let wt=new N.Vector3(p.start.x,p.start.y,0),ut=new N.Vector3(p.end.x,p.end.y,0),pt=new N.Vector3().subVectors(ut,wt).normalize(),kt=new N.Vector3,gt=1/0,Ft=-1,bt=!1,dt=!1;for(let J=0;J<t.length;J++){if(J==m)continue;let mt=new N.Vector3(t[J].start.x,t[J].start.y,0),at=new N.Vector3(t[J].end.x,t[J].end.y,0),Vt=Ee(wt,ut,mt,at,.03),Ht=ie(wt,ut,mt,at);if(Vt!=null){if(Ht&&Ht.angle>85){let Wt=Vt.point.distanceTo(wt);if(Vt.point.distanceTo(ut)<.3){dt=!0,gt=1/0,Ft=-1;break}}continue}if(Ht&&Ht.angle>85){let Bt=new N.Line3(mt,at).closestPointToPoint(ut,!0,kt),Ot=Xt(ut,wt,Bt,ut);if(!Ot||Ot.direction!="same")continue;let re=wt.clone().addScaledVector(pt.clone().negate(),Et),Kt=ut.clone().addScaledVector(pt,Et),se=new N.Vector3().subVectors(at,mt).normalize(),te=mt.clone().addScaledVector(se.clone().negate(),.03),Re=at.clone().addScaledVector(se,.03),ae=Ee(re,Kt,te,Re);if(ae==null)continue;let ne=ut.distanceTo(ae.point);ne<gt&&(gt=ne,Ft=J)}else if(Ht&&Ht.angle<5){let Wt=new N.Vector3(rt.x,rt.y,0),Bt=new N.Vector3(tt.x,tt.y,0),Ot=Zt(Wt,mt),re=Zt(Wt,at),Kt=Zt(Bt,mt),se=Zt(Bt,at),te=Xt(ut,wt,mt,Wt),Re=!1,ae=o.readLineById(t[J].id);if(ae.checkResults=o.getLineCheckReultsById(t[J].id),t[J].checkResults=ae.checkResults,ae=null,t[J].checkResults){for(let ne=0;ne<t[J].checkResults.length;ne++){let yn=[];for(let ye=0;ye<t[J].checkResults[ne].originalVertices.length;ye++){let De=Fn(t[J].checkResults[ne].originalVertices[ye]);De.z=0,yn.push(De)}let xe=We(yn);if(xe.length==2){let ye=Zt(Wt,xe[0]),De=Zt(Wt,xe[1]),go=Zt(Bt,xe[0]),xo=Zt(Bt,xe[1]);if(ye<.2||De<.2||go<.2||xo<.2){Re=!0;break}}}t[J].checkResults=null}if(te&&te.direction=="same"&&!Re&&(Ot<.1||re<.1||Kt<.1||se<.1)){bt=!0,gt=1/0,Ft=-1;break}}}if(gt!=1/0&&gt<2&&(p.length-U>.2||p.length-U<.02)&&gt<U*2)t[Ft].length>.25&&U+gt>.5&&(le=!0,Nt<X?(ft=ft.addScaledVector(pt,gt),rt=rt.addScaledVector(pt,gt)):(nt=nt.addScaledVector(pt,gt),tt=tt.addScaledVector(pt,gt)));else{if(!bt&&!dt&&p.length-U>.1)continue;if(!bt&&!dt&&p.length-U<.1&&p.length<1.8)continue}}else continue;U=ft.distanceTo(nt)}if(ht<85&&U>.3||U>.7&&it>S-S/2){if(!p.isLine2){if(U<3.5)for(let st=0;st<s.length;st++){let Ct=new N.Vector3(s[st].doorStartPt.x,s[st].doorStartPt.y,0),Nt=new N.Vector3(s[st].doorEndPt.x,s[st].doorEndPt.y,0),lt=new N.Vector3(Z.facePoints[0].x,Z.facePoints[0].y,0),X=new N.Vector3(Z.facePoints[1].x,Z.facePoints[1].y,0),Et=Z.facePoints[2].z<s[st].maxZ||Math.abs(Z.facePoints[2].z-s[st].maxZ)<.3,wt=Math.abs(Z.facePoints[2].z-s[st].maxZ)>.2;if(Et){let ut=be(Ct.clone(),Nt.clone(),lt.clone(),X.clone(),{distanceThreshold:2});if(ut&&ut.type=="collinear_gap"){let pt=At(Ct,Nt),kt=new N.Line3(new N.Vector3(p.start.x,p.start.y,0),new N.Vector3(p.end.x,p.end.y,0)),gt=new N.Vector3,Ft=kt.closestPointToPoint(pt,!0,gt);if(pt.distanceTo(Ft)<.15){let dt=lt.distanceTo(Ct),J=X.distanceTo(Ct);if(dt>.25&&J>.25){let Vt=lt.distanceTo(Nt),Ht=X.distanceTo(Nt);if(Vt>.2&&Ht>.2)continue}Mt=!0,zt=!0,Tt.push(s[st].index),s[st].isFind=!0,yt=st,xt=n[s[st].index].isFindBeam,n[s[st].index].isFindBeam=!0;let mt=new N.Vector3().subVectors(lt,X).normalize(),at=Ct.distanceTo(Nt)-ut.overlap;J<dt&&mt.negate(),dt>J?(nt=Z.facePoints[2].clone().addScaledVector(mt,at),ft=Z.facePoints[3].clone(),rt=ft.clone(),tt=nt.clone(),rt.z=tt.z=Z.facePoints[0].z):(nt=Z.facePoints[2].clone(),ft=Z.facePoints[3].clone().addScaledVector(mt,at),rt=ft.clone(),tt=nt.clone(),rt.z=tt.z=Z.facePoints[0].z)}}else if(ut&&ut.type=="collinear_overlap"&&ut.maxPerpendicularDistance<.35){let pt=lt.distanceTo(X),kt=Nt.distanceTo(Ct);if(Math.abs(pt-ut.overlap)<.25||Math.abs(kt-ut.overlap)<.25){if(kt>pt&&Math.abs(kt-ut.overlap)>1){let gt=lt.distanceTo(Ct),Ft=lt.distanceTo(Nt),bt=X.distanceTo(Ct),dt=X.distanceTo(Nt),J=gt<Ft?gt:Ft,mt=bt<dt?bt:dt,at=new N.Vector3().subVectors(lt,X).normalize(),Vt=kt-ut.overlap;J<mt?(at.negate(),nt=Z.facePoints[2].clone().addScaledVector(at,Vt),ft=Z.facePoints[3].clone(),rt=ft.clone(),tt=nt.clone(),rt.z=tt.z=Z.facePoints[0].z):(nt=Z.facePoints[2].clone(),ft=Z.facePoints[3].clone().addScaledVector(at,Vt),rt=ft.clone(),tt=nt.clone(),rt.z=tt.z=Z.facePoints[0].z)}U>1.5&&(Mt=!0,zt=!0,Tt.push(s[st].index),s[st].isFind=!0,xt=n[s[st].index].isFindBeam,n[s[st].index].isFindBeam=!0,yt=st)}}}}if(!zt&&U<1.5)for(let st=0;st<s.length;st++){if(s[st].inWall)continue;let Ct=new N.Vector3(s[st].doorStartPt.x,s[st].doorStartPt.y,0),Nt=new N.Vector3(s[st].doorEndPt.x,s[st].doorEndPt.y,0),lt=new N.Vector3(ft.x,ft.y,0),X=new N.Vector3(nt.x,nt.y,0),Et=new N.Vector3().subVectors(Ct,Nt).normalize(),wt=new N.Vector3().subVectors(lt,X).normalize(),ut=.25,pt=Ct.clone().addScaledVector(Et,ut),kt=Nt.clone().addScaledVector(Et.clone().negate(),ut),gt=lt.clone().addScaledVector(wt,ut),Ft=X.clone().addScaledVector(wt.clone().negate(),ut);if(Xt(pt,kt,gt,Ft).isParallel)continue;let dt=Ee(pt,kt,gt,Ft),J=!0,mt=Ct.distanceTo(lt),at=Ct.distanceTo(X),Vt=Nt.distanceTo(lt),Ht=Nt.distanceTo(X);if((mt<c||at<c||Vt<c||Ht<c||dt)&&J){let Bt=new N.Line3(lt,X);if(mt<c||Vt<c)if(vt=lt.clone(),mt<c){let Ot=Bt.closestPointToPoint(Nt,!1,new N.Vector3);Pt=new N.Vector3().subVectors(Nt,Ot).normalize()}else{let Ot=Bt.closestPointToPoint(Ct,!1,new N.Vector3);Pt=new N.Vector3().subVectors(Ct,Ot).normalize()}else if(at<c||Ht<c)if(vt=X.clone(),at<c){let Ot=Bt.closestPointToPoint(Nt,!1,new N.Vector3);Pt=new N.Vector3().subVectors(Nt,Ot).normalize()}else{let Ot=Bt.closestPointToPoint(Ct,!1,new N.Vector3);Pt=new N.Vector3().subVectors(Ct,Ot).normalize()}else if(dt){let Ot=dt.point.distanceTo(lt),re=dt.point.distanceTo(X),Kt=dt.point.distanceTo(pt),se=dt.point.distanceTo(kt);if(Ot<re?vt=lt.clone():vt=X.clone(),Kt<se){let te=Bt.closestPointToPoint(kt,!1,new N.Vector3);Pt=new N.Vector3().subVectors(kt,te).normalize()}else{let te=Bt.closestPointToPoint(pt,!1,new N.Vector3);Pt=new N.Vector3().subVectors(pt,te).normalize()}}xt=n[s[st].index].isFindBeam,n[s[st].index].isFindBeam=!0,Mt=!0,s[st].isFind=!0,yt=st}}}(Mt||Jt||Math.abs(it-S)>.1&&it>S/3&&U>.3)&&(ot=!0)}}if(ot){{let lt=new N.Vector3().subVectors(rt,tt),X=new N.Vector3().subVectors(tt,rt),Et=1/0,wt=1/0,ut=rt.clone(),pt=tt.clone(),kt=[],gt=[],Ft=new N.Line3(nt,ft),bt=new N.Vector3,dt=999999;if(p.originalPoints.forEach(J=>{if(J.z-rt.z<it/2+.1&&J.z-rt.z>it/2-.1&&kt.push(J),J.z>nt.z){let mt=Ft.closestPointToPoint(J,!0,bt),at=new N.Vector3().subVectors(mt,nt),Vt=new N.Vector3().subVectors(mt,ft),Ht=$t(lt,Vt),Wt=$t(X,at);Ht&&Ht.parallel&&!Ht.sameDirection&&Wt&&Wt.parallel&&!Wt.sameDirection&&gt.push(J)}}),kt.length>0){let{newProjectPoints:J}=wn(rt,tt,kt);for(let mt of J)mt.z=rt.z;J.forEach(mt=>{let at=new N.Vector3(mt.x,mt.y,rt.z),Vt=new N.Vector3().subVectors(at,tt),Ht=new N.Vector3().subVectors(at,rt),Wt=$t(lt,Ht),Bt=$t(X,Vt);if(Wt&&Wt.parallel&&Wt.sameDirection){let Ot=at.distanceTo(rt);Ot<Et&&(Et=Ot,ut=at)}else if(Bt&&Bt.parallel&&Bt.sameDirection){let Ot=at.distanceTo(tt);Ot<wt&&(wt=Ot,pt=at)}})}if(ut.equals(rt)||(rt=ut,ft=new N.Vector3(rt.x,rt.y,ft.z)),pt.equals(tt)||(tt=pt,nt=new N.Vector3(tt.x,tt.y,nt.z)),gt.length>10){let{newOnLinePoints:J}=wn(ft,nt,gt);for(let mt=0;mt<J.length;mt++){let Vt=Ft.closestPointToPoint(J[mt],!0,bt).distanceTo(J[mt]);Vt>.02&&dt>Vt&&(dt=Vt)}nt.z+=dt,ft.z+=dt,it+=dt}}let Jt=!1;for(let lt=0;lt<d.length;lt++){let X=be(d[lt].start,d[lt].end,rt,tt);X&&X.type=="collinear_overlap"&&X.maxPerpendicularDistance<.07&&(Jt=!0)}if(Jt){yt!=-1&&(xt||(n[s[yt].index].isFindBeam=!1,s[yt].isFind=!1));continue}let me=tt.distanceTo(rt);if(me<.4&&!Mt||me<.3&&Mt)continue;u.includes(m)||u.push(m);let Rt="",ee=Mt?"singleDoor":"";ee=zt?"pullOutDoor":ee;let et={id:i,beamStart:nt,beamEnd:ft,beamHeight:p.rooftopPz-nt.z,doorStart:rt,doorEnd:tt,doorHeight:it,nearId:-1,type:Rt,isExtend:le,isDoor:Mt,isPullOutDoor:zt,pcbDoorIndexs:Tt,beamNearWallVec:new N.Vector3(0,0,0),doorType:ee,doorOpenEndPoint:vt,doorOpenVector:Pt,lineIsOnlyBeam:!1};if(et.beamHeight<.1)continue;let pe=1/0,nn=!1,Le=new N.Vector3,gn=new N.Vector3,ke=-1,ge=-1,on=-1,Oe=!1,xn=!1;{let lt=new N.Line3(rt.clone(),tt.clone()),X=-1;for(let Et=0;Et<t.length;Et++){if(t[Et].length<.5||Et==m)continue;let wt=new N.Vector3(t[Et].start.x,t[Et].start.y,t[Et].start.z),ut=new N.Vector3(t[Et].end.x,t[Et].end.y,t[Et].end.z),pt=be(wt,ut,rt,tt),kt=Xt(wt,ut,rt,tt);if(pt&&pt.maxPerpendicularDistance<.5){if(kt&&kt.isParallel){let at=new N.Line3(wt,ut),Vt=new N.Vector3(rt.x,rt.y,wt.z),Ht=at.closestPointToPoint(Vt,!0,new N.Vector3),Wt=ie(Ht,Vt,rt,tt);Wt&&Wt.angle>85&&(et.beamNearWallVec=new N.Vector3().subVectors(Ht,Vt).normalize(),et.beamStart.z>t[Et].rooftopPz-.03&&U>1?xn=!0:xn=!1)}let gt=[],Ft=o.readLineById(t[Et].id);Ft.originalPoints=o.getOriginalPointsById(t[Et].id),t[Et].originalPoints=Ft.originalPoints,Ft=null,t[Et].originalPoints.forEach(at=>{at.z>rt.z+.2&&at.z<rt.z+it/2&&gt.push(new N.Vector3(at.x,at.y,rt.z))}),t[Et].originalPoints=null;let bt=new N.Vector3,dt=[],J=[];for(let at=0;at<gt.length;at++){let Vt=lt.closestPointToPoint(gt[at],!0,bt);Vt.distanceTo(gt[at])<.15&&(dt.push(gt[at]),J.push(Vt.clone()))}for(let at=0;at<J.length;at++)zo(lt,J[at])&&dt.push(J[at]);if(dt=J,dt=Mo(dt),dt.length<150)continue;let mt=We(dt);if(mt.length==2){let at=mt[0].distanceTo(mt[1]);if(Math.abs(at-lt.distance())<.3){X=pt.maxPerpendicularDistance;let Vt=new N.Line3(wt,ut),Ht=new N.Vector3(rt.x,rt.y,wt.z),Wt=Vt.closestPointToPoint(Ht,!0,new N.Vector3);if(et.beamNearWallVec=new N.Vector3().subVectors(Wt,Ht).normalize(),et.beamStart.z>t[Et].rooftopPz-.03&&U>.8)xn=!0,Oe=!0;else if(Jt=!0,yt!=-1&&(xt||(n[s[yt].index].isFindBeam=!1,s[yt].isFind=!1)),ge!=-1&&on!=-1){let Bt=t[ge].doorAndBeamData[on];Bt.nearId=-1}break}}if(Jt)break}else pt&&pt.type=="collinear_overlap"&&pt.maxPerpendicularDistance>.3&&pt.maxPerpendicularDistance<.7&&(Oe=!0)}if(Jt){if(U<1.5||X<.08)continue;Oe=!0}}let st=!1;if(ke!=-1)for(let lt=0;lt<f.length&&!(f[lt].length==1&&(f[lt][0].id==i?(f[lt].push({id:ke,beamStart:Le,beamEnd:gn}),st=!0):f[lt][0].id==ke&&(f[lt].push({id:i,beamStart:nt,beamEnd:ft}),st=!0),st));lt++);st||f.push([{id:i,beamStart:nt,beamEnd:ft}]);let Ct=et.doorStart.distanceTo(et.doorEnd),Nt=Ve(p.originalPoints);if(et.lineIsOnlyBeam=Nt.minZ>p.start.z&&Nt.minZ-p.start.z>1,Mt&&Oe){yt!=-1&&(xt||(n[s[yt].index].isFindBeam=!1,s[yt].isFind=!1));continue}if(Mt?Rt="door":Ct>1.5||le?Rt="beam":Rt=Oe&&Ct>.8?"beam":"hole",Rt!="beam"&&t[m].isLine2)continue;et.type=Rt,et.wallBelowBeam=xn;for(let lt=0;lt<a.length;lt++){let X=t[a[lt].linesIndex].doorAndBeamData[a[lt].doorIndex];if(X.nearId!=-1||et.type=="beam"&&X.type!="beam"||et.type!="beam"&&X.type=="beam")continue;let Et=Xt(nt.clone(),ft.clone(),X.beamStart.clone(),X.beamEnd.clone());if(Et&&Et.isParallel){let wt=nt.distanceTo(ft),ut=X.beamStart.distanceTo(X.beamEnd),pt=new N.Line3(nt.clone(),ft.clone());pt.start.z=0,pt.end.z=0;let kt=At(X.beamStart,X.beamEnd,!0);ut>wt&&(pt.start=new N.Vector3(X.beamStart.x,X.beamStart.y,0),pt.end=new N.Vector3(X.beamEnd.x,X.beamEnd.y,0),kt=At(nt,ft,!0));let gt=new N.Vector3,Ft=pt.closestPointToPoint(kt,!0,gt),bt=Ft.distanceTo(kt),dt=new N.Vector3().subVectors(kt,Ft).normalize(),J=dt.clone().negate(),mt=kt.clone().addScaledVector(J,bt+.1),at=Ee(pt.start,pt.end,kt,mt),Vt=!Mt&&!X.isDoor&&(wt>ut*2||ut>wt*2);if(bt<.4&&at!=null&&!Vt){let Ht=!1;for(let Kt=0;Kt<p.doorAndBeamData.length;Kt++)if(p.doorAndBeamData[Kt].id==X.id){Ht=!0;break}if(Ht)continue;X.nearId=i,et.nearId=X.id;let Wt=.8;(X.isDoor||et.isDoor)&&(X.isDoor=!0,et.isDoor=!0),X.doorType!=""?et.doorType=X.doorType:et.doorType!=""&&(X.doorType=et.doorType),X.isPullOutDoor?et.isPullOutDoor=!0:et.isPullOutDoor&&(X.isPullOutDoor=!0);let Bt=Tt.some(Kt=>X.pcbDoorIndexs.includes(Kt)),Ot=!1,re=!1;if(et.type=="beam"&&X.type=="beam"){let Kt=o.readLineById(t[a[lt].linesIndex].id);Kt.originalPoints=o.getOriginalPointsById(t[a[lt].linesIndex].id),t[a[lt].linesIndex].originalPoints=Kt.originalPoints,Kt=null;let se=Ve(t[a[lt].linesIndex].originalPoints),te=Ve(p.originalPoints);t[a[lt].linesIndex].originalPoints=null,se.minZ>t[a[lt].linesIndex].start.z&&se.minZ-t[a[lt].linesIndex].start.z>1&&te.minZ>p.start.z&&te.minZ-p.start.z>1&&(Ot=wt>ut,re=wt<ut,ut>wt&&(et.isExtend=!0),wt>ut&&(t[a[lt].linesIndex].isExtend=!0))}wt<ut&&!re||Ot?(Ot&&(J=J.negate(),dt=dt.negate()),wt>Wt&&!X.isPullOutDoor?(X.beamStart=nt.clone().addScaledVector(J,bt),X.beamEnd=ft.clone().addScaledVector(J,bt),X.doorStart=rt.clone().addScaledVector(J,bt),X.doorEnd=tt.clone().addScaledVector(J,bt),X.doorHeight=et.doorHeight,X.beamStart.z=X.beamEnd.z=X.doorStart.z+X.doorHeight,X.doorOpenEndPoint=et.doorOpenEndPoint.clone(),X.doorOpenVector=et.doorOpenVector.clone()):!zt||zt&&X.isPullOutDoor&&Bt?(et.beamStart=X.beamStart.clone().addScaledVector(dt,bt),et.beamEnd=X.beamEnd.clone().addScaledVector(dt,bt),et.doorStart=X.doorStart.clone().addScaledVector(dt,bt),et.doorEnd=X.doorEnd.clone().addScaledVector(dt,bt),et.doorHeight=X.doorHeight,et.beamStart.z=et.beamEnd.z=et.doorStart.z+et.doorHeight,et.doorOpenEndPoint=X.doorOpenEndPoint.clone(),et.doorOpenVector=X.doorOpenVector.clone()):(X.beamStart=nt.clone().addScaledVector(J,bt),X.beamEnd=ft.clone().addScaledVector(J,bt),X.doorStart=rt.clone().addScaledVector(J,bt),X.doorEnd=tt.clone().addScaledVector(J,bt),X.doorHeight=et.doorHeight,X.beamStart.z=X.beamEnd.z=X.doorStart.z+X.doorHeight,X.doorOpenEndPoint=et.doorOpenEndPoint.clone(),X.doorOpenVector=et.doorOpenVector.clone())):(wt>ut&&!Ot||re)&&(re&&(J=J.negate(),dt=dt.negate()),ut>Wt&&!zt?(et.beamStart=X.beamStart.clone().addScaledVector(J,bt),et.beamEnd=X.beamEnd.clone().addScaledVector(J,bt),et.doorStart=X.doorStart.clone().addScaledVector(J,bt),et.doorEnd=X.doorEnd.clone().addScaledVector(J,bt),et.doorHeight=X.doorHeight,et.beamStart.z=et.beamEnd.z=et.doorStart.z+et.doorHeight,et.doorOpenEndPoint=X.doorOpenEndPoint.clone(),et.doorOpenVector=X.doorOpenVector.clone()):!X.isPullOutDoor||zt&&X.isPullOutDoor&&Bt?(X.beamStart=nt.clone().addScaledVector(dt,bt),X.beamEnd=ft.clone().addScaledVector(dt,bt),X.doorStart=rt.clone().addScaledVector(dt,bt),X.doorEnd=tt.clone().addScaledVector(dt,bt),X.doorHeight=et.doorHeight,X.beamStart.z=X.beamEnd.z=X.doorStart.z+X.doorHeight,X.doorOpenEndPoint=et.doorOpenEndPoint.clone(),X.doorOpenVector=et.doorOpenVector.clone()):(et.beamStart=X.beamStart.clone().addScaledVector(J,bt),et.beamEnd=X.beamEnd.clone().addScaledVector(J,bt),et.doorStart=X.doorStart.clone().addScaledVector(J,bt),et.doorEnd=X.doorEnd.clone().addScaledVector(J,bt),et.doorHeight=X.doorHeight,et.beamStart.z=et.beamEnd.z=et.doorStart.z+et.doorHeight,et.doorOpenEndPoint=X.doorOpenEndPoint.clone(),et.doorOpenVector=X.doorOpenVector.clone())),ke=et.nearId,Le=X.beamStart.clone(),gn=X.beamEnd.clone(),nn=!0,ge=a[lt].linesIndex,on=a[lt].doorIndex;break}}}if(et.nearId!=-1&&Rt=="hole"){let lt=!1;for(let X=0;X<t.length;X++)if(t[X].doorAndBeamData){for(let Et=0;Et<t[X].doorAndBeamData.length;Et++)if(t[X].doorAndBeamData[Et].nearId==i&&t[X].doorAndBeamData[Et].type=="beam"){t[X].doorAndBeamData[Et].type=Rt,lt=!0;break}if(lt)break}}if(p.doorAndBeamIdToCheckResults.push([v.index,i]),p.doorAndBeamData.push(et),a.push({linesIndex:m,doorIndex:p.doorAndBeamData.length-1}),d.push({start:rt,end:tt}),i++,zt&&U<2)break}}}let W=p.length*(p.rooftopPz-p.start.z);p.completePointAreaPercentage=I/W*100,p.checkResults=null,p.originalPoints=null,p.mergeCheckRegion=null}if(u.length>1)for(let m=0;m<u.length;m++){let p=[];for(let b=0;b<t[u[m]].doorAndBeamData.length;b++){let w=!1;if(t[u[m]].doorAndBeamData[b].type==="beam"&&(t[u[m]].doorAndBeamData[b].beamNearWallVec.x==0&&t[u[m]].doorAndBeamData[b].beamNearWallVec.y==0&&t[u[m]].doorAndBeamData[b].beamNearWallVec.z==0&&t[u[m]].doorAndBeamData[b].nearId==-1||!t[u[m]].doorAndBeamData[b].wallBelowBeam&&t[u[m]].doorAndBeamData[b].nearId==-1)&&(t[u[m]].doorAndBeamData[b].lineIsOnlyBeam?w=!0:t[u[m]].doorAndBeamData[b].type="hole"),!w){let z=t[u[m]].doorAndBeamData[b].doorStart.clone(),M=t[u[m]].doorAndBeamData[b].doorEnd.clone();z.z=M.z=0;let P=z.distanceTo(M);for(let E=0;E<u.length;E++)if(m!=E)for(let S=0;S<t[u[E]].doorAndBeamData.length;S++){let I=t[u[E]].doorAndBeamData[S].doorStart.clone(),W=t[u[E]].doorAndBeamData[S].doorEnd.clone();I.z=W.z=0;let v=I.distanceTo(W),T=be(z,M,I,W);if(T.type=="collinear_overlap"&&T.maxPerpendicularDistance<.2){let R=new N.Line3(I,W),D=At(z,M),H=R.closestPointToPoint(D,!0,new N.Vector3),V=D.distanceTo(H);if(Math.abs(P-T.overlap)<.1&&V<.04){t[u[E]].doorAndBeamData[S].nearId==t[u[m]].doorAndBeamData[b].id&&(t[u[E]].doorAndBeamData[S].nearId=-1),w=!0;break}}}}if(w){for(let z=0;z<t[u[m]].doorAndBeamIdToCheckResults.length;z++)if(t[u[m]].doorAndBeamIdToCheckResults[1]==t[u[m]].doorAndBeamData[b].id){t[u[m]].doorAndBeamIdToCheckResults.splice(z,1);break}}w||p.push(t[u[m]].doorAndBeamData[b])}p.length!=t[u[m]].doorAndBeamData.length&&(t[u[m]].doorAndBeamData=p)}let l=.65,h=2,g=[],x=[];for(let m=0;m<s.length;m++){if(s[m].isFind||s[m].inWall)continue;let p=new N.Vector3(s[m].doorStartPt.x,s[m].doorStartPt.y,0),b=new N.Vector3(s[m].doorEndPt.x,s[m].doorEndPt.y,0),w=new N.Vector3().subVectors(p,b).normalize();for(let z=0;z<t.length;z++){if(t[z].length<.7||t[z].completePointAreaPercentage>60)continue;let M=new N.Vector3(t[z].start.x,t[z].start.y,0),P=new N.Vector3(t[z].end.x,t[z].end.y,0),E=new N.Vector3().subVectors(M,P).normalize(),S=.3,I=p.clone().addScaledVector(w,S),W=b.clone().addScaledVector(w.clone().negate(),S),v=M.clone().addScaledVector(E,S),T=P.clone().addScaledVector(E.clone().negate(),S),R=Ee(I,W,v,T),D=Xt(I,W,v,T),H=p.distanceTo(M)>b.distanceTo(M)?b:p,V=H.distanceTo(M),C=H.distanceTo(P);if((V<c||C<c)&&D&&!D.isParallel){let A=p.distanceTo(M),L=p.distanceTo(P),k=A<L?M:P,F=!1;for(let B=0;B<t.length;B++){if(B==z||g.includes(B)||t[B].length<.7)continue;let O=new N.Vector3(t[B].start.x,t[B].start.y,0),$=new N.Vector3(t[B].end.x,t[B].end.y,0),G=new N.Vector3().subVectors(O,$).normalize(),Q=G.clone().negate(),j=O.clone().addScaledVector(G,5),Z=$.clone().addScaledVector(Q,5),U=ie(M,P,O,$),it=be(M,P,O,$),ht=Ee(M,P,j,Z),St=!0;if(ht!=null&&(St=ht.point.distanceTo(M)<c||ht.point.distanceTo(P)<c),(U&&U.angle>85||it&&it.type=="collinear_gap")&&St){let ot=new N.Line3(M,P),Mt=new N.Vector3,xt=ot.closestPointToPoint(O,!0,Mt),vt=ot.closestPointToPoint($,!0,Mt),Pt=xt.distanceTo(O),zt=vt.distanceTo($),Tt,nt;if(Pt>zt&&zt>l)Tt=$,nt=vt;else if(Pt<zt&&Pt>l)Tt=O,nt=xt;else continue;if(Pt=k.distanceTo(O),zt=k.distanceTo($),Pt>h&&zt>h)continue;let ft=Xt(Tt,nt,O,$),rt=Xt(Tt,nt,I,W);if(rt&&rt.isParallel||!ft||!ft.isParallel)continue;let tt=be(p,b,Tt,nt);if(tt&&tt.type=="collinear_overlap"&&tt.maxPerpendicularDistance<.2)continue;Tt.z=nt.z=t[B].start.z;let le=!1;for(let Rt=0;Rt<x.length;Rt++){let ee=be(x[Rt].start,x[Rt].end,Tt,nt);if(ee&&ee.type=="collinear_overlap"&&ee.maxPerpendicularDistance<.2){le=!0;break}}if(le)continue;for(let Rt=0;Rt<t.length;Rt++){if(t[Rt].length<.5)continue;let ee=new N.Vector3(t[Rt].start.x,t[Rt].start.y,t[Rt].start.z),et=new N.Vector3(t[Rt].end.x,t[Rt].end.y,t[Rt].end.z),pe=be(ee,et,Tt,nt);if(pe&&pe.type=="collinear_overlap"&&pe.maxPerpendicularDistance<.1){le=!0;break}}if(le)continue;x.push({start:Tt,end:nt}),g.push(B);let yt=t[B].rooftopPz-t[B].start.z,Jt=t[z].rooftopPz-t[z].start.z,me=yt<Jt?yt:Jt;t[B].doorAndBeamData||(t[B].doorAndBeamData=[]),t[B].doorAndBeamData.push({id:i,beamStart:Tt,beamEnd:nt,beamHeight:me,doorStart:Tt,doorEnd:nt,doorHeight:me,nearId:-1,type:"onlyDoor",isDoor:!0,beamNearWallVec:new N.Vector3(0,0,0)}),n[s[m].index].isFindBeam=!0,n[s[m].index].isFindOnlyDoor=!0,f.push([{id:i,beamStart:Tt,beamEnd:nt}]),i++,F=!0;break}}if(F)break}}}let y=[];for(let m=0;m<t.length;m++)t[m].isLine2||y.push(t[m]),t[m].isLine2&&t[m].doorAndBeamData.length!=0&&y.push(t[m]);return t.length=0,t.push(...y),{lines:t,beamGroup:f,pcdData:n}},Xi=async(t,e)=>{for(let n=0;n<t.length;n++){let o=t[n],r=new N.Vector3(o.start.x,o.start.y,o.start.z),s=new N.Vector3(o.end.x,o.end.y,o.end.z),i=new N.Line3(r.clone(),s.clone()),a=-1;for(let u=0;u<o.doorAndBeamData.length;u++){let d=o.doorAndBeamData[u],f=d.doorHeight;d.isRebuild=!1;let c=!1,l=!1;if(!(d.nearId!=-1||d.type!="beam")){for(let h=0;h<t.length;h++){if(t[h].length<.5||h==n)continue;let g=new N.Vector3(t[h].start.x,t[h].start.y,t[h].start.z),x=new N.Vector3(t[h].end.x,t[h].end.y,t[h].end.z),y=be(g,x,r,s),m=Xt(g,x,r,s);if(y&&y.maxPerpendicularDistance<.5){if(m&&m.isParallel){let E=new N.Line3(g,x),S=new N.Vector3(r.x,r.y,g.z),I=E.closestPointToPoint(S,!0,new N.Vector3),W=ie(I,S,r,s);W&&W.angle>85&&(d.beamNearWallVec=new N.Vector3().subVectors(I,S).normalize())}let p=[],b=e.readLineById(t[h].id);b.originalPoints=e.getOriginalPointsById(t[h].id),t[h].originalPoints=b.originalPoints,b=null,t[h].originalPoints.forEach(E=>{E.z>r.z+.2&&E.z<r.z+f/2&&p.push(new N.Vector3(E.x,E.y,r.z))}),t[h].originalPoints=null;let w=new N.Vector3,z=[],M=[];for(let E=0;E<p.length;E++){let S=i.closestPointToPoint(p[E],!0,w);S.distanceTo(p[E])<.15&&(z.push(p[E]),M.push(S.clone()))}for(let E=0;E<M.length;E++)zo(i,M[E])&&z.push(M[E]);if(z=M,z=Mo(z),z.length<150)continue;let P=We(z);if(P.length==2){let E=P[0].distanceTo(P[1]);if(Math.abs(E-i.distance())<.3){a=y.maxPerpendicularDistance;let S=new N.Line3(g,x),I=new N.Vector3(r.x,r.y,g.z),W=S.closestPointToPoint(I,!0,new N.Vector3);d.beamNearWallVec=new N.Vector3().subVectors(W,I).normalize(),d.beamStart.z>t[h].rooftopPz-.03&&length>1?l=!0:c=!0;break}}if(c)break}else y&&y.type=="collinear_overlap"&&y.maxPerpendicularDistance>.3&&y.maxPerpendicularDistance<.7&&(l=!0)}if(c){if(length<1.8||a<.2)continue;l=!0}l||(d.isRebuild=!0)}}}};import*as Lt from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";var Pr=(t,e={})=>{let n=e.eps??1e-6,o=e.project??(m=>({x:m.x,y:m.z})),r=e.skipSplit??!1,s=e.insideAreaSign??"positive",i=r?t.map((m,p)=>({start:Nn(m.start),end:Nn(m.end),uuid:m.uuid,sourceIndex:p,sourceSegment:m})):Zi(t,o,n),a=[],u=new Map;function d(m){let p=Math.round(m.x/n),b=Math.round(m.y/n);return`${p},${b}`}function f(m){let p=d(m);if(u.has(p))return u.get(p);let b=a.length;return a.push({id:b,x:m.x,y:m.y}),u.set(p,b),b}let c=[],l=new Map;for(let m of i){let p=o(m.start),b=o(m.end);if(Tr(p,b)<n*n)continue;let w=f(p),z=f(b);if(w===z)continue;let M=w<z?`${w}_${z}`:`${z}_${w}`;if(l.has(M))continue;let P=c.length;c.push({id:P,a:w,b:z,uuid:m.uuid,sourceIndex:m.sourceIndex,sourceSegment:m.sourceSegment||m}),l.set(M,P)}let h=[],g=Array.from({length:a.length},()=>[]);function x(m,p,b,w,z){let M=a[m],P=a[p],E=h.length,S=Math.atan2(P.y-M.y,P.x-M.x),I={id:E,from:m,to:p,twin:null,next:null,angle:S,edgeId:b,uuid:w,sourceIndex:z,visited:!1};return h.push(I),g[m].push(E),E}for(let m of c){let p=x(m.a,m.b,m.id,m.uuid,m.sourceIndex),b=x(m.b,m.a,m.id,m.uuid,m.sourceIndex);h[p].twin=b,h[b].twin=p}for(let m of g)m.sort((p,b)=>h[p].angle-h[b].angle);for(let m of h){let p=g[m.to],b=m.twin,w=p.indexOf(b);if(w===-1)continue;let z=(w-1+p.length)%p.length;m.next=p[z]}let y=[];for(let m of h){if(m.visited)continue;let p=[],b=[],w=m.id,z=0,M=h.length+10;for(;;){let v=h[w];if(!v||v.visited)break;if(v.visited=!0,p.push(w),b.push(v.from),w=v.next,z++,z>M){console.warn("\u534A\u8FB9\u904D\u5386\u5F02\u5E38\uFF0C\u53EF\u80FD\u5B58\u5728\u975E\u5E73\u9762\u56FE\u3001\u91CD\u590D\u7EBF\u3001\u60AC\u6302\u7EBF\u6216\u810F\u6570\u636E");break}}if(p.length<3)continue;let P=b.map(v=>a[v]),E=Yi(P),S=!1;if(s==="positive"?S=E>n:S=E<-n,!S)continue;let I=p.map(v=>h[v].uuid),W=p.map(v=>h[v].sourceIndex);y.push({uuids:I,area:Math.abs(E),lineIndexes:W})}return y};function Zi(t,e,n=1e-6){let o=[];for(let r=0;r<t.length;r++){let s=t[r],i=e(s.start),a=e(s.end),u=[{t:0,point:Nn(s.start)},{t:1,point:Nn(s.end)}];for(let f=0;f<t.length;f++){if(r===f)continue;let c=t[f],l=e(c.start),h=e(c.end),g=Gi(i,a,l,h,n);g&&g.t>n&&g.t<1-n&&u.push({t:g.t,point:Ui(s.start,s.end,g.t)})}u.sort((f,c)=>f.t-c.t);let d=[];for(let f of u)(d.length===0||Math.abs(f.t-d[d.length-1].t)>n)&&d.push(f);for(let f=0;f<d.length-1;f++){let c=d[f].point,l=d[f+1].point,h=e(c),g=e(l);Tr(h,g)<n*n||o.push({start:c,end:l,uuid:s.uuid,sourceIndex:r,sourceSegment:s})}}return o}function Gi(t,e,n,o,r=1e-6){let s={x:e.x-t.x,y:e.y-t.y},i={x:o.x-n.x,y:o.y-n.y},a={x:n.x-t.x,y:n.y-t.y},u=Po(s,i);if(Math.abs(u)<r)return null;let d=Po(a,i)/u,f=Po(a,s)/u;return d>=-r&&d<=1+r&&f>=-r&&f<=1+r?{t:d,u:f,point:{x:t.x+d*s.x,y:t.y+d*s.y}}:null}function Po(t,e){return t.x*e.y-t.y*e.x}function Tr(t,e){let n=t.x-e.x,o=t.y-e.y;return n*n+o*o}function Yi(t){let e=0;for(let n=0;n<t.length;n++){let o=t[n],r=t[(n+1)%t.length];e+=o.x*r.y-r.x*o.y}return e/2}function Ui(t,e,n){return t&&typeof t.clone=="function"&&typeof t.lerp=="function"?t.clone().lerp(e,n):{x:t.x+(e.x-t.x)*n,y:t.y+(e.y-t.y)*n,z:t.z!==void 0&&e.z!==void 0?t.z+(e.z-t.z)*n:void 0}}function Nn(t){return t&&typeof t.clone=="function"?t.clone():{x:t.x,y:t.y,z:t.z}}var Ki=(t,e)=>{if(!t||t.length<=0)return;let n={rectangles:[],threeEdgeRect:[]},o=new Set,r=.1,s=1.2,i=.15,a=2,u=80,d=95;if(!t[0].uuid){let l=1e4;for(let h=0;h<t.length;h++)t[h].uuid=l++}let f=[];for(let l=0;l<t.length;l++)f.push({start:t[l].start,end:t[l].end,uuid:t[l].uuid});let c=Pr(f,{eps:1e-5,returnDetail:!0,project:l=>({x:l.x,y:l.y})});for(let l=0;l<c.length;l++){if(c[l].uuids.length<4||c[l].area<i||c[l].area>a)continue;let h=!1;for(let g=0;g<c[l].lineIndexes.length;g++){let x=t[c[l].lineIndexes[g]].length;if(x<r||x>s)break;let y=new Lt.Vector3(t[c[l].lineIndexes[g]].start.x,t[c[l].lineIndexes[g]].start.y,t[c[l].lineIndexes[g]].start.z),m=new Lt.Vector3(t[c[l].lineIndexes[g]].end.x,t[c[l].lineIndexes[g]].end.y,t[c[l].lineIndexes[g]].end.z),p=!1,b=new Lt.Vector3(0,0,0);for(let w=g+1;w<c[l].lineIndexes.length;w++){let z=t[c[l].lineIndexes[w]].length;if(z<r||z>s)break;let M=new Lt.Vector3(t[c[l].lineIndexes[w]].start.x,t[c[l].lineIndexes[w]].start.y,t[c[l].lineIndexes[w]].start.z),P=new Lt.Vector3(t[c[l].lineIndexes[w]].end.x,t[c[l].lineIndexes[w]].end.y,t[c[l].lineIndexes[w]].end.z),E=Xt(y,m,M,P);if(E&&E.isParallel){if(x+z>s){p=!0;break}x+=z;let I=ct(y,M),W=ct(y,P),v=ct(m,M),T=ct(m,P);I?(y=m.clone(),m=P.clone()):W?(y=m.clone(),m=M.clone()):v?m=P.clone():T&&(m=M.clone());continue}let S=ie(y,m,M,P);if(!S||S.angle<u||S.angle>d){p=!0;break}for(let I=w+1;I<c[l].lineIndexes.length;I++){let W=t[c[l].lineIndexes[I]].length;if(W<r||W>s)break;let v=new Lt.Vector3(t[c[l].lineIndexes[I]].start.x,t[c[l].lineIndexes[I]].start.y,t[c[l].lineIndexes[I]].start.z),T=new Lt.Vector3(t[c[l].lineIndexes[I]].end.x,t[c[l].lineIndexes[I]].end.y,t[c[l].lineIndexes[I]].end.z),R=Xt(M,P,v,T);if(R&&R.isParallel){if(z+W>s){p=!0;break}z+=W;let H=ct(M,v),V=ct(M,T),C=ct(P,v),_=ct(P,T);H?(M=P.clone(),P=T.clone()):V?(M=P.clone(),P=v.clone()):C?P=T.clone():_&&(P=v.clone());continue}let D=ie(M,P,v,T);if(!D||D.angle<u||D.angle>d){p=!0;break}for(let H=I+1;H<c[l].lineIndexes.length;H++){let V=t[c[l].lineIndexes[H]].length;if(V<r||V>s)break;let C=new Lt.Vector3(t[c[l].lineIndexes[H]].start.x,t[c[l].lineIndexes[H]].start.y,t[c[l].lineIndexes[H]].start.z),_=new Lt.Vector3(t[c[l].lineIndexes[H]].end.x,t[c[l].lineIndexes[H]].end.y,t[c[l].lineIndexes[H]].end.z),A=Xt(v,T,C,_);if(A&&A.isParallel){if(W+V>s){p=!0;break}W+=V;let $=ct(v,C),G=ct(v,_),Q=ct(T,C),j=ct(T,_);$?(v=T.clone(),T=_.clone()):G?(v=T.clone(),T=C.clone()):Q?T=_.clone():j&&(T=C.clone());continue}let L=ie(v,T,C,_);if(!L||L.angle<u||L.angle>d){p=!0;break}let k=ct(C,y),F=ct(C,m),B=ct(_,y),O=ct(_,m);if(k||F||B||O)h=!0;else for(let $=H+1;$<c[l].lineIndexes.length;$++){let G=t[c[l].lineIndexes[$]].length;if(G<r||G>s)break;let Q=new Lt.Vector3(t[c[l].lineIndexes[$]].start.x,t[c[l].lineIndexes[$]].start.y,t[c[l].lineIndexes[$]].start.z),j=new Lt.Vector3(t[c[l].lineIndexes[$]].end.x,t[c[l].lineIndexes[$]].end.y,t[c[l].lineIndexes[$]].end.z),Z=Xt(C,_,Q,j);if(Z&&Z.isParallel){if(V+G>s){p=!0;break}V+=G;let U=ct(C,Q),it=ct(C,j),ht=ct(_,Q),St=ct(_,j);if(U)C=_.clone(),_=j.clone();else if(it)C=_.clone(),_=Q.clone();else if(ht)_=j.clone();else if(St)_=Q.clone();else continue;if(k=ct(C,y),F=ct(C,m),B=ct(_,y),O=ct(_,m),k||F||B||O){h=!0;break}}}break}if(p)break}if(p)break}if(p)break}h&&n.rectangles.push(c[l].uuids)}for(let l=0;l<t.length;l++){let h=t[l].length;if(o.has(t[l].uuid)||h<r||h>s)continue;let g={uuids:[],missingEdge:{start:new Lt.Vector3,end:new Lt.Vector3}},x=new Lt.Vector3(t[l].start.x,t[l].start.y,t[l].start.z),y=new Lt.Vector3(t[l].end.x,t[l].end.y,t[l].end.z),m=!0,p=!0;for(let z=0;z<t.length;z++){if(z==l)continue;let M=new Lt.Vector3(t[z].start.x,t[z].start.y,t[z].start.z),P=new Lt.Vector3(t[z].end.x,t[z].end.y,t[z].end.z),E=ct(M,x),S=ct(P,x),I=ct(M,y),W=ct(P,y);if(E||S?m=!1:(I||W)&&(p=!1),!m&&!p)break}if(!m&&!p)continue;let b=new Lt.Vector3;m||([x,y]=[y,x]),b=new Lt.Vector3().subVectors(y,x).normalize();let w=b.clone().negate();for(let z=0;z<t.length;z++){let M=t[z].length;if(z==l||o.has(z)||M<r||M>s)continue;let P=new Lt.Vector3(t[z].start.x,t[z].start.y,t[z].start.z),E=new Lt.Vector3(t[z].end.x,t[z].end.y,t[z].end.z),S=ie(x,y,P,E);if(!S||S.angle<85)continue;let I=ct(P,y),W=ct(E,y);if(!I&&!W)continue;let v=new Lt.Vector3,T=new Lt.Vector3;I?(v=P.clone(),T=E.clone()):(v=E.clone(),T=P.clone());for(let R=0;R<t.length;R++){let D=t[R].length;if(R==l||R==z||o.has(R)||D<r||D>s)continue;let H=new Lt.Vector3(t[R].start.x,t[R].start.y,t[R].start.z),V=new Lt.Vector3(t[R].end.x,t[R].end.y,t[R].end.z),C=ie(P,E,H,V);if(!C||C.angle<85)continue;let _=ct(H,T),A=ct(V,T);if(!_&&!A)continue;let L=new Lt.Vector3,k=new Lt.Vector3;_?(L=H.clone(),k=V.clone()):(L=V.clone(),k=H.clone());let F=new Lt.Vector3().subVectors(k,L).normalize(),B=$t(w,F,.1);if(!B||!B.parallel||!B.sameDirection)continue;let O=!0,$=!0;for(let Q=0;Q<t.length;Q++){if(Q==R)continue;let j=new Lt.Vector3(t[Q].start.x,t[Q].start.y,t[Q].start.z),Z=new Lt.Vector3(t[Q].end.x,t[Q].end.y,t[Q].end.z),U=ct(j,H),it=ct(Z,H),ht=ct(j,V),St=ct(Z,V);if(U||it?O=!1:(ht||St)&&($=!1),!O&&!$)break}if(!O&&!$)continue;let G=h*M;if(!(G<i||G>a)){g.uuids=[t[l].uuid,t[z].uuid,t[R].uuid],g.missingEdge.start=k,g.missingEdge.end=x;break}}if(g.uuids.length==3)break}g.uuids.length==3&&(g.uuids.forEach(z=>o.add(z)),n.threeEdgeRect.push(g))}return n};import*as q from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";var It=11102230246251565e-32,Yt=134217729,zn=(3+8*It)*It;function je(t,e,n,o,r){let s,i,a,u,d=e[0],f=o[0],c=0,l=0;f>d==f>-d?(s=d,d=e[++c]):(s=f,f=o[++l]);let h=0;if(c<t&&l<n)for(f>d==f>-d?(i=d+s,a=s-(i-d),d=e[++c]):(i=f+s,a=s-(i-f),f=o[++l]),s=i,a!==0&&(r[h++]=a);c<t&&l<n;)f>d==f>-d?(i=s+d,u=i-s,a=s-(i-u)+(d-u),d=e[++c]):(i=s+f,u=i-s,a=s-(i-u)+(f-u),f=o[++l]),s=i,a!==0&&(r[h++]=a);for(;c<t;)i=s+d,u=i-s,a=s-(i-u)+(d-u),d=e[++c],s=i,a!==0&&(r[h++]=a);for(;l<n;)i=s+f,u=i-s,a=s-(i-u)+(f-u),f=o[++l],s=i,a!==0&&(r[h++]=a);return(s!==0||h===0)&&(r[h++]=s),h}function Mn(t,e){let n=e[0];for(let o=1;o<t;o++)n+=e[o];return n}function K(t){return new Float64Array(t)}var Qi=(3+16*It)*It,Ji=(2+12*It)*It,tl=(9+64*It)*It*It,rn=K(4),Sr=K(8),Rr=K(12),Dr=K(16),oe=K(4);function el(t,e,n,o,r,s,i){let a,u,d,f,c,l,h,g,x,y,m,p,b,w,z,M,P,E,S=t-r,I=n-r,W=e-s,v=o-s;w=S*v,l=Yt*S,h=l-(l-S),g=S-h,l=Yt*v,x=l-(l-v),y=v-x,z=g*y-(w-h*x-g*x-h*y),M=W*I,l=Yt*W,h=l-(l-W),g=W-h,l=Yt*I,x=l-(l-I),y=I-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,rn[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,rn[1]=b-(m+c)+(c-M),E=p+m,c=E-p,rn[2]=p-(E-c)+(m-c),rn[3]=E;let T=Mn(4,rn),R=Ji*i;if(T>=R||-T>=R||(c=t-S,a=t-(S+c)+(c-r),c=n-I,d=n-(I+c)+(c-r),c=e-W,u=e-(W+c)+(c-s),c=o-v,f=o-(v+c)+(c-s),a===0&&u===0&&d===0&&f===0)||(R=tl*i+zn*Math.abs(T),T+=S*f+v*a-(W*d+I*u),T>=R||-T>=R))return T;w=a*v,l=Yt*a,h=l-(l-a),g=a-h,l=Yt*v,x=l-(l-v),y=v-x,z=g*y-(w-h*x-g*x-h*y),M=u*I,l=Yt*u,h=l-(l-u),g=u-h,l=Yt*I,x=l-(l-I),y=I-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,oe[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,oe[1]=b-(m+c)+(c-M),E=p+m,c=E-p,oe[2]=p-(E-c)+(m-c),oe[3]=E;let D=je(4,rn,4,oe,Sr);w=S*f,l=Yt*S,h=l-(l-S),g=S-h,l=Yt*f,x=l-(l-f),y=f-x,z=g*y-(w-h*x-g*x-h*y),M=W*d,l=Yt*W,h=l-(l-W),g=W-h,l=Yt*d,x=l-(l-d),y=d-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,oe[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,oe[1]=b-(m+c)+(c-M),E=p+m,c=E-p,oe[2]=p-(E-c)+(m-c),oe[3]=E;let H=je(D,Sr,4,oe,Rr);w=a*f,l=Yt*a,h=l-(l-a),g=a-h,l=Yt*f,x=l-(l-f),y=f-x,z=g*y-(w-h*x-g*x-h*y),M=u*d,l=Yt*u,h=l-(l-u),g=u-h,l=Yt*d,x=l-(l-d),y=d-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,oe[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,oe[1]=b-(m+c)+(c-M),E=p+m,c=E-p,oe[2]=p-(E-c)+(m-c),oe[3]=E;let V=je(H,Rr,4,oe,Dr);return Dr[V-1]}function sn(t,e,n,o,r,s){let i=(e-s)*(n-r),a=(t-r)*(o-s),u=i-a,d=Math.abs(i+a);return Math.abs(u)>=Qi*d?u:-el(t,e,n,o,r,s,d)}var Tu=(7+56*It)*It,Su=(3+28*It)*It,Ru=(26+288*It)*It*It,Du=K(4),vu=K(4),Vu=K(4),Iu=K(4),Cu=K(4),Hu=K(4),Wu=K(4),_u=K(4),Au=K(4),Bu=K(8),Lu=K(8),ku=K(8),Ou=K(4),Fu=K(8),Nu=K(8),qu=K(16),ju=K(12),$u=K(192),Xu=K(192);var Yu=(10+96*It)*It,Uu=(4+48*It)*It,Ku=(44+576*It)*It*It,Qu=K(4),Ju=K(4),tf=K(4),ef=K(4),nf=K(4),of=K(4),rf=K(4),sf=K(4),lf=K(8),af=K(8),cf=K(8),uf=K(8),ff=K(8),df=K(8),hf=K(8),mf=K(8),pf=K(8),gf=K(4),xf=K(4),yf=K(4),bf=K(8),wf=K(16),zf=K(16),Mf=K(16),Ef=K(32),Pf=K(32),Tf=K(48),Sf=K(64),Rf=K(1152),Df=K(1152);var Cf=(16+224*It)*It,Hf=(5+72*It)*It,Wf=(71+1408*It)*It*It,_f=K(4),Af=K(4),Bf=K(4),Lf=K(4),kf=K(4),Of=K(4),Ff=K(4),Nf=K(4),qf=K(4),jf=K(4),$f=K(24),Xf=K(24),Zf=K(24),Gf=K(24),Yf=K(24),Uf=K(24),Kf=K(24),Qf=K(24),Jf=K(24),td=K(24),ed=K(1152),nd=K(1152),od=K(1152),rd=K(1152),sd=K(1152),id=K(2304),ld=K(2304),ad=K(3456),cd=K(5760),ud=K(8),fd=K(8),dd=K(8),hd=K(16),md=K(24),pd=K(48),gd=K(48),xd=K(96),yd=K(192),bd=K(384),wd=K(384),zd=K(384),Md=K(768);var Ed=K(96),Pd=K(96),Td=K(96),Sd=K(1152);var Vr=Math.pow(2,-52),qn=new Uint32Array(512),an=class t{static from(e,n=ll,o=al){let r=e.length,s=new Float64Array(r*2);for(let i=0;i<r;i++){let a=e[i];s[2*i]=n(a),s[2*i+1]=o(a)}return new t(s)}constructor(e){let n=e.length>>1;if(n>0&&typeof e[0]!="number")throw new Error("Expected coords to contain numbers.");this.coords=e;let o=Math.max(2*n-5,0);this._triangles=new Uint32Array(o*3),this._halfedges=new Int32Array(o*3),this._hashSize=Math.ceil(Math.sqrt(n)),this._hullPrev=new Uint32Array(n),this._hullNext=new Uint32Array(n),this._hullTri=new Uint32Array(n),this._hullHash=new Int32Array(this._hashSize),this._ids=new Uint32Array(n),this._dists=new Float64Array(n),this.trianglesLen=0,this._cx=0,this._cy=0,this._hullStart=0,this.hull=this._triangles,this.triangles=this._triangles,this.halfedges=this._halfedges,this.update()}update(){let{coords:e,_hullPrev:n,_hullNext:o,_hullTri:r,_hullHash:s}=this,i=e.length>>1,a=1/0,u=1/0,d=-1/0,f=-1/0;for(let S=0;S<i;S++){let I=e[2*S],W=e[2*S+1];I<a&&(a=I),W<u&&(u=W),I>d&&(d=I),W>f&&(f=W),this._ids[S]=S}let c=(a+d)/2,l=(u+f)/2,h=0,g=0,x=0;for(let S=0,I=1/0;S<i;S++){let W=To(c,l,e[2*S],e[2*S+1]);W<I&&(h=S,I=W)}let y=e[2*h],m=e[2*h+1];for(let S=0,I=1/0;S<i;S++){if(S===h)continue;let W=To(y,m,e[2*S],e[2*S+1]);W<I&&W>0&&(g=S,I=W)}let p=e[2*g],b=e[2*g+1],w=1/0;for(let S=0;S<i;S++){if(S===h||S===g)continue;let I=sl(y,m,p,b,e[2*S],e[2*S+1]);I<w&&(x=S,w=I)}let z=e[2*x],M=e[2*x+1];if(w===1/0){for(let W=0;W<i;W++)this._dists[W]=e[2*W]-e[0]||e[2*W+1]-e[1];ln(this._ids,this._dists,0,i-1);let S=new Uint32Array(i),I=0;for(let W=0,v=-1/0;W<i;W++){let T=this._ids[W],R=this._dists[T];R>v&&(S[I++]=T,v=R)}this.hull=S.subarray(0,I),this.triangles=new Uint32Array(0),this.halfedges=new Int32Array(0);return}if(sn(y,m,p,b,z,M)<0){let S=g,I=p,W=b;g=x,p=z,b=M,x=S,z=I,M=W}let P=il(y,m,p,b,z,M);this._cx=P.x,this._cy=P.y;for(let S=0;S<i;S++)this._dists[S]=To(e[2*S],e[2*S+1],P.x,P.y);ln(this._ids,this._dists,0,i-1),this._hullStart=h;let E=3;o[h]=n[x]=g,o[g]=n[h]=x,o[x]=n[g]=h,r[h]=0,r[g]=1,r[x]=2,s.fill(-1),s[this._hashKey(y,m)]=h,s[this._hashKey(p,b)]=g,s[this._hashKey(z,M)]=x,this.trianglesLen=0,this._addTriangle(h,g,x,-1,-1,-1);for(let S=0,I=0,W=0;S<this._ids.length;S++){let v=this._ids[S],T=e[2*v],R=e[2*v+1];if(S>0&&Math.abs(T-I)<=Vr&&Math.abs(R-W)<=Vr||(I=T,W=R,v===h||v===g||v===x))continue;let D=0;for(let A=0,L=this._hashKey(T,R);A<this._hashSize&&(D=s[(L+A)%this._hashSize],!(D!==-1&&D!==o[D]));A++);D=n[D];let H=D,V;for(;V=o[H],sn(T,R,e[2*H],e[2*H+1],e[2*V],e[2*V+1])>=0;)if(H=V,H===D){H=-1;break}if(H===-1)continue;let C=this._addTriangle(H,v,o[H],-1,-1,r[H]);r[v]=this._legalize(C+2),r[H]=C,E++;let _=o[H];for(;V=o[_],sn(T,R,e[2*_],e[2*_+1],e[2*V],e[2*V+1])<0;)C=this._addTriangle(_,v,V,r[v],-1,r[_]),r[v]=this._legalize(C+2),o[_]=_,E--,_=V;if(H===D)for(;V=n[H],sn(T,R,e[2*V],e[2*V+1],e[2*H],e[2*H+1])<0;)C=this._addTriangle(V,v,H,-1,r[H],r[V]),this._legalize(C+2),r[V]=C,o[H]=H,E--,H=V;this._hullStart=n[v]=H,o[H]=n[_]=v,o[v]=_,s[this._hashKey(T,R)]=v,s[this._hashKey(e[2*H],e[2*H+1])]=H}this.hull=new Uint32Array(E);for(let S=0,I=this._hullStart;S<E;S++)this.hull[S]=I,I=o[I];this.triangles=this._triangles.subarray(0,this.trianglesLen),this.halfedges=this._halfedges.subarray(0,this.trianglesLen)}_hashKey(e,n){return Math.floor(ol(e-this._cx,n-this._cy)*this._hashSize)%this._hashSize}_legalize(e){let{_triangles:n,_halfedges:o,coords:r}=this,s=0,i=0;for(;;){let a=o[e],u=e-e%3;if(i=u+(e+2)%3,a===-1){if(s===0)break;e=qn[--s];continue}let d=a-a%3,f=u+(e+1)%3,c=d+(a+2)%3,l=n[i],h=n[e],g=n[f],x=n[c];if(rl(r[2*l],r[2*l+1],r[2*h],r[2*h+1],r[2*g],r[2*g+1],r[2*x],r[2*x+1])){n[e]=x,n[a]=l;let m=o[c];if(m===-1){let b=this._hullStart;do{if(this._hullTri[b]===c){this._hullTri[b]=e;break}b=this._hullPrev[b]}while(b!==this._hullStart)}this._link(e,m),this._link(a,o[i]),this._link(i,c);let p=d+(a+1)%3;s<qn.length&&(qn[s++]=p)}else{if(s===0)break;e=qn[--s]}}return i}_link(e,n){this._halfedges[e]=n,n!==-1&&(this._halfedges[n]=e)}_addTriangle(e,n,o,r,s,i){let a=this.trianglesLen;return this._triangles[a]=e,this._triangles[a+1]=n,this._triangles[a+2]=o,this._link(a,r),this._link(a+1,s),this._link(a+2,i),this.trianglesLen+=3,a}};function ol(t,e){let n=t/(Math.abs(t)+Math.abs(e));return(e>0?3-n:1+n)/4}function To(t,e,n,o){let r=t-n,s=e-o;return r*r+s*s}function rl(t,e,n,o,r,s,i,a){let u=t-i,d=e-a,f=n-i,c=o-a,l=r-i,h=s-a,g=u*u+d*d,x=f*f+c*c,y=l*l+h*h;return u*(c*y-x*h)-d*(f*y-x*l)+g*(f*h-c*l)<0}function sl(t,e,n,o,r,s){let i=n-t,a=o-e,u=r-t,d=s-e,f=i*i+a*a,c=u*u+d*d,l=.5/(i*d-a*u),h=(d*f-a*c)*l,g=(i*c-u*f)*l;return h*h+g*g}function il(t,e,n,o,r,s){let i=n-t,a=o-e,u=r-t,d=s-e,f=i*i+a*a,c=u*u+d*d,l=.5/(i*d-a*u),h=t+(d*f-a*c)*l,g=e+(i*c-u*f)*l;return{x:h,y:g}}function ln(t,e,n,o){if(o-n<=20)for(let r=n+1;r<=o;r++){let s=t[r],i=e[s],a=r-1;for(;a>=n&&e[t[a]]>i;)t[a+1]=t[a--];t[a+1]=s}else{let r=n+o>>1,s=n+1,i=o;En(t,r,s),e[t[n]]>e[t[o]]&&En(t,n,o),e[t[s]]>e[t[o]]&&En(t,s,o),e[t[n]]>e[t[s]]&&En(t,n,s);let a=t[s],u=e[a];for(;;){do s++;while(e[t[s]]<u);do i--;while(e[t[i]]>u);if(i<s)break;En(t,s,i)}t[n+1]=t[i],t[i]=a,o-s+1>=i-n?(ln(t,e,s,o),ln(t,e,n,i-1)):(ln(t,e,n,i-1),ln(t,e,s,o))}}function En(t,e,n){let o=t[e];t[e]=t[n],t[n]=o}function ll(t){return t[0]}function al(t){return t[1]}var fe=class{constructor(){this._x0=this._y0=this._x1=this._y1=null,this._=""}moveTo(e,n){this._+=`M${this._x0=this._x1=+e},${this._y0=this._y1=+n}`}closePath(){this._x1!==null&&(this._x1=this._x0,this._y1=this._y0,this._+="Z")}lineTo(e,n){this._+=`L${this._x1=+e},${this._y1=+n}`}arc(e,n,o){e=+e,n=+n,o=+o;let r=e+o,s=n;if(o<0)throw new Error("negative radius");this._x1===null?this._+=`M${r},${s}`:(Math.abs(this._x1-r)>1e-6||Math.abs(this._y1-s)>1e-6)&&(this._+="L"+r+","+s),o&&(this._+=`A${o},${o},0,1,1,${e-o},${n}A${o},${o},0,1,1,${this._x1=r},${this._y1=s}`)}rect(e,n,o,r){this._+=`M${this._x0=this._x1=+e},${this._y0=this._y1=+n}h${+o}v${+r}h${-o}Z`}value(){return this._||null}};var _e=class{constructor(){this._=[]}moveTo(e,n){this._.push([e,n])}closePath(){this._.push(this._[0].slice())}lineTo(e,n){this._.push([e,n])}value(){return this._.length?this._:null}};var Pn=class{constructor(e,[n,o,r,s]=[0,0,960,500]){if(!((r=+r)>=(n=+n))||!((s=+s)>=(o=+o)))throw new Error("invalid bounds");this.delaunay=e,this._circumcenters=new Float64Array(e.points.length*2),this.vectors=new Float64Array(e.points.length*2),this.xmax=r,this.xmin=n,this.ymax=s,this.ymin=o,this._init()}update(){return this.delaunay.update(),this._init(),this}_init(){let{delaunay:{points:e,hull:n,triangles:o},vectors:r}=this,s,i,a=this.circumcenters=this._circumcenters.subarray(0,o.length/3*2);for(let x=0,y=0,m=o.length,p,b;x<m;x+=3,y+=2){let w=o[x]*2,z=o[x+1]*2,M=o[x+2]*2,P=e[w],E=e[w+1],S=e[z],I=e[z+1],W=e[M],v=e[M+1],T=S-P,R=I-E,D=W-P,H=v-E,V=(T*H-R*D)*2;if(Math.abs(V)<1e-9){if(s===void 0){s=i=0;for(let _ of n)s+=e[_*2],i+=e[_*2+1];s/=n.length,i/=n.length}let C=1e9*Math.sign((s-P)*H-(i-E)*D);p=(P+W)/2-C*H,b=(E+v)/2+C*D}else{let C=1/V,_=T*T+R*R,A=D*D+H*H;p=P+(H*_-R*A)*C,b=E+(T*A-D*_)*C}a[y]=p,a[y+1]=b}let u=n[n.length-1],d,f=u*4,c,l=e[2*u],h,g=e[2*u+1];r.fill(0);for(let x=0;x<n.length;++x)u=n[x],d=f,c=l,h=g,f=u*4,l=e[2*u],g=e[2*u+1],r[d+2]=r[f]=h-g,r[d+3]=r[f+1]=l-c}render(e){let n=e==null?e=new fe:void 0,{delaunay:{halfedges:o,inedges:r,hull:s},circumcenters:i,vectors:a}=this;if(s.length<=1)return null;for(let f=0,c=o.length;f<c;++f){let l=o[f];if(l<f)continue;let h=Math.floor(f/3)*2,g=Math.floor(l/3)*2,x=i[h],y=i[h+1],m=i[g],p=i[g+1];this._renderSegment(x,y,m,p,e)}let u,d=s[s.length-1];for(let f=0;f<s.length;++f){u=d,d=s[f];let c=Math.floor(r[d]/3)*2,l=i[c],h=i[c+1],g=u*4,x=this._project(l,h,a[g+2],a[g+3]);x&&this._renderSegment(l,h,x[0],x[1],e)}return n&&n.value()}renderBounds(e){let n=e==null?e=new fe:void 0;return e.rect(this.xmin,this.ymin,this.xmax-this.xmin,this.ymax-this.ymin),n&&n.value()}renderCell(e,n){let o=n==null?n=new fe:void 0,r=this._clip(e);if(r===null||!r.length)return;n.moveTo(r[0],r[1]);let s=r.length;for(;r[0]===r[s-2]&&r[1]===r[s-1]&&s>1;)s-=2;for(let i=2;i<s;i+=2)(r[i]!==r[i-2]||r[i+1]!==r[i-1])&&n.lineTo(r[i],r[i+1]);return n.closePath(),o&&o.value()}*cellPolygons(){let{delaunay:{points:e}}=this;for(let n=0,o=e.length/2;n<o;++n){let r=this.cellPolygon(n);r&&(r.index=n,yield r)}}cellPolygon(e){let n=new _e;return this.renderCell(e,n),n.value()}_renderSegment(e,n,o,r,s){let i,a=this._regioncode(e,n),u=this._regioncode(o,r);a===0&&u===0?(s.moveTo(e,n),s.lineTo(o,r)):(i=this._clipSegment(e,n,o,r,a,u))&&(s.moveTo(i[0],i[1]),s.lineTo(i[2],i[3]))}contains(e,n,o){return n=+n,n!==n||(o=+o,o!==o)?!1:this.delaunay._step(e,n,o)===e}*neighbors(e){let n=this._clip(e);if(n)for(let o of this.delaunay.neighbors(e)){let r=this._clip(o);if(r){t:for(let s=0,i=n.length;s<i;s+=2)for(let a=0,u=r.length;a<u;a+=2)if(n[s]===r[a]&&n[s+1]===r[a+1]&&n[(s+2)%i]===r[(a+u-2)%u]&&n[(s+3)%i]===r[(a+u-1)%u]){yield o;break t}}}}_cell(e){let{circumcenters:n,delaunay:{inedges:o,halfedges:r,triangles:s}}=this,i=o[e];if(i===-1)return null;let a=[],u=i;do{let d=Math.floor(u/3);if(a.push(n[d*2],n[d*2+1]),u=u%3===2?u-2:u+1,s[u]!==e)break;u=r[u]}while(u!==i&&u!==-1);return a}_clip(e){if(e===0&&this.delaunay.hull.length===1)return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];let n=this._cell(e);if(n===null)return null;let{vectors:o}=this,r=e*4;return this._simplify(o[r]||o[r+1]?this._clipInfinite(e,n,o[r],o[r+1],o[r+2],o[r+3]):this._clipFinite(e,n))}_clipFinite(e,n){let o=n.length,r=null,s,i,a=n[o-2],u=n[o-1],d,f=this._regioncode(a,u),c,l=0;for(let h=0;h<o;h+=2)if(s=a,i=u,a=n[h],u=n[h+1],d=f,f=this._regioncode(a,u),d===0&&f===0)c=l,l=0,r?r.push(a,u):r=[a,u];else{let g,x,y,m,p;if(d===0){if((g=this._clipSegment(s,i,a,u,d,f))===null)continue;[x,y,m,p]=g}else{if((g=this._clipSegment(a,u,s,i,f,d))===null)continue;[m,p,x,y]=g,c=l,l=this._edgecode(x,y),c&&l&&this._edge(e,c,l,r,r.length),r?r.push(x,y):r=[x,y]}c=l,l=this._edgecode(m,p),c&&l&&this._edge(e,c,l,r,r.length),r?r.push(m,p):r=[m,p]}if(r)c=l,l=this._edgecode(r[0],r[1]),c&&l&&this._edge(e,c,l,r,r.length);else if(this.contains(e,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2))return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];return r}_clipSegment(e,n,o,r,s,i){let a=s<i;for(a&&([e,n,o,r,s,i]=[o,r,e,n,i,s]);;){if(s===0&&i===0)return a?[o,r,e,n]:[e,n,o,r];if(s&i)return null;let u,d,f=s||i;f&8?(u=e+(o-e)*(this.ymax-n)/(r-n),d=this.ymax):f&4?(u=e+(o-e)*(this.ymin-n)/(r-n),d=this.ymin):f&2?(d=n+(r-n)*(this.xmax-e)/(o-e),u=this.xmax):(d=n+(r-n)*(this.xmin-e)/(o-e),u=this.xmin),s?(e=u,n=d,s=this._regioncode(e,n)):(o=u,r=d,i=this._regioncode(o,r))}}_clipInfinite(e,n,o,r,s,i){let a=Array.from(n),u;if((u=this._project(a[0],a[1],o,r))&&a.unshift(u[0],u[1]),(u=this._project(a[a.length-2],a[a.length-1],s,i))&&a.push(u[0],u[1]),a=this._clipFinite(e,a))for(let d=0,f=a.length,c,l=this._edgecode(a[f-2],a[f-1]);d<f;d+=2)c=l,l=this._edgecode(a[d],a[d+1]),c&&l&&(d=this._edge(e,c,l,a,d),f=a.length);else this.contains(e,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2)&&(a=[this.xmin,this.ymin,this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax]);return a}_edge(e,n,o,r,s){for(;n!==o;){let i,a;switch(n){case 5:n=4;continue;case 4:n=6,i=this.xmax,a=this.ymin;break;case 6:n=2;continue;case 2:n=10,i=this.xmax,a=this.ymax;break;case 10:n=8;continue;case 8:n=9,i=this.xmin,a=this.ymax;break;case 9:n=1;continue;case 1:n=5,i=this.xmin,a=this.ymin;break}(r[s]!==i||r[s+1]!==a)&&this.contains(e,i,a)&&(r.splice(s,0,i,a),s+=2)}return s}_project(e,n,o,r){let s=1/0,i,a,u;if(r<0){if(n<=this.ymin)return null;(i=(this.ymin-n)/r)<s&&(u=this.ymin,a=e+(s=i)*o)}else if(r>0){if(n>=this.ymax)return null;(i=(this.ymax-n)/r)<s&&(u=this.ymax,a=e+(s=i)*o)}if(o>0){if(e>=this.xmax)return null;(i=(this.xmax-e)/o)<s&&(a=this.xmax,u=n+(s=i)*r)}else if(o<0){if(e<=this.xmin)return null;(i=(this.xmin-e)/o)<s&&(a=this.xmin,u=n+(s=i)*r)}return[a,u]}_edgecode(e,n){return(e===this.xmin?1:e===this.xmax?2:0)|(n===this.ymin?4:n===this.ymax?8:0)}_regioncode(e,n){return(e<this.xmin?1:e>this.xmax?2:0)|(n<this.ymin?4:n>this.ymax?8:0)}_simplify(e){if(e&&e.length>4){for(let n=0;n<e.length;n+=2){let o=(n+2)%e.length,r=(n+4)%e.length;(e[n]===e[o]&&e[o]===e[r]||e[n+1]===e[o+1]&&e[o+1]===e[r+1])&&(e.splice(o,2),n-=2)}e.length||(e=null)}return e}};var cl=2*Math.PI,cn=Math.pow;function ul(t){return t[0]}function fl(t){return t[1]}function dl(t){let{triangles:e,coords:n}=t;for(let o=0;o<e.length;o+=3){let r=2*e[o],s=2*e[o+1],i=2*e[o+2];if((n[i]-n[r])*(n[s+1]-n[r+1])-(n[s]-n[r])*(n[i+1]-n[r+1])>1e-10)return!1}return!0}function hl(t,e,n){return[t+Math.sin(t+e)*n,e+Math.cos(t-e)*n]}var Ae=class t{static from(e,n=ul,o=fl,r){return new t("length"in e?ml(e,n,o,r):Float64Array.from(pl(e,n,o,r)))}constructor(e){this._delaunator=new an(e),this.inedges=new Int32Array(e.length/2),this._hullIndex=new Int32Array(e.length/2),this.points=this._delaunator.coords,this._init()}update(){return this._delaunator.update(),this._init(),this}_init(){let e=this._delaunator,n=this.points;if(e.hull&&e.hull.length>2&&dl(e)){this.collinear=Int32Array.from({length:n.length/2},(l,h)=>h).sort((l,h)=>n[2*l]-n[2*h]||n[2*l+1]-n[2*h+1]);let u=this.collinear[0],d=this.collinear[this.collinear.length-1],f=[n[2*u],n[2*u+1],n[2*d],n[2*d+1]],c=1e-8*Math.hypot(f[3]-f[1],f[2]-f[0]);for(let l=0,h=n.length/2;l<h;++l){let g=hl(n[2*l],n[2*l+1],c);n[2*l]=g[0],n[2*l+1]=g[1]}this._delaunator=new an(n)}else delete this.collinear;let o=this.halfedges=this._delaunator.halfedges,r=this.hull=this._delaunator.hull,s=this.triangles=this._delaunator.triangles,i=this.inedges.fill(-1),a=this._hullIndex.fill(-1);for(let u=0,d=o.length;u<d;++u){let f=s[u%3===2?u-2:u+1];(o[u]===-1||i[f]===-1)&&(i[f]=u)}for(let u=0,d=r.length;u<d;++u)a[r[u]]=u;r.length<=2&&r.length>0&&(this.triangles=new Int32Array(3).fill(-1),this.halfedges=new Int32Array(3).fill(-1),this.triangles[0]=r[0],i[r[0]]=1,r.length===2&&(i[r[1]]=0,this.triangles[1]=r[1],this.triangles[2]=r[1]))}voronoi(e){return new Pn(this,e)}*neighbors(e){let{inedges:n,hull:o,_hullIndex:r,halfedges:s,triangles:i,collinear:a}=this;if(a){let c=a.indexOf(e);c>0&&(yield a[c-1]),c<a.length-1&&(yield a[c+1]);return}let u=n[e];if(u===-1)return;let d=u,f=-1;do{if(yield f=i[d],d=d%3===2?d-2:d+1,i[d]!==e)return;if(d=s[d],d===-1){let c=o[(r[e]+1)%o.length];c!==f&&(yield c);return}}while(d!==u)}find(e,n,o=0){if(e=+e,e!==e||(n=+n,n!==n))return-1;let r=o,s;for(;(s=this._step(o,e,n))>=0&&s!==o&&s!==r;)o=s;return s}_step(e,n,o){let{inedges:r,hull:s,_hullIndex:i,halfedges:a,triangles:u,points:d}=this;if(r[e]===-1||!d.length)return(e+1)%(d.length>>1);let f=e,c=cn(n-d[e*2],2)+cn(o-d[e*2+1],2),l=r[e],h=l;do{let g=u[h],x=cn(n-d[g*2],2)+cn(o-d[g*2+1],2);if(x<c&&(c=x,f=g),h=h%3===2?h-2:h+1,u[h]!==e)break;if(h=a[h],h===-1){if(h=s[(i[e]+1)%s.length],h!==g&&cn(n-d[h*2],2)+cn(o-d[h*2+1],2)<c)return h;break}}while(h!==l);return f}render(e){let n=e==null?e=new fe:void 0,{points:o,halfedges:r,triangles:s}=this;for(let i=0,a=r.length;i<a;++i){let u=r[i];if(u<i)continue;let d=s[i]*2,f=s[u]*2;e.moveTo(o[d],o[d+1]),e.lineTo(o[f],o[f+1])}return this.renderHull(e),n&&n.value()}renderPoints(e,n){n===void 0&&(!e||typeof e.moveTo!="function")&&(n=e,e=null),n=n==null?2:+n;let o=e==null?e=new fe:void 0,{points:r}=this;for(let s=0,i=r.length;s<i;s+=2){let a=r[s],u=r[s+1];e.moveTo(a+n,u),e.arc(a,u,n,0,cl)}return o&&o.value()}renderHull(e){let n=e==null?e=new fe:void 0,{hull:o,points:r}=this,s=o[0]*2,i=o.length;e.moveTo(r[s],r[s+1]);for(let a=1;a<i;++a){let u=2*o[a];e.lineTo(r[u],r[u+1])}return e.closePath(),n&&n.value()}hullPolygon(){let e=new _e;return this.renderHull(e),e.value()}renderTriangle(e,n){let o=n==null?n=new fe:void 0,{points:r,triangles:s}=this,i=s[e*=3]*2,a=s[e+1]*2,u=s[e+2]*2;return n.moveTo(r[i],r[i+1]),n.lineTo(r[a],r[a+1]),n.lineTo(r[u],r[u+1]),n.closePath(),o&&o.value()}*trianglePolygons(){let{triangles:e}=this;for(let n=0,o=e.length/3;n<o;++n)yield this.trianglePolygon(n)}trianglePolygon(e){let n=new _e;return this.renderTriangle(e,n),n.value()}};function ml(t,e,n,o){let r=t.length,s=new Float64Array(r*2);for(let i=0;i<r;++i){let a=t[i];s[i*2]=e.call(o,a,i,t),s[i*2+1]=n.call(o,a,i,t)}return s}function*pl(t,e,n,o){let r=0;for(let s of t)yield e.call(o,s,r,t),yield n.call(o,s,r,t),++r}function So(t,e,n,o){let r,s;n<o?(r=n,s=o):(r=o,s=n);let i=r*e+s;t.set(i,(t.get(i)||0)+1)}function Ir(t,e,n){let o=t.get(e);o===void 0&&(o=[],t.set(e,o)),o.push(n)}function de(t){if(!t||t.length<3)return[];let e=new Set,n=[];for(let x=0;x<t.length;x++){let y=t[x],m=`${y[0]}_${y[1]}`;e.has(m)||(e.add(m),n.push(y))}let o=n.length;if(o<3)return[];let r=new Float64Array(o*2);for(let x=0,y=0;x<o;x++){let m=n[x];r[y++]=m[0],r[y++]=m[1]}let s=new Ae(r),{triangles:i}=s;if(!i.length)return[];let a=new Map;for(let x=0;x<i.length;x+=3){let y=i[x],m=i[x+1],p=i[x+2],b=y*2,w=m*2,z=p*2,M=r[b],P=r[b+1],E=r[w],S=r[w+1],I=r[z],W=r[z+1],v=M-E,T=P-S,R=M-I,D=P-W,H=E-I,V=S-W,C=H*H+V*V,_=R*R+D*D,A=v*v+T*T,L=(E-M)*(W-P)-(I-M)*(S-P);if(L===0)continue;let k=L*L,B=M===0||E===0||I===0?k:4*k;C*_*A<=B&&(So(a,o,y,m),So(a,o,m,p),So(a,o,p,y))}if(!a.size)return[];let u=new Map,d=0,f=-1;for(let[x,y]of a){if(y!==1)continue;let m=Math.floor(x/o),p=x-m*o;f===-1&&(f=m),Ir(u,m,p),Ir(u,p,m),d++}if(d===0)return[];let c=[],l=new Uint8Array(o),h=f,g=-1;do{c.push(n[h]),l[h]=1;let x=u.get(h)||[],y=-1;for(let m=0;m<x.length;m++){let p=x[m];if(p!==g&&!l[p]){y=p;break}}if(y===-1)for(let m=0;m<x.length;m++){let p=x[m];if(p!==g){y=p;break}}if(y===-1)break;g=h,h=y}while(h!==f&&c.length<=d+1);return c.length>0&&c.push(c[0]),c}import*as qt from"three";import{OBB as Wl}from"three/examples/jsm/math/OBB.js";var Ut=63710088e-1,Hr={centimeters:Ut*100,centimetres:Ut*100,degrees:360/(2*Math.PI),feet:Ut*3.28084,inches:Ut*39.37,kilometers:Ut/1e3,kilometres:Ut/1e3,meters:Ut,metres:Ut,miles:Ut/1609.344,millimeters:Ut*1e3,millimetres:Ut*1e3,nauticalmiles:Ut/1852,radians:1,yards:Ut*1.0936};function Wr(t,e,n={}){let o={type:"Feature"};return(n.id===0||n.id)&&(o.id=n.id),n.bbox&&(o.bbox=n.bbox),o.properties=e||{},o.geometry=t,o}function jn(t,e,n={}){if(!t)throw new Error("coordinates is required");if(!Array.isArray(t))throw new Error("coordinates must be an Array");if(t.length<2)throw new Error("coordinates must be at least 2 numbers long");if(!Cr(t[0])||!Cr(t[1]))throw new Error("coordinates must contain numbers");return Wr({type:"Point",coordinates:t},e,n)}function $n(t,e,n={}){for(let r of t){if(r.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");if(r[r.length-1].length!==r[0].length)throw new Error("First and last Position are not equivalent.");for(let s=0;s<r[r.length-1].length;s++)if(r[r.length-1][s]!==r[0][s])throw new Error("First and last Position are not equivalent.")}return Wr({type:"Polygon",coordinates:t},e,n)}function gl(t,e="kilometers"){let n=Hr[e];if(!n)throw new Error(e+" units is invalid");return t*n}function xl(t,e="kilometers"){let n=Hr[e];if(!n)throw new Error(e+" units is invalid");return t/n}function Xn(t){return t%(2*Math.PI)*180/Math.PI}function he(t){return t%360*Math.PI/180}function Zn(t,e="kilometers",n="kilometers"){if(!(t>=0))throw new Error("length must be a positive number");return gl(xl(t,e),n)}function Cr(t){return!isNaN(t)&&t!==null&&!Array.isArray(t)}function _r(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}function Pe(t,e,n){if(t!==null)for(var o,r,s,i,a,u,d,f=0,c=0,l,h=t.type,g=h==="FeatureCollection",x=h==="Feature",y=g?t.features.length:1,m=0;m<y;m++){d=g?t.features[m].geometry:x?t.geometry:t,l=d?d.type==="GeometryCollection":!1,a=l?d.geometries.length:1;for(var p=0;p<a;p++){var b=0,w=0;if(i=l?d.geometries[p]:d,i!==null){u=i.coordinates;var z=i.type;switch(f=n&&(z==="Polygon"||z==="MultiPolygon")?1:0,z){case null:break;case"Point":if(e(u,c,m,b,w)===!1)return!1;c++,b++;break;case"LineString":case"MultiPoint":for(o=0;o<u.length;o++){if(e(u[o],c,m,b,w)===!1)return!1;c++,z==="MultiPoint"&&b++}z==="LineString"&&b++;break;case"Polygon":case"MultiLineString":for(o=0;o<u.length;o++){for(r=0;r<u[o].length-f;r++){if(e(u[o][r],c,m,b,w)===!1)return!1;c++}z==="MultiLineString"&&b++,z==="Polygon"&&w++}z==="Polygon"&&b++;break;case"MultiPolygon":for(o=0;o<u.length;o++){for(w=0,r=0;r<u[o].length;r++){for(s=0;s<u[o][r].length-f;s++){if(e(u[o][r][s],c,m,b,w)===!1)return!1;c++}w++}b++}break;case"GeometryCollection":for(o=0;o<i.geometries.length;o++)if(Pe(i.geometries[o],e,n)===!1)return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}function Ar(t){var e=[];return Pe(t,function(n){e.push(n)}),e}function yl(t,e){var n,o,r,s,i,a,u,d,f,c,l=0,h=t.type==="FeatureCollection",g=t.type==="Feature",x=h?t.features.length:1;for(n=0;n<x;n++){for(a=h?t.features[n].geometry:g?t.geometry:t,d=h?t.features[n].properties:g?t.properties:{},f=h?t.features[n].bbox:g?t.bbox:void 0,c=h?t.features[n].id:g?t.id:void 0,u=a?a.type==="GeometryCollection":!1,i=u?a.geometries.length:1,r=0;r<i;r++){if(s=u?a.geometries[r]:a,s===null){if(e(null,l,d,f,c)===!1)return!1;continue}switch(s.type){case"Point":case"LineString":case"MultiPoint":case"Polygon":case"MultiLineString":case"MultiPolygon":{if(e(s,l,d,f,c)===!1)return!1;break}case"GeometryCollection":{for(o=0;o<s.geometries.length;o++)if(e(s.geometries[o],l,d,f,c)===!1)return!1;break}default:throw new Error("Unknown Geometry Type")}}l++}}function Br(t,e,n){var o=n;return yl(t,function(r,s,i,a,u){s===0&&n===void 0?o=r:o=e(o,r,s,i,a,u)}),o}var es=Ei(ts(),1);function ns(t,e={}){e.concavity=e.concavity||1/0;let n=[];if(Pe(t,r=>{n.push([r[0],r[1]])}),!n.length)return null;let o=(0,es.default)(n,e.concavity);return o.length>3?$n([o]):null}function Qn(t,e={}){let n=0,o=0,r=0;return Pe(t,function(s){n+=s[0],o+=s[1],r++},!0),jn([n/r,o/r],e.properties)}function ce(t){if(!t)throw new Error("coord is required");if(!Array.isArray(t)){if(t.type==="Feature"&&t.geometry!==null&&t.geometry.type==="Point")return[...t.geometry.coordinates];if(t.type==="Point")return[...t.coordinates]}if(Array.isArray(t)&&t.length>=2&&!Array.isArray(t[0])&&!Array.isArray(t[1]))return[...t];throw new Error("coord must be GeoJSON Point or an Array of numbers")}function os(t){if(Array.isArray(t))return t;if(t.type==="Feature"){if(t.geometry!==null)return t.geometry.coordinates}else if(t.coordinates)return t.coordinates;throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array")}function ss(t,e,n={}){let o;return n.final?o=rs(ce(e),ce(t)):o=rs(ce(t),ce(e)),o>180?-(360-o):o}function rs(t,e){let n=he(t[1]),o=he(e[1]),r=he(e[0]-t[0]);r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI);let s=Math.log(Math.tan(o/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),i=Math.atan2(r,s);return(Xn(i)+360)%360}function is(t,e,n={}){let o=ce(t),r=ce(e);r[0]+=r[0]-o[0]>180?-360:o[0]-r[0]>180?360:0;let s=Dl(o,r);return Zn(s,"meters",n.units)}function Dl(t,e,n){n=n===void 0?Ut:Number(n);let o=n,r=t[1]*Math.PI/180,s=e[1]*Math.PI/180,i=s-r,a=Math.abs(e[0]-t[0])*Math.PI/180;a>Math.PI&&(a-=2*Math.PI);let u=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),d=Math.abs(u)>1e-11?i/u:Math.cos(r);return Math.sqrt(i*i+d*d*a*a)*o}function ls(t,e,n,o={}){let r=e<0,s=Zn(Math.abs(e),o.units,"meters");r&&(s=-Math.abs(s));let i=ce(t),a=vl(i,s,n);return a[0]+=a[0]-i[0]>180?-360:i[0]-a[0]>180?360:0,jn(a,o.properties)}function vl(t,e,n,o){o=o===void 0?Ut:Number(o);let r=e/o,s=t[0]*Math.PI/180,i=he(t[1]),a=he(n),u=r*Math.cos(a),d=i+u;Math.abs(d)>Math.PI/2&&(d=d>0?Math.PI-d:-Math.PI-d);let f=Math.log(Math.tan(d/2+Math.PI/4)/Math.tan(i/2+Math.PI/4)),c=Math.abs(f)>1e-11?u/f:Math.cos(i),l=r*Math.sin(a)/c;return[((s+l)*180/Math.PI+540)%360-180,d*180/Math.PI]}function as(t){if(!t)throw new Error("geojson is required");switch(t.type){case"Feature":return cs(t);case"FeatureCollection":return Vl(t);case"Point":case"LineString":case"Polygon":case"MultiPoint":case"MultiLineString":case"MultiPolygon":case"GeometryCollection":return _o(t);default:throw new Error("unknown GeoJSON type")}}function cs(t){let e={type:"Feature"};return Object.keys(t).forEach(n=>{switch(n){case"type":case"properties":case"geometry":return;default:e[n]=t[n]}}),e.properties=us(t.properties),t.geometry==null?e.geometry=null:e.geometry=_o(t.geometry),e}function us(t){let e={};return t&&Object.keys(t).forEach(n=>{let o=t[n];typeof o=="object"?o===null?e[n]=null:Array.isArray(o)?e[n]=o.map(r=>r):e[n]=us(o):e[n]=o}),e}function Vl(t){let e={type:"FeatureCollection"};return Object.keys(t).forEach(n=>{switch(n){case"type":case"features":return;default:e[n]=t[n]}}),e.features=t.features.map(n=>cs(n)),e}function _o(t){let e={type:t.type};return t.bbox&&(e.bbox=t.bbox),t.type==="GeometryCollection"?(e.geometries=t.geometries.map(n=>_o(n)),e):(e.coordinates=fs(t.coordinates),e)}function fs(t){let e=t;return typeof e[0]!="object"?e.slice():e.map(n=>fs(n))}function Ao(t,e,n){if(n=n||{},!_r(n))throw new Error("options is invalid");let o=n.pivot,r=n.mutate;if(!t)throw new Error("geojson is required");if(e==null||isNaN(e))throw new Error("angle is required");if(e===0)return t;let s=o??Qn(t);return(r===!1||r===void 0)&&(t=as(t)),Pe(t,function(i){let u=ss(s,i)+e,d=is(s,i),f=os(ls(s,d,u));i[0]=f[0],i[1]=f[1]}),t}function Bo(t,e,n={}){if(n.final===!0)return Il(t,e);let o=ce(t),r=ce(e),s=he(o[0]),i=he(r[0]),a=he(o[1]),u=he(r[1]),d=Math.sin(i-s)*Math.cos(u),f=Math.cos(a)*Math.sin(u)-Math.sin(a)*Math.cos(u)*Math.cos(i-s);return Xn(Math.atan2(d,f))}function Il(t,e){let n=Bo(e,t);return n=(n+180)%360,n}function ds(t,e={}){if(t.bbox!=null&&e.recompute!==!0)return t.bbox;let n=[1/0,1/0,-1/0,-1/0];return Pe(t,o=>{n[0]>o[0]&&(n[0]=o[0]),n[1]>o[1]&&(n[1]=o[1]),n[2]<o[0]&&(n[2]=o[0]),n[3]<o[1]&&(n[3]=o[1])}),n}function hs(t,e={}){let n=Number(t[0]),o=Number(t[1]),r=Number(t[2]),s=Number(t[3]);if(t.length===6)throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");let i=[n,o];return $n([[i,[r,o],[r,s],[n,s],i]],e.properties,{bbox:t,id:e.id})}function ms(t){return hs(ds(t))}function xs(t){return Br(t,(e,n)=>e+Cl(n),0)}function Cl(t){let e=0,n;switch(t.type){case"Polygon":return ps(t.coordinates);case"MultiPolygon":for(n=0;n<t.coordinates.length;n++)e+=ps(t.coordinates[n]);return e;case"Point":case"MultiPoint":case"LineString":case"MultiLineString":return 0}return 0}function ps(t){let e=0;if(t&&t.length>0){e+=Math.abs(gs(t[0]));for(let n=1;n<t.length;n++)e-=Math.abs(gs(t[n]))}return e}var Hl=Ut*Ut/2,Lo=Math.PI/180;function gs(t){let e=t.length-1;if(e<=2)return 0;let n=0,o=0;for(;o<e;){let r=t[o],s=t[o+1===e?0:o+1],i=t[o+2>=e?(o+2)%e:o+2],a=r[0]*Lo,u=s[1]*Lo,d=i[0]*Lo;n+=(d-a)*Math.sin(u),o++}return n*Hl}function ys(t){let e=ns(t);if(!e)throw new Error("Can't calculate smallestSurroundingRectangleByArea for given geometry");let n=Qn(e),o=Ar(e),r=Number.MAX_SAFE_INTEGER,s=null;for(let i=0;i<o.length-1;i++){let a=Bo(o[i],o[i+1]),u=Ao(e,-1*a,{pivot:n}),d=ms(u),f=xs(d);f<r&&(r=f,s=Ao(d,a,{pivot:n}))}return s}function bs(t,e){let n=e[0]-t[0],o=e[1]-t[1];return Math.sqrt(n*n+o*o)}function _l(t){let e=1/0,n=-1/0,o=1/0,r=-1/0;for(let[s,i]of t)s<e&&(e=s),s>n&&(n=s),i<o&&(o=i),i>r&&(r=i);return[(n+e)*.5,(r+o)*.5]}var to=class t extends Wl{getBoxMesh(){Be.identity(),Be.setFromMatrix3(this.rotation),Be.decompose(Jn,$e,new qt.Vector3);let e=this.halfSize.clone().multiplyScalar(2),n=new qt.Mesh(new qt.BoxGeometry(e.x,e.y,e.z));return n.position.copy(this.center),n.rotation.setFromQuaternion($e),n}getBoxEdge(e=16776960){let n=this.getBoxMesh();Bl.setFromObject(n);let o=new qt.EdgesGeometry(n.geometry.clone().applyMatrix4(n.matrixWorld));return new qt.LineSegments(o,new qt.LineBasicMaterial({color:e,toneMapped:!1}))}toJson(){return Be.identity(),Be.setFromMatrix3(this.rotation),Be.decompose(Jn,$e,new qt.Vector3),{size:this.halfSize.toArray(),center:this.center.toArray(),quaternion:$e.toArray()}}static from(e,n,o,r=new t){if(o instanceof qt.Euler)$e.setFromEuler(o);else if(o instanceof qt.Quaternion)$e.copy(o);else throw new Error("\u4F20\u5165\u7684\u65CB\u8F6C\u4E0D\u662F\u6B27\u62C9\u89D2\u6216\u8005\u56DB\u5143\u6570");return Be.compose(n,$e,Al),r.center.copy(n),r.halfSize.copy(e.multiplyScalar(.5)),r.rotation.setFromMatrix4(Be),r}static fromByPath2D(e,n,o,r=new t){let s=ys({type:"Polygon",coordinates:[e.map(h=>[h.x,h.y])]});if(!s)throw new Error("2d obb \u83B7\u53D6\u5931\u8D25");let i=s.geometry.coordinates[0],a=bs(i[0],i[1]),u=bs(i[1],i[2]),d=Math.atan2(i[1][1]-i[0][1],i[1][0]-i[0][0]);Jn.set(a,u,o),ws.set(0,0,d);let[f,c]=_l(i);return n.x=f,n.y=c,this.from(Jn,n,ws,r)}},Be=new qt.Matrix4,$e=new qt.Quaternion,ws=new qt.Euler,Al=new qt.Vector3(1,1,1),Jn=new qt.Vector3,Bl=new qt.Box3;import"three";function Ll(t,e,n=.005,o=.2){if(!t.length||!e.length)return 0;let r=Math.max(n,.03),s=n*n,i=new Map,a=(c,l,h)=>`${Math.floor(c/r)},${Math.floor(l/r)},${Math.floor(h/r)}`;for(let c of e){let l=a(c.x,c.y,c.z);i.has(l)||i.set(l,[]),i.get(l).push(c)}let u=0,d=Math.ceil(n/r)+1,f=[];for(let c=-d;c<=d;c++)for(let l=-d;l<=d;l++)for(let h=-d;h<=d;h++)c*c+l*l+h*h<=d*d&&f.push([c,l,h]);for(let c of t){let l=Math.floor(c.x/r),h=Math.floor(c.y/r),g=Math.floor(c.z/r),x=!1;for(let[y,m,p]of f){let b=`${l+y},${h+m},${g+p}`;if(i.has(b)){for(let w of i.get(b))if(c.distanceToSquared(w)<s){x=!0;break}if(x)break}}if(x&&(u++,u/t.length>o))return u/t.length}return u/t.length}function Ie(t,e,n=.5){let o=0,r=0;for(let s of t)if(e.containsPoint(s)&&(o++,r=o/t.length,r>n))break;return r}function ko(t,e,n,o){if(t.intersectsOBB(e))return!0;{let s=Qt(t,o),i=Qt(e,n);if(s||i)return!0}return!1}function Oo(t,e){let n=new q.Vector3,o=new q.Vector3;t.getSize(n),e.getSize(o);let r=n.x*n.y*n.z,s=o.x*o.y*o.z;return r>s?1:r<s?-1:0}function Qt(t,e){let n=!0;for(let o=0;o<e.length;o++)if(!t.containsPoint(e[o])){n=!1;break}return n}function ue(t){if(t.length===0)return{x:0,y:0,z:0,h:0};let e=0,n=0,o=-1/0,r=1/0;for(let s of t)e+=s.x,n+=s.y,o=Math.max(o,s.z),r=Math.min(r,s.z);return{x:e/t.length,y:n/t.length,z:0,maxz:o,minz:r,h:o-r}}function we(t,e,n,o){let r=[];for(let i=0;i<t.length;i++)r.push(new q.Vector3(t[i][0],t[i][1],0));if(r.length==0)return null;let s=to.fromByPath2D(r,e,n);return s.center.z=o,s}var kl=(t,e)=>{let n=new q.Group,o=new q.MeshStandardMaterial({color:16737792,side:q.DoubleSide});for(let r=0;r<t.length;r++){let s=[];for(let x=0;x<t[r].path.length;x++)s.push(new q.Vector3(t[r].path[x][0],t[r].path[x][1],0));if(s.length==0)continue;n.add(t[r].obb.getBoxEdge());let i=new q.BufferGeometry;i.setFromPoints(s);let a="green",u=new q.LineBasicMaterial({color:a,depthTest:!1,depthWrite:!1}),d=new q.Line(i,u);d.position.z=t[r].maxz,n.add(d);let f=t[r].obb.center.clone(),c=pr(t[r].type,f),l=t[r].stairBottomPt1.clone(),h=t[r].stairBottomPt2.clone();for(let x=0;x<t[r].stairStepNum;x++){let y=[l,h];y.push(h.clone().addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth)),y.push(l.clone().addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth));let m=Eo(y,t[r].stairStepHeight),p=new q.Mesh(m,o);n.add(p),l=l.addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth),l=l.addScaledVector(new q.Vector3(0,0,1),t[r].stairStepHeight),h=h.addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth),h=h.addScaledVector(new q.Vector3(0,0,1),t[r].stairStepHeight)}if(t[r].rectPt1.x!=0&&t[r].rectPt1.y!=0){let x=[t[r].stairBottomPt1,t[r].stairBottomPt2,t[r].rectPt2,t[r].rectPt1],y=Eo(x,t[r].stairStepHeight),m=new q.Mesh(y,o);n.add(m)}let g=new q.MeshBasicMaterial({color:16776960});for(let x=0;x<t[r].stairBottom1BalconyRailing.length;x++){let y=new q.CylinderGeometry(.03,.03,t[r].balconyRailingHeight,32),m=new q.Mesh(y,g);m.position.set(t[r].stairBottom1BalconyRailing[x].x,t[r].stairBottom1BalconyRailing[x].z+t[r].balconyRailingHeight/2,-t[r].stairBottom1BalconyRailing[x].y),e.add(m)}for(let x=0;x<t[r].stairBottom2BalconyRailing.length;x++){let y=new q.CylinderGeometry(.03,.03,t[r].balconyRailingHeight,32),m=new q.Mesh(y,g);m.position.set(t[r].stairBottom2BalconyRailing[x].x,t[r].stairBottom2BalconyRailing[x].z+t[r].balconyRailingHeight/2,-t[r].stairBottom2BalconyRailing[x].y),e.add(m)}}return n},Ol=(t,e,n=null)=>{if(t.length==0)return[];e||(e=t[0].averagePz);let o=[],r=new Map;for(let i=0;i<t.length;i++){let a=t[i].points.map(p=>[p.x,p.y]),u=t[i].obj_id;u=u.replace(/calcony railing/g,"balcony railing").replace(/glass balcony railing/g,"balcony railing").replace(/metal balcony railing/g,"balcony railing").replace(/temporary construction guardrail/g,"balcony railing").replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor").replace(/roof/g,"Ceiling");let d=u.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],f=u.indexOf("_")+1,c=u.lastIndexOf("_"),l=u.substring(f,c);if(l.includes("top")&&(f=l.indexOf("top")+4,l=l.substring(f,c)),l.includes("left")&&(f=l.indexOf("left")+5,l=l.substring(f,c)),l.includes("right")&&(f=l.indexOf("right")+6,l=l.substring(f,c)),l.includes("front")&&(f=l.indexOf("front")+6,l=l.substring(f,c)),l==="wall"||l==="stair guardrail")continue;let h=[];for(let p=0;p<t[i].points.length;p++)h.push(new q.Vector3(t[i].points[p].x,t[i].points[p].y,t[i].points[p].z));let g=-99999;if(l==="floor"){let p=new Map;for(let w=0;w<t[i].points.length;w++){let z=t[i].points[w].z;if(z=Math.floor(z*10)/10,p.has(z)){let M=p.get(z);M.num+=1,M.points.push(t[i].points[w]),p.set(z,M)}else p.set(z,{num:1,points:[t[i].points[w]]})}let b=[...p.entries()].reduce((w,z)=>z[1].num>w[1].num?z:w);a=[],a=b[1].points.map(w=>[w.x,w.y]),h=[],h=b[1].points.map(w=>new q.Vector3(w.x,w.y,w.z)),g=b[0]}let x=ue(h),y={points:h,path:de(a),h:x.h,minz:x.minz,maxz:x.maxz,center:{x:x.x,y:x.y,z:x.z},obj_id:t[i].obj_id,type:l,averagePz:e,obb:null,isDel:!1,obj_id:u,positionArr:[t[i].position],isStaircaseBalconyRailing:!1};if(y.obb=we(y.path,y.center,y.h,(y.maxz+y.minz)/2),y.obb==null)continue;let m=-1;for(let p=0;p<o.length;p++)if(o[p].type==l){m=p;break}if(m!=-1?o[m].array.push(y):o.push({type:l,array:[y]}),l=="floor"&&g!=-99999)if(r.has(g)){let p=r.get(g);p.num+=y.points.length,p.datas.push(y),r.set(g,p)}else r.set(g,{num:y.points.length,datas:[y]})}if(r.size>0){let i=[];for(let[a,u]of r.entries()){let{num:d,datas:f}=u;d>3e3&&i.push(...f)}for(let a=0;a<o.length;a++)if(o[a].type=="floor"){o[a].array=i;break}}for(let i=0;i<o.length;i++){let a=o[i].array,u=new Set,d=[];for(let c=0;c<a.length;c++){if(a[c].obb==null||u.has(c))continue;let l={...a[c],obb:a[c].obb.clone()},h=0,g=0;if(l.type=="pipe"){let y=Ve(l.points);h=y.minZ,g=y.maxZ}let x=!0;h!=0&&g!=0&&Math.abs(g-h)<.3&&(x=!1);for(let y=c+1;y<a.length;y++){if(a[y].obb==null||u.has(y))continue;let m=0,p=0;if(a[y].type=="pipe"){let z=Ve(a[y].points);m=z.minZ,p=z.maxZ}let b=!0;if(m!=0&&p!=0&&Math.abs(p-m)<.3&&(b=!1),l.type=="pipe"&&a[y].type=="pipe"&&x!=b)continue;let w=!1;if(l.type=="pipe"&&!x?w=Ie(a[y].points,l.obb)>.3:(w=l.obb.intersectsOBB(a[y].obb,.01),w||(w=Gl(l.obb,a[y].obb))),w){u.add(y);for(let P=0;P<a[y].points.length;P++)(a[y].points[P].z>e&&a[y].points[P].z-e>.1||l.type=="floor"||l.type=="Staircase")&&l.points.push(a[y].points[P]);let z=l.points.map(P=>[P.x,P.y]);l.type=="floor"&&(z=Kl(z,.1)),l.path=de(z);let M=ue(l.points);l.h=M.h,l.minz=M.minz,l.maxz=M.maxz,l.center={x:M.x,y:M.y,z:M.z},l.obb=we(l.path,l.center,l.h,(l.maxz+l.minz)/2),l.positionArr.push(...a[y].positionArr),y=c}}l.path.length>a[c].path.length&&(u.add(c),d.push(l))}let f=[];for(let c=0;c<a.length;c++)u.has(c)||f.push(a[c]);f.push(...d),o[i].array=f}for(let i=0;i<o.length;i++){let a=o[i].type;if(a==="people"||a==="floor")continue;let u=o[i].array;for(let d=0;d<u.length;d++){let f=u[d].obb,c=u[d].minz,l=u[d].maxz,h=u[d].averagePz;if(a==="window"||a==="door"||a==="tv"||a==="chair"||a==="cabinet"||a==="table"){let g=new q.Vector3;f.getSize(g);let x=[g.x,g.y,g.z],y=0;for(let m of x)if(m<.2&&(y++,y===2)){u[d].isDel=!0;break}}if(!u[d].isDel)for(let g=i+1;g<o.length;g++){let x=o[g].type;if(x==="people")continue;let y=o[g].array;for(let m=0;m<y.length;m++){if(y[m].isDel)continue;let p=y[m].obb,b=y[m].minz,w=y[m].maxz;if(f.intersectsOBB(p)){let z=Ie(y[m].points,f),M=Ie(u[d].points,p),P=Oo(f,p);if(M>=.5&&P==-1&&a!=="Staircase"&&x!=="Staircase"){u[d].isDel=!0;break}if(a!=="wall"&&a!=="floor"&&a!=="roof"){if(a==="window"){if(x==="balcony railing"&&Qt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="balcony railing"&&Qt(p,u[d].points)){u[d].isDel=!0;break}if(x==="balcony railing"&&ko(f,p,u[d].points,y[m].points)&&(Math.abs(c-b)<.1||Math.abs(b-h)>.3)){y[m].isDel=!0;continue}if(x==="door"&&Qt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Qt(p,u[d].points)){y[m].isDel=!0;continue}if(x==="window sill")continue}else if(a==="balcony railing"){if(x==="window"&&Qt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="window"&&Qt(p,u[d].points)){u[d].isDel=!0;break}if(x==="window"&&ko(f,p,u[d].points,y[m].points)&&(Math.abs(c-b)<.1||Math.abs(c-h)>.3)){u[d].isDel=!0;break}if(x==="Staircase"&&Qt(f,y[m].points)){u[d].isStaircaseBalconyRailing=!0;break}if(x==="Staircase"&&Qt(p,u[d].points)){u[d].isStaircaseBalconyRailing=!0;break}if(x==="Staircase"&&f.intersectsOBB(p)){u[d].isStaircaseBalconyRailing=!0;break}}else if(a==="door"){if(x==="window"&&Qt(f,y[m].points)){u[d].isDel=!0;break}if(x==="window"&&Qt(p,u[d].points)){u[d].isDel=!0;break}if(x==="window sill"&&Qt(f,y[m].points)){u[d].isDel=!0;break}if(x==="window sill"&&Qt(p,u[d].points)){u[d].isDel=!0;break}}else if(a==="window sill"){if(x==="window")continue;if(x==="floor"&&Qt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Qt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Qt(p,u[d].points)){y[m].isDel=!0;continue}}else if(a==="Staircase"){if(x==="balcony railing"&&Qt(f,y[m].points)){y[m].isStaircaseBalconyRailing=!0;continue}if(x==="balcony railing"&&Qt(p,u[d].points)){y[m].isStaircaseBalconyRailing=!0;continue}if(x==="balcony railing"&&f.intersectsOBB(p)){y[m].isStaircaseBalconyRailing=!0;continue}}let E=!1,S=[],I=[],W=u[d].points,v=y[m].points;if(W.length>v.length?(E=!0,S=W,I=v):(S=v,I=W),Ll(I,S,.03,.5)>.5)if(E)y[m].isDel=!0;else{u[d].isDel=!0;break}else if(ko(f,p,u[d].points,y[m].points)){let R=Oo(f,p);if(R===1||R===0){if(x==="switch"||x==="Staircase")continue;W.length>v.length?Ie(I,f)>=.5&&(y[m].isDel=!0):Ie(S,f)>=.5&&(y[m].isDel=!0)}else{if(a==="switch"||a==="Staircase")continue;if(W.length>v.length){if(Ie(S,p)>=.5){u[d].isDel=!0;break}}else if(Ie(I,p)>=.5){u[d].isDel=!0;break}}}}}}if(u[d].isDel)break}}}for(let i=0;i<o.length;i++){let a=o[i].type,u=o[i].array;for(let d=0;d<u.length;d++){let f=u[d].maxz,c=u[d].minz,l=a==="switch"?.1:.3;if(Math.abs(f-e)<l&&a!="floor"){u[d].isDel=!0;continue}if(a==="window"||a==="door"||a==="tv"){let h=Math.abs(f-e),g=Math.abs(c-e);(a==="tv"&&g<1.6||a!=="window"&&h<1.6)&&(u[d].isDel=!0),a==="window"&&h<1&&(u[d].isDel=!0),a==="door"&&g>1&&(u[d].isDel=!0)}if(a==="chair"){let h=new q.Vector3;u[d].obb.getSize(h),(h.x<.1||h.y<.1||h.z<.2)&&(u[d].isDel=!0)}if(a==="balcony railing"&&(Math.abs(f-c)<.4&&(u[d].isDel=!0),(c<e&&Math.abs(c-e)>1||c>e&&Math.abs(c-e)>1)&&(u[d].isDel=!0),f<e&&(u[d].isDel=!0)),a==="people"&&(Math.abs(f-c)<1&&(u[d].isDel=!0),c>e&&c-e>.3&&(u[d].isDel=!0),f<e&&(u[d].isDel=!0)),a==="floor"){let h=new q.Vector3;u[d].obb.getSize(h),(h.x<.1||h.y<.1)&&(u[d].isDel=!0)}a==="stair guardrail"&&u[d].points.length<100&&(u[d].isDel=!0)}}for(let i=0;i<o.length;i++)o[i].array=o[i].array.filter(a=>!a.isDel);if(n!=null)for(let i=0;i<o.length;i++){if(o[i].type=="floor")continue;let u=o[i].array,d=new Set,f=[];for(let c=0;c<u.length;c++){if(d.has(c))continue;let l=u[c].points,h=u[c].obj_id,g=u[c].type,x=u[c].averagePz,y=u[c].positionArr,m=u[c].isStaircaseBalconyRailing;if(l.length<50)continue;let p=n([l],{tolerance:.2,minClusterSize:50,maxClusterSize:1e7,threadCount:1}),b=f.length;if(p.length>0&&p[0].length>1)for(let w of p[0]){if(w.length<50)continue;let z=w.map(S=>l[S]),M=ue(z),P=z.map(S=>[S.x,S.y]),E={points:z,path:de(P),h:M.h,minz:M.minz,maxz:M.maxz,center:{x:M.x,y:M.y,z:M.z},type:g,averagePz:x,obb:null,obj_id:h,positionArr:y,isStaircaseBalconyRailing:m};E.obb=we(E.path,E.center,E.h,(E.maxz+E.minz)/2),E.obb!=null&&f.push(E)}f.length!=b&&d.add(c)}if(d.size>0){for(let c=0;c<u.length;c++)d.has(c)||f.push(u[c]);o[i].array=f}}let s=[];for(let i=0;i<o.length;i++){let a=o[i].array;for(let u=0;u<a.length;u++)s.push({obbBox:a[u].obb,box:new q.Box3().setFromPoints(a[u].points),category:a[u].type,center:{x:a[u].center.x,y:a[u].center.y,z:(a[u].minz+a[u].maxz)/2},name:a[u].obj_id,obj_id:a[u].obj_id,pcdPoints:a[u].points,positionArr:a[u].positionArr,isStaircaseBalconyRailing:a[u].isStaircaseBalconyRailing})}return s},Fl=t=>{if(t.length==0)return[];let e=[],n=new Map;for(let r=0;r<t.length;r++){let s=t[r].points.map(y=>[y.x,y.y]),i=t[r].obj_id;i=i.replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor");let a=i.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],u=i.indexOf("_")+1,d=i.lastIndexOf("_"),f=i.substring(u,d);if(f!=="floor")continue;let c=[];for(let y=0;y<t[r].points.length;y++)c.push(new q.Vector3(t[r].points[y].x,t[r].points[y].y,t[r].points[y].z));let l=-99999;if(f==="floor"){let y=new Map;for(let p=0;p<t[r].points.length;p++){let b=t[r].points[p].z;if(b=Math.floor(b*10)/10,y.has(b)){let w=y.get(b);w.num+=1,w.points.push(t[r].points[p]),y.set(b,w)}else y.set(b,{num:1,points:[t[r].points[p]]})}let m=[...y.entries()].reduce((p,b)=>b[1].num>p[1].num?b:p);s=[],s=m[1].points.map(p=>[p.x,p.y]),c=[],c=m[1].points.map(p=>new q.Vector3(p.x,p.y,p.z)),l=m[0]}let h=ue(c),g={points:c,path:de(s),h:h.h,minz:h.minz,maxz:h.maxz,center:{x:h.x,y:h.y,z:h.z},obj_id:t[r].obj_id,type:f,obb:null,isDel:!1,obj_id:i,positionArr:[t[r].position]};if(g.obb=we(g.path,g.center,g.h,(g.maxz+g.minz)/2),g.obb==null)continue;let x=-1;for(let y=0;y<e.length;y++)if(e[y].type==f){x=y;break}if(x!=-1?e[x].array.push(g):e.push({type:f,array:[g]}),f=="floor"&&l!=-99999)if(n.has(l)){let y=n.get(l);y.num+=g.points.length,y.datas.push(g),n.set(l,y)}else n.set(l,{num:g.points.length,datas:[g]})}for(let r=0;r<e.length;r++){let s=e[r].array,i=[],a=[];for(let d=0;d<s.length;d++){if(s[d].obb==null||i.includes(d))continue;let f={...s[d],obb:s[d].obb.clone()};for(let c=d+1;c<s.length;c++){if(s[c].obb==null||i.includes(c))continue;if(f.obb.intersectsOBB(s[c].obb)){i.push(c);for(let x=0;x<s[c].points.length;x++)f.points.push(s[c].points[x]);let h=f.points.map(x=>[x.x,x.y]);f.path=de(h);let g=ue(f.points);f.h=g.h,f.minz=g.minz,f.maxz=g.maxz,f.center={x:g.x,y:g.y,z:g.z},f.obb=we(f.path,f.center,f.h,(f.maxz+f.minz)/2),f.positionArr.push(...s[c].positionArr),c=d}}f.path.length>s[d].path.length&&(i.push(d),a.push(f))}let u=[];for(let d=0;d<s.length;d++)i.includes(d)||u.push(s[d]);u.push(...a),e[r].array=u}for(let r=0;r<e.length;r++){let s=e[r].type,i=e[r].array;for(let a=0;a<i.length;a++)s==="floor"&&i[a].points.length<500&&(i[a].isDel=!0)}for(let r=0;r<e.length;r++)e[r].array=e[r].array.filter(s=>!s.isDel);let o=[];for(let r=0;r<e.length;r++){let s=e[r].array;for(let i=0;i<s.length;i++)o.push({obbBox:s[i].obb,box:new q.Box3().setFromPoints(s[i].points),category:s[i].type,center:{x:s[i].center.x,y:s[i].center.y,z:(s[i].minz+s[i].maxz)/2},name:s[i].obj_id,obj_id:s[i].obj_id,pcdPoints:s[i].points,positionArr:s[i].positionArr})}return o},Nl=(t,e)=>{if(!t||!t.splitFloorProjectsResults||!t.trajectoryResults)return[];let n=t.splitFloorProjectsResults,o=t.trajectoryResults,r=new Map;for(let l=0;l<o.length;l++){let h=o[l].floor;r.has(h)?r.get(h).push(o[l]):r.set(h,[o[l]])}let s=new Map,i=[];for(let l=0;l<n.length;l++){let h=[],g=n[l];for(let x=0;x<g.length;x++){let y=g[x].points.map(S=>[S.x,S.y]),m=g[x].obj_id;m=m.replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor").replace(/calcony railing/g,"balcony railing").replace(/glass balcony railing/g,"balcony railing").replace(/metal balcony railing/g,"balcony railing").replace(/temporary construction guardrail/g,"balcony railing");let p=m.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],b=m.indexOf("_")+1,w=m.lastIndexOf("_"),z=m.substring(b,w);if(z.includes("top")&&(b=z.indexOf("top")+4,z=z.substring(b,w)),z.includes("left")&&(b=z.indexOf("left")+5,z=z.substring(b,w)),z.includes("right")&&(b=z.indexOf("right")+6,z=z.substring(b,w)),z.includes("front")&&(b=z.indexOf("front")+6,z=z.substring(b,w)),z!=="Staircase"&&z!=="balcony railing")continue;let M=[];for(let S=0;S<g[x].points.length;S++)M.push(new q.Vector3(g[x].points[S].x,g[x].points[S].y,g[x].points[S].z));let P=ue(M),E={points:M,path:de(y),h:P.h,minz:P.minz,maxz:P.maxz,center:{x:P.x,y:P.y,z:P.z},obj_id:g[x].obj_id,type:z,averagePz:0,obb:null,isDel:!1,obj_id:m,positionArr:[g[x].position],aabbBox:new q.Box3().setFromPoints(M)};E.obb=we(E.path,E.center,E.h,(E.maxz+E.minz)/2),E.obb!=null&&(z=="Staircase"?h.push(E):z=="balcony railing"&&i.push(E))}s.set(l+1,h)}let a=[];for(let[l,h]of s){let g={floor:l,findTrajectoryResults:[],otherTrajectoryResults:[],lastTrajectoryResults:[]},x=r.get(l);if(!x){g.otherTrajectoryResults=h,a.push(g);continue}let y=[],m=[],p=[];for(let b=0;b<x.length;b++){let w=[];for(let M=0;M<x[b].points.length;M++){let P=[new q.Vector3(x[b].points[M].x,x[b].points[M].y,x[b].points[M].z)];P.push(P[0].clone().addScaledVector(new q.Vector3(0,0,-1),2));let E=new q.Box3().setFromPoints(P);w.push(E)}let z=[];for(let M=0;M<h.length;M++)for(let P=0;P<w.length;P++)!p.includes(M)&&h[M].aabbBox.intersectsBox(w[P])&&(p.push(M),z.push(h[M]));z.length>0&&m.push(z)}for(let b=0;b<h.length;b++)p.includes(b)||y.push(h[b]);g.findTrajectoryResults=m,g.otherTrajectoryResults=y,a.push(g)}function u(l,h){let g=l,x=[],y=[];for(let p=0;p<l.length;p++){if(l[p].obb==null||x.includes(p))continue;let b={...l[p],obb:l[p].obb.clone()};for(let w=p+1;w<l.length;w++){if(l[w].obb==null||x.includes(w))continue;if(b.obb.intersectsOBB(l[w].obb)||h){x.push(w);for(let E=0;E<l[w].points.length;E++)b.points.push(l[w].points[E]);let M=b.points.map(E=>[E.x,E.y]);b.path=de(M);let P=ue(b.points);b.h=P.h,b.minz=P.minz,b.maxz=P.maxz,b.center={x:P.x,y:P.y,z:P.z},b.obb=we(b.path,b.center,b.h,(b.maxz+b.minz)/2),b.positionArr.push(...l[w].positionArr),w=p}}b.path.length>l[p].path.length&&(x.push(p),y.push(b))}let m=[];for(let p=0;p<l.length;p++)x.includes(p)||m.push(l[p]);return m.push(...y),l=m,l}for(let l=0;l<a.length;l++){for(let h=0;h<a[l].findTrajectoryResults.length;h++)a[l].findTrajectoryResults[h]=u(a[l].findTrajectoryResults[h],!0);a[l].otherTrajectoryResults=u(a[l].otherTrajectoryResults,!1)}for(let l=0;l<a.length;l++){for(let h=0;h<a[l].findTrajectoryResults.length;h++){for(let g=0;g<a[l].findTrajectoryResults[h].length;g++){let x=zs(a[l].findTrajectoryResults[h][g].points,{binCount:100,minBinPoints:5,sideK:2.2,zK:2.2,minSideRange:.2,minZRange:.2,lowerZScale:.4,upperZScale:1.8}),y=Ms(x.keep,{radius:.1,minClusterSize:50,lowPercent:.45});if(y.keep.length==0){a[l].findTrajectoryResults[h][g].isDel=!0;continue}a[l].findTrajectoryResults[h][g].points=y.keep;let m=a[l].findTrajectoryResults[h][g].points.map(b=>[b.x,b.y]);a[l].findTrajectoryResults[h][g].path=de(m);let p=ue(a[l].findTrajectoryResults[h][g].points);a[l].findTrajectoryResults[h][g].h=p.h,a[l].findTrajectoryResults[h][g].minz=p.minz,a[l].findTrajectoryResults[h][g].maxz=p.maxz,a[l].findTrajectoryResults[h][g].center={x:p.x,y:p.y,z:p.z},a[l].findTrajectoryResults[h][g].obb=we(a[l].findTrajectoryResults[h][g].path,a[l].findTrajectoryResults[h][g].center,a[l].findTrajectoryResults[h][g].h,(a[l].findTrajectoryResults[h][g].maxz+a[l].findTrajectoryResults[h][g].minz)/2)}a[l].findTrajectoryResults[h]=a[l].findTrajectoryResults[h].filter(g=>!g.isDel)}for(let h=0;h<a[l].otherTrajectoryResults.length;h++){let g=zs(a[l].otherTrajectoryResults[h].points,{binCount:100,minBinPoints:5,sideK:2.2,zK:2.2,minSideRange:.2,minZRange:.2,lowerZScale:.4,upperZScale:1.8}),x=Ms(g.keep,{radius:.1,minClusterSize:50,lowPercent:.45});if(x.keep.length==0){a[l].otherTrajectoryResults[h].isDel=!0;continue}a[l].otherTrajectoryResults[h].points=x.keep;let y=a[l].otherTrajectoryResults[h].points.map(p=>[p.x,p.y]);a[l].otherTrajectoryResults[h].path=de(y);let m=ue(a[l].otherTrajectoryResults[h].points);a[l].otherTrajectoryResults[h].h=m.h,a[l].otherTrajectoryResults[h].minz=m.minz,a[l].otherTrajectoryResults[h].maxz=m.maxz,a[l].otherTrajectoryResults[h].center={x:m.x,y:m.y,z:m.z},a[l].otherTrajectoryResults[h].obb=we(a[l].otherTrajectoryResults[h].path,a[l].otherTrajectoryResults[h].center,a[l].otherTrajectoryResults[h].h,(a[l].otherTrajectoryResults[h].maxz+a[l].otherTrajectoryResults[h].minz)/2)}a[l].otherTrajectoryResults.filter(h=>!h.isDel)}function d(l){for(let h=0;h<l.length;h++){let g=l[h].obb;for(let x=h+1;x<l.length;x++){let y=l[x].obb;if(!l[x].isDel){if(g.intersectsOBB(y)){let m=Ie(l[x].points,g),p=Ie(l[h].points,y),b=Oo(g,y);if(m>=.5&&(b==1||b==0)&&(l[x].isDel=!0),p>=.5&&b==-1){l[h].isDel=!0;break}}if(l[h].isDel)break}}}return l=l.filter(h=>!h.isDel),l}for(let l=0;l<a.length;l++){let h=[];for(let g=0;g<a[l].findTrajectoryResults.length;g++)h.push(...a[l].findTrajectoryResults[g]);h.push(...a[l].otherTrajectoryResults),a[l].lastTrajectoryResults=d(h)}function f(l){let h=[];if(l.length>0){let g=.3,x=.5,y=.5,m=.2;for(let p=0;p<l.length;p++){let b=l[p].obb.halfSize.clone().multiplyScalar(2),w=l[p].points,z=b.x,M=b.y,P=b.z;if(z>M&&([z,M]=[M,z]),z<x||M<g||P<y)continue;let E=wr(l[p].obb);if(E.length!=8)continue;let S=new q.Line3(E[0],E[1]),I=new q.Line3(E[1],E[2]),W=new q.Line3(E[2],E[3]),v=new q.Line3(E[3],E[0]),T=new q.Line3(E[4],E[5]),R=new q.Line3(E[5],E[6]),D=new q.Line3(E[6],E[7]),H=new q.Line3(E[7],E[4]),V=[],C=[],_=[],A=[],L=[],k=[],F=[],B=[];for(let yt=0;yt<w.length;yt++){let Jt=S.closestPointToPoint(w[yt],!0,new q.Vector3),me=I.closestPointToPoint(w[yt],!0,new q.Vector3),Rt=W.closestPointToPoint(w[yt],!0,new q.Vector3),ee=v.closestPointToPoint(w[yt],!0,new q.Vector3),et=T.closestPointToPoint(w[yt],!0,new q.Vector3),pe=R.closestPointToPoint(w[yt],!0,new q.Vector3),nn=D.closestPointToPoint(w[yt],!0,new q.Vector3),Le=H.closestPointToPoint(w[yt],!0,new q.Vector3);Jt.distanceTo(w[yt])<m&&V.push(w[yt]),me.distanceTo(w[yt])<m&&C.push(w[yt]),Rt.distanceTo(w[yt])<m&&_.push(w[yt]),ee.distanceTo(w[yt])<m&&A.push(w[yt]),et.distanceTo(w[yt])<m&&L.push(w[yt]),pe.distanceTo(w[yt])<m&&k.push(w[yt]),nn.distanceTo(w[yt])<m&&F.push(w[yt]),Le.distanceTo(w[yt])<m&&B.push(w[yt])}let O=E[0].distanceTo(E[1]),$=E[1].distanceTo(E[2]),G=E[2].distanceTo(E[3]),Q=E[3].distanceTo(E[0]),j=new q.Vector3,Z=new q.Vector3,U=new q.Vector3,it=new q.Vector3;Math.abs(O-z)<.01?V.length>_.length?(j=E[0].clone(),Z=E[1].clone(),U=new q.Vector3().subVectors(Z,j).normalize(),it=new q.Vector3().subVectors(E[2],E[1]).normalize()):(j=E[2].clone(),Z=E[3].clone(),U=new q.Vector3().subVectors(Z,j).normalize(),it=new q.Vector3().subVectors(E[0],E[3]).normalize()):C.length>A.length?(j=E[1].clone(),Z=E[2].clone(),U=new q.Vector3().subVectors(Z,j).normalize(),it=new q.Vector3().subVectors(E[3],E[2]).normalize()):(j=E[3].clone(),Z=E[0].clone(),U=new q.Vector3().subVectors(Z,j).normalize(),it=new q.Vector3().subVectors(E[1],E[0]).normalize());let ht=E[4].distanceTo(E[5]),St=E[5].distanceTo(E[6]),ot=E[6].distanceTo(E[7]),Mt=E[7].distanceTo(E[4]),xt=new q.Vector3,vt=new q.Vector3,Pt=new q.Vector3(j.x,j.y,E[4].z),zt=new q.Vector3(Z.x,Z.y,E[4].z);(ct(E[4],Pt)||ct(E[4],zt))&&(ct(E[5],Pt)||ct(E[5],zt))?(xt=E[6].clone(),vt=E[7].clone()):(ct(E[5],Pt)||ct(E[5],zt))&&(ct(E[6],Pt)||ct(E[6],zt))?(xt=E[7].clone(),vt=E[4].clone()):(ct(E[6],Pt)||ct(E[6],zt))&&(ct(E[7],Pt)||ct(E[7],zt))?(xt=E[4].clone(),vt=E[5].clone()):(ct(E[7],Pt)||ct(E[7],zt))&&(ct(E[4],Pt)||ct(E[4],zt))&&(xt=E[5].clone(),vt=E[6].clone());let Tt=j.distanceTo(Z),nt=xt.distanceTo(vt),ft=new q.Vector3().subVectors(new q.Vector3(j.x,j.y,xt.z),j).normalize();if(Math.abs(Tt-nt)>.01||Math.abs(Tt-z)>.01){console.log("\u4E0A\u4E0B\u957F\u5EA6\u4E0D\u4E00\u81F4,\u6216\u4E0E\u5BBD\u4E0D\u4E00\u81F4",Tt,nt,z);continue}let rt=Math.sqrt(Tt*Tt+nt*nt),tt=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(rt)))),le={stairStepHeight:P/tt,stairStepWidth:M/tt,staitWidth:z,stairLength:M,stairHeigth:P,stairStepNum:tt,stairBottomPt1:j,stairBottomPt2:Z,stairTopPt1:xt,stairTopPt2:vt,vecStepLength:U,vecStepWidth:it,vecStepHeight:ft,path:l[p].path,obb:l[p].obb,maxz:l[p].maxz,points:l[p].points,rectPt1:new q.Vector3(0,0,0),rectPt2:new q.Vector3(0,0,0)};h.push(le)}}return h}let c=[];for(let l=0;l<a.length;l++)c.push(...f(a[l].lastTrajectoryResults));for(let l=0;l<c.length;l++){let h=c[l].stairTopPt1.clone(),g=c[l].stairTopPt2.clone(),x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=h.clone(),p=g.clone(),b=x.clone(),w=y.clone();m.z=p.z=b.z=w.z=0;let z=At(h,g,!0),M=At(x,y,!0),P=At(z,M),E=new q.Vector3().subVectors(z,P).normalize(),S=new q.Vector3().subVectors(M,P).normalize(),I=z.distanceTo(M),W=!1,v=!1;for(let T=0;T<c.length;T++){if(l==T)continue;let R=c[T].stairTopPt1.clone(),D=c[T].stairTopPt2.clone(),H=c[T].stairBottomPt1.clone(),V=c[T].stairBottomPt2.clone(),C=new q.Vector3().subVectors(R,D).normalize(),_=new q.Vector3().subVectors(H,V).normalize(),A=R.clone(),L=D.clone(),k=H.clone(),F=V.clone();A.z=L.z=k.z=F.z=0;let B=At(R,D,!0),O=At(H,V,!0),$=B.distanceTo(O),G=Xt(z,M,B,O),Q=!1;if(G&&G.isParallel&&R.z<=x.z&&x.z-R.z<.3){Q=!0,v=!0;let j=A.addScaledVector(C.clone().negate(),100),Z=L.addScaledVector(C,100),U=k.addScaledVector(_.clone().negate(),100),it=F.addScaledVector(_,100),ht=new q.Line3(j,Z),St=new q.Line3(U,it),ot=ht.closestPointToPoint(P,!0,new q.Vector3),Mt=St.closestPointToPoint(P,!0,new q.Vector3),xt=new q.Vector3().subVectors(ot,P),vt=new q.Vector3().subVectors(Mt,P),Pt=xt.length(),zt=vt.length();vt=vt.normalize(),xt=xt.normalize();let Tt=$t(E,xt,.1),nt=$t(S,xt,.1),ft=$t(S,vt,.1),rt=$t(E,vt,.1);if(Tt&&Tt.sameDirection&&Pt>I/2){let tt=Pt-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,tt),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,tt)}else if(nt&&nt.sameDirection&&Pt>I/2){let tt=Pt-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,tt),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,tt)}if(ft&&ft.sameDirection&&zt>I/2){let tt=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,tt),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,tt)}else if(rt&&rt.sameDirection&&zt>I/2){let tt=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,tt),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,tt)}}else if(G&&G.isParallel&&h.z<H.z&&H.z-h.z<.2){Q=!0,W=!0;let j=A.addScaledVector(C.clone().negate(),100),Z=L.addScaledVector(C,100),U=k.addScaledVector(_.clone().negate(),100),it=F.addScaledVector(_,100),ht=new q.Line3(j,Z),St=new q.Line3(U,it),ot=ht.closestPointToPoint(P,!1,new q.Vector3),Mt=St.closestPointToPoint(P,!1,new q.Vector3),xt=new q.Vector3().subVectors(ot,P),vt=new q.Vector3().subVectors(Mt,P),Pt=xt.length(),zt=vt.length();vt=vt.normalize(),xt=xt.normalize();let Tt=$t(E,xt,.1),nt=$t(S,xt,.1),ft=$t(S,vt,.1),rt=$t(E,vt,.1);if(Tt&&Tt.sameDirection&&Pt>I/2){let tt=Pt-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,tt),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,tt)}else if(nt&&nt.sameDirection&&Pt>I/2){let tt=Pt-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,tt),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,tt)}if(ft&&ft.sameDirection&&zt>I/2){let tt=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,tt),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,tt)}else if(rt&&rt.sameDirection&&zt>I/2){let tt=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,tt),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,tt)}c[l].stairTopPt1.z=c[l].stairTopPt2.z=H.z}if(Q&&(h=c[l].stairTopPt1.clone(),g=c[l].stairTopPt2.clone(),x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=h.clone(),p=g.clone(),b=x.clone(),w=y.clone(),m.z=p.z=b.z=w.z=0,z=At(h,g,!0),M=At(x,y,!0),P=At(z,M),E=new q.Vector3().subVectors(z,P).normalize(),S=new q.Vector3().subVectors(M,P).normalize(),I=z.distanceTo(M)),v&&W)break}if(v||W){let T=c[l].stairTopPt1.clone(),R=c[l].stairTopPt2.clone(),D=c[l].stairBottomPt1.clone(),H=c[l].stairBottomPt2.clone(),V=At(T,R,!0),C=At(D,H,!0),_=V.distanceTo(C),A=c[l].stairTopPt1.distanceTo(c[l].stairTopPt2),L=c[l].stairBottomPt1.distanceTo(c[l].stairBottomPt2),k=Math.sqrt(A*A+L*L),F=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(k))));c[l].stairStepNum=F,c[l].stairHeigth=c[l].stairTopPt1.z-c[l].stairBottomPt1.z,c[l].stairStepHeight=c[l].stairHeigth/c[l].stairStepNum,c[l].stairStepWidth=_/c[l].stairStepNum}}for(let l=0;l<c.length;l++){let h=c[l].stairTopPt1,g=c[l].stairTopPt2,x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=new q.Vector3(h.x,h.y,x.z),p=c[l].vecStepWidth;for(let b=0;b<c.length;b++){if(l==b)continue;let w=c[b].stairTopPt1,z=c[b].stairTopPt2,M=c[b].stairBottomPt1.clone(),P=c[b].stairBottomPt2.clone(),E=new q.Vector3(w.x,w.y,M.z),S=c[b].vecStepWidth;if(Math.abs(h.z-M.z)<.3&&M.z>x.z&&c[l].obb.intersectsOBB(c[b].obb)){let I=ie(m,x,E,M);if(I&&I.angle>75){let W=M.distanceTo(h),v=M.distanceTo(g),T=P.distanceTo(h),R=P.distanceTo(g),D=h.clone(),H=D.clone().addScaledVector(p,c[b].staitWidth),V=new q.Vector3().subVectors(g,h).normalize(),C=h.distanceTo(g),_=D.clone().addScaledVector(V,C),A=H.clone().addScaledVector(V,C),L=D.clone().addScaledVector(V,c[b].stairLength),k=H.clone().addScaledVector(V,c[b].stairLength),F=W;(F>v||F>R)&&(F=v,D=g.clone(),H=D.clone().addScaledVector(p,c[b].staitWidth),V=new q.Vector3().subVectors(h,g).normalize(),C=h.distanceTo(g),_=D.clone().addScaledVector(V,C),A=H.clone().addScaledVector(V,C),L=D.clone().addScaledVector(V,c[b].stairLength),k=H.clone().addScaledVector(V,c[b].stairLength));let B=new q.Vector3().subVectors(L,_).normalize(),O=L.distanceTo(_);L.z=k.z=c[b].maxz,c[b].stairBottomPt1=_,c[b].stairBottomPt2=A,c[b].stairTopPt1=L,c[b].stairTopPt2=k,c[b].rectPt1=D,c[b].rectPt2=H;let $=_.distanceTo(A),G=L.distanceTo(k),Q=Math.sqrt(O*O+c[b].stairHeigth*c[b].stairHeigth),j=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(Q))));c[b].stairStepNum=j,c[b].stairHeigth=L.z-_.z,c[b].stairStepHeight=c[b].stairHeigth/c[b].stairStepNum,c[b].stairStepWidth=O/c[b].stairStepNum,c[b].vecStepLength=V.clone().negate(),c[b].vecStepWidth=B;break}}}}return t.finalFloorMap&&jl(c,t.finalFloorMap),ql(c,i,e),c},ql=(t,e,n)=>{if(!t||t.length<=0||!e||e.length<=0)return;let o=1.5,r=0;for(let s=0;s<t.length;s++){t[s].stairBottom1BalconyRailing=[],t[s].stairBottom2BalconyRailing=[],t[s].balconyRailingHeight=0;let i=t[s].stairBottomPt1.clone(),a=t[s].stairBottomPt2.clone(),u=t[s].stairBottomPt1.clone(),d=t[s].stairBottomPt2.clone(),f=new q.Vector3().subVectors(a,i).normalize();i=i.addScaledVector(f,.05),a=a.addScaledVector(f.clone().negate(),.05);let c=[],l=[];for(let h=0;h<t[s].stairStepNum;h++){let g=[i,u];g.push(g[1].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),g.push(g[0].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth));let x=g.map(z=>[z.x,z.y]);g.push(new q.Vector3(g[3].x,g[3].y,g[3].z+o)),g.push(new q.Vector3(g[2].x,g[2].y,g[2].z+o)),g.push(new q.Vector3(g[1].x,g[1].y,g[1].z+o)),g.push(new q.Vector3(g[0].x,g[0].y,g[0].z+o));let y=ue(g),m=we(x,{x:y.x,y:y.y,z:y.z},y.h,(y.maxz+y.minz)/2),p=[],b=!1,w=0;for(let z=0;z<e.length;z++)if(m.intersectsOBB(e[z].obb)){for(let M=0;M<e[z].points.length&&!(m.containsPoint(e[z].points[M])&&(h==0&&p.push(e[z].points[M]),e[z].points[M].z>i.z&&e[z].points[M].z-i.z<1&&(w++,w>10&&h!=0)));M++);if(w>10&&(b=!0),h!=0&&b)break}if(r==0&&(r=ue(p).maxz),b){let z=At(g[1],g[2]);z.z+=t[s].stairStepHeight,c.push(z)}g=[],g=[a,d],g.push(g[1].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),g.push(g[0].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),x=g.map(z=>[z.x,z.y]),g.push(new q.Vector3(g[3].x,g[3].y,g[3].z+o)),g.push(new q.Vector3(g[2].x,g[2].y,g[2].z+o)),g.push(new q.Vector3(g[1].x,g[1].y,g[1].z+o)),g.push(new q.Vector3(g[0].x,g[0].y,g[0].z+o)),y=ue(g),m=we(x,{x:y.x,y:y.y,z:y.z},y.h,(y.maxz+y.minz)/2),p=[],b=!1;for(let z=0;z<e.length;z++)if(m.intersectsOBB(e[z].obb)){let M=0;for(let P=0;P<e[z].points.length&&!(m.containsPoint(e[z].points[P])&&(h==0&&p.push(e[z].points[P]),e[z].points[P].z>i.z&&e[z].points[P].z-i.z<1&&(M++,M>10&&h!=0)));P++);if(M>10&&(b=!0),h!=0&&b)break}if(r==0&&(r=ue(p).maxz),b){let z=At(g[1],g[2]);z.z+=t[s].stairStepHeight,l.push(z)}i=i.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),i=i.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),a=a.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),a=a.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),u=u.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),u=u.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),d=d.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),d=d.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight)}c.length>t[s].stairStepNum-3&&(t[s].stairBottom1BalconyRailing=c),l.length>t[s].stairStepNum-3&&(t[s].stairBottom2BalconyRailing=l),t[s].balconyRailingHeight=r}};function jl(t,e){if(!t||!e)return;let n=[];for(let o=0;o<t.length;o++){let r=t[o].stairTopPt1.clone(),s=t[o].stairTopPt2.clone(),i=t[o].stairBottomPt1.clone(),a=t[o].stairBottomPt2.clone(),u=r.clone(),d=s.clone(),f=i.clone(),c=a.clone();u.z=d.z=f.z=c.z=0;let l=At(r,s,!0),h=At(i,a,!0),g=At(l,h),x=new q.Vector3().subVectors(l,g).normalize(),y=new q.Vector3().subVectors(h,g).normalize(),m=l.distanceTo(h),p=-1,b=-1;for(let w=0;w<t.length;w++){if(o==w)continue;let z=t[w].stairTopPt1.clone(),M=t[w].stairTopPt2.clone(),P=t[w].stairBottomPt1.clone(),E=t[w].stairBottomPt2.clone(),S=new q.Vector3().subVectors(z,M).normalize(),I=new q.Vector3().subVectors(P,E).normalize(),W=z.clone(),v=M.clone(),T=P.clone(),R=E.clone();W.z=v.z=T.z=R.z=0;let D=At(z,M,!0),H=At(P,E,!0),V=D.distanceTo(H),C=Xt(l,h,D,H);if(C&&C.isParallel&&r.z<=P.z&&P.z-r.z<=.2?p=w:C&&C.isParallel&&z.z<=i.z&&i.z-z.z<=.3&&(b=w),p!=-1&&b!=-1)break}for(let w=0;w<e.length;w++){let z=e[w][1];for(let M=0;M<z.length;M++){let P=`${w}+${M}`;if(n.includes(P))continue;let E=z[M].points;if(o==0&&Math.abs(E[0].z-i.z)<.2){let S=i.clone().addScaledVector(t[o].vecStepWidth.clone().negate(),5),I=a.clone().addScaledVector(t[o].vecStepWidth.clone().negate(),5),W=new q.Line3(S,I),v=new q.Line3(i,a),T=999999,R=0,D=new q.Vector3,H=0;for(let V=0;V<E.length;V++){let C=new q.Vector3(E[V].x,E[V].y,S.z),A=W.closestPointToPoint(C,!1,new q.Vector3).distanceTo(C),L=v.closestPointToPoint(C,!1,new q.Vector3),k=new q.Vector3().subVectors(C,L).normalize(),F=$t(k,t[o].vecStepWidth.clone().negate(),.1);A<T&&F.sameDirection&&(T=A,R=C.distanceTo(L),D=C.clone(),H=E[V].z)}T!=-999999&&(n.push(P),t[o].stairBottomPt1.z=t[o].stairBottomPt2.z=H)}else if(Math.abs(E[0].z-r.z)<.2){let S=r.clone().addScaledVector(t[o].vecStepWidth,5),I=s.clone().addScaledVector(t[o].vecStepWidth,5),W=new q.Line3(S,I),v=new q.Line3(r,s),T=0,R=999999,D=new q.Vector3,H=0;for(let V=0;V<E.length;V++){let C=new q.Vector3(E[V].x,E[V].y,S.z),_=W.closestPointToPoint(C,!1,new q.Vector3),A=v.closestPointToPoint(C,!1,new q.Vector3),L=new q.Vector3().subVectors(C,A).normalize(),k=_.distanceTo(C),F=$t(L,t[o].vecStepWidth);k<R&&F.sameDirection&&(R=k,T=C.distanceTo(A),D=C,H=E[V].z)}if(R!=-999999){if(n.push(P),t[o].stairTopPt1.z=t[o].stairTopPt2.z=H,p!=-1){t[p].stairBottomPt1.z=t[p].stairBottomPt2.z=H;let V=[t[o].stairTopPt1.clone(),t[o].stairTopPt2.clone(),t[p].stairBottomPt1.clone(),t[p].stairBottomPt2.clone()],C=We(V);C.length==2&&(t[o].stairTopFloorPts=C,t[o].stairTopFloorPts.push(t[o].stairTopFloorPts[1].clone().addScaledVector(t[o].vecStepWidth,T)),t[o].stairTopFloorPts.push(t[o].stairTopFloorPts[0].clone().addScaledVector(t[o].vecStepWidth,T)))}else t[o].stairTopFloorPts=[t[o].stairTopPt1.clone(),t[o].stairTopPt2.clone()],t[o].stairTopFloorPts.push(t[o].stairTopPt2.clone().addScaledVector(t[o].vecStepWidth,T)),t[o].stairTopFloorPts.push(t[o].stairTopPt1.clone().addScaledVector(t[o].vecStepWidth,T));if(t[o].stairTopFloorPts)for(let V=0;V<t[o].stairTopFloorPts.length;V++)t[o].stairTopFloorPts[V].z-=t[o].stairStepHeight;break}}}}if(p!=-1){let w=t[o].stairTopPt1.clone(),z=t[o].stairTopPt2.clone(),M=t[o].stairBottomPt1.clone(),P=t[o].stairBottomPt2.clone(),E=At(w,z,!0),S=At(M,P,!0),I=E.distanceTo(S),W=t[o].stairTopPt1.distanceTo(t[o].stairTopPt2),v=t[o].stairBottomPt1.distanceTo(t[o].stairBottomPt2),T=Math.sqrt(W*W+v*v),R=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(T))));t[o].stairStepNum=R,t[o].stairHeigth=t[o].stairTopPt1.z-t[o].stairBottomPt1.z,t[o].stairStepHeight=t[o].stairHeigth/t[o].stairStepNum,t[o].stairStepWidth=I/t[o].stairStepNum;let D=t[p].stairTopPt1.clone(),H=t[p].stairTopPt2.clone(),V=t[p].stairBottomPt1.clone(),C=t[p].stairBottomPt2.clone(),_=At(D,H,!0),A=At(V,C,!0),L=_.distanceTo(A),k=t[p].stairTopPt1.distanceTo(t[p].stairTopPt2),F=t[p].stairBottomPt1.distanceTo(t[p].stairBottomPt2),B=Math.sqrt(k*k+F*F),O=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(B))));t[p].stairStepNum=O,t[p].stairHeigth=t[p].stairTopPt1.z-t[p].stairBottomPt1.z,t[p].stairStepHeight=t[p].stairHeigth/t[p].stairStepNum,t[p].stairStepWidth=L/t[p].stairStepNum}}}function zs(t,e={}){let{binCount:n=80,minBinPoints:o=5,sideK:r=3,zK:s=3,minSideRange:i=.2,minZRange:a=.2,maxSearchBins:u=3,lowerZScale:d=1,upperZScale:f=1}=e;if(!t||t.length===0)return[];let c=t.map((_,A)=>({index:A,x:_.x,y:_.y,z:_.z,raw:_})),l=0,h=0;for(let _ of c)l+=_.x,h+=_.y;l/=c.length,h/=c.length;let g=0,x=0,y=0;for(let _ of c){let A=_.x-l,L=_.y-h;g+=A*A,x+=A*L,y+=L*L}g/=c.length,x/=c.length,y/=c.length;let m=g+y,p=Math.sqrt((g-y)*(g-y)+4*x*x),b=(m+p)/2,w,z;Math.abs(x)>1e-8?(w=b-y,z=x):g>=y?(w=1,z=0):(w=0,z=1);let M=Math.sqrt(w*w+z*z)||1;w/=M,z/=M;let P=-z,E=w,S=1/0,I=-1/0,W=c.map(_=>{let A=_.x-l,L=_.y-h,k=A*w+L*z,F=A*P+L*E;return S=Math.min(S,k),I=Math.max(I,k),{..._,t:k,side:F}}),T=(I-S||1)/n,R=new Map;for(let _ of W){let A=Math.floor((_.t-S)/T);A<0&&(A=0),A>=n&&(A=n-1),R.has(A)||R.set(A,[]),R.get(A).push(_)}let D=new Map;for(let[_,A]of R){if(A.length<o)continue;let L=A.map(j=>j.side).sort((j,Z)=>j-Z),k=A.map(j=>j.z).sort((j,Z)=>j-Z),F=eo(L),B=eo(k),O=eo(L.map(j=>Math.abs(j-F)).sort((j,Z)=>j-Z)),$=eo(k.map(j=>Math.abs(j-B)).sort((j,Z)=>j-Z)),G=Math.max(i,O*1.4826*r),Q=Math.max(a,$*1.4826*s);D.set(_,{sideMedian:F,zMedian:B,sideRange:G,zRange:Q})}function H(_){if(D.has(_))return D.get(_);for(let A=1;A<=u;A++){if(D.has(_-A))return D.get(_-A);if(D.has(_+A))return D.get(_+A)}return null}let V=[],C=[];for(let _ of W){let A=Math.floor((_.t-S)/T);A<0&&(A=0),A>=n&&(A=n-1);let L=H(A);if(!L){C.push(_.raw);continue}let k=Math.abs(_.side-L.sideMedian)<=L.sideRange,F=L.zMedian-L.zRange*d,B=L.zMedian+L.zRange*f,O=_.z>=F&&_.z<=B;k&&O?V.push(_.raw):C.push(_.raw)}return{keep:V,removed:C,debug:{meanX:l,meanY:h,dirX:w,dirY:z,sideX:P,sideY:E,minT:S,maxT:I,binSize:T,stats:D}}}function eo(t){if(t.length===0)return 0;let e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]}function $l(t,e){let n=new Map;for(let o=0;o<t.length;o++){let r=t[o],s=Math.floor(r.x/e),i=Math.floor(r.y/e),a=Math.floor(r.z/e),u=`${s},${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}function Xl(t,e,n,o,r){let s=t[n],i=Math.floor(s.x/o),a=Math.floor(s.y/o),u=Math.floor(s.z/o),d=[];for(let f=-1;f<=1;f++)for(let c=-1;c<=1;c++)for(let l=-1;l<=1;l++){let h=`${i+f},${a+c},${u+l}`,g=e.get(h);if(g)for(let x of g){if(x===n)continue;let y=t[x],m=s.x-y.x,p=s.y-y.y,b=s.z-y.z;m*m+p*p+b*b<=r&&d.push(x)}}return d}function Ms(t,e={}){let{radius:n=.35,minClusterSize:o=20,lowPercent:r=.35}=e,s=Zl(t,{radius:n}),i=t.map(f=>f.z).sort((f,c)=>f-c),a=i[Math.floor(i.length*r)],u=[],d=[];for(let f of s.clusters){let c=f.map(g=>s.points[g]),l=c.reduce((g,x)=>g+x.z,0)/c.length;c.length<o&&l<=a?d.push(...c.map(g=>g.raw)):u.push(...c.map(g=>g.raw))}return{keep:u,removed:d}}function Zl(t,e={}){let{radius:n=.35}=e,o=t.map((u,d)=>({index:d,x:u.x,y:u.y,z:u.z,raw:u})),r=$l(o,n),s=n*n,i=new Array(o.length).fill(!1),a=[];for(let u=0;u<o.length;u++){if(i[u])continue;let d=[],f=[u];for(i[u]=!0;f.length>0;){let c=f.shift();d.push(c);let l=Xl(o,r,c,n,s);for(let h of l)i[h]||(i[h]=!0,f.push(h))}a.push(d)}return{points:o,clusters:a}}function Gl(t,e,n=1e-6){let o=Yl(e);for(let r of o)if(!Ul(t,r,n))return!1;return!0}function Yl(t){let e=t.center,n=t.halfSize,o=t.rotation.elements,r=new q.Vector3(o[0],o[1],o[2]).multiplyScalar(n.x),s=new q.Vector3(o[3],o[4],o[5]).multiplyScalar(n.y),i=new q.Vector3(o[6],o[7],o[8]).multiplyScalar(n.z),a=[];for(let u of[-1,1])for(let d of[-1,1])for(let f of[-1,1]){let c=e.clone().addScaledVector(r,u).addScaledVector(s,d).addScaledVector(i,f);a.push(c)}return a}function Ul(t,e,n=1e-6){let o=new q.Vector3().subVectors(e,t.center),r=t.rotation.elements,s=t.halfSize,i=new q.Vector3(r[0],r[1],r[2]),a=new q.Vector3(r[3],r[4],r[5]),u=new q.Vector3(r[6],r[7],r[8]),d=o.dot(i),f=o.dot(a),c=o.dot(u);return Math.abs(d)<=s.x+n&&Math.abs(f)<=s.y+n&&Math.abs(c)<=s.z+n}function Kl(t,e){if(!t||t.length===0)return[];let n=new Map;for(let i=0;i<t.length;i++){let a=t[i],u=a[0],d=a[1],f=Math.floor(u/e),c=Math.floor(d/e),l=`${f}_${c}`,h=n.get(l);if(!h){let m=(f+.5)*e,p=(c+.5)*e,b=u-m,w=d-p;h={minX:a,maxX:a,minY:a,maxY:a,center:a,centerDist2:b*b+w*w,cx:m,cy:p},n.set(l,h);continue}u<h.minX[0]&&(h.minX=a),u>h.maxX[0]&&(h.maxX=a),d<h.minY[1]&&(h.minY=a),d>h.maxY[1]&&(h.maxY=a);let g=u-h.cx,x=d-h.cy,y=g*g+x*x;y<h.centerDist2&&(h.center=a,h.centerDist2=y)}let o=new Set,r=[];function s(i){let a=`${i[0]}_${i[1]}`;o.has(a)||(o.add(a),r.push(i))}for(let i of n.values())s(i.minX),s(i.maxX),s(i.minY),s(i.maxY),s(i.center);return r}function Ql(t,e,n,o){return 1+(t[e][0]===0||t[n][0]===0||t[o][0]===0?1:0)}function Jl(t,e,n,o,r,s){let i=Math.hypot(n-r,o-s),a=Math.hypot(t-r,e-s),u=Math.hypot(t-n,e-o),d=Math.abs((n-t)*(s-e)-(r-t)*(o-e))/2;return d===0?1/0:i*a*u/(4*d)}function ta(t){if(!t||t.length<3)return[];let e=new Set,n=[];for(let g of t){let x=`${g[0]}_${g[1]}`;e.has(x)||(e.add(x),n.push(g))}if(n.length<3)return[];let o=new Ae(Float64Array.from(n.flat())),{triangles:r}=o;if(!r.length)return[];let s=new Map,i=(g,x)=>g<x?`${g}_${x}`:`${x}_${g}`;for(let g=0;g<r.length;g+=3){let x=r[g],y=r[g+1],m=r[g+2],[p,b]=n[x],[w,z]=n[y],[M,P]=n[m],E=Jl(p,b,w,z,M,P),S=Ql(n,x,y,m);if(E<=1/S)for(let[I,W]of[[x,y],[y,m],[m,x]]){let v=i(I,W);s.set(v,(s.get(v)||0)+1)}}let a=[];for(let[g,x]of s)x===1&&a.push(g.split("_").map(Number));if(!a.length)return[];let u=new Map;for(let[g,x]of a)(u.get(g)||u.set(g,[]).get(g)).push(x),(u.get(x)||u.set(x,[]).get(x)).push(g);let d=a[0][0],f=[],c=new Set,l=d,h=-1;do{f.push(n[l]),c.add(l);let g=u.get(l)||[],x=g.find(y=>y!==h&&!c.has(y));if(x===void 0&&(x=g.find(y=>y!==h)),x===void 0)break;h=l,l=x}while(l!==d&&f.length<=a.length+1);return f.length>0&&f.push(f[0]),f}import*as Fo from"three";var ea=(t,e,n=null)=>{if(t.length===0)return;let o=[],r=new Fo.Vector3(e[0].x,e[0].y,e[0].z),s=0;for(let f=1;f<e.length;f++)s+=e[f].z,e[f].z>r.z&&r.set(e[f].x,e[f].y,e[f].z);let i=s/(e.length-1)+.3,a=0,u=0;for(let f of t){let c=f[1].points;if(c.length<30)continue;let l=new Map;for(let x=0;x<c.length;x++){let y=c[x].z;if(y=Math.floor(y*10)/10,l.has(y)){let m=l.get(y);m.num+=1,m.points.push(c[x]),l.set(y,m)}else l.set(y,{num:1,points:[c[x]]})}c=[...l.entries()].reduce((x,y)=>y[1].num>x[1].num?y:x)[1].points.map(x=>new Fo.Vector3(x.x,x.y,x.z));let g=[];if(n!=null){let x=n([c],{tolerance:.1,minClusterSize:30,maxClusterSize:1e7,threadCount:1});if(x.length>0)for(let y of x[0]){if(y.length<50)continue;let m=y.map(p=>c[p]);g.push(m)}else g.push(c)}else g.push(c);for(let x=0;x<g.length;x++){let{minZ:y,maxZ:m}=Ve(g[x]),p=m-y;if(i>m)continue;let b=g[x].map(P=>[P.x,P.y]),w=de(b);if(zr(w)<.5)continue;let M={contour:w,averagePz:y};g[x].length>a&&(a=g[x].length,u=y),o.push(M)}}let d=[];for(let f of o)(f.averagePz>=u||f.averagePz>=i-.5)&&d.push(f);return d};function Xe(t,e){let n=t.x-e.x,o=t.y-e.y;return Math.sqrt(n*n+o*o)}function Ze(t){let e=0,n=0,o=0;for(let r=0;r<4;r++){let s=t[r],i=t[(r+1)%4],a=i.x-s.x,u=i.y-s.y,d=Math.sqrt(a*a+u*u);d>e&&(e=d,n=a/d,o=u/d)}return{longSide:e,dirX:n,dirY:o}}function Ge(t,e,n){let r=[];for(let s of t){let i=(s.start.x+s.end.x)/2,a=(s.start.y+s.end.y)/2,u=i-e,d=a-n;u*u+d*d<=9&&r.push(s.start.z,s.end.z)}if(r.length===0){let s=[];for(let i of t)s.push(i.start.z,i.end.z);return s.length?(s.sort((i,a)=>i-a),s[Math.floor(s.length/2)]):-1/0}return r.sort((s,i)=>s-i),r[Math.floor(r.length/2)]}function Ce(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-12)return{dist:Math.sqrt((t-n)**2+(e-o)**2),t:0};let d=t-n,f=e-o,c=(d*i+f*a)/u,l=n+c*i,h=o+c*a;return{dist:Math.sqrt((t-l)**2+(e-h)**2),t:c}}function No(t,e,n){let o=0;for(let r=0;r<4;r++){let s=n[r],i=n[(r+1)%4],a=(i.x-s.x)*(e-s.y)-(i.y-s.y)*(t-s.x);if(Math.abs(a)<1e-12)continue;let u=a>0?1:-1;if(o===0)o=u;else if(o!==u)return!1}return!0}function Es(t,e,n,o,r,s,i,a){let u=n-t,d=o-e,f=i-r,c=a-s,l=u*c-d*f;if(Math.abs(l)<1e-12)return null;let h=((r-t)*c-(s-e)*f)/l,g=((r-t)*d-(s-e)*u)/l;return h<-1e-12||h>1+1e-12||g<-1e-12||g>1+1e-12?null:Math.max(0,Math.min(1,h))}function Ps(t,e,n,o,r){let s=[];No(t,e,r)&&s.push(0),No(n,o,r)&&s.push(1);for(let u=0;u<4;u++){let d=r[u],f=r[(u+1)%4],c=Es(t,e,n,o,d.x,d.y,f.x,f.y);c!==null&&s.push(c)}if(s.length<2)return 0;s.sort((u,d)=>u-d);let i=Math.sqrt((n-t)**2+(o-e)**2),a=0;for(let u=0;u<s.length-1;u++){let d=s[u],f=s[u+1],c=(d+f)/2,l=t+c*(n-t),h=e+c*(o-e);No(l,h,r)&&(a+=(f-d)*i)}return a}function Ts(t,e,n=1e-6){let o=[],r=t.start.x,s=t.start.y,i=t.end.x,a=t.end.y;for(let f of e){if(f===t)continue;let c=Es(r,s,i,a,f.start.x,f.start.y,f.end.x,f.end.y);c!==null&&c>n&&c<1-n&&o.push(c)}o.sort((f,c)=>f-c);let u=[],d=-1/0;for(let f of o)f-d>n&&(u.push(f),d=f);return u}function Ss(t,e){if(!e.length)return[t];let n=[],o=0;for(let s of e){let i=t.start.x+s*(t.end.x-t.start.x),a=t.start.y+s*(t.end.y-t.start.y),u=t.start.z+s*(t.end.z-t.start.z),d=o===0?{...t.start}:{x:t.start.x+o*(t.end.x-t.start.x),y:t.start.y+o*(t.end.y-t.start.y),z:t.start.z+o*(t.end.z-t.start.z)},f={...t,start:d,end:{x:i,y:a,z:u},length:t.length*(s-o)};f.drawWindow=void 0,f.isWindow=!1,n.push(f),o=s}let r={...t,start:{x:t.start.x+o*(t.end.x-t.start.x),y:t.start.y+o*(t.end.y-t.start.y),z:t.start.z+o*(t.end.z-t.start.z)},end:{...t.end},length:t.length*(1-o)};return r.drawWindow=void 0,r.isWindow=!1,n.push(r),n}function na(t,e,n=.15){let o=0,r=0;for(let i of e)i!==t&&((Xe(t.start,i.start)<=n||Xe(t.start,i.end)<=n)&&o++,(Xe(t.end,i.start)<=n||Xe(t.end,i.end)<=n)&&r++);let s=0;return o>=1&&r>=1?s=5:(o>=1||r>=1)&&(s=2),{left:o,right:r,score:s}}function oa(t,e){let n=[],{start:o,end:r}=t;for(let a of e){let{dist:u}=Ce(a.x,a.y,o.x,o.y,r.x,r.y);n.push(u)}let s=n.reduce((a,u)=>a+u,0)/n.length,i=n.reduce((a,u)=>a+(u-s)**2,0)/n.length;return i<.01?3:i<.05?2:i<.1?1:0}function ra(t,e,n=.15){let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=Math.sqrt(o*o+r*r);if(s<1e-12)return 0;let i=-r/s,a=o/s,u=[[],[]];for(let c=0;c<2;c++){let l=c===0?t.start:t.end;for(let h of e){if(h===t)continue;let g=Xe(l,h.start),x=Xe(l,h.end),y=null;if(g<=n?y=h.end:x<=n&&(y=h.start),!y)continue;let m=h.end.x-h.start.x,p=h.end.y-h.start.y,b=Math.sqrt(m*m+p*p);if(b<1e-12||Math.abs(o*m+r*p)/(s*b)>=.342)continue;let z=(y.x-l.x)*i+(y.y-l.y)*a;u[c].push(z>0?1:-1)}}if(u[0].length===0||u[1].length===0)return 0;let d=[...u[0],...u[1]];return d.every(c=>c===d[0])?2:1}function Rs(t,e,n){let o=na(t,n),r=oa(t,e),s=ra(t,n);return{score:o.score*3+r*2+s*2,details:{conn:o.score,variance:r,sameSide:s}}}function sa(t){let e=t.end.x-t.start.x,n=t.end.y-t.start.y,o=Math.atan2(n,e)*(180/Math.PI);return o<0&&(o+=180),o}function ia(t,e){let n=[];for(let o of t){let r=!1;for(let s of n){let i=Math.abs(o.angle-s[0].angle);if(Math.min(i,180-i)<=e){s.push(o),r=!0;break}}r||n.push([o])}return n}function la(t,e=.05){let n=[];for(let o of t){let r=!1;for(let s of n)if(Math.abs(o.offset-s[0].offset)<=e){s.push(o),r=!0;break}r||n.push([o])}return n}function aa(t,e,n,o,r){let s=[];for(let d of t){let f=d.seg,c=(f.start.x-e.start.x)*n+(f.start.y-e.start.y)*o,l=(f.end.x-e.start.x)*n+(f.end.y-e.start.y)*o;c<=l?s.push({l:c,r:l,idx:d.idx,startPt:f.start,endPt:f.end}):s.push({l,r:c,idx:d.idx,startPt:f.end,endPt:f.start})}s.sort((d,f)=>d.l-f.l);let i=[],a=.2,u=.5;for(let{l:d,r:f,idx:c,startPt:l,endPt:h}of s){if(i.length===0){i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h});continue}let g=i[i.length-1];if(d<=g.r+r){let x=Math.min(g.r,f)-Math.max(g.l,d),y=Math.hypot(g.endPt.x-l.x,g.endPt.y-l.y);x>=u||y<=a?(d<g.l&&(g.l=d,g.startPt=l),f>g.r&&(g.r=f,g.endPt=h),g.srcIdxs.includes(c)||g.srcIdxs.push(c)):i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}else i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}return i}function ca(t,e,n,o,r,s,i){let a=Math.hypot(n.x-e.x,n.y-e.y),u={...t};return u.start={...e},u.end={...n},u.length=a,u.direction={x:o,y:r,z:0},s!==void 0&&(u.rooftopPz=s),u.buildRosource=i,u.isRebuild=!1,u.doorAndBeamData||(u.doorAndBeamData=[]),u.insetionArr||(u.insetionArr=[]),u}function ua(t,e){let n=!0;for(;n;){n=!1;for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o],i=t[r];if(s.rooftopPz!==void 0&&i.rooftopPz!==void 0&&Math.abs(s.rooftopPz-i.rooftopPz)>.05)continue;let a=Math.abs(s.end.x-i.start.x)<.001&&Math.abs(s.end.y-i.start.y)<.001,u=Math.abs(s.start.x-i.end.x)<.001&&Math.abs(s.start.y-i.end.y)<.001,d=Math.abs(s.end.x-i.end.x)<.001&&Math.abs(s.end.y-i.end.y)<.001,f=Math.abs(s.start.x-i.start.x)<.001&&Math.abs(s.start.y-i.start.y)<.001;if(!(a||u||d||f)||s.direction.x*i.direction.x+s.direction.y*i.direction.y<.98)continue;let l=s.start,h=s.end;a?h=i.end:u?l=i.start:d?h=i.start:f&&(l=i.end);let g=Math.hypot(h.x-l.x,h.y-l.y),x={...s};x.start=l,x.end=h,x.length=g,x.direction={x:(h.x-l.x)/g,y:(h.y-l.y)/g,z:0},s.rooftopPz!==void 0&&i.rooftopPz!==void 0?x.rooftopPz=Math.max(s.rooftopPz,i.rooftopPz):x.rooftopPz=s.rooftopPz!==void 0?s.rooftopPz:i.rooftopPz,x.buildRosource=s.buildRosource||i.buildRosource,x.isRebuild=!1,x.doorAndBeamData||(x.doorAndBeamData=[]),x.insetionArr||(x.insetionArr=[]),t[o]=x,e[o]=[...new Set([...e[o],...e[r]])],t.splice(r,1),e.splice(r,1),r--,n=!0}}return{segments:t,sourceMap:e}}function Ye(t,e=5,n=.05){if(t.length<=1)return{segments:t,sourceMap:t.map((u,d)=>[d])};let o=t.map((u,d)=>({seg:u,angle:sa(u),idx:d}));o.sort((u,d)=>u.angle-d.angle);let r=ia(o,e),s=[],i=[],a=[];for(let u of r){let d=u[0].seg;for(let m of u)m.seg.length>d.length&&(d=m.seg);let f=d.end.x-d.start.x,c=d.end.y-d.start.y,l=Math.sqrt(f*f+c*c);if(l<1e-12)continue;let h=-c/l,g=f/l,x=u.map(m=>{let p=m.seg.start.x-d.start.x,b=m.seg.start.y-d.start.y;return{seg:m.seg,offset:Math.abs(p*h+b*g),idx:m.idx}});x.sort((m,p)=>m.offset-p.offset);let y=la(x);for(let m of y){let p=m[0].seg;for(let T of m)T.seg.length>p.length&&(p=T.seg);let b=p.end.x-p.start.x,w=p.end.y-p.start.y,z=Math.sqrt(b*b+w*w);if(z<1e-12)continue;let M=b/z,P=w/z,E=m.map(T=>T.seg.rooftopPz).filter(T=>T!==void 0),S=E.length>0?Math.max(...E):void 0,I=[...new Set(E.map(T=>T.toFixed(3)))];if(I.length>1){let T=m.map(R=>({idx:R.idx,start:{x:R.seg.start.x,y:R.seg.start.y,z:R.seg.start.z},end:{x:R.seg.end.x,y:R.seg.end.y,z:R.seg.end.z},length:R.seg.length,rooftopPz:R.seg.rooftopPz}));a.push({values:[...I],segments:T})}let W=[...new Set(m.map(T=>String(T.seg.buildRosource)))],v=aa(m,p,M,P,n);for(let{l:T,r:R,srcIdxs:D,startPt:H,endPt:V}of v){if(R-T<.01)continue;let C=ca(p,H,V,M,P,S,p.buildRosource);s.push(C),i.push(D)}}}return{...ua(s,i),debugRPZGroups:a}}function fa(t){return{minLengthRatio:.95,maxDistance:.5,angleThreshold:45,projectionMargin:.1,printOnly:!0,minEmbedRatioThreshold:.3,noWallSurroundDistance:.3,minEmbedRatio:.05,...t}}function da(t,e,n){return[{x:e,y:n,z:0},{x:t[0].x,y:t[0].y,z:0},{x:t[1].x,y:t[1].y,z:0},{x:t[2].x,y:t[2].y,z:0},{x:t[3].x,y:t[3].y,z:0}]}function ha(t,e,n,o,r,s,i,a,u){if(!["1527725434500","1527725386500","1527726772000","1527725058500"].some(c=>t.includes(c)))return;e.map(c=>{let{start:l,end:h}=c,g=Ce(n,o,l.x,l.y,h.x,h.y),x=r.map(z=>{let M=Ce(z.x,z.y,l.x,l.y,h.x,h.y);return`t=${M.t.toFixed(3)},d=${M.dist.toFixed(3)}`}),y=h.x-l.x,m=h.y-l.y,p=Math.sqrt(y*y+m*m)||1,b=Math.abs(i*(y/p)+a*(m/p)),w=Math.acos(Math.min(b,1))*(180/Math.PI);return{seg:c,lineDist:g.dist,t:g.t,pts:x,angle:w,length:c.length}}).sort((c,l)=>c.lineDist-l.lineDist)}function ma(t,e,n,o,r,s,i){let a=[{margin:s.projectionMargin,maxDist:s.maxDistance,label:"\u4E25\u683C"},{margin:.4,maxDist:.65,label:"\u5BBD\u677E"}],u=[];for(let d of a){if(u.length>0)break;for(let f of t){let c=!1,l=!1,h=!1,g=e*s.minLengthRatio;if(f.length<g){continue}c=!0;let{start:x,end:y}=f,m=!1,p=1/0;for(let v of r){let{dist:T,t:R}=Ce(v.x,v.y,x.x,x.y,y.x,y.y);R>=-d.margin&&R<=1+d.margin&&T<=d.maxDist&&(m=!0,p=Math.min(p,T))}if(!m){`${d.maxDist}${(-d.margin).toFixed(1)}${(1+d.margin).toFixed(1)}`;continue}l=!0;let b=Ce(r[0].x,r[0].y,x.x,x.y,y.x,y.y);if(b.t<0||b.t>1){`${b.t.toFixed(3)}`;continue}let w=Math.sqrt((y.x-x.x)**2+(y.y-x.y)**2);if(w<1e-12)continue;let z=(y.x-x.x)/w,M=(y.y-x.y)/w,P=Math.abs(n*z+o*M),E=Math.acos(Math.min(P,1))*(180/Math.PI);if(E>s.angleThreshold){`${E.toFixed(1)}${s.angleThreshold}`;continue}h=!0;let S=[r[1],r[2],r[3],r[4]],I=Ps(x.x,x.y,y.x,y.y,S),W=e>0?I/e:0;u.push({seg:f,minDist:p,embedRatio:W,ratio:f.length/e,score:0,details:{conn:0,variance:0,sameSide:0}})}}return u}function pa(t,e,n,o){if(t.length<=1)return t.length===1&&(t[0].score=-1),t;for(let r of t){let{score:s,details:i}=Rs(r.seg,e,n);r.score=s,r.details=i}return t.sort((r,s)=>s.embedRatio-r.embedRatio||s.score-r.score||r.minDist-s.minDist),t}function ga(t,e,n,o){let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=Math.sqrt(r*r+s*s),a=r/i,u=s/i,d=e.map(P=>{let E=P.x-t.start.x,S=P.y-t.start.y;return(E*a+S*u)/i}),f=Math.min(...d),c=Math.max(...d),l=(c-f)*i,h=(f+c)/2,g=t.start.x+h*r,x=t.start.y+h*s,y=n.box.min.z,m=n.box.max.z,p=Math.abs(m-y),b=Ge(o,t.start.x,t.start.y),w=Math.max(0,y-b),z=f<=0&&c>=1,M=t.start.z+h*(t.end.z-t.start.z);return{p:{x:g,y:x,z:M},width:l,full:z,height:p,groundClearance:w}}function qo(t,e,n){let o=e.map(f=>n[f]),r=[],s=[],i=[],a=[],u,d;for(let f of o){if(f.originalPoints?.length)for(let c of f.originalPoints)r.push(c);if(f.doorAndBeamData?.length)for(let c of f.doorAndBeamData)s.push(c);if(f.insetionArr?.length)for(let c of f.insetionArr)i.push(c);if(f.drawWindow?.length)for(let c of f.drawWindow)a.push(c);f.boxData&&!u&&(u=f.boxData),f.rooftopPz!==void 0&&(d===void 0||f.rooftopPz>d)&&(d=f.rooftopPz)}r.length&&(t.originalPoints=r),s.length&&(t.doorAndBeamData=s),i.length&&(t.insetionArr=i),a.length&&(t.drawWindow=a),u&&(t.boxData=u),d!==void 0&&(t.rooftopPz=d)}function Tn(t,e,n=10,o=.2,r=.2){let s=[];if(!t.drawWindow)return s;let i=t.drawWindow.p,a=t.wallSegment,u=a.end.x-a.start.x,d=a.end.y-a.start.y,f=Math.sqrt(u*u+d*d)||1,c=u/f,l=d/f,h=i.x-a.start.x,g=i.y-a.start.y,x=(h*c+g*l)/f;for(let y of e){if(y===a)continue;let m=y.end.x-y.start.x,p=y.end.y-y.start.y,b=Math.sqrt(m*m+p*p)||1,w=m/b,z=p/b,M=Math.abs(c*w+l*z),E=Math.acos(Math.min(M,1))*(180/Math.PI);if(Math.abs(E-90)>n)continue;let S=[y.start,y.end];for(let I of S){let W=I.x-a.start.x,v=I.y-a.start.y,T=(W*c+v*l)/f,R=a.start.x+T*f*c,D=a.start.y+T*f*l;if(Math.sqrt((I.x-R)**2+(I.y-D)**2)>r)continue;let V=Math.abs((T-x)*f);if(V<=o){s.push({seg:y,distAlongWall:V,angle:E,endPoint:I});break}}}return s}function Sn(t,e){let n=t.start,o=t.end,r=o.x-n.x,s=o.y-n.y,i=Math.sqrt(r*r+s*s)||1,a=((e.x-n.x)*r+(e.y-n.y)*s)/(i*i),u=Math.max(0,Math.min(1,a)),d={x:n.x+u*r,y:n.y+u*s,z:n.z+(o.z-n.z)*u||0};function f(b){if(!b||!b.length)return[[],[]];let w=[],z=[];for(let M of b){let P=((M.x-n.x)*r+(M.y-n.y)*s)/(i*i);P<=u+1e-8&&w.push(M),P>=u-1e-8&&z.push(M)}return(w.length===0||c(w[w.length-1],d)>1e-6)&&w.push({...d}),(z.length===0||c(z[0],d)>1e-6)&&z.unshift({...d}),[w,z]}function c(b,w){return Math.sqrt((b.x-w.x)**2+(b.y-w.y)**2)}let[l,h]=f(t.originalPoints),g=Math.max(u*i,1e-6),x=Math.max((1-u)*i,1e-6),y=t.direction||{x:r/i,y:s/i,z:(o.z-n.z)/i||0},m={...t,start:{...n},end:{...d},length:g,direction:{...y},originalPoints:l.length?l:void 0},p={...t,start:{...d},end:{...o},length:x,direction:{...y},originalPoints:h.length?h:void 0};if(t.drawWindow&&t.drawWindow.length>0){let b=[],w=[];for(let z of t.drawWindow){let M=((z.p.x-n.x)*r+(z.p.y-n.y)*s)/(i*i);M<=u+1e-8&&b.push(z),M>=u-1e-8&&w.push(z)}m.drawWindow=b.length?b:void 0,p.drawWindow=w.length?w:void 0}else m.drawWindow=void 0,p.drawWindow=void 0;if(t.boxData){let b=w=>{if(!w.length)return;let z=1/0,M=1/0,P=1/0,E=-1/0,S=-1/0,I=-1/0;for(let W of w)W.x<z&&(z=W.x),W.y<M&&(M=W.y),W.z!==void 0&&W.z<P&&(P=W.z),W.x>E&&(E=W.x),W.y>S&&(S=W.y),W.z!==void 0&&W.z>I&&(I=W.z);return{isBox3:!0,min:{x:z,y:M,z:P},max:{x:E,y:S,z:I}}};m.boxData=b(l),p.boxData=b(h)}return[m,p]}function Rn(t,e,n,o){let r=fa(n),s=[],{segments:i,sourceMap:a}=o??Ye(e),u=i;for(let d of t){let f=d.coordinatesByArea?.coordinates;if(!f||f.length<4)continue;let c=f.slice(0,4),l=c.reduce((I,W)=>I+W.x,0)/4,h=c.reduce((I,W)=>I+W.y,0)/4,{longSide:g,dirX:x,dirY:y}=Ze(c);if(g<.01)continue;let m=c.reduce((I,W)=>I+W.z,0)/4,p=Ge(e,l,h);if(m<p-.3)continue;let b=da(c,l,h);ha(d.name,u,l,h,b,g,x,y,r);let w=ma(u,g,x,y,b,r,d.name);if(w.length>1&&r.minEmbedRatioThreshold>0){let I=Math.max(...w.map(W=>W.embedRatio));if(I>0){let W=w.length;w=w.filter(v=>v.embedRatio>=I*r.minEmbedRatioThreshold)}}if(w.length>0&&r.minEmbedRatio>0){let I=w.length;w=w.filter(W=>W.embedRatio>=r.minEmbedRatio)}w=pa(w,b,e,d.name);let z=null,M=1/0,P=0,E=0;if(w.length>0){let I=w[0];z=I.seg,M=I.minDist,P=I.ratio,E=I.embedRatio}if(z&&E<=0&&r.noWallSurroundDistance>0){let I=r.noWallSurroundDistance,W=c.map(C=>C.x),v=c.map(C=>C.y),T=Math.min(...W)-I,R=Math.max(...W)+I,D=Math.min(...v)-I,H=Math.max(...v)+I,V=!1;for(let C of u){let _=Math.min(C.start.x,C.end.x),A=Math.max(C.start.x,C.end.x),L=Math.min(C.start.y,C.end.y),k=Math.max(C.start.y,C.end.y);if(!(A<T||_>R)&&!(k<D||L>H)){V=!0;break}}V||(z=null)}let S;if(z&&(S=ga(z,c,d,i),!r.printOnly)){let I=z;I.drawWindow||(I.drawWindow=[]),I.drawWindow.push(S),I.isWindow=!0}if(z){let I={windowName:d.name,windowCategory:d.category,windowCenter:d.center,wallSegment:z,distance:M,wallLengthRatio:P,drawWindow:S};s.push(I)}}for(let d of s){let f=d.wallSegment,c=i.indexOf(f);if(c===-1)continue;let l=a[c];!l||l.length<=1||qo(f,l,e)}return s}function jo(t,e,n){let o={printOnly:!1,skipMerge:!1,...n};for(let y of t)y.drawWindow&&(y.drawWindow=void 0),y.isWindow&&(y.isWindow=void 0);let r=Array.isArray(e)?e.filter(y=>y&&y.category==="window"&&!y.isBayWindowObj):[],s,i;if(o.skipMerge)s=t,i=t.map((y,m)=>[m]);else{let y=Ye(t);s=y.segments,i=y.sourceMap}let a=Rn(r,t,o,{segments:s,sourceMap:i}),u=new Set;for(let y of a){let m=s.indexOf(y.wallSegment);m>=0&&u.add(m)}let d=[],f=new Set;for(let y of u){let m=i[y];if(m){qo(s[y],m,t);for(let p of m)f.add(p)}d.push(s[y])}for(let y=0;y<t.length;y++)f.has(y)||d.push(t[y]);let c=new Set(a.map(y=>y.windowName));for(let y of e)y&&y.category==="window"&&!y.isBayWindowObj&&(c.has(y.name)?delete y.AbnormalWindow:y.AbnormalWindow=!0);let l=new Map;for(let y of e)y&&y.name&&l.set(y.name,y);let h=new Map;for(let y of a){let m=y.wallSegment;h.has(m)||h.set(m,[]),h.get(m).push(y)}let g=new Map,x=d;for(let[y,m]of h){if(!y.drawWindow||!y.drawWindow.length||g.has(y))continue;let p=y.length,b=y.end.x-y.start.x,w=y.end.y-y.start.y,z=[];for(let T of m){if(!T.drawWindow)continue;let R=T.drawWindow.width/2,D=((T.drawWindow.p.x-y.start.x)*b+(T.drawWindow.p.y-y.start.y)*w)/(p*p),H=R/p;z.push({left:D-H,right:D+H,match:T})}if(z.length===0)continue;let M=Ts(y,x),P=1e-6,E=new Set,S=!1;for(let T of z){let R=!1,D=!1,H=null;for(let C of M)C<T.left-P&&((H===null||C>H)&&(H=C),R=!0);let V=null;for(let C of M)C>T.right+P&&((V===null||C<V)&&(V=C),D=!0);H!==null&&(E.add(H),S=!0),V!==null&&(E.add(V),S=!0)}if(!S)continue;let I=!1,W=null,v=null;for(let T of z){let R=T.match;if(!R.drawWindow||R.drawWindow.width<=1)continue;let D=l.get(R.windowName);if(!D||!D.positionArr||!D.positionArr.length)continue;let H=Tn(R,s);if(H.length<=1)continue;let V=D.positionArr[0],C=R.drawWindow.p,_=V.x-C.x,A=V.y-C.y,L=Math.sqrt(_*_+A*A);if(L<.001)continue;let k=_/L,F=A/L,B=H.filter(O=>{let $=(O.seg.start.x+O.seg.end.x)/2,G=(O.seg.start.y+O.seg.end.y)/2,Q=$-C.x,j=G-C.y;return Q*k+j*F>0});if(B.length){I=!0,W=B[0].endPoint,v=R;break}}if(I&&W&&v){let[T,R]=Sn(y,W);g.set(y,[T,R]);let D=((v.drawWindow.p.x-y.start.x)*b+(v.drawWindow.p.y-y.start.y)*w)/(p*p),H=((W.x-y.start.x)*b+(W.y-y.start.y)*w)/(p*p);D<=H?v.wallSegment=T:v.wallSegment=R}else{let T=Array.from(E).sort((D,H)=>D-H),R=Ss(y,T);for(let D of z){let H=(D.left+D.right)/2,V=0;for(let C of R){let _=C.length/p;if(H>=V-P&&H<=V+_+P){let A=D.match.drawWindow;A&&(C.drawWindow||(C.drawWindow=[]),C.drawWindow.push(A),C.isWindow=!0),D.match.wallSegment=C;break}V+=_}}g.set(y,R)}}if(g.size>0){let y=[];for(let m of d)g.has(m)?y.push(...g.get(m)):y.push(m);for(let m of y)m.isWindow===void 0&&(m.isWindow=!!(m.drawWindow&&m.drawWindow.length>0));return{segments:y,matches:a}}for(let y of d)y.isWindow=!!(y.drawWindow&&y.drawWindow.length>0);return{segments:d,matches:a}}function Ue(t,e){let n=t.x-e.x,o=t.y-e.y;return Math.sqrt(n*n+o*o)}function no(t){let e=0,n=1/0,o=0,r=0;for(let s=0;s<4;s++){let i=t[s],a=t[(s+1)%4],u=a.x-i.x,d=a.y-i.y,f=Math.sqrt(u*u+d*d);f>e&&(e=f,o=u/f,r=d/f),f<n&&(n=f)}return{longSide:e,shortSide:n===1/0?e:n,dirX:o,dirY:r}}function Ke(t,e,n){let r=[];for(let s of t){let i=(s.start.x+s.end.x)/2,a=(s.start.y+s.end.y)/2,u=i-e,d=a-n;u*u+d*d<=9&&r.push(s.start.z,s.end.z)}if(r.length===0){let s=[];for(let i of t)s.push(i.start.z,i.end.z);return s.length?(s.sort((i,a)=>i-a),s[Math.floor(s.length/2)]):-1/0}return r.sort((s,i)=>s-i),r[Math.floor(r.length/2)]}function jt(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-12)return{dist:Math.sqrt((t-n)**2+(e-o)**2),t:0};let d=t-n,f=e-o,c=(d*i+f*a)/u,l=n+c*i,h=o+c*a;return{dist:Math.sqrt((t-l)**2+(e-h)**2),t:c}}function $o(t,e,n){let o=0;for(let r=0;r<4;r++){let s=n[r],i=n[(r+1)%4],a=(i.x-s.x)*(e-s.y)-(i.y-s.y)*(t-s.x);if(Math.abs(a)<1e-12)continue;let u=a>0?1:-1;if(o===0)o=u;else if(o!==u)return!1}return!0}function xa(t,e,n,o,r,s,i,a){let u=n-t,d=o-e,f=i-r,c=a-s,l=u*c-d*f;if(Math.abs(l)<1e-12)return null;let h=((r-t)*c-(s-e)*f)/l,g=((r-t)*d-(s-e)*u)/l;return h<-1e-12||h>1+1e-12||g<-1e-12||g>1+1e-12?null:Math.max(0,Math.min(1,h))}function vs(t,e,n,o,r){let s=[];$o(t,e,r)&&s.push(0),$o(n,o,r)&&s.push(1);for(let u=0;u<4;u++){let d=r[u],f=r[(u+1)%4],c=xa(t,e,n,o,d.x,d.y,f.x,f.y);c!==null&&s.push(c)}if(s.length<2)return 0;s.sort((u,d)=>u-d);let i=Math.sqrt((n-t)**2+(o-e)**2),a=0;for(let u=0;u<s.length-1;u++){let d=s[u],f=s[u+1],c=(d+f)/2,l=t+c*(n-t),h=e+c*(o-e);$o(l,h,r)&&(a+=(f-d)*i)}return a}function Vs(t){if(!t||t.length<4)return[];let e=[...t].sort((i,a)=>i.z-a.z),n=e.slice(0,2),o=e.slice(2,4),r=[],s=new Set;for(let i of n){let a=-1,u=1/0;for(let d=0;d<o.length;d++){if(s.has(d))continue;let f=o[d].x-i.x,c=o[d].y-i.y,l=Math.sqrt(f*f+c*c);l<u&&(u=l,a=d)}if(a>=0){s.add(a);let d=o[a];r.push({bottom:i,top:d,midX:(i.x+d.x)/2,midY:(i.y+d.y)/2,height:Math.abs(d.z-i.z)})}}return r}function Ds(t,e,n){return jt(t.midX,t.midY,e.x,e.y,n.x,n.y)}function Is(t,e,n,o=.08){if(!t||t.length<2)return null;let r=Ds(t[0],e,n),s=Ds(t[1],e,n);if(r.dist<=o&&s.dist<=o){let i=r.dist<=s.dist?0:1,a=1-i;return{hingeEdge:t[i],otherEdge:t[a],hingeDist:i===0?r.dist:s.dist,otherDist:a===0?r.dist:s.dist,isClosed:!0,hingeT:i===0?r.t:s.t}}if(r.dist<=o||s.dist<=o){let i=r.dist<=o?0:1,a=1-i;return{hingeEdge:t[i],otherEdge:t[a],hingeDist:i===0?r.dist:s.dist,otherDist:a===0?r.dist:s.dist,isClosed:!1,hingeT:i===0?r.t:s.t}}return null}function Cs(t){if(!t||t.length<2)return 0;let e=t[0].midX-t[1].midX,n=t[0].midY-t[1].midY;return Math.sqrt(e*e+n*n)}function Xo(t){if(!t||!t.start||t.start.x==null||t.start.y==null||t.start.z==null||!t.end||t.end.x==null||t.end.y==null||t.end.z==null)return null;let e=t.start,n=t.end,o=n.x-e.x,r=n.y-e.y,s=Math.sqrt(o*o+r*r)||1,i=0,a=0;return t.centerDir?(i=t.centerDir.x,a=t.centerDir.y):(i=-r/s,a=o/s),{p1:e,p2:n,dirX:o/s,dirY:r/s,length:s,midX:(e.x+n.x)/2,midY:(e.y+n.y)/2,normalX:i,normalY:a}}function Hs(t,e,n,o=.08){let r=Xo(t);if(!r)return null;let s=jt(r.p1.x,r.p1.y,e.x,e.y,n.x,n.y),i=jt(r.p2.x,r.p2.y,e.x,e.y,n.x,n.y);if(s.dist<=o&&i.dist<=o){let a=s.dist<=i.dist;return{hingePoint:a?r.p1:r.p2,otherPoint:a?r.p2:r.p1,hingeDist:a?s.dist:i.dist,otherDist:a?i.dist:s.dist,isClosed:!0,hingeT:a?s.t:i.t}}if(s.dist<=o||i.dist<=o){let a=s.dist<=o;return{hingePoint:a?r.p1:r.p2,otherPoint:a?r.p2:r.p1,hingeDist:a?s.dist:i.dist,otherDist:a?i.dist:s.dist,isClosed:!1,hingeT:a?s.t:i.t}}return null}function oo(t,e=.1){let n=t.originalPoints;if(!n||n.length===0)return{densityProfile:[],binStartT:[],binEndT:[],avgDensity:0,maxDensity:0,minDensity:0,binSize:e};let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=t.length;if(s<=0)return{densityProfile:[],binStartT:[],binEndT:[],avgDensity:0,maxDensity:0,minDensity:0,binSize:e};let i=[];for(let y of n){let m=y.x-t.start.x,p=y.y-t.start.y,b=(m*o+p*r)/(s*s);i.push(Math.max(0,Math.min(1,b)))}let a=Math.max(1,Math.ceil(s/e)),u=s/a,d=new Array(a).fill(0);for(let y of i){let m=Math.min(a-1,Math.floor(y*a));d[m]++}let f=d.map(y=>y/u),c=d.map((y,m)=>m/a),l=d.map((y,m)=>(m+1)/a),h=f.reduce((y,m)=>y+m,0)/a,g=Math.max(...f),x=Math.min(...f);return{densityProfile:f,binStartT:c,binEndT:l,avgDensity:h,maxDensity:g,minDensity:x,binSize:u}}function Ws(t){let{densityProfile:e,avgDensity:n}=t;if(e.length===0||n===0)return 0;let o=e.length,r=e.reduce((i,a)=>i+(a-n)**2,0)/o;return Math.sqrt(r)/n}function _s(t){let e=[];for(let o of t)if(o.originalPoints&&o.originalPoints.length>=50&&o.length>=.5){let r=oo(o,.1);r.avgDensity>0&&e.push(r.avgDensity)}e.sort((o,r)=>o-r);let n=e.length>0?e[Math.floor(e.length/2)]:1;return{sortedDensities:e,medianDensity:n}}function Zo(t,e){let{sortedDensities:n}=e;if(n.length===0)return .5;if(t<=0)return 1;let o=0;for(let r of n)r<t&&o++;return 1-o/n.length}function As(t,e,n=.5,o=.5,r=3){let{densityProfile:s,avgDensity:i}=t;if(s.length===0||i===0)return[];let a=i*n,u=s.length,d=[],f=!1,c=0,l=0,h=0;for(let g=0;g<u;g++){let x=s[g]<a;if(x&&!f)f=!0,c=g,l=s[g],h=1;else if(x&&f)l+=s[g],h++;else if(!x&&f){f=!1;let y=g-1,m=c/u,p=(y+1)/u,b=(p-m)*e;b>=o&&b<=r&&d.push({startT:m,endT:p,startLen:m*e,endLen:p*e,avgDensity:l/h}),l=0,h=0}}if(f){let g=u-1,x=c/u,y=(g+1)/u,m=(y-x)*e;m>=o&&m<=r&&d.push({startT:x,endT:y,startLen:x*e,endLen:y*e,avgDensity:l/h})}return d}function ya(t,e,n){let o=t.originalPoints;if(!o||o.length<5)return{verticalGapRatio:1,isDoorSized:!0};let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=t.length*t.length;if(i<=0)return{verticalGapRatio:0,isDoorSized:!1};let a=[];for(let m of o){let p=m.x-t.start.x,b=m.y-t.start.y,w=(p*r+b*s)/i;w>=e&&w<=n&&m.z!==void 0&&isFinite(m.z)&&a.push(m.z)}if(a.length<5)return{verticalGapRatio:1,isDoorSized:!0};let u=1/0,d=-1/0;for(let m of o)m.z!==void 0&&isFinite(m.z)&&(m.z<u&&(u=m.z),m.z>d&&(d=m.z));let f=d-u;if(f<.5)return{verticalGapRatio:0,isDoorSized:!1};let c=15,l=new Array(c).fill(!1);for(let m of a){let p=Math.min(c-1,Math.max(0,Math.floor((m-u)/f*c)));l[p]=!0}let h=0,g=0;for(let m=0;m<c;m++)l[m]?(h=Math.max(h,g),g=0):g++;h=Math.max(h,g);let x=h/c,y=x>=.35;return{verticalGapRatio:x,isDoorSized:y}}function Bs(t,e,n,o=.5,r=.5,s=3,i=.35){let a=As(e,n,o,r,s),u=[];for(let d of a){let{verticalGapRatio:f,isDoorSized:c}=ya(t,d.startT,d.endT);u.push({...d,verticalGapRatio:f,isRectangular:c})}return u}function Ls(t,e,n){if(!t||!t.start||!t.end)return null;let o=t.start,r=t.end,s=jt(o.x,o.y,e.x,e.y,n.x,n.y),i=jt(r.x,r.y,e.x,e.y,n.x,n.y),a=s.dist<=i.dist,u=a?o:r,d=a?s.dist:i.dist,f=jt(u.x,u.y,e.x,e.y,n.x,n.y);return{hingeT:Math.max(0,Math.min(1,f.t)),hingeDist:d}}function ks(t,e,n,o=2e3){let{hingeT:r}=t,s=.25,i=0,a=null,u=0;for(let d of e){let f=(d.startT+d.endT)/2,c=Math.abs(f-r),l=Math.max(0,1-c/s),h=d.isRectangular?1:.3,g=Math.max(0,1-d.avgDensity/o),x=l*.5+h*.3+g*.2;x>i&&(i=x,a=d,u=l)}return{score:i>.5?i:-1,bestRegion:a,bestOverlap:u,bestOpeningStartT:a?.startT??0,bestOpeningEndT:a?.endT??0}}function ba(t,e,n=.15){let o=0,r=0;for(let i of e)i!==t&&((Ue(t.start,i.start)<=n||Ue(t.start,i.end)<=n)&&o++,(Ue(t.end,i.start)<=n||Ue(t.end,i.end)<=n)&&r++);let s=0;return o>=1&&r>=1?s=5:(o>=1||r>=1)&&(s=2),{left:o,right:r,score:s}}function wa(t,e){let n=[],{start:o,end:r}=t;for(let a of e){let{dist:u}=jt(a.x,a.y,o.x,o.y,r.x,r.y);n.push(u)}let s=n.reduce((a,u)=>a+u,0)/n.length,i=n.reduce((a,u)=>a+(u-s)**2,0)/n.length;return i<.01?3:i<.05?2:i<.1?1:0}function za(t,e,n=.15){let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=Math.sqrt(o*o+r*r);if(s<1e-12)return 0;let i=-r/s,a=o/s,u=[[],[]];for(let c=0;c<2;c++){let l=c===0?t.start:t.end;for(let h of e){if(h===t)continue;let g=Ue(l,h.start),x=Ue(l,h.end),y=null;if(g<=n?y=h.end:x<=n&&(y=h.start),!y)continue;let m=h.end.x-h.start.x,p=h.end.y-h.start.y,b=Math.sqrt(m*m+p*p);if(b<1e-12||Math.abs(o*m+r*p)/(s*b)>=.342)continue;let z=(y.x-l.x)*i+(y.y-l.y)*a;u[c].push(z>0?1:-1)}}if(u[0].length===0||u[1].length===0)return 0;let d=[...u[0],...u[1]];return d.every(c=>c===d[0])?2:1}function Os(t,e,n){let o=ba(t,n),r=wa(t,e),s=za(t,n);return{score:o.score*3+r*2+s*2,details:{conn:o.score,variance:r,sameSide:s}}}function Ma(t){let e=t.end.x-t.start.x,n=t.end.y-t.start.y,o=Math.atan2(n,e)*(180/Math.PI);return o<0&&(o+=180),o}function Ea(t,e){let n=[];for(let o of t){let r=!1;for(let s of n){let i=Math.abs(o.angle-s[0].angle);if(Math.min(i,180-i)<=e){s.push(o),r=!0;break}}r||n.push([o])}return n}function Pa(t,e=.05){let n=[];for(let o of t){let r=!1;for(let s of n)if(Math.abs(o.offset-s[0].offset)<=e){s.push(o),r=!0;break}r||n.push([o])}return n}function Ta(t,e,n,o,r){let s=[];for(let d of t){let f=d.seg,c=(f.start.x-e.start.x)*n+(f.start.y-e.start.y)*o,l=(f.end.x-e.start.x)*n+(f.end.y-e.start.y)*o;c<=l?s.push({l:c,r:l,idx:d.idx,startPt:f.start,endPt:f.end}):s.push({l,r:c,idx:d.idx,startPt:f.end,endPt:f.start})}s.sort((d,f)=>d.l-f.l);let i=[],a=.01,u=.5;for(let{l:d,r:f,idx:c,startPt:l,endPt:h}of s){if(i.length===0){i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h});continue}let g=i[i.length-1];if(d<=g.r+r){let x=Math.min(g.r,f)-Math.max(g.l,d),y=Math.hypot(g.endPt.x-l.x,g.endPt.y-l.y);x>=u||y<=a?(d<g.l&&(g.l=d,g.startPt=l),f>g.r&&(g.r=f,g.endPt=h),g.srcIdxs.includes(c)||g.srcIdxs.push(c)):i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}else i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}return i}function Sa(t,e,n,o,r,s,i){let a=Math.hypot(n.x-e.x,n.y-e.y),u={...t};return u.start={...e},u.end={...n},u.length=a,u.direction={x:o,y:r,z:0},s!==void 0&&(u.rooftopPz=s),u.buildRosource=i,u.isRebuild=!1,u.doorAndBeamData||(u.doorAndBeamData=[]),u.insetionArr||(u.insetionArr=[]),u}function Ra(t,e){let n=!0;for(;n;){n=!1;for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o],i=t[r];if(s.rooftopPz!==void 0&&i.rooftopPz!==void 0&&Math.abs(s.rooftopPz-i.rooftopPz)>.05)continue;let a=Math.abs(s.end.x-i.start.x)<.001&&Math.abs(s.end.y-i.start.y)<.001,u=Math.abs(s.start.x-i.end.x)<.001&&Math.abs(s.start.y-i.end.y)<.001,d=Math.abs(s.end.x-i.end.x)<.001&&Math.abs(s.end.y-i.end.y)<.001,f=Math.abs(s.start.x-i.start.x)<.001&&Math.abs(s.start.y-i.start.y)<.001;if(!(a||u||d||f)||s.direction.x*i.direction.x+s.direction.y*i.direction.y<.98)continue;let l=s.start,h=s.end;a?h=i.end:u?l=i.start:d?h=i.start:f&&(l=i.end);let g=Math.hypot(h.x-l.x,h.y-l.y),x={...s};x.start=l,x.end=h,x.length=g,x.direction={x:(h.x-l.x)/g,y:(h.y-l.y)/g,z:0},s.rooftopPz!==void 0&&i.rooftopPz!==void 0?x.rooftopPz=Math.max(s.rooftopPz,i.rooftopPz):x.rooftopPz=s.rooftopPz!==void 0?s.rooftopPz:i.rooftopPz,x.buildRosource=s.buildRosource||i.buildRosource,x.isRebuild=!1,x.doorAndBeamData||(x.doorAndBeamData=[]),x.insetionArr||(x.insetionArr=[]),t[o]=x,e[o]=[...new Set([...e[o],...e[r]])],t.splice(r,1),e.splice(r,1),r--,n=!0}}return{segments:t,sourceMap:e}}function Qe(t,e=5,n=.05){if(t.length<=1)return{segments:t,sourceMap:t.map((u,d)=>[d])};let o=t.map((u,d)=>({seg:u,angle:Ma(u),idx:d}));o.sort((u,d)=>u.angle-d.angle);let r=Ea(o,e),s=[],i=[],a=[];for(let u of r){let d=u[0].seg;for(let m of u)m.seg.length>d.length&&(d=m.seg);let f=d.end.x-d.start.x,c=d.end.y-d.start.y,l=Math.sqrt(f*f+c*c);if(l<1e-12)continue;let h=-c/l,g=f/l,x=u.map(m=>{let p=m.seg.start.x-d.start.x,b=m.seg.start.y-d.start.y;return{seg:m.seg,offset:Math.abs(p*h+b*g),idx:m.idx}});x.sort((m,p)=>m.offset-p.offset);let y=Pa(x);for(let m of y){let p=m[0].seg;for(let T of m)T.seg.length>p.length&&(p=T.seg);let b=p.end.x-p.start.x,w=p.end.y-p.start.y,z=Math.sqrt(b*b+w*w);if(z<1e-12)continue;let M=b/z,P=w/z,E=m.map(T=>T.seg.rooftopPz).filter(T=>T!==void 0),S=E.length>0?Math.max(...E):void 0,I=[...new Set(E.map(T=>T.toFixed(3)))];if(I.length>1){let T=m.map(R=>({idx:R.idx,start:{x:R.seg.start.x,y:R.seg.start.y,z:R.seg.start.z},end:{x:R.seg.end.x,y:R.seg.end.y,z:R.seg.end.z},length:R.seg.length,rooftopPz:R.seg.rooftopPz}));a.push({values:[...I],segments:T})}let W=[...new Set(m.map(T=>String(T.seg.buildRosource)))],v=Ta(m,p,M,P,n);for(let{l:T,r:R,srcIdxs:D,startPt:H,endPt:V}of v){if(R-T<.01)continue;let C=Sa(p,H,V,M,P,S,p.buildRosource);s.push(C),i.push(D)}}}return{...Ra(s,i),debugRPZGroups:a}}function Da(t){return{minLengthRatio:0,maxDistance:.5,angleThreshold:45,projectionMargin:.1,printOnly:!0,minEmbedRatioThreshold:.3,noWallSurroundDistance:.3,minEmbedRatio:.05,...t}}function va(t,e,n){return[{x:e,y:n,z:0},{x:t[0].x,y:t[0].y,z:0},{x:t[1].x,y:t[1].y,z:0},{x:t[2].x,y:t[2].y,z:0},{x:t[3].x,y:t[3].y,z:0}]}function Va(t,e,n,o,r,s,i,a,u){if(!["_008","_002","_014","_015","_020"].some(c=>t.includes(c)))return;e.map(c=>{let{start:l,end:h}=c,g=jt(n,o,l.x,l.y,h.x,h.y),x=r.map(z=>{let M=jt(z.x,z.y,l.x,l.y,h.x,h.y);return`t=${M.t.toFixed(3)},d=${M.dist.toFixed(3)}`}),y=h.x-l.x,m=h.y-l.y,p=Math.sqrt(y*y+m*m)||1,b=Math.abs(i*(y/p)+a*(m/p)),w=Math.acos(Math.min(b,1))*(180/Math.PI);return{seg:c,lineDist:g.dist,t:g.t,pts:x,angle:w,length:c.length}}).sort((c,l)=>c.lineDist-l.lineDist)}function Ia(t,e,n,o,r,s,i,a,u){let d=[{margin:s.projectionMargin,maxDist:s.maxDistance,label:"\u4E25\u683C"},{margin:.4,maxDist:.65,label:"\u5BBD\u677E"}],f=[],c=Cs(a)||e;for(let l of d){if(f.length>0)break;for(let h of t){let{start:g,end:x}=h,y=!1,m=1/0;for(let E of r){let{dist:S,t:I}=jt(E.x,E.y,g.x,g.y,x.x,x.y);I>=-l.margin&&I<=1+l.margin&&S<=l.maxDist&&(y=!0,S<m&&(m=S))}if(!y)continue;let p=null;if(u&&(p=Hs(u,g,x,.3)),!p||!p.hingePoint){let E=Is(a,g,x,.1);E&&E.hingeEdge&&(p={...E,hingePoint:E.hingeEdge?{x:E.hingeEdge.midX,y:E.hingeEdge.midY,z:0}:null,otherPoint:E.otherEdge?{x:E.otherEdge.midX,y:E.otherEdge.midY,z:0}:null})}p&&p.hingePoint&&jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y).dist>.3&&(p=null);let b=!1;if(!p||!p.hingePoint){let E=jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y);if(m<.15&&E.dist<.3&&h.originalPoints&&h.originalPoints.length>=50&&h.length>=.5)b=!0;else continue}if(!b&&(p.hingeT<-l.margin||p.hingeT>1+l.margin)){let E=jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y);if(m<.15&&E.dist<.3&&h.originalPoints&&h.originalPoints.length>=50&&h.length>=.5)b=!0,h.__candidatesDebug="hingeT\u8D85\u8303\u56F4-\u5BC6\u5EA6\u515C\u5E95";else continue}let w=b?0:p.isClosed?1:.5,z=[r[1],r[2],r[3],r[4]],M=vs(g.x,g.y,x.x,x.y,z),P=c>0?M/c:0;f.push({seg:h,minDist:m,embedRatio:P,ratio:h.length/c,score:0,details:{conn:0,variance:0,sameSide:0},hingeInfo:p,hingeScore:w})}}return f}function Ca(t,e,n,o){if(t.length<=1)return t.length===1&&(t[0].score=-1),t;for(let r of t){let{score:s,details:i}=Os(r.seg,e,n);r.score=s,r.details=i}return t.sort((r,s)=>{let i=r.hingeScore||0;return(s.hingeScore||0)-i||s.embedRatio-r.embedRatio||s.score-r.score||r.minDist-s.minDist}),t}function Ha(t,e,n,o){let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=Math.sqrt(r*r+s*s),a=r/i,u=s/i,d=e.map(P=>{let E=P.x-t.start.x,S=P.y-t.start.y;return(E*a+S*u)/i}),f=Math.min(...d),c=Math.max(...d),l=(c-f)*i,h=(f+c)/2,g=t.start.x+h*r,x=t.start.y+h*s,y=n.coordinatesByArea.heightData?.minZ?n.coordinatesByArea.heightData.minZ:0,m=n.coordinatesByArea.heightData?.maxZ?n.coordinatesByArea.heightData.maxZ:0,p=Math.abs(m-y),b=Ke(o,t.start.x,t.start.y),w=Math.max(0,y-b),z=f<=0&&c>=1,M=t.start.z+h*(t.end.z-t.start.z);return{p:{x:g,y:x,z:M},width:l,full:z,height:p,groundClearance:w,wallTMin:Math.max(0,Math.min(1,f)),wallTMax:Math.max(0,Math.min(1,c))}}function Go(t,e,n){let o=e.map(f=>n[f]),r=[],s=[],i=[],a=[],u,d;for(let f of o){if(f.originalPoints?.length)for(let c of f.originalPoints)r.push(c);if(f.doorAndBeamData?.length)for(let c of f.doorAndBeamData)s.push(c);if(f.insetionArr?.length)for(let c of f.insetionArr)i.push(c);if(f.DrawDoor?.length)for(let c of f.DrawDoor)a.push(c);f.boxData&&!u&&(u=f.boxData),f.rooftopPz!==void 0&&(d===void 0||f.rooftopPz>d)&&(d=f.rooftopPz)}r.length&&(t.originalPoints=r),s.length&&(t.doorAndBeamData=s),i.length&&(t.insetionArr=i),a.length&&(t.DrawDoor=a),u&&(t.boxData=u),d!==void 0&&(t.rooftopPz=d)}function fn(t,e,n=10,o=.2,r=.2){let s=[];if(!t.DrawDoor)return s;let i=t.DrawDoor.p,a=t.wallSegment,u=a.end.x-a.start.x,d=a.end.y-a.start.y,f=Math.sqrt(u*u+d*d)||1,c=u/f,l=d/f,h=i.x-a.start.x,g=i.y-a.start.y,x=(h*c+g*l)/f;for(let y of e){if(y===a)continue;let m=y.end.x-y.start.x,p=y.end.y-y.start.y,b=Math.sqrt(m*m+p*p)||1,w=m/b,z=p/b,M=Math.abs(c*w+l*z),E=Math.acos(Math.min(M,1))*(180/Math.PI);if(Math.abs(E-90)>n)continue;let S=[y.start,y.end];for(let I of S){let W=I.x-a.start.x,v=I.y-a.start.y,T=(W*c+v*l)/f,R=a.start.x+T*f*c,D=a.start.y+T*f*l;if(Math.sqrt((I.x-R)**2+(I.y-D)**2)>r)continue;let V=Math.abs((T-x)*f);if(V<=o){s.push({seg:y,distAlongWall:V,angle:E,endPoint:I});break}}}return s}function Dn(t,e){let n=t.start,o=t.end,r=o.x-n.x,s=o.y-n.y,i=Math.sqrt(r*r+s*s)||1,a=((e.x-n.x)*r+(e.y-n.y)*s)/(i*i),u=Math.max(0,Math.min(1,a)),d={x:n.x+u*r,y:n.y+u*s,z:n.z+(o.z-n.z)*u||0};function f(b){if(!b||!b.length)return[[],[]];let w=[],z=[];for(let M of b){let P=((M.x-n.x)*r+(M.y-n.y)*s)/(i*i);P<=u+1e-8&&w.push(M),P>=u-1e-8&&z.push(M)}return(w.length===0||c(w[w.length-1],d)>1e-6)&&w.push({...d}),(z.length===0||c(z[0],d)>1e-6)&&z.unshift({...d}),[w,z]}function c(b,w){return Math.sqrt((b.x-w.x)**2+(b.y-w.y)**2)}let[l,h]=f(t.originalPoints),g=Math.max(u*i,1e-6),x=Math.max((1-u)*i,1e-6),y=t.direction||{x:r/i,y:s/i,z:(o.z-n.z)/i||0},m={...t,start:{...n},end:{...d},length:g,direction:{...y},originalPoints:l.length?l:void 0},p={...t,start:{...d},end:{...o},length:x,direction:{...y},originalPoints:h.length?h:void 0};if("isRebuild"in t&&(m.isRebuild=t.isRebuild,p.isRebuild=t.isRebuild),t.DrawDoor&&t.DrawDoor.length>0){let b=[],w=[];for(let z of t.DrawDoor){let M=((z.p.x-n.x)*r+(z.p.y-n.y)*s)/(i*i);M<=u+1e-8&&b.push(z),M>=u-1e-8&&w.push(z)}m.DrawDoor=b.length?b:void 0,p.DrawDoor=w.length?w:void 0}else m.DrawDoor=void 0,p.DrawDoor=void 0;if(t.boxData){let b=w=>{if(!w.length)return;let z=1/0,M=1/0,P=1/0,E=-1/0,S=-1/0,I=-1/0;for(let W of w)W.x<z&&(z=W.x),W.y<M&&(M=W.y),W.z!==void 0&&W.z<P&&(P=W.z),W.x>E&&(E=W.x),W.y>S&&(S=W.y),W.z!==void 0&&W.z>I&&(I=W.z);return{isBox3:!0,min:{x:z,y:M,z:P},max:{x:E,y:S,z:I}}};m.boxData=b(l),p.boxData=b(h)}return[m,p]}function vn(t,e,n,o){let r=Da(n),s=[],{segments:i,sourceMap:a}=o??Qe(e),u=i,d=_s(u);for(let f of t){let c=f.coordinatesByArea?.coordinates;if(!c||c.length<4)continue;let l=c.slice(0,4),h=Xo(f.nline),g=no(l),x,y,m,p,b;if(h?(x=h.midX,y=h.midY,m=h.length,p=h.dirX,b=h.dirY):(x=l.reduce((B,O)=>B+O.x,0)/4,y=l.reduce((B,O)=>B+O.y,0)/4,m=g.longSide,p=g.dirX,b=g.dirY),m<.01)continue;let w=l.reduce((B,O)=>B+O.z,0)/4,z=Ke(e,x,y);if(w<z-.3)continue;let M=Vs(l),P=[...l].sort((B,O)=>B.z-O.z),E=P[0].x-P[1].x,S=P[0].y-P[1].y,I=Math.sqrt(E*E+S*S),W=va(l,x,y);Va(f.name,u,x,y,W,m,p,b,r);let v=Ia(u,I,p,b,W,r,f.name,M,f.nline);if(v.length>0)for(let B=0;B<v.length;B++){let O=v[B],$=O.seg,G=!!O.hingeInfo?.hingePoint,Q=O.hingeInfo?.isClosed,j=O.hingeInfo?.hingeDist??-1,Z=jt(W[0].x,W[0].y,$.start.x,$.start.y,$.end.x,$.end.y)}if(v=Ca(v,W,e,f.name),v.length===0){for(let O of u){let $=O.start.x+(O.end.x-O.start.x)/2-x,G=O.start.y+(O.end.y-O.start.y)/2-y;if(Math.sqrt($*$+G*G)>2)continue;let j=O.end.x-O.start.x,Z=O.end.y-O.start.y,U=Math.sqrt(j*j+Z*Z),it=j/U,ht=Z/U,St=Math.abs(it*p+ht*b),ot=Math.acos(Math.min(1,Math.abs(St)))*180/Math.PI,Mt=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y)}let B=0;for(let O of u)jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y).dist<2&&B++}let T=I;for(let B of v){let O=B.seg;if(O.originalPoints&&O.originalPoints.length>100&&O.length>.5)try{let $=oo(O,.1),G=Bs(O,$,O.length,.5,.5,3,.35),Q,j=!1;if(f.nline){let ht=Ls(f.nline,O.start,O.end);if(ht)Q=ht.hingeT,j=ht.hingeDist>.05;else{let St=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y);Q=Math.max(0,Math.min(1,St.t))}}else{let ht=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y);Q=Math.max(0,Math.min(1,ht.t))}let Z=Ws($),U,it=Z<=.3;it?U={score:Zo($.avgDensity,d),bestRegion:null,bestOverlap:0,bestOpeningStartT:0,bestOpeningEndT:1}:(U=ks({hingeT:Q,doorWidth:T,isOpenDoor:j},G,O.length,d.medianDensity),U.score<0&&(U={score:Zo($.avgDensity,d),bestRegion:null,bestOverlap:0,bestOpeningStartT:0,bestOpeningEndT:1})),f.name.includes("door_002")&&(window.__doorLog=!0),B.densityScore=U.score,B.densityOverlap=U.bestOverlap,B.rectangularRegions=G,B.densityResult=$,B.densityOpeningStartT=U.bestOpeningStartT,B.densityOpeningEndT=U.bestOpeningEndT,B.densityCV=Z,B.densityUsedGlobalRank=it}catch{B.densityScore=-1}else B.densityScore=-1}if(f.name.includes("door_002"))for(let B=0;B<v.length;B++){let $=v[B].seg,G=u.findIndex(j=>Math.abs(j.start.x-$.start.x)<.001&&Math.abs(j.start.y-$.start.y)<.001&&Math.abs(j.end.x-$.end.x)<.001&&Math.abs(j.end.y-$.end.y)<.001),Q=G>=0?a[G]||[]:[];for(let j of Q){let Z=e[j]}}if(v.sort((B,O)=>{let $=B.densityScore>=0?B.densityScore:-1/0,G=O.densityScore>=0?O.densityScore:-1/0;if($!==G)return G-$;if(Math.abs($-G)<.3){let Z=B.ratio||0,U=O.ratio||0,it=Math.abs(Z-1),ht=Math.abs(U-1);if(it!==ht)return it-ht}let Q=B.hingeScore||0,j=O.hingeScore||0;return Q!==j?j-Q:B.minDist-O.minDist}),v.length>0)for(let B=0;B<v.length;B++){let O=v[B],$=O.seg,G=O.ratio||$.length/T,Q=Math.abs(G-1)}let R=null,D=1/0,H=0,V=0,C=null,_=-1,A=0,L=0,k=0;if(v.length>0){let B=v[0];R=B.seg,D=B.minDist,H=B.ratio,V=B.embedRatio,C=B.hingeInfo,_=B.densityScore??-1,A=B.densityOverlap??0,L=B.densityOpeningStartT??0,k=B.densityOpeningEndT??0}let F;if(R&&(F=Ha(R,l,f,i),F&&k>L&&(F.wallTMin=Math.max(0,Math.min(1,L)),F.wallTMax=Math.max(0,Math.min(1,k))),!r.printOnly)){let B=R;B.DrawDoor||(B.DrawDoor=[]),B.DrawDoor.push(F),B.isDoor=!0}if(R){let B=v.map(($,G)=>{let Q=$.seg;return{start:{x:Q.start.x,y:Q.start.y,z:Q.start.z},end:{x:Q.end.x,y:Q.end.y,z:Q.end.z},length:Q.length,densityScore:$.densityScore??-1,hingeScore:$.hingeScore??0,minDist:$.minDist,ratio:$.ratio,isWinner:G===0}}),O={doorName:f.name,doorCategory:f.category,doorCenter:f.center,wallSegment:R,distance:D,wallLengthRatio:H,DrawDoor:F,densityScore:_,densityOverlap:A,candidates:B,hingePoint:C?.hingePoint,otherPoint:C?.otherPoint,isClosed:C?.isClosed,doorWidth:F?.width,wallTMin:F?.wallTMin,wallTMax:F?.wallTMax};if(s.push(O),(f.name==="1527727501200_window_001"||f.name==="1527727427200_window_001")&&F){let $=R.end.x-R.start.x,G=R.end.y-R.start.y,Q=Math.sqrt($*$+G*G),j=$/Q,Z=G/Q,U=F.p.x-R.start.x,it=F.p.y-R.start.y,ht=(U*j+it*Z)/Q,St=0;for(let ot of i){if(ot===R)continue;let Mt=ot.end.x-ot.start.x,xt=ot.end.y-ot.start.y,vt=Math.sqrt(Mt*Mt+xt*xt)||1,Pt=Mt/vt,zt=xt/vt,Tt=Math.abs(j*Pt+Z*zt),nt=Math.acos(Math.min(Tt,1))*(180/Math.PI);if(Math.abs(nt-90)>10)continue;St++;let ft=[ot.start,ot.end],rt="";for(let tt of ft){let le=tt.x-R.start.x,yt=tt.y-R.start.y,Jt=(le*j+yt*Z)/Q,me=R.start.x+Jt*Q*j,Rt=R.start.y+Jt*Q*Z,ee=Math.sqrt((tt.x-me)**2+(tt.y-Rt)**2),et=Math.abs((Jt-ht)*Q);rt+=`\u7AEF(${tt.x.toFixed(2)},${tt.y.toFixed(2)})\u22A5=${ee.toFixed(3)}m \u6CBF\u5899=${et.toFixed(3)}m `}}}if(F&&F.width>1){let $=fn(O,i);$.length>0&&$.forEach((G,Q)=>{})}}}for(let f of s){let c=f.wallSegment,l=i.indexOf(c);if(l===-1)continue;let h=a[l];!h||h.length<=1||Go(c,h,e)}return s}function ro(t){return[t.wallTMin??0,t.wallTMax??1]}function Wa(t,e){let[n,o]=ro(t),[r,s]=ro(e),i=Math.max(0,Math.min(o,s)-Math.max(n,r)),a=Math.min(o-n,s-r);return a>0?i/a:0}function _a(t,e){if(!t.hingePoint||!e.hingePoint)return null;let n=t.hingePoint.x-e.hingePoint.x,o=t.hingePoint.y-e.hingePoint.y;return Math.sqrt(n*n+o*o)}function Fs(t,e){let n=e.end.x-e.start.x,o=e.end.y-e.start.y,r=n*n+o*o;if(r<1e-10)return 0;let s=((t.x-e.start.x)*n+(t.y-e.start.y)*o)/r;return Math.max(0,Math.min(1,s))}function Ns(t,e={}){let n=e.minTOverlapRatio??.5,o=e.maxHingeDist??.3,r=e.minHingeTOverlapRatio??.2;if(t.length<2)return{matches:t,removed:[]};let s=new Set,i=new Map;for(let f=0;f<t.length;f++){let l=t[f].wallSegment;i.has(l)||i.set(l,[]),i.get(l).push(f)}let a=0;for(let[f,c]of i){a++;let l=c.map(x=>t[x].doorName).join(", "),h=f.start,g=f.end}for(let[,f]of i){if(f.length<2)continue;let c=[...f].sort((l,h)=>{let g=t[l].densityScore??0;return(t[h].densityScore??0)-g});for(let l=0;l<c.length;l++){let h=c[l];if(s.has(h))continue;let g=t[h];for(let x=l+1;x<c.length;x++){let y=c[x];if(s.has(y))continue;let m=t[y],p=!1,b="",w=Wa(g,m),z=[g.hingePoint,g.otherPoint].filter(Boolean),M=[m.hingePoint,m.otherPoint].filter(Boolean),P=null;for(let H of z)for(let V of M){let C=Math.sqrt((H.x-V.x)**2+(H.y-V.y)**2);if(C>.25)continue;let _=Fs(H,g.wallSegment),A=Fs(V,m.wallSegment),L=Math.abs(_-A);(!P||C<P.dist)&&(P={dist:C,tDiff:L})}P&&P.tDiff<=.08&&(p=!0,b=`\u6700\u77ED\u8FB9\u8DDD=${P.dist.toFixed(3)}m t\u5DEE=${P.tDiff.toFixed(3)}`);let[E,S]=ro(g),[I,W]=ro(m),v=_a(g,m);g.doorName.includes("door_002")||g.doorName.includes("door_004")||m.doorName.includes("door_002")||m.doorName.includes("door_004");let T=[g.hingePoint,g.otherPoint].filter(Boolean),R=[m.hingePoint,m.otherPoint].filter(Boolean),D=1/0;for(let H of T)for(let V of R){let C=Math.sqrt((H.x-V.x)**2+(H.y-V.y)**2);C<D&&(D=C)}p&&s.add(y)}}}let u=t.filter((f,c)=>!s.has(c)),d=t.filter((f,c)=>s.has(c)).map(f=>f.doorName);return{matches:u,removed:d}}function Yo(t,e,n){let o={printOnly:!1,skipMerge:!1,...n};for(let m of t)m.DrawDoor&&(m.DrawDoor=void 0),m.isDoor&&(m.isDoor=void 0),m.drawDoorData&&(m.drawDoorData=void 0);let r=Array.isArray(e)?e.filter(m=>m&&m.category==="door"&&!m.isFindBeam):[],s,i;if(o.skipMerge)s=t,i=t.map((m,p)=>[p]);else{let m=Qe(t);s=m.segments,i=m.sourceMap}for(let m=0;m<s.length;m++){let p=i[m];p&&!s[m].originalPoints&&Go(s[m],p,t)}let a=vn(r,t,o,{segments:s,sourceMap:i}),d=Ns(a,{minTOverlapRatio:.5}).matches,f=new Set(d.map(m=>m.doorName));for(let m of s){let p=m;if(p.DrawDoor&&p.DrawDoor.length>0){let b=a.filter(z=>z.wallSegment===m).length;d.filter(z=>z.wallSegment===m).length===0&&(p.DrawDoor=[],p.isDoor=!1)}}let c=new Map;for(let m=0;m<d.length;m++){let p=d[m],b=s.indexOf(p.wallSegment);b>=0&&c.set(b,m)}let l=[],h=new Set,g=new Map;for(let m of e)m&&m.name&&g.set(m.name,m);for(let[m,p]of c){let b=i[m];if(!b)continue;let w=s[m],z=w.DrawDoor,M=w.isDoor,P=w.end.x-w.start.x,E=w.end.y-w.start.y,S=P*P+E*E,I=0,W=1,v=d[p],R=(v?g.get(v.doorName):void 0)?.coordinatesByArea?.coordinates;if(R&&R.length>=4&&S>0){let C=R.slice(0,4).map(_=>{let A=_.x-w.start.x,L=_.y-w.start.y;return(A*P+L*E)/S});I=Math.max(0,Math.min(1,Math.min(...C))),W=Math.max(0,Math.min(1,Math.max(...C)))}let D,H;S>0&&W>I&&(D={x:w.start.x+I*P,y:w.start.y+I*E,z:w.start.z+I*(w.end.z-w.start.z)},H={x:w.start.x+W*P,y:w.start.y+W*E,z:w.start.z+W*(w.end.z-w.start.z)});let V=!1;for(let C of b){let _=t[C];if(!_)continue;let A=_.end.x-_.start.x,L=_.end.y-_.start.y,k=A*A+L*L,F=Math.sqrt(k);if(k<1e-10||!D||!H){h.add(C),l.push(_);continue}let B=((D.x-_.start.x)*A+(D.y-_.start.y)*L)/k,O=((H.x-_.start.x)*A+(H.y-_.start.y)*L)/k,$=Math.max(0,Math.min(1,Math.min(B,O))),G=Math.max(0,Math.min(1,Math.max(B,O)));if(G-$<.001)continue;h.add(C);let Q=ot=>({x:_.start.x+ot*A,y:_.start.y+ot*L,z:_.start.z+ot*(_.end.z-_.start.z)}),j=Q($),Z=Q(G),U=_.direction||{x:A/F,y:L/F,z:0},it,ht,St;if(_.originalPoints&&_.originalPoints.length>0){it=[],ht=[],St=[];for(let ot of _.originalPoints){let Mt=((ot.x-_.start.x)*A+(ot.y-_.start.y)*L)/k;Mt<$-1e-8?it.push(ot):Mt>G+1e-8?St.push(ot):ht.push(ot)}it.length===0&&(it=void 0),ht.length===0&&(ht=void 0),St.length===0&&(St=void 0)}if($>.001){let ot={..._,start:{..._.start},end:{...j},length:$*F,direction:{...U}};ot.DrawDoor=void 0,ot.isDoor=!1,ot.drawDoorData=void 0,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=it,l.push(ot)}{let ot={..._,start:{...j},end:{...Z},length:(G-$)*F,direction:{...U}};if(z&&(ot.DrawDoor=z),M&&(ot.isDoor=!0),ot.__matchIdx=p,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=ht,!V&&z&&z.length>0){let Mt=0;for(let Tt=0;Tt<z.length;Tt++)if(yr(D,H,z[Tt].p)){Mt=Tt;break}V=!0;let xt=Math.sqrt(S),vt=xt>0?-E/xt:0,Pt=xt>0?P/xt:0,zt={pointNum:2,start:D,end:H,n:{x:vt,y:Pt,z:0},height:z[Mt].height,groundClearance:z[Mt].groundClearance};ot.drawDoorData=zt}l.push(ot)}if(G<.999){let ot={..._,start:{...Z},end:{..._.end},length:(1-G)*F,direction:{...U}};ot.DrawDoor=void 0,ot.isDoor=!1,ot.drawDoorData=void 0,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=St,l.push(ot)}}}for(let m=0;m<t.length;m++)h.has(m)||l.push(t[m]);let x=new Set(d.map(m=>m.doorName));for(let m of e)m&&m.category==="door"&&(x.has(m.name)?delete m.AbnormalDoor:m.AbnormalDoor=!0);let y=new Map;for(let m of d){if(!m.DrawDoor||m.DrawDoor.width<=1)continue;let p=g.get(m.doorName);if(!p||!p.positionArr||!p.positionArr.length)continue;let b=fn(m,s);if(b.length<=1)continue;let w=p.positionArr[0],z=m.DrawDoor.p,M=w.x-z.x,P=w.y-z.y,E=Math.sqrt(M*M+P*P);if(E<.001)continue;let S=M/E,I=P/E,W=b.filter(C=>{let _=(C.seg.start.x+C.seg.end.x)/2,A=(C.seg.start.y+C.seg.end.y)/2,L=_-z.x,k=A-z.y;return L*S+k*I>0});if(!W.length)continue;let v=W[0].endPoint,T=m.wallSegment;if(y.has(T))continue;let[R,D]=Dn(T,v);y.set(T,[R,D]);let H=((z.x-T.start.x)*(T.end.x-T.start.x)+(z.y-T.start.y)*(T.end.y-T.start.y))/(T.length*T.length),V=((v.x-T.start.x)*(T.end.x-T.start.x)+(v.y-T.start.y)*(T.end.y-T.start.y))/(T.length*T.length);H<=V?m.wallSegment=R:m.wallSegment=D}if(y.size>0){let m=[];for(let p of l)y.has(p)?m.push(...y.get(p)):m.push(p);for(let p of m)p.isDoor=!!(p.DrawDoor&&p.DrawDoor.length>0);return{segments:m,matches:d}}for(let m of l)m.isDoor=!!(m.DrawDoor&&m.DrawDoor.length>0);return{segments:l,matches:d}}var Aa={findWalls:Rn,mergeSegments:Ye,computeFloorZ:Ge,analyzeQuadrangle:Ze,detectPerpWalls:Tn,splitSegment:Sn,process:jo},Ba={findWalls:vn,mergeSegments:Qe,computeFloorZ:Ke,analyzeQuadrangle:Ze,detectPerpWalls:fn,splitSegment:Dn,process:Yo};function dn(t){return t==="window"?Aa:Ba}function La(t,e,n,o,r){return dn(t).findWalls(e,n,o,r)}function ka(t,e,n,o){return dn(t).process(e,n,o)}function Oa(t,e){return dn(t).mergeSegments(e)}function Fa(t,e,n){return dn(t).detectPerpWalls(e,n)}function Na(t,e,n){return dn(t).splitSegment(e,n)}function Je(t,e){let n=Math.atan2(e,t);return n<0&&(n+=Math.PI),n>=Math.PI&&(n-=Math.PI),n}function He(t,e,n){let o=Math.cos(n),r=Math.sin(n);return{x:t*o-e*r,z:t*r+e*o}}function so(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-10){let g=t-n,x=e-o;return Math.sqrt(g*g+x*x)}let d=((t-n)*i+(e-o)*a)/u;d=Math.max(0,Math.min(1,d));let f=n+d*i,c=o+d*a,l=t-f,h=e-c;return Math.sqrt(l*l+h*h)}function qs(t){if(t.length<3)return 0;let e=0,n=t.length;for(let o=0;o<n;o++){let r=(o+1)%n;e+=t[o].x*t[r].z,e-=t[r].x*t[o].z}return Math.abs(e)/2}function Uo(t){let e=t.x2-t.x1,n=t.z2-t.z1;return Math.sqrt(e*e+n*n)}function io(t){if(!t.length)return{rotated:[],mainAngle:0};let e=180,n=new Array(e).fill(0),o=Math.PI/e;for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Je(c,l),x=Math.floor(g/o);x>=e&&(x=e-1),n[x]+=h}let r=new Array(e).fill(0),s=5;for(let f=0;f<e;f++){let c=0;for(let l=-s;l<=s;l++){let h=f+l;h<0&&(h+=e),h>=e&&(h-=e),c+=n[h]}r[f]=c}let i=0,a=0;for(let f=0;f<e;f++)r[f]>i&&(i=r[f],a=f);let u=(a+.5)*o;return{rotated:t.map(f=>{let c=He(f.x1,f.z1,-u),l=He(f.x2,f.z2,-u);return{...f,x1:c.x,z1:c.z,x2:l.x,z2:l.z}}),mainAngle:u}}var Ko=15*(Math.PI/180);function lo(t,e,n=.15,o=.5){let r=[];for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Je(c,l),x;if(e==="X"?x=g<Ko||g>Math.PI-Ko:x=Math.abs(g-Math.PI/2)<Ko,!x)continue;let y,m,p;e==="X"?(y=(f.z1+f.z2)/2,m=Math.min(f.x1,f.x2),p=Math.max(f.x1,f.x2)):(y=(f.x1+f.x2)/2,m=Math.min(f.z1,f.z2),p=Math.max(f.z1,f.z2)),r.push({line:f,offset:y,pStart:m,pEnd:p,length:h})}if(r.length===0)return{groups:[],lineToGroup:new Map};r.sort((f,c)=>f.offset-c.offset);let s=[],i=[r[0]],a=r[0].offset;for(let f=1;f<r.length;f++){let c=r[f];Math.abs(c.offset-a)<n?i.push(c):(s.push(i),i=[c],a=c.offset)}i.length>0&&s.push(i);let u=[],d=new Map;for(let f of s){f.sort((l,h)=>l.pStart-h.pStart);let c=[];for(let l of f){let h=!1;for(let g of c)if(l.pStart<=g.end+o){g.items.push(l),g.end=Math.max(g.end,l.pEnd),h=!0;break}h||c.push({items:[l],end:l.pEnd})}for(let l of c){let h=l.items.map(x=>x.line),g=u.length;u.push(h);for(let x of h)d.set(x.id,g)}}return{groups:u,lineToGroup:d}}function ao(t,e,n=.3){let o=new Map;for(let r of t){let s=null,i=1/0;for(let a of e){let u=so(r.x,r.z,a.x1,a.z1,a.x2,a.z2);u<i&&(i=u,s=a.id)}s!==null&&i<=n&&o.set(r.id,s)}return o}var hn=.15;function qa(t,e){return Math.round(t/hn)+"_"+Math.round(e/hn)}function js(t,e=5){if(t.length<3)return new Set;let n=new Map,o=0,r=0;function s(u,d){let f=qa(u,d),c=n.get(f);if(c)return c;let l=Math.round(u/hn),h=Math.round(d/hn),g=null,x=hn*hn;for(let y=-1;y<=1;y++)for(let m=-1;m<=1;m++){let p=l+y+"_"+(h+m),b=n.get(p);if(!b)continue;let w=b.x-u,z=b.z-d,M=w*w+z*z;M<x&&(x=M,g=b)}return g?(n.set(f,g),g):(c={id:o++,x:u,z:d,edges:[]},n.set(f,c),c)}let i=[];for(let u of t){let d=s(u.x1,u.z1),f=s(u.x2,u.z2);if(d.id===f.id)continue;let c=Math.atan2(f.z-d.z,f.x-d.x),l=Math.atan2(d.z-f.z,d.x-f.x),h={id:r++,wallId:u.id,from:d,to:f,twin:null,angle:c,visited:!1},g={id:r++,wallId:u.id,from:f,to:d,twin:h,angle:l,visited:!1};h.twin=g,d.edges.push(h),f.edges.push(g),i.push(h,g)}if(i.length<6)return new Set;for(let u of n.values())u.edges.sort((d,f)=>d.angle-f.angle);let a=new Set;for(let u of i){if(u.visited)continue;let d=[],f=u,c=i.length+10;for(;!f.visited&&d.length<c;){f.visited=!0,d.push(f);let l=f.to,h=f.twin,g=l.edges.indexOf(h);if(g<0)break;let x=(g+1)%l.edges.length;f=l.edges[x]}if(d.length>=3){let l=d[0];if(d[d.length-1].to.id===l.from.id){let g=[],x=new Set;for(let m of d)x.add(m.wallId),g.length===0&&g.push({x:m.from.x,z:m.from.z}),g.push({x:m.to.x,z:m.to.z});if(qs(g)>=e)for(let m of x)a.add(m)}}}return a}var Ep=12*(Math.PI/180);function $s(t,e=2){let n=new Map;for(let o of t){let r=(o.x1+o.x2)/2,s=(o.z1+o.z2)/2,i=Math.floor(r/e),a=Math.floor(s/e),u=`${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}var ja=0;function Xs(){return++ja}function Zs(t,e,n=.15,o=.5){if(!t.length)return[];let r=10*Math.PI/180,s=[];for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(!(h<.01))if(e==="X"){if(Math.abs(l)>h*Math.sin(r))continue;let g=(f.z1+f.z2)/2,x=Math.min(f.x1,f.x2),y=Math.max(f.x1,f.x2);s.push({line:f,pStart:x,pEnd:y,offset:g,yMin:Math.min(f.y1,f.y2),yMax:Math.max(f.y1,f.y2)})}else{if(Math.abs(c)>h*Math.sin(r))continue;let g=(f.x1+f.x2)/2,x=Math.min(f.z1,f.z2),y=Math.max(f.z1,f.z2);s.push({line:f,pStart:x,pEnd:y,offset:g,yMin:Math.min(f.y1,f.y2),yMax:Math.max(f.y1,f.y2)})}}if(!s.length)return[];s.sort((f,c)=>f.offset-c.offset);let i=[],a=[s[0]],u=s[0].offset;for(let f=1;f<s.length;f++){let c=s[f];Math.abs(c.offset-u)<n?a.push(c):(i.push(a),a=[c],u=c.offset)}i.push(a);let d=[];for(let f of i){f.sort((l,h)=>l.pStart-h.pStart);let c=[];for(let l of f){if(c.length===0){c.push({pStart:l.pStart,pEnd:l.pEnd,offset:l.offset,count:1,yMin:l.yMin,yMax:l.yMax});continue}let h=c[c.length-1];l.pStart<=h.pEnd+o?(h.pEnd=Math.max(h.pEnd,l.pEnd),h.count+=1,h.yMin=Math.min(h.yMin,l.yMin),h.yMax=Math.max(h.yMax,l.yMax),h.offset=(h.offset*(h.count-1)+l.offset)/h.count):c.push({pStart:l.pStart,pEnd:l.pEnd,offset:l.offset,count:1,yMin:l.yMin,yMax:l.yMax})}for(let l of c){let h=l.pEnd-l.pStart;if(h<.01)continue;let g;e==="X"?g={id:Xs(),x1:l.pStart,y1:l.yMin,z1:l.offset,x2:l.pEnd,y2:l.yMax,z2:l.offset,length:h,pointCount:l.count,isMain:!1}:g={id:Xs(),x1:l.offset,y1:l.yMin,z1:l.pStart,x2:l.offset,y2:l.yMax,z2:l.pEnd,length:h,pointCount:l.count,isMain:!1},d.push(g)}}return d}function Gs(t,e=.8,n=3){return t.filter(o=>o.length>=e&&o.pointCount>=n)}function Ys(t,e=1,n=3){return t.filter(o=>o.length>=e&&o.pointCount>=n)}function $a(t,e,n={}){let{distTol:o=.15,gapTol:r=.5,minLen:s=.2,minGroupLen:i=1,minArea:a=3,maxWinDist:u=.3,minLongWall:d=2}=n,f=performance.now(),{rotated:c,mainAngle:l}=io(t),h=performance.now(),g=e.map(D=>{let H=He(D.x,D.z,-l);return{...D,x:H.x,z:H.z}}),{groups:x}=lo(c,"X",o,r),{groups:y}=lo(c,"Z",o,r),m=[...x,...y],p=performance.now(),b=new Map,w=[];for(let D=0;D<m.length;D++){let H=m[D];if(H.length===0){w.push(0);continue}let V=0;for(let C of H)V+=Uo(C),b.set(C.id,D);w.push(V)}let z=performance.now(),M=new Set;for(let D=0;D<m.length;D++)w[D]>=d&&M.add(D);if(M.size>0){let D=[...M];for(;D.length>0;){let H=D.shift(),V=m[H];for(let C=0;C<m.length;C++){if(M.has(C)||w[C]<i)continue;let _=m[C],A=!1;t:for(let L of V)for(let k of _)if(Math.hypot(L.x1-k.x1,L.z1-k.z1)<=co||Math.hypot(L.x1-k.x2,L.z1-k.z2)<=co||Math.hypot(L.x2-k.x1,L.z2-k.z1)<=co||Math.hypot(L.x2-k.x2,L.z2-k.z2)<=co){A=!0;break t}A&&(M.add(C),D.push(C))}}}let P=c.map((D,H)=>{let V=Uo(D),C=b.get(D.id),_=C!==void 0&&M.has(C),A=V>=d;return{id:H,x1:D.x1,y1:D.y1,z1:D.z1,x2:D.x2,y2:D.y2,z2:D.z2,length:V,pointCount:1,isMain:_||A}}),E=P.filter(D=>D.isMain),S=P.filter(D=>!D.isMain),I=performance.now(),W=ao(g,E,u),v=performance.now(),T=D=>D.map(H=>{let V=He(H.x1,H.z1,l),C=He(H.x2,H.z2,l);return{...H,x1:V.x,z1:V.z,x2:C.x,z2:C.z}}),R=performance.now();return console.log(`[\u4E3B\u4F53\u67B6\u6784] \u89D2\u5EA6\u5F52\u4E00\u5316: ${(h-f).toFixed(0)}ms`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u5171\u7EBF\u5206\u7EC4: ${(p-h).toFixed(0)}ms (\u6C34\u5E73${x.length}\u7EC4 \u5782\u76F4${y.length}\u7EC4)`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u7EC4\u5206\u6790+\u4E3B\u4F53\u5224\u5B9A: ${(R-p).toFixed(0)}ms (\u4E3B\u4F53\u7EC4${M.size} / \u603B\u7EC4${m.length})`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u6807\u8BB0\u539F\u59CB\u7EBF\u6BB5: ${(I-R).toFixed(0)}ms (\u4E3B\u4F53${E.length} / \u6742\u7EBF${S.length})`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u7A97\u6237\u6302\u63A5: ${(v-I).toFixed(0)}ms`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u603B\u8BA1: ${(v-f).toFixed(0)}ms`),{mainWalls:T(E),noiseWalls:T(S),windowToWall:W}}var co=.2;function Te(t,e){let n=Math.atan2(e,t);return n<0&&(n+=Math.PI),n>=Math.PI&&(n-=Math.PI),n}function Vn(t){for(;t>Math.PI;)t-=2*Math.PI;for(;t<-Math.PI;)t+=2*Math.PI;return t}function uo(t){return t*Math.PI/180}function In(t,e,n){let o=Math.cos(n),r=Math.sin(n);return{x:t*o-e*r,z:t*r+e*o}}function Se(t){let e=0;for(let n=1;n<t.length;n++){let o=t[n].x-t[n-1].x,r=t[n].z-t[n-1].z;e+=Math.sqrt(o*o+r*r)}return e}function Qo(t){if(t.length<3)return 0;let e=0,n=t.length;for(let o=0;o<n;o++){let r=(o+1)%n;e+=t[o].x*t[r].z,e-=t[r].x*t[o].z}return Math.abs(e)/2}function ze(t,e,n,o){let r=n-t,s=o-e;return Math.sqrt(r*r+s*s)}function Cn(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-6)return ze(t,e,n,o);let d=((t-n)*i+(e-o)*a)/u;return d=Math.max(0,Math.min(1,d)),ze(t,e,n+d*i,o+d*a)}function Jo(t){if(t.length<4)return t;let e=t.map(n=>({x:n.x,z:n.z}));for(let n=0;n<3;n++){let o=[{...e[0]}];for(let r=1;r<e.length-1;r++)o.push({x:(e[r-1].x+e[r].x+e[r+1].x)/3,z:(e[r-1].z+e[r].z+e[r+1].z)/3});o.push({...e[e.length-1]}),e=o}return e}function Us(t,e){let n=1/0;for(let o of t)for(let r=0;r<e.length-1;r++){let s=Cn(o.x,o.z,e[r].x,e[r].z,e[r+1].x,e[r+1].z);s<n&&(n=s)}return n}function fo(t,e){let n=new Map,o=1/0,r=-1/0,s=1/0,i=-1/0;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.z/e),f=u+","+d;n.set(f,(n.get(f)||0)+1),u<o&&(o=u),u>r&&(r=u),d<s&&(s=d),d>i&&(i=d)}return{cells:n,minX:o,maxX:r,minZ:s,maxZ:i}}function Ks(t,e){let n=new Set,o=1/0,r=-1/0,s=1/0,i=-1/0,a=(u,d)=>{let f=u+","+d;n.add(f),u<o&&(o=u),u>r&&(r=u),d<s&&(s=d),d>i&&(i=d)};for(let u of t){let d=u.x1,f=u.z1,c=u.x2,l=u.z2,h=Math.floor(d/e),g=Math.floor(f/e),x=Math.floor(c/e),y=Math.floor(l/e),m=Math.abs(x-h),p=Math.abs(y-g),b=h<x?1:-1,w=g<y?1:-1,z=m-p,M=Math.max(m,p)*2+5,P=0;for(;P++<M&&(a(h,g),!(h===x&&g===y));){let E=2*z;E>-p&&(z-=p,h+=b),E<m&&(z+=m,g+=w)}}return{active:n,minX:o,maxX:r,minZ:s,maxZ:i}}function Wn(t,e=1){let n=new Set(t);for(let o=0;o<e;o++){let r=new Set(n);for(let i of n){let[a,u]=i.split(",").map(Number);for(let d=-1;d<=1;d++)for(let f=-1;f<=1;f++)d===0&&f===0||r.add(a+d+","+(u+f))}let s=new Set(r);for(let i of r){let[a,u]=i.split(",").map(Number),d=!0;for(let f=-1;f<=1;f++){for(let c=-1;c<=1;c++)if(!r.has(a+f+","+(u+c))){d=!1;break}if(!d)break}d||s.delete(i)}n.clear();for(let i of s)n.add(i)}return n}function tr(t){let e=[],n=new Set;for(let o of t){if(n.has(o))continue;let r=new Set,s=[o];for(n.add(o);s.length>0;){let i=s.shift();r.add(i);let[a,u]=i.split(",").map(Number);for(let d=-1;d<=1;d++)for(let f=-1;f<=1;f++){if(d===0&&f===0)continue;let c=a+d+","+(u+f);t.has(c)&&!n.has(c)&&(n.add(c),s.push(c))}}r.size>=4&&e.push(r)}return e}function er(t,e){if(t.size<3)return[];let n=new Set,o=(g,x)=>t.has(g+","+x);for(let g of t){let[x,y]=g.split(",").map(Number);(!o(x+1,y)||!o(x-1,y)||!o(x,y+1)||!o(x,y-1))&&n.add(g)}if(n.size<3)return[];let r="",s=1/0,i=1/0;for(let g of n){let[x,y]=g.split(",").map(Number);(y<s||y===s&&x<i)&&(s=y,i=x,r=g)}if(!r)return[];let a=[[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]],u=[],[d,f]=r.split(",").map(Number),c=0,l=new Set,h=n.size*8;for(let g=0;g<h;g++){let y=d+","+f+"|"+c;if(l.has(y))break;l.add(y),u.push({x:(d+.5)*e,z:(f+.5)*e});let m=(c+3)%8,p=!1;for(let b=0;b<8;b++){let w=(m+b)%8,z=d+a[w][0],M=f+a[w][1];if(n.has(z+","+M)){c=(w+4)%8,d=z,f=M,p=!0;break}}if(!p||d+","+f===r&&u.length>2)break}return u}function Hn(t,e){if(t.length<=2)return[...t];let n=0,o=0,r=t[0],s=t[t.length-1];for(let i=1;i<t.length-1;i++){let a=Cn(t[i].x,t[i].z,r.x,r.z,s.x,s.z);a>n&&(n=a,o=i)}if(n>e){let i=Hn(t.slice(0,o+1),e),a=Hn(t.slice(o),e);return[...i.slice(0,-1),...a]}return[r,s]}var nr={cellSize:.1,densityThreshold:1,minComponentCells:20,simplifyEpsilon:.01,heightMin:.3,heightMax:2.8,closeIterations:5,sparseRadius:.3,sparseMinNeighbors:5,lineDensityMin:0,lineDensityMinLen:1,densityRatio:.12};function mn(t){let e=t.length;if(e<3)return{cx:0,cz:0,r:0,rmsResidual:0};let n=0,o=0,r=0,s=0,i=0,a=0,u=0,d=0,f=0;for(let m of t){let p=m.x,b=m.z;n+=p,o+=b,r+=p*p,s+=b*b,i+=p*b,a+=p*p*p,u+=b*b*b,d+=p*p*b,f+=p*b*b}let c=2*(e*(r+s)-n*n-o*o);if(Math.abs(c)<1e-10)return{cx:0,cz:0,r:0,rmsResidual:0};let l=(e*(a+f)-n*(r+s))/c,h=(e*(d+u)-o*(r+s))/c,g=Math.sqrt(Math.abs((r+s-2*l*n-2*h*o)/e+l*l+h*h)),x=0;for(let m of t){let b=Math.hypot(m.x-l,m.z-h)-g;x+=b*b}let y=Math.sqrt(x/e);return{cx:l,cz:h,r:g,rmsResidual:y}}function Xa(t){let e=t.length;if(e<4)return{eigenRatio:1,isLinear:!1,spread:0,mx:0,mz:0,v1x:1,v1z:0,v2x:0,v2z:1,s1:0,s2:0};let n=0,o=0;for(let w of t)n+=w.x,o+=w.z;n/=e,o/=e;let r=0,s=0,i=0;for(let w of t){let z=w.x-n,M=w.z-o;r+=z*z,s+=M*M,i+=z*M}r/=e,s/=e,i/=e;let a=r+s,u=Math.max(0,(r-s)*(r-s)+4*i*i),d=Math.sqrt(u),f=(a+d)/2,c=(a-d)/2,l=Math.sqrt(f),h=Math.sqrt(c),g=h,x=1,y=0,m=0,p=1;if(Math.abs(i)>1e-12||Math.abs(r-s)>1e-12){let w=i,z=f-r,M=Math.sqrt(w*w+z*z);M>1e-12&&(x=w/M,y=z/M);let P=i,E=c-r,S=Math.sqrt(P*P+E*E);S>1e-12&&(m=P/S,p=E/S)}let b=f>1e-10?c/f:0;return{eigenRatio:b,isLinear:b<.05,spread:g,mx:n,mz:o,v1x:x,v1z:y,v2x:m,v2z:p,s1:l,s2:h}}function Qs(t,e,n){if(t.length<6)return{type:"straight",eigenRatio:.5,spread:0,cloudUsed:t.length,mx:0,mz:0,v1x:1,v1z:0,v2x:0,v2z:1,s1:0,s2:0};let{eigenRatio:o,isLinear:r,spread:s,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h}=Xa(t),g=0;for(let w=1;w<t.length;w++){let z=t[w].x-t[w-1].x,M=t[w].z-t[w-1].z;g+=Math.sqrt(z*z+M*M)}let x=Za(t),y=mn(x);if(y.r>.1&&y.r<Math.max(g*5,2)&&y.rmsResidual<.07)return{type:"curved",eigenRatio:o,spread:s,cloudUsed:t.length,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h};let p=g>.01?s/g:1;return{type:r&&(s<.05||p<.1)?"straight":"curved",eigenRatio:o,spread:s,cloudUsed:t.length,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h}}function Za(t){if(t.length<4)return[...t];let e=[{...t[0]}];for(let n=1;n<t.length-1;n++)e.push({x:(t[n-1].x+t[n].x+t[n+1].x)/3,z:(t[n-1].z+t[n].z+t[n+1].z)/3});return e.push({...t[t.length-1]}),e}function Ga(t,e,n=1.57){if(e<10)return[];let o=[];for(let x=0;x<e;x++){let y=(x+1)%e;o.push(Math.atan2(t[y].z-t[x].z,t[y].x-t[x].x))}let r=[o[0]];for(let x=1;x<e;x++){let y=o[x]-o[x-1];y>Math.PI?y-=2*Math.PI:y<-Math.PI&&(y+=2*Math.PI),r.push(r[x-1]+y)}let s=0;{let x=o[0]-o[e-1];x>Math.PI?x-=2*Math.PI:x<-Math.PI&&(x+=2*Math.PI),s=x}Math.abs(s)<.1;let i=[],a=5;for(let x=0;x<e;x++){let y=0;for(let m=-a;m<=a;m++)y+=r[(x+m+e)%e];i.push(y/(a*2+1))}let u=5,d=[];for(let x=0;x<e;x++){let y=i[(x+u)%e]-i[x];y>Math.PI?y-=2*Math.PI:y<-Math.PI&&(y+=2*Math.PI),d.push(y)}let f=10,c=.003,l=10,h=.05,g=[];for(let x=0;x<e;x++){let y=x,m=x,p=0,b=0,w=x,z=e+10;for(;z-- >0;){let P=d[w],E=Math.abs(P),S=P>0?1:P<0?-1:0;if(E<c){w=(w+1)%e;continue}if(b===0){b=S,p=E,m=w,w=(w+1)%e;continue}if(S!==b){let I=E,W=S,v=(w+1)%e,T=1;for(let R=0;R<l&&T<l*2;R++){let D=d[v],H=Math.abs(D);if(H<c){v=(v+1)%e;continue}(D>0?1:-1)===W?I+=H:(I-=H,I<0&&(W=-W,I=-I)),v=(v+1)%e,T++}if(W===b&&I>h){w=(w+1)%e;continue}break}if(p+=E,m=w,w=(w+1)%e,w===x)break}if((m>y?m-y+1:e-y+m+1)>=f&&p>=n){let P=y,E=(y+u)%e,S=(m-u+e)%e,I=m,W=t[P],v=t[E],T=t[S],R=t[I],D=Math.atan2(v.z-W.z,v.x-W.x),V=Math.atan2(R.z-T.z,R.x-T.x)-D;V>Math.PI?V-=2*Math.PI:V<-Math.PI&&(V+=2*Math.PI);let C=Math.abs(V);if(!g.some(A=>{function L(B,O,$){return B<=O?$>=B&&$<=O:$>=B||$<=O}let k=0,F=0;for(let B=y;F++,L(A.start,A.end,B)&&k++,B!==m;B=(B+1)%e);return F>0&&k/F>.7})){let A=g.findIndex(L=>{function k(O,$,G){return O<=$?G>=O&&G<=$:G>=O||G<=$}let F=0,B=0;for(let O=L.start;B++,k(y,m,O)&&F++,O!==L.end;O=(O+1)%e);return B>0&&F/B>.7});A>=0?p>g[A].cumAngle&&(g[A]={start:y,end:m,cumAngle:p,actualAngle:C}):g.push({start:y,end:m,cumAngle:p,actualAngle:C})}}}return g}function Ya(t,e){let s=new Array(e).fill(!1);for(let d=0;d<e;d++){let f=t[d],c=t[(d+15)%e],l=t[(d+30)%e],h=Math.atan2(c.z-f.z,c.x-f.x),g=Math.atan2(l.z-c.z,l.x-c.x),x=Math.abs(g-h);x>Math.PI&&(x=2*Math.PI-x),x>.07&&(s[(d+15)%e]=!0)}let i=new Set,a=[],u=-1;for(let d=0;d<e*2;d++){let f=d%e;if(s[f])u<0&&(u=f);else if(u>=0){let c=(f-1+e)%e,l=0;for(let h=u;l++,h!==c;h=(h+1)%e);if(l>=12){let h=`${u},${c}`;i.has(h)||(i.add(h),a.push({start:u,end:c}))}u=-1}}if(a.length>1){let d=a[0],f=a[a.length-1];f.end>e-3&&d.start<3&&(a[0]={start:f.start,end:d.end},a.pop())}return a}function Ua(t,e){let n=[10,15,20,25,30,40],o=.15,r=.15,s=.5,i=3,a=6,u=new Array(e).fill(0);for(let c of n)if(!(c>=e))for(let l=0;l<e;l+=i){let h=l+c;if(h>=e)break;let g=[];for(let w=l;w<=h;w++)g.push(t[w]);if(g.length<c-1)continue;let{cx:x,cz:y,r:m,rmsResidual:p}=mn(g);if(m<.1||p>o||m<s)continue;let b=0;for(let w=1;w<g.length;w++){let z=g[w-1].x-x,M=g[w-1].z-y,P=g[w].x-x,E=g[w].z-y;b+=Math.abs(Math.atan2(z*E-M*P,z*P+M*E))}if(!(b<r))for(let w=l;w<=h;w++)u[w]++}let d=[],f=-1;for(let c=0;c<e*2;c++){let l=c%e;if(u[l]>=2)f<0&&(f=l);else if(f>=0){let h=(l-1+e)%e,g=0;for(let x=f;g++,x!==h;x=(x+1)%e);g>=a&&d.push({start:f,end:h}),f=-1}}if(d.length>1){let c=d[0],l=d[d.length-1];l.end>e-3&&c.start<3&&(d[0]={start:l.start,end:c.end},d.pop())}return d}function Ka(t,e,n){function o(h,g){return h<=g?g-h+1:n-h+(g+1)}function r(h,g,x){return h<=g?x>=h&&x<=g:x>=h||x<=g}let s=o(t.start,t.end),i=o(e.start,e.end),a=s<=i?t.start:e.start,u=s<=i?t.end:e.end,d=s<=i?e.start:t.start,f=s<=i?e.end:t.end,c=0;for(let h=a;r(d,f,h)&&c++,h!==u;h=(h+1)%n);let l=s+i-c;return l>0?c/l:0}function Qa(t){if(t.length<2)return[];let e=[],n=.15,o=1.22,r=1.92;for(let a=0;a<t.length;a++){let u=t[a];for(let d=a+1;d<t.length;d++){let f=t[d],c=u.bx-u.ax,l=u.bz-u.az,h=f.bx-f.ax,g=f.bz-f.az,x=Math.sqrt(c*c+l*l),y=Math.sqrt(h*h+g*g);if(x<.01||y<.01)continue;let m=Math.abs(Math.atan2(c*g-l*h,c*h+l*g));if(m<o||m>r)continue;let p=[{x:u.ax,z:u.az},{x:u.bx,z:u.bz}],b=[{x:f.ax,z:f.az},{x:f.bx,z:f.bz}];for(let w of p)for(let z of b){let M=w.x-z.x,P=w.z-z.z;Math.sqrt(M*M+P*P)<n&&e.push({x:(w.x+z.x)/2,z:(w.z+z.z)/2})}}}if(e.length<2)return e;let s=[],i=new Array(e.length).fill(!1);for(let a=0;a<e.length;a++){if(i[a])continue;let u=e[a].x,d=e[a].z,f=1;i[a]=!0;for(let c=a+1;c<e.length;c++){if(i[c])continue;let l=e[c].x-u/f,h=e[c].z-d/f;Math.sqrt(l*l+h*h)<.3&&(u+=e[c].x,d+=e[c].z,f++,i[c]=!0)}s.push({x:u/f,z:d/f})}return s}function Js(t,e){if(t.length<6)return[];let n={minCurveAngle:.05,minCurvePoints:5,smoothWindow:3,...e},o=t.length,r=[];for(let T=0;T<o;T++){let R=t[(T-1+o)%o],D=t[T],H=t[(T+1)%o],V=D.x-R.x,C=D.z-R.z,_=H.x-D.x,A=H.z-D.z,L=Math.sqrt(V*V+C*C),k=Math.sqrt(_*_+A*A);if(L<.001||k<.001){r.push(0);continue}r.push(Math.atan2((V*A-C*_)/(L*k),(V*_+C*A)/(L*k)))}let s=[],i=n.smoothWindow;for(let T=0;T<o;T++){let R=0;for(let D=-i;D<=i;D++)R+=r[(T+D+o)%o];s.push(R/(i*2+1))}let a=.008,u=[],d=-1,f=0,c=new Set;for(let T=0;T<o*2;T++){let R=T%o,D=s[R],H=D>=0?1:-1;if(Math.abs(D)>a&&(f===0||f===H))d<0&&(d=R,f=H);else if(d>=0){let V=(R-1+o)%o,C=0,_=0;for(let A=d;C+=Math.abs(s[A]),_++,A!==V;A=(A+1)%o);if(_>=n.minCurvePoints&&C>n.minCurveAngle){let A=`${d},${V}`;c.has(A)||(c.add(A),u.push({start:d,end:V,sign:f}))}d=-1,f=0}}if(u.length>1){let T=u[0],R=u[u.length-1];T.sign===R.sign&&R.end>o-3&&T.start<3&&(u[0]={start:R.start,end:T.end,sign:T.sign},u.pop())}{let T=s.filter(V=>Math.abs(V)>.001).map(V=>Math.abs(V)),R=T.length>0?Math.max(...T):0,D=T.length>0?T.reduce((V,C)=>V+C,0)/T.length:0,H=u.reduce((V,C)=>{let _=0;for(let A=C.start;_++,A!==C.end;A=(A+1)%o);return V+_},0)}let l=Ya(t,o),h=Ua(t,o),g=[...u,...l.map(T=>({start:T.start,end:T.end,sign:1})),...h.map(T=>({start:T.start,end:T.end,sign:1}))],x=u.length,m=g.map((T,R)=>{let D=[];for(let G=T.start;D.push({...t[G]}),G!==T.end;G=(G+1)%o);let H=0;for(let G=1;G<D.length;G++)s[(T.start+G-1)%o]*s[(T.start+G)%o]<-1e-8&&H++;let{cx:V,cz:C,r:_,rmsResidual:A}=mn(D),L=0,k=0;for(let G=1;G<D.length;G++){let Q=D[G-1].x-V,j=D[G-1].z-C,Z=D[G].x-V,U=D[G].z-C,it=Math.atan2(Q*U-j*Z,Q*Z+j*U);L+=Math.abs(it),k+=it}let F=Se(D),B=H===0?.12:.08;if(_>.1&&A>B||_>.1&&_>Math.max(F*15,8)||Math.abs(k)<.05)return null;let O=R<x?"turning":"sliding",$=O!=="turning"?Math.abs(k)>.35?"C_curve":"S_curve":H>0?"S_curve":"C_curve";return{seg:{points:D,type:$,radius:_,centerX:V,centerZ:C,arcAngle:L,arcLength:F},start:T.start,end:T.end,source:O}}).filter(T=>T!==null),p=new Set;function b(T,R){return T<=R?R-T+1:o-T+(R+1)}function w(T,R,D,H){for(let V=T;;V=(V+1)%o){let C;if(D<=H?C=V>=D&&V<=H:C=V>=D||V<=H,!C)return!1;if(V===R)break}return!0}for(let T=0;T<m.length;T++)if(!p.has(T))for(let R=T+1;R<m.length;R++){if(p.has(R))continue;if(Ka({start:m[T].start,end:m[T].end},{start:m[R].start,end:m[R].end},o)>.6){let V=(m[T].seg.arcAngle>=m[R].seg.arcAngle?T:R)===T?R:T;if(m[T].seg.arcAngle>=m[R].seg.arcAngle)p.add(R);else{p.add(T);break}}else{let H=b(m[T].start,m[T].end),V=b(m[R].start,m[R].end),C=H<=V?T:R,_=H<=V?R:T;w(m[C].start,m[C].end,m[_].start,m[_].end)&&b(m[C].start,m[C].end)/b(m[_].start,m[_].end)<.7&&p.add(C)}}let z=m.filter((T,R)=>!p.has(R)),M=10,P=10,E=30,S=15,I=[],W=0;if(z.length>0){for(let D of z)D.start=(D.start-S+o)%o,D.end=(D.end+S)%o;let T=[...z].sort((D,H)=>D.start-H.start),R=T[0];for(let D=1;D<T.length;D++){let H=T[D],V=H.start-R.end;V<0&&(R.end>o*.5&&H.start<o*.5?V=H.start+o-R.end:V=0);let C=V===0,_=V<=E&&C;if(!_&&V<=E){let A=Math.sqrt((R.seg.centerX-H.seg.centerX)**2+(R.seg.centerZ-H.seg.centerZ)**2),L=Math.max(R.seg.radius,H.seg.radius),k=Math.min(R.seg.radius,H.seg.radius),F=k>0?L/k:99;_=A<P&&F<M}if(_){let A=Math.min(R.start,H.start),L=Math.max(R.end,H.end),k=[];for(let G=A;k.push({...t[G]}),G!==L;G=(G+1)%o);let{cx:F,cz:B,r:O}=mn(k),$=0;for(let G=1;G<k.length;G++){let Q=k[G-1].x-F,j=k[G-1].z-B,Z=k[G].x-F,U=k[G].z-B;$+=Math.abs(Math.atan2(Q*U-j*Z,Q*Z+j*U))}R={seg:{points:k,type:"C_curve",radius:O,centerX:F,centerZ:B,arcAngle:$,arcLength:Se(k)},start:R.start,end:H.end,source:R.source},W++}else I.push(R),R=H}I.push(R)}{if(I.length>1){let R=I.map((D,H)=>{if(H===0)return 0;let V=D.start-I[H-1].end;return V<0&&(V=D.start+o-I[H-1].end),V})}let T=[];for(let R=0;R<I.length;R++){if(T.length===0){T.push(I[R]);continue}let D=T[T.length-1],H=I[R].start-D.end;if(H<0&&(H=I[R].start+o-D.end),H<=30){let V=[...D.seg.points];for(let k=(D.end+1)%o;V.push({...t[k]}),k!==I[R].end;k=(k+1)%o);let{cx:C,cz:_,r:A}=mn(V),L=0;for(let k=1;k<V.length;k++){let F=V[k-1].x-C,B=V[k-1].z-_,O=V[k].x-C,$=V[k].z-_;L+=Math.abs(Math.atan2(F*$-B*O,F*O+B*$))}T[T.length-1]={seg:{points:V,type:"C_curve",radius:A,centerX:C,centerZ:_,arcAngle:L,arcLength:Se(V)},start:D.start,end:I[R].end,source:D.source}}else T.push(I[R])}I=T}return I.map((T,R)=>{let D=T.seg;return D.candidateId=R,D})}function Ja(t){let e=t.length;if(e<6)return null;let n=Ga(t,e,.35);if(n.length===0)return null;let o=n.filter(l=>l.actualAngle!=null&&l.actualAngle>.17);if(o.length===0)return null;o.sort((l,h)=>{let g=l.end>=l.start?l.end-l.start+1:e-l.start+l.end+1;return(h.end>=h.start?h.end-h.start+1:e-h.start+h.end+1)-g});let r=o[0],s=[];for(let l=r.start;s.push({...t[l]}),l!==r.end;l=(l+1)%e);if(s.length<10)return null;let{cx:i,cz:a,r:u,rmsResidual:d}=mn(s);if(u>.1&&d>.1)return null;let f=r.actualAngle,c=s.reduce((l,h,g)=>g===0?0:l+Math.hypot(h.x-s[g-1].x,h.z-s[g-1].z),0);return u>.3&&f>.17?{points:s,type:"C_curve",radius:u,centerX:i,centerZ:a,arcAngle:f,arcLength:c}:null}function ho(t,e,n){let o={...nr,...n},r=Date.now(),s=t,i=Array.isArray(e)?e:[],a=[];if(i.length>10)try{for(let T of i){let R=T.x1,D=T.z1,H=T.x2,V=T.z2;if(R==null||D==null||H==null||V==null){let C=T.start?.x,_=T.start?.y,A=T.end?.x,L=T.end?.y;if(C==null||_==null||A==null||L==null)continue;a.push({ax:C,az:_,bx:A,bz:L})}else a.push({ax:R,az:D,bx:H,bz:V})}let v=.1*.1;if(a.length>5){let T=[],R=[],D=new Array(a.length).fill(0);for(let V of t){let C=1/0,_=-1;for(let A=0;A<a.length;A++){let L=a[A],k=L.bx-L.ax,F=L.bz-L.az,B=k*k+F*F,O;if(B<1e-8)O=(V.x-L.ax)**2+(V.y-L.az)**2;else{let $=((V.x-L.ax)*k+(V.y-L.az)*F)/B;$=Math.max(0,Math.min(1,$));let G=L.ax+$*k,Q=L.az+$*F;O=(V.x-G)**2+(V.y-Q)**2}if(O<C&&(C=O,_=A),C<=v)break}C<=v&&(R.push(V),T.push(_),D[_]++)}let H=R;if(o.lineDensityMin>0&&o.lineDensityMinLen>0){let V=new Set;for(let C=0;C<a.length;C++){let _=a[C],A=Math.hypot(_.bx-_.ax,_.bz-_.az);if(A>=o.lineDensityMinLen)continue;(A>.01?D[C]/A:0)<o.lineDensityMin&&V.add(C)}if(V.size>0){let C=[];for(let _=0;_<R.length;_++)V.has(T[_])||C.push(R[_]);H=C}}s=H}}catch(v){console.warn("[\u70B9\u4E91\u8F6E\u5ED3] \u7EBF\u6BB5\u8FC7\u6EE4\u5931\u8D25:",v)}let u=Qa(a),d=[];for(let v of s){let T=v.z;T<o.heightMin||T>o.heightMax||d.push({x:v.x,z:v.y})}if(d.length<50)return tn(e,r);if(o.sparseRadius>0){let v=new Map,T=o.cellSize;for(let V of d){let C=`${Math.round(V.x/T)},${Math.round(V.z/T)}`;v.has(C)||v.set(C,[]),v.get(C).push(V)}let R=[],D=Math.ceil(o.sparseRadius/T);for(let V=-D;V<=D;V++)for(let C=-D;C<=D;C++)R.push(`${V},${C}`);let H=[];for(let V of d){let C=Math.round(V.x/T),_=Math.round(V.z/T),A=0;for(let L of R){let[k,F]=L.split(",").map(Number),B=v.get(`${C+k},${_+F}`);if(B&&(A+=B.length),A>=o.sparseMinNeighbors)break}A>=o.sparseMinNeighbors&&H.push(V)}d.length=0;for(let V=0;V<H.length;V++)d.push(H[V])}if(d.length<50)return tn(e,r);let f=Date.now(),{cells:c}=fo(d,o.cellSize),l=new Set;if(o.densityRatio>0&&c.size>10){let v=Array.from(c.values()).sort((D,H)=>D-H),T=v[Math.floor(v.length/2)],R=Math.max(o.densityThreshold,Math.ceil(T*o.densityRatio));for(let[D,H]of c)H>=R&&l.add(D);if(l.size<o.minComponentCells){l.clear();for(let[D,H]of c)H>=o.densityThreshold&&l.add(D)}}else for(let[v,T]of c)T>=o.densityThreshold&&l.add(v);if(l.size<o.minComponentCells)return tn(e,r);let h=l;o.closeIterations>0&&(h=Wn(l,o.closeIterations));let g=Date.now(),x=tr(h).filter(v=>v.size>=o.minComponentCells);x.sort((v,T)=>T.size-v.size);let y=20;x.length>y&&(x=x.slice(0,y));let m=Date.now(),p=[];for(let v of x){let T=er(v,o.cellSize);T.length>=4&&p.push(T)}let b=Date.now(),w=p.map(v=>{let T=Hn(v,o.simplifyEpsilon);if(T.length>=7)for(let R=0;R<2;R++){let D=T.length,H=T.map(V=>({x:V.x,z:V.z}));for(let V=0;V<D;V++){let C=T[(V-1+D)%D],_=T[V],A=T[(V+1)%D],L=_.x-C.x,k=_.z-C.z,F=A.x-_.x,B=A.z-_.z,O=Math.sqrt(L*L+k*k),$=Math.sqrt(F*F+B*B);if(O<.001||$<.001)continue;if(Math.abs(Math.atan2(L*B-k*F,L*F+k*B))>1.8){let Q=[-2,-1,0,1,2].map(U=>T[(V+U+D)%D]),j=Q.map(U=>U.x).sort((U,it)=>U-it),Z=Q.map(U=>U.z).sort((U,it)=>U-it);H[V]={x:j[2],z:Z[2]}}}T=H}if(T=Jo(T),u.length>0&&T.length>=5)for(let R of u){let D=1/0,H=-1;for(let V=0;V<T.length;V++){let C=T[V].x-R.x,_=T[V].z-R.z,A=C*C+_*_;A<D&&(D=A,H=V)}H>=0&&D<.3*.3&&(T[H]={x:R.x,z:R.z})}if(T.length>2){let R=T[0],D=T[T.length-1];(Math.abs(R.x-D.x)>.001||Math.abs(R.z-D.z)>.001)&&T.push({...R})}return T}),z=o.cellSize/2;function M(v){let T=0;for(let R=0;R<v.length;R++){let D=(R+1)%v.length;T+=v[R].x*v[D].z-v[D].x*v[R].z}return T/2}let P=w.map(v=>{if(v.length<3)return v;let T=M(v)>=0;return v.map((R,D)=>{let H=v[(D-1+v.length)%v.length],V=v[(D+1)%v.length],C=V.x-H.x,_=V.z-H.z;if(Math.sqrt(C*C+_*_)<.001)return{...R};let L=T?_:-_,k=T?-C:C,F=Math.sqrt(L*L+k*k);return F<.001?{...R}:{x:R.x+L/F*z,z:R.z+k/F*z}})}),E=Date.now(),S=[];for(let v=0;v<P.length;v++){let T=P[v];T.length<3||S.push({id:-v-1,points:T,length:Se(T),pointCount:T.length,type:"straight",isMain:!1})}let I=Date.now()-r,W=[];for(let v of w){let T=Js(v);for(let D of T)W.push(D);let R=Ja(v);R&&W.push(R)}return{mainWalls:[],noiseWalls:[],contours:S,curvedSegments:W,filteredPoints:s,stats:{totalRawLines:e?.length??t.length,totalWalls:0,mainCount:0,noiseCount:S.length,elapsedMs:I}}}function ti(t,e){let n=e?.cellSize??.05,o=e?.closeIterations??2,r=e?.minComponentCells??10,s=e?.simplifyEpsilon??.02,i=Date.now();if(t.length<3)return tn(t,i);let a=t.map(S=>({x1:S.x1,z1:S.z1,x2:S.x2,z2:S.z2})),{active:u}=Ks(a,n);if(u.size<r)return tn(t,i);let d=Date.now(),f=Wn(u,o),c=Date.now(),l=tr(f).filter(S=>S.size>=r);if(l.length===0)return tn(t,i);let h=l.reduce((S,I)=>S.size>=I.size?S:I),g=Date.now(),x=er(h,n);if(x.length<4)return tn(t,i);let y=Date.now(),m=Hn(x,s);if(m=Jo(m),m.length>2){let S=m[0],I=m[m.length-1];(Math.abs(S.x-I.x)>.001||Math.abs(S.z-I.z)>.001)&&m.push({...S})}let p=n/2;function b(S){let I=0;for(let W=0;W<S.length;W++){let v=(W+1)%S.length;I+=S[W].x*S[v].z-S[v].x*S[W].z}return I/2}let w=b(m)>=0,z=m.map((S,I)=>{let W=m[(I-1+m.length)%m.length],v=m[(I+1)%m.length],T=v.x-W.x,R=v.z-W.z;if(Math.sqrt(T*T+R*R)<.001)return{...S};let H=w?R:-R,V=w?-T:T,C=Math.sqrt(H*H+V*V);return C<.001?{...S}:{x:S.x+H/C*p,z:S.z+V/C*p}}),M=Date.now(),P=[];z.length>=3&&P.push({id:-1,points:z,length:Se(z),pointCount:z.length,type:"straight",isMain:!0});let E=Date.now()-i;return{mainWalls:[],noiseWalls:[],contours:P,stats:{totalRawLines:t.length,totalWalls:0,mainCount:0,noiseCount:P.length,elapsedMs:E}}}function tn(t,e){return{mainWalls:[],noiseWalls:[],stats:{totalRawLines:t?.length??0,totalWalls:0,mainCount:0,noiseCount:0,elapsedMs:Date.now()-e}}}function mo(t,e,n){if(!t.length||n<=1)return t;let o=new Map;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.y/e),f=`${u},${d}`;o.has(f)||o.set(f,[]),o.get(f).push(a)}let r=new Map;for(let[a,u]of o){let[d,f]=a.split(","),c=parseInt(d),l=parseInt(f),h=0;for(let g=-2;g<=2;g++)for(let x=-2;x<=2;x++){let y=`${c+g},${l+x}`;h+=o.get(y)?.length??0}r.set(a,h)}let s=[],i=0;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.y/e),f=`${u},${d}`;(r.get(f)??0)>=n?s.push(a):i++}return s}function po(t,e,n){if(!t.length||n<=1||e<=0)return t;let o=new Map;for(let s of t){let i=Math.floor(s.x/e),a=Math.floor(s.y/e),u=Math.floor(s.z/e),d=`${i},${a},${u}`;o.has(d)||o.set(d,[]),o.get(d).push(s)}let r=[];for(let s of o.values())if(s.length>=n){let i=s.reduce((d,f)=>d+f.x,0)/s.length,a=s.reduce((d,f)=>d+f.y,0)/s.length,u=s.reduce((d,f)=>d+f.z,0)/s.length;r.push({x:i,y:a,z:u})}return r}function ei(t,e,n){let o=po(t,e,n),r=t.length-o.length;return o}function ni(t){let e=[];for(let d of t){let f=d.originalPoints??d.points??[];if(!Array.isArray(f)||f.length<2)continue;let c=Math.min(f.length,200);for(let l=1;l<c;l++){let h=Math.hypot(f[l].x-f[l-1].x,f[l].y-f[l-1].y,f[l].z-f[l-1].z);h>.001&&e.push(h)}}let n={voxelSize:.08,minPoints:3,minCentroids:3};if(e.length<10)return n;e.sort((d,f)=>d-f);let o=e[Math.floor(e.length/2)],r=Math.max(.03,Math.min(.25,o*3.5));r=Math.round(r*100)/100;let s=Math.pow(r/o,2),i=Math.max(2,Math.min(5,Math.round(s/4))),a=e.length/Math.max(1,t.length),u=Math.max(2,Math.min(4,Math.round(a/50)));return{voxelSize:r,minPoints:i,minCentroids:u}}function oi(t,e,n,o,r=3){if(!t.length||n<=0||o<=1)return{data:t,lines:e,removedCount:0};let s=[],i=[],a=0;for(let u=0;u<t.length;u++){let d=t[u],f=d.originalPoints??d.points??[];if(!Array.isArray(f)||f.length===0){s.push(d),i.push(e[u]);continue}let c=po(f,n,o);c.length>=r?(d.originalPoints&&(d.originalPoints=c),d.points&&(d.points=c),s.push(d),i.push(e[u])):a++}return{data:s,lines:i,removedCount:a}}function ri(t,e,n,o){let r=[],s=[],i=0;for(let a=0;a<t.length;a++){let u=t[a],d=u.originalPoints??u.points??[],c=(Array.isArray(d)?d.filter(y=>y&&y.x!==void 0&&y.y!==void 0):[]).length;if(c===0){r.push(u),s.push({...e[a],id:r.length-1});continue}let l=u.start??u,h=u.end??u,g=Math.hypot(h.x??0-l.x??0,h.y??0-l.y??0);if(g>=o||g<=.01){r.push(u),s.push({...e[a],id:r.length-1});continue}if(c/g<n){i++;continue}r.push(u),s.push({...e[a],id:r.length-1})}return{data:r,lines:s}}function en(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u===0)return Math.hypot(t-n,e-o);let d=((t-n)*i+(e-o)*a)/u;return d=Math.max(0,Math.min(1,d)),Math.hypot(t-(n+d*i),e-(o+d*a))}function or(t,e,n=.2){for(let o of e)if(en(o.x,o.z,t.x1,t.z1,t.x2,t.z2)<=n)return!0;return!1}function si(t,e,n=.2){let o=1/0,r={x:0,z:0};for(let i of e){let a=en(i.x,i.z,t.x1,t.z1,t.x2,t.z2);a<o&&(o=a,r=i)}return o<=n}function ii(t,e,n,o=!0){function g(b,w,z,M){let P=z.points[0],E=z.points[z.points.length-1];return Math.hypot(b-P.x,w-P.z)<=M||Math.hypot(b-E.x,w-E.z)<=M}function x(b,w,z,M){for(let P=0;P<z.points.length-1;P++)if(en(b,w,z.points[P].x,z.points[P].z,z.points[P+1].x,z.points[P+1].z)<=M)return!0;return!1}function y(b,w,z){let M=0,P=b[0],E=b[b.length-1];for(let S of w)if(g(P.x,P.z,S,z)||x(P.x,P.z,S,z)){M++;break}for(let S of w)if(g(E.x,E.z,S,z)||x(E.x,E.z,S,z)){M++;break}return M}function m(b){if(b.length<2)return 0;if(b.length<=30){let P=0;for(let E=0;E<b.length;E++)for(let S=E+1;S<b.length;S++){let I=Math.hypot(b[E].x-b[S].x,b[E].z-b[S].z);I>P&&(P=I)}return P}let w=Math.max(1,Math.floor(b.length/15)),z=[];for(let P=0;P<b.length;P+=w)z.push(b[P]);z[z.length-1]!==b[b.length-1]&&z.push(b[b.length-1]);let M=0;for(let P=0;P<z.length;P++)for(let E=P+1;E<z.length;E++){let S=Math.hypot(z[P].x-z[E].x,z[P].z-z[E].z);S>M&&(M=S)}return M}let p=[];for(let b of t){let w=b.points,z=w.length>=2?m(w):0,M=b.candidateId!==void 0?`#${b.candidateId}`:"#?";if(o&&console.log(`[\u5F27\u6BB5\u5206\u6790] ${M} r=${b.radius.toFixed(3)}m \u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0 \u5F26\u957F=${z.toFixed(3)}m pts=${w.length}`),b.radius<.2){o&&console.log(` \u2192 \u534A\u5F84<${.2}m \u8FC7\u6EE4`);continue}if(w.length<3){o&&console.log(" \u2192 \u70B9\u6570<3 \u8FC7\u6EE4");continue}let P=z/b.radius,E=z/2,S=0;E<=b.radius?S=b.radius-Math.sqrt(b.radius*b.radius-E*E):S=z;let I=z>.01?S/z:0,W=y(w,e,.8),v=b.arcAngle>1.745&&b.radius<5,T=b.radius>15&&P>.15&&w.length>=10,R=P>.12&&b.radius>1&&b.radius<4&&b.arcAngle>=.35&&w.length>=3;if(o&&console.log(` \u7279\u5F81: \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} \u5F13\u9AD8/\u5F26\u957F=${I.toFixed(4)} \u8FDE\u63A5=${W}/2${v?" \u5927\u5F27\u89D2\u8C41\u514D":""}${T?" \u5927\u534A\u5F84\u7ED3\u6784\u5F27":""}${R?" \u5408\u7406\u77ED\u5F27\u8C41\u514D":""}`),w.length>20&&b.arcAngle>5.2&&b.radius>.2){o&&console.log(` \u2192 \u8FD1\u95ED\u5408\u5706\u8C41\u514D(\u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0, ${w.length}pts)`),p.push(b);continue}if(P<.15)if(W>=2&&b.arcAngle>=.35)o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} < ${.15}\uFF0C\u4F46\u4E24\u7AEF\u8FDE\u63A5+\u5F27\u89D2>20\xB0\uFF0C\u8C41\u514D`);else{o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} < ${.15}\uFF0C\u8F6E\u5ED3\u6296\u52A8\u5047\u5F27 \u8FC7\u6EE4`);continue}let D=W>=2?.025:.035;if(I<D&&!v&&!R&&!T){o&&console.log(` \u2192 \u5F13\u9AD8/\u5F26\u957F=${I.toFixed(4)} < ${D}(${W>=2?"\u5BBD\u677E":"\u6807\u51C6"})\uFF0C\u6781\u6241\u5F27(\u76F4\u7EBF\u6BB5\u566A\u58F0) \u8FC7\u6EE4`);continue}if(!v&&!R&&!T){if(b.arcAngle<.17){o&&console.log(` \u2192 \u5F27\u89D2<${(.17*180/Math.PI).toFixed(0)}\xB0 \u8FC7\u6EE4`);continue}if(b.arcAngle<.35&&W<2){o&&console.log(` \u2192 \u5F27\u89D2<${(.35*180/Math.PI).toFixed(0)}\xB0 \u4E14\u672A\u4E24\u7AEF\u8FDE\u63A5 \u8FC7\u6EE4`);continue}}if(P>2.01){o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} > 2.01\uFF0C\u5F26\u957F\u8D85\u8FC7\u76F4\u5F84\uFF0C\u5F02\u5E38 \u8FC7\u6EE4`);continue}if(!v&&!T){let H=W>=2?2.5:1.5,V=Math.floor(w.length/2),C=w[V].x-w[0].x,_=w[V].z-w[0].z,A=w[w.length-1].x-w[V].x,L=w[w.length-1].z-w[V].z,k=Math.atan2(_,C),F=Math.atan2(L,A),B=Math.abs(F-k);if(B>Math.PI&&(B=2*Math.PI-B),B>60*Math.PI/180&&B>b.arcAngle*H){o&&console.log(` \u2192 V\u5F62\u62D0\u89D2\u8FC7\u6EE4 \u8F6C\u89D2=${(B*180/Math.PI).toFixed(1)}\xB0 > \u5F27\u89D2\xD7${H.toFixed(1)}=${(b.arcAngle*180/Math.PI*H).toFixed(1)}\xB0`);continue}}if(n&&n.length>5&&!(b.radius>15&&b.arcAngle>.26)){let H=w[0],V=w[w.length-1],C=.3,_=null,A=1/0,L=null,k=1/0;for(let F of n){let B=en(H.x,H.z,F.x1,F.z1,F.x2,F.z2),O=en(V.x,V.z,F.x1,F.z1,F.x2,F.z2);B<C&&B<A&&(_=F,A=B),O<C&&O<k&&(L=F,k=O)}if(!_||!L)console.log(` [\u7EBF\u6BB5\u8F6C\u89D2\u8C03\u8BD5 #${M}] \u672A\u627E\u5230\u5B8C\u6574\u7EBF\u6BB5: \u8D77\u7AEF=${!!_}(d=${A.toFixed(3)}) \u672B\u7AEF=${!!L}(d=${k.toFixed(3)})`);else if(_===L){console.log(" \u2192 \u7EBF\u6BB5\u8F6C\u89D2\u9A8C\u8BC1: \u5F27\u4E24\u7AEF\u5728\u540C\u4E00\u6761\u7EBF\u6BB5\u4E0A, \u76F4\u7EBF\u566A\u58F0\u5047\u5F27 \u8FC7\u6EE4");continue}else{let F=_.x2-_.x1,B=_.z2-_.z1,O=L.x2-L.x1,$=L.z2-L.z1,G=Math.sqrt(F*F+B*B),Q=Math.sqrt(O*O+$*$);if(G>.01&&Q>.01){let j=Math.abs(Math.atan2(F*$-B*O,F*O+B*$)),Z=j*180/Math.PI;if(console.log(` [\u7EBF\u6BB5\u8F6C\u89D2\u8C03\u8BD5 #${M}] \u5F27\u4E24\u7AEF\u6BB5\u5939\u89D2=${Z.toFixed(1)}\xB0 \u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0`),j>.35&&j<3.05){let U=j>2.09;if(!U||b.arcAngle<.44){console.log(` \u2192 \u7EBF\u6BB5\u8F6C\u89D2\u9A8C\u8BC1: \u5F27\u4E24\u7AEF\u6BB5\u5939\u89D2=${Z.toFixed(1)}\xB0\uFF0C${U?"\u659C\u5899":"\u6298\u5899"}\u78E8\u5706\u5047\u5F27 \u8FC7\u6EE4`);continue}}}}}o&&console.log(" \u2192 \u2713 \u4FDD\u7559"),p.push(b)}return o&&console.log(`[\u56DE\u52A0\u5224\u65AD] ${t.length} \u2192 ${p.length} \u4E2A\u5F27\u6BB5`),p}function li(t,e){for(let n=0;n<t.length;n++){let o=t[n],r=[];for(let i of e)or(i,o.points)&&r.push({id:i.id,len:Math.hypot(i.x2-i.x1,i.z2-i.z1)});r.sort((i,a)=>a.len-i.len);let s=r.map(i=>`#${i.id}(${i.len.toFixed(2)}m)`).join(", ");console.log(`[\u5F27\u6BB5\u539F\u59CB\u7EBF\u6BB5] #${n} r=${o.radius.toFixed(3)}m \u2192 ${r.length} \u6761: ${s}`)}}var sr={gridSize:2,connectDist:.8,angleTol:25,minLength:.8,minPointCount:3,hardCornerThreshold:30,minArea:5,minCurvePoints:6,curveStraightDist:.25,longWallMinLength:5};function ai(t){if(!t.length)return{rotated:[],mainAngle:0};let e=180,n=new Array(e).fill(0),o=Math.PI/e;for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Te(c,l),x=Math.floor(g/o);x>=e&&(x=e-1),n[x]+=h}let r=new Array(e).fill(0),s=5;for(let f=0;f<e;f++){let c=0;for(let l=-s;l<=s;l++){let h=f+l;h<0&&(h+=e),h>=e&&(h-=e),c+=n[h]}r[f]=c}let i=0,a=0;for(let f=0;f<e;f++)r[f]>i&&(i=r[f],a=f);let u=(a+.5)*o;return{rotated:t.map(f=>{let c=In(f.x1,f.z1,-u),l=In(f.x2,f.z2,-u);return{...f,x1:c.x,z1:c.z,x2:l.x,z2:l.z}}),mainAngle:u}}function ci(t,e){let n=new Map;for(let o of t){let r=(o.x1+o.x2)/2,s=(o.z1+o.z2)/2,i=Math.floor(r/e),a=Math.floor(s/e),u=`${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}function ui(t,e,n){if(t.length===0)return[];let o=new Set,r=[];for(let s of t){if(o.has(s.id))continue;let i=[s],a=[],u=new Set([s.id]);o.add(s.id);let d=!0;for(;d;){d=!1;let l=i[i.length-1],h=l.x2,g=l.z2,x=Te(l.x2-l.x1,l.z2-l.z1),y=null,m=e;for(let p of t){if(o.has(p.id))continue;let b=ze(h,g,p.x1,p.z1),w=ze(h,g,p.x2,p.z2),z=w<b,M=Math.min(b,w);if(M>m)continue;let P=z?Te(p.x1-p.x2,p.z1-p.z2):Te(p.x2-p.x1,p.z2-p.z1);Math.abs(Vn(P-x))>n||(y=p,m=M)}if(y){o.add(y.id),u.add(y.id);let p=i[i.length-1],b=ze(p.x2,p.z2,y.x1,y.z1),w=ze(p.x2,p.z2,y.x2,y.z2);b<=w?i.push(y):i.push({id:y.id,x1:y.x2,z1:y.z2,x2:y.x1,z2:y.z1}),d=!0}}for(d=!0;d;){d=!1;let l=a.length>0?a[a.length-1]:i[0],h=a.length>0?l.x2:l.x1,g=a.length>0?l.z2:l.z1,x=a.length>0?Te(l.x2-l.x1,l.z2-l.z1):Te(l.x2-l.x1,l.z2-l.z1),y=null,m=e;for(let p of t){if(o.has(p.id))continue;let b=ze(h,g,p.x1,p.z1),w=ze(h,g,p.x2,p.z2),z=w<b,M=Math.min(b,w);if(M>m)continue;let P=z?Te(p.x1-p.x2,p.z1-p.z2):Te(p.x2-p.x1,p.z2-p.z1);Math.abs(Vn(P-x))>n||(y=p,m=M)}if(y){o.add(y.id),u.add(y.id);let p=ze(h,g,y.x1,y.z1),b=ze(h,g,y.x2,y.z2);p<=b?a.push({id:y.id,x1:y.x2,z1:y.z2,x2:y.x1,z2:y.z1}):a.push({...y}),d=!0}}let f=[];for(let l=a.length-1;l>=0;l--){let h=a[l];l===a.length-1&&f.push({x:h.x1,z:h.z1}),f.push({x:h.x2,z:h.z2})}for(let l=0;l<i.length;l++){let h=i[l];l===0&&f.length===0&&f.push({x:h.x1,z:h.z1}),f.push({x:h.x2,z:h.z2})}let c=[];for(let l of f){if(c.length>0){let h=c[c.length-1];if(Math.abs(l.x-h.x)<.001&&Math.abs(l.z-h.z)<.001)continue}c.push(l)}if(c.length>=2){let l=Se(c);r.push({points:c,length:l,pointCount:i.length+a.length,lineIds:[...u]})}}return r}function fi(t){if(t.length<3)return[];let e=[];for(let n=1;n<t.length-1;n++){let o=t[n].x-t[n-1].x,r=t[n].z-t[n-1].z,s=t[n+1].x-t[n].x,i=t[n+1].z-t[n].z,a=Math.atan2(r,o),u=Math.atan2(i,s),d=Vn(u-a);e.push(d)}return e}function di(t,e=6){if(t.length<3)return"straight";let n=t[0],o=t[t.length-1],r=o.x-n.x,s=o.z-n.z;if(Math.sqrt(r*r+s*s)>.1){let f=0;for(let c of t){let l=Cn(c.x,c.z,n.x,n.z,o.x,o.z);l>f&&(f=l)}if(f<.05)return"straight"}let a=fi(t);if(a.reduce((f,c)=>f+Math.abs(c),0)<.1||t.length<e)return"straight";let d=0;for(let f=0;f<a.length-1;f++)a[f]*a[f+1]<0&&d++;return d>0?"S_curve":"C_curve"}function hi(t,e){if(t.length<3)return[t];let n=uo(e),o=fi(t),r=[],s=0;for(let i=0;i<o.length;i++)Math.abs(o[i])>n&&(r.push(t.slice(s,i+2)),s=i+1);return s<t.length-1&&r.push(t.slice(s)),r}function mi(t,e,n=6){let o=[],r=0;for(let s of t){let i=hi(s.points,e);for(let a of i){if(a.length<2)continue;let u=di(a,n),d=Se(a),f=d/(s.length||1),c=Math.max(1,Math.round(s.pointCount*f));o.push({id:r++,points:a,length:d,pointCount:c,lineIds:s.lineIds,type:u,isMain:!1})}}return o}function pi(t,e){if(t.length<3)return new Set;let n=.15;function o(d,f){return Math.round(d/n)+"_"+Math.round(f/n)}let r=new Map,s=0;function i(d,f){let c=o(d,f),l=r.get(c);if(l)return l;let h=Math.round(d/n),g=Math.round(f/n),x=null,y=n*n;for(let m=-1;m<=1;m++)for(let p=-1;p<=1;p++){let b=h+m+"_"+(g+p),w=r.get(b);if(!w)continue;let z=w.x-d,M=w.z-f,P=z*z+M*M;P<y&&(y=P,x=w)}return x?(r.set(c,x),x):(l={id:s++,x:d,z:f,edges:[]},r.set(c,l),l)}for(let d of t){if(d.points.length<2)continue;let f=d.points[0],c=d.points[d.points.length-1],l=i(f.x,f.z),h=i(c.x,c.z);if(l.id===h.id)continue;let g={to:h,wallId:d.id,visited:!1,twin:null},x={to:l,wallId:d.id,visited:!1,twin:g};g.twin=x,l.edges.push(g),h.edges.push(x)}for(let d of r.values())d.edges.sort((f,c)=>{let l=Math.atan2(f.to.z-d.z,f.to.x-d.x),h=Math.atan2(c.to.z-d.z,c.to.x-d.x);return l-h});let a=new Set,u=new Set;for(let d of r.values())for(let f of d.edges)u.add(f);for(let d of u){if(d.visited)continue;let f=[],c=d,l=u.size+10;for(;!c.visited&&f.length<l;){c.visited=!0,f.push(c);let h=c.to,g=c.twin,x=h.edges.indexOf(g);if(x<0)break;let y=(x+1)%h.edges.length;c=h.edges[y]}if(f.length>=3){let h=f[0];if(f[f.length-1].to.id===h.from?.id){let x=[],y=new Set,m=null;for(let p of r.values())if(p.edges.includes(f[0])){m=p;break}if(!m)continue;x.push({x:m.x,z:m.z});for(let p of f)y.add(p.wallId),x.push({x:p.to.x,z:p.to.z});if(f[f.length-1].to.id===m.id&&Qo(x)>=e)for(let b of y)a.add(b)}}}return a}function pn(t,e){let n={...sr,...e},o=Date.now(),{rotated:r,mainAngle:s}=ai(t),i=Date.now(),a=ci(r,n.gridSize),u=Date.now(),d=uo(n.angleTol),f=[];for(let v of a.values()){let T=v.map(D=>({id:D.id,x1:D.x1,z1:D.z1,x2:D.x2,z2:D.z2})),R=ui(T,n.connectDist,d);f.push(...R)}let c=Date.now(),l=new Set,h=f.filter(v=>{let T=v.length>=n.minLength||v.pointCount>=n.minPointCount;return T&&l.add(v),T}),g=f.filter(v=>!l.has(v)),x=Date.now(),y=mi(h,n.hardCornerThreshold,n.minCurvePoints),m=Date.now(),p=pi(y,n.minArea);y.forEach(v=>{v.isMain=p.has(v.id),v.length>=n.longWallMinLength&&(v.isMain=!0)});let b=Date.now();function w(v,T,R=.1){let D=v[0],H=v[v.length-1],V=T[0],C=T[T.length-1];return Math.hypot(D.x-V.x,D.z-V.z)<R||Math.hypot(D.x-C.x,D.z-C.z)<R||Math.hypot(H.x-V.x,H.z-V.z)<R||Math.hypot(H.x-C.x,H.z-C.z)<R}let z=[],M=[];for(let v of y){if(v.type==="straight"&&v.points.length>=2){let T=!1;for(let R of y){if(R.id===v.id||R.type==="straight"||w(v.points,R.points))continue;if(Us(v.points,R.points)<n.curveStraightDist){T=!0;break}}if(T){z.push({...v,id:-z.length-1});continue}}M.push(v)}let P=Date.now(),E=M,S=[];for(let v=0;v<g.length;v++){let T=g[v];S.push({id:-v-1,points:T.points,length:T.length,pointCount:T.pointCount,lineIds:T.lineIds,type:"straight",isMain:!1})}for(let v of z)S.push(v);let I=v=>v.map(T=>({...T,points:T.points.map(R=>In(R.x,R.z,s))})),W=Date.now()-o;return{mainWalls:I(E),noiseWalls:I(S),stats:{totalRawLines:t.length,totalWalls:y.length,mainCount:E.length,noiseCount:S.length,elapsedMs:W}}}function ir(t,e,n){let o={...nr,...n},r=Date.now(),s=[];for(let h of t){let g=h.z;g<o.heightMin||g>o.heightMax||s.push({x:h.x,z:h.y})}if(s.length<100)return pn(e);let{cells:i}=fo(s,o.cellSize),a=new Set;for(let[h,g]of i)g>=o.densityThreshold&&a.add(h);let u=Wn(a,o.morphIterations),d=[],f={total:0,lowDensity:0};for(let h of e){let g=h.x1,x=h.z1,y=h.x2,m=h.z2;if(Math.sqrt((y-g)**2+(m-x)**2)<.3){d.push(h);continue}let b=0,w=9;for(let z=0;z<w;z++){let M=z/(w-1),P=g+(y-g)*M,E=x+(m-x)*M,S=Math.floor(P/o.cellSize),I=Math.floor(E/o.cellSize);u.has(S+","+I)&&b++}b>=Math.ceil(w*.5)?d.push(h):f.lowDensity++,f.total++}if(d.length<5)return pn(e);let c=pn(d),l=Date.now()-r;return c}function rr(t){return{id:t.id,points:[{x:t.x1,z:t.z1},{x:t.x2,z:t.z2}],length:Math.hypot(t.x2-t.x1,t.z2-t.z1),pointCount:1,type:"straight",isMain:!1}}function lr(t,e,n){let o={outlierEnabled:!0,outlierCellSize:.1,outlierMinNeighbors:3,densityFilterEnabled:!1,...n},r=Date.now(),s=pn(t),i=s.mainWalls,a=s.noiseWalls,u=new Set(i.map(M=>M.id)),d=[];for(let M=0;M<e.length;M++){if(!u.has(M))continue;let P=e[M];if(!(!P||!Array.isArray(P)))for(let E of P)E&&E.x!==void 0&&E.y!==void 0&&E.z!==void 0&&d.push({x:E.x,y:E.y,z:E.z})}let f=d.length,c=d;o.outlierEnabled&&c.length>0&&(c=mo(c,o.outlierCellSize,o.outlierMinNeighbors));let l=[],h=[];if(c.length>=100){let M=o.pcConfig??{},P=ho(c,t,{cellSize:.05,densityThreshold:1,minComponentCells:8,simplifyEpsilon:.008,heightMin:.3,heightMax:2.8,closeIterations:8,densityRatio:.12,...M});l=P.contours??[],h=P.curvedSegments??[]}let g=[];if(h.length>0)for(let M=0;M<h.length;M++){let E=h[M].points;if(E.length<2)continue;let S=1/0,I=-1/0,W=1/0,v=-1/0;for(let D of E)D.x<S&&(S=D.x),D.x>I&&(I=D.x),D.z<W&&(W=D.z),D.z>v&&(v=D.z);let T=[];for(let D of t)si(D,E,.2)&&T.push(D.id);let R=E.map(D=>({x:D.x,y:D.z,z:0}));g.push({id:M,originalPoints:R,rawLineIds:T})}let x;if(o.densityFilterEnabled&&c.length>0&&i.length>0){let M=new Set(s.mainWalls.map(S=>S.id)),P=t.filter(S=>M.has(S.id));x=ir(c,P).mainWalls}let y=new Set;for(let M of s.mainWalls)if(M.lineIds)for(let P of M.lineIds)y.add(P);let m=new Set;for(let M of s.noiseWalls)if(M.lineIds)for(let P of M.lineIds)m.add(P);let p=t.filter(M=>y.has(M.id)).map(rr),b=t.filter(M=>m.has(M.id)).map(rr),w;if(x&&x.length>0){let M=new Set;for(let P of x)if(P.lineIds)for(let E of P.lineIds)M.add(E);w=t.filter(P=>M.has(P.id)).map(rr)}let z=Date.now()-r;return{mainWalls:p,noiseWalls:b,contours:l,curvedSegments:h,lineCurves:g.length>0?g:void 0,filteredPoints:c,secondMainWalls:w,stats:{totalRawLines:t.length,firstMainCount:p.length,firstNoiseCount:b.length,contourCount:l.length,curvedCount:h.length,secondMainCount:x?.length,mainWallPointCount:f,filteredPointCount:c.length,elapsedMs:z}}}function gi(t,e){let n=Array.isArray(t)?t:t?.data??[],o=[],r=[];for(let a=0;a<n.length;a++){let u=n[a],d=u.start??u,f=u.end??u;o.push({id:a,x1:d.x??0,y1:d.z??0,z1:d.y??0,x2:f.x??0,y2:f.z??0,z2:f.y??0});let c=u.originalPoints??u.points??[],l=Array.isArray(c)?c.filter(h=>h&&h.x!==void 0):[];r.push(l)}let s=lr(o,r,e),i=Array.isArray(t)?{data:t}:{...t};return i.lineCurves=s.lineCurves??[],i.lineCurveNum=s.curvedSegments?.length??0,i}export{sr as CONFIG,no as analyzeDoorQuad,Ze as analyzeQuad,po as applyVoxelFilter,ao as attachWindows,Je as calcAngle,Xi as checkBeamIsReasonable,Qs as classifyWallByPointCloudPCA,pn as classifyWallShapes,ta as computeContour,Ke as computeDoorLocalFloorZ,Ge as computeLocalFloorZ,ni as computeVoxelParams,ri as densityPreFilter,fn as detectDoorPerpendicularWalls,Fa as detectPerpWalls,Tn as detectPerpendicularWalls,kl as drawStaircase,ii as filterFalseArcCurves,Gs as filterWalls,Ys as filterWallsHard,vn as findDoorWalls,js as findMainByLoops,Rn as findWindowWalls,$i as getBeamLine,Ki as getColLine,Ri as getLineCheckResults,dn as getMatcherAPI,Fl as getMergeFloorMeaning,Ol as getMergeMeaning,Si as getOneLineCheckResults,vi as getOneLineClusterResults,ea as getSlabData,Nl as getStaircase,or as isRawLineNearArc,li as logArcRawSegments,La as matchWalls,Zs as mergeCollinear,Ye as mergeCollinearSegments,Qe as mergeDoorCollinearSegments,Oa as mergeWallSegments,io as normalizeAngles,jt as pointToDoorSegmentDist,so as pointToSegDist,en as pointToSegDist2D,Ce as pointToSegmentDist,jo as processData,Yo as processDoorData,ir as processFilteredByPointCloud,ho as processFromPointCloud,ti as processFromSegments,$a as processPipeline,gi as processSegments,ka as processWallData,mo as removeOutlierPoints,He as rotateXZ,lr as runMainWallPipeline,$s as spatialGrid,Dn as splitDoorWallSegment,Na as splitWallSeg,Sn as splitWallSegment,Bi as updateStEdPoint,ei as voxelFilter,oi as voxelFilterLines};
1
+ var xi=Object.create;var ar=Object.defineProperty;var yi=Object.getOwnPropertyDescriptor;var bi=Object.getOwnPropertyNames;var wi=Object.getPrototypeOf,zi=Object.prototype.hasOwnProperty;var Le=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var Mi=(t,e,n,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of bi(e))!zi.call(t,r)&&r!==n&&ar(t,r,{get:()=>e[r],enumerable:!(o=yi(e,r))||o.enumerable});return t};var Ei=(t,e,n)=>(n=t!=null?xi(wi(t)):{},Mi(e||!t||!t.__esModule?ar(n,"default",{value:t,enumerable:!0}):n,t));var Lr=Le((Ro,Do)=>{(function(t,e){typeof Ro=="object"&&typeof Do<"u"?Do.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self,t.RBush=e())})(Ro,function(){"use strict";function t(b,w,z,M,P){e(b,w,z||0,M||b.length-1,P||o)}function e(b,w,z,M,P){for(;M>z;){if(M-z>600){var E=M-z+1,S=w-z+1,I=Math.log(E),W=.5*Math.exp(2*I/3),v=.5*Math.sqrt(I*W*(E-W)/E)*(S-E/2<0?-1:1),T=Math.max(z,Math.floor(w-S*W/E+v)),R=Math.min(M,Math.floor(w+(E-S)*W/E+v));e(b,w,T,R,P)}var D=b[w],H=z,V=M;for(n(b,z,w),P(b[M],D)>0&&n(b,z,M);H<V;){for(n(b,H,V),H++,V--;P(b[H],D)<0;)H++;for(;P(b[V],D)>0;)V--}P(b[z],D)===0?n(b,z,V):(V++,n(b,V,M)),V<=w&&(z=V+1),w<=V&&(M=V-1)}}function n(b,w,z){var M=b[w];b[w]=b[z],b[z]=M}function o(b,w){return b<w?-1:b>w?1:0}var r=function(w){w===void 0&&(w=9),this._maxEntries=Math.max(4,w),this._minEntries=Math.max(2,Math.ceil(this._maxEntries*.4)),this.clear()};r.prototype.all=function(){return this._all(this.data,[])},r.prototype.search=function(w){var z=this.data,M=[];if(!y(w,z))return M;for(var P=this.toBBox,E=[];z;){for(var S=0;S<z.children.length;S++){var I=z.children[S],W=z.leaf?P(I):I;y(w,W)&&(z.leaf?M.push(I):x(w,W)?this._all(I,M):E.push(I))}z=E.pop()}return M},r.prototype.collides=function(w){var z=this.data;if(!y(w,z))return!1;for(var M=[];z;){for(var P=0;P<z.children.length;P++){var E=z.children[P],S=z.leaf?this.toBBox(E):E;if(y(w,S)){if(z.leaf||x(w,S))return!0;M.push(E)}}z=M.pop()}return!1},r.prototype.load=function(w){if(!(w&&w.length))return this;if(w.length<this._minEntries){for(var z=0;z<w.length;z++)this.insert(w[z]);return this}var M=this._build(w.slice(),0,w.length-1,0);if(!this.data.children.length)this.data=M;else if(this.data.height===M.height)this._splitRoot(this.data,M);else{if(this.data.height<M.height){var P=this.data;this.data=M,M=P}this._insert(M,this.data.height-M.height-1,!0)}return this},r.prototype.insert=function(w){return w&&this._insert(w,this.data.height-1),this},r.prototype.clear=function(){return this.data=m([]),this},r.prototype.remove=function(w,z){if(!w)return this;for(var M=this.data,P=this.toBBox(w),E=[],S=[],I,W,v;M||E.length;){if(M||(M=E.pop(),W=E[E.length-1],I=S.pop(),v=!0),M.leaf){var T=s(w,M.children,z);if(T!==-1)return M.children.splice(T,1),E.push(M),this._condense(E),this}!v&&!M.leaf&&x(M,P)?(E.push(M),S.push(I),I=0,W=M,M=M.children[0]):W?(I++,M=W.children[I],v=!1):M=null}return this},r.prototype.toBBox=function(w){return w},r.prototype.compareMinX=function(w,z){return w.minX-z.minX},r.prototype.compareMinY=function(w,z){return w.minY-z.minY},r.prototype.toJSON=function(){return this.data},r.prototype.fromJSON=function(w){return this.data=w,this},r.prototype._all=function(w,z){for(var M=[];w;)w.leaf?z.push.apply(z,w.children):M.push.apply(M,w.children),w=M.pop();return z},r.prototype._build=function(w,z,M,P){var E=M-z+1,S=this._maxEntries,I;if(E<=S)return I=m(w.slice(z,M+1)),i(I,this.toBBox),I;P||(P=Math.ceil(Math.log(E)/Math.log(S)),S=Math.ceil(E/Math.pow(S,P-1))),I=m([]),I.leaf=!1,I.height=P;var W=Math.ceil(E/S),v=W*Math.ceil(Math.sqrt(S));p(w,z,M,v,this.compareMinX);for(var T=z;T<=M;T+=v){var R=Math.min(T+v-1,M);p(w,T,R,W,this.compareMinY);for(var D=T;D<=R;D+=W){var H=Math.min(D+W-1,R);I.children.push(this._build(w,D,H,P-1))}}return i(I,this.toBBox),I},r.prototype._chooseSubtree=function(w,z,M,P){for(;P.push(z),!(z.leaf||P.length-1===M);){for(var E=1/0,S=1/0,I=void 0,W=0;W<z.children.length;W++){var v=z.children[W],T=c(v),R=h(w,v)-T;R<S?(S=R,E=T<E?T:E,I=v):R===S&&T<E&&(E=T,I=v)}z=I||z.children[0]}return z},r.prototype._insert=function(w,z,M){var P=M?w:this.toBBox(w),E=[],S=this._chooseSubtree(P,this.data,z,E);for(S.children.push(w),u(S,P);z>=0&&E[z].children.length>this._maxEntries;)this._split(E,z),z--;this._adjustParentBBoxes(P,E,z)},r.prototype._split=function(w,z){var M=w[z],P=M.children.length,E=this._minEntries;this._chooseSplitAxis(M,E,P);var S=this._chooseSplitIndex(M,E,P),I=m(M.children.splice(S,M.children.length-S));I.height=M.height,I.leaf=M.leaf,i(M,this.toBBox),i(I,this.toBBox),z?w[z-1].children.push(I):this._splitRoot(M,I)},r.prototype._splitRoot=function(w,z){this.data=m([w,z]),this.data.height=w.height+1,this.data.leaf=!1,i(this.data,this.toBBox)},r.prototype._chooseSplitIndex=function(w,z,M){for(var P,E=1/0,S=1/0,I=z;I<=M-z;I++){var W=a(w,0,I,this.toBBox),v=a(w,I,M,this.toBBox),T=g(W,v),R=c(W)+c(v);T<E?(E=T,P=I,S=R<S?R:S):T===E&&R<S&&(S=R,P=I)}return P||M-z},r.prototype._chooseSplitAxis=function(w,z,M){var P=w.leaf?this.compareMinX:d,E=w.leaf?this.compareMinY:f,S=this._allDistMargin(w,z,M,P),I=this._allDistMargin(w,z,M,E);S<I&&w.children.sort(P)},r.prototype._allDistMargin=function(w,z,M,P){w.children.sort(P);for(var E=this.toBBox,S=a(w,0,z,E),I=a(w,M-z,M,E),W=l(S)+l(I),v=z;v<M-z;v++){var T=w.children[v];u(S,w.leaf?E(T):T),W+=l(S)}for(var R=M-z-1;R>=z;R--){var D=w.children[R];u(I,w.leaf?E(D):D),W+=l(I)}return W},r.prototype._adjustParentBBoxes=function(w,z,M){for(var P=M;P>=0;P--)u(z[P],w)},r.prototype._condense=function(w){for(var z=w.length-1,M=void 0;z>=0;z--)w[z].children.length===0?z>0?(M=w[z-1].children,M.splice(M.indexOf(w[z]),1)):this.clear():i(w[z],this.toBBox)};function s(b,w,z){if(!z)return w.indexOf(b);for(var M=0;M<w.length;M++)if(z(b,w[M]))return M;return-1}function i(b,w){a(b,0,b.children.length,w,b)}function a(b,w,z,M,P){P||(P=m(null)),P.minX=1/0,P.minY=1/0,P.maxX=-1/0,P.maxY=-1/0;for(var E=w;E<z;E++){var S=b.children[E];u(P,b.leaf?M(S):S)}return P}function u(b,w){return b.minX=Math.min(b.minX,w.minX),b.minY=Math.min(b.minY,w.minY),b.maxX=Math.max(b.maxX,w.maxX),b.maxY=Math.max(b.maxY,w.maxY),b}function d(b,w){return b.minX-w.minX}function f(b,w){return b.minY-w.minY}function c(b){return(b.maxX-b.minX)*(b.maxY-b.minY)}function l(b){return b.maxX-b.minX+(b.maxY-b.minY)}function h(b,w){return(Math.max(w.maxX,b.maxX)-Math.min(w.minX,b.minX))*(Math.max(w.maxY,b.maxY)-Math.min(w.minY,b.minY))}function g(b,w){var z=Math.max(b.minX,w.minX),M=Math.max(b.minY,w.minY),P=Math.min(b.maxX,w.maxX),E=Math.min(b.maxY,w.maxY);return Math.max(0,P-z)*Math.max(0,E-M)}function x(b,w){return b.minX<=w.minX&&b.minY<=w.minY&&w.maxX<=b.maxX&&w.maxY<=b.maxY}function y(b,w){return w.minX<=b.maxX&&w.minY<=b.maxY&&w.maxX>=b.minX&&w.maxY>=b.minY}function m(b){return{children:b,height:1,leaf:!0,minX:1/0,minY:1/0,maxX:-1/0,maxY:-1/0}}function p(b,w,z,M,P){for(var E=[w,z];E.length;)if(z=E.pop(),w=E.pop(),!(z-w<=M)){var S=w+Math.ceil((z-w)/M/2)*M;t(b,S,w,z,P),E.push(w,S,S,z)}}return r})});var kr=Le((vo,Vo)=>{(function(t,e){typeof vo=="object"&&typeof Vo<"u"?Vo.exports=e():typeof define=="function"&&define.amd?define(e):(t=t||self,t.TinyQueue=e())})(vo,function(){"use strict";var t=function(o,r){if(o===void 0&&(o=[]),r===void 0&&(r=e),this.data=o,this.length=this.data.length,this.compare=r,this.length>0)for(var s=(this.length>>1)-1;s>=0;s--)this._down(s)};t.prototype.push=function(o){this.data.push(o),this.length++,this._up(this.length-1)},t.prototype.pop=function(){if(this.length!==0){var o=this.data[0],r=this.data.pop();return this.length--,this.length>0&&(this.data[0]=r,this._down(0)),o}},t.prototype.peek=function(){return this.data[0]},t.prototype._up=function(o){for(var r=this,s=r.data,i=r.compare,a=s[o];o>0;){var u=o-1>>1,d=s[u];if(i(a,d)>=0)break;s[o]=d,o=u}s[o]=a},t.prototype._down=function(o){for(var r=this,s=r.data,i=r.compare,a=this.length>>1,u=s[o];o<a;){var d=(o<<1)+1,f=s[d],c=d+1;if(c<this.length&&i(s[c],f)<0&&(d=c,f=s[c]),i(f,u)>=0)break;s[o]=f,o=d}s[o]=u};function e(n,o){return n<o?-1:n>o?1:0}return t})});var Fr=Le((rh,Or)=>{Or.exports=function(e,n,o,r){var s=e[0],i=e[1],a=!1;o===void 0&&(o=0),r===void 0&&(r=n.length);for(var u=(r-o)/2,d=0,f=u-1;d<u;f=d++){var c=n[o+d*2+0],l=n[o+d*2+1],h=n[o+f*2+0],g=n[o+f*2+1],x=l>i!=g>i&&s<(h-c)*(i-l)/(g-l)+c;x&&(a=!a)}return a}});var qr=Le((sh,Nr)=>{Nr.exports=function(e,n,o,r){var s=e[0],i=e[1],a=!1;o===void 0&&(o=0),r===void 0&&(r=n.length);for(var u=r-o,d=0,f=u-1;d<u;f=d++){var c=n[d+o][0],l=n[d+o][1],h=n[f+o][0],g=n[f+o][1],x=l>i!=g>i&&s<(h-c)*(i-l)/(g-l)+c;x&&(a=!a)}return a}});var Xr=Le((ih,Gn)=>{var jr=Fr(),$r=qr();Gn.exports=function(e,n,o,r){return n.length>0&&Array.isArray(n[0])?$r(e,n,o,r):jr(e,n,o,r)};Gn.exports.nested=$r;Gn.exports.flat=jr});var Gr=Le((Yn,Zr)=>{(function(t,e){typeof Yn=="object"&&typeof Zr<"u"?e(Yn):typeof define=="function"&&define.amd?define(["exports"],e):e((t=t||self).predicates={})})(Yn,function(t){"use strict";let n=33306690738754706e-32;function o(h,g,x,y,m){let p,b,w,z,M=g[0],P=y[0],E=0,S=0;P>M==P>-M?(p=M,M=g[++E]):(p=P,P=y[++S]);let I=0;if(E<h&&S<x)for(P>M==P>-M?(w=p-((b=M+p)-M),M=g[++E]):(w=p-((b=P+p)-P),P=y[++S]),p=b,w!==0&&(m[I++]=w);E<h&&S<x;)P>M==P>-M?(w=p-((b=p+M)-(z=b-p))+(M-z),M=g[++E]):(w=p-((b=p+P)-(z=b-p))+(P-z),P=y[++S]),p=b,w!==0&&(m[I++]=w);for(;E<h;)w=p-((b=p+M)-(z=b-p))+(M-z),M=g[++E],p=b,w!==0&&(m[I++]=w);for(;S<x;)w=p-((b=p+P)-(z=b-p))+(P-z),P=y[++S],p=b,w!==0&&(m[I++]=w);return p===0&&I!==0||(m[I++]=p),I}function r(h){return new Float64Array(h)}let s=33306690738754716e-32,i=22204460492503146e-32,a=11093356479670487e-47,u=r(4),d=r(8),f=r(12),c=r(16),l=r(4);t.orient2d=function(h,g,x,y,m,p){let b=(g-p)*(x-m),w=(h-m)*(y-p),z=b-w;if(b===0||w===0||b>0!=w>0)return z;let M=Math.abs(b+w);return Math.abs(z)>=s*M?z:-(function(P,E,S,I,W,v,T){let R,D,H,V,C,_,A,L,k,F,B,O,$,Z,Q,j,X,U,at=P-W,mt=S-W,St=E-v,ot=I-v;C=(Q=(L=at-(A=(_=134217729*at)-(_-at)))*(F=ot-(k=(_=134217729*ot)-(_-ot)))-((Z=at*ot)-A*k-L*k-A*F))-(B=Q-(X=(L=St-(A=(_=134217729*St)-(_-St)))*(F=mt-(k=(_=134217729*mt)-(_-mt)))-((j=St*mt)-A*k-L*k-A*F))),u[0]=Q-(B+C)+(C-X),C=($=Z-((O=Z+B)-(C=O-Z))+(B-C))-(B=$-j),u[1]=$-(B+C)+(C-j),C=(U=O+B)-O,u[2]=O-(U-C)+(B-C),u[3]=U;let Mt=(function(Pt,et){let ht=et[0];for(let nt=1;nt<Pt;nt++)ht+=et[nt];return ht})(4,u),bt=i*T;if(Mt>=bt||-Mt>=bt||(R=P-(at+(C=P-at))+(C-W),H=S-(mt+(C=S-mt))+(C-W),D=E-(St+(C=E-St))+(C-v),V=I-(ot+(C=I-ot))+(C-v),R===0&&D===0&&H===0&&V===0)||(bt=a*T+n*Math.abs(Mt),(Mt+=at*V+ot*R-(St*H+mt*D))>=bt||-Mt>=bt))return Mt;C=(Q=(L=R-(A=(_=134217729*R)-(_-R)))*(F=ot-(k=(_=134217729*ot)-(_-ot)))-((Z=R*ot)-A*k-L*k-A*F))-(B=Q-(X=(L=D-(A=(_=134217729*D)-(_-D)))*(F=mt-(k=(_=134217729*mt)-(_-mt)))-((j=D*mt)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-X),C=($=Z-((O=Z+B)-(C=O-Z))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let Ct=o(4,u,4,l,d);C=(Q=(L=at-(A=(_=134217729*at)-(_-at)))*(F=V-(k=(_=134217729*V)-(_-V)))-((Z=at*V)-A*k-L*k-A*F))-(B=Q-(X=(L=St-(A=(_=134217729*St)-(_-St)))*(F=H-(k=(_=134217729*H)-(_-H)))-((j=St*H)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-X),C=($=Z-((O=Z+B)-(C=O-Z))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let Et=o(Ct,d,4,l,f);C=(Q=(L=R-(A=(_=134217729*R)-(_-R)))*(F=V-(k=(_=134217729*V)-(_-V)))-((Z=R*V)-A*k-L*k-A*F))-(B=Q-(X=(L=D-(A=(_=134217729*D)-(_-D)))*(F=H-(k=(_=134217729*H)-(_-H)))-((j=D*H)-A*k-L*k-A*F))),l[0]=Q-(B+C)+(C-X),C=($=Z-((O=Z+B)-(C=O-Z))+(B-C))-(B=$-j),l[1]=$-(B+C)+(C-j),C=(U=O+B)-O,l[2]=O-(U-C)+(B-C),l[3]=U;let zt=o(Et,f,4,l,c);return c[zt-1]})(h,g,x,y,m,p,M)},t.orient2dfast=function(h,g,x,y,m,p){return(g-p)*(x-m)-(h-m)*(y-p)},Object.defineProperty(t,"__esModule",{value:!0})})});var ts=Le((lh,Wo)=>{"use strict";var Yr=Lr(),Kn=kr(),bl=Xr(),wl=Gr().orient2d;Kn.default&&(Kn=Kn.default);Wo.exports=Jr;Wo.exports.default=Jr;function Jr(t,e,n){e=Math.max(0,e===void 0?2:e),n=n||0;var o=Tl(t),r=new Yr(16);r.toBBox=function(p){return{minX:p[0],minY:p[1],maxX:p[0],maxY:p[1]}},r.compareMinX=function(p,b){return p[0]-b[0]},r.compareMinY=function(p,b){return p[1]-b[1]},r.load(t);for(var s=[],i=0,a;i<o.length;i++){var u=o[i];r.remove(u),a=Qr(u,a),s.push(a)}var d=new Yr(16);for(i=0;i<s.length;i++)d.insert(Io(s[i]));for(var f=e*e,c=n*n;s.length;){var l=s.shift(),h=l.p,g=l.next.p,x=Co(h,g);if(!(x<c)){var y=x/f;u=zl(r,l.prev.p,h,g,l.next.next.p,y,d),u&&Math.min(Co(u,h),Co(u,g))<=y&&(s.push(l),s.push(Qr(u,l)),r.remove(u),d.remove(l),d.insert(Io(l)),d.insert(Io(l.next)))}}l=a;var m=[];do m.push(l.p),l=l.next;while(l!==a);return m.push(l.p),m}function zl(t,e,n,o,r,s,i){for(var a=new Kn([],Ml),u=t.data;u;){for(var d=0;d<u.children.length;d++){var f=u.children[d],c=u.leaf?Ho(f,n,o):El(n,o,f);c>s||a.push({node:f,dist:c})}for(;a.length&&!a.peek().node.children;){var l=a.pop(),h=l.node,g=Ho(h,e,n),x=Ho(h,o,r);if(l.dist<g&&l.dist<x&&Kr(n,h,i)&&Kr(o,h,i))return h}u=a.pop(),u&&(u=u.node)}return null}function Ml(t,e){return t.dist-e.dist}function El(t,e,n){if(Ur(t,n)||Ur(e,n))return 0;var o=Un(t[0],t[1],e[0],e[1],n.minX,n.minY,n.maxX,n.minY);if(o===0)return 0;var r=Un(t[0],t[1],e[0],e[1],n.minX,n.minY,n.minX,n.maxY);if(r===0)return 0;var s=Un(t[0],t[1],e[0],e[1],n.maxX,n.minY,n.maxX,n.maxY);if(s===0)return 0;var i=Un(t[0],t[1],e[0],e[1],n.minX,n.maxY,n.maxX,n.maxY);return i===0?0:Math.min(o,r,s,i)}function Ur(t,e){return t[0]>=e.minX&&t[0]<=e.maxX&&t[1]>=e.minY&&t[1]<=e.maxY}function Kr(t,e,n){for(var o=Math.min(t[0],e[0]),r=Math.min(t[1],e[1]),s=Math.max(t[0],e[0]),i=Math.max(t[1],e[1]),a=n.search({minX:o,minY:r,maxX:s,maxY:i}),u=0;u<a.length;u++)if(Pl(a[u].p,a[u].next.p,t,e))return!1;return!0}function an(t,e,n){return wl(t[0],t[1],e[0],e[1],n[0],n[1])}function Pl(t,e,n,o){return t!==o&&e!==n&&an(t,e,n)>0!=an(t,e,o)>0&&an(n,o,t)>0!=an(n,o,e)>0}function Io(t){var e=t.p,n=t.next.p;return t.minX=Math.min(e[0],n[0]),t.minY=Math.min(e[1],n[1]),t.maxX=Math.max(e[0],n[0]),t.maxY=Math.max(e[1],n[1]),t}function Tl(t){for(var e=t[0],n=t[0],o=t[0],r=t[0],s=0;s<t.length;s++){var i=t[s];i[0]<e[0]&&(e=i),i[0]>o[0]&&(o=i),i[1]<n[1]&&(n=i),i[1]>r[1]&&(r=i)}var a=[e,n,o,r],u=a.slice();for(s=0;s<t.length;s++)bl(t[s],a)||u.push(t[s]);return Rl(u)}function Qr(t,e){var n={p:t,prev:null,next:null,minX:0,minY:0,maxX:0,maxY:0};return e?(n.next=e.next,n.prev=e,e.next.prev=n,e.next=n):(n.prev=n,n.next=n),n}function Co(t,e){var n=t[0]-e[0],o=t[1]-e[1];return n*n+o*o}function Ho(t,e,n){var o=e[0],r=e[1],s=n[0]-o,i=n[1]-r;if(s!==0||i!==0){var a=((t[0]-o)*s+(t[1]-r)*i)/(s*s+i*i);a>1?(o=n[0],r=n[1]):a>0&&(o+=s*a,r+=i*a)}return s=t[0]-o,i=t[1]-r,s*s+i*i}function Un(t,e,n,o,r,s,i,a){var u=n-t,d=o-e,f=i-r,c=a-s,l=t-r,h=e-s,g=u*u+d*d,x=u*f+d*c,y=f*f+c*c,m=u*l+d*h,p=f*l+c*h,b=g*y-x*x,w,z,M,P,E=b,S=b;b===0?(z=0,E=1,P=p,S=y):(z=x*p-y*m,P=g*p-x*m,z<0?(z=0,P=p,S=y):z>E&&(z=E,P=p+x,S=y)),P<0?(P=0,-m<0?z=0:-m>g?z=E:(z=-m,E=g)):P>S&&(P=S,-m+x<0?z=0:-m+x>g?z=E:(z=-m+x,E=g)),w=z===0?0:z/E,M=P===0?0:P/S;var I=(1-w)*t+w*n,W=(1-w)*e+w*o,v=(1-M)*r+M*i,T=(1-M)*s+M*a,R=v-I,D=T-W;return R*R+D*D}function Sl(t,e){return t[0]===e[0]?t[1]-e[1]:t[0]-e[0]}function Rl(t){t.sort(Sl);for(var e=[],n=0;n<t.length;n++){for(;e.length>=2&&an(e[e.length-2],e[e.length-1],t[n])<=0;)e.pop();e.push(t[n])}for(var o=[],r=t.length-1;r>=0;r--){for(;o.length>=2&&an(o[o.length-2],o[o.length-1],t[r])<=0;)o.pop();o.push(t[r])}return o.pop(),e.pop(),e.concat(o)}});import*as Oe from"three";import*as ke from"three";var _t=class t{constructor(e=0,n=0,o=0){this.x=e,this.y=n,this.z=o}set(e,n,o){return this.x=e,this.y=n,this.z=o,this}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this}clone(){return new t(this.x,this.y,this.z)}add(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this}sub(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this}subVectors(e,n){return this.x=e.x-n.x,this.y=e.y-n.y,this.z=e.z-n.z,this}multiplyScalar(e){return this.x*=e,this.y*=e,this.z*=e,this}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}crossVectors(e,n){let o=e.x,r=e.y,s=e.z,i=n.x,a=n.y,u=n.z;return this.x=r*u-s*a,this.y=s*i-o*u,this.z=o*a-r*i,this}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z}length(){return Math.sqrt(this.lengthSq())}normalize(){let e=this.length();return e>0&&(this.x/=e,this.y/=e,this.z/=e),this}distanceTo(e){return Math.sqrt(this.distanceToSquared(e))}distanceToSquared(e){let n=this.x-e.x,o=this.y-e.y,r=this.z-e.z;return n*n+o*o+r*r}toArray(){return[this.x,this.y,this.z]}fromArray(e,n=0){return this.x=e[n],this.y=e[n+1],this.z=e[n+2],this}applyAxisAngle(e,n){let o=this.x,r=this.y,s=this.z,i=e.x,a=e.y,u=e.z,d=Math.cos(n),f=Math.sin(n),c=i*o+a*r+u*s;return this.x=i*c*(1-d)+o*d+(-u*r+a*s)*f,this.y=a*c*(1-d)+r*d+(u*o-i*s)*f,this.z=u*c*(1-d)+s*d+(-a*o+i*r)*f,this}},An=class{constructor(e=new _t(1,0,0),n=0){this.normal=e,this.constant=n}set(e,n){return this.normal.copy(e),this.constant=n,this}setFromNormalAndCoplanarPoint(e,n){return this.normal.copy(e),this.constant=-n.dot(this.normal),this}setFromCoplanarPoints(e,n,o){let r=new _t().subVectors(n,e),s=new _t().subVectors(o,e),i=new _t().crossVectors(r,s).normalize();return this.normal.copy(i),this.constant=-e.dot(this.normal),this}distanceToPoint(e){return this.normal.dot(e)+this.constant}projectPoint(e,n=new _t){let o=this.distanceToPoint(e);return n.copy(e),n.x-=this.normal.x*o,n.y-=this.normal.y*o,n.z-=this.normal.z*o,n}},_n=()=>typeof performance<"u"&&performance.now?performance.now():Date.now(),yo=class{constructor(){this.results=null,this.clusterResults=null}initLimits1(e,n){let o,r;return e<.3?o=5:e<.5?o=8:e<.8?o=10:e<1?o=15:e<2?o=30:e<3?o=35:e<4?o=40:e<5?o=45:e<6?o=50:e<7?o=60:e<8.5?o=70:e<10?o=80:e<15?o=100:e<20?o=120:e<25?o=140:e<30?o=160:e<35?o=180:e<40?o=200:e<45?o=230:e<50?o=250:o=350,n<1?r=30:n<2?r=40:n<2.5?r=45:n<3?r=60:n<3.6?r=70:r=80,{horizontalSubdivisions:o,verticalSubdivisions:r}}initLimits2(e,n){let s=Math.round(Math.max(5,Math.min(350,e/.08))),i=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:s,verticalSubdivisions:i}}initLimits3(e,n){let o;e<2?o=.06:e<10?o=.1:o=.15;let r=Math.round(Math.max(5,Math.min(350,e/o))),s=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:r,verticalSubdivisions:s}}initLimits4(e,n){let o=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(e)))),r=Math.round(Math.max(30,Math.min(80,20*Math.sqrt(n))));return{horizontalSubdivisions:o,verticalSubdivisions:r}}createSubdividedWallPlane(e,n,o,r=null){let s=[];if(!e||!e.start||!e.end||isNaN(e.rooftopPz))return s;let i=new _t(e.start.x,e.start.y,e.start.z),a=new _t(e.end.x,e.end.y,e.end.z),u=new _t(e.end.x,e.end.y,e.rooftopPz),d=new _t(e.start.x,e.start.y,e.rooftopPz),f=[i,a,u,d];for(let P=0;P<f.length;P++)if(!this.isValidVector3(f[P]))return console.error(`\u5899\u4F53\u57FA\u7840\u70B9${P}\u65E0\u6548:`,f[P]),s;let c=i.distanceTo(a),l=Math.abs(e.rooftopPz-e.start.z);if(c===0||l===0)return s;let h=c/o,g=l/n,x=new _t().subVectors(a,i).normalize(),y=i.x,m=i.y,p=i.z,b=x.x,w=x.y,z=x.z,M=(P,E)=>new _t(y+b*P,m+w*P,p+z*P+E);for(let P=0;P<n;P++){let E=P*g,S=(P+1)*g;for(let I=0;I<o;I++){let W=I*h,v=(I+1)*h;s.push({points:[M(W,E),M(v,E),M(v,S),M(W,S)],userData:{type:"wallSegment",row:P,col:I,width:h,height:g}})}}return s}analyzePointDistribution(e,n,o=.01,r={}){let s=r.projectPlane||null;this.results={totalSegments:0,segmentsWithPoints:0,segmentsWithoutPoints:0,missingSegments:[],existSegments:[],pointsInSegments:new Map,segmentsByPoint:new Map,segmentDetails:new Map,tolerance:o,analysisTime:null};let i=_n(),a=[],u=new Map,d=1/0,f=-1/0,c=1/0,l=-1/0;for(let W=0;W<e.length;W++){let v=e[W];if(v.userData&&v.userData.type==="wallSegment"){let T=this.getWorldVerticesNew(v.points);if(T.length<4)continue;let R=new _t().add(T[0]).add(T[1]).add(T[2]).add(T[3]).multiplyScalar(.25);v.userData.center=R,v.vertices=T;let D=v.userData.row,H=v.userData.col,V=`row${D}_col${H}`;a.push(v),u.set(V,v),D<d&&(d=D),D>f&&(f=D),H<c&&(c=H),H>l&&(l=H)}}if(this.results.totalSegments=a.length,a.length===0)return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results;let h=`row${d}_col${c}`,g=u.get(h)||a[0];if(!g||!g.vertices||g.vertices.length<4)return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results;let x=g.vertices[0],y=g.vertices[1],m=g.vertices[3],p=new _t().subVectors(y,x).normalize(),b=new _t().subVectors(m,x).normalize(),w=g.userData.width,z=g.userData.height,M=f-d+1,P=l-c+1,E=new An;E.setFromCoplanarPoints(x,y,m);let S=new _t,I=new _t;for(let W=0;W<n.length;W+=3){let v=new _t(n[W],n[W+1],n[W+2]),T=s?s.projectPoint(v,I):v;if(Math.abs(E.distanceToPoint(T))>o)continue;S.subVectors(T,x);let D=S.dot(p),H=S.dot(b),V=Math.ceil((D-o)/w-1),C=Math.floor((D+o)/w),_=Math.ceil((H-o)/z-1),A=Math.floor((H+o)/z);if(C<0||V>P-1||A<0||_>M-1)continue;let L=Math.max(0,V),k=Math.min(P-1,C),F=Math.max(0,_),B=Math.min(M-1,A);for(let O=F;O<=B;O++){let $=d+O;for(let Z=L;Z<=k;Z++){let Q=c+Z,j=`row${$}_col${Q}`;u.has(j)&&(this.results.pointsInSegments.has(j)||this.results.pointsInSegments.set(j,[W]))}}}for(let W of a){let v=`row${W.userData.row}_col${W.userData.col}`,T=this.results.pointsInSegments.has(v)?this.results.pointsInSegments.get(v).length:0,R=T>=1,D={row:W.userData.row,col:W.userData.col,width:W.userData.width,height:W.userData.height,segmentKey:v,vertices:W.vertices,center:W.userData.center,mesh:W,pointCount:T,hasPointsButNotEnough:T>0&&T<1};R?(this.results.segmentsWithPoints++,this.results.existSegments.push(D)):(this.results.segmentsWithoutPoints++,this.results.missingSegments.push(D))}return this.results.analysisTime=_n()-i,this.printAnalysisResults(),this.results}isPointInWallSegmentOptimized(e,n,o,r,s,i,a,u,d,f,c){if(Math.abs(o.distanceToPoint(e))>c)return!1;let h=new _t().subVectors(e,n),g=Math.abs(h.dot(s)),x=Math.abs(h.dot(i));if(g>d&&x>f)return!1;let y=new _t().subVectors(e,r),m=y.dot(s),p=y.dot(i),b=m>=-c&&m<=a+c,w=p>=-c&&p<=u+c;return b&&w}getWorldVerticesNew(e){return e}highlightEmptySegments(e,n=null,o=null,r=1){if(!this.results)return console.warn("\u8BF7\u5148\u8FD0\u884C\u5206\u6790\u51FD\u6570"),null;let s=new Map,i=new Map;for(let d of this.results.missingSegments)s.set(d.segmentKey,d),i.has(d.row)||i.set(d.row,new Map),i.get(d.row).set(d.col,{segmentKey:d.segmentKey,isEmpty:!0,visited:!1});for(let d of e)if(d.userData&&d.userData.type==="wallSegment"){let f=`row${d.userData.row}_col${d.userData.col}`,c=s.has(f);i.has(d.userData.row)||i.set(d.userData.row,new Map),i.get(d.userData.row).has(d.userData.col)||i.get(d.userData.row).set(d.userData.col,{segmentKey:f,isEmpty:c,visited:!1})}let a=[],u=[[0,1],[1,0],[0,-1],[-1,0]];for(let[d,f]of s){let c=i.get(f.row)?.get(f.col);if(c&&!c.visited){let l=[],h=[[f.row,f.col]],g=0;for(c.visited=!0;g<h.length;){let x=h[g++],y=x[0],m=x[1],p=`row${y}_col${m}`;s.has(p)&&l.push(s.get(p));for(let b of u){let w=y+b[0],z=m+b[1];if(i.has(w)&&i.get(w).has(z)){let M=i.get(w).get(z);!M.visited&&M.isEmpty&&(M.visited=!0,h.push([w,z]))}}}l.length>=r&&a.push(l)}}return this.clusterResults={clusters:a.sort((d,f)=>f.length-d.length),totalClusters:a.length},this.clusterResults}getClusterInfo(){if(!this.clusterResults)return null;let e=this.clusterResults.clusters.map(n=>n.length);return{totalClusters:this.clusterResults.totalClusters,clusterSizes:this.clusterResults.clusters.map((n,o)=>({clusterIndex:o,size:n.length,segments:n.map(r=>r.segmentKey)})),largestCluster:e.length?Math.max(...e):0,smallestCluster:e.length?Math.min(...e):0}}printAnalysisResults(){if(!this.results){console.warn("\u6CA1\u6709\u53EF\u7528\u7684\u5206\u6790\u7ED3\u679C");return}}isValidVector3(e){return e&&!isNaN(e.x)&&!isNaN(e.y)&&!isNaN(e.z)&&isFinite(e.x)&&isFinite(e.y)&&isFinite(e.z)}},cr=(t,e)=>{let n={},o=[],r=[];if(!t||t.length===0)return n.originalVertices=o,n.allCenterPoints=r,n;if(e=="xyz")for(let s of t){r.push({x:s.center.x,y:s.center.y,z:s.center.z});let i=[];for(let a=0;a<s.vertices.length;a++)i.push({x:s.vertices[a].x,y:s.vertices[a].y,z:s.vertices[a].z});o.push(i)}else if(e=="three")for(let s of t){r.push(new ke.Vector3(s.center.x,s.center.y,s.center.z));let i=[];for(let a=0;a<s.vertices.length;a++)i.push(new ke.Vector3(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z));o.push(i)}else if(e=="array")for(let s of t){r.push(s.center.x),r.push(s.center.y),r.push(s.center.z);let i=[];for(let a=0;a<s.vertices.length;a++)i.push(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z);o.push(i)}return n.originalVertices=o,n.allCenterPoints=r,n},Pi=(t,e)=>{let n=new _t().copy(t.start).add(t.end).multiplyScalar(.5),o=new _t().subVectors(new _t(n.x,n.y,n.z+1),n).normalize(),r=new _t(t.direction.x,t.direction.y,0);r.applyAxisAngle(o,Math.PI/2);let s=new _t().copy(n).add(r.clone().multiplyScalar(t.length)),i=new _t().copy(n).sub(r.clone().multiplyScalar(t.length));if(n.z=t.start.z,s.z=t.start.z,i.z=t.start.z,e=="three")return{start:new ke.Vector3(s.x,s.y,s.z),end:new ke.Vector3(i.x,i.y,i.z),center:new ke.Vector3(n.x,n.y,n.z),direction:new ke.Vector3(r.x,r.y,r.z)};if(e=="xyz")return{start:{x:s.x,y:s.y,z:s.z},end:{x:i.x,y:i.y,z:i.z},center:{x:n.x,y:n.y,z:n.z},direction:{x:r.x,y:r.y,z:r.z}};if(e=="array")return{start:[s.x,s.y,s.z],end:[i.x,i.y,i.z],center:[n.x,n.y,n.z],direction:[r.x,r.y,r.z]}},ur=async(t,e=!0,n="three")=>{for(let o=0;o<t.length;o++){let r=t[o];await bo(r,e,n)}},bo=(t,e=!0,n="three")=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&e)return;let o=new An().setFromCoplanarPoints(new _t(t.start.x,t.start.y,t.start.z),new _t(t.end.x,t.end.y,t.end.z),new _t(t.start.x,t.start.y,t.rooftopPz)),r=new _t(t.start.x,t.start.y,t.start.z).distanceTo(new _t(t.end.x,t.end.y,t.end.z)),s=Math.abs(t.rooftopPz-t.start.z),i=new yo,{horizontalSubdivisions:a,verticalSubdivisions:u}=i.initLimits4(r,s),d=i.createSubdividedWallPlane(t,u,a,null);i.analyzePointDistribution(d,t.originalPoints,.01,{projectPlane:o});let f=i.highlightEmptySegments(d,null,null),c=[];if(f&&f.clusters)for(let g of f.clusters){let x=cr(g,n);x&&c.push(x)}let h=Pi(t,n).direction;if(t.checkResults=c,t.verticalDirection=h,t.saveExistSegments){let g=[],x=cr(i.results.existSegments,n);x&&g.push(x),t.existSegments=g}};var Vt=class t{constructor(e=0,n=0,o=0){this.x=e,this.y=n,this.z=o}set(e,n,o){return this.x=e,this.y=n,this.z=o,this}copy(e){return this.x=e.x,this.y=e.y,this.z=e.z,this}clone(){return new t(this.x,this.y,this.z)}add(e){return this.x+=e.x,this.y+=e.y,this.z+=e.z,this}sub(e){return this.x-=e.x,this.y-=e.y,this.z-=e.z,this}subVectors(e,n){return this.x=e.x-n.x,this.y=e.y-n.y,this.z=e.z-n.z,this}multiplyScalar(e){return this.x*=e,this.y*=e,this.z*=e,this}dot(e){return this.x*e.x+this.y*e.y+this.z*e.z}crossVectors(e,n){let o=e.x,r=e.y,s=e.z,i=n.x,a=n.y,u=n.z;return this.x=r*u-s*a,this.y=s*i-o*u,this.z=o*a-r*i,this}lengthSq(){return this.x*this.x+this.y*this.y+this.z*this.z}length(){return Math.sqrt(this.lengthSq())}normalize(){let e=this.length();return e>0&&(this.x/=e,this.y/=e,this.z/=e),this}distanceTo(e){return Math.sqrt(this.distanceToSquared(e))}distanceToSquared(e){let n=this.x-e.x,o=this.y-e.y,r=this.z-e.z;return n*n+o*o+r*r}toArray(){return[this.x,this.y,this.z]}fromArray(e,n=0){return this.x=e[n],this.y=e[n+1],this.z=e[n+2],this}applyAxisAngle(e,n){let o=this.x,r=this.y,s=this.z,i=e.x,a=e.y,u=e.z,d=Math.cos(n),f=Math.sin(n),c=i*o+a*r+u*s;return this.x=i*c*(1-d)+o*d+(-u*r+a*s)*f,this.y=a*c*(1-d)+r*d+(u*o-i*s)*f,this.z=u*c*(1-d)+s*d+(-a*o+i*r)*f,this}},bn=class{constructor(e=new Vt(1,0,0),n=0){this.normal=e,this.constant=n}set(e,n){return this.normal.copy(e),this.constant=n,this}setFromNormalAndCoplanarPoint(e,n){return this.normal.copy(e),this.constant=-n.dot(this.normal),this}setFromCoplanarPoints(e,n,o){let r=new Vt().subVectors(n,e),s=new Vt().subVectors(o,e),i=new Vt().crossVectors(r,s).normalize();return this.normal.copy(i),this.constant=-e.dot(this.normal),this}distanceToPoint(e){return this.normal.dot(e)+this.constant}projectPoint(e,n=new Vt){let o=this.distanceToPoint(e);return n.copy(e),n.x-=this.normal.x*o,n.y-=this.normal.y*o,n.z-=this.normal.z*o,n}},Bn=()=>typeof performance<"u"&&performance.now?performance.now():Date.now(),Ln=class{constructor(){this.results=null,this.clusterResults=null}initLimits1(e,n){let o,r;return e<.3?o=5:e<.5?o=8:e<.8?o=10:e<1?o=15:e<2?o=30:e<3?o=35:e<4?o=40:e<5?o=45:e<6?o=50:e<7?o=60:e<8.5?o=70:e<10?o=80:e<15?o=100:e<20?o=120:e<25?o=140:e<30?o=160:e<35?o=180:e<40?o=200:e<45?o=230:e<50?o=250:o=350,n<1?r=30:n<2?r=40:n<2.5?r=45:n<3?r=60:n<3.6?r=70:r=80,{horizontalSubdivisions:o,verticalSubdivisions:r}}initLimits2(e,n){let s=Math.round(Math.max(5,Math.min(350,e/.08))),i=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:s,verticalSubdivisions:i}}initLimits3(e,n){let o;e<2?o=.06:e<10?o=.1:o=.15;let r=Math.round(Math.max(5,Math.min(350,e/o))),s=Math.round(Math.max(30,Math.min(80,n/.06)));return{horizontalSubdivisions:r,verticalSubdivisions:s}}initLimits4(e,n){let o=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(e)))),r=Math.round(Math.max(30,Math.min(80,20*Math.sqrt(n))));return{horizontalSubdivisions:o,verticalSubdivisions:r}}createSubdividedWallPlane(e,n,o,r=null){let s=[];if(!e||!e.start||!e.end||isNaN(e.rooftopPz))return s;let i=new Vt(e.start.x,e.start.y,e.start.z),a=new Vt(e.end.x,e.end.y,e.end.z),u=new Vt(e.end.x,e.end.y,e.rooftopPz),d=new Vt(e.start.x,e.start.y,e.rooftopPz),f=[i,a,u,d];for(let P=0;P<f.length;P++)if(!this.isValidVector3(f[P]))return console.error(`\u5899\u4F53\u57FA\u7840\u70B9${P}\u65E0\u6548:`,f[P]),s;let c=i.distanceTo(a),l=Math.abs(e.rooftopPz-e.start.z);if(c===0||l===0)return s;let h=c/o,g=l/n,x=new Vt().subVectors(a,i).normalize(),y=i.x,m=i.y,p=i.z,b=x.x,w=x.y,z=x.z,M=(P,E)=>new Vt(y+b*P,m+w*P,p+z*P+E);for(let P=0;P<n;P++){let E=P*g,S=(P+1)*g;for(let I=0;I<o;I++){let W=I*h,v=(I+1)*h;s.push({points:[M(W,E),M(v,E),M(v,S),M(W,S)],userData:{type:"wallSegment",row:P,col:I,width:h,height:g}})}}return s}analyzePointDistribution(e,n,o=.01,r={}){let s=r.projectPlane||null;this.results={totalSegments:0,segmentsWithPoints:0,segmentsWithoutPoints:0,missingSegments:[],existSegments:[],pointsInSegments:new Map,segmentsByPoint:new Map,segmentDetails:new Map,tolerance:o,analysisTime:null};let i=Bn(),a=[],u=new Map,d=1/0,f=-1/0,c=1/0,l=-1/0;for(let W=0;W<e.length;W++){let v=e[W];if(v.userData&&v.userData.type==="wallSegment"){let T=this.getWorldVerticesNew(v.points);if(T.length<4)continue;let R=new Vt().add(T[0]).add(T[1]).add(T[2]).add(T[3]).multiplyScalar(.25);v.userData.center=R,v.vertices=T;let D=v.userData.row,H=v.userData.col,V=`row${D}_col${H}`;a.push(v),u.set(V,v),D<d&&(d=D),D>f&&(f=D),H<c&&(c=H),H>l&&(l=H)}}if(this.results.totalSegments=a.length,a.length===0)return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results;let h=`row${d}_col${c}`,g=u.get(h)||a[0];if(!g||!g.vertices||g.vertices.length<4)return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results;let x=g.vertices[0],y=g.vertices[1],m=g.vertices[3],p=new Vt().subVectors(y,x).normalize(),b=new Vt().subVectors(m,x).normalize(),w=g.userData.width,z=g.userData.height,M=f-d+1,P=l-c+1,E=new bn;E.setFromCoplanarPoints(x,y,m);let S=new Vt,I=new Vt;for(let W=0;W<n.length;W++){let v=n[W],T=s?s.projectPoint(v,I):v;if(Math.abs(E.distanceToPoint(T))>o)continue;S.subVectors(T,x);let D=S.dot(p),H=S.dot(b),V=Math.ceil((D-o)/w-1),C=Math.floor((D+o)/w),_=Math.ceil((H-o)/z-1),A=Math.floor((H+o)/z);if(C<0||V>P-1||A<0||_>M-1)continue;let L=Math.max(0,V),k=Math.min(P-1,C),F=Math.max(0,_),B=Math.min(M-1,A);for(let O=F;O<=B;O++){let $=d+O;for(let Z=L;Z<=k;Z++){let Q=c+Z,j=`row${$}_col${Q}`;u.has(j)&&(this.results.pointsInSegments.has(j)||this.results.pointsInSegments.set(j,[W]))}}}for(let W of a){let v=`row${W.userData.row}_col${W.userData.col}`,T=this.results.pointsInSegments.has(v)?this.results.pointsInSegments.get(v).length:0,R=T>=1,D={row:W.userData.row,col:W.userData.col,width:W.userData.width,height:W.userData.height,segmentKey:v,vertices:W.vertices,center:W.userData.center,mesh:W,pointCount:T,hasPointsButNotEnough:T>0&&T<1};R?(this.results.segmentsWithPoints++,this.results.existSegments.push(D)):(this.results.segmentsWithoutPoints++,this.results.missingSegments.push(D))}return this.results.analysisTime=Bn()-i,this.printAnalysisResults(),this.results}isPointInWallSegmentOptimized(e,n,o,r,s,i,a,u,d,f,c){if(Math.abs(o.distanceToPoint(e))>c)return!1;let h=new Vt().subVectors(e,n),g=Math.abs(h.dot(s)),x=Math.abs(h.dot(i));if(g>d&&x>f)return!1;let y=new Vt().subVectors(e,r),m=y.dot(s),p=y.dot(i),b=m>=-c&&m<=a+c,w=p>=-c&&p<=u+c;return b&&w}getWorldVerticesNew(e){return e}highlightEmptySegments(e,n=null,o=null,r=1){if(!this.results)return console.warn("\u8BF7\u5148\u8FD0\u884C\u5206\u6790\u51FD\u6570"),null;let s=new Map,i=new Map;for(let d of this.results.missingSegments)s.set(d.segmentKey,d),i.has(d.row)||i.set(d.row,new Map),i.get(d.row).set(d.col,{segmentKey:d.segmentKey,isEmpty:!0,visited:!1});for(let d of e)if(d.userData&&d.userData.type==="wallSegment"){let f=`row${d.userData.row}_col${d.userData.col}`,c=s.has(f);i.has(d.userData.row)||i.set(d.userData.row,new Map),i.get(d.userData.row).has(d.userData.col)||i.get(d.userData.row).set(d.userData.col,{segmentKey:f,isEmpty:c,visited:!1})}let a=[],u=[[0,1],[1,0],[0,-1],[-1,0]];for(let[d,f]of s){let c=i.get(f.row)?.get(f.col);if(c&&!c.visited){let l=[],h=[[f.row,f.col]],g=0;for(c.visited=!0;g<h.length;){let x=h[g++],y=x[0],m=x[1],p=`row${y}_col${m}`;s.has(p)&&l.push(s.get(p));for(let b of u){let w=y+b[0],z=m+b[1];if(i.has(w)&&i.get(w).has(z)){let M=i.get(w).get(z);!M.visited&&M.isEmpty&&(M.visited=!0,h.push([w,z]))}}}l.length>=r&&a.push(l)}}return this.clusterResults={clusters:a.sort((d,f)=>f.length-d.length),totalClusters:a.length},this.clusterResults}getClusterInfo(){if(!this.clusterResults)return null;let e=this.clusterResults.clusters.map(n=>n.length);return{totalClusters:this.clusterResults.totalClusters,clusterSizes:this.clusterResults.clusters.map((n,o)=>({clusterIndex:o,size:n.length,segments:n.map(r=>r.segmentKey)})),largestCluster:e.length?Math.max(...e):0,smallestCluster:e.length?Math.min(...e):0}}printAnalysisResults(){if(!this.results){console.warn("\u6CA1\u6709\u53EF\u7528\u7684\u5206\u6790\u7ED3\u679C");return}}isValidVector3(e){return e&&!isNaN(e.x)&&!isNaN(e.y)&&!isNaN(e.z)&&isFinite(e.x)&&isFinite(e.y)&&isFinite(e.z)}},fr=(t,e)=>{let n={},o=[],r=[];if(!t||t.length===0)return n.originalVertices=o,n.allCenterPoints=r,n;if(e=="xyz")for(let s of t){r.push({x:s.center.x,y:s.center.y,z:s.center.z});let i=[];for(let a=0;a<s.vertices.length;a++)i.push({x:s.vertices[a].x,y:s.vertices[a].y,z:s.vertices[a].z});o.push(i)}else if(e=="three")for(let s of t){r.push(new Oe.Vector3(s.center.x,s.center.y,s.center.z));let i=[];for(let a=0;a<s.vertices.length;a++)i.push(new Oe.Vector3(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z));o.push(i)}else if(e=="array")for(let s of t){r.push(s.center.x),r.push(s.center.y),r.push(s.center.z);let i=[];for(let a=0;a<s.vertices.length;a++)i.push(s.vertices[a].x,s.vertices[a].y,s.vertices[a].z);o.push(i)}return n.originalVertices=o,n.allCenterPoints=r,n},Ti=(t,e)=>{let n=new Vt().copy(t.start).add(t.end).multiplyScalar(.5),o=new Vt().subVectors(new Vt(n.x,n.y,n.z+1),n).normalize(),r=new Vt(t.direction.x,t.direction.y,0);r.applyAxisAngle(o,Math.PI/2);let s=new Vt().copy(n).add(r.clone().multiplyScalar(t.length)),i=new Vt().copy(n).sub(r.clone().multiplyScalar(t.length));if(n.z=t.start.z,s.z=t.start.z,i.z=t.start.z,e=="three")return{start:new Oe.Vector3(s.x,s.y,s.z),end:new Oe.Vector3(i.x,i.y,i.z),center:new Oe.Vector3(n.x,n.y,n.z),direction:new Oe.Vector3(r.x,r.y,r.z)};if(e=="xyz")return{start:{x:s.x,y:s.y,z:s.z},end:{x:i.x,y:i.y,z:i.z},center:{x:n.x,y:n.y,z:n.z},direction:{x:r.x,y:r.y,z:r.z}};if(e=="array")return{start:[s.x,s.y,s.z],end:[i.x,i.y,i.z],center:[n.x,n.y,n.z],direction:[r.x,r.y,r.z]}},Si=(t,e=!0,n="three",o="xyz")=>{o=="xyz"?dr(t,e,n):o=="array"&&bo(t,e,n)},Ri=(t,e=!0,n="three",o="xyz")=>{o=="xyz"?Di(t,e,n):o=="array"&&ur(t,e,n)},Di=(t,e=!0,n="three")=>{for(let o=0;o<t.length;o++){let r=t[o];dr(r,e,n)}},dr=(t,e=!0,n="three")=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&e)return;let o=new bn().setFromCoplanarPoints(new Vt(t.start.x,t.start.y,t.start.z),new Vt(t.end.x,t.end.y,t.end.z),new Vt(t.start.x,t.start.y,t.rooftopPz)),r=new Vt(t.start.x,t.start.y,t.start.z).distanceTo(new Vt(t.end.x,t.end.y,t.end.z)),s=Math.abs(t.rooftopPz-t.start.z),i=new Ln,{horizontalSubdivisions:a,verticalSubdivisions:u}=i.initLimits4(r,s),d=i.createSubdividedWallPlane(t,u,a,null);i.analyzePointDistribution(d,t.originalPoints,.01,{projectPlane:o});let f=i.highlightEmptySegments(d,null,null),c=[];if(f&&f.clusters)for(let g of f.clusters){let x=fr(g,n);x&&c.push(x)}let h=Ti(t,n).direction;if(t.checkResults=c,t.verticalDirection=h,t.saveExistSegments){let g=[],x=fr(i.results.existSegments,n);x&&g.push(x),t.existSegments=g}},vi=t=>{if((t.isBayWindow||t.isWindow||t.isBalconyRailing)&&limit)return;let e=new bn().setFromCoplanarPoints(new Vt(t.start.x,t.start.y,t.start.z),new Vt(t.end.x,t.end.y,t.end.z),new Vt(t.start.x,t.start.y,t.rooftopPz)),n=new Vt(t.start.x,t.start.y,t.start.z).distanceTo(new Vt(t.end.x,t.end.y,t.end.z)),o=Math.abs(t.rooftopPz-t.start.z),r=new Ln,{horizontalSubdivisions:s,verticalSubdivisions:i}=r.initLimits4(n,o),a=r.createSubdividedWallPlane(t,i,s,null);return r.analyzePointDistribution(a,t.originalPoints,.01,{projectPlane:e}),r.highlightEmptySegments(a,null,null)};import*as N from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";import*as Y from"three";import{CSS2DObject as vc}from"three/examples/jsm/renderers/CSS2DRenderer.js";import"three/examples/jsm/utils/BufferGeometryUtils.js";import Ee from"path";import{fileURLToPath as Ci}from"url";import kn from"path";import{fileURLToPath as Vi}from"url";import{Vector3 as lc}from"three";var Ii=Vi(import.meta.url),wo=kn.dirname(Ii),ac=kn.resolve(wo,"./data/lines.meta.json"),cc=kn.resolve(wo,"./data/lines.bin"),uc=kn.resolve(wo,"./data/checks.bin");var Hi=Ci(import.meta.url),ve=Ee.dirname(Hi),gc=Ee.resolve(ve,"./data/lines.tmp.bin"),xc=Ee.resolve(ve,"./data/checks.tmp.bin"),yc=Ee.resolve(ve,"./data/lines.new_points.tmp.bin"),bc=Ee.resolve(ve,"./data/lines.new_points.bin"),wc=Ee.resolve(ve,"./data/lines.meta.json"),zc=Ee.resolve(ve,"./data/lines.bin"),Mc=Ee.resolve(ve,"./data/checks.bin"),Ec=Ee.resolve(ve,"./data/lines.exist_segments.bin"),Pc=Ee.resolve(ve,"./data/lines.exist_segments.tmp.bin");import{Vector3 as Wi}from"three";var On=class{points=[];_tempArr=[0,0,0];constructor(e){}initPoints(e){this.points=e}getPointByIndex(e,n){let o=e*3;return n.x=this.points[o],n.y=this.points[o+1],n.z=this.points[o+2],n}getPointArrByIndex(e){let n=e*3;return[this.points[n],this.points[n+1],this.points[n+2]]}getPoint2ArrByIndex(e){let n=e*3;return[this.points[n],this.points[n+1]]}getIndexsByIndex(e,n){n.length=0,n.push(e*3),n.push(e*3+1),n.push(e*3+2)}pointsToVector3Arr(e){let n=[];for(let o=0;o<e.length/3;o++)n.push(new Wi(e[o*3],e[o*3+1],e[o*3+2]));return n}vector3ArrToPoints(e){let n=[];for(let o=0;o<e.length;o++)n.push(e[o].x,e[o].y,e[o].z);return n}vector3ArrToFloat32Array(e){return new Float32Array(e.flatMap(o=>[o.x,o.y,o.z]))}clsoe(){this.points=[]}};var mr=(t,e,n)=>{let o=new Y.LineBasicMaterial({color:"#0011ff"}),r=[];r.push(t),r.push(e);let s=new Y.BufferGeometry;s.setFromPoints(r),s.rotateX(-Math.PI/2),n.add(new Y.Line(s,o))},Pe=(t,e,n,o,r=1e-8)=>{let s=e.x-t.x,i=e.y-t.y,a=o.x-n.x,u=o.y-n.y,d=s*u-i*a;if(Math.abs(d)<r)return null;let f=((n.x-t.x)*u-(n.y-t.y)*a)/d,c=((n.x-t.x)*i-(n.y-t.y)*s)/d;return f<-r||f>1+r||c<-r||c>1+r?null:{point:new Y.Vector3(t.x+f*s,t.y+f*i,t.z)}},pr=(t,e,n={})=>{let{fontSize:o=36,fontFamily:r="sans-serif",backgroundColor:s="rgba(0,0,0,0)",textColor:i="#ffffff",padding:a=10,maxWidth:u=256}=n,f=document.createElement("canvas").getContext("2d");f.font=`bold ${o}px ${r}`;let l=f.measureText(t).width,h=Math.min(l+a*2,u),g=o+a*2,x=document.createElement("canvas");x.width=h,x.height=g;let y=x.getContext("2d");y.fillStyle=s,y.fillRect(0,0,h,g),y.fillStyle=i,y.font=`bold ${o}px ${r}`,y.textAlign="center",y.textBaseline="middle",l>u-a*2?_i(y,t,h/2,g/2,u,o):y.fillText(t,h/2,g/2);let m=new Y.CanvasTexture(x),p=new Y.SpriteMaterial({map:m,depthTest:!1,transparent:!0}),b=new Y.Sprite(p);b.position.set(e.x,e.y,e.z);let w=.005;return b.scale.set(h*w,g*w,1),b};function _i(t,e,n,o,r,s){let i=e.split(""),a="",u=s*1.2,d=o;for(let f=0;f<i.length;f++){let c=a+i[f];t.measureText(c).width>r&&f>0?(t.fillText(a,n,d),a=i[f],d+=u):a=c}t.fillText(a,n,d)}var Xt=(t,e,n=.01)=>{let o=Math.sqrt(t.x*t.x+t.y*t.y+t.z*t.z),r=Math.sqrt(e.x*e.x+e.y*e.y+e.z*e.z);if(o<1e-10||r<1e-10)return{parallel:!1,sameDirection:null,angle:NaN};let s={x:t.x/o,y:t.y/o,z:t.z/o},i={x:e.x/r,y:e.y/r,z:e.z/r},a=s.x*i.x+s.y*i.y+s.z*i.z;a=Math.max(-1,Math.min(1,a));let u=Math.acos(Math.abs(a)),d=u<=n;return{parallel:d,sameDirection:d?a>0:null,angle:u}};var gr=(t,e,n,o)=>{if(e<1e-4||n<1e-4)return{};let r=t,s=[];if(o&&o.length&&(s=o.map(M=>M.clone())),!r||r.length<4)return{};let i=new Y.Vector3().subVectors(r[1],r[0]).normalize(),a=new Y.Vector3().subVectors(r[3],r[0]).normalize(),u=i.clone().cross(a).normalize(),d=1/0,f=-1/0,c=1/0,l=-1/0;for(let M of r){let P=M.dot(i),E=M.dot(a);P<d&&(d=P),P>f&&(f=P),E<c&&(c=E),E>l&&(l=E)}let h=r[0].dot(u),g=i.clone().multiplyScalar(d).add(a.clone().multiplyScalar(c)).add(u.clone().multiplyScalar(h)),x=i.clone().multiplyScalar(f).add(a.clone().multiplyScalar(c)).add(u.clone().multiplyScalar(h)),y=i.clone().multiplyScalar(f).add(a.clone().multiplyScalar(l)).add(u.clone().multiplyScalar(h)),m=i.clone().multiplyScalar(d).add(a.clone().multiplyScalar(l)).add(u.clone().multiplyScalar(h)),p=[g,x,y,m],b=Xt(new Y.Vector3().subVectors(g,x),new Y.Vector3(0,0,1));b&&b.parallel&&(p=[g,m,y,x]);let w=0;if(o.length>0){let M=new Y.Vector3().subVectors(p[1],p[0]).normalize(),P=M.clone().negate(),E=new Y.Vector3().subVectors(p[3],p[0]).normalize(),S=E.clone().negate(),I=p[0].distanceTo(p[1]),W=p[0].distanceTo(p[3]),v=I/e,T=W/n,R=.001;for(let D=0;D<v;D++){let H=p[0],V=0;for(let C=0;C<T;C++){let _=H.clone().addScaledVector(M,e),A=_.clone().addScaledVector(E,n),L=H.clone().addScaledVector(E,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=L}if(V>=T/2)break;w+=V,p[0]=p[0].addScaledVector(M,e),p[3]=p[3].addScaledVector(M,e)}I=p[0].distanceTo(p[1]),v=I/e;for(let D=0;D<v;D++){let H=p[1],V=0;for(let C=0;C<T;C++){let _=H.clone().addScaledVector(P,e),A=_.clone().addScaledVector(E,n),L=H.clone().addScaledVector(E,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=L}if(V>=T/2)break;w+=V,p[1]=p[1].addScaledVector(P,e),p[2]=p[2].addScaledVector(P,e)}I=p[0].distanceTo(p[1]),v=I/e;for(let D=0;D<T;D++){let H=p[3],V=0;for(let C=0;C<v;C++){let _=H.clone().addScaledVector(M,e),A=_.clone().addScaledVector(S,n),L=H.clone().addScaledVector(S,n),k=new Y.Vector3().add(H).add(_).add(A).add(L).multiplyScalar(.25);for(let F=0;F<s.length;F++)if(s[F].distanceTo(k)<R){V++;break}H=_}if(V>=v/2)break;w+=V,p[2]=p[2].addScaledVector(S,n),p[3]=p[3].addScaledVector(S,n)}}let z=p[0].distanceTo(p[1])*p[0].distanceTo(p[3]);return{facePoints:p,boxArea:z,totalInPlaneNum:w}},Zt=(t,e,n,o,r=Y.MathUtils.degToRad(8))=>{let s=new Y.Vector3,i=new Y.Vector3;s.subVectors(new Y.Vector3(e.x,e.y,0),new Y.Vector3(t.x,t.y,0)).normalize(),i.subVectors(new Y.Vector3(o.x,o.y,0),new Y.Vector3(n.x,n.y,0)).normalize();let a=s.dot(i),u=Math.acos(Math.min(Math.abs(a),1)),d=1-Math.min(u/r,1),f=a>0?"same":"opposite";return{parallelism:d,angle:u,angleDeg:Y.MathUtils.radToDeg(u),isParallel:u<r,direction:f,vectors:{v1:s,v2:i}}},ie=(t,e,n,o)=>{let r=new Y.Vector3().subVectors(e,t),s=new Y.Vector3().subVectors(o,n),i=r.length(),a=s.length();if(i===0||a===0)return{rate:NaN,angle:NaN,isPerpendicular:!1,isParallel:!1};r.normalize(),s.normalize();let u=Math.abs(r.dot(s)),d=Math.min(1,Math.max(0,u)),f=Math.acos(d),c=Y.MathUtils.radToDeg(f),l=1-d,h=1e-6;return{rate:l,percent:`${(l*100).toFixed(2)}%`,angle:c,isPerpendicular:Math.abs(c-90)<.01,isParallel:c<.01}},At=(t,e,n=!1)=>new Y.Vector3((t.x+e.x)/2,(t.y+e.y)/2,n?0:(t.z+e.z)/2),we=(t,e,n,o,r={})=>{let{parallelAngleMax:s=Y.MathUtils.degToRad(10),collinearAngleMax:i=Y.MathUtils.degToRad(7),distanceThreshold:a=2,overlapThreshold:u=.01}=r,d=new Y.Vector3().subVectors(e,t),f=new Y.Vector3().subVectors(o,n),c=d.clone().normalize(),l=f.clone().normalize(),h=Y.MathUtils.clamp(c.dot(l),-1,1),g=Math.acos(Math.abs(h)),x=Y.MathUtils.radToDeg(g);if(g>s)return{type:"not_parallel",angleDeg:x,avgPerpendicularDistance:null,maxPerpendicularDistance:null,gap:null,overlap:null,closestDistance:null};h<0&&l.negate();let y=new Y.Vector3().addVectors(c,l).normalize(),m=new Y.Vector3().addVectors(t,e).multiplyScalar(.5),p=hr(n,m,c),b=hr(o,m,c),w=(p+b)/2,z=Math.max(p,b),M=t.dot(y),P=e.dot(y),E=n.dot(y),S=o.dot(y),I=Math.min(M,P),W=Math.max(M,P),v=Math.min(E,S),T=Math.max(E,S),R=W-I,D=T-v,H=Math.min(R,D),V=Math.min(W,T)-Math.max(I,v),C=V<0?-V:0,_=V>0?V:0,A=H>0?_/H:0,L=Ai(t,e,n,o),k=g<=i&&w<a,F;return k?F=A>.5?"collinear_overlap":"collinear_gap":g<=s&&(F="parallel_offset"),{type:F,angleDeg:x,avgPerpendicularDistance:w,maxPerpendicularDistance:z,gap:C,overlap:_,closestDistance:L}};function hr(t,e,n){let o=new Y.Vector3().subVectors(t,e),r=o.dot(n),s=n.clone().multiplyScalar(r);return new Y.Vector3().subVectors(o,s).length()}function Ai(t,e,n,o){let r=new Y.Vector3().subVectors(e,t),s=new Y.Vector3().subVectors(o,n),i=new Y.Vector3().subVectors(n,t),a=r.dot(r),u=r.dot(s),d=s.dot(s),f=r.dot(i),c=s.dot(i),l=a*d-u*u,h,g;l<1e-10?(h=0,g=d!==0?c/d:0):(h=(u*c-d*f)/l,g=(a*c-u*f)/l),h=Y.MathUtils.clamp(h,0,1),g=d!==0?Y.MathUtils.clamp((u*h+c)/d,0,1):0,h=a!==0?Y.MathUtils.clamp((u*g-f)/a,0,1):0;let x=t.clone().add(r.clone().multiplyScalar(h)),y=n.clone().add(s.clone().multiplyScalar(g));return x.distanceTo(y)}var Ve=t=>{let e=999999,n=-999999,o=0;for(let r=0;r<t.length;r++)t[r].z>n&&(n=t[r].z),t[r].z<e&&(e=t[r].z),o+=t[r].z;return o=o/t.length,{minZ:e,maxZ:n,averagePz:o}},wn=(t,e,n)=>{let o=new Y.Plane,r=new Y.Vector3(t.x,t.y,t.z),s=new Y.Vector3(e.x,e.y,e.z),i=new Y.Vector3(e.x,e.y,e.z+5);o.setFromCoplanarPoints(r,s,i);let a=[],u=[];n.forEach(y=>{let m=new Y.Vector3;o.projectPoint(y,m),u.push(m.clone()),m.z=r.z,a.push(m)});let d=new Y.Vector3,f=new Y.Vector3;o.projectPoint(r,d),o.projectPoint(s,f);let c=new Y.Line3(d,f),l=[],h=[],g=[],x=new Y.Vector3;return a.forEach((y,m)=>{c.closestPointToPoint(y,!0,x).distanceTo(y)<.001?(l.push(n[m]),h.push(u[m])):g.push(y)}),{newOriginalPoints:l,newProjectPoints:g,newOnLinePoints:h}},xr=(t,e,n)=>{function o(r,s,i,a,u,d,f,c,l){let h=new Y.Line3(r,s),g=new Map,x=new Y.Vector3,y=r.clone(),m=s.clone(),p=0;for(;;){let b=new Map,w=0;for(let z=0;z<l.checkResults.length;z++)if(!(l.checkResults[z].allCenterPoints.length/c*100<1))for(let P=0;P<l.checkResults[z].allCenterPoints.length;P++){if(g.has(z)&&g.get(z).removePtsIndex.includes(P))continue;let E=l.checkResults[z].allCenterPoints[P];E=new Y.Vector3(E.x,E.y,E.z);let S=h.closestPointToPoint(E,!0,x),I=E.distanceTo(S);Math.abs(I-u)<.01&&(w++,b.has(z)?b.get(z).removePtsIndex.push(P):b.set(z,{index:z,removePtsIndex:[P]}))}if(p==f){r=y,s=m;break}if(w<d/20*19||w==0){b.clear(),f-p<5&&(r=y,s=m,g.clear());break}else{p++;for(let[z,M]of b)g.has(z)?g.get(z).removePtsIndex.push(...M.removePtsIndex):g.set(z,M);b.clear(),r=r.addScaledVector(i,a),s=s.addScaledVector(i,a),h.set(r,s)}}if(g.size>0&&p!=f)for(let[b,w]of g){let z=[],M=[];for(let P=0;P<l.checkResults[b].allCenterPoints.length;P++)w.removePtsIndex.includes(P)||(z.push(l.checkResults[b].allCenterPoints[P]),M.push(l.checkResults[b].originalVertices[P]));l.checkResults[b].allCenterPoints=z,l.checkResults[b].originalVertices=M}}for(let r=0;r<t.length;r++){let s=t[r];if(s.length<.5)continue;let i=-1,a=-1,u=e.readLineById(s.id);u.checkResults=e.getLineCheckReultsById(s.id),u.originalPoints=e.getOriginalPointsById(s.id),s.originalPoints=u.originalPoints,s.checkResults=u.checkResults,s.checkResults.allCenterPoints=[],u=null;for(let D=0;D<s.checkResults.length;D++)for(let H=0;H<s.checkResults[D].originalVertices.length;H++){let V=Fn(s.checkResults[D].originalVertices[H]);s.checkResults.allCenterPoints.push(V)}if(!s.checkResults||s.checkResults.length<=0)continue;if(s.checkResults[0].originalVertices&&s.checkResults[0].originalVertices.length>0){let D=s.checkResults[0].originalVertices[0],[H,V,C,_]=D;H=new Y.Vector3(H.x,H.y,H.z),V=new Y.Vector3(V.x,V.y,V.z),C=new Y.Vector3(C.x,C.y,C.z),_=new Y.Vector3(_.x,_.y,_.z),i=H.distanceTo(V),a=H.distanceTo(_);let A=new Y.Vector3().subVectors(H,V).normalize(),L=Xt(A,new Y.Vector3(0,0,1));L&&L.parallel&&([a,i]=[i,a])}if(i<0||a<0)continue;let d=new Y.Vector3(s.start.x,s.start.y,s.start.z),f=new Y.Vector3(s.end.x,s.end.y,s.end.z),c=d.distanceTo(f),l=s.rooftopPz-s.start.z,h=Math.ceil(c/i),g=Math.ceil(l/a),x=d.clone(),y=f.clone(),m=y.clone().add(new Y.Vector3(0,0,l)),p=x.clone().add(new Y.Vector3(0,0,l)),b=new Y.Vector3().subVectors(y,x).normalize(),w=b.clone().negate(),M=new Y.Vector3().subVectors(m,x).normalize().clone().negate(),P=i/2,E=a/2,S=c*l,I=i*a,W=S/I;o(x,p,b,i,P,g,h,W,s),o(y,m,w,i,P,g,h,W,s),s.start=x,s.end=y;let{newOriginalPoints:v}=wn(x,y,s.originalPoints);s.originalPoints=v,n.stageReplaceCheckResults(s.id,s.checkResults);let R=new On().vector3ArrToFloat32Array(s.originalPoints);n.stageReplacePoints(s.id,R),s.checkResults=null,s.originalPoints=null}n.commit()},Gt=(t,e)=>new Y.Vector3(t.x,t.y,0).distanceTo(new Y.Vector3(e.x,e.y,0)),zo=(t,e,n=1e-6)=>{let o=new Y.Vector3;return t.closestPointToPoint(e,!0,o),o.distanceTo(e)<n},yr=(t,e,n,o=1e-6)=>{let r=new Y.Line3(new Y.Vector3(t.x,t.y,t.z),new Y.Vector3(e.x,e.y,e.z)),s=new Y.Vector3;return r.closestPointToPoint(new Y.Vector3(n.x,n.y,n.z),!0,s),s.distanceTo(new Y.Vector3(n.x,n.y,n.z))<o},We=t=>{let e=0,n=[];for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o].distanceTo(t[r]);s>e&&(e=s,n=[t[o],t[r]])}return n},ct=(t,e,n=.03)=>Math.abs(t.x-e.x)<n&&Math.abs(t.y-e.y)<n&&Math.abs(t.z-e.z)<n;var br=()=>{let t=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0"),e=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0"),n=Math.floor(Math.random()*75+180).toString(16).padStart(2,"0");return`#${t}${e}${n}`};var Mo=(t,e=.001)=>{let n=new Map,o=[];for(let r of t){let s=Math.round(r.x/e),i=Math.round(r.y/e),a=Math.round(r.z/e),u=`${s},${i},${a}`;n.has(u)||(n.set(u,!0),o.push(r))}return o},wr=t=>{let e=t.center,n=t.halfSize,r=t.rotation.elements,s=new Y.Vector3(r[0],r[1],r[2]).multiplyScalar(n.x),i=new Y.Vector3(r[3],r[4],r[5]).multiplyScalar(n.y),a=new Y.Vector3(r[6],r[7],r[8]).multiplyScalar(n.z);function u(f,c,l){return e.clone().addScaledVector(s,f).addScaledVector(i,c).addScaledVector(a,l)}return[u(-1,-1,-1),u(1,-1,-1),u(1,1,-1),u(-1,1,-1),u(-1,-1,1),u(1,-1,1),u(1,1,1),u(-1,1,1)]},Eo=(t,e)=>{let n=t[0],o=t[1],r=t[2],s=t[3],i=new Y.Vector3().subVectors(o,n),a=new Y.Vector3().subVectors(s,n),u=i.length(),d=a.length();i.normalize(),a.normalize();let f=new Y.Vector3(0,0,1),l=new Y.Vector3().add(n).add(o).add(r).add(s).multiplyScalar(.25).clone().addScaledVector(f,e/2),h=new Y.BoxGeometry(u,d,e),g=new Y.Matrix4;return g.makeBasis(i,a,f),g.setPosition(l),h.applyMatrix4(g),h.computeVertexNormals(),h},Fn=t=>{if(!t||t.length===0)return null;let e=new Y.Vector3(0,0,0);for(let n of t)Array.isArray(n)?(e.x+=n[0],e.y+=n[1],e.z+=n[2]):(e.x+=n.x,e.y+=n.y,e.z+=n.z);return e.divideScalar(t.length),e},zr=t=>{if(!t||t.length<3)return 0;let e=Array.isArray(t[0]),n=0;for(let o=0;o<t.length;o++){let r=t[o],s=t[(o+1)%t.length];e?n+=r[0]*s[1]-s[0]*r[1]:n+=r.x*s.y-s.x*r.y}return Math.abs(n)/2};import*as Yt from"three";var Bi=(t,e)=>{console.log("lines",t);for(let n=0;n<t.length;n++){let o=t[n],r=[];o.originalPoints.forEach(b=>{let w=new Yt.Vector3(b.x,b.y,0),z=!1;for(let M=0;M<r.length;M++)if(ct(w,r[M],.01)){z=!0;break}z||r.push(w)});let s=Math.round(Math.max(5,Math.min(350,20*Math.sqrt(o.length)))),i=1,a=new Yt.Vector3(o.start.x,o.start.y,o.start.z),u=new Yt.Vector3(o.end.x,o.end.y,o.end.z),d=new Yt.Vector3().subVectors(u,a).normalize(),f=d.clone().cross(new Yt.Vector3(0,0,1)).normalize(),c=f.clone().negate(),l=[],h=[],g=o.length/s;console.log(g);for(let b=0;b<s;b++){let w=[];w.push(a.clone().addScaledVector(f,i)),w.push(w[0].clone().addScaledVector(d,g)),w.push(w[1].clone().addScaledVector(c,i*2)),w.push(a.clone().addScaledVector(c,g));let z=[];for(let M=0;M<r.length;M++)l.includes(M)||ki(r[M],w)&&(l.push(M),z.push(r[M]));z.length>0&&h.push(Oi(z)),a=a.addScaledVector(d,g)}let x=new Float32Array(r.length*3);r.forEach((b,w)=>{x[w*3]=b.x,x[w*3+1]=b.y,x[w*3+2]=b.z});let y=new Yt.BufferGeometry;y.setAttribute("position",new Yt.BufferAttribute(x,3)),y.rotateX(-Math.PI/2),e.add(new Yt.Points(y,new Yt.PointsMaterial({color:br(),size:.02})));let[m,p]=ji(h,{plane:"xy",trimRatio:0});mr(m,p,e)}};function Mr(t,e="xy"){if(e==="xy")return{x:t.x,y:t.y};if(e==="xz")return{x:t.x,y:t.z};if(e==="yz")return{x:t.y,y:t.z};throw new Error(`Unsupported plane: ${e}`)}function Li(t,e,n){let o=e.x-t.x,r=e.y-t.y,s=n.x-t.x,i=n.y-t.y;return o*i-r*s}function ki(t,e,n="xy"){if(!e||e.length!==4)throw new Error("rectPoints must contain 4 points");let o=Mr(t,n),r=e.map(a=>Mr(a,n)),s=!1,i=!1;for(let a=0;a<4;a++){let u=r[a],d=r[(a+1)%4],f=Li(u,d,o);if(f>1e-9&&(s=!0),f<-1e-9&&(i=!0),s&&i)return!1}return!0}function Oi(t){if(!t||t.length===0)return null;let e=new Yt.Vector3(0,0,0);for(let n of t)Array.isArray(n)?(e.x+=n[0],e.y+=n[1],e.z+=n[2]):(e.x+=n.x,e.y+=n.y,e.z+=n.z);return e.divideScalar(t.length),e}function Fi(t){return t instanceof Yt.Vector3?t.clone():Array.isArray(t)?new Yt.Vector3(t[0],t[1],t[2]):new Yt.Vector3(t.x,t.y,t.z)}function Ni(t,e="xy"){let n=Fi(t);if(e==="xy")return{x:n.x,y:n.y,up:n.z};if(e==="xz")return{x:n.x,y:n.z,up:n.y};if(e==="yz")return{x:n.y,y:n.z,up:n.x};throw new Error(`Unsupported plane: ${e}`)}function Er(t,e,n,o="xy"){if(o==="xy")return new Yt.Vector3(t,e,n);if(o==="xz")return new Yt.Vector3(t,n,e);if(o==="yz")return new Yt.Vector3(n,t,e);throw new Error(`Unsupported plane: ${o}`)}function qi(t,e,n){if(Math.abs(e)<1e-12)return t>=n?{x:1,y:0}:{x:0,y:1};let r=t+n,s=t*n-e*e,i=Math.sqrt(Math.max(0,r*r*.25-s)),a=r*.5+i,u=e,d=a-t,f=Math.hypot(u,d);return f<1e-12?{x:1,y:0}:{x:u/f,y:d/f}}function ji(t,e={}){let{plane:n="xy",trimRatio:o=0}=e;if(!t||t.length<2)return null;let r=t.map(V=>Ni(V,n)),s=0,i=0,a=0;for(let V of r)s+=V.x,i+=V.y,a+=V.up;s/=r.length,i/=r.length,a/=r.length;let u=0,d=0,f=0;for(let V of r){let C=V.x-s,_=V.y-i;u+=C*C,d+=C*_,f+=_*_}u/=r.length,d/=r.length,f/=r.length;let c=qi(u,d,f),l={x:-c.y,y:c.x},h=[];for(let V of r){let C=V.x-s,_=V.y-i,A=C*c.x+_*c.y,L=C*l.x+_*l.y;h.push({s:A,t:L,up:V.up})}h.sort((V,C)=>V.s-C.s);let g=h.length,x=Math.floor(g*o),y=Math.ceil(g*(1-o))-1;x=Math.max(0,Math.min(g-1,x)),y=Math.max(0,Math.min(g-1,y));let m=h[x],p=h[y],b=(()=>{let V=h.map(_=>_.t).sort((_,A)=>_-A),C=Math.floor(V.length/2);return V.length%2===0?(V[C-1]+V[C])*.5:V[C]})(),w=(()=>{let V=h.map(_=>_.up).sort((_,A)=>_-A),C=Math.floor(V.length/2);return V.length%2===0?(V[C-1]+V[C])*.5:V[C]})(),z=m.s,M=p.s,P=b,E=b,S=w,I=w,W=s+c.x*z+l.x*P,v=i+c.y*z+l.y*P,T=s+c.x*M+l.x*E,R=i+c.y*M+l.y*E,D=Er(W,v,S,n),H=Er(T,R,I,n);return[D,H]}var $i=async(t,e,n,o,r)=>{if(xr(t,o,r),t.length<=0||e.length<=0)return t;n||(n=[]);for(let m of t){let p=[],b=o.readLineById(m.id);if(b.checkResults=o.getLineCheckReultsById(m.id),m.checkResults=b.checkResults,!(!m.checkResults||m.checkResults.length<=0)){for(let w=0;w<m.checkResults.length;w++){let S=function(R){for(;E[R]!==R;)E[R]=E[E[R]],R=E[R];return R},z=m.checkResults[w];if(!z.originalVertices||z.originalVertices.length==0)continue;let M=[];z.originalVertices.forEach(R=>{let D=[];R.forEach(H=>{D.push(new N.Vector3(H.x,H.y,H.z))}),M.push(D)});let P=M.length,E=Array.from({length:P},(R,D)=>D);for(let R=0;R<P;R++)for(let D=R+1;D<P;D++){let H=M[R],V=M[D];if(H.some(_=>V.some(A=>_.equals(A)))){let _=S(R),A=S(D);_!==A&&(E[_]=A)}}let I=new Map,W=-1,v=-1;for(let R=0;R<P;R++){let D=S(R);I.has(D)||I.set(D,{points:[],area:0,index:-1,gridWidth:-1,gridHeight:-1,centerPts:[],facePoints:[],boxArea:-1,totalInPlaneNum:-1});let H=I.get(D),[V,C,_,A]=M[R],L=new N.Vector3().add(V).add(C).add(_).add(A).multiplyScalar(.25);if(H.points.push(...M[R]),H.area+=V.distanceTo(C)*V.distanceTo(A),H.index=w,H.centerPts.push(L),W==-1||v==-1){let k=new N.Vector3().subVectors(V,C).normalize();v=V.distanceTo(A),W=V.distanceTo(C);let F=Xt(k,new N.Vector3(0,0,1));F&&F.parallel&&([v,W]=[W,v])}H.gridWidth=W,H.gridHeight=v}let T=I.values();for(let R of I.values()){let{facePoints:D,boxArea:H,totalInPlaneNum:V}=gr(R.points,R.gridWidth,R.gridHeight,R.centerPts);R.centerPts=null,R.facePoints=D,R.boxArea=H,R.totalInPlaneNum=V}p.push(...T)}m.checkResults=null,m.mergeCheckRegion=p}}let s=[];for(let m=0;m<n.length;m++){let p=n[m];if(p.isFindBeam=!1,p.category=="door"){let b=[];p.coordinatesByArea.coordinates.forEach(E=>{b.push(new N.Vector3(E.x,E.y,E.z))}),b[0].equals(b[b.length-1])&&b.pop();let w=b[0].distanceTo(b[1]),z=b[0].distanceTo(b[3]);z>w&&([w,z]=[z,w],b=[b[0],b[3],b[2],b[1]]);let M=At(b[0],b[3],!0),P=At(b[1],b[2],!0);s.push({doorStartPt:M,doorEndPt:P,boxPoints:b,minZ:p.coordinatesByArea.heightData.minZ,maxZ:p.coordinatesByArea.heightData.maxZ,index:m,isFind:!1,inWall:!1})}}for(let m=0;m<s.length;m++){let p=s[m];for(let b=0;b<t.length;b++){if(t[b].length<.5)continue;let w=[],z=At(t[b].start,t[b].end,!0);w.push(z),w.push(At(z,t[b].start,!0)),w.push(At(z,t[b].end,!0));let M=!1,P=p.boxPoints.length;for(let E=0;E<w.length;E++){let S=w[E].x,I=w[E].y;for(let W=0,v=P-1;W<P;v=W++){let T=p.boxPoints[W].x,R=p.boxPoints[W].y,D=p.boxPoints[v].x,H=p.boxPoints[v].y;R>I!=H>I&&S<(D-T)*(I-R)/(H-R)+T&&(M=!M)}if(M)break}if(M){let E=new N.Vector3(t[b].start.x,t[b].start.y,0),S=new N.Vector3(t[b].end.x,t[b].end.y,0),I=p.doorStartPt.distanceTo(p.doorEndPt),W=E.distanceTo(S),v=Zt(p.doorStartPt,p.doorEndPt,E,S);if(Math.abs(W-I)<.5&&v&&v.angleDeg<15){p.doorStartPt=new N.Vector3(t[b].start.x,t[b].start.y,0),p.doorEndPt=new N.Vector3(t[b].end.x,t[b].end.y,0);break}else if(Math.abs(W-I)>1.3&&v&&v.angleDeg<15){let T=we(p.doorStartPt,p.doorEndPt,E,S,{parallelAngleMax:N.MathUtils.degToRad(15)}),R=p.doorStartPt.distanceTo(E),D=p.doorStartPt.distanceTo(S),H=p.doorEndPt.distanceTo(E),V=p.doorEndPt.distanceTo(S),C=R<.2||D<.2||H<.2||V<.2;if(T&&T.type=="collinear_overlap"&&T.maxPerpendicularDistance<.1&&!C){p.inWall=!0;break}else{let _=new N.Line3(E,S),A=_.closestPointToPoint(p.doorStartPt,!0,new N.Vector3),L=_.closestPointToPoint(p.doorEndPt,!0,new N.Vector3),k=A.distanceTo(p.doorStartPt),F=L.distanceTo(p.doorEndPt);if(k<.1||F<.1){p.inWall=!0;break}}}}else continue}}let i=0,a=[],u=[],d=[],f=[],c=.15;for(let m=0;m<t.length;m++){let p=t[m];if(p.noDetection)continue;let b=o.readLineById(p.id);if(b.originalPoints=o.getOriginalPointsById(p.id),b.checkResults=o.getLineCheckReultsById(p.id),p.originalPoints=b.originalPoints,p.checkResults=b.checkResults,b=null,p.doorAndBeamData=[],p.doorAndBeamIdToCheckResults=[],!p.mergeCheckRegion||p.mergeCheckRegion.length==0)continue;p.completePointAreaPercentage=-1;let w=new N.Box3;w.setFromPoints(p.originalPoints);let z=w.max.z,M=w.min.z,P=new N.Vector3(p.start.x,p.start.y,p.start.z),E=new N.Vector3(p.end.x,p.end.y,p.end.z),S=p.rooftopPz-p.start.z,I=0;for(let v of p.mergeCheckRegion){let T=v.facePoints,R=v.boxArea,D=v.totalInPlaneNum;if(!T||T.length<=0)continue;let H=T[0].distanceTo(T[1]),V=T[0].distanceTo(T[3]),C=v.gridHeight*(V/v.gridHeight/3*2)+v.gridHeight/2,_=new N.Vector3(T[0].x,T[0].y,T[0].z+C),A=new N.Vector3(T[1].x,T[1].y,T[1].z+C),L=new N.Line3(_,A),k=[],F=new N.Vector3;v.points.forEach(j=>{L.closestPointToPoint(j,!0,F).distanceTo(j)<v.gridHeight+.01&&k.push(j)});let B=[],O=[];for(;;){let j=[];for(let X=0;X<k.length;X++)O.includes(X)||j.length==0&&(j.push(k[X]),O.push(X));for(let X=0;X<k.length;X++)if(!O.includes(X)){for(let U=0;U<j.length;U++)if(k[X].distanceTo(j[U])<v.gridWidth+.01){j.push(k[X]),O.push(X),X=-1;break}}if(j.length==0)break;B.push(j)}let $=[];for(let j=0;j<B.length;j++){let X={lineSt:new N.Vector3,lineEd:new N.Vector3,facePoints:[]},U=We(B[j]);Array.isArray(U)&&U.length==2&&(X.lineSt=U[0],X.lineEd=U[1],X.facePoints.push(new N.Vector3(U[0].x,U[0].y,T[0].z)),X.facePoints.push(new N.Vector3(U[1].x,U[1].y,T[0].z)),X.facePoints.push(new N.Vector3(U[1].x,U[1].y,T[2].z)),X.facePoints.push(new N.Vector3(U[0].x,U[0].y,T[2].z)),$.push(X))}I+=v.area;let Z=0;if(D!=0&&(Z=v.gridHeight*v.gridWidth*D),(v.area-Z)/R*100>70&&$.length==1||$.length>1)for(let j=0;j<$.length;j++){let X=$[j],U=X.facePoints[0].distanceTo(X.facePoints[1]),at=X.facePoints[0].distanceTo(X.facePoints[3]),mt=U/p.length*100,St=p.rooftopPz-X.facePoints[2].z;if(at<1.5||z<X.facePoints[2].z||St>S/3*2)continue;let ot=!1,Mt=!1,bt=!1,Ct=new N.Vector3(0,0,0),Et=new N.Vector3(0,0,0),zt=!1,Pt=[],et=X.facePoints[2].clone(),ht=X.facePoints[3].clone(),nt=X.facePoints[0].clone(),J=X.facePoints[1].clone(),le=!1,wt=-1;if(Math.abs(X.facePoints[0].z-p.start.z)<.25){let te=!1;for(let lt=0;lt<e.length;lt++){let It=lt+1;if(It>=e.length)continue;let Ot=new N.Vector3(e[lt].x,e[lt].y,e[lt].z),Ft=new N.Vector3(e[It].x,e[It].y,e[It].z);if(Pe(new N.Vector3(Ot.x,Ot.y,0),new N.Vector3(Ft.x,Ft.y,0),new N.Vector3(X.facePoints[0].x,X.facePoints[0].y,0),new N.Vector3(X.facePoints[1].x,X.facePoints[1].y,0),.1)!=null){te=!0;break}}let{newOriginalPoints:ge}=wn(nt,J,p.originalPoints),Dt=[],ne=[];if(ge.forEach(lt=>{lt.z>=et.z?Dt.push(lt):lt.z<=nt.z&&ne.push(lt)}),Dt.length<20)continue;let xe=p.rooftopPz-et.z,tt=et.clone();tt.z+=xe;let mn=et.distanceTo(ht),Je=new N.Vector3().subVectors(ht,et).normalize(),pn=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(mn)))),gn=mn/pn,fe=[],tn=[];for(let lt=0;lt<pn;lt++)fe.push(new N.Vector3(et.x,et.y,et.z+xe/5).addScaledVector(Je,gn*lt)),fe.push(new N.Vector3(tt.x,tt.y,tt.z-xe/5).addScaledVector(Je,gn*lt));let xn=fe.length;for(let lt=0;lt<Dt.length;lt++)for(let It=0;It<fe.length;It++)if(new N.Vector3(Dt[lt].x,Dt[lt].y,Dt[lt].z).distanceTo(fe[It])<.05){tn.push(fe[It]),fe.splice(It,1);break}if(tn.length/xn<.5)continue;if(U>.17&&p.length>.5){let lt=Gt(P,nt),It=Gt(P,J),Ot=Gt(E,nt),Ft=Gt(E,J),it=.15,G=10;if(lt<it||It<it)if(Dt.length>10){let dt=new N.Vector3(p.start.x,p.start.y,0),ut=new N.Vector3(p.end.x,p.end.y,0),xt=new N.Vector3().subVectors(dt,ut).normalize(),vt=new N.Vector3,pt=1/0,Lt=-1,qt=!1,ft=!1;for(let rt=0;rt<t.length;rt++){if(rt==m)continue;let st=new N.Vector3(t[rt].start.x,t[rt].start.y,0),yt=new N.Vector3(t[rt].end.x,t[rt].end.y,0),gt=Pe(dt,ut,st,yt,.1),Rt=ie(dt,ut,st,yt);if(gt!=null){if(Rt&&Rt.angle>85){let Wt=gt.point.distanceTo(dt),Tt=gt.point.distanceTo(ut);if(Wt<.3){ft=!0,pt=1/0,Lt=-1;break}}continue}if(Rt&&Rt.angle>85){let Tt=new N.Line3(st,yt).closestPointToPoint(dt,!0,vt),kt=Zt(dt,ut,Tt,dt);if(!kt||kt.direction!="same")continue;let Ut=dt.clone().addScaledVector(xt,G),se=ut.clone().addScaledVector(xt.clone().negate(),G),$t=new N.Vector3().subVectors(yt,st).normalize(),ee=st.clone().addScaledVector($t.clone().negate(),.03),de=yt.clone().addScaledVector($t,.03),ae=Pe(Ut,se,ee,de);if(ae==null)continue;let oe=dt.distanceTo(ae.point);oe<pt&&(pt=oe,Lt=rt)}else if(Rt&&Rt.angle<5){let Wt=new N.Vector3(nt.x,nt.y,0),Tt=new N.Vector3(J.x,J.y,0),kt=Gt(Wt,st),Ut=Gt(Wt,yt),se=Gt(Tt,st),$t=Gt(Tt,yt),ee=Zt(dt,ut,st,Wt),de=!1,ae=o.readLineById(t[rt].id);if(ae.checkResults=o.getLineCheckReultsById(t[rt].id),t[rt].checkResults=ae.checkResults,ae=null,t[rt].checkResults){for(let oe=0;oe<t[rt].checkResults.length;oe++){let yn=[];for(let be=0;be<t[rt].checkResults[oe].originalVertices.length;be++){let De=Fn(t[rt].checkResults[oe].originalVertices[be]);De.z=0,yn.push(De)}let ye=We(yn);if(ye.length==2){let be=Gt(Wt,ye[0]),De=Gt(Wt,ye[1]),go=Gt(Tt,ye[0]),xo=Gt(Tt,ye[1]);if(be<.2||De<.2||go<.2||xo<.2){de=!0;break}}}t[rt].checkResults=null}if(ee&&ee.direction=="same"&&!de&&(kt<.1||Ut<.1||se<.1||$t<.1)){qt=!0,pt=1/0,Lt=-1;break}}}if(pt!=1/0&&pt<2&&(p.length-U>.2||p.length-U<.02)&&pt<U*2)t[Lt].length>.25&&U+pt>.5&&(le=!0,lt<it?(ht=ht.addScaledVector(xt,pt),nt=nt.addScaledVector(xt,pt)):(et=et.addScaledVector(xt,pt),J=J.addScaledVector(xt,pt)));else{if(!qt&&!ft&&p.length-U>.1)continue;if(!qt&&!ft&&p.length-U<.1&&p.length<1.8)continue}}else continue;if(Ot<it||Ft<it)if(Dt.length>10){let dt=new N.Vector3(p.start.x,p.start.y,0),ut=new N.Vector3(p.end.x,p.end.y,0),xt=new N.Vector3().subVectors(ut,dt).normalize(),vt=new N.Vector3,pt=1/0,Lt=-1,qt=!1,ft=!1;for(let rt=0;rt<t.length;rt++){if(rt==m)continue;let st=new N.Vector3(t[rt].start.x,t[rt].start.y,0),yt=new N.Vector3(t[rt].end.x,t[rt].end.y,0),gt=Pe(dt,ut,st,yt,.03),Rt=ie(dt,ut,st,yt);if(gt!=null){if(Rt&&Rt.angle>85){let Wt=gt.point.distanceTo(dt);if(gt.point.distanceTo(ut)<.3){ft=!0,pt=1/0,Lt=-1;break}}continue}if(Rt&&Rt.angle>85){let Tt=new N.Line3(st,yt).closestPointToPoint(ut,!0,vt),kt=Zt(ut,dt,Tt,ut);if(!kt||kt.direction!="same")continue;let Ut=dt.clone().addScaledVector(xt.clone().negate(),G),se=ut.clone().addScaledVector(xt,G),$t=new N.Vector3().subVectors(yt,st).normalize(),ee=st.clone().addScaledVector($t.clone().negate(),.03),de=yt.clone().addScaledVector($t,.03),ae=Pe(Ut,se,ee,de);if(ae==null)continue;let oe=ut.distanceTo(ae.point);oe<pt&&(pt=oe,Lt=rt)}else if(Rt&&Rt.angle<5){let Wt=new N.Vector3(nt.x,nt.y,0),Tt=new N.Vector3(J.x,J.y,0),kt=Gt(Wt,st),Ut=Gt(Wt,yt),se=Gt(Tt,st),$t=Gt(Tt,yt),ee=Zt(ut,dt,st,Wt),de=!1,ae=o.readLineById(t[rt].id);if(ae.checkResults=o.getLineCheckReultsById(t[rt].id),t[rt].checkResults=ae.checkResults,ae=null,t[rt].checkResults){for(let oe=0;oe<t[rt].checkResults.length;oe++){let yn=[];for(let be=0;be<t[rt].checkResults[oe].originalVertices.length;be++){let De=Fn(t[rt].checkResults[oe].originalVertices[be]);De.z=0,yn.push(De)}let ye=We(yn);if(ye.length==2){let be=Gt(Wt,ye[0]),De=Gt(Wt,ye[1]),go=Gt(Tt,ye[0]),xo=Gt(Tt,ye[1]);if(be<.2||De<.2||go<.2||xo<.2){de=!0;break}}}t[rt].checkResults=null}if(ee&&ee.direction=="same"&&!de&&(kt<.1||Ut<.1||se<.1||$t<.1)){qt=!0,pt=1/0,Lt=-1;break}}}if(pt!=1/0&&pt<2&&(p.length-U>.2||p.length-U<.02)&&pt<U*2)t[Lt].length>.25&&U+pt>.5&&(le=!0,Ot<it?(ht=ht.addScaledVector(xt,pt),nt=nt.addScaledVector(xt,pt)):(et=et.addScaledVector(xt,pt),J=J.addScaledVector(xt,pt)));else{if(!qt&&!ft&&p.length-U>.1)continue;if(!qt&&!ft&&p.length-U<.1&&p.length<1.8)continue}}else continue;U=ht.distanceTo(et)}if(mt<85&&U>.3||U>.7&&at>S-S/2){if(!p.isLine2){if(U<3.5)for(let lt=0;lt<s.length;lt++){let It=new N.Vector3(s[lt].doorStartPt.x,s[lt].doorStartPt.y,0),Ot=new N.Vector3(s[lt].doorEndPt.x,s[lt].doorEndPt.y,0),Ft=new N.Vector3(X.facePoints[0].x,X.facePoints[0].y,0),it=new N.Vector3(X.facePoints[1].x,X.facePoints[1].y,0),G=X.facePoints[2].z<s[lt].maxZ||Math.abs(X.facePoints[2].z-s[lt].maxZ)<.3,dt=Math.abs(X.facePoints[2].z-s[lt].maxZ)>.2;if(G){let ut=we(It.clone(),Ot.clone(),Ft.clone(),it.clone(),{distanceThreshold:2});if(ut&&ut.type=="collinear_gap"){let xt=At(It,Ot),vt=new N.Line3(new N.Vector3(p.start.x,p.start.y,0),new N.Vector3(p.end.x,p.end.y,0)),pt=new N.Vector3,Lt=vt.closestPointToPoint(xt,!0,pt);if(xt.distanceTo(Lt)<.15){let ft=Ft.distanceTo(It),rt=it.distanceTo(It);if(ft>.25&&rt>.25){let gt=Ft.distanceTo(Ot),Rt=it.distanceTo(Ot);if(gt>.2&&Rt>.2)continue}Mt=!0,zt=!0,Pt.push(s[lt].index),s[lt].isFind=!0,wt=lt,bt=n[s[lt].index].isFindBeam,n[s[lt].index].isFindBeam=!0;let st=new N.Vector3().subVectors(Ft,it).normalize(),yt=It.distanceTo(Ot)-ut.overlap;rt<ft&&st.negate(),ft>rt?(et=X.facePoints[2].clone().addScaledVector(st,yt),ht=X.facePoints[3].clone(),nt=ht.clone(),J=et.clone(),nt.z=J.z=X.facePoints[0].z):(et=X.facePoints[2].clone(),ht=X.facePoints[3].clone().addScaledVector(st,yt),nt=ht.clone(),J=et.clone(),nt.z=J.z=X.facePoints[0].z)}}else if(ut&&ut.type=="collinear_overlap"&&ut.maxPerpendicularDistance<.35){let xt=Ft.distanceTo(it),vt=Ot.distanceTo(It);if(Math.abs(xt-ut.overlap)<.25||Math.abs(vt-ut.overlap)<.25){if(vt>xt&&Math.abs(vt-ut.overlap)>1){let pt=Ft.distanceTo(It),Lt=Ft.distanceTo(Ot),qt=it.distanceTo(It),ft=it.distanceTo(Ot),rt=pt<Lt?pt:Lt,st=qt<ft?qt:ft,yt=new N.Vector3().subVectors(Ft,it).normalize(),gt=vt-ut.overlap;rt<st?(yt.negate(),et=X.facePoints[2].clone().addScaledVector(yt,gt),ht=X.facePoints[3].clone(),nt=ht.clone(),J=et.clone(),nt.z=J.z=X.facePoints[0].z):(et=X.facePoints[2].clone(),ht=X.facePoints[3].clone().addScaledVector(yt,gt),nt=ht.clone(),J=et.clone(),nt.z=J.z=X.facePoints[0].z)}U>1.5&&(Mt=!0,zt=!0,Pt.push(s[lt].index),s[lt].isFind=!0,bt=n[s[lt].index].isFindBeam,n[s[lt].index].isFindBeam=!0,wt=lt)}}}}if(!zt&&U<1.5)for(let lt=0;lt<s.length;lt++){if(s[lt].inWall)continue;let It=new N.Vector3(s[lt].doorStartPt.x,s[lt].doorStartPt.y,0),Ot=new N.Vector3(s[lt].doorEndPt.x,s[lt].doorEndPt.y,0),Ft=new N.Vector3(ht.x,ht.y,0),it=new N.Vector3(et.x,et.y,0),G=new N.Vector3().subVectors(It,Ot).normalize(),dt=new N.Vector3().subVectors(Ft,it).normalize(),ut=.25,xt=It.clone().addScaledVector(G,ut),vt=Ot.clone().addScaledVector(G.clone().negate(),ut),pt=Ft.clone().addScaledVector(dt,ut),Lt=it.clone().addScaledVector(dt.clone().negate(),ut);if(Zt(xt,vt,pt,Lt).isParallel)continue;let ft=Pe(xt,vt,pt,Lt),rt=!0,st=It.distanceTo(Ft),yt=It.distanceTo(it),gt=Ot.distanceTo(Ft),Rt=Ot.distanceTo(it);if((st<c||yt<c||gt<c||Rt<c||ft)&&rt){let Tt=new N.Line3(Ft,it);if(st<c||gt<c)if(Ct=Ft.clone(),st<c){let kt=Tt.closestPointToPoint(Ot,!1,new N.Vector3);Et=new N.Vector3().subVectors(Ot,kt).normalize()}else{let kt=Tt.closestPointToPoint(It,!1,new N.Vector3);Et=new N.Vector3().subVectors(It,kt).normalize()}else if(yt<c||Rt<c)if(Ct=it.clone(),yt<c){let kt=Tt.closestPointToPoint(Ot,!1,new N.Vector3);Et=new N.Vector3().subVectors(Ot,kt).normalize()}else{let kt=Tt.closestPointToPoint(It,!1,new N.Vector3);Et=new N.Vector3().subVectors(It,kt).normalize()}else if(ft){let kt=ft.point.distanceTo(Ft),Ut=ft.point.distanceTo(it),se=ft.point.distanceTo(xt),$t=ft.point.distanceTo(vt);if(kt<Ut?Ct=Ft.clone():Ct=it.clone(),se<$t){let ee=Tt.closestPointToPoint(vt,!1,new N.Vector3);Et=new N.Vector3().subVectors(vt,ee).normalize()}else{let ee=Tt.closestPointToPoint(xt,!1,new N.Vector3);Et=new N.Vector3().subVectors(xt,ee).normalize()}}bt=n[s[lt].index].isFindBeam,n[s[lt].index].isFindBeam=!0,Mt=!0,s[lt].isFind=!0,wt=lt}}}(Mt||te||Math.abs(at-S)>.1&&at>S/3&&U>.3)&&(ot=!0)}}if(ot){{let it=new N.Vector3().subVectors(nt,J),G=new N.Vector3().subVectors(J,nt),dt=1/0,ut=1/0,xt=nt.clone(),vt=J.clone(),pt=[],Lt=[],qt=new N.Line3(et,ht),ft=new N.Vector3,rt=999999;if(p.originalPoints.forEach(st=>{if(st.z-nt.z<at/2+.1&&st.z-nt.z>at/2-.1&&pt.push(st),st.z>et.z){let yt=qt.closestPointToPoint(st,!0,ft),gt=new N.Vector3().subVectors(yt,et),Rt=new N.Vector3().subVectors(yt,ht),Wt=Xt(it,Rt),Tt=Xt(G,gt);Wt&&Wt.parallel&&!Wt.sameDirection&&Tt&&Tt.parallel&&!Tt.sameDirection&&Lt.push(st)}}),pt.length>0){let{newProjectPoints:st}=wn(nt,J,pt);for(let yt of st)yt.z=nt.z;st.forEach(yt=>{let gt=new N.Vector3(yt.x,yt.y,nt.z),Rt=new N.Vector3().subVectors(gt,J),Wt=new N.Vector3().subVectors(gt,nt),Tt=Xt(it,Wt),kt=Xt(G,Rt);if(Tt&&Tt.parallel&&Tt.sameDirection){let Ut=gt.distanceTo(nt);Ut<dt&&(dt=Ut,xt=gt)}else if(kt&&kt.parallel&&kt.sameDirection){let Ut=gt.distanceTo(J);Ut<ut&&(ut=Ut,vt=gt)}})}if(xt.equals(nt)||(nt=xt,ht=new N.Vector3(nt.x,nt.y,ht.z)),vt.equals(J)||(J=vt,et=new N.Vector3(J.x,J.y,et.z)),Lt.length>10){let{newOnLinePoints:st}=wn(ht,et,Lt);for(let yt=0;yt<st.length;yt++){let Rt=qt.closestPointToPoint(st[yt],!0,ft).distanceTo(st[yt]);Rt>.02&&rt>Rt&&(rt=Rt)}et.z+=rt,ht.z+=rt,at+=rt}}let te=!1;for(let it=0;it<d.length;it++){let G=we(d[it].start,d[it].end,nt,J);G&&G.type=="collinear_overlap"&&G.maxPerpendicularDistance<.07&&(te=!0)}if(te){wt!=-1&&(bt||(n[s[wt].index].isFindBeam=!1,s[wt].isFind=!1));continue}let ge=J.distanceTo(nt);if(ge<.4&&!Mt||ge<.3&&Mt)continue;u.includes(m)||u.push(m);let Dt="",ne=Mt?"singleDoor":"";ne=zt?"pullOutDoor":ne;let xe=nt.z-p.start.z>1e-4?nt.z-p.start.z:0,tt={id:i,beamStart:et,beamEnd:ht,beamHeight:p.rooftopPz-et.z,doorStart:nt,doorEnd:J,doorHeight:at,nearId:-1,type:Dt,isExtend:le,isDoor:Mt,isPullOutDoor:zt,pcbDoorIndexs:Pt,beamNearWallVec:new N.Vector3(0,0,0),doorType:ne,doorOpenEndPoint:Ct,doorOpenVector:Et,lineIsOnlyBeam:!1,groundClearance:xe};if(tt.beamHeight<.1)continue;let mn=1/0,Je=!1,pn=new N.Vector3,gn=new N.Vector3,fe=-1,tn=-1,xn=-1,en=!1,lt=!1;{let it=new N.Line3(nt.clone(),J.clone()),G=-1;for(let dt=0;dt<t.length;dt++){if(t[dt].length<.5||dt==m)continue;let ut=new N.Vector3(t[dt].start.x,t[dt].start.y,t[dt].start.z),xt=new N.Vector3(t[dt].end.x,t[dt].end.y,t[dt].end.z),vt=we(ut,xt,nt,J),pt=Zt(ut,xt,nt,J);if(vt&&vt.maxPerpendicularDistance<.5){if(pt&&pt.isParallel){let gt=new N.Line3(ut,xt),Rt=new N.Vector3(nt.x,nt.y,ut.z),Wt=gt.closestPointToPoint(Rt,!0,new N.Vector3),Tt=ie(Wt,Rt,nt,J);Tt&&Tt.angle>85&&(tt.beamNearWallVec=new N.Vector3().subVectors(Wt,Rt).normalize(),tt.beamStart.z>t[dt].rooftopPz-.03&&U>1?lt=!0:lt=!1)}let Lt=[],qt=o.readLineById(t[dt].id);qt.originalPoints=o.getOriginalPointsById(t[dt].id),t[dt].originalPoints=qt.originalPoints,qt=null,t[dt].originalPoints.forEach(gt=>{gt.z>nt.z+.2&&gt.z<nt.z+at/2&&Lt.push(new N.Vector3(gt.x,gt.y,nt.z))}),t[dt].originalPoints=null;let ft=new N.Vector3,rt=[],st=[];for(let gt=0;gt<Lt.length;gt++){let Rt=it.closestPointToPoint(Lt[gt],!0,ft);Rt.distanceTo(Lt[gt])<.15&&(rt.push(Lt[gt]),st.push(Rt.clone()))}for(let gt=0;gt<st.length;gt++)zo(it,st[gt])&&rt.push(st[gt]);if(rt=st,rt=Mo(rt),rt.length<150)continue;let yt=We(rt);if(yt.length==2){let gt=yt[0].distanceTo(yt[1]);if(Math.abs(gt-it.distance())<.3){G=vt.maxPerpendicularDistance;let Rt=new N.Line3(ut,xt),Wt=new N.Vector3(nt.x,nt.y,ut.z),Tt=Rt.closestPointToPoint(Wt,!0,new N.Vector3);if(tt.beamNearWallVec=new N.Vector3().subVectors(Tt,Wt).normalize(),tt.beamStart.z>t[dt].rooftopPz-.03&&U>.8)lt=!0,en=!0;else if(te=!0,wt!=-1&&(bt||(n[s[wt].index].isFindBeam=!1,s[wt].isFind=!1)),tn!=-1&&xn!=-1){let kt=t[tn].doorAndBeamData[xn];kt.nearId=-1}break}}if(te)break}else vt&&vt.type=="collinear_overlap"&&vt.maxPerpendicularDistance>.3&&vt.maxPerpendicularDistance<.7&&(en=!0)}if(te){if(U<1.5||G<.08)continue;en=!0}}let It=!1;if(fe!=-1)for(let it=0;it<f.length&&!(f[it].length==1&&(f[it][0].id==i?(f[it].push({id:fe,beamStart:pn,beamEnd:gn}),It=!0):f[it][0].id==fe&&(f[it].push({id:i,beamStart:et,beamEnd:ht}),It=!0),It));it++);It||f.push([{id:i,beamStart:et,beamEnd:ht}]);let Ot=tt.doorStart.distanceTo(tt.doorEnd),Ft=Ve(p.originalPoints);if(tt.lineIsOnlyBeam=Ft.minZ>p.start.z&&Ft.minZ-p.start.z>1,Mt&&en){wt!=-1&&(bt||(n[s[wt].index].isFindBeam=!1,s[wt].isFind=!1));continue}if(Mt?Dt="door":Ot>1.5||le?Dt="beam":Dt=en&&Ot>.8?"beam":"hole",Dt!="beam"&&t[m].isLine2)continue;tt.type=Dt,tt.wallBelowBeam=lt;for(let it=0;it<a.length;it++){let G=t[a[it].linesIndex].doorAndBeamData[a[it].doorIndex];if(G.nearId!=-1||tt.type=="beam"&&G.type!="beam"||tt.type!="beam"&&G.type=="beam")continue;let dt=Zt(et.clone(),ht.clone(),G.beamStart.clone(),G.beamEnd.clone());if(dt&&dt.isParallel){let ut=et.distanceTo(ht),xt=G.beamStart.distanceTo(G.beamEnd),vt=new N.Line3(et.clone(),ht.clone());vt.start.z=0,vt.end.z=0;let pt=At(G.beamStart,G.beamEnd,!0);xt>ut&&(vt.start=new N.Vector3(G.beamStart.x,G.beamStart.y,0),vt.end=new N.Vector3(G.beamEnd.x,G.beamEnd.y,0),pt=At(et,ht,!0));let Lt=new N.Vector3,qt=vt.closestPointToPoint(pt,!0,Lt),ft=qt.distanceTo(pt),rt=new N.Vector3().subVectors(pt,qt).normalize(),st=rt.clone().negate(),yt=pt.clone().addScaledVector(st,ft+.1),gt=Pe(vt.start,vt.end,pt,yt),Rt=!Mt&&!G.isDoor&&(ut>xt*2||xt>ut*2);if(ft<.4&&gt!=null&&!Rt){let Wt=!1;for(let $t=0;$t<p.doorAndBeamData.length;$t++)if(p.doorAndBeamData[$t].id==G.id){Wt=!0;break}if(Wt)continue;G.nearId=i,tt.nearId=G.id;let Tt=.8;(G.isDoor||tt.isDoor)&&(G.isDoor=!0,tt.isDoor=!0),G.doorType!=""?tt.doorType=G.doorType:tt.doorType!=""&&(G.doorType=tt.doorType),G.isPullOutDoor?tt.isPullOutDoor=!0:tt.isPullOutDoor&&(G.isPullOutDoor=!0);let kt=Pt.some($t=>G.pcbDoorIndexs.includes($t)),Ut=!1,se=!1;if(tt.type=="beam"&&G.type=="beam"){let $t=o.readLineById(t[a[it].linesIndex].id);$t.originalPoints=o.getOriginalPointsById(t[a[it].linesIndex].id),t[a[it].linesIndex].originalPoints=$t.originalPoints,$t=null;let ee=Ve(t[a[it].linesIndex].originalPoints),de=Ve(p.originalPoints);t[a[it].linesIndex].originalPoints=null,ee.minZ>t[a[it].linesIndex].start.z&&ee.minZ-t[a[it].linesIndex].start.z>1&&de.minZ>p.start.z&&de.minZ-p.start.z>1&&(Ut=ut>xt,se=ut<xt,xt>ut&&(tt.isExtend=!0),ut>xt&&(t[a[it].linesIndex].isExtend=!0))}ut<xt&&!se||Ut?(Ut&&(st=st.negate(),rt=rt.negate()),ut>Tt&&!G.isPullOutDoor?(G.beamStart=et.clone().addScaledVector(st,ft),G.beamEnd=ht.clone().addScaledVector(st,ft),G.doorStart=nt.clone().addScaledVector(st,ft),G.doorEnd=J.clone().addScaledVector(st,ft),G.doorHeight=tt.doorHeight,G.beamStart.z=G.beamEnd.z=G.doorStart.z+G.doorHeight,G.doorOpenEndPoint=tt.doorOpenEndPoint.clone(),G.doorOpenVector=tt.doorOpenVector.clone()):!zt||zt&&G.isPullOutDoor&&kt?(tt.beamStart=G.beamStart.clone().addScaledVector(rt,ft),tt.beamEnd=G.beamEnd.clone().addScaledVector(rt,ft),tt.doorStart=G.doorStart.clone().addScaledVector(rt,ft),tt.doorEnd=G.doorEnd.clone().addScaledVector(rt,ft),tt.doorHeight=G.doorHeight,tt.beamStart.z=tt.beamEnd.z=tt.doorStart.z+tt.doorHeight,tt.doorOpenEndPoint=G.doorOpenEndPoint.clone(),tt.doorOpenVector=G.doorOpenVector.clone()):(G.beamStart=et.clone().addScaledVector(st,ft),G.beamEnd=ht.clone().addScaledVector(st,ft),G.doorStart=nt.clone().addScaledVector(st,ft),G.doorEnd=J.clone().addScaledVector(st,ft),G.doorHeight=tt.doorHeight,G.beamStart.z=G.beamEnd.z=G.doorStart.z+G.doorHeight,G.doorOpenEndPoint=tt.doorOpenEndPoint.clone(),G.doorOpenVector=tt.doorOpenVector.clone())):(ut>xt&&!Ut||se)&&(se&&(st=st.negate(),rt=rt.negate()),xt>Tt&&!zt?(tt.beamStart=G.beamStart.clone().addScaledVector(st,ft),tt.beamEnd=G.beamEnd.clone().addScaledVector(st,ft),tt.doorStart=G.doorStart.clone().addScaledVector(st,ft),tt.doorEnd=G.doorEnd.clone().addScaledVector(st,ft),tt.doorHeight=G.doorHeight,tt.beamStart.z=tt.beamEnd.z=tt.doorStart.z+tt.doorHeight,tt.doorOpenEndPoint=G.doorOpenEndPoint.clone(),tt.doorOpenVector=G.doorOpenVector.clone()):!G.isPullOutDoor||zt&&G.isPullOutDoor&&kt?(G.beamStart=et.clone().addScaledVector(rt,ft),G.beamEnd=ht.clone().addScaledVector(rt,ft),G.doorStart=nt.clone().addScaledVector(rt,ft),G.doorEnd=J.clone().addScaledVector(rt,ft),G.doorHeight=tt.doorHeight,G.beamStart.z=G.beamEnd.z=G.doorStart.z+G.doorHeight,G.doorOpenEndPoint=tt.doorOpenEndPoint.clone(),G.doorOpenVector=tt.doorOpenVector.clone()):(tt.beamStart=G.beamStart.clone().addScaledVector(st,ft),tt.beamEnd=G.beamEnd.clone().addScaledVector(st,ft),tt.doorStart=G.doorStart.clone().addScaledVector(st,ft),tt.doorEnd=G.doorEnd.clone().addScaledVector(st,ft),tt.doorHeight=G.doorHeight,tt.beamStart.z=tt.beamEnd.z=tt.doorStart.z+tt.doorHeight,tt.doorOpenEndPoint=G.doorOpenEndPoint.clone(),tt.doorOpenVector=G.doorOpenVector.clone())),fe=tt.nearId,pn=G.beamStart.clone(),gn=G.beamEnd.clone(),Je=!0,tn=a[it].linesIndex,xn=a[it].doorIndex;break}}}if(tt.nearId!=-1&&Dt=="hole"){let it=!1;for(let G=0;G<t.length;G++)if(t[G].doorAndBeamData){for(let dt=0;dt<t[G].doorAndBeamData.length;dt++)if(t[G].doorAndBeamData[dt].nearId==i&&t[G].doorAndBeamData[dt].type=="beam"){t[G].doorAndBeamData[dt].type=Dt,it=!0;break}if(it)break}}if(p.doorAndBeamIdToCheckResults.push([v.index,i]),p.doorAndBeamData.push(tt),a.push({linesIndex:m,doorIndex:p.doorAndBeamData.length-1}),d.push({start:nt,end:J}),i++,zt&&U<2)break}}}let W=p.length*(p.rooftopPz-p.start.z);p.completePointAreaPercentage=I/W*100,p.checkResults=null,p.originalPoints=null,p.mergeCheckRegion=null}if(u.length>1)for(let m=0;m<u.length;m++){let p=[];for(let b=0;b<t[u[m]].doorAndBeamData.length;b++){let w=!1;if(t[u[m]].doorAndBeamData[b].type==="beam"&&(t[u[m]].doorAndBeamData[b].beamNearWallVec.x==0&&t[u[m]].doorAndBeamData[b].beamNearWallVec.y==0&&t[u[m]].doorAndBeamData[b].beamNearWallVec.z==0&&t[u[m]].doorAndBeamData[b].nearId==-1||!t[u[m]].doorAndBeamData[b].wallBelowBeam&&t[u[m]].doorAndBeamData[b].nearId==-1)&&(t[u[m]].doorAndBeamData[b].lineIsOnlyBeam?w=!0:t[u[m]].doorAndBeamData[b].type="hole"),!w){let z=t[u[m]].doorAndBeamData[b].doorStart.clone(),M=t[u[m]].doorAndBeamData[b].doorEnd.clone();z.z=M.z=0;let P=z.distanceTo(M);for(let E=0;E<u.length;E++)if(m!=E)for(let S=0;S<t[u[E]].doorAndBeamData.length;S++){let I=t[u[E]].doorAndBeamData[S].doorStart.clone(),W=t[u[E]].doorAndBeamData[S].doorEnd.clone();I.z=W.z=0;let v=I.distanceTo(W),T=we(z,M,I,W);if(T.type=="collinear_overlap"&&T.maxPerpendicularDistance<.2){let R=new N.Line3(I,W),D=At(z,M),H=R.closestPointToPoint(D,!0,new N.Vector3),V=D.distanceTo(H);if(Math.abs(P-T.overlap)<.1&&V<.04){t[u[E]].doorAndBeamData[S].nearId==t[u[m]].doorAndBeamData[b].id&&(t[u[E]].doorAndBeamData[S].nearId=-1),w=!0;break}}}}if(w){for(let z=0;z<t[u[m]].doorAndBeamIdToCheckResults.length;z++)if(t[u[m]].doorAndBeamIdToCheckResults[1]==t[u[m]].doorAndBeamData[b].id){t[u[m]].doorAndBeamIdToCheckResults.splice(z,1);break}}w||p.push(t[u[m]].doorAndBeamData[b])}p.length!=t[u[m]].doorAndBeamData.length&&(t[u[m]].doorAndBeamData=p)}let l=.65,h=2,g=[],x=[];for(let m=0;m<s.length;m++){if(s[m].isFind||s[m].inWall)continue;let p=new N.Vector3(s[m].doorStartPt.x,s[m].doorStartPt.y,0),b=new N.Vector3(s[m].doorEndPt.x,s[m].doorEndPt.y,0),w=new N.Vector3().subVectors(p,b).normalize();for(let z=0;z<t.length;z++){if(t[z].length<.7||t[z].completePointAreaPercentage>60)continue;let M=new N.Vector3(t[z].start.x,t[z].start.y,0),P=new N.Vector3(t[z].end.x,t[z].end.y,0),E=new N.Vector3().subVectors(M,P).normalize(),S=.3,I=p.clone().addScaledVector(w,S),W=b.clone().addScaledVector(w.clone().negate(),S),v=M.clone().addScaledVector(E,S),T=P.clone().addScaledVector(E.clone().negate(),S),R=Pe(I,W,v,T),D=Zt(I,W,v,T),H=p.distanceTo(M)>b.distanceTo(M)?b:p,V=H.distanceTo(M),C=H.distanceTo(P);if((V<c||C<c)&&D&&!D.isParallel){let A=p.distanceTo(M),L=p.distanceTo(P),k=A<L?M:P,F=!1;for(let B=0;B<t.length;B++){if(B==z||g.includes(B)||t[B].length<.7)continue;let O=new N.Vector3(t[B].start.x,t[B].start.y,0),$=new N.Vector3(t[B].end.x,t[B].end.y,0),Z=new N.Vector3().subVectors(O,$).normalize(),Q=Z.clone().negate(),j=O.clone().addScaledVector(Z,5),X=$.clone().addScaledVector(Q,5),U=ie(M,P,O,$),at=we(M,P,O,$),mt=Pe(M,P,j,X),St=!0;if(mt!=null&&(St=mt.point.distanceTo(M)<c||mt.point.distanceTo(P)<c),(U&&U.angle>85||at&&at.type=="collinear_gap")&&St){let ot=new N.Line3(M,P),Mt=new N.Vector3,bt=ot.closestPointToPoint(O,!0,Mt),Ct=ot.closestPointToPoint($,!0,Mt),Et=bt.distanceTo(O),zt=Ct.distanceTo($),Pt,et;if(Et>zt&&zt>l)Pt=$,et=Ct;else if(Et<zt&&Et>l)Pt=O,et=bt;else continue;if(Et=k.distanceTo(O),zt=k.distanceTo($),Et>h&&zt>h)continue;let ht=Zt(Pt,et,O,$),nt=Zt(Pt,et,I,W);if(nt&&nt.isParallel||!ht||!ht.isParallel)continue;let J=we(p,b,Pt,et);if(J&&J.type=="collinear_overlap"&&J.maxPerpendicularDistance<.2)continue;Pt.z=et.z=t[B].start.z;let le=!1;for(let Dt=0;Dt<x.length;Dt++){let ne=we(x[Dt].start,x[Dt].end,Pt,et);if(ne&&ne.type=="collinear_overlap"&&ne.maxPerpendicularDistance<.2){le=!0;break}}if(le)continue;for(let Dt=0;Dt<t.length;Dt++){if(t[Dt].length<.5)continue;let ne=new N.Vector3(t[Dt].start.x,t[Dt].start.y,t[Dt].start.z),xe=new N.Vector3(t[Dt].end.x,t[Dt].end.y,t[Dt].end.z),tt=we(ne,xe,Pt,et);if(tt&&tt.type=="collinear_overlap"&&tt.maxPerpendicularDistance<.1){le=!0;break}}if(le)continue;x.push({start:Pt,end:et}),g.push(B);let wt=t[B].rooftopPz-t[B].start.z,te=t[z].rooftopPz-t[z].start.z,ge=wt<te?wt:te;t[B].doorAndBeamData||(t[B].doorAndBeamData=[]),t[B].doorAndBeamData.push({id:i,beamStart:Pt,beamEnd:et,beamHeight:ge,doorStart:Pt,doorEnd:et,doorHeight:ge,nearId:-1,type:"onlyDoor",isDoor:!0,beamNearWallVec:new N.Vector3(0,0,0),groundClearance:0}),n[s[m].index].isFindBeam=!0,n[s[m].index].isFindOnlyDoor=!0,f.push([{id:i,beamStart:Pt,beamEnd:et}]),i++,F=!0;break}}if(F)break}}}let y=[];for(let m=0;m<t.length;m++)t[m].isLine2||y.push(t[m]),t[m].isLine2&&t[m].doorAndBeamData.length!=0&&y.push(t[m]);return t.length=0,t.push(...y),{lines:t,beamGroup:f,pcdData:n}},Xi=async(t,e)=>{for(let n=0;n<t.length;n++){let o=t[n],r=new N.Vector3(o.start.x,o.start.y,o.start.z),s=new N.Vector3(o.end.x,o.end.y,o.end.z),i=new N.Line3(r.clone(),s.clone()),a=-1;for(let u=0;u<o.doorAndBeamData.length;u++){let d=o.doorAndBeamData[u],f=d.doorHeight;d.isRebuild=!1;let c=!1,l=!1;if(!(d.nearId!=-1||d.type!="beam")){for(let h=0;h<t.length;h++){if(t[h].length<.5||h==n)continue;let g=new N.Vector3(t[h].start.x,t[h].start.y,t[h].start.z),x=new N.Vector3(t[h].end.x,t[h].end.y,t[h].end.z),y=we(g,x,r,s),m=Zt(g,x,r,s);if(y&&y.maxPerpendicularDistance<.5){if(m&&m.isParallel){let E=new N.Line3(g,x),S=new N.Vector3(r.x,r.y,g.z),I=E.closestPointToPoint(S,!0,new N.Vector3),W=ie(I,S,r,s);W&&W.angle>85&&(d.beamNearWallVec=new N.Vector3().subVectors(I,S).normalize())}let p=[],b=e.readLineById(t[h].id);b.originalPoints=e.getOriginalPointsById(t[h].id),t[h].originalPoints=b.originalPoints,b=null,t[h].originalPoints.forEach(E=>{E.z>r.z+.2&&E.z<r.z+f/2&&p.push(new N.Vector3(E.x,E.y,r.z))}),t[h].originalPoints=null;let w=new N.Vector3,z=[],M=[];for(let E=0;E<p.length;E++){let S=i.closestPointToPoint(p[E],!0,w);S.distanceTo(p[E])<.15&&(z.push(p[E]),M.push(S.clone()))}for(let E=0;E<M.length;E++)zo(i,M[E])&&z.push(M[E]);if(z=M,z=Mo(z),z.length<150)continue;let P=We(z);if(P.length==2){let E=P[0].distanceTo(P[1]);if(Math.abs(E-i.distance())<.3){a=y.maxPerpendicularDistance;let S=new N.Line3(g,x),I=new N.Vector3(r.x,r.y,g.z),W=S.closestPointToPoint(I,!0,new N.Vector3);d.beamNearWallVec=new N.Vector3().subVectors(W,I).normalize(),d.beamStart.z>t[h].rooftopPz-.03&&length>1?l=!0:c=!0;break}}if(c)break}else y&&y.type=="collinear_overlap"&&y.maxPerpendicularDistance>.3&&y.maxPerpendicularDistance<.7&&(l=!0)}if(c){if(length<1.8||a<.2)continue;l=!0}l||(d.isRebuild=!0)}}}};import*as Bt from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";var Pr=(t,e={})=>{let n=e.eps??1e-6,o=e.project??(m=>({x:m.x,y:m.z})),r=e.skipSplit??!1,s=e.insideAreaSign??"positive",i=r?t.map((m,p)=>({start:Nn(m.start),end:Nn(m.end),uuid:m.uuid,sourceIndex:p,sourceSegment:m})):Zi(t,o,n),a=[],u=new Map;function d(m){let p=Math.round(m.x/n),b=Math.round(m.y/n);return`${p},${b}`}function f(m){let p=d(m);if(u.has(p))return u.get(p);let b=a.length;return a.push({id:b,x:m.x,y:m.y}),u.set(p,b),b}let c=[],l=new Map;for(let m of i){let p=o(m.start),b=o(m.end);if(Tr(p,b)<n*n)continue;let w=f(p),z=f(b);if(w===z)continue;let M=w<z?`${w}_${z}`:`${z}_${w}`;if(l.has(M))continue;let P=c.length;c.push({id:P,a:w,b:z,uuid:m.uuid,sourceIndex:m.sourceIndex,sourceSegment:m.sourceSegment||m}),l.set(M,P)}let h=[],g=Array.from({length:a.length},()=>[]);function x(m,p,b,w,z){let M=a[m],P=a[p],E=h.length,S=Math.atan2(P.y-M.y,P.x-M.x),I={id:E,from:m,to:p,twin:null,next:null,angle:S,edgeId:b,uuid:w,sourceIndex:z,visited:!1};return h.push(I),g[m].push(E),E}for(let m of c){let p=x(m.a,m.b,m.id,m.uuid,m.sourceIndex),b=x(m.b,m.a,m.id,m.uuid,m.sourceIndex);h[p].twin=b,h[b].twin=p}for(let m of g)m.sort((p,b)=>h[p].angle-h[b].angle);for(let m of h){let p=g[m.to],b=m.twin,w=p.indexOf(b);if(w===-1)continue;let z=(w-1+p.length)%p.length;m.next=p[z]}let y=[];for(let m of h){if(m.visited)continue;let p=[],b=[],w=m.id,z=0,M=h.length+10;for(;;){let v=h[w];if(!v||v.visited)break;if(v.visited=!0,p.push(w),b.push(v.from),w=v.next,z++,z>M){console.warn("\u534A\u8FB9\u904D\u5386\u5F02\u5E38\uFF0C\u53EF\u80FD\u5B58\u5728\u975E\u5E73\u9762\u56FE\u3001\u91CD\u590D\u7EBF\u3001\u60AC\u6302\u7EBF\u6216\u810F\u6570\u636E");break}}if(p.length<3)continue;let P=b.map(v=>a[v]),E=Yi(P),S=!1;if(s==="positive"?S=E>n:S=E<-n,!S)continue;let I=p.map(v=>h[v].uuid),W=p.map(v=>h[v].sourceIndex);y.push({uuids:I,area:Math.abs(E),lineIndexes:W})}return y};function Zi(t,e,n=1e-6){let o=[];for(let r=0;r<t.length;r++){let s=t[r],i=e(s.start),a=e(s.end),u=[{t:0,point:Nn(s.start)},{t:1,point:Nn(s.end)}];for(let f=0;f<t.length;f++){if(r===f)continue;let c=t[f],l=e(c.start),h=e(c.end),g=Gi(i,a,l,h,n);g&&g.t>n&&g.t<1-n&&u.push({t:g.t,point:Ui(s.start,s.end,g.t)})}u.sort((f,c)=>f.t-c.t);let d=[];for(let f of u)(d.length===0||Math.abs(f.t-d[d.length-1].t)>n)&&d.push(f);for(let f=0;f<d.length-1;f++){let c=d[f].point,l=d[f+1].point,h=e(c),g=e(l);Tr(h,g)<n*n||o.push({start:c,end:l,uuid:s.uuid,sourceIndex:r,sourceSegment:s})}}return o}function Gi(t,e,n,o,r=1e-6){let s={x:e.x-t.x,y:e.y-t.y},i={x:o.x-n.x,y:o.y-n.y},a={x:n.x-t.x,y:n.y-t.y},u=Po(s,i);if(Math.abs(u)<r)return null;let d=Po(a,i)/u,f=Po(a,s)/u;return d>=-r&&d<=1+r&&f>=-r&&f<=1+r?{t:d,u:f,point:{x:t.x+d*s.x,y:t.y+d*s.y}}:null}function Po(t,e){return t.x*e.y-t.y*e.x}function Tr(t,e){let n=t.x-e.x,o=t.y-e.y;return n*n+o*o}function Yi(t){let e=0;for(let n=0;n<t.length;n++){let o=t[n],r=t[(n+1)%t.length];e+=o.x*r.y-r.x*o.y}return e/2}function Ui(t,e,n){return t&&typeof t.clone=="function"&&typeof t.lerp=="function"?t.clone().lerp(e,n):{x:t.x+(e.x-t.x)*n,y:t.y+(e.y-t.y)*n,z:t.z!==void 0&&e.z!==void 0?t.z+(e.z-t.z)*n:void 0}}function Nn(t){return t&&typeof t.clone=="function"?t.clone():{x:t.x,y:t.y,z:t.z}}var Ki=(t,e)=>{if(!t||t.length<=0)return;let n={rectangles:[],threeEdgeRect:[]},o=new Set,r=.1,s=1.2,i=.15,a=2,u=80,d=95;if(!t[0].uuid){let l=1e4;for(let h=0;h<t.length;h++)t[h].uuid=l++}let f=[];for(let l=0;l<t.length;l++)f.push({start:t[l].start,end:t[l].end,uuid:t[l].uuid});let c=Pr(f,{eps:1e-5,returnDetail:!0,project:l=>({x:l.x,y:l.y})});for(let l=0;l<c.length;l++){if(c[l].uuids.length<4||c[l].area<i||c[l].area>a)continue;let h=!1;for(let g=0;g<c[l].lineIndexes.length;g++){let x=t[c[l].lineIndexes[g]].length;if(x<r||x>s)break;let y=new Bt.Vector3(t[c[l].lineIndexes[g]].start.x,t[c[l].lineIndexes[g]].start.y,t[c[l].lineIndexes[g]].start.z),m=new Bt.Vector3(t[c[l].lineIndexes[g]].end.x,t[c[l].lineIndexes[g]].end.y,t[c[l].lineIndexes[g]].end.z),p=!1,b=new Bt.Vector3(0,0,0);for(let w=g+1;w<c[l].lineIndexes.length;w++){let z=t[c[l].lineIndexes[w]].length;if(z<r||z>s)break;let M=new Bt.Vector3(t[c[l].lineIndexes[w]].start.x,t[c[l].lineIndexes[w]].start.y,t[c[l].lineIndexes[w]].start.z),P=new Bt.Vector3(t[c[l].lineIndexes[w]].end.x,t[c[l].lineIndexes[w]].end.y,t[c[l].lineIndexes[w]].end.z),E=Zt(y,m,M,P);if(E&&E.isParallel){if(x+z>s){p=!0;break}x+=z;let I=ct(y,M),W=ct(y,P),v=ct(m,M),T=ct(m,P);I?(y=m.clone(),m=P.clone()):W?(y=m.clone(),m=M.clone()):v?m=P.clone():T&&(m=M.clone());continue}let S=ie(y,m,M,P);if(!S||S.angle<u||S.angle>d){p=!0;break}for(let I=w+1;I<c[l].lineIndexes.length;I++){let W=t[c[l].lineIndexes[I]].length;if(W<r||W>s)break;let v=new Bt.Vector3(t[c[l].lineIndexes[I]].start.x,t[c[l].lineIndexes[I]].start.y,t[c[l].lineIndexes[I]].start.z),T=new Bt.Vector3(t[c[l].lineIndexes[I]].end.x,t[c[l].lineIndexes[I]].end.y,t[c[l].lineIndexes[I]].end.z),R=Zt(M,P,v,T);if(R&&R.isParallel){if(z+W>s){p=!0;break}z+=W;let H=ct(M,v),V=ct(M,T),C=ct(P,v),_=ct(P,T);H?(M=P.clone(),P=T.clone()):V?(M=P.clone(),P=v.clone()):C?P=T.clone():_&&(P=v.clone());continue}let D=ie(M,P,v,T);if(!D||D.angle<u||D.angle>d){p=!0;break}for(let H=I+1;H<c[l].lineIndexes.length;H++){let V=t[c[l].lineIndexes[H]].length;if(V<r||V>s)break;let C=new Bt.Vector3(t[c[l].lineIndexes[H]].start.x,t[c[l].lineIndexes[H]].start.y,t[c[l].lineIndexes[H]].start.z),_=new Bt.Vector3(t[c[l].lineIndexes[H]].end.x,t[c[l].lineIndexes[H]].end.y,t[c[l].lineIndexes[H]].end.z),A=Zt(v,T,C,_);if(A&&A.isParallel){if(W+V>s){p=!0;break}W+=V;let $=ct(v,C),Z=ct(v,_),Q=ct(T,C),j=ct(T,_);$?(v=T.clone(),T=_.clone()):Z?(v=T.clone(),T=C.clone()):Q?T=_.clone():j&&(T=C.clone());continue}let L=ie(v,T,C,_);if(!L||L.angle<u||L.angle>d){p=!0;break}let k=ct(C,y),F=ct(C,m),B=ct(_,y),O=ct(_,m);if(k||F||B||O)h=!0;else for(let $=H+1;$<c[l].lineIndexes.length;$++){let Z=t[c[l].lineIndexes[$]].length;if(Z<r||Z>s)break;let Q=new Bt.Vector3(t[c[l].lineIndexes[$]].start.x,t[c[l].lineIndexes[$]].start.y,t[c[l].lineIndexes[$]].start.z),j=new Bt.Vector3(t[c[l].lineIndexes[$]].end.x,t[c[l].lineIndexes[$]].end.y,t[c[l].lineIndexes[$]].end.z),X=Zt(C,_,Q,j);if(X&&X.isParallel){if(V+Z>s){p=!0;break}V+=Z;let U=ct(C,Q),at=ct(C,j),mt=ct(_,Q),St=ct(_,j);if(U)C=_.clone(),_=j.clone();else if(at)C=_.clone(),_=Q.clone();else if(mt)_=j.clone();else if(St)_=Q.clone();else continue;if(k=ct(C,y),F=ct(C,m),B=ct(_,y),O=ct(_,m),k||F||B||O){h=!0;break}}}break}if(p)break}if(p)break}if(p)break}h&&n.rectangles.push(c[l].uuids)}for(let l=0;l<t.length;l++){let h=t[l].length;if(o.has(t[l].uuid)||h<r||h>s)continue;let g={uuids:[],missingEdge:{start:new Bt.Vector3,end:new Bt.Vector3}},x=new Bt.Vector3(t[l].start.x,t[l].start.y,t[l].start.z),y=new Bt.Vector3(t[l].end.x,t[l].end.y,t[l].end.z),m=!0,p=!0;for(let z=0;z<t.length;z++){if(z==l)continue;let M=new Bt.Vector3(t[z].start.x,t[z].start.y,t[z].start.z),P=new Bt.Vector3(t[z].end.x,t[z].end.y,t[z].end.z),E=ct(M,x),S=ct(P,x),I=ct(M,y),W=ct(P,y);if(E||S?m=!1:(I||W)&&(p=!1),!m&&!p)break}if(!m&&!p)continue;let b=new Bt.Vector3;m||([x,y]=[y,x]),b=new Bt.Vector3().subVectors(y,x).normalize();let w=b.clone().negate();for(let z=0;z<t.length;z++){let M=t[z].length;if(z==l||o.has(z)||M<r||M>s)continue;let P=new Bt.Vector3(t[z].start.x,t[z].start.y,t[z].start.z),E=new Bt.Vector3(t[z].end.x,t[z].end.y,t[z].end.z),S=ie(x,y,P,E);if(!S||S.angle<85)continue;let I=ct(P,y),W=ct(E,y);if(!I&&!W)continue;let v=new Bt.Vector3,T=new Bt.Vector3;I?(v=P.clone(),T=E.clone()):(v=E.clone(),T=P.clone());for(let R=0;R<t.length;R++){let D=t[R].length;if(R==l||R==z||o.has(R)||D<r||D>s)continue;let H=new Bt.Vector3(t[R].start.x,t[R].start.y,t[R].start.z),V=new Bt.Vector3(t[R].end.x,t[R].end.y,t[R].end.z),C=ie(P,E,H,V);if(!C||C.angle<85)continue;let _=ct(H,T),A=ct(V,T);if(!_&&!A)continue;let L=new Bt.Vector3,k=new Bt.Vector3;_?(L=H.clone(),k=V.clone()):(L=V.clone(),k=H.clone());let F=new Bt.Vector3().subVectors(k,L).normalize(),B=Xt(w,F,.1);if(!B||!B.parallel||!B.sameDirection)continue;let O=!0,$=!0;for(let Q=0;Q<t.length;Q++){if(Q==R)continue;let j=new Bt.Vector3(t[Q].start.x,t[Q].start.y,t[Q].start.z),X=new Bt.Vector3(t[Q].end.x,t[Q].end.y,t[Q].end.z),U=ct(j,H),at=ct(X,H),mt=ct(j,V),St=ct(X,V);if(U||at?O=!1:(mt||St)&&($=!1),!O&&!$)break}if(!O&&!$)continue;let Z=h*M;if(!(Z<i||Z>a)){g.uuids=[t[l].uuid,t[z].uuid,t[R].uuid],g.missingEdge.start=k,g.missingEdge.end=x;break}}if(g.uuids.length==3)break}g.uuids.length==3&&(g.uuids.forEach(z=>o.add(z)),n.threeEdgeRect.push(g))}return n};import*as q from"three";import"three/examples/jsm/utils/BufferGeometryUtils.js";var Ht=11102230246251565e-32,Kt=134217729,zn=(3+8*Ht)*Ht;function Fe(t,e,n,o,r){let s,i,a,u,d=e[0],f=o[0],c=0,l=0;f>d==f>-d?(s=d,d=e[++c]):(s=f,f=o[++l]);let h=0;if(c<t&&l<n)for(f>d==f>-d?(i=d+s,a=s-(i-d),d=e[++c]):(i=f+s,a=s-(i-f),f=o[++l]),s=i,a!==0&&(r[h++]=a);c<t&&l<n;)f>d==f>-d?(i=s+d,u=i-s,a=s-(i-u)+(d-u),d=e[++c]):(i=s+f,u=i-s,a=s-(i-u)+(f-u),f=o[++l]),s=i,a!==0&&(r[h++]=a);for(;c<t;)i=s+d,u=i-s,a=s-(i-u)+(d-u),d=e[++c],s=i,a!==0&&(r[h++]=a);for(;l<n;)i=s+f,u=i-s,a=s-(i-u)+(f-u),f=o[++l],s=i,a!==0&&(r[h++]=a);return(s!==0||h===0)&&(r[h++]=s),h}function Mn(t,e){let n=e[0];for(let o=1;o<t;o++)n+=e[o];return n}function K(t){return new Float64Array(t)}var Qi=(3+16*Ht)*Ht,Ji=(2+12*Ht)*Ht,tl=(9+64*Ht)*Ht*Ht,nn=K(4),Sr=K(8),Rr=K(12),Dr=K(16),re=K(4);function el(t,e,n,o,r,s,i){let a,u,d,f,c,l,h,g,x,y,m,p,b,w,z,M,P,E,S=t-r,I=n-r,W=e-s,v=o-s;w=S*v,l=Kt*S,h=l-(l-S),g=S-h,l=Kt*v,x=l-(l-v),y=v-x,z=g*y-(w-h*x-g*x-h*y),M=W*I,l=Kt*W,h=l-(l-W),g=W-h,l=Kt*I,x=l-(l-I),y=I-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,nn[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,nn[1]=b-(m+c)+(c-M),E=p+m,c=E-p,nn[2]=p-(E-c)+(m-c),nn[3]=E;let T=Mn(4,nn),R=Ji*i;if(T>=R||-T>=R||(c=t-S,a=t-(S+c)+(c-r),c=n-I,d=n-(I+c)+(c-r),c=e-W,u=e-(W+c)+(c-s),c=o-v,f=o-(v+c)+(c-s),a===0&&u===0&&d===0&&f===0)||(R=tl*i+zn*Math.abs(T),T+=S*f+v*a-(W*d+I*u),T>=R||-T>=R))return T;w=a*v,l=Kt*a,h=l-(l-a),g=a-h,l=Kt*v,x=l-(l-v),y=v-x,z=g*y-(w-h*x-g*x-h*y),M=u*I,l=Kt*u,h=l-(l-u),g=u-h,l=Kt*I,x=l-(l-I),y=I-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,re[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,re[1]=b-(m+c)+(c-M),E=p+m,c=E-p,re[2]=p-(E-c)+(m-c),re[3]=E;let D=Fe(4,nn,4,re,Sr);w=S*f,l=Kt*S,h=l-(l-S),g=S-h,l=Kt*f,x=l-(l-f),y=f-x,z=g*y-(w-h*x-g*x-h*y),M=W*d,l=Kt*W,h=l-(l-W),g=W-h,l=Kt*d,x=l-(l-d),y=d-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,re[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,re[1]=b-(m+c)+(c-M),E=p+m,c=E-p,re[2]=p-(E-c)+(m-c),re[3]=E;let H=Fe(D,Sr,4,re,Rr);w=a*f,l=Kt*a,h=l-(l-a),g=a-h,l=Kt*f,x=l-(l-f),y=f-x,z=g*y-(w-h*x-g*x-h*y),M=u*d,l=Kt*u,h=l-(l-u),g=u-h,l=Kt*d,x=l-(l-d),y=d-x,P=g*y-(M-h*x-g*x-h*y),m=z-P,c=z-m,re[0]=z-(m+c)+(c-P),p=w+m,c=p-w,b=w-(p-c)+(m-c),m=b-M,c=b-m,re[1]=b-(m+c)+(c-M),E=p+m,c=E-p,re[2]=p-(E-c)+(m-c),re[3]=E;let V=Fe(H,Rr,4,re,Dr);return Dr[V-1]}function on(t,e,n,o,r,s){let i=(e-s)*(n-r),a=(t-r)*(o-s),u=i-a,d=Math.abs(i+a);return Math.abs(u)>=Qi*d?u:-el(t,e,n,o,r,s,d)}var Tu=(7+56*Ht)*Ht,Su=(3+28*Ht)*Ht,Ru=(26+288*Ht)*Ht*Ht,Du=K(4),vu=K(4),Vu=K(4),Iu=K(4),Cu=K(4),Hu=K(4),Wu=K(4),_u=K(4),Au=K(4),Bu=K(8),Lu=K(8),ku=K(8),Ou=K(4),Fu=K(8),Nu=K(8),qu=K(16),ju=K(12),$u=K(192),Xu=K(192);var Yu=(10+96*Ht)*Ht,Uu=(4+48*Ht)*Ht,Ku=(44+576*Ht)*Ht*Ht,Qu=K(4),Ju=K(4),tf=K(4),ef=K(4),nf=K(4),of=K(4),rf=K(4),sf=K(4),lf=K(8),af=K(8),cf=K(8),uf=K(8),ff=K(8),df=K(8),hf=K(8),mf=K(8),pf=K(8),gf=K(4),xf=K(4),yf=K(4),bf=K(8),wf=K(16),zf=K(16),Mf=K(16),Ef=K(32),Pf=K(32),Tf=K(48),Sf=K(64),Rf=K(1152),Df=K(1152);var Cf=(16+224*Ht)*Ht,Hf=(5+72*Ht)*Ht,Wf=(71+1408*Ht)*Ht*Ht,_f=K(4),Af=K(4),Bf=K(4),Lf=K(4),kf=K(4),Of=K(4),Ff=K(4),Nf=K(4),qf=K(4),jf=K(4),$f=K(24),Xf=K(24),Zf=K(24),Gf=K(24),Yf=K(24),Uf=K(24),Kf=K(24),Qf=K(24),Jf=K(24),td=K(24),ed=K(1152),nd=K(1152),od=K(1152),rd=K(1152),sd=K(1152),id=K(2304),ld=K(2304),ad=K(3456),cd=K(5760),ud=K(8),fd=K(8),dd=K(8),hd=K(16),md=K(24),pd=K(48),gd=K(48),xd=K(96),yd=K(192),bd=K(384),wd=K(384),zd=K(384),Md=K(768);var Ed=K(96),Pd=K(96),Td=K(96),Sd=K(1152);var Vr=Math.pow(2,-52),qn=new Uint32Array(512),sn=class t{static from(e,n=ll,o=al){let r=e.length,s=new Float64Array(r*2);for(let i=0;i<r;i++){let a=e[i];s[2*i]=n(a),s[2*i+1]=o(a)}return new t(s)}constructor(e){let n=e.length>>1;if(n>0&&typeof e[0]!="number")throw new Error("Expected coords to contain numbers.");this.coords=e;let o=Math.max(2*n-5,0);this._triangles=new Uint32Array(o*3),this._halfedges=new Int32Array(o*3),this._hashSize=Math.ceil(Math.sqrt(n)),this._hullPrev=new Uint32Array(n),this._hullNext=new Uint32Array(n),this._hullTri=new Uint32Array(n),this._hullHash=new Int32Array(this._hashSize),this._ids=new Uint32Array(n),this._dists=new Float64Array(n),this.trianglesLen=0,this._cx=0,this._cy=0,this._hullStart=0,this.hull=this._triangles,this.triangles=this._triangles,this.halfedges=this._halfedges,this.update()}update(){let{coords:e,_hullPrev:n,_hullNext:o,_hullTri:r,_hullHash:s}=this,i=e.length>>1,a=1/0,u=1/0,d=-1/0,f=-1/0;for(let S=0;S<i;S++){let I=e[2*S],W=e[2*S+1];I<a&&(a=I),W<u&&(u=W),I>d&&(d=I),W>f&&(f=W),this._ids[S]=S}let c=(a+d)/2,l=(u+f)/2,h=0,g=0,x=0;for(let S=0,I=1/0;S<i;S++){let W=To(c,l,e[2*S],e[2*S+1]);W<I&&(h=S,I=W)}let y=e[2*h],m=e[2*h+1];for(let S=0,I=1/0;S<i;S++){if(S===h)continue;let W=To(y,m,e[2*S],e[2*S+1]);W<I&&W>0&&(g=S,I=W)}let p=e[2*g],b=e[2*g+1],w=1/0;for(let S=0;S<i;S++){if(S===h||S===g)continue;let I=sl(y,m,p,b,e[2*S],e[2*S+1]);I<w&&(x=S,w=I)}let z=e[2*x],M=e[2*x+1];if(w===1/0){for(let W=0;W<i;W++)this._dists[W]=e[2*W]-e[0]||e[2*W+1]-e[1];rn(this._ids,this._dists,0,i-1);let S=new Uint32Array(i),I=0;for(let W=0,v=-1/0;W<i;W++){let T=this._ids[W],R=this._dists[T];R>v&&(S[I++]=T,v=R)}this.hull=S.subarray(0,I),this.triangles=new Uint32Array(0),this.halfedges=new Int32Array(0);return}if(on(y,m,p,b,z,M)<0){let S=g,I=p,W=b;g=x,p=z,b=M,x=S,z=I,M=W}let P=il(y,m,p,b,z,M);this._cx=P.x,this._cy=P.y;for(let S=0;S<i;S++)this._dists[S]=To(e[2*S],e[2*S+1],P.x,P.y);rn(this._ids,this._dists,0,i-1),this._hullStart=h;let E=3;o[h]=n[x]=g,o[g]=n[h]=x,o[x]=n[g]=h,r[h]=0,r[g]=1,r[x]=2,s.fill(-1),s[this._hashKey(y,m)]=h,s[this._hashKey(p,b)]=g,s[this._hashKey(z,M)]=x,this.trianglesLen=0,this._addTriangle(h,g,x,-1,-1,-1);for(let S=0,I=0,W=0;S<this._ids.length;S++){let v=this._ids[S],T=e[2*v],R=e[2*v+1];if(S>0&&Math.abs(T-I)<=Vr&&Math.abs(R-W)<=Vr||(I=T,W=R,v===h||v===g||v===x))continue;let D=0;for(let A=0,L=this._hashKey(T,R);A<this._hashSize&&(D=s[(L+A)%this._hashSize],!(D!==-1&&D!==o[D]));A++);D=n[D];let H=D,V;for(;V=o[H],on(T,R,e[2*H],e[2*H+1],e[2*V],e[2*V+1])>=0;)if(H=V,H===D){H=-1;break}if(H===-1)continue;let C=this._addTriangle(H,v,o[H],-1,-1,r[H]);r[v]=this._legalize(C+2),r[H]=C,E++;let _=o[H];for(;V=o[_],on(T,R,e[2*_],e[2*_+1],e[2*V],e[2*V+1])<0;)C=this._addTriangle(_,v,V,r[v],-1,r[_]),r[v]=this._legalize(C+2),o[_]=_,E--,_=V;if(H===D)for(;V=n[H],on(T,R,e[2*V],e[2*V+1],e[2*H],e[2*H+1])<0;)C=this._addTriangle(V,v,H,-1,r[H],r[V]),this._legalize(C+2),r[V]=C,o[H]=H,E--,H=V;this._hullStart=n[v]=H,o[H]=n[_]=v,o[v]=_,s[this._hashKey(T,R)]=v,s[this._hashKey(e[2*H],e[2*H+1])]=H}this.hull=new Uint32Array(E);for(let S=0,I=this._hullStart;S<E;S++)this.hull[S]=I,I=o[I];this.triangles=this._triangles.subarray(0,this.trianglesLen),this.halfedges=this._halfedges.subarray(0,this.trianglesLen)}_hashKey(e,n){return Math.floor(ol(e-this._cx,n-this._cy)*this._hashSize)%this._hashSize}_legalize(e){let{_triangles:n,_halfedges:o,coords:r}=this,s=0,i=0;for(;;){let a=o[e],u=e-e%3;if(i=u+(e+2)%3,a===-1){if(s===0)break;e=qn[--s];continue}let d=a-a%3,f=u+(e+1)%3,c=d+(a+2)%3,l=n[i],h=n[e],g=n[f],x=n[c];if(rl(r[2*l],r[2*l+1],r[2*h],r[2*h+1],r[2*g],r[2*g+1],r[2*x],r[2*x+1])){n[e]=x,n[a]=l;let m=o[c];if(m===-1){let b=this._hullStart;do{if(this._hullTri[b]===c){this._hullTri[b]=e;break}b=this._hullPrev[b]}while(b!==this._hullStart)}this._link(e,m),this._link(a,o[i]),this._link(i,c);let p=d+(a+1)%3;s<qn.length&&(qn[s++]=p)}else{if(s===0)break;e=qn[--s]}}return i}_link(e,n){this._halfedges[e]=n,n!==-1&&(this._halfedges[n]=e)}_addTriangle(e,n,o,r,s,i){let a=this.trianglesLen;return this._triangles[a]=e,this._triangles[a+1]=n,this._triangles[a+2]=o,this._link(a,r),this._link(a+1,s),this._link(a+2,i),this.trianglesLen+=3,a}};function ol(t,e){let n=t/(Math.abs(t)+Math.abs(e));return(e>0?3-n:1+n)/4}function To(t,e,n,o){let r=t-n,s=e-o;return r*r+s*s}function rl(t,e,n,o,r,s,i,a){let u=t-i,d=e-a,f=n-i,c=o-a,l=r-i,h=s-a,g=u*u+d*d,x=f*f+c*c,y=l*l+h*h;return u*(c*y-x*h)-d*(f*y-x*l)+g*(f*h-c*l)<0}function sl(t,e,n,o,r,s){let i=n-t,a=o-e,u=r-t,d=s-e,f=i*i+a*a,c=u*u+d*d,l=.5/(i*d-a*u),h=(d*f-a*c)*l,g=(i*c-u*f)*l;return h*h+g*g}function il(t,e,n,o,r,s){let i=n-t,a=o-e,u=r-t,d=s-e,f=i*i+a*a,c=u*u+d*d,l=.5/(i*d-a*u),h=t+(d*f-a*c)*l,g=e+(i*c-u*f)*l;return{x:h,y:g}}function rn(t,e,n,o){if(o-n<=20)for(let r=n+1;r<=o;r++){let s=t[r],i=e[s],a=r-1;for(;a>=n&&e[t[a]]>i;)t[a+1]=t[a--];t[a+1]=s}else{let r=n+o>>1,s=n+1,i=o;En(t,r,s),e[t[n]]>e[t[o]]&&En(t,n,o),e[t[s]]>e[t[o]]&&En(t,s,o),e[t[n]]>e[t[s]]&&En(t,n,s);let a=t[s],u=e[a];for(;;){do s++;while(e[t[s]]<u);do i--;while(e[t[i]]>u);if(i<s)break;En(t,s,i)}t[n+1]=t[i],t[i]=a,o-s+1>=i-n?(rn(t,e,s,o),rn(t,e,n,i-1)):(rn(t,e,n,i-1),rn(t,e,s,o))}}function En(t,e,n){let o=t[e];t[e]=t[n],t[n]=o}function ll(t){return t[0]}function al(t){return t[1]}var he=class{constructor(){this._x0=this._y0=this._x1=this._y1=null,this._=""}moveTo(e,n){this._+=`M${this._x0=this._x1=+e},${this._y0=this._y1=+n}`}closePath(){this._x1!==null&&(this._x1=this._x0,this._y1=this._y0,this._+="Z")}lineTo(e,n){this._+=`L${this._x1=+e},${this._y1=+n}`}arc(e,n,o){e=+e,n=+n,o=+o;let r=e+o,s=n;if(o<0)throw new Error("negative radius");this._x1===null?this._+=`M${r},${s}`:(Math.abs(this._x1-r)>1e-6||Math.abs(this._y1-s)>1e-6)&&(this._+="L"+r+","+s),o&&(this._+=`A${o},${o},0,1,1,${e-o},${n}A${o},${o},0,1,1,${this._x1=r},${this._y1=s}`)}rect(e,n,o,r){this._+=`M${this._x0=this._x1=+e},${this._y0=this._y1=+n}h${+o}v${+r}h${-o}Z`}value(){return this._||null}};var _e=class{constructor(){this._=[]}moveTo(e,n){this._.push([e,n])}closePath(){this._.push(this._[0].slice())}lineTo(e,n){this._.push([e,n])}value(){return this._.length?this._:null}};var Pn=class{constructor(e,[n,o,r,s]=[0,0,960,500]){if(!((r=+r)>=(n=+n))||!((s=+s)>=(o=+o)))throw new Error("invalid bounds");this.delaunay=e,this._circumcenters=new Float64Array(e.points.length*2),this.vectors=new Float64Array(e.points.length*2),this.xmax=r,this.xmin=n,this.ymax=s,this.ymin=o,this._init()}update(){return this.delaunay.update(),this._init(),this}_init(){let{delaunay:{points:e,hull:n,triangles:o},vectors:r}=this,s,i,a=this.circumcenters=this._circumcenters.subarray(0,o.length/3*2);for(let x=0,y=0,m=o.length,p,b;x<m;x+=3,y+=2){let w=o[x]*2,z=o[x+1]*2,M=o[x+2]*2,P=e[w],E=e[w+1],S=e[z],I=e[z+1],W=e[M],v=e[M+1],T=S-P,R=I-E,D=W-P,H=v-E,V=(T*H-R*D)*2;if(Math.abs(V)<1e-9){if(s===void 0){s=i=0;for(let _ of n)s+=e[_*2],i+=e[_*2+1];s/=n.length,i/=n.length}let C=1e9*Math.sign((s-P)*H-(i-E)*D);p=(P+W)/2-C*H,b=(E+v)/2+C*D}else{let C=1/V,_=T*T+R*R,A=D*D+H*H;p=P+(H*_-R*A)*C,b=E+(T*A-D*_)*C}a[y]=p,a[y+1]=b}let u=n[n.length-1],d,f=u*4,c,l=e[2*u],h,g=e[2*u+1];r.fill(0);for(let x=0;x<n.length;++x)u=n[x],d=f,c=l,h=g,f=u*4,l=e[2*u],g=e[2*u+1],r[d+2]=r[f]=h-g,r[d+3]=r[f+1]=l-c}render(e){let n=e==null?e=new he:void 0,{delaunay:{halfedges:o,inedges:r,hull:s},circumcenters:i,vectors:a}=this;if(s.length<=1)return null;for(let f=0,c=o.length;f<c;++f){let l=o[f];if(l<f)continue;let h=Math.floor(f/3)*2,g=Math.floor(l/3)*2,x=i[h],y=i[h+1],m=i[g],p=i[g+1];this._renderSegment(x,y,m,p,e)}let u,d=s[s.length-1];for(let f=0;f<s.length;++f){u=d,d=s[f];let c=Math.floor(r[d]/3)*2,l=i[c],h=i[c+1],g=u*4,x=this._project(l,h,a[g+2],a[g+3]);x&&this._renderSegment(l,h,x[0],x[1],e)}return n&&n.value()}renderBounds(e){let n=e==null?e=new he:void 0;return e.rect(this.xmin,this.ymin,this.xmax-this.xmin,this.ymax-this.ymin),n&&n.value()}renderCell(e,n){let o=n==null?n=new he:void 0,r=this._clip(e);if(r===null||!r.length)return;n.moveTo(r[0],r[1]);let s=r.length;for(;r[0]===r[s-2]&&r[1]===r[s-1]&&s>1;)s-=2;for(let i=2;i<s;i+=2)(r[i]!==r[i-2]||r[i+1]!==r[i-1])&&n.lineTo(r[i],r[i+1]);return n.closePath(),o&&o.value()}*cellPolygons(){let{delaunay:{points:e}}=this;for(let n=0,o=e.length/2;n<o;++n){let r=this.cellPolygon(n);r&&(r.index=n,yield r)}}cellPolygon(e){let n=new _e;return this.renderCell(e,n),n.value()}_renderSegment(e,n,o,r,s){let i,a=this._regioncode(e,n),u=this._regioncode(o,r);a===0&&u===0?(s.moveTo(e,n),s.lineTo(o,r)):(i=this._clipSegment(e,n,o,r,a,u))&&(s.moveTo(i[0],i[1]),s.lineTo(i[2],i[3]))}contains(e,n,o){return n=+n,n!==n||(o=+o,o!==o)?!1:this.delaunay._step(e,n,o)===e}*neighbors(e){let n=this._clip(e);if(n)for(let o of this.delaunay.neighbors(e)){let r=this._clip(o);if(r){t:for(let s=0,i=n.length;s<i;s+=2)for(let a=0,u=r.length;a<u;a+=2)if(n[s]===r[a]&&n[s+1]===r[a+1]&&n[(s+2)%i]===r[(a+u-2)%u]&&n[(s+3)%i]===r[(a+u-1)%u]){yield o;break t}}}}_cell(e){let{circumcenters:n,delaunay:{inedges:o,halfedges:r,triangles:s}}=this,i=o[e];if(i===-1)return null;let a=[],u=i;do{let d=Math.floor(u/3);if(a.push(n[d*2],n[d*2+1]),u=u%3===2?u-2:u+1,s[u]!==e)break;u=r[u]}while(u!==i&&u!==-1);return a}_clip(e){if(e===0&&this.delaunay.hull.length===1)return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];let n=this._cell(e);if(n===null)return null;let{vectors:o}=this,r=e*4;return this._simplify(o[r]||o[r+1]?this._clipInfinite(e,n,o[r],o[r+1],o[r+2],o[r+3]):this._clipFinite(e,n))}_clipFinite(e,n){let o=n.length,r=null,s,i,a=n[o-2],u=n[o-1],d,f=this._regioncode(a,u),c,l=0;for(let h=0;h<o;h+=2)if(s=a,i=u,a=n[h],u=n[h+1],d=f,f=this._regioncode(a,u),d===0&&f===0)c=l,l=0,r?r.push(a,u):r=[a,u];else{let g,x,y,m,p;if(d===0){if((g=this._clipSegment(s,i,a,u,d,f))===null)continue;[x,y,m,p]=g}else{if((g=this._clipSegment(a,u,s,i,f,d))===null)continue;[m,p,x,y]=g,c=l,l=this._edgecode(x,y),c&&l&&this._edge(e,c,l,r,r.length),r?r.push(x,y):r=[x,y]}c=l,l=this._edgecode(m,p),c&&l&&this._edge(e,c,l,r,r.length),r?r.push(m,p):r=[m,p]}if(r)c=l,l=this._edgecode(r[0],r[1]),c&&l&&this._edge(e,c,l,r,r.length);else if(this.contains(e,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2))return[this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax,this.xmin,this.ymin];return r}_clipSegment(e,n,o,r,s,i){let a=s<i;for(a&&([e,n,o,r,s,i]=[o,r,e,n,i,s]);;){if(s===0&&i===0)return a?[o,r,e,n]:[e,n,o,r];if(s&i)return null;let u,d,f=s||i;f&8?(u=e+(o-e)*(this.ymax-n)/(r-n),d=this.ymax):f&4?(u=e+(o-e)*(this.ymin-n)/(r-n),d=this.ymin):f&2?(d=n+(r-n)*(this.xmax-e)/(o-e),u=this.xmax):(d=n+(r-n)*(this.xmin-e)/(o-e),u=this.xmin),s?(e=u,n=d,s=this._regioncode(e,n)):(o=u,r=d,i=this._regioncode(o,r))}}_clipInfinite(e,n,o,r,s,i){let a=Array.from(n),u;if((u=this._project(a[0],a[1],o,r))&&a.unshift(u[0],u[1]),(u=this._project(a[a.length-2],a[a.length-1],s,i))&&a.push(u[0],u[1]),a=this._clipFinite(e,a))for(let d=0,f=a.length,c,l=this._edgecode(a[f-2],a[f-1]);d<f;d+=2)c=l,l=this._edgecode(a[d],a[d+1]),c&&l&&(d=this._edge(e,c,l,a,d),f=a.length);else this.contains(e,(this.xmin+this.xmax)/2,(this.ymin+this.ymax)/2)&&(a=[this.xmin,this.ymin,this.xmax,this.ymin,this.xmax,this.ymax,this.xmin,this.ymax]);return a}_edge(e,n,o,r,s){for(;n!==o;){let i,a;switch(n){case 5:n=4;continue;case 4:n=6,i=this.xmax,a=this.ymin;break;case 6:n=2;continue;case 2:n=10,i=this.xmax,a=this.ymax;break;case 10:n=8;continue;case 8:n=9,i=this.xmin,a=this.ymax;break;case 9:n=1;continue;case 1:n=5,i=this.xmin,a=this.ymin;break}(r[s]!==i||r[s+1]!==a)&&this.contains(e,i,a)&&(r.splice(s,0,i,a),s+=2)}return s}_project(e,n,o,r){let s=1/0,i,a,u;if(r<0){if(n<=this.ymin)return null;(i=(this.ymin-n)/r)<s&&(u=this.ymin,a=e+(s=i)*o)}else if(r>0){if(n>=this.ymax)return null;(i=(this.ymax-n)/r)<s&&(u=this.ymax,a=e+(s=i)*o)}if(o>0){if(e>=this.xmax)return null;(i=(this.xmax-e)/o)<s&&(a=this.xmax,u=n+(s=i)*r)}else if(o<0){if(e<=this.xmin)return null;(i=(this.xmin-e)/o)<s&&(a=this.xmin,u=n+(s=i)*r)}return[a,u]}_edgecode(e,n){return(e===this.xmin?1:e===this.xmax?2:0)|(n===this.ymin?4:n===this.ymax?8:0)}_regioncode(e,n){return(e<this.xmin?1:e>this.xmax?2:0)|(n<this.ymin?4:n>this.ymax?8:0)}_simplify(e){if(e&&e.length>4){for(let n=0;n<e.length;n+=2){let o=(n+2)%e.length,r=(n+4)%e.length;(e[n]===e[o]&&e[o]===e[r]||e[n+1]===e[o+1]&&e[o+1]===e[r+1])&&(e.splice(o,2),n-=2)}e.length||(e=null)}return e}};var cl=2*Math.PI,ln=Math.pow;function ul(t){return t[0]}function fl(t){return t[1]}function dl(t){let{triangles:e,coords:n}=t;for(let o=0;o<e.length;o+=3){let r=2*e[o],s=2*e[o+1],i=2*e[o+2];if((n[i]-n[r])*(n[s+1]-n[r+1])-(n[s]-n[r])*(n[i+1]-n[r+1])>1e-10)return!1}return!0}function hl(t,e,n){return[t+Math.sin(t+e)*n,e+Math.cos(t-e)*n]}var Ae=class t{static from(e,n=ul,o=fl,r){return new t("length"in e?ml(e,n,o,r):Float64Array.from(pl(e,n,o,r)))}constructor(e){this._delaunator=new sn(e),this.inedges=new Int32Array(e.length/2),this._hullIndex=new Int32Array(e.length/2),this.points=this._delaunator.coords,this._init()}update(){return this._delaunator.update(),this._init(),this}_init(){let e=this._delaunator,n=this.points;if(e.hull&&e.hull.length>2&&dl(e)){this.collinear=Int32Array.from({length:n.length/2},(l,h)=>h).sort((l,h)=>n[2*l]-n[2*h]||n[2*l+1]-n[2*h+1]);let u=this.collinear[0],d=this.collinear[this.collinear.length-1],f=[n[2*u],n[2*u+1],n[2*d],n[2*d+1]],c=1e-8*Math.hypot(f[3]-f[1],f[2]-f[0]);for(let l=0,h=n.length/2;l<h;++l){let g=hl(n[2*l],n[2*l+1],c);n[2*l]=g[0],n[2*l+1]=g[1]}this._delaunator=new sn(n)}else delete this.collinear;let o=this.halfedges=this._delaunator.halfedges,r=this.hull=this._delaunator.hull,s=this.triangles=this._delaunator.triangles,i=this.inedges.fill(-1),a=this._hullIndex.fill(-1);for(let u=0,d=o.length;u<d;++u){let f=s[u%3===2?u-2:u+1];(o[u]===-1||i[f]===-1)&&(i[f]=u)}for(let u=0,d=r.length;u<d;++u)a[r[u]]=u;r.length<=2&&r.length>0&&(this.triangles=new Int32Array(3).fill(-1),this.halfedges=new Int32Array(3).fill(-1),this.triangles[0]=r[0],i[r[0]]=1,r.length===2&&(i[r[1]]=0,this.triangles[1]=r[1],this.triangles[2]=r[1]))}voronoi(e){return new Pn(this,e)}*neighbors(e){let{inedges:n,hull:o,_hullIndex:r,halfedges:s,triangles:i,collinear:a}=this;if(a){let c=a.indexOf(e);c>0&&(yield a[c-1]),c<a.length-1&&(yield a[c+1]);return}let u=n[e];if(u===-1)return;let d=u,f=-1;do{if(yield f=i[d],d=d%3===2?d-2:d+1,i[d]!==e)return;if(d=s[d],d===-1){let c=o[(r[e]+1)%o.length];c!==f&&(yield c);return}}while(d!==u)}find(e,n,o=0){if(e=+e,e!==e||(n=+n,n!==n))return-1;let r=o,s;for(;(s=this._step(o,e,n))>=0&&s!==o&&s!==r;)o=s;return s}_step(e,n,o){let{inedges:r,hull:s,_hullIndex:i,halfedges:a,triangles:u,points:d}=this;if(r[e]===-1||!d.length)return(e+1)%(d.length>>1);let f=e,c=ln(n-d[e*2],2)+ln(o-d[e*2+1],2),l=r[e],h=l;do{let g=u[h],x=ln(n-d[g*2],2)+ln(o-d[g*2+1],2);if(x<c&&(c=x,f=g),h=h%3===2?h-2:h+1,u[h]!==e)break;if(h=a[h],h===-1){if(h=s[(i[e]+1)%s.length],h!==g&&ln(n-d[h*2],2)+ln(o-d[h*2+1],2)<c)return h;break}}while(h!==l);return f}render(e){let n=e==null?e=new he:void 0,{points:o,halfedges:r,triangles:s}=this;for(let i=0,a=r.length;i<a;++i){let u=r[i];if(u<i)continue;let d=s[i]*2,f=s[u]*2;e.moveTo(o[d],o[d+1]),e.lineTo(o[f],o[f+1])}return this.renderHull(e),n&&n.value()}renderPoints(e,n){n===void 0&&(!e||typeof e.moveTo!="function")&&(n=e,e=null),n=n==null?2:+n;let o=e==null?e=new he:void 0,{points:r}=this;for(let s=0,i=r.length;s<i;s+=2){let a=r[s],u=r[s+1];e.moveTo(a+n,u),e.arc(a,u,n,0,cl)}return o&&o.value()}renderHull(e){let n=e==null?e=new he:void 0,{hull:o,points:r}=this,s=o[0]*2,i=o.length;e.moveTo(r[s],r[s+1]);for(let a=1;a<i;++a){let u=2*o[a];e.lineTo(r[u],r[u+1])}return e.closePath(),n&&n.value()}hullPolygon(){let e=new _e;return this.renderHull(e),e.value()}renderTriangle(e,n){let o=n==null?n=new he:void 0,{points:r,triangles:s}=this,i=s[e*=3]*2,a=s[e+1]*2,u=s[e+2]*2;return n.moveTo(r[i],r[i+1]),n.lineTo(r[a],r[a+1]),n.lineTo(r[u],r[u+1]),n.closePath(),o&&o.value()}*trianglePolygons(){let{triangles:e}=this;for(let n=0,o=e.length/3;n<o;++n)yield this.trianglePolygon(n)}trianglePolygon(e){let n=new _e;return this.renderTriangle(e,n),n.value()}};function ml(t,e,n,o){let r=t.length,s=new Float64Array(r*2);for(let i=0;i<r;++i){let a=t[i];s[i*2]=e.call(o,a,i,t),s[i*2+1]=n.call(o,a,i,t)}return s}function*pl(t,e,n,o){let r=0;for(let s of t)yield e.call(o,s,r,t),yield n.call(o,s,r,t),++r}function So(t,e,n,o){let r,s;n<o?(r=n,s=o):(r=o,s=n);let i=r*e+s;t.set(i,(t.get(i)||0)+1)}function Ir(t,e,n){let o=t.get(e);o===void 0&&(o=[],t.set(e,o)),o.push(n)}function me(t){if(!t||t.length<3)return[];let e=new Set,n=[];for(let x=0;x<t.length;x++){let y=t[x],m=`${y[0]}_${y[1]}`;e.has(m)||(e.add(m),n.push(y))}let o=n.length;if(o<3)return[];let r=new Float64Array(o*2);for(let x=0,y=0;x<o;x++){let m=n[x];r[y++]=m[0],r[y++]=m[1]}let s=new Ae(r),{triangles:i}=s;if(!i.length)return[];let a=new Map;for(let x=0;x<i.length;x+=3){let y=i[x],m=i[x+1],p=i[x+2],b=y*2,w=m*2,z=p*2,M=r[b],P=r[b+1],E=r[w],S=r[w+1],I=r[z],W=r[z+1],v=M-E,T=P-S,R=M-I,D=P-W,H=E-I,V=S-W,C=H*H+V*V,_=R*R+D*D,A=v*v+T*T,L=(E-M)*(W-P)-(I-M)*(S-P);if(L===0)continue;let k=L*L,B=M===0||E===0||I===0?k:4*k;C*_*A<=B&&(So(a,o,y,m),So(a,o,m,p),So(a,o,p,y))}if(!a.size)return[];let u=new Map,d=0,f=-1;for(let[x,y]of a){if(y!==1)continue;let m=Math.floor(x/o),p=x-m*o;f===-1&&(f=m),Ir(u,m,p),Ir(u,p,m),d++}if(d===0)return[];let c=[],l=new Uint8Array(o),h=f,g=-1;do{c.push(n[h]),l[h]=1;let x=u.get(h)||[],y=-1;for(let m=0;m<x.length;m++){let p=x[m];if(p!==g&&!l[p]){y=p;break}}if(y===-1)for(let m=0;m<x.length;m++){let p=x[m];if(p!==g){y=p;break}}if(y===-1)break;g=h,h=y}while(h!==f&&c.length<=d+1);return c.length>0&&c.push(c[0]),c}import*as Nt from"three";import{OBB as Wl}from"three/examples/jsm/math/OBB.js";var Qt=63710088e-1,Hr={centimeters:Qt*100,centimetres:Qt*100,degrees:360/(2*Math.PI),feet:Qt*3.28084,inches:Qt*39.37,kilometers:Qt/1e3,kilometres:Qt/1e3,meters:Qt,metres:Qt,miles:Qt/1609.344,millimeters:Qt*1e3,millimetres:Qt*1e3,nauticalmiles:Qt/1852,radians:1,yards:Qt*1.0936};function Wr(t,e,n={}){let o={type:"Feature"};return(n.id===0||n.id)&&(o.id=n.id),n.bbox&&(o.bbox=n.bbox),o.properties=e||{},o.geometry=t,o}function jn(t,e,n={}){if(!t)throw new Error("coordinates is required");if(!Array.isArray(t))throw new Error("coordinates must be an Array");if(t.length<2)throw new Error("coordinates must be at least 2 numbers long");if(!Cr(t[0])||!Cr(t[1]))throw new Error("coordinates must contain numbers");return Wr({type:"Point",coordinates:t},e,n)}function $n(t,e,n={}){for(let r of t){if(r.length<4)throw new Error("Each LinearRing of a Polygon must have 4 or more Positions.");if(r[r.length-1].length!==r[0].length)throw new Error("First and last Position are not equivalent.");for(let s=0;s<r[r.length-1].length;s++)if(r[r.length-1][s]!==r[0][s])throw new Error("First and last Position are not equivalent.")}return Wr({type:"Polygon",coordinates:t},e,n)}function gl(t,e="kilometers"){let n=Hr[e];if(!n)throw new Error(e+" units is invalid");return t*n}function xl(t,e="kilometers"){let n=Hr[e];if(!n)throw new Error(e+" units is invalid");return t/n}function Xn(t){return t%(2*Math.PI)*180/Math.PI}function pe(t){return t%360*Math.PI/180}function Zn(t,e="kilometers",n="kilometers"){if(!(t>=0))throw new Error("length must be a positive number");return gl(xl(t,e),n)}function Cr(t){return!isNaN(t)&&t!==null&&!Array.isArray(t)}function _r(t){return t!==null&&typeof t=="object"&&!Array.isArray(t)}function Te(t,e,n){if(t!==null)for(var o,r,s,i,a,u,d,f=0,c=0,l,h=t.type,g=h==="FeatureCollection",x=h==="Feature",y=g?t.features.length:1,m=0;m<y;m++){d=g?t.features[m].geometry:x?t.geometry:t,l=d?d.type==="GeometryCollection":!1,a=l?d.geometries.length:1;for(var p=0;p<a;p++){var b=0,w=0;if(i=l?d.geometries[p]:d,i!==null){u=i.coordinates;var z=i.type;switch(f=n&&(z==="Polygon"||z==="MultiPolygon")?1:0,z){case null:break;case"Point":if(e(u,c,m,b,w)===!1)return!1;c++,b++;break;case"LineString":case"MultiPoint":for(o=0;o<u.length;o++){if(e(u[o],c,m,b,w)===!1)return!1;c++,z==="MultiPoint"&&b++}z==="LineString"&&b++;break;case"Polygon":case"MultiLineString":for(o=0;o<u.length;o++){for(r=0;r<u[o].length-f;r++){if(e(u[o][r],c,m,b,w)===!1)return!1;c++}z==="MultiLineString"&&b++,z==="Polygon"&&w++}z==="Polygon"&&b++;break;case"MultiPolygon":for(o=0;o<u.length;o++){for(w=0,r=0;r<u[o].length;r++){for(s=0;s<u[o][r].length-f;s++){if(e(u[o][r][s],c,m,b,w)===!1)return!1;c++}w++}b++}break;case"GeometryCollection":for(o=0;o<i.geometries.length;o++)if(Te(i.geometries[o],e,n)===!1)return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}function Ar(t){var e=[];return Te(t,function(n){e.push(n)}),e}function yl(t,e){var n,o,r,s,i,a,u,d,f,c,l=0,h=t.type==="FeatureCollection",g=t.type==="Feature",x=h?t.features.length:1;for(n=0;n<x;n++){for(a=h?t.features[n].geometry:g?t.geometry:t,d=h?t.features[n].properties:g?t.properties:{},f=h?t.features[n].bbox:g?t.bbox:void 0,c=h?t.features[n].id:g?t.id:void 0,u=a?a.type==="GeometryCollection":!1,i=u?a.geometries.length:1,r=0;r<i;r++){if(s=u?a.geometries[r]:a,s===null){if(e(null,l,d,f,c)===!1)return!1;continue}switch(s.type){case"Point":case"LineString":case"MultiPoint":case"Polygon":case"MultiLineString":case"MultiPolygon":{if(e(s,l,d,f,c)===!1)return!1;break}case"GeometryCollection":{for(o=0;o<s.geometries.length;o++)if(e(s.geometries[o],l,d,f,c)===!1)return!1;break}default:throw new Error("Unknown Geometry Type")}}l++}}function Br(t,e,n){var o=n;return yl(t,function(r,s,i,a,u){s===0&&n===void 0?o=r:o=e(o,r,s,i,a,u)}),o}var es=Ei(ts(),1);function ns(t,e={}){e.concavity=e.concavity||1/0;let n=[];if(Te(t,r=>{n.push([r[0],r[1]])}),!n.length)return null;let o=(0,es.default)(n,e.concavity);return o.length>3?$n([o]):null}function Qn(t,e={}){let n=0,o=0,r=0;return Te(t,function(s){n+=s[0],o+=s[1],r++},!0),jn([n/r,o/r],e.properties)}function ce(t){if(!t)throw new Error("coord is required");if(!Array.isArray(t)){if(t.type==="Feature"&&t.geometry!==null&&t.geometry.type==="Point")return[...t.geometry.coordinates];if(t.type==="Point")return[...t.coordinates]}if(Array.isArray(t)&&t.length>=2&&!Array.isArray(t[0])&&!Array.isArray(t[1]))return[...t];throw new Error("coord must be GeoJSON Point or an Array of numbers")}function os(t){if(Array.isArray(t))return t;if(t.type==="Feature"){if(t.geometry!==null)return t.geometry.coordinates}else if(t.coordinates)return t.coordinates;throw new Error("coords must be GeoJSON Feature, Geometry Object or an Array")}function ss(t,e,n={}){let o;return n.final?o=rs(ce(e),ce(t)):o=rs(ce(t),ce(e)),o>180?-(360-o):o}function rs(t,e){let n=pe(t[1]),o=pe(e[1]),r=pe(e[0]-t[0]);r>Math.PI&&(r-=2*Math.PI),r<-Math.PI&&(r+=2*Math.PI);let s=Math.log(Math.tan(o/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),i=Math.atan2(r,s);return(Xn(i)+360)%360}function is(t,e,n={}){let o=ce(t),r=ce(e);r[0]+=r[0]-o[0]>180?-360:o[0]-r[0]>180?360:0;let s=Dl(o,r);return Zn(s,"meters",n.units)}function Dl(t,e,n){n=n===void 0?Qt:Number(n);let o=n,r=t[1]*Math.PI/180,s=e[1]*Math.PI/180,i=s-r,a=Math.abs(e[0]-t[0])*Math.PI/180;a>Math.PI&&(a-=2*Math.PI);let u=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(r/2+Math.PI/4)),d=Math.abs(u)>1e-11?i/u:Math.cos(r);return Math.sqrt(i*i+d*d*a*a)*o}function ls(t,e,n,o={}){let r=e<0,s=Zn(Math.abs(e),o.units,"meters");r&&(s=-Math.abs(s));let i=ce(t),a=vl(i,s,n);return a[0]+=a[0]-i[0]>180?-360:i[0]-a[0]>180?360:0,jn(a,o.properties)}function vl(t,e,n,o){o=o===void 0?Qt:Number(o);let r=e/o,s=t[0]*Math.PI/180,i=pe(t[1]),a=pe(n),u=r*Math.cos(a),d=i+u;Math.abs(d)>Math.PI/2&&(d=d>0?Math.PI-d:-Math.PI-d);let f=Math.log(Math.tan(d/2+Math.PI/4)/Math.tan(i/2+Math.PI/4)),c=Math.abs(f)>1e-11?u/f:Math.cos(i),l=r*Math.sin(a)/c;return[((s+l)*180/Math.PI+540)%360-180,d*180/Math.PI]}function as(t){if(!t)throw new Error("geojson is required");switch(t.type){case"Feature":return cs(t);case"FeatureCollection":return Vl(t);case"Point":case"LineString":case"Polygon":case"MultiPoint":case"MultiLineString":case"MultiPolygon":case"GeometryCollection":return _o(t);default:throw new Error("unknown GeoJSON type")}}function cs(t){let e={type:"Feature"};return Object.keys(t).forEach(n=>{switch(n){case"type":case"properties":case"geometry":return;default:e[n]=t[n]}}),e.properties=us(t.properties),t.geometry==null?e.geometry=null:e.geometry=_o(t.geometry),e}function us(t){let e={};return t&&Object.keys(t).forEach(n=>{let o=t[n];typeof o=="object"?o===null?e[n]=null:Array.isArray(o)?e[n]=o.map(r=>r):e[n]=us(o):e[n]=o}),e}function Vl(t){let e={type:"FeatureCollection"};return Object.keys(t).forEach(n=>{switch(n){case"type":case"features":return;default:e[n]=t[n]}}),e.features=t.features.map(n=>cs(n)),e}function _o(t){let e={type:t.type};return t.bbox&&(e.bbox=t.bbox),t.type==="GeometryCollection"?(e.geometries=t.geometries.map(n=>_o(n)),e):(e.coordinates=fs(t.coordinates),e)}function fs(t){let e=t;return typeof e[0]!="object"?e.slice():e.map(n=>fs(n))}function Ao(t,e,n){if(n=n||{},!_r(n))throw new Error("options is invalid");let o=n.pivot,r=n.mutate;if(!t)throw new Error("geojson is required");if(e==null||isNaN(e))throw new Error("angle is required");if(e===0)return t;let s=o??Qn(t);return(r===!1||r===void 0)&&(t=as(t)),Te(t,function(i){let u=ss(s,i)+e,d=is(s,i),f=os(ls(s,d,u));i[0]=f[0],i[1]=f[1]}),t}function Bo(t,e,n={}){if(n.final===!0)return Il(t,e);let o=ce(t),r=ce(e),s=pe(o[0]),i=pe(r[0]),a=pe(o[1]),u=pe(r[1]),d=Math.sin(i-s)*Math.cos(u),f=Math.cos(a)*Math.sin(u)-Math.sin(a)*Math.cos(u)*Math.cos(i-s);return Xn(Math.atan2(d,f))}function Il(t,e){let n=Bo(e,t);return n=(n+180)%360,n}function ds(t,e={}){if(t.bbox!=null&&e.recompute!==!0)return t.bbox;let n=[1/0,1/0,-1/0,-1/0];return Te(t,o=>{n[0]>o[0]&&(n[0]=o[0]),n[1]>o[1]&&(n[1]=o[1]),n[2]<o[0]&&(n[2]=o[0]),n[3]<o[1]&&(n[3]=o[1])}),n}function hs(t,e={}){let n=Number(t[0]),o=Number(t[1]),r=Number(t[2]),s=Number(t[3]);if(t.length===6)throw new Error("@turf/bbox-polygon does not support BBox with 6 positions");let i=[n,o];return $n([[i,[r,o],[r,s],[n,s],i]],e.properties,{bbox:t,id:e.id})}function ms(t){return hs(ds(t))}function xs(t){return Br(t,(e,n)=>e+Cl(n),0)}function Cl(t){let e=0,n;switch(t.type){case"Polygon":return ps(t.coordinates);case"MultiPolygon":for(n=0;n<t.coordinates.length;n++)e+=ps(t.coordinates[n]);return e;case"Point":case"MultiPoint":case"LineString":case"MultiLineString":return 0}return 0}function ps(t){let e=0;if(t&&t.length>0){e+=Math.abs(gs(t[0]));for(let n=1;n<t.length;n++)e-=Math.abs(gs(t[n]))}return e}var Hl=Qt*Qt/2,Lo=Math.PI/180;function gs(t){let e=t.length-1;if(e<=2)return 0;let n=0,o=0;for(;o<e;){let r=t[o],s=t[o+1===e?0:o+1],i=t[o+2>=e?(o+2)%e:o+2],a=r[0]*Lo,u=s[1]*Lo,d=i[0]*Lo;n+=(d-a)*Math.sin(u),o++}return n*Hl}function ys(t){let e=ns(t);if(!e)throw new Error("Can't calculate smallestSurroundingRectangleByArea for given geometry");let n=Qn(e),o=Ar(e),r=Number.MAX_SAFE_INTEGER,s=null;for(let i=0;i<o.length-1;i++){let a=Bo(o[i],o[i+1]),u=Ao(e,-1*a,{pivot:n}),d=ms(u),f=xs(d);f<r&&(r=f,s=Ao(d,a,{pivot:n}))}return s}function bs(t,e){let n=e[0]-t[0],o=e[1]-t[1];return Math.sqrt(n*n+o*o)}function _l(t){let e=1/0,n=-1/0,o=1/0,r=-1/0;for(let[s,i]of t)s<e&&(e=s),s>n&&(n=s),i<o&&(o=i),i>r&&(r=i);return[(n+e)*.5,(r+o)*.5]}var to=class t extends Wl{getBoxMesh(){Be.identity(),Be.setFromMatrix3(this.rotation),Be.decompose(Jn,Ne,new Nt.Vector3);let e=this.halfSize.clone().multiplyScalar(2),n=new Nt.Mesh(new Nt.BoxGeometry(e.x,e.y,e.z));return n.position.copy(this.center),n.rotation.setFromQuaternion(Ne),n}getBoxEdge(e=16776960){let n=this.getBoxMesh();Bl.setFromObject(n);let o=new Nt.EdgesGeometry(n.geometry.clone().applyMatrix4(n.matrixWorld));return new Nt.LineSegments(o,new Nt.LineBasicMaterial({color:e,toneMapped:!1}))}toJson(){return Be.identity(),Be.setFromMatrix3(this.rotation),Be.decompose(Jn,Ne,new Nt.Vector3),{size:this.halfSize.toArray(),center:this.center.toArray(),quaternion:Ne.toArray()}}static from(e,n,o,r=new t){if(o instanceof Nt.Euler)Ne.setFromEuler(o);else if(o instanceof Nt.Quaternion)Ne.copy(o);else throw new Error("\u4F20\u5165\u7684\u65CB\u8F6C\u4E0D\u662F\u6B27\u62C9\u89D2\u6216\u8005\u56DB\u5143\u6570");return Be.compose(n,Ne,Al),r.center.copy(n),r.halfSize.copy(e.multiplyScalar(.5)),r.rotation.setFromMatrix4(Be),r}static fromByPath2D(e,n,o,r=new t){let s=ys({type:"Polygon",coordinates:[e.map(h=>[h.x,h.y])]});if(!s)throw new Error("2d obb \u83B7\u53D6\u5931\u8D25");let i=s.geometry.coordinates[0],a=bs(i[0],i[1]),u=bs(i[1],i[2]),d=Math.atan2(i[1][1]-i[0][1],i[1][0]-i[0][0]);Jn.set(a,u,o),ws.set(0,0,d);let[f,c]=_l(i);return n.x=f,n.y=c,this.from(Jn,n,ws,r)}},Be=new Nt.Matrix4,Ne=new Nt.Quaternion,ws=new Nt.Euler,Al=new Nt.Vector3(1,1,1),Jn=new Nt.Vector3,Bl=new Nt.Box3;import"three";function Ll(t,e,n=.005,o=.2){if(!t.length||!e.length)return 0;let r=Math.max(n,.03),s=n*n,i=new Map,a=(c,l,h)=>`${Math.floor(c/r)},${Math.floor(l/r)},${Math.floor(h/r)}`;for(let c of e){let l=a(c.x,c.y,c.z);i.has(l)||i.set(l,[]),i.get(l).push(c)}let u=0,d=Math.ceil(n/r)+1,f=[];for(let c=-d;c<=d;c++)for(let l=-d;l<=d;l++)for(let h=-d;h<=d;h++)c*c+l*l+h*h<=d*d&&f.push([c,l,h]);for(let c of t){let l=Math.floor(c.x/r),h=Math.floor(c.y/r),g=Math.floor(c.z/r),x=!1;for(let[y,m,p]of f){let b=`${l+y},${h+m},${g+p}`;if(i.has(b)){for(let w of i.get(b))if(c.distanceToSquared(w)<s){x=!0;break}if(x)break}}if(x&&(u++,u/t.length>o))return u/t.length}return u/t.length}function Ie(t,e,n=.5){let o=0,r=0;for(let s of t)if(e.containsPoint(s)&&(o++,r=o/t.length,r>n))break;return r}function ko(t,e,n,o){if(t.intersectsOBB(e))return!0;{let s=Jt(t,o),i=Jt(e,n);if(s||i)return!0}return!1}function Oo(t,e){let n=new q.Vector3,o=new q.Vector3;t.getSize(n),e.getSize(o);let r=n.x*n.y*n.z,s=o.x*o.y*o.z;return r>s?1:r<s?-1:0}function Jt(t,e){let n=!0;for(let o=0;o<e.length;o++)if(!t.containsPoint(e[o])){n=!1;break}return n}function ue(t){if(t.length===0)return{x:0,y:0,z:0,h:0};let e=0,n=0,o=-1/0,r=1/0;for(let s of t)e+=s.x,n+=s.y,o=Math.max(o,s.z),r=Math.min(r,s.z);return{x:e/t.length,y:n/t.length,z:0,maxz:o,minz:r,h:o-r}}function ze(t,e,n,o){let r=[];for(let i=0;i<t.length;i++)r.push(new q.Vector3(t[i][0],t[i][1],0));if(r.length==0)return null;let s=to.fromByPath2D(r,e,n);return s.center.z=o,s}var kl=(t,e)=>{let n=new q.Group,o=new q.MeshStandardMaterial({color:16737792,side:q.DoubleSide});for(let r=0;r<t.length;r++){let s=[];for(let x=0;x<t[r].path.length;x++)s.push(new q.Vector3(t[r].path[x][0],t[r].path[x][1],0));if(s.length==0)continue;n.add(t[r].obb.getBoxEdge());let i=new q.BufferGeometry;i.setFromPoints(s);let a="green",u=new q.LineBasicMaterial({color:a,depthTest:!1,depthWrite:!1}),d=new q.Line(i,u);d.position.z=t[r].maxz,n.add(d);let f=t[r].obb.center.clone(),c=pr(t[r].type,f),l=t[r].stairBottomPt1.clone(),h=t[r].stairBottomPt2.clone();for(let x=0;x<t[r].stairStepNum;x++){let y=[l,h];y.push(h.clone().addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth)),y.push(l.clone().addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth));let m=Eo(y,t[r].stairStepHeight),p=new q.Mesh(m,o);n.add(p),l=l.addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth),l=l.addScaledVector(new q.Vector3(0,0,1),t[r].stairStepHeight),h=h.addScaledVector(t[r].vecStepWidth,t[r].stairStepWidth),h=h.addScaledVector(new q.Vector3(0,0,1),t[r].stairStepHeight)}if(t[r].rectPt1.x!=0&&t[r].rectPt1.y!=0){let x=[t[r].stairBottomPt1,t[r].stairBottomPt2,t[r].rectPt2,t[r].rectPt1],y=Eo(x,t[r].stairStepHeight),m=new q.Mesh(y,o);n.add(m)}let g=new q.MeshBasicMaterial({color:16776960});for(let x=0;x<t[r].stairBottom1BalconyRailing.length;x++){let y=new q.CylinderGeometry(.03,.03,t[r].balconyRailingHeight,32),m=new q.Mesh(y,g);m.position.set(t[r].stairBottom1BalconyRailing[x].x,t[r].stairBottom1BalconyRailing[x].z+t[r].balconyRailingHeight/2,-t[r].stairBottom1BalconyRailing[x].y),e.add(m)}for(let x=0;x<t[r].stairBottom2BalconyRailing.length;x++){let y=new q.CylinderGeometry(.03,.03,t[r].balconyRailingHeight,32),m=new q.Mesh(y,g);m.position.set(t[r].stairBottom2BalconyRailing[x].x,t[r].stairBottom2BalconyRailing[x].z+t[r].balconyRailingHeight/2,-t[r].stairBottom2BalconyRailing[x].y),e.add(m)}}return n},Ol=(t,e,n=null)=>{if(t.length==0)return[];e||(e=t[0].averagePz);let o=[],r=new Map;for(let i=0;i<t.length;i++){let a=t[i].points.map(p=>[p.x,p.y]),u=t[i].obj_id;u=u.replace(/calcony railing/g,"balcony railing").replace(/glass balcony railing/g,"balcony railing").replace(/metal balcony railing/g,"balcony railing").replace(/temporary construction guardrail/g,"balcony railing").replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor").replace(/roof/g,"Ceiling");let d=u.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],f=u.indexOf("_")+1,c=u.lastIndexOf("_"),l=u.substring(f,c);if(l.includes("top")&&(f=l.indexOf("top")+4,l=l.substring(f,c)),l.includes("left")&&(f=l.indexOf("left")+5,l=l.substring(f,c)),l.includes("right")&&(f=l.indexOf("right")+6,l=l.substring(f,c)),l.includes("front")&&(f=l.indexOf("front")+6,l=l.substring(f,c)),l==="wall"||l==="stair guardrail")continue;let h=[];for(let p=0;p<t[i].points.length;p++)h.push(new q.Vector3(t[i].points[p].x,t[i].points[p].y,t[i].points[p].z));let g=-99999;if(l==="floor"){let p=new Map;for(let w=0;w<t[i].points.length;w++){let z=t[i].points[w].z;if(z=Math.floor(z*10)/10,p.has(z)){let M=p.get(z);M.num+=1,M.points.push(t[i].points[w]),p.set(z,M)}else p.set(z,{num:1,points:[t[i].points[w]]})}let b=[...p.entries()].reduce((w,z)=>z[1].num>w[1].num?z:w);a=[],a=b[1].points.map(w=>[w.x,w.y]),h=[],h=b[1].points.map(w=>new q.Vector3(w.x,w.y,w.z)),g=b[0]}let x=ue(h),y={points:h,path:me(a),h:x.h,minz:x.minz,maxz:x.maxz,center:{x:x.x,y:x.y,z:x.z},obj_id:t[i].obj_id,type:l,averagePz:e,obb:null,isDel:!1,obj_id:u,positionArr:[t[i].position],isStaircaseBalconyRailing:!1};if(y.obb=ze(y.path,y.center,y.h,(y.maxz+y.minz)/2),y.obb==null)continue;let m=-1;for(let p=0;p<o.length;p++)if(o[p].type==l){m=p;break}if(m!=-1?o[m].array.push(y):o.push({type:l,array:[y]}),l=="floor"&&g!=-99999)if(r.has(g)){let p=r.get(g);p.num+=y.points.length,p.datas.push(y),r.set(g,p)}else r.set(g,{num:y.points.length,datas:[y]})}if(r.size>0){let i=[];for(let[a,u]of r.entries()){let{num:d,datas:f}=u;d>3e3&&i.push(...f)}for(let a=0;a<o.length;a++)if(o[a].type=="floor"){o[a].array=i;break}}for(let i=0;i<o.length;i++){let a=o[i].array,u=new Set,d=[];for(let c=0;c<a.length;c++){if(a[c].obb==null||u.has(c))continue;let l={...a[c],obb:a[c].obb.clone()},h=0,g=0;if(l.type=="pipe"){let y=Ve(l.points);h=y.minZ,g=y.maxZ}let x=!0;h!=0&&g!=0&&Math.abs(g-h)<.3&&(x=!1);for(let y=c+1;y<a.length;y++){if(a[y].obb==null||u.has(y))continue;let m=0,p=0;if(a[y].type=="pipe"){let z=Ve(a[y].points);m=z.minZ,p=z.maxZ}let b=!0;if(m!=0&&p!=0&&Math.abs(p-m)<.3&&(b=!1),l.type=="pipe"&&a[y].type=="pipe"&&x!=b)continue;let w=!1;if(l.type=="pipe"&&!x?w=Ie(a[y].points,l.obb)>.3:(w=l.obb.intersectsOBB(a[y].obb,.01),w||(w=Gl(l.obb,a[y].obb))),w){u.add(y);for(let P=0;P<a[y].points.length;P++)(a[y].points[P].z>e&&a[y].points[P].z-e>.1||l.type=="floor"||l.type=="Staircase")&&l.points.push(a[y].points[P]);let z=l.points.map(P=>[P.x,P.y]);l.type=="floor"&&(z=Kl(z,.1)),l.path=me(z);let M=ue(l.points);l.h=M.h,l.minz=M.minz,l.maxz=M.maxz,l.center={x:M.x,y:M.y,z:M.z},l.obb=ze(l.path,l.center,l.h,(l.maxz+l.minz)/2),l.positionArr.push(...a[y].positionArr),y=c}}l.path.length>a[c].path.length&&(u.add(c),d.push(l))}let f=[];for(let c=0;c<a.length;c++)u.has(c)||f.push(a[c]);f.push(...d),o[i].array=f}for(let i=0;i<o.length;i++){let a=o[i].type;if(a==="people"||a==="floor")continue;let u=o[i].array;for(let d=0;d<u.length;d++){let f=u[d].obb,c=u[d].minz,l=u[d].maxz,h=u[d].averagePz;if(a==="window"||a==="door"||a==="tv"||a==="chair"||a==="cabinet"||a==="table"){let g=new q.Vector3;f.getSize(g);let x=[g.x,g.y,g.z],y=0;for(let m of x)if(m<.2&&(y++,y===2)){u[d].isDel=!0;break}}if(!u[d].isDel)for(let g=i+1;g<o.length;g++){let x=o[g].type;if(x==="people")continue;let y=o[g].array;for(let m=0;m<y.length;m++){if(y[m].isDel)continue;let p=y[m].obb,b=y[m].minz,w=y[m].maxz;if(f.intersectsOBB(p)){let z=Ie(y[m].points,f),M=Ie(u[d].points,p),P=Oo(f,p);if(M>=.5&&P==-1&&a!=="Staircase"&&x!=="Staircase"){u[d].isDel=!0;break}if(a!=="wall"&&a!=="floor"&&a!=="roof"){if(a==="window"){if(x==="balcony railing"&&Jt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="balcony railing"&&Jt(p,u[d].points)){u[d].isDel=!0;break}if(x==="balcony railing"&&ko(f,p,u[d].points,y[m].points)&&(Math.abs(c-b)<.1||Math.abs(b-h)>.3)){y[m].isDel=!0;continue}if(x==="door"&&Jt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Jt(p,u[d].points)){y[m].isDel=!0;continue}if(x==="window sill")continue}else if(a==="balcony railing"){if(x==="window"&&Jt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="window"&&Jt(p,u[d].points)){u[d].isDel=!0;break}if(x==="window"&&ko(f,p,u[d].points,y[m].points)&&(Math.abs(c-b)<.1||Math.abs(c-h)>.3)){u[d].isDel=!0;break}if(x==="Staircase"&&Jt(f,y[m].points)){u[d].isStaircaseBalconyRailing=!0;break}if(x==="Staircase"&&Jt(p,u[d].points)){u[d].isStaircaseBalconyRailing=!0;break}if(x==="Staircase"&&f.intersectsOBB(p)){u[d].isStaircaseBalconyRailing=!0;break}}else if(a==="door"){if(x==="window"&&Jt(f,y[m].points)){u[d].isDel=!0;break}if(x==="window"&&Jt(p,u[d].points)){u[d].isDel=!0;break}if(x==="window sill"&&Jt(f,y[m].points)){u[d].isDel=!0;break}if(x==="window sill"&&Jt(p,u[d].points)){u[d].isDel=!0;break}}else if(a==="window sill"){if(x==="window")continue;if(x==="floor"&&Jt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Jt(f,y[m].points)){y[m].isDel=!0;continue}if(x==="door"&&Jt(p,u[d].points)){y[m].isDel=!0;continue}}else if(a==="Staircase"){if(x==="balcony railing"&&Jt(f,y[m].points)){y[m].isStaircaseBalconyRailing=!0;continue}if(x==="balcony railing"&&Jt(p,u[d].points)){y[m].isStaircaseBalconyRailing=!0;continue}if(x==="balcony railing"&&f.intersectsOBB(p)){y[m].isStaircaseBalconyRailing=!0;continue}}let E=!1,S=[],I=[],W=u[d].points,v=y[m].points;if(W.length>v.length?(E=!0,S=W,I=v):(S=v,I=W),Ll(I,S,.03,.5)>.5)if(E)y[m].isDel=!0;else{u[d].isDel=!0;break}else if(ko(f,p,u[d].points,y[m].points)){let R=Oo(f,p);if(R===1||R===0){if(x==="switch"||x==="Staircase")continue;W.length>v.length?Ie(I,f)>=.5&&(y[m].isDel=!0):Ie(S,f)>=.5&&(y[m].isDel=!0)}else{if(a==="switch"||a==="Staircase")continue;if(W.length>v.length){if(Ie(S,p)>=.5){u[d].isDel=!0;break}}else if(Ie(I,p)>=.5){u[d].isDel=!0;break}}}}}}if(u[d].isDel)break}}}for(let i=0;i<o.length;i++){let a=o[i].type,u=o[i].array;for(let d=0;d<u.length;d++){let f=u[d].maxz,c=u[d].minz,l=a==="switch"?.1:.3;if(Math.abs(f-e)<l&&a!="floor"){u[d].isDel=!0;continue}if(a==="window"||a==="door"||a==="tv"){let h=Math.abs(f-e),g=Math.abs(c-e);(a==="tv"&&g<1.6||a!=="window"&&h<1.6)&&(u[d].isDel=!0),a==="window"&&h<1&&(u[d].isDel=!0),a==="door"&&g>1&&(u[d].isDel=!0)}if(a==="chair"){let h=new q.Vector3;u[d].obb.getSize(h),(h.x<.1||h.y<.1||h.z<.2)&&(u[d].isDel=!0)}if(a==="balcony railing"&&(Math.abs(f-c)<.4&&(u[d].isDel=!0),(c<e&&Math.abs(c-e)>1||c>e&&Math.abs(c-e)>1)&&(u[d].isDel=!0),f<e&&(u[d].isDel=!0)),a==="people"&&(Math.abs(f-c)<1&&(u[d].isDel=!0),c>e&&c-e>.3&&(u[d].isDel=!0),f<e&&(u[d].isDel=!0)),a==="floor"){let h=new q.Vector3;u[d].obb.getSize(h),(h.x<.1||h.y<.1)&&(u[d].isDel=!0)}a==="stair guardrail"&&u[d].points.length<100&&(u[d].isDel=!0)}}for(let i=0;i<o.length;i++)o[i].array=o[i].array.filter(a=>!a.isDel);if(n!=null)for(let i=0;i<o.length;i++){if(o[i].type=="floor")continue;let u=o[i].array,d=new Set,f=[];for(let c=0;c<u.length;c++){if(d.has(c))continue;let l=u[c].points,h=u[c].obj_id,g=u[c].type,x=u[c].averagePz,y=u[c].positionArr,m=u[c].isStaircaseBalconyRailing;if(l.length<50)continue;let p=n([l],{tolerance:.2,minClusterSize:50,maxClusterSize:1e7,threadCount:1}),b=f.length;if(p.length>0&&p[0].length>1)for(let w of p[0]){if(w.length<50)continue;let z=w.map(S=>l[S]),M=ue(z),P=z.map(S=>[S.x,S.y]),E={points:z,path:me(P),h:M.h,minz:M.minz,maxz:M.maxz,center:{x:M.x,y:M.y,z:M.z},type:g,averagePz:x,obb:null,obj_id:h,positionArr:y,isStaircaseBalconyRailing:m};E.obb=ze(E.path,E.center,E.h,(E.maxz+E.minz)/2),E.obb!=null&&f.push(E)}f.length!=b&&d.add(c)}if(d.size>0){for(let c=0;c<u.length;c++)d.has(c)||f.push(u[c]);o[i].array=f}}let s=[];for(let i=0;i<o.length;i++){let a=o[i].array;for(let u=0;u<a.length;u++)s.push({obbBox:a[u].obb,box:new q.Box3().setFromPoints(a[u].points),category:a[u].type,center:{x:a[u].center.x,y:a[u].center.y,z:(a[u].minz+a[u].maxz)/2},name:a[u].obj_id,obj_id:a[u].obj_id,pcdPoints:a[u].points,positionArr:a[u].positionArr,isStaircaseBalconyRailing:a[u].isStaircaseBalconyRailing})}return s},Fl=t=>{if(t.length==0)return[];let e=[],n=new Map;for(let r=0;r<t.length;r++){let s=t[r].points.map(y=>[y.x,y.y]),i=t[r].obj_id;i=i.replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor");let a=i.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],u=i.indexOf("_")+1,d=i.lastIndexOf("_"),f=i.substring(u,d);if(f!=="floor")continue;let c=[];for(let y=0;y<t[r].points.length;y++)c.push(new q.Vector3(t[r].points[y].x,t[r].points[y].y,t[r].points[y].z));let l=-99999;if(f==="floor"){let y=new Map;for(let p=0;p<t[r].points.length;p++){let b=t[r].points[p].z;if(b=Math.floor(b*10)/10,y.has(b)){let w=y.get(b);w.num+=1,w.points.push(t[r].points[p]),y.set(b,w)}else y.set(b,{num:1,points:[t[r].points[p]]})}let m=[...y.entries()].reduce((p,b)=>b[1].num>p[1].num?b:p);s=[],s=m[1].points.map(p=>[p.x,p.y]),c=[],c=m[1].points.map(p=>new q.Vector3(p.x,p.y,p.z)),l=m[0]}let h=ue(c),g={points:c,path:me(s),h:h.h,minz:h.minz,maxz:h.maxz,center:{x:h.x,y:h.y,z:h.z},obj_id:t[r].obj_id,type:f,obb:null,isDel:!1,obj_id:i,positionArr:[t[r].position]};if(g.obb=ze(g.path,g.center,g.h,(g.maxz+g.minz)/2),g.obb==null)continue;let x=-1;for(let y=0;y<e.length;y++)if(e[y].type==f){x=y;break}if(x!=-1?e[x].array.push(g):e.push({type:f,array:[g]}),f=="floor"&&l!=-99999)if(n.has(l)){let y=n.get(l);y.num+=g.points.length,y.datas.push(g),n.set(l,y)}else n.set(l,{num:g.points.length,datas:[g]})}for(let r=0;r<e.length;r++){let s=e[r].array,i=[],a=[];for(let d=0;d<s.length;d++){if(s[d].obb==null||i.includes(d))continue;let f={...s[d],obb:s[d].obb.clone()};for(let c=d+1;c<s.length;c++){if(s[c].obb==null||i.includes(c))continue;if(f.obb.intersectsOBB(s[c].obb)){i.push(c);for(let x=0;x<s[c].points.length;x++)f.points.push(s[c].points[x]);let h=f.points.map(x=>[x.x,x.y]);f.path=me(h);let g=ue(f.points);f.h=g.h,f.minz=g.minz,f.maxz=g.maxz,f.center={x:g.x,y:g.y,z:g.z},f.obb=ze(f.path,f.center,f.h,(f.maxz+f.minz)/2),f.positionArr.push(...s[c].positionArr),c=d}}f.path.length>s[d].path.length&&(i.push(d),a.push(f))}let u=[];for(let d=0;d<s.length;d++)i.includes(d)||u.push(s[d]);u.push(...a),e[r].array=u}for(let r=0;r<e.length;r++){let s=e[r].type,i=e[r].array;for(let a=0;a<i.length;a++)s==="floor"&&i[a].points.length<500&&(i[a].isDel=!0)}for(let r=0;r<e.length;r++)e[r].array=e[r].array.filter(s=>!s.isDel);let o=[];for(let r=0;r<e.length;r++){let s=e[r].array;for(let i=0;i<s.length;i++)o.push({obbBox:s[i].obb,box:new q.Box3().setFromPoints(s[i].points),category:s[i].type,center:{x:s[i].center.x,y:s[i].center.y,z:(s[i].minz+s[i].maxz)/2},name:s[i].obj_id,obj_id:s[i].obj_id,pcdPoints:s[i].points,positionArr:s[i].positionArr})}return o},Nl=(t,e)=>{if(!t||!t.splitFloorProjectsResults||!t.trajectoryResults)return[];let n=t.splitFloorProjectsResults,o=t.trajectoryResults,r=new Map;for(let l=0;l<o.length;l++){let h=o[l].floor;r.has(h)?r.get(h).push(o[l]):r.set(h,[o[l]])}let s=new Map,i=[];for(let l=0;l<n.length;l++){let h=[],g=n[l];for(let x=0;x<g.length;x++){let y=g[x].points.map(S=>[S.x,S.y]),m=g[x].obj_id;m=m.replace(/the floor of the house/g,"floor").replace(/the outdoor floor/g,"floor").replace(/calcony railing/g,"balcony railing").replace(/glass balcony railing/g,"balcony railing").replace(/metal balcony railing/g,"balcony railing").replace(/temporary construction guardrail/g,"balcony railing");let p=m.split("_").slice(1,3).join("_").split(".")[0].split("_")[0],b=m.indexOf("_")+1,w=m.lastIndexOf("_"),z=m.substring(b,w);if(z.includes("top")&&(b=z.indexOf("top")+4,z=z.substring(b,w)),z.includes("left")&&(b=z.indexOf("left")+5,z=z.substring(b,w)),z.includes("right")&&(b=z.indexOf("right")+6,z=z.substring(b,w)),z.includes("front")&&(b=z.indexOf("front")+6,z=z.substring(b,w)),z!=="Staircase"&&z!=="balcony railing")continue;let M=[];for(let S=0;S<g[x].points.length;S++)M.push(new q.Vector3(g[x].points[S].x,g[x].points[S].y,g[x].points[S].z));let P=ue(M),E={points:M,path:me(y),h:P.h,minz:P.minz,maxz:P.maxz,center:{x:P.x,y:P.y,z:P.z},obj_id:g[x].obj_id,type:z,averagePz:0,obb:null,isDel:!1,obj_id:m,positionArr:[g[x].position],aabbBox:new q.Box3().setFromPoints(M)};E.obb=ze(E.path,E.center,E.h,(E.maxz+E.minz)/2),E.obb!=null&&(z=="Staircase"?h.push(E):z=="balcony railing"&&i.push(E))}s.set(l+1,h)}let a=[];for(let[l,h]of s){let g={floor:l,findTrajectoryResults:[],otherTrajectoryResults:[],lastTrajectoryResults:[]},x=r.get(l);if(!x){g.otherTrajectoryResults=h,a.push(g);continue}let y=[],m=[],p=[];for(let b=0;b<x.length;b++){let w=[];for(let M=0;M<x[b].points.length;M++){let P=[new q.Vector3(x[b].points[M].x,x[b].points[M].y,x[b].points[M].z)];P.push(P[0].clone().addScaledVector(new q.Vector3(0,0,-1),2));let E=new q.Box3().setFromPoints(P);w.push(E)}let z=[];for(let M=0;M<h.length;M++)for(let P=0;P<w.length;P++)!p.includes(M)&&h[M].aabbBox.intersectsBox(w[P])&&(p.push(M),z.push(h[M]));z.length>0&&m.push(z)}for(let b=0;b<h.length;b++)p.includes(b)||y.push(h[b]);g.findTrajectoryResults=m,g.otherTrajectoryResults=y,a.push(g)}function u(l,h){let g=l,x=[],y=[];for(let p=0;p<l.length;p++){if(l[p].obb==null||x.includes(p))continue;let b={...l[p],obb:l[p].obb.clone()};for(let w=p+1;w<l.length;w++){if(l[w].obb==null||x.includes(w))continue;if(b.obb.intersectsOBB(l[w].obb)||h){x.push(w);for(let E=0;E<l[w].points.length;E++)b.points.push(l[w].points[E]);let M=b.points.map(E=>[E.x,E.y]);b.path=me(M);let P=ue(b.points);b.h=P.h,b.minz=P.minz,b.maxz=P.maxz,b.center={x:P.x,y:P.y,z:P.z},b.obb=ze(b.path,b.center,b.h,(b.maxz+b.minz)/2),b.positionArr.push(...l[w].positionArr),w=p}}b.path.length>l[p].path.length&&(x.push(p),y.push(b))}let m=[];for(let p=0;p<l.length;p++)x.includes(p)||m.push(l[p]);return m.push(...y),l=m,l}for(let l=0;l<a.length;l++){for(let h=0;h<a[l].findTrajectoryResults.length;h++)a[l].findTrajectoryResults[h]=u(a[l].findTrajectoryResults[h],!0);a[l].otherTrajectoryResults=u(a[l].otherTrajectoryResults,!1)}for(let l=0;l<a.length;l++){for(let h=0;h<a[l].findTrajectoryResults.length;h++){for(let g=0;g<a[l].findTrajectoryResults[h].length;g++){let x=zs(a[l].findTrajectoryResults[h][g].points,{binCount:100,minBinPoints:5,sideK:2.2,zK:2.2,minSideRange:.2,minZRange:.2,lowerZScale:.4,upperZScale:1.8}),y=Ms(x.keep,{radius:.1,minClusterSize:50,lowPercent:.45});if(y.keep.length==0){a[l].findTrajectoryResults[h][g].isDel=!0;continue}a[l].findTrajectoryResults[h][g].points=y.keep;let m=a[l].findTrajectoryResults[h][g].points.map(b=>[b.x,b.y]);a[l].findTrajectoryResults[h][g].path=me(m);let p=ue(a[l].findTrajectoryResults[h][g].points);a[l].findTrajectoryResults[h][g].h=p.h,a[l].findTrajectoryResults[h][g].minz=p.minz,a[l].findTrajectoryResults[h][g].maxz=p.maxz,a[l].findTrajectoryResults[h][g].center={x:p.x,y:p.y,z:p.z},a[l].findTrajectoryResults[h][g].obb=ze(a[l].findTrajectoryResults[h][g].path,a[l].findTrajectoryResults[h][g].center,a[l].findTrajectoryResults[h][g].h,(a[l].findTrajectoryResults[h][g].maxz+a[l].findTrajectoryResults[h][g].minz)/2)}a[l].findTrajectoryResults[h]=a[l].findTrajectoryResults[h].filter(g=>!g.isDel)}for(let h=0;h<a[l].otherTrajectoryResults.length;h++){let g=zs(a[l].otherTrajectoryResults[h].points,{binCount:100,minBinPoints:5,sideK:2.2,zK:2.2,minSideRange:.2,minZRange:.2,lowerZScale:.4,upperZScale:1.8}),x=Ms(g.keep,{radius:.1,minClusterSize:50,lowPercent:.45});if(x.keep.length==0){a[l].otherTrajectoryResults[h].isDel=!0;continue}a[l].otherTrajectoryResults[h].points=x.keep;let y=a[l].otherTrajectoryResults[h].points.map(p=>[p.x,p.y]);a[l].otherTrajectoryResults[h].path=me(y);let m=ue(a[l].otherTrajectoryResults[h].points);a[l].otherTrajectoryResults[h].h=m.h,a[l].otherTrajectoryResults[h].minz=m.minz,a[l].otherTrajectoryResults[h].maxz=m.maxz,a[l].otherTrajectoryResults[h].center={x:m.x,y:m.y,z:m.z},a[l].otherTrajectoryResults[h].obb=ze(a[l].otherTrajectoryResults[h].path,a[l].otherTrajectoryResults[h].center,a[l].otherTrajectoryResults[h].h,(a[l].otherTrajectoryResults[h].maxz+a[l].otherTrajectoryResults[h].minz)/2)}a[l].otherTrajectoryResults.filter(h=>!h.isDel)}function d(l){for(let h=0;h<l.length;h++){let g=l[h].obb;for(let x=h+1;x<l.length;x++){let y=l[x].obb;if(!l[x].isDel){if(g.intersectsOBB(y)){let m=Ie(l[x].points,g),p=Ie(l[h].points,y),b=Oo(g,y);if(m>=.5&&(b==1||b==0)&&(l[x].isDel=!0),p>=.5&&b==-1){l[h].isDel=!0;break}}if(l[h].isDel)break}}}return l=l.filter(h=>!h.isDel),l}for(let l=0;l<a.length;l++){let h=[];for(let g=0;g<a[l].findTrajectoryResults.length;g++)h.push(...a[l].findTrajectoryResults[g]);h.push(...a[l].otherTrajectoryResults),a[l].lastTrajectoryResults=d(h)}function f(l){let h=[];if(l.length>0){let g=.3,x=.5,y=.5,m=.2;for(let p=0;p<l.length;p++){let b=l[p].obb.halfSize.clone().multiplyScalar(2),w=l[p].points,z=b.x,M=b.y,P=b.z;if(z>M&&([z,M]=[M,z]),z<x||M<g||P<y)continue;let E=wr(l[p].obb);if(E.length!=8)continue;let S=new q.Line3(E[0],E[1]),I=new q.Line3(E[1],E[2]),W=new q.Line3(E[2],E[3]),v=new q.Line3(E[3],E[0]),T=new q.Line3(E[4],E[5]),R=new q.Line3(E[5],E[6]),D=new q.Line3(E[6],E[7]),H=new q.Line3(E[7],E[4]),V=[],C=[],_=[],A=[],L=[],k=[],F=[],B=[];for(let wt=0;wt<w.length;wt++){let te=S.closestPointToPoint(w[wt],!0,new q.Vector3),ge=I.closestPointToPoint(w[wt],!0,new q.Vector3),Dt=W.closestPointToPoint(w[wt],!0,new q.Vector3),ne=v.closestPointToPoint(w[wt],!0,new q.Vector3),xe=T.closestPointToPoint(w[wt],!0,new q.Vector3),tt=R.closestPointToPoint(w[wt],!0,new q.Vector3),mn=D.closestPointToPoint(w[wt],!0,new q.Vector3),Je=H.closestPointToPoint(w[wt],!0,new q.Vector3);te.distanceTo(w[wt])<m&&V.push(w[wt]),ge.distanceTo(w[wt])<m&&C.push(w[wt]),Dt.distanceTo(w[wt])<m&&_.push(w[wt]),ne.distanceTo(w[wt])<m&&A.push(w[wt]),xe.distanceTo(w[wt])<m&&L.push(w[wt]),tt.distanceTo(w[wt])<m&&k.push(w[wt]),mn.distanceTo(w[wt])<m&&F.push(w[wt]),Je.distanceTo(w[wt])<m&&B.push(w[wt])}let O=E[0].distanceTo(E[1]),$=E[1].distanceTo(E[2]),Z=E[2].distanceTo(E[3]),Q=E[3].distanceTo(E[0]),j=new q.Vector3,X=new q.Vector3,U=new q.Vector3,at=new q.Vector3;Math.abs(O-z)<.01?V.length>_.length?(j=E[0].clone(),X=E[1].clone(),U=new q.Vector3().subVectors(X,j).normalize(),at=new q.Vector3().subVectors(E[2],E[1]).normalize()):(j=E[2].clone(),X=E[3].clone(),U=new q.Vector3().subVectors(X,j).normalize(),at=new q.Vector3().subVectors(E[0],E[3]).normalize()):C.length>A.length?(j=E[1].clone(),X=E[2].clone(),U=new q.Vector3().subVectors(X,j).normalize(),at=new q.Vector3().subVectors(E[3],E[2]).normalize()):(j=E[3].clone(),X=E[0].clone(),U=new q.Vector3().subVectors(X,j).normalize(),at=new q.Vector3().subVectors(E[1],E[0]).normalize());let mt=E[4].distanceTo(E[5]),St=E[5].distanceTo(E[6]),ot=E[6].distanceTo(E[7]),Mt=E[7].distanceTo(E[4]),bt=new q.Vector3,Ct=new q.Vector3,Et=new q.Vector3(j.x,j.y,E[4].z),zt=new q.Vector3(X.x,X.y,E[4].z);(ct(E[4],Et)||ct(E[4],zt))&&(ct(E[5],Et)||ct(E[5],zt))?(bt=E[6].clone(),Ct=E[7].clone()):(ct(E[5],Et)||ct(E[5],zt))&&(ct(E[6],Et)||ct(E[6],zt))?(bt=E[7].clone(),Ct=E[4].clone()):(ct(E[6],Et)||ct(E[6],zt))&&(ct(E[7],Et)||ct(E[7],zt))?(bt=E[4].clone(),Ct=E[5].clone()):(ct(E[7],Et)||ct(E[7],zt))&&(ct(E[4],Et)||ct(E[4],zt))&&(bt=E[5].clone(),Ct=E[6].clone());let Pt=j.distanceTo(X),et=bt.distanceTo(Ct),ht=new q.Vector3().subVectors(new q.Vector3(j.x,j.y,bt.z),j).normalize();if(Math.abs(Pt-et)>.01||Math.abs(Pt-z)>.01){console.log("\u4E0A\u4E0B\u957F\u5EA6\u4E0D\u4E00\u81F4,\u6216\u4E0E\u5BBD\u4E0D\u4E00\u81F4",Pt,et,z);continue}let nt=Math.sqrt(Pt*Pt+et*et),J=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(nt)))),le={stairStepHeight:P/J,stairStepWidth:M/J,staitWidth:z,stairLength:M,stairHeigth:P,stairStepNum:J,stairBottomPt1:j,stairBottomPt2:X,stairTopPt1:bt,stairTopPt2:Ct,vecStepLength:U,vecStepWidth:at,vecStepHeight:ht,path:l[p].path,obb:l[p].obb,maxz:l[p].maxz,points:l[p].points,rectPt1:new q.Vector3(0,0,0),rectPt2:new q.Vector3(0,0,0)};h.push(le)}}return h}let c=[];for(let l=0;l<a.length;l++)c.push(...f(a[l].lastTrajectoryResults));for(let l=0;l<c.length;l++){let h=c[l].stairTopPt1.clone(),g=c[l].stairTopPt2.clone(),x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=h.clone(),p=g.clone(),b=x.clone(),w=y.clone();m.z=p.z=b.z=w.z=0;let z=At(h,g,!0),M=At(x,y,!0),P=At(z,M),E=new q.Vector3().subVectors(z,P).normalize(),S=new q.Vector3().subVectors(M,P).normalize(),I=z.distanceTo(M),W=!1,v=!1;for(let T=0;T<c.length;T++){if(l==T)continue;let R=c[T].stairTopPt1.clone(),D=c[T].stairTopPt2.clone(),H=c[T].stairBottomPt1.clone(),V=c[T].stairBottomPt2.clone(),C=new q.Vector3().subVectors(R,D).normalize(),_=new q.Vector3().subVectors(H,V).normalize(),A=R.clone(),L=D.clone(),k=H.clone(),F=V.clone();A.z=L.z=k.z=F.z=0;let B=At(R,D,!0),O=At(H,V,!0),$=B.distanceTo(O),Z=Zt(z,M,B,O),Q=!1;if(Z&&Z.isParallel&&R.z<=x.z&&x.z-R.z<.3){Q=!0,v=!0;let j=A.addScaledVector(C.clone().negate(),100),X=L.addScaledVector(C,100),U=k.addScaledVector(_.clone().negate(),100),at=F.addScaledVector(_,100),mt=new q.Line3(j,X),St=new q.Line3(U,at),ot=mt.closestPointToPoint(P,!0,new q.Vector3),Mt=St.closestPointToPoint(P,!0,new q.Vector3),bt=new q.Vector3().subVectors(ot,P),Ct=new q.Vector3().subVectors(Mt,P),Et=bt.length(),zt=Ct.length();Ct=Ct.normalize(),bt=bt.normalize();let Pt=Xt(E,bt,.1),et=Xt(S,bt,.1),ht=Xt(S,Ct,.1),nt=Xt(E,Ct,.1);if(Pt&&Pt.sameDirection&&Et>I/2){let J=Et-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,J),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,J)}else if(et&&et.sameDirection&&Et>I/2){let J=Et-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,J),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,J)}if(ht&&ht.sameDirection&&zt>I/2){let J=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,J),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,J)}else if(nt&&nt.sameDirection&&zt>I/2){let J=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,J),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,J)}}else if(Z&&Z.isParallel&&h.z<H.z&&H.z-h.z<.2){Q=!0,W=!0;let j=A.addScaledVector(C.clone().negate(),100),X=L.addScaledVector(C,100),U=k.addScaledVector(_.clone().negate(),100),at=F.addScaledVector(_,100),mt=new q.Line3(j,X),St=new q.Line3(U,at),ot=mt.closestPointToPoint(P,!1,new q.Vector3),Mt=St.closestPointToPoint(P,!1,new q.Vector3),bt=new q.Vector3().subVectors(ot,P),Ct=new q.Vector3().subVectors(Mt,P),Et=bt.length(),zt=Ct.length();Ct=Ct.normalize(),bt=bt.normalize();let Pt=Xt(E,bt,.1),et=Xt(S,bt,.1),ht=Xt(S,Ct,.1),nt=Xt(E,Ct,.1);if(Pt&&Pt.sameDirection&&Et>I/2){let J=Et-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,J),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,J)}else if(et&&et.sameDirection&&Et>I/2){let J=Et-I/2;c[l].stairBottomPt1=c[l].stairBottomPt1.addScaledVector(S,J),c[l].stairBottomPt2=c[l].stairBottomPt2.addScaledVector(S,J)}if(ht&&ht.sameDirection&&zt>I/2){let J=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,J),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,J)}else if(nt&&nt.sameDirection&&zt>I/2){let J=zt-I/2;c[l].stairTopPt1=c[l].stairTopPt1.addScaledVector(E,J),c[l].stairTopPt2=c[l].stairTopPt2.addScaledVector(E,J)}c[l].stairTopPt1.z=c[l].stairTopPt2.z=H.z}if(Q&&(h=c[l].stairTopPt1.clone(),g=c[l].stairTopPt2.clone(),x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=h.clone(),p=g.clone(),b=x.clone(),w=y.clone(),m.z=p.z=b.z=w.z=0,z=At(h,g,!0),M=At(x,y,!0),P=At(z,M),E=new q.Vector3().subVectors(z,P).normalize(),S=new q.Vector3().subVectors(M,P).normalize(),I=z.distanceTo(M)),v&&W)break}if(v||W){let T=c[l].stairTopPt1.clone(),R=c[l].stairTopPt2.clone(),D=c[l].stairBottomPt1.clone(),H=c[l].stairBottomPt2.clone(),V=At(T,R,!0),C=At(D,H,!0),_=V.distanceTo(C),A=c[l].stairTopPt1.distanceTo(c[l].stairTopPt2),L=c[l].stairBottomPt1.distanceTo(c[l].stairBottomPt2),k=Math.sqrt(A*A+L*L),F=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(k))));c[l].stairStepNum=F,c[l].stairHeigth=c[l].stairTopPt1.z-c[l].stairBottomPt1.z,c[l].stairStepHeight=c[l].stairHeigth/c[l].stairStepNum,c[l].stairStepWidth=_/c[l].stairStepNum}}for(let l=0;l<c.length;l++){let h=c[l].stairTopPt1,g=c[l].stairTopPt2,x=c[l].stairBottomPt1.clone(),y=c[l].stairBottomPt2.clone(),m=new q.Vector3(h.x,h.y,x.z),p=c[l].vecStepWidth;for(let b=0;b<c.length;b++){if(l==b)continue;let w=c[b].stairTopPt1,z=c[b].stairTopPt2,M=c[b].stairBottomPt1.clone(),P=c[b].stairBottomPt2.clone(),E=new q.Vector3(w.x,w.y,M.z),S=c[b].vecStepWidth;if(Math.abs(h.z-M.z)<.3&&M.z>x.z&&c[l].obb.intersectsOBB(c[b].obb)){let I=ie(m,x,E,M);if(I&&I.angle>75){let W=M.distanceTo(h),v=M.distanceTo(g),T=P.distanceTo(h),R=P.distanceTo(g),D=h.clone(),H=D.clone().addScaledVector(p,c[b].staitWidth),V=new q.Vector3().subVectors(g,h).normalize(),C=h.distanceTo(g),_=D.clone().addScaledVector(V,C),A=H.clone().addScaledVector(V,C),L=D.clone().addScaledVector(V,c[b].stairLength),k=H.clone().addScaledVector(V,c[b].stairLength),F=W;(F>v||F>R)&&(F=v,D=g.clone(),H=D.clone().addScaledVector(p,c[b].staitWidth),V=new q.Vector3().subVectors(h,g).normalize(),C=h.distanceTo(g),_=D.clone().addScaledVector(V,C),A=H.clone().addScaledVector(V,C),L=D.clone().addScaledVector(V,c[b].stairLength),k=H.clone().addScaledVector(V,c[b].stairLength));let B=new q.Vector3().subVectors(L,_).normalize(),O=L.distanceTo(_);L.z=k.z=c[b].maxz,c[b].stairBottomPt1=_,c[b].stairBottomPt2=A,c[b].stairTopPt1=L,c[b].stairTopPt2=k,c[b].rectPt1=D,c[b].rectPt2=H;let $=_.distanceTo(A),Z=L.distanceTo(k),Q=Math.sqrt(O*O+c[b].stairHeigth*c[b].stairHeigth),j=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(Q))));c[b].stairStepNum=j,c[b].stairHeigth=L.z-_.z,c[b].stairStepHeight=c[b].stairHeigth/c[b].stairStepNum,c[b].stairStepWidth=O/c[b].stairStepNum,c[b].vecStepLength=V.clone().negate(),c[b].vecStepWidth=B;break}}}}return t.finalFloorMap&&jl(c,t.finalFloorMap),ql(c,i,e),c},ql=(t,e,n)=>{if(!t||t.length<=0||!e||e.length<=0)return;let o=1.5,r=0;for(let s=0;s<t.length;s++){t[s].stairBottom1BalconyRailing=[],t[s].stairBottom2BalconyRailing=[],t[s].balconyRailingHeight=0;let i=t[s].stairBottomPt1.clone(),a=t[s].stairBottomPt2.clone(),u=t[s].stairBottomPt1.clone(),d=t[s].stairBottomPt2.clone(),f=new q.Vector3().subVectors(a,i).normalize();i=i.addScaledVector(f,.05),a=a.addScaledVector(f.clone().negate(),.05);let c=[],l=[];for(let h=0;h<t[s].stairStepNum;h++){let g=[i,u];g.push(g[1].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),g.push(g[0].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth));let x=g.map(z=>[z.x,z.y]);g.push(new q.Vector3(g[3].x,g[3].y,g[3].z+o)),g.push(new q.Vector3(g[2].x,g[2].y,g[2].z+o)),g.push(new q.Vector3(g[1].x,g[1].y,g[1].z+o)),g.push(new q.Vector3(g[0].x,g[0].y,g[0].z+o));let y=ue(g),m=ze(x,{x:y.x,y:y.y,z:y.z},y.h,(y.maxz+y.minz)/2),p=[],b=!1,w=0;for(let z=0;z<e.length;z++)if(m.intersectsOBB(e[z].obb)){for(let M=0;M<e[z].points.length&&!(m.containsPoint(e[z].points[M])&&(h==0&&p.push(e[z].points[M]),e[z].points[M].z>i.z&&e[z].points[M].z-i.z<1&&(w++,w>10&&h!=0)));M++);if(w>10&&(b=!0),h!=0&&b)break}if(r==0&&(r=ue(p).maxz),b){let z=At(g[1],g[2]);z.z+=t[s].stairStepHeight,c.push(z)}g=[],g=[a,d],g.push(g[1].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),g.push(g[0].clone().addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth)),x=g.map(z=>[z.x,z.y]),g.push(new q.Vector3(g[3].x,g[3].y,g[3].z+o)),g.push(new q.Vector3(g[2].x,g[2].y,g[2].z+o)),g.push(new q.Vector3(g[1].x,g[1].y,g[1].z+o)),g.push(new q.Vector3(g[0].x,g[0].y,g[0].z+o)),y=ue(g),m=ze(x,{x:y.x,y:y.y,z:y.z},y.h,(y.maxz+y.minz)/2),p=[],b=!1;for(let z=0;z<e.length;z++)if(m.intersectsOBB(e[z].obb)){let M=0;for(let P=0;P<e[z].points.length&&!(m.containsPoint(e[z].points[P])&&(h==0&&p.push(e[z].points[P]),e[z].points[P].z>i.z&&e[z].points[P].z-i.z<1&&(M++,M>10&&h!=0)));P++);if(M>10&&(b=!0),h!=0&&b)break}if(r==0&&(r=ue(p).maxz),b){let z=At(g[1],g[2]);z.z+=t[s].stairStepHeight,l.push(z)}i=i.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),i=i.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),a=a.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),a=a.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),u=u.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),u=u.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight),d=d.addScaledVector(t[s].vecStepWidth,t[s].stairStepWidth),d=d.addScaledVector(t[s].vecStepHeight,t[s].stairStepHeight)}c.length>t[s].stairStepNum-3&&(t[s].stairBottom1BalconyRailing=c),l.length>t[s].stairStepNum-3&&(t[s].stairBottom2BalconyRailing=l),t[s].balconyRailingHeight=r}};function jl(t,e){if(!t||!e)return;let n=[];for(let o=0;o<t.length;o++){let r=t[o].stairTopPt1.clone(),s=t[o].stairTopPt2.clone(),i=t[o].stairBottomPt1.clone(),a=t[o].stairBottomPt2.clone(),u=r.clone(),d=s.clone(),f=i.clone(),c=a.clone();u.z=d.z=f.z=c.z=0;let l=At(r,s,!0),h=At(i,a,!0),g=At(l,h),x=new q.Vector3().subVectors(l,g).normalize(),y=new q.Vector3().subVectors(h,g).normalize(),m=l.distanceTo(h),p=-1,b=-1;for(let w=0;w<t.length;w++){if(o==w)continue;let z=t[w].stairTopPt1.clone(),M=t[w].stairTopPt2.clone(),P=t[w].stairBottomPt1.clone(),E=t[w].stairBottomPt2.clone(),S=new q.Vector3().subVectors(z,M).normalize(),I=new q.Vector3().subVectors(P,E).normalize(),W=z.clone(),v=M.clone(),T=P.clone(),R=E.clone();W.z=v.z=T.z=R.z=0;let D=At(z,M,!0),H=At(P,E,!0),V=D.distanceTo(H),C=Zt(l,h,D,H);if(C&&C.isParallel&&r.z<=P.z&&P.z-r.z<=.2?p=w:C&&C.isParallel&&z.z<=i.z&&i.z-z.z<=.3&&(b=w),p!=-1&&b!=-1)break}for(let w=0;w<e.length;w++){let z=e[w][1];for(let M=0;M<z.length;M++){let P=`${w}+${M}`;if(n.includes(P))continue;let E=z[M].points;if(o==0&&Math.abs(E[0].z-i.z)<.2){let S=i.clone().addScaledVector(t[o].vecStepWidth.clone().negate(),5),I=a.clone().addScaledVector(t[o].vecStepWidth.clone().negate(),5),W=new q.Line3(S,I),v=new q.Line3(i,a),T=999999,R=0,D=new q.Vector3,H=0;for(let V=0;V<E.length;V++){let C=new q.Vector3(E[V].x,E[V].y,S.z),A=W.closestPointToPoint(C,!1,new q.Vector3).distanceTo(C),L=v.closestPointToPoint(C,!1,new q.Vector3),k=new q.Vector3().subVectors(C,L).normalize(),F=Xt(k,t[o].vecStepWidth.clone().negate(),.1);A<T&&F.sameDirection&&(T=A,R=C.distanceTo(L),D=C.clone(),H=E[V].z)}T!=-999999&&(n.push(P),t[o].stairBottomPt1.z=t[o].stairBottomPt2.z=H)}else if(Math.abs(E[0].z-r.z)<.2){let S=r.clone().addScaledVector(t[o].vecStepWidth,5),I=s.clone().addScaledVector(t[o].vecStepWidth,5),W=new q.Line3(S,I),v=new q.Line3(r,s),T=0,R=999999,D=new q.Vector3,H=0;for(let V=0;V<E.length;V++){let C=new q.Vector3(E[V].x,E[V].y,S.z),_=W.closestPointToPoint(C,!1,new q.Vector3),A=v.closestPointToPoint(C,!1,new q.Vector3),L=new q.Vector3().subVectors(C,A).normalize(),k=_.distanceTo(C),F=Xt(L,t[o].vecStepWidth);k<R&&F.sameDirection&&(R=k,T=C.distanceTo(A),D=C,H=E[V].z)}if(R!=-999999){if(n.push(P),t[o].stairTopPt1.z=t[o].stairTopPt2.z=H,p!=-1){t[p].stairBottomPt1.z=t[p].stairBottomPt2.z=H;let V=[t[o].stairTopPt1.clone(),t[o].stairTopPt2.clone(),t[p].stairBottomPt1.clone(),t[p].stairBottomPt2.clone()],C=We(V);C.length==2&&(t[o].stairTopFloorPts=C,t[o].stairTopFloorPts.push(t[o].stairTopFloorPts[1].clone().addScaledVector(t[o].vecStepWidth,T)),t[o].stairTopFloorPts.push(t[o].stairTopFloorPts[0].clone().addScaledVector(t[o].vecStepWidth,T)))}else t[o].stairTopFloorPts=[t[o].stairTopPt1.clone(),t[o].stairTopPt2.clone()],t[o].stairTopFloorPts.push(t[o].stairTopPt2.clone().addScaledVector(t[o].vecStepWidth,T)),t[o].stairTopFloorPts.push(t[o].stairTopPt1.clone().addScaledVector(t[o].vecStepWidth,T));if(t[o].stairTopFloorPts)for(let V=0;V<t[o].stairTopFloorPts.length;V++)t[o].stairTopFloorPts[V].z-=t[o].stairStepHeight;break}}}}if(p!=-1){let w=t[o].stairTopPt1.clone(),z=t[o].stairTopPt2.clone(),M=t[o].stairBottomPt1.clone(),P=t[o].stairBottomPt2.clone(),E=At(w,z,!0),S=At(M,P,!0),I=E.distanceTo(S),W=t[o].stairTopPt1.distanceTo(t[o].stairTopPt2),v=t[o].stairBottomPt1.distanceTo(t[o].stairBottomPt2),T=Math.sqrt(W*W+v*v),R=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(T))));t[o].stairStepNum=R,t[o].stairHeigth=t[o].stairTopPt1.z-t[o].stairBottomPt1.z,t[o].stairStepHeight=t[o].stairHeigth/t[o].stairStepNum,t[o].stairStepWidth=I/t[o].stairStepNum;let D=t[p].stairTopPt1.clone(),H=t[p].stairTopPt2.clone(),V=t[p].stairBottomPt1.clone(),C=t[p].stairBottomPt2.clone(),_=At(D,H,!0),A=At(V,C,!0),L=_.distanceTo(A),k=t[p].stairTopPt1.distanceTo(t[p].stairTopPt2),F=t[p].stairBottomPt1.distanceTo(t[p].stairBottomPt2),B=Math.sqrt(k*k+F*F),O=Math.round(Math.max(5,Math.min(350,10*Math.sqrt(B))));t[p].stairStepNum=O,t[p].stairHeigth=t[p].stairTopPt1.z-t[p].stairBottomPt1.z,t[p].stairStepHeight=t[p].stairHeigth/t[p].stairStepNum,t[p].stairStepWidth=L/t[p].stairStepNum}}}function zs(t,e={}){let{binCount:n=80,minBinPoints:o=5,sideK:r=3,zK:s=3,minSideRange:i=.2,minZRange:a=.2,maxSearchBins:u=3,lowerZScale:d=1,upperZScale:f=1}=e;if(!t||t.length===0)return[];let c=t.map((_,A)=>({index:A,x:_.x,y:_.y,z:_.z,raw:_})),l=0,h=0;for(let _ of c)l+=_.x,h+=_.y;l/=c.length,h/=c.length;let g=0,x=0,y=0;for(let _ of c){let A=_.x-l,L=_.y-h;g+=A*A,x+=A*L,y+=L*L}g/=c.length,x/=c.length,y/=c.length;let m=g+y,p=Math.sqrt((g-y)*(g-y)+4*x*x),b=(m+p)/2,w,z;Math.abs(x)>1e-8?(w=b-y,z=x):g>=y?(w=1,z=0):(w=0,z=1);let M=Math.sqrt(w*w+z*z)||1;w/=M,z/=M;let P=-z,E=w,S=1/0,I=-1/0,W=c.map(_=>{let A=_.x-l,L=_.y-h,k=A*w+L*z,F=A*P+L*E;return S=Math.min(S,k),I=Math.max(I,k),{..._,t:k,side:F}}),T=(I-S||1)/n,R=new Map;for(let _ of W){let A=Math.floor((_.t-S)/T);A<0&&(A=0),A>=n&&(A=n-1),R.has(A)||R.set(A,[]),R.get(A).push(_)}let D=new Map;for(let[_,A]of R){if(A.length<o)continue;let L=A.map(j=>j.side).sort((j,X)=>j-X),k=A.map(j=>j.z).sort((j,X)=>j-X),F=eo(L),B=eo(k),O=eo(L.map(j=>Math.abs(j-F)).sort((j,X)=>j-X)),$=eo(k.map(j=>Math.abs(j-B)).sort((j,X)=>j-X)),Z=Math.max(i,O*1.4826*r),Q=Math.max(a,$*1.4826*s);D.set(_,{sideMedian:F,zMedian:B,sideRange:Z,zRange:Q})}function H(_){if(D.has(_))return D.get(_);for(let A=1;A<=u;A++){if(D.has(_-A))return D.get(_-A);if(D.has(_+A))return D.get(_+A)}return null}let V=[],C=[];for(let _ of W){let A=Math.floor((_.t-S)/T);A<0&&(A=0),A>=n&&(A=n-1);let L=H(A);if(!L){C.push(_.raw);continue}let k=Math.abs(_.side-L.sideMedian)<=L.sideRange,F=L.zMedian-L.zRange*d,B=L.zMedian+L.zRange*f,O=_.z>=F&&_.z<=B;k&&O?V.push(_.raw):C.push(_.raw)}return{keep:V,removed:C,debug:{meanX:l,meanY:h,dirX:w,dirY:z,sideX:P,sideY:E,minT:S,maxT:I,binSize:T,stats:D}}}function eo(t){if(t.length===0)return 0;let e=Math.floor(t.length/2);return t.length%2===0?(t[e-1]+t[e])/2:t[e]}function $l(t,e){let n=new Map;for(let o=0;o<t.length;o++){let r=t[o],s=Math.floor(r.x/e),i=Math.floor(r.y/e),a=Math.floor(r.z/e),u=`${s},${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}function Xl(t,e,n,o,r){let s=t[n],i=Math.floor(s.x/o),a=Math.floor(s.y/o),u=Math.floor(s.z/o),d=[];for(let f=-1;f<=1;f++)for(let c=-1;c<=1;c++)for(let l=-1;l<=1;l++){let h=`${i+f},${a+c},${u+l}`,g=e.get(h);if(g)for(let x of g){if(x===n)continue;let y=t[x],m=s.x-y.x,p=s.y-y.y,b=s.z-y.z;m*m+p*p+b*b<=r&&d.push(x)}}return d}function Ms(t,e={}){let{radius:n=.35,minClusterSize:o=20,lowPercent:r=.35}=e,s=Zl(t,{radius:n}),i=t.map(f=>f.z).sort((f,c)=>f-c),a=i[Math.floor(i.length*r)],u=[],d=[];for(let f of s.clusters){let c=f.map(g=>s.points[g]),l=c.reduce((g,x)=>g+x.z,0)/c.length;c.length<o&&l<=a?d.push(...c.map(g=>g.raw)):u.push(...c.map(g=>g.raw))}return{keep:u,removed:d}}function Zl(t,e={}){let{radius:n=.35}=e,o=t.map((u,d)=>({index:d,x:u.x,y:u.y,z:u.z,raw:u})),r=$l(o,n),s=n*n,i=new Array(o.length).fill(!1),a=[];for(let u=0;u<o.length;u++){if(i[u])continue;let d=[],f=[u];for(i[u]=!0;f.length>0;){let c=f.shift();d.push(c);let l=Xl(o,r,c,n,s);for(let h of l)i[h]||(i[h]=!0,f.push(h))}a.push(d)}return{points:o,clusters:a}}function Gl(t,e,n=1e-6){let o=Yl(e);for(let r of o)if(!Ul(t,r,n))return!1;return!0}function Yl(t){let e=t.center,n=t.halfSize,o=t.rotation.elements,r=new q.Vector3(o[0],o[1],o[2]).multiplyScalar(n.x),s=new q.Vector3(o[3],o[4],o[5]).multiplyScalar(n.y),i=new q.Vector3(o[6],o[7],o[8]).multiplyScalar(n.z),a=[];for(let u of[-1,1])for(let d of[-1,1])for(let f of[-1,1]){let c=e.clone().addScaledVector(r,u).addScaledVector(s,d).addScaledVector(i,f);a.push(c)}return a}function Ul(t,e,n=1e-6){let o=new q.Vector3().subVectors(e,t.center),r=t.rotation.elements,s=t.halfSize,i=new q.Vector3(r[0],r[1],r[2]),a=new q.Vector3(r[3],r[4],r[5]),u=new q.Vector3(r[6],r[7],r[8]),d=o.dot(i),f=o.dot(a),c=o.dot(u);return Math.abs(d)<=s.x+n&&Math.abs(f)<=s.y+n&&Math.abs(c)<=s.z+n}function Kl(t,e){if(!t||t.length===0)return[];let n=new Map;for(let i=0;i<t.length;i++){let a=t[i],u=a[0],d=a[1],f=Math.floor(u/e),c=Math.floor(d/e),l=`${f}_${c}`,h=n.get(l);if(!h){let m=(f+.5)*e,p=(c+.5)*e,b=u-m,w=d-p;h={minX:a,maxX:a,minY:a,maxY:a,center:a,centerDist2:b*b+w*w,cx:m,cy:p},n.set(l,h);continue}u<h.minX[0]&&(h.minX=a),u>h.maxX[0]&&(h.maxX=a),d<h.minY[1]&&(h.minY=a),d>h.maxY[1]&&(h.maxY=a);let g=u-h.cx,x=d-h.cy,y=g*g+x*x;y<h.centerDist2&&(h.center=a,h.centerDist2=y)}let o=new Set,r=[];function s(i){let a=`${i[0]}_${i[1]}`;o.has(a)||(o.add(a),r.push(i))}for(let i of n.values())s(i.minX),s(i.maxX),s(i.minY),s(i.maxY),s(i.center);return r}function Ql(t,e,n,o){return 1+(t[e][0]===0||t[n][0]===0||t[o][0]===0?1:0)}function Jl(t,e,n,o,r,s){let i=Math.hypot(n-r,o-s),a=Math.hypot(t-r,e-s),u=Math.hypot(t-n,e-o),d=Math.abs((n-t)*(s-e)-(r-t)*(o-e))/2;return d===0?1/0:i*a*u/(4*d)}function ta(t){if(!t||t.length<3)return[];let e=new Set,n=[];for(let g of t){let x=`${g[0]}_${g[1]}`;e.has(x)||(e.add(x),n.push(g))}if(n.length<3)return[];let o=new Ae(Float64Array.from(n.flat())),{triangles:r}=o;if(!r.length)return[];let s=new Map,i=(g,x)=>g<x?`${g}_${x}`:`${x}_${g}`;for(let g=0;g<r.length;g+=3){let x=r[g],y=r[g+1],m=r[g+2],[p,b]=n[x],[w,z]=n[y],[M,P]=n[m],E=Jl(p,b,w,z,M,P),S=Ql(n,x,y,m);if(E<=1/S)for(let[I,W]of[[x,y],[y,m],[m,x]]){let v=i(I,W);s.set(v,(s.get(v)||0)+1)}}let a=[];for(let[g,x]of s)x===1&&a.push(g.split("_").map(Number));if(!a.length)return[];let u=new Map;for(let[g,x]of a)(u.get(g)||u.set(g,[]).get(g)).push(x),(u.get(x)||u.set(x,[]).get(x)).push(g);let d=a[0][0],f=[],c=new Set,l=d,h=-1;do{f.push(n[l]),c.add(l);let g=u.get(l)||[],x=g.find(y=>y!==h&&!c.has(y));if(x===void 0&&(x=g.find(y=>y!==h)),x===void 0)break;h=l,l=x}while(l!==d&&f.length<=a.length+1);return f.length>0&&f.push(f[0]),f}import*as Fo from"three";var ea=(t,e,n=null)=>{if(t.length===0)return;let o=[],r=new Fo.Vector3(e[0].x,e[0].y,e[0].z),s=0;for(let f=1;f<e.length;f++)s+=e[f].z,e[f].z>r.z&&r.set(e[f].x,e[f].y,e[f].z);let i=s/(e.length-1)+.3,a=0,u=0;for(let f of t){let c=f[1].points;if(c.length<30)continue;let l=new Map;for(let x=0;x<c.length;x++){let y=c[x].z;if(y=Math.floor(y*10)/10,l.has(y)){let m=l.get(y);m.num+=1,m.points.push(c[x]),l.set(y,m)}else l.set(y,{num:1,points:[c[x]]})}c=[...l.entries()].reduce((x,y)=>y[1].num>x[1].num?y:x)[1].points.map(x=>new Fo.Vector3(x.x,x.y,x.z));let g=[];if(n!=null){let x=n([c],{tolerance:.1,minClusterSize:30,maxClusterSize:1e7,threadCount:1});if(x.length>0)for(let y of x[0]){if(y.length<50)continue;let m=y.map(p=>c[p]);g.push(m)}else g.push(c)}else g.push(c);for(let x=0;x<g.length;x++){let{minZ:y,maxZ:m,averagePz:p}=Ve(g[x]),b=m-y;if(i>m)continue;let w=g[x].map(E=>[E.x,E.y]),z=me(w);if(zr(z)<.5)continue;let P={contour:z,averagePz:p};g[x].length>a&&(a=g[x].length,u=p),o.push(P)}}let d=[];for(let f of o)(f.averagePz>=u||f.averagePz>=i-.5)&&d.push(f);return d};function qe(t,e){let n=t.x-e.x,o=t.y-e.y;return Math.sqrt(n*n+o*o)}function je(t){let e=0,n=0,o=0;for(let r=0;r<4;r++){let s=t[r],i=t[(r+1)%4],a=i.x-s.x,u=i.y-s.y,d=Math.sqrt(a*a+u*u);d>e&&(e=d,n=a/d,o=u/d)}return{longSide:e,dirX:n,dirY:o}}function $e(t,e,n){let r=[];for(let s of t){let i=(s.start.x+s.end.x)/2,a=(s.start.y+s.end.y)/2,u=i-e,d=a-n;u*u+d*d<=9&&r.push(s.start.z,s.end.z)}if(r.length===0){let s=[];for(let i of t)s.push(i.start.z,i.end.z);return s.length?(s.sort((i,a)=>i-a),s[Math.floor(s.length/2)]):-1/0}return r.sort((s,i)=>s-i),r[Math.floor(r.length/2)]}function Ce(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-12)return{dist:Math.sqrt((t-n)**2+(e-o)**2),t:0};let d=t-n,f=e-o,c=(d*i+f*a)/u,l=n+c*i,h=o+c*a;return{dist:Math.sqrt((t-l)**2+(e-h)**2),t:c}}function No(t,e,n){let o=0;for(let r=0;r<4;r++){let s=n[r],i=n[(r+1)%4],a=(i.x-s.x)*(e-s.y)-(i.y-s.y)*(t-s.x);if(Math.abs(a)<1e-12)continue;let u=a>0?1:-1;if(o===0)o=u;else if(o!==u)return!1}return!0}function Es(t,e,n,o,r,s,i,a){let u=n-t,d=o-e,f=i-r,c=a-s,l=u*c-d*f;if(Math.abs(l)<1e-12)return null;let h=((r-t)*c-(s-e)*f)/l,g=((r-t)*d-(s-e)*u)/l;return h<-1e-12||h>1+1e-12||g<-1e-12||g>1+1e-12?null:Math.max(0,Math.min(1,h))}function Ps(t,e,n,o,r){let s=[];No(t,e,r)&&s.push(0),No(n,o,r)&&s.push(1);for(let u=0;u<4;u++){let d=r[u],f=r[(u+1)%4],c=Es(t,e,n,o,d.x,d.y,f.x,f.y);c!==null&&s.push(c)}if(s.length<2)return 0;s.sort((u,d)=>u-d);let i=Math.sqrt((n-t)**2+(o-e)**2),a=0;for(let u=0;u<s.length-1;u++){let d=s[u],f=s[u+1],c=(d+f)/2,l=t+c*(n-t),h=e+c*(o-e);No(l,h,r)&&(a+=(f-d)*i)}return a}function Ts(t,e,n=1e-6){let o=[],r=t.start.x,s=t.start.y,i=t.end.x,a=t.end.y;for(let f of e){if(f===t)continue;let c=Es(r,s,i,a,f.start.x,f.start.y,f.end.x,f.end.y);c!==null&&c>n&&c<1-n&&o.push(c)}o.sort((f,c)=>f-c);let u=[],d=-1/0;for(let f of o)f-d>n&&(u.push(f),d=f);return u}function Ss(t,e){if(!e.length)return[t];let n=[],o=0;for(let s of e){let i=t.start.x+s*(t.end.x-t.start.x),a=t.start.y+s*(t.end.y-t.start.y),u=t.start.z+s*(t.end.z-t.start.z),d=o===0?{...t.start}:{x:t.start.x+o*(t.end.x-t.start.x),y:t.start.y+o*(t.end.y-t.start.y),z:t.start.z+o*(t.end.z-t.start.z)},f={...t,start:d,end:{x:i,y:a,z:u},length:t.length*(s-o)};f.drawWindow=void 0,f.isWindow=!1,n.push(f),o=s}let r={...t,start:{x:t.start.x+o*(t.end.x-t.start.x),y:t.start.y+o*(t.end.y-t.start.y),z:t.start.z+o*(t.end.z-t.start.z)},end:{...t.end},length:t.length*(1-o)};return r.drawWindow=void 0,r.isWindow=!1,n.push(r),n}function na(t,e,n=.15){let o=0,r=0;for(let i of e)i!==t&&((qe(t.start,i.start)<=n||qe(t.start,i.end)<=n)&&o++,(qe(t.end,i.start)<=n||qe(t.end,i.end)<=n)&&r++);let s=0;return o>=1&&r>=1?s=5:(o>=1||r>=1)&&(s=2),{left:o,right:r,score:s}}function oa(t,e){let n=[],{start:o,end:r}=t;for(let a of e){let{dist:u}=Ce(a.x,a.y,o.x,o.y,r.x,r.y);n.push(u)}let s=n.reduce((a,u)=>a+u,0)/n.length,i=n.reduce((a,u)=>a+(u-s)**2,0)/n.length;return i<.01?3:i<.05?2:i<.1?1:0}function ra(t,e,n=.15){let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=Math.sqrt(o*o+r*r);if(s<1e-12)return 0;let i=-r/s,a=o/s,u=[[],[]];for(let c=0;c<2;c++){let l=c===0?t.start:t.end;for(let h of e){if(h===t)continue;let g=qe(l,h.start),x=qe(l,h.end),y=null;if(g<=n?y=h.end:x<=n&&(y=h.start),!y)continue;let m=h.end.x-h.start.x,p=h.end.y-h.start.y,b=Math.sqrt(m*m+p*p);if(b<1e-12||Math.abs(o*m+r*p)/(s*b)>=.342)continue;let z=(y.x-l.x)*i+(y.y-l.y)*a;u[c].push(z>0?1:-1)}}if(u[0].length===0||u[1].length===0)return 0;let d=[...u[0],...u[1]];return d.every(c=>c===d[0])?2:1}function Rs(t,e,n){let o=na(t,n),r=oa(t,e),s=ra(t,n);return{score:o.score*3+r*2+s*2,details:{conn:o.score,variance:r,sameSide:s}}}function sa(t){let e=t.end.x-t.start.x,n=t.end.y-t.start.y,o=Math.atan2(n,e)*(180/Math.PI);return o<0&&(o+=180),o}function ia(t,e){let n=[];for(let o of t){let r=!1;for(let s of n){let i=Math.abs(o.angle-s[0].angle);if(Math.min(i,180-i)<=e){s.push(o),r=!0;break}}r||n.push([o])}return n}function la(t,e=.05){let n=[];for(let o of t){let r=!1;for(let s of n)if(Math.abs(o.offset-s[0].offset)<=e){s.push(o),r=!0;break}r||n.push([o])}return n}function aa(t,e,n,o,r){let s=[];for(let d of t){let f=d.seg,c=(f.start.x-e.start.x)*n+(f.start.y-e.start.y)*o,l=(f.end.x-e.start.x)*n+(f.end.y-e.start.y)*o;c<=l?s.push({l:c,r:l,idx:d.idx,startPt:f.start,endPt:f.end}):s.push({l,r:c,idx:d.idx,startPt:f.end,endPt:f.start})}s.sort((d,f)=>d.l-f.l);let i=[],a=.2,u=.5;for(let{l:d,r:f,idx:c,startPt:l,endPt:h}of s){if(i.length===0){i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h});continue}let g=i[i.length-1];if(d<=g.r+r){let x=Math.min(g.r,f)-Math.max(g.l,d),y=Math.hypot(g.endPt.x-l.x,g.endPt.y-l.y);x>=u||y<=a?(d<g.l&&(g.l=d,g.startPt=l),f>g.r&&(g.r=f,g.endPt=h),g.srcIdxs.includes(c)||g.srcIdxs.push(c)):i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}else i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}return i}function ca(t,e,n,o,r,s,i){let a=Math.hypot(n.x-e.x,n.y-e.y),u={...t};return u.start={...e},u.end={...n},u.length=a,u.direction={x:o,y:r,z:0},s!==void 0&&(u.rooftopPz=s),u.buildRosource=i,u.isRebuild=!1,u.doorAndBeamData||(u.doorAndBeamData=[]),u.insetionArr||(u.insetionArr=[]),u}function ua(t,e){let n=!0;for(;n;){n=!1;for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o],i=t[r];if(s.rooftopPz!==void 0&&i.rooftopPz!==void 0&&Math.abs(s.rooftopPz-i.rooftopPz)>.05)continue;let a=Math.abs(s.end.x-i.start.x)<.001&&Math.abs(s.end.y-i.start.y)<.001,u=Math.abs(s.start.x-i.end.x)<.001&&Math.abs(s.start.y-i.end.y)<.001,d=Math.abs(s.end.x-i.end.x)<.001&&Math.abs(s.end.y-i.end.y)<.001,f=Math.abs(s.start.x-i.start.x)<.001&&Math.abs(s.start.y-i.start.y)<.001;if(!(a||u||d||f)||s.direction.x*i.direction.x+s.direction.y*i.direction.y<.98)continue;let l=s.start,h=s.end;a?h=i.end:u?l=i.start:d?h=i.start:f&&(l=i.end);let g=Math.hypot(h.x-l.x,h.y-l.y),x={...s};x.start=l,x.end=h,x.length=g,x.direction={x:(h.x-l.x)/g,y:(h.y-l.y)/g,z:0},s.rooftopPz!==void 0&&i.rooftopPz!==void 0?x.rooftopPz=Math.max(s.rooftopPz,i.rooftopPz):x.rooftopPz=s.rooftopPz!==void 0?s.rooftopPz:i.rooftopPz,x.buildRosource=s.buildRosource||i.buildRosource,x.isRebuild=!1,x.doorAndBeamData||(x.doorAndBeamData=[]),x.insetionArr||(x.insetionArr=[]),t[o]=x,e[o]=[...new Set([...e[o],...e[r]])],t.splice(r,1),e.splice(r,1),r--,n=!0}}return{segments:t,sourceMap:e}}function Xe(t,e=5,n=.05){if(t.length<=1)return{segments:t,sourceMap:t.map((u,d)=>[d])};let o=t.map((u,d)=>({seg:u,angle:sa(u),idx:d}));o.sort((u,d)=>u.angle-d.angle);let r=ia(o,e),s=[],i=[],a=[];for(let u of r){let d=u[0].seg;for(let m of u)m.seg.length>d.length&&(d=m.seg);let f=d.end.x-d.start.x,c=d.end.y-d.start.y,l=Math.sqrt(f*f+c*c);if(l<1e-12)continue;let h=-c/l,g=f/l,x=u.map(m=>{let p=m.seg.start.x-d.start.x,b=m.seg.start.y-d.start.y;return{seg:m.seg,offset:Math.abs(p*h+b*g),idx:m.idx}});x.sort((m,p)=>m.offset-p.offset);let y=la(x);for(let m of y){let p=m[0].seg;for(let T of m)T.seg.length>p.length&&(p=T.seg);let b=p.end.x-p.start.x,w=p.end.y-p.start.y,z=Math.sqrt(b*b+w*w);if(z<1e-12)continue;let M=b/z,P=w/z,E=m.map(T=>T.seg.rooftopPz).filter(T=>T!==void 0),S=E.length>0?Math.max(...E):void 0,I=[...new Set(E.map(T=>T.toFixed(3)))];if(I.length>1){let T=m.map(R=>({idx:R.idx,start:{x:R.seg.start.x,y:R.seg.start.y,z:R.seg.start.z},end:{x:R.seg.end.x,y:R.seg.end.y,z:R.seg.end.z},length:R.seg.length,rooftopPz:R.seg.rooftopPz}));a.push({values:[...I],segments:T})}let W=[...new Set(m.map(T=>String(T.seg.buildRosource)))],v=aa(m,p,M,P,n);for(let{l:T,r:R,srcIdxs:D,startPt:H,endPt:V}of v){if(R-T<.01)continue;let C=ca(p,H,V,M,P,S,p.buildRosource);s.push(C),i.push(D)}}}return{...ua(s,i),debugRPZGroups:a}}function fa(t){return{minLengthRatio:.95,maxDistance:.5,angleThreshold:45,projectionMargin:.1,printOnly:!0,minEmbedRatioThreshold:.3,noWallSurroundDistance:.3,minEmbedRatio:.05,...t}}function da(t,e,n){return[{x:e,y:n,z:0},{x:t[0].x,y:t[0].y,z:0},{x:t[1].x,y:t[1].y,z:0},{x:t[2].x,y:t[2].y,z:0},{x:t[3].x,y:t[3].y,z:0}]}function ha(t,e,n,o,r,s,i,a,u){if(!["1527725434500","1527725386500","1527726772000","1527725058500"].some(c=>t.includes(c)))return;e.map(c=>{let{start:l,end:h}=c,g=Ce(n,o,l.x,l.y,h.x,h.y),x=r.map(z=>{let M=Ce(z.x,z.y,l.x,l.y,h.x,h.y);return`t=${M.t.toFixed(3)},d=${M.dist.toFixed(3)}`}),y=h.x-l.x,m=h.y-l.y,p=Math.sqrt(y*y+m*m)||1,b=Math.abs(i*(y/p)+a*(m/p)),w=Math.acos(Math.min(b,1))*(180/Math.PI);return{seg:c,lineDist:g.dist,t:g.t,pts:x,angle:w,length:c.length}}).sort((c,l)=>c.lineDist-l.lineDist)}function ma(t,e,n,o,r,s,i){let a=[{margin:s.projectionMargin,maxDist:s.maxDistance,label:"\u4E25\u683C"},{margin:.4,maxDist:.65,label:"\u5BBD\u677E"}],u=[];for(let d of a){if(u.length>0)break;for(let f of t){let c=!1,l=!1,h=!1,g=e*s.minLengthRatio;if(f.length<g){continue}c=!0;let{start:x,end:y}=f,m=!1,p=1/0;for(let v of r){let{dist:T,t:R}=Ce(v.x,v.y,x.x,x.y,y.x,y.y);R>=-d.margin&&R<=1+d.margin&&T<=d.maxDist&&(m=!0,p=Math.min(p,T))}if(!m){`${d.maxDist}${(-d.margin).toFixed(1)}${(1+d.margin).toFixed(1)}`;continue}l=!0;let b=Ce(r[0].x,r[0].y,x.x,x.y,y.x,y.y);if(b.t<0||b.t>1){`${b.t.toFixed(3)}`;continue}let w=Math.sqrt((y.x-x.x)**2+(y.y-x.y)**2);if(w<1e-12)continue;let z=(y.x-x.x)/w,M=(y.y-x.y)/w,P=Math.abs(n*z+o*M),E=Math.acos(Math.min(P,1))*(180/Math.PI);if(E>s.angleThreshold){`${E.toFixed(1)}${s.angleThreshold}`;continue}h=!0;let S=[r[1],r[2],r[3],r[4]],I=Ps(x.x,x.y,y.x,y.y,S),W=e>0?I/e:0;u.push({seg:f,minDist:p,embedRatio:W,ratio:f.length/e,score:0,details:{conn:0,variance:0,sameSide:0}})}}return u}function pa(t,e,n,o){if(t.length<=1)return t.length===1&&(t[0].score=-1),t;for(let r of t){let{score:s,details:i}=Rs(r.seg,e,n);r.score=s,r.details=i}return t.sort((r,s)=>s.embedRatio-r.embedRatio||s.score-r.score||r.minDist-s.minDist),t}function ga(t,e,n,o){let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=Math.sqrt(r*r+s*s),a=r/i,u=s/i,d=e.map(P=>{let E=P.x-t.start.x,S=P.y-t.start.y;return(E*a+S*u)/i}),f=Math.min(...d),c=Math.max(...d),l=(c-f)*i,h=(f+c)/2,g=t.start.x+h*r,x=t.start.y+h*s,y=n.box.min.z,m=n.box.max.z,p=Math.abs(m-y),b=$e(o,t.start.x,t.start.y),w=Math.max(0,y-b),z=f<=0&&c>=1,M=t.start.z+h*(t.end.z-t.start.z);return{p:{x:g,y:x,z:M},width:l,full:z,height:p,groundClearance:w}}function qo(t,e,n){let o=e.map(f=>n[f]),r=[],s=[],i=[],a=[],u,d;for(let f of o){if(f.originalPoints?.length)for(let c of f.originalPoints)r.push(c);if(f.doorAndBeamData?.length)for(let c of f.doorAndBeamData)s.push(c);if(f.insetionArr?.length)for(let c of f.insetionArr)i.push(c);if(f.drawWindow?.length)for(let c of f.drawWindow)a.push(c);f.boxData&&!u&&(u=f.boxData),f.rooftopPz!==void 0&&(d===void 0||f.rooftopPz>d)&&(d=f.rooftopPz)}r.length&&(t.originalPoints=r),s.length&&(t.doorAndBeamData=s),i.length&&(t.insetionArr=i),a.length&&(t.drawWindow=a),u&&(t.boxData=u),d!==void 0&&(t.rooftopPz=d)}function Tn(t,e,n=10,o=.2,r=.2){let s=[];if(!t.drawWindow)return s;let i=t.drawWindow.p,a=t.wallSegment,u=a.end.x-a.start.x,d=a.end.y-a.start.y,f=Math.sqrt(u*u+d*d)||1,c=u/f,l=d/f,h=i.x-a.start.x,g=i.y-a.start.y,x=(h*c+g*l)/f;for(let y of e){if(y===a)continue;let m=y.end.x-y.start.x,p=y.end.y-y.start.y,b=Math.sqrt(m*m+p*p)||1,w=m/b,z=p/b,M=Math.abs(c*w+l*z),E=Math.acos(Math.min(M,1))*(180/Math.PI);if(Math.abs(E-90)>n)continue;let S=[y.start,y.end];for(let I of S){let W=I.x-a.start.x,v=I.y-a.start.y,T=(W*c+v*l)/f,R=a.start.x+T*f*c,D=a.start.y+T*f*l;if(Math.sqrt((I.x-R)**2+(I.y-D)**2)>r)continue;let V=Math.abs((T-x)*f);if(V<=o){s.push({seg:y,distAlongWall:V,angle:E,endPoint:I});break}}}return s}function Sn(t,e){let n=t.start,o=t.end,r=o.x-n.x,s=o.y-n.y,i=Math.sqrt(r*r+s*s)||1,a=((e.x-n.x)*r+(e.y-n.y)*s)/(i*i),u=Math.max(0,Math.min(1,a)),d={x:n.x+u*r,y:n.y+u*s,z:n.z+(o.z-n.z)*u||0};function f(b){if(!b||!b.length)return[[],[]];let w=[],z=[];for(let M of b){let P=((M.x-n.x)*r+(M.y-n.y)*s)/(i*i);P<=u+1e-8&&w.push(M),P>=u-1e-8&&z.push(M)}return(w.length===0||c(w[w.length-1],d)>1e-6)&&w.push({...d}),(z.length===0||c(z[0],d)>1e-6)&&z.unshift({...d}),[w,z]}function c(b,w){return Math.sqrt((b.x-w.x)**2+(b.y-w.y)**2)}let[l,h]=f(t.originalPoints),g=Math.max(u*i,1e-6),x=Math.max((1-u)*i,1e-6),y=t.direction||{x:r/i,y:s/i,z:(o.z-n.z)/i||0},m={...t,start:{...n},end:{...d},length:g,direction:{...y},originalPoints:l.length?l:void 0},p={...t,start:{...d},end:{...o},length:x,direction:{...y},originalPoints:h.length?h:void 0};if(t.drawWindow&&t.drawWindow.length>0){let b=[],w=[];for(let z of t.drawWindow){let M=((z.p.x-n.x)*r+(z.p.y-n.y)*s)/(i*i);M<=u+1e-8&&b.push(z),M>=u-1e-8&&w.push(z)}m.drawWindow=b.length?b:void 0,p.drawWindow=w.length?w:void 0}else m.drawWindow=void 0,p.drawWindow=void 0;if(t.boxData){let b=w=>{if(!w.length)return;let z=1/0,M=1/0,P=1/0,E=-1/0,S=-1/0,I=-1/0;for(let W of w)W.x<z&&(z=W.x),W.y<M&&(M=W.y),W.z!==void 0&&W.z<P&&(P=W.z),W.x>E&&(E=W.x),W.y>S&&(S=W.y),W.z!==void 0&&W.z>I&&(I=W.z);return{isBox3:!0,min:{x:z,y:M,z:P},max:{x:E,y:S,z:I}}};m.boxData=b(l),p.boxData=b(h)}return[m,p]}function Rn(t,e,n,o){let r=fa(n),s=[],{segments:i,sourceMap:a}=o??Xe(e),u=i;for(let d of t){let f=d.coordinatesByArea?.coordinates;if(!f||f.length<4)continue;let c=f.slice(0,4),l=c.reduce((I,W)=>I+W.x,0)/4,h=c.reduce((I,W)=>I+W.y,0)/4,{longSide:g,dirX:x,dirY:y}=je(c);if(g<.01)continue;let m=c.reduce((I,W)=>I+W.z,0)/4,p=$e(e,l,h);if(m<p-.3)continue;let b=da(c,l,h);ha(d.name,u,l,h,b,g,x,y,r);let w=ma(u,g,x,y,b,r,d.name);if(w.length>1&&r.minEmbedRatioThreshold>0){let I=Math.max(...w.map(W=>W.embedRatio));if(I>0){let W=w.length;w=w.filter(v=>v.embedRatio>=I*r.minEmbedRatioThreshold)}}if(w.length>0&&r.minEmbedRatio>0){let I=w.length;w=w.filter(W=>W.embedRatio>=r.minEmbedRatio)}w=pa(w,b,e,d.name);let z=null,M=1/0,P=0,E=0;if(w.length>0){let I=w[0];z=I.seg,M=I.minDist,P=I.ratio,E=I.embedRatio}if(z&&E<=0&&r.noWallSurroundDistance>0){let I=r.noWallSurroundDistance,W=c.map(C=>C.x),v=c.map(C=>C.y),T=Math.min(...W)-I,R=Math.max(...W)+I,D=Math.min(...v)-I,H=Math.max(...v)+I,V=!1;for(let C of u){let _=Math.min(C.start.x,C.end.x),A=Math.max(C.start.x,C.end.x),L=Math.min(C.start.y,C.end.y),k=Math.max(C.start.y,C.end.y);if(!(A<T||_>R)&&!(k<D||L>H)){V=!0;break}}V||(z=null)}let S;if(z&&(S=ga(z,c,d,i),!r.printOnly)){let I=z;I.drawWindow||(I.drawWindow=[]),I.drawWindow.push(S),I.isWindow=!0}if(z){let I={windowName:d.name,windowCategory:d.category,windowCenter:d.center,wallSegment:z,distance:M,wallLengthRatio:P,drawWindow:S};s.push(I)}}for(let d of s){let f=d.wallSegment,c=i.indexOf(f);if(c===-1)continue;let l=a[c];!l||l.length<=1||qo(f,l,e)}return s}function jo(t,e,n){let o={printOnly:!1,skipMerge:!1,...n};for(let y of t)y.drawWindow&&(y.drawWindow=void 0),y.isWindow&&(y.isWindow=void 0);let r=Array.isArray(e)?e.filter(y=>y&&y.category==="window"&&!y.isBayWindowObj):[],s,i;if(o.skipMerge)s=t,i=t.map((y,m)=>[m]);else{let y=Xe(t);s=y.segments,i=y.sourceMap}let a=Rn(r,t,o,{segments:s,sourceMap:i}),u=new Set;for(let y of a){let m=s.indexOf(y.wallSegment);m>=0&&u.add(m)}let d=[],f=new Set;for(let y of u){let m=i[y];if(m){qo(s[y],m,t);for(let p of m)f.add(p)}d.push(s[y])}for(let y=0;y<t.length;y++)f.has(y)||d.push(t[y]);let c=new Set(a.map(y=>y.windowName));for(let y of e)y&&y.category==="window"&&!y.isBayWindowObj&&(c.has(y.name)?delete y.AbnormalWindow:y.AbnormalWindow=!0);let l=new Map;for(let y of e)y&&y.name&&l.set(y.name,y);let h=new Map;for(let y of a){let m=y.wallSegment;h.has(m)||h.set(m,[]),h.get(m).push(y)}let g=new Map,x=d;for(let[y,m]of h){if(!y.drawWindow||!y.drawWindow.length||g.has(y))continue;let p=y.length,b=y.end.x-y.start.x,w=y.end.y-y.start.y,z=[];for(let T of m){if(!T.drawWindow)continue;let R=T.drawWindow.width/2,D=((T.drawWindow.p.x-y.start.x)*b+(T.drawWindow.p.y-y.start.y)*w)/(p*p),H=R/p;z.push({left:D-H,right:D+H,match:T})}if(z.length===0)continue;let M=Ts(y,x),P=1e-6,E=new Set,S=!1;for(let T of z){let R=!1,D=!1,H=null;for(let C of M)C<T.left-P&&((H===null||C>H)&&(H=C),R=!0);let V=null;for(let C of M)C>T.right+P&&((V===null||C<V)&&(V=C),D=!0);H!==null&&(E.add(H),S=!0),V!==null&&(E.add(V),S=!0)}if(!S)continue;let I=!1,W=null,v=null;for(let T of z){let R=T.match;if(!R.drawWindow||R.drawWindow.width<=1)continue;let D=l.get(R.windowName);if(!D||!D.positionArr||!D.positionArr.length)continue;let H=Tn(R,s);if(H.length<=1)continue;let V=D.positionArr[0],C=R.drawWindow.p,_=V.x-C.x,A=V.y-C.y,L=Math.sqrt(_*_+A*A);if(L<.001)continue;let k=_/L,F=A/L,B=H.filter(O=>{let $=(O.seg.start.x+O.seg.end.x)/2,Z=(O.seg.start.y+O.seg.end.y)/2,Q=$-C.x,j=Z-C.y;return Q*k+j*F>0});if(B.length){I=!0,W=B[0].endPoint,v=R;break}}if(I&&W&&v){let[T,R]=Sn(y,W);g.set(y,[T,R]);let D=((v.drawWindow.p.x-y.start.x)*b+(v.drawWindow.p.y-y.start.y)*w)/(p*p),H=((W.x-y.start.x)*b+(W.y-y.start.y)*w)/(p*p);D<=H?v.wallSegment=T:v.wallSegment=R}else{let T=Array.from(E).sort((D,H)=>D-H),R=Ss(y,T);for(let D of z){let H=(D.left+D.right)/2,V=0;for(let C of R){let _=C.length/p;if(H>=V-P&&H<=V+_+P){let A=D.match.drawWindow;A&&(C.drawWindow||(C.drawWindow=[]),C.drawWindow.push(A),C.isWindow=!0),D.match.wallSegment=C;break}V+=_}}g.set(y,R)}}if(g.size>0){let y=[];for(let m of d)g.has(m)?y.push(...g.get(m)):y.push(m);for(let m of y)m.isWindow===void 0&&(m.isWindow=!!(m.drawWindow&&m.drawWindow.length>0));return{segments:y,matches:a}}for(let y of d)y.isWindow=!!(y.drawWindow&&y.drawWindow.length>0);return{segments:d,matches:a}}function Ze(t,e){let n=t.x-e.x,o=t.y-e.y;return Math.sqrt(n*n+o*o)}function no(t){let e=0,n=1/0,o=0,r=0;for(let s=0;s<4;s++){let i=t[s],a=t[(s+1)%4],u=a.x-i.x,d=a.y-i.y,f=Math.sqrt(u*u+d*d);f>e&&(e=f,o=u/f,r=d/f),f<n&&(n=f)}return{longSide:e,shortSide:n===1/0?e:n,dirX:o,dirY:r}}function Ge(t,e,n){let r=[];for(let s of t){let i=(s.start.x+s.end.x)/2,a=(s.start.y+s.end.y)/2,u=i-e,d=a-n;u*u+d*d<=9&&r.push(s.start.z,s.end.z)}if(r.length===0){let s=[];for(let i of t)s.push(i.start.z,i.end.z);return s.length?(s.sort((i,a)=>i-a),s[Math.floor(s.length/2)]):-1/0}return r.sort((s,i)=>s-i),r[Math.floor(r.length/2)]}function jt(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-12)return{dist:Math.sqrt((t-n)**2+(e-o)**2),t:0};let d=t-n,f=e-o,c=(d*i+f*a)/u,l=n+c*i,h=o+c*a;return{dist:Math.sqrt((t-l)**2+(e-h)**2),t:c}}function $o(t,e,n){let o=0;for(let r=0;r<4;r++){let s=n[r],i=n[(r+1)%4],a=(i.x-s.x)*(e-s.y)-(i.y-s.y)*(t-s.x);if(Math.abs(a)<1e-12)continue;let u=a>0?1:-1;if(o===0)o=u;else if(o!==u)return!1}return!0}function xa(t,e,n,o,r,s,i,a){let u=n-t,d=o-e,f=i-r,c=a-s,l=u*c-d*f;if(Math.abs(l)<1e-12)return null;let h=((r-t)*c-(s-e)*f)/l,g=((r-t)*d-(s-e)*u)/l;return h<-1e-12||h>1+1e-12||g<-1e-12||g>1+1e-12?null:Math.max(0,Math.min(1,h))}function vs(t,e,n,o,r){let s=[];$o(t,e,r)&&s.push(0),$o(n,o,r)&&s.push(1);for(let u=0;u<4;u++){let d=r[u],f=r[(u+1)%4],c=xa(t,e,n,o,d.x,d.y,f.x,f.y);c!==null&&s.push(c)}if(s.length<2)return 0;s.sort((u,d)=>u-d);let i=Math.sqrt((n-t)**2+(o-e)**2),a=0;for(let u=0;u<s.length-1;u++){let d=s[u],f=s[u+1],c=(d+f)/2,l=t+c*(n-t),h=e+c*(o-e);$o(l,h,r)&&(a+=(f-d)*i)}return a}function Vs(t){if(!t||t.length<4)return[];let e=[...t].sort((i,a)=>i.z-a.z),n=e.slice(0,2),o=e.slice(2,4),r=[],s=new Set;for(let i of n){let a=-1,u=1/0;for(let d=0;d<o.length;d++){if(s.has(d))continue;let f=o[d].x-i.x,c=o[d].y-i.y,l=Math.sqrt(f*f+c*c);l<u&&(u=l,a=d)}if(a>=0){s.add(a);let d=o[a];r.push({bottom:i,top:d,midX:(i.x+d.x)/2,midY:(i.y+d.y)/2,height:Math.abs(d.z-i.z)})}}return r}function Ds(t,e,n){return jt(t.midX,t.midY,e.x,e.y,n.x,n.y)}function Is(t,e,n,o=.08){if(!t||t.length<2)return null;let r=Ds(t[0],e,n),s=Ds(t[1],e,n);if(r.dist<=o&&s.dist<=o){let i=r.dist<=s.dist?0:1,a=1-i;return{hingeEdge:t[i],otherEdge:t[a],hingeDist:i===0?r.dist:s.dist,otherDist:a===0?r.dist:s.dist,isClosed:!0,hingeT:i===0?r.t:s.t}}if(r.dist<=o||s.dist<=o){let i=r.dist<=o?0:1,a=1-i;return{hingeEdge:t[i],otherEdge:t[a],hingeDist:i===0?r.dist:s.dist,otherDist:a===0?r.dist:s.dist,isClosed:!1,hingeT:i===0?r.t:s.t}}return null}function Cs(t){if(!t||t.length<2)return 0;let e=t[0].midX-t[1].midX,n=t[0].midY-t[1].midY;return Math.sqrt(e*e+n*n)}function Xo(t){if(!t||!t.start||t.start.x==null||t.start.y==null||t.start.z==null||!t.end||t.end.x==null||t.end.y==null||t.end.z==null)return null;let e=t.start,n=t.end,o=n.x-e.x,r=n.y-e.y,s=Math.sqrt(o*o+r*r)||1,i=0,a=0;return t.centerDir?(i=t.centerDir.x,a=t.centerDir.y):(i=-r/s,a=o/s),{p1:e,p2:n,dirX:o/s,dirY:r/s,length:s,midX:(e.x+n.x)/2,midY:(e.y+n.y)/2,normalX:i,normalY:a}}function Hs(t,e,n,o=.08){let r=Xo(t);if(!r)return null;let s=jt(r.p1.x,r.p1.y,e.x,e.y,n.x,n.y),i=jt(r.p2.x,r.p2.y,e.x,e.y,n.x,n.y);if(s.dist<=o&&i.dist<=o){let a=s.dist<=i.dist;return{hingePoint:a?r.p1:r.p2,otherPoint:a?r.p2:r.p1,hingeDist:a?s.dist:i.dist,otherDist:a?i.dist:s.dist,isClosed:!0,hingeT:a?s.t:i.t}}if(s.dist<=o||i.dist<=o){let a=s.dist<=o;return{hingePoint:a?r.p1:r.p2,otherPoint:a?r.p2:r.p1,hingeDist:a?s.dist:i.dist,otherDist:a?i.dist:s.dist,isClosed:!1,hingeT:a?s.t:i.t}}return null}function oo(t,e=.1){let n=t.originalPoints;if(!n||n.length===0)return{densityProfile:[],binStartT:[],binEndT:[],avgDensity:0,maxDensity:0,minDensity:0,binSize:e};let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=t.length;if(s<=0)return{densityProfile:[],binStartT:[],binEndT:[],avgDensity:0,maxDensity:0,minDensity:0,binSize:e};let i=[];for(let y of n){let m=y.x-t.start.x,p=y.y-t.start.y,b=(m*o+p*r)/(s*s);i.push(Math.max(0,Math.min(1,b)))}let a=Math.max(1,Math.ceil(s/e)),u=s/a,d=new Array(a).fill(0);for(let y of i){let m=Math.min(a-1,Math.floor(y*a));d[m]++}let f=d.map(y=>y/u),c=d.map((y,m)=>m/a),l=d.map((y,m)=>(m+1)/a),h=f.reduce((y,m)=>y+m,0)/a,g=Math.max(...f),x=Math.min(...f);return{densityProfile:f,binStartT:c,binEndT:l,avgDensity:h,maxDensity:g,minDensity:x,binSize:u}}function Ws(t){let{densityProfile:e,avgDensity:n}=t;if(e.length===0||n===0)return 0;let o=e.length,r=e.reduce((i,a)=>i+(a-n)**2,0)/o;return Math.sqrt(r)/n}function _s(t){let e=[];for(let o of t)if(o.originalPoints&&o.originalPoints.length>=50&&o.length>=.5){let r=oo(o,.1);r.avgDensity>0&&e.push(r.avgDensity)}e.sort((o,r)=>o-r);let n=e.length>0?e[Math.floor(e.length/2)]:1;return{sortedDensities:e,medianDensity:n}}function Zo(t,e){let{sortedDensities:n}=e;if(n.length===0)return .5;if(t<=0)return 1;let o=0;for(let r of n)r<t&&o++;return 1-o/n.length}function As(t,e,n=.5,o=.5,r=3){let{densityProfile:s,avgDensity:i}=t;if(s.length===0||i===0)return[];let a=i*n,u=s.length,d=[],f=!1,c=0,l=0,h=0;for(let g=0;g<u;g++){let x=s[g]<a;if(x&&!f)f=!0,c=g,l=s[g],h=1;else if(x&&f)l+=s[g],h++;else if(!x&&f){f=!1;let y=g-1,m=c/u,p=(y+1)/u,b=(p-m)*e;b>=o&&b<=r&&d.push({startT:m,endT:p,startLen:m*e,endLen:p*e,avgDensity:l/h}),l=0,h=0}}if(f){let g=u-1,x=c/u,y=(g+1)/u,m=(y-x)*e;m>=o&&m<=r&&d.push({startT:x,endT:y,startLen:x*e,endLen:y*e,avgDensity:l/h})}return d}function ya(t,e,n){let o=t.originalPoints;if(!o||o.length<5)return{verticalGapRatio:1,isDoorSized:!0};let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=t.length*t.length;if(i<=0)return{verticalGapRatio:0,isDoorSized:!1};let a=[];for(let m of o){let p=m.x-t.start.x,b=m.y-t.start.y,w=(p*r+b*s)/i;w>=e&&w<=n&&m.z!==void 0&&isFinite(m.z)&&a.push(m.z)}if(a.length<5)return{verticalGapRatio:1,isDoorSized:!0};let u=1/0,d=-1/0;for(let m of o)m.z!==void 0&&isFinite(m.z)&&(m.z<u&&(u=m.z),m.z>d&&(d=m.z));let f=d-u;if(f<.5)return{verticalGapRatio:0,isDoorSized:!1};let c=15,l=new Array(c).fill(!1);for(let m of a){let p=Math.min(c-1,Math.max(0,Math.floor((m-u)/f*c)));l[p]=!0}let h=0,g=0;for(let m=0;m<c;m++)l[m]?(h=Math.max(h,g),g=0):g++;h=Math.max(h,g);let x=h/c,y=x>=.35;return{verticalGapRatio:x,isDoorSized:y}}function Bs(t,e,n,o=.5,r=.5,s=3,i=.35){let a=As(e,n,o,r,s),u=[];for(let d of a){let{verticalGapRatio:f,isDoorSized:c}=ya(t,d.startT,d.endT);u.push({...d,verticalGapRatio:f,isRectangular:c})}return u}function Ls(t,e,n){if(!t||!t.start||!t.end)return null;let o=t.start,r=t.end,s=jt(o.x,o.y,e.x,e.y,n.x,n.y),i=jt(r.x,r.y,e.x,e.y,n.x,n.y),a=s.dist<=i.dist,u=a?o:r,d=a?s.dist:i.dist,f=jt(u.x,u.y,e.x,e.y,n.x,n.y);return{hingeT:Math.max(0,Math.min(1,f.t)),hingeDist:d}}function ks(t,e,n,o=2e3){let{hingeT:r}=t,s=.25,i=0,a=null,u=0;for(let d of e){let f=(d.startT+d.endT)/2,c=Math.abs(f-r),l=Math.max(0,1-c/s),h=d.isRectangular?1:.3,g=Math.max(0,1-d.avgDensity/o),x=l*.5+h*.3+g*.2;x>i&&(i=x,a=d,u=l)}return{score:i>.5?i:-1,bestRegion:a,bestOverlap:u,bestOpeningStartT:a?.startT??0,bestOpeningEndT:a?.endT??0}}function ba(t,e,n=.15){let o=0,r=0;for(let i of e)i!==t&&((Ze(t.start,i.start)<=n||Ze(t.start,i.end)<=n)&&o++,(Ze(t.end,i.start)<=n||Ze(t.end,i.end)<=n)&&r++);let s=0;return o>=1&&r>=1?s=5:(o>=1||r>=1)&&(s=2),{left:o,right:r,score:s}}function wa(t,e){let n=[],{start:o,end:r}=t;for(let a of e){let{dist:u}=jt(a.x,a.y,o.x,o.y,r.x,r.y);n.push(u)}let s=n.reduce((a,u)=>a+u,0)/n.length,i=n.reduce((a,u)=>a+(u-s)**2,0)/n.length;return i<.01?3:i<.05?2:i<.1?1:0}function za(t,e,n=.15){let o=t.end.x-t.start.x,r=t.end.y-t.start.y,s=Math.sqrt(o*o+r*r);if(s<1e-12)return 0;let i=-r/s,a=o/s,u=[[],[]];for(let c=0;c<2;c++){let l=c===0?t.start:t.end;for(let h of e){if(h===t)continue;let g=Ze(l,h.start),x=Ze(l,h.end),y=null;if(g<=n?y=h.end:x<=n&&(y=h.start),!y)continue;let m=h.end.x-h.start.x,p=h.end.y-h.start.y,b=Math.sqrt(m*m+p*p);if(b<1e-12||Math.abs(o*m+r*p)/(s*b)>=.342)continue;let z=(y.x-l.x)*i+(y.y-l.y)*a;u[c].push(z>0?1:-1)}}if(u[0].length===0||u[1].length===0)return 0;let d=[...u[0],...u[1]];return d.every(c=>c===d[0])?2:1}function Os(t,e,n){let o=ba(t,n),r=wa(t,e),s=za(t,n);return{score:o.score*3+r*2+s*2,details:{conn:o.score,variance:r,sameSide:s}}}function Ma(t){let e=t.end.x-t.start.x,n=t.end.y-t.start.y,o=Math.atan2(n,e)*(180/Math.PI);return o<0&&(o+=180),o}function Ea(t,e){let n=[];for(let o of t){let r=!1;for(let s of n){let i=Math.abs(o.angle-s[0].angle);if(Math.min(i,180-i)<=e){s.push(o),r=!0;break}}r||n.push([o])}return n}function Pa(t,e=.04){let n=[];for(let o of t){let r=!1;for(let s of n)if(Math.abs(o.offset-s[0].offset)<=e){s.push(o),r=!0;break}r||n.push([o])}return n}function Ta(t,e,n,o,r){let s=[];for(let d of t){let f=d.seg,c=(f.start.x-e.start.x)*n+(f.start.y-e.start.y)*o,l=(f.end.x-e.start.x)*n+(f.end.y-e.start.y)*o;c<=l?s.push({l:c,r:l,idx:d.idx,startPt:f.start,endPt:f.end}):s.push({l,r:c,idx:d.idx,startPt:f.end,endPt:f.start})}s.sort((d,f)=>d.l-f.l);let i=[],a=.01,u=.5;for(let{l:d,r:f,idx:c,startPt:l,endPt:h}of s){if(i.length===0){i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h});continue}let g=i[i.length-1];if(d<=g.r+r){let x=Math.min(g.r,f)-Math.max(g.l,d),y=Math.hypot(g.endPt.x-l.x,g.endPt.y-l.y);x>=u||y<=a?(d<g.l&&(g.l=d,g.startPt=l),f>g.r&&(g.r=f,g.endPt=h),g.srcIdxs.includes(c)||g.srcIdxs.push(c)):i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}else i.push({l:d,r:f,srcIdxs:[c],startPt:l,endPt:h})}return i}function Sa(t,e,n,o,r,s,i){let a=Math.hypot(n.x-e.x,n.y-e.y),u={...t};return u.start={...e},u.end={...n},u.length=a,u.direction={x:o,y:r,z:0},s!==void 0&&(u.rooftopPz=s),u.buildRosource=i,u.isRebuild=!1,u.doorAndBeamData||(u.doorAndBeamData=[]),u.insetionArr||(u.insetionArr=[]),u}function Ra(t,e){let n=!0;for(;n;){n=!1;for(let o=0;o<t.length;o++)for(let r=o+1;r<t.length;r++){let s=t[o],i=t[r];if(s.rooftopPz!==void 0&&i.rooftopPz!==void 0&&Math.abs(s.rooftopPz-i.rooftopPz)>.05)continue;let a=Math.abs(s.end.x-i.start.x)<.001&&Math.abs(s.end.y-i.start.y)<.001,u=Math.abs(s.start.x-i.end.x)<.001&&Math.abs(s.start.y-i.end.y)<.001,d=Math.abs(s.end.x-i.end.x)<.001&&Math.abs(s.end.y-i.end.y)<.001,f=Math.abs(s.start.x-i.start.x)<.001&&Math.abs(s.start.y-i.start.y)<.001;if(!(a||u||d||f)||s.direction.x*i.direction.x+s.direction.y*i.direction.y<.98)continue;let l=s.start,h=s.end;a?h=i.end:u?l=i.start:d?h=i.start:f&&(l=i.end);let g=Math.hypot(h.x-l.x,h.y-l.y),x={...s};x.start=l,x.end=h,x.length=g,x.direction={x:(h.x-l.x)/g,y:(h.y-l.y)/g,z:0},s.rooftopPz!==void 0&&i.rooftopPz!==void 0?x.rooftopPz=Math.max(s.rooftopPz,i.rooftopPz):x.rooftopPz=s.rooftopPz!==void 0?s.rooftopPz:i.rooftopPz,x.buildRosource=s.buildRosource||i.buildRosource,x.isRebuild=!1,x.doorAndBeamData||(x.doorAndBeamData=[]),x.insetionArr||(x.insetionArr=[]),t[o]=x,e[o]=[...new Set([...e[o],...e[r]])],t.splice(r,1),e.splice(r,1),r--,n=!0}}return{segments:t,sourceMap:e}}function Ye(t,e=5,n=.05){if(t.length<=1)return{segments:t,sourceMap:t.map((u,d)=>[d])};let o=t.map((u,d)=>({seg:u,angle:Ma(u),idx:d}));o.sort((u,d)=>u.angle-d.angle);let r=Ea(o,e),s=[],i=[],a=[];for(let u of r){let d=u[0].seg;for(let m of u)m.seg.length>d.length&&(d=m.seg);let f=d.end.x-d.start.x,c=d.end.y-d.start.y,l=Math.sqrt(f*f+c*c);if(l<1e-12)continue;let h=-c/l,g=f/l,x=u.map(m=>{let p=m.seg.start.x-d.start.x,b=m.seg.start.y-d.start.y;return{seg:m.seg,offset:Math.abs(p*h+b*g),idx:m.idx}});x.sort((m,p)=>m.offset-p.offset);let y=Pa(x);for(let m of y){let p=m[0].seg;for(let T of m)T.seg.length>p.length&&(p=T.seg);let b=p.end.x-p.start.x,w=p.end.y-p.start.y,z=Math.sqrt(b*b+w*w);if(z<1e-12)continue;let M=b/z,P=w/z,E=m.map(T=>T.seg.rooftopPz).filter(T=>T!==void 0),S=E.length>0?Math.max(...E):void 0,I=[...new Set(E.map(T=>T.toFixed(3)))];if(I.length>1){let T=m.map(R=>({idx:R.idx,start:{x:R.seg.start.x,y:R.seg.start.y,z:R.seg.start.z},end:{x:R.seg.end.x,y:R.seg.end.y,z:R.seg.end.z},length:R.seg.length,rooftopPz:R.seg.rooftopPz}));a.push({values:[...I],segments:T})}let W=[...new Set(m.map(T=>String(T.seg.buildRosource)))],v=Ta(m,p,M,P,n);for(let{l:T,r:R,srcIdxs:D,startPt:H,endPt:V}of v){if(R-T<.01)continue;let C=Sa(p,H,V,M,P,S,p.buildRosource);s.push(C),i.push(D)}}}return{...Ra(s,i),debugRPZGroups:a}}function Da(t){return{minLengthRatio:0,maxDistance:.5,angleThreshold:45,projectionMargin:.1,printOnly:!0,minEmbedRatioThreshold:.3,noWallSurroundDistance:.3,minEmbedRatio:.05,...t}}function va(t,e,n){return[{x:e,y:n,z:0},{x:t[0].x,y:t[0].y,z:0},{x:t[1].x,y:t[1].y,z:0},{x:t[2].x,y:t[2].y,z:0},{x:t[3].x,y:t[3].y,z:0}]}function Va(t,e,n,o,r,s,i,a,u){if(!["_008","_002","_014","_015","_020"].some(c=>t.includes(c)))return;e.map(c=>{let{start:l,end:h}=c,g=jt(n,o,l.x,l.y,h.x,h.y),x=r.map(z=>{let M=jt(z.x,z.y,l.x,l.y,h.x,h.y);return`t=${M.t.toFixed(3)},d=${M.dist.toFixed(3)}`}),y=h.x-l.x,m=h.y-l.y,p=Math.sqrt(y*y+m*m)||1,b=Math.abs(i*(y/p)+a*(m/p)),w=Math.acos(Math.min(b,1))*(180/Math.PI);return{seg:c,lineDist:g.dist,t:g.t,pts:x,angle:w,length:c.length}}).sort((c,l)=>c.lineDist-l.lineDist)}function Ia(t,e,n,o,r,s,i,a,u){let d=[{margin:s.projectionMargin,maxDist:s.maxDistance,label:"\u4E25\u683C"},{margin:.4,maxDist:.65,label:"\u5BBD\u677E"}],f=[],c=Cs(a)||e;for(let l of d){if(f.length>0)break;for(let h of t){let{start:g,end:x}=h,y=!1,m=1/0;for(let E of r){let{dist:S,t:I}=jt(E.x,E.y,g.x,g.y,x.x,x.y);I>=-l.margin&&I<=1+l.margin&&S<=l.maxDist&&(y=!0,S<m&&(m=S))}if(!y)continue;let p=null;if(u&&(p=Hs(u,g,x,.3)),!p||!p.hingePoint){let E=Is(a,g,x,.1);E&&E.hingeEdge&&(p={...E,hingePoint:E.hingeEdge?{x:E.hingeEdge.midX,y:E.hingeEdge.midY,z:0}:null,otherPoint:E.otherEdge?{x:E.otherEdge.midX,y:E.otherEdge.midY,z:0}:null})}p&&p.hingePoint&&jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y).dist>.3&&(p=null);let b=!1;if(!p||!p.hingePoint){let E=jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y);if(m<.15&&E.dist<.3&&h.originalPoints&&h.originalPoints.length>=50&&h.length>=.5)b=!0;else continue}if(!b&&(p.hingeT<-l.margin||p.hingeT>1+l.margin)){let E=jt(r[0].x,r[0].y,g.x,g.y,x.x,x.y);if(m<.15&&E.dist<.3&&h.originalPoints&&h.originalPoints.length>=50&&h.length>=.5)b=!0,h.__candidatesDebug="hingeT\u8D85\u8303\u56F4-\u5BC6\u5EA6\u515C\u5E95";else continue}let w=b?0:p.isClosed?1:.5,z=[r[1],r[2],r[3],r[4]],M=vs(g.x,g.y,x.x,x.y,z),P=c>0?M/c:0;f.push({seg:h,minDist:m,embedRatio:P,ratio:h.length/c,score:0,details:{conn:0,variance:0,sameSide:0},hingeInfo:p,hingeScore:w})}}return f}function Ca(t,e,n,o){if(t.length<=1)return t.length===1&&(t[0].score=-1),t;for(let r of t){let{score:s,details:i}=Os(r.seg,e,n);r.score=s,r.details=i}return t.sort((r,s)=>{let i=r.hingeScore||0;return(s.hingeScore||0)-i||s.embedRatio-r.embedRatio||s.score-r.score||r.minDist-s.minDist}),t}function Ha(t,e,n,o){let r=t.end.x-t.start.x,s=t.end.y-t.start.y,i=Math.sqrt(r*r+s*s),a=r/i,u=s/i,d=e.map(P=>{let E=P.x-t.start.x,S=P.y-t.start.y;return(E*a+S*u)/i}),f=Math.min(...d),c=Math.max(...d),l=(c-f)*i,h=(f+c)/2,g=t.start.x+h*r,x=t.start.y+h*s,y=n.coordinatesByArea.heightData?.minZ?n.coordinatesByArea.heightData.minZ:0,m=n.coordinatesByArea.heightData?.maxZ?n.coordinatesByArea.heightData.maxZ:0,p=Math.abs(m-y),b=Ge(o,t.start.x,t.start.y),w=Math.max(0,y-b),z=f<=0&&c>=1,M=t.start.z+h*(t.end.z-t.start.z);return{p:{x:g,y:x,z:M},width:l,full:z,height:p,groundClearance:w,wallTMin:Math.max(0,Math.min(1,f)),wallTMax:Math.max(0,Math.min(1,c))}}function Go(t,e,n){let o=e.map(f=>n[f]),r=[],s=[],i=[],a=[],u,d;for(let f of o){if(f.originalPoints?.length)for(let c of f.originalPoints)r.push(c);if(f.doorAndBeamData?.length)for(let c of f.doorAndBeamData)s.push(c);if(f.insetionArr?.length)for(let c of f.insetionArr)i.push(c);if(f.DrawDoor?.length)for(let c of f.DrawDoor)a.push(c);f.boxData&&!u&&(u=f.boxData),f.rooftopPz!==void 0&&(d===void 0||f.rooftopPz>d)&&(d=f.rooftopPz)}r.length&&(t.originalPoints=r),s.length&&(t.doorAndBeamData=s),i.length&&(t.insetionArr=i),a.length&&(t.DrawDoor=a),u&&(t.boxData=u),d!==void 0&&(t.rooftopPz=d)}function cn(t,e,n=10,o=.2,r=.2){let s=[];if(!t.DrawDoor)return s;let i=t.DrawDoor.p,a=t.wallSegment,u=a.end.x-a.start.x,d=a.end.y-a.start.y,f=Math.sqrt(u*u+d*d)||1,c=u/f,l=d/f,h=i.x-a.start.x,g=i.y-a.start.y,x=(h*c+g*l)/f;for(let y of e){if(y===a)continue;let m=y.end.x-y.start.x,p=y.end.y-y.start.y,b=Math.sqrt(m*m+p*p)||1,w=m/b,z=p/b,M=Math.abs(c*w+l*z),E=Math.acos(Math.min(M,1))*(180/Math.PI);if(Math.abs(E-90)>n)continue;let S=[y.start,y.end];for(let I of S){let W=I.x-a.start.x,v=I.y-a.start.y,T=(W*c+v*l)/f,R=a.start.x+T*f*c,D=a.start.y+T*f*l;if(Math.sqrt((I.x-R)**2+(I.y-D)**2)>r)continue;let V=Math.abs((T-x)*f);if(V<=o){s.push({seg:y,distAlongWall:V,angle:E,endPoint:I});break}}}return s}function Dn(t,e){let n=t.start,o=t.end,r=o.x-n.x,s=o.y-n.y,i=Math.sqrt(r*r+s*s)||1,a=((e.x-n.x)*r+(e.y-n.y)*s)/(i*i),u=Math.max(0,Math.min(1,a)),d={x:n.x+u*r,y:n.y+u*s,z:n.z+(o.z-n.z)*u||0};function f(b){if(!b||!b.length)return[[],[]];let w=[],z=[];for(let M of b){let P=((M.x-n.x)*r+(M.y-n.y)*s)/(i*i);P<=u+1e-8&&w.push(M),P>=u-1e-8&&z.push(M)}return(w.length===0||c(w[w.length-1],d)>1e-6)&&w.push({...d}),(z.length===0||c(z[0],d)>1e-6)&&z.unshift({...d}),[w,z]}function c(b,w){return Math.sqrt((b.x-w.x)**2+(b.y-w.y)**2)}let[l,h]=f(t.originalPoints),g=Math.max(u*i,1e-6),x=Math.max((1-u)*i,1e-6),y=t.direction||{x:r/i,y:s/i,z:(o.z-n.z)/i||0},m={...t,start:{...n},end:{...d},length:g,direction:{...y},originalPoints:l.length?l:void 0},p={...t,start:{...d},end:{...o},length:x,direction:{...y},originalPoints:h.length?h:void 0};if("isRebuild"in t&&(m.isRebuild=t.isRebuild,p.isRebuild=t.isRebuild),t.DrawDoor&&t.DrawDoor.length>0){let b=[],w=[];for(let z of t.DrawDoor){let M=((z.p.x-n.x)*r+(z.p.y-n.y)*s)/(i*i);M<=u+1e-8&&b.push(z),M>=u-1e-8&&w.push(z)}m.DrawDoor=b.length?b:void 0,p.DrawDoor=w.length?w:void 0}else m.DrawDoor=void 0,p.DrawDoor=void 0;if(t.boxData){let b=w=>{if(!w.length)return;let z=1/0,M=1/0,P=1/0,E=-1/0,S=-1/0,I=-1/0;for(let W of w)W.x<z&&(z=W.x),W.y<M&&(M=W.y),W.z!==void 0&&W.z<P&&(P=W.z),W.x>E&&(E=W.x),W.y>S&&(S=W.y),W.z!==void 0&&W.z>I&&(I=W.z);return{isBox3:!0,min:{x:z,y:M,z:P},max:{x:E,y:S,z:I}}};m.boxData=b(l),p.boxData=b(h)}return[m,p]}function vn(t,e,n,o){let r=Da(n),s=[],{segments:i,sourceMap:a}=o??Ye(e),u=i,d=_s(u);for(let f of t){let c=f.coordinatesByArea?.coordinates;if(!c||c.length<4)continue;let l=c.slice(0,4),h=Xo(f.nline),g=no(l),x,y,m,p,b;if(h?(x=h.midX,y=h.midY,m=h.length,p=h.dirX,b=h.dirY):(x=l.reduce((B,O)=>B+O.x,0)/4,y=l.reduce((B,O)=>B+O.y,0)/4,m=g.longSide,p=g.dirX,b=g.dirY),m<.01)continue;let w=l.reduce((B,O)=>B+O.z,0)/4,z=Ge(e,x,y);if(w<z-.3)continue;let M=Vs(l),P=[...l].sort((B,O)=>B.z-O.z),E=P[0].x-P[1].x,S=P[0].y-P[1].y,I=Math.sqrt(E*E+S*S),W=va(l,x,y);Va(f.name,u,x,y,W,m,p,b,r);let v=Ia(u,I,p,b,W,r,f.name,M,f.nline);if(v.length>0)for(let B=0;B<v.length;B++){let O=v[B],$=O.seg,Z=!!O.hingeInfo?.hingePoint,Q=O.hingeInfo?.isClosed,j=O.hingeInfo?.hingeDist??-1,X=jt(W[0].x,W[0].y,$.start.x,$.start.y,$.end.x,$.end.y)}if(v=Ca(v,W,e,f.name),v.length===0){for(let O of u){let $=O.start.x+(O.end.x-O.start.x)/2-x,Z=O.start.y+(O.end.y-O.start.y)/2-y;if(Math.sqrt($*$+Z*Z)>2)continue;let j=O.end.x-O.start.x,X=O.end.y-O.start.y,U=Math.sqrt(j*j+X*X),at=j/U,mt=X/U,St=Math.abs(at*p+mt*b),ot=Math.acos(Math.min(1,Math.abs(St)))*180/Math.PI,Mt=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y)}let B=0;for(let O of u)jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y).dist<2&&B++}let T=I;for(let B of v){let O=B.seg;if(O.originalPoints&&O.originalPoints.length>100&&O.length>.5)try{let $=oo(O,.1),Z=Bs(O,$,O.length,.5,.5,3,.35),Q,j=!1;if(f.nline){let mt=Ls(f.nline,O.start,O.end);if(mt)Q=mt.hingeT,j=mt.hingeDist>.05;else{let St=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y);Q=Math.max(0,Math.min(1,St.t))}}else{let mt=jt(x,y,O.start.x,O.start.y,O.end.x,O.end.y);Q=Math.max(0,Math.min(1,mt.t))}let X=Ws($),U,at=X<=.3;at?U={score:Zo($.avgDensity,d),bestRegion:null,bestOverlap:0,bestOpeningStartT:0,bestOpeningEndT:1}:(U=ks({hingeT:Q,doorWidth:T,isOpenDoor:j},Z,O.length,d.medianDensity),U.score<0&&(U={score:Zo($.avgDensity,d),bestRegion:null,bestOverlap:0,bestOpeningStartT:0,bestOpeningEndT:1})),f.name.includes("door_002")&&(window.__doorLog=!0),B.densityScore=U.score,B.densityOverlap=U.bestOverlap,B.rectangularRegions=Z,B.densityResult=$,B.densityOpeningStartT=U.bestOpeningStartT,B.densityOpeningEndT=U.bestOpeningEndT,B.densityCV=X,B.densityUsedGlobalRank=at}catch{B.densityScore=-1}else B.densityScore=-1}if(f.name.includes("door_002"))for(let B=0;B<v.length;B++){let $=v[B].seg,Z=u.findIndex(j=>Math.abs(j.start.x-$.start.x)<.001&&Math.abs(j.start.y-$.start.y)<.001&&Math.abs(j.end.x-$.end.x)<.001&&Math.abs(j.end.y-$.end.y)<.001),Q=Z>=0?a[Z]||[]:[];for(let j of Q){let X=e[j]}}if(v.sort((B,O)=>{let $=B.densityScore>=0?B.densityScore:-1/0,Z=O.densityScore>=0?O.densityScore:-1/0;if($!==Z)return Z-$;if(Math.abs($-Z)<.3){let X=B.ratio||0,U=O.ratio||0,at=Math.abs(X-1),mt=Math.abs(U-1);if(at!==mt)return at-mt}let Q=B.hingeScore||0,j=O.hingeScore||0;return Q!==j?j-Q:B.minDist-O.minDist}),v.length>0)for(let B=0;B<v.length;B++){let O=v[B],$=O.seg,Z=O.ratio||$.length/T,Q=Math.abs(Z-1)}let R=null,D=1/0,H=0,V=0,C=null,_=-1,A=0,L=0,k=0;if(v.length>0){let B=v[0];R=B.seg,D=B.minDist,H=B.ratio,V=B.embedRatio,C=B.hingeInfo,_=B.densityScore??-1,A=B.densityOverlap??0,L=B.densityOpeningStartT??0,k=B.densityOpeningEndT??0}let F;if(R&&(F=Ha(R,l,f,i),F&&k>L&&(F.wallTMin=Math.max(0,Math.min(1,L)),F.wallTMax=Math.max(0,Math.min(1,k))),!r.printOnly)){let B=R;B.DrawDoor||(B.DrawDoor=[]),B.DrawDoor.push(F),B.isDoor=!0}if(R){let B=v.map(($,Z)=>{let Q=$.seg;return{start:{x:Q.start.x,y:Q.start.y,z:Q.start.z},end:{x:Q.end.x,y:Q.end.y,z:Q.end.z},length:Q.length,densityScore:$.densityScore??-1,hingeScore:$.hingeScore??0,minDist:$.minDist,ratio:$.ratio,isWinner:Z===0}}),O={doorName:f.name,doorCategory:f.category,doorCenter:f.center,wallSegment:R,distance:D,wallLengthRatio:H,DrawDoor:F,densityScore:_,densityOverlap:A,candidates:B,hingePoint:C?.hingePoint,otherPoint:C?.otherPoint,isClosed:C?.isClosed,doorWidth:F?.width,wallTMin:F?.wallTMin,wallTMax:F?.wallTMax};if(s.push(O),(f.name==="1527727501200_window_001"||f.name==="1527727427200_window_001")&&F){let $=R.end.x-R.start.x,Z=R.end.y-R.start.y,Q=Math.sqrt($*$+Z*Z),j=$/Q,X=Z/Q,U=F.p.x-R.start.x,at=F.p.y-R.start.y,mt=(U*j+at*X)/Q,St=0;for(let ot of i){if(ot===R)continue;let Mt=ot.end.x-ot.start.x,bt=ot.end.y-ot.start.y,Ct=Math.sqrt(Mt*Mt+bt*bt)||1,Et=Mt/Ct,zt=bt/Ct,Pt=Math.abs(j*Et+X*zt),et=Math.acos(Math.min(Pt,1))*(180/Math.PI);if(Math.abs(et-90)>10)continue;St++;let ht=[ot.start,ot.end],nt="";for(let J of ht){let le=J.x-R.start.x,wt=J.y-R.start.y,te=(le*j+wt*X)/Q,ge=R.start.x+te*Q*j,Dt=R.start.y+te*Q*X,ne=Math.sqrt((J.x-ge)**2+(J.y-Dt)**2),xe=Math.abs((te-mt)*Q);nt+=`\u7AEF(${J.x.toFixed(2)},${J.y.toFixed(2)})\u22A5=${ne.toFixed(3)}m \u6CBF\u5899=${xe.toFixed(3)}m `}}}if(F&&F.width>1){let $=cn(O,i);$.length>0&&$.forEach((Z,Q)=>{})}}}for(let f of s){let c=f.wallSegment,l=i.indexOf(c);if(l===-1)continue;let h=a[l];!h||h.length<=1||Go(c,h,e)}return s}function ro(t){return[t.wallTMin??0,t.wallTMax??1]}function Wa(t,e){let[n,o]=ro(t),[r,s]=ro(e),i=Math.max(0,Math.min(o,s)-Math.max(n,r)),a=Math.min(o-n,s-r);return a>0?i/a:0}function _a(t,e){if(!t.hingePoint||!e.hingePoint)return null;let n=t.hingePoint.x-e.hingePoint.x,o=t.hingePoint.y-e.hingePoint.y;return Math.sqrt(n*n+o*o)}function Fs(t,e){let n=e.end.x-e.start.x,o=e.end.y-e.start.y,r=n*n+o*o;if(r<1e-10)return 0;let s=((t.x-e.start.x)*n+(t.y-e.start.y)*o)/r;return Math.max(0,Math.min(1,s))}function Ns(t,e={}){let n=e.minTOverlapRatio??.5,o=e.maxHingeDist??.3,r=e.minHingeTOverlapRatio??.2;if(t.length<2)return{matches:t,removed:[]};let s=new Set,i=new Map;for(let f=0;f<t.length;f++){let l=t[f].wallSegment;i.has(l)||i.set(l,[]),i.get(l).push(f)}let a=0;for(let[f,c]of i){a++;let l=c.map(x=>t[x].doorName).join(", "),h=f.start,g=f.end}for(let[,f]of i){if(f.length<2)continue;let c=[...f].sort((l,h)=>{let g=t[l].densityScore??0;return(t[h].densityScore??0)-g});for(let l=0;l<c.length;l++){let h=c[l];if(s.has(h))continue;let g=t[h];for(let x=l+1;x<c.length;x++){let y=c[x];if(s.has(y))continue;let m=t[y],p=!1,b="",w=Wa(g,m),z=[g.hingePoint,g.otherPoint].filter(Boolean),M=[m.hingePoint,m.otherPoint].filter(Boolean),P=null;for(let H of z)for(let V of M){let C=Math.sqrt((H.x-V.x)**2+(H.y-V.y)**2);if(C>.25)continue;let _=Fs(H,g.wallSegment),A=Fs(V,m.wallSegment),L=Math.abs(_-A);(!P||C<P.dist)&&(P={dist:C,tDiff:L})}P&&P.tDiff<=.08&&(p=!0,b=`\u6700\u77ED\u8FB9\u8DDD=${P.dist.toFixed(3)}m t\u5DEE=${P.tDiff.toFixed(3)}`);let[E,S]=ro(g),[I,W]=ro(m),v=_a(g,m);g.doorName.includes("door_002")||g.doorName.includes("door_004")||m.doorName.includes("door_002")||m.doorName.includes("door_004");let T=[g.hingePoint,g.otherPoint].filter(Boolean),R=[m.hingePoint,m.otherPoint].filter(Boolean),D=1/0;for(let H of T)for(let V of R){let C=Math.sqrt((H.x-V.x)**2+(H.y-V.y)**2);C<D&&(D=C)}p&&s.add(y)}}}let u=t.filter((f,c)=>!s.has(c)),d=t.filter((f,c)=>s.has(c)).map(f=>f.doorName);return{matches:u,removed:d}}function Yo(t,e,n){let o={printOnly:!1,skipMerge:!1,...n};for(let m of t)m.DrawDoor&&(m.DrawDoor=void 0),m.isDoor&&(m.isDoor=void 0),m.drawDoorData&&(m.drawDoorData=void 0);let r=Array.isArray(e)?e.filter(m=>m&&m.category==="door"&&!m.isFindBeam):[],s,i;if(o.skipMerge)s=t,i=t.map((m,p)=>[p]);else{let m=Ye(t);s=m.segments,i=m.sourceMap}for(let m=0;m<s.length;m++){let p=i[m];p&&!s[m].originalPoints&&Go(s[m],p,t)}let a=vn(r,t,o,{segments:s,sourceMap:i}),d=Ns(a,{minTOverlapRatio:.5}).matches,f=new Set(d.map(m=>m.doorName));for(let m of s){let p=m;if(p.DrawDoor&&p.DrawDoor.length>0){let b=a.filter(z=>z.wallSegment===m).length;d.filter(z=>z.wallSegment===m).length===0&&(p.DrawDoor=[],p.isDoor=!1)}}let c=new Map;for(let m=0;m<d.length;m++){let p=d[m],b=s.indexOf(p.wallSegment);b>=0&&c.set(b,m)}let l=[],h=new Set,g=new Map;for(let m of e)m&&m.name&&g.set(m.name,m);for(let[m,p]of c){let b=i[m];if(!b)continue;let w=s[m],z=w.DrawDoor,M=w.isDoor,P=w.end.x-w.start.x,E=w.end.y-w.start.y,S=P*P+E*E,I=0,W=1,v=d[p],R=(v?g.get(v.doorName):void 0)?.coordinatesByArea?.coordinates;if(R&&R.length>=4&&S>0){let C=R.slice(0,4).map(_=>{let A=_.x-w.start.x,L=_.y-w.start.y;return(A*P+L*E)/S});I=Math.max(0,Math.min(1,Math.min(...C))),W=Math.max(0,Math.min(1,Math.max(...C)))}let D,H;S>0&&W>I&&(D={x:w.start.x+I*P,y:w.start.y+I*E,z:w.start.z+I*(w.end.z-w.start.z)},H={x:w.start.x+W*P,y:w.start.y+W*E,z:w.start.z+W*(w.end.z-w.start.z)});let V=!1;for(let C of b){let _=t[C];if(!_)continue;let A=_.end.x-_.start.x,L=_.end.y-_.start.y,k=A*A+L*L,F=Math.sqrt(k);if(k<1e-10||!D||!H){h.add(C),l.push(_);continue}let B=((D.x-_.start.x)*A+(D.y-_.start.y)*L)/k,O=((H.x-_.start.x)*A+(H.y-_.start.y)*L)/k,$=Math.max(0,Math.min(1,Math.min(B,O))),Z=Math.max(0,Math.min(1,Math.max(B,O)));if(Z-$<.001)continue;h.add(C);let Q=ot=>({x:_.start.x+ot*A,y:_.start.y+ot*L,z:_.start.z+ot*(_.end.z-_.start.z)}),j=Q($),X=Q(Z),U=_.direction||{x:A/F,y:L/F,z:0},at,mt,St;if(_.originalPoints&&_.originalPoints.length>0){at=[],mt=[],St=[];for(let ot of _.originalPoints){let Mt=((ot.x-_.start.x)*A+(ot.y-_.start.y)*L)/k;Mt<$-1e-8?at.push(ot):Mt>Z+1e-8?St.push(ot):mt.push(ot)}at.length===0&&(at=void 0),mt.length===0&&(mt=void 0),St.length===0&&(St=void 0)}if($>.001){let ot={..._,start:{..._.start},end:{...j},length:$*F,direction:{...U}};ot.DrawDoor=void 0,ot.isDoor=!1,ot.drawDoorData=void 0,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=at,l.push(ot)}{let ot={..._,start:{...j},end:{...X},length:(Z-$)*F,direction:{...U}};if(z&&(ot.DrawDoor=z),M&&(ot.isDoor=!0),ot.__matchIdx=p,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=mt,!V&&z&&z.length>0){let Mt=0;for(let Pt=0;Pt<z.length;Pt++)if(yr(D,H,z[Pt].p)){Mt=Pt;break}V=!0;let bt=Math.sqrt(S),Ct=bt>0?-E/bt:0,Et=bt>0?P/bt:0,zt={pointNum:2,start:D,end:H,n:{x:Ct,y:Et,z:0},height:z[Mt].height,groundClearance:z[Mt].groundClearance};ot.drawDoorData=zt}l.push(ot)}if(Z<.999){let ot={..._,start:{...X},end:{..._.end},length:(1-Z)*F,direction:{...U}};ot.DrawDoor=void 0,ot.isDoor=!1,ot.drawDoorData=void 0,ot.length<.01?(ot.isRebuild=!0,ot.originalPoints=[]):ot.originalPoints=St,l.push(ot)}}}for(let m=0;m<t.length;m++)h.has(m)||l.push(t[m]);let x=new Set(d.map(m=>m.doorName));for(let m of e)m&&m.category==="door"&&(x.has(m.name)?delete m.AbnormalDoor:m.AbnormalDoor=!0);let y=new Map;for(let m of d){if(!m.DrawDoor||m.DrawDoor.width<=1)continue;let p=g.get(m.doorName);if(!p||!p.positionArr||!p.positionArr.length)continue;let b=cn(m,s);if(b.length<=1)continue;let w=p.positionArr[0],z=m.DrawDoor.p,M=w.x-z.x,P=w.y-z.y,E=Math.sqrt(M*M+P*P);if(E<.001)continue;let S=M/E,I=P/E,W=b.filter(C=>{let _=(C.seg.start.x+C.seg.end.x)/2,A=(C.seg.start.y+C.seg.end.y)/2,L=_-z.x,k=A-z.y;return L*S+k*I>0});if(!W.length)continue;let v=W[0].endPoint,T=m.wallSegment;if(y.has(T))continue;let[R,D]=Dn(T,v);y.set(T,[R,D]);let H=((z.x-T.start.x)*(T.end.x-T.start.x)+(z.y-T.start.y)*(T.end.y-T.start.y))/(T.length*T.length),V=((v.x-T.start.x)*(T.end.x-T.start.x)+(v.y-T.start.y)*(T.end.y-T.start.y))/(T.length*T.length);H<=V?m.wallSegment=R:m.wallSegment=D}if(y.size>0){let m=[];for(let p of l)y.has(p)?m.push(...y.get(p)):m.push(p);for(let p of m)p.isDoor=!!(p.DrawDoor&&p.DrawDoor.length>0);return{segments:m,matches:d}}for(let m of l)m.isDoor=!!(m.DrawDoor&&m.DrawDoor.length>0);return{segments:l,matches:d}}var Aa={findWalls:Rn,mergeSegments:Xe,computeFloorZ:$e,analyzeQuadrangle:je,detectPerpWalls:Tn,splitSegment:Sn,process:jo},Ba={findWalls:vn,mergeSegments:Ye,computeFloorZ:Ge,analyzeQuadrangle:je,detectPerpWalls:cn,splitSegment:Dn,process:Yo};function un(t){return t==="window"?Aa:Ba}function La(t,e,n,o,r){return un(t).findWalls(e,n,o,r)}function ka(t,e,n,o){return un(t).process(e,n,o)}function Oa(t,e){return un(t).mergeSegments(e)}function Fa(t,e,n){return un(t).detectPerpWalls(e,n)}function Na(t,e,n){return un(t).splitSegment(e,n)}function Ue(t,e){let n=Math.atan2(e,t);return n<0&&(n+=Math.PI),n>=Math.PI&&(n-=Math.PI),n}function He(t,e,n){let o=Math.cos(n),r=Math.sin(n);return{x:t*o-e*r,z:t*r+e*o}}function so(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-10){let g=t-n,x=e-o;return Math.sqrt(g*g+x*x)}let d=((t-n)*i+(e-o)*a)/u;d=Math.max(0,Math.min(1,d));let f=n+d*i,c=o+d*a,l=t-f,h=e-c;return Math.sqrt(l*l+h*h)}function qs(t){if(t.length<3)return 0;let e=0,n=t.length;for(let o=0;o<n;o++){let r=(o+1)%n;e+=t[o].x*t[r].z,e-=t[r].x*t[o].z}return Math.abs(e)/2}function Uo(t){let e=t.x2-t.x1,n=t.z2-t.z1;return Math.sqrt(e*e+n*n)}function io(t){if(!t.length)return{rotated:[],mainAngle:0};let e=180,n=new Array(e).fill(0),o=Math.PI/e;for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Ue(c,l),x=Math.floor(g/o);x>=e&&(x=e-1),n[x]+=h}let r=new Array(e).fill(0),s=5;for(let f=0;f<e;f++){let c=0;for(let l=-s;l<=s;l++){let h=f+l;h<0&&(h+=e),h>=e&&(h-=e),c+=n[h]}r[f]=c}let i=0,a=0;for(let f=0;f<e;f++)r[f]>i&&(i=r[f],a=f);let u=(a+.5)*o;return{rotated:t.map(f=>{let c=He(f.x1,f.z1,-u),l=He(f.x2,f.z2,-u);return{...f,x1:c.x,z1:c.z,x2:l.x,z2:l.z}}),mainAngle:u}}var Ko=15*(Math.PI/180);function lo(t,e,n=.15,o=.5){let r=[];for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Ue(c,l),x;if(e==="X"?x=g<Ko||g>Math.PI-Ko:x=Math.abs(g-Math.PI/2)<Ko,!x)continue;let y,m,p;e==="X"?(y=(f.z1+f.z2)/2,m=Math.min(f.x1,f.x2),p=Math.max(f.x1,f.x2)):(y=(f.x1+f.x2)/2,m=Math.min(f.z1,f.z2),p=Math.max(f.z1,f.z2)),r.push({line:f,offset:y,pStart:m,pEnd:p,length:h})}if(r.length===0)return{groups:[],lineToGroup:new Map};r.sort((f,c)=>f.offset-c.offset);let s=[],i=[r[0]],a=r[0].offset;for(let f=1;f<r.length;f++){let c=r[f];Math.abs(c.offset-a)<n?i.push(c):(s.push(i),i=[c],a=c.offset)}i.length>0&&s.push(i);let u=[],d=new Map;for(let f of s){f.sort((l,h)=>l.pStart-h.pStart);let c=[];for(let l of f){let h=!1;for(let g of c)if(l.pStart<=g.end+o){g.items.push(l),g.end=Math.max(g.end,l.pEnd),h=!0;break}h||c.push({items:[l],end:l.pEnd})}for(let l of c){let h=l.items.map(x=>x.line),g=u.length;u.push(h);for(let x of h)d.set(x.id,g)}}return{groups:u,lineToGroup:d}}function ao(t,e,n=.3){let o=new Map;for(let r of t){let s=null,i=1/0;for(let a of e){let u=so(r.x,r.z,a.x1,a.z1,a.x2,a.z2);u<i&&(i=u,s=a.id)}s!==null&&i<=n&&o.set(r.id,s)}return o}var fn=.15;function qa(t,e){return Math.round(t/fn)+"_"+Math.round(e/fn)}function js(t,e=5){if(t.length<3)return new Set;let n=new Map,o=0,r=0;function s(u,d){let f=qa(u,d),c=n.get(f);if(c)return c;let l=Math.round(u/fn),h=Math.round(d/fn),g=null,x=fn*fn;for(let y=-1;y<=1;y++)for(let m=-1;m<=1;m++){let p=l+y+"_"+(h+m),b=n.get(p);if(!b)continue;let w=b.x-u,z=b.z-d,M=w*w+z*z;M<x&&(x=M,g=b)}return g?(n.set(f,g),g):(c={id:o++,x:u,z:d,edges:[]},n.set(f,c),c)}let i=[];for(let u of t){let d=s(u.x1,u.z1),f=s(u.x2,u.z2);if(d.id===f.id)continue;let c=Math.atan2(f.z-d.z,f.x-d.x),l=Math.atan2(d.z-f.z,d.x-f.x),h={id:r++,wallId:u.id,from:d,to:f,twin:null,angle:c,visited:!1},g={id:r++,wallId:u.id,from:f,to:d,twin:h,angle:l,visited:!1};h.twin=g,d.edges.push(h),f.edges.push(g),i.push(h,g)}if(i.length<6)return new Set;for(let u of n.values())u.edges.sort((d,f)=>d.angle-f.angle);let a=new Set;for(let u of i){if(u.visited)continue;let d=[],f=u,c=i.length+10;for(;!f.visited&&d.length<c;){f.visited=!0,d.push(f);let l=f.to,h=f.twin,g=l.edges.indexOf(h);if(g<0)break;let x=(g+1)%l.edges.length;f=l.edges[x]}if(d.length>=3){let l=d[0];if(d[d.length-1].to.id===l.from.id){let g=[],x=new Set;for(let m of d)x.add(m.wallId),g.length===0&&g.push({x:m.from.x,z:m.from.z}),g.push({x:m.to.x,z:m.to.z});if(qs(g)>=e)for(let m of x)a.add(m)}}}return a}var Ep=12*(Math.PI/180);function $s(t,e=2){let n=new Map;for(let o of t){let r=(o.x1+o.x2)/2,s=(o.z1+o.z2)/2,i=Math.floor(r/e),a=Math.floor(s/e),u=`${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}var ja=0;function Xs(){return++ja}function Zs(t,e,n=.15,o=.5){if(!t.length)return[];let r=10*Math.PI/180,s=[];for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(!(h<.01))if(e==="X"){if(Math.abs(l)>h*Math.sin(r))continue;let g=(f.z1+f.z2)/2,x=Math.min(f.x1,f.x2),y=Math.max(f.x1,f.x2);s.push({line:f,pStart:x,pEnd:y,offset:g,yMin:Math.min(f.y1,f.y2),yMax:Math.max(f.y1,f.y2)})}else{if(Math.abs(c)>h*Math.sin(r))continue;let g=(f.x1+f.x2)/2,x=Math.min(f.z1,f.z2),y=Math.max(f.z1,f.z2);s.push({line:f,pStart:x,pEnd:y,offset:g,yMin:Math.min(f.y1,f.y2),yMax:Math.max(f.y1,f.y2)})}}if(!s.length)return[];s.sort((f,c)=>f.offset-c.offset);let i=[],a=[s[0]],u=s[0].offset;for(let f=1;f<s.length;f++){let c=s[f];Math.abs(c.offset-u)<n?a.push(c):(i.push(a),a=[c],u=c.offset)}i.push(a);let d=[];for(let f of i){f.sort((l,h)=>l.pStart-h.pStart);let c=[];for(let l of f){if(c.length===0){c.push({pStart:l.pStart,pEnd:l.pEnd,offset:l.offset,count:1,yMin:l.yMin,yMax:l.yMax});continue}let h=c[c.length-1];l.pStart<=h.pEnd+o?(h.pEnd=Math.max(h.pEnd,l.pEnd),h.count+=1,h.yMin=Math.min(h.yMin,l.yMin),h.yMax=Math.max(h.yMax,l.yMax),h.offset=(h.offset*(h.count-1)+l.offset)/h.count):c.push({pStart:l.pStart,pEnd:l.pEnd,offset:l.offset,count:1,yMin:l.yMin,yMax:l.yMax})}for(let l of c){let h=l.pEnd-l.pStart;if(h<.01)continue;let g;e==="X"?g={id:Xs(),x1:l.pStart,y1:l.yMin,z1:l.offset,x2:l.pEnd,y2:l.yMax,z2:l.offset,length:h,pointCount:l.count,isMain:!1}:g={id:Xs(),x1:l.offset,y1:l.yMin,z1:l.pStart,x2:l.offset,y2:l.yMax,z2:l.pEnd,length:h,pointCount:l.count,isMain:!1},d.push(g)}}return d}function Gs(t,e=.8,n=3){return t.filter(o=>o.length>=e&&o.pointCount>=n)}function Ys(t,e=1,n=3){return t.filter(o=>o.length>=e&&o.pointCount>=n)}function $a(t,e,n={}){let{distTol:o=.15,gapTol:r=.5,minLen:s=.2,minGroupLen:i=1,minArea:a=3,maxWinDist:u=.3,minLongWall:d=2}=n,f=performance.now(),{rotated:c,mainAngle:l}=io(t),h=performance.now(),g=e.map(D=>{let H=He(D.x,D.z,-l);return{...D,x:H.x,z:H.z}}),{groups:x}=lo(c,"X",o,r),{groups:y}=lo(c,"Z",o,r),m=[...x,...y],p=performance.now(),b=new Map,w=[];for(let D=0;D<m.length;D++){let H=m[D];if(H.length===0){w.push(0);continue}let V=0;for(let C of H)V+=Uo(C),b.set(C.id,D);w.push(V)}let z=performance.now(),M=new Set;for(let D=0;D<m.length;D++)w[D]>=d&&M.add(D);if(M.size>0){let D=[...M];for(;D.length>0;){let H=D.shift(),V=m[H];for(let C=0;C<m.length;C++){if(M.has(C)||w[C]<i)continue;let _=m[C],A=!1;t:for(let L of V)for(let k of _)if(Math.hypot(L.x1-k.x1,L.z1-k.z1)<=co||Math.hypot(L.x1-k.x2,L.z1-k.z2)<=co||Math.hypot(L.x2-k.x1,L.z2-k.z1)<=co||Math.hypot(L.x2-k.x2,L.z2-k.z2)<=co){A=!0;break t}A&&(M.add(C),D.push(C))}}}let P=c.map((D,H)=>{let V=Uo(D),C=b.get(D.id),_=C!==void 0&&M.has(C),A=V>=d;return{id:H,x1:D.x1,y1:D.y1,z1:D.z1,x2:D.x2,y2:D.y2,z2:D.z2,length:V,pointCount:1,isMain:_||A}}),E=P.filter(D=>D.isMain),S=P.filter(D=>!D.isMain),I=performance.now(),W=ao(g,E,u),v=performance.now(),T=D=>D.map(H=>{let V=He(H.x1,H.z1,l),C=He(H.x2,H.z2,l);return{...H,x1:V.x,z1:V.z,x2:C.x,z2:C.z}}),R=performance.now();return console.log(`[\u4E3B\u4F53\u67B6\u6784] \u89D2\u5EA6\u5F52\u4E00\u5316: ${(h-f).toFixed(0)}ms`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u5171\u7EBF\u5206\u7EC4: ${(p-h).toFixed(0)}ms (\u6C34\u5E73${x.length}\u7EC4 \u5782\u76F4${y.length}\u7EC4)`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u7EC4\u5206\u6790+\u4E3B\u4F53\u5224\u5B9A: ${(R-p).toFixed(0)}ms (\u4E3B\u4F53\u7EC4${M.size} / \u603B\u7EC4${m.length})`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u6807\u8BB0\u539F\u59CB\u7EBF\u6BB5: ${(I-R).toFixed(0)}ms (\u4E3B\u4F53${E.length} / \u6742\u7EBF${S.length})`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u7A97\u6237\u6302\u63A5: ${(v-I).toFixed(0)}ms`),console.log(`[\u4E3B\u4F53\u67B6\u6784] \u603B\u8BA1: ${(v-f).toFixed(0)}ms`),{mainWalls:T(E),noiseWalls:T(S),windowToWall:W}}var co=.2;function Se(t,e){let n=Math.atan2(e,t);return n<0&&(n+=Math.PI),n>=Math.PI&&(n-=Math.PI),n}function Vn(t){for(;t>Math.PI;)t-=2*Math.PI;for(;t<-Math.PI;)t+=2*Math.PI;return t}function uo(t){return t*Math.PI/180}function In(t,e,n){let o=Math.cos(n),r=Math.sin(n);return{x:t*o-e*r,z:t*r+e*o}}function Re(t){let e=0;for(let n=1;n<t.length;n++){let o=t[n].x-t[n-1].x,r=t[n].z-t[n-1].z;e+=Math.sqrt(o*o+r*r)}return e}function Qo(t){if(t.length<3)return 0;let e=0,n=t.length;for(let o=0;o<n;o++){let r=(o+1)%n;e+=t[o].x*t[r].z,e-=t[r].x*t[o].z}return Math.abs(e)/2}function Me(t,e,n,o){let r=n-t,s=o-e;return Math.sqrt(r*r+s*s)}function Cn(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u<1e-6)return Me(t,e,n,o);let d=((t-n)*i+(e-o)*a)/u;return d=Math.max(0,Math.min(1,d)),Me(t,e,n+d*i,o+d*a)}function Jo(t){if(t.length<4)return t;let e=t.map(n=>({x:n.x,z:n.z}));for(let n=0;n<3;n++){let o=[{...e[0]}];for(let r=1;r<e.length-1;r++)o.push({x:(e[r-1].x+e[r].x+e[r+1].x)/3,z:(e[r-1].z+e[r].z+e[r+1].z)/3});o.push({...e[e.length-1]}),e=o}return e}function Us(t,e){let n=1/0;for(let o of t)for(let r=0;r<e.length-1;r++){let s=Cn(o.x,o.z,e[r].x,e[r].z,e[r+1].x,e[r+1].z);s<n&&(n=s)}return n}function fo(t,e){let n=new Map,o=1/0,r=-1/0,s=1/0,i=-1/0;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.z/e),f=u+","+d;n.set(f,(n.get(f)||0)+1),u<o&&(o=u),u>r&&(r=u),d<s&&(s=d),d>i&&(i=d)}return{cells:n,minX:o,maxX:r,minZ:s,maxZ:i}}function Ks(t,e){let n=new Set,o=1/0,r=-1/0,s=1/0,i=-1/0,a=(u,d)=>{let f=u+","+d;n.add(f),u<o&&(o=u),u>r&&(r=u),d<s&&(s=d),d>i&&(i=d)};for(let u of t){let d=u.x1,f=u.z1,c=u.x2,l=u.z2,h=Math.floor(d/e),g=Math.floor(f/e),x=Math.floor(c/e),y=Math.floor(l/e),m=Math.abs(x-h),p=Math.abs(y-g),b=h<x?1:-1,w=g<y?1:-1,z=m-p,M=Math.max(m,p)*2+5,P=0;for(;P++<M&&(a(h,g),!(h===x&&g===y));){let E=2*z;E>-p&&(z-=p,h+=b),E<m&&(z+=m,g+=w)}}return{active:n,minX:o,maxX:r,minZ:s,maxZ:i}}function Wn(t,e=1){let n=new Set(t);for(let o=0;o<e;o++){let r=new Set(n);for(let i of n){let[a,u]=i.split(",").map(Number);for(let d=-1;d<=1;d++)for(let f=-1;f<=1;f++)d===0&&f===0||r.add(a+d+","+(u+f))}let s=new Set(r);for(let i of r){let[a,u]=i.split(",").map(Number),d=!0;for(let f=-1;f<=1;f++){for(let c=-1;c<=1;c++)if(!r.has(a+f+","+(u+c))){d=!1;break}if(!d)break}d||s.delete(i)}n.clear();for(let i of s)n.add(i)}return n}function tr(t){let e=[],n=new Set;for(let o of t){if(n.has(o))continue;let r=new Set,s=[o];for(n.add(o);s.length>0;){let i=s.shift();r.add(i);let[a,u]=i.split(",").map(Number);for(let d=-1;d<=1;d++)for(let f=-1;f<=1;f++){if(d===0&&f===0)continue;let c=a+d+","+(u+f);t.has(c)&&!n.has(c)&&(n.add(c),s.push(c))}}r.size>=4&&e.push(r)}return e}function er(t,e){if(t.size<3)return[];let n=new Set,o=(g,x)=>t.has(g+","+x);for(let g of t){let[x,y]=g.split(",").map(Number);(!o(x+1,y)||!o(x-1,y)||!o(x,y+1)||!o(x,y-1))&&n.add(g)}if(n.size<3)return[];let r="",s=1/0,i=1/0;for(let g of n){let[x,y]=g.split(",").map(Number);(y<s||y===s&&x<i)&&(s=y,i=x,r=g)}if(!r)return[];let a=[[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]],u=[],[d,f]=r.split(",").map(Number),c=0,l=new Set,h=n.size*8;for(let g=0;g<h;g++){let y=d+","+f+"|"+c;if(l.has(y))break;l.add(y),u.push({x:(d+.5)*e,z:(f+.5)*e});let m=(c+3)%8,p=!1;for(let b=0;b<8;b++){let w=(m+b)%8,z=d+a[w][0],M=f+a[w][1];if(n.has(z+","+M)){c=(w+4)%8,d=z,f=M,p=!0;break}}if(!p||d+","+f===r&&u.length>2)break}return u}function Hn(t,e){if(t.length<=2)return[...t];let n=0,o=0,r=t[0],s=t[t.length-1];for(let i=1;i<t.length-1;i++){let a=Cn(t[i].x,t[i].z,r.x,r.z,s.x,s.z);a>n&&(n=a,o=i)}if(n>e){let i=Hn(t.slice(0,o+1),e),a=Hn(t.slice(o),e);return[...i.slice(0,-1),...a]}return[r,s]}var nr={cellSize:.1,densityThreshold:1,minComponentCells:20,simplifyEpsilon:.01,heightMin:.3,heightMax:2.8,closeIterations:5,sparseRadius:.3,sparseMinNeighbors:5,lineDensityMin:0,lineDensityMinLen:1,densityRatio:.12};function dn(t){let e=t.length;if(e<3)return{cx:0,cz:0,r:0,rmsResidual:0};let n=0,o=0,r=0,s=0,i=0,a=0,u=0,d=0,f=0;for(let m of t){let p=m.x,b=m.z;n+=p,o+=b,r+=p*p,s+=b*b,i+=p*b,a+=p*p*p,u+=b*b*b,d+=p*p*b,f+=p*b*b}let c=2*(e*(r+s)-n*n-o*o);if(Math.abs(c)<1e-10)return{cx:0,cz:0,r:0,rmsResidual:0};let l=(e*(a+f)-n*(r+s))/c,h=(e*(d+u)-o*(r+s))/c,g=Math.sqrt(Math.abs((r+s-2*l*n-2*h*o)/e+l*l+h*h)),x=0;for(let m of t){let b=Math.hypot(m.x-l,m.z-h)-g;x+=b*b}let y=Math.sqrt(x/e);return{cx:l,cz:h,r:g,rmsResidual:y}}function Xa(t){let e=t.length;if(e<4)return{eigenRatio:1,isLinear:!1,spread:0,mx:0,mz:0,v1x:1,v1z:0,v2x:0,v2z:1,s1:0,s2:0};let n=0,o=0;for(let w of t)n+=w.x,o+=w.z;n/=e,o/=e;let r=0,s=0,i=0;for(let w of t){let z=w.x-n,M=w.z-o;r+=z*z,s+=M*M,i+=z*M}r/=e,s/=e,i/=e;let a=r+s,u=Math.max(0,(r-s)*(r-s)+4*i*i),d=Math.sqrt(u),f=(a+d)/2,c=(a-d)/2,l=Math.sqrt(f),h=Math.sqrt(c),g=h,x=1,y=0,m=0,p=1;if(Math.abs(i)>1e-12||Math.abs(r-s)>1e-12){let w=i,z=f-r,M=Math.sqrt(w*w+z*z);M>1e-12&&(x=w/M,y=z/M);let P=i,E=c-r,S=Math.sqrt(P*P+E*E);S>1e-12&&(m=P/S,p=E/S)}let b=f>1e-10?c/f:0;return{eigenRatio:b,isLinear:b<.05,spread:g,mx:n,mz:o,v1x:x,v1z:y,v2x:m,v2z:p,s1:l,s2:h}}function Qs(t,e,n){if(t.length<6)return{type:"straight",eigenRatio:.5,spread:0,cloudUsed:t.length,mx:0,mz:0,v1x:1,v1z:0,v2x:0,v2z:1,s1:0,s2:0};let{eigenRatio:o,isLinear:r,spread:s,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h}=Xa(t),g=0;for(let w=1;w<t.length;w++){let z=t[w].x-t[w-1].x,M=t[w].z-t[w-1].z;g+=Math.sqrt(z*z+M*M)}let x=Za(t),y=dn(x);if(y.r>.1&&y.r<Math.max(g*5,2)&&y.rmsResidual<.07)return{type:"curved",eigenRatio:o,spread:s,cloudUsed:t.length,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h};let p=g>.01?s/g:1;return{type:r&&(s<.05||p<.1)?"straight":"curved",eigenRatio:o,spread:s,cloudUsed:t.length,mx:i,mz:a,v1x:u,v1z:d,v2x:f,v2z:c,s1:l,s2:h}}function Za(t){if(t.length<4)return[...t];let e=[{...t[0]}];for(let n=1;n<t.length-1;n++)e.push({x:(t[n-1].x+t[n].x+t[n+1].x)/3,z:(t[n-1].z+t[n].z+t[n+1].z)/3});return e.push({...t[t.length-1]}),e}function Ga(t,e,n=1.57){if(e<10)return[];let o=[];for(let x=0;x<e;x++){let y=(x+1)%e;o.push(Math.atan2(t[y].z-t[x].z,t[y].x-t[x].x))}let r=[o[0]];for(let x=1;x<e;x++){let y=o[x]-o[x-1];y>Math.PI?y-=2*Math.PI:y<-Math.PI&&(y+=2*Math.PI),r.push(r[x-1]+y)}let s=0;{let x=o[0]-o[e-1];x>Math.PI?x-=2*Math.PI:x<-Math.PI&&(x+=2*Math.PI),s=x}Math.abs(s)<.1;let i=[],a=5;for(let x=0;x<e;x++){let y=0;for(let m=-a;m<=a;m++)y+=r[(x+m+e)%e];i.push(y/(a*2+1))}let u=5,d=[];for(let x=0;x<e;x++){let y=i[(x+u)%e]-i[x];y>Math.PI?y-=2*Math.PI:y<-Math.PI&&(y+=2*Math.PI),d.push(y)}let f=10,c=.003,l=10,h=.05,g=[];for(let x=0;x<e;x++){let y=x,m=x,p=0,b=0,w=x,z=e+10;for(;z-- >0;){let P=d[w],E=Math.abs(P),S=P>0?1:P<0?-1:0;if(E<c){w=(w+1)%e;continue}if(b===0){b=S,p=E,m=w,w=(w+1)%e;continue}if(S!==b){let I=E,W=S,v=(w+1)%e,T=1;for(let R=0;R<l&&T<l*2;R++){let D=d[v],H=Math.abs(D);if(H<c){v=(v+1)%e;continue}(D>0?1:-1)===W?I+=H:(I-=H,I<0&&(W=-W,I=-I)),v=(v+1)%e,T++}if(W===b&&I>h){w=(w+1)%e;continue}break}if(p+=E,m=w,w=(w+1)%e,w===x)break}if((m>y?m-y+1:e-y+m+1)>=f&&p>=n){let P=y,E=(y+u)%e,S=(m-u+e)%e,I=m,W=t[P],v=t[E],T=t[S],R=t[I],D=Math.atan2(v.z-W.z,v.x-W.x),V=Math.atan2(R.z-T.z,R.x-T.x)-D;V>Math.PI?V-=2*Math.PI:V<-Math.PI&&(V+=2*Math.PI);let C=Math.abs(V);if(!g.some(A=>{function L(B,O,$){return B<=O?$>=B&&$<=O:$>=B||$<=O}let k=0,F=0;for(let B=y;F++,L(A.start,A.end,B)&&k++,B!==m;B=(B+1)%e);return F>0&&k/F>.7})){let A=g.findIndex(L=>{function k(O,$,Z){return O<=$?Z>=O&&Z<=$:Z>=O||Z<=$}let F=0,B=0;for(let O=L.start;B++,k(y,m,O)&&F++,O!==L.end;O=(O+1)%e);return B>0&&F/B>.7});A>=0?p>g[A].cumAngle&&(g[A]={start:y,end:m,cumAngle:p,actualAngle:C}):g.push({start:y,end:m,cumAngle:p,actualAngle:C})}}}return g}function Ya(t,e){let s=new Array(e).fill(!1);for(let d=0;d<e;d++){let f=t[d],c=t[(d+15)%e],l=t[(d+30)%e],h=Math.atan2(c.z-f.z,c.x-f.x),g=Math.atan2(l.z-c.z,l.x-c.x),x=Math.abs(g-h);x>Math.PI&&(x=2*Math.PI-x),x>.07&&(s[(d+15)%e]=!0)}let i=new Set,a=[],u=-1;for(let d=0;d<e*2;d++){let f=d%e;if(s[f])u<0&&(u=f);else if(u>=0){let c=(f-1+e)%e,l=0;for(let h=u;l++,h!==c;h=(h+1)%e);if(l>=12){let h=`${u},${c}`;i.has(h)||(i.add(h),a.push({start:u,end:c}))}u=-1}}if(a.length>1){let d=a[0],f=a[a.length-1];f.end>e-3&&d.start<3&&(a[0]={start:f.start,end:d.end},a.pop())}return a}function Ua(t,e){let n=[10,15,20,25,30,40],o=.15,r=.15,s=.5,i=3,a=6,u=new Array(e).fill(0);for(let c of n)if(!(c>=e))for(let l=0;l<e;l+=i){let h=l+c;if(h>=e)break;let g=[];for(let w=l;w<=h;w++)g.push(t[w]);if(g.length<c-1)continue;let{cx:x,cz:y,r:m,rmsResidual:p}=dn(g);if(m<.1||p>o||m<s)continue;let b=0;for(let w=1;w<g.length;w++){let z=g[w-1].x-x,M=g[w-1].z-y,P=g[w].x-x,E=g[w].z-y;b+=Math.abs(Math.atan2(z*E-M*P,z*P+M*E))}if(!(b<r))for(let w=l;w<=h;w++)u[w]++}let d=[],f=-1;for(let c=0;c<e*2;c++){let l=c%e;if(u[l]>=2)f<0&&(f=l);else if(f>=0){let h=(l-1+e)%e,g=0;for(let x=f;g++,x!==h;x=(x+1)%e);g>=a&&d.push({start:f,end:h}),f=-1}}if(d.length>1){let c=d[0],l=d[d.length-1];l.end>e-3&&c.start<3&&(d[0]={start:l.start,end:c.end},d.pop())}return d}function Ka(t,e,n){function o(h,g){return h<=g?g-h+1:n-h+(g+1)}function r(h,g,x){return h<=g?x>=h&&x<=g:x>=h||x<=g}let s=o(t.start,t.end),i=o(e.start,e.end),a=s<=i?t.start:e.start,u=s<=i?t.end:e.end,d=s<=i?e.start:t.start,f=s<=i?e.end:t.end,c=0;for(let h=a;r(d,f,h)&&c++,h!==u;h=(h+1)%n);let l=s+i-c;return l>0?c/l:0}function Qa(t){if(t.length<2)return[];let e=[],n=.15,o=1.22,r=1.92;for(let a=0;a<t.length;a++){let u=t[a];for(let d=a+1;d<t.length;d++){let f=t[d],c=u.bx-u.ax,l=u.bz-u.az,h=f.bx-f.ax,g=f.bz-f.az,x=Math.sqrt(c*c+l*l),y=Math.sqrt(h*h+g*g);if(x<.01||y<.01)continue;let m=Math.abs(Math.atan2(c*g-l*h,c*h+l*g));if(m<o||m>r)continue;let p=[{x:u.ax,z:u.az},{x:u.bx,z:u.bz}],b=[{x:f.ax,z:f.az},{x:f.bx,z:f.bz}];for(let w of p)for(let z of b){let M=w.x-z.x,P=w.z-z.z;Math.sqrt(M*M+P*P)<n&&e.push({x:(w.x+z.x)/2,z:(w.z+z.z)/2})}}}if(e.length<2)return e;let s=[],i=new Array(e.length).fill(!1);for(let a=0;a<e.length;a++){if(i[a])continue;let u=e[a].x,d=e[a].z,f=1;i[a]=!0;for(let c=a+1;c<e.length;c++){if(i[c])continue;let l=e[c].x-u/f,h=e[c].z-d/f;Math.sqrt(l*l+h*h)<.3&&(u+=e[c].x,d+=e[c].z,f++,i[c]=!0)}s.push({x:u/f,z:d/f})}return s}function Js(t,e){if(t.length<6)return[];let n={minCurveAngle:.05,minCurvePoints:5,smoothWindow:3,...e},o=t.length,r=[];for(let T=0;T<o;T++){let R=t[(T-1+o)%o],D=t[T],H=t[(T+1)%o],V=D.x-R.x,C=D.z-R.z,_=H.x-D.x,A=H.z-D.z,L=Math.sqrt(V*V+C*C),k=Math.sqrt(_*_+A*A);if(L<.001||k<.001){r.push(0);continue}r.push(Math.atan2((V*A-C*_)/(L*k),(V*_+C*A)/(L*k)))}let s=[],i=n.smoothWindow;for(let T=0;T<o;T++){let R=0;for(let D=-i;D<=i;D++)R+=r[(T+D+o)%o];s.push(R/(i*2+1))}let a=.008,u=[],d=-1,f=0,c=new Set;for(let T=0;T<o*2;T++){let R=T%o,D=s[R],H=D>=0?1:-1;if(Math.abs(D)>a&&(f===0||f===H))d<0&&(d=R,f=H);else if(d>=0){let V=(R-1+o)%o,C=0,_=0;for(let A=d;C+=Math.abs(s[A]),_++,A!==V;A=(A+1)%o);if(_>=n.minCurvePoints&&C>n.minCurveAngle){let A=`${d},${V}`;c.has(A)||(c.add(A),u.push({start:d,end:V,sign:f}))}d=-1,f=0}}if(u.length>1){let T=u[0],R=u[u.length-1];T.sign===R.sign&&R.end>o-3&&T.start<3&&(u[0]={start:R.start,end:T.end,sign:T.sign},u.pop())}{let T=s.filter(V=>Math.abs(V)>.001).map(V=>Math.abs(V)),R=T.length>0?Math.max(...T):0,D=T.length>0?T.reduce((V,C)=>V+C,0)/T.length:0,H=u.reduce((V,C)=>{let _=0;for(let A=C.start;_++,A!==C.end;A=(A+1)%o);return V+_},0)}let l=Ya(t,o),h=Ua(t,o),g=[...u,...l.map(T=>({start:T.start,end:T.end,sign:1})),...h.map(T=>({start:T.start,end:T.end,sign:1}))],x=u.length,m=g.map((T,R)=>{let D=[];for(let Z=T.start;D.push({...t[Z]}),Z!==T.end;Z=(Z+1)%o);let H=0;for(let Z=1;Z<D.length;Z++)s[(T.start+Z-1)%o]*s[(T.start+Z)%o]<-1e-8&&H++;let{cx:V,cz:C,r:_,rmsResidual:A}=dn(D),L=0,k=0;for(let Z=1;Z<D.length;Z++){let Q=D[Z-1].x-V,j=D[Z-1].z-C,X=D[Z].x-V,U=D[Z].z-C,at=Math.atan2(Q*U-j*X,Q*X+j*U);L+=Math.abs(at),k+=at}let F=Re(D),B=H===0?.12:.08;if(_>.1&&A>B||_>.1&&_>Math.max(F*15,8)||Math.abs(k)<.05)return null;let O=R<x?"turning":"sliding",$=O!=="turning"?Math.abs(k)>.35?"C_curve":"S_curve":H>0?"S_curve":"C_curve";return{seg:{points:D,type:$,radius:_,centerX:V,centerZ:C,arcAngle:L,arcLength:F},start:T.start,end:T.end,source:O}}).filter(T=>T!==null),p=new Set;function b(T,R){return T<=R?R-T+1:o-T+(R+1)}function w(T,R,D,H){for(let V=T;;V=(V+1)%o){let C;if(D<=H?C=V>=D&&V<=H:C=V>=D||V<=H,!C)return!1;if(V===R)break}return!0}for(let T=0;T<m.length;T++)if(!p.has(T))for(let R=T+1;R<m.length;R++){if(p.has(R))continue;if(Ka({start:m[T].start,end:m[T].end},{start:m[R].start,end:m[R].end},o)>.6){let V=(m[T].seg.arcAngle>=m[R].seg.arcAngle?T:R)===T?R:T;if(m[T].seg.arcAngle>=m[R].seg.arcAngle)p.add(R);else{p.add(T);break}}else{let H=b(m[T].start,m[T].end),V=b(m[R].start,m[R].end),C=H<=V?T:R,_=H<=V?R:T;w(m[C].start,m[C].end,m[_].start,m[_].end)&&b(m[C].start,m[C].end)/b(m[_].start,m[_].end)<.7&&p.add(C)}}let z=m.filter((T,R)=>!p.has(R)),M=10,P=10,E=30,S=15,I=[],W=0;if(z.length>0){for(let D of z)D.start=(D.start-S+o)%o,D.end=(D.end+S)%o;let T=[...z].sort((D,H)=>D.start-H.start),R=T[0];for(let D=1;D<T.length;D++){let H=T[D],V=H.start-R.end;V<0&&(R.end>o*.5&&H.start<o*.5?V=H.start+o-R.end:V=0);let C=V===0,_=V<=E&&C;if(!_&&V<=E){let A=Math.sqrt((R.seg.centerX-H.seg.centerX)**2+(R.seg.centerZ-H.seg.centerZ)**2),L=Math.max(R.seg.radius,H.seg.radius),k=Math.min(R.seg.radius,H.seg.radius),F=k>0?L/k:99;_=A<P&&F<M}if(_){let A=Math.min(R.start,H.start),L=Math.max(R.end,H.end),k=[];for(let Z=A;k.push({...t[Z]}),Z!==L;Z=(Z+1)%o);let{cx:F,cz:B,r:O}=dn(k),$=0;for(let Z=1;Z<k.length;Z++){let Q=k[Z-1].x-F,j=k[Z-1].z-B,X=k[Z].x-F,U=k[Z].z-B;$+=Math.abs(Math.atan2(Q*U-j*X,Q*X+j*U))}R={seg:{points:k,type:"C_curve",radius:O,centerX:F,centerZ:B,arcAngle:$,arcLength:Re(k)},start:R.start,end:H.end,source:R.source},W++}else I.push(R),R=H}I.push(R)}{if(I.length>1){let R=I.map((D,H)=>{if(H===0)return 0;let V=D.start-I[H-1].end;return V<0&&(V=D.start+o-I[H-1].end),V})}let T=[];for(let R=0;R<I.length;R++){if(T.length===0){T.push(I[R]);continue}let D=T[T.length-1],H=I[R].start-D.end;if(H<0&&(H=I[R].start+o-D.end),H<=30){let V=[...D.seg.points];for(let k=(D.end+1)%o;V.push({...t[k]}),k!==I[R].end;k=(k+1)%o);let{cx:C,cz:_,r:A}=dn(V),L=0;for(let k=1;k<V.length;k++){let F=V[k-1].x-C,B=V[k-1].z-_,O=V[k].x-C,$=V[k].z-_;L+=Math.abs(Math.atan2(F*$-B*O,F*O+B*$))}T[T.length-1]={seg:{points:V,type:"C_curve",radius:A,centerX:C,centerZ:_,arcAngle:L,arcLength:Re(V)},start:D.start,end:I[R].end,source:D.source}}else T.push(I[R])}I=T}return I.map((T,R)=>{let D=T.seg;return D.candidateId=R,D})}function Ja(t){let e=t.length;if(e<6)return null;let n=Ga(t,e,.35);if(n.length===0)return null;let o=n.filter(l=>l.actualAngle!=null&&l.actualAngle>.17);if(o.length===0)return null;o.sort((l,h)=>{let g=l.end>=l.start?l.end-l.start+1:e-l.start+l.end+1;return(h.end>=h.start?h.end-h.start+1:e-h.start+h.end+1)-g});let r=o[0],s=[];for(let l=r.start;s.push({...t[l]}),l!==r.end;l=(l+1)%e);if(s.length<10)return null;let{cx:i,cz:a,r:u,rmsResidual:d}=dn(s);if(u>.1&&d>.1)return null;let f=r.actualAngle,c=s.reduce((l,h,g)=>g===0?0:l+Math.hypot(h.x-s[g-1].x,h.z-s[g-1].z),0);return u>.3&&f>.17?{points:s,type:"C_curve",radius:u,centerX:i,centerZ:a,arcAngle:f,arcLength:c}:null}function ho(t,e,n){let o={...nr,...n},r=Date.now(),s=t,i=Array.isArray(e)?e:[],a=[];if(i.length>10)try{for(let T of i){let R=T.x1,D=T.z1,H=T.x2,V=T.z2;if(R==null||D==null||H==null||V==null){let C=T.start?.x,_=T.start?.y,A=T.end?.x,L=T.end?.y;if(C==null||_==null||A==null||L==null)continue;a.push({ax:C,az:_,bx:A,bz:L})}else a.push({ax:R,az:D,bx:H,bz:V})}let v=.1*.1;if(a.length>5){let T=[],R=[],D=new Array(a.length).fill(0);for(let V of t){let C=1/0,_=-1;for(let A=0;A<a.length;A++){let L=a[A],k=L.bx-L.ax,F=L.bz-L.az,B=k*k+F*F,O;if(B<1e-8)O=(V.x-L.ax)**2+(V.y-L.az)**2;else{let $=((V.x-L.ax)*k+(V.y-L.az)*F)/B;$=Math.max(0,Math.min(1,$));let Z=L.ax+$*k,Q=L.az+$*F;O=(V.x-Z)**2+(V.y-Q)**2}if(O<C&&(C=O,_=A),C<=v)break}C<=v&&(R.push(V),T.push(_),D[_]++)}let H=R;if(o.lineDensityMin>0&&o.lineDensityMinLen>0){let V=new Set;for(let C=0;C<a.length;C++){let _=a[C],A=Math.hypot(_.bx-_.ax,_.bz-_.az);if(A>=o.lineDensityMinLen)continue;(A>.01?D[C]/A:0)<o.lineDensityMin&&V.add(C)}if(V.size>0){let C=[];for(let _=0;_<R.length;_++)V.has(T[_])||C.push(R[_]);H=C}}s=H}}catch(v){console.warn("[\u70B9\u4E91\u8F6E\u5ED3] \u7EBF\u6BB5\u8FC7\u6EE4\u5931\u8D25:",v)}let u=Qa(a),d=[];for(let v of s){let T=v.z;T<o.heightMin||T>o.heightMax||d.push({x:v.x,z:v.y})}if(d.length<50)return Ke(e,r);if(o.sparseRadius>0){let v=new Map,T=o.cellSize;for(let V of d){let C=`${Math.round(V.x/T)},${Math.round(V.z/T)}`;v.has(C)||v.set(C,[]),v.get(C).push(V)}let R=[],D=Math.ceil(o.sparseRadius/T);for(let V=-D;V<=D;V++)for(let C=-D;C<=D;C++)R.push(`${V},${C}`);let H=[];for(let V of d){let C=Math.round(V.x/T),_=Math.round(V.z/T),A=0;for(let L of R){let[k,F]=L.split(",").map(Number),B=v.get(`${C+k},${_+F}`);if(B&&(A+=B.length),A>=o.sparseMinNeighbors)break}A>=o.sparseMinNeighbors&&H.push(V)}d.length=0;for(let V=0;V<H.length;V++)d.push(H[V])}if(d.length<50)return Ke(e,r);let f=Date.now(),{cells:c}=fo(d,o.cellSize),l=new Set;if(o.densityRatio>0&&c.size>10){let v=Array.from(c.values()).sort((D,H)=>D-H),T=v[Math.floor(v.length/2)],R=Math.max(o.densityThreshold,Math.ceil(T*o.densityRatio));for(let[D,H]of c)H>=R&&l.add(D);if(l.size<o.minComponentCells){l.clear();for(let[D,H]of c)H>=o.densityThreshold&&l.add(D)}}else for(let[v,T]of c)T>=o.densityThreshold&&l.add(v);if(l.size<o.minComponentCells)return Ke(e,r);let h=l;o.closeIterations>0&&(h=Wn(l,o.closeIterations));let g=Date.now(),x=tr(h).filter(v=>v.size>=o.minComponentCells);x.sort((v,T)=>T.size-v.size);let y=20;x.length>y&&(x=x.slice(0,y));let m=Date.now(),p=[];for(let v of x){let T=er(v,o.cellSize);T.length>=4&&p.push(T)}let b=Date.now(),w=p.map(v=>{let T=Hn(v,o.simplifyEpsilon);if(T.length>=7)for(let R=0;R<2;R++){let D=T.length,H=T.map(V=>({x:V.x,z:V.z}));for(let V=0;V<D;V++){let C=T[(V-1+D)%D],_=T[V],A=T[(V+1)%D],L=_.x-C.x,k=_.z-C.z,F=A.x-_.x,B=A.z-_.z,O=Math.sqrt(L*L+k*k),$=Math.sqrt(F*F+B*B);if(O<.001||$<.001)continue;if(Math.abs(Math.atan2(L*B-k*F,L*F+k*B))>1.8){let Q=[-2,-1,0,1,2].map(U=>T[(V+U+D)%D]),j=Q.map(U=>U.x).sort((U,at)=>U-at),X=Q.map(U=>U.z).sort((U,at)=>U-at);H[V]={x:j[2],z:X[2]}}}T=H}if(T=Jo(T),u.length>0&&T.length>=5)for(let R of u){let D=1/0,H=-1;for(let V=0;V<T.length;V++){let C=T[V].x-R.x,_=T[V].z-R.z,A=C*C+_*_;A<D&&(D=A,H=V)}H>=0&&D<.3*.3&&(T[H]={x:R.x,z:R.z})}if(T.length>2){let R=T[0],D=T[T.length-1];(Math.abs(R.x-D.x)>.001||Math.abs(R.z-D.z)>.001)&&T.push({...R})}return T}),z=o.cellSize/2;function M(v){let T=0;for(let R=0;R<v.length;R++){let D=(R+1)%v.length;T+=v[R].x*v[D].z-v[D].x*v[R].z}return T/2}let P=w.map(v=>{if(v.length<3)return v;let T=M(v)>=0;return v.map((R,D)=>{let H=v[(D-1+v.length)%v.length],V=v[(D+1)%v.length],C=V.x-H.x,_=V.z-H.z;if(Math.sqrt(C*C+_*_)<.001)return{...R};let L=T?_:-_,k=T?-C:C,F=Math.sqrt(L*L+k*k);return F<.001?{...R}:{x:R.x+L/F*z,z:R.z+k/F*z}})}),E=Date.now(),S=[];for(let v=0;v<P.length;v++){let T=P[v];T.length<3||S.push({id:-v-1,points:T,length:Re(T),pointCount:T.length,type:"straight",isMain:!1})}let I=Date.now()-r,W=[];for(let v of w){let T=Js(v);for(let D of T)W.push(D);let R=Ja(v);R&&W.push(R)}return{mainWalls:[],noiseWalls:[],contours:S,curvedSegments:W,filteredPoints:s,stats:{totalRawLines:e?.length??t.length,totalWalls:0,mainCount:0,noiseCount:S.length,elapsedMs:I}}}function ti(t,e){let n=e?.cellSize??.05,o=e?.closeIterations??2,r=e?.minComponentCells??10,s=e?.simplifyEpsilon??.02,i=Date.now();if(t.length<3)return Ke(t,i);let a=t.map(S=>({x1:S.x1,z1:S.z1,x2:S.x2,z2:S.z2})),{active:u}=Ks(a,n);if(u.size<r)return Ke(t,i);let d=Date.now(),f=Wn(u,o),c=Date.now(),l=tr(f).filter(S=>S.size>=r);if(l.length===0)return Ke(t,i);let h=l.reduce((S,I)=>S.size>=I.size?S:I),g=Date.now(),x=er(h,n);if(x.length<4)return Ke(t,i);let y=Date.now(),m=Hn(x,s);if(m=Jo(m),m.length>2){let S=m[0],I=m[m.length-1];(Math.abs(S.x-I.x)>.001||Math.abs(S.z-I.z)>.001)&&m.push({...S})}let p=n/2;function b(S){let I=0;for(let W=0;W<S.length;W++){let v=(W+1)%S.length;I+=S[W].x*S[v].z-S[v].x*S[W].z}return I/2}let w=b(m)>=0,z=m.map((S,I)=>{let W=m[(I-1+m.length)%m.length],v=m[(I+1)%m.length],T=v.x-W.x,R=v.z-W.z;if(Math.sqrt(T*T+R*R)<.001)return{...S};let H=w?R:-R,V=w?-T:T,C=Math.sqrt(H*H+V*V);return C<.001?{...S}:{x:S.x+H/C*p,z:S.z+V/C*p}}),M=Date.now(),P=[];z.length>=3&&P.push({id:-1,points:z,length:Re(z),pointCount:z.length,type:"straight",isMain:!0});let E=Date.now()-i;return{mainWalls:[],noiseWalls:[],contours:P,stats:{totalRawLines:t.length,totalWalls:0,mainCount:0,noiseCount:P.length,elapsedMs:E}}}function Ke(t,e){return{mainWalls:[],noiseWalls:[],stats:{totalRawLines:t?.length??0,totalWalls:0,mainCount:0,noiseCount:0,elapsedMs:Date.now()-e}}}function mo(t,e,n){if(!t.length||n<=1)return t;let o=new Map;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.y/e),f=`${u},${d}`;o.has(f)||o.set(f,[]),o.get(f).push(a)}let r=new Map;for(let[a,u]of o){let[d,f]=a.split(","),c=parseInt(d),l=parseInt(f),h=0;for(let g=-2;g<=2;g++)for(let x=-2;x<=2;x++){let y=`${c+g},${l+x}`;h+=o.get(y)?.length??0}r.set(a,h)}let s=[],i=0;for(let a of t){let u=Math.floor(a.x/e),d=Math.floor(a.y/e),f=`${u},${d}`;(r.get(f)??0)>=n?s.push(a):i++}return s}function po(t,e,n){if(!t.length||n<=1||e<=0)return t;let o=new Map;for(let s of t){let i=Math.floor(s.x/e),a=Math.floor(s.y/e),u=Math.floor(s.z/e),d=`${i},${a},${u}`;o.has(d)||o.set(d,[]),o.get(d).push(s)}let r=[];for(let s of o.values())if(s.length>=n){let i=s.reduce((d,f)=>d+f.x,0)/s.length,a=s.reduce((d,f)=>d+f.y,0)/s.length,u=s.reduce((d,f)=>d+f.z,0)/s.length;r.push({x:i,y:a,z:u})}return r}function ei(t,e,n){let o=po(t,e,n),r=t.length-o.length;return o}function ni(t){let e=[];for(let d of t){let f=d.originalPoints??d.points??[];if(!Array.isArray(f)||f.length<2)continue;let c=Math.min(f.length,200);for(let l=1;l<c;l++){let h=Math.hypot(f[l].x-f[l-1].x,f[l].y-f[l-1].y,f[l].z-f[l-1].z);h>.001&&e.push(h)}}let n={voxelSize:.08,minPoints:3,minCentroids:3};if(e.length<10)return n;e.sort((d,f)=>d-f);let o=e[Math.floor(e.length/2)],r=Math.max(.03,Math.min(.25,o*3.5));r=Math.round(r*100)/100;let s=Math.pow(r/o,2),i=Math.max(2,Math.min(5,Math.round(s/4))),a=e.length/Math.max(1,t.length),u=Math.max(2,Math.min(4,Math.round(a/50)));return{voxelSize:r,minPoints:i,minCentroids:u}}function oi(t,e,n,o,r=3){if(!t.length||n<=0||o<=1)return{data:t,lines:e,removedCount:0};let s=[],i=[],a=0;for(let u=0;u<t.length;u++){let d=t[u],f=d.originalPoints??d.points??[];if(!Array.isArray(f)||f.length===0){s.push(d),i.push(e[u]);continue}let c=po(f,n,o);c.length>=r?(d.originalPoints&&(d.originalPoints=c),d.points&&(d.points=c),s.push(d),i.push(e[u])):a++}return{data:s,lines:i,removedCount:a}}function ri(t,e,n,o){let r=[],s=[],i=0;for(let a=0;a<t.length;a++){let u=t[a],d=u.originalPoints??u.points??[],c=(Array.isArray(d)?d.filter(y=>y&&y.x!==void 0&&y.y!==void 0):[]).length;if(c===0){r.push(u),s.push({...e[a],id:r.length-1});continue}let l=u.start??u,h=u.end??u,g=Math.hypot(h.x??0-l.x??0,h.y??0-l.y??0);if(g>=o||g<=.01){r.push(u),s.push({...e[a],id:r.length-1});continue}if(c/g<n){i++;continue}r.push(u),s.push({...e[a],id:r.length-1})}return{data:r,lines:s}}function Qe(t,e,n,o,r,s){let i=r-n,a=s-o,u=i*i+a*a;if(u===0)return Math.hypot(t-n,e-o);let d=((t-n)*i+(e-o)*a)/u;return d=Math.max(0,Math.min(1,d)),Math.hypot(t-(n+d*i),e-(o+d*a))}function or(t,e,n=.2){for(let o of e)if(Qe(o.x,o.z,t.x1,t.z1,t.x2,t.z2)<=n)return!0;return!1}function si(t,e,n=.2){let o=1/0,r={x:0,z:0};for(let i of e){let a=Qe(i.x,i.z,t.x1,t.z1,t.x2,t.z2);a<o&&(o=a,r=i)}return o<=n}function ii(t,e,n,o=!0){function g(b,w,z,M){let P=z.points[0],E=z.points[z.points.length-1];return Math.hypot(b-P.x,w-P.z)<=M||Math.hypot(b-E.x,w-E.z)<=M}function x(b,w,z,M){for(let P=0;P<z.points.length-1;P++)if(Qe(b,w,z.points[P].x,z.points[P].z,z.points[P+1].x,z.points[P+1].z)<=M)return!0;return!1}function y(b,w,z){let M=0,P=b[0],E=b[b.length-1];for(let S of w)if(g(P.x,P.z,S,z)||x(P.x,P.z,S,z)){M++;break}for(let S of w)if(g(E.x,E.z,S,z)||x(E.x,E.z,S,z)){M++;break}return M}function m(b){if(b.length<2)return 0;if(b.length<=30){let P=0;for(let E=0;E<b.length;E++)for(let S=E+1;S<b.length;S++){let I=Math.hypot(b[E].x-b[S].x,b[E].z-b[S].z);I>P&&(P=I)}return P}let w=Math.max(1,Math.floor(b.length/15)),z=[];for(let P=0;P<b.length;P+=w)z.push(b[P]);z[z.length-1]!==b[b.length-1]&&z.push(b[b.length-1]);let M=0;for(let P=0;P<z.length;P++)for(let E=P+1;E<z.length;E++){let S=Math.hypot(z[P].x-z[E].x,z[P].z-z[E].z);S>M&&(M=S)}return M}let p=[];for(let b of t){let w=b.points,z=w.length>=2?m(w):0,M=b.candidateId!==void 0?`#${b.candidateId}`:"#?";if(o&&console.log(`[\u5F27\u6BB5\u5206\u6790] ${M} r=${b.radius.toFixed(3)}m \u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0 \u5F26\u957F=${z.toFixed(3)}m pts=${w.length}`),b.radius<.2){o&&console.log(` \u2192 \u534A\u5F84<${.2}m \u8FC7\u6EE4`);continue}if(w.length<3){o&&console.log(" \u2192 \u70B9\u6570<3 \u8FC7\u6EE4");continue}let P=z/b.radius,E=z/2,S=0;E<=b.radius?S=b.radius-Math.sqrt(b.radius*b.radius-E*E):S=z;let I=z>.01?S/z:0,W=y(w,e,.8),v=b.arcAngle>1.745&&b.radius<5,T=b.radius>15&&P>.15&&w.length>=10,R=P>.12&&b.radius>1&&b.radius<4&&b.arcAngle>=.35&&w.length>=3;if(o&&console.log(` \u7279\u5F81: \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} \u5F13\u9AD8/\u5F26\u957F=${I.toFixed(4)} \u8FDE\u63A5=${W}/2${v?" \u5927\u5F27\u89D2\u8C41\u514D":""}${T?" \u5927\u534A\u5F84\u7ED3\u6784\u5F27":""}${R?" \u5408\u7406\u77ED\u5F27\u8C41\u514D":""}`),w.length>20&&b.arcAngle>5.2&&b.radius>.2){o&&console.log(` \u2192 \u8FD1\u95ED\u5408\u5706\u8C41\u514D(\u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0, ${w.length}pts)`),p.push(b);continue}if(P<.15)if(W>=2&&b.arcAngle>=.35)o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} < ${.15}\uFF0C\u4F46\u4E24\u7AEF\u8FDE\u63A5+\u5F27\u89D2>20\xB0\uFF0C\u8C41\u514D`);else{o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} < ${.15}\uFF0C\u8F6E\u5ED3\u6296\u52A8\u5047\u5F27 \u8FC7\u6EE4`);continue}let D=W>=2?.025:.035;if(I<D&&!v&&!R&&!T){o&&console.log(` \u2192 \u5F13\u9AD8/\u5F26\u957F=${I.toFixed(4)} < ${D}(${W>=2?"\u5BBD\u677E":"\u6807\u51C6"})\uFF0C\u6781\u6241\u5F27(\u76F4\u7EBF\u6BB5\u566A\u58F0) \u8FC7\u6EE4`);continue}if(!v&&!R&&!T){if(b.arcAngle<.17){o&&console.log(` \u2192 \u5F27\u89D2<${(.17*180/Math.PI).toFixed(0)}\xB0 \u8FC7\u6EE4`);continue}if(b.arcAngle<.35&&W<2){o&&console.log(` \u2192 \u5F27\u89D2<${(.35*180/Math.PI).toFixed(0)}\xB0 \u4E14\u672A\u4E24\u7AEF\u8FDE\u63A5 \u8FC7\u6EE4`);continue}}if(P>2.01){o&&console.log(` \u2192 \u5F26\u957F/\u534A\u5F84=${P.toFixed(3)} > 2.01\uFF0C\u5F26\u957F\u8D85\u8FC7\u76F4\u5F84\uFF0C\u5F02\u5E38 \u8FC7\u6EE4`);continue}if(!v&&!T){let H=W>=2?2.5:1.5,V=Math.floor(w.length/2),C=w[V].x-w[0].x,_=w[V].z-w[0].z,A=w[w.length-1].x-w[V].x,L=w[w.length-1].z-w[V].z,k=Math.atan2(_,C),F=Math.atan2(L,A),B=Math.abs(F-k);if(B>Math.PI&&(B=2*Math.PI-B),B>60*Math.PI/180&&B>b.arcAngle*H){o&&console.log(` \u2192 V\u5F62\u62D0\u89D2\u8FC7\u6EE4 \u8F6C\u89D2=${(B*180/Math.PI).toFixed(1)}\xB0 > \u5F27\u89D2\xD7${H.toFixed(1)}=${(b.arcAngle*180/Math.PI*H).toFixed(1)}\xB0`);continue}}if(n&&n.length>5&&!(b.radius>15&&b.arcAngle>.26)){let H=w[0],V=w[w.length-1],C=.3,_=null,A=1/0,L=null,k=1/0;for(let F of n){let B=Qe(H.x,H.z,F.x1,F.z1,F.x2,F.z2),O=Qe(V.x,V.z,F.x1,F.z1,F.x2,F.z2);B<C&&B<A&&(_=F,A=B),O<C&&O<k&&(L=F,k=O)}if(!_||!L)console.log(` [\u7EBF\u6BB5\u8F6C\u89D2\u8C03\u8BD5 #${M}] \u672A\u627E\u5230\u5B8C\u6574\u7EBF\u6BB5: \u8D77\u7AEF=${!!_}(d=${A.toFixed(3)}) \u672B\u7AEF=${!!L}(d=${k.toFixed(3)})`);else if(_===L){console.log(" \u2192 \u7EBF\u6BB5\u8F6C\u89D2\u9A8C\u8BC1: \u5F27\u4E24\u7AEF\u5728\u540C\u4E00\u6761\u7EBF\u6BB5\u4E0A, \u76F4\u7EBF\u566A\u58F0\u5047\u5F27 \u8FC7\u6EE4");continue}else{let F=_.x2-_.x1,B=_.z2-_.z1,O=L.x2-L.x1,$=L.z2-L.z1,Z=Math.sqrt(F*F+B*B),Q=Math.sqrt(O*O+$*$);if(Z>.01&&Q>.01){let j=Math.abs(Math.atan2(F*$-B*O,F*O+B*$)),X=j*180/Math.PI;if(console.log(` [\u7EBF\u6BB5\u8F6C\u89D2\u8C03\u8BD5 #${M}] \u5F27\u4E24\u7AEF\u6BB5\u5939\u89D2=${X.toFixed(1)}\xB0 \u5F27\u89D2=${(b.arcAngle*180/Math.PI).toFixed(1)}\xB0`),j>.35&&j<3.05){let U=j>2.09;if(!U||b.arcAngle<.44){console.log(` \u2192 \u7EBF\u6BB5\u8F6C\u89D2\u9A8C\u8BC1: \u5F27\u4E24\u7AEF\u6BB5\u5939\u89D2=${X.toFixed(1)}\xB0\uFF0C${U?"\u659C\u5899":"\u6298\u5899"}\u78E8\u5706\u5047\u5F27 \u8FC7\u6EE4`);continue}}}}}o&&console.log(" \u2192 \u2713 \u4FDD\u7559"),p.push(b)}return o&&console.log(`[\u56DE\u52A0\u5224\u65AD] ${t.length} \u2192 ${p.length} \u4E2A\u5F27\u6BB5`),p}function li(t,e){for(let n=0;n<t.length;n++){let o=t[n],r=[];for(let i of e)or(i,o.points)&&r.push({id:i.id,len:Math.hypot(i.x2-i.x1,i.z2-i.z1)});r.sort((i,a)=>a.len-i.len);let s=r.map(i=>`#${i.id}(${i.len.toFixed(2)}m)`).join(", ");console.log(`[\u5F27\u6BB5\u539F\u59CB\u7EBF\u6BB5] #${n} r=${o.radius.toFixed(3)}m \u2192 ${r.length} \u6761: ${s}`)}}var sr={gridSize:2,connectDist:.8,angleTol:25,minLength:.8,minPointCount:3,hardCornerThreshold:30,minArea:5,minCurvePoints:6,curveStraightDist:.25,longWallMinLength:5};function ai(t){if(!t.length)return{rotated:[],mainAngle:0};let e=180,n=new Array(e).fill(0),o=Math.PI/e;for(let f of t){let c=f.x2-f.x1,l=f.z2-f.z1,h=Math.sqrt(c*c+l*l);if(h<.01)continue;let g=Se(c,l),x=Math.floor(g/o);x>=e&&(x=e-1),n[x]+=h}let r=new Array(e).fill(0),s=5;for(let f=0;f<e;f++){let c=0;for(let l=-s;l<=s;l++){let h=f+l;h<0&&(h+=e),h>=e&&(h-=e),c+=n[h]}r[f]=c}let i=0,a=0;for(let f=0;f<e;f++)r[f]>i&&(i=r[f],a=f);let u=(a+.5)*o;return{rotated:t.map(f=>{let c=In(f.x1,f.z1,-u),l=In(f.x2,f.z2,-u);return{...f,x1:c.x,z1:c.z,x2:l.x,z2:l.z}}),mainAngle:u}}function ci(t,e){let n=new Map;for(let o of t){let r=(o.x1+o.x2)/2,s=(o.z1+o.z2)/2,i=Math.floor(r/e),a=Math.floor(s/e),u=`${i},${a}`;n.has(u)||n.set(u,[]),n.get(u).push(o)}return n}function ui(t,e,n){if(t.length===0)return[];let o=new Set,r=[];for(let s of t){if(o.has(s.id))continue;let i=[s],a=[],u=new Set([s.id]);o.add(s.id);let d=!0;for(;d;){d=!1;let l=i[i.length-1],h=l.x2,g=l.z2,x=Se(l.x2-l.x1,l.z2-l.z1),y=null,m=e;for(let p of t){if(o.has(p.id))continue;let b=Me(h,g,p.x1,p.z1),w=Me(h,g,p.x2,p.z2),z=w<b,M=Math.min(b,w);if(M>m)continue;let P=z?Se(p.x1-p.x2,p.z1-p.z2):Se(p.x2-p.x1,p.z2-p.z1);Math.abs(Vn(P-x))>n||(y=p,m=M)}if(y){o.add(y.id),u.add(y.id);let p=i[i.length-1],b=Me(p.x2,p.z2,y.x1,y.z1),w=Me(p.x2,p.z2,y.x2,y.z2);b<=w?i.push(y):i.push({id:y.id,x1:y.x2,z1:y.z2,x2:y.x1,z2:y.z1}),d=!0}}for(d=!0;d;){d=!1;let l=a.length>0?a[a.length-1]:i[0],h=a.length>0?l.x2:l.x1,g=a.length>0?l.z2:l.z1,x=a.length>0?Se(l.x2-l.x1,l.z2-l.z1):Se(l.x2-l.x1,l.z2-l.z1),y=null,m=e;for(let p of t){if(o.has(p.id))continue;let b=Me(h,g,p.x1,p.z1),w=Me(h,g,p.x2,p.z2),z=w<b,M=Math.min(b,w);if(M>m)continue;let P=z?Se(p.x1-p.x2,p.z1-p.z2):Se(p.x2-p.x1,p.z2-p.z1);Math.abs(Vn(P-x))>n||(y=p,m=M)}if(y){o.add(y.id),u.add(y.id);let p=Me(h,g,y.x1,y.z1),b=Me(h,g,y.x2,y.z2);p<=b?a.push({id:y.id,x1:y.x2,z1:y.z2,x2:y.x1,z2:y.z1}):a.push({...y}),d=!0}}let f=[];for(let l=a.length-1;l>=0;l--){let h=a[l];l===a.length-1&&f.push({x:h.x1,z:h.z1}),f.push({x:h.x2,z:h.z2})}for(let l=0;l<i.length;l++){let h=i[l];l===0&&f.length===0&&f.push({x:h.x1,z:h.z1}),f.push({x:h.x2,z:h.z2})}let c=[];for(let l of f){if(c.length>0){let h=c[c.length-1];if(Math.abs(l.x-h.x)<.001&&Math.abs(l.z-h.z)<.001)continue}c.push(l)}if(c.length>=2){let l=Re(c);r.push({points:c,length:l,pointCount:i.length+a.length,lineIds:[...u]})}}return r}function fi(t){if(t.length<3)return[];let e=[];for(let n=1;n<t.length-1;n++){let o=t[n].x-t[n-1].x,r=t[n].z-t[n-1].z,s=t[n+1].x-t[n].x,i=t[n+1].z-t[n].z,a=Math.atan2(r,o),u=Math.atan2(i,s),d=Vn(u-a);e.push(d)}return e}function di(t,e=6){if(t.length<3)return"straight";let n=t[0],o=t[t.length-1],r=o.x-n.x,s=o.z-n.z;if(Math.sqrt(r*r+s*s)>.1){let f=0;for(let c of t){let l=Cn(c.x,c.z,n.x,n.z,o.x,o.z);l>f&&(f=l)}if(f<.05)return"straight"}let a=fi(t);if(a.reduce((f,c)=>f+Math.abs(c),0)<.1||t.length<e)return"straight";let d=0;for(let f=0;f<a.length-1;f++)a[f]*a[f+1]<0&&d++;return d>0?"S_curve":"C_curve"}function hi(t,e){if(t.length<3)return[t];let n=uo(e),o=fi(t),r=[],s=0;for(let i=0;i<o.length;i++)Math.abs(o[i])>n&&(r.push(t.slice(s,i+2)),s=i+1);return s<t.length-1&&r.push(t.slice(s)),r}function mi(t,e,n=6){let o=[],r=0;for(let s of t){let i=hi(s.points,e);for(let a of i){if(a.length<2)continue;let u=di(a,n),d=Re(a),f=d/(s.length||1),c=Math.max(1,Math.round(s.pointCount*f));o.push({id:r++,points:a,length:d,pointCount:c,lineIds:s.lineIds,type:u,isMain:!1})}}return o}function pi(t,e){if(t.length<3)return new Set;let n=.15;function o(d,f){return Math.round(d/n)+"_"+Math.round(f/n)}let r=new Map,s=0;function i(d,f){let c=o(d,f),l=r.get(c);if(l)return l;let h=Math.round(d/n),g=Math.round(f/n),x=null,y=n*n;for(let m=-1;m<=1;m++)for(let p=-1;p<=1;p++){let b=h+m+"_"+(g+p),w=r.get(b);if(!w)continue;let z=w.x-d,M=w.z-f,P=z*z+M*M;P<y&&(y=P,x=w)}return x?(r.set(c,x),x):(l={id:s++,x:d,z:f,edges:[]},r.set(c,l),l)}for(let d of t){if(d.points.length<2)continue;let f=d.points[0],c=d.points[d.points.length-1],l=i(f.x,f.z),h=i(c.x,c.z);if(l.id===h.id)continue;let g={to:h,wallId:d.id,visited:!1,twin:null},x={to:l,wallId:d.id,visited:!1,twin:g};g.twin=x,l.edges.push(g),h.edges.push(x)}for(let d of r.values())d.edges.sort((f,c)=>{let l=Math.atan2(f.to.z-d.z,f.to.x-d.x),h=Math.atan2(c.to.z-d.z,c.to.x-d.x);return l-h});let a=new Set,u=new Set;for(let d of r.values())for(let f of d.edges)u.add(f);for(let d of u){if(d.visited)continue;let f=[],c=d,l=u.size+10;for(;!c.visited&&f.length<l;){c.visited=!0,f.push(c);let h=c.to,g=c.twin,x=h.edges.indexOf(g);if(x<0)break;let y=(x+1)%h.edges.length;c=h.edges[y]}if(f.length>=3){let h=f[0];if(f[f.length-1].to.id===h.from?.id){let x=[],y=new Set,m=null;for(let p of r.values())if(p.edges.includes(f[0])){m=p;break}if(!m)continue;x.push({x:m.x,z:m.z});for(let p of f)y.add(p.wallId),x.push({x:p.to.x,z:p.to.z});if(f[f.length-1].to.id===m.id&&Qo(x)>=e)for(let b of y)a.add(b)}}}return a}function hn(t,e){let n={...sr,...e},o=Date.now(),{rotated:r,mainAngle:s}=ai(t),i=Date.now(),a=ci(r,n.gridSize),u=Date.now(),d=uo(n.angleTol),f=[];for(let v of a.values()){let T=v.map(D=>({id:D.id,x1:D.x1,z1:D.z1,x2:D.x2,z2:D.z2})),R=ui(T,n.connectDist,d);f.push(...R)}let c=Date.now(),l=new Set,h=f.filter(v=>{let T=v.length>=n.minLength||v.pointCount>=n.minPointCount;return T&&l.add(v),T}),g=f.filter(v=>!l.has(v)),x=Date.now(),y=mi(h,n.hardCornerThreshold,n.minCurvePoints),m=Date.now(),p=pi(y,n.minArea);y.forEach(v=>{v.isMain=p.has(v.id),v.length>=n.longWallMinLength&&(v.isMain=!0)});let b=Date.now();function w(v,T,R=.1){let D=v[0],H=v[v.length-1],V=T[0],C=T[T.length-1];return Math.hypot(D.x-V.x,D.z-V.z)<R||Math.hypot(D.x-C.x,D.z-C.z)<R||Math.hypot(H.x-V.x,H.z-V.z)<R||Math.hypot(H.x-C.x,H.z-C.z)<R}let z=[],M=[];for(let v of y){if(v.type==="straight"&&v.points.length>=2){let T=!1;for(let R of y){if(R.id===v.id||R.type==="straight"||w(v.points,R.points))continue;if(Us(v.points,R.points)<n.curveStraightDist){T=!0;break}}if(T){z.push({...v,id:-z.length-1});continue}}M.push(v)}let P=Date.now(),E=M,S=[];for(let v=0;v<g.length;v++){let T=g[v];S.push({id:-v-1,points:T.points,length:T.length,pointCount:T.pointCount,lineIds:T.lineIds,type:"straight",isMain:!1})}for(let v of z)S.push(v);let I=v=>v.map(T=>({...T,points:T.points.map(R=>In(R.x,R.z,s))})),W=Date.now()-o;return{mainWalls:I(E),noiseWalls:I(S),stats:{totalRawLines:t.length,totalWalls:y.length,mainCount:E.length,noiseCount:S.length,elapsedMs:W}}}function ir(t,e,n){let o={...nr,...n},r=Date.now(),s=[];for(let h of t){let g=h.z;g<o.heightMin||g>o.heightMax||s.push({x:h.x,z:h.y})}if(s.length<100)return hn(e);let{cells:i}=fo(s,o.cellSize),a=new Set;for(let[h,g]of i)g>=o.densityThreshold&&a.add(h);let u=Wn(a,o.morphIterations),d=[],f={total:0,lowDensity:0};for(let h of e){let g=h.x1,x=h.z1,y=h.x2,m=h.z2;if(Math.sqrt((y-g)**2+(m-x)**2)<.3){d.push(h);continue}let b=0,w=9;for(let z=0;z<w;z++){let M=z/(w-1),P=g+(y-g)*M,E=x+(m-x)*M,S=Math.floor(P/o.cellSize),I=Math.floor(E/o.cellSize);u.has(S+","+I)&&b++}b>=Math.ceil(w*.5)?d.push(h):f.lowDensity++,f.total++}if(d.length<5)return hn(e);let c=hn(d),l=Date.now()-r;return c}function rr(t){return{id:t.id,points:[{x:t.x1,z:t.z1},{x:t.x2,z:t.z2}],length:Math.hypot(t.x2-t.x1,t.z2-t.z1),pointCount:1,type:"straight",isMain:!1}}function lr(t,e,n){let o={outlierEnabled:!0,outlierCellSize:.1,outlierMinNeighbors:3,densityFilterEnabled:!1,...n},r=Date.now(),s=hn(t),i=s.mainWalls,a=s.noiseWalls,u=new Set(i.map(M=>M.id)),d=[];for(let M=0;M<e.length;M++){if(!u.has(M))continue;let P=e[M];if(!(!P||!Array.isArray(P)))for(let E of P)E&&E.x!==void 0&&E.y!==void 0&&E.z!==void 0&&d.push({x:E.x,y:E.y,z:E.z})}let f=d.length,c=d;o.outlierEnabled&&c.length>0&&(c=mo(c,o.outlierCellSize,o.outlierMinNeighbors));let l=[],h=[];if(c.length>=100){let M=o.pcConfig??{},P=ho(c,t,{cellSize:.05,densityThreshold:1,minComponentCells:8,simplifyEpsilon:.008,heightMin:.3,heightMax:2.8,closeIterations:8,densityRatio:.12,...M});l=P.contours??[],h=P.curvedSegments??[]}let g=[];if(h.length>0)for(let M=0;M<h.length;M++){let E=h[M].points;if(E.length<2)continue;let S=1/0,I=-1/0,W=1/0,v=-1/0;for(let D of E)D.x<S&&(S=D.x),D.x>I&&(I=D.x),D.z<W&&(W=D.z),D.z>v&&(v=D.z);let T=[];for(let D of t)si(D,E,.2)&&T.push(D.id);let R=E.map(D=>({x:D.x,y:D.z,z:0}));g.push({id:M,originalPoints:R,rawLineIds:T})}let x;if(o.densityFilterEnabled&&c.length>0&&i.length>0){let M=new Set(s.mainWalls.map(S=>S.id)),P=t.filter(S=>M.has(S.id));x=ir(c,P).mainWalls}let y=new Set;for(let M of s.mainWalls)if(M.lineIds)for(let P of M.lineIds)y.add(P);let m=new Set;for(let M of s.noiseWalls)if(M.lineIds)for(let P of M.lineIds)m.add(P);let p=t.filter(M=>y.has(M.id)).map(rr),b=t.filter(M=>m.has(M.id)).map(rr),w;if(x&&x.length>0){let M=new Set;for(let P of x)if(P.lineIds)for(let E of P.lineIds)M.add(E);w=t.filter(P=>M.has(P.id)).map(rr)}let z=Date.now()-r;return{mainWalls:p,noiseWalls:b,contours:l,curvedSegments:h,lineCurves:g.length>0?g:void 0,filteredPoints:c,secondMainWalls:w,stats:{totalRawLines:t.length,firstMainCount:p.length,firstNoiseCount:b.length,contourCount:l.length,curvedCount:h.length,secondMainCount:x?.length,mainWallPointCount:f,filteredPointCount:c.length,elapsedMs:z}}}function gi(t,e){let n=Array.isArray(t)?t:t?.data??[],o=[],r=[];for(let a=0;a<n.length;a++){let u=n[a],d=u.start??u,f=u.end??u;o.push({id:a,x1:d.x??0,y1:d.z??0,z1:d.y??0,x2:f.x??0,y2:f.z??0,z2:f.y??0});let c=u.originalPoints??u.points??[],l=Array.isArray(c)?c.filter(h=>h&&h.x!==void 0):[];r.push(l)}let s=lr(o,r,e),i=Array.isArray(t)?{data:t}:{...t};return i.lineCurves=s.lineCurves??[],i.lineCurveNum=s.curvedSegments?.length??0,i}export{sr as CONFIG,no as analyzeDoorQuad,je as analyzeQuad,po as applyVoxelFilter,ao as attachWindows,Ue as calcAngle,Xi as checkBeamIsReasonable,Qs as classifyWallByPointCloudPCA,hn as classifyWallShapes,ta as computeContour,Ge as computeDoorLocalFloorZ,$e as computeLocalFloorZ,ni as computeVoxelParams,ri as densityPreFilter,cn as detectDoorPerpendicularWalls,Fa as detectPerpWalls,Tn as detectPerpendicularWalls,kl as drawStaircase,ii as filterFalseArcCurves,Gs as filterWalls,Ys as filterWallsHard,vn as findDoorWalls,js as findMainByLoops,Rn as findWindowWalls,$i as getBeamLine,Ki as getColLine,Ri as getLineCheckResults,un as getMatcherAPI,Fl as getMergeFloorMeaning,Ol as getMergeMeaning,Si as getOneLineCheckResults,vi as getOneLineClusterResults,ea as getSlabData,Nl as getStaircase,or as isRawLineNearArc,li as logArcRawSegments,La as matchWalls,Zs as mergeCollinear,Xe as mergeCollinearSegments,Ye as mergeDoorCollinearSegments,Oa as mergeWallSegments,io as normalizeAngles,jt as pointToDoorSegmentDist,so as pointToSegDist,Qe as pointToSegDist2D,Ce as pointToSegmentDist,jo as processData,Yo as processDoorData,ir as processFilteredByPointCloud,ho as processFromPointCloud,ti as processFromSegments,$a as processPipeline,gi as processSegments,ka as processWallData,mo as removeOutlierPoints,He as rotateXZ,lr as runMainWallPipeline,$s as spatialGrid,Dn as splitDoorWallSegment,Na as splitWallSeg,Sn as splitWallSegment,Bi as updateStEdPoint,ei as voxelFilter,oi as voxelFilterLines};