@cornerstonejs/tools 0.56.1 → 0.56.3

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 (360) hide show
  1. package/dist/cjs/tools/CrosshairsTool.d.ts +1 -0
  2. package/dist/cjs/tools/CrosshairsTool.js +4 -1
  3. package/dist/cjs/tools/CrosshairsTool.js.map +1 -1
  4. package/dist/esm/tools/CrosshairsTool.d.ts +1 -0
  5. package/dist/esm/tools/CrosshairsTool.js +4 -1
  6. package/dist/esm/tools/CrosshairsTool.js.map +1 -1
  7. package/dist/umd/index.js +1 -1
  8. package/dist/umd/index.js.map +1 -1
  9. package/package.json +5 -4
  10. package/src/constants/COLOR_LUT.ts +262 -0
  11. package/src/constants/index.ts +3 -0
  12. package/src/cursors/ImageMouseCursor.ts +39 -0
  13. package/src/cursors/MouseCursor.ts +114 -0
  14. package/src/cursors/SVGCursorDescriptor.ts +462 -0
  15. package/src/cursors/SVGMouseCursor.ts +145 -0
  16. package/src/cursors/elementCursor.ts +69 -0
  17. package/src/cursors/index.ts +24 -0
  18. package/src/cursors/setCursorForElement.ts +33 -0
  19. package/src/drawingSvg/_getHash.ts +9 -0
  20. package/src/drawingSvg/_setAttributesIfNecessary.ts +13 -0
  21. package/src/drawingSvg/_setNewAttributesIfValid.ts +10 -0
  22. package/src/drawingSvg/clearByToolType.ts +26 -0
  23. package/src/drawingSvg/draw.ts +16 -0
  24. package/src/drawingSvg/drawArrow.ts +82 -0
  25. package/src/drawingSvg/drawCircle.ts +62 -0
  26. package/src/drawingSvg/drawEllipse.ts +71 -0
  27. package/src/drawingSvg/drawHandles.ts +87 -0
  28. package/src/drawingSvg/drawLine.ts +70 -0
  29. package/src/drawingSvg/drawLink.ts +76 -0
  30. package/src/drawingSvg/drawLinkedTextBox.ts +64 -0
  31. package/src/drawingSvg/drawPolyline.ts +80 -0
  32. package/src/drawingSvg/drawRect.ts +70 -0
  33. package/src/drawingSvg/drawTextBox.ts +213 -0
  34. package/src/drawingSvg/getSvgDrawingHelper.ts +98 -0
  35. package/src/drawingSvg/index.ts +23 -0
  36. package/src/enums/AnnotationStyleStates.ts +22 -0
  37. package/src/enums/Events.ts +242 -0
  38. package/src/enums/SegmentationRepresentations.ts +12 -0
  39. package/src/enums/ToolBindings.ts +37 -0
  40. package/src/enums/ToolModes.ts +31 -0
  41. package/src/enums/Touch.ts +8 -0
  42. package/src/enums/index.js +16 -0
  43. package/src/eventDispatchers/annotationModifiedEventDispatcher.ts +41 -0
  44. package/src/eventDispatchers/cameraModifiedEventDispatcher.ts +41 -0
  45. package/src/eventDispatchers/imageRenderedEventDispatcher.ts +37 -0
  46. package/src/eventDispatchers/imageSpacingCalibratedEventDispatcher.ts +50 -0
  47. package/src/eventDispatchers/index.js +15 -0
  48. package/src/eventDispatchers/keyboardEventHandlers/index.js +4 -0
  49. package/src/eventDispatchers/keyboardEventHandlers/keyDown.ts +29 -0
  50. package/src/eventDispatchers/keyboardEventHandlers/keyUp.ts +33 -0
  51. package/src/eventDispatchers/keyboardToolEventDispatcher.ts +28 -0
  52. package/src/eventDispatchers/mouseEventHandlers/index.js +19 -0
  53. package/src/eventDispatchers/mouseEventHandlers/mouseClick.ts +13 -0
  54. package/src/eventDispatchers/mouseEventHandlers/mouseDoubleClick.ts +13 -0
  55. package/src/eventDispatchers/mouseEventHandlers/mouseDown.ts +196 -0
  56. package/src/eventDispatchers/mouseEventHandlers/mouseDownActivate.ts +35 -0
  57. package/src/eventDispatchers/mouseEventHandlers/mouseDrag.ts +25 -0
  58. package/src/eventDispatchers/mouseEventHandlers/mouseMove.ts +70 -0
  59. package/src/eventDispatchers/mouseEventHandlers/mouseUp.ts +9 -0
  60. package/src/eventDispatchers/mouseEventHandlers/mouseWheel.ts +13 -0
  61. package/src/eventDispatchers/mouseToolEventDispatcher.ts +64 -0
  62. package/src/eventDispatchers/shared/customCallbackHandler.ts +73 -0
  63. package/src/eventDispatchers/shared/getActiveToolForKeyboardEvent.ts +58 -0
  64. package/src/eventDispatchers/shared/getActiveToolForMouseEvent.ts +61 -0
  65. package/src/eventDispatchers/shared/getActiveToolForTouchEvent.ts +64 -0
  66. package/src/eventDispatchers/shared/getMouseModifier.ts +30 -0
  67. package/src/eventDispatchers/shared/getToolsWithModesForMouseEvent.ts +56 -0
  68. package/src/eventDispatchers/shared/getToolsWithModesForTouchEvent.ts +54 -0
  69. package/src/eventDispatchers/touchEventHandlers/index.js +15 -0
  70. package/src/eventDispatchers/touchEventHandlers/touchDrag.ts +23 -0
  71. package/src/eventDispatchers/touchEventHandlers/touchEnd.ts +9 -0
  72. package/src/eventDispatchers/touchEventHandlers/touchPress.ts +13 -0
  73. package/src/eventDispatchers/touchEventHandlers/touchStart.ts +174 -0
  74. package/src/eventDispatchers/touchEventHandlers/touchStartActivate.ts +36 -0
  75. package/src/eventDispatchers/touchEventHandlers/touchTap.ts +9 -0
  76. package/src/eventDispatchers/touchToolEventDispatcher.ts +51 -0
  77. package/src/eventListeners/annotations/annotationModifiedListener.ts +22 -0
  78. package/src/eventListeners/annotations/annotationSelectionListener.ts +29 -0
  79. package/src/eventListeners/annotations/index.ts +4 -0
  80. package/src/eventListeners/index.ts +28 -0
  81. package/src/eventListeners/keyboard/index.ts +16 -0
  82. package/src/eventListeners/keyboard/keyDownListener.ts +99 -0
  83. package/src/eventListeners/mouse/getMouseEventPoints.ts +66 -0
  84. package/src/eventListeners/mouse/index.ts +55 -0
  85. package/src/eventListeners/mouse/mouseDoubleClickListener.ts +55 -0
  86. package/src/eventListeners/mouse/mouseDownListener.ts +519 -0
  87. package/src/eventListeners/mouse/mouseMoveListener.ts +33 -0
  88. package/src/eventListeners/segmentation/index.ts +11 -0
  89. package/src/eventListeners/segmentation/segmentationDataModifiedEventListener.ts +61 -0
  90. package/src/eventListeners/segmentation/segmentationModifiedEventListener.ts +32 -0
  91. package/src/eventListeners/segmentation/segmentationRepresentationModifiedEventListener.ts +15 -0
  92. package/src/eventListeners/segmentation/segmentationRepresentationRemovedEventListener.ts +16 -0
  93. package/src/eventListeners/touch/getTouchEventPoints.ts +75 -0
  94. package/src/eventListeners/touch/index.ts +37 -0
  95. package/src/eventListeners/touch/preventGhostClick.js +72 -0
  96. package/src/eventListeners/touch/touchStartListener.ts +499 -0
  97. package/src/eventListeners/wheel/index.ts +27 -0
  98. package/src/eventListeners/wheel/normalizeWheel.ts +69 -0
  99. package/src/eventListeners/wheel/wheelListener.ts +51 -0
  100. package/src/index.ts +133 -0
  101. package/src/init.ts +187 -0
  102. package/src/stateManagement/annotation/FrameOfReferenceSpecificAnnotationManager.ts +399 -0
  103. package/src/stateManagement/annotation/annotationLocking.ts +178 -0
  104. package/src/stateManagement/annotation/annotationSelection.ts +163 -0
  105. package/src/stateManagement/annotation/annotationState.ts +180 -0
  106. package/src/stateManagement/annotation/annotationVisibility.ts +156 -0
  107. package/src/stateManagement/annotation/config/ToolStyle.ts +265 -0
  108. package/src/stateManagement/annotation/config/getFont.ts +36 -0
  109. package/src/stateManagement/annotation/config/getState.ts +26 -0
  110. package/src/stateManagement/annotation/config/helpers.ts +55 -0
  111. package/src/stateManagement/annotation/config/index.ts +5 -0
  112. package/src/stateManagement/annotation/helpers/state.ts +83 -0
  113. package/src/stateManagement/annotation/index.ts +15 -0
  114. package/src/stateManagement/index.js +40 -0
  115. package/src/stateManagement/segmentation/SegmentationStateManager.ts +491 -0
  116. package/src/stateManagement/segmentation/activeSegmentation.ts +60 -0
  117. package/src/stateManagement/segmentation/addSegmentationRepresentations.ts +77 -0
  118. package/src/stateManagement/segmentation/addSegmentations.ts +27 -0
  119. package/src/stateManagement/segmentation/config/index.ts +29 -0
  120. package/src/stateManagement/segmentation/config/segmentationColor.ts +132 -0
  121. package/src/stateManagement/segmentation/config/segmentationConfig.ts +195 -0
  122. package/src/stateManagement/segmentation/config/segmentationVisibility.ts +171 -0
  123. package/src/stateManagement/segmentation/helpers/index.ts +3 -0
  124. package/src/stateManagement/segmentation/helpers/normalizeSegmentationInput.ts +35 -0
  125. package/src/stateManagement/segmentation/helpers/validateSegmentationInput.ts +41 -0
  126. package/src/stateManagement/segmentation/index.ts +22 -0
  127. package/src/stateManagement/segmentation/removeSegmentationsFromToolGroup.ts +85 -0
  128. package/src/stateManagement/segmentation/segmentIndex.ts +38 -0
  129. package/src/stateManagement/segmentation/segmentLocking.ts +72 -0
  130. package/src/stateManagement/segmentation/segmentationState.ts +429 -0
  131. package/src/stateManagement/segmentation/triggerSegmentationEvents.ts +157 -0
  132. package/src/store/SynchronizerManager/Synchronizer.ts +344 -0
  133. package/src/store/SynchronizerManager/createSynchronizer.ts +41 -0
  134. package/src/store/SynchronizerManager/destroy.ts +14 -0
  135. package/src/store/SynchronizerManager/destroySynchronizer.ts +25 -0
  136. package/src/store/SynchronizerManager/getAllSynchronizers.ts +12 -0
  137. package/src/store/SynchronizerManager/getSynchronizer.ts +13 -0
  138. package/src/store/SynchronizerManager/getSynchronizersForViewport.ts +44 -0
  139. package/src/store/SynchronizerManager/index.js +15 -0
  140. package/src/store/ToolGroupManager/ToolGroup.ts +679 -0
  141. package/src/store/ToolGroupManager/createToolGroup.ts +33 -0
  142. package/src/store/ToolGroupManager/destroy.ts +24 -0
  143. package/src/store/ToolGroupManager/destroyToolGroup.ts +26 -0
  144. package/src/store/ToolGroupManager/getAllToolGroups.ts +12 -0
  145. package/src/store/ToolGroupManager/getToolGroup.ts +14 -0
  146. package/src/store/ToolGroupManager/getToolGroupForViewport.ts +44 -0
  147. package/src/store/ToolGroupManager/getToolGroupsWithToolName.ts +33 -0
  148. package/src/store/ToolGroupManager/index.ts +17 -0
  149. package/src/store/addEnabledElement.ts +137 -0
  150. package/src/store/addTool.ts +56 -0
  151. package/src/store/cancelActiveManipulations.ts +30 -0
  152. package/src/store/filterMoveableAnnotationTools.ts +61 -0
  153. package/src/store/filterToolsWithAnnotationsForElement.ts +51 -0
  154. package/src/store/filterToolsWithMoveableHandles.ts +51 -0
  155. package/src/store/index.ts +29 -0
  156. package/src/store/removeEnabledElement.ts +132 -0
  157. package/src/store/state.ts +57 -0
  158. package/src/store/svgNodeCache.ts +7 -0
  159. package/src/synchronizers/callbacks/areViewportsCoplanar .ts +12 -0
  160. package/src/synchronizers/callbacks/cameraSyncCallback.ts +33 -0
  161. package/src/synchronizers/callbacks/stackImageSyncCallback.ts +157 -0
  162. package/src/synchronizers/callbacks/voiSyncCallback.ts +51 -0
  163. package/src/synchronizers/callbacks/zoomPanSyncCallback.ts +43 -0
  164. package/src/synchronizers/index.ts +11 -0
  165. package/src/synchronizers/synchronizers/createCameraPositionSynchronizer.ts +25 -0
  166. package/src/synchronizers/synchronizers/createStackImageSynchronizer.ts +25 -0
  167. package/src/synchronizers/synchronizers/createVOISynchronizer.ts +24 -0
  168. package/src/synchronizers/synchronizers/createZoomPanSynchronizer.ts +25 -0
  169. package/src/synchronizers/synchronizers/index.ts +11 -0
  170. package/src/tools/CrosshairsTool.ts +2693 -0
  171. package/src/tools/MIPJumpToClickTool.ts +99 -0
  172. package/src/tools/MagnifyTool.ts +319 -0
  173. package/src/tools/PanTool.ts +58 -0
  174. package/src/tools/PlanarRotateTool.ts +77 -0
  175. package/src/tools/ReferenceCursors.ts +466 -0
  176. package/src/tools/ReferenceLinesTool.ts +279 -0
  177. package/src/tools/ScaleOverlayTool.ts +685 -0
  178. package/src/tools/StackScrollTool.ts +97 -0
  179. package/src/tools/StackScrollToolMouseWheelTool.ts +58 -0
  180. package/src/tools/TrackballRotateTool.ts +141 -0
  181. package/src/tools/VolumeRotateMouseWheelTool.ts +86 -0
  182. package/src/tools/WindowLevelTool.ts +260 -0
  183. package/src/tools/ZoomTool.ts +293 -0
  184. package/src/tools/annotation/AngleTool.ts +835 -0
  185. package/src/tools/annotation/ArrowAnnotateTool.ts +820 -0
  186. package/src/tools/annotation/BidirectionalTool.ts +1350 -0
  187. package/src/tools/annotation/CircleROITool.ts +1070 -0
  188. package/src/tools/annotation/CobbAngleTool.ts +815 -0
  189. package/src/tools/annotation/DragProbeTool.ts +213 -0
  190. package/src/tools/annotation/EllipticalROITool.ts +1223 -0
  191. package/src/tools/annotation/LengthTool.ts +861 -0
  192. package/src/tools/annotation/PlanarFreehandROITool.ts +636 -0
  193. package/src/tools/annotation/ProbeTool.ts +681 -0
  194. package/src/tools/annotation/RectangleROITool.ts +1028 -0
  195. package/src/tools/annotation/planarFreehandROITool/closedContourEditLoop.ts +488 -0
  196. package/src/tools/annotation/planarFreehandROITool/drawLoop.ts +462 -0
  197. package/src/tools/annotation/planarFreehandROITool/editLoopCommon.ts +331 -0
  198. package/src/tools/annotation/planarFreehandROITool/findOpenUShapedContourVectorToPeak.ts +74 -0
  199. package/src/tools/annotation/planarFreehandROITool/openContourEditLoop.ts +612 -0
  200. package/src/tools/annotation/planarFreehandROITool/openContourEndEditLoop.ts +74 -0
  201. package/src/tools/annotation/planarFreehandROITool/renderMethods.ts +407 -0
  202. package/src/tools/base/AnnotationDisplayTool.ts +228 -0
  203. package/src/tools/base/AnnotationTool.ts +307 -0
  204. package/src/tools/base/BaseTool.ts +215 -0
  205. package/src/tools/base/index.ts +4 -0
  206. package/src/tools/displayTools/Contour/addContourToElement.ts +135 -0
  207. package/src/tools/displayTools/Contour/contourDisplay.ts +252 -0
  208. package/src/tools/displayTools/Contour/index.ts +3 -0
  209. package/src/tools/displayTools/Contour/removeContourFromElement.ts +35 -0
  210. package/src/tools/displayTools/Labelmap/addLabelmapToElement.ts +57 -0
  211. package/src/tools/displayTools/Labelmap/index.ts +4 -0
  212. package/src/tools/displayTools/Labelmap/labelmapConfig.ts +37 -0
  213. package/src/tools/displayTools/Labelmap/labelmapDisplay.ts +461 -0
  214. package/src/tools/displayTools/Labelmap/removeLabelmapFromElement.ts +27 -0
  215. package/src/tools/displayTools/Labelmap/validateRepresentationData.ts +30 -0
  216. package/src/tools/displayTools/SegmentationDisplayTool.ts +198 -0
  217. package/src/tools/index.ts +84 -0
  218. package/src/tools/segmentation/BrushTool.ts +474 -0
  219. package/src/tools/segmentation/CircleScissorsTool.ts +365 -0
  220. package/src/tools/segmentation/PaintFillTool.ts +370 -0
  221. package/src/tools/segmentation/RectangleROIStartEndThresholdTool.ts +471 -0
  222. package/src/tools/segmentation/RectangleROIThresholdTool.ts +281 -0
  223. package/src/tools/segmentation/RectangleScissorsTool.ts +382 -0
  224. package/src/tools/segmentation/SphereScissorsTool.ts +368 -0
  225. package/src/tools/segmentation/strategies/eraseCircle.ts +30 -0
  226. package/src/tools/segmentation/strategies/eraseRectangle.ts +81 -0
  227. package/src/tools/segmentation/strategies/eraseSphere.ts +27 -0
  228. package/src/tools/segmentation/strategies/fillCircle.ts +185 -0
  229. package/src/tools/segmentation/strategies/fillRectangle.ts +110 -0
  230. package/src/tools/segmentation/strategies/fillSphere.ts +88 -0
  231. package/src/tools/segmentation/strategies/index.ts +9 -0
  232. package/src/types/AnnotationGroupSelector.ts +7 -0
  233. package/src/types/AnnotationStyle.ts +42 -0
  234. package/src/types/AnnotationTypes.ts +109 -0
  235. package/src/types/BoundsIJK.ts +5 -0
  236. package/src/types/CINETypes.ts +32 -0
  237. package/src/types/ContourTypes.ts +26 -0
  238. package/src/types/CursorTypes.ts +12 -0
  239. package/src/types/EventTypes.ts +657 -0
  240. package/src/types/FloodFillTypes.ts +19 -0
  241. package/src/types/IAnnotationManager.ts +89 -0
  242. package/src/types/IDistance.ts +16 -0
  243. package/src/types/IPoints.ts +18 -0
  244. package/src/types/ISetToolModeOptions.ts +29 -0
  245. package/src/types/ISynchronizerEventHandler.ts +11 -0
  246. package/src/types/IToolClassReference.ts +5 -0
  247. package/src/types/IToolGroup.ts +72 -0
  248. package/src/types/ITouchPoints.ts +14 -0
  249. package/src/types/InteractionTypes.ts +6 -0
  250. package/src/types/InternalToolTypes.ts +19 -0
  251. package/src/types/JumpToSliceOptions.ts +7 -0
  252. package/src/types/LabelmapTypes.ts +41 -0
  253. package/src/types/PlanarBoundingBox.ts +8 -0
  254. package/src/types/SVGDrawingHelper.ts +10 -0
  255. package/src/types/ScrollOptions.ts +9 -0
  256. package/src/types/SegmentationStateTypes.ts +248 -0
  257. package/src/types/ToolHandle.ts +26 -0
  258. package/src/types/ToolProps.ts +16 -0
  259. package/src/types/ToolSpecificAnnotationTypes.ts +311 -0
  260. package/src/types/index.ts +115 -0
  261. package/src/utilities/boundingBox/extend2DBoundingBoxInViewAxis.ts +29 -0
  262. package/src/utilities/boundingBox/getBoundingBoxAroundShape.ts +57 -0
  263. package/src/utilities/boundingBox/index.ts +4 -0
  264. package/src/utilities/calibrateImageSpacing.ts +46 -0
  265. package/src/utilities/cine/events.ts +9 -0
  266. package/src/utilities/cine/index.ts +5 -0
  267. package/src/utilities/cine/playClip.ts +435 -0
  268. package/src/utilities/cine/state.ts +18 -0
  269. package/src/utilities/clip.js +30 -0
  270. package/src/utilities/debounce.js +217 -0
  271. package/src/utilities/drawing/getTextBoxCoordsCanvas.ts +45 -0
  272. package/src/utilities/drawing/index.ts +3 -0
  273. package/src/utilities/dynamicVolume/getDataInTime.ts +110 -0
  274. package/src/utilities/dynamicVolume/index.ts +2 -0
  275. package/src/utilities/getAnnotationNearPoint.ts +130 -0
  276. package/src/utilities/getModalityUnit.ts +11 -0
  277. package/src/utilities/getToolsWithModesForElement.ts +52 -0
  278. package/src/utilities/index.ts +68 -0
  279. package/src/utilities/isObject.js +29 -0
  280. package/src/utilities/math/angle/angleBetweenLines.ts +29 -0
  281. package/src/utilities/math/circle/_types.ts +6 -0
  282. package/src/utilities/math/circle/getCanvasCircleCorners.ts +23 -0
  283. package/src/utilities/math/circle/getCanvasCircleRadius.ts +16 -0
  284. package/src/utilities/math/circle/index.ts +4 -0
  285. package/src/utilities/math/ellipse/getCanvasEllipseCorners.ts +26 -0
  286. package/src/utilities/math/ellipse/index.ts +4 -0
  287. package/src/utilities/math/ellipse/pointInEllipse.ts +38 -0
  288. package/src/utilities/math/ellipse/pointInEllipsoidWithConstraint.ts +35 -0
  289. package/src/utilities/math/index.ts +8 -0
  290. package/src/utilities/math/line/distanceToPoint.ts +24 -0
  291. package/src/utilities/math/line/distanceToPointSquared.ts +44 -0
  292. package/src/utilities/math/line/index.ts +5 -0
  293. package/src/utilities/math/line/intersectLine.ts +92 -0
  294. package/src/utilities/math/midPoint.ts +24 -0
  295. package/src/utilities/math/point/distanceToPoint.ts +22 -0
  296. package/src/utilities/math/point/index.ts +3 -0
  297. package/src/utilities/math/polyline/addCanvasPointsToArray.ts +62 -0
  298. package/src/utilities/math/polyline/calculateAreaOfPoints.ts +23 -0
  299. package/src/utilities/math/polyline/getIntersectionWithPolyline.ts +182 -0
  300. package/src/utilities/math/polyline/getSubPixelSpacingAndXYDirections.ts +99 -0
  301. package/src/utilities/math/polyline/index.ts +19 -0
  302. package/src/utilities/math/polyline/planarFreehandROIInternalTypes.ts +36 -0
  303. package/src/utilities/math/polyline/pointCanProjectOnLine.ts +57 -0
  304. package/src/utilities/math/polyline/pointsAreWithinCloseContourProximity.ts +15 -0
  305. package/src/utilities/math/rectangle/distanceToPoint.ts +82 -0
  306. package/src/utilities/math/rectangle/index.ts +3 -0
  307. package/src/utilities/math/sphere/index.ts +3 -0
  308. package/src/utilities/math/sphere/pointInSphere.ts +31 -0
  309. package/src/utilities/math/vec2/findClosestPoint.ts +40 -0
  310. package/src/utilities/math/vec2/index.ts +4 -0
  311. package/src/utilities/math/vec2/liangBarksyClip.ts +84 -0
  312. package/src/utilities/orientation/getOrientationStringLPS.ts +52 -0
  313. package/src/utilities/orientation/index.ts +4 -0
  314. package/src/utilities/orientation/invertOrientationStringLPS.ts +21 -0
  315. package/src/utilities/planar/filterAnnotationsForDisplay.ts +68 -0
  316. package/src/utilities/planar/filterAnnotationsWithinSlice.ts +85 -0
  317. package/src/utilities/planar/getPointInLineOfSightWithCriteria.ts +104 -0
  318. package/src/utilities/planar/getWorldWidthAndHeightFromCorners.ts +51 -0
  319. package/src/utilities/planar/getWorldWidthAndHeightFromTwoPoints.ts +51 -0
  320. package/src/utilities/planar/index.ts +18 -0
  321. package/src/utilities/planarFreehandROITool/index.ts +7 -0
  322. package/src/utilities/planarFreehandROITool/interpolateAnnotation.ts +87 -0
  323. package/src/utilities/planarFreehandROITool/interpolatePoints.ts +214 -0
  324. package/src/utilities/planarFreehandROITool/interpolation/algorithms/bspline.ts +55 -0
  325. package/src/utilities/planarFreehandROITool/interpolation/interpolateSegmentPoints.ts +90 -0
  326. package/src/utilities/pointInShapeCallback.ts +138 -0
  327. package/src/utilities/pointInSurroundingSphereCallback.ts +188 -0
  328. package/src/utilities/rectangleROITool/getBoundsIJKFromRectangleAnnotations.ts +76 -0
  329. package/src/utilities/rectangleROITool/index.ts +3 -0
  330. package/src/utilities/scroll.ts +62 -0
  331. package/src/utilities/segmentation/brushSizeForToolGroup.ts +72 -0
  332. package/src/utilities/segmentation/brushThresholdForToolGroup.ts +65 -0
  333. package/src/utilities/segmentation/createLabelmapVolumeForViewport.ts +74 -0
  334. package/src/utilities/segmentation/createMergedLabelmapForIndex.ts +65 -0
  335. package/src/utilities/segmentation/floodFill.ts +194 -0
  336. package/src/utilities/segmentation/getDefaultRepresentationConfig.ts +20 -0
  337. package/src/utilities/segmentation/index.ts +33 -0
  338. package/src/utilities/segmentation/isValidRepresentationConfig.ts +22 -0
  339. package/src/utilities/segmentation/rectangleROIThresholdVolumeByRange.ts +91 -0
  340. package/src/utilities/segmentation/thresholdSegmentationByRange.ts +129 -0
  341. package/src/utilities/segmentation/thresholdVolumeByRange.ts +150 -0
  342. package/src/utilities/segmentation/triggerSegmentationRender.ts +206 -0
  343. package/src/utilities/segmentation/utilities.ts +116 -0
  344. package/src/utilities/stackPrefetch/index.ts +8 -0
  345. package/src/utilities/stackPrefetch/stackPrefetch.ts +405 -0
  346. package/src/utilities/stackPrefetch/state.ts +17 -0
  347. package/src/utilities/throttle.js +69 -0
  348. package/src/utilities/touch/index.ts +246 -0
  349. package/src/utilities/triggerAnnotationRender.ts +237 -0
  350. package/src/utilities/triggerAnnotationRenderForViewportIds.ts +18 -0
  351. package/src/utilities/viewport/index.ts +5 -0
  352. package/src/utilities/viewport/isViewportPreScaled.ts +24 -0
  353. package/src/utilities/viewport/jumpToSlice.ts +73 -0
  354. package/src/utilities/viewport/jumpToWorld.ts +58 -0
  355. package/src/utilities/viewportFilters/filterViewportsWithFrameOfReferenceUID.ts +28 -0
  356. package/src/utilities/viewportFilters/filterViewportsWithParallelNormals.ts +26 -0
  357. package/src/utilities/viewportFilters/filterViewportsWithSameOrientation.ts +15 -0
  358. package/src/utilities/viewportFilters/filterViewportsWithToolEnabled.ts +72 -0
  359. package/src/utilities/viewportFilters/getViewportIdsWithToolToRender.ts +45 -0
  360. package/src/utilities/viewportFilters/index.ts +11 -0
