-
Notifications
You must be signed in to change notification settings - Fork 0
/
TNear.h
6894 lines (6381 loc) · 263 KB
/
TNear.h
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
//*
//* TNear.h
//* NearTree
//*
//* Copyright 2001, 2008 Larry Andrews. All rights reserved
//* Revised 12 Dec 2008 for sourceforge release -- H. J. Bernstein
//* Revised 30 May 2009, release with full containerization of C++
//* version and KNear/Far in C++ and C, LCA + HJB
//* Revised 13 Nov 2010, revisions to C++ version for balanced
//* searches, LCA+HJB
//**********************************************************************
//* *
//* YOU MAY REDISTRIBUTE NearTree UNDER THE TERMS OF THE LGPL *
//* *
//**********************************************************************/
//************************* LGPL NOTICES *******************************
//* *
//* This library is free software; you can redistribute it and/or *
//* modify it under the terms of the GNU Lesser General Public *
//* License as published by the Free Software Foundation; either *
//* version 2.1 of the License, or (at your option) any later version. *
//* *
//* This library is distributed in the hope that it will be useful, *
//* but WITHOUT ANY WARRANTY; without even the implied warranty of *
//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
//* Lesser General Public License for more details. *
//* *
//* You should have received a copy of the GNU Lesser General Public *
//* License along with this library; if not, write to the Free *
//* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
//* MA 02110-1301 USA *
//* *
//**********************************************************************/
// This is a revised release of
// template <typename T> class CNearTree;
//
// Nearest Neighbor algorithm after Kalantari and McDonald,
// (IEEE Transactions on Software Engineering, v. SE-9, pp.
// 631-634,1983)
// modified to use recursion instead of a double-linked tree
// and simplified so that it does a bit less checking for
// things like is the distance to the right less than the
// distance to the left; it was found that these checks made little
// to no difference in timing.
// Later revisions have replaced the use of recursion with a stack,
// except for the case of inserting data into the tree.
// This template is used to contain a collection of objects. After the
// collection has been loaded into this structure, it can be quickly
// queried for which object is "closest" to some probe object of the
// same type. The major restriction on applicability of the near-tree
// is that the algorithm only works if the objects obey the triangle
// inequality. The triangle rule states that the length of any side of
// a triangle cannot exceed the sum of the lengths of the other two sides.
// The user of this class needs to provide at least the following
// functionality for the template to work. For the built-in
// numerics of C++, they are provided here (or else you should create them).
// DistanceType Norm( ); // conversion constructor from the templated class to DistanceType
// (usually will return a "length" of type double)
// operator- ( ); // geometrical (vector) difference of two objects
// a copy constructor would be nice
// a constructor would be nice
// a destructor would be nice
// The provided interface is:
//
// #include "TNear.h"
//
// CNearTree( void ) // constructor
// instantiated by something like: CNearTree <T> vTree;
// for some type T
// the following additional convenience constructors are available
//
// CNearTree( const ContainerType<T> ) // constructor from containers, std::vector, ..., or CNearTree
//
// void insert( const T& t )
// where t is an object of the type T
// the following additional convenience insert template available
// all inserts are delayed until a search is performed or until an explicit call to CompleteDelayedInsertions
// is called or a search is called. The purpose is to distribute the objects a bit more
// randomly. Excessively ordered objects leads to less than optimal trees.
// Places objects in a queue for insertion later when CompleteDelayInsert
//
// void insert( ContainerType ) // for containers, std::vector, ..., or CNearTree
// all inserts are delayed until a search is performed or until an explicit call to CompleteDelayedInsertions
//
// bool NearestNeighbor ( const DistanceType dRadius, T& tClosest, const T& t ) const
// dRadius is the largest radius within which to search; make it
// very large if you want to include every point that was loaded; dRadius
// is returned as the closest distance to the probe (or the search radius
// if nothing is found)
// tClosest is returned as the object that was found closest to the probe
// point (if any were within radius dRadius of the probe)
// t is the probe point, used to search in the group of points insert'ed
// return value is true if some object was found within the search radius, false otherwise
//
// iterator NearestNeighbor( const DistanceType radius, const T& probe ); returns an iterator
// to the nearest point to the probe point or end() if there is none
//
// bool FarthestNeighbor ( T& tFarthest, const T& t ) const
// tFarthest is returned as the object that was found farthest to the probe
// point
// t is the probe point, used to search in the group of points insert'ed
// return value is true if some object was found, false otherwise
//
// iterator FarthestNeighbor( const T& probe ); returns an iterator
// to the farthest point to the probe point or end() if there is none
//
//
// the following functions (FindInSphere, FindOutSphere, and FindInAnnulus) all return a container
// (ContainerType) that can be any standard library container (such as std::vector< T >) or CNearTree.
// each has an alternate version in which, in addition the indices of the objects in the
// object store are returned in a second parallel vector.
//
// long FindInSphere ( const DistanceType dRadius, ContainerType& tClosest, const T& t ) const
// long FindInSphere ( const DistanceType dRadius, ContainerType& tClosest, std::vector<size_t>& tIndices, const T& t ) const
// dRadius is the radius within which to search; make it very large if you want to
// include every point that was loaded;
// tClosest is returned as the ContainerType of objects that were found within a radius dRadius
// of the probe point
// if tIndices is used, it is a vector to which to add the indices of the points found
// t is the probe point, used to search in the group of points insert'ed
// return value is the number of objects found within the search radius
//
// long FindOutSphere ( const DistanceType dRadius, ContainerType& tClosest, const T& t ) const
// long FindOutSphere ( const DistanceType dRadius, ContainerType& tClosest, std::vector<size_t>& tIndices, const T& t ) const
// dRadius is the radius outside which to search; make it very small if you want to
// include every point that was loaded;
// tClosest is returned as the ContainerType of objects that were found within a radius dRadius
// of the probe point
// if tIndices is used, it is a vector to which to add the indices of the points found
// t is the probe point, used to search in the group of points insert'ed
// return value is the number of objects found within the search radius
//
// long FindInAnnulus (const DistanceType dRadius1, const DistanceType dRadius2, ContainerType& tClosest, const T& t ) const
// long FindInAnnulus (const DistanceType dRadius1, const DistanceType dRadius2, ContainerType& tClosest, std::vector<size_t>& tIndices, const T& t ) const
// dRadius1 and dRadius2 are the two radii between which to find data points
// tClosest is returned ContainerType of the objects found in the annulus
// if tIndices is used, it is a vector to which to add the indices of the points found
// t is the probe point, used to search in the group of points insert'ed
// return value is the number of objects found within the search radius
//
// long FindK_NearestNeighbors ( const size_t k, const DistanceType& radius, OutputContainerType& tClosest, const T& t )
// long FindK_NearestNeighbors ( const size_t k, const DistanceType& radius, OutputContainerType& tClosest, std::vector<size_t>& tIndices, const T& t )
// k is the maximum number of nearest neighbors to return. Finds this many if possible
// radius Within a sphere defined by radius, search for the k-nearest-neighbors
// tClosest is returned ContainerType of the objects found within the sphere
// if tIndices is used, it is a vector to which to add the indices of the points found
// t is the probe point, used to search in the group of points insert'ed
//
// long FindK_FarthestNeighbors ( const size_t k, OutputContainerType& tClosest, const T& t )
// long FindK_FarthestNeighbors ( const size_t k, OutputContainerType& tClosest, std::vector<size_t>& tIndices, const T& t )
// k is the maximum number of farthest neighbors to return. Finds this many if possible
// tClosest is returned ContainerType of the objects found
// if tIndices is used, it is a vector to which to add the indices of the points found
// t is the probe point, used to search in the group of points insert'ed
//
// The variants LeftNearestNeighbor, LeftFarthestNeighbor, LeftFindInSphere, LeftFindOutSphere,
// LeftFindInAnnulus, LeftFindK_NearestNeighbors, and LeftFindK_FarthestNeighbors are the
// older, search-left-first versions, retained for exiting applications that may require support
// for those versions and for testing and validation. Those older versions are deprecated
// and may be removed in an upcoming release.
//
// ~CNearTree( void ) // destructor
//
// =====================================================================================================
// access functions
//
// T at( const size_t n ) const
// returns the n'th item of the internal data store
//
// T operator[] ( const size_t n)
// returns the n'th item of the internal data store
//
// operator ContainerType( void ) const
// returns all of the inserted objects in the tree in a container of type ContainerType.
// ContainerType can be std::vector<T>, etc, or other containers.
// The returned vector contents are not guaranteed to be returned in the order loaded.
//
// iterator begin ( void ) const
// returns an iterator to the beginning of the internal data store
//
// iterator end ( void ) const
// returns an iterator to the end of the data store (one beyond the last item)
//
// iterator back ( void ) const
// returns an iterator to the last data item of the internal data store
//
// =====================================================================================================
// information and special operation functions
// =====================================================================================================
//
// void ImmediateInsert( void ) Places objects immediately into the tree. The usual insert function
// delays insertions, allowing them to be inserted into the tree in a more random order. The delay
// can improve the structure of the tree and speed searches.
//
// void CompleteDelayedInsert( void ) Calls insert for all delayed objects. sqrt(n) of them are inserted
// by random choice. The rest are inserted in linear order as originally queued. CompleteDelayedInsert
// is invoked at the beginning of all searches, so the average user will never need
// to call it.
//
// size_t GetDeferredSize( void ) Returns the number of delayed objects that have not
// yet been insert'ed. This is mainly for information about details of the tree.
//
// size_t GetTotalSize( void ) Returns the number of objects that have been insert'ed plus
// those DelayInsert'ed
//
// size_t size( void ) identical to GetTotalSize
//
// size_t GetDepth( void ) Returns the maximum tree layers from the root. This is
// mainly for information about details of the tree.
//
// bool empty( void ) returns true if the tree is empty, otherwise false
//
// =====================================================================================================
// iterators
// Random access iterators are provided for accessing the data in a CNearTree. The most important
// expected use is to retrieve the objects returned from one of the sphere search functions that
// return a CNearTree. However, they can be used with any CNearTree.
// They should function in a fashion essentially the same as STL iterators. There is no assurance
// that data will be returned in the order it was loaded, just that it is accessible. The same set is
// provided for const_iterator.
// =====================================================================================================
// iterator( void ) { }; // constructor
//
// iterator& operator= ( const iterator& s )
// iterator operator++ ( const int n )
// iterator operator-- ( const int n )
// iterator& operator++ ( void )
// iterator& operator-- ( void )
// iterator operator+ ( const long n ) const
// iterator operator- ( const long n ) const
// iterator& operator+= ( const long n )
// iterator& operator-= ( const long n )
// T operator* ( void ) const
//
// bool operator== ( const iterator& t ) const
// bool operator!= ( const iterator& t ) const
//
// const T * const operator-> ( void ) const
//
// =====================================================================================================
//
// So a complete program is:
//
// #include "TNear.h"
// #include <cstdio>
// void main()
// {
// CNearTree< double > dT;
// double dNear;
// dT.insert( 1.5 );
// if ( dT.FindNearestNeighbor( 10000.0, dNear, 2.0 )) printf( "%f\n",DistanceType(dNear-2.0) );
// }
//
// and it should print 0.5 (that's how for 2.0 is from 1.5)
//
//
//-------------------------------------------------------------------------
#if !defined(TNEAR_H_INCLUDED)
#define TNEAR_H_INCLUDED
#include <stdlib.h>
#include <limits.h>
#include <cfloat>
#include <algorithm>
#include <cmath>
#define USE_LOCAL_HEADERS
#define USE_ARMADILLO_LIBRARY
#ifdef _MSC_VER
#define USE_LOCAL_HEADERS
#endif
#ifndef USE_LOCAL_HEADERS
#include <rhrand.h>
#include <triple.h>
#else
#include "rhrand.h"
#include "triple.h"
#endif
//
//#ifdef USE_ARMADILLO_LIBRARY
//#define ARMA_DONT_USE_BLAS
//#define ARMA_DONT_USE_LAPACK
//#include <armadillo>
//#endif
#include <vector>
#include <set>
#include <iterator>
#ifdef CNEARTREE_SAFE_TRIANG
#define TRIANG(a,b,c) ( (((b)+(c))-(a) >= 0) \
|| ((b)-((a)-(c)) >= 0) \
|| ((c)-((a)-(b)) >= 0))
#else
#define TRIANG(a,b,c) ( (((b)+(c))-(a) >= 0))
#endif
#define CNEARTREE_COLLIDE 1.e-38
//=======================================================================
// CNearTree is the root class for the neartree. The actual data of the
// tree is stored in NearTreeNode objects descending from a CNearTree.
//=======================================================================
template <typename T, typename DistanceType=double, int distMinValue=-1 > class CNearTree
{
//=======================================================================
// NOTES:
//
// The types of objects that can be stored in the tree is quite broad. The
// biggest limitation is that the objects must reside in some sort of metric
// space and must obey the triangle rule. They must also be all of the same
// size because they are stored in an std::vector. If your application
// requires object of varying storage, then your best way to use this
// code is to store pointers or handles and to write your own distance functions.
//
// The type of the objects to be stored is the only _required_ template argument.
// The type of the distance measure (DistanceType) defaults to double. If your
// applications is for an integer type then the type for DistanceType can be your
// integer type. This has the potential for speeding the calculations by
// avoiding FP computation. Other general types can be used if desired, but you
// may need to also input a value of distMinValue.
//
// The template argument distMinValue must be something that your class will
// understand as a negative number. The default input is negative one. Internally,
// that is cast to DistanceType. Since most uses will be for DistanceType
// to be double, that is a simple conversion. Obviously, for integer types,
// there is no problem either. The need for this value is to have something
// internally that is recognizable as smaller than the smallest "distance"
// that can exist between any two objects in your type. For most users,
// there is no need to input anything other than the default, -1. -1 must
// be castable to DistanceType. It seems unlikely that anyone would actually
// need this optional parameter, but it is here for completeness.
//
// It is a design decision that this class cannot work for unsigned types.
// It is hard to see how to verify the triangle rule for unsigned types,
// and distance computations become more complex. Sorry, unsigned types
// are left as an exercise for the reader.
//
//=======================================================================
// insert copies the input objects into a binary NEAR tree. When a node has
// two entries, a descending node is used or created. The current datum is
// put into the branch descending from the nearer of the two
// objects in the current node.
// NearestNeighbor retrieves the object nearest to some probe by descending
// the tree to search out the appropriate object. Speed is gained
// by pruning the tree if there can be no data below that are
// nearer than the best so far found.
// The tree is built in time O(n log n), and retrievals take place in
// average time O(log n). However, worst case is O(n).
public:
// DistanceBetween
// template function for calculating the "distance" between two objects.
// The specific functions for the built-in types must be here also. For
// the common types (int, float, ...) they are provided.
template <typename TT>
static inline DistanceType DistanceBetween( const TT& t1, const TT& t2 )
{
DistanceType d = ( t1-t2 ).norm( );
return( d>0?d:-d ); // apparent compiler error makes this necessary
}
static inline size_t DistanceBetween( const size_t& t1, const size_t& t2 )
{
size_t diff = t1-t2;
return diff >0?diff:-diff; // apparent compiler error makes this necessary
}
static inline DistanceType DistanceBetween( const double t1, const double t2 )
{
return( (DistanceType)fabs( t1-t2 ) ); // encourage the compiler to get the correct abs
}
//static inline DistanceType DistanceBetween( const long double t1, const long double t2 )
//{
// return( (DistanceType)fabsl(t1-t2) ); // encourage the compiler to get the correct abs
//}
static inline DistanceType DistanceBetween( const float t1, const float t2 )
{
return( (DistanceType)fabsf( t1-t2 )); // encourage the compiler to get the correct abs
}
static inline DistanceType DistanceBetween( const int t1, const int t2 )
{
return( (DistanceType)abs(t1-t2) ); // encourage the compiler to get the correct abs
}
static inline DistanceType DistanceBetween( const long t1, const long t2 )
{
return( (DistanceType)labs(t1-t2) ); // encourage the compiler to get the correct abs
}
//static inline DistanceType DistanceBetween( const long long t1, const long long t2 )
//{
// return( (DistanceType)llabs(t1-t2) ); // encourage the compiler to get the correct abs
//}
static inline DistanceType DistanceBetween( const short t1, const short t2 )
{
return( (DistanceType)abs(t1-t2) ); // encourage the compiler to get the correct abs
}
private:
RHrand rhr;
// forward declaration of nested class NearTreeNode
template <typename TNode, typename DistanceTypeNode, int distMinValueNode >
class NearTreeNode;
public:
// Forward declaration for the nested classes, iterator and const_iterator. Friend is necessary
// for the access to the appropriate data elements
class iterator;
friend class iterator;
class const_iterator;
friend class const_iterator;
static const long NTF_NoPrePrune = 1; //flag to supress all search prepruning
static const long NTF_ForcePrePrune = 2; //flag to force search prepruning
static const long NTF_NoFlip = 4; //flag to suppress flips on insert
static const long NTF_ForceFlip = 8; //flag to force flips on insert
static const long NTF_NoDefer =16; //flag to prevent deferred insert
#ifdef CNEARTREE_FORCEPREPRUNE
static const long NFT_FlagDefaultPrune = NTF_ForcePrePrune;
#ifdef CNEARTREE_NOPREPRUNE
#error "CNEARTREE_NOPREPRUNE conflicts with CNEARTREE_FORCEPREPRUNE"
#endif
#else
#ifdef CNEARTREE_NOPREPRUNE
static const long NFT_FlagDefaultPrune = NTF_NoPrePrune;
#else
static const long NFT_FlagDefaultPrune = 0;
#endif
#endif
#ifdef CNEARTREE_FORCEFLIP
static const long NFT_FlagDefaultFlip = NTF_ForceFlip;
#ifdef CNEARTREE_NOFLIP
#error "CNEARTREE_NOFLIP conflicts with CNEARTREE_FORCEFLIP"
#endif
#else
#ifdef CNEARTREE_NOFLIP
static const long NFT_FlagDefaultFlip = NTF_NoFlip;
#else
static const long NFT_FlagDefaultFlip = 0;
#endif
#endif
#ifdef CNEARTREE_NODEFER
static const long NFT_FlagDefaultDefer = NTF_NoDefer;
#else
static const long NFT_FlagDefaultDefer = 0;
#endif
static const long NTF_FlagsDefault = NFT_FlagDefaultPrune|NFT_FlagDefaultFlip|NFT_FlagDefaultDefer;
private: // start of real definition of CNearTree
std::vector<long> m_DelayedIndices; // objects queued for insertion, possibly in random order
std::vector<T> m_ObjectStore; // all inserted objects go here
std::vector<size_t>
m_ObjectCollide; // overflow chain of colliding objects
size_t m_DeepestDepth; // maximum depth of the tree
NearTreeNode<T, DistanceType, distMinValue> m_BaseNode; // the tree's data is stored down from here
long m_Flags; // flags for operational control (mainly for testing)
DistanceType m_DiamEstimate; // estimated diameter
DistanceType m_SumSpacings; // sum of spacings at time of insertion
DistanceType m_SumSpacingsSq; // sum of squares of spacings at time of insertion
double m_DimEstimate; // estimated dimension
double m_DimEstimateEsd; // estimated dimension estimated standard deviation
#ifdef CNEARTREE_INSTRUMENTED
mutable size_t m_NodeVisits; // number of node visits
#endif
public:
//=======================================================================
// CNearTree ( )
//
// Default constructor for class CNearTree
// creates an empty tree with no right or left node and with the dMax-below
// set to negative values so that any match found will be stored since it will
// greater than the negative value
//
//=======================================================================
CNearTree ( void ) // constructor
: m_DelayedIndices ( )
, m_ObjectStore ( )
, m_ObjectCollide ( )
, m_DeepestDepth ( 0 )
, m_BaseNode ( )
, m_Flags ( NTF_FlagsDefault )
, m_DiamEstimate ( DistanceType( 0 ) )
, m_SumSpacings ( DistanceType( 0 ) )
, m_SumSpacingsSq ( DistanceType( 0 ) )
, m_DimEstimate ( 0 )
, m_DimEstimateEsd( 0 )
#ifdef CNEARTREE_INSTRUMENTED
, m_NodeVisits( 0 )
#endif
{
} // CNearTree constructor
//=======================================================================
// CNearTree ( const InputContainer& o )
//
// templated constructor for class CNearTree for input of containers.
// The containers can be standard library containers or a CNearTree.
//
//=======================================================================
template<typename InputContainer>
CNearTree ( const InputContainer& o ) // constructor
: m_DelayedIndices ( )
, m_ObjectStore ( )
, m_ObjectCollide ( )
, m_DeepestDepth ( 0 )
, m_BaseNode ( )
, m_Flags ( NTF_FlagsDefault )
, m_DiamEstimate ( DistanceType( 0 ) )
, m_SumSpacings ( DistanceType( 0 ) )
, m_SumSpacingsSq ( DistanceType( 0 ) )
, m_DimEstimate ( 0 )
, m_DimEstimateEsd( 0 )
#ifdef CNEARTREE_INSTRUMENTED
, m_NodeVisits( 0 )
#endif
{
typename InputContainer::const_iterator it;
for( it=o.begin(); it!=o.end(); ++it )
{
insert( *it );
}
} // CNearTree constructor
//=======================================================================
// CNearTree ( InputContainer& o )
//
// templated constructor for class CNearTree for input of containers.
// The containers can be standard library containers or a CNearTree.
//
//=======================================================================
template<typename InputContainer>
explicit CNearTree ( InputContainer& o ) // constructor
: m_DelayedIndices ( )
, m_ObjectStore ( )
, m_ObjectCollide ( )
, m_DeepestDepth ( 0 )
, m_BaseNode ( )
, m_Flags ( NTF_FlagsDefault )
, m_DiamEstimate ( DistanceType( 0 ) )
, m_SumSpacings ( DistanceType( 0 ) )
, m_SumSpacingsSq ( DistanceType( 0 ) )
, m_DimEstimate ( 0 )
, m_DimEstimateEsd( 0 )
#ifdef CNEARTREE_INSTRUMENTED
, m_NodeVisits( 0 )
#endif
{
typename InputContainer::iterator it;
for( it=o.begin(); it!=o.end(); ++it )
{
insert( *it );
}
} // CNearTree constructor
//=======================================================================
// CNearTree ( const InputContainer& o1, const InputContainer& o1 )
//
// templated constructor for class CNearTree for merging multiple
// containers.
// The containers can be standard library containers or CNearTrees.
//
//=======================================================================
template<typename InputContainer1, typename InputContainer2>
CNearTree ( const InputContainer1& o1, const InputContainer2& o2 ) // constructor
: m_DelayedIndices ( )
, m_ObjectStore ( )
, m_ObjectCollide ( )
, m_DeepestDepth ( 0 )
, m_BaseNode ( )
, m_Flags ( NTF_FlagsDefault )
, m_DiamEstimate ( DistanceType( 0 ) )
, m_SumSpacings ( DistanceType( 0 ) )
, m_SumSpacingsSq ( DistanceType( 0 ) )
, m_DimEstimate ( 0 )
, m_DimEstimateEsd( 0 )
#ifdef CNEARTREE_INSTRUMENTED
, m_NodeVisits( 0 )
#endif
{
typename InputContainer1::const_iterator it1;
for( it1=o1.begin(); it1!=o1.end(); ++it1 )
{
insert( *it1 );
}
typename InputContainer2::const_iterator it2;
for( it2=o2.begin(); it2!=o2.end(); ++it2 )
{
insert( *it2 );
}
} // CNearTree constructor
//=======================================================================
// ~CNearTree ( )
//
// Destructor for class CNearTree
//
//=======================================================================
~CNearTree ( void ) // destructor
{
clear ( );
} // ~CNearTree
//=======================================================================
// Name: Get and Set Flags
// Description: get and set tree flags
//
//=======================================================================
long GetFlags( void ) const
{
return m_Flags;
}
void SetFlags( const long flags )
{
m_Flags = flags;
}
long GetFlags( const long mask ) const
{
return m_Flags&mask;
}
void SetFlags( const long flags, const long mask )
{
m_Flags = (flags&mask)|(m_Flags&(~mask));
}
//=======================================================================
// Name: operator=()
// Description: put container's contents into a NearTree,
// wiping out the current contents
//
//=======================================================================
template<typename InputContainer>
CNearTree& operator= ( const InputContainer& o )
{
if (this != &o) {
this->clear();
this->insert( o );
this->CompleteDelayedInsert( );
}
return( *this );
}
template<typename InputContainer>
CNearTree& operator= ( InputContainer& o )
{
if (this != &o) {
this->clear();
this->insert( o );
this->CompleteDelayedInsert( );
}
return( *this );
}
/*
template<typename InputContainer>
CNearTree& operator= ( const InputContainer& o ) const
{
if (this != &o) {
this->clear();
this->insert( o );
this->CompleteDelayedInsert( );
}
return( *this );
}
template<typename InputContainer>
CNearTree& operator= ( InputContainer& o ) const
{
if (this != &o) {
this->clear();
this->insert( o );
this->CompleteDelayedInsert( );
}
return( *this );
}
*/
//=======================================================================
// Name: operator+=()
// Description: add a container's contents to a NearTree
//
//=======================================================================
template<typename InputContainer>
CNearTree& operator+= ( const InputContainer& o )
{
if ( this->empty( ) )
{ // if "this" is empty, all that will remain is "o"
this->insert( o );
}
else if ( o.empty( ) )
{ // do nothing if there is nothing to be added to "this"
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_union(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
template<typename InputContainer>
CNearTree& operator+= ( InputContainer& o )
{
if ( this->empty( ) )
{ // if "this" is empty, all that will remain is "o"
this->insert( o );
}
else if ( o.empty( ) )
{ // do nothing if there is nothing to be added to "this"
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_union(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
//=======================================================================
// Name: operator-=()
// Description: removes a container's contents from a NearTree
//
//=======================================================================
template<typename InputContainer>
CNearTree& operator-= ( const InputContainer& o )
{
if ( this->empty( ) )
{// nothing to do if there's nothing to remove from
}
else if ( o.empty( ) )
{ // do nothing if there is nothing to be removed
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_difference(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
template<typename InputContainer>
CNearTree& operator-= ( InputContainer& o )
{
if ( this->empty( ) )
{// nothing to do if there's nothing to remove from
}
else if ( o.empty( ) )
{ // do nothing if there is nothing to be removed
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_difference(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
//=======================================================================
// Name: set_symmetric_difference()
// Description: removes the portion container's contents from a NearTree
// that is already in the NearTree and add in the portion
// of the container's contents that is not already in the
// NearTree
// (= Sheffer stroke operation and NAND = exclusive or)
//
//=======================================================================
template<typename InputContainer>
CNearTree& set_symmetric_difference ( const InputContainer& o )
{
if ( o.empty( ) )
{ // do nothing if "this" is already complete
}
else if ( this->empty( ) )
{ // all that will remain is the content of "o"
this->insert( o );
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_symmetric_difference(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
template<typename InputContainer>
CNearTree& set_symmetric_difference ( InputContainer& o )
{
if ( o.empty( ) )
{ // do nothing if "this" is already complete
}
else if ( this->empty( ) )
{ // all that will remain is the content of "o"
this->insert( o );
}
else
{
std::set<T> s1, s2, s3;
s1.insert( this->begin( ), this->end( ) );
s2.insert( o.begin(), o.end( ) );
this->clear( );
std::set_symmetric_difference(
s1.begin( ), s1.end( ),
s2.begin( ), s2.end( ),
std::inserter( s3, s3.end( ) ) );
this->insert( s3 );
}
this->CompleteDelayedInsert( );
return( *this );
}
//=======================================================================
// clear( void )
//
// removes all content from a tree
//
//=======================================================================
void clear ( void )
{
this->m_BaseNode .clear( ); // clear the nodes of the tree
if ( ! this->m_DelayedIndices.empty( ) )
{
std::vector<long> vtempLong;
m_DelayedIndices.swap( vtempLong ); // release any delayed indices list
}
if ( ! this->m_ObjectStore.empty( ) )
{
std::vector<T> vtempT;
this->m_ObjectStore.swap( vtempT ); // release the object store
std::vector<size_t> vtempOC;
this->m_ObjectCollide.swap( vtempOC); // release the object collision store;
}
this->m_DeepestDepth = 0;
this->m_DiamEstimate = DistanceType( 0 );
this->m_SumSpacings = DistanceType( 0 );
this->m_SumSpacingsSq = DistanceType( 0 );
this->m_DimEstimate = 0;
this->m_DimEstimateEsd = 0;
#ifdef CNEARTREE_INSTRUMENTED
this->m_NodeVisits = 0;
#endif
}
//=======================================================================
// empty ( )
//
// Test for an empty CNearTree
//
//=======================================================================
bool empty ( void ) const
{
return ( m_ObjectStore.empty( ) );
}
//=======================================================================
// void insert ( const T& t )
//
// Function to insert some "point" as an object into a CNearTree for
// later searching
//
// t is an object of the templated type which is to be inserted into a
// NearTree
//
// The function ImmediateInsert immediately inserts the object into the tree.
// insert keeps the object in an internal store, but does not