-
Notifications
You must be signed in to change notification settings - Fork 1
/
xx_space_ex.h
1063 lines (961 loc) · 24.6 KB
/
xx_space_ex.h
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
#pragma once
#include "xx_blocklink.h"
#include "xx_space_.h"
namespace xx {
// required: XY T::pos
// required: float T::radius
// Ex == add count for every cell
template <typename T>
using SpaceGridExWeak = BlockLinkWeak<T, SpaceGridNode>;
template <typename T, typename ST = BlockLink<T, SpaceGridNode>>
struct SpaceGridEx : protected ST {
using ST::ST;
using NodeType = typename ST::NodeType;
using ST::Count;
using ST::Reserve;
using ST::TryGet;
int32_t numRows{}, numCols{}, cellSize{};
double _1_cellSize{}; // = 1 / cellSize
XYi max{};
int32_t cellsLen{};
std::unique_ptr<int32_t[]> cells;
std::unique_ptr<int32_t[]> counts;
void Init(int32_t numRows_, int32_t numCols_, int32_t cellSize_) {
assert(!cells);
assert(numRows_ > 0 && numCols_ > 0 && cellSize_ > 0);
numRows = numRows_;
numCols = numCols_;
cellSize = cellSize_;
_1_cellSize = 1. / cellSize_;
max.x = cellSize_ * numCols_;
max.y = cellSize_ * numRows_;
cellsLen = numRows * numCols;
cells = std::make_unique<int32_t[]>(cellsLen);
counts = std::make_unique<int32_t[]>(cellsLen);
Clear();
}
template <bool freeBuf = false, bool resetVersion = false>
void Clear() {
if (!cells) return;
ST::template Clear<freeBuf, resetVersion>();
memset(cells.get(), -1, sizeof(int32_t) * cellsLen);
memset(counts.get(), 0, sizeof(int32_t) * cellsLen);
}
// Emplace + Init( args ) + cells[ pos ] = o
template <typename... Args>
NodeType& EmplaceNodeInit(Args&&... args) {
assert(cells);
auto& o = ST::EmplaceCore();
o.value.Init(std::forward<Args>(args)...);
auto cidx = PosToCIdx(o.value.pos);
auto head = cells[cidx]; // backup
if (head >= 0) {
ST::RefNode(head).pre = o.index;
}
cells[cidx] = o.index; // assign new
o.nex = head;
o.pre = -1;
o.cidx = cidx;
++counts[cidx]; // sync count
return o;
}
template <typename... Args>
T& EmplaceInit(Args&&... args) {
return EmplaceNodeInit(std::forward<Args>(args)...).value;
}
protected:
XX_INLINE void Free(NodeType& o) {
assert(o.pre != o.index && o.nex != o.index && o.cidx >= 0);
--counts[o.cidx]; // sync count
if (o.index == cells[o.cidx]) {
cells[o.cidx] = o.nex;
}
if (o.pre >= 0) {
ST::RefNode(o.pre).nex = o.nex;
}
if (o.nex >= 0) {
ST::RefNode(o.nex).pre = o.pre;
}
//o.pre = -1;
//o.cidx = -1;
ST::Free(o);
}
public:
void Remove(T const& v) {
auto o = container_of(&v, NodeType, value);
Free(*o);
}
bool Remove(BlockLinkVI const& vi) {
if (vi.version >= -2 || vi.index < 0 || vi.index >= this->len) return false;
auto& o = ST::RefNode(vi.index);
if (o.version != vi.version) return false;
Free(o);
return true;
}
void Update(T& v) {
auto& o = *container_of(&v, NodeType, value);
assert(o.index >= 0);
assert(o.pre != o.index);
assert(o.nex != o.index);
auto cidx = PosToCIdx(v.pos);
if (cidx == o.cidx) return; // no change
--counts[o.cidx]; // sync count
++counts[cidx]; // sync count
// unlink
if (o.index != cells[o.cidx]) {
// isn't head
ST::RefNode(o.pre).nex = o.nex;
if (o.nex >= 0) {
ST::RefNode(o.nex).pre = o.pre;
//o.nex = -1;
}
//o.pre = -1;
} else {
// is head
assert(o.pre == -1);
cells[o.cidx] = o.nex;
if (o.nex >= 0) {
ST::RefNode(o.nex).pre = -1;
//o.nex = -1;
}
}
//o.cidx = -1;
// relink
if (cells[cidx] >= 0) {
ST::RefNode(cells[cidx]).pre = o.index;
}
o.nex = cells[cidx];
o.pre = -1;
cells[cidx] = o.index;
o.cidx = cidx;
}
// foreach by flags ( copy ForeachFlags to here )
// .Foreach([](T& o)->void { });
// .Foreach([](T& o)->xx::ForeachResult { });
template <typename F, typename R = std::invoke_result_t<F, T&>>
void Foreach(F&& func) {
if (ST::len <= 0) return;
for (int32_t i = 0, n = ST::blocks.len - 1; i <= n; ++i) {
auto& block = *(typename ST::Block*)ST::blocks[i];
auto& flags = block.flags;
if (!flags) continue;
auto left = ST::len & 0b111111;
int32_t e = (i < n || !left) ? 64 : left;
for (int32_t j = 0; j < e; ++j) {
auto& o = block.buf[j];
auto bit = uint64_t(1) << j;
if ((flags & bit) == 0) {
assert(o.version >= -2);
continue;
}
assert(o.version < -2);
if constexpr (std::is_void_v<R>) {
func(o.value);
}
else {
auto r = func(o.value);
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
}
}
XX_INLINE int32_t PosToCIdx(XYf const& p) {
assert(p.x >= 0 && p.x < cellSize * numCols);
assert(p.y >= 0 && p.y < cellSize * numRows);
auto c = int32_t(p.x * _1_cellSize);
assert(c >= 0 && c < numCols);
auto r = int32_t(p.y * _1_cellSize);
assert(r >= 0 && r < numRows);
return r * numCols + c;
}
// return x: col index y: row index
XX_INLINE XYi PosToCrIdx(XYf const& p) {
assert(p.x >= 0 && p.x < cellSize * numCols);
assert(p.y >= 0 && p.y < cellSize * numRows);
return {p.x * _1_cellSize, p.y * _1_cellSize};
}
// return cell's index
XX_INLINE int32_t CrIdxToCIdx(XYi const& crIdx) {
return crIdx.y * numCols + crIdx.x;
}
// cell's index to pos( left top corner )
XX_INLINE XYf CIdxToPos(int32_t cidx) {
assert(cidx >= 0 && cidx < cellsLen);
auto row = cidx / numCols;
auto col = cidx - row * numCols;
return {float(col * cellSize), float(row * cellSize)};
}
// cell's index to cell center pos
XX_INLINE XYf CIdxToCenterPos(int32_t cidx) {
return CIdxToPos(cidx) + float(cellSize) * 0.5f;
}
XX_INLINE XYf CrIdxToPos(int32_t colIdx, int32_t rowIdx) {
return CIdxToPos(rowIdx * numCols + colIdx);
}
XX_INLINE XYf CrIdxToCenterPos(int32_t colIdx, int32_t rowIdx) {
return CIdxToCenterPos(rowIdx * numCols + colIdx);
}
T* TryGetCellItemByPos(XY const& p) {
if (p.x < 0 || p.x >= max.x || p.y < 0 || p.y >= max.y) return nullptr;
auto cidx = PosToCIdx(p);
auto idx = cells[cidx];
if (idx < 0) return nullptr;
return &ST::RefNode(idx).value;
}
/*******************************************************************************************************/
/*******************************************************************************************************/
// search functions
// todo: more test & bug fix ( because copy from c# )
// .ForeachCell([](T& o)->void { all });
// .ForeachCell([](T& o)->bool { break });
// .ForeachCell([](T& o)->xx::ForeachResult { });
// return is Break or RemoveAndBreak
template <typename F, typename R = std::invoke_result_t<F, T&>>
XX_INLINE bool ForeachCell(int32_t cidx, F&& func) {
auto idx = cells[cidx];
while (idx >= 0) {
auto& o = ST::RefNode(idx);
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return true;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return true;
case ForeachResult::RemoveAndBreak:
Free(o);
return true;
default:
XX_ASSUME(false);
}
}
}
idx = o.nex;
}
return false;
}
// ring diffuse foreach ( usually for update logic )
// .ForeachByRange([](T& o)->void { all });
// .ForeachByRange([](T& o)->bool { break });
// .ForeachByRange([](T& o)->xx::ForeachResult { });
template <bool enableExcept = false, typename F, typename R = std::invoke_result_t<F, T&>>
void ForeachByRange(SpaceGridRingDiffuseData const& d, float x, float y, float maxDistance, F&& func, T* except = {}) {
auto cIdxBase = (int32_t)(x * _1_cellSize);
if (cIdxBase < 0 || cIdxBase >= numCols) return;
auto rIdxBase = (int32_t)(y * _1_cellSize);
if (rIdxBase < 0 || rIdxBase >= numRows) return;
auto searchRange = maxDistance + cellSize; // todo: scale by d.cellsize ?
auto& lens = d.lens;
auto& idxs = d.idxs;
for (int32_t i = 1, e = lens.len; i < e; i++) {
auto offsets = lens[i - 1].count;
auto size = lens[i].count - lens[i - 1].count;
for (int32_t j = 0; j < size; ++j) {
auto& tmp = idxs[offsets + j];
auto cIdx = cIdxBase + tmp.x;
if (cIdx < 0 || cIdx >= numCols) continue;
auto rIdx = rIdxBase + tmp.y;
if (rIdx < 0 || rIdx >= numRows) continue;
auto cidx = rIdx * numCols + cIdx;
auto idx = cells[cidx];
while (idx >= 0) {
auto& c = ST::RefNode(idx);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
idx = c.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o);
} else {
auto r = func(o);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(c);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(c);
return;
default:
XX_ASSUME(false);
}
}
}
idx = c.nex;
}
}
if (lens[i].radius > searchRange) break;
}
}
// foreach target cell + round 8 = 9 cells
// .Foreach9All([](T& o)->void { all });
// .Foreach9All([](T& o)->bool { break });
// .Foreach9All([](T& o)->xx::ForeachResult { });
template <bool enableExcept = false, typename F, typename R = std::invoke_result_t<F, T&>>
void Foreach9All(float x, float y, F&& func, T* except = {}) {
auto cIdx = (int32_t)(x * _1_cellSize);
if (cIdx < 0 || cIdx >= numCols) return;
auto rIdx = (int32_t)(y * _1_cellSize);
if (rIdx < 0 || rIdx >= numRows) return;
// 5
auto idx = rIdx * numCols + cIdx;
auto i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 6
++cIdx;
if (cIdx >= numCols) return;
++idx;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 3
++rIdx;
if (rIdx >= numRows) return;
idx += numCols;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 2
--idx;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 1
cIdx -= 2;
if (cIdx < 0) return;
--idx;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 4
idx -= numCols;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 7
rIdx -= 2;
if (rIdx < 0) return;
idx -= numCols;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 8
++idx;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
// 9
++idx;
i = cells[idx];
while (i >= 0) {
auto& o = ST::RefNode(i);
if constexpr (enableExcept) {
if (&o.value == except) {
i = o.nex;
continue;
}
}
if constexpr (std::is_void_v<R>) {
func(o.value);
} else {
auto r = func(o.value);
if constexpr (std::is_same_v<R, bool>) {
if (r) return;
} else {
switch (r) {
case ForeachResult::Continue: break;
case ForeachResult::RemoveAndContinue:
Free(o);
break;
case ForeachResult::Break: return;
case ForeachResult::RemoveAndBreak:
Free(o);
return;
default:
XX_ASSUME(false);
}
}
}
i = o.nex;
}
}
// foreach target cell + round 8 = 9 cells find first cross and return ( tested )
template<bool enableExcept = false>
T* FindFirstCrossBy9(float x, float y, float radius, T* except = {}) {
auto cIdx = (int32_t)(x * _1_cellSize);
if (cIdx < 0 || cIdx >= numCols) return nullptr;
auto rIdx = (int32_t)(y * _1_cellSize);
if (rIdx < 0 || rIdx >= numRows) return nullptr;
// 5
auto idx = rIdx * numCols + cIdx;
auto i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 6
++cIdx;
if (cIdx >= numCols) return nullptr;
++idx;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 3
++rIdx;
if (rIdx >= numRows) return nullptr;
idx += numCols;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 2
--idx;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 1
cIdx -= 2;
if (cIdx < 0) return nullptr;
--idx;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 4
idx -= numCols;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 7
rIdx -= 2;
if (rIdx < 0) return nullptr;
idx -= numCols;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 8
++idx;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
// 9
++idx;
i = cells[idx];
while (i >= 0) {
auto& c = ST::RefNode(i);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
i = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto r = o.radius + radius;
if (vx * vx + vy * vy < r * r) return &o;
i = c.nex;
}
return nullptr;
}
// ring diffuse search nearest edge best one and return
template<bool enableExcept = false>
T* FindNearestByRange(SpaceGridRingDiffuseData const& d, float x, float y, float maxDistance, T* except = {}) {
auto cIdxBase = (int32_t)(x * _1_cellSize);
if (cIdxBase < 0 || cIdxBase >= numCols) return nullptr;
auto rIdxBase = (int32_t)(y * _1_cellSize);
if (rIdxBase < 0 || rIdxBase >= numRows) return nullptr;
auto searchRange = maxDistance + cellSize; // todo: scale by d.cellsize ?
T* rtv = nullptr;
float maxV{};
auto& lens = d.lens;
auto& idxs = d.idxs;
for (int32_t i = 1, e = lens.len; i < e; i++) {
auto offsets = lens[i - 1].count;
auto size = lens[i].count - lens[i - 1].count;
for (int32_t j = 0; j < size; ++j) {
auto& tmp = idxs[offsets + j];
auto cIdx = cIdxBase + tmp.x;
if (cIdx < 0 || cIdx >= numCols) continue;
auto rIdx = rIdxBase + tmp.y;
if (rIdx < 0 || rIdx >= numRows) continue;
auto cidx = rIdx * numCols + cIdx;
auto idx = cells[cidx];
while (idx >= 0) {
auto& c = ST::RefNode(idx);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
idx = c.nex;
continue;
}
}
auto vx = o.pos.x - x;
auto vy = o.pos.y - y;
auto dd = vx * vx + vy * vy;
auto r = maxDistance + o.radius;
auto v = r * r - dd;
if (v > maxV)
{
rtv = &o;
maxV = v;
}
idx = c.nex;
}
}
if (lens[i].radius > searchRange) break;
}
return rtv;
}
// search result container. first: edge distance
Listi32<std::pair<float, T*>> result_FindNearestN;
// ring diffuse search nearest edge best N and return
// maxDistance: search limit( edge distance )
template<bool enableExcept = false>
int32_t FindNearestNByRange(SpaceGridRingDiffuseData const& d, float x, float y, float maxDistance, int32_t n, T* except = {}) {
auto cIdxBase = (int32_t)(x * _1_cellSize);
if (cIdxBase < 0 || cIdxBase >= numCols) return 0;
auto rIdxBase = (int32_t)(y * _1_cellSize);
if (rIdxBase < 0 || rIdxBase >= numRows) return 0;
auto searchRange = maxDistance + cellSize; // todo: scale by d.cellsize ?
auto& os = result_FindNearestN;
os.Clear();
auto& lens = d.lens;
auto& idxs = d.idxs;
for (int32_t i = 1, e = lens.len; i < e; i++) {
auto offsets = lens[i - 1].count;
auto size = lens[i].count - lens[i - 1].count;
for (int32_t j = 0; j < size; ++j) {
auto& tmp = idxs[offsets + j];
auto cIdx = cIdxBase + tmp.x;
if (cIdx < 0 || cIdx >= numCols) continue;
auto rIdx = rIdxBase + tmp.y;
if (rIdx < 0 || rIdx >= numRows) continue;
auto cidx = rIdx * numCols + cIdx;
auto idx = cells[cidx];
while (idx >= 0) {
auto& c = ST::RefNode(idx);
auto& o = c.value;
if constexpr (enableExcept) {
if (&o == except) {
idx = c.nex;
continue;
}
}
auto vx = o.pos.x - x;