@labelbee/lb-annotation 1.27.0-alpha.53 → 1.27.0-alpha.54
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.
- package/dist/core/pointCloud/OrbitControls.js +1 -1
- package/dist/core/pointCloud/index.js +2 -2
- package/dist/core/toolOperation/ViewOperation.js +1 -1
- package/dist/core/toolOperation/basicToolOperation.js +1 -1
- package/dist/core/toolOperation/pointCloud2DRectOperation.js +1 -1
- package/dist/core/toolOperation/rectOperation.js +1 -1
- package/dist/types/core/pointCloud/index.d.ts +8 -0
- package/dist/types/core/toolOperation/ViewOperation.d.ts +5 -0
- package/dist/types/core/toolOperation/basicToolOperation.d.ts +1 -0
- package/dist/types/core/toolOperation/pointCloud2DRectOperation.d.ts +3 -0
- package/dist/types/core/toolOperation/rectOperation.d.ts +1 -1
- package/dist/types/utils/tool/PolygonUtils.d.ts +12 -0
- package/dist/utils/tool/PolygonUtils.js +1 -1
- package/es/core/pointCloud/OrbitControls.js +1 -1
- package/es/core/pointCloud/index.js +2 -2
- package/es/core/toolOperation/ViewOperation.js +1 -1
- package/es/core/toolOperation/basicToolOperation.js +1 -1
- package/es/core/toolOperation/pointCloud2DRectOperation.js +1 -1
- package/es/core/toolOperation/rectOperation.js +1 -1
- package/es/utils/tool/PolygonUtils.js +1 -1
- package/package.json +1 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import $ from"lodash";import{distance as F}from"@turf/turf";import H from"../../utils/MathUtils.js";import y from"../../utils/tool/AxisUtils.js";import v from"../../utils/tool/RectUtils.js";import z from"../../utils/tool/TagUtils.js";import{EDragStatus as S,ESortDirection as j,DEFAULT_TEXT_SHADOW as O}from"../../constant/annotation.js";import{EDragTarget as D,EOperationMode as X}from"../../constant/tool.js";import L from"../../constant/keyCode.js";import T from"../../locales/index.js";import{EMessage as M}from"../../locales/constants.js";import p from"../../utils/tool/AttributeUtils.js";import W from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import A from"../../utils/tool/DrawUtils.js";import Y from"../../utils/tool/MarkerUtils.js";import{getPolygonPointUnderZoom as U}from"../../utils/tool/polygonTool.js";import G from"../../utils/uuid.js";import{BasicToolOperation as J}from"./basicToolOperation.js";import q from"./textAttributeClass.js";import Q from"./Selection.js";var tt=Object.defineProperty,et=Object.defineProperties,it=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,st=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,B=(w,t,e)=>t in w?tt(w,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):w[t]=e,g=(w,t)=>{for(var e in t||(t={}))st.call(t,e)&&B(w,e,t[e]);if(Z)for(var e of Z(t))rt.call(t,e)&&B(w,e,t[e]);return w},R=(w,t)=>et(w,it(t));const C=6;class N extends J{constructor(t){super(t);this.highlightVisible=!1,this.enableAddRect=!0,this.setMarkerIndex=e=>{this.markerIndex=e},this.setMarkerIndexAndSelect=e=>{if(!this.config.markerList)return;this.markerIndex=e;const i=this.config.markerList[e].value,r=this.currentPageResult.find(o=>o.label===i);r&&(this.setSelectedRectID(r.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(r.attribute)),this.emit("markIndexChange")},this.getHoverRectID=e=>{const i=this.getCoordinateUnderZoom(e),r=C,{currentShowList:o}=this;if(o.length>0){if(this.selectedRectID){const{selectedRect:c}=this;if(c&&v.isInRect(i,c,r,this.zoom))return c.id}const s=o.filter(c=>v.isInRect(i,c,r,this.zoom));if(s.length===0)return"";if(s.length===1)return s[0].id;if(s.length>1)return s.map(n=>({size:n.width*n.height,id:n.id})).sort((n,h)=>n.size-h.size)[0].id}return""},this.getSelectedRectTextAttribute=e=>{if(e){const i=this.rectList.find(r=>r.id===e);return(i==null?void 0:i.textAttribute)||""}return""},this.setDefaultSubAttribute=()=>{var e,i,r,o;if(((e=this.config)==null?void 0:e.secondaryAttributeConfigurable)&&((i=this.config)==null?void 0:i.subAttributeList)){const s=z.getDefaultResultByConfig((o=(r=this.config)==null?void 0:r.subAttributeList)!=null?o:[]);this.setRectList(this.rectList.map(c=>{var n;if(c.id===this.selectedRectID){const h=(n=c==null?void 0:c.subAttribute)!=null?n:s;return R(g({},c),{subAttribute:h})}return c}),!0)}},this.textChange=e=>{this.config.textConfigurable!==!0||!this.selectedRectID||(this.setRectList(p.textChange(e,this.selectedRectID,this.rectList),!0),this.emit("selectedChange"),this.render())},this._drawOutSideTarget=t.drawOutSideTarget||!1,this.rectList=[],this.isFlow=!0,this.config=f.jsonParser(t.config),this.hoverRectEdgeIndex=-1,this.hoverRectPointIndex=-1,this.selectedRectTextAttribute="",this.markerIndex=0,this.createNewDrawingRect=this.createNewDrawingRect.bind(this),this.getDrawingRectWithRectList=this.getDrawingRectWithRectList.bind(this),this.setSelectedIdAfterAddingDrawingRect=this.setSelectedIdAfterAddingDrawingRect.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedRectTextAttribute=this.updateSelectedRectTextAttribute.bind(this),this.setSelectedID=this.setSelectedID.bind(this),this.updateSelectedRectSubAttribute=this.updateSelectedRectSubAttribute.bind(this),this.selection=new Q(this)}setResult(t){if(this.clearActiveStatus(),this.setRectList(t),this.hasMarkerConfig){const e=f.getNextMarker(this.getCurrentPageResult(t),this.config.markerList);e&&this.setMarkerIndex(e.index)}this.render()}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}setConfig(t,e=!1){this.config=f.jsonParser(t),e===!0&&this.clearResult(!1)}setRectList(t,e=!1){const i=this.rectList.length;this.rectList=t,i!==t.length&&this.emit("updatePageNumber"),e&&this.emit("updateResult")}get selectedRectID(){return this.selectedIDs.length===1?this.selectedIDs[0]:void 0}get selectedID(){return this.selectedRectID}get drawOutSideTarget(){return this._drawOutSideTarget||this.config.drawOutsideTarget}get selectedRect(){return this.selectedRects.length===1?this.selectedRects[0]:void 0}get selectedIDs(){return this.selection.selectedIDs}get selectedRects(){return this.rectList.filter(t=>this.selectedIDs.includes(t.id))}get selectedText(){var t;return(t=this.selectedRect)==null?void 0:t.textAttribute}get dataList(){return this.rectList}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),[]);return t}getCurrentPageResult(t){const[e]=f.getRenderResultList(t,f.getSourceID(this.basicResult),[]);return e}setSelectedID(t){this.setSelectedRectID(t)}setSelectedRectID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}updateSelectedRectTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedRectID){let e=t;p.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",p.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setRectList(this.rectList.map(i=>i.id===this.selectedRectID?R(g({},i),{textAttribute:e}):i),!0),this.emit("updateTextAttribute"),this.render()}}updateSelectedRectSubAttribute(t,e){e&&this.config.secondaryAttributeConfigurable===!0&&this.selectedRectID&&(this.setRectList(this.rectList.map(i=>{var r;if(i.id===this.selectedRectID){const o=(r=i==null?void 0:i.subAttribute)!=null?r:{};return R(g({},i),{subAttribute:R(g({},o),{[t]:e})})}return i}),!0),this.render())}getHoverRectPointIndex(t){return this.selectedRect?y.returnClosePointIndex(this.getCoordinateUnderZoom(t),v.getRectPointList(this.selectedRect,this.zoom),C+2):-1}getHoverRectEdgeIndex(t){if(!this.selectedRect)return-1;let e=-1;const{selectedRect:i}=this,r=v.getRectEdgeList(i,this.zoom),o=this.getCoordinateUnderZoom(t);for(let s=0;s<r.length;s++){const c=r[s],{length:n}=H.getFootOfPerpendicular(o,c.begin,c.end);n<C+10&&(e=s)}return e}getTextIconSvg(t=""){return p.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}onMouseDown(t){if(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0)return;const e=this.getCoordinateUnderZoom(t),i=this.getHoverRectID(t);if(!(t.button===2||t.button===0&&this.isSpaceKey===!0)){if(this.selectedRectID){const r=this.currentShowList.find(o=>o.id===this.selectedRectID);if(!r)return;if(this.getHoverRectPointIndex(t)>-1){const o=D.Point;this.dragInfo={dragStartCoord:e,dragTarget:o,startTime:+new Date,firstRect:r},this.dragStatus=S.Start;return}if(this.getHoverRectEdgeIndex(t)>-1){const o=D.Line;this.dragInfo={dragStartCoord:e,dragTarget:o,startTime:+new Date,firstRect:r},this.dragStatus=S.Start;return}}if(this.selection.isIdSelected(i)&&!this.drawingRect){const r=D.Plane;this.dragInfo={dragStartCoord:e,dragTarget:r,firstRect:this.selectedRect,startTime:+new Date,originRectList:this.selectedRects},this.dragStatus=S.Start}}}appendOffsetRect(t,e){return R(g({},t),{x:t.x+e.x/this.zoom,y:t.y+e.y/this.zoom})}getRectsBoundaries(t,e){const i=t.reduce((r,o)=>r.concat(...v.getRectPointList(this.appendOffsetRect(o,e))),[]);return H.calcViewportBoundaries(i)}isRectsOutOfTarget(t,e){var i,r,o,s;if(this.drawOutSideTarget!==!1)return!1;const c=this.getRectsBoundaries(t,e),n={y:c.top,x:c.left,height:c.bottom-c.top,width:c.right-c.left};if(((r=(i=this.basicResult)==null?void 0:i.pointList)==null?void 0:r.length)>0)return v.isRectNotInPolygon(n,U(this.basicResult.pointList,this.zoom));if(this.basicResult||this.imgInfo){const h=this.basicResult?this.basicResult:{x:0,y:0,height:(o=this.imgNode)==null?void 0:o.height,width:(s=this.imgNode)==null?void 0:s.width};if(h)return[{x:c.left,y:c.top},{x:c.right,y:c.bottom}].some(d=>!v.isInRect(d,h))}return!1}moveRects(t){var e;if((e=this.dragInfo)==null?void 0:e.originRectList){let i=$.cloneDeep(this.dragInfo.originRectList);if(this.isRectsOutOfTarget(i,t))return;i=this.dragInfo.originRectList.map(r=>R(g({},r),{x:r.x+t.x/this.zoom,y:r.y+t.y/this.zoom})),this.setRectList(this.rectList.map(r=>{const o=i.find(s=>s.id===r.id);return o||r}))}}onDragMoveAll(t){var e,i;!((i=(e=this.dragInfo)==null?void 0:e.originRectList)==null?void 0:i.length)||(this.setRectList(this.dragInfo.originRectList.map(r=>R(g({},r),{x:r.x+t.x/this.zoom,y:r.y+t.y/this.zoom}))),this.render())}onDragMove(t){var e,i,r,o;if(!this.dragInfo)return;const s={x:t.x-this.dragInfo.dragStartCoord.x,y:t.y-this.dragInfo.dragStartCoord.y};if(this.dragStatus=S.Move,this.dragInfo.dragTarget===D.Plane){this.moveRects(s);return}if(!this.dragInfo.firstRect)return;const c=v.getRectUnderZoom(this.dragInfo.firstRect,this.zoom),{x:n,y:h,width:d,height:a}=c;let l=this.rectList.filter(u=>u.id===this.selectedRectID)[0];switch(this.dragInfo.dragTarget){case D.Point:{let u=n,x=h,m=d,b=a;switch(this.hoverRectPointIndex){case 0:{u=d-s.x<0?n+d:n+s.x,x=a-s.y<0?h+a:h+s.y,m=Math.abs(s.x-d),b=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,x=a-s.y<0?h+a:h+s.y,m=Math.abs(d+s.x),b=Math.abs(a-s.y);break}case 2:{u=d+s.x>0?n:n+d+s.x,x=a+s.y>0?h:h+a+s.y,m=Math.abs(d+s.x),b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}case 3:{u=d-s.x<0?n+d:n+s.x,x=a+s.y>0?h:h+a+s.y,m=Math.abs(s.x-d),b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}default:return}l=R(g({},l),{x:u,y:x,width:m,height:b})}break;case D.Line:{let u=n,x=h,m=d,b=a;switch(this.hoverRectEdgeIndex){case 0:{x=a-s.y<0?h+a:h+s.y,b=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,m=Math.abs(d+s.x);break}case 2:{x=a+s.y>0?h:h+a+s.y,b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}case 3:{u=d-s.x<0?n+d:n+s.x,m=Math.abs(s.x-d);break}default:return}l=R(g({},l),{x:u,y:x,width:m,height:b});break}default:return}if(this.drawOutSideTarget===!1){if(this.basicResult){if(((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&v.isRectNotInPolygon(l,U(this.basicResult.pointList,this.zoom)))return;const u=this.basicResult.x*this.zoom,x=this.basicResult.y*this.zoom,m=this.basicResult.width*this.zoom,b=this.basicResult.height*this.zoom;if(l.x<u-.01||l.y<x-.01||l.width>u+m-l.x+.01||l.height>x+b-l.y+.01)return;if(l.x<u&&(l.x=u),l.y<x&&(l.y=x),l.width>u+m-l.x)switch(this.dragInfo.dragTarget){case D.Point:case D.Line:s.x>0&&s.y>0&&(l.width=u+m-l.x);break;default:return}if(l.height>x+b-l.y)switch(this.dragInfo.dragTarget){}}else if(l.x<0&&(l.x=0),l.y<0&&(l.y=0),this.imgInfo)switch(this.dragInfo.dragTarget){default:if(l.x+l.width>((r=this.imgInfo)==null?void 0:r.width)+.01||l.y+l.height>((o=this.imgInfo)==null?void 0:o.height)+.01)return}}this.setRectList(this.rectList.map(u=>u.id===l.id?v.getRectUnderZoom(l,1/this.zoom):u)),this.render()}onMouseMove(t){var e,i;if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;const r=this.getCoordinateUnderZoom(t),o=y.changeDrawOutsideTarget(r,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.selectedIDs.length>0&&this.dragInfo){this.onDragMove(o);return}if(this.selectedRectID){const n=this.getHoverRectPointIndex(t);if(n!==this.hoverRectPointIndex){this.hoverRectPointIndex=n,this.hoverRectEdgeIndex=-1,this.render();return}if(this.hoverRectPointIndex===-1){const h=this.getHoverRectEdgeIndex(t);if(h!==this.hoverRectEdgeIndex){this.hoverRectEdgeIndex=h,this.render();return}}}const s=this.getHoverRectID(t),c=this.hoverRectID;if(this.hoverRectID=s,s!==c&&this.render(),this.enableAddRect&&this.drawingRect&&this.firstClickCoord){let{x:n,y:h}=this.firstClickCoord,{width:d,height:a}=this.drawingRect;if(d=Math.abs(n-o.x),a=Math.abs(h-o.y),o.x<n&&(n=o.x),o.y<h&&(h=o.y),this.drawOutSideTarget===!1){if(((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&v.isRectNotInPolygon(R(g({},this.drawingRect),{x:n,y:h,width:d,height:a}),U(this.basicResult.pointList,this.zoom)))return;o.x<0&&(d=Math.abs(this.firstClickCoord.x),n=0),o.y<0&&(a=Math.abs(this.firstClickCoord.y),h=0),this.imgInfo&&(n+d>this.imgInfo.width&&(d=Math.abs(this.imgInfo.width-n)),h+a>this.imgInfo.height&&(a=Math.abs(this.imgInfo.height-h)))}this.drawingRect=R(g({},this.drawingRect),{x:n,y:h,width:d,height:a}),this.render()}}setHighlightVisible(t){this.highlightVisible=t,this.setAttributeLockList([])}setAttributeLockList(t){this.setSelectedRectID(void 0),(t==null?void 0:t.length)&&(this.highlightVisible=!1),super.setAttributeLockList(t)}setBasicResult(t){super.setBasicResult(t),this.clearActiveStatus()}setRectValidAndRender(t){!t||(this.setRectList(this.rectList.map(e=>e.id===t?R(g({},e),{valid:!e.valid}):e),!0),this.render(),this.emit("updateResult"))}createNewDrawingRect(t,e){var i,r,o,s;if(!this.imgInfo)return;const c=this.getCoordinateUnderZoom(t),n=y.changeDrawOutsideTarget(c,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.setSelectedRectID(""),this.hoverRectID="",this.drawOutSideTarget&&(n.x<0&&(n.x=0),n.y<0&&(n.y=0)),this.drawingRect=R(g({},n),{width:0,height:0,attribute:this.defaultAttribute,valid:!t.ctrlKey,id:G(8,62),sourceID:e,textAttribute:""}),this.hasMarkerConfig){const h=f.getNextMarker(this.currentPageResult,this.config.markerList,this.markerIndex);if(h)this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{label:h.label})),this.markerIndex=h.index,this.emit("markIndexChange");else{this.emit("messageInfo",T.getMessagesByLocale(M.MarkerFinish,this.lang)),this.drawingRect=void 0;return}}if(this.config.textConfigurable){let h="";h=p.getTextAttribute(this.rectList.filter(d=>f.isSameSourceID(d.sourceID,e)),this.config.textCheckType),this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{textAttribute:h}))}if(((i=this.config)==null?void 0:i.secondaryAttributeConfigurable)&&((r=this.config)==null?void 0:r.subAttributeList)){const h=z.getDefaultResultByConfig((s=(o=this.config)==null?void 0:o.subAttributeList)!=null?s:[]);this.drawingRect=R(g({},this.drawingRect),{subAttribute:h})}if(Object.assign(this.drawingRect,{order:f.getMaxOrder(this.rectList.filter(h=>f.isSameSourceID(h.sourceID,e)))+1}),this.firstClickCoord=g({},n),this.firstCurrentPos=g({},this.currentPos),this.dataInjectionAtCreation){const h=this.dataInjectionAtCreation(this.drawingRect);h&&Object.assign(this.drawingRect,h)}}addDrawingRectToRectList(){if(!this.drawingRect)return;let{width:t,height:e}=this.drawingRect;if(t/=this.zoom,e/=this.zoom,Math.round(t)<this.config.minWidth||Math.round(e)<this.config.minHeight){this.emit("messageInfo",T.getMessagesByLocale(M.RectErrorSizeNotice,this.lang)),this.clearDrawingStatus(),this.render();return}const i=this.getDrawingRectWithRectList();this.setRectList(i,!0),this.history.pushHistory(this.rectList),this.setSelectedIdAfterAddingDrawingRect(),this.clearDrawingStatus()}setSelectedIdAfterAddingDrawingRect(){!this.drawingRect||(this.config.textConfigurable?this.setSelectedRectID(this.drawingRect.id):this.setSelectedRectID())}getDrawingRectWithRectList(){if(!this.drawingRect)return this.rectList;let{x:t,y:e,width:i,height:r}=this.drawingRect;return t/=this.zoom,e/=this.zoom,i/=this.zoom,r/=this.zoom,[...this.rectList,R(g({},this.drawingRect),{x:t,y:e,width:i,height:r})]}rightMouseUp(t){var e,i,r,o,s;const c=this.getHoverRectID(t),n=this.rectList.find(h=>h.id===c);if(this.selectedRectTextAttribute="",this.drawingRect)this.drawingRect=void 0,this.firstClickCoord=void 0;else{if(((e=this.dragInfo)==null?void 0:e.dragStartCoord)&&F((i=this.dragInfo)==null?void 0:i.dragStartCoord,this.getCoordinateUnderZoom(t))>3&&this.dblClickListener.clearRightDblClick(),this.setSelectedRectID(c,t.ctrlKey),n&&((r=this.selectedIDs)==null?void 0:r.length)===1&&(this.selectedRectTextAttribute=this.getSelectedRectTextAttribute(c),this.setDefaultAttribute(n.attribute)),this.hoverRectID="",(n==null?void 0:n.label)&&this.hasMarkerConfig){const h=f.getCurrentMarkerIndex(n.label,this.config.markerList);h>=0&&(this.setMarkerIndex(h),this.emit("markIndexChange"))}((o=this.config)==null?void 0:o.secondaryAttributeConfigurable)&&((s=this.config)==null?void 0:s.subAttributeList)&&this.setDefaultSubAttribute()}this.render()}shiftRightMouseUp(t){const e=this.getHoverRectID(t);this.emit("shiftRightMouseUp",e)}updateDragResult(){this.emit("updateDragResult",g({},this.selectedRect))}setEnableAddRect(t){this.enableAddRect=t}onMouseUp(t){if(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)return!0;if(this.dragInfo&&this.dragStatus===S.Move){this.dragInfo=void 0,this.dragStatus=S.Wait,this.history.pushHistory(this.rectList),this.emit("updateResult"),this.updateDragResult();return}if(t.button===2&&t.shiftKey===!0){this.shiftRightMouseUp(t);return}if(t.button===2){this.rightMouseUp(t);return}const e=f.getSourceID(this.basicResult);if(this.drawingRect){this.enableAddRect?this.addDrawingRectToRectList():(this.clearDrawingStatus(),this.render());return}if(t.ctrlKey===!0&&t.button===0&&this.hoverRectID){this.setRectValidAndRender(this.hoverRectID);return}this.enableAddRect&&(this.createNewDrawingRect(t,e),this.render())}onRightDblClick(t){super.onRightDblClick(t);const e=this.getHoverRectID(t);this.selection.isIdSelected(e)&&this.deleteSelectedRect(t)}deleteSelectedRect(t){this.selectedRects.forEach(e=>{this.deleteRect(e.id)})}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setRectList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!t.ctrlKey}),this.render());break;case L.F:this.selectedRectID&&this.setRectValidAndRender(this.selectedRectID);break;case L.Z:this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deleteSelectedRect(t);break;case L.Tab:{if(t.preventDefault(),this.drawingRect)return;let i=j.ascend;t.shiftKey&&(i=j.descend);const[r,o]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...r];o&&(s=[...s,...o]);const c=W.getViewPort(this.canvas,this.currentPos,this.zoom);s=s.filter(h=>W.inViewPort({x:h.x,y:h.y},c));const n=f.getNextSelectedRectID(s,i,this.selectedRectID);n&&(this.setSelectedRectID(n.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(n.attribute));break}default:{if(this.config.attributeConfigurable){const i=p.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}return!0}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!0}),this.render());break}}onWheel(t){const e=this.zoom;let i,r;this.drawingRect&&this.firstClickCoord&&(i=v.getRectUnderZoom(this.drawingRect,1/e),r=y.changePointByZoom(this.firstClickCoord,1/e)),super.onWheel(t,!1),i&&r&&(this.drawingRect=v.getRectUnderZoom(i,this.zoom),this.firstClickCoord=y.changePointByZoom(r,this.zoom)),this.render()}getCurrentSelectedData(){const{selectedRect:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:t.width*this.zoom*.6,textAttribute:t.textAttribute,color:i}}getRenderStyle(t){const e=this.customRenderStyle&&this.customRenderStyle(t);if(e)return e;const i=this.getColor(t.attribute);let r,o;return t.valid===!1?(r=i==null?void 0:i.invalid.stroke,o=i==null?void 0:i.invalid.fill):(r=i==null?void 0:i.valid.stroke,o=i==null?void 0:i.valid.fill),{strokeColor:r,fillColor:o,textColor:r,toolColor:i}}renderTextAttribute(){var t;const{selectedRect:e}=this;if(!this.ctx||this.config.textConfigurable!==!0||!e)return;const{x:i,y:r,width:o,height:s,attribute:c,valid:n}=e,h=o*this.zoom*.6,d=y.getOffsetCoordinate({x:i,y:r+s},this.currentPos,this.zoom),a=this.getColor(c),l=n?a==null?void 0:a.valid.stroke:a==null?void 0:a.invalid.stroke,u=4;this._textAttributeInstance||(this._textAttributeInstance=new q({width:h,container:this.container,icon:this.getTextIconSvg(c),color:l,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedRectTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${e.textAttribute}`,{left:d.x,top:d.y+u,color:l,width:h})}renderSelectedRect(t){if(!this.ctx||!t)return;const{ctx:e}=this;let i=10;const r=v.getRectPointList(t),o=r.length,s=this.getColor(t.attribute);r.forEach((c,n)=>{var h,d;if(e.save(),e.moveTo(c.x,c.y),e.beginPath(),this.hoverRectPointIndex===n?i=C+6:i=C,t.valid===!1?(e.strokeStyle=s==null?void 0:s.invalid.stroke,e.fillStyle=s==null?void 0:s.invalid.stroke):(e.strokeStyle=s==null?void 0:s.valid.stroke,e.fillStyle=s==null?void 0:s.valid.stroke),e.arc(c.x*this.zoom+this.currentPos.x,c.y*this.zoom+this.currentPos.y,i,0,2*Math.PI),e.fill(),this.hoverRectEdgeIndex===n){e.beginPath(),e.lineWidth=10;const a=this.getColor(t.attribute),l=t.valid===!1?(h=a==null?void 0:a.invalid)==null?void 0:h.stroke:(d=a==null?void 0:a.valid)==null?void 0:d.stroke;e.strokeStyle=l,e.moveTo(r[n].x*this.zoom+this.currentPos.x,r[n].y*this.zoom+this.currentPos.y),e.lineTo(r[(n+1)%o].x*this.zoom+this.currentPos.x,r[(n+1)%o].y*this.zoom+this.currentPos.y),e.stroke()}e.restore()}),this.renderTextAttribute()}renderDrawingRect(t,e=this.zoom,i=!1){var r,o,s,c,n,h;if(this.ctx&&t){const{ctx:d,style:a}=this,{hiddenText:l=!1}=a;d.save();const{strokeColor:u,fillColor:x,textColor:m}=this.getRenderStyle(t);d.font="lighter 14px Arial";let b="";((r=this.config)==null?void 0:r.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(b=`${t.order}`),t.label&&this.hasMarkerConfig&&(b=`${f.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${Y.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&(b=`${b} ${p.getAttributeShowText(t.attribute,(o=this.config)==null?void 0:o.attributeList)}`,(t==null?void 0:t.subAttribute)&&((s=this.config)==null?void 0:s.secondaryAttributeConfigurable)&&((c=this.config)==null?void 0:c.subAttributeList)&&z.getTagNameList(t.subAttribute,this.config.subAttributeList).forEach(k=>{b+=`
|
|
1
|
+
import $ from"lodash";import{distance as F}from"@turf/turf";import H from"../../utils/MathUtils.js";import y from"../../utils/tool/AxisUtils.js";import v from"../../utils/tool/RectUtils.js";import z from"../../utils/tool/TagUtils.js";import{EDragStatus as S,ESortDirection as j,DEFAULT_TEXT_SHADOW as O}from"../../constant/annotation.js";import{EDragTarget as D,EOperationMode as X}from"../../constant/tool.js";import L from"../../constant/keyCode.js";import T from"../../locales/index.js";import{EMessage as M}from"../../locales/constants.js";import p from"../../utils/tool/AttributeUtils.js";import W from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import A from"../../utils/tool/DrawUtils.js";import Y from"../../utils/tool/MarkerUtils.js";import{getPolygonPointUnderZoom as U}from"../../utils/tool/polygonTool.js";import G from"../../utils/uuid.js";import{BasicToolOperation as J}from"./basicToolOperation.js";import q from"./textAttributeClass.js";import Q from"./Selection.js";var tt=Object.defineProperty,et=Object.defineProperties,it=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,st=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,B=(w,t,e)=>t in w?tt(w,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):w[t]=e,g=(w,t)=>{for(var e in t||(t={}))st.call(t,e)&&B(w,e,t[e]);if(Z)for(var e of Z(t))rt.call(t,e)&&B(w,e,t[e]);return w},R=(w,t)=>et(w,it(t));const C=6;class N extends J{constructor(t){super(t);this.highlightVisible=!1,this.enableAddRect=!0,this.setMarkerIndex=e=>{this.markerIndex=e},this.setMarkerIndexAndSelect=e=>{if(!this.config.markerList)return;this.markerIndex=e;const i=this.config.markerList[e].value,r=this.currentPageResult.find(o=>o.label===i);r&&(this.setSelectedRectID(r.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(r.attribute)),this.emit("markIndexChange")},this.getHoverRectID=e=>{const i=this.getCoordinateUnderZoom(e),r=C,{currentShowList:o}=this;if(o.length>0){if(this.selectedRectID){const{selectedRect:c}=this;if(c&&v.isInRect(i,c,r,this.zoom))return c.id}const s=o.filter(c=>v.isInRect(i,c,r,this.zoom));if(s.length===0)return"";if(s.length===1)return s[0].id;if(s.length>1)return s.map(n=>({size:n.width*n.height,id:n.id})).sort((n,h)=>n.size-h.size)[0].id}return""},this.getSelectedRectTextAttribute=e=>{if(e){const i=this.rectList.find(r=>r.id===e);return(i==null?void 0:i.textAttribute)||""}return""},this.setDefaultSubAttribute=()=>{var e,i,r,o;if(((e=this.config)==null?void 0:e.secondaryAttributeConfigurable)&&((i=this.config)==null?void 0:i.subAttributeList)){const s=z.getDefaultResultByConfig((o=(r=this.config)==null?void 0:r.subAttributeList)!=null?o:[]);this.setRectList(this.rectList.map(c=>{var n;if(c.id===this.selectedRectID){const h=(n=c==null?void 0:c.subAttribute)!=null?n:s;return R(g({},c),{subAttribute:h})}return c}),!0)}},this.textChange=e=>{this.config.textConfigurable!==!0||!this.selectedRectID||(this.setRectList(p.textChange(e,this.selectedRectID,this.rectList),!0),this.emit("selectedChange"),this.render())},this._drawOutSideTarget=t.drawOutSideTarget||!1,this.rectList=[],this.isFlow=!0,this.config=f.jsonParser(t.config),this.hoverRectEdgeIndex=-1,this.hoverRectPointIndex=-1,this.selectedRectTextAttribute="",this.markerIndex=0,this.createNewDrawingRect=this.createNewDrawingRect.bind(this),this.getDrawingRectWithRectList=this.getDrawingRectWithRectList.bind(this),this.setSelectedIdAfterAddingDrawingRect=this.setSelectedIdAfterAddingDrawingRect.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedRectTextAttribute=this.updateSelectedRectTextAttribute.bind(this),this.setSelectedID=this.setSelectedID.bind(this),this.updateSelectedRectSubAttribute=this.updateSelectedRectSubAttribute.bind(this),this.selection=new Q(this)}setResult(t){if(this.clearActiveStatus(),this.setRectList(t),this.hasMarkerConfig){const e=f.getNextMarker(this.getCurrentPageResult(t),this.config.markerList);e&&this.setMarkerIndex(e.index)}this.render()}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}setConfig(t,e=!1){this.config=f.jsonParser(t),e===!0&&this.clearResult(!1)}setRectList(t,e=!1){const i=this.rectList.length;this.rectList=t,i!==t.length&&this.emit("updatePageNumber"),e&&this.emit("updateResult")}get selectedRectID(){return this.selectedIDs.length===1?this.selectedIDs[0]:void 0}get selectedID(){return this.selectedRectID}get drawOutSideTarget(){return this._drawOutSideTarget||this.config.drawOutsideTarget}get selectedRect(){return this.selectedRects.length===1?this.selectedRects[0]:void 0}get selectedIDs(){return this.selection.selectedIDs}get selectedRects(){return this.rectList.filter(t=>this.selectedIDs.includes(t.id))}get selectedText(){var t;return(t=this.selectedRect)==null?void 0:t.textAttribute}get dataList(){return this.rectList}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);return t=e,this.isHidden&&(t=[]),i&&t.push(...i),t}get currentPageResult(){const[t]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),[]);return t}getCurrentPageResult(t){const[e]=f.getRenderResultList(t,f.getSourceID(this.basicResult),[]);return e}setSelectedID(t){this.setSelectedRectID(t)}setSelectedRectID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}updateSelectedRectTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedRectID){let e=t;p.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",p.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setRectList(this.rectList.map(i=>i.id===this.selectedRectID?R(g({},i),{textAttribute:e}):i),!0),this.emit("updateTextAttribute"),this.render()}}updateSelectedRectSubAttribute(t,e){e&&this.config.secondaryAttributeConfigurable===!0&&this.selectedRectID&&(this.setRectList(this.rectList.map(i=>{var r;if(i.id===this.selectedRectID){const o=(r=i==null?void 0:i.subAttribute)!=null?r:{};return R(g({},i),{subAttribute:R(g({},o),{[t]:e})})}return i}),!0),this.render())}getHoverRectPointIndex(t){return this.selectedRect?y.returnClosePointIndex(this.getCoordinateUnderZoom(t),v.getRectPointList(this.selectedRect,this.zoom),C+2):-1}getHoverRectEdgeIndex(t){if(!this.selectedRect)return-1;let e=-1;const{selectedRect:i}=this,r=v.getRectEdgeList(i,this.zoom),o=this.getCoordinateUnderZoom(t);for(let s=0;s<r.length;s++){const c=r[s],{length:n}=H.getFootOfPerpendicular(o,c.begin,c.end);n<C+10&&(e=s)}return e}getTextIconSvg(t=""){return p.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}onMouseDown(t){if(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0)return;const e=this.getCoordinateUnderZoom(t),i=this.getHoverRectID(t);if(!(t.button===2||t.button===0&&this.isSpaceKey===!0)){if(this.selectedRectID){const r=this.currentShowList.find(o=>o.id===this.selectedRectID);if(!r)return;if(this.getHoverRectPointIndex(t)>-1){const o=D.Point;this.dragInfo={dragStartCoord:e,dragTarget:o,startTime:+new Date,firstRect:r},this.dragStatus=S.Start;return}if(this.getHoverRectEdgeIndex(t)>-1){const o=D.Line;this.dragInfo={dragStartCoord:e,dragTarget:o,startTime:+new Date,firstRect:r},this.dragStatus=S.Start;return}}if(this.selection.isIdSelected(i)&&!this.drawingRect){const r=D.Plane;this.dragInfo={dragStartCoord:e,dragTarget:r,firstRect:this.selectedRect,startTime:+new Date,originRectList:this.selectedRects},this.dragStatus=S.Start}}}appendOffsetRect(t,e){return R(g({},t),{x:t.x+e.x/this.zoom,y:t.y+e.y/this.zoom})}getRectsBoundaries(t,e){const i=t.reduce((r,o)=>r.concat(...v.getRectPointList(this.appendOffsetRect(o,e))),[]);return H.calcViewportBoundaries(i)}isRectsOutOfTarget(t,e){var i,r,o,s;if(this.drawOutSideTarget!==!1)return!1;const c=this.getRectsBoundaries(t,e),n={y:c.top,x:c.left,height:c.bottom-c.top,width:c.right-c.left};if(((r=(i=this.basicResult)==null?void 0:i.pointList)==null?void 0:r.length)>0)return v.isRectNotInPolygon(n,U(this.basicResult.pointList,this.zoom));if(this.basicResult||this.imgInfo){const h=this.basicResult?this.basicResult:{x:0,y:0,height:(o=this.imgNode)==null?void 0:o.height,width:(s=this.imgNode)==null?void 0:s.width};if(h)return[{x:c.left,y:c.top},{x:c.right,y:c.bottom}].some(d=>!v.isInRect(d,h))}return!1}moveRects(t){var e;if((e=this.dragInfo)==null?void 0:e.originRectList){let i=$.cloneDeep(this.dragInfo.originRectList);if(this.isRectsOutOfTarget(i,t))return;i=this.dragInfo.originRectList.map(r=>R(g({},r),{x:r.x+t.x/this.zoom,y:r.y+t.y/this.zoom})),this.setRectList(this.rectList.map(r=>{const o=i.find(s=>s.id===r.id);return o||r}))}}onDragMoveAll(t){var e,i;!((i=(e=this.dragInfo)==null?void 0:e.originRectList)==null?void 0:i.length)||(this.setRectList(this.dragInfo.originRectList.map(r=>R(g({},r),{x:r.x+t.x/this.zoom,y:r.y+t.y/this.zoom}))),this.render())}onDragMove(t){var e,i,r,o;if(!this.dragInfo)return;const s={x:t.x-this.dragInfo.dragStartCoord.x,y:t.y-this.dragInfo.dragStartCoord.y};if(this.dragStatus=S.Move,this.dragInfo.dragTarget===D.Plane){this.moveRects(s);return}if(!this.dragInfo.firstRect)return;const c=v.getRectUnderZoom(this.dragInfo.firstRect,this.zoom),{x:n,y:h,width:d,height:a}=c;let l=this.rectList.filter(u=>u.id===this.selectedRectID)[0];switch(this.dragInfo.dragTarget){case D.Point:{let u=n,x=h,m=d,b=a;switch(this.hoverRectPointIndex){case 0:{u=d-s.x<0?n+d:n+s.x,x=a-s.y<0?h+a:h+s.y,m=Math.abs(s.x-d),b=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,x=a-s.y<0?h+a:h+s.y,m=Math.abs(d+s.x),b=Math.abs(a-s.y);break}case 2:{u=d+s.x>0?n:n+d+s.x,x=a+s.y>0?h:h+a+s.y,m=Math.abs(d+s.x),b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}case 3:{u=d-s.x<0?n+d:n+s.x,x=a+s.y>0?h:h+a+s.y,m=Math.abs(s.x-d),b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}default:return}l=R(g({},l),{x:u,y:x,width:m,height:b})}break;case D.Line:{let u=n,x=h,m=d,b=a;switch(this.hoverRectEdgeIndex){case 0:{x=a-s.y<0?h+a:h+s.y,b=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,m=Math.abs(d+s.x);break}case 2:{x=a+s.y>0?h:h+a+s.y,b=a+s.y>0?a+s.y:Math.abs(a+s.y);break}case 3:{u=d-s.x<0?n+d:n+s.x,m=Math.abs(s.x-d);break}default:return}l=R(g({},l),{x:u,y:x,width:m,height:b});break}default:return}if(this.drawOutSideTarget===!1){if(this.basicResult){if(((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&v.isRectNotInPolygon(l,U(this.basicResult.pointList,this.zoom)))return;const u=this.basicResult.x*this.zoom,x=this.basicResult.y*this.zoom,m=this.basicResult.width*this.zoom,b=this.basicResult.height*this.zoom;if(l.x<u-.01||l.y<x-.01||l.width>u+m-l.x+.01||l.height>x+b-l.y+.01)return;if(l.x<u&&(l.x=u),l.y<x&&(l.y=x),l.width>u+m-l.x)switch(this.dragInfo.dragTarget){case D.Point:case D.Line:s.x>0&&s.y>0&&(l.width=u+m-l.x);break;default:return}if(l.height>x+b-l.y)switch(this.dragInfo.dragTarget){}}else if(l.x<0&&(l.x=0),l.y<0&&(l.y=0),this.imgInfo)switch(this.dragInfo.dragTarget){default:if(l.x+l.width>((r=this.imgInfo)==null?void 0:r.width)+.01||l.y+l.height>((o=this.imgInfo)==null?void 0:o.height)+.01)return}}this.setRectList(this.rectList.map(u=>u.id===l.id?v.getRectUnderZoom(l,1/this.zoom):u)),this.render()}onMouseMove(t){var e,i;if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;const r=this.getCoordinateUnderZoom(t),o=y.changeDrawOutsideTarget(r,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.selectedIDs.length>0&&this.dragInfo){this.onDragMove(o);return}if(this.selectedRectID){const n=this.getHoverRectPointIndex(t);if(n!==this.hoverRectPointIndex){this.hoverRectPointIndex=n,this.hoverRectEdgeIndex=-1,this.render();return}if(this.hoverRectPointIndex===-1){const h=this.getHoverRectEdgeIndex(t);if(h!==this.hoverRectEdgeIndex){this.hoverRectEdgeIndex=h,this.render();return}}}const s=this.getHoverRectID(t),c=this.hoverRectID;if(this.hoverRectID=s,s!==c&&this.render(),this.enableAddRect&&this.drawingRect&&this.firstClickCoord){let{x:n,y:h}=this.firstClickCoord,{width:d,height:a}=this.drawingRect;if(d=Math.abs(n-o.x),a=Math.abs(h-o.y),o.x<n&&(n=o.x),o.y<h&&(h=o.y),this.drawOutSideTarget===!1){if(((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&v.isRectNotInPolygon(R(g({},this.drawingRect),{x:n,y:h,width:d,height:a}),U(this.basicResult.pointList,this.zoom)))return;o.x<0&&(d=Math.abs(this.firstClickCoord.x),n=0),o.y<0&&(a=Math.abs(this.firstClickCoord.y),h=0),this.imgInfo&&(n+d>this.imgInfo.width&&(d=Math.abs(this.imgInfo.width-n)),h+a>this.imgInfo.height&&(a=Math.abs(this.imgInfo.height-h)))}this.drawingRect=R(g({},this.drawingRect),{x:n,y:h,width:d,height:a}),this.render()}}setHighlightVisible(t){this.highlightVisible=t,this.setAttributeLockList([])}setAttributeLockList(t){this.setSelectedRectID(void 0),(t==null?void 0:t.length)&&(this.highlightVisible=!1),super.setAttributeLockList(t)}setBasicResult(t){super.setBasicResult(t),this.clearActiveStatus()}setRectValidAndRender(t){!t||(this.setRectList(this.rectList.map(e=>e.id===t?R(g({},e),{valid:!e.valid}):e),!0),this.render(),this.emit("updateResult"))}createNewDrawingRect(t,e){var i,r,o,s;if(!this.imgInfo)return;const c=this.getCoordinateUnderZoom(t),n=y.changeDrawOutsideTarget(c,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.setSelectedRectID(""),this.hoverRectID="",this.drawOutSideTarget&&(n.x<0&&(n.x=0),n.y<0&&(n.y=0)),this.drawingRect=R(g({},n),{width:0,height:0,attribute:this.defaultAttribute,valid:!t.ctrlKey,id:G(8,62),sourceID:e,textAttribute:""}),this.hasMarkerConfig){const h=f.getNextMarker(this.currentPageResult,this.config.markerList,this.markerIndex);if(h)this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{label:h.label})),this.markerIndex=h.index,this.emit("markIndexChange");else{this.emit("messageInfo",T.getMessagesByLocale(M.MarkerFinish,this.lang)),this.drawingRect=void 0;return}}if(this.config.textConfigurable){let h="";h=p.getTextAttribute(this.rectList.filter(d=>f.isSameSourceID(d.sourceID,e)),this.config.textCheckType),this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{textAttribute:h}))}if(((i=this.config)==null?void 0:i.secondaryAttributeConfigurable)&&((r=this.config)==null?void 0:r.subAttributeList)){const h=z.getDefaultResultByConfig((s=(o=this.config)==null?void 0:o.subAttributeList)!=null?s:[]);this.drawingRect=R(g({},this.drawingRect),{subAttribute:h})}if(Object.assign(this.drawingRect,{order:f.getMaxOrder(this.rectList.filter(h=>f.isSameSourceID(h.sourceID,e)))+1}),this.firstClickCoord=g({},n),this.firstCurrentPos=g({},this.currentPos),this.dataInjectionAtCreation){const h=this.dataInjectionAtCreation(this.drawingRect);h&&Object.assign(this.drawingRect,h)}}addDrawingRectToRectList(){if(!this.drawingRect)return;let{width:t,height:e}=this.drawingRect;if(t/=this.zoom,e/=this.zoom,Math.round(t)<this.config.minWidth||Math.round(e)<this.config.minHeight){this.emit("messageInfo",T.getMessagesByLocale(M.RectErrorSizeNotice,this.lang)),this.clearDrawingStatus(),this.render();return}const i=this.getDrawingRectWithRectList();this.setRectList(i,!0),this.history.pushHistory(this.rectList),this.setSelectedIdAfterAddingDrawingRect(),this.clearDrawingStatus()}setSelectedIdAfterAddingDrawingRect(){!this.drawingRect||(this.config.textConfigurable?this.setSelectedRectID(this.drawingRect.id):this.setSelectedRectID())}getDrawingRectWithRectList(){if(!this.drawingRect)return this.rectList;let{x:t,y:e,width:i,height:r}=this.drawingRect;return t/=this.zoom,e/=this.zoom,i/=this.zoom,r/=this.zoom,[...this.rectList,R(g({},this.drawingRect),{x:t,y:e,width:i,height:r})]}rightMouseUp(t){var e,i,r,o,s;const c=this.getHoverRectID(t),n=this.rectList.find(h=>h.id===c);if(this.selectedRectTextAttribute="",this.drawingRect)this.drawingRect=void 0,this.firstClickCoord=void 0;else{if(((e=this.dragInfo)==null?void 0:e.dragStartCoord)&&F((i=this.dragInfo)==null?void 0:i.dragStartCoord,this.getCoordinateUnderZoom(t))>3&&this.dblClickListener.clearRightDblClick(),this.setSelectedRectID(c,t.ctrlKey),n&&((r=this.selectedIDs)==null?void 0:r.length)===1&&(this.selectedRectTextAttribute=this.getSelectedRectTextAttribute(c),this.setDefaultAttribute(n.attribute)),this.hoverRectID="",(n==null?void 0:n.label)&&this.hasMarkerConfig){const h=f.getCurrentMarkerIndex(n.label,this.config.markerList);h>=0&&(this.setMarkerIndex(h),this.emit("markIndexChange"))}((o=this.config)==null?void 0:o.secondaryAttributeConfigurable)&&((s=this.config)==null?void 0:s.subAttributeList)&&this.setDefaultSubAttribute()}return this.render(),n}shiftRightMouseUp(t){const e=this.getHoverRectID(t);this.emit("shiftRightMouseUp",e)}updateDragResult(){this.emit("updateDragResult",g({},this.selectedRect))}setEnableAddRect(t){this.enableAddRect=t}onMouseUp(t){if(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)return!0;if(this.dragInfo&&this.dragStatus===S.Move){this.dragInfo=void 0,this.dragStatus=S.Wait,this.history.pushHistory(this.rectList),this.emit("updateResult"),this.updateDragResult();return}if(t.button===2&&t.shiftKey===!0){this.shiftRightMouseUp(t);return}if(t.button===2){this.rightMouseUp(t);return}const e=f.getSourceID(this.basicResult);if(this.drawingRect){this.enableAddRect?this.addDrawingRectToRectList():(this.clearDrawingStatus(),this.render());return}if(t.ctrlKey===!0&&t.button===0&&this.hoverRectID){this.setRectValidAndRender(this.hoverRectID);return}this.enableAddRect&&(this.createNewDrawingRect(t,e),this.render())}onRightDblClick(t){super.onRightDblClick(t);const e=this.getHoverRectID(t);this.selection.isIdSelected(e)&&this.deleteSelectedRect(t)}deleteSelectedRect(t){this.selectedRects.forEach(e=>{this.deleteRect(e.id)})}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setRectList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!t.ctrlKey}),this.render());break;case L.F:this.selectedRectID&&this.setRectValidAndRender(this.selectedRectID);break;case L.Z:this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deleteSelectedRect(t);break;case L.Tab:{if(t.preventDefault(),this.drawingRect)return;let i=j.ascend;t.shiftKey&&(i=j.descend);const[r,o]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...r];o&&(s=[...s,...o]);const c=W.getViewPort(this.canvas,this.currentPos,this.zoom);s=s.filter(h=>W.inViewPort({x:h.x,y:h.y},c));const n=f.getNextSelectedRectID(s,i,this.selectedRectID);n&&(this.setSelectedRectID(n.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(n.attribute));break}default:{if(this.config.attributeConfigurable){const i=p.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}return!0}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!0}),this.render());break}}onWheel(t){const e=this.zoom;let i,r;this.drawingRect&&this.firstClickCoord&&(i=v.getRectUnderZoom(this.drawingRect,1/e),r=y.changePointByZoom(this.firstClickCoord,1/e)),super.onWheel(t,!1),i&&r&&(this.drawingRect=v.getRectUnderZoom(i,this.zoom),this.firstClickCoord=y.changePointByZoom(r,this.zoom)),this.render()}getCurrentSelectedData(){const{selectedRect:t}=this;if(!t)return;const e=this.getColor(t.attribute),i=t.valid?e==null?void 0:e.valid.stroke:e==null?void 0:e.invalid.stroke;return{width:t.width*this.zoom*.6,textAttribute:t.textAttribute,color:i}}getRenderStyle(t){const e=this.customRenderStyle&&this.customRenderStyle(t);if(e)return e;const i=this.getColor(t.attribute);let r,o;return t.valid===!1?(r=i==null?void 0:i.invalid.stroke,o=i==null?void 0:i.invalid.fill):(r=i==null?void 0:i.valid.stroke,o=i==null?void 0:i.valid.fill),{strokeColor:r,fillColor:o,textColor:r,toolColor:i}}renderTextAttribute(){var t;const{selectedRect:e}=this;if(!this.ctx||this.config.textConfigurable!==!0||!e)return;const{x:i,y:r,width:o,height:s,attribute:c,valid:n}=e,h=o*this.zoom*.6,d=y.getOffsetCoordinate({x:i,y:r+s},this.currentPos,this.zoom),a=this.getColor(c),l=n?a==null?void 0:a.valid.stroke:a==null?void 0:a.invalid.stroke,u=4;this._textAttributeInstance||(this._textAttributeInstance=new q({width:h,container:this.container,icon:this.getTextIconSvg(c),color:l,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedRectTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${e.textAttribute}`,{left:d.x,top:d.y+u,color:l,width:h})}renderSelectedRect(t){if(!this.ctx||!t)return;const{ctx:e}=this;let i=10;const r=v.getRectPointList(t),o=r.length,s=this.getColor(t.attribute);r.forEach((c,n)=>{var h,d;if(e.save(),e.moveTo(c.x,c.y),e.beginPath(),this.hoverRectPointIndex===n?i=C+6:i=C,t.valid===!1?(e.strokeStyle=s==null?void 0:s.invalid.stroke,e.fillStyle=s==null?void 0:s.invalid.stroke):(e.strokeStyle=s==null?void 0:s.valid.stroke,e.fillStyle=s==null?void 0:s.valid.stroke),e.arc(c.x*this.zoom+this.currentPos.x,c.y*this.zoom+this.currentPos.y,i,0,2*Math.PI),e.fill(),this.hoverRectEdgeIndex===n){e.beginPath(),e.lineWidth=10;const a=this.getColor(t.attribute),l=t.valid===!1?(h=a==null?void 0:a.invalid)==null?void 0:h.stroke:(d=a==null?void 0:a.valid)==null?void 0:d.stroke;e.strokeStyle=l,e.moveTo(r[n].x*this.zoom+this.currentPos.x,r[n].y*this.zoom+this.currentPos.y),e.lineTo(r[(n+1)%o].x*this.zoom+this.currentPos.x,r[(n+1)%o].y*this.zoom+this.currentPos.y),e.stroke()}e.restore()}),this.renderTextAttribute()}renderDrawingRect(t,e=this.zoom,i=!1){var r,o,s,c,n,h;if(this.ctx&&t){const{ctx:d,style:a}=this,{hiddenText:l=!1}=a;d.save();const{strokeColor:u,fillColor:x,textColor:m}=this.getRenderStyle(t);d.font="lighter 14px Arial";let b="";((r=this.config)==null?void 0:r.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(b=`${t.order}`),t.label&&this.hasMarkerConfig&&(b=`${f.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${Y.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&(b=`${b} ${p.getAttributeShowText(t.attribute,(o=this.config)==null?void 0:o.attributeList)}`,(t==null?void 0:t.subAttribute)&&((s=this.config)==null?void 0:s.secondaryAttributeConfigurable)&&((c=this.config)==null?void 0:c.subAttributeList)&&z.getTagNameList(t.subAttribute,this.config.subAttributeList).forEach(k=>{b+=`
|
|
2
2
|
${k.keyName}: ${k.value.join("\u3001")}`}));const I=y.changeRectByZoom(t,i?e:this.zoom,this.currentPos);l||A.drawText(this.canvas,{x:I.x,y:I.y-6},b,R(g({color:u,font:"normal normal 900 14px SourceHanSansCN-Regular"},O),{textMaxWidth:300})),this.highlightVisible&&(t==null?void 0:t.isHighlight)&&A.drawHighlightFlag({canvas:this.canvas,color:u,position:{x:I.x-5,y:I.y-16}});const V=(h=(n=this.style)==null?void 0:n.width)!=null?h:2,K=this.config.textConfigurable&&this.config.isHighlightSameTextAttribute&&this.selectedRectTextAttribute!==""&&t.textAttribute===this.selectedRectTextAttribute;(t.id===this.hoverRectID||K||t.id===this.selectedRectID||this.isMultiMoveMode)&&A.drawRectWithFill(this.canvas,I,{color:x}),A.drawRect(this.canvas,I,{color:u,thickness:V,hiddenText:!0,lineDash:t.lineDash}),d.restore();let _=`${Math.round(t.width)} * ${Math.round(t.height)}`;i===!0&&(_=`${Math.round(t.width/this.zoom)} * ${Math.round(I.height/this.zoom)}`);const E=_.length*7;if(l||A.drawText(this.canvas,{x:I.x+I.width-E,y:I.y+I.height+15},_,g({color:m,font:"normal normal 600 14px Arial"},O)),!l&&t.textAttribute&&t.id!==this.selectedRectID){const P=0,k=Math.max(20,I.width-E);A.drawText(this.canvas,{x:I.x,y:I.y+I.height+20+P},t.textAttribute,g({color:m,font:"italic normal 900 14px Arial",textMaxWidth:k},O))}}}renderStaticRect(){var t;if(!(((t=this.rectList)==null?void 0:t.length)>0&&JSON.stringify(this.rectList)))return;const{renderEnhance:e={}}=this,[i,r]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs,this.highlightVisible);this.isHidden||i==null||i.forEach(o=>{this.renderDrawingRect(o),e.staticRender&&e.staticRender(this.canvas,y.changeRectByZoom(o,this.zoom,this.currentPos),this.getRenderStyle(o))}),r&&r.forEach(o=>{this.renderDrawingRect(o),this.renderSelectedRect(o),e.selectedRender&&e.selectedRender(this.canvas,y.changeRectByZoom(o,this.zoom,this.currentPos),this.getRenderStyle(o))})}renderCreatingRect(){if(!this.drawingRect)return;const{renderEnhance:t={}}=this;this.renderDrawingRect(this.drawingRect,1,!0),t.creatingRender&&t.creatingRender(this.canvas,y.changeRectByZoom(this.drawingRect,1,this.currentPos),this.getRenderStyle(this.drawingRect))}renderRect(){this.renderStaticRect(),this.renderCreatingRect()}render(){!this.ctx||(super.render(),this.renderRect(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}setDefaultAttribute(t){const e=this.defaultAttribute;if(this.defaultAttribute=t!=null?t:"",e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedRects&&(this.setRectList(this.rectList.map(i=>this.selection.isIdSelected(i.id)?R(g({},i),{attribute:this.defaultAttribute}):i),!0),this.history.pushHistory(this.rectList),this.render()),this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{attribute:this.defaultAttribute}),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(this.defaultAttribute)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setValid(t){super.setValid(t),this.emit("updateResult")}clearDrawingStatus(){this.drawingRect=void 0,this.firstClickCoord=void 0,this.dragInfo=void 0,this.dragStatus=S.Wait}clearActiveStatus(){this.clearDrawingStatus(),this.setSelectedRectID(void 0),this.setOperationMode(X.General)}clearResult(t=!0){const e=this.rectList.filter(i=>i.disableDelete===!0);e.length>0&&t&&this.emit("messageInfo",T.getMessagesByLocale(M.ClearPartialData,this.lang)),this.setRectList(e,!0),this.setSelectedRectID(void 0)}deleteRect(t){var e;if(!t)return;const i=this.rectList.find(r=>r.id===t);if((i==null?void 0:i.disableDelete)===!0){this.emit("messageInfo",T.getMessagesByLocale(M.DisableDelete,this.lang));return}this.setRectList(this.rectList.filter(r=>r.id!==t),!0),this.history.pushHistory(this.rectList),this.setSelectedRectID(void 0),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.render()}exportData(){const{rectList:t}=this;return[t,this.basicImgInfo]}undo(){const t=this.history.undo();t&&(t.length!==this.rectList.length&&this.setSelectedRectID(""),this.setRectList(t,!0),this.render())}redo(){const t=this.history.redo();t&&(t.length!==this.rectList.length&&this.setSelectedRectID(""),this.setRectList(t,!0),this.render())}}export{N as RectOperation,N as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{polygon as S,difference as _,union as B}from"@turf/turf";import{ERotateDirection as F}from"../../constant/annotation.js";import
|
|
1
|
+
import{polygon as S,difference as _,union as B}from"@turf/turf";import{ERotateDirection as F}from"../../constant/annotation.js";import H from"./CommonToolUtils.js";import{ELineTypes as m,SEGMENT_NUMBER as L}from"../../constant/tool.js";import O from"./AxisUtils.js";import M from"../MathUtils.js";import j from"./LineToolUtils.js";var N=Object.defineProperty,b=Object.getOwnPropertySymbols,z=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable,D=(C,t,e)=>t in C?N(C,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):C[t]=e,A=(C,t)=>{for(var e in t||(t={}))z.call(t,e)&&D(C,e,t[e]);if(b)for(var e of b(t))R.call(t,e)&&D(C,e,t[e]);return C};class d{static getHoverPolygonID(t,e,n=3,i=m.Line){let o="",r=Infinity;const l=O.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=m.Line){return e=[...e],n===m.Curve&&(e=this.createSmoothCurvePoints(e.reduce((i,o)=>[...i,o.x,o.y],[]),.5,!0,20)),this.isPointInOrOnPolygon(t,e)}static isPointInOrOnPolygon(t,e,n=1e-9){const{x:i,y:o}=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)&&o<=Math.max(u,g)&&o>=Math.min(u,g),x=(o-u)*(h-c)-(i-c)*(g-u);if(P&&Math.abs(x)<n)return!0;u>o!=g>o&&i<(h-c)*(o-u)/(g-u)+c&&(r=!r)}return r}static createSmoothCurvePointsFromPointList(t,e=L){return this.createSmoothCurvePoints(t.reduce((i,o)=>[...i,o.x,o.y],[]),.5,!1,e).map((i,o)=>{var r;const l=o/(L+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,n=!1,i=L){if(t.length<4)return t;const o=[],r=t.slice(0);let l,s,c,u,h,g,P,x,v,E,a,y,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,h=(r[f+3]-r[f-1])*e,g=(r[f+5]-r[f+1])*e,y=0;y<=i;y++)a=y/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,E=Math.pow(a,3)-Math.pow(a,2),l=P*r[f]+x*r[f+2]+v*c+E*u,s=P*r[f+1]+x*r[f+3]+v*h+E*g,o.push(l),o.push(s);const I=[];for(let p=0;p<o.length-1;p+=2)I.push({x:o[p],y:o[p+1]});if(n)for(let p=0;p<i+1;p++){const w=I.shift();I.push(w)}return I}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=m.Line,i=5){let o=[...e];n===m.Curve?o=this.createSmoothCurvePoints(e.reduce((s,c)=>[...s,c.x,c.y],[]),.5,!0,L):n===m.Line&&o.push(o[0]);let r=-1,l=i;for(let s=0;s<o.length-1;s++){const{length:c}=M.getFootOfPerpendicular(t,o[s],o[s+1],!0);c<l&&(r=s,l=c)}return r===-1?-1:n===m.Curve?Math.floor(r/L):r}static getClosestPoint(t,e,n=m.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,h=Infinity,g=t;const P=20;let x=!1;return e.forEach(v=>{if(!x&&!!v.pointList)switch(n){case m.Line:H.findAllLine(v.pointList,s).forEach((a,y)=>{if(x)return;let{length:f,footPoint:I}=M.getFootOfPerpendicular(t,a.point1,a.point2);const p=M.getLineLength(a.point1,t),w=M.getLineLength(a.point2,t);p<i*2&&(I=a.point1,f=p,x=!0),w<i*2&&(I=a.point2,f=w,x=!0),f<h&&f<i&&(c=v.id,u=y,h=f,g=I,l=!0)});break;case m.Curve:{const E=this.createSmoothCurvePoints(v.pointList.reduce((a,y)=>[...a,y.x,y.y],[]),.5,s,P);for(let a=0;a<E.length-1;a++){const{length:y,footPoint:f}=M.getFootOfPerpendicular(t,E[a],E[a+1]);y<h&&y<i&&(c=v.id,u=Math.floor(a/(P+1)),h=y,g=f,l=!0)}}break}}),{dropFoot:g,closestEdgeIndex:u,closestPolygonID:c,hasClosed:l}}static isPointListInPolygon(t,e,n=m.Line){return t.every(i=>this.isInPolygon(i,e,n))}static isPointListOutSidePolygon(t,e,n=m.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,M.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=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=_(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(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!O.getIsInScope(h,s[P],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=>d.isPointListInPolygon(t,n.pointList))}static clipPolygonFromWrapPolygon(t,e){const n=d.isPolygonClosewise(e),i=d.isPolygonClosewise(t),o=d.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=j.calcDistance(t,o);l<n&&(n=l,i=r)}),i}static combinePolygonWithPolygon(t,e){var n,i;try{const o=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(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(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(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=j.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&&d.isPointListInPolygon(s,l)&&(t[o].pointList=d.composePointList(l,s),i.unshift(r))}return i.forEach(o=>{t.splice(o,1)}),t}static computeConvexHull(t){t.sort((o,r)=>o.x!==r.x?o.x-r.x:o.y-r.y);const e=(o,r,l)=>(r.x-o.x)*(l.y-o.y)-(r.y-o.y)*(l.x-o.x),n=[];for(const o of t){for(;n.length>=2&&e(n[n.length-2],n[n.length-1],o)<=0;)n.pop();n.push(o)}const i=[];for(let o=t.length-1;o>=0;o--){const r=t[o];for(;i.length>=2&&e(i[i.length-2],i[i.length-1],r)<=0;)i.pop();i.push(r)}return i.pop(),n.pop(),n.concat(i)}static createConvexHullGroup(t){const e={};return t.forEach(n=>{n.type==="line"&&(e[n.annotation.id]||(e[n.annotation.id]={points:[],convexHull:[]}),e[n.annotation.id].points.push(...n.annotation.pointList))}),Object.keys(e).forEach(n=>{e[n].convexHull=this.computeConvexHull(e[n].points)}),e}}export{d as default};
|