@labelbee/lb-annotation 1.28.0-alpha.8 → 1.28.0-alpha.9

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.
@@ -1,2 +1,2 @@
1
- import F from"lodash";import{distance as X}from"@turf/turf";import j from"../../utils/MathUtils.js";import y from"../../utils/tool/AxisUtils.js";import v from"../../utils/tool/RectUtils.js";import O from"../../utils/tool/TagUtils.js";import{EDragStatus as S,ESortDirection as W,DEFAULT_TEXT_SHADOW as U}from"../../constant/annotation.js";import{EDragTarget as D,EOperationMode as Y}from"../../constant/tool.js";import A from"../../constant/keyCode.js";import _ from"../../locales/index.js";import{EMessage as M}from"../../locales/constants.js";import p from"../../utils/tool/AttributeUtils.js";import Z from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import C from"../../utils/tool/DrawUtils.js";import G from"../../utils/tool/MarkerUtils.js";import{getPolygonPointUnderZoom as E}from"../../utils/tool/polygonTool.js";import J from"../../utils/uuid.js";import{BasicToolOperation as q}from"./basicToolOperation.js";import Q from"./textAttributeClass.js";import tt from"./Selection.js";var et=Object.defineProperty,it=Object.defineProperties,st=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertySymbols,rt=Object.prototype.hasOwnProperty,ot=Object.prototype.propertyIsEnumerable,N=(w,t,e)=>t in w?et(w,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):w[t]=e,g=(w,t)=>{for(var e in t||(t={}))rt.call(t,e)&&N(w,e,t[e]);if(B)for(var e of B(t))ot.call(t,e)&&N(w,e,t[e]);return w},R=(w,t)=>it(w,st(t));const k=6;class V extends q{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=k,{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=O.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 tt(this)}renderPointCloud2DHighlight(){}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),k+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}=j.getFootOfPerpendicular(o,c.begin,c.end);n<k+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 j.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,E(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=F.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,b=h,x=d,m=a;switch(this.hoverRectPointIndex){case 0:{u=d-s.x<0?n+d:n+s.x,b=a-s.y<0?h+a:h+s.y,x=Math.abs(s.x-d),m=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,b=a-s.y<0?h+a:h+s.y,x=Math.abs(d+s.x),m=Math.abs(a-s.y);break}case 2:{u=d+s.x>0?n:n+d+s.x,b=a+s.y>0?h:h+a+s.y,x=Math.abs(d+s.x),m=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,b=a+s.y>0?h:h+a+s.y,x=Math.abs(s.x-d),m=a+s.y>0?a+s.y:Math.abs(a+s.y);break}default:return}l=R(g({},l),{x:u,y:b,width:x,height:m})}break;case D.Line:{let u=n,b=h,x=d,m=a;switch(this.hoverRectEdgeIndex){case 0:{b=a-s.y<0?h+a:h+s.y,m=Math.abs(s.y-a);break}case 1:{u=d+s.x>0?n:n+d+s.x,x=Math.abs(d+s.x);break}case 2:{b=a+s.y>0?h:h+a+s.y,m=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=Math.abs(s.x-d);break}default:return}l=R(g({},l),{x:u,y:b,width:x,height:m});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,E(this.basicResult.pointList,this.zoom)))return;const u=this.basicResult.x*this.zoom,b=this.basicResult.y*this.zoom,x=this.basicResult.width*this.zoom,m=this.basicResult.height*this.zoom;if(l.x<u-.01||l.y<b-.01||l.width>u+x-l.x+.01||l.height>b+m-l.y+.01)return;if(l.x<u&&(l.x=u),l.y<b&&(l.y=b),l.width>u+x-l.x)switch(this.dragInfo.dragTarget){case D.Point:case D.Line:s.x>0&&s.y>0&&(l.width=u+x-l.x);break;default:return}if(l.height>b+m-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}),E(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){const e=this.rectList.some(i=>i==null?void 0:i.valid);this.setRectList(this.rectList.map(i=>i.id===t?R(g({},i),{valid:!i.valid}):(this.selection.isIdSelected(i.id)&&(i.valid=!e),i)),!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:J(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",_.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=O.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",_.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(){var t;!this.drawingRect||(this.config.textConfigurable||((t=this.config)==null?void 0:t.secondaryAttributeConfigurable)?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)&&X((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 A.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!t.ctrlKey}),this.render());break;case A.F:this.selectedIDs.length>0&&this.setRectValidAndRender(this.selectedRectID);break;case A.Z:this.setIsHidden(!this.isHidden),this.render();break;case A.Delete:this.deleteSelectedRect(t);break;case A.Tab:{if(t.preventDefault(),this.drawingRect)return;let i=W.ascend;t.shiftKey&&(i=W.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=Z.getViewPort(this.canvas,this.currentPos,this.zoom);s=s.filter(h=>Z.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 A.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=k+6:i=k,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,r=!1){var o,s,c,n,h,d;if(this.ctx&&t){const{ctx:a,style:l}=this,{hiddenText:u=!1}=l;a.save();const{strokeColor:b,fillColor:x,textColor:m}=this.getRenderStyle(t);a.font="lighter 14px Arial";let L="";((o=this.config)==null?void 0:o.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(L=`${t.order}`),t.label&&this.hasMarkerConfig&&(L=`${f.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${G.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&(L=`${L} ${p.getAttributeShowText(t.attribute,(s=this.config)==null?void 0:s.attributeList)}`,(t==null?void 0:t.subAttribute)&&((c=this.config)==null?void 0:c.secondaryAttributeConfigurable)&&((n=this.config)==null?void 0:n.subAttributeList)&&O.getTagNameList(t.subAttribute,this.config.subAttributeList).forEach(T=>{L+=`
2
- ${T.keyName}: ${T.value.join("\u3001")}`}));const I=y.changeRectByZoom(t,i?e:this.zoom,this.currentPos);u||C.drawText(this.canvas,{x:I.x,y:I.y-6},L,R(g({color:b,font:"normal normal 900 14px SourceHanSansCN-Regular"},U),{textMaxWidth:300})),this.highlightVisible&&(t==null?void 0:t.isHighlight)&&C.drawHighlightFlag({canvas:this.canvas,color:b,position:{x:I.x-5,y:I.y-16}});const K=(d=(h=this.style)==null?void 0:h.width)!=null?d:2,$=this.config.textConfigurable&&this.config.isHighlightSameTextAttribute&&this.selectedRectTextAttribute!==""&&t.textAttribute===this.selectedRectTextAttribute;(t.id===this.hoverRectID||$||t.id===this.selectedRectID||this.isMultiMoveMode||r)&&C.drawRectWithFill(this.canvas,I,{color:x}),C.drawRect(this.canvas,I,{color:b,thickness:K,hiddenText:!0,lineDash:t.lineDash}),a.restore();let P=`${Math.round(t.width)} * ${Math.round(t.height)}`;i===!0&&(P=`${Math.round(t.width/this.zoom)} * ${Math.round(I.height/this.zoom)}`);const H=P.length*7;if(u||C.drawText(this.canvas,{x:I.x+I.width-H,y:I.y+I.height+15},P,g({color:m,font:"normal normal 600 14px Arial"},U)),!u&&t.textAttribute&&t.id!==this.selectedRectID){const z=0,T=Math.max(20,I.width-H);C.drawText(this.canvas,{x:I.x,y:I.y+I.height+20+z},t.textAttribute,g({color:m,font:"italic normal 900 14px Arial",textMaxWidth:T},U))}}}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))}),this.renderPointCloud2DHighlight()}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(Y.General)}clearResult(t=!0){const e=this.rectList.filter(i=>i.disableDelete===!0);e.length>0&&t&&this.emit("messageInfo",_.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",_.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())}setHoverRectID(t){this.hoverRectID=t}}export{V as RectOperation,V as default};
1
+ import F from"lodash";import{distance as X}from"@turf/turf";import j from"../../utils/MathUtils.js";import y from"../../utils/tool/AxisUtils.js";import I from"../../utils/tool/RectUtils.js";import O from"../../utils/tool/TagUtils.js";import{EDragStatus as S,ESortDirection as W,DEFAULT_TEXT_SHADOW as U}from"../../constant/annotation.js";import{EDragTarget as D,EOperationMode as Y}from"../../constant/tool.js";import A from"../../constant/keyCode.js";import M from"../../locales/index.js";import{EMessage as _}from"../../locales/constants.js";import p from"../../utils/tool/AttributeUtils.js";import Z from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import C from"../../utils/tool/DrawUtils.js";import q from"../../utils/tool/MarkerUtils.js";import{getPolygonPointUnderZoom as E}from"../../utils/tool/polygonTool.js";import G from"../../utils/uuid.js";import{BasicToolOperation as J}from"./basicToolOperation.js";import Q from"./textAttributeClass.js";import tt from"./Selection.js";var et=Object.defineProperty,it=Object.defineProperties,st=Object.getOwnPropertyDescriptors,B=Object.getOwnPropertySymbols,rt=Object.prototype.hasOwnProperty,ot=Object.prototype.propertyIsEnumerable,N=(w,t,e)=>t in w?et(w,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):w[t]=e,g=(w,t)=>{for(var e in t||(t={}))rt.call(t,e)&&N(w,e,t[e]);if(B)for(var e of B(t))ot.call(t,e)&&N(w,e,t[e]);return w},R=(w,t)=>it(w,st(t));const k=6;class V 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(n=>n.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=k,{currentShowList:n}=this;if(n.length>0){if(this.selectedRectID){const{selectedRect:a}=this;if(a&&I.isInRect(i,a,r,this.zoom))return a.id}const s=n.filter(a=>I.isInRect(i,a,r,this.zoom));if(s.length===0)return"";if(s.length===1)return s[0].id;if(s.length>1)return s.map(h=>({size:h.width*h.height,id:h.id})).sort((h,o)=>h.size-o.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.lastMouseMoveTime=0,this.mouseMoveThrottle=16,this.setDefaultSubAttribute=()=>{var e,i,r,n;if(((e=this.config)==null?void 0:e.secondaryAttributeConfigurable)&&((i=this.config)==null?void 0:i.subAttributeList)){const s=O.getDefaultResultByConfig((n=(r=this.config)==null?void 0:r.subAttributeList)!=null?n:[]);this.setRectList(this.rectList.map(a=>{var h;if(a.id===this.selectedRectID){const o=(h=a==null?void 0:a.subAttribute)!=null?h:s;return R(g({},a),{subAttribute:o})}return a}),!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 tt(this)}renderPointCloud2DHighlight(){}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 n=(r=i==null?void 0:i.subAttribute)!=null?r:{};return R(g({},i),{subAttribute:R(g({},n),{[t]:e})})}return i}),!0),this.render())}getHoverRectPointIndex(t){return this.selectedRect?y.returnClosePointIndex(this.getCoordinateUnderZoom(t),I.getRectPointList(this.selectedRect,this.zoom),k+2):-1}getHoverRectEdgeIndex(t){if(!this.selectedRect)return-1;let e=-1;const{selectedRect:i}=this,r=I.getRectEdgeList(i,this.zoom),n=this.getCoordinateUnderZoom(t);for(let s=0;s<r.length;s++){const a=r[s],{length:h}=j.getFootOfPerpendicular(n,a.begin,a.end);h<k+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(n=>n.id===this.selectedRectID);if(!r)return;if(this.getHoverRectPointIndex(t)>-1){const n=D.Point;this.dragInfo={dragStartCoord:e,dragTarget:n,startTime:+new Date,firstRect:r},this.dragStatus=S.Start;return}if(this.getHoverRectEdgeIndex(t)>-1){const n=D.Line;this.dragInfo={dragStartCoord:e,dragTarget:n,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,n)=>r.concat(...I.getRectPointList(this.appendOffsetRect(n,e))),[]);return j.calcViewportBoundaries(i)}isRectsOutOfTarget(t,e){var i,r,n,s;if(this.drawOutSideTarget!==!1)return!1;const a=this.getRectsBoundaries(t,e),h={y:a.top,x:a.left,height:a.bottom-a.top,width:a.right-a.left};if(((r=(i=this.basicResult)==null?void 0:i.pointList)==null?void 0:r.length)>0)return I.isRectNotInPolygon(h,E(this.basicResult.pointList,this.zoom));if(this.basicResult||this.imgInfo){const o=this.basicResult?this.basicResult:{x:0,y:0,height:(n=this.imgNode)==null?void 0:n.height,width:(s=this.imgNode)==null?void 0:s.width};if(o)return[{x:a.left,y:a.top},{x:a.right,y:a.bottom}].some(d=>!I.isInRect(d,o))}return!1}moveRects(t){var e;if((e=this.dragInfo)==null?void 0:e.originRectList){let i=F.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 n=i.find(s=>s.id===r.id);return n||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,n;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 a=I.getRectUnderZoom(this.dragInfo.firstRect,this.zoom),{x:h,y:o,width:d,height:c}=a;let l=this.rectList.filter(u=>u.id===this.selectedRectID)[0];switch(this.dragInfo.dragTarget){case D.Point:{let u=h,b=o,m=d,x=c;switch(this.hoverRectPointIndex){case 0:{u=d-s.x<0?h+d:h+s.x,b=c-s.y<0?o+c:o+s.y,m=Math.abs(s.x-d),x=Math.abs(s.y-c);break}case 1:{u=d+s.x>0?h:h+d+s.x,b=c-s.y<0?o+c:o+s.y,m=Math.abs(d+s.x),x=Math.abs(c-s.y);break}case 2:{u=d+s.x>0?h:h+d+s.x,b=c+s.y>0?o:o+c+s.y,m=Math.abs(d+s.x),x=c+s.y>0?c+s.y:Math.abs(c+s.y);break}case 3:{u=d-s.x<0?h+d:h+s.x,b=c+s.y>0?o:o+c+s.y,m=Math.abs(s.x-d),x=c+s.y>0?c+s.y:Math.abs(c+s.y);break}default:return}l=R(g({},l),{x:u,y:b,width:m,height:x})}break;case D.Line:{let u=h,b=o,m=d,x=c;switch(this.hoverRectEdgeIndex){case 0:{b=c-s.y<0?o+c:o+s.y,x=Math.abs(s.y-c);break}case 1:{u=d+s.x>0?h:h+d+s.x,m=Math.abs(d+s.x);break}case 2:{b=c+s.y>0?o:o+c+s.y,x=c+s.y>0?c+s.y:Math.abs(c+s.y);break}case 3:{u=d-s.x<0?h+d:h+s.x,m=Math.abs(s.x-d);break}default:return}l=R(g({},l),{x:u,y:b,width:m,height:x});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&&I.isRectNotInPolygon(l,E(this.basicResult.pointList,this.zoom)))return;const u=this.basicResult.x*this.zoom,b=this.basicResult.y*this.zoom,m=this.basicResult.width*this.zoom,x=this.basicResult.height*this.zoom;if(l.x<u-.01||l.y<b-.01||l.width>u+m-l.x+.01||l.height>b+x-l.y+.01)return;if(l.x<u&&(l.x=u),l.y<b&&(l.y=b),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>b+x-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>((n=this.imgInfo)==null?void 0:n.height)+.01)return}}this.setRectList(this.rectList.map(u=>u.id===l.id?I.getRectUnderZoom(l,1/this.zoom):u)),this.render()}onMouseMove(t){requestAnimationFrame(()=>{var e,i;const r=Date.now();if(r-this.lastMouseMoveTime<this.mouseMoveThrottle||(this.lastMouseMoveTime=r,super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo))return;const n=this.getCoordinateUnderZoom(t),s=y.changeDrawOutsideTarget(n,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.selectedIDs.length>0&&this.dragInfo){this.onDragMove(s);return}if(this.selectedRectID){const o=this.getHoverRectPointIndex(t);if(o!==this.hoverRectPointIndex){this.hoverRectPointIndex=o,this.hoverRectEdgeIndex=-1,this.render();return}if(this.hoverRectPointIndex===-1){const d=this.getHoverRectEdgeIndex(t);if(d!==this.hoverRectEdgeIndex){this.hoverRectEdgeIndex=d,this.render();return}}}const a=this.getHoverRectID(t),h=this.hoverRectID;if(this.hoverRectID=a,a!==h&&this.render(),this.enableAddRect&&this.drawingRect&&this.firstClickCoord){let{x:o,y:d}=this.firstClickCoord,{width:c,height:l}=this.drawingRect;if(c=Math.abs(o-s.x),l=Math.abs(d-s.y),s.x<o&&(o=s.x),s.y<d&&(d=s.y),this.drawOutSideTarget===!1){if(((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&I.isRectNotInPolygon(R(g({},this.drawingRect),{x:o,y:d,width:c,height:l}),E(this.basicResult.pointList,this.zoom)))return;s.x<0&&(c=Math.abs(this.firstClickCoord.x),o=0),s.y<0&&(l=Math.abs(this.firstClickCoord.y),d=0),this.imgInfo&&(o+c>this.imgInfo.width&&(c=Math.abs(this.imgInfo.width-o)),d+l>this.imgInfo.height&&(l=Math.abs(this.imgInfo.height-d)))}this.drawingRect=R(g({},this.drawingRect),{x:o,y:d,width:c,height:l}),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){const e=this.rectList.some(i=>i==null?void 0:i.valid);this.setRectList(this.rectList.map(i=>i.id===t?R(g({},i),{valid:!i.valid}):(this.selection.isIdSelected(i.id)&&(i.valid=!e),i)),!0),this.render(),this.emit("updateResult")}createNewDrawingRect(t,e){var i,r,n,s;if(!this.imgInfo)return;const a=this.getCoordinateUnderZoom(t),h=y.changeDrawOutsideTarget(a,{x:0,y:0},this.imgInfo,this.drawOutSideTarget,this.basicResult,this.zoom);if(this.setSelectedRectID(""),this.hoverRectID="",this.drawOutSideTarget&&(h.x<0&&(h.x=0),h.y<0&&(h.y=0)),this.drawingRect=R(g({},h),{width:0,height:0,attribute:this.defaultAttribute,valid:!t.ctrlKey,id:G(8,62),sourceID:e,textAttribute:""}),this.hasMarkerConfig){const o=f.getNextMarker(this.currentPageResult,this.config.markerList,this.markerIndex);if(o)this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{label:o.label})),this.markerIndex=o.index,this.emit("markIndexChange");else{this.emit("messageInfo",M.getMessagesByLocale(_.MarkerFinish,this.lang)),this.drawingRect=void 0;return}}if(this.config.textConfigurable){let o="";o=p.getTextAttribute(this.rectList.filter(d=>f.isSameSourceID(d.sourceID,e)),this.config.textCheckType),this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{textAttribute:o}))}if(((i=this.config)==null?void 0:i.secondaryAttributeConfigurable)&&((r=this.config)==null?void 0:r.subAttributeList)){const o=O.getDefaultResultByConfig((s=(n=this.config)==null?void 0:n.subAttributeList)!=null?s:[]);this.drawingRect=R(g({},this.drawingRect),{subAttribute:o})}if(Object.assign(this.drawingRect,{order:f.getMaxOrder(this.rectList.filter(o=>f.isSameSourceID(o.sourceID,e)))+1}),this.firstClickCoord=g({},h),this.firstCurrentPos=g({},this.currentPos),this.dataInjectionAtCreation){const o=this.dataInjectionAtCreation(this.drawingRect);o&&Object.assign(this.drawingRect,o)}}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",M.getMessagesByLocale(_.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(){var t;!this.drawingRect||(this.config.textConfigurable||((t=this.config)==null?void 0:t.secondaryAttributeConfigurable)?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,n,s;const a=this.getHoverRectID(t),h=this.rectList.find(o=>o.id===a);if(this.selectedRectTextAttribute="",this.drawingRect)this.drawingRect=void 0,this.firstClickCoord=void 0;else{if(((e=this.dragInfo)==null?void 0:e.dragStartCoord)&&X((i=this.dragInfo)==null?void 0:i.dragStartCoord,this.getCoordinateUnderZoom(t))>3&&this.dblClickListener.clearRightDblClick(),this.setSelectedRectID(a,t.ctrlKey),h&&((r=this.selectedIDs)==null?void 0:r.length)===1&&(this.selectedRectTextAttribute=this.getSelectedRectTextAttribute(a),this.setDefaultAttribute(h.attribute)),this.hoverRectID="",(h==null?void 0:h.label)&&this.hasMarkerConfig){const o=f.getCurrentMarkerIndex(h.label,this.config.markerList);o>=0&&(this.setMarkerIndex(o),this.emit("markIndexChange"))}((n=this.config)==null?void 0:n.secondaryAttributeConfigurable)&&((s=this.config)==null?void 0:s.subAttributeList)&&this.setDefaultSubAttribute()}return this.render(),h}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 A.Ctrl:this.drawingRect&&(this.drawingRect=R(g({},this.drawingRect),{valid:!t.ctrlKey}),this.render());break;case A.F:this.selectedIDs.length>0&&this.setRectValidAndRender(this.selectedRectID);break;case A.Z:this.setIsHidden(!this.isHidden),this.render();break;case A.Delete:this.deleteSelectedRect(t);break;case A.Tab:{if(t.preventDefault(),this.drawingRect)return;let i=W.ascend;t.shiftKey&&(i=W.descend);const[r,n]=f.getRenderResultList(this.rectList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...r];n&&(s=[...s,...n]);const a=Z.getViewPort(this.canvas,this.currentPos,this.zoom);s=s.filter(o=>Z.inViewPort({x:o.x,y:o.y},a));const h=f.getNextSelectedRectID(s,i,this.selectedRectID);h&&(this.setSelectedRectID(h.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(h.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 A.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=I.getRectUnderZoom(this.drawingRect,1/e),r=y.changePointByZoom(this.firstClickCoord,1/e)),super.onWheel(t,!1),i&&r&&(this.drawingRect=I.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,n;return t.valid===!1?(r=i==null?void 0:i.invalid.stroke,n=i==null?void 0:i.invalid.fill):(r=i==null?void 0:i.valid.stroke,n=i==null?void 0:i.valid.fill),{strokeColor:r,fillColor:n,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:n,height:s,attribute:a,valid:h}=e,o=n*this.zoom*.6,d=y.getOffsetCoordinate({x:i,y:r+s},this.currentPos,this.zoom),c=this.getColor(a),l=h?c==null?void 0:c.valid.stroke:c==null?void 0:c.invalid.stroke,u=4;this._textAttributeInstance||(this._textAttributeInstance=new Q({width:o,container:this.container,icon:this.getTextIconSvg(a),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:o})}renderSelectedRect(t){if(!this.ctx||!t)return;const{ctx:e}=this;let i=10;const r=I.getRectPointList(t),n=r.length,s=this.getColor(t.attribute);r.forEach((a,h)=>{var o,d;if(e.save(),e.moveTo(a.x,a.y),e.beginPath(),this.hoverRectPointIndex===h?i=k+6:i=k,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(a.x*this.zoom+this.currentPos.x,a.y*this.zoom+this.currentPos.y,i,0,2*Math.PI),e.fill(),this.hoverRectEdgeIndex===h){e.beginPath(),e.lineWidth=10;const c=this.getColor(t.attribute),l=t.valid===!1?(o=c==null?void 0:c.invalid)==null?void 0:o.stroke:(d=c==null?void 0:c.valid)==null?void 0:d.stroke;e.strokeStyle=l,e.moveTo(r[h].x*this.zoom+this.currentPos.x,r[h].y*this.zoom+this.currentPos.y),e.lineTo(r[(h+1)%n].x*this.zoom+this.currentPos.x,r[(h+1)%n].y*this.zoom+this.currentPos.y),e.stroke()}e.restore()}),this.renderTextAttribute()}renderDrawingRect(t,e=this.zoom,i=!1,r=!1){var n,s,a,h,o,d;if(this.ctx&&t){const{ctx:c,style:l}=this,{hiddenText:u=!1}=l;c.save();const{strokeColor:b,fillColor:m,textColor:x}=this.getRenderStyle(t);c.font="lighter 14px Arial";let L="";((n=this.config)==null?void 0:n.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(L=`${t.order}`),t.label&&this.hasMarkerConfig&&(L=`${f.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${q.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&(L=`${L} ${p.getAttributeShowText(t.attribute,(s=this.config)==null?void 0:s.attributeList)}`,(t==null?void 0:t.subAttribute)&&((a=this.config)==null?void 0:a.secondaryAttributeConfigurable)&&((h=this.config)==null?void 0:h.subAttributeList)&&O.getTagNameList(t.subAttribute,this.config.subAttributeList).forEach(T=>{L+=`
2
+ ${T.keyName}: ${T.value.join("\u3001")}`}));const v=y.changeRectByZoom(t,i?e:this.zoom,this.currentPos);u||C.drawText(this.canvas,{x:v.x,y:v.y-6},L,R(g({color:b,font:"normal normal 900 14px SourceHanSansCN-Regular"},U),{textMaxWidth:300})),this.highlightVisible&&(t==null?void 0:t.isHighlight)&&C.drawHighlightFlag({canvas:this.canvas,color:b,position:{x:v.x-5,y:v.y-16}});const K=(d=(o=this.style)==null?void 0:o.width)!=null?d:2,$=this.config.textConfigurable&&this.config.isHighlightSameTextAttribute&&this.selectedRectTextAttribute!==""&&t.textAttribute===this.selectedRectTextAttribute;(t.id===this.hoverRectID||$||t.id===this.selectedRectID||this.isMultiMoveMode||r)&&C.drawRectWithFill(this.canvas,v,{color:m}),C.drawRect(this.canvas,v,{color:b,thickness:K,hiddenText:!0,lineDash:t.lineDash}),c.restore();let P=`${Math.round(t.width)} * ${Math.round(t.height)}`;i===!0&&(P=`${Math.round(t.width/this.zoom)} * ${Math.round(v.height/this.zoom)}`);const H=P.length*7;if(u||C.drawText(this.canvas,{x:v.x+v.width-H,y:v.y+v.height+15},P,g({color:x,font:"normal normal 600 14px Arial"},U)),!u&&t.textAttribute&&t.id!==this.selectedRectID){const z=0,T=Math.max(20,v.width-H);C.drawText(this.canvas,{x:v.x,y:v.y+v.height+20+z},t.textAttribute,g({color:x,font:"italic normal 900 14px Arial",textMaxWidth:T},U))}}}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(n=>{this.renderDrawingRect(n),e.staticRender&&e.staticRender(this.canvas,y.changeRectByZoom(n,this.zoom,this.currentPos),this.getRenderStyle(n))}),r&&r.forEach(n=>{this.renderDrawingRect(n),this.renderSelectedRect(n),e.selectedRender&&e.selectedRender(this.canvas,y.changeRectByZoom(n,this.zoom,this.currentPos),this.getRenderStyle(n))}),this.renderPointCloud2DHighlight()}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(Y.General)}clearResult(t=!0){const e=this.rectList.filter(i=>i.disableDelete===!0);e.length>0&&t&&this.emit("messageInfo",M.getMessagesByLocale(_.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",M.getMessagesByLocale(_.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())}setHoverRectID(t){this.hoverRectID=t}}export{V as RectOperation,V as default};
@@ -1 +1 @@
1
- import{EToolName as u,ELineTypes as C}from"../../constant/tool.js";import y from"../MathUtils.js";import x from"./PolygonUtils.js";import v,{POINT_RADIUS as _}from"./LineToolUtils.js";import{getHighlightPoints as P,getHighlightLines as m,getCuboidHoverRange as B}from"./CuboidUtils.js";var L=Object.defineProperty,R=Object.defineProperties,D=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,T=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,w=(a,t,e)=>t in a?L(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,f=(a,t)=>{for(var e in t||(t={}))T.call(t,e)&&w(a,e,t[e]);if(O)for(var e of O(t))Z.call(t,e)&&w(a,e,t[e]);return a},c=(a,t)=>R(a,D(t));class I{static getOffsetCoordinate(t,e,i){return{x:t.x*i+e.x,y:t.y*i+e.y}}static changeDrawOutsideTarget(t,e,i,s,n,o){return typeof s=="boolean"&&!s&&(n&&o?(t.x-e.x>(n.x+n.width)*o&&(t.x=(n.x+n.width)*o+e.x),t.x-e.x<n.x*o&&(t.x=n.x*o+e.x),t.y-e.y>(n.y+n.height)*o&&(t.y=(n.y+n.height)*o+e.y),t.y-e.y<n.y*o&&(t.y=n.y*o+e.y)):(t.x-e.x>i.width&&(t.x=i.width+e.x),t.x-e.x<0&&(t.x=e.x),t.y-e.y>i.height&&(t.y=i.height+e.y),t.y-e.y<0&&(t.y=e.y))),t}static changeCoordinateByRotate(t,e,i){const{width:s,height:n}=i,{x:o,y:r}=t;switch(e%360){case 90:return{x:n-r,y:o};case 180:return{x:s-o,y:n-r};case 270:return{x:r,y:s-o};default:return t}}static changeRectByZoom(t,e,i={x:0,y:0}){return c(f({},t),{x:t.x*e+i.x,y:t.y*e+i.y,width:t.width*e,height:t.height*e})}static changePointByZoom(t,e,i={x:0,y:0}){return c(f({},t),{x:t.x*e+i.x,y:t.y*e+i.y})}static changePointListByZoom(t,e,i={x:0,y:0}){return t==null?void 0:t.map(s=>this.changePointByZoom(s,e,i))}static changePlanePointByZoom(t,e,i={x:0,y:0}){return Object.entries(t).reduce((s,[n,o])=>{const r=I.changePointByZoom(o,e,i);return c(f({},s),{[n]:r})},{})}static changeCuboidByZoom(t,e,i={x:0,y:0}){return c(f({},t),{frontPoints:this.changePlanePointByZoom(t.frontPoints,e,i),backPoints:t.backPoints?this.changePlanePointByZoom(t.backPoints,e,i):t.backPoints})}static transformPlain2PointList({tl:t,tr:e,br:i,bl:s}){return[t,e,i,s]}static axisArea(t,e=3){const{x:i,y:s}=t,n=[];for(let o=i-e;o<i+e;o+=e/3)for(let r=s-e;r<s+e;r+=e/3)n.push({x:o,y:r});return n}static getOriginCoordinateWithOffsetCoordinate(t,e=1,i={x:0,y:0}){return{x:(t.x-i.x)/e,y:(t.y-i.y)/e}}static returnClosePointIndex(t,e,i=3){let s=-1;for(let n=0;n<e.length;n++){const o=e[n];this.getIsInScope(t,o,i)&&(s=n)}return s}static isCloseCuboid(t,e,i={scope:5,zoom:1}){const s=P(e),{scope:n=5}=i;for(let r=0;r<s.length;r++){const l=s[r];if(this.getIsInScope(t,l.point,n))return!0}const o=m(e);for(let r=0;r<o.length;r++){const l=o[r],{length:p}=y.getFootOfPerpendicular(t,l.p1,l.p2,!0);if(p<n)return!0}return!!x.isInPolygon(t,B(e))}static isCloseCuboidList(t,e,i={scope:5,zoom:1}){return e.some(s=>this.isCloseCuboid(t,s,i))}static returnClosePointOrLineInCuboid(t,e,i={scope:5,zoom:1}){const s=P(e),{scope:n=5,zoom:o=1}=i;for(let h=0;h<s.length;h++){const g=s[h];if(this.getIsInScope(t,g.point,n))return[{type:"point",points:[this.changePointByZoom(s[h].point,o)],originCuboid:e,positions:g.positions}]}let r=n;const l=m(e);let p;for(let h=0;h<l.length;h++){const g=l[h],{length:d}=y.getFootOfPerpendicular(t,g.p1,g.p2,!0);d<r&&(r=d,p=[{type:"line",points:this.changePointListByZoom([g.p1,g.p2],o),originCuboid:e,positions:g.positions}])}if(p)return p}static getIsInScope(t,e,i){return Math.abs(t.x-e.x)<i&&Math.abs(t.y-e.y)<i}}class b{constructor(t){this.currentPos=t.currentPos,this.zoom=t.zoom,this.basicImgInfo=t.basicImgInfo,this.dependToolName=""}get isDependPolygon(){return this.dependToolName===u.Polygon}get isDependRect(){return this.dependToolName===u.Rect}get isDependOriginalImage(){return this.dependToolName===""}getAbsCoord(t){return{x:(t.x-this.currentPos.x)/this.zoom,y:(t.y-this.currentPos.y)/this.zoom}}getRenderCoord(t){return{x:t.x*this.zoom+this.currentPos.x,y:t.y*this.zoom+this.currentPos.y}}coordInsideRect(t,e){const{x:i,y:s,width:n,height:o}=e;return{x:y.withinRange(t.x,[i,i+n]),y:y.withinRange(t.y,[s,s+o])}}getPolygonPointList(t,e){return t===C.Curve?x.createSmoothCurvePointsFromPointList(e):e}getIntersection(t,e,i){const s=this.getRenderCoord(e),n=this.getRenderCoord(t),o={pointA:s,pointB:n};return v.calcOptimalIntersection(i,o,s,_,this.zoom)}coordInsidePolygon(t,e,i,s){const{pointList:n}=i,o=s==null?void 0:s.lineType;if(n.length===0)return t;const r=this.getPolygonPointList(o,n);if(x.isInPolygon(t,r))return t;const p=r.concat(r[0]).map(g=>this.getRenderCoord(g)),h=this.getIntersection(t,e,p);return h?this.getAbsCoord(h==null?void 0:h.point):t}coordInsideImage(t){return this.coordInsideRect(t,c(f({},this.basicImgInfo),{x:0,y:0}))}getNextCoordByDependTool(t,e){if(this.isDependRect)return this.coordInsideRect(t,this.basicResult);if(this.isDependPolygon)return this.coordInsidePolygon(t,e,this.basicResult,this.dependToolConfig);if(this.isDependOriginalImage)return this.coordInsideImage(t)}setDependInfo(t,e){this.dependToolName=t!=null?t:"",this.dependToolConfig=t?e:void 0}setBasicImgInfo(t){this.basicImgInfo=t}setBasicResult(t){this.basicResult=t}setZoomAndCurrentPos(t,e){this.zoom=t,this.currentPos=e}isCoordInsideTarget(t){if(this.isDependPolygon)return this.isInBasicPolygon(t);if(this.isDependRect){const{x:e,y:i,width:s,height:n}=this.basicResult,o=[e,e+s],r=[i,i+n];return y.isInRange(t.x,o)&&y.isInRange(t.y,r)}return y.isInRange(t.x,[0,this.basicImgInfo.width])&&y.isInRange(t.y,[0,this.basicImgInfo.height])}isInBasicPolygon(t){var e,i;return x.isInPolygon(t,((e=this.basicResult)==null?void 0:e.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}}export{b as CoordinateUtils,I as default};
1
+ import{EToolName as u,ELineTypes as C}from"../../constant/tool.js";import y from"../MathUtils.js";import x from"./PolygonUtils.js";import v,{POINT_RADIUS as _}from"./LineToolUtils.js";import{getHighlightPoints as m,getHighlightLines as P,getCuboidHoverRange as B}from"./CuboidUtils.js";var L=Object.defineProperty,R=Object.defineProperties,D=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,T=Object.prototype.hasOwnProperty,Z=Object.prototype.propertyIsEnumerable,w=(a,t,e)=>t in a?L(a,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):a[t]=e,f=(a,t)=>{for(var e in t||(t={}))T.call(t,e)&&w(a,e,t[e]);if(O)for(var e of O(t))Z.call(t,e)&&w(a,e,t[e]);return a},c=(a,t)=>R(a,D(t));class I{static getOffsetCoordinate(t,e,i){return{x:t.x*i+e.x,y:t.y*i+e.y}}static changeDrawOutsideTarget(t,e,i,s,n,o){return typeof s=="boolean"&&!s&&(n&&o?(t.x-e.x>(n.x+n.width)*o&&(t.x=(n.x+n.width)*o+e.x),t.x-e.x<n.x*o&&(t.x=n.x*o+e.x),t.y-e.y>(n.y+n.height)*o&&(t.y=(n.y+n.height)*o+e.y),t.y-e.y<n.y*o&&(t.y=n.y*o+e.y)):(t.x-e.x>i.width&&(t.x=i.width+e.x),t.x-e.x<0&&(t.x=e.x),t.y-e.y>i.height&&(t.y=i.height+e.y),t.y-e.y<0&&(t.y=e.y))),t}static changeCoordinateByRotate(t,e,i){const{width:s,height:n}=i,{x:o,y:r}=t;switch(e%360){case 90:return{x:n-r,y:o};case 180:return{x:s-o,y:n-r};case 270:return{x:r,y:s-o};default:return t}}static changeRectByZoom(t,e,i={x:0,y:0}){return c(f({},t),{x:t.x*e+i.x,y:t.y*e+i.y,width:t.width*e,height:t.height*e})}static changePointByZoom(t,e,i={x:0,y:0}){return e===1&&i.x===0&&i.y===0?t:c(f({},t),{x:t.x*e+i.x,y:t.y*e+i.y})}static changePointListByZoom(t,e,i={x:0,y:0}){return t==null?void 0:t.map(s=>this.changePointByZoom(s,e,i))}static changePlanePointByZoom(t,e,i={x:0,y:0}){return Object.entries(t).reduce((s,[n,o])=>{const r=I.changePointByZoom(o,e,i);return c(f({},s),{[n]:r})},{})}static changeCuboidByZoom(t,e,i={x:0,y:0}){return c(f({},t),{frontPoints:this.changePlanePointByZoom(t.frontPoints,e,i),backPoints:t.backPoints?this.changePlanePointByZoom(t.backPoints,e,i):t.backPoints})}static transformPlain2PointList({tl:t,tr:e,br:i,bl:s}){return[t,e,i,s]}static axisArea(t,e=3){const{x:i,y:s}=t,n=[];for(let o=i-e;o<i+e;o+=e/3)for(let r=s-e;r<s+e;r+=e/3)n.push({x:o,y:r});return n}static getOriginCoordinateWithOffsetCoordinate(t,e=1,i={x:0,y:0}){return{x:(t.x-i.x)/e,y:(t.y-i.y)/e}}static returnClosePointIndex(t,e,i=3){let s=-1;for(let n=0;n<e.length;n++){const o=e[n];this.getIsInScope(t,o,i)&&(s=n)}return s}static isCloseCuboid(t,e,i={scope:5,zoom:1}){const s=m(e),{scope:n=5}=i;for(let r=0;r<s.length;r++){const l=s[r];if(this.getIsInScope(t,l.point,n))return!0}const o=P(e);for(let r=0;r<o.length;r++){const l=o[r],{length:p}=y.getFootOfPerpendicular(t,l.p1,l.p2,!0);if(p<n)return!0}return!!x.isInPolygon(t,B(e))}static isCloseCuboidList(t,e,i={scope:5,zoom:1}){return e.some(s=>this.isCloseCuboid(t,s,i))}static returnClosePointOrLineInCuboid(t,e,i={scope:5,zoom:1}){const s=m(e),{scope:n=5,zoom:o=1}=i;for(let h=0;h<s.length;h++){const g=s[h];if(this.getIsInScope(t,g.point,n))return[{type:"point",points:[this.changePointByZoom(s[h].point,o)],originCuboid:e,positions:g.positions}]}let r=n;const l=P(e);let p;for(let h=0;h<l.length;h++){const g=l[h],{length:d}=y.getFootOfPerpendicular(t,g.p1,g.p2,!0);d<r&&(r=d,p=[{type:"line",points:this.changePointListByZoom([g.p1,g.p2],o),originCuboid:e,positions:g.positions}])}if(p)return p}static getIsInScope(t,e,i){return Math.abs(t.x-e.x)<i&&Math.abs(t.y-e.y)<i}}class b{constructor(t){this.currentPos=t.currentPos,this.zoom=t.zoom,this.basicImgInfo=t.basicImgInfo,this.dependToolName=""}get isDependPolygon(){return this.dependToolName===u.Polygon}get isDependRect(){return this.dependToolName===u.Rect}get isDependOriginalImage(){return this.dependToolName===""}getAbsCoord(t){return{x:(t.x-this.currentPos.x)/this.zoom,y:(t.y-this.currentPos.y)/this.zoom}}getRenderCoord(t){return{x:t.x*this.zoom+this.currentPos.x,y:t.y*this.zoom+this.currentPos.y}}coordInsideRect(t,e){const{x:i,y:s,width:n,height:o}=e;return{x:y.withinRange(t.x,[i,i+n]),y:y.withinRange(t.y,[s,s+o])}}getPolygonPointList(t,e){return t===C.Curve?x.createSmoothCurvePointsFromPointList(e):e}getIntersection(t,e,i){const s=this.getRenderCoord(e),n=this.getRenderCoord(t),o={pointA:s,pointB:n};return v.calcOptimalIntersection(i,o,s,_,this.zoom)}coordInsidePolygon(t,e,i,s){const{pointList:n}=i,o=s==null?void 0:s.lineType;if(n.length===0)return t;const r=this.getPolygonPointList(o,n);if(x.isInPolygon(t,r))return t;const p=r.concat(r[0]).map(g=>this.getRenderCoord(g)),h=this.getIntersection(t,e,p);return h?this.getAbsCoord(h==null?void 0:h.point):t}coordInsideImage(t){return this.coordInsideRect(t,c(f({},this.basicImgInfo),{x:0,y:0}))}getNextCoordByDependTool(t,e){if(this.isDependRect)return this.coordInsideRect(t,this.basicResult);if(this.isDependPolygon)return this.coordInsidePolygon(t,e,this.basicResult,this.dependToolConfig);if(this.isDependOriginalImage)return this.coordInsideImage(t)}setDependInfo(t,e){this.dependToolName=t!=null?t:"",this.dependToolConfig=t?e:void 0}setBasicImgInfo(t){this.basicImgInfo=t}setBasicResult(t){this.basicResult=t}setZoomAndCurrentPos(t,e){this.zoom=t,this.currentPos=e}isCoordInsideTarget(t){if(this.isDependPolygon)return this.isInBasicPolygon(t);if(this.isDependRect){const{x:e,y:i,width:s,height:n}=this.basicResult,o=[e,e+s],r=[i,i+n];return y.isInRange(t.x,o)&&y.isInRange(t.y,r)}return y.isInRange(t.x,[0,this.basicImgInfo.width])&&y.isInRange(t.y,[0,this.basicImgInfo.height])}isInBasicPolygon(t){var e,i;return x.isInPolygon(t,((e=this.basicResult)==null?void 0:e.pointList)||[],(i=this.dependToolConfig)==null?void 0:i.lineType)}}export{b as CoordinateUtils,I as default};
@@ -1,3 +1,3 @@
1
- import E from"color-rgba";import{NULL_COLOR as M,MathUtils as W}from"@labelbee/lb-utils";import{ELineTypes as P,SEGMENT_NUMBER as b,DEFAULT_FONT as U}from"../../constant/tool.js";import A from"./PolygonUtils.js";import T from"./UnitUtils.js";import k from"./AxisUtils.js";import{getCuboidAllSideLine as F,getPointListsByDirection as L,getCuboidTextAttributeOffset as I}from"./CuboidUtils.js";import R from"./AttributeUtils.js";var $=Object.defineProperty,j=Object.defineProperties,B=Object.getOwnPropertyDescriptors,v=Object.getOwnPropertySymbols,H=Object.prototype.hasOwnProperty,N=Object.prototype.propertyIsEnumerable,_=(r,t,i)=>t in r?$(r,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[t]=i,m=(r,t)=>{for(var i in t||(t={}))H.call(t,i)&&_(r,i,t[i]);if(v)for(var i of v(t))N.call(t,i)&&_(r,i,t[i]);return r},S=(r,t)=>j(r,B(t));const z=1,X={x:0,y:0},Y=0,y="",Z=[{d:"M0.423514 10.1595C-0.362666 7.22543 1.37854 4.20957 4.3126 3.42339C7.24666 2.63721 10.2625 4.37842 11.0487 7.31248L49.8716 152.201C50.6577 155.135 48.9165 158.151 45.9825 158.937C43.0484 159.724 40.0325 157.982 39.2464 155.048L0.423514 10.1595Z"},{d:"M14.0774 9.47294C28.5 -16.5001 91.5 25.5001 113.138 0.529419L131.773 70.076C112 96.9999 50.5 54 32.7124 79.0196L14.0774 9.47294Z"}],x=class{static drawLine(r,t,i,e={}){const o=r.getContext("2d"),{color:s=y,thickness:l=1,lineCap:c="round",lineDash:n}=e;o.save(),o.strokeStyle=s,o.lineWidth=l,o.lineCap=c,n&&o.setLineDash(n),o.beginPath(),o.moveTo(t.x,t.y),o.lineTo(i.x+1,i.y+1),o.stroke(),o.restore()}static drawRect(r,t,i={}){const e=r.getContext("2d"),{color:o=y,thickness:s=1,lineCap:l="round",hiddenText:c=!1,lineDash:n}=i;if(e.save(),e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)&&e.setLineDash(n),e.fillStyle=o,e.strokeRect(t.x,t.y,t.width,t.height),c===!1){let a="";if(t.attribute&&(a=`${a} ${t.attribute}`),this.drawText(r,{x:t.x,y:t.y-5},a),t.textAttribute){const h=`${~~t.width} * ${~~t.height}`.length*7,g=0,u=Math.max(20,t.width-h);this.drawText(r,{x:t.x,y:t.y+t.height+20+g},t.textAttribute,{textMaxWidth:u})}}e.restore()}static drawRectWithFill(r,t,i={}){const e=r.getContext("2d"),{color:o=y}=i;e.save(),e.fillStyle=o,e.fillRect(t.x,t.y,t.width,t.height),e.restore()}static drawTagByDom(r,t,i){const e=r;if(!((t==null?void 0:t.length)>0))return;const o=document.createElement("div");return o.innerHTML=t,o.setAttribute("id",i),e==null||e.appendChild(o),o}static drawTag(r,t){var i;const e=r==null?void 0:r.parentNode,o=window.self.document.getElementById("tagToolTag");if(o&&e&&e.contains(o)&&(e==null||e.removeChild(o)),!((t==null?void 0:t.length)>0))return;const s=document.createElement("div");return s.innerHTML=(i=t.reduce((l,c)=>`${l}${c.keyName}: ${c.value.join(" \u3001 ")}
2
- `,""))!=null?i:"",s.setAttribute("id","tagToolTag"),e==null||e.appendChild(s),s}static drawLineWithPointList(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=y,thickness:s=1,lineCap:l="round",lineType:c=P.Line,lineDash:n,hoverEdgeIndex:a}=i;e.save(),(()=>{e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)?e.setLineDash(n):e.setLineDash([])})(),c===P.Curve?(a!==void 0&&a>-1&&t.push(t[0]),t=A.createSmoothCurvePointsFromPointList([...t],b),a!==void 0&&a>-1&&(t=t.slice((b+1)*a,(b+1)*(a+1)))):a!==void 0&&a>-1&&(t=[...t,t[0]],t=t.slice(a,a+2));const h=[];e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let f=0;f<t.length-1;f++)t[f].specialEdge&&h.push({i1:f,i2:f+1}),e.lineTo(t[f+1].x,t[f+1].y);e.stroke(),e.save(),e.lineWidth=s*.8,e.lineCap="butt",e.strokeStyle="white",e.setLineDash([3,3]),h.forEach(f=>{const w=t[f.i1],C=t[f.i2];e.beginPath(),e.moveTo(w.x,w.y),e.lineTo(C.x,C.y),e.stroke()}),e.restore();const g=4,u=2;return t.forEach(f=>{f.specialPoint&&(this.drawSpecialPoint(r,f,g+u,o),this.drawSpecialPoint(r,f,g,"white"))}),e.restore(),t}static drawCircle(r,t,i,e={}){const o=r.getContext("2d"),{startAngleDeg:s=0,endAngleDeg:l=360,thickness:c=1,color:n=y,fill:a=y}=e,d=T.deg2rad(s),h=T.deg2rad(l);o.save(),o.beginPath(),o.strokeStyle=n,o.fillStyle=a,o.lineWidth=c,o.arc(t.x,t.y,i,d,h,!1),o.stroke(),a&&o.fill(),o.closePath(),o.restore()}static drawCircleWithFill(r,t,i=3,e={}){const o=r.getContext("2d"),{color:s=y}=e;o.save();const l=T.deg2rad(0),c=T.deg2rad(360);o.fillStyle=s,o.beginPath(),o.arc(t.x,t.y,i,l,c,!1),o.fill(),o.restore()}static drawSpecialPoint(r,t,i=6,e){const o=r.getContext("2d"),{x:s,y:l}=t;o.save(),o.beginPath(),o.fillStyle=e;const c=i*1.5,n=c*Math.sqrt(3)/2,a=c/2;o.moveTo(s,l-c),o.lineTo(s-n,l+a),o.lineTo(s+n,l+a),o.closePath(),o.fill(),o.restore()}static drawPolygon(r,t,i={}){const{isClose:e=!1,lineType:o=P.Line}=i;return e===!0&&(t=[...t,t[0]]),o===P.Curve&&(t=A.createSmoothCurvePointsFromPointList([...t])),this.drawLineWithPointList(r,t,S(m({},i),{lineType:P.Line})),t}static drawPolygonWithFill(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=y,lineType:s=P.Line}=i;e.save(),e.fillStyle=o,e.beginPath(),s===P.Curve&&(t=A.createSmoothCurvePointsFromPointList([...t,t[0]]));const[l,...c]=t;return e.moveTo(l.x,l.y),c.forEach(n=>{e.lineTo(n.x,n.y)}),e.fill(),e.restore(),t}static drawPolygonWithFillAndLine(r,t,i={}){const{strokeColor:e,fillColor:o,thickness:s,lineCap:l,isClose:c,lineType:n}=i,a=this.drawPolygon(r,t,{color:e,thickness:s,lineCap:l,isClose:c,lineType:n});return this.drawPolygonWithFill(r,t,{color:o,lineType:n}),a}static drawPolygonWithKeyPoint(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygon(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawSelectedPolygonWithFillAndLine(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygonWithFillAndLine(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawText(r,t,i,e={}){if(!i)return;const o=r.getContext("2d"),{color:s=y,font:l=U,shadowColor:c="",shadowBlur:n=0,shadowOffsetX:a=0,shadowOffsetY:d=0,textMaxWidth:h=164,offsetX:g=0,offsetY:u=0,textAlign:f="start",lineHeight:w}=e;o.save(),o.textAlign=f,o.fillStyle=s!=null?s:"white",o.font=l,o.shadowColor=c,o.shadowOffsetX=a,o.shadowOffsetY=d,o.shadowBlur=n,this.wrapText(r,`${i}`,t.x+g,t.y+u,h,w),o.restore()}static wrapText(r,t,i,e,o,s){if(typeof t!="string"||typeof i!="number"||typeof e!="number")return;const l=r.getContext("2d");typeof o=="undefined"&&(o=r&&r.width||300),typeof s=="undefined"&&(s=r&&parseInt(window.getComputedStyle(r).lineHeight,10)||parseInt(window.getComputedStyle(document.body).lineHeight,10));const c=t.split(`
3
- `);for(let n=0;n<c.length;n++){const a=c[n].split("");let d="";for(let h=0;h<a.length;h++){const g=d+a[h],f=l.measureText(g).width;o||(o=300),f>o&&h>0?(l.fillText(d,i,e),d=a[h],e+=s):d=g}l.fillText(d,i,e),e+=s}}static drawArrow(r,t,i,e={}){const{color:o=y,thickness:s=1,lineCap:l="round",theta:c=30,headLen:n=10}=e,a=Math.atan2(t.y-i.y,t.x-i.x)*180/Math.PI,d=(a+c)*Math.PI/180,h=(a-c)*Math.PI/180,g=n*Math.cos(d),u=n*Math.sin(d),f=n*Math.cos(h),w=n*Math.sin(h);r.save(),r.strokeStyle=o,r.lineWidth=s,r.lineCap=l,r.beginPath(),r.moveTo(i.x+g,i.y+u),r.lineTo(i.x,i.y),r.lineTo(i.x+f,i.y+w),r.stroke(),r.restore()}static drawArrowByCanvas(r,t,i,e={}){const o=r.getContext("2d");this.drawArrow(o,t,i,e)}static drawCuboid(r,t,i={}){const{backPoints:e,direction:o,frontPoints:s}=t,{strokeColor:l,thickness:c,fillColor:n}=i,a={color:l,thickness:c};if(e){const h=k.transformPlain2PointList(e);x.drawPolygon(r,h,S(m({},a),{isClose:!0}));const g=F(t);g==null||g.forEach(u=>{x.drawLine(r,u.p1,u.p2,m({},a))})}const d=k.transformPlain2PointList(s);if(o&&e&&s){const h=L({direction:o,frontPoints:s,backPoints:e});h&&x.drawPolygonWithFill(r,h,{color:n})}x.drawPolygon(r,d,S(m({},a),{isClose:!0}))}static drawCuboidWithText(r,t,i,e){const{strokeColor:o}=i,s=o,{config:l,hiddenText:c,selectedID:n,headerText:a,bottomText:d}=e,{backPoints:h,frontPoints:g,textAttribute:u}=t,f=g.br.x-g.bl.x;x.drawCuboid(r,t,i);let w="";(l==null?void 0:l.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(w=`${t.order}`),t.attribute&&(w=`${w} ${R.getAttributeShowText(t.attribute,l==null?void 0:l.attributeList)}`),!c&&h&&w&&x.drawText(r,{x:h.tl.x,y:h.tl.y-5},a!=null?a:w,{color:o,textMaxWidth:300});const C=I({cuboid:t,currentPos:{x:0,y:0},zoom:1,topOffset:16,leftOffset:0});if(!c&&u&&t.id!==n){const p=Math.max(20,f*.8);x.drawText(r,{x:C.left,y:C.top},d!=null?d:u,{color:s,textMaxWidth:p})}}static drawPixel({canvas:r,points:t,size:i,defaultRGBA:e,pixelSize:o=13}){const s=r.getContext("2d"),{width:l,height:c}=i,n=s.getImageData(0,0,l,c),[a,d,h,g]=E(e!=null?e:M),u=w=>{n.data[w]=a,n.data[w+1]=d,n.data[w+2]=h,n.data[w+3]=Math.floor(255*g)},f=W.generateCoordinates(o);return t.forEach(w=>{for(const[C,p]of f){const D=(w.y+p)*(n.width*4)+(w.x+C)*4;u(D)}}),s.putImageData(n,0,0),{canvas:r}}static drawHighlightFlag(r){const{canvas:t,position:i,color:e,scale:o=.1}=r,s=t.getContext("2d");if(!!s)for(const l of Z){s.beginPath();const c=l.d.split(/(?=[CLMZclmz])/);for(const n of c){const d=n.slice(1).split(" ").map(Number).map((h,g)=>g%2==0?h*o+i.x:h*o+i.y);switch(n[0]){case"M":s.moveTo(d[0],d[1]);break;case"C":s.bezierCurveTo(d[0],d[1],d[2],d[3],d[4],d[5]);break;case"L":s.lineTo(d[0],d[1]);break;case"Z":s.closePath();break}}s.fillStyle=e,s.fill()}}};let O=x;O.drawImg=(r,t,i={})=>{const e=r.getContext("2d"),{zoom:o=z,currentPos:s=X,rotate:l=Y,imgAttribute:c}=i;switch(e.save(),l){case 0:e.translate(s.x,s.y);break;case 90:e.translate(s.x+t.height*o,s.y),e.rotate(90*Math.PI/180);break;case 180:e.translate(s.x+t.width*o,s.y+t.height*o),e.rotate(Math.PI);break;case 270:e.translate(s.x,s.y+t.width*o),e.rotate(270*Math.PI/180);break;default:e.translate(s.x,s.y);break}if(c){const{contrast:n,saturation:a,brightness:d}=c;e.filter=`saturate(${a+100}%) contrast(${n+100}%) brightness(${d+100}%)`}e.drawImage(t,0,0,t.width*o,t.height*o),e.restore()};export{O as default};
1
+ import E from"color-rgba";import{NULL_COLOR as M,MathUtils as W}from"@labelbee/lb-utils";import{ELineTypes as P,SEGMENT_NUMBER as b,DEFAULT_FONT as U}from"../../constant/tool.js";import A from"./PolygonUtils.js";import T from"./UnitUtils.js";import k from"./AxisUtils.js";import{getCuboidAllSideLine as F,getPointListsByDirection as L,getCuboidTextAttributeOffset as I}from"./CuboidUtils.js";import R from"./AttributeUtils.js";var $=Object.defineProperty,j=Object.defineProperties,H=Object.getOwnPropertyDescriptors,v=Object.getOwnPropertySymbols,N=Object.prototype.hasOwnProperty,z=Object.prototype.propertyIsEnumerable,_=(r,t,i)=>t in r?$(r,t,{enumerable:!0,configurable:!0,writable:!0,value:i}):r[t]=i,m=(r,t)=>{for(var i in t||(t={}))N.call(t,i)&&_(r,i,t[i]);if(v)for(var i of v(t))z.call(t,i)&&_(r,i,t[i]);return r},S=(r,t)=>j(r,H(t));const B=1,Z={x:0,y:0},X=0,w="",Y=[{d:"M0.423514 10.1595C-0.362666 7.22543 1.37854 4.20957 4.3126 3.42339C7.24666 2.63721 10.2625 4.37842 11.0487 7.31248L49.8716 152.201C50.6577 155.135 48.9165 158.151 45.9825 158.937C43.0484 159.724 40.0325 157.982 39.2464 155.048L0.423514 10.1595Z"},{d:"M14.0774 9.47294C28.5 -16.5001 91.5 25.5001 113.138 0.529419L131.773 70.076C112 96.9999 50.5 54 32.7124 79.0196L14.0774 9.47294Z"}],x=class{static drawLine(r,t,i,e={}){const o=r.getContext("2d"),{color:s=w,thickness:l=1,lineCap:c="round",lineDash:n}=e;o.save(),o.strokeStyle=s,o.lineWidth=l,o.lineCap=c,n&&o.setLineDash(n),o.beginPath(),o.moveTo(t.x,t.y),o.lineTo(i.x+1,i.y+1),o.stroke(),o.restore()}static drawRect(r,t,i={}){const e=r.getContext("2d"),{color:o=w,thickness:s=1,lineCap:l="round",hiddenText:c=!1,lineDash:n}=i;if(e.save(),e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)&&e.setLineDash(n),e.fillStyle=o,e.strokeRect(t.x,t.y,t.width,t.height),c===!1){let a="";if(t.attribute&&(a=`${a} ${t.attribute}`),this.drawText(r,{x:t.x,y:t.y-5},a),t.textAttribute){const h=`${~~t.width} * ${~~t.height}`.length*7,g=0,y=Math.max(20,t.width-h);this.drawText(r,{x:t.x,y:t.y+t.height+20+g},t.textAttribute,{textMaxWidth:y})}}e.restore()}static drawRectWithFill(r,t,i={}){const e=r.getContext("2d"),{color:o=w}=i;e.save(),e.fillStyle=o,e.fillRect(t.x,t.y,t.width,t.height),e.restore()}static drawTagByDom(r,t,i){const e=r;if(!((t==null?void 0:t.length)>0))return;const o=document.createElement("div");return o.innerHTML=t,o.setAttribute("id",i),e==null||e.appendChild(o),o}static drawTag(r,t){var i;const e=r==null?void 0:r.parentNode,o=window.self.document.getElementById("tagToolTag");if(o&&e&&e.contains(o)&&(e==null||e.removeChild(o)),!((t==null?void 0:t.length)>0))return;const s=document.createElement("div");return s.innerHTML=(i=t.reduce((l,c)=>`${l}${c.keyName}: ${c.value.join(" \u3001 ")}
2
+ `,""))!=null?i:"",s.setAttribute("id","tagToolTag"),e==null||e.appendChild(s),s}static drawLineWithPointList(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=w,thickness:s=1,lineCap:l="round",lineType:c=P.Line,lineDash:n,hoverEdgeIndex:a}=i;e.save(),(()=>{e.strokeStyle=o,e.lineWidth=s,e.lineCap=l,Array.isArray(n)?e.setLineDash(n):e.setLineDash([])})(),c===P.Curve?(a!==void 0&&a>-1&&t.push(t[0]),t=A.createSmoothCurvePointsFromPointList([...t],b),a!==void 0&&a>-1&&(t=t.slice((b+1)*a,(b+1)*(a+1)))):a!==void 0&&a>-1&&(t=[...t,t[0]],t=t.slice(a,a+2));const h=[];e.beginPath(),e.moveTo(t[0].x,t[0].y);for(let f=0;f<t.length-1;f++)t[f].specialEdge&&h.push({i1:f,i2:f+1}),e.lineTo(t[f+1].x,t[f+1].y);e.stroke(),e.save(),e.lineWidth=s*.8,e.lineCap="butt",e.strokeStyle="white",e.setLineDash([3,3]),h.forEach(f=>{const u=t[f.i1],C=t[f.i2];e.beginPath(),e.moveTo(u.x,u.y),e.lineTo(C.x,C.y),e.stroke()}),e.restore();const g=4,y=2;return t.forEach(f=>{f.specialPoint&&(this.drawSpecialPoint(r,f,g+y,o),this.drawSpecialPoint(r,f,g,"white"))}),e.restore(),t}static drawCircle(r,t,i,e={}){const o=r.getContext("2d"),{startAngleDeg:s=0,endAngleDeg:l=360,thickness:c=1,color:n=w,fill:a=w}=e,d=T.deg2rad(s),h=T.deg2rad(l);o.save(),o.beginPath(),o.strokeStyle=n,o.fillStyle=a,o.lineWidth=c,o.arc(t.x,t.y,i,d,h,!1),o.stroke(),a&&o.fill(),o.closePath(),o.restore()}static drawCircleWithFill(r,t,i=3,e={}){const o=r.getContext("2d"),{color:s=w}=e;o.save();const l=T.deg2rad(0),c=T.deg2rad(360);o.fillStyle=s,o.beginPath(),o.arc(t.x,t.y,i,l,c,!1),o.fill(),o.restore()}static drawSpecialPoint(r,t,i=6,e){const o=r.getContext("2d"),{x:s,y:l}=t;o.save(),o.beginPath(),o.fillStyle=e;const c=i*1.5,n=c*Math.sqrt(3)/2,a=c/2;o.moveTo(s,l-c),o.lineTo(s-n,l+a),o.lineTo(s+n,l+a),o.closePath(),o.fill(),o.restore()}static drawPolygon(r,t,i={}){const{isClose:e=!1,lineType:o=P.Line}=i;return e===!0&&(t=[...t,t[0]]),o===P.Curve&&(t=A.createSmoothCurvePointsFromPointList([...t])),this.drawLineWithPointList(r,t,S(m({},i),{lineType:P.Line})),t}static drawPolygonWithFill(r,t,i={}){if(t.length<2)return;const e=r.getContext("2d"),{color:o=w,lineType:s=P.Line}=i;e.save(),e.fillStyle=o,e.beginPath(),s===P.Curve&&(t=A.createSmoothCurvePointsFromPointList([...t,t[0]]));const[l,...c]=t;return e.moveTo(l.x,l.y),c.forEach(n=>{e.lineTo(n.x,n.y)}),e.fill(),e.restore(),t}static drawPolygonWithFillAndLine(r,t,i={}){const{strokeColor:e,fillColor:o,thickness:s,lineCap:l,isClose:c,lineType:n}=i,a=this.drawPolygon(r,t,{color:e,thickness:s,lineCap:l,isClose:c,lineType:n});return this.drawPolygonWithFill(r,t,{color:o,lineType:n}),a}static drawPolygonWithKeyPoint(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygon(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawSelectedPolygonWithFillAndLine(r,t,i={}){const{pointColor:e="white",strokeColor:o}=i,s=this.drawPolygonWithFillAndLine(r,t,i);return s.forEach(l=>{this.drawCircleWithFill(r,l,4,{color:o}),this.drawCircleWithFill(r,l,3,{color:e})}),s}static drawText(r,t,i,e={}){if(!i)return;const o=r.getContext("2d"),{color:s=w,font:l=U,shadowColor:c="",shadowBlur:n=0,shadowOffsetX:a=0,shadowOffsetY:d=0,textMaxWidth:h=164,offsetX:g=0,offsetY:y=0,textAlign:f="start",lineHeight:u}=e;!o||(o.save(),Object.assign(o,{textAlign:f,fillStyle:s!=null?s:"white",font:l,shadowColor:c,shadowOffsetX:a,shadowOffsetY:d,shadowBlur:n}),this.wrapText(r,i,t.x+g,t.y+y,h,u),o.restore())}static wrapText(r,t,i,e,o,s){if(typeof t!="string"||typeof i!="number"||typeof e!="number")return;const l=r.getContext("2d");typeof o=="undefined"&&(o=r&&r.width||300),typeof s=="undefined"&&(s=r&&parseInt(window.getComputedStyle(r).lineHeight,10)||parseInt(window.getComputedStyle(document.body).lineHeight,10));const c=t.split(`
3
+ `);for(let n=0;n<c.length;n++){const a=c[n].split("");let d="";for(let h=0;h<a.length;h++){const g=d+a[h],f=l.measureText(g).width;o||(o=300),f>o&&h>0?(l.fillText(d,i,e),d=a[h],e+=s):d=g}l.fillText(d,i,e),e+=s}}static drawArrow(r,t,i,e={}){const{color:o=w,thickness:s=1,lineCap:l="round",theta:c=30,headLen:n=10}=e,a=Math.atan2(t.y-i.y,t.x-i.x)*180/Math.PI,d=(a+c)*Math.PI/180,h=(a-c)*Math.PI/180,g=n*Math.cos(d),y=n*Math.sin(d),f=n*Math.cos(h),u=n*Math.sin(h);r.save(),r.strokeStyle=o,r.lineWidth=s,r.lineCap=l,r.beginPath(),r.moveTo(i.x+g,i.y+y),r.lineTo(i.x,i.y),r.lineTo(i.x+f,i.y+u),r.stroke(),r.restore()}static drawArrowByCanvas(r,t,i,e={}){const o=r.getContext("2d");this.drawArrow(o,t,i,e)}static drawCuboid(r,t,i={}){const{backPoints:e,direction:o,frontPoints:s}=t,{strokeColor:l,thickness:c,fillColor:n}=i,a={color:l,thickness:c};if(e){const h=k.transformPlain2PointList(e);x.drawPolygon(r,h,S(m({},a),{isClose:!0}));const g=F(t);g==null||g.forEach(y=>{x.drawLine(r,y.p1,y.p2,m({},a))})}const d=k.transformPlain2PointList(s);if(o&&e&&s){const h=L({direction:o,frontPoints:s,backPoints:e});h&&x.drawPolygonWithFill(r,h,{color:n})}x.drawPolygon(r,d,S(m({},a),{isClose:!0}))}static drawCuboidWithText(r,t,i,e){const{strokeColor:o}=i,s=o,{config:l,hiddenText:c,selectedID:n,headerText:a,bottomText:d}=e,{backPoints:h,frontPoints:g,textAttribute:y}=t,f=g.br.x-g.bl.x;x.drawCuboid(r,t,i);let u="";(l==null?void 0:l.isShowOrder)&&t.order&&(t==null?void 0:t.order)>0&&(u=`${t.order}`),t.attribute&&(u=`${u} ${R.getAttributeShowText(t.attribute,l==null?void 0:l.attributeList)}`),!c&&h&&u&&x.drawText(r,{x:h.tl.x,y:h.tl.y-5},a!=null?a:u,{color:o,textMaxWidth:300});const C=I({cuboid:t,currentPos:{x:0,y:0},zoom:1,topOffset:16,leftOffset:0});if(!c&&y&&t.id!==n){const p=Math.max(20,f*.8);x.drawText(r,{x:C.left,y:C.top},d!=null?d:y,{color:s,textMaxWidth:p})}}static drawPixel({canvas:r,points:t,size:i,defaultRGBA:e,pixelSize:o=13}){const s=r.getContext("2d"),{width:l,height:c}=i,n=s.getImageData(0,0,l,c),[a,d,h,g]=E(e!=null?e:M),y=u=>{n.data[u]=a,n.data[u+1]=d,n.data[u+2]=h,n.data[u+3]=Math.floor(255*g)},f=W.generateCoordinates(o);return t.forEach(u=>{for(const[C,p]of f){const D=(u.y+p)*(n.width*4)+(u.x+C)*4;y(D)}}),s.putImageData(n,0,0),{canvas:r}}static drawHighlightFlag(r){const{canvas:t,position:i,color:e,scale:o=.1}=r,s=t.getContext("2d");if(!!s)for(const l of Y){s.beginPath();const c=l.d.split(/(?=[CLMZclmz])/);for(const n of c){const d=n.slice(1).split(" ").map(Number).map((h,g)=>g%2==0?h*o+i.x:h*o+i.y);switch(n[0]){case"M":s.moveTo(d[0],d[1]);break;case"C":s.bezierCurveTo(d[0],d[1],d[2],d[3],d[4],d[5]);break;case"L":s.lineTo(d[0],d[1]);break;case"Z":s.closePath();break}}s.fillStyle=e,s.fill()}}};let O=x;O.drawImg=(r,t,i={})=>{const e=r.getContext("2d"),{zoom:o=B,currentPos:s=Z,rotate:l=X,imgAttribute:c}=i;switch(e.save(),l){case 0:e.translate(s.x,s.y);break;case 90:e.translate(s.x+t.height*o,s.y),e.rotate(90*Math.PI/180);break;case 180:e.translate(s.x+t.width*o,s.y+t.height*o),e.rotate(Math.PI);break;case 270:e.translate(s.x,s.y+t.width*o),e.rotate(270*Math.PI/180);break;default:e.translate(s.x,s.y);break}if(c){const{contrast:n,saturation:a,brightness:d}=c;e.filter=`saturate(${a+100}%) contrast(${n+100}%) brightness(${d+100}%)`}e.drawImage(t,0,0,t.width*o,t.height*o),e.restore()};export{O 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 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};
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=>{if(this.isInPolygon(c,s.pointList,i)){const u=this.calcPolygonSize(s.pointList);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};
@@ -1 +1 @@
1
- import{ELineTypes as g,SEGMENT_NUMBER as E}from"../../constant/tool.js";var C=Object.defineProperty,I=Object.defineProperties,L=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,N=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable,P=(e,t,l)=>t in e?C(e,t,{enumerable:!0,configurable:!0,writable:!0,value:l}):e[t]=l,b=(e,t)=>{for(var l in t||(t={}))N.call(t,l)&&P(e,l,t[l]);if(O)for(var l of O(t))S.call(t,l)&&P(e,l,t[l]);return e},D=(e,t)=>I(e,L(t));function v(e,t=.5,l=!1,s=16){if(e.length<4)return e;const h=[],r=e.slice(0);let f,u,n,y,p,d,w,i,M,m,o,c,a;for(l?(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])),a=2;a<r.length-4;a+=2)for(n=(r[a+2]-r[a-2])*t,y=(r[a+4]-r[a-0])*t,p=(r[a+3]-r[a-1])*t,d=(r[a+5]-r[a+1])*t,c=0;c<=s;c++)o=c/s,w=2*Math.pow(o,3)-3*Math.pow(o,2)+1,i=-(2*Math.pow(o,3))+3*Math.pow(o,2),M=Math.pow(o,3)-2*Math.pow(o,2)+o,m=Math.pow(o,3)-Math.pow(o,2),f=w*r[a]+i*r[a+2]+M*n+m*y,u=w*r[a+1]+i*r[a+3]+M*p+m*d,h.push(f),h.push(u);const _=[];for(let x=0;x<h.length-1;x+=2)_.push({x:h[x],y:h[x+1]});if(l)for(let x=0;x<s+1;x++){const j=_.shift();_.push(j)}return _}const R=(e,t=16)=>v(e.reduce((l,s)=>[...l,s.x,s.y],[]),.5,!1,t);function T(e,t,l=g.Line){let s=0,h,r,f,u;t=[...t],l===g.Curve&&(t=v(t.reduce((y,p)=>[...y,p.x,p.y],[]),.5,!0,E)),[f]=t;const n=t.length;for(h=1;h<=n;h++)u=t[h%n],e.x>Math.min(f.x,u.x)&&e.x<=Math.max(f.x,u.x)&&e.y<=Math.max(f.y,u.y)&&f.x!==u.x&&(r=(e.x-f.x)*(u.y-f.y)/(u.x-f.x)+f.y,(f.y===u.y||e.y<=r)&&s++),f=u;return s%2!=0}function U(e,t=1){return e.map(l=>D(b({},l),{x:l.x*t,y:l.y*t}))}export{v as createSmoothCurvePoints,R as createSmoothCurvePointsFromPointList,U as getPolygonPointUnderZoom,T as isInPolygon};
1
+ import{ELineTypes as d,SEGMENT_NUMBER as E}from"../../constant/tool.js";var b=Object.defineProperty,C=Object.defineProperties,L=Object.getOwnPropertyDescriptors,O=Object.getOwnPropertySymbols,N=Object.prototype.hasOwnProperty,P=Object.prototype.propertyIsEnumerable,j=(t,e,a)=>e in t?b(t,e,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[e]=a,S=(t,e)=>{for(var a in e||(e={}))N.call(e,a)&&j(t,a,e[a]);if(O)for(var a of O(e))P.call(e,a)&&j(t,a,e[a]);return t},D=(t,e)=>C(t,L(e));function v(t,e=.5,a=!1,u=16){if(t.length<4)return t;const l=[],r=t.slice(0);let _,M,o,w,h,x,i,p,s,m,n,c,f;for(a?(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(o=(r[f+2]-r[f-2])*e,w=(r[f+4]-r[f-0])*e,h=(r[f+3]-r[f-1])*e,x=(r[f+5]-r[f+1])*e,c=0;c<=u;c++)n=c/u,i=2*Math.pow(n,3)-3*Math.pow(n,2)+1,p=-(2*Math.pow(n,3))+3*Math.pow(n,2),s=Math.pow(n,3)-2*Math.pow(n,2)+n,m=Math.pow(n,3)-Math.pow(n,2),_=i*r[f]+p*r[f+2]+s*o+m*w,M=i*r[f+1]+p*r[f+3]+s*h+m*x,l.push(_),l.push(M);const g=[];for(let y=0;y<l.length-1;y+=2)g.push({x:l[y],y:l[y+1]});if(a)for(let y=0;y<u+1;y++){const I=g.shift();g.push(I)}return g}const R=(t,e=16)=>v(t.reduce((a,u)=>[...a,u.x,u.y],[]),.5,!1,e);function T(t,e,a=d.Line){let u=0,l,r;a===d.Curve&&(e=v(e.flatMap(h=>[h.x,h.y]),.5,!0,E));const[_,M,o,w]=e.reduce(([h,x,i,p],{x:s,y:m})=>[Math.min(h,s),Math.max(x,s),Math.min(i,m),Math.max(p,m)],[Infinity,-Infinity,Infinity,-Infinity]);if(t.x<_||t.x>M||t.y<o||t.y>w)return!1;l=e[e.length-1];for(let h=0;h<e.length;h++){if(r=e[h],t.y>Math.min(l.y,r.y)&&t.y<=Math.max(l.y,r.y)&&t.x<=Math.max(l.x,r.x)){const x=l.y!==r.y?(t.y-l.y)*(r.x-l.x)/(r.y-l.y)+l.x:l.x;t.x<=x&&u++}if(l=r,u%2!=0&&h>e.length/2)break}return u%2!=0}function U(t,e=1){return t.map(a=>D(S({},a),{x:a.x*e,y:a.y*e}))}export{v as createSmoothCurvePoints,R as createSmoothCurvePointsFromPointList,U as getPolygonPointUnderZoom,T as isInPolygon};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@labelbee/lb-annotation",
3
- "version": "1.28.0-alpha.8",
3
+ "version": "1.28.0-alpha.9",
4
4
  "description": "Annotation tool collection",
5
5
  "keywords": [
6
6
  "annotation",