forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkIncrementalOctreePointLocator.cxx
1371 lines (1197 loc) · 44.7 KB
/
vtkIncrementalOctreePointLocator.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: vtkIncrementalOctreePointLocator.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 "vtkMath.h"
#include "vtkPoints.h"
#include "vtkIdList.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"
#include "vtkObjectFactory.h"
#include "vtkIncrementalOctreeNode.h"
#include "vtkIncrementalOctreePointLocator.h"
#include <vtkstd/map>
#include <vtkstd/list>
#include <vtkstd/stack>
#include <vtkstd/queue>
#include <vtkstd/vector>
vtkStandardNewMacro( vtkIncrementalOctreePointLocator );
// ---------------------------------------------------------------------------
// ----------------------------- Sorting Points -----------------------------
// ---------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Helper class for sorting points in support of point location, specifically,
// vtkIncrementalOctreePointLocator::FindClosestNPoints().
namespace
{
class SortPoints
{
public:
SortPoints( int N )
{
this->NumberPoints = 0;
this->NumRequested = N;
this->LargestDist2 = VTK_DOUBLE_MAX;
}
void InsertPoint( double dist2, vtkIdType pntId )
{
// a new pair may be inserted as long as the squared distance is less
// than the largest one of the current map OR the number of inserted
// points is still less than that of requested points
if ( dist2 <= this->LargestDist2
|| this->NumberPoints < this->NumRequested
)
{
this->NumberPoints ++;
vtkstd::map< double, vtkstd::list< vtkIdType > >::iterator
it = this->dist2ToIds.find( dist2 );
if ( it == this->dist2ToIds.end() )
{
// no any entry corresponds to this squared distance
vtkstd::list< vtkIdType > idset;
idset.push_back( pntId );
this->dist2ToIds[ dist2 ] = idset;
}
else
{
// there is an entry corresponding to this squared distance
it->second.push_back( pntId );
}
if ( this->NumberPoints > this->NumRequested )
{
// we need to go to the very last entry
it = this->dist2ToIds.end();
it --;
// Even if we remove the very last entry, the number of points
// will still be greater than that of requested points. This
// indicates we can safely remove the very last entry and update
// the largest squared distance with that of the entry before the
// very last one.
if ( this->NumberPoints - it->second.size()
> this->NumRequested
)
{
this->NumberPoints -= it->second.size();
vtkstd::map< double, vtkstd::list< vtkIdType > >::iterator
it2 = it;
it2 --;
this->LargestDist2 = it2->first;
this->dist2ToIds.erase( it );
}
}
}
}
void GetSortedIds( vtkIdList * idList )
{
// determine how many points will be actually exported
idList->Reset();
vtkIdType numIds = ( this->NumRequested < this->NumberPoints )
? this->NumRequested : this->NumberPoints;
idList->SetNumberOfIds( numIds );
// clear the counter and go to the very first entry
vtkIdType counter = 0;
vtkstd::map< double, vtkstd::list< vtkIdType > >::iterator
it = this->dist2ToIds.begin();
// export the point indices
while ( counter < numIds && it != this->dist2ToIds.end() )
{
vtkstd::list<vtkIdType>::iterator lit = it->second.begin();
while ( counter < numIds && lit != it->second.end() )
{
idList->InsertId( counter, *lit );
counter ++;
lit ++;
}
it ++;
}
}
double GetLargestDist2()
{
return this->LargestDist2;
}
private:
size_t NumRequested;
size_t NumberPoints;
double LargestDist2;
vtkstd::map< double, vtkstd::list< vtkIdType > > dist2ToIds;
};
}
// ---------------------------------------------------------------------------
// --------------------- vtkIncrementalOctreePointLocator --------------------
// ---------------------------------------------------------------------------
//----------------------------------------------------------------------------
vtkIncrementalOctreePointLocator::vtkIncrementalOctreePointLocator()
{
this->FudgeFactor = 0;
this->OctreeMaxDimSize = 0;
this->BuildCubicOctree = 0;
this->MaxPointsPerLeaf = 128;
this->InsertTolerance2 = 0.000001;
this->LocatorPoints = NULL;
this->OctreeRootNode = NULL;
}
//----------------------------------------------------------------------------
vtkIncrementalOctreePointLocator::~vtkIncrementalOctreePointLocator()
{
this->FreeSearchStructure();
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::DeleteAllDescendants
( vtkIncrementalOctreeNode * node )
{
if ( node->IsLeaf() == 0 )
{
for ( int i = 0; i < 8; i ++ )
{
vtkIncrementalOctreeNode * child = node->GetChild( i );
vtkIncrementalOctreePointLocator::DeleteAllDescendants( child );
child = NULL;
}
node->DeleteChildNodes();
}
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::FreeSearchStructure()
{
if ( this->OctreeRootNode )
{
vtkIncrementalOctreePointLocator::DeleteAllDescendants
( this->OctreeRootNode );
this->OctreeRootNode->Delete();
this->OctreeRootNode = NULL;
}
if ( this->LocatorPoints )
{
this->LocatorPoints->UnRegister( this );
this->LocatorPoints = NULL;
}
}
//----------------------------------------------------------------------------
int vtkIncrementalOctreePointLocator::GetNumberOfPoints()
{
return ( this->OctreeRootNode == NULL )
? 0
: this->OctreeRootNode->GetNumberOfPoints();
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::GetBounds( double * bounds )
{
if ( this->OctreeRootNode )
{
double * minBounds = this->OctreeRootNode->GetMinBounds();
double * maxBounds = this->OctreeRootNode->GetMaxBounds();
bounds[0] = minBounds[0];
bounds[1] = maxBounds[0];
bounds[2] = minBounds[1];
bounds[3] = maxBounds[1];
bounds[4] = minBounds[2];
bounds[5] = maxBounds[2];
minBounds = maxBounds = NULL;
}
}
//----------------------------------------------------------------------------
vtkIncrementalOctreeNode * vtkIncrementalOctreePointLocator::GetLeafContainer
( vtkIncrementalOctreeNode * node, const double pnt[3] )
{
return ( ( node->IsLeaf() )
? node
: this->GetLeafContainer
( node->GetChild( node->GetChildIndex( pnt ) ), pnt )
);
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestInsertedPoint
( const double x[3] )
{
if ( this->OctreeRootNode == NULL ||
this->OctreeRootNode->GetNumberOfPoints() == 0 ||
this->OctreeRootNode->ContainsPoint( x ) == 0
)
{
return -1;
}
double miniDist2 = this->OctreeMaxDimSize * this->OctreeMaxDimSize * 4.0;
double elseDist2; // inter-node search
vtkIdType elsePntId; // inter-node search
vtkIdType pointIndx = -1;
vtkIncrementalOctreeNode * pLeafNode = NULL;
pLeafNode = this->GetLeafContainer( this->OctreeRootNode, x );
pointIndx = this->FindClosestPointInLeafNode( pLeafNode, x, &miniDist2 );
if ( miniDist2 > 0.0 )
{
if ( pLeafNode->GetDistance2ToInnerBoundary( x, this->OctreeRootNode )
< miniDist2
)
{
elsePntId = this->FindClosestPointInSphereWithoutTolerance
( x, miniDist2, pLeafNode, &elseDist2 );
if ( elseDist2 < miniDist2 )
{
pointIndx = elsePntId;
miniDist2 = elseDist2;
}
}
}
pLeafNode = NULL;
return pointIndx;
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::PrintSelf( ostream & os, vtkIndent indent )
{
this->Superclass::PrintSelf( os, indent );
os << indent << "FudgeFactor: " << this->FudgeFactor << endl;
os << indent << "LocatorPoints: " << this->LocatorPoints << endl;
os << indent << "OctreeRootNode: " << this->OctreeRootNode << endl;
os << indent << "BuildCubicOctree: " << this->BuildCubicOctree << endl;
os << indent << "MaxPointsPerLeaf: " << this->MaxPointsPerLeaf << endl;
os << indent << "InsertTolerance2: " << this->InsertTolerance2 << endl;
os << indent << "OctreeMaxDimSize: " << this->OctreeMaxDimSize << endl;
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::GenerateRepresentation
( int nodeLevel, vtkPolyData * polysData )
{
if ( this->OctreeRootNode == NULL )
{
vtkErrorMacro( "vtkIncrementalOctreePointLocator::GenerateRepresentation" );
vtkErrorMacro( "(): the octree is not yet available" );
return;
}
int tempLevel;
vtkPoints * thePoints = NULL;
vtkCellArray * nodeQuads = NULL;
vtkIncrementalOctreeNode * pTempNode = NULL;
vtkstd::list < vtkIncrementalOctreeNode * > nodesList;
vtkstd::queue< vtkstd::pair < vtkIncrementalOctreeNode *, int > > pairQueue;
// recursively process the nodes in the octree
pairQueue.push( vtkstd::make_pair( this->OctreeRootNode, 0 ) );
while ( !pairQueue.empty() )
{
pTempNode = pairQueue.front().first;
tempLevel = pairQueue.front().second;
pairQueue.pop();
if ( tempLevel == nodeLevel )
{
nodesList.push_back( pTempNode );
}
else
if ( !pTempNode->IsLeaf() )
{
for ( int i = 0; i < 8; i ++ )
{
pairQueue.push( vtkstd::make_pair( pTempNode->GetChild( i ),
nodeLevel + 1
)
);
}
}
}
// collect the vertices and quads of each node
thePoints = vtkPoints::New();
thePoints->Allocate( 8 * static_cast < int > ( nodesList.size() ) );
nodeQuads = vtkCellArray::New();
nodeQuads->Allocate( 6 * static_cast < int > ( nodesList.size() ) );
for ( vtkstd::list< vtkIncrementalOctreeNode * >::iterator
lit = nodesList.begin(); lit != nodesList.end(); lit ++ )
{
vtkIncrementalOctreePointLocator::AddPolys( *lit, thePoints, nodeQuads );
}
// attach points and quads
polysData->SetPoints( thePoints );
polysData->SetPolys ( nodeQuads );
thePoints->Delete();
nodeQuads->Delete();
thePoints = NULL;
nodeQuads = NULL;
pTempNode = NULL;
}
//----------------------------------------------------------------------------
static vtkIdType OCTREE_NODE_FACES_LUT[6][4] =
{
{ 0, 1, 5, 4 }, { 0, 4, 6, 2 },
{ 6, 7, 3, 2 }, { 1, 3, 7, 5 },
{ 2, 3, 1, 0 }, { 4, 5, 7, 6 }
};
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::AddPolys
( vtkIncrementalOctreeNode * node, vtkPoints * points, vtkCellArray * polygs )
{
int i;
double bounds[6];
double ptCord[3];
vtkIdType pntIds[8];
vtkIdType idList[4];
node->GetBounds( bounds );
for ( i = 0; i < 8; i ++ )
{
ptCord[0] = bounds[ i & 1 ];
ptCord[1] = bounds[ i & 2 ];
ptCord[2] = bounds[ i & 4 ];
pntIds[i] = points->InsertNextPoint( ptCord );
}
for ( i = 0; i < 6; i ++ )
{
idList[0] = pntIds[ OCTREE_NODE_FACES_LUT[i][0] ];
idList[1] = pntIds[ OCTREE_NODE_FACES_LUT[i][1] ];
idList[2] = pntIds[ OCTREE_NODE_FACES_LUT[i][2] ];
idList[3] = pntIds[ OCTREE_NODE_FACES_LUT[i][3] ];
polygs->InsertNextCell( 4, idList );
}
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointInLeafNode
( vtkIncrementalOctreeNode * leafNode, const double point[3], double * dist2 )
{
// NOTE: dist2 MUST be inited with a very huge value below, but instead of
// this->OctreeMaxDimSize * this->OctreeMaxDimSize * 4.0, because the point
// under check may be outside the octree and hence the squared distance can
// be greater than the latter or other similar octree-based specific values.
*dist2 = VTK_DOUBLE_MAX;
if ( leafNode->GetPointIdSet() == NULL )
{
return -1;
}
int numPts = 0;
double tmpDst = 0.0;
double tmpPnt[3];
vtkIdType tmpIdx = -1;
vtkIdType pntIdx = -1;
vtkIdList * idList = NULL;
idList = leafNode->GetPointIdSet();
numPts = idList->GetNumberOfIds( );
for ( int i = 0; i < numPts; i ++ )
{
tmpIdx = idList->GetId( i );
this->LocatorPoints->GetPoint( tmpIdx, tmpPnt );
tmpDst = vtkMath::Distance2BetweenPoints( tmpPnt, point );
if ( tmpDst < ( *dist2 ) )
{
*dist2 = tmpDst;
pntIdx = tmpIdx;
}
if ( ( *dist2 ) == 0.0 )
{
break;
}
}
idList = NULL;
return pntIdx;
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointInSphere
( const double point[3], double radius2, vtkIncrementalOctreeNode * maskNode,
double * minDist2, const double * refDist2 )
{
vtkIdType pointIndx = -1;
vtkstd::stack< vtkIncrementalOctreeNode * > nodesBase;
nodesBase.push( this->OctreeRootNode );
while ( !nodesBase.empty() && ( *minDist2 ) > 0.0 )
{
vtkIncrementalOctreeNode * checkNode = nodesBase.top();
nodesBase.pop();
if ( !checkNode->IsLeaf() )
{
for ( int i = 0; i < 8; i ++ )
{
vtkIncrementalOctreeNode * childNode = checkNode->GetChild( i );
// use ( radius2 + radius2 ) to skip empty nodes
double distToData = ( childNode->GetNumberOfPoints() )
? childNode->GetDistance2ToBoundary
( point, this->OctreeRootNode, 1 )
: ( radius2 + radius2 );
// If a child node is not the mask node AND its distance, specifically
// the data bounding box (determined by the points inside or under) to
// the point, is less than the threshold radius (one exception is the
// point's container nodes), it is pushed to the stack as a suspect.
if ( ( childNode != maskNode )
&& ( ( distToData <= ( *refDist2 ) )
|| ( childNode->ContainsPoint( point ) == 1 )
)
)
{
nodesBase.push( childNode );
}
childNode = NULL;
}
}
else
{
// now that the node under check is a leaf, let's find the closest
// point therein and the minimum distance
double tempDist2;
int tempPntId = this->FindClosestPointInLeafNode
( checkNode, point, &tempDist2 );
if ( tempDist2 < ( *minDist2 ) )
{
*minDist2 = tempDist2;
pointIndx = tempPntId;
}
}
checkNode = NULL;
}
return ( ( *minDist2 ) <= radius2 ) ? pointIndx : -1;
}
// ---------------------------------------------------------------------------
// ----------------------------- Point Location -----------------------------
// ---------------------------------------------------------------------------
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::BuildLocator()
{
// assume point location is necessary for vtkPointSet data only
if ( !this->DataSet || !this->DataSet->IsA( "vtkPointSet" ) )
{
vtkErrorMacro( "Dataset is NULL or it is not of type vtkPointSet" );
return;
}
int numPoints = this->DataSet->GetNumberOfPoints();
if ( numPoints < 1 || numPoints >= VTK_INT_MAX )
{
// current implementation does not support 64-bit point indices
// due to performance consideration
vtkErrorMacro( << "No points to build an octree with or " );
vtkErrorMacro( << "failure to support 64-bit point ids" );
return;
}
// construct an octree only if necessary
if ( ( this->BuildTime > this->MTime )
&& ( this->BuildTime > this->DataSet->GetMTime() )
)
{
return;
}
vtkDebugMacro( << "Creating an incremental octree" );
// build an octree by populating it with check-free insertion of point ids
double theBounds[6];
double theCoords[3];
vtkIdType pointIndx;
vtkPoints * thePoints = vtkPointSet::SafeDownCast( this->DataSet )
->GetPoints();
thePoints->GetBounds( theBounds );
this->InitPointInsertion( thePoints, theBounds );
for ( pointIndx = 0; pointIndx < numPoints; pointIndx ++ )
{
thePoints->GetPoint( pointIndx, theCoords );
// the 3D point coordinate is actually not inserted to vtkPoints at all
// while only the point index is inserted to the vtkIdList of the
// container leaf
this->InsertPointWithoutChecking( theCoords, pointIndx, 0 );
}
thePoints = NULL;
this->BuildTime.Modified();
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointInSphereWithoutTolerance
( const double point[3], double radius2,
vtkIncrementalOctreeNode * maskNode, double * minDist2 )
{
// It might be unsafe to use a ratio less than 1.1 since radius2 itself
// could be very small and 1.00001 might just be equal to radius2.
*minDist2 = radius2 * 1.1;
return this->FindClosestPointInSphere( point, radius2, maskNode,
minDist2, minDist2 );
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPoint
( double x, double y, double z )
{
double dumbDist2;
double theCoords[3] = { x, y, z };
return this->FindClosestPoint( theCoords, &dumbDist2 );
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPoint( const double x[3] )
{
double dumbDist2;
return this->FindClosestPoint( x, &dumbDist2 );
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPoint
( double x, double y, double z, double * miniDist2 )
{
double theCoords[3] = { x, y, z };
return this->FindClosestPoint( theCoords, miniDist2 );
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPoint
( const double x[3], double * miniDist2 )
{
this->BuildLocator();
// init miniDist2 for early exit
*miniDist2 = this->OctreeMaxDimSize * this->OctreeMaxDimSize * 4.0;
if ( this->OctreeRootNode == NULL ||
this->OctreeRootNode->GetNumberOfPoints() == 0
)
{
return -1;
}
double elseDist2; // inter-node search
vtkIdType elsePntId; // inter-node search
vtkIdType pointIndx = -1;
vtkIncrementalOctreeNode * pLeafNode = NULL;
if ( this->OctreeRootNode->ContainsPoint( x ) )
{ // the point is inside the octree
pLeafNode = this->GetLeafContainer( this->OctreeRootNode, x );
pointIndx = this->FindClosestPointInLeafNode( pLeafNode, x, miniDist2 );
if ( ( *miniDist2 ) > 0.0 )
{
if ( pLeafNode->GetDistance2ToInnerBoundary( x, this->OctreeRootNode )
< ( *miniDist2 )
)
{
elsePntId = this->FindClosestPointInSphereWithoutTolerance
( x, *miniDist2, pLeafNode, &elseDist2 );
if ( elseDist2 < ( *miniDist2) )
{
pointIndx = elsePntId;
*miniDist2 = elseDist2;
}
}
}
}
else // the point is outside the octree
{
double initialPt[3];
double * minBounds = this->OctreeRootNode->GetMinBounds();
double * maxBounds = this->OctreeRootNode->GetMaxBounds();
this->OctreeRootNode->GetDistance2ToBoundary
( x, initialPt, this->OctreeRootNode, 1 );
// This intial (closest) point might be outside the octree a little bit
if ( initialPt[0] <= minBounds[0] )
{
initialPt[0] = minBounds[0] + this->FudgeFactor;
}
else
if ( initialPt[0] >= maxBounds[0] )
{
initialPt[0] = maxBounds[0] - this->FudgeFactor;
}
if ( initialPt[1] <= minBounds[1] )
{
initialPt[1] = minBounds[1] + this->FudgeFactor;
}
else
if ( initialPt[1] >= maxBounds[1] )
{
initialPt[1] = maxBounds[1] - this->FudgeFactor;
}
if ( initialPt[2] <= minBounds[2] )
{
initialPt[2] = minBounds[2] + this->FudgeFactor;
}
else
if ( initialPt[2] >= maxBounds[2] )
{
initialPt[2] = maxBounds[2] - this->FudgeFactor;
}
pLeafNode = this->GetLeafContainer( this->OctreeRootNode, initialPt );
pointIndx = this->FindClosestPointInLeafNode( pLeafNode, x, miniDist2 );
elsePntId = this->FindClosestPointInSphereWithoutTolerance
( x, *miniDist2, pLeafNode, &elseDist2 );
if ( elseDist2 < ( *miniDist2 ) )
{
pointIndx = elsePntId;
*miniDist2 = elseDist2;
}
minBounds = maxBounds = NULL;
}
pLeafNode = NULL;
return pointIndx;
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointWithinRadius
( double radius, const double x[3], double & dist2 )
{
this->BuildLocator();
return this->FindClosestPointInSphereWithoutTolerance
( x, radius * radius, NULL, &dist2 );
}
//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointWithinSquaredRadius
( double radius2, const double x[3], double & dist2 )
{
this->BuildLocator();
return this->FindClosestPointInSphereWithoutTolerance
( x, radius2, NULL, &dist2 );
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::FindPointsWithinSquaredRadius
( vtkIncrementalOctreeNode * node, double radius2,
const double point[3], vtkIdList * idList )
{
int i, j;
int numberPnts;
double tempValue0;
double tempValue1;
double pt2PtDist2;
double pointCoord[3];
double nodeBounds[6];
double outMinDst2 = 0.0; // min distance to the node: for outside point
double maximDist2 = 0.0; // max distance to the node: inside or outside
vtkIdType localIndex = 0;
vtkIdType pointIndex = 0;
vtkIdList * nodePntIds = NULL;
node->GetBounds( nodeBounds );
for ( i = 0; i < 3; i ++ ) // for each axis
{
j = ( i << 1 );
tempValue0 = point[i] - nodeBounds[j];
tempValue1 = nodeBounds[ j + 1 ] - point[i];
if ( tempValue0 < 0.0 )
{
outMinDst2 += tempValue0 * tempValue0;
maximDist2 += tempValue1 * tempValue1;
}
else
if ( tempValue1 < 0.0 )
{
outMinDst2 += tempValue1 * tempValue1;
maximDist2 += tempValue0 * tempValue0;
}
else
if ( tempValue1 > tempValue0 )
{
maximDist2 += tempValue1 * tempValue1;
}
else
{
maximDist2 += tempValue0 * tempValue0;
}
}
if ( outMinDst2 > radius2 )
{
// the node is totally outside the search sphere
return;
}
if ( maximDist2 <= radius2 )
{
// the node is totally inside the search sphere
node->ExportAllPointIdsByInsertion( idList );
return;
}
// the node intersects with, but is not totally inside, the search sphere
if ( node->IsLeaf() )
{
numberPnts = node->GetNumberOfPoints();
nodePntIds = node->GetPointIdSet();
for ( localIndex = 0; localIndex < numberPnts; localIndex ++ )
{
pointIndex = nodePntIds->GetId( localIndex );
this->LocatorPoints->GetPoint( pointIndex, pointCoord );
pt2PtDist2 = vtkMath::Distance2BetweenPoints( pointCoord, point );
if ( pt2PtDist2 <= radius2 )
{
idList->InsertNextId( pointIndex );
}
}
nodePntIds = NULL;
}
else
{
for ( i = 0; i < 8; i ++ )
{
this->FindPointsWithinSquaredRadius( node->GetChild( i ),
radius2, point, idList );
}
}
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::FindPointsWithinSquaredRadius
( double R2, const double x[3], vtkIdList * result )
{
result->Reset();
this->BuildLocator();
this->FindPointsWithinSquaredRadius( this->OctreeRootNode, R2, x, result );
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::FindPointsWithinRadius( double R,
const double x[3], vtkIdList * result )
{
result->Reset();
this->BuildLocator();
this->FindPointsWithinSquaredRadius
( this->OctreeRootNode, R * R, x, result );
}
//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::FindClosestNPoints
( int N, const double x[3], vtkIdList * result )
{
result->Reset();
this->BuildLocator();
int totalPnts = this->OctreeRootNode->GetNumberOfPoints(); // possibly 0
if ( N > totalPnts )
{
N = totalPnts;
vtkWarningMacro( "Number of requested points > that of available points" );
}
if ( N <= 0 )
{
vtkWarningMacro( "invalid N or the octree is still empty" );
return;
}
// We are going to find the lowest-possible node to start with, startNode,
// by using a top-down recursive search mechanism. Such a starting node
// belongs to one of the following cases (numPoints: number of points in or
// under startNode).
//
// (1) startNode is a leaf node AND numPoints = N
// (2) startNode is a leaf node AND numPoints > N
// (3) startNode is a non-leaf node AND numPoints = N
// (4) startNode is a non-leaf node AND numPoints > N
//
// * case 4 occurs, when none of the other three cases holds, by going one
// level up --- one-step regression.
//
// * The point may be outside startNode, as is usually the case, even if it
// is inside the octree root node. To address such scenarios, the initial
// point-inside-the-node case might be followed by the point-outside-the-
// node case to quickly locate the most compact startNode. Otherwise the
// resulting startNode might contain a huge number of points, which would
// significantly degrade the search performance.
int i;
int beenFound;
int numPoints;
double tempDist2;
double miniDist2;
double maxiDist2;
double pntCoords[3];
vtkIdType pointIndx;
vtkIdList * pntIdList = NULL;
vtkIncrementalOctreeNode * startNode = NULL;
vtkIncrementalOctreeNode * pTheChild = NULL;
vtkIncrementalOctreeNode * pThisNode = this->OctreeRootNode;
vtkIncrementalOctreeNode * theParent = pThisNode;
vtkstd::queue< vtkIncrementalOctreeNode * > nodeQueue;
beenFound = 0;
numPoints = pThisNode->GetNumberOfPoints();
while ( beenFound == 0 )
{
if ( pThisNode->ContainsPoint( x ) ) // point inside the node
{
while ( !pThisNode->IsLeaf() && numPoints > N )
{
theParent = pThisNode;
pThisNode = pThisNode->GetChild( pThisNode->GetChildIndex( x ) );
numPoints = pThisNode->GetNumberOfPoints();
}
if ( numPoints )
{
// The point is still inside pThisNode
beenFound = 1;
pThisNode = ( numPoints >= N ) ? pThisNode : theParent;
}
else
{
// The point is inside an empty node (pThisNode), but outside the node
// with closest points --- the closest node (a sibling of pThisNode).
// We need to locate this closest node via the parent node and proceed
// with it (the closest node) further in search for startNode, but by
// means of the other case (point outside the node).
miniDist2 = VTK_DOUBLE_MAX;
for ( i = 0; i < 8; i ++ )
{
pTheChild = theParent->GetChild( i );
tempDist2 = pTheChild->GetDistance2ToBoundary
( x, this->OctreeRootNode, 1 );
if ( tempDist2 < miniDist2 )
{
miniDist2 = tempDist2;
pThisNode = pTheChild;
}
}
}
}
else // point outside the node
{
while ( !pThisNode->IsLeaf() && numPoints > N )
{
// find the child closest (in terms of data) to the given point
theParent = pThisNode;
miniDist2 = VTK_DOUBLE_MAX;
for ( i = 0; i < 8; i ++ )
{
pTheChild = theParent->GetChild( i );
tempDist2 = pTheChild->GetDistance2ToBoundary
( x, this->OctreeRootNode, 1 );
if ( tempDist2 < miniDist2 )
{
miniDist2 = tempDist2;
pThisNode = pTheChild;
}
}
numPoints = pThisNode->GetNumberOfPoints();
}
beenFound = 1;
pThisNode = ( numPoints >= N ) ? pThisNode : theParent;
}
// update the number of points in the node in case of a switch from point-
// inside-the-node to point-outside-the-node.
numPoints = pThisNode->GetNumberOfPoints();
}
// this is where we can get the really most compact starting node
startNode = pThisNode;
numPoints = startNode->GetNumberOfPoints();
// Given the starting node, we select the points inside it and sort them
SortPoints ptsSorter( N );
pointIndx = 0;
pntIdList = vtkIdList::New();
pntIdList->SetNumberOfIds( numPoints );
startNode->ExportAllPointIdsByDirectSet( &pointIndx, pntIdList );
for ( i = 0; i < numPoints; i ++ )
{
pointIndx = pntIdList->GetId( i );
this->LocatorPoints->GetPoint( pointIndx, pntCoords );
tempDist2 = vtkMath::Distance2BetweenPoints( x, pntCoords );
ptsSorter.InsertPoint( tempDist2, pointIndx );
}
// We still need to check other nodes in case they contain closer points
nodeQueue.push( this->OctreeRootNode );
maxiDist2 = ptsSorter.GetLargestDist2();
while ( !nodeQueue.empty() )
{
pThisNode = nodeQueue.front();
nodeQueue.pop();
// skip the start node as we have just processed it
if ( pThisNode == startNode )
{
continue;
}
if ( !pThisNode->IsLeaf() )
{
// this is a non-leaf node and we need to push some children if necessary
for ( i = 0; i < 8; i ++ )
{
pTheChild = pThisNode->GetChild( i );
if ( pTheChild->ContainsPointByData( x ) == 1
|| pTheChild->GetDistance2ToBoundary( x, this->OctreeRootNode, 1 )
< maxiDist2
)
{
nodeQueue.push( pTheChild );
}