-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal.go
More file actions
596 lines (521 loc) · 13.1 KB
/
internal.go
File metadata and controls
596 lines (521 loc) · 13.1 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
package region
// overlapFunc is called to handle overlapping bands during region operations.
type overlapFunc func(result *[]Box, r1, r2 []Box, y1, y2 int32)
// quickSortRects sorts rectangles by (Y1, X1) in ascending order.
func quickSortRects(rects []Box) {
numRects := len(rects)
if numRects <= 1 {
return
}
// Always called with numRects > 1
for numRects > 1 {
if numRects == 2 {
if rects[0].Y1 > rects[1].Y1 ||
(rects[0].Y1 == rects[1].Y1 && rects[0].X1 > rects[1].X1) {
rects[0], rects[1] = rects[1], rects[0]
}
return
}
// Choose partition element, stick in location 0
rects[0], rects[numRects>>1] = rects[numRects>>1], rects[0]
y1 := rects[0].Y1
x1 := rects[0].X1
// Partition array
i := 0
j := numRects
for {
// Find element from left that should be on right
for {
i++
if i == numRects || !(rects[i].Y1 < y1 || (rects[i].Y1 == y1 && rects[i].X1 < x1)) {
break
}
}
// Find element from right that should be on left
for {
j--
if !(y1 < rects[j].Y1 || (y1 == rects[j].Y1 && x1 < rects[j].X1)) {
break
}
}
if i < j {
rects[i], rects[j] = rects[j], rects[i]
} else {
break
}
}
// Move partition element back to middle
rects[0], rects[j] = rects[j], rects[0]
// Recurse on right side (larger partition)
if numRects-j-1 > 1 {
quickSortRects(rects[j+1 : numRects])
}
// Loop on left side (tail recursion optimization)
numRects = j
}
}
// coalesce attempts to merge the current band with the previous band.
// Returns the new index for the previous band.
func coalesce(rects []Box, prevStart, curStart int) ([]Box, int) {
if curStart >= len(rects) {
return rects, prevStart
}
numRects := curStart - prevStart
curNum := len(rects) - curStart
// Must have same number of rectangles in both bands
if numRects != curNum || numRects == 0 {
return rects, curStart
}
// Check if bands can be merged (bottom of prev == top of cur)
if rects[prevStart].Y2 != rects[curStart].Y1 {
return rects, curStart
}
// Check if all rectangles have same X coordinates
for i := 0; i < numRects; i++ {
if rects[prevStart+i].X1 != rects[curStart+i].X1 ||
rects[prevStart+i].X2 != rects[curStart+i].X2 {
return rects, curStart
}
}
// Merge: extend Y2 of previous band and remove current band
y2 := rects[curStart].Y2
for i := 0; i < numRects; i++ {
rects[prevStart+i].Y2 = y2
}
// Remove current band
return rects[:curStart], prevStart
}
// findBandEnd finds the end index of the current band (rectangles with same Y1).
func findBandEnd(rects []Box, start int) int {
if start >= len(rects) {
return start
}
y1 := rects[start].Y1
end := start + 1
for end < len(rects) && rects[end].Y1 == y1 {
end++
}
return end
}
// regionOp performs a generic region operation.
func regionOp(r *Region, reg1, reg2 *Region, overlap overlapFunc, appendNon1, appendNon2 bool) {
r1 := reg1.getRects()
r2 := reg2.getRects()
if len(r1) == 0 && len(r2) == 0 {
r.Clear()
return
}
if len(r1) == 0 {
if appendNon2 {
r.rects = make([]Box, len(r2))
copy(r.rects, r2)
r.setExtents()
} else {
r.Clear()
}
return
}
if len(r2) == 0 {
if appendNon1 {
r.rects = make([]Box, len(r1))
copy(r.rects, r1)
r.setExtents()
} else {
r.Clear()
}
return
}
// Result buffer
result := make([]Box, 0, len(r1)+len(r2))
// Indices into r1 and r2
i1, i2 := 0, 0
// Initialize ybot
ybot := r1[0].Y1
if r2[0].Y1 < ybot {
ybot = r2[0].Y1
}
prevBand := 0
for i1 < len(r1) && i2 < len(r2) {
// Find current bands
band1End := findBandEnd(r1, i1)
band2End := findBandEnd(r2, i2)
r1y1 := r1[i1].Y1
r2y1 := r2[i2].Y1
r1y2 := r1[i1].Y2
r2y2 := r2[i2].Y2
// Handle non-overlapping bands
if r1y1 < r2y1 {
if appendNon1 {
top := max32(r1y1, ybot)
bot := min32(r1y2, r2y1)
if top < bot {
curBand := len(result)
for k := i1; k < band1End; k++ {
result = append(result, Box{r1[k].X1, top, r1[k].X2, bot})
}
result, prevBand = coalesce(result, prevBand, curBand)
}
}
if r1y2 <= r2y1 {
i1 = band1End
continue
}
} else if r2y1 < r1y1 {
if appendNon2 {
top := max32(r2y1, ybot)
bot := min32(r2y2, r1y1)
if top < bot {
curBand := len(result)
for k := i2; k < band2End; k++ {
result = append(result, Box{r2[k].X1, top, r2[k].X2, bot})
}
result, prevBand = coalesce(result, prevBand, curBand)
}
}
if r2y2 <= r1y1 {
i2 = band2End
continue
}
}
// Handle overlapping region
ytop := max32(r1y1, r2y1)
ybot = min32(r1y2, r2y2)
if ybot > ytop {
curBand := len(result)
overlap(&result, r1[i1:band1End], r2[i2:band2End], ytop, ybot)
if len(result) > curBand {
result, prevBand = coalesce(result, prevBand, curBand)
}
}
// Move to next band
if r1y2 == ybot {
i1 = band1End
}
if r2y2 == ybot {
i2 = band2End
}
}
// Handle remaining bands from r1
// First band gets Y clipping + COALESCE, rest are appended as-is
if i1 < len(r1) && appendNon1 {
// Do first non-overlap call, which may be able to coalesce
band1End := findBandEnd(r1, i1)
r1y1 := r1[i1].Y1
r1y2 := r1[i1].Y2
top := max32(r1y1, ybot)
if top < r1y2 {
curBand := len(result)
for k := i1; k < band1End; k++ {
result = append(result, Box{r1[k].X1, top, r1[k].X2, r1y2})
}
result, prevBand = coalesce(result, prevBand, curBand)
}
// Just append the rest of the boxes (APPEND_REGIONS)
for k := band1End; k < len(r1); k++ {
result = append(result, r1[k])
}
}
// Handle remaining bands from r2
// First band gets Y clipping + COALESCE, rest are appended as-is
if i2 < len(r2) && appendNon2 {
// Do first non-overlap call, which may be able to coalesce
band2End := findBandEnd(r2, i2)
r2y1 := r2[i2].Y1
r2y2 := r2[i2].Y2
top := max32(r2y1, ybot)
if top < r2y2 {
curBand := len(result)
for k := i2; k < band2End; k++ {
result = append(result, Box{r2[k].X1, top, r2[k].X2, r2y2})
}
result, _ = coalesce(result, prevBand, curBand)
}
// Just append the rest of the boxes (APPEND_REGIONS)
for k := band2End; k < len(r2); k++ {
result = append(result, r2[k])
}
}
// Set result
if len(result) == 0 {
r.Clear()
} else if len(result) == 1 {
r.extents = result[0]
r.rects = nil
} else {
r.rects = result
r.setExtents()
}
}
// regionInfo tracks a mini-region during validate's Step 2.
type regionInfo struct {
reg Region
prevBand int
curBand int
}
// validate normalizes an unsorted collection of rectangles into Y-X banded format.
//
// Take a "region" which is a non-y-x-banded random collection of
// rectangles, and compute a nice region which is the union of all the
// rectangles.
//
// Strategy:
// - Step 1: Sort the rectangles into ascending order with primary key y1
// and secondary key x1.
// - Step 2: Split the rectangles into the minimum number of proper y-x
// banded regions.
// - Step 3: Merge the regions and set the extents.
func (r *Region) validate() {
if r.rects == nil {
return
}
numRects := len(r.rects)
if numRects == 0 {
r.Clear()
return
}
// If extents is already valid, region is already normalized
if r.extents.X1 < r.extents.X2 {
if numRects == 1 {
r.rects = nil
}
return
}
// Step 1: Sort the rects array into ascending (y1, x1) order
quickSortRects(r.rects)
// Step 2: Scatter the sorted array into the minimum number of regions
// Set up the first region to be the first rectangle
ri := make([]regionInfo, 0, 64)
ri = append(ri, regionInfo{
reg: Region{
extents: r.rects[0],
rects: []Box{r.rects[0]},
},
prevBand: 0,
curBand: 0,
})
// Now scatter rectangles into the minimum set of valid regions.
// If the next rectangle to be added to a region would force an existing rectangle
// in the region to be split up in order to maintain y-x banding, just
// forget it. Try the next region. If it doesn't fit cleanly into any
// region, make a new one.
for i := 1; i < numRects; i++ {
box := r.rects[i]
foundRegion := false
// Look for a region to append box to
for j := 0; j < len(ri); j++ {
rit := &ri[j]
reg := &rit.reg
riBox := reg.rects[len(reg.rects)-1] // Last box in this region
if box.Y1 == riBox.Y1 && box.Y2 == riBox.Y2 {
// box is in same band as riBox. Merge or append it
if box.X1 <= riBox.X2 {
// Merge it with riBox
if box.X2 > reg.rects[len(reg.rects)-1].X2 {
reg.rects[len(reg.rects)-1].X2 = box.X2
}
} else {
// Append to band
reg.rects = append(reg.rects, box)
}
foundRegion = true
break
} else if box.Y1 >= riBox.Y2 {
// Put box into new band
if reg.extents.X2 < riBox.X2 {
reg.extents.X2 = riBox.X2
}
if reg.extents.X1 > box.X1 {
reg.extents.X1 = box.X1
}
// Coalesce previous band
reg.rects, rit.prevBand = coalesce(reg.rects, rit.prevBand, rit.curBand)
rit.curBand = len(reg.rects)
// Append new box
reg.rects = append(reg.rects, box)
foundRegion = true
break
}
// Well, this region was inappropriate. Try the next one.
}
if !foundRegion {
// No regions were appropriate. Create a new one.
ri = append(ri, regionInfo{
reg: Region{
extents: box,
rects: []Box{box},
},
prevBand: 0,
curBand: 0,
})
}
}
// Finish up each region: coalesce and set extents
for j := 0; j < len(ri); j++ {
rit := &ri[j]
reg := &rit.reg
// Coalesce final band
reg.rects, _ = coalesce(reg.rects, rit.prevBand, rit.curBand)
// Update extents from last rectangle
if len(reg.rects) > 0 {
lastBox := reg.rects[len(reg.rects)-1]
if reg.extents.X2 < lastBox.X2 {
reg.extents.X2 = lastBox.X2
}
}
// Set Y extents
if len(reg.rects) > 0 {
reg.extents.Y1 = reg.rects[0].Y1
reg.extents.Y2 = reg.rects[len(reg.rects)-1].Y2
}
// Handle single rectangle optimization
if len(reg.rects) == 1 {
reg.rects = nil
}
}
// Step 3: Union all regions into a single region (binary merge)
for len(ri) > 1 {
half := len(ri) / 2
start := len(ri) & 1 // Start from 0 if even, 1 if odd
for j := start; j < half+start; j++ {
reg := &ri[j].reg
hreg := &ri[j+half].reg
// Union hreg into reg
regionOp(reg, reg, hreg, unionOverlap, true, true)
// Merge extents
if hreg.extents.X1 < reg.extents.X1 {
reg.extents.X1 = hreg.extents.X1
}
if hreg.extents.Y1 < reg.extents.Y1 {
reg.extents.Y1 = hreg.extents.Y1
}
if hreg.extents.X2 > reg.extents.X2 {
reg.extents.X2 = hreg.extents.X2
}
if hreg.extents.Y2 > reg.extents.Y2 {
reg.extents.Y2 = hreg.extents.Y2
}
}
ri = ri[:len(ri)-half]
}
// Copy result to r
if len(ri) > 0 {
*r = ri[0].reg
} else {
r.Clear()
}
}
// unionOverlap handles overlapping bands for union operation.
func unionOverlap(result *[]Box, r1, r2 []Box, y1, y2 int32) {
i1, i2 := 0, 0
var x1, x2 int32
// Start with leftmost rectangle
if r1[0].X1 < r2[0].X1 {
x1 = r1[0].X1
x2 = r1[0].X2
i1++
} else {
x1 = r2[0].X1
x2 = r2[0].X2
i2++
}
for i1 < len(r1) || i2 < len(r2) {
var nextX1, nextX2 int32
if i1 < len(r1) && (i2 >= len(r2) || r1[i1].X1 < r2[i2].X1) {
nextX1 = r1[i1].X1
nextX2 = r1[i1].X2
i1++
} else {
nextX1 = r2[i2].X1
nextX2 = r2[i2].X2
i2++
}
if nextX1 <= x2 {
// Merge
if nextX2 > x2 {
x2 = nextX2
}
} else {
// Add current rectangle, start new one
*result = append(*result, Box{x1, y1, x2, y2})
x1 = nextX1
x2 = nextX2
}
}
// Add final rectangle
*result = append(*result, Box{x1, y1, x2, y2})
}
// intersectOverlap handles overlapping bands for intersection operation.
func intersectOverlap(result *[]Box, r1, r2 []Box, y1, y2 int32) {
i1, i2 := 0, 0
for i1 < len(r1) && i2 < len(r2) {
x1 := max32(r1[i1].X1, r2[i2].X1)
x2 := min32(r1[i1].X2, r2[i2].X2)
if x1 < x2 {
*result = append(*result, Box{x1, y1, x2, y2})
}
// Advance pointer with leftmost right edge
if r1[i1].X2 < r2[i2].X2 {
i1++
} else if r2[i2].X2 < r1[i1].X2 {
i2++
} else {
// Equal right edges - advance both
i1++
i2++
}
}
}
// subtractOverlap handles overlapping bands for subtraction operation.
func subtractOverlap(result *[]Box, r1, r2 []Box, y1, y2 int32) {
i1, i2 := 0, 0
x1 := r1[0].X1
for i1 < len(r1) && i2 < len(r2) {
if r2[i2].X2 <= x1 {
// Subtrahend entirely to left: skip it
i2++
} else if r2[i2].X1 <= x1 {
// Subtrahend precedes or covers left edge of minuend
x1 = r2[i2].X2
if x1 >= r1[i1].X2 {
// Minuend completely covered
i1++
if i1 < len(r1) {
x1 = r1[i1].X1
}
} else {
i2++
}
} else if r2[i2].X1 < r1[i1].X2 {
// Add uncovered part of minuend
*result = append(*result, Box{x1, y1, r2[i2].X1, y2})
x1 = r2[i2].X2
if x1 >= r1[i1].X2 {
i1++
if i1 < len(r1) {
x1 = r1[i1].X1
}
} else {
i2++
}
} else {
// Subtrahend is to the right of minuend
if r1[i1].X2 > x1 {
*result = append(*result, Box{x1, y1, r1[i1].X2, y2})
}
i1++
if i1 < len(r1) {
x1 = r1[i1].X1
}
}
}
// Add remaining minuend rectangles
for i1 < len(r1) {
if r1[i1].X2 > x1 {
*result = append(*result, Box{x1, y1, r1[i1].X2, y2})
}
i1++
if i1 < len(r1) {
x1 = r1[i1].X1
}
}
}