Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
import { wrapArray } from "../../utils/utilities";
import { Geometry } from "./Geometry";
 
/**
 * @type {import("./Geometry").BBox}
 */
const DEFAULT_BBOX = { x: 0, y: 0, width: 0, height: 0 };
 
/**
 * Provides an abstract boudnign box for any types of regions
 */
export class BoundingBox {
  options = {};
 
  static bbox(region) {
    const bbox = _detect(region);
 
    return wrapArray(bbox).map((bbox) => Object.assign({ ...DEFAULT_BBOX }, bbox));
  }
 
  /**
   * Contructor
   *
   * _source_ might be any object that provides its dimensions and position
   *
   * @param {{
   * source: any,
   * getX: (any) => number,
   * getY: (any) => number,
   * getXWidth: (any) => number,
   * getHeight: (any) => number
   * }} options
   */
  constructor(options) {
    Object.assign(this.options, options);
  }
 
  get _source() {
    return this.options.source;
  }
 
  get x() {
    return this.options.getX(this._source);
  }
 
  get y() {
    return this.options.getY(this._source);
  }
 
  get width() {
    return this.options.getWidth(this._source);
  }
 
  get height() {
    return this.options.getHeight(this._source);
  }
}
 
const stageRelatedBBox = (region, bbox) => {
  // If there is no stageRef we just wait for it in the next renders
  if (!region.parent?.stageRef) return null;
  const imageBbox = Geometry.getDOMBBox(region.parent.stageRef.content, true);
  const transformedBBox = Geometry.clampBBox(
    Geometry.modifyBBoxCoords(bbox, region.parent.zoomOriginalCoords),
    { x: 0, y: 0 },
    { x: region.parent.canvasSize.width, y: region.parent.canvasSize.height },
  );
 
  return {
    ...transformedBBox,
    x: imageBbox.x + transformedBBox.x,
    y: imageBbox.y + transformedBBox.y,
  };
};
 
const _detect = (region) => {
  // that's a tricky way to detect bbox of exact result instead of whole region
  // works for global classifications and per-regions
  const isResult = !!region.from_name;
  if (isResult) {
    return Geometry.getDOMBBox(region.from_name.elementRef?.current);
  }
 
  switch (region.type) {
    case "textrange":
    case "richtextregion":
    case "textarearegion":
    case "paragraphs":
    case "timeseriesregion": {
      const regionBbox = Geometry.getDOMBBox(region.getRegionElement());
      const container = region.parent?.mountNodeRef?.current;
 
      if (container?.tagName === "IFRAME") {
        const iframeBbox = Geometry.getDOMBBox(container, true);
 
        return (
          regionBbox?.map((bbox) => ({
            ...bbox,
            x: bbox.x + iframeBbox.x,
            y: bbox.y + iframeBbox.y,
          })) || null
        );
      }
 
      return regionBbox;
    }
    case "audioregion": {
      const bbox = region.bboxCoordsCanvas;
      const stageEl = region.parent?.stageRef?.current;
      const stageBbox = Geometry.getDOMBBox(stageEl, true);
 
      return bbox
        ? stageBbox
          ? {
              x: stageBbox.x + bbox.left,
              y: stageBbox.y + bbox.top,
              width: bbox.right - bbox.left,
              height: bbox.bottom - bbox.top,
            }
          : bbox
        : DEFAULT_BBOX;
    }
    case "rectangleregion":
    case "ellipseregion":
    case "polygonregion":
    case "vectorregion":
    case "keypointregion":
    case "brushregion":
    case "bitmaskregion": {
      const bbox = region.bboxCoordsCanvas;
 
      return bbox
        ? stageRelatedBBox(region, {
            x: bbox.left,
            y: bbox.top,
            width: bbox.right - bbox.left,
            height: bbox.bottom - bbox.top,
          })
        : DEFAULT_BBOX;
    }
    default: {
      console.warn(`Unknown region type: ${region.type}`);
      return { ...DEFAULT_BBOX };
    }
  }
};