Bin
2025-12-17 262fecaa75b2909ad244f12c3b079ed3ff4ae329
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { types } from "mobx-state-tree";
 
import BaseTool, { DEFAULT_DIMENSIONS } from "./Base";
import ToolMixin from "../mixins/Tool";
import { ThreePointsDrawingTool, TwoPointsDrawingTool } from "../mixins/DrawingTool";
import { AnnotationMixin } from "../mixins/AnnotationMixin";
import { NodeViews } from "../components/Node/Node";
import { FF_DEV_3793, isFF } from "../utils/feature-flags";
 
const _BaseNPointTool = types
  .model("BaseNTool", {
    group: "segmentation",
    smart: true,
    shortcut: "tool:rect",
  })
  .views((self) => {
    const Super = {
      createRegionOptions: self.createRegionOptions,
      isIncorrectControl: self.isIncorrectControl,
      isIncorrectLabel: self.isIncorrectLabel,
    };
 
    return {
      get getActivePolygon() {
        const poly = self.currentArea;
 
        if (poly && poly.closed) return null;
        if (poly === undefined) return null;
        if (poly && poly.type !== "rectangleregion") return null;
 
        return poly;
      },
 
      get tagTypes() {
        return {
          stateTypes: "rectanglelabels",
          controlTagTypes: ["rectanglelabels", "rectangle"],
        };
      },
      get defaultDimensions() {
        return DEFAULT_DIMENSIONS.rect;
      },
      createRegionOptions({ x, y }) {
        return Super.createRegionOptions({
          x,
          y,
          height: isFF(FF_DEV_3793) ? self.obj.canvasToInternalY(1) : 1,
          width: isFF(FF_DEV_3793) ? self.obj.canvasToInternalX(1) : 1,
        });
      },
 
      isIncorrectControl() {
        return Super.isIncorrectControl() && self.current() === null;
      },
      isIncorrectLabel() {
        return !self.current() && Super.isIncorrectLabel();
      },
      canStart() {
        return self.current() === null && !self.annotation.isReadOnly();
      },
 
      current() {
        return self.getActivePolygon;
      },
    };
  })
  .actions((self) => {
    const Super = {
      commitDrawingRegion: self.commitDrawingRegion,
    };
 
    return {
      beforeCommitDrawing() {
        const s = self.getActiveShape;
 
        return s.width > self.MIN_SIZE.X && s.height > self.MIN_SIZE.Y;
      },
 
      commitDrawingRegion() {
        const { currentArea, control, obj } = self;
 
        if (!currentArea) return;
 
        // Apply snap to pixel if enabled before finalizing the region
        if (control?.snap === "pixel") {
          const canvasX = currentArea.parent.internalToCanvasX(currentArea.x);
          const canvasY = currentArea.parent.internalToCanvasY(currentArea.y);
          const canvasWidth = currentArea.parent.internalToCanvasX(currentArea.width);
          const canvasHeight = currentArea.parent.internalToCanvasY(currentArea.height);
 
          // Apply snap logic through setPosition which handles both corners
          currentArea.setPosition(canvasX, canvasY, canvasWidth, canvasHeight, currentArea.rotation);
        }
 
        // Use the parent commitDrawingRegion to finalize the region
        return Super.commitDrawingRegion();
      },
    };
  });
 
const _Tool = types
  .model("RectangleTool", {
    shortcut: "tool:rect",
  })
  .views((self) => ({
    get viewTooltip() {
      return "Rectangle";
    },
    get iconComponent() {
      return self.dynamic ? NodeViews.RectRegionModel.altIcon : NodeViews.RectRegionModel.icon;
    },
  }));
 
const _Tool3Point = types
  .model("Rectangle3PointTool", {
    shortcut: "tool:rect-3point",
  })
  .views((self) => ({
    get viewTooltip() {
      return "3 Point Rectangle";
    },
    get iconComponent() {
      return self.dynamic ? NodeViews.Rect3PointRegionModel.altIcon : NodeViews.Rect3PointRegionModel.icon;
    },
  }));
 
const Rect = types.compose(
  _Tool.name,
  ToolMixin,
  BaseTool,
  TwoPointsDrawingTool,
  _BaseNPointTool,
  _Tool,
  AnnotationMixin,
);
 
const Rect3Point = types.compose(
  _Tool3Point.name,
  ToolMixin,
  BaseTool,
  ThreePointsDrawingTool,
  _BaseNPointTool,
  _Tool3Point,
  AnnotationMixin,
);
 
export { Rect, Rect3Point };