@@ -0,0 +1,636 @@
1
+ import {
2
+ CONSTANTS,
3
+ getEnabledElement,
4
+ triggerEvent,
5
+ eventTarget,
6
+ StackViewport,
7
+ VolumeViewport,
8
+ utilities as csUtils,
9
+ } from '@cornerstonejs/core';
10
+ import type { Types } from '@cornerstonejs/core';
11
+ import { vec3 } from 'gl-matrix';
12
+ import { Events } from '../../enums';
13
+ import { AnnotationTool } from '../base';
14
+ import {
15
+ addAnnotation,
16
+ getAnnotations,
17
+ } from '../../stateManagement/annotation/annotationState';
18
+ import { polyline } from '../../utilities/math';
19
+ import { filterAnnotationsForDisplay } from '../../utilities/planar';
20
+ import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
21
+ import triggerAnnotationRenderForViewportIds from '../../utilities/triggerAnnotationRenderForViewportIds';
22
+ import registerDrawLoop from './planarFreehandROITool/drawLoop';
23
+ import registerEditLoopCommon from './planarFreehandROITool/editLoopCommon';
24
+ import registerClosedContourEditLoop from './planarFreehandROITool/closedContourEditLoop';
25
+ import registerOpenContourEditLoop from './planarFreehandROITool/openContourEditLoop';
26
+ import registerOpenContourEndEditLoop from './planarFreehandROITool/openContourEndEditLoop';
27
+ import registerRenderMethods from './planarFreehandROITool/renderMethods';
28
+ import {
29
+ AnnotationCompletedEventDetail,
30
+ AnnotationModifiedEventDetail,
31
+ } from '../../types/EventTypes';
32
+ import {
33
+ EventTypes,
34
+ ToolHandle,
35
+ Annotation,
36
+ Annotations,
37
+ PublicToolProps,
38
+ ToolProps,
39
+ InteractionTypes,
40
+ SVGDrawingHelper,
41
+ } from '../../types';
42
+ import { PlanarFreehandROIAnnotation } from '../../types/ToolSpecificAnnotationTypes';
43
+ import { PlanarFreehandROICommonData } from '../../utilities/math/polyline/planarFreehandROIInternalTypes';
44
+
45
+ const { pointCanProjectOnLine } = polyline;
46
+ const { EPSILON } = CONSTANTS;
47
+
48
+ const PARALLEL_THRESHOLD = 1 - EPSILON;
49
+ /**
50
+ * PlanarFreehandROITool lets you draw annotations that define an arbitrarily drawn region.
51
+ * You can use the PlanarFreehandROITool in all perpendicular views (axial, sagittal, coronal),
52
+ * support for oblique views is possible, but not yet supported, due to the implementation of
53
+ * `getSubPixelSpacingAndXYDirections`.
54
+ *
55
+ * The resulting annotation's data and metadata (the
56
+ * state of the viewport while drawing was happening) will get added to the
57
+ * ToolState manager and can be accessed from the ToolState by calling getAnnotations
58
+ * or similar methods.
59
+ *
60
+ * PlanarFreehandROITool annotation can be smoothed on drawing completion. This is a configured based approach.
61
+ * The interpolation process uses b-spline algorithm and consider 4 configurations properties:
62
+ * - interpolation.interpolateOnAdd: to tell whether it should be interpolated or not (for editing it is considered the property interpolateOnEdit) (default: false)
63
+ * - interpolation.interpolateOnEdit: to tell whether it should be interpolated or not when editing (default: false)
64
+ * - interpolation.knotsRatioPercentageOnAdd: percentage of points from Segment that are likely to be considered knots during interpolation (for editing it is considered the property knotsRatioPercentageOnEdit) ( default: 40)
65
+ * - interpolation.knotsRatioPercentageOnEdit: same as knotsRatioPercentageOnAdd but applicable only when editing the tool (default: 40)
66
+ *
67
+ * So, with that said the interpolation might occur when:
68
+ * - drawing is done (i.e mouse is released) and interpolation.interpolateOnAdd is true. Interpolation algorithm uses knotsRatioPercentageOnAdd
69
+ * - edit drawing is done (i.e mouse is released) and interpolation.interpolateOnEdit is true. Interpolation algorithm uses knotsRatioPercentageOnEdit and its only applied to changed segment
70
+ * Interpolation does not occur when:
71
+ * - interpolation.interpolateOnAdd is false and drawing is completed
72
+ * - interpolation.interpolateOnEdit is false and edit is completed
73
+ * - drawing still happening (editing or not)
74
+ *
75
+ * The result of interpolation will be a smoother set of segments.
76
+ * Changing tool configuration (see below) you can fine-tune the interpolation process by changing knotsRatioPercentageOnAdd and knotsRatioPercentageOnEdit value, which smaller values produces a more agressive interpolation.
77
+ * A smaller value of knotsRatioPercentageOnAdd/knotsRatioPercentageOnEdit produces a more agressive interpolation.
78
+ *
79
+ * ```js
80
+ * cornerstoneTools.addTool(PlanarFreehandROITool)
81
+ *
82
+ * const toolGroup = ToolGroupManager.createToolGroup('toolGroupId')
83
+ *
84
+ * toolGroup.addTool(PlanarFreehandROITool.toolName)
85
+ *
86
+ * toolGroup.addViewport('viewportId', 'renderingEngineId')
87
+ *
88
+ * toolGroup.setToolActive(PlanarFreehandROITool.toolName, {
89
+ * bindings: [
90
+ * {
91
+ * mouseButton: MouseBindings.Primary, // Left Click
92
+ * },
93
+ * ],
94
+ * })
95
+ *
96
+ * // set interpolation agressiveness while adding new annotation (ps: this does not change if interpolation is ON or OFF)
97
+ * toolGroup.setToolConfiguration(PlanarFreehandROITool.toolName, {
98
+ * interpolation: { knotsRatioPercentageOnAdd: 30 },
99
+ * });
100
+ *
101
+ * // set interpolation to be ON while editing only
102
+ * toolGroup.setToolConfiguration(PlanarFreehandROITool.toolName, {
103
+ * interpolation: { interpolateOnAdd: false, interpolateOnEdit: true },
104
+ * });
105
+ * ```
106
+ *
107
+ * Read more in the Docs section of the website.
108
+ */
109
+ class PlanarFreehandROITool extends AnnotationTool {
110
+ static toolName;
111
+
112
+ public touchDragCallback: any;
113
+ public mouseDragCallback: any;
114
+ _throttledCalculateCachedStats: any;
115
+ private commonData?: PlanarFreehandROICommonData;
116
+ isDrawing = false;
117
+ isEditingClosed = false;
118
+ isEditingOpen = false;
119
+
120
+ private activateDraw: (
121
+ evt: EventTypes.InteractionEventType,
122
+ annotation: PlanarFreehandROIAnnotation,
123
+ viewportIdsToRender: string[]
124
+ ) => void;
125
+ private activateClosedContourEdit: (
126
+ evt: EventTypes.InteractionEventType,
127
+ annotation: PlanarFreehandROIAnnotation,
128
+ viewportIdsToRender: string[]
129
+ ) => void;
130
+ private activateOpenContourEdit: (
131
+ evt: EventTypes.InteractionEventType,
132
+ annotation: PlanarFreehandROIAnnotation,
133
+ viewportIdsToRender: string[]
134
+ ) => void;
135
+ private activateOpenContourEndEdit: (
136
+ evt: EventTypes.InteractionEventType,
137
+ annotation: PlanarFreehandROIAnnotation,
138
+ viewportIdsToRender: string[]
139
+ ) => void;
140
+ private cancelDrawing: (element: HTMLDivElement) => void;
141
+ private cancelClosedContourEdit: (element: HTMLDivElement) => void;
142
+ private cancelOpenContourEdit: (element: HTMLDivElement) => void;
143
+
144
+ private renderContour: (
145
+ enabledElement: Types.IEnabledElement,
146
+ svgDrawingHelper: SVGDrawingHelper,
147
+ annotation: PlanarFreehandROIAnnotation
148
+ ) => void;
149
+ private renderContourBeingDrawn: (
150
+ enabledElement: Types.IEnabledElement,
151
+ svgDrawingHelper: SVGDrawingHelper,
152
+ annotation: PlanarFreehandROIAnnotation
153
+ ) => void;
154
+ private renderClosedContourBeingEdited: (
155
+ enabledElement: Types.IEnabledElement,
156
+ svgDrawingHelper: SVGDrawingHelper,
157
+ annotation: PlanarFreehandROIAnnotation
158
+ ) => void;
159
+ private renderOpenContourBeingEdited: (
160
+ enabledElement: Types.IEnabledElement,
161
+ svgDrawingHelper: SVGDrawingHelper,
162
+ annotation: PlanarFreehandROIAnnotation
163
+ ) => void;
164
+
165
+ constructor(
166
+ toolProps: PublicToolProps = {},
167
+ defaultToolProps: ToolProps = {
168
+ supportedInteractionTypes: ['Mouse', 'Touch'],
169
+ configuration: {
170
+ shadow: true,
171
+ preventHandleOutsideImage: false,
172
+ alwaysRenderOpenContourHandles: {
173
+ // When true, always render end points when you have an open contour, rather
174
+ // than just rendering a line.
175
+ enabled: false,
176
+ // When enabled, use this radius to draw the endpoints whilst not hovering.
177
+ radius: 2,
178
+ },
179
+ allowOpenContours: true,
180
+ // Proximity in canvas coordinates used to join contours.
181
+ closeContourProximity: 10,
182
+ // The proximity at which we fallback to the simplest grabbing logic for
183
+ // determining what index of the contour to start editing.
184
+ checkCanvasEditFallbackProximity: 6,
185
+ // The relative distance that points should be dropped along the polyline
186
+ // in units of the image pixel spacing. A value of 1 means that nodes must
187
+ // be placed no closed than the image spacing apart. A value of 4 means that 4
188
+ // nodes should be placed within the space of one image pixel size. A higher
189
+ // value gives more finese to the tool/smoother lines, but the value cannot
190
+ // be infinite as the lines become very computationally expensive to draw.
191
+ subPixelResolution: 4,
192
+ interpolation: {
193
+ interpolateOnAdd: false,
194
+ interpolateOnEdit: false, // used for edit only
195
+ knotsRatioPercentageOnAdd: 40,
196
+ knotsRatioPercentageOnEdit: 40,
197
+ },
198
+ },
199
+ }
200
+ ) {
201
+ super(toolProps, defaultToolProps);
202
+
203
+ // Register event loops and rendering logic, which are stored in different
204
+ // Files due to their complexity/size.
205
+ registerDrawLoop(this);
206
+ registerEditLoopCommon(this);
207
+ registerClosedContourEditLoop(this);
208
+ registerOpenContourEditLoop(this);
209
+ registerOpenContourEndEditLoop(this);
210
+ registerRenderMethods(this);
211
+ }
212
+
213
+ /**
214
+ * Based on the current position of the mouse and the current image, creates
215
+ * a `PlanarFreehandROIAnnotation` and stores it in the annotationManager.
216
+ *
217
+ * @param evt - `EventTypes.NormalizedMouseEventType`
218
+ * @returns The `PlanarFreehandROIAnnotation` object.
219
+ */
220
+ addNewAnnotation = (
221
+ evt: EventTypes.InteractionEventType
222
+ ): PlanarFreehandROIAnnotation => {
223
+ const eventDetail = evt.detail;
224
+ const { currentPoints, element } = eventDetail;
225
+ const worldPos = currentPoints.world;
226
+ const enabledElement = getEnabledElement(element);
227
+ const { viewport, renderingEngine } = enabledElement;
228
+ const camera = viewport.getCamera();
229
+ const { viewPlaneNormal, viewUp } = camera;
230
+
231
+ const referencedImageId = this.getReferencedImageId(
232
+ viewport,
233
+ worldPos,
234
+ viewPlaneNormal,
235
+ viewUp
236
+ );
237
+ const viewportIdsToRender = getViewportIdsWithToolToRender(
238
+ element,
239
+ this.getToolName()
240
+ );
241
+
242
+ const FrameOfReferenceUID = viewport.getFrameOfReferenceUID();
243
+
244
+ const annotation: PlanarFreehandROIAnnotation = {
245
+ highlighted: true,
246
+ invalidated: true,
247
+ metadata: {
248
+ viewPlaneNormal: <Types.Point3>[...viewPlaneNormal],
249
+ viewUp: <Types.Point3>[...viewUp],
250
+ FrameOfReferenceUID,
251
+ referencedImageId,
252
+ toolName: this.getToolName(),
253
+ },
254
+ data: {
255
+ handles: {
256
+ points: [], // Handle points for open contours
257
+ activeHandleIndex: null,
258
+ textBox: {
259
+ hasMoved: false,
260
+ worldPosition: <Types.Point3>[0, 0, 0],
261
+ worldBoundingBox: {
262
+ topLeft: <Types.Point3>[0, 0, 0],
263
+ topRight: <Types.Point3>[0, 0, 0],
264
+ bottomLeft: <Types.Point3>[0, 0, 0],
265
+ bottomRight: <Types.Point3>[0, 0, 0],
266
+ },
267
+ },
268
+ },
269
+ polyline: [<Types.Point3>[...worldPos]], // Polyline coordinates
270
+ label: '',
271
+ },
272
+ };
273
+
274
+ addAnnotation(annotation, element);
275
+
276
+ this.activateDraw(evt, annotation, viewportIdsToRender);
277
+
278
+ evt.preventDefault();
279
+
280
+ triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
281
+
282
+ return annotation;
283
+ };
284
+
285
+ /**
286
+ * Begins an edit of an open contour, when the mouse has selected a handle
287
+ * (end) of the open contour.
288
+ *
289
+ * @param evt - `EventTypes.MouseDownEventType`
290
+ * @param annotation - `PlanarFreehandROIAnnotation` annotation.
291
+ * @param handle - The handle index, 0 for the start and 1 for the end.
292
+ * @param interactionType - interaction type (mouse, touch)
293
+ */
294
+ handleSelectedCallback = (
295
+ evt: EventTypes.InteractionEventType,
296
+ annotation: PlanarFreehandROIAnnotation
297
+ ): void => {
298
+ const eventDetail = evt.detail;
299
+ const { element } = eventDetail;
300
+
301
+ const viewportIdsToRender = getViewportIdsWithToolToRender(
302
+ element,
303
+ this.getToolName()
304
+ );
305
+
306
+ this.activateOpenContourEndEdit(evt, annotation, viewportIdsToRender);
307
+ };
308
+
309
+ /**
310
+ * Edits the open or closed contour when the line is grabbed and dragged.
311
+ */
312
+ toolSelectedCallback = (
313
+ evt: EventTypes.InteractionEventType,
314
+ annotation: PlanarFreehandROIAnnotation
315
+ ): void => {
316
+ const eventDetail = evt.detail;
317
+ const { element } = eventDetail;
318
+
319
+ const viewportIdsToRender = getViewportIdsWithToolToRender(
320
+ element,
321
+ this.getToolName()
322
+ );
323
+
324
+ if (annotation.data.isOpenContour) {
325
+ this.activateOpenContourEdit(evt, annotation, viewportIdsToRender);
326
+ } else {
327
+ this.activateClosedContourEdit(evt, annotation, viewportIdsToRender);
328
+ }
329
+ };
330
+
331
+ /**
332
+ * Returns if the canvas point is near the line of the given annotation in the
333
+ * provided element or not. A proximity is passed to the function to determine the
334
+ * proximity of the point to the annotation in number of pixels.
335
+ *
336
+ * @param element - HTML Element
337
+ * @param annotation - The `PlanarFreehandROIAnnotation`.
338
+ * @param canvasCoords - Canvas coordinates
339
+ * @param proximity - Proximity to tool to consider
340
+ * @returns Boolean, whether the canvas point is near tool
341
+ */
342
+ isPointNearTool = (
343
+ element: HTMLDivElement,
344
+ annotation: PlanarFreehandROIAnnotation,
345
+ canvasCoords: Types.Point2,
346
+ proximity: number
347
+ ): boolean => {
348
+ const enabledElement = getEnabledElement(element);
349
+ const { viewport } = enabledElement;
350
+
351
+ const points = annotation.data.polyline;
352
+
353
+ // NOTE: It is implemented this way so that we do not double calculate
354
+ // points when number crunching adjacent line segments.
355
+ let previousPoint = viewport.worldToCanvas(points[0]);
356
+
357
+ for (let i = 1; i < points.length; i++) {
358
+ const p1 = previousPoint;
359
+ const p2 = viewport.worldToCanvas(points[i]);
360
+
361
+ const distance = pointCanProjectOnLine(canvasCoords, p1, p2, proximity);
362
+
363
+ if (distance === true) {
364
+ return true;
365
+ }
366
+
367
+ previousPoint = p2;
368
+ }
369
+
370
+ if (annotation.data.isOpenContour) {
371
+ // Contour is open, don't check last point to first point.
372
+ return false;
373
+ }
374
+
375
+ // check last point to first point
376
+ const pStart = viewport.worldToCanvas(points[0]);
377
+ const pEnd = viewport.worldToCanvas(points[points.length - 1]);
378
+
379
+ const distance = pointCanProjectOnLine(
380
+ canvasCoords,
381
+ pStart,
382
+ pEnd,
383
+ proximity
384
+ );
385
+
386
+ if (distance === true) {
387
+ return true;
388
+ }
389
+
390
+ return false;
391
+ };
392
+
393
+ cancel = (element: HTMLDivElement): void => {
394
+ const isDrawing = this.isDrawing;
395
+ const isEditingOpen = this.isEditingOpen;
396
+ const isEditingClosed = this.isEditingClosed;
397
+
398
+ if (isDrawing) {
399
+ this.cancelDrawing(element);
400
+ } else if (isEditingOpen) {
401
+ this.cancelOpenContourEdit(element);
402
+ } else if (isEditingClosed) {
403
+ this.cancelClosedContourEdit(element);
404
+ }
405
+ };
406
+
407
+ /**
408
+ * Triggers an annotation modified event.
409
+ */
410
+ triggerAnnotationModified = (
411
+ annotation: PlanarFreehandROIAnnotation,
412
+ enabledElement: Types.IEnabledElement
413
+ ): void => {
414
+ const { viewportId, renderingEngineId } = enabledElement;
415
+ // Dispatching annotation modified
416
+ const eventType = Events.ANNOTATION_MODIFIED;
417
+
418
+ const eventDetail: AnnotationModifiedEventDetail = {
419
+ annotation,
420
+ viewportId,
421
+ renderingEngineId,
422
+ };
423
+ triggerEvent(eventTarget, eventType, eventDetail);
424
+ };
425
+
426
+ /**
427
+ * Triggers an annotation completed event.
428
+ */
429
+ triggerAnnotationCompleted = (
430
+ annotation: PlanarFreehandROIAnnotation
431
+ ): void => {
432
+ const eventType = Events.ANNOTATION_COMPLETED;
433
+
434
+ const eventDetail: AnnotationCompletedEventDetail = {
435
+ annotation,
436
+ };
437
+
438
+ triggerEvent(eventTarget, eventType, eventDetail);
439
+ };
440
+
441
+ /**
442
+ * @override We need to override this method as the tool doesn't always have
443
+ * `handles`, which means `filterAnnotationsForDisplay` fails inside
444
+ * `filterAnnotationsWithinSlice`.
445
+ */
446
+ filterInteractableAnnotationsForElement(
447
+ element: HTMLDivElement,
448
+ annotations: Annotations
449
+ ): Annotations | undefined {
450
+ if (!annotations || !annotations.length) {
451
+ return;
452
+ }
453
+
454
+ const enabledElement = getEnabledElement(element);
455
+ const { viewport } = enabledElement;
456
+
457
+ let annotationsToDisplay;
458
+
459
+ if (viewport instanceof StackViewport) {
460
+ // Use the default `filterAnnotationsForDisplay` utility, as the stack
461
+ // path doesn't require handles.
462
+ annotationsToDisplay = filterAnnotationsForDisplay(viewport, annotations);
463
+ } else if (viewport instanceof VolumeViewport) {
464
+ const camera = viewport.getCamera();
465
+
466
+ const { spacingInNormalDirection } =
467
+ csUtils.getTargetVolumeAndSpacingInNormalDir(viewport, camera);
468
+
469
+ // Get data with same normal and within the same slice
470
+ annotationsToDisplay = this.filterAnnotationsWithinSlice(
471
+ annotations,
472
+ camera,
473
+ spacingInNormalDirection
474
+ );
475
+ } else {
476
+ throw new Error(`Viewport Type ${viewport.type} not supported`);
477
+ }
478
+
479
+ return annotationsToDisplay;
480
+ }
481
+
482
+ /**
483
+ * Altered version of the `utilities.planar.filterAnnotationsWithinSlice`,
484
+ * which uses the polyline position rather than the handle. As the polyline is
485
+ * always present.
486
+ */
487
+ private filterAnnotationsWithinSlice(
488
+ annotations: Annotations,
489
+ camera: Types.ICamera,
490
+ spacingInNormalDirection: number
491
+ ): Annotations {
492
+ const { viewPlaneNormal } = camera;
493
+
494
+ const annotationsWithParallelNormals = annotations.filter(
495
+ (td: Annotation) => {
496
+ const annotationViewPlaneNormal = td.metadata.viewPlaneNormal;
497
+
498
+ const isParallel =
499
+ Math.abs(vec3.dot(viewPlaneNormal, annotationViewPlaneNormal)) >
500
+ PARALLEL_THRESHOLD;
501
+
502
+ return annotationViewPlaneNormal && isParallel;
503
+ }
504
+ );
505
+
506
+ // No in plane annotations.
507
+ if (!annotationsWithParallelNormals.length) {
508
+ return [];
509
+ }
510
+
511
+ // Annotation should be within the slice, which means that it should be between
512
+ // camera's focalPoint +/- spacingInNormalDirection.
513
+
514
+ const halfSpacingInNormalDirection = spacingInNormalDirection / 2;
515
+ const { focalPoint } = camera;
516
+
517
+ const annotationsWithinSlice = [];
518
+
519
+ for (const annotation of annotationsWithParallelNormals) {
520
+ const data = annotation.data;
521
+ const point = data.polyline[0];
522
+
523
+ if (!annotation.isVisible) {
524
+ continue;
525
+ }
526
+
527
+ // A = point
528
+ // B = focal point
529
+ // P = normal
530
+
531
+ // B-A dot P => Distance in the view direction.
532
+ // this should be less than half the slice distance.
533
+
534
+ const dir = vec3.create();
535
+
536
+ vec3.sub(dir, focalPoint, point);
537
+
538
+ const dot = vec3.dot(dir, viewPlaneNormal);
539
+
540
+ if (Math.abs(dot) < halfSpacingInNormalDirection) {
541
+ annotationsWithinSlice.push(annotation);
542
+ }
543
+ }
544
+
545
+ return annotationsWithinSlice;
546
+ }
547
+
548
+ /**
549
+ * Draws the `PlanarFreehandROIAnnotation`s at each request animation frame.
550
+ *
551
+ * @param enabledElement - The Cornerstone's enabledElement.
552
+ * @param svgDrawingHelper - The svgDrawingHelper providing the context for drawing.
553
+ */
554
+ renderAnnotation = (
555
+ enabledElement: Types.IEnabledElement,
556
+ svgDrawingHelper: SVGDrawingHelper
557
+ ): boolean => {
558
+ const renderStatus = false;
559
+ const { viewport } = enabledElement;
560
+ const { element } = viewport;
561
+
562
+ let annotations = <PlanarFreehandROIAnnotation[]>(
563
+ getAnnotations(this.getToolName(), element)
564
+ );
565
+
566
+ // Todo: We don't need this anymore, filtering happens in triggerAnnotationRender
567
+ if (!annotations?.length) {
568
+ return renderStatus;
569
+ }
570
+
571
+ annotations = this.filterInteractableAnnotationsForElement(
572
+ element,
573
+ annotations
574
+ ) as PlanarFreehandROIAnnotation[];
575
+
576
+ if (!annotations?.length) {
577
+ return renderStatus;
578
+ }
579
+
580
+ const isDrawing = this.isDrawing;
581
+ const isEditingOpen = this.isEditingOpen;
582
+ const isEditingClosed = this.isEditingClosed;
583
+
584
+ if (!(isDrawing || isEditingOpen || isEditingClosed)) {
585
+ // No annotations are currently being modified, so we can just use the
586
+ // render contour method to render all of them
587
+ annotations.forEach((annotation) => {
588
+ if (!annotation) return;
589
+ this.renderContour(enabledElement, svgDrawingHelper, annotation);
590
+ });
591
+
592
+ return renderStatus;
593
+ }
594
+
595
+ // One of the annotations will need special rendering treatment, render all
596
+ // other annotations not being interacted with using the standard renderContour
597
+ // rendering path.
598
+ const activeAnnotationUID = this.commonData.annotation.annotationUID;
599
+
600
+ annotations.forEach((annotation) => {
601
+ if (annotation.annotationUID === activeAnnotationUID) {
602
+ if (isDrawing) {
603
+ this.renderContourBeingDrawn(
604
+ enabledElement,
605
+ svgDrawingHelper,
606
+ annotation
607
+ );
608
+ } else if (isEditingClosed) {
609
+ this.renderClosedContourBeingEdited(
610
+ enabledElement,
611
+ svgDrawingHelper,
612
+ annotation
613
+ );
614
+ } else if (isEditingOpen) {
615
+ this.renderOpenContourBeingEdited(
616
+ enabledElement,
617
+ svgDrawingHelper,
618
+ annotation
619
+ );
620
+ } else {
621
+ throw new Error(
622
+ `Unknown ${this.getToolName()} annotation rendering state`
623
+ );
624
+ }
625
+ } else {
626
+ this.renderContour(enabledElement, svgDrawingHelper, annotation);
627
+ }
628
+ });
629
+
630
+ // Todo: return boolean flag for each rendering route in the planar tool.
631
+ return true;
632
+ };
633
+ }
634
+
635
+ PlanarFreehandROITool.toolName = 'PlanarFreehandROI';
636
+ export default PlanarFreehandROITool;