File: vtkIncrementalOctreePointLocator.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (1371 lines) | stat: -rw-r--r-- 45,057 bytes parent folder | download | duplicates (8)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
/*=========================================================================

  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 <map>
#include <list>
#include <stack>
#include <queue>
#include <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 ++;
          std::map<  double,  std::list< vtkIdType >  >::iterator
            it = this->dist2ToIds.find( dist2 );

          if ( it == this->dist2ToIds.end() )
            {
            // no any entry corresponds to this squared distance
            std::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();
              std::map<  double,  std::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;
        std::map<  double,  std::list< vtkIdType >  >::iterator
          it = this->dist2ToIds.begin();

        // export the point indices
        while ( counter < numIds && it != this->dist2ToIds.end() )
          {
          std::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;
    std::map<  double,  std::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;
  std::list < vtkIncrementalOctreeNode * > nodesList;
  std::queue< std::pair < vtkIncrementalOctreeNode *, int > > pairQueue;

  // recursively process the nodes in the octree
  pairQueue.push(  std::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(    std::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 ( std::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;
  std::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 initial (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;
  std::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 );
          }
        }
      }
    else
    if (  pThisNode->GetDistance2ToBoundary( x, this->OctreeRootNode, 1 )
        < maxiDist2  )
      {
      // This is a leaf node AND its data bounding box is close enough for us
      // to process the points inside the node. Note that the success of the
      // above distance check indicates that there is at least one point in
      // the node. Otherwise the point-to-node distance (in terms of data)
      // would be VTK_DOUBLE_MAX.

      // obtain the point indices
      numPoints = pThisNode->GetNumberOfPoints();
      pointIndx = 0;
      pntIdList->Reset();
      pntIdList->SetNumberOfIds( numPoints );
      pThisNode->ExportAllPointIdsByDirectSet( &pointIndx, pntIdList );

      // insert the points to the sorter if necessary
      for ( i = 0; i < numPoints; i ++ )
        {
        pointIndx = pntIdList->GetId( i );
        this->LocatorPoints->GetPoint( pointIndx, pntCoords );
        tempDist2 = vtkMath::Distance2BetweenPoints( x, pntCoords );
        ptsSorter.InsertPoint( tempDist2, pointIndx );
        }

      // as we might have inserted some points, we need to update maxiDist2
      maxiDist2 = ptsSorter.GetLargestDist2();
      }
    }


  // obtain the point indices
  result->SetNumberOfIds( N );
  ptsSorter.GetSortedIds( result );


  // release memory
  pntIdList->Delete();
  pntIdList = NULL;
  startNode = NULL;
  pTheChild = NULL;
  pThisNode = NULL;
  theParent = NULL;
}

// ---------------------------------------------------------------------------
// ----------------------------- Point Insertion -----------------------------
// ---------------------------------------------------------------------------

//----------------------------------------------------------------------------
int vtkIncrementalOctreePointLocator::InitPointInsertion( vtkPoints * points,
  const double bounds[6] )
{
  return this->InitPointInsertion( points, bounds, 0 );
}

//----------------------------------------------------------------------------
int vtkIncrementalOctreePointLocator::InitPointInsertion( vtkPoints * points,
  const double bounds[6], vtkIdType vtkNotUsed( estNumPts ) )
{
  int     i,  bbIndex;
  double  dimDiff[3], tmpBbox[6];

  if ( points == NULL )
    {
    vtkErrorMacro( << "a valid vtkPoints object required for point insertion" );
    return 0;
    }

  // destroy the existing octree, if any
  this->FreeSearchStructure();

  // detach the old vtkPoints object, if any, before attaching a new one
  if ( this->LocatorPoints != NULL )
    {
    this->LocatorPoints->UnRegister( this );
    }
  this->LocatorPoints = points;
  this->LocatorPoints->Register( this );

  // obtain the threshold squared distance
  this->InsertTolerance2 = this->Tolerance * this->Tolerance;

  // Fix bounds
  // (1) push out a little bit if the original volume is too flat --- a slab
  // (2) pull back the x, y, and z's lower bounds a little bit such that
  //     points are clearly "inside" the spatial region.  Point p is taken as
  //     "inside" range r = [r1, r2] if and only if r1 < p <= r2.
  this->OctreeMaxDimSize = 0.0;
  for ( i = 0; i < 3; i ++ )
    {
    bbIndex = ( i << 1 );
    tmpBbox[ bbIndex     ] = bounds[ bbIndex     ];
    tmpBbox[ bbIndex + 1 ] = bounds[ bbIndex + 1 ];
    dimDiff[i] = tmpBbox[ bbIndex + 1 ] - tmpBbox[ bbIndex ];
    this->OctreeMaxDimSize = ( dimDiff[i] > this->OctreeMaxDimSize )
                             ? dimDiff[i] : this->OctreeMaxDimSize;
    }

  if ( this->BuildCubicOctree )
    {
    // make the bounding box a cube and hence descendant octants cubes too
    for ( i = 0; i < 3; i ++ )
      {
      if ( dimDiff[i] != this->OctreeMaxDimSize )
        {
        double delta = this->OctreeMaxDimSize - dimDiff[i];
        tmpBbox[   i << 1       ] -= 0.5 * delta;
        tmpBbox[ ( i << 1 ) + 1 ] += 0.5 * delta;
        dimDiff[i] = this->OctreeMaxDimSize;
        }
      }
    }

  this->FudgeFactor  = this->OctreeMaxDimSize * 10e-6;
  double minSideSize = this->OctreeMaxDimSize * 10e-2;

  for ( i = 0; i < 3; i ++ )
    {
    if ( dimDiff[i] < minSideSize ) // case (1) above
      {
      bbIndex  =  ( i << 1 );
      double  tempVal = tmpBbox[ bbIndex ];
      tmpBbox[ bbIndex     ] = tmpBbox[ bbIndex + 1 ] - minSideSize;
      tmpBbox[ bbIndex + 1 ] = tempVal + minSideSize;
      }
    else                             // case (2) above
      {
      tmpBbox[ i << 1 ] -= this->FudgeFactor;
      }
    }

  // init the octree with an empty leaf node
  this->OctreeRootNode = vtkIncrementalOctreeNode::New();

  // this call internally inits the middle (center) and data range, too
  this->OctreeRootNode->SetBounds( tmpBbox[0], tmpBbox[1],
                                   tmpBbox[2], tmpBbox[3],
                                   tmpBbox[4], tmpBbox[5] );

  return 1;
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindClosestPointInSphereWithTolerance
  ( const double point[3], double radius2,
    vtkIncrementalOctreeNode * maskNode, double * minDist2 )
{
  *minDist2 = this->OctreeMaxDimSize * this->OctreeMaxDimSize * 4.0;
  return this->FindClosestPointInSphere( point, radius2, maskNode,
                                               minDist2, &radius2 );
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindDuplicateFloatTypePointInVisitedLeafNode
  ( vtkIncrementalOctreeNode * leafNode, const double point[3] )
{
  int         numPts = 0;
  float       thePnt[3];
  float *     pFloat = NULL;
  float *     tmpPnt = NULL;
  vtkIdType   tmpIdx = -1;
  vtkIdType   pntIdx = -1;
  vtkIdList * idList = NULL;

  thePnt[0] = static_cast< float >( point[0] );
  thePnt[1] = static_cast< float >( point[1] );
  thePnt[2] = static_cast< float >( point[2] );

  idList = leafNode->GetPointIdSet();
  numPts = idList->GetNumberOfIds( );
  pFloat = (  static_cast< vtkFloatArray * > ( this->LocatorPoints->GetData() )  )
           ->GetPointer( 0 );

  for ( int i = 0; i < numPts; i ++ )
    {
    tmpIdx = idList->GetId( i );
    tmpPnt = pFloat + (  ( tmpIdx << 1 )  +  tmpIdx  );

    if (  ( thePnt[0] == tmpPnt[0] ) &&
          ( thePnt[1] == tmpPnt[1] ) &&
          ( thePnt[2] == tmpPnt[2] )
       )
      {
      pntIdx = tmpIdx;
      break;
      }
    }

  pFloat = NULL;
  tmpPnt = NULL;
  idList = NULL;

  return pntIdx;
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindDuplicateDoubleTypePointInVisitedLeafNode
  ( vtkIncrementalOctreeNode * leafNode, const double point[3] )
{
  int         numPts = 0;
  double *    pArray = NULL;
  double *    tmpPnt = NULL;
  vtkIdType   tmpIdx = -1;
  vtkIdType   pntIdx = -1;
  vtkIdList * idList = NULL;

  idList = leafNode->GetPointIdSet();
  numPts = idList->GetNumberOfIds( );
  pArray = (  static_cast< vtkDoubleArray * > ( this->LocatorPoints->GetData() )  )
           ->GetPointer( 0 );

  for ( int i = 0; i < numPts; i ++ )
    {
    tmpIdx = idList->GetId( i );
    tmpPnt = pArray + (  ( tmpIdx << 1 )  +  tmpIdx  );

    if (  ( point[0] == tmpPnt[0] ) &&
          ( point[1] == tmpPnt[1] ) &&
          ( point[2] == tmpPnt[2] )
       )
      {
      pntIdx = tmpIdx;
      break;
      }
    }

  pArray = NULL;
  tmpPnt = NULL;
  idList = NULL;

  return pntIdx;
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::FindDuplicatePointInLeafNode
  ( vtkIncrementalOctreeNode * leafNode, const double point[3] )
{
  if ( leafNode->GetPointIdSet() == NULL )
    {
    return -1;
    }

  return ( this->LocatorPoints->GetDataType() == VTK_FLOAT )
         ? this->FindDuplicateFloatTypePointInVisitedLeafNode
                 ( leafNode, point )
         : this->FindDuplicateDoubleTypePointInVisitedLeafNode
                 ( leafNode, point );
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::IsInsertedPointForZeroTolerance
  ( const double x[3], vtkIncrementalOctreeNode ** leafContainer )
{
  // the target leaf node always exists there since the root node of the
  // octree has been initialized to cover all possible points to be inserted
  // and therefore we do not need to check it here
  *leafContainer = this->GetLeafContainer( this->OctreeRootNode, x );
  vtkIdType  pointIdx = this->FindDuplicatePointInLeafNode
                              ( *leafContainer, x );
  return pointIdx;
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::IsInsertedPointForNonZeroTolerance
  ( const double x[3], vtkIncrementalOctreeNode ** leafContainer )
{
  double     minDist2;  // min distance to ALL existing points
  double     elseDst2;  // min DiSTance to other nodes (inner boundaries)
  double     dist2Ext;  // min distance to an EXTended set of nodes
  vtkIdType  pntIdExt;

  // the target leaf node always exists there since the root node of the
  // octree has been initialized to cover all possible points to be inserted
  // and therefore we do not need to check it here
  *leafContainer = this->GetLeafContainer( this->OctreeRootNode, x );
  vtkIdType  pointIdx = this->FindClosestPointInLeafNode
                              ( *leafContainer, x, &minDist2 );

  if ( minDist2 == 0.0 )
    {
    return pointIdx;
    }

  // As no any 'duplicate' point exists in this leaf node, we need to expand
  // the search scope to capture possible closer points in other nodes.
  elseDst2 = ( *leafContainer )->GetDistance2ToInnerBoundary
                                 ( x, this->OctreeRootNode );

  if ( elseDst2 < this->InsertTolerance2 )
    {
    // one or multiple closer points might exist in the neighboring nodes
    pntIdExt = this->FindClosestPointInSphereWithTolerance
                     (  x, this->InsertTolerance2, *leafContainer, &dist2Ext );

    if ( dist2Ext < minDist2 )
      {
      minDist2 = dist2Ext;
      pointIdx = pntIdExt;
      }
    }

  return  ( minDist2 <= this->InsertTolerance2 )  ?  pointIdx  :  -1;
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::IsInsertedPoint
  ( double x, double  y, double z )
{
  double xyz[3] = { x, y, z };
  return this->IsInsertedPoint( xyz );
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::IsInsertedPoint( const double x[3] )
{
  vtkIncrementalOctreeNode * leafContainer = NULL;
  return this->IsInsertedPoint( x, &leafContainer );
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::IsInsertedPoint
  ( const double x[3], vtkIncrementalOctreeNode ** leafContainer )
{
  return ( this->InsertTolerance2 == 0.0 )
         ? this->IsInsertedPointForZeroTolerance( x, leafContainer )
         : this->IsInsertedPointForNonZeroTolerance( x, leafContainer );
}

//----------------------------------------------------------------------------
int vtkIncrementalOctreePointLocator::InsertUniquePoint( const double point[3],
                                                         vtkIdType & pntId )
{
  vtkIncrementalOctreeNode * leafContainer = NULL;
  pntId = this->IsInsertedPoint( point, &leafContainer );
  return (    ( pntId > -1 )
           ?  0
           :  leafContainer->InsertPoint( this->LocatorPoints,     point,
                                          this->MaxPointsPerLeaf, &pntId, 2 )
         );
}

//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::InsertPointWithoutChecking
  ( const double point[3], vtkIdType & pntId, int insert )
{
  this->GetLeafContainer( this->OctreeRootNode, point )
      ->InsertPoint( this->LocatorPoints,     point,
                     this->MaxPointsPerLeaf, &pntId, ( insert << 1 ) );
}

//----------------------------------------------------------------------------
void vtkIncrementalOctreePointLocator::InsertPoint
  ( vtkIdType ptId, const double x[3] )
{
  this->GetLeafContainer( this->OctreeRootNode, x )
      ->InsertPoint( this->LocatorPoints, x, this->MaxPointsPerLeaf, &ptId, 1 );
}

//----------------------------------------------------------------------------
vtkIdType vtkIncrementalOctreePointLocator::InsertNextPoint( const double x[3] )
{
  vtkIdType  pntId = -1;
  this->GetLeafContainer( this->OctreeRootNode, x )
      ->InsertPoint( this->LocatorPoints, x, this->MaxPointsPerLeaf, &pntId, 2 );
  return pntId;
}