@labelbee/lb-annotation 1.27.0-alpha.48 → 1.27.0-alpha.49

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.
@@ -13,6 +13,7 @@ export default class PolygonUtils {
13
13
  */
14
14
  static calcPolygonSize(pointList?: IPolygonPoint[]): number;
15
15
  static isInPolygon(checkPoint: IPolygonPoint, polygonPoints: (IPolygonPoint | ICoordinate)[], lineType?: ELineTypes): boolean;
16
+ static isPointInOrOnPolygon(point: IPolygonPoint, polygonData: (IPolygonPoint | ICoordinate)[], errorMargin?: number): boolean;
16
17
  /**
17
18
  * 通过数据转换为平滑曲线的点提交 [{x: number, y: number}...] => [x, ...smoothCurvePoints ,y]
18
19
  * @param pointList
@@ -1 +1 @@
1
- "use strict";var turf=require("@turf/turf"),annotation=require("../../constant/annotation.js"),CommonToolUtils=require("./CommonToolUtils.js"),tool=require("../../constant/tool.js"),AxisUtils=require("./AxisUtils.js"),MathUtils=require("../MathUtils.js"),LineToolUtils=require("./LineToolUtils.js"),__defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(L,e,t)=>e in L?__defProp(L,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):L[e]=t,__spreadValues=(L,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(L,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(L,t,e[t]);return L};class PolygonUtils{static getHoverPolygonID(e,t,n=3,i=tool.ELineTypes.Line){let r="",o=Infinity;const l=AxisUtils.default.axisArea(e,n);return t.forEach(s=>{s.pointList&&l.forEach(u=>{const a=this.calcPolygonSize(s.pointList);this.isInPolygon(u,s.pointList,i)&&a<o&&(r=s.id,o=a)})}),r}static calcPolygonSize(e=[]){if((e==null?void 0:e.length)<=2)return 0;const t=e.length,n=e.reduce((i,r,o,l)=>{const s=l[(o+1)%t];return i+r.x*s.y-s.x*r.y},0);return Math.abs(n)/2}static isInPolygon(e,t,n=tool.ELineTypes.Line){let i=0,r,o,l,s;t=[...t],n===tool.ELineTypes.Curve&&(t=this.createSmoothCurvePoints(t.reduce((a,h)=>[...a,h.x,h.y],[]),.5,!0,20)),[l]=t;const u=t.length;for(r=1;r<=u;r++)s=t[r%u],e.x>Math.min(l.x,s.x)&&e.x<=Math.max(l.x,s.x)&&e.y<=Math.max(l.y,s.y)&&l.x!==s.x&&(o=(e.x-l.x)*(s.y-l.y)/(s.x-l.x)+l.y,(l.y===s.y||e.y<=o)&&i++),l=s;return i%2!=0}static createSmoothCurvePointsFromPointList(e,t=tool.SEGMENT_NUMBER){return this.createSmoothCurvePoints(e.reduce((i,r)=>[...i,r.x,r.y],[]),.5,!1,t).map((i,r)=>{var o;const l=r/(tool.SEGMENT_NUMBER+1),s=Math.floor(l),u=(o=e[s])!=null?o:{};return __spreadValues(s===l?__spreadValues({},u):{specialEdge:u.specialEdge},i)})}static createSmoothCurvePoints(e,t=.5,n=!1,i=tool.SEGMENT_NUMBER){if(e.length<4)return e;const r=[],o=e.slice(0);let l,s,u,a,h,g,P,E,x,m,c,d,f;for(n?(o.unshift(e[e.length-1]),o.unshift(e[e.length-2]),o.unshift(e[e.length-1]),o.unshift(e[e.length-2]),o.push(e[0]),o.push(e[1])):(o.unshift(e[1]),o.unshift(e[0]),o.push(e[e.length-2]),o.push(e[e.length-1])),f=2;f<o.length-4;f+=2)for(u=(o[f+2]-o[f-2])*t,a=(o[f+4]-o[f-0])*t,h=(o[f+3]-o[f-1])*t,g=(o[f+5]-o[f+1])*t,d=0;d<=i;d++)c=d/i,P=2*Math.pow(c,3)-3*Math.pow(c,2)+1,E=-(2*Math.pow(c,3))+3*Math.pow(c,2),x=Math.pow(c,3)-2*Math.pow(c,2)+c,m=Math.pow(c,3)-Math.pow(c,2),l=P*o[f]+E*o[f+2]+x*u+m*a,s=P*o[f+1]+E*o[f+3]+x*h+m*g,r.push(l),r.push(s);const v=[];for(let y=0;y<r.length-1;y+=2)v.push({x:r[y],y:r[y+1]});if(n)for(let y=0;y<i+1;y++){const M=v.shift();v.push(M)}return v}static getPolygonByID(e,t){return e.find(n=>n.id===t)}static getPolygonByIDs(e,t){return t&&(t==null?void 0:t.length)>0?e.filter(n=>t.includes(n.id)):[]}static getHoverEdgeIndex(e,t,n=tool.ELineTypes.Line,i=5){let r=[...t];n===tool.ELineTypes.Curve?r=this.createSmoothCurvePoints(t.reduce((s,u)=>[...s,u.x,u.y],[]),.5,!0,tool.SEGMENT_NUMBER):n===tool.ELineTypes.Line&&r.push(r[0]);let o=-1,l=i;for(let s=0;s<r.length-1;s++){const{length:u}=MathUtils.default.getFootOfPerpendicular(e,r[s],r[s+1],!0);u<l&&(o=s,l=u)}return o===-1?-1:n===tool.ELineTypes.Curve?Math.floor(o/tool.SEGMENT_NUMBER):o}static getClosestPoint(e,t,n=tool.ELineTypes.Line,i=3,r){var o;let l=!1;const s=(o=r==null?void 0:r.isClose)!=null?o:!0;let u="",a=-1,h=Infinity,g=e;const P=20;let E=!1;return t.forEach(x=>{if(!E&&!!x.pointList)switch(n){case tool.ELineTypes.Line:CommonToolUtils.findAllLine(x.pointList,s).forEach((c,d)=>{if(E)return;let{length:f,footPoint:v}=MathUtils.default.getFootOfPerpendicular(e,c.point1,c.point2);const y=MathUtils.default.getLineLength(c.point1,e),M=MathUtils.default.getLineLength(c.point2,e);y<i*2&&(v=c.point1,f=y,E=!0),M<i*2&&(v=c.point2,f=M,E=!0),f<h&&f<i&&(u=x.id,a=d,h=f,g=v,l=!0)});break;case tool.ELineTypes.Curve:{const m=this.createSmoothCurvePoints(x.pointList.reduce((c,d)=>[...c,d.x,d.y],[]),.5,s,P);for(let c=0;c<m.length-1;c++){const{length:d,footPoint:f}=MathUtils.default.getFootOfPerpendicular(e,m[c],m[c+1]);d<h&&d<i&&(u=x.id,a=Math.floor(c/(P+1)),h=d,g=f,l=!0)}}break}}),{dropFoot:g,closestEdgeIndex:a,closestPolygonID:u,hasClosed:l}}static isPointListInPolygon(e,t,n=tool.ELineTypes.Line){return e.every(i=>this.isInPolygon(i,t,n))}static isPointListOutSidePolygon(e,t,n=tool.ELineTypes.Line){return e.some(i=>!this.isInPolygon(i,t,n))}static getPolygonArea(e){let t=0;for(let n=0,i=e.length;n<i;n++){const r=e[n].x,o=e[n===e.length-1?0:n+1].y,l=e[n===e.length-1?0:n+1].x,s=e[n].y;t+=r*o*.5,t-=l*s*.5}return Math.abs(t)}static updatePolygonByRotate(e,t=1,n){let i=1;return e===annotation.ERotateDirection.Anticlockwise&&(i=-1),i*=t,MathUtils.default.rotateRectPointList(i,n)}static deletePolygonLastPoint(e,t,n,i){return n===i.length-1?e:[...e,{x:t[0],y:t[1]}]}static concatBeginAndEnd(e){return e.length<1?e:[...e,e[0]]}static segmentPolygonByPolygon(e,t){var n,i;try{let r=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.map(l=>[l.x,l.y]))]]);return t.forEach(l=>{const s=turf.polygon([[...PolygonUtils.concatBeginAndEnd(l.pointList.map(a=>[a.x,a.y]))]]),u=turf.difference(r,s);u&&(r=u)}),((i=(n=r==null?void 0:r.geometry)==null?void 0:n.coordinates.map(l=>{var s;return((s=r==null?void 0:r.geometry)==null?void 0:s.type)==="MultiPolygon"?l[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):l.reduce(PolygonUtils.deletePolygonLastPoint,[])}))!=null?i:[]).reduce((l,s)=>{const u=s.length,a=s.filter((h,g)=>{const P=(g+1)%u;return!AxisUtils.default.getIsInScope(h,s[P],1)});return a.length<3?l:[...l,a]},[])}catch(r){console.error(r)}}static getPolygonPointList(e,t){const n=t.find(i=>i.id===e);return n&&n.pointList&&n.pointList.length>0?n.pointList:[]}static getWrapPolygonIndex(e,t){return t.findIndex(n=>PolygonUtils.isPointListInPolygon(e,n.pointList))}static clipPolygonFromWrapPolygon(e,t){const n=PolygonUtils.isPolygonClosewise(t),i=PolygonUtils.isPolygonClosewise(e),r=PolygonUtils.getClosePointDistanceFromPolygon(e[0],t),o=t[r];let l=[...t.slice(0,r),o,...e,e[0],...t.slice(r,t.length)];return n===i&&(l=[...t.slice(0,r),o,e[0],...e.reverse(),...t.slice(r,t.length)]),l}static isPolygonClosewise(e){const t=e.length;let n,i,r,o=0,l;if(t<3)return 0;for(n=0;n<t;n++)i=(n+1)%t,r=(n+2)%t,l=(e[i].x-e[n].x)*(e[r].y-e[i].y),l-=(e[i].y-e[n].y)*(e[r].x-e[i].x),l<0?o--:l>0&&o++;return o>0?1:o<0?-1:0}static getClosePointDistanceFromPolygon(e,t){let n=Number.MAX_SAFE_INTEGER,i=-1;return t.forEach((r,o)=>{const l=LineToolUtils.default.calcDistance(e,r);l<n&&(n=l,i=o)}),i}static combinePolygonWithPolygon(e,t){var n,i;try{const r=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.pointList.map(a=>[a.x,a.y]))]]),o=turf.polygon([[...PolygonUtils.concatBeginAndEnd(t.pointList.map(a=>[a.x,a.y]))]]),l=turf.union(r,o),s=[],u=e;if(((i=(n=l==null?void 0:l.geometry)==null?void 0:n.coordinates)==null?void 0:i.length)===1){s.push(t.id);const a=l==null?void 0:l.geometry.coordinates.map(h=>{var g;return((g=l==null?void 0:l.geometry)==null?void 0:g.type)==="MultiPolygon"?h[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):h.reduce(PolygonUtils.deletePolygonLastPoint,[])})[0];u.pointList=a}return{newPolygon:u,unionList:s}}catch(r){console.error(r)}}static composePointList(e,t){let n=0,i=0,r=Number.MAX_VALUE;return e.forEach((o,l)=>{t.forEach((s,u)=>{const a=LineToolUtils.default.calcDistance(o,s);a<r&&(n=l+1,i=u+1,r=a)})}),[...e.slice(0,n),...t.slice(i,t.length),...t.slice(0,i),...e.slice(n,e.length)]}static composeSegmentPolygonList(e){var t,n;const i=[];for(let r=0;r<e.length;r++)for(let o=0;o<e.length;o++){const l=(t=e[r])==null?void 0:t.pointList,s=(n=e[o])==null?void 0:n.pointList;r!==o&&l&&s&&PolygonUtils.isPointListInPolygon(s,l)&&(e[r].pointList=PolygonUtils.composePointList(l,s),i.unshift(o))}return i.forEach(r=>{e.splice(r,1)}),e}}module.exports=PolygonUtils;
1
+ "use strict";var turf=require("@turf/turf"),annotation=require("../../constant/annotation.js"),CommonToolUtils=require("./CommonToolUtils.js"),tool=require("../../constant/tool.js"),AxisUtils=require("./AxisUtils.js"),MathUtils=require("../MathUtils.js"),LineToolUtils=require("./LineToolUtils.js"),__defProp=Object.defineProperty,__getOwnPropSymbols=Object.getOwnPropertySymbols,__hasOwnProp=Object.prototype.hasOwnProperty,__propIsEnum=Object.prototype.propertyIsEnumerable,__defNormalProp=(L,e,t)=>e in L?__defProp(L,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):L[e]=t,__spreadValues=(L,e)=>{for(var t in e||(e={}))__hasOwnProp.call(e,t)&&__defNormalProp(L,t,e[t]);if(__getOwnPropSymbols)for(var t of __getOwnPropSymbols(e))__propIsEnum.call(e,t)&&__defNormalProp(L,t,e[t]);return L};class PolygonUtils{static getHoverPolygonID(e,t,o=3,i=tool.ELineTypes.Line){let n="",r=Infinity;const l=AxisUtils.default.axisArea(e,o);return t.forEach(s=>{s.pointList&&l.forEach(c=>{const u=this.calcPolygonSize(s.pointList);this.isInPolygon(c,s.pointList,i)&&u<r&&(n=s.id,r=u)})}),n}static calcPolygonSize(e=[]){if((e==null?void 0:e.length)<=2)return 0;const t=e.length,o=e.reduce((i,n,r,l)=>{const s=l[(r+1)%t];return i+n.x*s.y-s.x*n.y},0);return Math.abs(o)/2}static isInPolygon(e,t,o=tool.ELineTypes.Line){return t=[...t],o===tool.ELineTypes.Curve&&(t=this.createSmoothCurvePoints(t.reduce((i,n)=>[...i,n.x,n.y],[]),.5,!0,20)),this.isPointInOrOnPolygon(e,t)}static isPointInOrOnPolygon(e,t,o=1e-9){const{x:i,y:n}=e;let r=!1;for(let l=0,s=t.length-1;l<t.length;s=l++){const{x:c,y:u}=t[l],{x:h,y:d}=t[s],y=i<=Math.max(c,h)&&i>=Math.min(c,h)&&n<=Math.max(u,d)&&n>=Math.min(u,d),P=(n-u)*(h-c)-(i-c)*(d-u);if(y&&Math.abs(P)<o)return!0;u>n!=d>n&&i<(h-c)*(n-u)/(d-u)+c&&(r=!r)}return r}static createSmoothCurvePointsFromPointList(e,t=tool.SEGMENT_NUMBER){return this.createSmoothCurvePoints(e.reduce((i,n)=>[...i,n.x,n.y],[]),.5,!1,t).map((i,n)=>{var r;const l=n/(tool.SEGMENT_NUMBER+1),s=Math.floor(l),c=(r=e[s])!=null?r:{};return __spreadValues(s===l?__spreadValues({},c):{specialEdge:c.specialEdge},i)})}static createSmoothCurvePoints(e,t=.5,o=!1,i=tool.SEGMENT_NUMBER){if(e.length<4)return e;const n=[],r=e.slice(0);let l,s,c,u,h,d,y,P,m,x,a,g,f;for(o?(r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.unshift(e[e.length-1]),r.unshift(e[e.length-2]),r.push(e[0]),r.push(e[1])):(r.unshift(e[1]),r.unshift(e[0]),r.push(e[e.length-2]),r.push(e[e.length-1])),f=2;f<r.length-4;f+=2)for(c=(r[f+2]-r[f-2])*t,u=(r[f+4]-r[f-0])*t,h=(r[f+3]-r[f-1])*t,d=(r[f+5]-r[f+1])*t,g=0;g<=i;g++)a=g/i,y=2*Math.pow(a,3)-3*Math.pow(a,2)+1,P=-(2*Math.pow(a,3))+3*Math.pow(a,2),m=Math.pow(a,3)-2*Math.pow(a,2)+a,x=Math.pow(a,3)-Math.pow(a,2),l=y*r[f]+P*r[f+2]+m*c+x*u,s=y*r[f+1]+P*r[f+3]+m*h+x*d,n.push(l),n.push(s);const v=[];for(let E=0;E<n.length-1;E+=2)v.push({x:n[E],y:n[E+1]});if(o)for(let E=0;E<i+1;E++){const M=v.shift();v.push(M)}return v}static getPolygonByID(e,t){return e.find(o=>o.id===t)}static getPolygonByIDs(e,t){return t&&(t==null?void 0:t.length)>0?e.filter(o=>t.includes(o.id)):[]}static getHoverEdgeIndex(e,t,o=tool.ELineTypes.Line,i=5){let n=[...t];o===tool.ELineTypes.Curve?n=this.createSmoothCurvePoints(t.reduce((s,c)=>[...s,c.x,c.y],[]),.5,!0,tool.SEGMENT_NUMBER):o===tool.ELineTypes.Line&&n.push(n[0]);let r=-1,l=i;for(let s=0;s<n.length-1;s++){const{length:c}=MathUtils.default.getFootOfPerpendicular(e,n[s],n[s+1],!0);c<l&&(r=s,l=c)}return r===-1?-1:o===tool.ELineTypes.Curve?Math.floor(r/tool.SEGMENT_NUMBER):r}static getClosestPoint(e,t,o=tool.ELineTypes.Line,i=3,n){var r;let l=!1;const s=(r=n==null?void 0:n.isClose)!=null?r:!0;let c="",u=-1,h=Infinity,d=e;const y=20;let P=!1;return t.forEach(m=>{if(!P&&!!m.pointList)switch(o){case tool.ELineTypes.Line:CommonToolUtils.findAllLine(m.pointList,s).forEach((a,g)=>{if(P)return;let{length:f,footPoint:v}=MathUtils.default.getFootOfPerpendicular(e,a.point1,a.point2);const E=MathUtils.default.getLineLength(a.point1,e),M=MathUtils.default.getLineLength(a.point2,e);E<i*2&&(v=a.point1,f=E,P=!0),M<i*2&&(v=a.point2,f=M,P=!0),f<h&&f<i&&(c=m.id,u=g,h=f,d=v,l=!0)});break;case tool.ELineTypes.Curve:{const x=this.createSmoothCurvePoints(m.pointList.reduce((a,g)=>[...a,g.x,g.y],[]),.5,s,y);for(let a=0;a<x.length-1;a++){const{length:g,footPoint:f}=MathUtils.default.getFootOfPerpendicular(e,x[a],x[a+1]);g<h&&g<i&&(c=m.id,u=Math.floor(a/(y+1)),h=g,d=f,l=!0)}}break}}),{dropFoot:d,closestEdgeIndex:u,closestPolygonID:c,hasClosed:l}}static isPointListInPolygon(e,t,o=tool.ELineTypes.Line){return e.every(i=>this.isInPolygon(i,t,o))}static isPointListOutSidePolygon(e,t,o=tool.ELineTypes.Line){return e.some(i=>!this.isInPolygon(i,t,o))}static getPolygonArea(e){let t=0;for(let o=0,i=e.length;o<i;o++){const n=e[o].x,r=e[o===e.length-1?0:o+1].y,l=e[o===e.length-1?0:o+1].x,s=e[o].y;t+=n*r*.5,t-=l*s*.5}return Math.abs(t)}static updatePolygonByRotate(e,t=1,o){let i=1;return e===annotation.ERotateDirection.Anticlockwise&&(i=-1),i*=t,MathUtils.default.rotateRectPointList(i,o)}static deletePolygonLastPoint(e,t,o,i){return o===i.length-1?e:[...e,{x:t[0],y:t[1]}]}static concatBeginAndEnd(e){return e.length<1?e:[...e,e[0]]}static segmentPolygonByPolygon(e,t){var o,i;try{let n=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.map(l=>[l.x,l.y]))]]);return t.forEach(l=>{const s=turf.polygon([[...PolygonUtils.concatBeginAndEnd(l.pointList.map(u=>[u.x,u.y]))]]),c=turf.difference(n,s);c&&(n=c)}),((i=(o=n==null?void 0:n.geometry)==null?void 0:o.coordinates.map(l=>{var s;return((s=n==null?void 0:n.geometry)==null?void 0:s.type)==="MultiPolygon"?l[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):l.reduce(PolygonUtils.deletePolygonLastPoint,[])}))!=null?i:[]).reduce((l,s)=>{const c=s.length,u=s.filter((h,d)=>{const y=(d+1)%c;return!AxisUtils.default.getIsInScope(h,s[y],1)});return u.length<3?l:[...l,u]},[])}catch(n){console.error(n)}}static getPolygonPointList(e,t){const o=t.find(i=>i.id===e);return o&&o.pointList&&o.pointList.length>0?o.pointList:[]}static getWrapPolygonIndex(e,t){return t.findIndex(o=>PolygonUtils.isPointListInPolygon(e,o.pointList))}static clipPolygonFromWrapPolygon(e,t){const o=PolygonUtils.isPolygonClosewise(t),i=PolygonUtils.isPolygonClosewise(e),n=PolygonUtils.getClosePointDistanceFromPolygon(e[0],t),r=t[n];let l=[...t.slice(0,n),r,...e,e[0],...t.slice(n,t.length)];return o===i&&(l=[...t.slice(0,n),r,e[0],...e.reverse(),...t.slice(n,t.length)]),l}static isPolygonClosewise(e){const t=e.length;let o,i,n,r=0,l;if(t<3)return 0;for(o=0;o<t;o++)i=(o+1)%t,n=(o+2)%t,l=(e[i].x-e[o].x)*(e[n].y-e[i].y),l-=(e[i].y-e[o].y)*(e[n].x-e[i].x),l<0?r--:l>0&&r++;return r>0?1:r<0?-1:0}static getClosePointDistanceFromPolygon(e,t){let o=Number.MAX_SAFE_INTEGER,i=-1;return t.forEach((n,r)=>{const l=LineToolUtils.default.calcDistance(e,n);l<o&&(o=l,i=r)}),i}static combinePolygonWithPolygon(e,t){var o,i;try{const n=turf.polygon([[...PolygonUtils.concatBeginAndEnd(e.pointList.map(u=>[u.x,u.y]))]]),r=turf.polygon([[...PolygonUtils.concatBeginAndEnd(t.pointList.map(u=>[u.x,u.y]))]]),l=turf.union(n,r),s=[],c=e;if(((i=(o=l==null?void 0:l.geometry)==null?void 0:o.coordinates)==null?void 0:i.length)===1){s.push(t.id);const u=l==null?void 0:l.geometry.coordinates.map(h=>{var d;return((d=l==null?void 0:l.geometry)==null?void 0:d.type)==="MultiPolygon"?h[0].reduce(PolygonUtils.deletePolygonLastPoint,[]):h.reduce(PolygonUtils.deletePolygonLastPoint,[])})[0];c.pointList=u}return{newPolygon:c,unionList:s}}catch(n){console.error(n)}}static composePointList(e,t){let o=0,i=0,n=Number.MAX_VALUE;return e.forEach((r,l)=>{t.forEach((s,c)=>{const u=LineToolUtils.default.calcDistance(r,s);u<n&&(o=l+1,i=c+1,n=u)})}),[...e.slice(0,o),...t.slice(i,t.length),...t.slice(0,i),...e.slice(o,e.length)]}static composeSegmentPolygonList(e){var t,o;const i=[];for(let n=0;n<e.length;n++)for(let r=0;r<e.length;r++){const l=(t=e[n])==null?void 0:t.pointList,s=(o=e[r])==null?void 0:o.pointList;n!==r&&l&&s&&PolygonUtils.isPointListInPolygon(s,l)&&(e[n].pointList=PolygonUtils.composePointList(l,s),i.unshift(r))}return i.forEach(n=>{e.splice(n,1)}),e}}module.exports=PolygonUtils;
@@ -1 +1 @@
1
- import{polygon as A,difference as B,union as O}from"@turf/turf";import{ERotateDirection as F}from"../../constant/annotation.js";import N from"./CommonToolUtils.js";import{ELineTypes as y,SEGMENT_NUMBER as p}from"../../constant/tool.js";import D from"./AxisUtils.js";import L from"../MathUtils.js";import b from"./LineToolUtils.js";var z=Object.defineProperty,_=Object.getOwnPropertySymbols,R=Object.prototype.hasOwnProperty,T=Object.prototype.propertyIsEnumerable,j=(M,t,e)=>t in M?z(M,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):M[t]=e,S=(M,t)=>{for(var e in t||(t={}))R.call(t,e)&&j(M,e,t[e]);if(_)for(var e of _(t))T.call(t,e)&&j(M,e,t[e]);return M};class h{static getHoverPolygonID(t,e,n=3,i=y.Line){let o="",r=Infinity;const l=D.axisArea(t,n);return e.forEach(s=>{s.pointList&&l.forEach(c=>{const u=this.calcPolygonSize(s.pointList);this.isInPolygon(c,s.pointList,i)&&u<r&&(o=s.id,r=u)})}),o}static calcPolygonSize(t=[]){if((t==null?void 0:t.length)<=2)return 0;const e=t.length,n=t.reduce((i,o,r,l)=>{const s=l[(r+1)%e];return i+o.x*s.y-s.x*o.y},0);return Math.abs(n)/2}static isInPolygon(t,e,n=y.Line){let i=0,o,r,l,s;e=[...e],n===y.Curve&&(e=this.createSmoothCurvePoints(e.reduce((u,d)=>[...u,d.x,d.y],[]),.5,!0,20)),[l]=e;const c=e.length;for(o=1;o<=c;o++)s=e[o%c],t.x>Math.min(l.x,s.x)&&t.x<=Math.max(l.x,s.x)&&t.y<=Math.max(l.y,s.y)&&l.x!==s.x&&(r=(t.x-l.x)*(s.y-l.y)/(s.x-l.x)+l.y,(l.y===s.y||t.y<=r)&&i++),l=s;return i%2!=0}static createSmoothCurvePointsFromPointList(t,e=p){return this.createSmoothCurvePoints(t.reduce((i,o)=>[...i,o.x,o.y],[]),.5,!1,e).map((i,o)=>{var r;const l=o/(p+1),s=Math.floor(l),c=(r=t[s])!=null?r:{};return S(s===l?S({},c):{specialEdge:c.specialEdge},i)})}static createSmoothCurvePoints(t,e=.5,n=!1,i=p){if(t.length<4)return t;const o=[],r=t.slice(0);let l,s,c,u,d,m,v,E,P,I,a,g,f;for(n?(r.unshift(t[t.length-1]),r.unshift(t[t.length-2]),r.unshift(t[t.length-1]),r.unshift(t[t.length-2]),r.push(t[0]),r.push(t[1])):(r.unshift(t[1]),r.unshift(t[0]),r.push(t[t.length-2]),r.push(t[t.length-1])),f=2;f<r.length-4;f+=2)for(c=(r[f+2]-r[f-2])*e,u=(r[f+4]-r[f-0])*e,d=(r[f+3]-r[f-1])*e,m=(r[f+5]-r[f+1])*e,g=0;g<=i;g++)a=g/i,v=2*Math.pow(a,3)-3*Math.pow(a,2)+1,E=-(2*Math.pow(a,3))+3*Math.pow(a,2),P=Math.pow(a,3)-2*Math.pow(a,2)+a,I=Math.pow(a,3)-Math.pow(a,2),l=v*r[f]+E*r[f+2]+P*c+I*u,s=v*r[f+1]+E*r[f+3]+P*d+I*m,o.push(l),o.push(s);const C=[];for(let x=0;x<o.length-1;x+=2)C.push({x:o[x],y:o[x+1]});if(n)for(let x=0;x<i+1;x++){const w=C.shift();C.push(w)}return C}static getPolygonByID(t,e){return t.find(n=>n.id===e)}static getPolygonByIDs(t,e){return e&&(e==null?void 0:e.length)>0?t.filter(n=>e.includes(n.id)):[]}static getHoverEdgeIndex(t,e,n=y.Line,i=5){let o=[...e];n===y.Curve?o=this.createSmoothCurvePoints(e.reduce((s,c)=>[...s,c.x,c.y],[]),.5,!0,p):n===y.Line&&o.push(o[0]);let r=-1,l=i;for(let s=0;s<o.length-1;s++){const{length:c}=L.getFootOfPerpendicular(t,o[s],o[s+1],!0);c<l&&(r=s,l=c)}return r===-1?-1:n===y.Curve?Math.floor(r/p):r}static getClosestPoint(t,e,n=y.Line,i=3,o){var r;let l=!1;const s=(r=o==null?void 0:o.isClose)!=null?r:!0;let c="",u=-1,d=Infinity,m=t;const v=20;let E=!1;return e.forEach(P=>{if(!E&&!!P.pointList)switch(n){case y.Line:N.findAllLine(P.pointList,s).forEach((a,g)=>{if(E)return;let{length:f,footPoint:C}=L.getFootOfPerpendicular(t,a.point1,a.point2);const x=L.getLineLength(a.point1,t),w=L.getLineLength(a.point2,t);x<i*2&&(C=a.point1,f=x,E=!0),w<i*2&&(C=a.point2,f=w,E=!0),f<d&&f<i&&(c=P.id,u=g,d=f,m=C,l=!0)});break;case y.Curve:{const I=this.createSmoothCurvePoints(P.pointList.reduce((a,g)=>[...a,g.x,g.y],[]),.5,s,v);for(let a=0;a<I.length-1;a++){const{length:g,footPoint:f}=L.getFootOfPerpendicular(t,I[a],I[a+1]);g<d&&g<i&&(c=P.id,u=Math.floor(a/(v+1)),d=g,m=f,l=!0)}}break}}),{dropFoot:m,closestEdgeIndex:u,closestPolygonID:c,hasClosed:l}}static isPointListInPolygon(t,e,n=y.Line){return t.every(i=>this.isInPolygon(i,e,n))}static isPointListOutSidePolygon(t,e,n=y.Line){return t.some(i=>!this.isInPolygon(i,e,n))}static getPolygonArea(t){let e=0;for(let n=0,i=t.length;n<i;n++){const o=t[n].x,r=t[n===t.length-1?0:n+1].y,l=t[n===t.length-1?0:n+1].x,s=t[n].y;e+=o*r*.5,e-=l*s*.5}return Math.abs(e)}static updatePolygonByRotate(t,e=1,n){let i=1;return t===F.Anticlockwise&&(i=-1),i*=e,L.rotateRectPointList(i,n)}static deletePolygonLastPoint(t,e,n,i){return n===i.length-1?t:[...t,{x:e[0],y:e[1]}]}static concatBeginAndEnd(t){return t.length<1?t:[...t,t[0]]}static segmentPolygonByPolygon(t,e){var n,i;try{let o=A([[...h.concatBeginAndEnd(t.map(l=>[l.x,l.y]))]]);return e.forEach(l=>{const s=A([[...h.concatBeginAndEnd(l.pointList.map(u=>[u.x,u.y]))]]),c=B(o,s);c&&(o=c)}),((i=(n=o==null?void 0:o.geometry)==null?void 0:n.coordinates.map(l=>{var s;return((s=o==null?void 0:o.geometry)==null?void 0:s.type)==="MultiPolygon"?l[0].reduce(h.deletePolygonLastPoint,[]):l.reduce(h.deletePolygonLastPoint,[])}))!=null?i:[]).reduce((l,s)=>{const c=s.length,u=s.filter((d,m)=>{const v=(m+1)%c;return!D.getIsInScope(d,s[v],1)});return u.length<3?l:[...l,u]},[])}catch(o){console.error(o)}}static getPolygonPointList(t,e){const n=e.find(i=>i.id===t);return n&&n.pointList&&n.pointList.length>0?n.pointList:[]}static getWrapPolygonIndex(t,e){return e.findIndex(n=>h.isPointListInPolygon(t,n.pointList))}static clipPolygonFromWrapPolygon(t,e){const n=h.isPolygonClosewise(e),i=h.isPolygonClosewise(t),o=h.getClosePointDistanceFromPolygon(t[0],e),r=e[o];let l=[...e.slice(0,o),r,...t,t[0],...e.slice(o,e.length)];return n===i&&(l=[...e.slice(0,o),r,t[0],...t.reverse(),...e.slice(o,e.length)]),l}static isPolygonClosewise(t){const e=t.length;let n,i,o,r=0,l;if(e<3)return 0;for(n=0;n<e;n++)i=(n+1)%e,o=(n+2)%e,l=(t[i].x-t[n].x)*(t[o].y-t[i].y),l-=(t[i].y-t[n].y)*(t[o].x-t[i].x),l<0?r--:l>0&&r++;return r>0?1:r<0?-1:0}static getClosePointDistanceFromPolygon(t,e){let n=Number.MAX_SAFE_INTEGER,i=-1;return e.forEach((o,r)=>{const l=b.calcDistance(t,o);l<n&&(n=l,i=r)}),i}static combinePolygonWithPolygon(t,e){var n,i;try{const o=A([[...h.concatBeginAndEnd(t.pointList.map(u=>[u.x,u.y]))]]),r=A([[...h.concatBeginAndEnd(e.pointList.map(u=>[u.x,u.y]))]]),l=O(o,r),s=[],c=t;if(((i=(n=l==null?void 0:l.geometry)==null?void 0:n.coordinates)==null?void 0:i.length)===1){s.push(e.id);const u=l==null?void 0:l.geometry.coordinates.map(d=>{var m;return((m=l==null?void 0:l.geometry)==null?void 0:m.type)==="MultiPolygon"?d[0].reduce(h.deletePolygonLastPoint,[]):d.reduce(h.deletePolygonLastPoint,[])})[0];c.pointList=u}return{newPolygon:c,unionList:s}}catch(o){console.error(o)}}static composePointList(t,e){let n=0,i=0,o=Number.MAX_VALUE;return t.forEach((r,l)=>{e.forEach((s,c)=>{const u=b.calcDistance(r,s);u<o&&(n=l+1,i=c+1,o=u)})}),[...t.slice(0,n),...e.slice(i,e.length),...e.slice(0,i),...t.slice(n,t.length)]}static composeSegmentPolygonList(t){var e,n;const i=[];for(let o=0;o<t.length;o++)for(let r=0;r<t.length;r++){const l=(e=t[o])==null?void 0:e.pointList,s=(n=t[r])==null?void 0:n.pointList;o!==r&&l&&s&&h.isPointListInPolygon(s,l)&&(t[o].pointList=h.composePointList(l,s),i.unshift(r))}return i.forEach(o=>{t.splice(o,1)}),t}}export{h as default};
1
+ import{polygon as S,difference as _,union as B}from"@turf/turf";import{ERotateDirection as F}from"../../constant/annotation.js";import N from"./CommonToolUtils.js";import{ELineTypes as y,SEGMENT_NUMBER as p}from"../../constant/tool.js";import b from"./AxisUtils.js";import L from"../MathUtils.js";import O from"./LineToolUtils.js";var z=Object.defineProperty,j=Object.getOwnPropertySymbols,R=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable,D=(C,t,e)=>t in C?z(C,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):C[t]=e,A=(C,t)=>{for(var e in t||(t={}))R.call(t,e)&&D(C,e,t[e]);if(j)for(var e of j(t))k.call(t,e)&&D(C,e,t[e]);return C};class d{static getHoverPolygonID(t,e,o=3,i=y.Line){let n="",r=Infinity;const l=b.axisArea(t,o);return e.forEach(s=>{s.pointList&&l.forEach(c=>{const u=this.calcPolygonSize(s.pointList);this.isInPolygon(c,s.pointList,i)&&u<r&&(n=s.id,r=u)})}),n}static calcPolygonSize(t=[]){if((t==null?void 0:t.length)<=2)return 0;const e=t.length,o=t.reduce((i,n,r,l)=>{const s=l[(r+1)%e];return i+n.x*s.y-s.x*n.y},0);return Math.abs(o)/2}static isInPolygon(t,e,o=y.Line){return e=[...e],o===y.Curve&&(e=this.createSmoothCurvePoints(e.reduce((i,n)=>[...i,n.x,n.y],[]),.5,!0,20)),this.isPointInOrOnPolygon(t,e)}static isPointInOrOnPolygon(t,e,o=1e-9){const{x:i,y:n}=t;let r=!1;for(let l=0,s=e.length-1;l<e.length;s=l++){const{x:c,y:u}=e[l],{x:h,y:g}=e[s],P=i<=Math.max(c,h)&&i>=Math.min(c,h)&&n<=Math.max(u,g)&&n>=Math.min(u,g),x=(n-u)*(h-c)-(i-c)*(g-u);if(P&&Math.abs(x)<o)return!0;u>n!=g>n&&i<(h-c)*(n-u)/(g-u)+c&&(r=!r)}return r}static createSmoothCurvePointsFromPointList(t,e=p){return this.createSmoothCurvePoints(t.reduce((i,n)=>[...i,n.x,n.y],[]),.5,!1,e).map((i,n)=>{var r;const l=n/(p+1),s=Math.floor(l),c=(r=t[s])!=null?r:{};return A(s===l?A({},c):{specialEdge:c.specialEdge},i)})}static createSmoothCurvePoints(t,e=.5,o=!1,i=p){if(t.length<4)return t;const n=[],r=t.slice(0);let l,s,c,u,h,g,P,x,v,I,a,m,f;for(o?(r.unshift(t[t.length-1]),r.unshift(t[t.length-2]),r.unshift(t[t.length-1]),r.unshift(t[t.length-2]),r.push(t[0]),r.push(t[1])):(r.unshift(t[1]),r.unshift(t[0]),r.push(t[t.length-2]),r.push(t[t.length-1])),f=2;f<r.length-4;f+=2)for(c=(r[f+2]-r[f-2])*e,u=(r[f+4]-r[f-0])*e,h=(r[f+3]-r[f-1])*e,g=(r[f+5]-r[f+1])*e,m=0;m<=i;m++)a=m/i,P=2*Math.pow(a,3)-3*Math.pow(a,2)+1,x=-(2*Math.pow(a,3))+3*Math.pow(a,2),v=Math.pow(a,3)-2*Math.pow(a,2)+a,I=Math.pow(a,3)-Math.pow(a,2),l=P*r[f]+x*r[f+2]+v*c+I*u,s=P*r[f+1]+x*r[f+3]+v*h+I*g,n.push(l),n.push(s);const M=[];for(let E=0;E<n.length-1;E+=2)M.push({x:n[E],y:n[E+1]});if(o)for(let E=0;E<i+1;E++){const w=M.shift();M.push(w)}return M}static getPolygonByID(t,e){return t.find(o=>o.id===e)}static getPolygonByIDs(t,e){return e&&(e==null?void 0:e.length)>0?t.filter(o=>e.includes(o.id)):[]}static getHoverEdgeIndex(t,e,o=y.Line,i=5){let n=[...e];o===y.Curve?n=this.createSmoothCurvePoints(e.reduce((s,c)=>[...s,c.x,c.y],[]),.5,!0,p):o===y.Line&&n.push(n[0]);let r=-1,l=i;for(let s=0;s<n.length-1;s++){const{length:c}=L.getFootOfPerpendicular(t,n[s],n[s+1],!0);c<l&&(r=s,l=c)}return r===-1?-1:o===y.Curve?Math.floor(r/p):r}static getClosestPoint(t,e,o=y.Line,i=3,n){var r;let l=!1;const s=(r=n==null?void 0:n.isClose)!=null?r:!0;let c="",u=-1,h=Infinity,g=t;const P=20;let x=!1;return e.forEach(v=>{if(!x&&!!v.pointList)switch(o){case y.Line:N.findAllLine(v.pointList,s).forEach((a,m)=>{if(x)return;let{length:f,footPoint:M}=L.getFootOfPerpendicular(t,a.point1,a.point2);const E=L.getLineLength(a.point1,t),w=L.getLineLength(a.point2,t);E<i*2&&(M=a.point1,f=E,x=!0),w<i*2&&(M=a.point2,f=w,x=!0),f<h&&f<i&&(c=v.id,u=m,h=f,g=M,l=!0)});break;case y.Curve:{const I=this.createSmoothCurvePoints(v.pointList.reduce((a,m)=>[...a,m.x,m.y],[]),.5,s,P);for(let a=0;a<I.length-1;a++){const{length:m,footPoint:f}=L.getFootOfPerpendicular(t,I[a],I[a+1]);m<h&&m<i&&(c=v.id,u=Math.floor(a/(P+1)),h=m,g=f,l=!0)}}break}}),{dropFoot:g,closestEdgeIndex:u,closestPolygonID:c,hasClosed:l}}static isPointListInPolygon(t,e,o=y.Line){return t.every(i=>this.isInPolygon(i,e,o))}static isPointListOutSidePolygon(t,e,o=y.Line){return t.some(i=>!this.isInPolygon(i,e,o))}static getPolygonArea(t){let e=0;for(let o=0,i=t.length;o<i;o++){const n=t[o].x,r=t[o===t.length-1?0:o+1].y,l=t[o===t.length-1?0:o+1].x,s=t[o].y;e+=n*r*.5,e-=l*s*.5}return Math.abs(e)}static updatePolygonByRotate(t,e=1,o){let i=1;return t===F.Anticlockwise&&(i=-1),i*=e,L.rotateRectPointList(i,o)}static deletePolygonLastPoint(t,e,o,i){return o===i.length-1?t:[...t,{x:e[0],y:e[1]}]}static concatBeginAndEnd(t){return t.length<1?t:[...t,t[0]]}static segmentPolygonByPolygon(t,e){var o,i;try{let n=S([[...d.concatBeginAndEnd(t.map(l=>[l.x,l.y]))]]);return e.forEach(l=>{const s=S([[...d.concatBeginAndEnd(l.pointList.map(u=>[u.x,u.y]))]]),c=_(n,s);c&&(n=c)}),((i=(o=n==null?void 0:n.geometry)==null?void 0:o.coordinates.map(l=>{var s;return((s=n==null?void 0:n.geometry)==null?void 0:s.type)==="MultiPolygon"?l[0].reduce(d.deletePolygonLastPoint,[]):l.reduce(d.deletePolygonLastPoint,[])}))!=null?i:[]).reduce((l,s)=>{const c=s.length,u=s.filter((h,g)=>{const P=(g+1)%c;return!b.getIsInScope(h,s[P],1)});return u.length<3?l:[...l,u]},[])}catch(n){console.error(n)}}static getPolygonPointList(t,e){const o=e.find(i=>i.id===t);return o&&o.pointList&&o.pointList.length>0?o.pointList:[]}static getWrapPolygonIndex(t,e){return e.findIndex(o=>d.isPointListInPolygon(t,o.pointList))}static clipPolygonFromWrapPolygon(t,e){const o=d.isPolygonClosewise(e),i=d.isPolygonClosewise(t),n=d.getClosePointDistanceFromPolygon(t[0],e),r=e[n];let l=[...e.slice(0,n),r,...t,t[0],...e.slice(n,e.length)];return o===i&&(l=[...e.slice(0,n),r,t[0],...t.reverse(),...e.slice(n,e.length)]),l}static isPolygonClosewise(t){const e=t.length;let o,i,n,r=0,l;if(e<3)return 0;for(o=0;o<e;o++)i=(o+1)%e,n=(o+2)%e,l=(t[i].x-t[o].x)*(t[n].y-t[i].y),l-=(t[i].y-t[o].y)*(t[n].x-t[i].x),l<0?r--:l>0&&r++;return r>0?1:r<0?-1:0}static getClosePointDistanceFromPolygon(t,e){let o=Number.MAX_SAFE_INTEGER,i=-1;return e.forEach((n,r)=>{const l=O.calcDistance(t,n);l<o&&(o=l,i=r)}),i}static combinePolygonWithPolygon(t,e){var o,i;try{const n=S([[...d.concatBeginAndEnd(t.pointList.map(u=>[u.x,u.y]))]]),r=S([[...d.concatBeginAndEnd(e.pointList.map(u=>[u.x,u.y]))]]),l=B(n,r),s=[],c=t;if(((i=(o=l==null?void 0:l.geometry)==null?void 0:o.coordinates)==null?void 0:i.length)===1){s.push(e.id);const u=l==null?void 0:l.geometry.coordinates.map(h=>{var g;return((g=l==null?void 0:l.geometry)==null?void 0:g.type)==="MultiPolygon"?h[0].reduce(d.deletePolygonLastPoint,[]):h.reduce(d.deletePolygonLastPoint,[])})[0];c.pointList=u}return{newPolygon:c,unionList:s}}catch(n){console.error(n)}}static composePointList(t,e){let o=0,i=0,n=Number.MAX_VALUE;return t.forEach((r,l)=>{e.forEach((s,c)=>{const u=O.calcDistance(r,s);u<n&&(o=l+1,i=c+1,n=u)})}),[...t.slice(0,o),...e.slice(i,e.length),...e.slice(0,i),...t.slice(o,t.length)]}static composeSegmentPolygonList(t){var e,o;const i=[];for(let n=0;n<t.length;n++)for(let r=0;r<t.length;r++){const l=(e=t[n])==null?void 0:e.pointList,s=(o=t[r])==null?void 0:o.pointList;n!==r&&l&&s&&d.isPointListInPolygon(s,l)&&(t[n].pointList=d.composePointList(l,s),i.unshift(r))}return i.forEach(n=>{t.splice(n,1)}),t}}export{d as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labelbee/lb-annotation",
3
- "version": "1.27.0-alpha.48",
3
+ "version": "1.27.0-alpha.49",
4
4
  "description": "Annotation tool collection",
5
5
  "keywords": [
6
6
  "annotation",