@labelbee/lb-annotation 1.27.0-alpha.5 → 1.27.0-alpha.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/_virtual/highlightWorker.js +1 -1
  2. package/dist/constant/keyCode.js +1 -1
  3. package/dist/core/pointCloud/index.js +2 -2
  4. package/dist/core/pointCloud/matrix.js +1 -1
  5. package/dist/core/pointCloud/omniCamera.js +1 -1
  6. package/dist/core/toolOperation/ViewOperation.js +2 -1
  7. package/dist/core/toolOperation/pointCloud2DRectOperation.js +1 -1
  8. package/dist/core/toolOperation/pointOperation.js +1 -1
  9. package/dist/core/toolOperation/polygonOperation.js +1 -1
  10. package/dist/core/toolOperation/rectOperation.js +1 -1
  11. package/dist/core/toolOperation/utils/reCalcRect.js +1 -0
  12. package/dist/index.js +1 -1
  13. package/dist/types/constant/keyCode.d.ts +1 -0
  14. package/dist/types/core/pointCloud/index.d.ts +5 -1
  15. package/dist/types/core/toolOperation/ViewOperation.d.ts +3 -0
  16. package/dist/types/core/toolOperation/pointCloud2DRectOperation.d.ts +8 -3
  17. package/dist/types/core/toolOperation/pointOperation.d.ts +4 -0
  18. package/dist/types/core/toolOperation/polygonOperation.d.ts +2 -0
  19. package/dist/types/core/toolOperation/rectOperation.d.ts +14 -3
  20. package/dist/types/core/toolOperation/utils/reCalcRect.d.ts +9 -0
  21. package/dist/types/index.d.ts +3 -1
  22. package/dist/types/utils/tool/AxisUtils.d.ts +1 -0
  23. package/dist/types/utils/tool/PolygonUtils.d.ts +1 -0
  24. package/dist/types/utils/tool/RectUtils.d.ts +1 -0
  25. package/dist/utils/tool/PolygonUtils.js +1 -1
  26. package/es/_virtual/highlightWorker.js +1 -1
  27. package/es/constant/keyCode.js +1 -1
  28. package/es/core/pointCloud/index.js +2 -2
  29. package/es/core/pointCloud/matrix.js +1 -1
  30. package/es/core/pointCloud/omniCamera.js +1 -1
  31. package/es/core/toolOperation/ViewOperation.js +2 -1
  32. package/es/core/toolOperation/pointCloud2DRectOperation.js +1 -1
  33. package/es/core/toolOperation/pointOperation.js +1 -1
  34. package/es/core/toolOperation/polygonOperation.js +1 -1
  35. package/es/core/toolOperation/rectOperation.js +1 -1
  36. package/es/core/toolOperation/utils/reCalcRect.js +1 -0
  37. package/es/index.js +1 -1
  38. package/es/utils/tool/PolygonUtils.js +1 -1
  39. package/package.json +2 -2
