forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkCellLocator.cxx
2004 lines (1788 loc) · 59.3 KB
/
vtkCellLocator.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: vtkCellLocator.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 "vtkCellLocator.h"
#include "vtkCellArray.h"
#include "vtkGenericCell.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPolyData.h"
#include "vtkBox.h"
#include <math.h>
vtkStandardNewMacro(vtkCellLocator);
#define VTK_CELL_OUTSIDE 0
#define VTK_CELL_INSIDE 1
typedef vtkIdList *vtkIdListPtr;
//----------------------------------------------------------------------------
class vtkNeighborCells
{
public:
vtkNeighborCells(const int sz, const int ext=1000)
{this->P = vtkIntArray::New(); this->P->Allocate(3*sz,3*ext);};
~vtkNeighborCells(){this->P->Delete();};
int GetNumberOfNeighbors() {return (this->P->GetMaxId()+1)/3;};
void Reset() {this->P->Reset();};
int *GetPoint(int i) {return this->P->GetPointer(3*i);};
int InsertNextPoint(int *x);
protected:
vtkIntArray *P;
};
inline int vtkNeighborCells::InsertNextPoint(int *x)
{
int id = this->P->GetMaxId() + 3;
this->P->InsertValue(id,x[2]);
this->P->SetValue(id-2, x[0]);
this->P->SetValue(id-1, x[1]);
return id/3;
}
//----------------------------------------------------------------------------
// Construct with automatic computation of divisions, averaging
// 25 cells per bucket.
vtkCellLocator::vtkCellLocator()
{
this->MaxLevel = 8;
this->Level = 8;
this->NumberOfCellsPerNode = 25;
this->Tree = NULL;
this->CellHasBeenVisited = NULL;
this->QueryNumber = 0;
this->NumberOfDivisions = 1;
this->H[0] = this->H[1] = this->H[2] = 1.0;
this->Buckets = new vtkNeighborCells(10, 10);
}
//----------------------------------------------------------------------------
vtkCellLocator::~vtkCellLocator()
{
if (this->Buckets)
{
delete this->Buckets;
this->Buckets = NULL;
}
this->FreeSearchStructure();
this->FreeCellBounds();
if (this->CellHasBeenVisited)
{
delete [] this->CellHasBeenVisited;
this->CellHasBeenVisited = NULL;
}
}
//----------------------------------------------------------------------------
void vtkCellLocator::FreeSearchStructure()
{
vtkIdList *cellIds;
int i;
if ( this->Tree )
{
for (i=0; i<this->NumberOfOctants; i++)
{
cellIds = this->Tree[i];
if (cellIds == reinterpret_cast<void *>(VTK_CELL_INSIDE))
{
cellIds = 0;
}
if (cellIds)
{
cellIds->Delete();
}
}
delete [] this->Tree;
this->Tree = NULL;
}
}
//----------------------------------------------------------------------------
// Given an offset into the structure, the number of divisions in the octree,
// an i,j,k location in the octree; return the index (idx) into the structure.
// Method returns 1 is the specified i,j,k location is "outside" of the octree.
int vtkCellLocator::GenerateIndex(int offset, int numDivs, int i, int j,
int k, vtkIdType &idx)
{
if ( i < 0 || i >= numDivs ||
j < 0 || j >= numDivs || k < 0 || k >= numDivs )
{
return 1;
}
idx = offset + i + j*numDivs + k*numDivs*numDivs;
return 0;
}
//----------------------------------------------------------------------------
void vtkCellLocator::ComputeOctantBounds(int i, int j, int k)
{
this->OctantBounds[0] = this->Bounds[0] + i*H[0];
this->OctantBounds[1] = this->OctantBounds[0] + H[0];
this->OctantBounds[2] = this->Bounds[2] + j*H[1];
this->OctantBounds[3] = this->OctantBounds[2] + H[1];
this->OctantBounds[4] = this->Bounds[4] + k*H[2];
this->OctantBounds[5] = this->OctantBounds[4] + H[2];
}
//----------------------------------------------------------------------------
// Return intersection point (if any) AND the cell which was intersected by
// finite line
int vtkCellLocator::IntersectWithLine(double a0[3], double a1[3], double tol,
double& t, double x[3], double pcoords[3],
int &subId, vtkIdType &cellId,
vtkGenericCell *cell)
{
double origin[3];
double direction1[3];
double direction2[3];
double direction3[3];
double hitPosition[3];
double hitCellBoundsPosition[3], cellBounds[6];
int hitCellBounds;
double result;
double bounds2[6];
int i, leafStart, prod, loop;
vtkIdType bestCellId = -1, cId;
int idx;
double tMax, dist[3];
int npos[3];
int pos[3];
int bestDir;
double stopDist, currDist;
double deltaT, pDistance, minPDistance=1.0e38;
double length, maxLength=0.0;
this->BuildLocatorIfNeeded();
// convert the line into i,j,k coordinates
tMax = 0.0;
for (i=0; i < 3; i++)
{
direction1[i] = a1[i] - a0[i];
length = this->Bounds[2*i+1] - this->Bounds[2*i];
if ( length > maxLength )
{
maxLength = length;
}
origin[i] = (a0[i] - this->Bounds[2*i]) / length;
direction2[i] = direction1[i]/length;
bounds2[2*i] = 0.0;
bounds2[2*i+1] = 1.0;
tMax += direction2[i]*direction2[i];
}
tMax = sqrt(tMax);
// create a parametric range around the tolerance
deltaT = tol/maxLength;
stopDist = tMax*this->NumberOfDivisions;
for (i = 0; i < 3; i++)
{
direction3[i] = direction2[i]/tMax;
}
if (vtkBox::IntersectBox(bounds2, origin, direction2, hitPosition, result))
{
// start walking through the octants
prod = this->NumberOfDivisions*this->NumberOfDivisions;
leafStart = this->NumberOfOctants - this->NumberOfDivisions*prod;
bestCellId = -1;
// Clear the array that indicates whether we have visited this cell.
// The array is only cleared when the query number rolls over. This
// saves a number of calls to memset.
this->QueryNumber++;
if (this->QueryNumber == 0)
{
this->ClearCellHasBeenVisited();
this->QueryNumber++; // can't use 0 as a marker
}
// set up curr and stop dist
currDist = 0;
for (i = 0; i < 3; i++)
{
currDist += (hitPosition[i] - origin[i])*(hitPosition[i] - origin[i]);
}
currDist = sqrt(currDist)*this->NumberOfDivisions;
// add one offset due to the problems around zero
for (loop = 0; loop <3; loop++)
{
hitPosition[loop] = hitPosition[loop]*this->NumberOfDivisions + 1.0;
pos[loop] = static_cast<int>(hitPosition[loop]);
// Adjust right boundary condition: if we intersect from the top, right,
// or back; then pos must be adjusted to a valid octant index
if (pos[loop] > this->NumberOfDivisions)
{
pos[loop] = this->NumberOfDivisions;
}
}
idx = leafStart + pos[0] - 1 + (pos[1] - 1)*this->NumberOfDivisions
+ (pos[2] - 1)*prod;
while ((bestCellId < 0) && (pos[0] > 0) && (pos[1] > 0) && (pos[2] > 0) &&
(pos[0] <= this->NumberOfDivisions) &&
(pos[1] <= this->NumberOfDivisions) &&
(pos[2] <= this->NumberOfDivisions) &&
(currDist < stopDist))
{
if (this->Tree[idx])
{
this->ComputeOctantBounds(pos[0]-1,pos[1]-1,pos[2]-1);
for (tMax = VTK_DOUBLE_MAX, cellId=0;
cellId < this->Tree[idx]->GetNumberOfIds(); cellId++)
{
cId = this->Tree[idx]->GetId(cellId);
if (this->CellHasBeenVisited[cId] != this->QueryNumber)
{
this->CellHasBeenVisited[cId] = this->QueryNumber;
hitCellBounds = 0;
// check whether we intersect the cell bounds
if (this->CacheCellBounds)
{
hitCellBounds = vtkBox::IntersectBox(this->CellBounds[cId],
a0, direction1,
hitCellBoundsPosition, result);
}
else
{
this->DataSet->GetCellBounds(cId, cellBounds);
hitCellBounds = vtkBox::IntersectBox(cellBounds,
a0, direction1,
hitCellBoundsPosition, result);
}
if (hitCellBounds)
{
// now, do the expensive GetCell call and the expensive
// intersect with line call
this->DataSet->GetCell(cId, cell);
if (cell->IntersectWithLine(a0, a1, tol, t, x, pcoords, subId) )
{
if ( ! this->IsInOctantBounds(x) )
{
this->CellHasBeenVisited[cId] = 0; //mark the cell non-visited
}
else
{
if ( t < (tMax+deltaT) ) //it might be close
{
pDistance = cell->GetParametricDistance(pcoords);
if ( pDistance < minPDistance ||
(pDistance == minPDistance && t < tMax) )
{
tMax = t;
minPDistance = pDistance;
bestCellId = cId;
}
} //intersection point is in current octant
} //if within current parametric range
} // if intersection
} // if (hitCellBounds)
} // if (!this->CellHasBeenVisited[cId])
}
}
// move to the next octant
tMax = VTK_DOUBLE_MAX;
bestDir = 0;
for (loop = 0; loop < 3; loop++)
{
if (direction3[loop] > 0)
{
npos[loop] = pos[loop] + 1;
dist[loop] = (1.0 - hitPosition[loop] + pos[loop])/direction3[loop];
if (dist[loop] == 0)
{
dist[loop] = 1.0/direction3[loop];
}
if (dist[loop] < 0)
{
dist[loop] = 0;
}
if (dist[loop] < tMax)
{
bestDir = loop;
tMax = dist[loop];
}
}
if (direction3[loop] < 0)
{
npos[loop] = pos[loop] - 1;
dist[loop] = (pos[loop] - hitPosition[loop])/direction3[loop];
if (dist[loop] == 0)
{
dist[loop] = -0.01/direction3[loop];
}
if (dist[loop] < 0)
{
dist[loop] = 0;
}
if (dist[loop] < tMax)
{
bestDir = loop;
tMax = dist[loop];
}
}
}
// update our position
for (loop = 0; loop < 3; loop++)
{
hitPosition[loop] += dist[bestDir]*direction3[loop];
}
currDist += dist[bestDir];
// now make the move, find the smallest distance
// only cross one boundry at a time
pos[bestDir] = npos[bestDir];
idx = leafStart + pos[0] - 1 + (pos[1]-1)*this->NumberOfDivisions +
(pos[2]-1)*prod;
}
} // if (vtkBox::IntersectBox(...))
if (bestCellId >= 0)
{
this->DataSet->GetCell(bestCellId, cell);
cell->IntersectWithLine(a0, a1, tol, t, x, pcoords, subId);
// store the best cell id in the return "parameter"
cellId = bestCellId;
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
// Return closest point (if any) AND the cell on which this closest point lies
void vtkCellLocator::FindClosestPoint(double x[3], double closestPoint[3],
vtkGenericCell *cell, vtkIdType &cellId,
int &subId, double& dist2)
{
int i;
vtkIdType j;
int *nei;
vtkIdType closestCell = -1;
int closestSubCell = -1;
int leafStart;
int level;
int ijk[3];
double minDist2, refinedRadius2, distance2ToBucket;
double distance2ToCellBounds, cellBounds[6];
double pcoords[3], point[3], cachedPoint[3], weightsArray[6];
double *weights = weightsArray;
int nWeights = 6, nPoints;
vtkIdList *cellIds;
int stat;
//int minStat=0; //save this variable it is used for debugging
this->BuildLocatorIfNeeded();
cachedPoint[0] = 0.0;
cachedPoint[1] = 0.0;
cachedPoint[2] = 0.0;
leafStart = this->NumberOfOctants
- this->NumberOfDivisions*this->NumberOfDivisions*this->NumberOfDivisions;
// Clear the array that indicates whether we have visited this cell.
// The array is only cleared when the query number rolls over. This
// saves a number of calls to memset.
this->QueryNumber++;
if (this->QueryNumber == 0)
{
this->ClearCellHasBeenVisited();
this->QueryNumber++; // can't use 0 as a marker
}
// init
dist2 = -1.0;
refinedRadius2 = VTK_DOUBLE_MAX;
//
// Find bucket point is in.
//
for (j=0; j<3; j++)
{
ijk[j] = static_cast<int>((x[j] - this->Bounds[2*j]) / this->H[j]);
if (ijk[j] < 0)
{
ijk[j] = 0;
}
else if (ijk[j] >= this->NumberOfDivisions)
{
ijk[j] = this->NumberOfDivisions-1;
}
}
//
// Need to search this bucket for closest point. If there are no
// cells in this bucket, search 1st level neighbors, and so on,
// until closest point found.
//
for (closestCell=(-1),minDist2=VTK_DOUBLE_MAX,level=0;
(closestCell == -1) && (level < this->NumberOfDivisions); level++)
{
this->GetBucketNeighbors(ijk, this->NumberOfDivisions, level);
for (i=0; i<this->Buckets->GetNumberOfNeighbors(); i++)
{
nei = this->Buckets->GetPoint(i);
// if a neighboring bucket has cells,
if ( (cellIds =
this->Tree[leafStart + nei[0] + nei[1]*this->NumberOfDivisions +
nei[2]*this->NumberOfDivisions*this->NumberOfDivisions]) != NULL )
{
// do we still need to test this bucket?
distance2ToBucket = this->Distance2ToBucket(x, nei);
if (distance2ToBucket < refinedRadius2)
{
// still a viable bucket
for (j=0; j < cellIds->GetNumberOfIds(); j++)
{
// get the cell
cellId = cellIds->GetId(j);
if (this->CellHasBeenVisited[cellId] != this->QueryNumber)
{
this->CellHasBeenVisited[cellId] = this->QueryNumber;
// check whether we could be close enough to the cell by
// testing the cell bounds
if (this->CacheCellBounds)
{
distance2ToCellBounds =
this->Distance2ToBounds(x, this->CellBounds[cellId]);
}
else
{
this->DataSet->GetCellBounds(cellId, cellBounds);
distance2ToCellBounds = this->Distance2ToBounds(x, cellBounds);
}
if (distance2ToCellBounds < refinedRadius2)
{
this->DataSet->GetCell(cellId, cell);
// make sure we have enough storage space for the weights
nPoints = cell->GetPointIds()->GetNumberOfIds();
if (nPoints > nWeights)
{
if (nWeights > 6)
{
delete [] weights;
}
weights = new double[2*nPoints]; // allocate some extra room
nWeights = 2*nPoints;
}
// evaluate the position to find the closest point
// stat==(-1) is numerical error; stat==0 means outside;
// stat=1 means inside. However, for real world performance,
// we sometime select stat==0 cells if the distance is close
// enough
stat = cell->EvaluatePosition(x, point, subId, pcoords,
dist2, weights);
if ( stat != -1 && dist2 < minDist2 )
// This commented out code works better in many cases
// if ( stat != -1 && ((stat == minStat && dist2 < minDist2) ||
// (stat == 1 && minStat == 0)) )
{
closestCell = cellId;
closestSubCell = subId;
minDist2 = dist2;
cachedPoint[0] = point[0];
cachedPoint[1] = point[1];
cachedPoint[2] = point[2];
refinedRadius2 = dist2;
// minStat = stat;
}
}
} // if (!this->CellHasBeenVisited[cellId])
}
}
}
}
}
// Because of the relative location of the points in the buckets, the
// cell found previously may not be the closest cell. Have to
// search those bucket neighbors that might also contain nearby cells.
//
if ( (minDist2 > 0.0) && (level < this->NumberOfDivisions))
{
int prevMinLevel[3], prevMaxLevel[3];
// setup prevMinLevel and prevMaxLevel to indicate previously visited
// buckets
if (--level < 0)
{
level = 0;
}
for (i = 0; i < 3; i++)
{
prevMinLevel[i] = ijk[i] - level;
if (prevMinLevel[i] < 0)
{
prevMinLevel[i] = 0;
}
prevMaxLevel[i] = ijk[i] + level;
if (prevMaxLevel[i] >= this->NumberOfDivisions)
{
prevMaxLevel[i] = this->NumberOfDivisions - 1;
}
}
this->GetOverlappingBuckets(x, ijk, sqrt(minDist2), prevMinLevel,
prevMaxLevel);
for (i=0; i<this->Buckets->GetNumberOfNeighbors(); i++)
{
nei = this->Buckets->GetPoint(i);
if ( (cellIds =
this->Tree[leafStart + nei[0] + nei[1]*this->NumberOfDivisions +
nei[2]*this->NumberOfDivisions*this->NumberOfDivisions]) != NULL )
{
// do we still need to test this bucket?
distance2ToBucket = this->Distance2ToBucket(x, nei);
if (distance2ToBucket < refinedRadius2)
{
// still a viable bucket
for (j=0; j < cellIds->GetNumberOfIds(); j++)
{
// get the cell
cellId = cellIds->GetId(j);
if (this->CellHasBeenVisited[cellId] != this->QueryNumber)
{
this->CellHasBeenVisited[cellId] = this->QueryNumber;
// check whether we could be close enough to the cell by
// testing the cell bounds
if (this->CacheCellBounds)
{
distance2ToCellBounds =
this->Distance2ToBounds(x, this->CellBounds[cellId]);
}
else
{
this->DataSet->GetCellBounds(cellId, cellBounds);
distance2ToCellBounds = this->Distance2ToBounds(x, cellBounds);
}
if (distance2ToCellBounds < refinedRadius2)
{
this->DataSet->GetCell(cellId, cell);
// make sure we have enough storage space for the weights
nPoints = cell->GetPointIds()->GetNumberOfIds();
if (nPoints > nWeights)
{
if (nWeights > 6)
{
delete [] weights;
}
weights = new double[2*nPoints]; // allocate some extra room
nWeights = 2*nPoints;
}
// evaluate the position to find the closest point
cell->EvaluatePosition(x, point, subId, pcoords,
dist2, weights);
if ( dist2 < minDist2 )
{
closestCell = cellId;
closestSubCell = subId;
minDist2 = dist2;
cachedPoint[0] = point[0];
cachedPoint[1] = point[1];
cachedPoint[2] = point[2];
refinedRadius2 = dist2;
}
}//if point close enough to cell bounds
}//if cell has not been visited
}//for each cell
}//if bucket is still viable
}//if cells in bucket
}//for each overlapping bucket
}//if not identical point
if (closestCell != -1)
{
dist2 = minDist2;
cellId = closestCell;
subId = closestSubCell;
closestPoint[0] = cachedPoint[0];
closestPoint[1] = cachedPoint[1];
closestPoint[2] = cachedPoint[2];
this->DataSet->GetCell(cellId, cell);
}
if (nWeights > 6)
{
delete [] weights;
}
}
//----------------------------------------------------------------------------
vtkIdType vtkCellLocator::FindClosestPointWithinRadius(double x[3], double radius,
double closestPoint[3],
vtkGenericCell *cell,
vtkIdType &cellId, int &subId,
double& dist2, int &inside)
{
int i;
vtkIdType j;
int tmpInside;
int *nei;
int closestCell = -1;
int closestSubCell = -1;
int leafStart;
int ijk[3];
double minDist2;
double pcoords[3], point[3], cachedPoint[3], weightsArray[6];
double *weights = weightsArray;
int nWeights = 6, nPoints;
int returnVal = 0;
vtkIdList *cellIds;
double refinedRadius, radius2, refinedRadius2, distance2ToBucket;
double distance2ToCellBounds, cellBounds[6], currentRadius;
double distance2ToDataBounds, maxDistance;
int ii, radiusLevels[3], radiusLevel, prevMinLevel[3], prevMaxLevel[3];
this->BuildLocatorIfNeeded();
cachedPoint[0] = 0.0;
cachedPoint[1] = 0.0;
cachedPoint[2] = 0.0;
leafStart = this->NumberOfOctants
- this->NumberOfDivisions*this->NumberOfDivisions*this->NumberOfDivisions;
// Clear the array that indicates whether we have visited this cell.
// The array is only cleared when the query number rolls over. This
// saves a number of calls to memset.
this->QueryNumber++;
if (this->QueryNumber == 0)
{
this->ClearCellHasBeenVisited();
this->QueryNumber++; // can't use 0 as a marker
}
// init
dist2 = -1.0;
closestCell = -1;
radius2 = radius*radius;
minDist2 = 1.1*radius2; // something slightly bigger....
refinedRadius = radius;
refinedRadius2 = radius2;
// Find bucket point is in.
//
for (j=0; j<3; j++)
{
ijk[j] = static_cast<int>((x[j] - this->Bounds[2*j]) / this->H[j]);
if (ijk[j] < 0)
{
ijk[j] = 0;
}
else if (ijk[j] >= this->NumberOfDivisions)
{
ijk[j] = this->NumberOfDivisions-1;
}
}
// Start by searching the bucket that the point is in.
//
if ((cellIds =
this->Tree[leafStart + ijk[0] + ijk[1]*this->NumberOfDivisions +
ijk[2]*this->NumberOfDivisions*this->NumberOfDivisions]) != NULL )
{
// query each cell
for (j=0; j < cellIds->GetNumberOfIds(); j++)
{
// get the cell
cellId = cellIds->GetId(j);
if (this->CellHasBeenVisited[cellId] != this->QueryNumber)
{
this->CellHasBeenVisited[cellId] = this->QueryNumber;
// check whether we could be close enough to the cell by
// testing the cell bounds
if (this->CacheCellBounds)
{
distance2ToCellBounds =
this->Distance2ToBounds(x, this->CellBounds[cellId]);
}
else
{
this->DataSet->GetCellBounds(cellId, cellBounds);
distance2ToCellBounds = this->Distance2ToBounds(x, cellBounds);
}
if (distance2ToCellBounds < refinedRadius2)
{
this->DataSet->GetCell(cellId, cell);
// make sure we have enough storage space for the weights
nPoints = cell->GetPointIds()->GetNumberOfIds();
if (nPoints > nWeights)
{
if (nWeights > 6)
{
delete [] weights;
}
weights = new double[2*nPoints]; // allocate some extra room
nWeights = 2*nPoints;
}
// evaluate the position to find the closest point
tmpInside = cell->EvaluatePosition(x, point, subId, pcoords,
dist2, weights);
if ( dist2 < minDist2 )
{
inside = tmpInside;
closestCell = cellId;
closestSubCell = subId;
minDist2 = dist2;
cachedPoint[0] = point[0];
cachedPoint[1] = point[1];
cachedPoint[2] = point[2];
refinedRadius = sqrt(dist2);
refinedRadius2 = dist2;
}
}
} // if (this->CellHasBeenVisited[cellId])
}
}
// Now, search only those buckets that are within a radius. The radius used
// is the smaller of sqrt(dist2) 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 (dist2 < radius2 && dist2 >= 0.0)
{
refinedRadius = sqrt(dist2);
refinedRadius2 = dist2;
}
else
{
refinedRadius = radius;
refinedRadius2 = radius2;
}
distance2ToDataBounds = this->Distance2ToBounds(x, this->Bounds);
maxDistance = sqrt(distance2ToDataBounds) + this->DataSet->GetLength();
if (refinedRadius > maxDistance)
{
refinedRadius = maxDistance;
refinedRadius2 = maxDistance*maxDistance;
}
radiusLevels[0] = static_cast<int>(refinedRadius/this->H[0]);
radiusLevels[1] = static_cast<int>(refinedRadius/this->H[1]);
radiusLevels[2] = static_cast<int>(refinedRadius/this->H[2]);
radiusLevel = radiusLevels[0];
radiusLevel = radiusLevels[1] > radiusLevel ? radiusLevels[1] : radiusLevel;
radiusLevel = radiusLevels[2] > radiusLevel ? radiusLevels[2] : radiusLevel;
if (radiusLevel > this->NumberOfDivisions / 2 )
{
radiusLevel = this->NumberOfDivisions / 2;
}
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.
int numberOfBucketsPerPlane;
numberOfBucketsPerPlane = this->NumberOfDivisions*this->NumberOfDivisions;
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(x, ijk, refinedRadius/ii, prevMinLevel,
prevMaxLevel);
for (i=0; i<this->Buckets->GetNumberOfNeighbors(); i++)
{
nei = this->Buckets->GetPoint(i);
if ( (cellIds =
this->Tree[leafStart + nei[0] + nei[1]*this->NumberOfDivisions +
nei[2]*numberOfBucketsPerPlane]) != NULL )
{
// do we still need to test this bucket?
distance2ToBucket = this->Distance2ToBucket(x, nei);
if (distance2ToBucket < refinedRadius2)
{
// still a viable bucket
for (j=0; j < cellIds->GetNumberOfIds(); j++)
{
// get the cell
cellId = cellIds->GetId(j);
if (this->CellHasBeenVisited[cellId] != this->QueryNumber)
{
this->CellHasBeenVisited[cellId] = this->QueryNumber;
// check whether we could be close enough to the cell by
// testing the cell bounds
if (this->CacheCellBounds)
{
distance2ToCellBounds =
this->Distance2ToBounds(x, this->CellBounds[cellId]);
}
else
{
this->DataSet->GetCellBounds(cellId, cellBounds);
distance2ToCellBounds = this->Distance2ToBounds(x, cellBounds);
}
if (distance2ToCellBounds < refinedRadius2)
{
this->DataSet->GetCell(cellId, cell);
// make sure we have enough storage space for the weights
nPoints = cell->GetPointIds()->GetNumberOfIds();
if (nPoints > nWeights)
{
if (nWeights > 6)
{
delete [] weights;
}
weights = new double[2*nPoints]; // allocate some extra room
nWeights = 2*nPoints;
}
// evaluate the position to find the closest point
tmpInside = cell->EvaluatePosition(x, point, subId, pcoords,
dist2, weights);
if ( dist2 < minDist2 )
{
inside = tmpInside;
closestCell = cellId;
closestSubCell = subId;
minDist2 = dist2;
cachedPoint[0] = point[0];
cachedPoint[1] = point[1];
cachedPoint[2] = point[2];
refinedRadius = sqrt(minDist2);
refinedRadius2 = minDist2;
}
}//if point close enough to cell bounds
}//if cell has not been visited
}//for each cell in bucket
}//if bucket is within the current best distance
}//if cells in bucket
}//for each overlapping bucket
// don't want to checker a smaller radius than we just checked so update
// ii appropriately
if (refinedRadius < currentRadius && ii > 2) //always check ii==1
{
ii = static_cast<int>(
static_cast<double>(ii) * (refinedRadius / currentRadius)) + 1;
if (ii < 2)
{
ii = 2;
}
}
}//for each radius in the radius schedule
if ((closestCell != -1) && (minDist2 <= radius2))
{
dist2 = minDist2;
cellId = closestCell;
subId = closestSubCell;
closestPoint[0] = cachedPoint[0];
closestPoint[1] = cachedPoint[1];
closestPoint[2] = cachedPoint[2];
this->DataSet->GetCell(cellId, cell);
returnVal = 1;
}
if (nWeights > 6)
{
delete [] weights;
}
return returnVal;
}
//----------------------------------------------------------------------------
// Internal function to get bucket neighbors at specified "level". The
// bucket neighbors are indices into the "leaf-node" layer of the octree.
// These indices must be offset by number of octants before the leaf node
// layer before they can be used. Only those buckets with cells are returned.
//
void vtkCellLocator::GetBucketNeighbors(int ijk[3], int ndivs, int level)
{
int i, j, k, min, max, minLevel[3], maxLevel[3];
int nei[3];
int leafStart;
int numberOfBucketsPerPlane;
this->BuildLocatorIfNeeded();
numberOfBucketsPerPlane = this->NumberOfDivisions*this->NumberOfDivisions;
leafStart = this->NumberOfOctants
- numberOfBucketsPerPlane*this->NumberOfDivisions;
// Initialize
//
this->Buckets->Reset();
// If at this bucket, just place into list
//
if ( level == 0 )
{
if (this->Tree[leafStart + ijk[0] + ijk[1]*this->NumberOfDivisions
+ ijk[2]*numberOfBucketsPerPlane])
{
this->Buckets->InsertNextPoint(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;