forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkStaticPointLocator.cxx
2119 lines (1887 loc) · 63.4 KB
/
vtkStaticPointLocator.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkStaticPointLocator.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkStaticPointLocator.h"
#include "vtkCellArray.h"
#include "vtkIdList.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkBoundingBox.h"
#include "vtkBox.h"
#include "vtkLine.h"
#include "vtkSMPTools.h"
#include "vtkSMPThreadLocalObject.h"
#include <vector>
vtkStandardNewMacro(vtkStaticPointLocator);
// There are stack-allocated bucket neighbor lists. This is the initial
// value. Too small and heap allocation kicks in.
#define VTK_INITIAL_BUCKET_SIZE 10000
//-----------------------------------------------------------------------------
// The following code supports threaded point locator construction. The locator
// is assumed to be constructed once (i.e., it does not allow incremental point
// insertion). The algorithm proceeds in three steps:
// 1) All points are assigned a bucket index (combined i-j-k bucket location).
// The index is computed in parallel. This requires a one time allocation of an
// index array (which is also associated with the originating point ids).
// 2) vtkSMPTools::Sort() is used to sort the index array. Note that the sort
// carries along the point ids as well. This creates contiguous runs of points
// all resident in the same bucket.
// 3) The bucket offsets are updated to refer to the right entry location into
// the sorted point ids array. This enables quick access, and an indirect count
// of the number of points in each bucket.
// Believe it or not I had to change the name because MS Visual Studio was
// mistakenly linking the hidden, scoped classes (vtkNeighborBuckets) found
// in vtkPointLocator and vtkStaticPointLocator and causing weird faults.
struct NeighborBuckets;
//-----------------------------------------------------------------------------
// The bucketed points, including the sorted map. This is just a PIMPLd
// wrapper around the classes that do the real work.
struct vtkBucketList
{
vtkStaticPointLocator *Locator; //locater
vtkIdType NumPts; //the number of points to bucket
vtkIdType NumBuckets;
int BatchSize;
// These are internal data members used for performance reasons
vtkDataSet *DataSet;
int Divisions[3];
double Bounds[6];
double H[3];
double hX, hY, hZ;
double fX, fY, fZ, bX, bY, bZ;
vtkIdType xD, yD, zD, xyD;
// Construction
vtkBucketList(vtkStaticPointLocator *loc, vtkIdType numPts, int numBuckets)
{
this->Locator = loc;
this->NumPts = numPts;
this->NumBuckets = numBuckets;
this->BatchSize = 10000; //building the offset array
this->DataSet = loc->GetDataSet();
loc->GetDivisions(this->Divisions);
// Setup internal data members for more efficient processing.
double spacing[3], bounds[6];
loc->GetDivisions(this->Divisions);
loc->GetSpacing(spacing);
loc->GetBounds(bounds);
this->hX = this->H[0] = spacing[0];
this->hY = this->H[1] = spacing[1];
this->hZ = this->H[2] = spacing[2];
this->fX = 1.0 / spacing[0];
this->fY = 1.0 / spacing[1];
this->fZ = 1.0 / spacing[2];
this->bX = this->Bounds[0] = bounds[0];
this->Bounds[1] = bounds[1];
this->bY = this->Bounds[2] = bounds[2];
this->Bounds[3] = bounds[3];
this->bZ = this->Bounds[4] = bounds[4];
this->Bounds[5] = bounds[5];
this->xD = this->Divisions[0];
this->yD = this->Divisions[1];
this->zD = this->Divisions[2];
this->xyD = this->Divisions[0] * this->Divisions[1];
}
// Virtuals for templated subclasses
virtual ~vtkBucketList() = default;
virtual void BuildLocator() = 0;
// place points in appropriate buckets
void GetBucketNeighbors(NeighborBuckets* buckets,
const int ijk[3], const int ndivs[3], int level);
void GenerateFace(int face, int i, int j, int k,
vtkPoints *pts, vtkCellArray *polys);
double Distance2ToBucket(const double x[3], const int nei[3]);
double Distance2ToBounds(const double x[3], const double bounds[6]);
//-----------------------------------------------------------------------------
// Inlined for performance. These function invocations must be called after
// BuildLocator() is invoked, otherwise the output is indeterminate.
void GetBucketIndices(const double *x, int ijk[3]) const
{
// Compute point index. Make sure it lies within range of locator.
vtkIdType tmp0 = static_cast<vtkIdType>(((x[0] - bX) * fX));
vtkIdType tmp1 = static_cast<vtkIdType>(((x[1] - bY) * fY));
vtkIdType tmp2 = static_cast<vtkIdType>(((x[2] - bZ) * fZ));
ijk[0] = tmp0 < 0 ? 0 : (tmp0 >= xD ? xD-1 : tmp0);
ijk[1] = tmp1 < 0 ? 0 : (tmp1 >= yD ? yD-1 : tmp1);
ijk[2] = tmp2 < 0 ? 0 : (tmp2 >= zD ? zD-1 : tmp2);
}
//-----------------------------------------------------------------------------
vtkIdType GetBucketIndex(const double *x) const
{
int ijk[3];
this->GetBucketIndices(x, ijk);
return ijk[0] + ijk[1]*xD + ijk[2]*xyD;
}
};
//-----------------------------------------------------------------------------
// Utility class to store an array of ijk values
struct NeighborBuckets
{
NeighborBuckets()
{
this->Count = 0;
this->P = this->InitialBuffer;
this->MaxSize = VTK_INITIAL_BUCKET_SIZE;
}
~NeighborBuckets()
{
this->Count = 0;
if ( this->P != this->InitialBuffer )
{
delete[] this->P;
}
}
int GetNumberOfNeighbors() { return this->Count; }
void Reset() { this->Count = 0; }
int *GetPoint(vtkIdType i)
{
return this->P + 3*i;
}
vtkIdType InsertNextBucket(const int x[3])
{
// Re-allocate if beyond the current max size.
// (Increase by VTK_INITIAL_BUCKET_SIZE)
int *tmp;
vtkIdType offset=this->Count*3;
if (this->Count >= this->MaxSize)
{
tmp = this->P;
this->MaxSize *= 2;
this->P = new int[this->MaxSize*3];
memcpy(this->P, tmp, offset*sizeof(int));
if ( tmp != this->InitialBuffer )
{
delete [] tmp;
}
}
tmp = this->P + offset;
*tmp++ = *x++;
*tmp++ = *x++;
*tmp = *x;
this->Count++;
return this->Count-1;
}
protected:
// Start with an array to avoid memory allocation overhead
int InitialBuffer[VTK_INITIAL_BUCKET_SIZE*3];
int *P;
vtkIdType Count;
vtkIdType MaxSize;
};
//-----------------------------------------------------------------------------
// Internal function to get bucket neighbors at specified level
//
void vtkBucketList::
GetBucketNeighbors(NeighborBuckets* buckets, const int ijk[3],
const int ndivs[3], int level)
{
int i, j, k, min, max, minLevel[3], maxLevel[3];
int nei[3];
// Initialize
//
buckets->Reset();
// If at this bucket, just place into list
//
if ( level == 0 )
{
buckets->InsertNextBucket(ijk);
return;
}
// Create permutations of the ijk indices that are at the level
// required. If these are legal buckets, add to list for searching.
//
for ( i=0; i<3; i++ )
{
min = ijk[i] - level;
max = ijk[i] + level;
minLevel[i] = ( min > 0 ? min : 0);
maxLevel[i] = ( max < (ndivs[i]-1) ? max : (ndivs[i]-1));
}
for ( i= minLevel[0]; i <= maxLevel[0]; i++ )
{
for ( j= minLevel[1]; j <= maxLevel[1]; j++ )
{
for ( k= minLevel[2]; k <= maxLevel[2]; k++ )
{
if (i == (ijk[0] + level) || i == (ijk[0] - level) ||
j == (ijk[1] + level) || j == (ijk[1] - level) ||
k == (ijk[2] + level) || k == (ijk[2] - level) )
{
nei[0]=i; nei[1]=j; nei[2]=k;
buckets->InsertNextBucket(nei);
}
}
}
}
}
//-----------------------------------------------------------------------------
void vtkBucketList::
GenerateFace(int face, int i, int j, int k, vtkPoints *pts, vtkCellArray *polys)
{
vtkIdType ids[4];
double origin[3], x[3];
// define first corner
origin[0] = this->bX + i * this->hX;
origin[1] = this->bY + j * this->hY;
origin[2] = this->bZ + k * this->hZ;
ids[0] = pts->InsertNextPoint(origin);
if ( face == 0 ) //x face
{
x[0] = origin[0];
x[1] = origin[1] + this->hY;
x[2] = origin[2];
ids[1] = pts->InsertNextPoint(x);
x[0] = origin[0];
x[1] = origin[1] + this->hY;
x[2] = origin[2] + this->hZ;
ids[2] = pts->InsertNextPoint(x);
x[0] = origin[0];
x[1] = origin[1];
x[2] = origin[2] + this->hZ;
ids[3] = pts->InsertNextPoint(x);
}
else if ( face == 1 ) //y face
{
x[0] = origin[0] + this->hX;
x[1] = origin[1];
x[2] = origin[2];
ids[1] = pts->InsertNextPoint(x);
x[0] = origin[0] + this->hX;
x[1] = origin[1];
x[2] = origin[2] + this->hZ;
ids[2] = pts->InsertNextPoint(x);
x[0] = origin[0];
x[1] = origin[1];
x[2] = origin[2] + this->hZ;
ids[3] = pts->InsertNextPoint(x);
}
else //z face
{
x[0] = origin[0] + this->hX;
x[1] = origin[1];
x[2] = origin[2];
ids[1] = pts->InsertNextPoint(x);
x[0] = origin[0] + this->hX;
x[1] = origin[1] + this->hY;
x[2] = origin[2];
ids[2] = pts->InsertNextPoint(x);
x[0] = origin[0];
x[1] = origin[1] + this->hY;
x[2] = origin[2];
ids[3] = pts->InsertNextPoint(x);
}
polys->InsertNextCell(4,ids);
}
//-----------------------------------------------------------------------------
// Calculate the distance between the point x to the bucket "nei".
//
// WARNING!!!!! Be very careful altering this routine. Simple changes to this
// routine can make is 25% slower!!!!
//
double vtkBucketList::
Distance2ToBucket(const double x[3], const int nei[3])
{
double bounds[6];
bounds[0] = nei[0]*this->hX + this->bX;
bounds[1] = (nei[0]+1)*this->hX + this->bX;
bounds[2] = nei[1]*this->hY + this->bY;
bounds[3] = (nei[1]+1)*this->hY + this->bY;
bounds[4] = nei[2]*this->hZ + this->bZ;
bounds[5] = (nei[2]+1)*this->hZ + this->bZ;
return this->Distance2ToBounds(x, bounds);
}
//-----------------------------------------------------------------------------
// Calculate the distance between the point x and the specified bounds
//
// WARNING!!!!! Be very careful altering this routine. Simple changes to this
// routine can make is 25% slower!!!!
double vtkBucketList::
Distance2ToBounds(const double x[3], const double bounds[6])
{
double distance;
double deltas[3];
// Are we within the bounds?
if (x[0] >= bounds[0] && x[0] <= bounds[1]
&& x[1] >= bounds[2] && x[1] <= bounds[3]
&& x[2] >= bounds[4] && x[2] <= bounds[5])
{
return 0.0;
}
deltas[0] = deltas[1] = deltas[2] = 0.0;
// dx
//
if (x[0] < bounds[0])
{
deltas[0] = bounds[0] - x[0];
}
else if (x[0] > bounds[1])
{
deltas[0] = x[0] - bounds[1];
}
// dy
//
if (x[1] < bounds[2])
{
deltas[1] = bounds[2] - x[1];
}
else if (x[1] > bounds[3])
{
deltas[1] = x[1] - bounds[3];
}
// dz
//
if (x[2] < bounds[4])
{
deltas[2] = bounds[4] - x[2];
}
else if (x[2] > bounds[5])
{
deltas[2] = x[2] - bounds[5];
}
distance = vtkMath::Dot(deltas, deltas);
return distance;
}
//-----------------------------------------------------------------------------
// The following tuple is what is sorted in the map. Note that it is templated
// because depending on the number of points / buckets to process we may want
// to use vtkIdType. Otherwise for performance reasons it's best to use an int
// (or other integral type). Typically sort() is 25-30% faster on smaller
// integral types, plus it takes a heck less memory (when vtkIdType is 64-bit
// and int is 32-bit).
template <typename TTuple>
struct LocatorTuple
{
TTuple PtId; //originating point id
TTuple Bucket; //i-j-k index into bucket space
// Operator< used to support the subsequent sort operation. There are two
// implementations, one gives a stable sort (points ordered by id within
// each bucket) and the other a little faster but less stable (in parallel
// sorting the order of sorted points in a bucket may vary).
// bool operator< (const LocatorTuple& tuple) const
// {return Bucket < tuple.Bucket;}
bool operator< (const LocatorTuple& tuple) const
{
if ( Bucket < tuple.Bucket ) return true;
if ( tuple.Bucket < Bucket ) return false;
if ( PtId < tuple.PtId ) return true;
return false;
}
};
//-----------------------------------------------------------------------------
// This templates class manages the creation of the static locator
// structures. It also implements the operator() functors which are supplied
// to vtkSMPTools for threaded processesing.
template <typename TIds>
struct BucketList : public vtkBucketList
{
// Okay the various ivars
LocatorTuple<TIds> *Map; //the map to be sorted
TIds *Offsets; //offsets for each bucket into the map
// Construction
BucketList(vtkStaticPointLocator *loc, vtkIdType numPts, int numBuckets) :
vtkBucketList(loc, numPts, numBuckets)
{
//one extra to simplify traversal
this->Map = new LocatorTuple<TIds>[numPts+1];
this->Map[numPts].Bucket = numBuckets;
this->Offsets = new TIds[numBuckets+1];
this->Offsets[numBuckets] = numPts;
}
// Release allocated memory
~BucketList() override
{
delete [] this->Map;
delete [] this->Offsets;
}
// The number of point ids in a bucket is determined by computing the
// difference between the offsets into the sorted points array.
vtkIdType GetNumberOfIds(vtkIdType bucketNum)
{
return (this->Offsets[bucketNum+1] - this->Offsets[bucketNum]);
}
// Given a bucket number, return the point ids in that bucket.
const LocatorTuple<TIds> *GetIds(vtkIdType bucketNum)
{
return this->Map + this->Offsets[bucketNum];
}
// Given a bucket number, return the point ids in that bucket.
void GetIds(vtkIdType bucketNum, vtkIdList *bList)
{
const LocatorTuple<TIds> *ids = this->GetIds(bucketNum);
vtkIdType numIds = this->GetNumberOfIds(bucketNum);
bList->SetNumberOfIds(numIds);
for (int i=0; i < numIds; i++)
{
bList->SetId(i,ids[i].PtId);
}
}
// Templated implementations of the locator
vtkIdType FindClosestPoint(const double x[3]);
vtkIdType FindClosestPointWithinRadius(double radius, const double x[3],
double inputDataLength, double& dist2);
void FindClosestNPoints(int N, const double x[3], vtkIdList *result);
void FindPointsWithinRadius(double R, const double x[3], vtkIdList *result);
int IntersectWithLine(double a0[3], double a1[3], double tol, double& t,
double lineX[3], double ptX[3], vtkIdType &ptId);
void MergePoints(double tol, vtkIdType *pointMap);
void GenerateRepresentation(int vtkNotUsed(level), vtkPolyData *pd);
// Internal methods
void GetOverlappingBuckets(NeighborBuckets* buckets, const double x[3],
const int ijk[3], double dist, int level);
void GetOverlappingBuckets(NeighborBuckets* buckets,const double x[3],
double dist, int prevMinLevel[3], int prevMaxLevel[3]);
// Implicit point representation, slower path
template <typename T>
struct MapDataSet
{
BucketList<T> *BList;
vtkDataSet *DataSet;
MapDataSet(BucketList<T> *blist, vtkDataSet *ds) :
BList(blist), DataSet(ds)
{
}
void operator()(vtkIdType ptId, vtkIdType end)
{
double p[3];
LocatorTuple<T> *t = this->BList->Map + ptId;
for ( ; ptId < end; ++ptId, ++t )
{
this->DataSet->GetPoint(ptId,p);
t->PtId = ptId;
t->Bucket = this->BList->GetBucketIndex(p);
}//for all points in this batch
}
};
// Explicit point representation (e.g., vtkPointSet), faster path
template <typename T, typename TPts>
struct MapPointsArray
{
BucketList<T> *BList;
const TPts *Points;
MapPointsArray(BucketList<T> *blist, const TPts *pts) :
BList(blist), Points(pts)
{
}
void operator()(vtkIdType ptId, vtkIdType end)
{
double p[3];
const TPts *x = this->Points + 3*ptId;
LocatorTuple<T> *t = this->BList->Map + ptId;
for ( ; ptId < end; ++ptId, x+=3, ++t )
{
p[0] = static_cast<double>(x[0]);
p[1] = static_cast<double>(x[1]);
p[2] = static_cast<double>(x[2]);
t->PtId = ptId;
t->Bucket = this->BList->GetBucketIndex(p);
}//for all points in this batch
}
};
// A clever way to build offsets in parallel. Basically each thread builds
// offsets across a range of the sorted map. Recall that offsets are an
// integral value referring to the locations of the sorted points that
// reside in each bucket.
template <typename T>
struct MapOffsets
{
BucketList<T> *BList;
vtkIdType NumPts;
int NumBuckets;
MapOffsets(BucketList<T> *blist) : BList(blist)
{
this->NumPts = this->BList->NumPts;
this->NumBuckets = this->BList->NumBuckets;
}
// Traverse sorted points (i.e., tuples) and update bucket offsets.
void operator()(vtkIdType batch, vtkIdType batchEnd)
{
T *offsets = this->BList->Offsets;
const LocatorTuple<T> *curPt =
this->BList->Map + batch*this->BList->BatchSize;
const LocatorTuple<T> *endBatchPt =
this->BList->Map + batchEnd*this->BList->BatchSize;
const LocatorTuple<T> *endPt =
this->BList->Map + this->NumPts;
const LocatorTuple<T> *prevPt;
endBatchPt = ( endBatchPt > endPt ? endPt : endBatchPt );
// Special case at the very beginning of the mapped points array. If
// the first point is in bucket# N, then all buckets up and including
// N must refer to the first point.
if ( curPt == this->BList->Map )
{
prevPt = this->BList->Map;
std::fill_n(offsets, curPt->Bucket+1, 0); //point to the first points
}//at the very beginning of the map (sorted points array)
// We are entering this functor somewhere in the interior of the
// mapped points array. All we need to do is point to the entry
// position because we are interested only in prevPt->Bucket.
else
{
prevPt = curPt;
}//else in the middle of a batch
// Okay we have a starting point for a bucket run. Now we can begin
// filling in the offsets in this batch. A previous thread should
// have/will have completed the previous and subsequent runs outside
// of the [batch,batchEnd) range
for ( curPt=prevPt; curPt < endBatchPt; )
{
for ( ; curPt->Bucket == prevPt->Bucket && curPt <= endBatchPt;
++curPt )
{
; //advance
}
// Fill in any gaps in the offset array
std::fill_n(offsets + prevPt->Bucket + 1,
curPt->Bucket - prevPt->Bucket,
curPt - this->BList->Map);
prevPt = curPt;
}//for all batches in this range
}//operator()
};
// Merge points that are pecisely coincident. Operates in parallel on
// locator buckets. Does not need to check neighbor buckets.
template <typename T>
struct MergePrecise
{
BucketList<T> *BList;
vtkDataSet *DataSet;
vtkIdType *MergeMap;
MergePrecise(BucketList<T> *blist, vtkIdType *mergeMap) :
BList(blist), MergeMap(mergeMap)
{
this->DataSet = blist->DataSet;
}
void operator()(vtkIdType bucket, vtkIdType endBucket)
{
BucketList<T> *bList=this->BList;
vtkIdType *mergeMap=this->MergeMap;
int i, j;
const LocatorTuple<TIds> *ids;
double p[3], p2[3];
vtkIdType ptId, ptId2, numIds;
for ( ; bucket < endBucket; ++bucket )
{
if ( (numIds = bList->GetNumberOfIds(bucket)) > 0 )
{
ids = bList->GetIds(bucket);
for (i=0; i < numIds; i++)
{
ptId = ids[i].PtId;
if ( mergeMap[ptId] < 0 )
{
mergeMap[ptId] = ptId;
this->DataSet->GetPoint(ptId, p);
for (j=i+1; j < numIds; j++)
{
ptId2 = ids[j].PtId;
if ( mergeMap[ptId2] < 0 )
{
this->DataSet->GetPoint(ptId2, p2);
if ( p[0] == p2[0] && p[1] == p2[1] && p[2] == p2[2] )
{
mergeMap[ptId2] = ptId;
}
}
}
} //if point not yet visited
}
}
}
}
};
// Merge points that are coincident within a tolerance. Operates in
// parallel on points. Needs to check neighbor buckets which slows it down
// considerably. Note that merging is one direction: larger ids are merged
// to lower.
template <typename T>
struct MergeClose
{
BucketList<T> *BList;
vtkDataSet *DataSet;
vtkIdType *MergeMap;
double Tol;
vtkSMPThreadLocalObject<vtkIdList> PIds;
MergeClose(BucketList<T> *blist, double tol, vtkIdType *mergeMap) :
BList(blist), MergeMap(mergeMap), Tol(tol)
{
this->DataSet = blist->DataSet;
}
// Just allocate a little bit of memory to get started.
void Initialize()
{
vtkIdList*& pIds = this->PIds.Local();
pIds->Allocate(128); //allocate some memory
}
void operator()(vtkIdType ptId, vtkIdType endPtId)
{
BucketList<T> *bList=this->BList;
vtkIdType *mergeMap=this->MergeMap;
int i;
double p[3];
vtkIdType nearId, numIds;
vtkIdList*& nearby = this->PIds.Local();
for ( ; ptId < endPtId; ++ptId )
{
if ( mergeMap[ptId] < 0 )
{
mergeMap[ptId] = ptId;
this->DataSet->GetPoint(ptId, p);
bList->FindPointsWithinRadius(this->Tol, p, nearby);
if ( (numIds = nearby->GetNumberOfIds()) > 0 )
{
for (i=0; i < numIds; i++)
{
nearId = nearby->GetId(i);
if ( ptId < nearId &&
(mergeMap[nearId] < 0 || ptId < mergeMap[nearId]) )
{
mergeMap[nearId] = ptId;
}
}
}
}//if point not yet processed
}//for all points in this batch
}
void Reduce()
{}
};
// Build the map and other structures to support locator operations
void BuildLocator() override
{
// Place each point in a bucket
//
vtkPointSet *ps=static_cast<vtkPointSet *>(this->DataSet);
int mapped=0;
if ( ps )
{//map points array: explicit points representation
int dataType = ps->GetPoints()->GetDataType();
void *pts = ps->GetPoints()->GetVoidPointer(0);
if ( dataType == VTK_FLOAT )
{
MapPointsArray<TIds,float> mapper(this,static_cast<float*>(pts));
vtkSMPTools::For(0,this->NumPts, mapper);
mapped = 1;
}
else if ( dataType == VTK_DOUBLE )
{
MapPointsArray<TIds,double> mapper(this,static_cast<double*>(pts));
vtkSMPTools::For(0,this->NumPts, mapper);
mapped = 1;
}
}
if ( ! mapped )
{//map dataset points: non-float points or implicit points representation
MapDataSet<TIds> mapper(this,this->DataSet);
vtkSMPTools::For(0,this->NumPts, mapper);
}
// Now gather the points into contiguous runs in buckets
//
vtkSMPTools::Sort(this->Map, this->Map + this->NumPts);
// Build the offsets into the Map. The offsets are the positions of
// each bucket into the sorted list. They mark the beginning of the
// list of points in each bucket. Amazingly, this can be done in
// parallel.
//
int numBatches = static_cast<int>(
ceil(static_cast<double>(this->NumPts) / this->BatchSize));
MapOffsets<TIds> offMapper(this);
vtkSMPTools::For(0,numBatches, offMapper);
}
};
//-----------------------------------------------------------------------------
// Given a position x, return the id of the point closest to it.
template <typename TIds> vtkIdType BucketList<TIds>::
FindClosestPoint(const double x[3])
{
int i, j;
double minDist2;
double dist2 = VTK_DOUBLE_MAX;
double pt[3];
int closest, level;
vtkIdType ptId, cno, numIds;
int ijk[3], *nei;
NeighborBuckets buckets;
const LocatorTuple<TIds> *ids;
// Find bucket point is in.
//
this->GetBucketIndices(x, ijk);
// Need to search this bucket for the closest point. If there are no
// points in this bucket, search 1st level neighbors, and so on, until
// closest point found.
//
for (closest=(-1),minDist2=VTK_DOUBLE_MAX,level=0; (closest == -1) &&
(level < this->Divisions[0] || level < this->Divisions[1] ||
level < this->Divisions[2]); level++)
{
this->GetBucketNeighbors (&buckets, ijk, this->Divisions, level);
for (i=0; i<buckets.GetNumberOfNeighbors(); i++)
{
nei = buckets.GetPoint(i);
cno = nei[0] + nei[1]*this->xD + nei[2]*this->xyD;
if ( (numIds = this->GetNumberOfIds(cno)) > 0 )
{
ids = this->GetIds(cno);
for (j=0; j < numIds; j++)
{
ptId = ids[j].PtId;
this->DataSet->GetPoint(ptId, pt);
if ( (dist2 = vtkMath::Distance2BetweenPoints(x,pt)) < minDist2 )
{
closest = ptId;
minDist2 = dist2;
}
}
}
}
}
//
// Because of the relative location of the points in the buckets, the
// point found previously may not be the closest point. We have to
// search those bucket neighbors that might also contain the point.
//
if ( minDist2 > 0.0 )
{
this->GetOverlappingBuckets (&buckets, x, ijk, sqrt(minDist2), 0);
for (i=0; i<buckets.GetNumberOfNeighbors(); i++)
{
nei = buckets.GetPoint(i);
cno = nei[0] + nei[1]*this->xD + nei[2]*this->xyD;
if ( (numIds = this->GetNumberOfIds(cno)) > 0 )
{
ids = this->GetIds(cno);
for (j=0; j < numIds; j++)
{
ptId = ids[j].PtId;
this->DataSet->GetPoint(ptId, pt);
if ( (dist2 = vtkMath::Distance2BetweenPoints(x,pt)) < minDist2 )
{
closest = ptId;
minDist2 = dist2;
}
}//for each point
}//if points in bucket
}//for each overlapping bucket
}//if not identical point
return closest;
}
//-----------------------------------------------------------------------------
template <typename TIds> vtkIdType BucketList<TIds>::
FindClosestPointWithinRadius(double radius, const double x[3],
double inputDataLength, double& dist2)
{
int i, j;
double pt[3];
vtkIdType ptId, closest = -1;
int ijk[3], *nei;
double minDist2;
double refinedRadius, radius2, refinedRadius2;
double currentRadius;
double distance2ToDataBounds, maxDistance;
int ii, radiusLevels[3], radiusLevel, prevMinLevel[3], prevMaxLevel[3];
NeighborBuckets buckets;
const LocatorTuple<TIds> *ids;
// Initialize
dist2 = -1.0;
radius2 = radius*radius;
minDist2 = 1.01*radius2; // something slightly bigger....
vtkDataArray *pointData =
static_cast<vtkPointSet *>(this->DataSet)->GetPoints()->GetData();
// Find the bucket the point is in.
//
this->GetBucketIndices(x, ijk);
// Start by searching the bucket that the point is in.
//
vtkIdType numIds;
vtkIdType cno = ijk[0] + ijk[1]*this->xD + ijk[2]*this->xyD;
if ( (numIds = this->GetNumberOfIds(cno)) > 0 )
{
ids = this->GetIds(cno);
for (j=0; j < numIds; j++)
{
ptId = ids[j].PtId;
pointData->GetTuple(ptId, pt);
if ( (dist2 = vtkMath::Distance2BetweenPoints(x,pt)) < minDist2 )
{
closest = ptId;
minDist2 = dist2;
}
}
}
// Now, search only those buckets that are within a radius. The radius used
// is the smaller of sqrt(minDist2) and the radius that is passed in. To avoid
// checking a large number of buckets unnecessarily, if the radius is
// larger than the dimensions of a bucket, we search outward using a
// simple heuristic of rings. This heuristic ends up collecting inner
// buckets multiple times, but this only happens in the case where these
// buckets are empty, so they are discarded quickly.
//
if ( minDist2 < radius2 )
{
refinedRadius = sqrt(minDist2);
refinedRadius2 = dist2;
}
else
{
refinedRadius = radius;
refinedRadius2 = radius2;
}
if (inputDataLength != 0.0)
{
distance2ToDataBounds = this->Distance2ToBounds(x, this->Bounds);
maxDistance = sqrt(distance2ToDataBounds) + inputDataLength;
if (refinedRadius > maxDistance)
{
refinedRadius = maxDistance;
refinedRadius2 = maxDistance*maxDistance;
}
}
for (i = 0; i < 3; i++)
{
radiusLevels[i] = static_cast<int>(refinedRadius/this->H[i]);
if (radiusLevels[i] > this->Divisions[i] / 2)
{
radiusLevels[i] = this->Divisions[i] / 2;
}
}
radiusLevel = radiusLevels[0];
radiusLevel = radiusLevels[1] > radiusLevel ? radiusLevels[1] : radiusLevel;
radiusLevel = radiusLevels[2] > radiusLevel ? radiusLevels[2] : radiusLevel;
if (radiusLevel == 0)
{
radiusLevel = 1;
}
// radius schedule increases the radius each iteration, this is currently
// implemented by decreasing ii by 1 each iteration. another alternative
// is to double the radius each iteration, i.e. ii = ii >> 1
// In practice, reducing ii by one has been found to be more efficient.
prevMinLevel[0] = prevMaxLevel[0] = ijk[0];
prevMinLevel[1] = prevMaxLevel[1] = ijk[1];
prevMinLevel[2] = prevMaxLevel[2] = ijk[2];
for (ii=radiusLevel; ii >= 1; ii--)
{
currentRadius = refinedRadius; // used in if at bottom of this for loop
// Build up a list of buckets that are arranged in rings
this->GetOverlappingBuckets(&buckets, x, refinedRadius/ii, prevMinLevel,
prevMaxLevel);
for (i=0; i<buckets.GetNumberOfNeighbors(); i++)
{
nei = buckets.GetPoint(i);
// do we still need to test this bucket?
if (this->Distance2ToBucket(x, nei) < refinedRadius2)
{
cno = nei[0] + nei[1]*this->xD + nei[2]*this->xyD;
if ( (numIds = this->GetNumberOfIds(cno)) > 0 )
{
ids = this->GetIds(cno);
for (j=0; j < numIds; j++)
{