/usr/include/CGAL/kdtree_d.h is in libcgal-dev 4.2-5ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Copyright (c) 1997 Tel-Aviv University (Israel).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
// You can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL$
// $Id$
//
//
// Author(s) : Sariel Har-Peled (sariel@math.tau.ac.il)
// Eyal Flato (flato@math.tau.ac.il)
#ifndef CGAL_KDTREE_D_H
#define CGAL_KDTREE_D_H
#include <CGAL/basic.h>
#include <cstdlib>
#include <cstring>
#include <list>
using std::list; // to avoid compiler crash on MSVC++
namespace CGAL {
/*=======================================================================
* Kdtree_interface -
* This is the default interface of point. It assume that PT (the
* point type have the following properties:
* default constructor
* int dimension()
* const coord_type & operator[]( int ) const
* ... operator=( const Pt & ) - copy operator
\*=======================================================================*/
template <class PT>
class Kdtree_interface
{
public:
typedef PT Point;
static int dimension( const PT & pnt )
{
// return pnt.dimensions();
return pnt.dimension();
}
static int compare( int d, const PT & a, const PT & b )
{
if ( a[ d ] < b[ d ] )
return -1;
if ( a[ d ] > b[ d ] )
return 1;
return 0;
}
static void copy_coord( int d, PT & a, const PT & b )
{
a[ d ] = b[ d ];
}
};
template <class PT>
class Kdtree_interface_2d
{
public:
typedef PT Point;
static int dimension( const PT & pnt )
{
// return pnt.dimensions();
return pnt.dimension();
}
static int compare( int d, const PT & a, const PT & b )
{
if ( a[ d ] < b[ d ] )
return -1;
if ( a[ d ] > b[ d ] )
return 1;
return 0;
}
static void copy_coord( int d, PT & a, const PT & b )
{
if ( d == 0 )
a = PT( b[ 0 ], a[1] );
else
if ( d == 1 )
a = PT( a[ 0 ], b[1] );
else {
CGAL_error();
}
}
};
template <class PT>
class Kdtree_interface_3d
{
public:
typedef PT Point;
static int dimension( const PT & pnt )
{
// return pnt.dimensions();
return pnt.dimension();
}
static int compare( int d, const PT & a, const PT & b )
{
if ( a[ d ] < b[ d ] )
return -1;
if ( a[ d ] > b[ d ] )
return 1;
return 0;
}
static void copy_coord( int d, PT & a, const PT & b )
{
if ( d == 0 )
a = PT( b[ 0 ], a[1], a[ 2 ] );
else
if ( d == 1 )
a = PT( a[ 0 ], b[1], a[ 2 ] );
else
if ( d == 2 )
a = PT( a[ 0 ], a[1], b[ 2 ] );
else {
CGAL_error();
}
}
};
/*=========================================================================
* kdtree_d -
* A the kdtree class.
*
* Remark: The kd-trees allocates all the memory it needs in advance,
* This results in a rather efficient memory management, and a fast
* iplementation.
\*=========================================================================*/
template <class Traits>
class Kdtree_d
{
class Plane;
public:
typedef typename Traits::Point Point;
typedef list<Point> List_points;
//-------------------------------------------------------------------------
// Extended_Point_d -
// A class for representing an extended d-dimal point: with
// ability to support +/- infinity. Templated by the kd-treee interface
// type.
//-------------------------------------------------------------------------
class ExtPoint
{
private:
class coordinate_type
{
public:
const Point * p_pnt;
int type;
//signed char type;
};
coordinate_type * p_arr;
int dim;
Point def_pnt;
void init( int _dim )
{
CGAL_precondition( _dim > 0 );
dim = _dim;
p_arr = (coordinate_type *)std::malloc( sizeof( coordinate_type )
* dim );
//printf( "p_arr(new): %p\n", (void *)p_arr );
CGAL_assertion( p_arr != NULL );
std::memset( p_arr, 0, sizeof( coordinate_type ) * dim );
}
public:
enum { MINUS_INFINITY = -1, FINITE = 0, PLUS_INFINITY = 1 };
ExtPoint( int _type, int _dim )
{
CGAL_precondition( _type == MINUS_INFINITY || _type == PLUS_INFINITY );
init( _dim );
for ( int ind = 0; ind < dim; ind++ )
p_arr[ ind ].type = _type;
}
ExtPoint()
{
p_arr = NULL;
dim = -1;
}
ExtPoint( const ExtPoint & p )
{
init( p.dim );
def_pnt = p.def_pnt;
for ( int ind = 0; ind < dim; ind++ ) {
p_arr[ ind ] = p.p_arr[ ind ];
if ( p.p_arr[ ind ].p_pnt == &p.def_pnt )
p_arr[ ind ].p_pnt = &def_pnt;
}
}
ExtPoint & operator=( const ExtPoint & p )
{
term();
init( p.dim );
def_pnt = p.def_pnt;
for ( int ind = 0; ind < dim; ind++ ) {
p_arr[ ind ] = p.p_arr[ ind ];
if ( p.p_arr[ ind ].p_pnt == &p.def_pnt )
p_arr[ ind ].p_pnt = &def_pnt;
}
return *this;
}
ExtPoint( const Point & point, int _dim )
{
init( _dim );
def_pnt = point;
for ( int ind = 0; ind < _dim; ind++ ) {
p_arr[ ind ].p_pnt = &def_pnt;
p_arr[ ind ].type = FINITE;
}
}
void term()
{
if ( p_arr != NULL ) {
//printf( "a: %p\n", p_arr );
std::free( p_arr );
//printf( "_a\n" );
p_arr = NULL;
}
dim = 0;
}
~ExtPoint()
{
term();
}
void set_coord( int k, Point & point )
{
CGAL_precondition( 0 <= k && k < dim );
p_arr[ k ].type = FINITE;
//p_arr[ k ].p_pnt = &point;
Traits::copy_coord( k, def_pnt, point );
p_arr[ k ].p_pnt = &def_pnt;
}
void set_coord( int k, const ExtPoint & point )
{
CGAL_precondition( 0 <= k && k < dim );
CGAL_precondition( 0 <= k && k < point.dim );
p_arr[ k ] = point.p_arr[ k ];
if ( p_arr[ k ].type == FINITE ) {
Traits::copy_coord( k, def_pnt, *(p_arr[ k ].p_pnt) );
p_arr[ k ].p_pnt = &def_pnt;
}
}
int compare( int k, const ExtPoint & point ) const
{
CGAL_precondition( 0 <= k && k < dim );
// the following does not compile on msvc++...
// coordinate_type & a( p_arr[ k ] ),
// & b( point.p_arr[ k ] );
coordinate_type & a = p_arr[ k ];
coordinate_type & b = point.p_arr[ k ];
if ( a.type != FINITE ) {
if ( b.type != FINITE ) {
return a.type - b.type;
} else {
return a.type;
}
} else {
if ( b.type != FINITE )
return -b.type;
else
return Traits::compare( k, *a.p_pnt, *b.p_pnt );
}
}
int compare_vector( const ExtPoint & point ) const
{
int ind, res;
for ( ind = 0; ind < dim; ind++ ) {
res = compare( ind, point );
if ( res != 0 )
return res;
}
return 0;
}
int compare( int k, const Point & point ) const
{
CGAL_precondition( 0 <= k && k < dim );
// coordinate_type & a( p_arr[ k ] );
coordinate_type & a = p_arr[ k ];
if ( a.type != FINITE )
return a.type;
else
return Traits::compare( k, *a.p_pnt, point );
}
int dimension() const
{
return dim;
}
int get_coord_status( int d ) const
{
CGAL_precondition( 0 <= d && d < dim );
return p_arr[ d ].type;
}
const Point * get_coord_point( int d ) const
{
CGAL_precondition( 0 <= d && d < dim );
return p_arr[ d ].p_pnt;
}
};
// Box - represents an axis parallel box.
class Box
{
public:
int dim;
ExtPoint left, right;
private:
friend class Plane;
ExtPoint & get_vertex( bool f_left )
{
return f_left? left : right;
}
public:
// constructors
//CHECK
Box()
{
}
Box( const Box & box )
{
dim = box.dim;
left = box.left;
right = box.right;
}
Box( const Point &l, const Point &r, int _dim )
{
dim = _dim;
left = ExtPoint( l, dim );
right = ExtPoint( r, dim );
}
Box( int _dim ) : left( ExtPoint::MINUS_INFINITY, _dim ),
right( ExtPoint::PLUS_INFINITY, _dim )
{
dim = _dim;
}
// data access
void set_left( Point &l)
{
left = ExtPoint( l, dim );
};
void set_right( Point &r)
{
right = ExtPoint( r, dim );
}
const ExtPoint &get_left() const
{
return left;
}
const ExtPoint &get_right() const
{
return right;
}
void set_coord_left( int k, Point & p )
{
left.set_coord( k, p );
}
void set_coord_right( int k, Point & p )
{
right.set_coord( k, p );
}
// operations
bool is_in( const Box &o) const
// checks if o is completely inside <this>
{
int dim = left.dimension();
for (int i = 0; i < dim; i++)
{
if ( (left.compare(i, o.get_left()) > 0 )
|| (right.compare(i, o.get_right()) < 0 ) )
return false;
}
return true;
}
bool is_in( const Point & o ) const
// checks if o is completely inside <this>
{
int _dim = left.dimension();
for (int i = 0; i < _dim; i++)
{
if ( (left.compare( i, o ) > 0 ) ||
(right.compare( i, o ) <= 0 ) )
return false;
}
return true;
}
bool is_coord_in_range( int k, const Point & o ) const
// checks if o is completely inside <this>
{
return ( ! ( (left.compare( k, o ) > 0 )
|| (right.compare( k, o ) <= 0 ) ) );
}
bool is_intersect( const Box &o) const
// checks if there is an intersection between o and this
{
int dim = left.dimension();
for (int i = 0; i < dim; i++)
{
if ( (left.compare(i, o.get_right()) >= 0) ||
(right.compare(i, o.get_left()) <= 0) )
return false;
}
return true;
}
// checks if there is an intersection between o and this box
// only in a specific coordinate...
bool is_intersect_in_dim( int d, const Box & o ) const
{
return (! ( (left.compare( d, o.get_right() ) >= 0 )
|| (right.compare( d, o.get_left() ) <= 0) ));
}
// checks if there is an intersection between o and this box
// only in a specific coordinate...
bool is_intersect_in_dim_closed( int d, const Box & o ) const
{
return (! ( (left.compare( d, o.get_right() ) > 0 )
|| (right.compare( d, o.get_left() ) < 0) ));
}
bool intersect(Box &o)
// intersects this with o. the intersection will be in this
// returns false if intersection is empty
{
int dim = left.dimension();
for (int i = 0; i < dim; i++)
{
// left is the maximal of the lefts
if (left.compare(i, o.get_left()) == -1)
left.set_coord(i, o.get_left());
// right is the minimal of the rights
if (right.compare(i, o.get_right()) == 1)
right.set_coord(i, o.get_right());
}
return !(is_empty());
}
bool is_empty() const
// return true if this is not an interval (left[k] > right[k])
{
int dim = left.dimension();
for (int i = 0; i < dim; i++)
{
if (left.compare(i, right) == 1)
return true;
}
return false;
}
bool is_empty_open() const
// return true if this is not an interval (left[k] > right[k])
{
int dim = left.dimension();
for (int i = 0; i < dim; i++)
{
if ( left.compare(i, right) >= 0 )
return true;
}
return false;
}
int comp( const Box & o ) const
{
int res;
res = left.compare_vector( o.left );
if ( res != 0 )
return res;
return right.compare_vector( o.right );
}
// destructor - needed in ...recursive ... DVP
~Box()
{
left.term();
right.term();
dim = 0;
}
};
private:
class Plane
{
private:
int coord;
Point * normal;
bool f_plus; // orientation of half space
// is (0, 0, ... , +inifinity, 0, ..., 0) inside plane
public:
Plane()
{
normal = NULL;
coord = 0;
}
Plane( int k, Point & p )
{
normal = &p;
coord = k;
}
Plane( const Plane & p )
{
coord = p.coord;
normal = p.normal;
}
void dump( void )
{
std::cout << "(" << coord << ": " << *normal << ")";
}
bool is_in( const Point & p ) const
{
int cmp;
cmp = Traits::compare( coord, p, *normal );
if ( ! f_plus )
cmp = -cmp;
return cmp >= 0;
}
void set_plane(int k, Point &p)
{
coord = k;
normal = &p;
//normal->copy( coord, p );
}
void split( Box & region, bool f_neg )
{
ExtPoint * p_p = &(region.get_vertex( ! f_neg ));
if ( f_neg ) {
if ( p_p->compare( coord, *normal ) > 0 )
p_p->set_coord( coord, *normal );
} else
if ( p_p->compare( coord, *normal ) < 0 )
p_p->set_coord( coord, *normal );
}
void orient_half_space( bool f_neg_side )
{
f_plus = ! f_neg_side;
}
int get_coord() const
{
return coord;
}
};
private:
class Node
{
public:
Plane plane;
Point * pnt;
Node * left, * right;
enum { LEFT, RIGHT };
const Plane & get_hs( int side ) const
{
((Plane *)&plane)->orient_half_space( side == LEFT );
return plane;
}
bool is_points_in_hs( const Plane & pl ) const
{
if ( is_point() )
return pl.is_in( *pnt );
if ( left != NULL && ( ! left->is_points_in_hs( pl ) ) )
return false;
if ( right != NULL && ( ! right->is_points_in_hs( pl ) ) )
return false;
return true;
}
bool is_valid() const
{
if ( is_point() )
return true;
if ( left != NULL )
if ( ! left->is_points_in_hs( get_hs( LEFT ) ) )
return false;
if ( right != NULL )
if ( ! right->is_points_in_hs( get_hs( RIGHT ) ) )
return false;
return true;
}
void dump( int depth )
{
int ind;
for ( ind = 0; ind < depth; ind++ )
std::cout << " ";
if ( is_point() ) {
std::cout << *pnt << "\n";
return;
}
plane.dump();
std::cout << "\n";
left->dump( depth + 1 );
for ( ind = 0; ind < depth; ind++ )
std::cout << " ";
std::cout << "!!!!!!!!!!!!\n";
right->dump( depth + 1 );
}
bool is_point() const
{
return ((left == NULL) && (right == NULL));
}
typedef std::back_insert_iterator<List_points> back_iter;
Node() : plane()
{
left = right = NULL;
}
void copy_subtree_points( back_iter & result,
const Box & rect )
{
if ( is_point() ) {
if ( rect.is_in( *pnt ) )
(*result++) = *pnt;
return;
}
if ( left != NULL )
left->copy_subtree_points( result, rect );
if ( right != NULL )
right->copy_subtree_points( result, rect );
}
static void search_recursive( back_iter & result,
Node * node,
const Box & rect,
Box & _region,
Plane & plane,
bool f_split_plus )
{
//printf( "search_recusrive\n" );
Box * p_r = new Box( _region );
//printf( "z" );
//fflush( stdout );
plane.split( *p_r, f_split_plus );
//printf( "c" );
//fflush( stdout );
CGAL_precondition( node != NULL );
//printf( "b" );
//fflush( stdout );
if ( rect.is_in( *p_r ) )
{
//printf( "5" );
//fflush( stdout );
node->copy_subtree_points( result, rect );
//printf( "\tsearch_recursive done...\n" );
//printf( "6" );
//fflush( stdout );
delete p_r;
return;
}
//printf( "v" );
//fflush( stdout );
if ( rect.is_intersect_in_dim_closed( plane.get_coord(), *p_r ) )
node->search( result, rect, *p_r );
//printf( "x" );
//fflush( stdout );
delete p_r;
}
void search( std::back_insert_iterator<List_points> result,
const Box &rect, Box ®ion )
{
if (is_point()) {
if ( rect.is_in( *pnt ) )
(*result++) = *pnt;
return;
}
//this is not a point so it is a hypeplane
if ( left != NULL )
search_recursive( result, left, rect,
region, plane, true );
if ( right != NULL )
search_recursive( result, right, rect,
region, plane, false );
}
};
typedef Point * Point_ptr;
int size;
Point * p_arr_pt;
Node *root;
int dim;
Node * p_node_arr;
int node_count;
Node * malloc_node( void )
{
Node * p_ret;
p_ret = &(p_node_arr[ node_count ]);
node_count++;
CGAL_assertion( node_count <= ( 2 * size ));
*p_ret = Node();
return p_ret;
}
static int comp( const Point & a, const Point & b, int dim )
{
return Traits::compare( dim, a, b );
}
static int partition( Point_ptr * arr, int left, int right,
Point * p_pivot, int dim )
{
int i, j;
Point_ptr tmp;
if ( left >= right )
return left;
i = left;
j = right;
while ( i < j ) {
if ( comp( *(arr[ i ]), *(arr[ j ]), dim ) > 0 ) {
tmp = arr[ i ];
arr[ i ] = arr[ j ];
arr[ j ] = tmp;
}
if ( comp( *(arr[ i ]), *p_pivot, dim ) < 0 ) {
i++;
} else
if ( comp( *p_pivot, *(arr[ j ]), dim ) <= 0 )
j--;
}
return (i > left)? i - 1 : left;
}
/* split the array into two sub-arrays, such that all the elements
* from left to pos_mid are smaller than the elements from pos+1 to
* right.
*/
static void split_arr( Point_ptr * arr,
int left,
int right,
int pos_mid,
int dim )
{
int pos;
if ( left >= right )
return;
pos = partition( arr, left, right, arr[ (left + right ) / 2 ],
dim );
if ( pos == pos_mid )
return;
if ( pos < pos_mid )
split_arr( arr, pos+1, right, pos_mid, dim );
else
split_arr( arr, left, pos, pos_mid, dim );
}
static Point_ptr get_max_element( Point_ptr * arr,
int left, int right, int d )
{
int max_pos = left;
Point mx = *(arr[ max_pos ]);
for ( int ind = left + 1; ind <= right; ind++ )
if ( comp( mx, *(arr[ ind ]), d ) < 0 ) {
mx = *(arr[ ind ]);
max_pos = ind;
}
return arr[ max_pos ];
}
Node *build_r( Point_ptr * arr, int left, int right,
int d )
{
int num, pos, next_d;
Node * n;
num = right - left + 1;
if ( num < 1)
return NULL;
// if the list contains only one point,
// construct a leaf for this node
if ( num == 1) {
//n = new node;
n = malloc_node();
n->pnt = arr[ left ];
return n;
}
// else divide space into two regions in
// dim-dim and cotinue recursively
pos = (left + right) / 2;
split_arr( arr, left, right, pos, d );
Point * p_median = get_max_element( arr, left, pos, d );
// create division plane;
Plane plane( d, *p_median );
//n = new node;
// CGAL_assertion( n != NULL );
n = malloc_node();
n->plane = plane;
next_d = d + 1;
if ( next_d >= dim )
next_d = 0;
// build left sub-tree
n->left = build_r( arr, left, pos, next_d );
// build right sub-tree
n->right = build_r( arr, pos + 1, right, next_d );
return n;
}
public:
typedef list<Point> list_points;
Kdtree_d(int k = 2)
{
dim = k;
root = NULL;
p_arr_pt = NULL;
p_node_arr = NULL;
}
~Kdtree_d()
{
delete_all();
}
bool is_valid( bool verbose = false, int level = 0 ) const
{
(void)verbose;
(void)level;
if ( root == NULL )
return true;
return root->is_valid();
}
void dump()
{
root->dump( 0);
}
void delete_all()
{
root = NULL;
if ( p_arr_pt != NULL )
delete[] p_arr_pt;
p_arr_pt = NULL;
if ( p_node_arr != NULL )
delete[] p_node_arr;
p_node_arr = NULL;
}
void search( std::back_insert_iterator<list_points> result,
Box & rect )
{
if (root == NULL)
return; // it is an empty tree - nothing to search in
Box region = Box( dim );
root->search( result, rect, region );
}
void build(list<Point> &l)
{
int i;
Point_ptr * p_arr;
size = l.size();
p_arr_pt = new Point[ size ];
CGAL_assertion( p_arr_pt != NULL );
p_arr = new Point_ptr[ size ];
CGAL_assertion( p_arr != NULL );
p_node_arr = new Node[ 2 * size ];
CGAL_assertion( p_node_arr != NULL );
node_count = 0;
/* fill the array */
i = 0;
for ( typename std::list<Point>::iterator j = l.begin();
j != l.end();
j++ )
{
p_arr_pt[ i ] = (*j);
p_arr[ i ] = p_arr_pt + i;
i++;
}
// recursively build the tree from the sorted list
// starting to divide it in coordinate 0
root = build_r( p_arr, 0, size - 1, 0 );
//printf( "b\n" );
delete[] p_arr;
}
};
} //namespace CGAL
#endif /* CGAL_KDTREE_D_H */
|