Line data Source code
1 : import type {
2 : ReadOnlyRectEdges as RectEdges,
3 : ReadOnlyRect,
4 : PointTuple,
5 : Bounds
6 : } from '@comfyorg/shared-frontend-utils/geometry'
7 : import { clamp } from 'es-toolkit/math'
8 :
9 : export type { ReadOnlyRectEdges as RectEdges } from '@comfyorg/shared-frontend-utils/geometry'
10 :
11 : /**
12 : * Clips a rectangle to `bounds`: each edge is limited independently, so the
13 : * rect shrinks to the overlapping region rather than being moved to fit.
14 : * Keeps an interaction rectangle (the canvas selection box, the assets
15 : * marquee) inside its own surface. Both the rect and the bounds use
16 : * viewport-style edges (left/top/right/bottom), e.g. a DOMRect.
17 : */
18 54 : export function clipRectToBounds(
19 : rect: RectEdges,
20 : bounds: RectEdges
21 : ): RectEdges {
22 54 : return {
23 : left: clamp(rect.left, bounds.left, bounds.right),
24 : top: clamp(rect.top, bounds.top, bounds.bottom),
25 : right: clamp(rect.right, bounds.left, bounds.right),
26 : bottom: clamp(rect.bottom, bounds.top, bounds.bottom)
27 : }
28 : }
29 :
30 : /**
31 : * Linearly maps a value from [min, max] to [0, 1].
32 : * Returns 0 when min equals max to avoid division by zero.
33 : */
34 0 : export function normalize(value: number, min: number, max: number): number {
35 0 : return max === min ? 0 : (value - min) / (max - min)
36 : }
37 :
38 : /**
39 : * Linearly maps a normalized value from [0, 1] back to [min, max].
40 : */
41 0 : export function denormalize(
42 : normalized: number,
43 : min: number,
44 : max: number
45 : ): number {
46 0 : return min + normalized * (max - min)
47 : }
48 :
49 : type Vec2 = Readonly<PointTuple>
50 :
51 : /** `value` wrapped into `[0, length)`. Unlike `%`, negatives wrap to the end. */
52 2356 : export const wrapIndex = (value: number, length: number): number =>
53 : ((value % length) + length) % length
54 :
55 : /**
56 : * Finds the greatest common divisor (GCD) for two numbers using iterative
57 : * Euclidean algorithm. Uses iteration instead of recursion to avoid stack
58 : * overflow with large inputs or small floating-point step values.
59 : *
60 : * For floating-point numbers, uses a tolerance-based approach to handle
61 : * precision issues and limits iterations to prevent hangs.
62 : *
63 : * @param a - The first number.
64 : * @param b - The second number.
65 : * @returns The GCD of the two numbers.
66 : */
67 2356 : export const gcd = (a: number, b: number): number => {
68 : // Use absolute values to handle negative numbers
69 11 : let x = Math.abs(a)
70 11 : let y = Math.abs(b)
71 :
72 : // Handle edge cases
73 11 : if (x === 0) return y
74 11 : if (y === 0) return x
75 :
76 : // For floating-point numbers, use tolerance-based comparison
77 : // This prevents infinite loops due to floating-point precision issues
78 11 : const epsilon = 1e-10
79 11 : const maxIterations = 100
80 :
81 11 : let iterations = 0
82 11 : while (y > epsilon && iterations < maxIterations) {
83 15 : ;[x, y] = [y, x % y]
84 15 : iterations++
85 : }
86 :
87 11 : return x
88 : }
89 :
90 : /**
91 : * Finds the least common multiple (LCM) for two numbers.
92 : *
93 : * @param a - The first number.
94 : * @param b - The second number.
95 : * @returns The LCM of the two numbers.
96 : */
97 2356 : export const lcm = (a: number, b: number): number => {
98 11 : return Math.abs(a * b) / gcd(a, b)
99 : }
100 :
101 : /**
102 : * Computes the union (bounding box) of multiple rectangles using a single-pass algorithm.
103 : *
104 : * Finds the minimum and maximum x/y coordinates across all rectangles to create
105 : * a single bounding rectangle that contains all input rectangles. Optimized for
106 : * performance with V8-friendly tuple access patterns.
107 : *
108 : * @param rectangles - Array of rectangle tuples in [x, y, width, height] format
109 : * @returns Bounds object with union rectangle, or null if no rectangles provided
110 : */
111 282 : export function computeUnionBounds(
112 : rectangles: readonly ReadOnlyRect[]
113 : ): Bounds | null {
114 282 : const n = rectangles.length
115 282 : if (n === 0) {
116 0 : return null
117 : }
118 :
119 282 : const r0 = rectangles[0]
120 282 : let minX = r0[0]
121 282 : let minY = r0[1]
122 282 : let maxX = minX + r0[2]
123 282 : let maxY = minY + r0[3]
124 :
125 282 : for (let i = 1; i < n; i++) {
126 80 : const r = rectangles[i]
127 80 : const x1 = r[0]
128 80 : const y1 = r[1]
129 80 : const x2 = x1 + r[2]
130 80 : const y2 = y1 + r[3]
131 :
132 80 : if (x1 < minX) minX = x1
133 80 : if (y1 < minY) minY = y1
134 80 : if (x2 > maxX) maxX = x2
135 80 : if (y2 > maxY) maxY = y2
136 : }
137 :
138 282 : return {
139 : x: minX,
140 : y: minY,
141 : width: maxX - minX,
142 : height: maxY - minY
143 : }
144 : }
145 :
146 : /**
147 : * Checks if any item with pos/size overlaps a rectangle (AABB test).
148 : * @param items Items with pos [x, y] and size [width, height]
149 : * @param rect Rectangle as [x, y, width, height]
150 : * @returns `true` if any item overlaps the rect
151 : */
152 3589 : export function anyItemOverlapsRect(
153 : items: Iterable<{ pos: Vec2; size: Vec2 }>,
154 : rect: ReadOnlyRect
155 : ): boolean {
156 3589 : const rectRight = rect[0] + rect[2]
157 3589 : const rectBottom = rect[1] + rect[3]
158 :
159 3589 : for (const item of items) {
160 : const overlaps =
161 : item.pos[0] < rectRight &&
162 : item.pos[0] + item.size[0] > rect[0] &&
163 : item.pos[1] < rectBottom &&
164 : item.pos[1] + item.size[1] > rect[1]
165 :
166 3594 : if (overlaps) return true
167 : }
168 25 : return false
169 : }
|