-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcSpatialGrid3D.cls
1507 lines (1193 loc) · 56.5 KB
/
cSpatialGrid3D.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "cSpatialGrid3D"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
Private Type tCELL
pIDX() As Long
NP As Long
MaxNP As Long
myIDX As Long
End Type
Private GridSize As Long
Private MAXCellX As Long
Private MAXCellY As Long
Attribute MAXCellY.VB_VarUserMemId = 1610809345
Private MAXCellZ As Long
Attribute MAXCellZ.VB_VarUserMemId = 1073938435
Private MinCellX As Long
Attribute MinCellX.VB_VarUserMemId = 1073938436
Private MinCellY As Long
Attribute MinCellY.VB_VarUserMemId = 1073938437
Private MinCellZ As Long
Attribute MinCellZ.VB_VarUserMemId = 1073938438
Private CELL() As tCELL
Attribute CELL.VB_VarUserMemId = 1610809346
Private NP As Long
Attribute NP.VB_VarUserMemId = 1073938440
Private MaxNP As Long
Attribute MaxNP.VB_VarUserMemId = 1073938441
Private px() As Single
Attribute px.VB_VarUserMemId = 1073938442
Private py() As Single
Attribute py.VB_VarUserMemId = 1073938443
Private pz() As Single
Attribute pz.VB_VarUserMemId = 1073938444
Private MaxDistance2 As Single
Attribute MaxDistance2.VB_VarUserMemId = 1073938445
Private PairP1() As Long
Attribute PairP1.VB_VarUserMemId = 1610809348
Private PairP2() As Long
Attribute PairP2.VB_VarUserMemId = 1073938447
Private PairDX() As Single
Attribute PairDX.VB_VarUserMemId = 1073938448
Private PairDY() As Single
Attribute PairDY.VB_VarUserMemId = 1073938449
Private PairDZ() As Single
Attribute PairDZ.VB_VarUserMemId = 1610809349
Private PairD() As Single
Attribute PairD.VB_VarUserMemId = 1073938451
Private Npairs As Long
Attribute Npairs.VB_VarUserMemId = 1073938452
Private MAXNpair As Long
Attribute MAXNpair.VB_VarUserMemId = 1610809350
Private CountRN As Long
Attribute CountRN.VB_VarUserMemId = 1073938454
''Private XX() As Long
''Private YY() As Long
''Private ZZ() As Long
''Private Ninc As Long
''Private ZZpF() As Long
''Private ZZpT() As Long
Private iI As Long
Attribute iI.VB_VarUserMemId = 1073938455
Private PXI As Single
Attribute PXI.VB_VarUserMemId = 1073938456
Private PYI As Single
Attribute PYI.VB_VarUserMemId = 1073938457
Private PZI As Single
Attribute PZI.VB_VarUserMemId = 1073938458
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (ByVal pDest As Long, ByVal pSrc As Long, ByVal ByteLen As Long)
Private Declare Function GetMem4 Lib "MSVBVM60" (ByVal Src As Long, ByVal dest As Long) As Long
Private Declare Function ArrPtr Lib "MSVBVM60" Alias "VarPtr" (arr() As Any) As Long
Private Sub pvArrCopySingle(dest() As Single, Src() As Single)
Dim Size As Long
Dim W As Long
' Dim H As Long
' W = UBound(Src, 1)
' H = UBound(Src, 2)
' Size = (W + 1) * (H + 1) * LenB(Src(0, 0))
If pvArrayExists(ArrPtr(Src)) Then
W = UBound(Src)
Size = (W + 1) * LenB(Src(0))
If pvArrayExists(ArrPtr(dest)) Then
' If (W - UBound(dest, 1)) Or (H - UBound(dest, 2)) Then
If (W - UBound(dest)) > 0 Then
'ReDim dest(W, H)
ReDim dest(W)
End If
Else
'''Array DEST has No Dimension
'ReDim dest(W, H)
ReDim dest(W)
End If
' CopyMemory ByVal VarPtr(dest(0, 0)), ByVal VarPtr(Src(0, 0)), Size
CopyMemory ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0)), Size
End If
End Sub
Private Sub pvArrCopyLong(dest() As Long, Src() As Long)
Dim Size As Long
Dim W As Long
' Dim H As Long
' W = UBound(Src, 1)
' H = UBound(Src, 2)
' Size = (W + 1) * (H + 1) * LenB(Src(0, 0))
If pvArrayExists(ArrPtr(Src)) Then
W = UBound(Src)
Size = (W + 1) * LenB(Src(0))
If pvArrayExists(ArrPtr(dest)) Then
' If (W - UBound(dest, 1)) Or (H - UBound(dest, 2)) Then
If (W - UBound(dest)) > 0 Then
' Debug.Print W
' Stop
'ReDim dest(W, H)
ReDim dest(W)
End If
Else
'''Array DEST has No Dimension
'ReDim dest(W, H)
ReDim dest(W)
End If
' CopyMemory ByVal VarPtr(dest(0, 0)), ByVal VarPtr(Src(0, 0)), Size
CopyMemory ByVal VarPtr(dest(0)), ByVal VarPtr(Src(0)), Size
End If
End Sub
Private Function pvArrayExists(ByVal ppArray As Long) As Long
GetMem4 ppArray, VarPtr(pvArrayExists)
End Function
Public Sub Init(WorldW As Long, WorldH As Long, worldZ As Long, maxDist As Long)
GridSize = maxDist
MaxDistance2 = maxDist * maxDist
MAXCellX = WorldW \ GridSize
MAXCellY = WorldH \ GridSize
MAXCellZ = worldZ \ GridSize
MinCellX = 0
MinCellY = 0
MinCellZ = 0
'-1 / +1 because of pvX:
MAXCellX = MAXCellX + 1
MAXCellY = MAXCellY + 1
MAXCellZ = MAXCellZ + 1
MinCellX = MinCellX - 1
MinCellY = MinCellY - 1
MinCellZ = MinCellZ - 1
'-1 / +1 GetPairsWDist4 need to enlarge to avoid boundary check)
MAXCellX = MAXCellX + 1
MAXCellY = MAXCellY + 1
MAXCellZ = MAXCellZ + 1
MinCellX = MinCellX - 1
MinCellY = MinCellY - 1
MinCellZ = MinCellZ - 1
' ReDim CELL(0 To MAXCellX, 0 To MAXCellY, 0 To MAXCellZ) '
ReDim CELL(MinCellX To MAXCellX, MinCellY To MAXCellY, MinCellZ To MAXCellZ)
NP = 0
Npairs = 0
''' '--------------------------------------------------------------
''' Dim x1&, Y1&, z1&
''' Dim X2&, Y2&, z2&
'''
''' ReDim XX(1 To 8)
''' ReDim YY(1 To 8)
''' ReDim ZZ(1 To 8)
'''
'''
''' Ninc = 0
'''
''' For x1 = 0 To 1
''' For Y1 = 0 To 1
''' For z1 = 0 To 1
''' Ninc = Ninc + 1
''' XX(Ninc) = x1
''' YY(Ninc) = Y1
''' ZZ(Ninc) = z1
''' Next
''' Next
''' Next
'''
''' Dim I&, J&
''' Ninc = 0
''' For I = 1 To 7
''' For J = I + 1 To 8
'''
''' If Not (ZZpE(I, J)) Then
''' Ninc = Ninc + 1: ReDim Preserve ZZpF(Ninc): ReDim Preserve ZZpT(Ninc)
''' ZZpF(Ninc) = I
''' ZZpT(Ninc) = J
''' Debug.Print Ninc, , XX(I), YY(I), ZZ(I), , XX(J), YY(J), ZZ(J)
'''' Stop
''' End If
''' Next
''' Next
TESTdot
End Sub
'''Private Function ZZpE(I&, J&) As Boolean
''' Dim K As Long
'''
''' For K = 1 To Ninc
''' If ZZpF(K) = J And ZZpT(K) = I Then ZZpE = True
''' If ZZpF(K) = I And ZZpT(K) = J Then ZZpE = True
'''
''' If Ninc >= 7 Then
'''
''' If Abs(XX(I) - XX(J)) + Abs(YY(I) - YY(J)) + Abs(ZZ(I) - ZZ(J)) < 2 Then ZZpE = True
'''
''''If XX(ZZpF(I)) > XX(ZZpT(J)) Then ZZpE = True: Stop
''''If YY(ZZpF(I)) > YY(ZZpT(J)) Then ZZpE = True: Stop
''''If ZZ(ZZpF(I)) > ZZ(ZZpT(J)) Then ZZpE = True: Stop
'''
''' End If
''' Next
'''
'''End Function
Friend Sub ResetPoints()
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim I&
For Z = MinCellZ To MAXCellZ
For Y = MinCellY To MAXCellY
For X = MinCellX To MAXCellX
With CELL(X, Y, Z)
.NP = 0&
.myIDX = I: I = I + 1&
End With
Next
Next
Next
NP = 0
'......... Some memory saving
If (CNT And 255&) = 0& Then
For Z = MinCellZ To MAXCellZ
For Y = MinCellY To MAXCellY
For X = MinCellX To MAXCellX
With CELL(X, Y, Z)
.MaxNP = .MaxNP - 1
If .MaxNP < -10 Then
MaxNP = 0
ReDim px(MaxNP)
ReDim py(MaxNP)
ReDim pz(MaxNP)
End If
End With
Next
Next
Next
End If
End Sub
Friend Sub InsertPoint(ByVal X As Single, ByVal Y As Single, ByVal Z As Single)
NP = NP + 1
If NP > MaxNP Then
MaxNP = 1 + NP * 1.25
ReDim Preserve px(MaxNP)
ReDim Preserve py(MaxNP)
ReDim Preserve pz(MaxNP)
End If
px(NP) = X
py(NP) = Y
pz(NP) = Z
pvAddToCell X \ GridSize, Y \ GridSize, Z \ GridSize, NP
End Sub
Friend Sub InsertALLpoints(X() As Single, Y() As Single, Z() As Single)
'Remember to Call ResetPoints First
Dim I As Long
Dim U As Long
U = UBound(X)
For I = 1 To U
NP = NP + 1&
If NP > MaxNP Then
MaxNP = NP + 64 '* 2
' MaxNP = 1 + NP * 1.5
' ReDim Preserve PX(MaxNP)
' ReDim Preserve PY(MaxNP)
End If
' PX(NP) = X(I)
' PY(NP) = Y(I)
pvAddToCell X(I) \ GridSize, Y(I) \ GridSize, Z(I) \ GridSize, NP
Next
pvArrCopySingle px, X
pvArrCopySingle py, Y
pvArrCopySingle pz, Z
End Sub
Private Sub pvAddToCell(ByVal cellX As Long, ByVal cellY As Long, ByVal CellZ As Long, ByVal CurrP As Long)
With CELL(cellX, cellY, CellZ)
.NP = .NP + 1&
If .NP > .MaxNP Then
.MaxNP = 1 + .NP * 1.25
ReDim Preserve .pIDX(.MaxNP)
End If
.pIDX(.NP) = CurrP
End With
End Sub
Friend Sub GetPairsWDist(RP1() As Long, RP2() As Long, _
Rdx() As Single, Rdy() As Single, Rdz() As Single, _
rD() As Single, rPairsCount As Long)
'CORE SUB
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim DX As Single
Dim DY As Single
Dim DZ As Single
Dim I As Long
Dim J As Long
Dim D As Single
Dim Xp1 As Long
Dim Xm1 As Long
Dim Yp1 As Long
Dim Ym1 As Long
Dim Zp1 As Long
Dim Zm1 As Long
Dim iJ As Long
Npairs = 0&
For Z = MinCellZ To MAXCellZ
Zp1 = Z + 1&
Zm1 = Z - 1&
For Y = MinCellY To MAXCellY
Yp1 = Y + 1&
Ym1 = Y - 1
For X = MinCellX To MAXCellX
Xp1 = X + 1&
Xm1 = X - 1&
With CELL(X, Y, Z)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
iI = .pIDX(I)
PXI = px(iI)
PYI = py(iI)
PZI = pz(iI)
For J = I + 1& To .NP 'SELF
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
'--------------------------------------------
'--------------------------------------------
'--------------------------------------------
'--------------------------------------------
'--------------------------------------------
'-------------------------------- Along XY
''' If Xp1 <= MAXCellX Then
''' For J = 1& To CELL(Xp1, y, Z).NP ' X..
''' iJ = CELL(Xp1, y, Z).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' If Yp1 <= MAXCellY Then
''' For J = 1& To CELL(Xp1, Yp1, Z).NP 'XY.
''' iJ = CELL(Xp1, Yp1, Z).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' End If
''' If Yp1 <= MAXCellY Then
''' For J = 1& To CELL(X, Yp1, Z).NP '.Y.
''' iJ = CELL(X, Yp1, Z).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' '-------------------------------- Along XY at Z+1
''' If Zp1 <= MAXCellZ Then
''' If Xp1 <= MAXCellX Then
''' For J = 1& To CELL(Xp1, y, Zp1).NP ' X.Z
''' iJ = CELL(Xp1, y, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' If Yp1 <= MAXCellY Then
''' For J = 1& To CELL(Xp1, Yp1, Zp1).NP 'XYZ
''' iJ = CELL(Xp1, Yp1, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' End If
''' If Yp1 <= MAXCellY Then
''' For J = 1& To CELL(X, Yp1, Zp1).NP '.YZ
''' iJ = CELL(X, Yp1, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
'''
''' For J = 1& To CELL(X, y, Zp1).NP '..Z
''' iJ = CELL(X, y, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
'''
''' '-----------------------UP--------- Along XZ at y-1
''' If Ym1 >= 0& Then
''' If Xp1 <= MAXCellX Then
''' For J = 1& To CELL(Xp1, Ym1, Z).NP 'X -Y .
''' iJ = CELL(Xp1, Ym1, Z).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' If Zp1 <= MAXCellZ Then
''' For J = 1& To CELL(Xp1, Ym1, Zp1).NP 'X -Y Z
''' iJ = CELL(Xp1, Ym1, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' End If
''' If Zp1 <= MAXCellZ Then
''' For J = 1& To CELL(X, Ym1, Zp1).NP '. -Y Z
''' iJ = CELL(X, Ym1, Zp1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
'''
''' End If
'''
'''
'''
''' '----------------------------------------------
''' '----------------------------------------------
'''
''' If Xp1 <= MAXCellX Then
''' If Zm1 >= 0& Then
''' For J = 1& To CELL(Xp1, y, Zm1).NP ' X . -Z
''' iJ = CELL(Xp1, y, Zm1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' If Yp1 <= MAXCellY Then
''' For J = 1& To CELL(Xp1, Yp1, Zm1).NP ' X Y -Z
''' iJ = CELL(Xp1, Yp1, Zm1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' If Ym1 >= 0& Then
''' For J = 1& To CELL(Xp1, Ym1, Zm1).NP ' X -Y -Z
''' iJ = CELL(Xp1, Ym1, Zm1).pIDX(J)
''' dX = PX(iJ) - PXI
''' dY = PY(iJ) - PYI
''' dZ = PZ(iJ) - PZI
''' D = dX * dX + dY * dY + dZ * dZ
''' If D < MaxDistance2 Then pvAddPairWDist iI, iJ, dX, dY, dZ, D
''' Next
''' End If
''' End If
'''
'''
''' End If
'''' ' --------- SECONDA PROVA
If Xp1 <= MAXCellX Then
With CELL(Xp1, Y, Z)
For J = 1& To .NP ' X . .
'iJ = CELL(Xp1, y, z).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
If Ym1 >= MinCellY Then
With CELL(Xp1, Ym1, Z)
For J = 1& To .NP ' X -Y .
'iJ = CELL(Xp1, Ym1, z).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
If Yp1 <= MAXCellY Then
With CELL(Xp1, Yp1, Z)
For J = 1& To .NP ' X Y .
'iJ = CELL(Xp1, Yp1, z).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
End If
If Yp1 <= MAXCellY Then
With CELL(X, Yp1, Z)
For J = 1& To .NP ' . Y .
'iJ = CELL(x, Yp1, z).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
'------------------------------------
If Zp1 <= MAXCellZ Then
If Xp1 <= MAXCellX Then ' X + 1
With CELL(Xp1, Y, Zp1)
For J = 1& To .NP ' X . Z
'iJ = CELL(Xp1, y, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
If Yp1 <= MAXCellY Then
With CELL(Xp1, Yp1, Zp1)
For J = 1& To .NP ' X Y Z
'iJ = CELL(Xp1, Yp1, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
If Ym1 >= MinCellY Then
With CELL(Xp1, Ym1, Zp1)
For J = 1& To .NP ' X -Y Z
'iJ = CELL(Xp1, Ym1, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
End If
' X + 0
With CELL(X, Y, Zp1)
For J = 1& To .NP ' . . Z
'iJ = CELL(x, y, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
If Yp1 <= MAXCellY Then
With CELL(X, Yp1, Zp1)
For J = 1& To .NP ' . Y Z
'iJ = CELL(x, Yp1, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
If Ym1 >= MinCellY Then
With CELL(X, Ym1, Zp1)
For J = 1& To .NP ' . -Y Z
'iJ = CELL(x, Ym1, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
If Xm1 >= MinCellX Then 'MAXCellX Then 2022 BUG FIX ' X - 1
With CELL(Xm1, Y, Zp1)
For J = 1& To .NP ' -X . Z
'iJ = CELL(Xm1, y, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
If Yp1 <= MAXCellY Then
With CELL(Xm1, Yp1, Zp1)
For J = 1& To .NP ' -X Y Z
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
If Ym1 >= MinCellY Then
With CELL(Xm1, Ym1, Zp1)
For J = 1& To .NP ' -X -Y Z
' iJ = CELL(Xm1, Ym1, Zp1).pIDX(J)
iJ = .pIDX(J)
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
Next
End With
End If
End If
End If
Next I
End With
Next X
Next Y
Next Z
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
'--------------------------------------------------------------------------------------------------
rPairsCount = Npairs
pvArrCopyLong RP1, PairP1
pvArrCopyLong RP2, PairP2
pvArrCopySingle Rdx, PairDX
pvArrCopySingle Rdy, PairDY
pvArrCopySingle Rdz, PairDZ
pvArrCopySingle rD, PairD
'----------------------------------------
' Redim to low value if for many times actual Npairs is less than upper bound Pairs Array
' Honestly Don't know if somehow could improve perforamnces
If Npairs < UBound(PairP1) Then
CountRN = CountRN + 1
If CountRN > 200 Then
MAXNpair = Npairs + 1
ReDim Preserve PairP1(MAXNpair)
ReDim Preserve PairP2(MAXNpair)
ReDim Preserve PairDX(MAXNpair)
ReDim Preserve PairDY(MAXNpair)
ReDim Preserve PairDZ(MAXNpair)
ReDim Preserve PairD(MAXNpair)
CountRN = 0
End If
Else
CountRN = 0
End If
''Check duplicates
' For X = 1 To CountRN - 1
' For Y = X + 1 To CountRN
' If (PairP1(X) = PairP1(Y)) And (PairP2(X) = PairP2(Y)) Then Stop
' If (PairP1(X) = PairP2(Y)) And (PairP2(X) = PairP1(Y)) Then Stop
' Next
' Next
End Sub
Private Sub pvAddPairWDist(ByVal P1 As Long, ByVal P2 As Long, _
ByVal DX As Single, ByVal DY As Single, ByVal DZ As Single, _
ByVal D As Single)
Npairs = Npairs + 1&
If Npairs > MAXNpair Then
MAXNpair = (Npairs + 2) * 1.25
ReDim Preserve PairP1(MAXNpair)
ReDim Preserve PairP2(MAXNpair)
ReDim Preserve PairDX(MAXNpair)
ReDim Preserve PairDY(MAXNpair)
ReDim Preserve PairDZ(MAXNpair)
ReDim Preserve PairD(MAXNpair)
End If
PairP1(Npairs) = P1
PairP2(Npairs) = P2
PairDX(Npairs) = DX
PairDY(Npairs) = DY
PairDZ(Npairs) = DZ
PairD(Npairs) = D
End Sub
Friend Sub GetPairsWDist2(RP1() As Long, RP2() As Long, _
Rdx() As Single, Rdy() As Single, Rdz() As Single, _
rD() As Single, rPairsCount As Long)
'prova ----- non usare....
Dim X As Long
Dim Y As Long
Dim Z As Long
Dim DX As Single
Dim DY As Single
Dim DZ As Single
Dim I As Long
Dim J As Long
Dim D As Single
Dim cWm1 As Long
Dim cHm1 As Long
Dim cZm1 As Long
Dim Xp1 As Long
Dim Xm1 As Long
Dim Yp1 As Long
Dim Ym1 As Long
Dim Zp1 As Long
Dim Zm1 As Long
Dim iJ As Long
Dim iI As Long
Dim PXI As Single
Dim PYI As Single
Dim PZI As Single
cWm1 = MAXCellX '- 1&
cHm1 = MAXCellY '- 1&
cZm1 = MAXCellZ '- 1&
Npairs = 0&
Dim X2 As Long
Dim Y2 As Long
Dim z2 As Long
For X = 0& To MAXCellX
Xp1 = X + 1&
Xm1 = X - 1&
If Xp1 > MAXCellX Then Xp1 = MAXCellX
If Xm1 < 0& Then Xm1 = 0&
For Y = 0& To MAXCellY
Yp1 = Y + 1&
Ym1 = Y - 1
If Yp1 > MAXCellY Then Yp1 = MAXCellY
If Ym1 < 0& Then Ym1 = 0&
For Z = 0& To MAXCellZ
Zp1 = Z + 1&
Zm1 = Z - 1&
If Zp1 > MAXCellZ Then Zp1 = MAXCellZ
If Zm1 < 0& Then Zm1 = 0&
With CELL(X, Y, Z)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
iI = .pIDX(I)
PXI = px(iI)
PYI = py(iI)
PZI = pz(iI)
For X2 = X To Xp1
For Y2 = Y To Yp1
For z2 = Z To Zp1
For J = 1& To CELL(X2, Y2, z2).NP ' X . .
iJ = CELL(X2, Y2, z2).pIDX(J)
If iJ - iI Then
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI
D = DX * DX + DY * DY + DZ * DZ
If D < MaxDistance2 Then pvAddPairWDist iI, iJ, DX, DY, DZ, D
End If
Next
Next
Next
Next
Next I
End With
If Xp1 - X Then
With CELL(Xp1, Y, Z)
For I = 1& To .NP '- 1& ' Should be -1 to do only SELF but so we can do even others
iI = .pIDX(I)
PXI = px(iI)
PYI = py(iI)
PZI = pz(iI)
X2 = X
Y2 = Yp1
z2 = Z
If Y - Yp1 Then
For J = 1& To CELL(X2, Y2, z2).NP ' X . .
iJ = CELL(X2, Y2, z2).pIDX(J)
If iJ - iI Then
DX = px(iJ) - PXI
DY = py(iJ) - PYI
DZ = pz(iJ) - PZI