-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDistanceOcclusionLib.cpp
2773 lines (2184 loc) · 87.5 KB
/
DistanceOcclusionLib.cpp
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// Purpose: Distance Field and Occlusion Generation
// Author: Andrew Willmott
//------------------------------------------------------------------------------
#include "DistanceOcclusionLib.h"
#include <math.h>
#include <string.h>
using namespace DOL;
#if defined(DOS_DEBUG) || defined(DEBUG)
#include <assert.h>
#else
#define assert(e) ((void) 0)
#endif
#if !defined(__cplusplus) || (__cplusplus <= 201100)
#define constexpr const
#endif
// Declarations
namespace
{
inline int RNG(uint32_t& seed, int limit)
{
seed = uint32_t(seed * uint64_t(1103515245)) + 12345;
return int((seed * (uint64_t(limit))) >> 32);
}
}
// Convenience macro for iterating over set bits (and also testing different strategies)
#ifdef _MSC_VER
#include <intrin.h>
uint32_t TrailingZeroes32(uint32_t mask)
{
unsigned long index;
_BitScanForward(&index, mask);
return index;
}
#elif defined(__GNUC__)
#define TrailingZeroes32 __builtin_ctz
#else
// Hash table approach is actually comparable in speed to intrinsics, faster than hierarchical bit tricks
constexpr int kTrailingZeroesHashTable[37] = { 32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18 };
int TrailingZeroes32(uint32_t x)
{
return kTrailingZeroesHashTable[(x & -x) % 37];
}
#endif
#define ITER_SET_BITS_BEGIN(MASK) \
while (MASK) \
{ \
int i = TrailingZeroes32(MASK); \
MASK &= MASK - 1; \
\
if (j * 32 + i >= w) \
break; \
#define ITER_SET_BITS_BEGIN_BF(MASK) \
for (int i = 0; i < 32; i++) \
if (MASK & (1 << i)) \
{ \
if (j * 32 + i >= w) \
break; \
#define ITER_SET_BITS_END }
// --- 2D Images ---------------------------------------------------------------
uint32_t* DOL::CreateBitMask(int w, int h)
{
size_t maskSize = MaskSize(w, h);
uint32_t* mask = new uint32_t[maskSize];
memset(mask, 0, maskSize * sizeof(uint32_t));
return mask;
}
void DOL::DestroyBitMask(uint32_t*& mask)
{
delete[] mask;
mask = 0;
}
void DOL::BitMaskAddPoints(int w, int h, int count, uint32_t seed, uint32_t mask[])
{
int ws = MaskSize(w);
for (int i = 0; i < count; i++)
{
int x = RNG(seed, w);
int y = RNG(seed, h);
assert(x < w);
assert(y < h);
int fx = x & 31;
x >>= 5;
int index = y * ws + x;
mask[index] |= (1 << fx);
}
}
namespace
{
uint32_t LeftMask(int n)
{
assert(n < 32);
uint32_t bit = (1 << n);
return ~(bit - 1);
}
uint32_t RightMask(int n)
{
assert(n < 32);
uint32_t bit = 1 << n;
return bit + (bit - 1); // Inclusive
}
void FillMaskBlock(int rs, int x0, int x1, int y0, int y1, uint32_t mask[])
{
int sx0 = x0 / 32;
int sx1 = x1 / 32;
uint32_t mask0 = LeftMask (x0 - sx0 * 32);
uint32_t mask1 = RightMask(x1 - sx1 * 32);
if (sx0 == sx1)
{
mask0 = mask0 & mask1;
mask1 = 0;
}
for (int y = y0; y <= y1; y++)
{
uint32_t* row = mask + y * rs;
row[sx0] |= mask0;
for (int sx = sx0 + 1; sx < sx1; sx++)
row[sx] = ~0;
row[sx1] |= mask1;
}
}
}
void DOL::BitMaskAddBlock(int w, int h, int sides, uint32_t mask[])
{
int x0 = w / 4;
int x1 = x0 + w / 2 - 1;
int y0 = h / 3;
int y1 = y0 + h / 3 - 1;
int sw = MaskSize(w);
if (sides > 4)
return FillMaskBlock(sw, x0, x1, y0, y1, mask);
if (sides > 0)
FillMaskBlock(sw, x0, x1, y0, y0, mask);
if (sides > 1)
FillMaskBlock(sw, x0, x0, y0, y1, mask);
if (sides > 2)
FillMaskBlock(sw, x0, x1, y1, y1, mask);
if (sides > 3)
FillMaskBlock(sw, x1, x1, y0, y1, mask);
}
namespace
{
template<class T> void InitDistancesFromBitMask(int w, int h, const uint32_t mask[], T distances[], T maxD)
{
// mask is expected to have rows padded to whole numbers of uint32_ts
int s = w * h;
for (int i = 0; i < s; i++)
distances[i] = maxD;
int maskStride = MaskSize(w);
for (int y = 0; y < h; y++)
{
for (int j = 0; j < maskStride; j++)
{
uint32_t m = *mask++;
if (m == 0)
continue;
T* block = distances + y * w + 32 * j;
ITER_SET_BITS_BEGIN(m)
block[i] = 0;
ITER_SET_BITS_END
}
}
}
}
void DOL::InitDistancesFromBitMask(int w, int h, const uint32_t mask[], int16_t distances[], int16_t maxD)
{
return ::InitDistancesFromBitMask<int16_t>(w, h, mask, distances, maxD);
}
void DOL::InitDistancesFromBitMask(int w, int h, const uint32_t mask[], int32_t distances[], int32_t maxD)
{
return ::InitDistancesFromBitMask<int32_t>(w, h, mask, distances, maxD);
}
void DOL::InitDistancesFromBitMask(int w, int h, const uint32_t mask[], float distances[], float maxD)
{
return ::InitDistancesFromBitMask<float>(w, h, mask, distances, maxD);
}
void DOL::InitDeltasFromBitMask(int w, int h, const uint32_t mask[], cCellDelta2 deltas[], bool invert)
{
const cCellDelta2 maxDelta = { kMaxDelta2, kMaxDelta2 };
const cCellDelta2 minDelta = { 0, 0 };
for (int i = 0; i < w * h; i++)
deltas[i] = maxDelta;
const int maskStride = MaskSize(w);
const uint32_t maskInvert = invert ? ~uint32_t(0) : 0;
for (int y = 0; y < h; y++)
for (int j = 0; j < maskStride * 32; j += 32)
{
uint32_t m = (*mask++) ^ maskInvert;
if (m == 0)
continue;
cCellDelta2* block = deltas + y * w + j;
const int n = (w - j) >= 32 ? 32 : (w - j);
for (int i = 0; i < n; i++) // XXX
{
if (m & 1)
block[i] = minDelta;
m >>= 1;
}
}
}
namespace
{
// Utilities for setting deltas for cells adjacent to the interior/exterior
// boundary, in units of half a cell width. E.g., a cell immediately to the
// left of the boundary will have a delta of (+1, 0), reflecting that the
// boundary is half a cell to right of the cell's centre.
// By initialising the SDF this way, we can calculate both interior and
// exterior distances in a single pass, and distances are consistent across
// the border. (The standard techique of calculating the exterior distances,
// then inverting the problem to calculate interior distances, and combining
// the two results, is both 2x as expensive, and leads to inconsistent
// distances on border cells. The border effectively has a width of -ve 1.
constexpr cCellDelta2 kBH0 = { 0, +1 }; // level set is half a cell up
constexpr cCellDelta2 kBH1 = { 0, -1 }; // level set is half a cell down
constexpr cCellDelta2 kBW0 = { +1, 0 }; // level set is half a cell right
constexpr cCellDelta2 kBW1 = { -1, 0 }; // level set is half a cell left
#ifdef USE_BORDER_REFERENCE
// These routines treat each border case separately, in turn, for clarity.
// They are useful both for understanding the logic, but also for sanity-
// checking the more complex all-in-one routine. (See SetBorderCellsCombo.)
void SetBorderCellsH(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
// Find horizontal edges...
// x .
// . or x
// and set the cells to either side accordingly
const int maskStride = MaskSize(w);
for (int y = 0; y < h - 1; y++)
{
const uint32_t* maskRow0 = mask + (y + 0) * maskStride;
const uint32_t* maskRow1 = mask + (y + 1) * maskStride;
cCellDelta2* deltasRow0 = deltas + (y + 0) * w;
cCellDelta2* deltasRow1 = deltas + (y + 1) * w;
for (int j = 0; j < maskStride; j++)
{
uint32_t m0 = maskRow0[j];
uint32_t m1 = maskRow1[j];
uint32_t mx = m0 ^ m1; // 1 on change
if (mx)
{
cCellDelta2* block0 = deltasRow0 + j * 32;
cCellDelta2* block1 = deltasRow1 + j * 32;
for (int i = 0; i < 32; i++)
if ((mx & (1 << i)))
{
if (j * 32 + i >= w) // delayed check as we get here relatively rarely
break;
assert(j * 32 + i >= 0);
assert(j * 32 + i < w);
block0[i] = kBH0;
block1[i] = kBH1;
}
}
}
}
}
void SetBorderCellsW(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
// Find vertical edges...
// .|x or x|.
// and set the cells to either side accordingly
const int maskStride = MaskSize(w);
for (int y = 0; y < h; y++)
{
const uint32_t* maskRow = mask + y * maskStride;
cCellDelta2* deltasRow = deltas + y * w;
uint32_t lastBit = maskRow[0] & 1; // repeat border cell, avoids extra logic to loop from 1 rather than 0.
for (int j = 0; j < maskStride; j++)
{
uint32_t m0 = maskRow[j];
uint32_t m1 = (m0 << 1) | lastBit;
lastBit = m0 >> 31;
// Effectively bit 'i' indexes cell i (m0) and cell i-1 (m1). It's
// done this way to avoid having to look ahead one word up front.
uint32_t mx = m0 ^ m1;
if (mx)
{
cCellDelta2* block = deltasRow + j * 32;
for (int i = 0; i < 32; i++)
if ((mx & (1 << i)))
{
if (j * 32 + i >= w) // delayed check as we get here relatively rarely
break;
assert(j * 32 + i > 0);
assert(j * 32 + i < w);
block[i - 1] = kBW0;
block[i + 0] = kBW1;
}
}
}
}
}
constexpr cCellDelta2 kBC00 = { -1, +1 };
constexpr cCellDelta2 kBC01 = { +1, -1 };
constexpr cCellDelta2 kBC10 = { +1, +1 };
constexpr cCellDelta2 kBC11 = { -1, -1 };
void SetBorderCellsC(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
// Look for diagonal borders affecting corners, i.e.,
// . x x .
// x . or . x
// and set the diagonal cell deltas to +-1, +-1 as appropriate.
// These may be overwritten by horizontal/vertical deltas later, as they
// will always be closer.
const int maskStride = MaskSize(w);
for (int y = 0; y < h - 1; y++)
{
const uint32_t* maskRow0 = mask + (y + 0) * maskStride;
const uint32_t* maskRow1 = mask + (y + 1) * maskStride;
cCellDelta2* deltasRow0 = deltas + (y + 0) * w;
cCellDelta2* deltasRow1 = deltas + (y + 1) * w;
uint32_t lastBit0 = maskRow1[0] & 1; // pick to avoid detection on i=j=0 without explicit check
uint32_t lastBit1 = maskRow0[0] & 1;
for (int j = 0; j < maskStride; j++)
{
uint32_t m00 = maskRow0[j];
uint32_t m01 = maskRow1[j];
uint32_t m10 = (m00 << 1) | lastBit0;
uint32_t m11 = (m01 << 1) | lastBit1;
lastBit0 = m00 >> 31;
lastBit1 = m01 >> 31;
uint32_t mc0 = m00 ^ m11; // forward diagonal
uint32_t mc1 = m01 ^ m10; // backward diagonal
if (mc0 | mc1)
{
cCellDelta2* block[2] = { deltasRow0 + j * 32, deltasRow1 + j * 32 };
for (int i = 0; i < 32; i++)
{
if ((mc0 & (1 << i)))
{
if (j * 32 + i >= w) // delayed check as we get here relatively rarely
break;
assert(j * 32 + i > 0);
assert(j * 32 + i < w);
block[0][i + 0] = kBC00;
block[1][i - 1] = kBC01;
}
if ((mc1 & (1 << i)) && (j * 32 + i < w))
{
if (j * 32 + i >= w) // delayed check as we get here relatively rarely
break;
assert(j * 32 + i > 0);
assert(j * 32 + i < w);
block[0][i - 1] = kBC10;
block[1][i + 0] = kBC11;
}
}
}
}
}
}
void SetBorderCellsC2(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
// Smarter version that figures out which particular cell
// of the diagonal should be set. Reduces writes by 2x.
const int maskStride = MaskSize(w);
for (int y = 0; y < h - 1; y++)
{
const uint32_t* maskRow0 = mask + (y + 0) * maskStride;
const uint32_t* maskRow1 = mask + (y + 1) * maskStride;
cCellDelta2* deltasRow0 = deltas + (y + 0) * w;
cCellDelta2* deltasRow1 = deltas + (y + 1) * w;
uint32_t lastBit0 = maskRow0[0] & 1;
uint32_t lastBit1 = maskRow1[0] & 1;
for (int j = 0; j < maskStride; j++)
{
uint32_t m00 = maskRow0[j];
uint32_t m01 = maskRow1[j];
uint32_t m10 = (m00 << 1) | lastBit0;
uint32_t m11 = (m01 << 1) | lastBit1;
lastBit0 = m00 >> 31;
lastBit1 = m01 >> 31;
// we have a corner cell if both verticals (or horizontals) feature one flip
uint32_t mv0 = m00 ^ m01; // 1 on vert change
uint32_t mv1 = m10 ^ m11; // 1 on vert change
uint32_t mh0 = m00 ^ m10; // 1 on change
uint32_t mc = (mv0 ^ mv1);
if (mc)
{
cCellDelta2* block[2] = { deltasRow0 + j * 32, deltasRow1 + j * 32 };
for (int i = 0; i < 32; i++)
if (mc & (1 << i))
{
if (j * 32 + i >= w) // delayed check as we get here relatively rarely
break;
assert(j * 32 + i > 0);
assert(j * 32 + i < w);
int dx = (mv1 >> i) & 1;
int dy = (mh0 >> i) & 1;
block[dy][i + dx - 1].x = 1 - 2 * dx;
block[dy][i + dx - 1].y = 1 - 2 * dy;
}
}
}
}
}
#else
void SetBorderCellsCombo(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
// Rolls everything into one pass
const int maskStride = MaskSize(w);
for (int y = 0; y < h - 1; y++)
{
const uint32_t* maskRow0 = mask + (y + 0) * maskStride;
const uint32_t* maskRow1 = mask + (y + 1) * maskStride;
cCellDelta2* deltasRow0 = deltas + (y + 0) * w;
cCellDelta2* deltasRow1 = deltas + (y + 1) * w;
uint32_t lastBit0 = maskRow0[0] & 1; // pick to avoid vertical i=0 check
uint32_t lastBit1 = maskRow1[0] & 1;
for (int j = 0; j < maskStride; j++)
{
uint32_t m00 = maskRow0[j];
uint32_t m01 = maskRow1[j];
uint32_t m10 = (m00 << 1) | lastBit0;
uint32_t m11 = (m01 << 1) | lastBit1;
lastBit0 = m00 >> 31;
lastBit1 = m01 >> 31;
cCellDelta2* block[2] = { deltasRow0 + j * 32, deltasRow1 + j * 32 };
// Horizontal
uint32_t ey = m00 ^ m01; // 1 on change
// Corner. These are much rarer, and we must check to ensure we don't
// overwrite edge deltas, as they are larger.
uint32_t ex = m00 ^ m10;
uint32_t eyx = m10 ^ m11;
uint32_t bc = ey ^ eyx;
// The order here is important as iterating over the masks can destroy them
// if (ex | ey | bc) early out currently not worth it
ITER_SET_BITS_BEGIN(bc)
assert(j * 32 + i > 0);
int dx = (eyx >> i) & 1;
int dy = (ex >> i) & 1;
cCellDelta2& corner = block[dy][i + dx - 1];
if (corner.x != kMaxDelta2)
continue;
corner.x = 1 - 2 * dx;
corner.y = 1 - 2 * dy;
ITER_SET_BITS_END
ITER_SET_BITS_BEGIN(ex)
assert(j * 32 + i > 0);
block[0][i - 1] = kBW0;
block[0][i + 0] = kBW1;
ITER_SET_BITS_END
ITER_SET_BITS_BEGIN(ey)
block[0][i] = kBH0;
block[1][i] = kBH1;
ITER_SET_BITS_END
}
}
// Finish last vertical row
{
const int y = h - 1;
const uint32_t* maskRow = mask + y * maskStride;
cCellDelta2* deltasRow = deltas + y * w;
uint32_t lastBit = maskRow[0] & 1; // repeat border cell, avoids extra logic to loop from 1 rather than 0.
for (int j = 0; j < maskStride; j++)
{
uint32_t m0 = maskRow[j];
uint32_t m1 = (m0 << 1) | lastBit;
lastBit = m0 >> 31;
uint32_t mvx = m0 ^ m1;
if (mvx)
{
cCellDelta2* block = deltasRow + j * 32;
ITER_SET_BITS_BEGIN(mvx)
assert(j * 32 + i > 0);
block[i - 1] = kBW0;
block[i + 0] = kBW1;
ITER_SET_BITS_END
}
}
}
}
#endif
}
void DOL::InitBorderDeltasFromBitMask(int w, int h, const uint32_t* const mask, cCellDelta2 deltas[])
{
const cCellDelta2 maxDelta = { kMaxDelta2, kMaxDelta2 };
int s = w * h;
for (int i = 0; i < s; i++)
deltas[i] = maxDelta;
#ifdef USE_BORDER_REFERENCE
SetBorderCellsC(w, h, mask, deltas);
SetBorderCellsH(w, h, mask, deltas);
SetBorderCellsW(w, h, mask, deltas);
#else
SetBorderCellsCombo(w, h, mask, deltas);
#endif
}
namespace
{
/// Returns true if d1 is closer than d0
inline bool Closer(cCellDelta2 d0, cCellDelta2 d1)
{
int dw0 = d0.x * d0.x + d0.y * d0.y;
int dw1 = d1.x * d1.x + d1.y * d1.y;
return dw0 > dw1;
}
}
void DOL::FastSweep(int w, int h, cCellDelta2 deltas[], int cw)
{
// Fast sweep approach -- sweep each diagonal in turn.
// This is really the canonical sweeping method -- each sweep calculates
// distances over a quadrant, as for each cell in the sweep, you can
// guarantee all cells in the corresponding quadrant defined by that cell
// and the start point have already been visited.
// Other approaches such as Danielsson and Chamfer combine sweeps and vary
// the inspected neighbours to trade speed vs. accuracy.
//
// See, e.g., "A fast sweeping method for Eikonal equations" Zhao 2004.
const int lastRow = w * (h - 1);
cCellDelta2 cell;
for (int sweep = 0; sweep < 4; sweep++)
{
const int sx = (sweep >> 0) & 1;
const int sy = (sweep >> 1) & 1;
const int dx = 1 - 2 * sx;
const int dy = 1 - 2 * sy;
const int cx = -cw * dx;
const int cy = -cw * dy;
const int ib = sx * (w - 1);
const int ie = w - sx * (w + 1);
cCellDelta2* row0 = deltas + sy * lastRow;
for (int j = 1; j < h; j++)
{
cCellDelta2* row1 = row0 + w * dy;
for (int i = ib; i != ie; i += dx)
{
// Testing showed early out check for c = 0, 0 not worth it
// even with large contiguous areas of interior cells. Actively
// harmful in other cases due to branch overhead.
cell.x = row0[i].x;
cell.y = row0[i].y + cy;
if (Closer(row1[i], cell))
row1[i] = cell;
}
for (int i = ib + dx; i != ie; i += dx)
{
cell.x = row1[i - dx].x + cx;
cell.y = row1[i - dx].y;
if (Closer(row1[i], cell))
row1[i] = cell;
cell.x = row0[i - dx].x + cx;
cell.y = row0[i - dx].y + cy;
if (Closer(row1[i], cell))
row1[i] = cell;
}
row0 = row1;
}
}
}
// Danielsson is similar to the quadrant sweep approach (FastSweep), except it
// wraps alternating x scans into one vertical sweep.
void DOL::Danielsson(int w, int h, cCellDelta2 deltas[], int cw)
{
// store dx^2, dy^2 to nearest point. distance = sqrt(dx^2 + dy^2).
// Much more accurate, doesn't over-estimate like Chamfer.
cCellDelta2* row0 = deltas;
cCellDelta2 cell;
for (int j = 1; j < h; j++)
{
cCellDelta2* row1 = row0 + w;
for (int i = 0; i < w; i++)
{
cell.x = row0[i].x;
cell.y = row0[i].y - cw;
if (Closer(row1[i], cell))
row1[i] = cell;
}
for (int i = 1; i < w; i++)
{
cell.x = row1[i - 1].x - cw;
cell.y = row1[i - 1].y;
if (Closer(row1[i], cell))
row1[i] = cell;
}
for (int i = w - 2; i >= 0; i--)
{
cell.x = row1[i + 1].x + cw;
cell.y = row1[i + 1].y;
if (Closer(row1[i], cell))
row1[i] = cell;
}
row0 = row1;
}
row0 = deltas + h * w - w;
for (int j = h - 1; j > 0; j--)
{
cCellDelta2* row1 = row0 - w;
for (int i = 0; i < w; i++)
{
cell.x = row0[i].x;
cell.y = row0[i].y + cw;
if (Closer(row1[i], cell))
row1[i] = cell;
}
for (int i = 1; i < w; i++)
{
cell.x = row1[i - 1].x - cw;
cell.y = row1[i - 1].y;
if (Closer(row1[i], cell))
row1[i] = cell;
}
for (int i = w - 2; i >= 0; i--)
{
cell.x = row1[i + 1].x + cw;
cell.y = row1[i + 1].y;
if (Closer(row1[i], cell))
row1[i] = cell;
}
row0 = row1;
}
}
namespace
{
void JumpFlood(int step, int w, int h, cCellDelta2 deltasIn[], cCellDelta2 deltasOut[], int cw)
{
const cCellDelta2* row = deltasIn;
cCellDelta2* rowOut = deltasOut;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
cCellDelta2 minCell = row[i];
if (i - step >= 0)
{
cCellDelta2 cell = row[i - step];
cell.x -= cw * step;
if (Closer(minCell, cell))
minCell = cell;
if (j - step >= 0)
{
cCellDelta2 cell = row[i - step - step * w];
cell.x -= cw * step;
cell.y -= cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
if (j + step < h)
{
cCellDelta2 cell = row[i - step + step * w];
cell.x -= cw * step;
cell.y += cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
}
if (i + step < w)
{
cCellDelta2 cell = row[i + step];
cell.x += cw * step;
if (Closer(minCell, cell))
minCell = cell;
if (j - step >= 0)
{
cCellDelta2 cell = row[i + step - step * w];
cell.x += cw * step;
cell.y -= cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
if (j + step < h)
{
cCellDelta2 cell = row[i + step + step * w];
cell.x += cw * step;
cell.y += cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
}
if (j - step >= 0)
{
cCellDelta2 cell = row[i - step * w];
cell.y -= cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
if (j + step < h)
{
cCellDelta2 cell = row[i + step * w];
cell.y += cw * step;
if (Closer(minCell, cell))
minCell = cell;
}
rowOut[i] = minCell;
}
row += w;
rowOut += w;
}
}
int FloorPow2(int v)
{
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
return (v + 1) >> 1;
}
}
void DOL::JumpFlood(int w, int h, cCellDelta2 deltas[], int cw)
{
cCellDelta2* temp = new cCellDelta2[w * h];
cCellDelta2* d0 = deltas;
cCellDelta2* d1 = temp;
int step = FloorPow2((w >= h ? w : h) - 1);
for ( ; step > 0; step /= 2)
{
::JumpFlood(step, w, h, d0, d1, cw);
cCellDelta2* dt = d0;
d0 = d1;
d1 = dt;
}
if (d0 != deltas)
::JumpFlood(1, w, h, d0, deltas, cw); // we could copy, but might as well do another round
delete[] temp;
}
namespace
{
// G. Borgefors.
// Distance transformations in digital images.
template<class T> int Chamfer(int w, int h, T distances[])
{
// according to Borgefors, using these values leads to more
// stable and accurate results than 1, sqrt(2) with double precision.
// See Table 1 in Borgefors '86. The resulting distances must be
// divided by d0 once the algorithm is complete.
const T d0 = 3;
const T d1 = 4;
T* row0 = distances;
for (int x = 1; x < w; x++) // restricted mask for y=0
if (row0[x] > row0[x - 1] + d0)
row0[x] = row0[x - 1] + d0;
for (int y = 1; y < h; y++)
{
T* row1 = row0 + w;
if (row1[0] > row0[0] + d0) // restricted mask for x=0
row1[0] = row0[0] + d0;
if (row1[0] > row0[1] + d1)
row1[0] = row0[1] + d1;
for (int x = 1; x < w - 1; x++) // full mask
{
if (row1[x] > row0[x - 1] + d1)
row1[x] = row0[x - 1] + d1;
if (row1[x] > row0[x ] + d0)
row1[x] = row0[x ] + d0;
if (row1[x] > row0[x + 1] + d1)
row1[x] = row0[x + 1] + d1;
if (row1[x] > row1[x - 1] + d0)
row1[x] = row1[x - 1] + d0;
}
if (row1[w - 1] > row0[w - 2] + d1) // restricted mask for x=w-1
row1[w - 1] = row0[w - 2] + d1;
if (row1[w - 1] > row0[w - 1] + d0)
row1[w - 1] = row0[w - 1] + d0;
if (row1[w - 1] > row1[w - 2] + d0)
row1[w - 1] = row1[w - 2] + d0;
row0 = row1;
}
row0 = distances + h * w - w;
for (int x = w - 2; x >= 0; x--) // restricted reverse mask for y=h-1
if (row0[x] > row0[x + 1] + d0)
row0[x] = row0[x + 1] + d0;
for (int y = h - 2; y >= 0; y--)
{
T* row1 = row0 - w;
if (row1[w - 1] > row0[w - 1] + d0) // restricted mask for x=w-1
row1[w - 1] = row0[w - 1] + d0;
if (row1[w - 1] > row0[w - 2] + d1)
row1[w - 1] = row0[w - 2] + d1;
for (int x = w - 2; x > 0; x--) // full reverse mask
{
if (row1[x] > row0[x + 1] + d1)
row1[x] = row0[x + 1] + d1;
if (row1[x] > row0[x ] + d0)
row1[x] = row0[x ] + d0;
if (row1[x] > row0[x - 1] + d1)
row1[x] = row0[x - 1] + d1;
if (row1[x] > row1[x + 1] + d0)
row1[x] = row1[x + 1] + d0;
}
if (row1[0] > row0[1] + d1) // restricted mask for x=0
row1[0] = row0[1] + d1;
if (row1[0] > row0[0] + d0)
row1[0] = row0[0] + d0;
if (row1[0] > row1[1] + d0)
row1[0] = row1[1] + d0;