@@ -1,4 +1,4 @@
1
- import*as a from"three";import{toolStyleConverter as L,PerspectiveShiftUtils as B,EPerspectiveView as C,PointCloudUtils as E,DEFAULT_SPHERE_PARAMS as O}from"@labelbee/lb-utils";import V from"../../_virtual/highlightWorker.js";import R from"../../_virtual/filterBoxWorker.js";import{isInPolygon as H}from"../../utils/tool/polygonTool.js";import k from"../../utils/uuid.js";import A from"../../utils/MathUtils.js";import W from"../../utils/ImgUtils.js";import{PCDLoader as Z}from"./PCDLoader.js";import{OrbitControls as U}from"./OrbitControls.js";import{PointCloudCache as G}from"./cache.js";import{getCuboidFromPointCloudBox as D,getHighlightIndexByPoints as $,mergeHighlightList as Y}from"./matrix.js";export{createThreeMatrix4,getCuboidFromPointCloudBox,getHighlightIndexByPoints,isFisheyeCalibValid,isInImage,isMatrixValid,isMatrixValidByArr,lidar2FisheyeImage,lidar2image,mergeHighlightList,oCamFisheyeTransfer,omniCamera11VTransfer,point3DLidar2Image,pointCloudLidar2image,pointListLidar2Img,pointMappingLidar2image,rotatePoint,transferKitti2Matrix}from"./matrix.js";import{PointCloudSegmentOperation as X}from"./segmentation.js";import q from"./store/index.js";import K from"./render/index.js";import Q from"../toolOperation/eventListener.js";import"../../constant/tool.js";import"../scheduler.js";var J=Object.defineProperty,tt=Object.defineProperties,et=Object.getOwnPropertyDescriptors,_=Object.getOwnPropertySymbols,it=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,j=(w,t,e)=>t in w?J(w,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):w[t]=e,b=(w,t)=>{for(var e in t||(t={}))it.call(t,e)&&j(w,e,t[e]);if(_)for(var e of _(t))rt.call(t,e)&&j(w,e,t[e]);return w},P=(w,t)=>tt(w,et(t)),S=(w,t,e)=>new Promise((i,r)=>{var o=d=>{try{s(e.next(d))}catch(n){r(n)}},h=d=>{try{s(e.throw(d))}catch(n){r(n)}},s=d=>d.done?i(d.value):Promise.resolve(d.value).then(o,h);s((e=e.apply(w,t)).next())});const nt=30,I=new V({type:"module"});class ot extends Q{constructor({container:t,noAppend:e,isOrthographicCamera:i,orthographicParams:r,backgroundColor:o="#4C4C4C",config:h,isSegment:s,checkMode:d}){super();this.zAxisLimit=10,this.initCameraPosition=this.DEFAULT_INIT_CAMERA_POSITION,this.isOrthographicCamera=!1,this.pointsUuid="",this.pointCloudObjectName="pointCloud",this.rangeObjectName="range",this.highlightGroupName="highlightBoxes",this.showDirection=!0,this.pointsMaterialSize=1,this.isSegment=!1,this.checkMode=!1,this.workerLoading=!1,this.nextTick=()=>{!this.segmentOperation||this.segmentOperation._raycasting()},this.keydown=n=>{if(!!this.store)switch(n.key){case" ":this.controls.enablePan=!0,this.store.setForbidOperation(!0);break}},this.keyup=n=>{if(!!this.store)switch(n.key){case" ":this.controls.enablePan=!1,this.store.setForbidOperation(!1);break}},this.addSphereToSense=(n,l="blue")=>{var c;const u=(c=n.id)!=null?c:k();this.removeObjectByName(u,"sphere");const{radius:m,widthSegments:f,heightSegments:p}=O,{center:g}=n,x=new a.Group,y=new a.SphereGeometry(m,f,p),M=new a.MeshBasicMaterial({color:l}),v=new a.Mesh(y,M);v.position.set(g.x,g.y,g.z),x.add(v),x.name=`sphere${u}`,this.scene.add(x)},this.generateSphere=n=>{const{fill:l}=this.getColorFromConfig(n.attribute);this.addSphereToSense(n,l),this.render()},this.generateSpheres=n=>{n.forEach(l=>{const{fill:c}=this.getColorFromConfig(l.attribute);this.addSphereToSense(l,c)}),this.render()},this.addBoxToSense=(n,l=16777215)=>{var c;const u=(c=n.id)!=null?c:k();this.removeObjectByName(u,"box");const{center:m,width:f,height:p,depth:g,rotation:x}=n,y=new a.Group,M=new a.BoxGeometry(f,p,g),v=new a.MeshBasicMaterial({color:"blue"}),z=new a.Mesh(M,v),N=new a.BoxHelper(z,l),F=this.generateBoxArrow(n),T=this.generateBoxTrackID(n);T&&y.add(T),y.add(N),y.add(F),m&&y.position.set(m.x,m.y,m.z),x&&y.rotation.set(0,0,x),y.name=`box${u}`,this.scene.add(y)},this.applyCameraTarget=n=>{if(this.camera.type==="OrthographicCamera"&&n){const l=this.getOrthographicCameraTarget(n);this.updateCameraZoom(n.zoom),this.updateCamera(n.position,l)}},this.initShaderMaterial=()=>({vertexShader:`
1
+ import*as a from"three";import{toolStyleConverter as k,PerspectiveShiftUtils as S,EPerspectiveView as b,PointCloudUtils as E,DEFAULT_SPHERE_PARAMS as O}from"@labelbee/lb-utils";import V from"../../_virtual/highlightWorker.js";import R from"../../_virtual/filterBoxWorker.js";import{isInPolygon as H}from"../../utils/tool/polygonTool.js";import L from"../../utils/uuid.js";import A from"../../utils/MathUtils.js";import W from"../../utils/ImgUtils.js";import{PCDLoader as U}from"./PCDLoader.js";import{OrbitControls as Z}from"./OrbitControls.js";import{PointCloudCache as G}from"./cache.js";import{getCuboidFromPointCloudBox as I,getHighlightIndexByPoints as $,mergeHighlightList as Y}from"./matrix.js";export{createThreeMatrix4,getCuboidFromPointCloudBox,getHighlightIndexByPoints,isFisheyeCalibValid,isInImage,isMatrixValid,isMatrixValidByArr,lidar2FisheyeImage,lidar2image,mergeHighlightList,oCamFisheyeTransfer,omniCamera11VTransfer,point3DLidar2Image,pointCloudLidar2image,pointListLidar2Img,pointMappingLidar2image,rotatePoint,transferKitti2Matrix}from"./matrix.js";import{PointCloudSegmentOperation as X}from"./segmentation.js";import q from"./store/index.js";import K from"./render/index.js";import Q from"../toolOperation/eventListener.js";import"../../constant/tool.js";import"../scheduler.js";var J=Object.defineProperty,tt=Object.defineProperties,et=Object.getOwnPropertyDescriptors,_=Object.getOwnPropertySymbols,it=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,j=(C,t,e)=>t in C?J(C,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):C[t]=e,P=(C,t)=>{for(var e in t||(t={}))it.call(t,e)&&j(C,e,t[e]);if(_)for(var e of _(t))rt.call(t,e)&&j(C,e,t[e]);return C},B=(C,t)=>tt(C,et(t)),v=(C,t,e)=>new Promise((i,r)=>{var s=d=>{try{o(e.next(d))}catch(n){r(n)}},h=d=>{try{o(e.throw(d))}catch(n){r(n)}},o=d=>d.done?i(d.value):Promise.resolve(d.value).then(s,h);o((e=e.apply(C,t)).next())});const nt=30,D=new V({type:"module"});class ot extends Q{constructor({container:t,noAppend:e,isOrthographicCamera:i,orthographicParams:r,backgroundColor:s="#4C4C4C",config:h,isSegment:o,checkMode:d}){super();this.zAxisLimit=10,this.initCameraPosition=this.DEFAULT_INIT_CAMERA_POSITION,this.isOrthographicCamera=!1,this.pointsUuid="",this.pointCloudObjectName="pointCloud",this.rangeObjectName="range",this.highlightGroupName="highlightBoxes",this.showDirection=!0,this.pointsMaterialSize=1,this.isSegment=!1,this.checkMode=!1,this.workerLoading=!1,this.nextTick=()=>{!this.segmentOperation||this.segmentOperation._raycasting()},this.keydown=n=>{if(!!this.store)switch(n.key){case" ":this.controls.enablePan=!0,this.store.setForbidOperation(!0);break}},this.keyup=n=>{if(!!this.store)switch(n.key){case" ":this.controls.enablePan=!1,this.store.setForbidOperation(!1);break}},this.addSphereToSense=(n,l="blue")=>{var c;const u=(c=n.id)!=null?c:L();this.removeObjectByName(u,"sphere");const{radius:p,widthSegments:x,heightSegments:g}=O,{center:m}=n,f=new a.Group,y=new a.SphereGeometry(p,x,g),w=new a.MeshBasicMaterial({color:l}),M=new a.Mesh(y,w);M.position.set(m.x,m.y,m.z),f.add(M),f.name=`sphere${u}`,this.scene.add(f)},this.generateSphere=n=>{const{fill:l}=this.getColorFromConfig(n.attribute);this.addSphereToSense(n,l),this.render()},this.generateSpheres=n=>{n.forEach(l=>{const{fill:c}=this.getColorFromConfig(l.attribute);this.addSphereToSense(l,c)}),this.render()},this.addBoxToSense=(n,l=16777215)=>{var c;const u=(c=n.id)!=null?c:L();this.removeObjectByName(u,"box");const{center:p,width:x,height:g,depth:m,rotation:f}=n,y=new a.Group,w=new a.BoxGeometry(x,g,m),M=new a.MeshBasicMaterial({color:"blue"}),z=new a.Mesh(w,M),N=new a.BoxHelper(z,l),F=this.generateBoxArrow(n),T=this.generateBoxTrackID(n);T&&y.add(T),y.add(N),y.add(F),p&&y.position.set(p.x,p.y,p.z),f&&y.rotation.set(0,0,f),y.name=`box${u}`,this.scene.add(y)},this.applyCameraTarget=n=>{if(this.camera.type==="OrthographicCamera"&&n){const l=this.getOrthographicCameraTarget(n);this.updateCameraZoom(n.zoom),this.updateCamera(n.position,l)}},this.initShaderMaterial=()=>({vertexShader:`
2
2
  attribute vec3 dimensions;
3
3
  varying vec3 vDimensions;
4
4
  uniform float pointSize;
@@ -28,4 +28,4 @@ import*as a from"three";import{toolStyleConverter as L,PerspectiveShiftUtils as
28
28
 
29
29
  // Output the final color
30
30
  gl_FragColor = vec4(color, 1.0);
31
- }`,uniforms:{pointSize:{value:this.pointsMaterialSize}}}),this.loadPCDFile=(...n)=>S(this,[...n],function*(l=this.currentPCDSrc,c){if(!l)return;this.clearPointCloud(),this.cacheInstance.clearCache2DHighlightIndex(),this.currentPCDSrc=l;const{points:u,color:m}=yield this.cacheInstance.loadPCDFile(l),f=new a.BufferGeometry;f.setAttribute("position",new a.BufferAttribute(u,3)),this.isSegment||f.setAttribute("dimensions",new a.BufferAttribute(m,3)),this.initCloudData(u);const p=new a.Points(f);this.renderPointCloud(p,c),this.emit("loadPCDFileEnd")}),this.getHighlightIndexByMappingImgList=n=>S(this,[n],function*({mappingImgList:l,points:c}){const u=l.length===0?[]:yield Promise.all(l.map(p=>W.load(p.url))),m=l.map((p,g)=>{var x;if(this.cacheInstance.cache2DHighlightIndex.has(p.url))return(x=this.cacheInstance.cache2DHighlightIndex.get(p.url))!=null?x:[];const y=$({points:c,calib:p.calib,width:u[g].width,height:u[g].height});return this.cacheInstance.cache2DHighlightIndex.set(p.url,y),y});return Y(m)}),this.filterPreResult=(n,l,c)=>S(this,null,function*(){const{points:u}=yield this.cacheInstance.loadPCDFile(n),m=yield this.cacheInstance.loadIndexMap(n,u);return new Promise(f=>{const p=c.map(g=>{const x=A.calculatePointsInsideBox({indexMap:m,polygon:D(g).polygonPointList,zScope:[g.center.z-g.depth/2,g.center.z+g.depth/2],box:g}),y=x>=l.lowerLimitPointsNumInBox;return P(b({},g),{valid:y,count:x})});f(p)})}),this.loadPCDFileByBox=(n,l,c)=>S(this,null,function*(){const u=(p,g)=>S(this,null,function*(){const{width:x=0,height:y=0,depth:M=0}=c!=null?c:{},v=yield this.filterPointsByBox(P(b({},l),{width:l.width+x,height:l.height+y,depth:l.depth+M}),p,g);if(!v){console.error("filter Error");return}this.clearPointCloud(),this.currentPCDSrc=n;const z=new a.Points(v.geometry);z.name=this.pointCloudObjectName,this.scene.add(z),this.render()}),{points:m,color:f}=yield this.cacheInstance.loadPCDFile(n);u(m,f)}),this.generateRange=n=>{const l=this.createRange(n);this.scene.add(l)},this.generateBoxArrow=({width:n})=>{const l=new a.Vector3(1,0,0),c=new a.Vector3(n/2,0,0),u=2,m=16776960,f=new a.ArrowHelper(l,c,u,m);return f.visible=this.showDirection,f},this.generateBoxTrackID=n=>{if(!n.trackID)return;const l=new a.Texture(this.getTextCanvas(n.trackID.toString()));l.needsUpdate=!0;const c=new a.SpriteMaterial({map:l,depthWrite:!1}),u=new a.Sprite(c);return u.scale.set(5,5,5),u.position.set(-n.width/2,0,n.depth/2+.5),u},this.applyZAxisPoints=n=>{this.zAxisLimit=n,this.filterZAxisPoints(),this.render()},this.updatePointSize=({zoomIn:n,customSize:l})=>{const c=this.scene.getObjectByName(this.pointCloudObjectName);if(!c)return;const u=c.material.uniforms.pointSize.value;n?c.material.uniforms.pointSize.value=Math.min(u*1.2,10):c.material.uniforms.pointSize.value=Math.max(u/1.2,1),l&&(c.material.uniforms.pointSize.value=l,this.pointsMaterialSize=l),c.material.uniformsNeedUpdate=!0,this.render()},this.container=t,this.renderer=new a.WebGLRenderer({antialias:!0}),this.backgroundColor=o,this.config=h,this.checkMode=d!=null?d:!1,i&&r?(this.isOrthographicCamera=!0,this.camera=new a.OrthographicCamera(r.left,r.right,r.top,r.bottom,r.near,r.far)):this.camera=new a.PerspectiveCamera(30,this.containerWidth/this.containerHeight,1,1e3),this.initCamera(),this.scene=new a.Scene,this.controls=new U(this.camera,s?this.container:this.renderer.domElement),this.pcdLoader=new Z,this.axesHelper=new a.AxesHelper(1e3),this.scene.add(this.camera),e||t.appendChild(this.renderer.domElement),this.init(),this.cacheInstance=G.getInstance(),s===!0&&(this.initSegment(),this.isSegment=!0)}initSegment(){this.store=new q(this.pointCloudDelegate),this.controls.enablePan=!1,this.controls.addEventListener("start",this.orbiterStart.bind(this)),this.controls.addEventListener("change",this.orbiterChange.bind(this)),this.controls.addEventListener("end",this.orbiterEnd.bind(this)),this.segmentOperation=new X(b({dom:this.container,store:this.store},this.eventBus)),this.pointCloudRender=new K(P(b({store:this.store},this.eventBus),{nextTick:this.nextTick})),this.initMsg(),document.addEventListener("keydown",this.keydown),document.addEventListener("keyup",this.keyup)}orbiterStart(){}orbiterChange(){!this.store||(this.store.orbiting=!0)}orbiterEnd(){!this.store||(this.store.orbiting=!1)}get currentSegmentTool(){var t;return(t=this.segmentOperation)==null?void 0:t.currentToolName}get pointCloudObject(){return this.scene.getObjectByName(this.pointCloudObjectName)}initMsg(){!this.segmentOperation||(this.on("CircleSelector",this.segmentOperation.updateSelector2Circle.bind(this.segmentOperation)),this.on("LassoSelector",this.segmentOperation.updateSelector2Lasso.bind(this.segmentOperation)),this.on("RectSelector",this.segmentOperation.updateSelector2Rect.bind(this.segmentOperation)),this.on("clearPointCloud",this.clearPointCloud.bind(this)),this.on("loadPCDFile",this.loadPCDFile.bind(this)))}unbindMsg(){!this.segmentOperation||(this.unbind("CircleSelector",this.segmentOperation.updateSelector2Circle.bind(this.segmentOperation)),this.unbind("LassoSelector",this.segmentOperation.updateSelector2Lasso.bind(this.segmentOperation)),this.unbind("RectSelector",this.segmentOperation.updateSelector2Rect.bind(this.segmentOperation)),this.unbind("clearPointCloud",this.clearPointCloud.bind(this)),this.unbind("loadPCDFile",this.loadPCDFile.bind(this)))}get eventBus(){return{on:this.on.bind(this),emit:this.emit.bind(this),unbind:this.unbind.bind(this)}}get pointCloudDelegate(){return b({container:this.container,scene:this.scene,camera:this.camera,renderer:this.renderer,checkMode:this.checkMode,config:this.config},this.eventBus)}get DEFAULT_INIT_CAMERA_POSITION(){return new a.Vector3(-.01,0,1e3)}get containerWidth(){return this.container.clientWidth}get containerHeight(){return this.container.clientHeight}setInitCameraPosition(t){this.initCameraPosition=t}setConfig(t){var e;this.config=t,(e=this.store)==null||e.setConfig(t)}initOrthographicCamera(t){if(this.camera.type!=="OrthographicCamera")return;const{left:e,right:i,top:r,bottom:o,near:h,far:s}=t;this.camera.left=e,this.camera.right=i,this.camera.top=r,this.camera.bottom=o,this.camera.near=h,this.camera.far=s,this.camera.updateProjectionMatrix()}initPerspectiveCamera(){this.camera.type==="PerspectiveCamera"&&(this.camera.fov=30,this.camera.aspect=this.containerWidth/this.containerHeight,this.camera.near=1,this.camera.far=1e3,this.camera.updateProjectionMatrix())}initCamera(){const{camera:t}=this;if(this.isOrthographicCamera){const{x:e,y:i,z:r}=this.initCameraPosition;t.position.set(e,i,r)}else t.position.set(-1,0,500);t.up.set(0,0,1)}initControls(){const{controls:t}=this;t.addEventListener("change",()=>{this.render()}),this.setDefaultControls()}setDefaultControls(){const{controls:t}=this,e=[0,0,0];t.target=new a.Vector3(...e),t.addEventListener("change",()=>{this.render()}),t.maxPolarAngle=Math.PI/2,t.update()}initRenderer(){const{renderer:t}=this;t.setPixelRatio(window.devicePixelRatio),t.setSize(this.containerWidth,this.containerHeight)}init(){const{scene:t}=this;t.background=new a.Color(this.backgroundColor),this.initControls(),this.initRenderer()}removeObjectByName(t,e=""){const i=this.scene.getObjectByName(e+t);i&&i.removeFromParent()}getColorFromConfig(t){return L.getColorFromConfig({attribute:t},P(b({},this.config),{attributeConfigurable:!0}),{})}generateBox(t,e=16777215){const i=e;this.addBoxToSense(t,i),this.render()}getAllAttributeColor(t){return t.reduce((e,i)=>(e[i.attribute]=this.getColorFromConfig(i.attribute),e),{})}generateBoxes(t){t.forEach(e=>{this.addBoxToSense(e)}),this.render()}getOrthographicCamera(t){const{center:e,width:i,height:r}=t,o=10,h=e.x-i/2-o,s=e.x-i/2+o,d=e.y+r/2+o,n=e.y-r/2-o,l=100,c=-100,u=500/l;return{left:h,right:s,top:d,bottom:n,near:l,far:c,zoom:u}}updateCameraZoom(t){this.camera.zoom=t,this.camera.updateProjectionMatrix()}updateCameraByBox(t,e,i){const{center:r,width:o,height:h,depth:s,rotation:d}=t,n=this.getCameraVector(r,d,{width:o,height:h,depth:s},e);return i?(this.updateCamera(i,r),new a.Vector3(i.x,i.y,i.z)):(this.updateCamera(n,r),n)}updateCameraBySphere(t,e){const{center:i}=t,{radius:r}=O,o=this.getCameraVector(i,0,{width:r*2,height:r*2,depth:r*2},e);return this.updateCamera(o,i),o}updateOrthoCamera(t,e){const i=this.updateCameraByBox(t,e);return this.camera.zoom=1,this.camera.updateProjectionMatrix(),{cameraPositionVector:i}}updateOrthoCameraBySphere(t,e){const i=this.updateCameraBySphere(t,e);return this.camera.zoom=1,this.camera.updateProjectionMatrix(),{cameraPositionVector:i}}updateTopCamera(){this.setInitCameraPosition(this.DEFAULT_INIT_CAMERA_POSITION),this.camera.zoom=1,this.initCamera(),this.setDefaultControls(),this.camera.updateProjectionMatrix(),this.render()}updateCamera(t,e){this.camera.position.set(t.x,t.y,t.z),this.controls.target=new a.Vector3(e.x,e.y,e.z),this.controls.update()}resetCamera(){this.updateCamera(this.DEFAULT_INIT_CAMERA_POSITION,{x:0,y:0,z:0})}getOrthographicCameraTarget(t){const e=new a.Vector3(0,0,-1).applyQuaternion(t.quaternion);return t.position.clone().add(e)}createThreeMatrix4(t){return new a.Matrix4().set(...t)}filterPointsByBox(t,e,i){var r,o,h;if(!e){const s=this.scene.getObjectByName(this.pointCloudObjectName);if(!s)return console.error("There is no corresponding point cloud object"),Promise.resolve(void 0);e=(h=(o=(r=s==null?void 0:s.geometry)==null?void 0:r.attributes)==null?void 0:o.position)==null?void 0:h.array}if(window.Worker){const{zMin:s,zMax:d,polygonPointList:n}=D(t),l=e;i=i!=null?i:new Float32Array([]);const c={boxParams:t,zMin:s,zMax:d,polygonPointList:n,color:i,position:l};return new Promise(u=>{const m=new R;m.postMessage(c),m.onmessage=f=>{const{color:p,position:g,num:x}=f.data,y=new a.BufferGeometry;y.setAttribute("position",new a.Float32BufferAttribute(g,3)),y.setAttribute("color",new a.Float32BufferAttribute(p,3)),y.computeBoundingSphere(),m.terminate(),u({geometry:y,num:x})}})}return Promise.resolve(void 0)}getCameraVector(t,e,i,r=C.Front,o=nt){let h=B.frontViewMatrix4(o);switch(r){case C.Front:break;case C.Back:h=B.backViewMatrix4(o);break;case C.Left:h=B.leftViewMatrix4(o);break;case C.Right:h=B.rightViewMatrix4(o);break;case C.Top:h=B.topViewMatrix4(o);break;case C.LFT:h=B.leftFrontTopViewMatrix4(o,i);break;case C.RBT:h=B.rightBackTopViewMatrix4(o,i);break}const s=this.createThreeMatrix4(h),d=new a.Matrix4().makeTranslation(-t.x,-t.y,-t.z),n=new a.Matrix4().makeTranslation(t.x,t.y,t.z),l=new a.Matrix4().makeRotationZ(e);return new a.Vector3(t.x,t.y,t.z).clone().applyMatrix4(s).applyMatrix4(d).applyMatrix4(l).applyMatrix4(n)}createRange(t){this.removeObjectByName(this.rangeObjectName);const i=new a.EllipseCurve(0,0,t,t,0,2*Math.PI,!1,0).getPoints(50),r=new a.BufferGeometry().setFromPoints(i),o=new a.LineBasicMaterial({color:16711680}),h=new a.Line(r,o);return h.name=this.rangeObjectName,h}renderPointCloud(t,e){t.name=this.pointCloudObjectName;const i=new a.ShaderMaterial(this.initShaderMaterial());e&&this.generateRange(e),this.pointsUuid=t.uuid,t.material=i,this.filterZAxisPoints(t),this.scene.add(t),this.render()}clearAllBox(){this.clearAllGroupByPrefix("box")}clearAllSphere(){this.clearAllGroupByPrefix("sphere")}clearAllGroupByPrefix(t=""){const e=this.scene.children;for(let i=e.length-1;i>=0;i--){const r=e[i];r.type==="Group"&&r.name.startsWith(t)&&this.removeObjectByName(r.name)}}clearPointCloud(){this.removeObjectByName(this.pointCloudObjectName)}clearPointCloudAndRender(){this.clearPointCloud(),this.render()}initCloudData(t){if(!!this.store){for(let e=0;e<t.length;e+=3){const i=t[e],r=t[e+1],o=t[e+2];this.store.cloudData.set(`${i}@${r}@${o}`,{visible:!1})}this.store.setOriginPoints(t)}}handleWebworker(t){return S(this,null,function*(){return new Promise((e,i)=>{if(this.workerLoading){i(new Error("workerLoading"));return}this.workerLoading=!0,I.postMessage(t),I.onmessage=r=>{e(r.data),this.workerLoading=!1},I.onerror=r=>{i(r),this.workerLoading=!1}})})}highlightOriginPointCloud(t){return S(this,arguments,function*(e,i=[]){const r=this.scene.getObjectByName(this.pointCloudObjectName);if(!!r)return this.highlightPCDSrc=this.currentPCDSrc,new Promise((o,h)=>{if(window.Worker){const d=(e?[...e]:[]).map(c=>D(c)),n=this.getAllAttributeColor(d),l={cuboidList:d,position:r.geometry.attributes.position.array,color:r.geometry.attributes.dimensions.array,colorList:n,highlightIndex:i};this.handleWebworker(l).then(c=>{const{color:u}=c,m=new a.BufferAttribute(u,3);if(!this.highlightPCDSrc||this.highlightPCDSrc!==this.currentPCDSrc||r.geometry.attributes.position.array.length!==u.length){h(new Error("Error Path"));return}this.cacheInstance.updateColor(this.highlightPCDSrc,u),this.highlightPCDSrc=void 0,m.needsUpdate=!0,r.geometry.setAttribute("dimensions",m),r.geometry.attributes.dimensions.needsUpdate=!0,o(u),this.render()}).catch(c=>{h(c)})}})})}clearHighlightBoxes(){this.removeObjectByName(this.highlightGroupName)}clearHighlightBoxesAndRender(){this.clearHighlightBoxes(),this.render()}highlightBoxes(t){const e=new a.Group;t.forEach(i=>{const{center:{x:r,y:o,z:h},width:s,height:d,depth:n,rotation:l}=i,{fill:c}=L.getColorFromConfig({attribute:i.attribute},P(b({},this.config),{attributeConfigurable:!0}),{}),u=new a.BoxGeometry(s,d,n),m=new a.MeshBasicMaterial({color:c,transparent:!0,opacity:.2,depthTest:!1});u.rotateZ(l),u.translate(r,o,h);const f=new a.Mesh(u,m);e.add(f);const p=new a.PlaneGeometry(n,d);p.rotateY(Math.PI/2),p.rotateZ(l);const g=new a.Vector3(s/2,0,0),x=new a.Matrix4().makeRotationY(Math.PI/2).makeRotationZ(l);g.applyMatrix4(x),p.translate(r+g.x,o+g.y,h+g.z);const y=new a.MeshBasicMaterial({color:c,side:a.DoubleSide,transparent:!0,opacity:.8,depthTest:!1}),M=new a.Mesh(p,y);e.add(M)}),e.name=this.highlightGroupName,this.scene.add(e),this.render()}updateColor(t){const e=this.scene.getObjectByName(this.pointCloudObjectName);if(e){const i=new a.BufferAttribute(t,3);e.geometry.setAttribute("dimensions",i),e.geometry.attributes.dimensions.needsUpdate=!0,this.render()}}setShowDirection(t){this.showDirection=t,this.scene.children.forEach(e=>{e.type==="Group"&&e.children.forEach(i=>{i.type==="ArrowHelper"&&(i.visible=t)})}),this.render()}getTextCanvas(t){const e=document.createElement("canvas"),i=e.getContext("2d");return i&&(i.font=`${50}px " bold`,i.fillStyle="white",i.textAlign="center",i.textBaseline="middle",i.fillText(t,e.width/2,e.height/2)),e}filterNoise(t){let e=[...t];e.sort((c,u)=>c.z-u.z);const i=Math.floor(e.length*.05),r=e[i];e=e.filter(({z:c})=>c>r.z+.1);const o=.005,h=Math.floor(e.length*(1-o));e=e.slice(0,h),e.sort((c,u)=>c.x-u.x);const s=Math.floor(e.length*o),d=Math.floor(e.length*(1-o));e=e.slice(s,d),e.sort((c,u)=>c.y-u.y);const n=Math.floor(e.length*o),l=Math.floor(e.length*(1-o));return e=e.slice(n,l),e.length>100?e:t}getFittedCoordinates(t,e){const i=[];let r=[...t,t[0]];e.forEach(({x:d,y:n})=>{t.forEach((l,c)=>{const u=r[c+1],m=A.getFootOfPerpendicular({x:d,y:n},l,u,!1,!0).length;(!i[c]||m<i[c].distance)&&(i[c]={distance:m,point:{x:d,y:n}})})}),r=[t[t.length-1],...t,t[0]];const o=[i[i.length-1],...i],h=t.map((d,n)=>{const l=r[n],c=r[n+1],u=r[n+2];return E.getIntersectionBySlope({p1:o[n].point,line1:[l,c],p2:o[n+1].point,line2:[c,u]})});return h.some(d=>!(Number.isFinite(d.x)&&Number.isFinite(d.y)))?t:h}getSensesPointZAxisInPolygon(t,e,i){var r,o,h;const s=this.scene.children.find(p=>p.uuid===this.pointsUuid);let d=0,n=0,l=0,c=0,u=[],m=[];const f=((h=(o=(r=s==null?void 0:s.geometry)==null?void 0:r.attributes)==null?void 0:o.position)==null?void 0:h.array)||[];for(let p=0;p<f.length;p+=3){const g=f[p],x=f[p+1],y=f[p+2];H({x:g,y:x},t)&&(y||y===0)&&m.push({x:g,y:x,z:y})}return m.length?(i&&(m=this.filterNoise(m),u=this.getFittedCoordinates(t,m)),m.sort((p,g)=>p.z-g.z),d=m[0].z-.01,n=m[m.length-1].z+.01,c=m.length,e&&(l=m.filter(({z:p})=>p>=e[0]&&p<=e[1]).length),{maxZ:n,minZ:d,count:l,zCount:c,fittedCoordinates:u}):{maxZ:n,minZ:d,count:l,zCount:c,fittedCoordinates:u}}getBasicCoordinate2Canvas(t){const e=this.containerWidth/2,i=this.containerHeight/2;return{x:t.x*e+e,y:t.y*i+i,z:t.z}}get basicCoordinate2CanvasMatrix4(){const t=this.containerWidth/2,e=this.containerHeight/2;return new a.Matrix4().set(t,0,0,t,0,e,0,e,0,0,1,0,0,0,0,1)}getCanvas2BasicCoordinate(t){const e=this.containerWidth/2,i=this.containerHeight/2;return new a.Vector3(t.x/e-e/2,-(t.y/i-i/2),1)}getPolygonSidePoints(t){const{center:{x:e,y:i,z:r},height:o,width:h,depth:s}=t,d={x:e+h/2,y:i+o/2,z:r-s/2},n={x:e+h/2,y:i+o/2,z:r+s/2},l={x:e-h/2,y:i+o/2,z:r+s/2},c={x:e-h/2,y:i+o/2,z:r-s/2};return[d,n,l,c]}getPolygonBackPoints(t){const{center:{x:e,y:i,z:r},height:o,width:h,depth:s}=t,d={x:e-h/2,y:i+o/2,z:r+s/2},n={x:e-h/2,y:i+o/2,z:r-s/2},l={x:e-h/2,y:i-o/2,z:r-s/2},c={x:e-h/2,y:i-o/2,z:r+s/2};return[d,n,l,c]}getPolygonTopPoints(t){const{center:{x:e,y:i,z:r},height:o,width:h,depth:s}=t,d={x:e+h/2,y:i+o/2,z:r+s/2},n={x:e+h/2,y:i-o/2,z:r+s/2},l={x:e-h/2,y:i-o/2,z:r+s/2},c={x:e-h/2,y:i+o/2,z:r+s/2};return[d,n,l,c]}getModelTransformationMatrix(t){const{center:{x:e,y:i,z:r},rotation:o}=t,h=new a.Matrix4().makeTranslation(-e,-i,-r),s=new a.Matrix4().makeTranslation(e,i,r),d=new a.Matrix4().makeRotationZ(o);return new a.Matrix4().multiply(s).multiply(d).multiply(h)}getBoxSidePolygon2DCoordinate(t){return this.getBoxPolygon2DCoordinate(t,C.Left)}getBoxBackPolygon2DCoordinate(t){return this.getBoxPolygon2DCoordinate(t,C.Back)}getSphereSidePoint2DCoordinate(t){return this.getSpherePoint2DCoordinate(t)}getSphereBackPoint2DCoordinate(t){return this.getSpherePoint2DCoordinate(t)}boxParams2ViewPolygon(t,e){switch(e){case C.Left:return this.getPolygonSidePoints(t);case C.Back:return this.getPolygonBackPoints(t);default:return this.getPolygonTopPoints(t)}}getBoxPolygon2DCoordinate(t,e){const i=this.boxParams2ViewPolygon(t,e),{width:r,height:o}=t,h=new a.Matrix4().premultiply(this.camera.matrixWorldInverse).premultiply(this.camera.projectionMatrix),s=new a.Matrix4().premultiply(this.getModelTransformationMatrix(t)).premultiply(h).premultiply(this.basicCoordinate2CanvasMatrix4);this.sideMatrix=s;const d=i.map(c=>new a.Vector3(c.x,c.y,c.z)).map(c=>c.applyMatrix4(this.sideMatrix)),n=this.containerWidth/r,l=this.containerHeight/o;return{polygon2d:d,zoom:Math.min(n,l)/2}}getSpherePoint2DCoordinate(t){const{center:e,attribute:i,id:r,valid:o}=t,{radius:h}=O,s={center:e,attribute:i,id:r,valid:o,width:h*2,height:h*2,depth:h*2,rotation:0},d=new a.Matrix4().premultiply(this.camera.matrixWorldInverse).premultiply(this.camera.projectionMatrix),n=new a.Matrix4().premultiply(this.getModelTransformationMatrix(s)).premultiply(d).premultiply(this.basicCoordinate2CanvasMatrix4);this.sideMatrix=n;const l=new a.Vector3(e.x,e.y,e.z).applyMatrix4(this.sideMatrix),c=this.containerWidth/(h*2),u=this.containerHeight/(h*2);return{point2d:l,zoom:Math.min(c,u)/2}}getSphereTopPoint2DCoordinate(t){const{center:e}=t,{radius:i}=O,r={x:-(e.y-this.containerWidth/2),y:-(e.x-this.containerHeight/2)},o=this.containerWidth/(i*2),h=this.containerHeight/(i*2);return{point2d:r,zoom:Math.min(o,h)/2}}getBoxTopPolygon2DCoordinate(t){const{width:e,height:i}=t,o=this.getPolygonTopPoints(t).map(d=>new a.Vector3(d.x,d.y,d.z)).map(d=>d.applyMatrix4(this.getModelTransformationMatrix(t))).map(d=>({x:d.y,y:d.x})).map(d=>({x:-(d.x-this.containerWidth/2),y:-(d.y-this.containerHeight/2)})),h=this.containerWidth/e,s=this.containerHeight/i;return{polygon2d:o,zoom:Math.min(h,s)/2}}getNewBoxBySideUpdate(t,e,i,r){const o=new a.Matrix4().makeRotationZ(r.rotation),h=new a.Vector3(-t.x,0,0).applyMatrix4(o);let s=r;return s.center={x:s.center.x+h.x,y:s.center.y+h.y,z:s.center.z-t.z},s=P(b({},s),{width:Math.abs(s.width+e),height:s.height,depth:Math.abs(s.depth+i)}),{newBoxParams:s}}getNewBoxByBackUpdate(t,e,i,r){const o=new a.Matrix4().makeRotationZ(r.rotation),h=new a.Vector3(0,-t.x,0).applyMatrix4(o);let s=r;return s.center={x:s.center.x+h.x,y:s.center.y+h.y,z:s.center.z-t.z},s=P(b({},s),{width:s.width,height:Math.abs(s.height+e),depth:Math.abs(s.depth+i)}),{newBoxParams:s}}getNewBoxBySideUpdateByPoints(t,e,i,r){var o;const h=(o=this.sideMatrix)==null?void 0:o.invert();if(!this.sideMatrix||!h){console.error("No sideMatrix");return}this.camera.zoom=1,this.camera.updateProjectionMatrix();const s=t.map(g=>new a.Vector3(g.x,g.y,g.z)).map(g=>g.applyMatrix4(h)),[d,n,l,c]=s,u=Math.max(Math.abs(d.x-l.x),Math.abs(d.x-n.x)),f=n.add(c).applyMatrix3(new a.Matrix3().set(1/2,0,0,0,1/2,0,0,0,1/2)).clone().applyMatrix3(new a.Matrix3().set(-1,0,0,0,-1,0,0,0,-1)).add(new a.Vector3(r.center.x,r.center.y,r.center.z));return{newBoxParams:P(b({},r),{center:{x:r.center.x-f.x,y:r.center.y-f.y,z:r.center.z-i},width:u,height:r.height,depth:r.depth+e,rotation:r.rotation})}}filterZAxisPoints(t){const e=t||this.scene.children.find(i=>i.uuid===this.pointsUuid);if(e){const{attributes:i}=e.geometry,{position:r}=i,o=[],{count:h}=r;for(let s=0;s<h;s++){const d=r.getZ(s);o.push(d>this.zAxisLimit?0:1)}e.geometry.setAttribute("visibility",new a.Float32BufferAttribute(o,1)),e.geometry.attributes.visibility.needsUpdate=!0}}render(){this.renderer.render(this.scene,this.camera)}}export{ot as PointCloud};
31
+ }`,uniforms:{pointSize:{value:this.pointsMaterialSize}}}),this.loadPCDFile=(...n)=>v(this,[...n],function*(l=this.currentPCDSrc,c){if(!l)return;this.clearPointCloud(),this.cacheInstance.clearCache2DHighlightIndex(),this.currentPCDSrc=l;const{points:u,color:p}=yield this.cacheInstance.loadPCDFile(l),x=new a.BufferGeometry;x.setAttribute("position",new a.BufferAttribute(u,3)),this.isSegment||x.setAttribute("dimensions",new a.BufferAttribute(p,3)),this.initCloudData(u);const g=new a.Points(x);this.renderPointCloud(g,c),this.emit("loadPCDFileEnd")}),this.getHighlightIndexByMappingImgList=n=>v(this,[n],function*({mappingImgList:l,points:c}){const u=m=>v(this,null,function*(){try{return yield W.load(m)}catch(f){return console.error("Error loading image:",f),null}}),p=l.length===0?[]:(yield Promise.all(l.map(m=>v(this,null,function*(){let f=yield u(m.url);return!f&&m.fallbackUrl&&(f=yield u(m.fallbackUrl)),f})))).filter(m=>m!==null),x=l.map((m,f)=>{var y;if(this.cacheInstance.cache2DHighlightIndex.has(m.url))return(y=this.cacheInstance.cache2DHighlightIndex.get(m.url))!=null?y:[];const w=$({points:c,calib:m.calib,width:p[f].width,height:p[f].height});return this.cacheInstance.cache2DHighlightIndex.set(m.url,w),w});return Y(x)}),this.filterPreResult=(n,l,c)=>v(this,null,function*(){const{points:u}=yield this.cacheInstance.loadPCDFile(n),p=yield this.cacheInstance.loadIndexMap(n,u);return new Promise(x=>{const g=c.map(m=>{const f=A.calculatePointsInsideBox({indexMap:p,polygon:I(m).polygonPointList,zScope:[m.center.z-m.depth/2,m.center.z+m.depth/2],box:m}),y=f>=l.lowerLimitPointsNumInBox;return B(P({},m),{valid:y,count:f})});x(g)})}),this.loadPCDFileByBox=(n,l,c)=>v(this,null,function*(){const u=(g,m)=>v(this,null,function*(){const{width:f=0,height:y=0,depth:w=0}=c!=null?c:{},M=yield this.filterPointsByBox(B(P({},l),{width:l.width+f,height:l.height+y,depth:l.depth+w}),g,m);if(!M){console.error("filter Error");return}this.clearPointCloud(),this.currentPCDSrc=n;const z=new a.Points(M.geometry);z.name=this.pointCloudObjectName,this.scene.add(z),this.render()}),{points:p,color:x}=yield this.cacheInstance.loadPCDFile(n);u(p,x)}),this.generateRange=n=>{const l=this.createRange(n);this.scene.add(l)},this.generateBoxArrow=({width:n})=>{const l=new a.Vector3(1,0,0),c=new a.Vector3(n/2,0,0),u=2,p=16776960,x=new a.ArrowHelper(l,c,u,p);return x.visible=this.showDirection,x},this.generateBoxTrackID=n=>{if(!n.trackID)return;const l=new a.Texture(this.getTextCanvas(n.trackID.toString()));l.needsUpdate=!0;const c=new a.SpriteMaterial({map:l,depthWrite:!1}),u=new a.Sprite(c);return u.scale.set(5,5,5),u.position.set(-n.width/2,0,n.depth/2+.5),u},this.applyZAxisPoints=n=>{this.zAxisLimit=n,this.filterZAxisPoints(),this.render()},this.updatePointSize=({zoomIn:n,customSize:l})=>{const c=this.scene.getObjectByName(this.pointCloudObjectName);if(!c)return;const u=c.material.uniforms.pointSize.value;n?c.material.uniforms.pointSize.value=Math.min(u*1.2,10):c.material.uniforms.pointSize.value=Math.max(u/1.2,1),l&&(c.material.uniforms.pointSize.value=l,this.pointsMaterialSize=l),c.material.uniformsNeedUpdate=!0,this.render()},this.container=t,this.renderer=new a.WebGLRenderer({antialias:!0}),this.backgroundColor=s,this.config=h,this.checkMode=d!=null?d:!1,i&&r?(this.isOrthographicCamera=!0,this.camera=new a.OrthographicCamera(r.left,r.right,r.top,r.bottom,r.near,r.far)):this.camera=new a.PerspectiveCamera(30,this.containerWidth/this.containerHeight,1,1e3),this.initCamera(),this.scene=new a.Scene,this.controls=new Z(this.camera,o?this.container:this.renderer.domElement),this.pcdLoader=new U,this.axesHelper=new a.AxesHelper(1e3),this.scene.add(this.camera),e||t.appendChild(this.renderer.domElement),this.init(),this.cacheInstance=G.getInstance(),o===!0&&(this.initSegment(),this.isSegment=!0)}initSegment(){this.store=new q(this.pointCloudDelegate),this.controls.enablePan=!1,this.controls.addEventListener("start",this.orbiterStart.bind(this)),this.controls.addEventListener("change",this.orbiterChange.bind(this)),this.controls.addEventListener("end",this.orbiterEnd.bind(this)),this.segmentOperation=new X(P({dom:this.container,store:this.store},this.eventBus)),this.pointCloudRender=new K(B(P({store:this.store},this.eventBus),{nextTick:this.nextTick})),this.initMsg(),document.addEventListener("keydown",this.keydown),document.addEventListener("keyup",this.keyup)}orbiterStart(){}orbiterChange(){!this.store||(this.store.orbiting=!0)}orbiterEnd(){!this.store||(this.store.orbiting=!1)}get currentSegmentTool(){var t;return(t=this.segmentOperation)==null?void 0:t.currentToolName}get pointCloudObject(){return this.scene.getObjectByName(this.pointCloudObjectName)}initMsg(){!this.segmentOperation||(this.on("CircleSelector",this.segmentOperation.updateSelector2Circle.bind(this.segmentOperation)),this.on("LassoSelector",this.segmentOperation.updateSelector2Lasso.bind(this.segmentOperation)),this.on("RectSelector",this.segmentOperation.updateSelector2Rect.bind(this.segmentOperation)),this.on("clearPointCloud",this.clearPointCloud.bind(this)),this.on("loadPCDFile",this.loadPCDFile.bind(this)))}unbindMsg(){!this.segmentOperation||(this.unbind("CircleSelector",this.segmentOperation.updateSelector2Circle.bind(this.segmentOperation)),this.unbind("LassoSelector",this.segmentOperation.updateSelector2Lasso.bind(this.segmentOperation)),this.unbind("RectSelector",this.segmentOperation.updateSelector2Rect.bind(this.segmentOperation)),this.unbind("clearPointCloud",this.clearPointCloud.bind(this)),this.unbind("loadPCDFile",this.loadPCDFile.bind(this)))}get eventBus(){return{on:this.on.bind(this),emit:this.emit.bind(this),unbind:this.unbind.bind(this)}}get pointCloudDelegate(){return P({container:this.container,scene:this.scene,camera:this.camera,renderer:this.renderer,checkMode:this.checkMode,config:this.config},this.eventBus)}get DEFAULT_INIT_CAMERA_POSITION(){return new a.Vector3(-.01,0,1e3)}get containerWidth(){return this.container.clientWidth}get containerHeight(){return this.container.clientHeight}setInitCameraPosition(t){this.initCameraPosition=t}setConfig(t){var e;this.config=t,(e=this.store)==null||e.setConfig(t)}initOrthographicCamera(t){if(this.camera.type!=="OrthographicCamera")return;const{left:e,right:i,top:r,bottom:s,near:h,far:o}=t;this.camera.left=e,this.camera.right=i,this.camera.top=r,this.camera.bottom=s,this.camera.near=h,this.camera.far=o,this.camera.updateProjectionMatrix()}initPerspectiveCamera(){this.camera.type==="PerspectiveCamera"&&(this.camera.fov=30,this.camera.aspect=this.containerWidth/this.containerHeight,this.camera.near=1,this.camera.far=1e3,this.camera.updateProjectionMatrix())}initCamera(){const{camera:t}=this;if(this.isOrthographicCamera){const{x:e,y:i,z:r}=this.initCameraPosition;t.position.set(e,i,r)}else t.position.set(-1,0,500);t.up.set(0,0,1)}initControls(){const{controls:t}=this;t.addEventListener("change",()=>{this.render()}),this.setDefaultControls()}setDefaultControls(){const{controls:t}=this,e=[0,0,0];t.target=new a.Vector3(...e),t.addEventListener("change",()=>{this.render()}),t.maxPolarAngle=Math.PI/2,t.update()}initRenderer(){const{renderer:t}=this;t.setPixelRatio(window.devicePixelRatio),t.setSize(this.containerWidth,this.containerHeight)}init(){const{scene:t}=this;t.background=new a.Color(this.backgroundColor),this.initControls(),this.initRenderer()}removeObjectByName(t,e=""){const i=this.scene.getObjectByName(e+t);i&&i.removeFromParent()}getColorFromConfig(t){return k.getColorFromConfig({attribute:t},B(P({},this.config),{attributeConfigurable:!0}),{})}generateBox(t,e=16777215){const i=e;this.addBoxToSense(t,i),this.render()}getAllAttributeColor(t){return t.reduce((e,i)=>(e[i.attribute]=this.getColorFromConfig(i.attribute),e),{})}generateBoxes(t){t.forEach(e=>{this.addBoxToSense(e)}),this.render()}getOrthographicCamera(t){const{center:e,width:i,height:r}=t,s=10,h=e.x-i/2-s,o=e.x-i/2+s,d=e.y+r/2+s,n=e.y-r/2-s,l=100,c=-100,u=500/l;return{left:h,right:o,top:d,bottom:n,near:l,far:c,zoom:u}}updateCameraZoom(t){this.camera.zoom=t,this.camera.updateProjectionMatrix()}updateCameraByBox(t,e,i){const{center:r,width:s,height:h,depth:o,rotation:d}=t,n=this.getCameraVector(r,d,{width:s,height:h,depth:o},e);return i?(this.updateCamera(i,r),new a.Vector3(i.x,i.y,i.z)):(this.updateCamera(n,r),n)}updateCameraBySphere(t,e){const{center:i}=t,{radius:r}=O,s=this.getCameraVector(i,0,{width:r*2,height:r*2,depth:r*2},e);return this.updateCamera(s,i),s}updateOrthoCamera(t,e){const i=this.updateCameraByBox(t,e);return this.camera.zoom=1,this.camera.updateProjectionMatrix(),{cameraPositionVector:i}}updateOrthoCameraBySphere(t,e){const i=this.updateCameraBySphere(t,e);return this.camera.zoom=1,this.camera.updateProjectionMatrix(),{cameraPositionVector:i}}updateTopCamera(){this.setInitCameraPosition(this.DEFAULT_INIT_CAMERA_POSITION),this.camera.zoom=1,this.initCamera(),this.setDefaultControls(),this.camera.updateProjectionMatrix(),this.render()}updateCamera(t,e){this.camera.position.set(t.x,t.y,t.z),this.controls.target=new a.Vector3(e.x,e.y,e.z),this.controls.update()}resetCamera(){this.updateCamera(this.DEFAULT_INIT_CAMERA_POSITION,{x:0,y:0,z:0})}getOrthographicCameraTarget(t){const e=new a.Vector3(0,0,-1).applyQuaternion(t.quaternion);return t.position.clone().add(e)}createThreeMatrix4(t){return new a.Matrix4().set(...t)}filterPointsByBox(t,e,i){var r,s,h;if(!e){const o=this.scene.getObjectByName(this.pointCloudObjectName);if(!o)return console.error("There is no corresponding point cloud object"),Promise.resolve(void 0);e=(h=(s=(r=o==null?void 0:o.geometry)==null?void 0:r.attributes)==null?void 0:s.position)==null?void 0:h.array}if(window.Worker){const{zMin:o,zMax:d,polygonPointList:n}=I(t),l=e;i=i!=null?i:new Float32Array([]);const c={boxParams:t,zMin:o,zMax:d,polygonPointList:n,color:i,position:l};return new Promise(u=>{const p=new R;p.postMessage(c),p.onmessage=x=>{const{color:g,position:m,num:f}=x.data,y=new a.BufferGeometry;y.setAttribute("position",new a.Float32BufferAttribute(m,3)),y.setAttribute("color",new a.Float32BufferAttribute(g,3)),y.computeBoundingSphere(),p.terminate(),u({geometry:y,num:f})}})}return Promise.resolve(void 0)}getCameraVector(t,e,i,r=b.Front,s=nt){let h=S.frontViewMatrix4(s);switch(r){case b.Front:break;case b.Back:h=S.backViewMatrix4(s);break;case b.Left:h=S.leftViewMatrix4(s);break;case b.Right:h=S.rightViewMatrix4(s);break;case b.Top:h=S.topViewMatrix4(s);break;case b.LFT:h=S.leftFrontTopViewMatrix4(s,i);break;case b.RBT:h=S.rightBackTopViewMatrix4(s,i);break}const o=this.createThreeMatrix4(h),d=new a.Matrix4().makeTranslation(-t.x,-t.y,-t.z),n=new a.Matrix4().makeTranslation(t.x,t.y,t.z),l=new a.Matrix4().makeRotationZ(e);return new a.Vector3(t.x,t.y,t.z).clone().applyMatrix4(o).applyMatrix4(d).applyMatrix4(l).applyMatrix4(n)}createRange(t){this.removeObjectByName(this.rangeObjectName);const i=new a.EllipseCurve(0,0,t,t,0,2*Math.PI,!1,0).getPoints(50),r=new a.BufferGeometry().setFromPoints(i),s=new a.LineBasicMaterial({color:16711680}),h=new a.Line(r,s);return h.name=this.rangeObjectName,h}renderPointCloud(t,e){t.name=this.pointCloudObjectName;const i=new a.ShaderMaterial(this.initShaderMaterial());e&&this.generateRange(e),this.pointsUuid=t.uuid,t.material=i,this.filterZAxisPoints(t),this.scene.add(t),this.render()}clearAllBox(){this.clearAllGroupByPrefix("box")}clearAllSphere(){this.clearAllGroupByPrefix("sphere")}clearAllGroupByPrefix(t=""){const e=this.scene.children;for(let i=e.length-1;i>=0;i--){const r=e[i];r.type==="Group"&&r.name.startsWith(t)&&this.removeObjectByName(r.name)}}clearPointCloud(){this.removeObjectByName(this.pointCloudObjectName)}clearPointCloudAndRender(){this.clearPointCloud(),this.render()}initCloudData(t){if(!!this.store){for(let e=0;e<t.length;e+=3){const i=t[e],r=t[e+1],s=t[e+2];this.store.cloudData.set(`${i}@${r}@${s}`,{visible:!1})}this.store.setOriginPoints(t)}}handleWebworker(t){return v(this,null,function*(){return new Promise((e,i)=>{if(this.workerLoading){i(new Error("highlightWorker called repeatedly, new call discarded"));return}this.workerLoading=!0,D.postMessage(t),D.onmessage=r=>{e(r.data),this.workerLoading=!1},D.onerror=r=>{i(r),this.workerLoading=!1}})})}highlightOriginPointCloud(t){return v(this,arguments,function*(e,i=[],r={modifiedBoxIds:[],resetAreas:[]}){const{modifiedBoxIds:s,resetAreas:h}=r,o=this.scene.getObjectByName(this.pointCloudObjectName);if(!!o)return this.highlightPCDSrc=this.currentPCDSrc,new Promise((d,n)=>{if(window.Worker){const c=(e?[...e]:[]).map(m=>I(m)),u=this.getAllAttributeColor(c),p=o.geometry.attributes.position.array,x=o.geometry.attributes.dimensions.array,g={cuboidList:c,position:p,color:x,colorList:u,highlightIndex:i,modifiedBoxIds:s,resetAreas:h};this.handleWebworker(g).then(m=>{const{color:f}=m;let y=f;(s.length||h.length)&&(y=f.map((M,z)=>M===-1?x[z]:M));const w=new a.BufferAttribute(y,3);if(!this.highlightPCDSrc||this.highlightPCDSrc!==this.currentPCDSrc||o.geometry.attributes.position.array.length!==f.length){n(new Error("Error Path"));return}this.cacheInstance.updateColor(this.highlightPCDSrc,y),this.highlightPCDSrc=void 0,w.needsUpdate=!0,o.geometry.setAttribute("dimensions",w),o.geometry.attributes.dimensions.needsUpdate=!0,d(y),this.render()}).catch(m=>{n(m)})}})})}clearHighlightBoxes(){this.removeObjectByName(this.highlightGroupName)}clearHighlightBoxesAndRender(){this.clearHighlightBoxes(),this.render()}highlightBoxes(t){const e=new a.Group;t.forEach(i=>{const{center:{x:r,y:s,z:h},width:o,height:d,depth:n,rotation:l}=i,{fill:c}=k.getColorFromConfig({attribute:i.attribute},B(P({},this.config),{attributeConfigurable:!0}),{}),u=new a.BoxGeometry(o,d,n),p=new a.MeshBasicMaterial({color:c,transparent:!0,opacity:.2,depthTest:!1});u.rotateZ(l),u.translate(r,s,h);const x=new a.Mesh(u,p);e.add(x);const g=new a.PlaneGeometry(n,d);g.rotateY(Math.PI/2),g.rotateZ(l);const m=new a.Vector3(o/2,0,0),f=new a.Matrix4().makeRotationY(Math.PI/2).makeRotationZ(l);m.applyMatrix4(f),g.translate(r+m.x,s+m.y,h+m.z);const y=new a.MeshBasicMaterial({color:c,side:a.DoubleSide,transparent:!0,opacity:.8,depthTest:!1}),w=new a.Mesh(g,y);e.add(w)}),e.name=this.highlightGroupName,this.scene.add(e),this.render()}updateColor(t){const e=this.scene.getObjectByName(this.pointCloudObjectName);if(e){const i=new a.BufferAttribute(t,3);e.geometry.setAttribute("dimensions",i),e.geometry.attributes.dimensions.needsUpdate=!0,this.render()}}setShowDirection(t){this.showDirection=t,this.scene.children.forEach(e=>{e.type==="Group"&&e.children.forEach(i=>{i.type==="ArrowHelper"&&(i.visible=t)})}),this.render()}getTextCanvas(t){const e=document.createElement("canvas"),i=e.getContext("2d");return i&&(i.font=`${50}px " bold`,i.fillStyle="white",i.textAlign="center",i.textBaseline="middle",i.fillText(t,e.width/2,e.height/2)),e}filterNoise(t){let e=[...t];e.sort((c,u)=>c.z-u.z);const i=Math.floor(e.length*.05),r=e[i];e=e.filter(({z:c})=>c>r.z+.1);const s=.005,h=Math.floor(e.length*(1-s));e=e.slice(0,h),e.sort((c,u)=>c.x-u.x);const o=Math.floor(e.length*s),d=Math.floor(e.length*(1-s));e=e.slice(o,d),e.sort((c,u)=>c.y-u.y);const n=Math.floor(e.length*s),l=Math.floor(e.length*(1-s));return e=e.slice(n,l),e.length>100?e:t}getFittedCoordinates(t,e){const i=[];let r=[...t,t[0]];e.forEach(({x:d,y:n})=>{t.forEach((l,c)=>{const u=r[c+1],p=A.getFootOfPerpendicular({x:d,y:n},l,u,!1,!0).length;(!i[c]||p<i[c].distance)&&(i[c]={distance:p,point:{x:d,y:n}})})}),r=[t[t.length-1],...t,t[0]];const s=[i[i.length-1],...i],h=t.map((d,n)=>{const l=r[n],c=r[n+1],u=r[n+2];return E.getIntersectionBySlope({p1:s[n].point,line1:[l,c],p2:s[n+1].point,line2:[c,u]})});return h.some(d=>!(Number.isFinite(d.x)&&Number.isFinite(d.y)))?t:h}getSensesPointZAxisInPolygon(t,e,i){var r,s,h;const o=this.scene.children.find(g=>g.uuid===this.pointsUuid);let d=0,n=0,l=0,c=0,u=[],p=[];const x=((h=(s=(r=o==null?void 0:o.geometry)==null?void 0:r.attributes)==null?void 0:s.position)==null?void 0:h.array)||[];for(let g=0;g<x.length;g+=3){const m=x[g],f=x[g+1],y=x[g+2];H({x:m,y:f},t)&&(y||y===0)&&p.push({x:m,y:f,z:y})}return p.length?(i&&(p=this.filterNoise(p),u=this.getFittedCoordinates(t,p)),p.sort((g,m)=>g.z-m.z),d=p[0].z-.01,n=p[p.length-1].z+.01,c=p.length,e&&(l=p.filter(({z:g})=>g>=e[0]&&g<=e[1]).length),{maxZ:n,minZ:d,count:l,zCount:c,fittedCoordinates:u}):{maxZ:n,minZ:d,count:l,zCount:c,fittedCoordinates:u}}getBasicCoordinate2Canvas(t){const e=this.containerWidth/2,i=this.containerHeight/2;return{x:t.x*e+e,y:t.y*i+i,z:t.z}}get basicCoordinate2CanvasMatrix4(){const t=this.containerWidth/2,e=this.containerHeight/2;return new a.Matrix4().set(t,0,0,t,0,e,0,e,0,0,1,0,0,0,0,1)}getCanvas2BasicCoordinate(t){const e=this.containerWidth/2,i=this.containerHeight/2;return new a.Vector3(t.x/e-e/2,-(t.y/i-i/2),1)}getPolygonSidePoints(t){const{center:{x:e,y:i,z:r},height:s,width:h,depth:o}=t,d={x:e+h/2,y:i+s/2,z:r-o/2},n={x:e+h/2,y:i+s/2,z:r+o/2},l={x:e-h/2,y:i+s/2,z:r+o/2},c={x:e-h/2,y:i+s/2,z:r-o/2};return[d,n,l,c]}getPolygonBackPoints(t){const{center:{x:e,y:i,z:r},height:s,width:h,depth:o}=t,d={x:e-h/2,y:i+s/2,z:r+o/2},n={x:e-h/2,y:i+s/2,z:r-o/2},l={x:e-h/2,y:i-s/2,z:r-o/2},c={x:e-h/2,y:i-s/2,z:r+o/2};return[d,n,l,c]}getPolygonTopPoints(t){const{center:{x:e,y:i,z:r},height:s,width:h,depth:o}=t,d={x:e+h/2,y:i+s/2,z:r+o/2},n={x:e+h/2,y:i-s/2,z:r+o/2},l={x:e-h/2,y:i-s/2,z:r+o/2},c={x:e-h/2,y:i+s/2,z:r+o/2};return[d,n,l,c]}getModelTransformationMatrix(t){const{center:{x:e,y:i,z:r},rotation:s}=t,h=new a.Matrix4().makeTranslation(-e,-i,-r),o=new a.Matrix4().makeTranslation(e,i,r),d=new a.Matrix4().makeRotationZ(s);return new a.Matrix4().multiply(o).multiply(d).multiply(h)}getBoxSidePolygon2DCoordinate(t){return this.getBoxPolygon2DCoordinate(t,b.Left)}getBoxBackPolygon2DCoordinate(t){return this.getBoxPolygon2DCoordinate(t,b.Back)}getSphereSidePoint2DCoordinate(t){return this.getSpherePoint2DCoordinate(t)}getSphereBackPoint2DCoordinate(t){return this.getSpherePoint2DCoordinate(t)}boxParams2ViewPolygon(t,e){switch(e){case b.Left:return this.getPolygonSidePoints(t);case b.Back:return this.getPolygonBackPoints(t);default:return this.getPolygonTopPoints(t)}}getBoxPolygon2DCoordinate(t,e){const i=this.boxParams2ViewPolygon(t,e),{width:r,height:s}=t,h=new a.Matrix4().premultiply(this.camera.matrixWorldInverse).premultiply(this.camera.projectionMatrix),o=new a.Matrix4().premultiply(this.getModelTransformationMatrix(t)).premultiply(h).premultiply(this.basicCoordinate2CanvasMatrix4);this.sideMatrix=o;const d=i.map(c=>new a.Vector3(c.x,c.y,c.z)).map(c=>c.applyMatrix4(this.sideMatrix)),n=this.containerWidth/r,l=this.containerHeight/s;return{polygon2d:d,zoom:Math.min(n,l)/2}}getSpherePoint2DCoordinate(t){const{center:e,attribute:i,id:r,valid:s}=t,{radius:h}=O,o={center:e,attribute:i,id:r,valid:s,width:h*2,height:h*2,depth:h*2,rotation:0},d=new a.Matrix4().premultiply(this.camera.matrixWorldInverse).premultiply(this.camera.projectionMatrix),n=new a.Matrix4().premultiply(this.getModelTransformationMatrix(o)).premultiply(d).premultiply(this.basicCoordinate2CanvasMatrix4);this.sideMatrix=n;const l=new a.Vector3(e.x,e.y,e.z).applyMatrix4(this.sideMatrix),c=this.containerWidth/(h*2),u=this.containerHeight/(h*2);return{point2d:l,zoom:Math.min(c,u)/2}}getSphereTopPoint2DCoordinate(t){const{center:e}=t,{radius:i}=O,r={x:-(e.y-this.containerWidth/2),y:-(e.x-this.containerHeight/2)},s=this.containerWidth/(i*2),h=this.containerHeight/(i*2);return{point2d:r,zoom:Math.min(s,h)/2}}getBoxTopPolygon2DCoordinate(t){const{width:e,height:i}=t,s=this.getPolygonTopPoints(t).map(d=>new a.Vector3(d.x,d.y,d.z)).map(d=>d.applyMatrix4(this.getModelTransformationMatrix(t))).map(d=>({x:d.y,y:d.x})).map(d=>({x:-(d.x-this.containerWidth/2),y:-(d.y-this.containerHeight/2)})),h=this.containerWidth/e,o=this.containerHeight/i;return{polygon2d:s,zoom:Math.min(h,o)/2}}getNewBoxBySideUpdate(t,e,i,r){const s=new a.Matrix4().makeRotationZ(r.rotation),h=new a.Vector3(-t.x,0,0).applyMatrix4(s);let o=r;return o.center={x:o.center.x+h.x,y:o.center.y+h.y,z:o.center.z-t.z},o=B(P({},o),{width:Math.abs(o.width+e),height:o.height,depth:Math.abs(o.depth+i)}),{newBoxParams:o}}getNewBoxByBackUpdate(t,e,i,r){const s=new a.Matrix4().makeRotationZ(r.rotation),h=new a.Vector3(0,-t.x,0).applyMatrix4(s);let o=r;return o.center={x:o.center.x+h.x,y:o.center.y+h.y,z:o.center.z-t.z},o=B(P({},o),{width:o.width,height:Math.abs(o.height+e),depth:Math.abs(o.depth+i)}),{newBoxParams:o}}getNewBoxBySideUpdateByPoints(t,e,i,r){var s;const h=(s=this.sideMatrix)==null?void 0:s.invert();if(!this.sideMatrix||!h){console.error("No sideMatrix");return}this.camera.zoom=1,this.camera.updateProjectionMatrix();const o=t.map(m=>new a.Vector3(m.x,m.y,m.z)).map(m=>m.applyMatrix4(h)),[d,n,l,c]=o,u=Math.max(Math.abs(d.x-l.x),Math.abs(d.x-n.x)),x=n.add(c).applyMatrix3(new a.Matrix3().set(1/2,0,0,0,1/2,0,0,0,1/2)).clone().applyMatrix3(new a.Matrix3().set(-1,0,0,0,-1,0,0,0,-1)).add(new a.Vector3(r.center.x,r.center.y,r.center.z));return{newBoxParams:B(P({},r),{center:{x:r.center.x-x.x,y:r.center.y-x.y,z:r.center.z-i},width:u,height:r.height,depth:r.depth+e,rotation:r.rotation})}}filterZAxisPoints(t){const e=t||this.scene.children.find(i=>i.uuid===this.pointsUuid);if(e){const{attributes:i}=e.geometry,{position:r}=i,s=[],{count:h}=r;for(let o=0;o<h;o++){const d=r.getZ(o);s.push(d>this.zAxisLimit?0:1)}e.geometry.setAttribute("visibility",new a.Float32BufferAttribute(s,1)),e.geometry.attributes.visibility.needsUpdate=!0}}render(){this.renderer.render(this.scene,this.camera)}}export{ot as PointCloud};
@@ -1 +1 @@
1
- import*as p from"three";import{MatrixUtils as M,ECameraType as _,PointCloudUtils as K}from"@labelbee/lb-utils";import A from"../../utils/uuid.js";import{transformPointCloudToImage as k}from"./omniCamera.js";var B=Object.defineProperty,N=Object.defineProperties,U=Object.getOwnPropertyDescriptors,I=Object.getOwnPropertySymbols,Z=Object.prototype.hasOwnProperty,q=Object.prototype.propertyIsEnumerable,T=Math.pow,L=(r,t,e)=>t in r?B(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,V=(r,t)=>{for(var e in t||(t={}))Z.call(t,e)&&L(r,e,t[e]);if(I)for(var e of I(t))q.call(t,e)&&L(r,e,t[e]);return r},F=(r,t)=>N(r,U(t));function g(r){return new p.Matrix4().set(...r)}function v(r,t,e){try{const n=M.transferMatrix34FromKitti2Three(r),s=M.transferMatrix33FromKitti2Three(t),o=M.transferMatrix34FromKitti2Three(e),c=g(n),f=g(s),a=g(o);return{composeMatrix4:a.clone().premultiply(f).premultiply(c),PM:c,RM:f,TM:a}}catch(n){console.error(n)}}function C(r,t,e){var n;const s=new p.Vector3(r.x,r.y,(n=r==null?void 0:r.z)!=null?n:1),o=new p.Matrix4().makeRotationZ(e),c=new p.Matrix4().makeTranslation(t.x,t.y,t.z),f=new p.Matrix4().makeTranslation(-t.x,-t.y,-t.z);return s.clone().applyMatrix4(f).applyMatrix4(o).applyMatrix4(c)}function O(r,t,e){return!(r.length!==t||r.some(n=>n.length!==e))}const z=(r,t)=>t.some(e=>O(r,e[0],e[1])),m=r=>{var t;return!!(((t=r.fisheyeDistortion)==null?void 0:t.length)>0&&z(r.P,[[3,4],[4,4]])&&z(r.T,[[3,4],[4,4]]))},R=(r,t)=>{if(m(t)===!1){console.error("Error Calib, it need fisheye calib");return}const{P:e,fisheyeDistortion:n,T:s}=t,o=new p.Vector4(r.x,r.y,r.z),c=g(M.transferMatrix34FromKitti2Three(s)),f=g(M.transferMatrix34FromKitti2Three(e)),a=o.applyMatrix4(c),h=T(T(a.x,2)+T(a.y,2),.5),y=Math.atan(a.z/h),i=n.reduce((l,u,d)=>l+u*T(y,d),0);return a.x=a.x/h*i,a.y=a.y/h*i,a.z=1,a.applyMatrix4(f)},E=(r,t)=>{if(m(t)===!1){console.error("Error Calib, it need fisheye calib");return}const{P:e,fisheyeDistortion:n,T:s}=t,o=k([[r.x,r.y,r.z]],s,e,n);return new p.Vector4(o[0][0],o[0][1],1)},G=(r,t)=>{const e=r.length;let n=0;const s=[];for(let o=0;o<r.length;o++)s.push(r[e-o-1]+n*t),n=r[e-o-1]+n*t;return n},J=(r,t)=>{const{P:e,fisheyeDistortion:n}=t,{x:s,y:o,z:c}=r,f=[[e[0][0],e[0][1]],[e[1][0],e[1][1]]],a=e[0][2],h=e[1][2],y=1/Math.hypot(s,o),i=[],x=Math.atan2(Math.hypot(s,o),c),l=G(n,x);return i[0]=s*y*l,i[1]=o*y*l,{x:f[0][0]*i[0]+f[0][1]*i[1]+a,y:f[1][0]*i[0]+f[1][1]*i[1]+h}},P=(r,t)=>{if(m(t)===!1){console.error("Error Calib, it need fisheye calib");return}if((t==null?void 0:t.cameraType)===_.OmniCamera)return R(r,t);if((t==null?void 0:t.cameraType)===_.KannalaBrandt)return J(r,t);if((t==null?void 0:t.cameraType)===_.OmniCamera11V)return E(r,t)};function w(r,t){const n=new p.Vector4(r.x,r.y,r.z).applyMatrix4(t);if(n.z<0)return;const s=1/n.z,o=new p.Matrix4().set(s,0,0,0,0,s,0,0,0,0,s,0,0,0,0,1);return n.applyMatrix4(o)}function Q(r){const{center:t,width:e,height:n,depth:s,rotation:o}=r,c=[{x:t.x+e/2,y:t.y-n/2},{x:t.x+e/2,y:t.y+n/2},{x:t.x-e/2,y:t.y+n/2},{x:t.x-e/2,y:t.y-n/2}].map(h=>{const y=C(h,t,o);return{x:y.x,y:y.y}}),f=t.z+s/2,a=t.z-s/2;return F(V({},r),{polygonPointList:c,zMax:f,zMin:a})}function W(r){const t=r.slice();return t.sort((e,n)=>e.x===n.x?n.y-e.y:e.x-n.x),t}function D(r,t,e){const n=t.x-r.x,s=t.y-r.y,o=e.x-r.x,c=e.y-r.y;return n*c-o*s}function X(r){const t=W(r),e=[];for(let o=0;o<t.length;o++){for(;e.length>=2&&D(e[e.length-2],e[e.length-1],t[o])<=0;)e.pop();e.push(t[o])}const n=[];for(let o=t.length-1;o>=0;o--){for(;n.length>=2&&D(n[n.length-2],n[n.length-1],t[o])<=0;)n.pop();n.push(t[o])}return e.pop(),n.pop(),e.concat(n)}const H=(r,t)=>{var e;if(m(t))return P(r,t);const{P:n,R:s,T:o}=t,{composeMatrix4:c}=(e=v(n,s,o))!=null?e:{};if(!!c)return w(r,c)},j=({point:r,calib:t,width:e,height:n})=>{if(!t)return!1;const s=H(r,t);return s?s.x>=0&&s.x<=e&&s.y>=0&&s.y<=n:!1},Y=({points:r,calib:t,width:e,height:n})=>{const s=[];for(let o=0;o<r.length;o+=3){const c=r[o],f=r[o+1],a=r[o+2];j({point:{x:c,y:f,z:a},calib:t,width:e,height:n})?s.push(1):s.push(0)}return s},$=r=>{if(r.length===0)return[];const t=[];for(let e=0;e<r[0].length;e++){for(let n=0;n<r.length;n++)if(r[n][e]===1){t.push(1);break}t.length===e&&t.push(0)}return t};function S(r,t,e={createRange:!1}){var n;if(!t)return{transferViewData:[],viewRangePointList:[]};const{createRange:s}=e,o=K.getAllViewData(r),c=m(t),{P:f,R:a,T:h}=t;let y;if(c===!1){const{composeMatrix4:l}=(n=v(f,a,h))!=null?n:{};if(!l)return;y=l}const i=o.map(l=>({type:l.type,pointList:l.pointList.map(u=>C(u,r.center,r.rotation)).map(u=>c?P(u,t):y&&w(u,y)).map(u=>{if(!!u)return{id:A(),x:u==null?void 0:u.x,y:u==null?void 0:u.y}}).filter(u=>u!==void 0)})).filter(l=>l.pointList.length!==0);i[0]&&i[0].pointList&&c&&(i[0].pointList=i[0].pointList.map(l=>F(V({},l),{specialEdge:!0})));let x=[];if(i.length===6&&s===!0){const l=i[0].pointList,u=i[1].pointList;x=X([...l,...u])}return{transferViewData:i,viewRangePointList:x}}function b(r,t,e){var n;const s=m(t),{P:o,R:c,T:f}=t;let a;if(s===!1){const{composeMatrix4:i}=(n=v(o,c,f))!=null?n:{};if(!i)return;a=i}const h=r.length/3,y={};for(let i=0;i<h;i++){const x={x:r[i*3],y:r[i*3+1],z:r[i*3+2]};let l;if(s?l=P(x,t):l=a&&w({x:r[i*3],y:r[i*3+1],z:r[i*3+2]},a),l){const u=Math.floor(l.x),d=Math.floor(l.y);if(u>e.width||d>e.height||u<0||d<0)continue;y[i]={x:u,y:d}}}return{pcdMapping:y}}function tt(r,t,e){var n;if(!t||!e)return;const s=m(t),{P:o,R:c,T:f}=t;let a;if(s===!1){const{composeMatrix4:i}=(n=v(o,c,f))!=null?n:{};if(!i)return;a=i}const h=[];if(r.forEach(i=>{let x;if(s?x=P(i,t):x=a&&w(i,a),x){const{x:l,y:u}=x;h.push({x:l,y:u})}}),h.some(i=>i.x>0&&i.x<e.width&&i.y>0&&i.y<e.height))return h}export{g as createThreeMatrix4,Q as getCuboidFromPointCloudBox,Y as getHighlightIndexByPoints,m as isFisheyeCalibValid,j as isInImage,O as isMatrixValid,z as isMatrixValidByArr,P as lidar2FisheyeImage,w as lidar2image,$ as mergeHighlightList,R as oCamFisheyeTransfer,E as omniCamera11VTransfer,H as point3DLidar2Image,S as pointCloudLidar2image,tt as pointListLidar2Img,b as pointMappingLidar2image,C as rotatePoint,v as transferKitti2Matrix};
1
+ import*as p from"three";import{MatrixUtils as M,ECameraType as V,PointCloudUtils as G}from"@labelbee/lb-utils";import J from"../../utils/uuid.js";import{transformPointCloudToImage as Q}from"./omniCamera.js";var W=Object.defineProperty,X=Object.defineProperties,Y=Object.getOwnPropertyDescriptors,R=Object.getOwnPropertySymbols,$=Object.prototype.hasOwnProperty,S=Object.prototype.propertyIsEnumerable,C=Math.pow,D=(r,t,e)=>t in r?W(r,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):r[t]=e,E=(r,t)=>{for(var e in t||(t={}))$.call(t,e)&&D(r,e,t[e]);if(R)for(var e of R(t))S.call(t,e)&&D(r,e,t[e]);return r},A=(r,t)=>X(r,Y(t));function g(r){return new p.Matrix4().set(...r)}function v(r,t,e){try{const n=M.transferMatrix34FromKitti2Three(r),s=M.transferMatrix33FromKitti2Three(t),o=M.transferMatrix34FromKitti2Three(e),c=g(n),f=g(s),a=g(o);return{composeMatrix4:a.clone().premultiply(f).premultiply(c),PM:c,RM:f,TM:a}}catch(n){console.error(n)}}function F(r,t,e){var n;const s=new p.Vector3(r.x,r.y,(n=r==null?void 0:r.z)!=null?n:1),o=new p.Matrix4().makeRotationZ(e),c=new p.Matrix4().makeTranslation(t.x,t.y,t.z),f=new p.Matrix4().makeTranslation(-t.x,-t.y,-t.z);return s.clone().applyMatrix4(f).applyMatrix4(o).applyMatrix4(c)}function H(r,t,e){return!(r.length!==t||r.some(n=>n.length!==e))}const O=(r,t)=>t.some(e=>H(r,e[0],e[1])),d=r=>{var t;return!!(((t=r.fisheyeDistortion)==null?void 0:t.length)>0&&O(r.P,[[3,4],[4,4]])&&O(r.T,[[3,4],[4,4]]))},j=(r,t)=>{if(d(t)===!1){console.error("Error Calib, it need fisheye calib");return}const{P:e,fisheyeDistortion:n,T:s}=t,o=new p.Vector4(r.x,r.y,r.z),c=g(M.transferMatrix34FromKitti2Three(s)),f=g(M.transferMatrix34FromKitti2Three(e)),a=o.applyMatrix4(c),y=C(C(a.x,2)+C(a.y,2),.5),h=Math.atan(a.z/y),i=n.reduce((l,u,m)=>l+u*C(h,m),0);return a.x=a.x/y*i,a.y=a.y/y*i,a.z=1,a.applyMatrix4(f)},K=(r,t)=>{if(d(t)===!1){console.error("Error Calib, it need fisheye calib");return}const{P:e,fisheyeDistortion:n,T:s}=t,o=Q([[r.x,r.y,r.z]],s,e,n);return new p.Vector4(o[0][0],o[0][1],1)},b=(r,t)=>{const{P:e,T:n,fisheyeDistortion:s}=t,o=[r.x,r.y,r.z,1],c=n.map(z=>z.reduce((I,L,Z)=>I+L*o[Z],0)),f=c[0],a=c[1],y=c[2];if(!(y>=.01&&isFinite(y)))return;const h=[[e[0][0],e[0][1]],[e[1][0],e[1][1]]],i=e[0][2],x=e[1][2],l=Math.sqrt(f*f+a*a),u=Math.atan2(l,y),m=u+s.reduce((z,I,L)=>z+I*Math.pow(u,2*L+3),0);let P,_;l!==0?(P=f*m/l,_=a*m/l):(P=0,_=0);const q=h[0][0]*P+h[0][1]*_,N=h[1][0]*P+h[1][1]*_;return{x:q+i,y:N+x}},T=(r,t)=>{if(d(t)===!1){console.error("Error Calib, it need fisheye calib");return}if((t==null?void 0:t.cameraType)===V.OmniCamera)return j(r,t);if((t==null?void 0:t.cameraType)===V.KannalaBrandt)return b(r,t);if((t==null?void 0:t.cameraType)===V.OmniCamera11V)return K(r,t)};function w(r,t){const n=new p.Vector4(r.x,r.y,r.z).applyMatrix4(t);if(n.z<0)return;const s=1/n.z,o=new p.Matrix4().set(s,0,0,0,0,s,0,0,0,0,s,0,0,0,0,1);return n.applyMatrix4(o)}function tt(r){const{center:t,width:e,height:n,depth:s,rotation:o}=r,c=[{x:t.x+e/2,y:t.y-n/2},{x:t.x+e/2,y:t.y+n/2},{x:t.x-e/2,y:t.y+n/2},{x:t.x-e/2,y:t.y-n/2}].map(y=>{const h=F(y,t,o);return{x:h.x,y:h.y}}),f=t.z+s/2,a=t.z-s/2;return A(E({},r),{polygonPointList:c,zMax:f,zMin:a})}function rt(r){const t=r.slice();return t.sort((e,n)=>e.x===n.x?n.y-e.y:e.x-n.x),t}function k(r,t,e){const n=t.x-r.x,s=t.y-r.y,o=e.x-r.x,c=e.y-r.y;return n*c-o*s}function et(r){const t=rt(r),e=[];for(let o=0;o<t.length;o++){for(;e.length>=2&&k(e[e.length-2],e[e.length-1],t[o])<=0;)e.pop();e.push(t[o])}const n=[];for(let o=t.length-1;o>=0;o--){for(;n.length>=2&&k(n[n.length-2],n[n.length-1],t[o])<=0;)n.pop();n.push(t[o])}return e.pop(),n.pop(),e.concat(n)}const B=(r,t)=>{var e;if(d(t))return T(r,t);const{P:n,R:s,T:o}=t,{composeMatrix4:c}=(e=v(n,s,o))!=null?e:{};if(!!c)return w(r,c)},U=({point:r,calib:t,width:e,height:n})=>{if(!t)return!1;const s=B(r,t);return s?s.x>=0&&s.x<=e&&s.y>=0&&s.y<=n:!1},nt=({points:r,calib:t,width:e,height:n})=>{const s=[];for(let o=0;o<r.length;o+=3){const c=r[o],f=r[o+1],a=r[o+2];U({point:{x:c,y:f,z:a},calib:t,width:e,height:n})?s.push(1):s.push(0)}return s},ot=r=>{if(r.length===0)return[];const t=[];for(let e=0;e<r[0].length;e++){for(let n=0;n<r.length;n++)if(r[n][e]===1){t.push(1);break}t.length===e&&t.push(0)}return t};function st(r,t,e={createRange:!1}){var n;if(!t)return{transferViewData:[],viewRangePointList:[]};const{createRange:s}=e,o=G.getAllViewData(r),c=d(t),{P:f,R:a,T:y}=t;let h;if(c===!1){const{composeMatrix4:l}=(n=v(f,a,y))!=null?n:{};if(!l)return;h=l}const i=o.map(l=>({type:l.type,pointList:l.pointList.map(u=>F(u,r.center,r.rotation)).map(u=>c?T(u,t):h&&w(u,h)).map(u=>{if(!!u)return{id:J(),x:u==null?void 0:u.x,y:u==null?void 0:u.y}}).filter(u=>u!==void 0)})).filter(l=>l.pointList.length!==0);i[0]&&i[0].pointList&&(i[0].pointList=i[0].pointList.map(l=>A(E({},l),{specialEdge:!0})));let x=[];if(i.length===6&&s===!0){const l=i[0].pointList,u=i[1].pointList;x=et([...l,...u])}return{transferViewData:i,viewRangePointList:x}}function it(r,t,e){var n;const s=d(t),{P:o,R:c,T:f}=t;let a;if(s===!1){const{composeMatrix4:i}=(n=v(o,c,f))!=null?n:{};if(!i)return;a=i}const y=r.length/3,h={};for(let i=0;i<y;i++){const x={x:r[i*3],y:r[i*3+1],z:r[i*3+2]};let l;if(s?l=T(x,t):l=a&&w({x:r[i*3],y:r[i*3+1],z:r[i*3+2]},a),l){const u=Math.floor(l.x),m=Math.floor(l.y);if(u>e.width||m>e.height||u<0||m<0)continue;h[i]={x:u,y:m}}}return{pcdMapping:h}}function at(r,t,e){var n;if(!t||!e)return;const s=d(t),{P:o,R:c,T:f}=t;let a;if(s===!1){const{composeMatrix4:i}=(n=v(o,c,f))!=null?n:{};if(!i)return;a=i}const y=[];if(r.forEach(i=>{let x;if(s?x=T(i,t):x=a&&w(i,a),x){const{x:l,y:u}=x;y.push({x:l,y:u})}}),y.some(i=>i.x>0&&i.x<e.width&&i.y>0&&i.y<e.height))return y}export{g as createThreeMatrix4,tt as getCuboidFromPointCloudBox,nt as getHighlightIndexByPoints,d as isFisheyeCalibValid,U as isInImage,H as isMatrixValid,O as isMatrixValidByArr,T as lidar2FisheyeImage,w as lidar2image,ot as mergeHighlightList,j as oCamFisheyeTransfer,K as omniCamera11VTransfer,B as point3DLidar2Image,st as pointCloudLidar2image,at as pointListLidar2Img,it as pointMappingLidar2image,F as rotatePoint,v as transferKitti2Matrix};
@@ -1 +1 @@
1
- const t=require("mathjs");function P(n){if(n.length!==1||n[0].length!==3)throw new Error("Input matrix must be a 1x3 or 3x1 matrix");return[[n[0][0]],[n[0][1]],[n[0][2]],[1]]}function b(n){const o=[];for(let s=0;s<n.length;s++){const e=n[s],r=e[0]*e[0]+e[1]*e[1],i=Math.sqrt(r);o.push(i)}return o}function v(n,o){const s=n.length,e=[];for(let r=0;r<o.length;r++){let i=0;for(let l=0;l<n.length;l++)i=n[s-l-1]+i*o[r];e.push(i)}return e}function w(n,o,s){const e=t.matrix([[o[0][0],o[0][1]],[o[1][0],o[1][1]]]),r=o[0][2],i=o[1][2],l=s,u=b(n.toArray().map(z=>z.slice(0,2))),a=t.dotDivide(t.ones(t.matrix(u).size()),u),h=t.atan2(t.multiply(-1,n.subset(t.index(t.range(0,t.matrix(n).size()[0]),2))),u),c=v(l,h);let m=t.ones(2,n.size()[0]);const x=t.multiply(n.subset(t.index(t.range(0,n.size()[0]),0)),a*c),g=t.multiply(n.subset(t.index(t.range(0,n.size()[0]),1)),a*c),f=t.matrix([[x,0],[g,0]]);m=t.subset(f,t.index(t.range(0,f.size()[0]),0));const y=t.matrix([r,i]),d=t.multiply(e,m),p=y.reshape([2,1]);return t.transpose(t.add(d,p)).toArray()}function q(n=[],o,s,e){const r=t.matrix(P(n)),i=t.matrix(o),l=t.multiply(i,r);return w(t.transpose(l),s,e)}export{q as transformPointCloudToImage};
1
+ import*as t from"mathjs";function P(n){if(n.length!==1||n[0].length!==3)throw new Error("Input matrix must be a 1x3 or 3x1 matrix");return[[n[0][0]],[n[0][1]],[n[0][2]],[1]]}function b(n){const o=[];for(let e=0;e<n.length;e++){const s=n[e],r=s[0]*s[0]+s[1]*s[1],i=Math.sqrt(r);o.push(i)}return o}function v(n,o){const e=n.length,s=[];for(let r=0;r<o.length;r++){let i=0;for(let l=0;l<n.length;l++)i=n[e-l-1]+i*o[r];s.push(i)}return s}function w(n,o,e){const s=t.matrix([[o[0][0],o[0][1]],[o[1][0],o[1][1]]]),r=o[0][2],i=o[1][2],l=e,a=b(n.toArray().map(z=>z.slice(0,2))),u=t.dotDivide(t.ones(t.matrix(a).size()),a),h=t.atan2(t.multiply(-1,n.subset(t.index(t.range(0,t.matrix(n).size()[0]),2))),a),c=v(l,h);let m=t.ones(2,n.size()[0]);const x=t.multiply(n.subset(t.index(t.range(0,n.size()[0]),0)),u*c),g=t.multiply(n.subset(t.index(t.range(0,n.size()[0]),1)),u*c),f=t.matrix([[x,0],[g,0]]);m=t.subset(f,t.index(t.range(0,f.size()[0]),0));const y=t.matrix([r,i]),p=t.multiply(s,m),d=y.reshape([2,1]);return t.transpose(t.add(p,d)).toArray()}function C(n=[],o,e,s){const r=t.matrix(P(n)),i=t.matrix(o),l=t.multiply(i,r);return w(t.transpose(l),e,s)}export{C as transformPointCloudToImage};
@@ -1 +1,2 @@
1
- import{ImgPosUtils as H}from"@labelbee/lb-utils";import E from"lodash";import M from"color-rgba";import d from"../../utils/tool/DrawUtils.js";import b from"../../utils/tool/AxisUtils.js";import W from"../../utils/tool/RectUtils.js";import z from"../../utils/tool/PolygonUtils.js";import k from"../../utils/MathUtils.js";import j from"../../utils/tool/RenderDomClass.js";import{ELineTypes as R,DEFAULT_FONT as V,SEGMENT_NUMBER as U}from"../../constant/tool.js";import{DEFAULT_TEXT_OFFSET as B,TEXT_ATTRIBUTE_OFFSET as O,DEFAULT_TEXT_SHADOW as G}from"../../constant/annotation.js";import K,{cropAndEnlarge as X}from"../../utils/ImgUtils.js";import q from"../../utils/tool/CanvasUtils.js";import{BasicToolOperation as Y}from"./basicToolOperation.js";import{pointCloudLidar2image as J}from"../pointCloud/matrix.js";var Q=Object.defineProperty,tt=Object.defineProperties,et=Object.getOwnPropertyDescriptors,$=Object.getOwnPropertySymbols,ot=Object.prototype.hasOwnProperty,it=Object.prototype.propertyIsEnumerable,F=(v,t,e)=>t in v?Q(v,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):v[t]=e,m=(v,t)=>{for(var e in t||(t={}))ot.call(t,e)&&F(v,e,t[e]);if($)for(var e of $(t))it.call(t,e)&&F(v,e,t[e]);return v},T=(v,t)=>tt(v,et(t)),nt=(v,t,e)=>new Promise((o,i)=>{var r=a=>{try{s(e.next(a))}catch(n){i(n)}},c=a=>{try{s(e.throw(a))}catch(n){i(n)}},s=a=>a.done?o(a.value):Promise.resolve(a.value).then(r,c);s((e=e.apply(v,t)).next())});const st=3,rt=3,L="#6371FF";class at extends Y{constructor(t){super(T(m({},t),{showDefaultCursor:!0}));this.style={},this.annotations=[],this.connectionPoints=[],this.getHoverRectID=o=>{var i,r;const c=this.getCoordinateUnderZoom(o),s=b.changePointByZoom(c,1/this.zoom);if(((i=this.annotations)==null?void 0:i.length)<=0||!((r=this.annotations)==null?void 0:r.length))return;let a="",n=Number.MAX_SAFE_INTEGER;for(let h=0;h<this.annotations.length;h++){const l=this.annotations[h];switch(l.type){case"rect":{const u=l.annotation;if(W.isInRect(c,u,st,this.zoom)){const g=u.width*u.height;g<n&&(a=u.id,n=g)}break}case"polygon":{const u=l.annotation;if(z.isInPolygon(s,u.pointList)){const g=z.getPolygonArea(u.pointList);g<n&&(a=u.id,n=g)}break}}}return a};var e;this.style=(e=t.style)!=null?e:{stroke:L,thickness:3},this.annotations=t.annotations,this.loading=!1,this.renderDomInstance=new j({container:this.container,height:this.canvas.height})}clearConnectionPoints(){this.connectionPoints=[],this.render()}checkConnectionPoints(t=this.annotations){var e,o;this.connectPointsStatus&&((o=(e=this.connectPointsStatus).close)==null||o.call(e)),this.emit("connectionPointsStatusUpdate",()=>new Promise(i=>{const{promise:r,close:c}=k.getCollectionPointByAnnotationDataPromise(t);this.connectPointsStatus={close:c},r.then(s=>{this.connectionPoints=s.connectionPoints,this.render(),this.connectPointsStatus=void 0,i({connectionPoints:s.connectionPoints})})}))}setLoading(t){this.loading=t,this.render()}onMouseLeave(){super.onMouseLeave(),this.mouseHoverID=void 0,this.emit("onChange","hover",[])}onMouseDown(t){if(super.onMouseDown(t)||this.forbidMouseOperation||!this.imgInfo)return!0;const e=this.mouseHoverID;if(t.button===0){let o=[];e&&(o=[e]),this.emit("onChange","selected",o),this.render()}}setImgNode(t,e={}){super.setImgNode(t,e),this.staticMode&&this.generateStaticImgNode()}generateStaticImgNode(){var t,e;const o=X(this.canvas,(t=this.basicImgInfo)==null?void 0:t.width,(e=this.basicImgInfo)==null?void 0:e.height,1);K.load(o).then(i=>{this.staticImgNode=i,this.drawStaticImg()})}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;const e=this.mouseHoverID,o=this.getHoverRectID(t);if(e!==o){this.mouseHoverID=o;let i=[];o&&(i=[o]),this.emit("onChange","hover",i),this.render()}}updateData(t){return nt(this,null,function*(){if(!E.isEqual(this.annotations,t)&&(this.annotations=t,this.staticMode&&(this.staticImgNode=void 0),this.render(),this.staticMode)){const e=this.zoom,o=this.currentPos;this.initImgPos(),this.generateStaticImgNode();const i=this.staticImgNode;this.staticImgNode=void 0,this.updatePosition({zoom:e,currentPos:o}),this.staticImgNode=i}})}getSpecificStyle(t){const e=E.pick(t,["stroke","thickness","fill","radius"]),o=m(m({},this.style),e);return o.stroke&&Object.assign(o,{color:o.stroke}),o}getFontStyle(t,e){var o,i;const r=(o=t==null?void 0:t.fontSize)!=null?o:14,c=(i=t==null?void 0:t.fontFamily)!=null?i:"Arial";return T(m({},G),{color:e.stroke,font:`normal normal 600 ${r}px ${c}`})}appendOffset({x:t,y:e}){return{x:t+B.offsetX,y:e+B.offsetY}}getRenderText(t,e=!1){let o="",i="";return!t||e===!0?{headerText:o,bottomText:i}:((t==null?void 0:t.order)&&(o=`${t.order}`),(t==null?void 0:t.label)&&(o?o=`${o}_${t.label}`:o=`${t.label}`),(t==null?void 0:t.attribute)&&(o?o=`${o} ${t.attribute}`:o=`${t.attribute}`),(t==null?void 0:t.textAttribute)&&(i=t==null?void 0:t.textAttribute),{headerText:o,bottomText:i})}getReferenceOptions(t){return t?{lineCap:"butt",lineDash:[20,20]}:{}}focusPositionByPointList(t){const e=k.calcViewportBoundaries(t),o={x:e.left,y:e.top,width:e.right-e.left,height:e.bottom-e.top},i=H.getBasicRecPos(this.imgNode,o,this.size,.5);if(i){this.setCurrentPos(i.currentPos),this.setCurrentPosStorage(i.currentPos);const{imgInfo:r}=this,{innerZoom:c}=this.innerPosAndZoom;r&&this.setImgInfo(T(m({},r),{width:r.width/c*i.innerZoom,height:r.height/c*i.innerZoom})),this.setZoom(i.innerZoom),this.render(),this.renderBasicCanvas()}}renderConnectionPoints(){this.connectionPoints.forEach(t=>{const e=b.changePointByZoom(t,this.zoom,this.currentPos);d.drawCircleWithFill(this.canvas,e,4,{color:"#fff"}),d.drawCircleWithFill(this.canvas,e,2,{color:"#000"})})}getRenderStyle(t){const e=this.getSpecificStyle(t.annotation),o=this.getFontStyle(t.annotation,e);return{style:e,fontStyle:o}}renderLine(t){var e,o,i;if(t.type!=="line")return;const{style:r,fontStyle:c}=this.getRenderStyle(t),s=t.annotation;if(!(((e=s==null?void 0:s.pointList)==null?void 0:e.length)>=2))return;const{lineType:a=R.Line}=s,n=b.changePointListByZoom((o=s==null?void 0:s.pointList)!=null?o:[],this.zoom,this.currentPos),h=T(m(m({},r),this.getReferenceOptions(s==null?void 0:s.isReference)),{lineType:a,strokeColor:r.stroke});let l=[];if(s.showKeyPoint?l=d.drawPolygonWithKeyPoint(this.canvas,n,h):l=d.drawPolygon(this.canvas,n,h),(s==null?void 0:s.showDirection)===!0&&((i=s==null?void 0:s.pointList)==null?void 0:i.length)>=2){let p=n[0],P=k.getLineCenterPoint([n[0],n[1]]);if(a===R.Curve){const f=Math.floor(U/2);p=l[f],P=l[f+1]}d.drawArrowByCanvas(this.canvas,p,P,{color:r.stroke,thickness:r.thickness}),d.drawCircle(this.canvas,n[0],r.thickness+6,{color:r.stroke,thickness:r.thickness})}const{headerText:g,bottomText:y}=this.getRenderText(s,s==null?void 0:s.hiddenText);if(g&&d.drawText(this.canvas,this.appendOffset(n[0]),g,c),y){const p=n[n.length-1];d.drawText(this.canvas,this.appendOffset({x:p.x+O.x,y:p.y+O.y}),y,c)}}renderPolygon(t){var e,o,i,r,c;if(t.type!=="polygon")return;const{style:s,fontStyle:a}=this.getRenderStyle(t),n=t.annotation;if(!(((e=n==null?void 0:n.pointList)==null?void 0:e.length)>=3))return;const{lineType:h=R.Line}=n,l=b.changePointListByZoom((o=n==null?void 0:n.pointList)!=null?o:[],this.zoom,this.currentPos);if(n.id===this.mouseHoverID||s.fill){const f=M((r=(i=s==null?void 0:s.fill)!=null?i:s==null?void 0:s.stroke)!=null?r:L),x=`rgba(${f[0]}, ${f[1]}, ${f[2]},${f[3]*.8})`;d.drawPolygonWithFill(this.canvas,l,{color:x,lineType:h})}const u=T(m(T(m({},s),{isClose:!0}),this.getReferenceOptions(n==null?void 0:n.isReference)),{lineType:h,strokeColor:s.stroke});let g=[];if(n.showKeyPoint?g=d.drawPolygonWithKeyPoint(this.canvas,l,u):g=d.drawPolygon(this.canvas,l,u),(n==null?void 0:n.showDirection)===!0&&((c=n==null?void 0:n.pointList)==null?void 0:c.length)>=2){let f=l[0],x=k.getLineCenterPoint([l[0],l[1]]);if(h===R.Curve){const w=Math.floor(U/2);f=g[w],x=g[w+1]}d.drawArrowByCanvas(this.canvas,f,x,{color:s.stroke,thickness:s.thickness}),d.drawCircle(this.canvas,l[0],s.thickness+6,{color:s.stroke,thickness:s.thickness})}const{headerText:p,bottomText:P}=this.getRenderText(n,n==null?void 0:n.hiddenText);if(p&&d.drawText(this.canvas,this.appendOffset(l[0]),p,a),P){const f=l[l.length-1];d.drawText(this.canvas,this.appendOffset({x:f.x+O.x,y:f.y+O.y}),P,a)}}renderSingleCuboid(t){var e,o;const{style:i}=this.getRenderStyle(t),r=t.annotation,c=M((o=(e=i==null?void 0:i.fill)!=null?e:i==null?void 0:i.stroke)!=null?o:L),s=`rgba(${c[0]}, ${c[1]}, ${c[2]},${c[3]*.8})`,a=i.stroke,n=b.changeCuboidByZoom(r,this.zoom,this.currentPos),{headerText:h,bottomText:l}=this.getRenderText(r,r==null?void 0:r.hiddenText);d.drawCuboidWithText(this.canvas,n,{strokeColor:a,fillColor:s,thickness:i.thickness},{config:this.config,hiddenText:r==null?void 0:r.hiddenText,headerText:h,bottomText:l})}renderBox3d(t){var e;if(t.type!=="box3d")return;const o=t.annotation,{transferViewData:i}=(e=J(o,o.calib))!=null?e:{};if(!i)return;const r={fill:"transparent"},c=E.pick(o,["stroke","thickness"]);i.forEach((s,a)=>{const n=m(T(m({},c),{id:`${t.annotation.id}-${a}`,pointList:s.pointList}),r);switch(s.type){case"line":this.renderLine({type:"line",annotation:n});break;case"polygon":this.renderPolygon({type:"polygon",annotation:n});break}})}renderPixelPoints(t){var e;if(t.type!=="pixelPoints")return;const o=t.annotation;if(!this.imgNode){console.error("Need to load after imgLoaded");return}if(!(o.length>0)){console.warn("Empty pixelPoints");return}const i=this.imgNode.src+o.length+t.defaultRGBA,r=(e=this.cacheCanvas)==null?void 0:e[i];if(r){d.drawImg(this.canvas,r,{zoom:this.zoom,currentPos:this.currentPos});return}const c={width:this.imgNode.width,height:this.imgNode.height},{ctx:s,canvas:a}=q.createCanvas(c),n=typeof t.pixelSize=="number"?t.pixelSize:13;s&&(o==null?void 0:o.length)>0&&(d.drawPixel({canvas:a,points:o,size:c,defaultRGBA:t.defaultRGBA,pixelSize:n}),d.drawImg(this.canvas,a,{zoom:this.zoom,currentPos:this.currentPos}),this.cacheCanvas={[i]:a})}render(){try{if(this.staticImgNode||(super.render(),this.loading===!0))return;const t=this.annotations.filter(e=>e.type==="text"&&e.annotation.position==="rt").map(e=>e.annotation);this.renderDomInstance.render(t),this.annotations.forEach(e=>{var o,i,r,c,s;const a=this.getSpecificStyle(e.annotation),n=this.getFontStyle(e.annotation,a);switch(e.type){case"rect":{const h=e.annotation,{hiddenText:l=!1,isReference:u,hiddenRectSize:g=!1}=h,{zoom:y}=this,p=b.changeRectByZoom(h,this.zoom,this.currentPos),{x:P,y:f,width:x,height:w}=p,_=M((i=(o=a==null?void 0:a.fill)!=null?o:a==null?void 0:a.stroke)!=null?i:L),I=`rgba(${_[0]}, ${_[1]}, ${_[2]},${_[3]*.8})`;(h.id===this.mouseHoverID||a.fill)&&d.drawRectWithFill(this.canvas,p,{color:I}),d.drawRect(this.canvas,p,m(T(m({},a),{hiddenText:!0}),this.getReferenceOptions(u))),(h==null?void 0:h.isHighlight)&&d.drawHighlightFlag({canvas:this.canvas,color:I,position:{x:P-16,y:f-16}});const{headerText:S,bottomText:A}=this.getRenderText(h,h==null?void 0:h.hiddenText);S&&d.drawText(this.canvas,{x:P,y:f-6},S,m({textMaxWidth:300},n));const C=`${Math.round(x/y)} * ${Math.round(w/y)}`,D=C.length*7;if(!l&&!g&&d.drawText(this.canvas,{x:P+x-D,y:f+w+15},C,n),A){const N=20,Z=Math.max(20,x-D);d.drawText(this.canvas,{x:P,y:f+w+N},h.textAttribute,m({textMaxWidth:Z},n))}break}case"polygon":{this.renderPolygon(e);break}case"line":{this.renderLine(e);break}case"point":{const h=e.annotation,l=b.changePointByZoom(h,this.zoom,this.currentPos),u=(r=a.radius)!=null?r:rt;d.drawCircle(this.canvas,l,u,a);const{headerText:g,bottomText:y}=this.getRenderText(h,h==null?void 0:h.hiddenText);g&&d.drawText(this.canvas,{x:l.x+u/2,y:l.y-u-4},g,m({textAlign:"center"},n)),y&&d.drawText(this.canvas,this.appendOffset({x:l.x+u,y:l.y+u+24}),y,n);break}case"text":{const h=e.annotation,{text:l,x:u,y:g,textMaxWidth:y,color:p="white",background:P="rgba(0, 0, 0, 0.6)",lineHeight:f=25,font:x=V,position:w}=h,_=10,I=10,S=b.changePointByZoom({x:u,y:g},this.zoom,this.currentPos),{width:A,height:C,fontHeight:D=0}=k.getTextArea(this.canvas,h.text,y,x,f);if(w==="rt")break;d.drawRectWithFill(this.canvas,{x:S.x,y:S.y,width:A+I*2,height:C+_*2,id:"",sourceID:"",valid:!0,textAttribute:"",attribute:""},{color:P}),d.drawText(this.canvas,{x:S.x+I,y:S.y+D+_},l,{color:p,lineHeight:f,font:x,textMaxWidth:y});break}case"box3d":{this.renderBox3d(e);break}case"cuboid":{this.renderSingleCuboid(e);break}case"pixelPoints":{this.renderPixelPoints(e);break}default:break}"renderEnhance"in e.annotation&&((s=(c=e.annotation).renderEnhance)==null||s.call(c,{ctx:this.ctx,canvas:this.canvas,currentPos:this.currentPos,zoom:this.zoom,data:e,toolInstance:this}))}),this.renderConnectionPoints()}catch(t){console.error("ViewOperation Render Error",t)}}}export{at as default};
1
+ import{ImgPosUtils as W}from"@labelbee/lb-utils";import U from"lodash";import $ from"color-rgba";import h from"../../utils/tool/DrawUtils.js";import _ from"../../utils/tool/AxisUtils.js";import V from"../../utils/tool/RectUtils.js";import B from"../../utils/tool/PolygonUtils.js";import R from"../../utils/MathUtils.js";import G from"../../utils/tool/RenderDomClass.js";import{ELineTypes as O,DEFAULT_FONT as K,SEGMENT_NUMBER as N}from"../../constant/tool.js";import{DEFAULT_TEXT_OFFSET as F,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_SHADOW as X}from"../../constant/annotation.js";import q,{cropAndEnlarge as Y}from"../../utils/ImgUtils.js";import J from"../../utils/tool/CanvasUtils.js";import Q from"../../utils/tool/TagUtils.js";import{BasicToolOperation as tt}from"./basicToolOperation.js";import{pointCloudLidar2image as et}from"../pointCloud/matrix.js";var ot=Object.defineProperty,it=Object.defineProperties,nt=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,st=Object.prototype.hasOwnProperty,rt=Object.prototype.propertyIsEnumerable,j=(P,t,e)=>t in P?ot(P,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):P[t]=e,m=(P,t)=>{for(var e in t||(t={}))st.call(t,e)&&j(P,e,t[e]);if(Z)for(var e of Z(t))rt.call(t,e)&&j(P,e,t[e]);return P},S=(P,t)=>it(P,nt(t)),at=(P,t,e)=>new Promise((o,i)=>{var r=l=>{try{n(e.next(l))}catch(s){i(s)}},a=l=>{try{n(e.throw(l))}catch(s){i(s)}},n=l=>l.done?o(l.value):Promise.resolve(l.value).then(r,a);n((e=e.apply(P,t)).next())});const lt=3,ct=3,M="#6371FF";class ht extends tt{constructor(t){super(S(m({},t),{showDefaultCursor:!0}));this.style={},this.annotations=[],this.connectionPoints=[],this.getHoverRectID=o=>{var i,r;const a=this.getCoordinateUnderZoom(o),n=_.changePointByZoom(a,1/this.zoom);if(((i=this.annotations)==null?void 0:i.length)<=0||!((r=this.annotations)==null?void 0:r.length))return;let l="",s=Number.MAX_SAFE_INTEGER;for(let f=0;f<this.annotations.length;f++){const d=this.annotations[f];switch(d.type){case"rect":{const c=d.annotation;if(V.isInRect(a,c,lt,this.zoom)){const u=c.width*c.height;u<s&&(l=c.id,s=u)}break}case"polygon":{const c=d.annotation;if(B.isInPolygon(n,c.pointList)){const u=B.getPolygonArea(c.pointList);u<s&&(l=c.id,s=u)}break}}}return l};var e;this.style=(e=t.style)!=null?e:{stroke:M,thickness:3},this.annotations=t.annotations,this.loading=!1,this.renderDomInstance=new G({container:this.container,height:this.canvas.height})}clearConnectionPoints(){this.connectionPoints=[],this.render()}checkConnectionPoints(t=this.annotations){var e,o;this.connectPointsStatus&&((o=(e=this.connectPointsStatus).close)==null||o.call(e)),this.emit("connectionPointsStatusUpdate",()=>new Promise(i=>{const{promise:r,close:a}=R.getCollectionPointByAnnotationDataPromise(t);this.connectPointsStatus={close:a},r.then(n=>{this.connectionPoints=n.connectionPoints,this.render(),this.connectPointsStatus=void 0,i({connectionPoints:n.connectionPoints})})}))}setLoading(t){this.loading=t,this.render()}onMouseLeave(){super.onMouseLeave(),this.mouseHoverID=void 0,this.emit("onChange","hover",[])}onMouseDown(t){if(super.onMouseDown(t)||this.forbidMouseOperation||!this.imgInfo)return!0;const e=this.mouseHoverID;if(t.button===0){let o=[];e&&(o=[e]),this.emit("onChange","selected",o),this.render()}}setImgNode(t,e={}){super.setImgNode(t,e),this.staticMode&&this.generateStaticImgNode()}generateStaticImgNode(){var t,e;const o=Y(this.canvas,(t=this.basicImgInfo)==null?void 0:t.width,(e=this.basicImgInfo)==null?void 0:e.height,1);q.load(o).then(i=>{this.staticImgNode=i,this.drawStaticImg()})}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;const e=this.mouseHoverID,o=this.getHoverRectID(t);if(e!==o){this.mouseHoverID=o;let i=[];o&&(i=[o]),this.emit("onChange","hover",i),this.render()}}updateData(t){return at(this,null,function*(){if(!U.isEqual(this.annotations,t)&&(this.annotations=t,this.staticMode&&(this.staticImgNode=void 0),this.render(),this.staticMode)){const e=this.zoom,o=this.currentPos;this.initImgPos(),this.generateStaticImgNode();const i=this.staticImgNode;this.staticImgNode=void 0,this.updatePosition({zoom:e,currentPos:o}),this.staticImgNode=i}})}setConfig(t){this.config=t}getSpecificStyle(t){const e=U.pick(t,["stroke","thickness","fill","radius"]),o=m(m({},this.style),e);return o.stroke&&Object.assign(o,{color:o.stroke}),o}getFontStyle(t,e){var o,i;const r=(o=t==null?void 0:t.fontSize)!=null?o:14,a=(i=t==null?void 0:t.fontFamily)!=null?i:"Arial";return S(m({},X),{color:e.stroke,font:`normal normal 600 ${r}px ${a}`})}appendOffset({x:t,y:e}){return{x:t+F.offsetX,y:e+F.offsetY}}getRenderText(t,e=!1){let o="",i="";if(!t||e===!0)return{headerText:o,bottomText:i};(t==null?void 0:t.order)&&(o=`${t.order}`),(t==null?void 0:t.label)&&(o?o=`${o}_${t.label}`:o=`${t.label}`),(t==null?void 0:t.attribute)&&(o?o=`${o} ${t.attribute}`:o=`${t.attribute}`),(t==null?void 0:t.textAttribute)&&(i=t==null?void 0:t.textAttribute);const{secondaryAttributeConfigurable:r,subAttributeList:a}=this.config;return(t==null?void 0:t.subAttribute)&&r&&a&&Q.getTagNameList(t==null?void 0:t.subAttribute,a).forEach(l=>{o+=`
2
+ ${l.keyName}: ${l.value.join("\u3001")}`}),{headerText:o,bottomText:i}}getReferenceOptions(t){return t?{lineCap:"butt",lineDash:[20,20]}:{}}focusPositionByPointList(t){const e=R.calcViewportBoundaries(t),o={x:e.left,y:e.top,width:e.right-e.left,height:e.bottom-e.top},i=W.getBasicRecPos(this.imgNode,o,this.size,.5);if(i){this.setCurrentPos(i.currentPos),this.setCurrentPosStorage(i.currentPos);const{imgInfo:r}=this,{innerZoom:a}=this.innerPosAndZoom;r&&this.setImgInfo(S(m({},r),{width:r.width/a*i.innerZoom,height:r.height/a*i.innerZoom})),this.setZoom(i.innerZoom),this.render(),this.renderBasicCanvas()}}renderConnectionPoints(){this.connectionPoints.forEach(t=>{const e=_.changePointByZoom(t,this.zoom,this.currentPos);h.drawCircleWithFill(this.canvas,e,4,{color:"#fff"}),h.drawCircleWithFill(this.canvas,e,2,{color:"#000"})})}getRenderStyle(t){const e=this.getSpecificStyle(t.annotation),o=this.getFontStyle(t.annotation,e);return{style:e,fontStyle:o}}renderLine(t){var e,o,i;if(t.type!=="line")return;const{style:r,fontStyle:a}=this.getRenderStyle(t),n=t.annotation;if(!(((e=n==null?void 0:n.pointList)==null?void 0:e.length)>=2))return;const{lineType:l=O.Line}=n,s=_.changePointListByZoom((o=n==null?void 0:n.pointList)!=null?o:[],this.zoom,this.currentPos),f=S(m(m({},r),this.getReferenceOptions(n==null?void 0:n.isReference)),{lineType:l,strokeColor:r.stroke});let d=[];if(n.showKeyPoint?d=h.drawPolygonWithKeyPoint(this.canvas,s,f):d=h.drawPolygon(this.canvas,s,f),(n==null?void 0:n.showDirection)===!0&&((i=n==null?void 0:n.pointList)==null?void 0:i.length)>=2){let p=s[0],v=R.getLineCenterPoint([s[0],s[1]]);if(l===O.Curve){const g=Math.floor(N/2);p=d[g],v=d[g+1]}h.drawArrowByCanvas(this.canvas,p,v,{color:r.stroke,thickness:r.thickness}),h.drawCircle(this.canvas,s[0],r.thickness+6,{color:r.stroke,thickness:r.thickness})}const{headerText:u,bottomText:y}=this.getRenderText(n,n==null?void 0:n.hiddenText);if(u&&h.drawText(this.canvas,this.appendOffset(s[0]),u,a),y){const p=s[s.length-1];h.drawText(this.canvas,this.appendOffset({x:p.x+E.x,y:p.y+E.y}),y,a)}}renderPolygon(t){var e,o,i,r,a;if(t.type!=="polygon")return;const{style:n,fontStyle:l}=this.getRenderStyle(t),s=t.annotation;if(!(((e=s==null?void 0:s.pointList)==null?void 0:e.length)>=3))return;const{lineType:f=O.Line}=s,d=_.changePointListByZoom((o=s==null?void 0:s.pointList)!=null?o:[],this.zoom,this.currentPos);if(s.id===this.mouseHoverID||n.fill){const g=$((r=(i=n==null?void 0:n.fill)!=null?i:n==null?void 0:n.stroke)!=null?r:M),x=`rgba(${g[0]}, ${g[1]}, ${g[2]},${g[3]*.8})`;h.drawPolygonWithFill(this.canvas,d,{color:x,lineType:f})}const c=S(m(S(m({},n),{isClose:!0}),this.getReferenceOptions(s==null?void 0:s.isReference)),{lineType:f,strokeColor:n.stroke});let u=[];if(s.showKeyPoint?u=h.drawPolygonWithKeyPoint(this.canvas,d,c):u=h.drawPolygon(this.canvas,d,c),(s==null?void 0:s.showDirection)===!0&&((a=s==null?void 0:s.pointList)==null?void 0:a.length)>=2){let g=d[0],x=R.getLineCenterPoint([d[0],d[1]]);if(f===O.Curve){const w=Math.floor(N/2);g=u[w],x=u[w+1]}h.drawArrowByCanvas(this.canvas,g,x,{color:n.stroke,thickness:n.thickness}),h.drawCircle(this.canvas,d[0],n.thickness+6,{color:n.stroke,thickness:n.thickness})}const{headerText:p,bottomText:v}=this.getRenderText(s,s==null?void 0:s.hiddenText);if(p&&h.drawText(this.canvas,this.appendOffset(d[0]),p,l),v){const g=d[d.length-2];h.drawText(this.canvas,this.appendOffset({x:g.x+E.x,y:g.y+E.y}),v,l)}}renderSingleCuboid(t){var e,o;const{style:i}=this.getRenderStyle(t),r=t.annotation,a=$((o=(e=i==null?void 0:i.fill)!=null?e:i==null?void 0:i.stroke)!=null?o:M),n=`rgba(${a[0]}, ${a[1]}, ${a[2]},${a[3]*.8})`,l=i.stroke,s=_.changeCuboidByZoom(r,this.zoom,this.currentPos),{headerText:f,bottomText:d}=this.getRenderText(r,r==null?void 0:r.hiddenText);h.drawCuboidWithText(this.canvas,s,{strokeColor:l,fillColor:n,thickness:i.thickness},{config:this.config,hiddenText:r==null?void 0:r.hiddenText,headerText:f,bottomText:d})}renderBox3d(t){var e;if(t.type!=="box3d")return;const o=t.annotation,{transferViewData:i}=(e=et(o,o.calib))!=null?e:{};if(!i)return;const r={fill:"transparent"},a=U.pick(o,["stroke","thickness"]);i.forEach((n,l)=>{const s=m(S(m({},a),{id:`${t.annotation.id}-${l}`,pointList:n.pointList}),r);switch(n.type){case"line":this.renderLine({type:"line",annotation:s});break;case"polygon":this.renderPolygon({type:"polygon",annotation:s});break}})}renderPixelPoints(t){var e;if(t.type!=="pixelPoints")return;const o=t.annotation;if(!this.imgNode){console.error("Need to load after imgLoaded");return}if(!(o.length>0)){console.warn("Empty pixelPoints");return}const i=this.imgNode.src+o.length+t.defaultRGBA,r=(e=this.cacheCanvas)==null?void 0:e[i];if(r){h.drawImg(this.canvas,r,{zoom:this.zoom,currentPos:this.currentPos});return}const a={width:this.imgNode.width,height:this.imgNode.height},{ctx:n,canvas:l}=J.createCanvas(a),s=typeof t.pixelSize=="number"?t.pixelSize:13;n&&(o==null?void 0:o.length)>0&&(h.drawPixel({canvas:l,points:o,size:a,defaultRGBA:t.defaultRGBA,pixelSize:s}),h.drawImg(this.canvas,l,{zoom:this.zoom,currentPos:this.currentPos}),this.cacheCanvas={[i]:l})}render(){try{if(this.staticImgNode||(super.render(),this.loading===!0))return;const t=this.annotations.filter(e=>e.type==="text"&&e.annotation.position==="rt").map(e=>e.annotation);this.renderDomInstance.render(t),this.annotations.forEach(e=>{var o,i,r,a,n,l,s;const f=this.getSpecificStyle(e.annotation),d=this.getFontStyle(e.annotation,f);switch(e.type){case"rect":{const c=e.annotation,{hiddenText:u=!1,isReference:y,hiddenRectSize:p=!1}=c,{zoom:v}=this,g=_.changeRectByZoom(c,this.zoom,this.currentPos),{x,y:w,width:k,height:C}=g,b=$((i=(o=f==null?void 0:f.fill)!=null?o:f==null?void 0:f.stroke)!=null?i:M),I=`rgba(${b[0]}, ${b[1]}, ${b[2]},${b[3]*.8})`;(c.id===this.mouseHoverID||f.fill)&&h.drawRectWithFill(this.canvas,g,{color:I}),h.drawRect(this.canvas,g,m(S(m({},f),{hiddenText:!0}),this.getReferenceOptions(y))),(c==null?void 0:c.isHighlight)&&h.drawHighlightFlag({canvas:this.canvas,color:I,position:{x:x-16,y:w-16}});const{headerText:D,bottomText:T}=this.getRenderText(c,c==null?void 0:c.hiddenText);D&&h.drawText(this.canvas,{x,y:w-6},D,m({textMaxWidth:300},d));const A=`${Math.round(k/v)} * ${Math.round(C/v)}`,L=A.length*7;if(!u&&!p&&h.drawText(this.canvas,{x:x+k-L,y:w+C+15},A,d),T){const z=20,H=Math.max(20,k-L);h.drawText(this.canvas,{x,y:w+C+z},c.textAttribute,m({textMaxWidth:H},d))}break}case"polygon":{this.renderPolygon(e);break}case"line":{this.renderLine(e);break}case"point":{const c=e.annotation,u=_.changePointByZoom(c,this.zoom,this.currentPos),y=(r=f.radius)!=null?r:ct;h.drawCircle(this.canvas,u,y,f);const{headerText:p,bottomText:v}=this.getRenderText(c,c==null?void 0:c.hiddenText);p&&h.drawText(this.canvas,{x:u.x+y/2,y:u.y-y-4},p,m({textAlign:"center"},d)),v&&h.drawText(this.canvas,this.appendOffset({x:u.x+y,y:u.y+y+24}),v,d);break}case"text":{const c=e.annotation,{text:u,x:y,y:p,textMaxWidth:v,color:g="white",background:x="rgba(0, 0, 0, 0.6)",lineHeight:w=25,font:k=K,position:C,offset:b}=c,I=10,D=10,T=_.changePointByZoom({x:y,y:p},this.zoom,this.currentPos);b&&(T.x+=(a=b.x)!=null?a:0,T.y+=(n=b.y)!=null?n:0);const{width:A,height:L,fontHeight:z=0}=R.getTextArea(this.canvas,c.text,v,k,w);if(C==="rt")break;h.drawRectWithFill(this.canvas,{x:T.x,y:T.y,width:A+D*2,height:L+I*2,id:"",sourceID:"",valid:!0,textAttribute:"",attribute:""},{color:x}),h.drawText(this.canvas,{x:T.x+D,y:T.y+z+I},u,{color:g,lineHeight:w,font:k,textMaxWidth:v});break}case"box3d":{this.renderBox3d(e);break}case"cuboid":{this.renderSingleCuboid(e);break}case"pixelPoints":{this.renderPixelPoints(e);break}default:break}"renderEnhance"in e.annotation&&((s=(l=e.annotation).renderEnhance)==null||s.call(l,{ctx:this.ctx,canvas:this.canvas,currentPos:this.currentPos,zoom:this.zoom,data:e,toolInstance:this}))}),this.renderConnectionPoints()}catch(t){console.error("ViewOperation Render Error",t)}}}export{ht as default};
@@ -1 +1 @@
1
- import{RectOperation as t}from"./rectOperation.js";class o extends t{constructor(e){super(e);this.checkMode=e.checkMode}createNewDrawingRect(){}deleteSelectedRect(){}onMouseMove(e){this.checkMode||super.onMouseMove(e)}}export{o as default};
1
+ import d from"../../constant/keyCode.js";import{RectOperation as p}from"./rectOperation.js";import f from"./utils/reCalcRect.js";var R=Object.defineProperty,g=Object.defineProperties,w=Object.getOwnPropertyDescriptors,h=Object.getOwnPropertySymbols,D=Object.prototype.hasOwnProperty,m=Object.prototype.propertyIsEnumerable,u=(r,e,t)=>e in r?R(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,i=(r,e)=>{for(var t in e||(e={}))D.call(e,t)&&u(r,t,e[t]);if(h)for(var t of h(e))m.call(e,t)&&u(r,t,e[t]);return r},_=(r,e)=>g(r,w(e));class y extends p{constructor(e){super(e);this.checkMode=e.checkMode}createNewDrawingRect(e,t){this.checkMode||super.createNewDrawingRect(e,t)}deleteSelectedRect(e){if(this.checkMode)return;const{selectedRects:t}=this;t.length&&e.stopPropagation(),this.emit("deleteSelectedRects",t)}setSelectedIdAfterAddingDrawingRect(){!this.drawingRect||(this.setSelectedRectID(this.drawingRect.id),this.emit("afterAddingDrawingRect",i({},this.selectedRect)))}setSelectedRectID(e){this.checkMode||super.setSelectedRectID(e)}onKeyDown(e){if(this.checkMode||![d.Delete,d.R].includes(e.keyCode))return;super.onKeyDown(e);const{keyCode:t,ctrlKey:s,altKey:c,shiftKey:n,metaKey:o}=e;switch(t){case d.R:{if(s||c||n||o)return;this.resizeRect();break}}return!0}renderDrawingRect(e,t=this.zoom,s=!1){const c=(e==null?void 0:e.boxID)||(e==null?void 0:e.lineDash);super.renderDrawingRect(c?e:_(i({},e),{lineDash:[3]}),t,s)}resizeRect(e=100,t=100){var s;if(!this.imgNode||!((s=this.selectedRects)==null?void 0:s.length)||e<=0||t<=0||this.selectedRects[0].width<=e&&this.selectedRects[0].height<=t)return;const{width:c,height:n}=this.basicImgInfo,o={width:c,height:n,x:0,y:0},l=f(this.selectedRects[0],o,e,t);l.noChange||(this.setRectList(this.rectList.map(a=>this.selectedIDs.includes(a.id)?i(i({},a),l):a),!0),this.render(),this.updateDragResult())}}export{y as default};
@@ -1 +1 @@
1
- import U from"lodash";import{EToolName as v,ELineTypes as w,edgeAdsorptionScope as _}from"../../constant/tool.js";import j from"../../utils/tool/RectUtils.js";import L from"../../utils/tool/PolygonUtils.js";import S from"../../utils/tool/MarkerUtils.js";import z from"../../utils/MathUtils.js";import{EDragStatus as f,ESortDirection as M,DEFAULT_TEXT_OFFSET as N}from"../../constant/annotation.js";import P from"../../constant/keyCode.js";import C from"../../locales/index.js";import{EMessage as A}from"../../locales/constants.js";import p from"../../utils/tool/AttributeUtils.js";import c from"../../utils/tool/AxisUtils.js";import a from"../../utils/tool/CommonToolUtils.js";import D from"../../utils/tool/DrawUtils.js";import B from"../../utils/tool/StyleUtils.js";import Z from"../../utils/uuid.js";import{BasicToolOperation as H}from"./basicToolOperation.js";import K from"./textAttributeClass.js";import W from"./Selection.js";var F=Object.defineProperty,$=Object.defineProperties,V=Object.getOwnPropertyDescriptors,T=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,q=Object.prototype.propertyIsEnumerable,R=Math.pow,O=(m,t,e)=>t in m?F(m,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):m[t]=e,x=(m,t)=>{for(var e in t||(t={}))X.call(t,e)&&O(m,e,t[e]);if(T)for(var e of T(t))q.call(t,e)&&O(m,e,t[e]);return m},y=(m,t)=>$(m,V(t));const E=200;class G extends H{constructor(t){super(t);this.setMarkerIndex=s=>{this.markerIndex=s},this.setMarkerIndexAndSelect=s=>{if(!this.config.markerList)return;this.markerIndex=s;const o=this.config.markerList[s].value,n=this.currentPageResult.find(r=>r.label===o);n&&(this.setSelectedID(n.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(n.attribute)),this.emit("markIndexChange")},this.textChange=s=>{this.config.textConfigurable!==!0||!this.selectedID||(this.setPointList(p.textChange(s,this.selectedID,this.pointList)),this.emit("selectedChange"),this.render())},this.isMinDistance=s=>{const o=c.changePointByZoom(s,this.zoom);return this.pointList.some(n=>{const r=c.changePointByZoom(n,this.zoom);return z.getLineLength(r,o)<.2})};var e,i;this.config=a.jsonParser(t.config),this.pointList=[],this.markerIndex=0,this.selection=new W(this),this.createPoint=this.createPoint.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.setSelectedID=this.setSelectedID.bind(this),this.forbidAddNew=(e=t.forbidAddNew)!=null?e:!1,this.forbidDelete=(i=t.forbidDelete)!=null?i:!1}get dataList(){return this.pointList}get drawOutsideTarget(){var t;return(t=this.config.drawOutsideTarget)!=null?t:this.config.drawPointOut}get selectedID(){return this.selection.selectedID}get selectedIDs(){return this.selection.selectedIDs}get selectedPoints(){return this.pointList.filter(t=>this.selection.isIdSelected(t.id))}setNextMarker(t=this.pointList){if(this.hasMarkerConfig){const e=a.getNextMarker(this.getCurrentPageResult(t),this.config.markerList);e&&this.setMarkerIndexAndSelect(e.index)}}setResult(t){this.clearActiveStatus(),this.setPointList(t),this.setNextMarker(t),this.recoverOperationMode(),this.render()}setPointList(t,e=!1){const i=this.pointList.length;this.pointList=t,i!==t.length&&this.emit("updatePageNumber"),e&&this.emit("updateResult")}setConfig(t,e=!1){this.config=a.jsonParser(t),e===!0&&this.clearResult()}clearResult(){this.setPointList([]),this.setSelectedID(void 0),this.history.pushHistory([]),this.hoverID="",this.render()}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPoints.length>0&&(this.selectedPoints.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.pointList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}get selectedText(){var t;return(t=this.pointList.find(e=>e.id===this.selectedID))==null?void 0:t.textAttribute}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}getTextIconSvg(t=""){return p.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}clearActiveStatus(){this.hoverID=void 0,this.dragStatus=f.Wait,this.setSelectedID(void 0)}setBasicResult(t){super.setBasicResult(t),this.setNextMarker(),this.clearActiveStatus()}onDragMoveAll(t){var e,i;!((i=(e=this.dragInfo)==null?void 0:e.originPointList)==null?void 0:i.length)||(this.setPointList(this.dragInfo.originPointList.map(s=>y(x({},s),{x:s.x+t.x/this.zoom,y:s.y+t.y/this.zoom}))),this.render())}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation)){if(t.button===0&&!this.hoverID){this.recoverOperationMode(),this.createPoint(t),this.render();return}if(this.hoverID&&this.selection.isIdSelected(this.hoverID)&&t.button===0){this.dragStatus=f.Start,this.dragInfo={dragStartCoord:this.getCoordinateUnderZoom(t),originPointList:U.cloneDeep(this.selectedPoints)};return}return this.render(),!0}}onMouseMove(t){super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo||(this.hoverID=this.getHoverId(),(this.dragStatus===f.Start||this.dragStatus===f.Move)&&this.onDragMove(t),this.hoverID&&this.render())}onMouseUp(t){var e;if(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)return!0;t.button===2&&this.rightMouseUp(t),this.dragStatus===f.Move&&(this.history.pushHistory(this.pointList),this.emit("updatePointByDrag",this.pointList.find(i=>(i==null?void 0:i.id)===this.selectedID),(e=this.dragInfo)==null?void 0:e.originPointList),this.dragInfo=void 0),this.dragStatus=f.Wait,this.render()}onDragMove(t){var e,i;if(!this.imgInfo)return;this.dragStatus=f.Move;const s=this.getCoordinateUnderZoom(t);if(this.selectedIDs.length>1&&this.dragInfo){const r={x:s.x-this.dragInfo.dragStartCoord.x,y:s.y-this.dragInfo.dragStartCoord.y};this.onDragMoveAll(r);return}const o=c.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.drawOutsideTarget,this.basicResult,this.zoom),n=this.drawOutsideTarget?c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos):c.changePointByZoom(o,1/this.zoom);this.drawOutsideTarget===!1&&this.dependToolName===v.Polygon&&((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&!L.isInPolygon(n,this.basicResult.pointList)||(this.pointList.forEach(r=>{r.id===this.selectedID&&(r.x=n.x,r.y=n.y)}),this.render())}onKeyDown(t){if(!a.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;this.selection.triggerKeyboardEvent(t,this.setPointList.bind(this));const{keyCode:e}=t;switch(e){case P.Delete:this.deletePoint();break;case P.Tab:{this.onTabKeyDown(t);break}case P.Z:this.setIsHidden(!this.isHidden),this.render();break;case P.A:this.selection.selectAll();break;default:{if(this.config.attributeConfigurable){const i=p.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}createPoint(t){var e,i,s,o;if(!this.imgInfo||this.forbidAddNew)return;const{upperLimit:n}=this.config;if(n&&this.currentPageResult.length>=n){this.emit("messageInfo",`${C.getMessagesByLocale(A.LowerLimitPoint,this.lang)}`);return}const r=a.getSourceID(this.basicResult),u=this.getCoordinateUnderZoom(t);let d=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.config.edgeAdsorption&&this.referenceData&&[v.Polygon,v.Line].includes((e=this.referenceData)==null?void 0:e.toolName)){const b=((i=this.referenceData)==null?void 0:i.toolName)===v.Polygon,{dropFoot:I,hasClosed:k}=L.getClosestPoint(d,this.referenceData.result,(o=(s=this.referenceData.config)==null?void 0:s.lineType)!=null?o:w.Line,_/this.zoom,{isClose:b});I&&(d=I),k&&this.emit("messageSuccess",`${C.getMessagesByLocale(A.SuccessfulEdgeAdsorption,this.lang)}`)}if(this.drawOutsideTarget===!1){if(this.dependToolName&&this.basicCanvas){let l=!1;switch(this.dependToolName){case v.Rect:{l=!j.isInRect(d,this.basicResult);break}case v.Polygon:{l=!L.isInPolygon(d,this.basicResult.pointList);break}}if(l)return}if(u.x<0||u.y<0||u.x>this.imgInfo.width||u.y>this.imgInfo.height)return}if(this.isMinDistance(d))return;let h=y(x({},d),{attribute:this.defaultAttribute,valid:!t.ctrlKey,id:Z(8,62),sourceID:r,textAttribute:"",order:a.getMaxOrder(this.pointList.filter(l=>a.isSameSourceID(l.sourceID,r)))+1});if(this.config.textConfigurable){let l="";l=p.getTextAttribute(this.pointList.filter(b=>a.isSameSourceID(b.sourceID,r)),this.config.textCheckType),h=y(x({},h),{textAttribute:l})}if(this.hasMarkerConfig){const l=a.getNextMarker(this.currentPageResult,this.config.markerList,this.markerIndex);if(l)h=y(x({},h),{label:l.label}),this.markerIndex=l.index,this.emit("markIndexChange");else{this.emit("messageInfo",C.getMessagesByLocale(A.MarkerFinish,this.lang));return}}this.hoverID=h.id;const g=[...this.pointList,h];this.setPointList(g),this.emit("pointCreated",h,this.zoom),this.history.pushHistory(g),this.setSelectedID(h.id)}isInPoint(t,e,i=this.zoom){return(this.style.width+2)/i>=Math.sqrt(R(t.x-e.x,2)+R(t.y-e.y,2))}getHoverId(){var t;const e=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos),i=(t=this.pointList)==null?void 0:t.find(s=>this.isInPoint(e,s));return i==null?void 0:i.id}get selectedPoint(){return this.pointList.find(t=>t.id===this.selectedID)}rightMouseUp(t){if(this.recoverOperationMode(),this.selectedID===this.hoverID){if(this.forbidDelete)return;const i=this.pointList.filter(s=>s.id!==this.selectedID);this.setPointList(i),this.history.pushHistory(i),this.emit("pointDeleted",this.selectedID),this.setSelectedID(""),this.hoverID="";return}const e=this.pointList.find(i=>i.id===this.hoverID);if(this.setSelectedID(this.hoverID,t.ctrlKey),this.emit("pointSelected",this.hoverID),e&&this.setDefaultAttribute(e==null?void 0:e.attribute),(e==null?void 0:e.label)&&this.hasMarkerConfig){const i=a.getCurrentMarkerIndex(e.label,this.config.markerList);i>=0&&(this.setMarkerIndex(i),this.emit("markIndexChange"))}}onTabKeyDown(t){if(t.preventDefault(),this.dragStatus===f.Move||this.dragStatus===f.Start)return;let e=M.ascend;t.shiftKey&&(e=M.descend);const[i,s]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let o=[...i];s&&(o=[...o,...s]);const n=a.getNextSelectedRectID(o,e,this.selectedID);n&&this.setSelectedID(n.id)}get currentPageResult(){const[t]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),[]);return t}getCurrentPageResult(t){const[e]=a.getRenderResultList(t,a.getSourceID(this.basicResult),[]);return e}exportData(){const{pointList:t}=this;return[t,this.basicImgInfo]}deletePoint(){var t;this.selectedIDs.length>0&&(this.setPointList(this.pointList.filter(e=>this.selection.isIdSelected(e.id))),this.history.pushHistory(this.pointList),(t=this._textAttributeInstance)==null||t.clearTextAttribute(),this.emit("selectedChange"),this.emit("pointDeleted",this.selectedID),this.render())}undoAndRedo(t){var e,i;if(this.dragStatus===f.Move||this.dragStatus===f.Start)return;const s=(i=(e=this.history)[t])==null?void 0:i.call(e);(s==null?void 0:s.some(o=>o.id===this.selectedID))||this.setSelectedID(""),s&&(this.setPointList(s,!0),this.render())}undo(){this.undoAndRedo("undo")}redo(){this.undoAndRedo("redo")}getCurrentSelectedData(){var t;if(!this.selectedID)return;const e=(t=this.pointList)==null?void 0:t.find(o=>o.id===this.selectedID),i=this.getColor(e==null?void 0:e.attribute),s=(e==null?void 0:e.valid)?i==null?void 0:i.valid.stroke:i==null?void 0:i.invalid.stroke;return this.dragStatus=f.Wait,{width:E*this.zoom*.6,textAttribute:(e==null?void 0:e.textAttribute)||"",color:s}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;p.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",p.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPointList(p.textChange(e,this.selectedID,this.pointList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t,e;const i=(t=this.pointList)==null?void 0:t.find(b=>b.id===this.selectedID);if(!this.ctx||this.config.textConfigurable!==!0||!i)return;const{x:s,y:o,attribute:n,valid:r}=i,u=E*this.zoom*.6,d=c.getOffsetCoordinate({x:s,y:o},this.currentPos,this.zoom),h=this.getColor(n),g=r?h==null?void 0:h.valid.stroke:h==null?void 0:h.invalid.stroke,l=4;this._textAttributeInstance||(this._textAttributeInstance=new K({width:u,container:this.container,icon:this.getTextIconSvg(n),color:g,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((e=this._textAttributeInstance)==null?void 0:e.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${i.textAttribute}`,{left:d.x,top:d.y+l,color:g,width:u})}renderPoint(t,e=!1){var i,s,o;const{textAttribute:n="",attribute:r}=t,u=e||t.id===this.selectedID,d=this.getColor(r),h=c.changePointByZoom(t,this.zoom,this.currentPos),{width:g=2,hiddenText:l=!1}=this.style,b=B.getStrokeAndFill(d,t.valid,{isSelected:u||t.id===this.hoverID});D.drawCircle(this.canvas,h,g,{startAngleDeg:0,endAngleDeg:360,thickness:1,color:b.stroke,fill:b.fill});let I="";(((i=this.config)==null?void 0:i.isShowOrder)||((s=this.config)==null?void 0:s.showOrder))&&t.order&&(t==null?void 0:t.order)>0&&(I=`${t.order}`),t.label&&this.hasMarkerConfig&&(I=`${a.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${S.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&!this.config.hideAttribute&&(I=`${I} ${p.getAttributeShowText(t.attribute,(o=this.config)==null?void 0:o.attributeList)}`),l||D.drawText(this.canvas,{x:h.x+g/2,y:h.y-g-4},I,{textAlign:"center",color:b.stroke}),u?this.renderTextAttribute():l||D.drawText(this.canvas,{x:h.x+g,y:h.y+g+24},n,x({color:b.stroke},N))}renderMultiSelectedPoint(){this.selectedPoints.forEach(t=>{this.renderPoint(t,!0)})}renderPointList(){const[t,e]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);this.isHidden||t.forEach(i=>{this.renderPoint(i)}),e&&e.forEach(i=>{this.renderPoint(i,!0)})}renderAuxiliaryLine(){var t;if(!this.config.markerConfigurable||!((t=this.config.markerList)==null?void 0:t.length))return;const[e,i]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs),s=e;if(i&&s.push(...i),s.length<2)return;const o=S.getAuxiliaryLineByMarkerList(this.config.markerList,"value");S.getAuxiliaryLineCoord(o,s).forEach(r=>{const{start:u,end:d}=r,h=c.changePointListByZoom([u,d],this.zoom,this.currentPos);D.drawLine(this.canvas,h[0],h[1],{color:"#C5C5C5"})})}renderTop(){var t,e,i;const s=this.getLineColor(this.defaultAttribute);if(this.renderCursorLine(s),this.config.edgeAdsorption&&this.referenceData){let o=c.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const n=((t=this.referenceData)==null?void 0:t.toolName)===v.Polygon,{dropFoot:r}=L.getClosestPoint(o,this.referenceData.result,(i=(e=this.referenceData.config)==null?void 0:e.lineType)!=null?i:w.Line,_/this.zoom,{isClose:n});r&&o!==r&&o!==r&&(o=r,D.drawCircle(this.canvas,c.changePointByZoom(o,this.zoom,this.currentPos),5,{color:"white",fill:"white"}),D.drawCircle(this.canvas,c.changePointByZoom(o,this.zoom,this.currentPos),3,{fill:s,color:s}))}}render(){!this.ctx||(super.render(),this.renderAuxiliaryLine(),this.renderPointList(),this.renderTop())}}export{G as default};
1
+ import E from"lodash";import{EToolName as b,ELineTypes as w,edgeAdsorptionScope as k}from"../../constant/tool.js";import U from"../../utils/tool/RectUtils.js";import L from"../../utils/tool/PolygonUtils.js";import A from"../../utils/tool/MarkerUtils.js";import j from"../../utils/MathUtils.js";import{EDragStatus as c,ESortDirection as _,DEFAULT_TEXT_OFFSET as N}from"../../constant/annotation.js";import P from"../../constant/keyCode.js";import y from"../../locales/index.js";import{EMessage as S}from"../../locales/constants.js";import m from"../../utils/tool/AttributeUtils.js";import d from"../../utils/tool/AxisUtils.js";import a from"../../utils/tool/CommonToolUtils.js";import I from"../../utils/tool/DrawUtils.js";import z from"../../utils/tool/StyleUtils.js";import B from"../../utils/uuid.js";import{BasicToolOperation as Z}from"./basicToolOperation.js";import H from"./textAttributeClass.js";import K from"./Selection.js";var W=Object.defineProperty,$=Object.defineProperties,F=Object.getOwnPropertyDescriptors,T=Object.getOwnPropertySymbols,V=Object.prototype.hasOwnProperty,X=Object.prototype.propertyIsEnumerable,M=Math.pow,R=(g,t,e)=>t in g?W(g,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):g[t]=e,x=(g,t)=>{for(var e in t||(t={}))V.call(t,e)&&R(g,e,t[e]);if(T)for(var e of T(t))X.call(t,e)&&R(g,e,t[e]);return g},C=(g,t)=>$(g,F(t));const O=200;class q extends Z{constructor(t){super(t);this.setMarkerIndex=s=>{this.markerIndex=s},this.setMarkerIndexAndSelect=s=>{if(!this.config.markerList)return;this.markerIndex=s;const o=this.config.markerList[s].value,n=this.currentPageResult.find(l=>l.label===o);n&&(this.setSelectedID(n.id),this.config.attributeConfigurable===!0&&this.setDefaultAttribute(n.attribute)),this.emit("markIndexChange")},this.textChange=s=>{this.config.textConfigurable!==!0||!this.selectedID||(this.setPointList(m.textChange(s,this.selectedID,this.pointList)),this.emit("selectedChange"),this.render())},this.isMinDistance=s=>{const o=d.changePointByZoom(s,this.zoom);return this.pointList.some(n=>{const l=d.changePointByZoom(n,this.zoom);return j.getLineLength(l,o)<.2})};var e,i;this.config=a.jsonParser(t.config),this.pointList=[],this.markerIndex=0,this.selection=new K(this),this.createPoint=this.createPoint.bind(this),this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.setSelectedID=this.setSelectedID.bind(this),this.forbidAddNew=(e=t.forbidAddNew)!=null?e:!1,this.forbidDelete=(i=t.forbidDelete)!=null?i:!1}get dataList(){return this.pointList}get drawOutsideTarget(){var t;return(t=this.config.drawOutsideTarget)!=null?t:this.config.drawPointOut}get selectedID(){return this.selection.selectedID}get selectedIDs(){return this.selection.selectedIDs}get selectedPoints(){return this.pointList.filter(t=>this.selection.isIdSelected(t.id))}setNextMarker(t=this.pointList){if(this.hasMarkerConfig){const e=a.getNextMarker(this.getCurrentPageResult(t),this.config.markerList);e&&this.setMarkerIndexAndSelect(e.index)}}setResult(t){this.clearActiveStatus(),this.setPointList(t),this.setNextMarker(t),this.recoverOperationMode(),this.render()}setPointList(t,e=!1){const i=this.pointList.length;this.pointList=t,i!==t.length&&this.emit("updatePageNumber"),e&&this.emit("updateResult")}setConfig(t,e=!1){this.config=a.jsonParser(t),e===!0&&this.clearResult()}clearResult(){this.setPointList([]),this.setSelectedID(void 0),this.history.pushHistory([]),this.hoverID="",this.render()}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPoints.length>0&&(this.selectedPoints.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.pointList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}get selectedText(){var t;return(t=this.pointList.find(e=>e.id===this.selectedID))==null?void 0:t.textAttribute}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}getTextIconSvg(t=""){return m.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}clearActiveStatus(){this.hoverID=void 0,this.dragStatus=c.Wait,this.setSelectedID(void 0)}setBasicResult(t){super.setBasicResult(t),this.setNextMarker(),this.clearActiveStatus()}onDragMoveAll(t){var e,i;!((i=(e=this.dragInfo)==null?void 0:e.originPointList)==null?void 0:i.length)||(this.setPointList(this.dragInfo.originPointList.map(s=>C(x({},s),{x:s.x+t.x/this.zoom,y:s.y+t.y/this.zoom}))),this.render())}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation)){if(t.button===0&&!this.hoverID){this.recoverOperationMode(),this.createPoint(t),this.render();return}if(this.hoverID&&this.selection.isIdSelected(this.hoverID)&&t.button===0){this.dragStatus=c.Start,this.dragInfo={dragStartCoord:this.getCoordinateUnderZoom(t),originPointList:E.cloneDeep(this.selectedPoints)};return}return this.render(),!0}}onMouseMove(t){super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo||(this.hoverID=this.getHoverId(),(this.dragStatus===c.Start||this.dragStatus===c.Move)&&this.onDragMove(t),this.hoverID&&this.render())}onMouseUp(t){var e;if(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)return!0;if(t.button===2&&this.rightMouseUp(t),this.dragStatus===c.Move){const i=this.pointList.find(s=>(s==null?void 0:s.id)===this.selectedID);if(i){const{newPoint:s,hasClosed:o}=this.getNearPoint(i);s&&Object.assign(i,s),o&&this.emit("messageSuccess",`${y.getMessagesByLocale(S.SuccessfulEdgeAdsorption,this.lang)}`)}this.history.pushHistory(this.pointList),this.emit("updatePointByDrag",i,(e=this.dragInfo)==null?void 0:e.originPointList),this.dragInfo=void 0}this.dragStatus=c.Wait,this.render()}onDragMove(t){var e,i;if(!this.imgInfo)return;this.dragStatus=c.Move;const s=this.getCoordinateUnderZoom(t);if(this.selectedIDs.length>1&&this.dragInfo){const l={x:s.x-this.dragInfo.dragStartCoord.x,y:s.y-this.dragInfo.dragStartCoord.y};this.onDragMoveAll(l);return}const o=d.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.drawOutsideTarget,this.basicResult,this.zoom),n=this.drawOutsideTarget?d.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos):d.changePointByZoom(o,1/this.zoom);this.drawOutsideTarget===!1&&this.dependToolName===b.Polygon&&((i=(e=this.basicResult)==null?void 0:e.pointList)==null?void 0:i.length)>0&&!L.isInPolygon(n,this.basicResult.pointList)||(this.pointList.forEach(l=>{l.id===this.selectedID&&(l.x=n.x,l.y=n.y)}),this.render())}onKeyDown(t){if(!a.hotkeyFilter(t)||super.onKeyDown(t)===!1)return;this.selection.triggerKeyboardEvent(t,this.setPointList.bind(this));const{keyCode:e}=t;switch(e){case P.Delete:this.deletePoint();break;case P.Tab:{this.onTabKeyDown(t);break}case P.Z:this.setIsHidden(!this.isHidden),this.render();break;case P.A:this.selection.selectAll();break;default:{if(this.config.attributeConfigurable){const i=m.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}getNearPoint(t){var e,i,s;if(this.config.edgeAdsorption&&this.basicResult&&this.dependToolName){const o=[b.Polygon,b.Line].includes(this.dependToolName),n=[this.basicResult];if(o){const l=this.dependToolName===b.Polygon,{dropFoot:h,hasClosed:u}=L.getClosestPoint(t,n,(s=(i=(e=this.referenceData)==null?void 0:e.config)==null?void 0:i.lineType)!=null?s:w.Line,k/this.zoom,{isClose:l});return{newPoint:h,hasClosed:u}}}return{newPoint:t,hasClosed:!1}}createPoint(t){if(!this.imgInfo||this.forbidAddNew)return;const{upperLimit:e}=this.config;if(e&&this.currentPageResult.length>=e){this.emit("messageInfo",`${y.getMessagesByLocale(S.LowerLimitPoint,this.lang)}`);return}const i=a.getSourceID(this.basicResult),s=this.getCoordinateUnderZoom(t);let o=d.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const{newPoint:n,hasClosed:l}=this.getNearPoint(o);if(n&&(o=n),l&&this.emit("messageSuccess",`${y.getMessagesByLocale(S.SuccessfulEdgeAdsorption,this.lang)}`),this.drawOutsideTarget===!1){if(this.dependToolName&&this.basicCanvas){let r=!1;switch(this.dependToolName){case b.Rect:{r=!U.isInRect(o,this.basicResult);break}case b.Polygon:{r=!L.isInPolygon(o,this.basicResult.pointList);break}}if(r)return}if(s.x<0||s.y<0||s.x>this.imgInfo.width||s.y>this.imgInfo.height)return}if(this.isMinDistance(o))return;let h=C(x({},o),{attribute:this.defaultAttribute,valid:!t.ctrlKey,id:B(8,62),sourceID:i,textAttribute:"",order:a.getMaxOrder(this.pointList.filter(r=>a.isSameSourceID(r.sourceID,i)))+1});if(this.config.textConfigurable){let r="";r=m.getTextAttribute(this.pointList.filter(f=>a.isSameSourceID(f.sourceID,i)),this.config.textCheckType),h=C(x({},h),{textAttribute:r})}if(this.hasMarkerConfig){const r=a.getNextMarker(this.currentPageResult,this.config.markerList,this.markerIndex);if(r)h=C(x({},h),{label:r.label}),this.markerIndex=r.index,this.emit("markIndexChange");else{this.emit("messageInfo",y.getMessagesByLocale(S.MarkerFinish,this.lang));return}}this.hoverID=h.id;const u=[...this.pointList,h];this.setPointList(u),this.emit("pointCreated",h,this.zoom),this.history.pushHistory(u),this.setSelectedID(h.id)}isInPoint(t,e,i=this.zoom){return(this.style.width+2)/i>=Math.sqrt(M(t.x-e.x,2)+M(t.y-e.y,2))}getHoverId(){var t;const e=d.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos),i=(t=this.pointList)==null?void 0:t.find(s=>this.isInPoint(e,s));return i==null?void 0:i.id}get selectedPoint(){return this.pointList.find(t=>t.id===this.selectedID)}rightMouseUp(t){if(this.recoverOperationMode(),this.selectedID===this.hoverID){if(this.forbidDelete)return;const i=this.pointList.filter(s=>s.id!==this.selectedID);this.setPointList(i),this.history.pushHistory(i),this.emit("pointDeleted",this.selectedID),this.setSelectedID(""),this.hoverID="";return}const e=this.pointList.find(i=>i.id===this.hoverID);if(this.setSelectedID(this.hoverID,t.ctrlKey),this.emit("pointSelected",this.hoverID),e&&this.setDefaultAttribute(e==null?void 0:e.attribute),(e==null?void 0:e.label)&&this.hasMarkerConfig){const i=a.getCurrentMarkerIndex(e.label,this.config.markerList);i>=0&&(this.setMarkerIndex(i),this.emit("markIndexChange"))}}onTabKeyDown(t){if(t.preventDefault(),this.dragStatus===c.Move||this.dragStatus===c.Start)return;let e=_.ascend;t.shiftKey&&(e=_.descend);const[i,s]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let o=[...i];s&&(o=[...o,...s]);const n=a.getNextSelectedRectID(o,e,this.selectedID);n&&this.setSelectedID(n.id)}get currentPageResult(){const[t]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),[]);return t}getCurrentPageResult(t){const[e]=a.getRenderResultList(t,a.getSourceID(this.basicResult),[]);return e}exportData(){const{pointList:t}=this;return[t,this.basicImgInfo]}deletePoint(){var t;this.selectedIDs.length>0&&(this.setPointList(this.pointList.filter(e=>this.selection.isIdSelected(e.id))),this.history.pushHistory(this.pointList),(t=this._textAttributeInstance)==null||t.clearTextAttribute(),this.emit("selectedChange"),this.emit("pointDeleted",this.selectedID),this.render())}undoAndRedo(t){var e,i;if(this.dragStatus===c.Move||this.dragStatus===c.Start)return;const s=(i=(e=this.history)[t])==null?void 0:i.call(e);(s==null?void 0:s.some(o=>o.id===this.selectedID))||this.setSelectedID(""),s&&(this.setPointList(s,!0),this.render())}undo(){this.undoAndRedo("undo")}redo(){this.undoAndRedo("redo")}getCurrentSelectedData(){var t;if(!this.selectedID)return;const e=(t=this.pointList)==null?void 0:t.find(o=>o.id===this.selectedID),i=this.getColor(e==null?void 0:e.attribute),s=(e==null?void 0:e.valid)?i==null?void 0:i.valid.stroke:i==null?void 0:i.invalid.stroke;return this.dragStatus=c.Wait,{width:O*this.zoom*.6,textAttribute:(e==null?void 0:e.textAttribute)||"",color:s}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;m.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",m.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPointList(m.textChange(e,this.selectedID,this.pointList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t,e;const i=(t=this.pointList)==null?void 0:t.find(p=>p.id===this.selectedID);if(!this.ctx||this.config.textConfigurable!==!0||!i)return;const{x:s,y:o,attribute:n,valid:l}=i,h=O*this.zoom*.6,u=d.getOffsetCoordinate({x:s,y:o},this.currentPos,this.zoom),r=this.getColor(n),f=l?r==null?void 0:r.valid.stroke:r==null?void 0:r.invalid.stroke,D=4;this._textAttributeInstance||(this._textAttributeInstance=new H({width:h,container:this.container,icon:this.getTextIconSvg(n),color:f,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((e=this._textAttributeInstance)==null?void 0:e.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${i.textAttribute}`,{left:u.x,top:u.y+D,color:f,width:h})}renderPoint(t,e=!1){var i,s,o;const{textAttribute:n="",attribute:l}=t,h=e||t.id===this.selectedID,u=this.getColor(l),r=d.changePointByZoom(t,this.zoom,this.currentPos),{width:f=2,hiddenText:D=!1}=this.style,p=z.getStrokeAndFill(u,t.valid,{isSelected:h||t.id===this.hoverID});I.drawCircle(this.canvas,r,f,{startAngleDeg:0,endAngleDeg:360,thickness:1,color:p.stroke,fill:p.fill});let v="";(((i=this.config)==null?void 0:i.isShowOrder)||((s=this.config)==null?void 0:s.showOrder))&&t.order&&(t==null?void 0:t.order)>0&&(v=`${t.order}`),t.label&&this.hasMarkerConfig&&(v=`${a.getCurrentMarkerIndex(t.label,this.config.markerList)+1}_${A.getMarkerShowText(t.label,this.config.markerList)}`),t.attribute&&!this.config.hideAttribute&&(v=`${v} ${m.getAttributeShowText(t.attribute,(o=this.config)==null?void 0:o.attributeList)}`),D||I.drawText(this.canvas,{x:r.x+f/2,y:r.y-f-4},v,{textAlign:"center",color:p.stroke}),h?this.renderTextAttribute():D||I.drawText(this.canvas,{x:r.x+f,y:r.y+f+24},n,x({color:p.stroke},N))}renderMultiSelectedPoint(){this.selectedPoints.forEach(t=>{this.renderPoint(t,!0)})}renderPointList(){const[t,e]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);this.isHidden||t.forEach(i=>{this.renderPoint(i)}),e&&e.forEach(i=>{this.renderPoint(i,!0)})}renderAuxiliaryLine(){var t;if(!this.config.markerConfigurable||!((t=this.config.markerList)==null?void 0:t.length))return;const[e,i]=a.getRenderResultList(this.pointList,a.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs),s=e;if(i&&s.push(...i),s.length<2)return;const o=A.getAuxiliaryLineByMarkerList(this.config.markerList,"value");A.getAuxiliaryLineCoord(o,s).forEach(l=>{const{start:h,end:u}=l,r=d.changePointListByZoom([h,u],this.zoom,this.currentPos);I.drawLine(this.canvas,r[0],r[1],{color:"#C5C5C5"})})}renderTop(){var t,e,i;const s=this.getLineColor(this.defaultAttribute);if(this.renderCursorLine(s),this.config.edgeAdsorption&&this.basicResult&&this.dependToolName){let o=d.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);const n=this.dependToolName===b.Polygon,l=[this.basicResult],{dropFoot:h}=L.getClosestPoint(o,l,(i=(e=(t=this.referenceData)==null?void 0:t.config)==null?void 0:e.lineType)!=null?i:w.Line,k/this.zoom,{isClose:n});h&&o!==h&&o!==h&&(o=h,I.drawCircle(this.canvas,d.changePointByZoom(o,this.zoom,this.currentPos),5,{color:"white",fill:"white"}),I.drawCircle(this.canvas,d.changePointByZoom(o,this.zoom,this.currentPos),3,{fill:s,color:s}))}}render(){!this.ctx||(super.render(),this.renderAuxiliaryLine(),this.renderPointList(),this.renderTop())}}export{q as default};
@@ -1 +1 @@
1
- import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import D from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as W,EDragStatus as x,ESortDirection as U,EDragTarget as v,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as k}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as w,edgeAdsorptionScope as R,ELineTypes as O,EToolName as H}from"../../constant/tool.js";import C from"../../locales/index.js";import{EMessage as S}from"../../locales/constants.js";import K from"../../utils/ActionsHistory.js";import b from"../../utils/tool/AttributeUtils.js";import a from"../../utils/tool/AxisUtils.js";import B from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import A from"../../utils/tool/StyleUtils.js";import M from"../../utils/uuid.js";import{BasicToolOperation as $}from"./basicToolOperation.js";import V from"./textAttributeClass.js";import X from"./Selection.js";var Y=Object.defineProperty,q=Object.defineProperties,G=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,F=(I,t,e)=>t in I?Y(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))J.call(t,e)&&F(I,e,t[e]);if(Z)for(var e of Z(t))Q.call(t,e)&&F(I,e,t[e]);return I},m=(I,t)=>q(I,G(t));const z=164;class tt extends ${constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=f.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new K,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=w.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new X(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:a.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.polygonList,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.polygonList,f.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=W.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=a.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(a.returnClosePointIndex(o,a.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),r=a.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===w.Rect&&this.drawingPointList.length===2){const h=D.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.dragInfo=void 0,this.dragStatus=x.Wait,this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const l=(o=s==null?void 0:s.valid)!=null?o:!0;return m(y({},s),{valid:e!=null?e:!l})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${C.getMessagesByLocale(S.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=f.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const l=o.find(n=>n.id===this.editPolygonID);if(!l)return;l.pointList=this.drawingPointList,this.editPolygonID=""}else{const l=M(8,62);let n={id:l,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:f.getMaxOrder(o.filter(r=>f.isSameSourceID(r.sourceID,s)))+1};if(this.config.textConfigurable){let r="";r=b.getTextAttribute(this.polygonList.filter(h=>f.isSameSourceID(h.sourceID,s)),this.config.textCheckType),n=m(y({},n),{textAttribute:r})}this.pattern===w.Rect&&t===!0&&(n=m(y({},n),{isRect:!0})),o.push(n),i=n,this.setSelectedIdAfterAddingDrawing(l)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:a.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=a.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=a.changePointListByZoom(e.pointList,this.zoom);return a.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${C.getMessagesByLocale(S.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=U.ascend;t.shiftKey&&(e=U.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=B.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,c,g,d;return m(y({},r),{x:(c=(h=r.pointList[0])==null?void 0:h.x)!=null?c:0,y:(d=(g=r.pointList[0])==null?void 0:g.y)!=null?d:0})}).filter(r=>B.inViewPort({x:r.x,y:r.y},o)),n=f.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=b.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${C.getMessagesByLocale(S.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${C.getMessagesByLocale(S.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,a.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=x.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=v.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==O.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=u.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const c=f.getSourceID(this.basicResult);let g="";o=this.polygonList.map(d=>{var p,T;return d.id===this.selectedID?(r=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,g=(T=d==null?void 0:d.textAttribute)!=null?T:"",m(y({},d),{pointList:n})):d}),l.length>0&&l.forEach((d,p)=>{o.push({sourceID:c,id:M(8,62),pointList:d,valid:h,order:f.getMaxOrder(this.currentShowList)+1+p,attribute:r,textAttribute:g})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==O.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=u.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(c=>!r.includes(c.id)).map(c=>c.id===this.selectedID?n:c);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case H.Rect:{n=t.filter(r=>!N.isInRect(r,this.basicResult)).length>0;break}case H.Polygon:{n=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=D.calcViewportBoundaries(a.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===w.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[v.Line].includes(o)){const c=D.getArrayIndex(n[0]-2,4),g=D.getArrayIndex(n[0]-1,4),d=[r[c],r[g]],p=D.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=x.Move,o){case v.Plane:i=i.map(r=>m(y({},r),{x:r.x+n.x,y:r.y+n.y}));break;case v.Point:case v.Line:i=i.map((r,h)=>l&&l.includes(h)?m(y({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===w.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&l&&(i=D.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===x.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.dragInfo=void 0,this.dragStatus=x.Wait,this.history.pushHistory(this.polygonList);return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===x.Start&&(this.dragInfo=void 0,this.dragStatus=x.Wait)}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return b.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon: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:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;b.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",b.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(b.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=z,c=a.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),g=this.getColor(s),d=o?g==null?void 0:g.valid.stroke:g==null?void 0:g.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new V({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:c.x,top:c.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,c=this.getColor(h),g=A.getStrokeAndFill(c,e.valid),d=a.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:g.fill,strokeColor:g.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(l=b.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,d[0],p,y({color:g.stroke},k));const T=d[d.length-1];P.drawText(this.canvas,{x:T.x+E.x,y:T.y+E.y},r,y({color:g.stroke},k))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=A.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=b.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),P.drawText(this.canvas,a.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,y({color:l.stroke},k)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=A.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,a.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=A.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=a.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===w.Rect&&n.length===2)n=D.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=u.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,R/this.zoom);h&&(r=h)}n.push(r)}P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:c,y:g}=a.changePointByZoom(h,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:c,y:g},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,a.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{tt as default};
1
+ import j from"lodash";import{i18n as _}from"@labelbee/lb-utils";import b from"../../utils/MathUtils.js";import N from"../../utils/tool/RectUtils.js";import{ERotateDirection as K,ESortDirection as U,EDragTarget as v,EDragStatus as S,TEXT_ATTRIBUTE_OFFSET as E,DEFAULT_TEXT_OFFSET as k}from"../../constant/annotation.js";import L from"../../constant/keyCode.js";import{EPolygonPattern as x,edgeAdsorptionScope as R,ELineTypes as O,EToolName as H}from"../../constant/tool.js";import w from"../../locales/index.js";import{EMessage as C}from"../../locales/constants.js";import $ from"../../utils/ActionsHistory.js";import D from"../../utils/tool/AttributeUtils.js";import a from"../../utils/tool/AxisUtils.js";import B from"../../utils/tool/CanvasUtils.js";import f from"../../utils/tool/CommonToolUtils.js";import P from"../../utils/tool/DrawUtils.js";import u from"../../utils/tool/PolygonUtils.js";import A from"../../utils/tool/StyleUtils.js";import M from"../../utils/uuid.js";import{BasicToolOperation as W}from"./basicToolOperation.js";import V from"./textAttributeClass.js";import X from"./Selection.js";var Y=Object.defineProperty,q=Object.defineProperties,G=Object.getOwnPropertyDescriptors,Z=Object.getOwnPropertySymbols,J=Object.prototype.hasOwnProperty,Q=Object.prototype.propertyIsEnumerable,F=(I,t,e)=>t in I?Y(I,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):I[t]=e,y=(I,t)=>{for(var e in t||(t={}))J.call(t,e)&&F(I,e,t[e]);if(Z)for(var e of Z(t))Q.call(t,e)&&F(I,e,t[e]);return I},m=(I,t)=>q(I,G(t));const z=164;class tt extends W{constructor(t){super(t);this.isAllowDouble=e=>{const{selectedID:i}=this,s=this.getHoverID(e);return!!(i&&i===s)},this.textChange=e=>{this.config.textConfigurable===!1||!this.selectedID||(this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("selectedChange"),this.render())},this.emitUpdatePolygonByDrag=()=>{if(this.dragInfo){const{originPolygon:e}=this.dragInfo;this.emit("updatePolygonByDrag",[{newPolygon:this.selectedPolygon,originPolygon:e}])}},this.config=f.jsonParser(t.config),this.drawingPointList=[],this.polygonList=[],this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.drawingHistory=new $,this.isCtrl=!1,this.isAlt=!1,this.isCombined=!1,this.pattern=x.Normal,this.getCurrentSelectedData=this.getCurrentSelectedData.bind(this),this.updateSelectedTextAttribute=this.updateSelectedTextAttribute.bind(this),this.selection=new X(this)}get selectedIDs(){return this.selection.selectedIDs}get selectedID(){return this.selection.selectedID}get minArea(){return this.config.minArea||0}eventBinding(){super.eventBinding(),this.container.removeEventListener("mouseup",this.onMouseUp),this.container.addEventListener("mouseup",this.dragMouseUp),this.dblClickListener.addEvent(this.onMouseUp,this.onLeftDblClick,this.onRightDblClick,this.isAllowDouble)}eventUnbinding(){super.eventUnbinding(),this.container.removeEventListener("mouseup",this.dragMouseUp)}destroy(){super.destroy(),this._textAttributeInstance&&this._textAttributeInstance.clearTextAttribute()}get selectedPolygon(){return u.getPolygonByID(this.visiblePolygonList,this.selectedID)}get selectedPolygons(){return u.getPolygonByIDs(this.polygonList,this.selectedIDs)}get hoverPolygon(){return this.visiblePolygonList.find(t=>t.id===this.hoverID&&t.id!==this.selectedID)}get enableDrag(){return Boolean(this.selectedID&&this.dragInfo)}get visiblePolygonList(){return this.polygonList}get polygonListUnderZoom(){return this.visiblePolygonList.map(t=>m(y({},t),{pointList:a.changePointListByZoom(t.pointList,this.zoom)}))}get selectedText(){var t;return(t=this.selectedPolygon)==null?void 0:t.textAttribute}get dataList(){return this.polygonList}setPattern(t,e=!1){var i;((i=this.drawingPointList)==null?void 0:i.length)>0&&e===!0||(this.pattern=t)}get currentShowList(){let t=[];const[e,i]=f.getRenderResultList(this.polygonList,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.polygonList,f.getSourceID(this.basicResult),[]);return t}setResultAndSelectedID(t,e){this.setPolygonList(t),e&&this.setSelectedID(e),this.render()}setResult(t){this.clearActiveStatus(),this.setPolygonList(t),this.render()}setPolygonDataByID(t,e){return this.polygonList.map(i=>i.id===e?y(y({},i),t):i)}rotatePolygon(t=1,e=K.Clockwise,i=this.selectedID){if(!i)return;const s=u.getPolygonByID(this.polygonList,i);if(!s)return;const o=u.updatePolygonByRotate(e,t,s==null?void 0:s.pointList);this.setPolygonList(this.setPolygonDataByID({pointList:o},i)),this.render()}rotatePolygonEdge(t,e=this.selectedID){!e||!u.getPolygonByID(this.polygonList,e)||(this.setPolygonList(this.setPolygonDataByID({pointList:t},e)),this.render())}addPointInDrawing(t){if(!this.imgInfo||this.forbidAddNewPolygonFuc&&this.forbidAddNewPolygonFuc(t))return;const{upperLimitPointNum:e,edgeAdsorption:i}=this.config;if(e&&this.drawingPointList.length>=e){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${e}`);return}this.deleteSelectedID();const s=this.getCoordinateUnderZoom(t),o=a.changeDrawOutsideTarget(s,{x:0,y:0},this.imgInfo,this.config.drawOutsideTarget,this.basicResult,this.zoom);if(a.returnClosePointIndex(o,a.changePointListByZoom(this.drawingPointList,this.zoom))===0){this.addDrawingPointToPolygonList();return}const{dropFoot:n}=u.getClosestPoint(o,this.polygonListUnderZoom,this.config.lineType,R),r=a.changePointByZoom(n&&t.altKey===!1&&i?n:o,1/this.zoom);if(this.pattern===x.Rect&&this.drawingPointList.length===2){const h=b.getRectangleByRightAngle(r,this.drawingPointList);if(this.drawingPointList=h,this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(this.drawingPointList)){this.emit("messageInfo",`${w.getMessagesByLocale(C.ForbiddenCreationOutsideBoundary,this.lang)}`),this.drawingPointList=[];return}this.addDrawingPointToPolygonList(!0);return}this.drawingPointList.push(r),this.drawingPointList.length===1?this.drawingHistory.initRecord(this.drawingPointList):this.drawingHistory.pushHistory(this.drawingPointList)}clearResult(){this.setPolygonList([]),this.deleteSelectedID(),this.render()}clearPolygonDrag(){this.drawingPointList=[],this.stopDrag(),this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,this.hoverID=""}clearActiveStatus(){this.clearPolygonDrag(),this.deleteSelectedID()}clearDrawingStatus(){this.drawingPointList=[]}setPolygonList(t){const e=this.polygonList.length;this.polygonList=t,e!==t.length&&this.emit("updatePageNumber")}setSelectedID(t,e=!1){this.selection.setSelectedIDs(t,e),this.render(),this.emit("selectedChange")}setDefaultAttribute(t=""){const e=this.defaultAttribute;if(this.defaultAttribute=t,e!==t&&(this.changeStyle(t),this.emit("changeAttributeSidebar"),this.selectedPolygons.length>0&&(this.selectedPolygons.forEach(i=>{i.attribute=t}),this.history.pushHistory(this.polygonList),this.render()),this._textAttributeInstance)){if(this.attributeLockList.length>0&&!this.attributeLockList.includes(t)){this._textAttributeInstance.clearTextAttribute();return}this._textAttributeInstance.updateIcon(this.getTextIconSvg(t))}}setStyle(t){var e;super.setStyle(t),this._textAttributeInstance&&this.config.attributeConfigurable===!1&&((e=this._textAttributeInstance)==null||e.updateIcon(this.getTextIconSvg()))}setPolygonValidAndRender(t,e){if(!t)return;const i=this.polygonList.map(s=>{var o;if(s.id===t){const l=(o=s==null?void 0:s.valid)!=null?o:!0;return m(y({},s),{valid:e!=null?e:!l})}return s});this.setPolygonList(i),this.history.pushHistory(this.polygonList),this.render(),this.emit("updateResult")}addDrawingPointToPolygonList(t){let{lowerLimitPointNum:e=3}=this.config;e<3&&(e=3);let i;if(this.drawingPointList.length<e){this.drawingPointList=[],this.editPolygonID="";return}if(u.calcPolygonSize(this.drawingPointList||[])<this.minArea){this.emit("messageInfo",`${w.getMessagesByLocale(C.MinAreaLimitErrorNotice,this.lang)}${this.minArea}`);return}const s=f.getSourceID(this.basicResult),o=[...this.polygonList];if(this.editPolygonID){const l=o.find(n=>n.id===this.editPolygonID);if(!l)return;l.pointList=this.drawingPointList,this.editPolygonID=""}else{const l=M(8,62);let n={id:l,sourceID:s,valid:!this.isCtrl,textAttribute:"",pointList:this.drawingPointList,attribute:this.defaultAttribute,order:f.getMaxOrder(o.filter(r=>f.isSameSourceID(r.sourceID,s)))+1};if(this.config.textConfigurable){let r="";r=D.getTextAttribute(this.polygonList.filter(h=>f.isSameSourceID(h.sourceID,s)),this.config.textCheckType),n=m(y({},n),{textAttribute:r})}this.pattern===x.Rect&&t===!0&&(n=m(y({},n),{isRect:!0})),o.push(n),i=n,this.setSelectedIdAfterAddingDrawing(l)}this.setPolygonList(o),this.isCtrl=!1,this.drawingPointList=[],this.history.pushHistory(o),i&&this.emit("polygonCreated",i,this.zoom,this.currentPos)}setSelectedIdAfterAddingDrawing(t){this.drawingPointList.length!==0&&(this.config.textConfigurable?this.setSelectedID(t):this.deleteSelectedID())}getHoverID(t){var e;const i=this.getCoordinateUnderZoom(t),s=this.currentShowList.map(o=>m(y({},o),{pointList:a.changePointListByZoom(o.pointList,this.zoom)}));return u.getHoverPolygonID(i,s,10,(e=this.config)==null?void 0:e.lineType)}getHoverEdgeIndex(t){var e;if(!this.selectedID)return-1;const i=this.selectedPolygon;if(!i)return-1;const s=this.getCoordinateUnderZoom(t),o=a.changePointListByZoom(i.pointList,this.zoom);return u.getHoverEdgeIndex(s,o,(e=this.config)==null?void 0:e.lineType)}getHoverPointIndex(t){if(!this.selectedID)return-1;const e=this.selectedPolygon;if(!e)return-1;const i=this.getCoordinateUnderZoom(t),s=a.changePointListByZoom(e.pointList,this.zoom);return a.returnClosePointIndex(i,s)}deletePolygons(t){var e;if(!t||t.length===0)return;const i=this.polygonList.filter(s=>this.selection.isIdSelected(s.id));this.emit("deletedObject",{deletedObject:i[0],id:i[0].id}),this.setPolygonList(this.polygonList.filter(s=>!this.selection.isIdSelected(s.id))),this.history.pushHistory(this.polygonList),(e=this._textAttributeInstance)==null||e.clearTextAttribute(),this.emit("selectedChange"),this.render()}deletePolygonPoint(t){if(!this.selectedID)return;const{selectedPolygon:e}=this;if(!e)return;let{lowerLimitPointNum:i}=this.config;if(i<3&&(i=3),e.pointList.length<=i){this.emit("messageInfo",`${w.getMessagesByLocale(C.LowerLimitErrorNotice,this.lang)}${i}`);return}e==null||e.pointList.splice(t,1),this.history.pushHistory(this.polygonList),this.render()}spaceKeydown(){var t,e,i;if(this.selectedID){if(((t=this.selectedPolygon)==null?void 0:t.isRect)===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.UnableToReannotation,this.lang)}`);return}this.editPolygonID=this.selectedID,this.drawingPointList=(i=(e=this.selectedPolygon)==null?void 0:e.pointList)!=null?i:[],this.drawingHistory.empty(),this.drawingHistory.initRecord(this.drawingPointList),this.hoverID="",this.deleteSelectedID(),this.render()}}onTabKeyDown(t){t.preventDefault();let e=U.ascend;t.shiftKey&&(e=U.descend),this.switchToNextPolygon(e)}switchToNextPolygon(t){if(this.drawingPointList.length>0)return;const[e,i]=f.getRenderResultList(this.polygonList,f.getSourceID(this.basicResult),this.attributeLockList,this.selectedIDs);let s=[...e];i&&(s=[...s,...i]);const o=B.getViewPort(this.canvas,this.currentPos,this.zoom),l=s.map(r=>{var h,g,c,d;return m(y({},r),{x:(g=(h=r.pointList[0])==null?void 0:h.x)!=null?g:0,y:(d=(c=r.pointList[0])==null?void 0:c.y)!=null?d:0})}).filter(r=>B.inViewPort({x:r.x,y:r.y},o)),n=f.getNextSelectedRectID(l,t,this.selectedID);if(n){this.setSelectedID(n.id);const{selectedPolygon:r}=this;r&&this.setDefaultAttribute(r.attribute)}this.render()}onKeyDown(t){if(!f.hotkeyFilter(t)||super.onKeyDown(t)===!1||this.selection.triggerKeyboardEvent(t,this.setPolygonList.bind(this)))return;const{keyCode:e}=t;switch(e){case L.Space:this.spaceKeydown();break;case L.Esc:this.drawingPointList=[],this.editPolygonID="";break;case L.F:this.selectedID&&this.setPolygonValidAndRender(this.selectedID);break;case L.Z:if(t.altKey){this.onCombinedExecute();return}this.setIsHidden(!this.isHidden),this.render();break;case L.Delete:this.deletePolygons(this.selectedIDs),this.render();break;case L.Ctrl:this.isCtrl=!0;break;case L.Alt:this.isAlt===!1&&(t.preventDefault(),this.isAlt=!0,this.render());break;case L.Tab:{this.onTabKeyDown(t);break}case L.X:t.altKey&&this.segment();break;default:{if(this.config.attributeConfigurable){const i=D.getAttributeByKeycode(e,this.config.attributeList);i!==void 0&&this.setDefaultAttribute(i)}break}}}onKeyUp(t){switch(super.onKeyUp(t),t.keyCode){case L.Ctrl:this.isCtrl=!1;break;case L.Alt:{const e=this.isAlt;this.isAlt=!1,e===!0&&this.render();break}}}rightMouseUp(t){if(this.drawingPointList.length>0){this.addDrawingPointToPolygonList();return}this.setSelectedID(this.hoverID,t.ctrlKey);const{selectedPolygon:e}=this;e&&this.setDefaultAttribute(e.attribute)}onLeftDblClick(t){if(this.hoverEdgeIndex>-1){const e=this.getCoordinateUnderZoom(t),{selectedPolygon:i}=this;if(!i)return;if(i.isRect===!0){this.emit("messageInfo",`${w.getMessagesByLocale(C.ForbidAddNewPoint,this.lang)}`),this.clearPolygonDrag();return}const{dropFoot:s}=u.getClosestPoint(e,this.polygonListUnderZoom,this.config.lineType,R);if(!s)return;const{upperLimitPointNum:o}=this.config;if(o&&i.pointList.length>=o){this.emit("messageInfo",`${w.getMessagesByLocale(C.UpperLimitErrorNotice,this.lang)}${o}`),this.clearPolygonDrag();return}i==null||i.pointList.splice(this.hoverEdgeIndex+1,0,a.changePointByZoom(s,1/this.zoom)),this.setPolygonDataByID(i,this.selectedID),this.history.pushHistory(this.polygonList),this.hoverPointIndex=-1,this.hoverEdgeIndex=-1,this.render()}this.dragInfo=void 0}onRightDblClick(t){this.dragInfo=void 0,this.clearImgDrag();const e=this.getHoverID(t),i=this.getHoverPointIndex(t);if(this.hoverPointIndex>-1&&this.hoverPointIndex===i){this.deletePolygonPoint(i),this.dragInfo=void 0,this.hoverPointIndex=-1,this.render();return}this.hoverID===this.selectedID&&this.deletePolygons([e]),this.render()}onMouseDown(t){if(!(super.onMouseDown(t)||this.forbidMouseOperation||t.ctrlKey===!0))return this.dragMouseDown(t),!0}onMouseLeave(){this.dragInfo&&(this.emitUpdatePolygonByDrag(),this.clearPolygonDrag(),this.history.pushHistory(this.polygonList))}dragMouseDown(t){if(t.button!==0)return;const e=this.getHoverID(t);if(!this.selection.isIdSelected(e))return;let i=[0],s=v.Plane;const o=this.selectedPolygons[0].pointList,l=this.getCoordinateUnderZoom(t);if(this.selectedPolygons.length===1){this.dragStatus=S.Start;const n=this.getHoverPointIndex(t),r=this.getHoverEdgeIndex(t);n>-1?(s=v.Point,i=[n]):r>-1&&this.hoverEdgeIndex>-1&&(s=v.Line,i=[r,(r+1)%o.length])}this.dragInfo={dragStartCoord:l,dragTarget:s,initPointList:o,changePointIndex:i,originPolygon:this.selectedPolygon,selectedPolygons:this.selectedPolygons,dragPrevCoord:l,originPolygonList:this.polygonList}}segment(){var t;if(!this.selectedID||((t=this.config)==null?void 0:t.lineType)!==O.Line)return;const e=u.getPolygonPointList(this.selectedID,this.currentShowList),i=this.currentShowList.filter(l=>l.id!==this.selectedID);if(i.length===0||e.length===0)return;const s=u.getWrapPolygonIndex(e,i);let o=[...this.polygonList];if(s===-1){const l=u.segmentPolygonByPolygon(e,i);if(!l)return;const n=l.shift();if(!n)return;let r="",h=!0;const g=f.getSourceID(this.basicResult);let c="";o=this.polygonList.map(d=>{var p,T;return d.id===this.selectedID?(r=d.attribute,h=(p=d==null?void 0:d.valid)!=null?p:!0,c=(T=d==null?void 0:d.textAttribute)!=null?T:"",m(y({},d),{pointList:n})):d}),l.length>0&&l.forEach((d,p)=>{o.push({sourceID:g,id:M(8,62),pointList:d,valid:h,order:f.getMaxOrder(this.currentShowList)+1+p,attribute:r,textAttribute:c})})}else o[s].pointList=u.clipPolygonFromWrapPolygon(e,o[s].pointList),o=o.filter(l=>l.id!==this.selectedID);this.setPolygonList(o),this.history.pushHistory(o),this.render()}onCombinedExecute(){if(!this.selectedID){this.emit("messageInfo",_.t("PolygonsToBeCombinedNeedToBeSelected"));return}this.isCombined=!this.isCombined}combine(t){var e;const i=this.getHoverID(t);if(!i||!this.selectedID||this.selectedID===i)return;if(((e=this.config)==null?void 0:e.lineType)!==O.Line){this.emit("messageInfo",_.t("CurveModeDoesNotSupportCutting"));return}const s=this.polygonList.find(h=>h.id===this.selectedID),o=this.currentShowList.find(h=>h.id===i);if(!o||!s)return;const l=u.combinePolygonWithPolygon(s,o);if(!l)return;const{newPolygon:n,unionList:r}=l;if(r.length===1&&n){const h=this.polygonList.filter(g=>!r.includes(g.id)).map(g=>g.id===this.selectedID?n:g);this.setPolygonList(h),this.history.pushHistory(h),this.render(),this.emit("messageInfo",_.t("CombineSuccess"))}else this.emit("messageInfo",_.t("CombiningFailedNotify"));this.isCombined=!1}isPolygonOutSide(t){if(this.dependToolName&&this.basicCanvas&&this.basicResult){let n=!1;switch(this.dependToolName){case H.Rect:{n=t.filter(r=>!N.isInRect(r,this.basicResult)).length>0;break}case H.Polygon:{n=u.isPointListOutSidePolygon(t,this.basicResult.pointList,this.config.lineType);break}}return n}if(!this.imgInfo)return!1;const{left:e,top:i,right:s,bottom:o}=b.calcViewportBoundaries(a.changePointListByZoom(t,this.zoom)),l=1e-5;return e<0||i<0||s>this.imgInfo.width+l||o>this.imgInfo.height+l}onDragMove(t){const e=this.polygonList.map(i=>{if(this.selectedIDs.includes(i.id)){const s=this.dragPolygon(t,i);if(!s)return i;const o=m(y({},i),{pointList:s});return i.isRect===!0&&this.pattern===x.Normal&&Object.assign(o,{isRect:!1}),o}return i});this.dragInfo.dragPrevCoord=this.getCoordinateUnderZoom(t),this.setPolygonList(e),this.render()}getDragOffset(t,e){var i;const s=this.getCoordinateUnderZoom(t),{dragTarget:o,dragPrevCoord:l,changePointIndex:n,initPointList:r,dragStartCoord:h}=this.dragInfo;if(this.pattern===x.Rect&&(e==null?void 0:e.isRect)===!0&&n&&[v.Line].includes(o)){const g=b.getArrayIndex(n[0]-2,4),c=b.getArrayIndex(n[0]-1,4),d=[r[g],r[c]],p=b.getRectPerpendicularOffset(h,s,d);return{x:p.x/this.zoom,y:p.y/this.zoom}}return((i=this.dragInfo)==null?void 0:i.dragTarget)===v.Plane?{x:(s.x-l.x)/this.zoom,y:(s.y-l.y)/this.zoom}:{x:(s.x-h.x)/this.zoom,y:(s.y-h.y)/this.zoom}}dragPolygon(t,e){let i=j.cloneDeep(e==null?void 0:e.pointList);if(!i||!this.dragInfo)return;const{initPointList:s,dragTarget:o,changePointIndex:l}=this.dragInfo,n=this.getDragOffset(t,e);switch(this.dragStatus=S.Move,o){case v.Plane:i=i.map(r=>m(y({},r),{x:r.x+n.x,y:r.y+n.y}));break;case v.Point:case v.Line:i=i.map((r,h)=>l&&l.includes(h)?m(y({},r),{x:s[h].x+n.x,y:s[h].y+n.y}):r);break}if(this.pattern===x.Rect&&(e==null?void 0:e.isRect)===!0&&o===v.Point&&l&&(i=b.getPointListFromPointOffset(s,l[0],n)),!(this.config.drawOutsideTarget===!1&&this.imgInfo&&this.isPolygonOutSide(i)))return i}onMouseMove(t){if(super.onMouseMove(t)||this.forbidMouseOperation||!this.imgInfo)return;let e=-1,i=-1;const{selectedID:s}=this;if(s&&(this.hoverEdgeIndex=-1,this.hoverPointIndex=-1,e=this.getHoverPointIndex(t),e>-1?this.hoverPointIndex=e:(i=this.getHoverEdgeIndex(t),this.hoverEdgeIndex=i)),this.drawingPointList.length>0)return;const o=this.getHoverID(t);this.hoverID!==o&&(this.hoverID=o,this.render()),this.selectedIDs.length>0&&this.dragInfo&&this.onDragMove(t)}leftMouseUpdateValid(t){const e=this.getHoverID(t);return this.drawingPointList.length===0&&t.ctrlKey===!0&&e?(this.setPolygonValidAndRender(e),!0):!1}stopDrag(){this.dragInfo=void 0,this.dragStatus=S.Wait}leftMouseUp(t){this.leftMouseUpdateValid(t)||this.addPointInDrawing(t)}onMouseUp(t){if(this.isCombined){switch(t.button){case 0:this.combine(t);break;case 2:this.isCombined=!1;break;default:return}return}if(!(super.onMouseUp(t)||this.forbidMouseOperation||!this.imgInfo)){if(this.dragInfo&&this.dragStatus===S.Move){this.emit("updateResult"),this.emitUpdatePolygonByDrag(),this.stopDrag(),this.history.pushHistory(this.polygonList);return}if(this.dragInfo&&this.dragStatus===S.Start){this.stopDrag();return}switch(t.button){case 0:{this.leftMouseUp(t);break}case 2:{this.rightMouseUp(t);break}}this.render()}}dragMouseUp(){this.dragStatus===S.Start&&this.stopDrag()}exportData(){const{polygonList:t}=this;return[t,this.basicImgInfo]}getTextIconSvg(t=""){return D.getTextIconSvg(t,this.config.attributeList,this.config.attributeConfigurable,this.baseIcon)}getCurrentSelectedData(){const{selectedPolygon: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:z,textAttribute:t.textAttribute,color:i}}updateSelectedTextAttribute(t){if(this._textAttributeInstance&&t&&this.selectedID){let e=t;D.textAttributeValidate(this.config.textCheckType,"",e)===!1&&(this.emit("messageError",D.getErrorNotice(this.config.textCheckType,this.lang)),e=""),this.setPolygonList(D.textChange(e,this.selectedID,this.polygonList)),this.emit("updateTextAttribute"),this.render()}}renderTextAttribute(){var t;const{selectedPolygon:e}=this;if(!this.ctx||this.config.textConfigurable===!1||!e)return;const{pointList:i,attribute:s,valid:o,textAttribute:l}=e,{x:n,y:r}=i[i.length-1],h=z,g=a.getOffsetCoordinate({x:n,y:r},this.currentPos,this.zoom),c=this.getColor(s),d=o?c==null?void 0:c.valid.stroke:c==null?void 0:c.invalid.stroke;this._textAttributeInstance||(this._textAttributeInstance=new V({width:h,container:this.container,icon:this.getTextIconSvg(s),color:d,getCurrentSelectedData:this.getCurrentSelectedData,updateSelectedTextAttribute:this.updateSelectedTextAttribute})),this._textAttributeInstance&&!((t=this._textAttributeInstance)==null?void 0:t.isExit)&&this._textAttributeInstance.appendToContainer(),this._textAttributeInstance.update(`${l}`,{left:g.x,top:g.y,color:d,width:h})}renderStaticPolygon(){var t;this.isHidden===!1&&((t=this.visiblePolygonList)==null||t.forEach(e=>{var i,s,o,l,n;if([this.selectedID,this.editPolygonID].includes(e.id))return;const{textAttribute:r,attribute:h}=e,g=this.getColor(h),c=A.getStrokeAndFill(g,e.valid),d=a.changePointListByZoom(e.pointList||[],this.zoom,this.currentPos);P.drawPolygonWithFillAndLine(this.canvas,d,{fillColor:c.fill,strokeColor:c.stroke,pointColor:"white",thickness:(s=(i=this.style)==null?void 0:i.width)!=null?s:2,lineCap:"round",isClose:!0,lineType:(o=this.config)==null?void 0:o.lineType});let p=`${(l=D.getAttributeShowText(h,this.config.attributeList))!=null?l:""}`;((n=this.config)==null?void 0:n.isShowOrder)&&(e==null?void 0:e.order)>0&&(p=`${e.order} ${p}`),P.drawText(this.canvas,d[0],p,y({color:c.stroke},k));const T=d[d.length-1];P.drawText(this.canvas,{x:T.x+E.x,y:T.y+E.y},r,y({color:c.stroke},k))}))}renderSelectedPolygons(){this.selectedPolygons.forEach(t=>{this.renderSelectedPolygon(t)})}renderSelectedPolygon(t){var e,i,s;if(t){const o=this.getColor(t.attribute),l=A.getStrokeAndFill(o,t.valid,{isSelected:!0});P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(t.pointList,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!0,lineType:(e=this.config)==null?void 0:e.lineType});let n=`${(i=D.getAttributeShowText(t.attribute,this.config.attributeList))!=null?i:""}`;((s=this.config)==null?void 0:s.isShowOrder)&&(t==null?void 0:t.order)>0&&(n=`${t.order} ${n}`),P.drawText(this.canvas,a.changePointByZoom(t.pointList[0],this.zoom,this.currentPos),n,y({color:l.stroke},k)),this.renderTextAttribute()}}renderHoverPolygon(){var t;if(this.hoverID&&this.hoverID!==this.editPolygonID){const{hoverPolygon:e}=this;if(e){let i="";const s=this.getColor(e.attribute);e.valid?i=s.validHover.fill:i=A.getStrokeAndFill(s,!1,{isHover:!0}).fill,P.drawPolygonWithFill(this.canvas,a.changePointListByZoom(e.pointList,this.zoom,this.currentPos),{color:i,lineType:(t=this.config)==null?void 0:t.lineType})}}}renderPolygon(){var t,e,i,s;this.renderStaticPolygon(),this.renderHoverPolygon(),this.renderSelectedPolygons();const o=this.getColor(this.defaultAttribute),l=A.getStrokeAndFill(o,!this.isCtrl);if(((t=this.drawingPointList)==null?void 0:t.length)>0){let n=[...this.drawingPointList],r=a.getOriginCoordinateWithOffsetCoordinate(this.coord,this.zoom,this.currentPos);if(this.pattern===x.Rect&&n.length===2)n=b.getRectangleByRightAngle(r,n);else{if(((e=this.config)==null?void 0:e.edgeAdsorption)&&this.isAlt===!1){const{dropFoot:h}=u.getClosestPoint(r,this.polygonList,(i=this.config)==null?void 0:i.lineType,R/this.zoom);h&&(r=h)}n.push(r)}P.drawSelectedPolygonWithFillAndLine(this.canvas,a.changePointListByZoom(n,this.zoom,this.currentPos),{fillColor:l.fill,strokeColor:l.stroke,pointColor:"white",thickness:2,lineCap:"round",isClose:!1,lineType:this.config.lineType})}if(this.hoverPointIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0}),h=n==null?void 0:n.pointList[this.hoverPointIndex];if(h){const{x:g,y:c}=a.changePointByZoom(h,this.zoom,this.currentPos);P.drawCircleWithFill(this.canvas,{x:g,y:c},5,{color:r.fill})}}if(this.hoverEdgeIndex>-1&&this.selectedID){const n=this.selectedPolygon;if(!n)return;const r=A.getStrokeAndFill(o,n.valid,{isSelected:!0});P.drawLineWithPointList(this.canvas,a.changePointListByZoom(n.pointList,this.zoom,this.currentPos),{color:r.stroke,thickness:10,hoverEdgeIndex:this.hoverEdgeIndex,lineType:(s=this.config)==null?void 0:s.lineType})}}render(){!this.ctx||(super.render(),this.renderPolygon(),this.renderCursorLine(this.getLineColor(this.defaultAttribute)))}renderCursorLine(t){if(super.renderCursorLine(t),this.isCombined){const{x:e,y:i}=this.coord,s=10,o=186,l=32;P.drawRectWithFill(this.canvas,{x:e+s,y:i-s*4-1,width:o,height:l},{color:"black"}),P.drawText(this.canvas,{x:e,y:i},_.t("ClickAnotherPolygon"),{textAlign:"center",color:"white",offsetX:o/2+s,offsetY:-(l/2+s/2)}),P.drawRect(this.canvas,{x:e-s,y:i-s,width:s*2,height:s*2},{lineDash:[6],color:"white"})}}undo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.undo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.undo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}redo(){if(this.drawingPointList.length>0){const e=this.drawingHistory.redo();if(!e)return;this.drawingPointList=e,this.render();return}const t=this.history.redo();t&&(t.length!==this.polygonList.length&&this.deleteSelectedID(),this.setPolygonList(t),this.render())}deleteSelectedID(){this.setSelectedID("")}}export{tt as default};