Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
kdtree_d.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Rocstar Simulation Suite *
3  * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4  * *
5  * Illinois Rocstar LLC *
6  * Champaign, IL *
7  * www.illinoisrocstar.com *
8  * sales@illinoisrocstar.com *
9  * *
10  * License: See LICENSE file in top level of distribution package or *
11  * http://opensource.org/licenses/NCSA *
12  *********************************************************************/
13 /* *******************************************************************
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22  *********************************************************************/
23 // ======================================================================
24 //
25 // Copyright (c) 1997 The CGAL Consortium
26 
27 // This software and related documentation is part of the Computational
28 // Geometry Algorithms Library (CGAL).
29 // This software and documentation is provided "as-is" and without warranty
30 // of any kind. In no event shall the CGAL Consortium be liable for any
31 // damage of any kind.
32 //
33 // Every use of CGAL requires a license.
34 //
35 // Academic research and teaching license
36 // - For academic research and teaching purposes, permission to use and copy
37 // the software and its documentation is hereby granted free of charge,
38 // provided that it is not a component of a commercial product, and this
39 // notice appears in all copies of the software and related documentation.
40 //
41 // Commercial licenses
42 // - A commercial license is available through Algorithmic Solutions, who also
43 // markets LEDA (http://www.algorithmic-solutions.de).
44 // - Commercial users may apply for an evaluation license by writing to
45 // Algorithmic Solutions (contact@algorithmic-solutions.com).
46 //
47 // The CGAL Consortium consists of Utrecht University (The Netherlands),
48 // ETH Zurich (Switzerland), Free University of Berlin (Germany),
49 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
50 // (Germany), Max-Planck-Institute Saarbrucken (Germany), RISC Linz (Austria),
51 // and Tel-Aviv University (Israel).
52 //
53 // ----------------------------------------------------------------------
54 //
55 // release : CGAL-2.2
56 // release_date : 2000, September 30
57 //
58 // file : include/CGAL/kdtree_d.h
59 // package : kdtree (2.2.14)
60 // source :
61 // revision :
62 // revision_date :
63 // author(s) : Sariel Har-Peled
64 // Eyal Flato
65 //
66 // coordinator : Tel-Aviv University (Dan Halperin)
67 //
68 //
69 // email : contact@cgal.org
70 // www : http://www.cgal.org
71 //
72 // ======================================================================
73 
74 // ======================================================================
75 // Revised by Xiangmin Jiao. on Oct. 25, 2002
76 // Changed to use vector instead of list.
77 // ======================================================================
78 
79 #ifndef CGAL_KDTREE_D_H
80 #define CGAL_KDTREE_D_H
81 
82 #include <cstring>
83 #include <vector>
84 #include <cassert>
85 
86 #define CGAL_BEGIN_NAMESPACE namespace CGAL {
87 #define CGAL_END_NAMESPACE }
88 
90 
91 /*=======================================================================
92  * Kdtree_interface -
93  * This is the default interface of point. It assume that PT (the
94  * point type have the following properties:
95  * default constructor
96  * int dimension()
97  * const coord_type & operator[]( int ) const
98  * ... operator=( const Pt & ) - copy operator
99 \*=======================================================================*/
100 
101 template <class PT>
103 {
104 public:
105  typedef PT Point;
106 
107  static int dimension( const PT & pnt )
108  {
109  // return pnt.dimensions();
110  return pnt.dimension();
111  }
112 
113  static int compare( int d, const PT & a, const PT & b )
114  {
115  if ( a[ d ] < b[ d ] )
116  return -1;
117  if ( a[ d ] > b[ d ] )
118  return 1;
119 
120  return 0;
121  }
122  static void copy_coord( int d, PT & a, const PT & b )
123  {
124  a[ d ] = b[ d ];
125  }
126 };
127 
128 
129 /*=========================================================================
130  * kdtree_d -
131  * A the kdtree class.
132  *
133  * Remark: The kd-trees allocates all the memory it needs in advance,
134  * This results in a rather efficient memory management, and a fast
135  * iplementation.
136 \*=========================================================================*/
137 template <class Traits>
138 class Kdtree_d
139 {
140 
141 public:
142  typedef typename Traits::Point Point;
143  typedef std::vector<Point> List_points;
144 
145 
146  //-------------------------------------------------------------------------
147  // Extended_Point_d -
148  // A class for representing an extended d-dimal point: with
149  // ability to support +/- infinity. Templated by the kd-treee interface
150  // type.
151  //-------------------------------------------------------------------------
152  class ExtPoint
153  {
154  private:
156  {
157  public:
158  const Point * p_pnt;
159  int type;
160  //signed char type;
161  };
162 
164  int dim;
166 
167  void init( int _dim )
168  {
169  assert( _dim > 0 );
170  dim = _dim;
171 
172  p_arr = (coordinate_type *)malloc( sizeof( coordinate_type )
173  * dim );
174  //printf( "p_arr(new): %p\n", (void *)p_arr );
175  assert( p_arr != NULL );
176 
177  memset( p_arr, 0, sizeof( coordinate_type ) * dim );
178  }
179 
180  public:
181  enum { MINUS_INFINITY = -1, FINITE = 0, PLUS_INFINITY = 1 };
182 
183  ExtPoint( int _type, int _dim )
184  {
185  assert( _type == MINUS_INFINITY || _type == PLUS_INFINITY );
186  init( _dim );
187 
188  for ( int ind = 0; ind < dim; ind++ )
189  p_arr[ ind ].type = _type;
190  }
191 
193  {
194  p_arr = NULL;
195  dim = -1;
196  }
197 
198  ExtPoint( const ExtPoint & p )
199  {
200  init( p.dim );
201 
202  def_pnt = p.def_pnt;
203  for ( int ind = 0; ind < dim; ind++ ) {
204  p_arr[ ind ] = p.p_arr[ ind ];
205  if ( p.p_arr[ ind ].p_pnt == &p.def_pnt )
206  p_arr[ ind ].p_pnt = &def_pnt;
207  }
208  }
209 
210  ExtPoint & operator=( const ExtPoint & p )
211  {
212  term();
213 
214  init( p.dim );
215 
216  def_pnt = p.def_pnt;
217  for ( int ind = 0; ind < dim; ind++ ) {
218  p_arr[ ind ] = p.p_arr[ ind ];
219  if ( p.p_arr[ ind ].p_pnt == &p.def_pnt )
220  p_arr[ ind ].p_pnt = &def_pnt;
221  }
222 
223  return *this;
224  }
225 
226  ExtPoint( const Point & point, int dim )
227  {
228  init( dim );
229 
230  def_pnt = point;
231  for ( int ind = 0; ind < dim; ind++ ) {
232  p_arr[ ind ].p_pnt = &def_pnt;
233  p_arr[ ind ].type = FINITE;
234  }
235  }
236 
237  void term()
238  {
239  if ( p_arr != NULL ) {
240  //printf( "a: %p\n", p_arr );
241  free( p_arr );
242  //printf( "_a\n" );
243  p_arr = NULL;
244  }
245  dim = 0;
246  }
247 
249  {
250  term();
251  }
252 
253  void set_coord( int k, Point & point )
254  {
255  assert( 0 <= k && k < dim );
256 
257  p_arr[ k ].type = FINITE;
258  //p_arr[ k ].p_pnt = &point;
259 
260  Traits::copy_coord( k, def_pnt, point );
261  p_arr[ k ].p_pnt = &def_pnt;
262  }
263 
264  void set_coord( int k, const ExtPoint & point )
265  {
266  assert( 0 <= k && k < dim );
267  assert( 0 <= k && k < point.dim );
268 
269  p_arr[ k ] = point.p_arr[ k ];
270  if ( p_arr[ k ].type == FINITE ) {
271  Traits::copy_coord( k, def_pnt, *(p_arr[ k ].p_pnt) );
272  p_arr[ k ].p_pnt = &def_pnt;
273  }
274  }
275 
276  int compare( int k, const ExtPoint & point ) const
277  {
278  assert( 0 <= k && k < dim );
279  // the following does not compile on msvc++...
280  // coordinate_type & a( p_arr[ k ] ),
281  // & b( point.p_arr[ k ] );
282  coordinate_type & a = p_arr[ k ];
283  coordinate_type & b = point.p_arr[ k ];
284 
285  if ( a.type != FINITE ) {
286  if ( b.type != FINITE ) {
287  return a.type - b.type;
288  } else {
289  return a.type;
290  }
291  } else {
292  if ( b.type != FINITE )
293  return -b.type;
294  else
295  return Traits::compare( k, *a.p_pnt, *b.p_pnt );
296  }
297  }
298 
299 
300  int compare_vector( const ExtPoint & point ) const
301  {
302  int ind, res;
303 
304  for ( ind = 0; ind < dim; ind++ ) {
305  res = compare( ind, point );
306  if ( res != 0 )
307  return res;
308  }
309 
310  return 0;
311  }
312 
313  int compare( int k, const Point & point ) const
314  {
315  assert( 0 <= k && k < dim );
316 
317  // coordinate_type & a( p_arr[ k ] );
318  coordinate_type & a = p_arr[ k ];
319 
320  if ( a.type != FINITE )
321  return a.type;
322  else
323  return Traits::compare( k, *a.p_pnt, point );
324  }
325 
326  int dimension() const
327  {
328  return dim;
329  }
330 
331  int get_coord_status( int d ) const
332  {
333  assert( 0 <= d && d < dim );
334 
335  return p_arr[ d ].type;
336  }
337 
338 
339  const Point * get_coord_point( int d ) const
340  {
341  assert( 0 <= d && d < dim );
342 
343  return p_arr[ d ].p_pnt;
344  }
345  };
346 
347 
348  // Box - represents an axis parallel box.
349  class Box
350  {
351  public:
352  int dim;
354 
355  public:
356  // constructors
357 
358  //CHECK
359  Box()
360  {
361  }
362 
363  Box( const Box & box )
364  {
365  dim = box.dim;
366  left = box.left;
367  right = box.right;
368  }
369 
370 
371  Box( const Point &l, const Point &r, int _dim )
372  {
373  dim = _dim;
374  left = ExtPoint( l, dim );
375  right = ExtPoint( r, dim );
376  }
377 
378  Box( int _dim ) : left( ExtPoint::MINUS_INFINITY, _dim ),
379  right( ExtPoint::PLUS_INFINITY, _dim )
380  {
381  dim = _dim;
382  }
383 
384  ExtPoint & get_vertex( bool f_left )
385  {
386  return f_left? left : right;
387  }
388 
389  // data access
390  void set_left( Point &l)
391  {
392  left = ExtPoint( l, dim );
393  };
394 
395  void set_right( Point &r)
396  {
397  right = ExtPoint( r, dim );
398  }
399 
400  const ExtPoint &get_left() const
401  {
402  return left;
403  }
404 
405  const ExtPoint &get_right() const
406  {
407  return right;
408  }
409 
410 
411  void set_coord_left( int k, Point & p )
412  {
413  left.set_coord( k, p );
414  }
415 
416 
417  void set_coord_right( int k, Point & p )
418  {
419  right.set_coord( k, p );
420  }
421 
422 
423  // operations
424  bool is_in( const Box &o) const
425  // checks if o is completely inside <this>
426  {
427  int dim = left.dimension();
428 
429  for (int i = 0; i < dim; i++)
430  {
431  if ( (left.compare(i, o.get_left()) > 0 )
432  || (right.compare(i, o.get_right()) < 0 ) )
433  return false;
434  }
435 
436  return true;
437  }
438 
439 
440  bool is_in( const Point & o ) const
441  // checks if o is completely inside <this>
442  {
443  int dim = left.dimension();
444  for (int i = 0; i < dim; i++)
445  {
446  if ( (left.compare( i, o ) > 0 ) ||
447  (right.compare( i, o ) <= 0 ) )
448  return false;
449  }
450  return true;
451  }
452 
453 
454  bool is_coord_in_range( int k, const Point & o ) const
455  // checks if o is completely inside <this>
456  {
457  return ( ! ( (left.compare( k, o ) > 0 )
458  || (right.compare( k, o ) <= 0 ) ) );
459  }
460 
461 
462  bool is_intersect( const Box &o) const
463  // checks if there is an intersection between o and this
464  {
465  int dim = left.dimension();
466  for (int i = 0; i < dim; i++)
467  {
468  if ( (left.compare(i, o.get_right()) >= 0) ||
469  (right.compare(i, o.get_left()) <= 0) )
470  return false;
471  }
472  return true;
473  }
474 
475 
476  // checks if there is an intersection between o and this box
477  // only in a specific coordinate...
478  bool is_intersect_in_dim( int d, const Box & o ) const
479  {
480  return (! ( (left.compare( d, o.get_right() ) >= 0 )
481  || (right.compare( d, o.get_left() ) <= 0) ));
482  }
483 
484 
485  // checks if there is an intersection between o and this box
486  // only in a specific coordinate...
487  bool is_intersect_in_dim_closed( int d, const Box & o ) const
488  {
489  return (! ( (left.compare( d, o.get_right() ) > 0 )
490  || (right.compare( d, o.get_left() ) < 0) ));
491  }
492 
493 
494  bool intersect(Box &o)
495  // intersects this with o. the intersection will be in this
496  // returns false if intersection is empty
497  {
498  int dim = left.dimension();
499  for (int i = 0; i < dim; i++)
500  {
501  // left is the maximal of the lefts
502  if (left.compare(i, o.get_left()) == -1)
503  left.set_coord(i, o.get_left());
504 
505  // right is the minimal of the rights
506  if (right.compare(i, o.get_right()) == 1)
507  right.set_coord(i, o.get_right());
508  }
509  return !(is_empty());
510  }
511 
512  bool is_empty() const
513  // return true if this is not an interval (left[k] > right[k])
514  {
515  int dim = left.dimension();
516  for (int i = 0; i < dim; i++)
517  {
518  if (left.compare(i, right) == 1)
519  return true;
520  }
521  return false;
522  }
523 
524  bool is_empty_open() const
525  // return true if this is not an interval (left[k] > right[k])
526  {
527  int dim = left.dimension();
528  for (int i = 0; i < dim; i++)
529  {
530  if ( left.compare(i, right) >= 0 )
531  return true;
532  }
533  return false;
534  }
535 
536  int comp( const Box & o ) const
537  {
538  int res;
539 
540  res = left.compare_vector( o.left );
541  if ( res != 0 )
542  return res;
543 
544  return right.compare_vector( o.right );
545  }
546  // destructor - needed in ...recursive ... DVP
547 
548  ~Box()
549  {
550  left.term();
551  right.term();
552  dim = 0;
553  }
554 
555  };
556 
557 private:
558  class Node
559  {
560  public:
561  class Plane
562  {
563  private:
564  int coord;
566  bool f_plus; // orientation of half space
567  // is (0, 0, ... , +inifinity, 0, ..., 0) inside plane
568 
569  public:
570  Plane() : coord( 0), normal(NULL), f_plus(false) {}
571 
572  Plane( int k, Point & p ) : coord(k), normal(&p), f_plus(false) {}
573 
574  Plane( Plane & p ) : coord( p.coord), normal( p.normal), f_plus(p.f_plus){}
575 
576  void dump( void )
577  {
578  std::cout << "(" << coord << ": " << *normal << ")";
579  }
580 
581  bool is_in( const Point & p ) const
582  {
583  int cmp;
584 
585  cmp = Traits::compare( coord, p, *normal );
586 
587  if ( ! f_plus )
588  cmp = -cmp;
589 
590  return cmp >= 0;
591  }
592 
593  void set_plane(int k, Point &p)
594  {
595  coord = k;
596  normal = &p;
597  //normal->copy( coord, p );
598  }
599 
600  void split( Box & region, bool f_neg )
601  {
602  ExtPoint * p_p = &(region.get_vertex( ! f_neg ));
603 
604  if ( f_neg ) {
605  if ( p_p->compare( coord, *normal ) > 0 )
606  p_p->set_coord( coord, *normal );
607  } else
608  if ( p_p->compare( coord, *normal ) < 0 )
609  p_p->set_coord( coord, *normal );
610  }
611 
612  void orient_half_space( bool f_neg_side )
613  {
614  f_plus = ! f_neg_side;
615  }
616 
617  int get_coord() const
618  {
619  return coord;
620  }
621  };
622 
623  public:
626 
628 
629  enum { LEFT, RIGHT };
630 
631  const Plane & get_hs( int side ) const
632  {
633 
634  ((Plane *)&plane)->orient_half_space( side == LEFT );
635 
636  return plane;
637  }
638 
639 
640  bool is_points_in_hs( const Plane & pl ) const
641  {
642  if ( is_point() )
643  return pl.is_in( *pnt );
644 
645  if ( left != NULL && ( ! left->is_points_in_hs( pl ) ) )
646  return false;
647  if ( right != NULL && ( ! right->is_points_in_hs( pl ) ) )
648  return false;
649 
650  return true;
651  }
652 
653 
654  bool is_valid() const
655  {
656  if ( is_point() )
657  return true;
658 
659  if ( left != NULL )
660  if ( ! left->is_points_in_hs( get_hs( LEFT ) ) )
661  return false;
662  if ( right != NULL )
663  if ( ! right->is_points_in_hs( get_hs( RIGHT ) ) )
664  return false;
665 
666  return true;
667  }
668 
669 
670  void dump( int depth )
671  {
672  int ind;
673 
674  for ( ind = 0; ind < depth; ind++ )
675  std::cout << " ";
676 
677  if ( is_point() ) {
678  std::cout << *pnt << "\n";
679  return;
680  }
681 
682  plane.dump();
683  std::cout << "\n";
684  left->dump( depth + 1 );
685  for ( ind = 0; ind < depth; ind++ )
686  std::cout << " ";
687 
688  std::cout << "!!!!!!!!!!!!\n";
689  right->dump( depth + 1 );
690  }
691 
692  bool is_point() const
693  {
694  return ((left == NULL) && (right == NULL));
695  }
696 
697  typedef std::back_insert_iterator<List_points> back_iter;
698 
699  Node() : plane(), pnt(NULL), left(NULL), right(NULL)
700  {}
701 
702  ~Node()
703  {}
704 
706  const Box & rect )
707  {
708  if ( is_point() ) {
709  if ( rect.is_in( *pnt ) )
710  (*result++) = *pnt;
711  return;
712  }
713  if ( left != NULL )
714  left->copy_subtree_points( result, rect );
715  if ( right != NULL )
716  right->copy_subtree_points( result, rect );
717  }
718 
719  static void search_recursive( back_iter & result,
720  Node * node,
721  const Box & rect,
722  Box & _region,
723  Plane & plane,
724  bool f_split_plus )
725  {
726  //printf( "search_recusrive\n" );
727  Box * p_r = new Box( _region );
728 
729  //printf( "z" );
730  //fflush( stdout );
731 
732  plane.split( *p_r, f_split_plus );
733 
734  //printf( "c" );
735  //fflush( stdout );
736  assert( node != NULL );
737 
738  //printf( "b" );
739  //fflush( stdout );
740  if ( rect.is_in( *p_r ) )
741  {
742  //printf( "5" );
743  //fflush( stdout );
744  node->copy_subtree_points( result, rect );
745  //printf( "\tsearch_recursive done...\n" );
746  //printf( "6" );
747  //fflush( stdout );
748  delete p_r;
749  return;
750  }
751 
752  //printf( "v" );
753  //fflush( stdout );
754 
755  if ( rect.is_intersect_in_dim_closed( plane.get_coord(), *p_r ) )
756  node->search( result, rect, *p_r );
757  //printf( "x" );
758  //fflush( stdout );
759  delete p_r;
760  }
761 
762  void search( std::back_insert_iterator<List_points> result,
763  const Box &rect, Box &region )
764  {
765  if (is_point()) {
766  if ( rect.is_in( *pnt ) )
767  (*result++) = *pnt;
768  return;
769  }
770 
771  //this is not a point so it is a hypeplane
772  if ( left != NULL )
773  search_recursive( result, left, rect,
774  region, plane, true );
775  if ( right != NULL )
776  search_recursive( result, right, rect,
777  region, plane, false );
778  }
779  };
780 
781 
782  typedef Point * Point_ptr;
783 
784  int size;
787  int dim;
788 
791 
792  Node * malloc_node( void )
793  {
794  Node * p_ret;
795 
796  p_ret = &(p_node_arr[ node_count ]);
797  node_count++;
798 
799  assert( node_count <= ( 2 * size ));
800 
801  Node n;
802  *p_ret = n;
803 
804  return p_ret;
805  }
806 
807  static int comp( const Point & a, const Point & b, int dim )
808  {
809  return Traits::compare( dim, a, b );
810  }
811 
812  static int partition( Point_ptr * arr, int left, int right,
813  Point * p_pivot, int dim )
814  {
815  int i, j;
816  Point_ptr tmp;
817 
818  if ( left >= right )
819  return left;
820 
821  i = left;
822  j = right;
823 
824  while ( i < j ) {
825  if ( comp( *(arr[ i ]), *(arr[ j ]), dim ) > 0 ) {
826  tmp = arr[ i ];
827  arr[ i ] = arr[ j ];
828  arr[ j ] = tmp;
829  }
830  if ( comp( *(arr[ i ]), *p_pivot, dim ) < 0 ) {
831  i++;
832  } else
833  if ( comp( *p_pivot, *(arr[ j ]), dim ) <= 0 )
834  j--;
835  }
836 
837  return (i > left)? i - 1 : left;
838  }
839 
840 
841  /* split the array into two sub-arrays, such that all the elements
842  * from left to pos_mid are smaller than the elements from pos+1 to
843  * right.
844  */
845  static void split_arr( Point_ptr * arr,
846  int left,
847  int right,
848  int pos_mid,
849  int dim )
850  {
851  int pos;
852 
853  if ( left >= right )
854  return;
855 
856  pos = partition( arr, left, right, arr[ (left + right ) / 2 ],
857  dim );
858  if ( pos == pos_mid )
859  return;
860 
861  if ( pos < pos_mid )
862  split_arr( arr, pos+1, right, pos_mid, dim );
863  else
864  split_arr( arr, left, pos, pos_mid, dim );
865  }
866 
867 
869  int left, int right, int d )
870  {
871  int max_pos = left;
872  Point mx = *(arr[ max_pos ]);
873 
874  for ( int ind = left + 1; ind <= right; ind++ )
875  if ( comp( mx, *(arr[ ind ]), d ) < 0 ) {
876  mx = *(arr[ ind ]);
877  max_pos = ind;
878  }
879 
880  return arr[ max_pos ];
881  }
882 
883  Node *build_r( Point_ptr * arr, int left, int right,
884  int d )
885  {
886  int num, pos, next_d;
887  Node * n;
888 
889  num = right - left + 1;
890 
891  if ( num < 1)
892  return NULL;
893 
894  // if the list contains only one point,
895  // construct a leaf for this node
896  if ( num == 1) {
897  //n = new node;
898  n = malloc_node();
899  n->pnt = arr[ left ];
900 
901  return n;
902  }
903 
904  // else divide space into two regions in
905  // dim-dim and cotinue recursively
906  pos = (left + right) / 2;
907  split_arr( arr, left, right, pos, d );
908 
909  Point * p_median = get_max_element( arr, left, pos, d );
910 
911  // create division plane;
912  typename Node::Plane plane( d, *p_median );
913  //n = new node;
914  // assert( n != NULL );
915  n = malloc_node();
916 
917  n->plane = plane;
918 
919  next_d = d + 1;
920  if ( next_d >= dim )
921  next_d = 0;
922 
923  // build left sub-tree
924  n->left = build_r( arr, left, pos, next_d );
925 
926  // build right sub-tree
927  n->right = build_r( arr, pos + 1, right, next_d );
928 
929  return n;
930  }
931 
932 public:
933  typedef std::vector<Point> list_points;
934 
935  explicit Kdtree_d(int k = 2)
936  {
937  dim = k;
938  root = NULL;
939  p_arr_pt = NULL;
940  p_node_arr = NULL;
941  }
942 
944  {
945  delete_all();
946  }
947 
948 
949  bool is_valid( bool verbose = false, int level = 0 ) const
950  {
951  (void)verbose;
952  (void)level;
953 
954  if ( root == NULL )
955  return true;
956 
957  return root->is_valid();
958  }
959 
960 
961  void dump()
962  {
963  root->dump( 0);
964  }
965 
966  void delete_all()
967  {
968  root = NULL;
969 
970  if ( p_arr_pt != NULL )
971  delete[] p_arr_pt;
972  p_arr_pt = NULL;
973  if ( p_node_arr != NULL )
974  delete[] p_node_arr;
975  p_node_arr = NULL;
976  }
977 
978  void search( std::back_insert_iterator<list_points> result,
979  Box & rect )
980  {
981  if (root == NULL)
982  return; // it is an empty tree - nothing to search in
983 
984  Box region = Box( dim );
985 
986  root->search( result, rect, region );
987  }
988 
989  void build( std::vector<Point> &v)
990  {
991  int i;
992  Point_ptr * p_arr;
993 
994  size = v.size();
995  p_arr_pt = new Point[ size ];
996  assert( p_arr_pt != NULL );
997 
998  p_arr = new Point_ptr[ size ];
999  assert( p_arr != NULL );
1000 
1001  p_node_arr = new Node[ 2 * size ];
1002  assert( p_node_arr != NULL );
1003 
1004  node_count = 0;
1005 
1006  /* fill the array */
1007  i = 0;
1008  for ( typename std::vector<Point>::iterator j = v.begin();
1009  j != v.end();
1010  j++ )
1011  {
1012  p_arr_pt[ i ] = (*j);
1013  p_arr[ i ] = p_arr_pt + i;
1014  i++;
1015  }
1016 
1017  // recursively build the tree from the sorted list
1018  // starting to divide it in coordinate 0
1019  root = build_r( p_arr, 0, size - 1, 0 );
1020 
1021  //printf( "b\n" );
1022  delete[] p_arr;
1023  }
1024 };
1025 
1026 
1028 
1029 #endif /* CGAL_KDTREE_D_H */
1030 
1031 /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
1032  *
1033  * kdtree_d.h - End of File
1034 \*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
1035 
1036 
1037 
1038 
1039 
1040 
static Point_ptr get_max_element(Point_ptr *arr, int left, int right, int d)
Definition: kdtree_d.h:868
Point * pnt
Definition: kdtree_d.h:625
int size
Definition: kdtree_d.h:784
ExtPoint & get_vertex(bool f_left)
Definition: kdtree_d.h:384
Node * root
Definition: kdtree_d.h:786
Node * build_r(Point_ptr *arr, int left, int right, int d)
Definition: kdtree_d.h:883
void orient_half_space(bool f_neg_side)
Definition: kdtree_d.h:612
int comp(const Box &o) const
Definition: kdtree_d.h:536
void build(std::vector< Point > &v)
Definition: kdtree_d.h:989
bool is_valid(bool verbose=false, int level=0) const
Definition: kdtree_d.h:949
const NT & d
static void split_arr(Point_ptr *arr, int left, int right, int pos_mid, int dim)
Definition: kdtree_d.h:845
void set_coord(int k, const ExtPoint &point)
Definition: kdtree_d.h:264
const Point * get_coord_point(int d) const
Definition: kdtree_d.h:339
bool is_empty() const
Definition: kdtree_d.h:512
j indices k indices k
Definition: Indexing.h:6
const ExtPoint & get_left() const
Definition: kdtree_d.h:400
bool is_valid() const
Definition: kdtree_d.h:654
Node * malloc_node(void)
Definition: kdtree_d.h:792
int compare(int k, const Point &point) const
Definition: kdtree_d.h:313
bool is_point() const
Definition: kdtree_d.h:692
void init(int _dim)
Definition: kdtree_d.h:167
KD_tree::Box box
Definition: Overlay_0d.C:47
static int compare(int d, const PT &a, const PT &b)
Definition: kdtree_d.h:113
Traits::Point Point
Definition: kdtree_d.h:142
void search(std::back_insert_iterator< List_points > result, const Box &rect, Box &region)
Definition: kdtree_d.h:762
ExtPoint left
Definition: kdtree_d.h:353
int dimension() const
Definition: kdtree_d.h:326
ExtPoint(const Point &point, int dim)
Definition: kdtree_d.h:226
Point * Point_ptr
Definition: kdtree_d.h:782
Box(const Box &box)
Definition: kdtree_d.h:363
int compare(int k, const ExtPoint &point) const
Definition: kdtree_d.h:276
Kdtree_d(int k=2)
Definition: kdtree_d.h:935
ExtPoint right
Definition: kdtree_d.h:353
Definition: adj.h:150
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
Node * right
Definition: kdtree_d.h:627
Point object that represents a single point.
Definition: datatypedef.h:68
void dump(int depth)
Definition: kdtree_d.h:670
int get_coord() const
Definition: kdtree_d.h:617
int compare_vector(const ExtPoint &point) const
Definition: kdtree_d.h:300
bool is_coord_in_range(int k, const Point &o) const
Definition: kdtree_d.h:454
Plane(int k, Point &p)
Definition: kdtree_d.h:572
static void copy_coord(int d, PT &a, const PT &b)
Definition: kdtree_d.h:122
Box(const Point &l, const Point &r, int _dim)
Definition: kdtree_d.h:371
void set_coord(int k, Point &point)
Definition: kdtree_d.h:253
std::vector< Point > List_points
Definition: kdtree_d.h:143
Node * left
Definition: kdtree_d.h:627
void delete_all()
Definition: kdtree_d.h:966
void set_left(Point &l)
Definition: kdtree_d.h:390
int get_coord_status(int d) const
Definition: kdtree_d.h:331
int dim
Definition: kdtree_d.h:787
bool is_points_in_hs(const Plane &pl) const
Definition: kdtree_d.h:640
blockLoc i
Definition: read.cpp:79
static int comp(const Point &a, const Point &b, int dim)
Definition: kdtree_d.h:807
ExtPoint(int _type, int _dim)
Definition: kdtree_d.h:183
bool is_in(const Point &p) const
Definition: kdtree_d.h:581
void copy_subtree_points(back_iter &result, const Box &rect)
Definition: kdtree_d.h:705
const NT & n
void set_plane(int k, Point &p)
Definition: kdtree_d.h:593
void set_coord_left(int k, Point &p)
Definition: kdtree_d.h:411
void dump()
Definition: kdtree_d.h:961
ExtPoint(const ExtPoint &p)
Definition: kdtree_d.h:198
static int partition(Point_ptr *arr, int left, int right, Point *p_pivot, int dim)
Definition: kdtree_d.h:812
Node * p_node_arr
Definition: kdtree_d.h:789
bool is_intersect_in_dim(int d, const Box &o) const
Definition: kdtree_d.h:478
static void search_recursive(back_iter &result, Node *node, const Box &rect, Box &_region, Plane &plane, bool f_split_plus)
Definition: kdtree_d.h:719
void split(Box &region, bool f_neg)
Definition: kdtree_d.h:600
Box(int _dim)
Definition: kdtree_d.h:378
coordinate_type * p_arr
Definition: kdtree_d.h:163
bool is_empty_open() const
Definition: kdtree_d.h:524
j indices j
Definition: Indexing.h:6
static int dimension(const PT &pnt)
Definition: kdtree_d.h:107
~Kdtree_d()
Definition: kdtree_d.h:943
int node_count
Definition: kdtree_d.h:790
bool intersect(Box &o)
Definition: kdtree_d.h:494
#define CGAL_BEGIN_NAMESPACE
Definition: kdtree_d.h:86
ExtPoint & operator=(const ExtPoint &p)
Definition: kdtree_d.h:210
void search(std::back_insert_iterator< list_points > result, Box &rect)
Definition: kdtree_d.h:978
CGAL_KERNEL_INLINE Comparison_result compare(const NT &n1, const NT &n2)
Definition: number_utils.h:143
std::back_insert_iterator< List_points > back_iter
Definition: kdtree_d.h:697
bool is_in(const Box &o) const
Definition: kdtree_d.h:424
bool is_intersect(const Box &o) const
Definition: kdtree_d.h:462
const ExtPoint & get_right() const
Definition: kdtree_d.h:405
bool is_in(const Point &o) const
Definition: kdtree_d.h:440
const Plane & get_hs(int side) const
Definition: kdtree_d.h:631
#define CGAL_END_NAMESPACE
Definition: kdtree_d.h:87
void set_right(Point &r)
Definition: kdtree_d.h:395
std::vector< Point > list_points
Definition: kdtree_d.h:933
void set_coord_right(int k, Point &p)
Definition: kdtree_d.h:417
Point * p_arr_pt
Definition: kdtree_d.h:785
bool is_intersect_in_dim_closed(int d, const Box &o) const
Definition: kdtree_d.h:487