forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkOctreePointLocator.cxx
1316 lines (1163 loc) · 36.5 KB
/
vtkOctreePointLocator.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: vtkOctreePointLocator.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.
=========================================================================*/
/*----------------------------------------------------------------------------
Copyright (c) Sandia Corporation
See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
----------------------------------------------------------------------------*/
#include "vtkOctreePointLocator.h"
#include "vtkCellArray.h"
#include "vtkCommand.h"
#include "vtkDataSet.h"
#include "vtkFloatArray.h"
#include "vtkIdList.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkOctreePointLocatorNode.h"
#include "vtkPoints.h"
#include "vtkPointSet.h"
#include "vtkPolyData.h"
#include <vtkstd/list>
#include <vtkstd/map>
#include <vtkstd/queue>
#include <vtkstd/stack>
#include <vtkstd/vector>
vtkStandardNewMacro(vtkOctreePointLocator);
// helper class for ordering the points in
// vtkOctreePointLocator::FindClosestNPoints()
namespace
{
class OrderPoints
{
public:
OrderPoints(int numDesiredPoints)
{
this->NumDesiredPoints = numDesiredPoints;
this->NumPoints = 0;
this->LargestDist2 = VTK_LARGE_FLOAT;
}
void InsertPoint(float dist2, vtkIdType id)
{
if(dist2 <= this->LargestDist2 ||
this->NumPoints < this->NumDesiredPoints)
{
vtkstd::map<float, vtkstd::list<vtkIdType> >::iterator it=
this->dist2ToIds.find(dist2);
this->NumPoints++;
if(it == this->dist2ToIds.end())
{
vtkstd::list<vtkIdType> idset;
idset.push_back(id);
this->dist2ToIds[dist2] = idset;
}
else
{
it->second.push_back(id);
}
if(this->NumPoints > this->NumDesiredPoints)
{
it=this->dist2ToIds.end();
it--;
if((this->NumPoints-it->second.size()) > this->NumDesiredPoints)
{
this->NumPoints -= it->second.size();
vtkstd::map<float, vtkstd::list<vtkIdType> >::iterator it2 = it;
it2--;
this->LargestDist2 = it2->first;
this->dist2ToIds.erase(it);
}
}
}
}
void GetSortedIds(vtkIdList* ids)
{
ids->Reset();
vtkIdType numIds = (this->NumDesiredPoints < this->NumPoints)
? this->NumDesiredPoints : this->NumPoints;
ids->SetNumberOfIds(numIds);
vtkIdType counter = 0;
vtkstd::map<float, vtkstd::list<vtkIdType> >::iterator it=
this->dist2ToIds.begin();
while(counter < numIds && it!=this->dist2ToIds.end())
{
vtkstd::list<vtkIdType>::iterator lit=it->second.begin();
while(counter < numIds && lit!=it->second.end())
{
ids->InsertId(counter, *lit);
counter++;
lit++;
}
it++;
}
}
float GetLargestDist2()
{
return this->LargestDist2;
}
private:
size_t NumDesiredPoints, NumPoints;
float LargestDist2;
// map from dist^2 to a list of ids
vtkstd::map<float, vtkstd::list<vtkIdType> > dist2ToIds;
};
}
//----------------------------------------------------------------------------
vtkOctreePointLocator::vtkOctreePointLocator()
{
this->FudgeFactor = 0;
this->MaxWidth = 0.0;
this->MaxLevel = 20;
this->MaximumPointsPerRegion = 100;
this->Level = 0;
this->Top = NULL;
this->NumberOfLocatorPoints = 0;
this->LocatorPoints = NULL;
this->LocatorIds = NULL;
this->LeafNodeList = NULL;
this->CreateCubicOctants = 1;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::DeleteAllDescendants(
vtkOctreePointLocatorNode *octant)
{
if(octant->GetChild(0))
{
for(int i=0;i<8;i++)
{
vtkOctreePointLocatorNode* child = octant->GetChild(i);
vtkOctreePointLocator::DeleteAllDescendants(child);
}
octant->DeleteChildNodes();
}
}
//----------------------------------------------------------------------------
vtkOctreePointLocator::~vtkOctreePointLocator()
{
this->FreeSearchStructure();
if(this->LocatorPoints != NULL)
{
delete []this->LocatorPoints;
this->LocatorPoints = 0;
}
if(this->LocatorIds != NULL)
{
delete []this->LocatorIds;
this->LocatorIds = 0;
}
if(this->LeafNodeList != NULL)
{
delete []this->LeafNodeList;
this->LeafNodeList = 0;
}
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::GetBounds(double *bounds)
{
if (this->Top)
{
this->Top->GetBounds(bounds);
}
}
//----------------------------------------------------------------------------
double* vtkOctreePointLocator::GetBounds()
{
double* bounds = vtkAbstractPointLocator::GetBounds();
if (this->Top)
{
this->Top->GetBounds(bounds);
}
return bounds;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::GetRegionBounds(int leafNodeId,
double bounds[6])
{
if ( (leafNodeId < 0) || (leafNodeId >= this->NumberOfLeafNodes))
{
vtkErrorMacro("Invalid region.");
return;
}
vtkOctreePointLocatorNode *node = this->LeafNodeList[leafNodeId];
node->GetBounds(bounds);
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::GetRegionDataBounds(int leafNodeId,
double bounds[6])
{
if ( (leafNodeId < 0) || (leafNodeId >= this->NumberOfLeafNodes))
{
vtkErrorMacro("Invalid region.");
return;
}
vtkOctreePointLocatorNode *node = this->LeafNodeList[leafNodeId];
node->GetDataBounds(bounds);
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::SetDataBoundsToSpatialBounds(
vtkOctreePointLocatorNode *octant)
{
octant->SetMinDataBounds(octant->GetMinBounds());
octant->SetMaxDataBounds(octant->GetMaxBounds());
if (octant->GetChild(0))
{
for(int i=0;i<8;i++)
{
vtkOctreePointLocator::SetDataBoundsToSpatialBounds(octant->GetChild(i));
}
}
}
//----------------------------------------------------------------------------
int vtkOctreePointLocator::DivideTest(int size, int level)
{
if (level >= this->MaxLevel)
{
return 0;
}
if(size > this->GetMaximumPointsPerRegion())
{
return 1;
}
return 0;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::DivideRegion(
vtkOctreePointLocatorNode *node, int* ordering, int level)
{
if(!this->DivideTest(node->GetNumberOfPoints(), level))
{
return;
}
if(level >= this->Level)
{
this->Level = level + 1;
}
node->CreateChildNodes();
int numberOfPoints = node->GetNumberOfPoints();
vtkDataSet* ds = this->GetDataSet();
vtkstd::vector<int> points[7];
int i;
int subOctantNumberOfPoints[8] = {0,0,0,0,0,0,0,0};
for(i=0;i<numberOfPoints;i++)
{
int index = node->GetSubOctantIndex(ds->GetPoint(ordering[i]), 0);
if(index)
{
points[index-1].push_back(ordering[i]);
}
else
{
ordering[subOctantNumberOfPoints[0]] = ordering[i];
}
subOctantNumberOfPoints[index]++;
}
int counter = 0;
int sizeOfInt = sizeof(int);
for(i=0;i<7;i++)
{
counter += subOctantNumberOfPoints[i];
if(!points[i].empty())
{
memcpy(ordering+counter, &(points[i][0]),
subOctantNumberOfPoints[i+1]*sizeOfInt);
}
}
counter = 0;
for(i=0;i<8;i++)
{
node->GetChild(i)->SetNumberOfPoints(subOctantNumberOfPoints[i]);
this->DivideRegion(node->GetChild(i), ordering+counter, level + 1);
counter += subOctantNumberOfPoints[i];
}
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::BuildLocator()
{
if(!this->GetDataSet())
{
vtkErrorMacro("Must set a valid data set first.");
}
int numPoints = this->GetDataSet()->GetNumberOfPoints();
if (numPoints < 1)
{
vtkErrorMacro(<< "No points to build from.");
return;
}
if (numPoints >= VTK_INT_MAX)
{
// When point IDs are stored in an "int" instead of a vtkIdType,
// performance doubles. So we store point IDs in an "int" during
// the calculation. This will need to be rewritten if true 64 bit
// IDs are required.
vtkErrorMacro("Intentional 64 bit error - time to rewrite code.");
return;
}
vtkDebugMacro( << "Creating octree" );
if ( (this->BuildTime > this->MTime)
&& (this->BuildTime > this->DataSet->GetMTime()) )
{
return;
}
this->FreeSearchStructure();
// Fix bounds - (1) push out a little if flat
// (2) pull back the x, y and z lower bounds a little bit so that
// points are clearly "inside" the spatial region. Point p is
// "inside" region r = [r1, r2] if r1 < p <= r2.
double bounds[6], diff[3];
this->GetDataSet()->GetBounds(bounds);
this->MaxWidth = 0.0;
int i;
for (i=0; i<3; i++)
{
diff[i] = bounds[2*i+1] - bounds[2*i];
this->MaxWidth = static_cast<float>
((diff[i] > this->MaxWidth) ? diff[i] : this->MaxWidth);
}
if(this->CreateCubicOctants)
{
// make the bounding box have equal length sides so that all octants
// will also have equal length sides
for(i=0;i<3;i++)
{
if(diff[i] != this->MaxWidth)
{
double delta = this->MaxWidth - diff[i];
bounds[2*i] -= .5*delta;
bounds[2*i+1] += .5*delta;
diff[i] = this->MaxWidth;
}
}
}
this->FudgeFactor = this->MaxWidth * 10e-6;
double aLittle = this->MaxWidth * 10e-2;
for (i=0; i<3; i++)
{
if (diff[i] < aLittle) // case (1) above
{
double temp = bounds[2*i];
bounds[2*i] = bounds[2*i+1] - aLittle;
bounds[2*i+1] = temp + aLittle;
}
else // case (2) above
{
bounds[2*i] -= this->FudgeFactor;
}
}
// root node of octree - it's the whole space
vtkOctreePointLocatorNode *node = this->Top = vtkOctreePointLocatorNode::New();
node->SetBounds(bounds[0], bounds[1],
bounds[2], bounds[3],
bounds[4], bounds[5]);
node->SetNumberOfPoints(numPoints);
node->SetDataBounds(bounds[0], bounds[1],
bounds[2], bounds[3],
bounds[4], bounds[5]);
this->LocatorIds = new int [numPoints];
this->LocatorPoints = new float [3 * numPoints];
if ( !this->LocatorPoints || !this->LocatorIds)
{
this->FreeSearchStructure();
vtkErrorMacro("vtkOctreePointLocator::BuildLocatorFromPoints - memory allocation");
return;
}
for(i=0;i<numPoints;i++)
{
this->LocatorIds[i] = i;
}
this->DivideRegion(node, this->LocatorIds, 0);
// TODO: may want to directly check if there exists a point array that
// is of type float and directly copy that instead of dealing with
// all of the casts
vtkDataSet* ds = this->GetDataSet();
for(i=0;i<numPoints;i++)
{
double *pt = ds->GetPoint(this->LocatorIds[i]);
this->LocatorPoints[i*3] = static_cast<float>(pt[0]);
this->LocatorPoints[i*3+1] = static_cast<float>(pt[1]);
this->LocatorPoints[i*3+2] = static_cast<float>(pt[2]);
}
int nextLeafNodeId = 0;
int nextMinId = 0;
this->Top->ComputeOctreeNodeInformation(this->Top, nextLeafNodeId,
nextMinId, this->LocatorPoints);
this->NumberOfLeafNodes = nextLeafNodeId;
int index = 0;
this->LeafNodeList = new vtkOctreePointLocatorNode*[this->NumberOfLeafNodes];
this->BuildLeafNodeList(this->Top, index);
this->BuildTime.Modified();
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::BuildLeafNodeList(vtkOctreePointLocatorNode* node,
int & index)
{
if(node->GetChild(0))
{
for(int i=0;i<8;i++)
{
this->BuildLeafNodeList(node->GetChild(i), index);
}
}
else
{
this->LeafNodeList[index] = node;
index++;
}
}
//----------------------------------------------------------------------------
vtkIdType vtkOctreePointLocator::FindClosestPoint(const double x[3])
{
double dist2(0);
return this->FindClosestPoint(x[0], x[1], x[2], dist2);
}
//----------------------------------------------------------------------------
vtkIdType vtkOctreePointLocator::FindClosestPoint(
double x, double y, double z, double &dist2)
{
this->BuildLocator();
int closeId=-1;
vtkIdType newCloseId=-1;
double newDistance2 = 4 * this->MaxWidth * this->MaxWidth;
int regionId = this->GetRegionContainingPoint(x, y, z);
vtkIdType closePointId = -1;
if (regionId < 0)
{
// This point is not inside the space divided by the octree.
// Find the point on the boundary that is closest to it.
double pt[3];
this->Top->GetDistance2ToBoundary(x, y, z, pt, this->Top, 1);
double *min = this->Top->GetMinBounds();
double *max = this->Top->GetMaxBounds();
// GetDistance2ToBoundary will sometimes return a point *just*
// *barely* outside the bounds of the region. Move that point to
// just barely *inside* instead.
if (pt[0] <= min[0])
{
pt[0] = min[0] + this->FudgeFactor;
}
if (pt[1] <= min[1])
{
pt[1] = min[1] + this->FudgeFactor;
}
if (pt[2] <= min[2])
{
pt[2] = min[2] + this->FudgeFactor;
}
if (pt[0] >= max[0])
{
pt[0] = max[0] - this->FudgeFactor;
}
if (pt[1] >= max[1])
{
pt[1] = max[1] - this->FudgeFactor;
}
if (pt[2] >= max[2])
{
pt[2] = max[2] - this->FudgeFactor;
}
regionId = this->GetRegionContainingPoint(pt[0], pt[1], pt[2]);
closeId = this->_FindClosestPointInRegion(regionId, x, y, z, dist2);
closePointId = static_cast<vtkIdType>(this->LocatorIds[closeId]);
// Check to see if neighboring regions have a closer point
newCloseId = this->FindClosestPointInSphere(x, y, z,
sqrt(dist2), // radius
regionId, // skip this region
newDistance2);// distance to closest point
if(newDistance2 < dist2)
{
dist2 = newDistance2;
closePointId = newCloseId;
}
}
else // Point is inside an octree region
{
closeId = this->_FindClosestPointInRegion(regionId, x, y, z, dist2);
closePointId = static_cast<vtkIdType>(this->LocatorIds[closeId]);
if (dist2 > 0.0)
{
float dist2ToBoundary = static_cast<float>(
this->LeafNodeList[regionId]->GetDistance2ToInnerBoundary(x, y, z, this->Top));
if (dist2ToBoundary < dist2)
{
// The closest point may be in a neighboring region
newCloseId = this->FindClosestPointInSphere(x, y, z,
sqrt(dist2), // radius
regionId, // skip this region
newDistance2);
if(newDistance2 < dist2)
{
dist2 = newDistance2;
closePointId = newCloseId;
}
}
}
}
return closePointId;
}
//----------------------------------------------------------------------------
vtkIdType vtkOctreePointLocator::FindClosestPointWithinRadius(
double radius, const double x[3], double& dist2)
{
return this->FindClosestPointInSphere(x[0], x[1], x[2], radius, -2, dist2);
}
//----------------------------------------------------------------------------
vtkIdType vtkOctreePointLocator::FindClosestPointInRegion(
int regionId, double *x, double &dist2)
{
return this->FindClosestPointInRegion(regionId, x[0], x[1], x[2], dist2);
}
//----------------------------------------------------------------------------
vtkIdType vtkOctreePointLocator::FindClosestPointInRegion(
int regionId, double x, double y, double z, double &dist2)
{
if (!this->LocatorPoints)
{
// if the locator hasn't been built yet the regionId is garbage!
vtkErrorMacro("vtkOctreePointLocator::FindClosestPointInRegion - must build locator first");
return -1;
}
int localId = this->_FindClosestPointInRegion(regionId, x, y, z, dist2);
vtkIdType originalId = -1;
if (localId >= 0)
{
originalId = static_cast<vtkIdType>(this->LocatorIds[localId]);
}
return originalId;
}
//----------------------------------------------------------------------------
int vtkOctreePointLocator::_FindClosestPointInRegion(
int leafNodeId, double x, double y, double z, double &dist2)
{
int minId=0;
float fx = static_cast<float>(x);
float fy = static_cast<float>(y);
float fz = static_cast<float>(z);
float minDistance2 = 4 * this->MaxWidth * this->MaxWidth;
int idx = this->LeafNodeList[leafNodeId]->GetMinID();
float *candidate = this->LocatorPoints + (idx * 3);
int numPoints = this->LeafNodeList[leafNodeId]->GetNumberOfPoints();
for (int i=0; i < numPoints; i++)
{
float diffx = fx-candidate[0];
float diffy = fy-candidate[1];
float diffz = fz-candidate[2];
float dxyz = diffx*diffx + diffy*diffy + diffz*diffz;
if(dxyz < minDistance2)
{
minId = idx + i;
minDistance2 = dxyz;
if(dxyz == 0.0)
{
break;
}
}
candidate += 3;
}
dist2 = minDistance2;
return minId;
}
//----------------------------------------------------------------------------
int vtkOctreePointLocator::FindClosestPointInSphere(
double x, double y, double z, double radius, int skipRegion, double &dist2)
{
this->BuildLocator();
dist2 = radius * radius * 1.0001;
int localCloseId = -1;
vtkstd::stack<vtkOctreePointLocatorNode*> regions;
regions.push(this->Top);
while(!regions.empty())
{
vtkOctreePointLocatorNode* region = regions.top();
regions.pop();
if(region->GetChild(0))
{
for(int i=0;i<8;i++)
{
vtkOctreePointLocatorNode* child = region->GetChild(i);
// must check for leaf nodes here in case skipRegion == -1
// since all non-leaf nodes have Id = -1.
if(child->GetID() != skipRegion &&
(child->GetDistance2ToBoundary(x, y, z, this->Top, 1) < dist2 ||
child->ContainsPoint(x, y, z, 0)))
{
regions.push(child);
}
}
}
else
{
double tempDist2 = dist2;
int tempId =
this->_FindClosestPointInRegion(region->GetID(), x, y, z, tempDist2);
if (tempDist2 < dist2)
{
dist2 = tempDist2;
localCloseId = tempId;
}
}
}
vtkIdType originalId = -1;
if(localCloseId >= 0 && dist2 <= radius*radius)
{
originalId = static_cast<vtkIdType>(this->LocatorIds[localCloseId]);
}
return originalId;
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::FindPointsWithinRadius(
double radius, const double x[3], vtkIdList* result)
{
result->Reset();
this->BuildLocator();
// don't forget to square the radius
this->FindPointsWithinRadius(this->Top, radius*radius, x, result);
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::FindPointsWithinRadius(
vtkOctreePointLocatorNode* node, double radiusSquared, const double x[3],
vtkIdList* result)
{
double b[6];
node->GetBounds(b);
double mindist2 = 0; // distance to closest vertex of BB
double maxdist2 = 0; // distance to furthest vertex of BB
// x-dir
if(x[0] < b[0])
{
mindist2 = (b[0]-x[0])*(b[0]-x[0]);
maxdist2 = (b[1]-x[0])*(b[1]-x[0]);
}
else if(x[0] > b[1])
{
mindist2 = (b[1]-x[0])*(b[1]-x[0]);
maxdist2 = (b[0]-x[0])*(b[0]-x[0]);
}
else if((b[1]-x[0]) > (x[0]-b[0]))
{
maxdist2 = (b[1]-x[0])*(b[1]-x[0]);
}
else
{
maxdist2 = (b[0]-x[0])*(b[0]-x[0]);
}
// y-dir
if(x[1] < b[2])
{
mindist2 += (b[2]-x[1])*(b[2]-x[1]);
maxdist2 += (b[3]-x[1])*(b[3]-x[1]);
}
else if(x[1] > b[3])
{
mindist2 += (b[3]-x[1])*(b[3]-x[1]);
maxdist2 += (b[2]-x[1])*(b[2]-x[1]);
}
else if((b[3]-x[1]) > (x[1]-b[2]))
{
maxdist2 += (b[3]-x[1])*(b[3]-x[1]);
}
else
{
maxdist2 += (b[2]-x[1])*(b[2]-x[1]);
}
// z-dir
if(x[2] < b[4])
{
mindist2 += (b[4]-x[2])*(b[4]-x[2]);
maxdist2 += (b[5]-x[2])*(b[5]-x[2]);
}
else if(x[2] > b[5])
{
mindist2 += (b[5]-x[2])*(b[5]-x[2]);
maxdist2 += (b[4]-x[2])*(b[4]-x[2]);
}
else if((b[5]-x[2]) > (x[2]-b[4]))
{
maxdist2 += (b[5]-x[2])*(b[5]-x[2]);
}
else
{
maxdist2 += (x[2]-b[4])*(x[2]-b[4]);
}
if(mindist2 > radiusSquared)
{
// non-intersecting
return;
}
if(maxdist2 <= radiusSquared)
{
// sphere contains BB
this->AddAllPointsInRegion(node, result);
return;
}
// partial intersection of sphere & BB
if (node->GetChild(0) == NULL)
{
//int regionID = node->GetID();
int regionLoc = node->GetMinID();
float* pt = this->LocatorPoints + (regionLoc * 3);
vtkIdType numPoints = node->GetNumberOfPoints();
for (vtkIdType i = 0; i < numPoints; i++)
{
double dist2 = (pt[0]-x[0])*(pt[0]-x[0])+
(pt[1]-x[1])*(pt[1]-x[1])+(pt[2]-x[2])*(pt[2]-x[2]);
if(dist2 <= radiusSquared)
{
vtkIdType ptId =
static_cast<vtkIdType>(this->LocatorIds[regionLoc + i]);
result->InsertNextId(ptId);
}
pt += 3;
}
}
else
{
for(int i=0;i<8;i++)
{
this->FindPointsWithinRadius(
node->GetChild(i), radiusSquared, x, result);
}
}
}
//----------------------------------------------------------------------------
void vtkOctreePointLocator::FindClosestNPoints(int N, const double x[3],
vtkIdList* result)
{
result->Reset();
if(N<=0)
{
return;
}
this->BuildLocator();
int numTotalPoints = this->Top->GetNumberOfPoints();
if(numTotalPoints < N)
{
vtkWarningMacro("Number of requested points is greater than total number of points in OctreePointLocator");
N = numTotalPoints;
}
result->SetNumberOfIds(N);
// now we want to go about finding a region that contains at least N points
// but not many more -- hopefully the region contains X as well but we
// can't depend on that
vtkOctreePointLocatorNode* node = this->Top;
vtkOctreePointLocatorNode* startingNode = 0;
if(!node->ContainsPoint(x[0], x[1], x[2], 0))
{
// point is not in the region
int numPoints = node->GetNumberOfPoints();
vtkOctreePointLocatorNode* prevNode = node;
while(node->GetChild(0) && numPoints > N)
{
prevNode = node;
vtkOctreePointLocatorNode* nextNode = node->GetChild(0);
double minDist2 =
nextNode->GetDistance2ToBoundary(x[0], x[1], x[2], this->Top, 1);
for(int i=1;i<8;i++)
{
double dist2 = node->GetChild(i)->GetDistance2ToBoundary(
x[0], x[1], x[2], this->Top, 1);
if(dist2 < minDist2)
{
nextNode = node->GetChild(i);
minDist2 = dist2;
}
}
node = nextNode;
numPoints = node->GetNumberOfPoints();
}
if(numPoints < N)
{
startingNode = prevNode;
}
else
{
startingNode = node;
}
}
else
{
int numPoints = node->GetNumberOfPoints();
vtkOctreePointLocatorNode* prevNode = node;
while(node->GetChild(0) && numPoints > N)
{
prevNode = node;
int i;
for(i=0;i<8;i++)
{
if(node->GetChild(i)->ContainsPoint(x[0], x[1], x[2], 0))
{
node = node->GetChild(i);
break;
}
}
numPoints = node->GetNumberOfPoints();
}
if(numPoints < N)
{
startingNode = prevNode;
}
else
{
startingNode = node;
}
}
// now that we have a starting region, go through its points
// and order them
int regionId = startingNode->GetID();
int numPoints = startingNode->GetNumberOfPoints();
int where = startingNode->GetMinID();
if(regionId < 0) // if not a leaf node
{
vtkOctreePointLocatorNode* parentOfNext = startingNode->GetChild(0);
vtkOctreePointLocatorNode* next = parentOfNext->GetChild(0);
while(next)
{
parentOfNext = next;
next = next->GetChild(0);
}
where = parentOfNext->GetMinID();
}
int *ids = this->LocatorIds + where;
float* pt = this->LocatorPoints + (where*3);
float xfloat[3] = {static_cast<float>(x[0]), static_cast<float>(x[1]),
static_cast<float>(x[2])};
OrderPoints orderedPoints(N);
for (int i=0; i<numPoints; i++)
{
float dist2 = vtkMath::Distance2BetweenPoints(xfloat, pt);
orderedPoints.InsertPoint(dist2, ids[i]);
pt += 3;
}
// to finish up we have to check other regions for
// closer points
float largestDist2 = orderedPoints.GetLargestDist2();
double bounds[6];
node = this->Top;
vtkstd::queue<vtkOctreePointLocatorNode*> nodesToBeSearched;
nodesToBeSearched.push(node);
while(!nodesToBeSearched.empty())
{
node = nodesToBeSearched.front();
nodesToBeSearched.pop();
if(node == startingNode)
{
continue;
}
if(node->GetChild(0))
{
for(int j=0;j<8;j++)
{
vtkOctreePointLocatorNode* child = node->GetChild(j);
child->GetDataBounds(bounds);
double delta[3] = {0,0,0};
if(vtkMath::PointIsWithinBounds(const_cast<double*>(x), bounds, delta) == 1 ||
child->GetDistance2ToBoundary(x[0], x[1], x[2], 0, 1) < largestDist2)
{
nodesToBeSearched.push(child);
}
}
}
else if(node->GetDistance2ToBoundary(x[0], x[1], x[2], this->Top, 1) < largestDist2)
{
regionId = node->GetID();
numPoints = node->GetNumberOfPoints();
where = node->GetMinID();
ids = this->LocatorIds + where;
pt = this->LocatorPoints + (where*3);
for (int i=0; i<numPoints; i++)
{
float dist2 = vtkMath::Distance2BetweenPoints(xfloat, pt);
orderedPoints.InsertPoint(dist2, ids[i]);
pt += 3;
}
largestDist2 = orderedPoints.GetLargestDist2();
}
}
orderedPoints.GetSortedIds(result);
}
//----------------------------------------------------------------------------
vtkIdTypeArray *vtkOctreePointLocator::GetPointsInRegion(int leafNodeId)
{
if ( (leafNodeId < 0) || (leafNodeId >= this->NumberOfLeafNodes))
{
vtkErrorMacro("vtkOctreePointLocator::GetPointsInRegion invalid leaf node ID");
return NULL;
}
if (!this->LocatorIds)
{