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 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582
|
<html>
<head>
<title>
NearTree -- function library efficiently solving the Nearest Neighbor Problem
</title>
</head>
<body>
<font face="Arial,Helvetica,Times" size="3">
<table border=2>
<tr>
<td valign="center" width="88" align="center" height="35" bgcolor="cyan">
<font face="Arial,Helvetica,Times" size="2">
<a href="http://sf.net/projects/cvector" style="text-decoration:none">CVector</a>
</font>
</td>
<td valign="center" width="88" align="center" height="35">
<a href="http://sourceforge.net/projects/neartree"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=237121&type=10" width="80" height="15" alt="Get NearTree at SourceForge.net. Fast, secure and Free Open Source software downloads" /></a>
</td>
</tr>
</table>
<hr>
<center>
<h2 align=center>NearTree</h2>
Release 5.1.1<br />
<font size="-1">
24 April 2016<br />
© Copyright 2001, 2008, 2009, 2010, 2011, 2014, 2016 Larry Andrews.<br />
All rights reserved <br />
based on<br />
Lawrence C. Andrews, Herbert J. Bernstein, "<i>NearTree, a data structure <br />
and a software toolkit for the nearest-neighbor problem</i>",<br />
J. Appl. Crystallogr., Volume 49, Part 3 (June 2016),<br />
doi: 10.1107/S1600576716004350, ISSN:1600-5767,<br />
<p>
Larry Andrews, "<i><a href="Andrews_2001_TNEAR.pdf">A template for the nearest neighbor problem</a></i>",<br />
C/C++ Users Journal, Volume 19, Issue 11 (November 2001), 40 - 49 (2001),<br />
ISSN:1075-2838,<br />
<a href="http://www.ddj.com/architect/184401449" style="text-decoration:none">www.ddj.com/architect/184401449</a>
<p>
Revised 12 Dec 2008, for sourceforge release, Larry Andrews and Herbert J. Bernstein<br />
8 Jan 2009 Release 1.0 LCA and HJB<br />
11 Jan 2009 Release 1.0.1 LCA and HJB<br />
21 March 2009 Release 2.0 LCA and HJB<br />
30 May 2009 Release 2.1 LCA and HJB<br />
4 June 2009 Release 2.1.1 LCA and HJB<br />
7 June 2009 Release 2.1.2 LCA and HJB<br />
7 July 2009 Release 2.1.3 LCA and HJB<br />
29 November 2009 Release 2.1.4 LCA<br />
23 April 2010 Release 2.1.5 LCA and HJB<br />
18 July 2010 Release 2.2 HJB<br />
25 July 2010 Release 2.2.1 HJB<br />
31 August 2010 Release 2.3 LCA<br />
7 September 2010 Release 2.3.1 LCA<br />
30 October 2010 Release 2.3.2 LCA<br />
22 March 2011 Release 3.0 LCA and HJB<br />
5 April 2011 Release 3.0.1 LCA and HJB<br />
19 April 2011 Release 3.0.2 HJB<br />
23 April 2011 Release 3.1 HJB<br />
27 September 2011 Release 3.1.1 HJB<br />
18 April 2014 Release 4.0 HJB<br />
23 April 2016 Release 5.0 HJB<br />
25 April 2016 Release 5.1 LCA<br />
30 April 2016 Release 5.1.1 LCA and HJB<br />
</font>
<P>
<h4>YOU MAY REDISTRIBUTE NearTree UNDER THE TERMS OF THE <a href=lgpl.txt style="text-decoration:none">LGPL</a></h4>
<p>
</center>
<center>
<table border=1>
<tr>
<td><font face="Arial,Helvetica,Times" size="2">
<h4 align=center>LGPL NOTICES</h4>
<p>
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.
<p>
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.
<p>
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
</td>
</table>
</center>
<P>
This is a release of an API for finding nearest neighbors among points in spaces of
arbitrary dimensions. This release provides a C++ template, TNear.h, and a
C library, CNearTree.c, with example/test programs.
<font size="-1">
<p>Release 5.1.1 added a pdf of the 2001 Dr. Dobbs article, added with permission of
the editor.
<p>Release 5.1 augmented the Lloyd alogorithm code to allow for an arbitrary number
of cluster points. The Makefile and README have been updated.
<p>Release 5.0 has cummulative changes from 2014, 2015, 2016 (svn revisions 153 - 159)
including fixing a bug in the Lloyd cluster test and revising the KNN code to
do an annular search. A checkpoint capability and a recovery constructor have
been added to TNear.h.
<p>Release 4.0 cummulative changes from 2011, 2013, 2014, including
flattening the tree for the C++ template by moving collisions into a
separate vector, optional support for armadillo, and new code to facilitate
dumping and neartree from the C++ template. Miscellaneous bug fixes were
also applied. Thanks to Michael Tautschnig for a duplicate library load fix.
<p>Release 3.1.1 adjusted the libtool version from 5:0:1 to 6:0:1 to avoid
confusion on the SONAME of the library as requested by Teemu Ikonen for use as
a Debian package.
<p>Release 3.1 adjusted the randomization to be based on the depth rather
than the population and added an optional detailed height calculation.
<p>Release 3.0.2 added to randomization on insertion when the tree is not well balanced.
<p>Release 3.0.1 updated the diameter calculation and fixed some documentation errors.
<p>Release 3.0 (formerly named Release 2.4) is a major change to NearTree, restructuring
the default search from left-first to balanced and adding hooks to collect information
about the tree.
<p>Release 2.3.2 adds optional returns of vectors of ordinals of found objects
<p>Release 2.3.1 adds Centroid method for Lloyd clustering.
<p>Release 2.3 added methods for clustering.
<p>Release 2.2.1 was a minor revision to Release 2.2 to add an include of limits.h to TNear.h,
primarily for MINGW use.
<p>Release 2.2 added support for C code for fixed length string searches using a hamming distance norm,
and for spherical and hemispherical geodesic norm based searches. Because of the
addition of new type and norm flags, the version 2.2 shared libraries cannot be used
to support binaries compiled against earlier headers and vice-versa.
<p>Release 2.1.5 was a cleanup update to the 2.1 release of 30 May 2009 to increase
portability, in five stages (2.1.1 on 4 June 2009, 2.1.2 on 7 June 2009, 2.1.3 on 7 July 2009,
2.1.4 on 29 November 2009 and 2.1.5 on 23 April 2010)
dealing with the following issues:
<ul>
<li>Convert to use of a self-contained portable random-number generator from Rob Harrison (2.1.1)
<li>Ensure wider use of const where appropriate (2.1.1)
<li>Correct typos and unclear wording in the README (2.1.2)
<li>Reorganize use of USE_LOCAL_HEADERS in CNearTreeTest.cpp (2.1.2)
<li>Change FAR macro to CNEARTREE_FAR (2.1.3)
<li>Add BelongsToPoints and SeparateByRadius (2.1.4)
<li>Fix dimensions for rhrand (2.1.5)
</ul>
<p>The 2.1 release was a minor update to the 2.0 release of 21 March 2009 to deal with the
following issues:
<ul>
<li>Make delayed insertion the default
<li>Complete the containerization of TNear.h
<li>Add code for K-nearest/farthest in TNear.h and in CNearTree.c
<li>Correct the InAnnulus search filter
</ul>
<P>Release 2.0 was a major update to the 1.0 release of 8 January 2009 to deal with the
following issues:
<ul>
<li>Replace use recursion with a stack, except in insertion logic
<li>Replace use of double with templated DistanceType (usually double)
<li>Provide constuctors to build NearTree from vectors, lists or sets
<li>Change "Insert" to "insert" for consistency with
other containers
<li>Add access function "at" or array type references [], and provide
contents of a neartree as a vector
<li>Add iterator support
<li>Provide delayed insertion logic
<li>Functions added for searches outside of a sphere or in an annular region
</ul>
</font>
<P>
Our thanks to Nicolas Brodu for suggesting the more general handling of the
distance type.
<p>
<b>Note:</b> As Nicolas Brodu has noted, CNearTree is particularly well-suited
to multi-threaded applications. However, if the same CNearTree is to be searched
in multiple threads, it is important to complete all insertions and/or delayed
insertions before parallel execution of parallel searches.
<hr>
<h4>Contents</h4>
<P>
<ul>
<li><a href="#Installation">Installation</a>
<li><a href="#Tnear.h">The C++ template: TNear.h</a>
<li><a href="#CNearTree.c">The C NearTree API: CNearTree.c</a>
<li><a href="#rhrand.h">A Portable pseudo-random number generator: rhrand.h</a>
</ul>
<hr>
<hr>
<h4><a name="Installation">Installation</a></h4>
<p>
The NearTree package is available at <a href="http://www.sourceforge.net/projects/neartree">www.sourceforge.net/projects/neartree</a>.
A source tarball is available at <a
href="http://downloads.sourceforge.net/neartree/NearTree-5.1.1.tar.gz">downloads.sourceforge.net/neartree/NearTree-5.1.1.tar.gz</a>.
Later tarballs may be available.
<p>
If you decide to simply use the TNear.h header to add nearest neighbor support to C++ code under Visual
Studio, be sure to also use the rhrand.h and triple.h headers. It is no longer necessary to define
USE_LOCAL_HEADERS, which is automatically defined if _MSC_VER is defined. For
unix or MINGW, you will need to use the Makefile and to have libtool on your system. Be warned that
the default libtool under Mac OS X will not work for this installation.
<P>
When the source tarball is downloaded and unpacked, you should have a directory NearTree-5.1.1. To
see the current settings for a build
execute
<p>
make
</p>
which should give the following information:
<p>
<pre><tt>
PLEASE READ README_NearTree.txt and lgpl.txt
Before making the NearTree libraries and example programs, check
that the chosen settings are correct
The current C++ and C compile commands are:
libtool --mode=compile g++ -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -I. -c
libtool --mode=compile gcc -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -I. -c
The C API, CNearTree.c, depends on the sourceforge project CVector
You are currently setup to use the system defaults for CVector
If that is not correct, define the variable CVECTOR_INCLUDE
The current library link command is:
libtool --mode=link gcc -version-info 7:0:0 \
-no-undefined -rpath /usr/local/lib
The current C++ and C library local, and C dynamic and static build commands are:
libtool --mode=link g++ -no-undefined -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -I.
libtool --mode=link gcc -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -I.
libtool --mode=link gcc -no-undefined -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -shared -I/usr/local/include
libtool --mode=link gcc -g -O2 -Wall -ansi -pedantic \
-DCNEARTREE_SAFE_TRIANG=1 -static-libtool-libs -I/usr/local/include
Before installing the NearTree library and example programs, check
that the install directory and install commands are correct:
The current values are :
/usr/local
libtool --mode=install cp
To compile the NearTree library and example programs type:
make clean
make all
To run a set of tests type:
make tests
To clean up the directories type:
make clean
To install the library and headers type:
make install
</tt>
</pre>
<p>
If these settings need to be changed, edit Makefile. On some systems, e.g. Mac OS X, the default
libtool is not appropriate. In that case you should install a recent version of libtool. The
CVector kit has been tested with libtool versions 1.3.5 and 1.5.4. For MINGW, libtool version 2.2.6
and gcc version 4 are needed to work with shared libraries (DLLs). If the system libtool is
not to be used, define the variable LIBTOOL to be the path to the libtool executable, e.g.
in bash
<p>
export LIBTOOL=$HOME/bin/libtool
<p>
or in the Makefile
<p>
LIBTOOL = $(HOME)/bin/libtool
<p>
If you need to include local header files using #include "..." instead of #include <...>,
define the variable USE_LOCAL_HEADERS. USE_LOCAL_HEADERS is the default for Visual
Studio under Microsoft Windows.
<p>
Optionally, you may also define CNEARTREE_FORCEFLIP to maximize
tree reorganization on insertion, CNEARTREE_NOFLIP to suppress
tree reorganization on insertion, CNEARTREE_NODEFER to make
all insertions immediate, CNEARTREE_FORCEPREPUNE to do searches
first with a tighter estimate on the search radius, and CNEARTREE_NOPREPRUNE
to suppress that behavior. The defaults are to do tree reorganization
on insertion, to defer insertions, but not to preprune the search radius.
<p>
If you define CNEARTREE_INSTRUMENTED, code will be enabled to track
node visits in searching the tree.
<p>
If you define USE_ARMADILLO_LIBRARY, armadillo will be included without
BLAS and without LAPACK, and a DistanceBetween for arma::vec6 vectors
will be defined.
<p>
The triangle inequality that must be evaluated in building trees and
retrieving data may not be evaluated correctly if the range of the three values
is extremely large (>10**15 or so for doubles) or may be evaluated differently
by some compilers in different parts of a program (due to differing usage of registers).
The default in this API is to do the triangle inequality three different ways
under the control of CNEARTREE_SAFE_TRIANG
<p>
<pre><tt>
#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
</tt></pre>
<p>
Problems with the unsafe definition of TRIANG have been seen in Linux under gcc version 4
and in MS Window under VS 2003. There is a slight performance hit from the triple test.
If maximal speed is critical and misidentification of nearest points by relative distance
errors of about 1 part in 10**15 is not a serious problem, the definition of
-DCNEARTREE_SAFE_TRIANG=1 can be removed from the definition of CFLAGS in the Makefile.
<p>
NOTE: A range of 10**15 is comparable to the diameter of the earth
vs. the separation of two bonded atoms.
<p>
As of version 5.0, the default for processing K-nearest-neighbor request is to search in
nested annuli. For KNN searches on high dimension spaces or spaces with expensive
distance calculations, this is the best choice. However, flags are provided to revert
to the version 4.0 spherical KNN search if desired. See NTF_SphericalKNN in TNear.h
and CNTF_SKNN in CNearTree.h.
<p>
If you define CNEARTREE_DIMSAMPLES with a integer value, that value will be used as the
count of the number of radii to try to estimate the Hausdorff dimension in doing
K-nearest-neighbor processinf. Values as low as 2 can
be used. The default is 4. The higher the value, the more accurate the
dimension estimate. The Makefile includes tests of 2, 4, 6 and 8 samples.
<hr>
<hr>
<h4><a name="Tnear.h">The C++ template: TNear.h</a></h4>
<P>
This is a revised release of
<blockquote>
template <typename T, typename DistanceType=double, int distMinValue=-1 > class CNearTree;
</blockquote>
<P>
implementing the Nearest Neighbor algorithm after Kalantari and McDonald,
(IEEE Transactions on Software Engineering, v. SE-9, pp. 631-634,1983)
modified to use recursion for insertions and recursion (original version)
or a stack (current version) for searches
instead of a double-linked tree
and simplified. The default search algorithm no longer favors the left
branch first, but follows the more balanced Kalantari and McDonald
approach. The prior search algorithm is available in "Left"
versions of the search routines doing a bit less checking for
things like is the distance to the right less than the
distance to the left.
<p>
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.
<p>
CNearTree is the root class for the neartree. The actual data of the
tree is stored in NearTreeNode objects descending from a CNearTree.
<p>
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 objects of varying storage, then your best way to use this
code is to store pointers or handles and to write your own distance functions.
Note that std::string is a pointer type variable and so can be stored directly.
<p>
The type of the objects to be stored is the only <b>required</b> template argument.
The type of the distance measure (DistanceType) defaults to double. If your
application 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.
<p>
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.
<p>
It is a design decision that this class cannot work for unsigned types.
Verifying the triangle rule for unsigned types is more complex. Sorry,
unsigned types are left as an exercise for the reader.
<p>
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 by the system.
<p>
<center>
<table>
<tr>
<td><font face="Arial,Helvetica,Times" size="3" colspan=2>
DistanceType Norm( void );</font></td></tr>
<tr><td> </td>
<td><font face="Arial,Helvetica,Times" size="3">// a function "Norm( void )" of the templated class<br />
// to return DistanceType (usually will return a<br />
// "length" of type double)
</font></td>
</tr>
<tr>
<td><font face="Arial,Helvetica,Times" size="3">
operator- ( ); </font></td><td><font face="Arial,Helvetica,Times" size="3">// geometrical (vector) difference of two objects
</font></td>
</tr>
<tr>
<td></td><td><font face="Arial,Helvetica,Times" size="3">// a copy constructor would be nice
</font></td>
</tr>
<tr>
<td></td><td><font face="Arial,Helvetica,Times" size="3">// a constructor would be nice
</font></td>
</tr>
<tr>
<td></td><td><font face="Arial,Helvetica,Times" size="3">// a destructor would be nice
</font></td>
</tr>
</table>
</center>
<p>
The provided interface is:
<p>
<pre><tt>
<b>#include <TNear.h></b>
// Constructors
<b>CNearTree</b>( <b>void</b> ); // constructor
// nstantiated by something like: CNearTree <T> vTree;
// for some type T
<b>CNearTree</b>( <b>const ContainerType<T></b> & <i>o</i>); // constructor from containers, e.g. ...
<b>CNearTree</b>( <b>const std::vector<T></b> & <i>o</i> ); // constructor
<b>CNearTree</b>( <b>const std::list<T></b> & <i>o</i> ); // constructor
<b>CNearTree</b>( <b>const std::set<T></b> & <i>o</i> ); // constructor
<b>CNearTree</b>( <b>const CNearTree<T></b> & <i>o</i> ); // constructor
<b>CNearTree</b>( <b>const ContainerType<T></b> & <i>o1</i>, <b>const ContainerType<T></b> & <i>o2</i> );
// constructor merging 2 containers, The
// containers can be standard library containers or CNearTrees.
// *** The next constructor and the following Get_Checkpoint function are a matched
// pair. The constructor will recreate a NearTree save by Get_Checkpoint, even
// if the objects have been saved to a file and restored. **
<b>CNearTree</b>( <b>const std::vector<long></b> & <i>DelayedIndices</i>,
// objects queued for insertion, possibly in random order
<b>const std::vector<T></b> & <i>ObjectStore</i>,
// all inserted objects go here
<b>const std::vector<size_t></b> & <i>ObjectCollide</i>,
// overflow chain of colliding objects
<b>const size_t</b> <i>DeepestDepth</i>, // maximum depth of the tree
<b>const std::vector< NearTreeNode<T, DistanceType, distMinValue> * ></b> & <i>NearTreeNodes<i>,
// vector of pointers to nodes to build the tree
<b>const NearTreeNode<T, DistanceType, distMinValue></b> <i>BaseNode</i>,
// the tree's data is stored down
// this node in m_NearTreeNodes
<b>const long</b> <i>Flags</i>, // flags for operational control (mainly for testing)
<b>const DistanceType</b> <i>DiamEstimate</i>, // estimated diameter
<b>const DistanceType</b> <i>SumSpacings</i>, // sum of spacings at time of insertion
<b>const DistanceType</b> <i>SumSpacingsSq</i>,// sum of squares of spacings at time of insertion
<b>const double</b> <i>DimEstimate</i>, // estimated dimension
<b>const double</b> <i>DimEstimateEsd</i> // estimated dimension estimated standard deviation
#ifdef CNEARTREE_INSTRUMENTED
, <b>const size_t</b> <i>NodeVisits</i> // number of node visits
#endif
) // constructor
// Checkpoint Getter
<b>void Get_Checkpoint</b> ( <b>std::vector<long></b> * * <i>DelayedIndices</i>,
// objects queued for insertion, possibly in random order
<b>std::vector<T></b> * * <i>ObjectStore</i>, // all inserted objects go here
<b>std::vector<size_t></b> * * <i>ObjectCollide</i>,
// overflow chain of colliding objects
<b>size_t</b> * <i>DeepestDepth</i>, // maximum depth of the tree
<b>std::vector< NearTreeNode<T, DistanceType, distMinValue> * ></b> * * <i>NearTreeNodes</i>,
// vector of pointers to nodes to build the tree
<b>NearTreeNode<T, DistanceType, distMinValue></b> * * <i>BaseNode</i>,
// the tree's data is stored down
// this node in m_NearTreeNodes
<b>long</b> * <i>Flags</i>, // flags for operational control (mainly for testing)
<b>DistanceType</b> * <i>DiamEstimate</i>, // estimated diameter
<b>DistanceType</b> * <i>SumSpacings</i>, // sum of spacings at time of insertion
<b>DistanceType</b> * <i>SumSpacingsSq</i>, // sum of squares of spacings at time of insertion
<b>double</b> * <i>DimEstimate</i>, // estimated dimension
<b>double</b> * <i>DimEstimateEsd</i> // estimated dimension estimated standard deviation
#ifdef CNEARTREE_INSTRUMENTED
, <b>size_t</b> * <i>NodeVisits</i> // number of node visits
#endif
) // checkpoint getter
// Destructor
<b>~CNearTree</b>( <b>void</b> ); // destructor
<b>void clear</b>( <b>void</b> ); // clear the NearTree
// Flag Management
<b>long GetFlags</b>( <b>void</b> ) <b>const</b>; // Get all execution flags
<b>void SetFlags</b>( <b>const long</b> <i>flags</i> ); // Set all execution flags
<b>long GetFlags</b>( <b>const long</b> <i>mask</i> ) <b>const</b>; // Get execution flags within mask
<b>void SetFlags</b>( <b>const long</b> <i>flags</i>, <b>const long</b> <i>mask</i> );
// Set execution flags within mask
// The available execution flags are
<b>static const long</b> NTF_NoPrePrune = 1; //flag to suppress all search prepruning
<b>static const long</b> NTF_ForcePrePrune = 2; //flag to force search prepruning
<b>static const long</b> NTF_NoFlip = 4; //flag to suppress flips on insert
<b>static const long</b> NTF_ForceFlip = 8; //flag to force flips on insert
<b>static const long</b> NTF_NoDefer =16; //flag to prevent deferred insert
<b>static const long</b> NTF_AnnularKNN =32; //flag to do KNN in annular pieces
<b>static const long</b> NTF_SphericalKNN =64; //flag to do KNN as complete spheres
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator=</b>( <b>const InputContainer&</b> <i>o</i> );
// put container's contents into a NearTree,
// wiping out the current contents
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator=</b>( <b>InputContainer&</b> <i>o</i> );
// put container's contents into a NearTree,
// wiping out the current contents
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator+=</b>( <b>const InputContainer&</b> <i>o</i> );
// add a container's contents to a NearTree
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator+=</b>( <b>InputContainer&</b> <i>o</i> );
// add a container's contents to a NearTree
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator-=</b>( <b>const InputContainer&</b> <i>o</i> );
// remove a container's contents from a NearTree
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& operator-=</b>( <b>InputContainer&</b> <i>o</i> );
// remove a container's contents from a NearTree
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& set_symmetric_difference</b>( <b>const InputContainer&</b>, <i>o</i> );
// remove the part of a container's
// contents from a NearTree that is
// already in the Neartree and add
// in the contents of the container
// that is not already in the Neartree
// i.e. the exclusive or
<b>template<typename</b> <i>InputContainer</i>>
<b>CNearTree& set_symmetric_difference</b>( <b>InputContainer&</b>, <i>o</i> );
// remove the part of a container's
// contents from a NearTree that is
// already in the Neartree and add
// in the contents of the container
// that is not already in the Neartree
// i.e. the exclusive or
<b>void clear</b> ( void ); // removes all content from a tree
<b>void insert</b>( <b>const T&</b> <i>t</i> );
where t is an object of the type T
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.
Starting with the 2.1 release, places objects in a queue for
insertion later when CompleteDelayInsert is called. In
earlier releases the default was immediate insertion.
The following additional convenience insert template
allow insertion of containers of objects
<b>template< typename</b> <i>InputContainer</i> >
<b>void insert</b>( <b>ContainerType</b> & <i>o</i> ); // e. g. ...
<b>void insert</b>( <b>const std::vector<T></b> & <i>o</i> );
<b>void insert</b>( <b>const std::list<T></b> & <i>o</i> );
<b>void insert</b>( <b>const std::set<T></b> & <i>o</i> );
<b>void insert</b>( <b>const CNearTree<T></b> & <i>o</i> );
<b>iterator NearestNeighbor</b> ( <b>const DistanceType &</b> <i>dRadius</i>, <b>const T&</b> <i>t</i> ) <b>const</b>;
returns an iterator to the nearest point to the probe point <i>t</i> or end() if there is none
<b>bool NearestNeighbor</b> ( <b>const DistanceType&</b> <i>dRadius</i>, <b>T&</b> <i>tClosest</i>, <b>const T&</b> <i>t</i> ) <b>const</b>
<i>dRadius</i> is the largest radius within which to search; make it
very large if you want to include every point that was loaded.
<i>tClosest</i> is returned as the object that was found closest to the probe
point (if any were within radius dRadius of the probe)
<i>t</i> 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.
If false is returned, tClosest is invalid (at best).
<b>iterator FarthestNeighbor</b> ( <b>T&</b> <b>const T&</b> <i>t</i> ) <b>const</b>;
returns an iterator to the nearest point to the probe point <i>t</i> or end() if there is none
<b>bool FarthestNeighbor</b> ( <b>T&</b> <i>tFarthest</i>, <b>const T&</b> <i>t</i> ) <b>const</b>;
<i>tFarthest</i> is returned as the object that was found farthest from the probe
point
<i>t</i> 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
If false is returned, tFarthest is invalid (at best).
<b>iterator LeftNearestNeighbor</b> ( <b>const DistanceType &</b> <i>dRadius</i>, <b>const T&</b> <i>t</i> ) <b>const</b>;
returns an iterator to the nearest point to the probe point <i>t</i> or end() if there is none
<b>bool LeftNearestNeighbor</b> ( <b>const DistanceType&</b> <i>dRadius</i>, <b>T&</b> <i>tClosest</i>, <b>const T&</b> <i>t</i> ) <b>const</b>;
<i>dRadius</i> is the largest radius within which to search; make it
very large if you want to include every point that was loaded.
<i>tClosest</i> is returned as the object that was found closest to the probe
point (if any were within radius dRadius of the probe)
<i>t</i> 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.
If false is returned, tClosest is invalid (at best).
<b>iterator LeftFarthestNeighbor</b> ( <b>T&</b> <b>const T&</b> <i>t</i> ) <b>const</b>;
returns an iterator to the nearest point to the probe point <i>t</i> or end() if there is none
<b>bool LeftFarthestNeighbor</b> ( <b>T&</b> <i>tFarthest</i>, <b>const T&</b> <i>t</i> ) <b>const</b>;
<i>tFarthest</i> is returned as the object that was found farthest from the probe
point
<i>t</i> 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
If false is returned, tFarthest is invalid (at best).
The "Left..." versions of NearestNeighbor and FarthestNeighbor are deprecated versions
provided for compatibility with earlier releases of NearTree. There are also "Short..."
and "LeftShort..." versions of NearestNeighbor to support experimental prepruning logic.
The following functions (BelongsToPoints, SeparateByRadius, FindInSphere, FindOutSphere,
and FindInAnnulus) all return a container (ContainerType) that can be any standard library
container (such as std::vector< T >) or CNearTree.
If the input is a container of points t1: The NearTree is examined. For each
point input in the input container a new container of the same type is output
in the vector of containers that will be returned. If N points are input, then N
containers will be output. The points of the neartree will be examined. Copies
of the neartree points are put into the output container (in the output vector)
that corresponds to the input point that it is nearest to. If a point in the
NearTree is equidistant to more than one point in t1, then it is assigned to
the first point in the container at that distance
If the input is two points t1 and t2, then the corresponding Neartee point
are place into containers group1 and group2, and, if group1_ordinals and
group2_ordinals are provided the ordinals of into those vectors.
The ordinals can be used as indices into the CNearTree itself.
<b>template<typename</b> <i>ContainerType</i>>
<b>std::vector<ContainerType> BelongsToPoints</b> ( <b>const ContainerType&</b> <i>t1</i> ) <b>const</b>;
<b>template<typename</b> <i>ContainerType</i>>
<b>void BelongsToPoints</b> ( <b>const T&</b> <i>t1</i>, <b>const T&</b> <i>t2</i>,
<b>ContainerType&</b> <i>group1</i>, <b>ContainerType&</b> <i>group2</i> );
<b>template<typename</b> <i>ContainerType</i>>
<b>void BelongsToPoints</b> ( <b>const T&</b> <i>t1</i>, <b>const T&</b> <i>t2</i>,
<b>ContainerType&</b> <i>group1</i>, <b>ContainerType&</b> <i>group2</i>,
<b>std::vector<size_t>&</b> <i>group1_ordinals</i>, <b>std::vector<size_t>&</b> <i>group2_ordinals</i>);
<b>template<typename</b> <i>ContainerType</i>>
<b>std::vector<ContainerType> BelongsToPoints</b> ( <b>const T&</b> <i>t1</i> );
If the input is a container of points t1: The NearTree is examined. For each
point input in the input container a new container of the same type is output
in the vector of containers that will be returned. If N points are input, then N
containers will be output. The points of the neartree will be examined. Copies
of the neartree points are put into the output container (in the output vector)
that corresponds to the input point that it is nearest to. If a point in the
NearTree is equidistant to more than one point in t1, then it is assigned to
the first point in the container at that distance
If the input is two points t1 and t2, then the corresponding Neartee points
are place into containers group1 and group2, and, if group1_ordinals and
group2_ordinals are provided the ordinals of into those vectors.
The ordinals can be used as indices into the CNearTree itself.
<b>template<typename</b> <i>ContainerTypeInside</i>, <b>typename</b> <i>ContainerTypeOutside</i>>
<b>void SeparateByRadius</b> ( <b>const DistanceType</b> <i>radius</i>, <b>const T&</b> <i>probe</i>,
<b>ContainerTypeInside&</b> <i>inside</i>, <b>ContainerTypeOutside&</b> <i>outside</i> );
<b>template<typename</b> <i>ContainerTypeInside</i>, <b>typename</b> <i>ContainerTypeOutside</i>>
<b>void SeparateByRadius</b> ( <b>const DistanceType</b> <i>radius</i>, <b>const T&</b> <i>probe</i>,
<b>ContainerTypeInside&</b> <i>inside</i>, <b>ContainerTypeOutside&</b> <i>outside</i>,
<b>std::vector<size_t>&</b> <i>inside_ordinals</i>, <b>std::vector<size_t>&</b> <i>outside_ordinals</i>);
return the points within radius of the probe in inside and the rest in outside
if inside_ordinals and outside_ordinals are provided the ordinals of the
found objects in the object store are put into those vectors. The ordinals
can be used as indices into the CNearTree itself.
<b>long FindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tInside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long FindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tInside</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long FindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tInside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tInside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tInside</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tInside</i>, <b>const T&</b> t ) <b>const</b>;
<i>dRadius</i> is the radius within which to search; make it very large if you want to
include every point that was loaded;
<i>tInside</i> is returned as the NearTree or container of objects that were found within a radius dRadius
of the probe point
if the <i>tIndices</i> argument is given it will be returned as a vector
of indices in the near tree of the objects returned.
<i>t</i> is the probe point, used to search in the group of points Insert'ed
return value is the count of the number of points found within the search radius
the "Left..." versions are deprecated versions provided for compatibility with
earlier NearTree releases.
<b>long FindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tOutside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long FindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tOutside</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long FindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tOutside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tOutside</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tOutside</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindOutSphere</b> ( <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tOutside</i>, <b>const T&</b> t ) <b>const</b>;
<i>dRadius</i> is the radius outside of which to search
<i>tOutside</i> is returned as the NearTree or container of objects that were found at
or outside of radius dRadius of the probe point
if the <i>tIndices</i> argument is given it will be returned as a vector
of indices in the near tree of the objects returned.
<i>t</i> is the probe point, used to search in the group of points Insert'ed
return value is the count of the number of points found outside the search radius
the "Left..." versions are deprecated versions provided for compatibility with
earlier NearTree releases.
<b>long FindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>ContainerType&</b> <i>tInRing</i>, <b>const T&</b> t ) <b>const</b>;
<b>long FindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>ContainerType&</b> <i>tInRing</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long FindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>CNearTree< T >&</b> <i>tInRing</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>ContainerType&</b> <i>tInRing</i>, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>ContainerType&</b> <i>tInRing</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t ) <b>const</b>;
<b>long LeftFindInAnnulus</b> ( <b>const DistanceType&</b> <i>dRadius1</i>,
<b>const DistanceType&</b> <i>dRadius2</i>,
<b>CNearTree< T >&</b> <i>tInRing</i>, <b>const T&</b> t ) <b>const</b>;
<i>dRadius1</i> and <i>dRadius2</i> are the two radii between which to find data points
<i>tInRing</i> is returned as the NearTree or container of objects that were found at
or outside of a radius dRadius1 and at or inside of radius dRadius2 of the probe point
if the <i>tIndices</i> argument is given it will be returned as a vector
of indices in the near tree of the objects returned.
<i>t</i> is the probe point, used to search in the group of points Insert'ed
return value is the count of the number of points found within the annulus
the "Left..." versions are deprecated versions provided for compatibility with
earlier NearTree releases.
<b>long FindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tClosest</i>, <b>const T&</b> t );
<b>long FindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tClosest</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t );
<b>long FindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tClosest</i>, <b>const T&</b> t );
<b>long LeftFindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tClosest</i>, <b>const T&</b> t );
<b>long LeftFindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>ContainerType&</b> <i>tClosest</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t );
<b>long LeftFindK_NearestNeighbors</b> ( <b>const size_t</b> <i>k</i>, <b>const DistanceType&</b> <i>dRadius</i>,
<b>CNearTree< T >&</b> <i>tClosest</i>, <b>const T&</b> t );
<i>k</i> is the maximum number of nearest neighbors to return. Finds this many if possible
<i>dRadius</i> within a sphere defined by dRadius, to search for the k-nearest-neighbors
<i>tClosest</i> is returned as the ContainerType or NearTree of the objects found
if the <i>tIndices</i> argument is given it will be returned as a vector
of indices in the near tree of the objects returned.
<i>t</i> is the probe point, used to search in the group of points insert'ed
return value is the count of the number of points found within the sphere
the "Left..." versions are deprecated versions provided for compatibility with
earlier NearTree releases.
<b>long FindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>ContainerType&</b> <i>tFarthest</i>, <b>const T&</b> t );
<b>long FindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>ContainerType&</b> <i>tFarthest</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t );
<b>long FindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>CNearTree< T >&</b> <i>tFarthest</i>, <b>const T&</b> t );
<b>long LeftFindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>ContainerType&</b> <i>tFarthest</i>, <b>const T&</b> t );
<b>long LeftFindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>ContainerType&</b> <i>tFarthest</i>,
<b>std::vector<size_t>&</b> tIndices, <b>const T&</b> t );
<b>long LeftFindK_FarthestNeighbors</b> ( <b>const size_t</b> <i>k</i>,
<b>CNearTree< T >&</b> <i>tFarthest</i>, <b>const T&</b> t );
<i>k</i> is the maximum number of farthest neighbors to return. Finds this many if possible
<i>tFarthest</i> is returned as the ContainerType or NearTree of the objects found
if the <i>tIndices</i> argument is given it will be returned as a vector
of indices in the near tree of the objects returned.
<i>t</i> is the probe point, used to search in the group of points insert'ed
return value is the count of the number of points found within the sphere
the "Left..." versions are deprecated versions provided for compatibility with
earlier NearTree releases.
<hr>
Access Functions:
<b>T at</b> ( <b>const size_t</b> <i>n</i> ) <b>const</b>;
returns the n'th item of the internal data store. This is not
guaranteed to be in the order of insertion.
<b>T operator[]</b> ( <b>const size_t</b> <i>n</i> );
returns the n'th item of the internal data store. This is not
guaranteed to be in the order of insertion.
<b>template<typename</b> <i>ContainerType</i>>
<b>operator ContainerType</b> ( <b>void</b> ) <b>const</b> ;
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, including CNearTree<T>.
The returned vector contents are not guaranteed to be returned in the order loaded.
<b>iterator begin</b> ( <b>void</b> ) <b>const</b>;
returns an iterator to the beginning of the internal data store
<b>iterator end</b> ( <b>void</b> ) <b>const</b>;
returns an iterator to the end of the data store (one beyond the last item)
<b>iterator back</b> ( <b>void</b> ) <b>const</b>;
returns an iterator to the last data item of the internal data store
<hr>
Information and special operation functions:
<b>void ImmediateInsert</b>( <b>const</b> <b>T</b>&aamp; <i>t</i> );
insert places objects in a queue for insertion later when CompleteDelayInsert
is called or a search is called. ImmediateInsert inserts the data immediately
into the tree (with the potential of a less balanced tree). ImmediateInsert is not
intended for the ordinary user.
<b>void CompleteDelayedInsert</b> ( <b>void</b> );
completes insertion 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.
<b>size_t GetDeferredSize</b> ( <b>void</b> );
returns the number of delayed objects that have not yet completed insertion. This is
mainly for information about details of the tree.
<b>size_t GetTotalSize</b> ( <b>void</b> );
returns the number of objects that have been insert'ed plus those DelayInsert'ed
<b>size_t size</b> ( <b>void</b> );
identical to GetTotalSize
<b>size_t GetDepth</b> ( <b>void</b> );
returns the maximum tree layers from the root. This is mainly for information about
details of the tree.
<b>double GetDimEstimate</b> ( <b>void</b> ); // returns an estimate of the Hausdorff dimension
<b>double GetDimEstimate</b> ( <b>const double</b> <i>DimEstimateEsd</i> );
// returns an estimate of the Hausdorff dimension
// to within the given esd
<b>double GetDimEstimateEsd</b> ( <b>void</b> );
// returns an estimate of the esd
<b>double GetDiamEstimate</b> ( <b>void</b> ); // returns an estimate of the diameter
<b>DistanceType GetMeanSpacing</b> ( <b>void</b> ); // returns an estimate object spacing
<b>DistanceType GetVarSpacing</b> ( <b>void</b> ); // returns an estimate object spacing variance
<b>size_t GetNodeVisits</b> ( <b>void</b> ); // returns the number of node visits if
// CNEARTREE_INSTRUMENTED as defined, 0 otherwise
<b>void SetNodeVisits,/b> ( <b>const size_t</b> <i>visits</b>);
// set the number of node visits
<b>T Centroid</b> ( <b>void</b> );
returns the centroid of a neartree.
<b>bool empty</b> ( <b>void</b> );
returns true if the tree is empty, otherwise false
<hr>
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
can 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. This is the
list of iterators. The same set is available for const_iterator.
<b>iterator</b> ( <b>void</b> ) { }; // constructor
<b>explicit iterator</b> ( <b>const const_iterator&</b> <i>s</i> );
<b>iterator& operator= </b> ( <b>const iterator&</b> <i>s</i> );
<b>iterator& operator= </b> ( <b>const const_iterator&</b> <i>s</i> );
<b>iterator operator++ </b> ( <b>const int</b> <i>n</i> );
<b>iterator operator-- </b> ( <b>const int</b> <i>n</i> );
<b>iterator& operator++ </b> ( <b>void</b> );
<b>iterator& operator-- </b> ( <b>void</b> );
<b>iterator operator+ </b> ( <b>const long</b> <i>n</i> ) <b>const</b>;
<b>iterator operator- </b> ( <b>const long</b> <i>n</i> ) <b>const</b>;
<b>iterator& operator+= </b> ( <b>const long</b> <i>n</i> );
<b>iterator& operator-= </b> ( <b>const long</b> <i>n</i> );
<b>T operator* </b> ( <b>void</b> ) <b>const</b>;
<b>bool operator== </b> ( <b>const iterator&</b> <i>t</i> ) <b>const</b>;
<b>bool operator!= </b> ( <b>const iterator&</b> <i>t</i> ) <b>const</b>;
<b>bool operator== </b> ( <b>const const_iterator&</b> </i>t</i> ) <b>const</b>;
<b>bool operator!= </b> ( <b>const const_iterator&</b> </i>t</i> ) <b>const</b>;
<b>bool operator> </b> ( <b>const iterator&</b> </i>t</i> ) <b>const</b>;
<b>bool operator> </b> ( <b>const const_iterator&</b> </i>t</i> ) <b>const</b>;
<b>bool operator< </b> ( <b>const iterator&</b> </i>t</i> ) <b>const</b>;
<b>bool operator< </b> ( <b>const const_iterator&</b> </i>t</i> ) <b>const</b>;
<b>const T * const operator-> </b> ( <b>void</b> ) <b>const</b>;
<b>long get_position</b> ( <b>void</b> ) <b>const</b>;
<b>const CNearTree< T, DistanceType, distMinValue > * get_parent</b> ( <b>void</b> );
<hr>
</tt>
</pre>
<p>
So a complete program is:
<p>
<pre>
<tt>
#include "TNear.h"
#include <cstdio>
void main()
{
CNearTree< double > dT;
double dNear;
dT.Insert( 1.5 );
if ( dT.NearestNeighbor( 10000.0, dNear, 2.0 )) printf( "%f\n",double(dNear-2.0) );
}
</tt>
</pre>
and it should print 0.5 (that's how for 2.0 is from 1.5). For more examples of
the use of TNear.h, see <a href="main.cpp">main.cpp</a> and <a href="CNearTreeTest.cpp">CNearTreeTest.cpp</a>.
<hr>
<hr>
<h4><a name="CNearTree.c">The C NearTree API: CNearTree.c</a></h4>
<h4>Synopsis</h4>
<P>
<blockquote>
<font size="-1">
<b>#include <CNearTree.h></b>
<P>
<p>
<b>double</b> <b>CNearTreeDistsq</b> ( <b>CNearTreeHandle</b> <i>treehandle</i>, <b>void *</b> <i>coord1</i>,
<b>void *</b> <i>coord2</i> );
<p>
<b>double</b> <b>CNearTreeDist</b> ( <b>CNearTreeHandle</b> <i>treehandle</i>, <b>void *</b> <i>coord1</i>,
<b>void *</b> <i>coord2</i> );
<p>
<b>int</b> <b>CNearTreeSetNorm</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>int</b> <i>treenorm</i> );
<p>
<b>int</b> <b>CNearTreeNodeCreate</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>CNearTreeNodeHandle *</b> <i>treenodehandle</i> )
<p>
<b>int</b> <b>CNearTreeCreate</b> ( <b>CNearTreeHandle *</b> <i>treehandle</i>,
<b>size_t</b> <i>treedim</i>,
<b>int</b> <i>treetype</i> );
<p>
<b>int</b> <b>CNearTreeFree</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i> );
<p>
<b>int</b> <b>CNearTreeClear</b> ( <b>CNearTreeHandle *</b> <i>treehandle</i> );
<p>
<b>int</b> <b>CNearTreeNodeFree</b> ( <b>CNearTreeNodeHandle *</b> <i>treenodehandle</i> );
<p>
<b>int</b> <b>CNearTreeInsert</b>( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>const</b> <b>void *</b> <i>obj</i> );
<p>
<b>int</b> <b>CNearTreeImmediateInsert</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>const</b> <b>void *</b> <i>obj</i> );
<p>
<b>int</b> <b>CNearTreeDelayedInsert</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>const</b> <b>void *</b> <i>obj</i> ); /* ***DEPRECATED*** */
<p>
<b>int</b> <b>CNearTreeNodeInsert</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>CNearTreeNodeHandle</b> <i>treenodehandle</i>,
<b>size_t</b> <i>index</i>;
<b>size_t *</b> <i>depth</i> );
<p>
<b>int</b> <b>CNearTreeNodeInsert_Flip</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>CNearTreeNodeHandle</b> <i>treenodehandle</i>,
<b>size_t</b> <i>index</i>;
<b>size_t *</b> <i>depth</i> );
<p>
<b>int CNearTreeNodeReInsert_Flip</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>const CNearTreeNodeHandle</b> <i>treenodehandle</i>,
<b>const CNearTreeNodeHandle</b> <i>pntn</i>,
<b>size_t *</b> <i>depth</i> );
<p>
<b>int</b> <b>CNearTreeCompleteDelayedInsert</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i> )
<b>int</b> <b>CNearTreeZeroIfEmpty</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i> );
<p>
<b>int</b> <b>CNearTreeGetSize</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t *</b> <i>size</i> );
<p>
<b>int</b> <b>CNearTreeGetTotalSize</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t *</b> <i>size</i> ); /* ***DEPRECATED*** */
<p>
<b>size_t</b> <b>CNearTreeSize</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>);
<p>
<b>int</b> <b>CNearTreeGetDeferredSize</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t *</b> <i>size</i> );
<p>
<b>int</b> <b>CNearTreeGetDelayedSize</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t *</b> <i>size</i> ); /* ***DEPRECATED*** */
<p>
<b>int</b> <b>CNearTreeGetDepth</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t *</b> <i>depth</i> )
<p>
<b>int</b> <b>CNearTreeGetFlags</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>long *</b> <i>flags</i>, <b>const long</b> <i>mask</i> )
<p>
<b>int</b> <b>CNearTreeSetFlags</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>const long</b> <i>flags</i>, <b>const long</b> <i>mask</i> )
<p>
<b>int</b> <b>CNearTreeGetMeanSpacing</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>double *</b> <i>spacing</i> );
<p>
<b>int</b> <b>CNearTreeGetVarSpacing</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>double *</b> <i>varspacing</i> );
<p>
<b>int</b> <b>CNearTreeCount</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>size_t *</b> <i>count</i> );
<p>
<b>int</b> <b>CNearTreeNodeCount</b> ( <b>const CNearTreeNodeHandle</b> <i>treenodehandle</i>,
<b>size_t *</b> <i>count</i> );
<p>
#ifdef CNEARTREE_INSTRUMENTED
<p>
<b>int</b> <b>CNearTreeGetNodeVisits</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>size_t *</b> <i>visits</i>);
<p>
<b>int</b> <b>CNearTreeSetNodeVisits</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>const size_t</b> <i>visits</i> );
<p>
#endif
<p>
<b>int</b> <b>CNearTreeGetDiamEstimate</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>double *</b> <i>diamest</i> );
<p>
<b>int</b> <b>CNearTreeGetDimEstimateEsd</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>double *</b> <i>dimestesd</i> );
<p>
<b>int CNearTreeGetDimEstimate</b> ( <b>const CNearTreeHandle</b> <i>treehandle</i>,
<b>double *</b> <i>dimest</i>,
<b>const double</b> <i>DimEstimateEsd</i> );
<p>
<b>int</b> <b>CNearTreeNearestNeighbor</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> dRadius,
<b>void * *</b> <i>coordClosest</i>,
<b>void * *</b> <i>objClosest</i>,
<b>const</b> <b>void *</b> <i>coord</i> );
<p>
<b>int</b> <b>CNearLeftTreeNearestNeighbor</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> dRadius,
<b>void * *</b> <i>coordClosest</i>,
<b>void * *</b> <i>objClosest</i>,
<b>const</b> <b>void *</b> <i>coord</i> ); /* ***DEPRECATED*** */
<p>
<b>int</b> <b>CNearTreeFarthestNeighbor</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>void *</b> * <i>coordFarthest</i>,
<b>void *</b> * <i>objFarthest</i>,
<b>const</b> <b>void *</b> <i>coord</i> );
<p>
<b>int</b> <b>CNearTreeFindInSphere</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CVectorHandle</b> <i>coordInside</i>,
<b>CVectorHandle</b> <i>objInside</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> );
<p>
<b>int</b> <b>CNearTreeFindTreeInSphere</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CNearTreeHandle</b> <i>foundInside</i>,
<b>const void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> )
<p>
<b>int</b> <b>CNearTreeFindOutSphere</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CVectorHandle</b> <i>coordOutside</i>,
<b>CVectorHandle</b> <i>objOutside</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> );
<p>
<b>int</b> <b>CNearTreeFindTreeOutSphere</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CNearTreeHandle</b> <i>foundOutside</i>,
<b>const void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> )
<p>
<b>int</b> <b>CNearTreeFindInAnnulus</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadiusInner</i>,
<b>const</b> <b>double</b> <i>dRadiusOuter</i>,
<b>CVectorHandle</b> <i>coordInRing</i>,
<b>CVectorHandle</b> <i>objInRing</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> );
<p>
<b>int</b> <b>CNearTreeFindTreeInAnnulus</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>double</b> <i>dRadiusInner</i>,
<b>const</b> <b>double</b> <i>dRadiusOuter</i>,
<b>CNearTreeHandle</b> <i>foundInRing</i>,
<b>const void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> )
<p>
<b>int</b> <b>CNearTreeFindKNearest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>size_t</b> <i>k</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CVectorHandle</b> <i>coordClosest</i>,
<b>CVectorHandle</b> <i>objClosest</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> );
<p>
<b>int</b> <b>CNearTreeFindKTreeNearest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>size_t</b> <i>k</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CNearTreeHandle</b> <i>foundClosest</i>,
<b>const void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> )
<p>
<b>int</b> <b>CNearTreeFindKFarthest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>size_t</b> <i>k</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CVectorHandle</b> <i>coordFarthest</i>,
<b>CVectorHandle</b> <i>objFarthest</i>,
<b>const</b> <b>void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> );
<p>
<b>int</b> <b>CNearTreeFindKTreeFarthest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>const</b> <b>size_t</b> <i>k</i>,
<b>const</b> <b>double</b> <i>dRadius</i>,
<b>CNearTreeHandle</b> <i>foundFarthest</i>,
<b>const void *</b> <i>coord</i>,
<b>int</b> <i>resetcount</i> )
<p>
<b>int</b> <b>CNearTreeNearest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>double</b> * dRadius,
<b>void *</b> * <i>coordClosest</i>,
<b>void *</b> * <i>objClosest</i>,
<b>const</b> <b>void *</b> <i>coord</i> );
<p>
<b>int</b> <b>CNearTreeLeftNearest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>double</b> * dRadius,
<b>void *</b> * <i>coordClosest</i>,
<b>void *</b> * <i>objClosest</i>,
<b>const</b> <b>void *</b> <i>coord</i> ); /* ***DEPRECATED*** */
<p>
<b>int</b> <b>CNearTreeFindFarthest</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>double</b> * dRadius,
<b>void *</b> * <i>coordFarthest</i>,
<b>void *</b> * <i>objFarthest</i>,
<b>const</b> <b>void *</b> <i>coord</i> );
<p>
<b>int</b> <b>CNearTreeObjects</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>CVectorHandle *</b> <i>vectorhandle</i> );
<p>
<b>void *</b> <b>CNearTreeObjectAt</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>size_t</b> <i>index</i> );
<p>
<b>int</b> <b>CNearTreeCoords</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>, <b>CVectorHandle *</b> <i>vectorhandle</i> );
<p>
<b>void *</b> <b>CNearTreeCoordAt</b> ( <b>const</b> <b>CNearTreeHandle</b> <i>treehandle</i>,
<b>size_t</b> <i>index</i> );
</font>
</blockquote>
<p>
The NearTree API works with coordinate vectors in an arbitrary number of
dimensions. Each neartree is accessed by a pointer of type <b>CNearTreeHandle</b>
which points to a struct of type <b>CNearTree</b>, which points to a tree of
nodes of type CNearTreeNode:
<p>
<pre><tt>
typedef struct _CNearTreeNode {
size_t m_indexLeft; /* index of left coords in m_CoordStore
and of left object in m_ObjectStore */
size_t m_indexRight; /* index of right coords in m_CoordStore
and of right object in m_ObjectStore */
double m_dMaxLeft; /* longest distance from the left object
to anything below it in the tree */
double m_dMaxRight; /* longest distance from the right object
to anything below it in the tree */
struct _CNearTreeNode * m_pLeftBranch;
/* tree descending from the left object */
struct _CNearTreeNode * m_pRightBranch;
/* tree descending from the right object */
long m_iflags; /* flags
size_t m_iTreeSize; /* size of this node tree */
#ifdef CNEARTREE_INSTRUMENTED
size_t m_Height; /* height of this node */
#endif
*/
} CNearTreeNode;
typedef CNearTreeNode * CNearTreeNodeHandle;
typedef struct {
CNearTreeNodeHandle m_ptTree; /* pointer to the actual tree */
size_t m_szdimension; /* dimension of the coordinates */
size_t m_szsize; /* size of this tree */
size_t m_szdepth; /* depth of this tree */
int m_iflags; /* flags */
CVectorHandle m_ObjectStore; /* all inserted objects */
CVectorHandle m_CoordStore; /* all inserted coordinates */
CVectorHandle m_DelayedIndices;/* objects queued for insertion */
CRHrand m_rhr; /* random number generator */
double m_DiamEstimate; /* estimated diameter */
double m_SumSpacings; /* sum of spacings at time of insertion */
double m_SumSpacingsSq; /* sum of spacings squared at time of insertion */
double m_DimEstimate; /* estimated dimension */
double m_DimEstimateEsd;/* estimated dimension estimated standard deviation */
#ifdef CNEARTREE_INSTRUMENTED
size_t m_NodeVisits; /* number of node visits */
#endif
} CNearTree;
typedef CNearTree FAR * CNearTreeHandle;
/* Execution Control Flags */
#define CNTF_NOPREPRUNE 0x10000L /*flag to suppress all search prepruning */
#define CNTF_FORCEPREPRUNE 0x20000L /*flag to force search prepruning */
#define CNTF_NOFLIP 0x40000L /*flag to suppress flips on insert */
#define CNTF_FORCEFLIP 0x80000L /*flag to force flips on insert */
#define CNTF_NODEFER 0x100000L /*flag to prevent deferred insert */
#define CNTF_SKNN 0x200000L /*flag to use spherical KNN */
</pre></tt>
</p>
The internal operation of the API depends on the function <b>CNearTreeDist</b>
that returns the distance (L1, L2 or L-infinity) between two
coordinate vectors as a double according to the parameters of the given tree.
Note that the tree may store the coordinates as integers or as doubles, but
the distance is always computed as a double.
If this function is replaced by a user function, it is important that the
replacement obey the triangle inequality.
<P>
A neartree is created by <b>CNearTreeCreate</b> and freed by <b>CNearTreeFree</b>.
<i>treedim</i> is the dimension of the coordinate vectors and <i>treetype</i>
is one of the three predefined constants <b>CNEARTREE_TYPE_DOUBLE</b> for double
or <b>CNEARTREE_TYPE_INTEGER</b> for integer or <b>CNEARTREE_TYPE_STRING</b>,
optionally ORed with <b>CNEARTREE_NORM_L1</b>, <b>CNEARTREE_NORM_L2</b> or CNEARTREE_NORM_LINF</b>
for L1, L2 or L-infinity norms, <b>CNEARTREE_NORM_SPHERE</b> or <b>CNEARTREE_NORM_HSPHERE</b> for
a spherical or hemispherical norm (L1-norm combination of radial and spherical/hemispherical
triangle distances), or <b>CNEARTREE_NORM_HAMMING</b> for the string-Hamming distance norm
(add one for each differing character position).
<p>
Starting with release 2.1, all insertions are delayed by default, unless the insertions is
done by a call to <b>CNearTreeImmediateInsert</b>. The insertions that have been queued
are completed by a call to <b>CNearTreeCompleteDelayedInsert</b> or by any search. The insertions
are actually done in a randomized order, either for an initial block of sqrt(#queue) by
default. or for the entire queue if the flag <b>CNEARTREE_DEFER_ALL</b> is ored with
<i>treetype</i>.
<p>Starting with release 3 (formerly called release 2.4) optionally, you may also define
<b>CNEARTREE_FORCEFLIP</b> to maximize tree reorganization on insertion,
<b>CNEARTREE_NOFLIP</b> to suppress tree reorganization on insertion, <b>CNEARTREE_NODEFER</b>
to make all insertions immediate, <b>CNEARTREE_FORCEPREPUNE</b> to do searches first with a
tighter estimate on the search radius, and <b>CNEARTREE_NOPREPRUNE</b> to suppress that behavior.
The defaults are to do tree reorganization on insertion, to defer insertions, but not
to preprune the search radius.
If you define CNEARTREE_INSTRUMENTED, code will be enabled to track node visits in
searching the tree.
<p>
The flags <b>CNEARTREE_DEFER_ALL</b> and <b>CNEARTREE_FLIP</b> used in prior releases
are deprecated, but are still defined. They have no effect.
<p>
When first created, a neartree has 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. The tree is then populated by calls to
<b>CNearTreeInsert</b>, with each call providing a coordinate vector <i>coord</i>
and an optional object pointer <i>obj</i>. The API copies the coordinate vector,
but does not copy the object. Later, when a search is requested or an explicit call
to <b>CNearTreeCompleteDelayedInsert</b> is made, the tree is populated in the order left, right
and then the nearer child, working from a randomized selection from the items queued
for insertion.
<p>
Optionally, the actual insertions may done immediately by calling <b>CNearTreeImmediateInsert</b>
instead of <b>CNearTreeInsert</b>. For upwards compatibility of the library for existing
code, the deprecated <b>CNearTreeDelayedInsert</b> is provided as an deprecated alternate
call to <b>CNearTreeInsert</b>.
<p>
The neartree is searched for the nearest or farthest coordinate vector in the neartree to a given
probe coordinate vector <i>coord</i> by <b>CNearTreeNearestNeighbor</b> and
<b>CNearTreeFarthestNeighbor</b>, respectively. Starting with release 3, the search is
balanced, following the left or right branch first depending on which child node is
closest. The former left-first behavior is deprecated, but still available in
<b>CNearLeftTreeNearestNeighbor</b>. The given radius confines the search
to a sphere around the probe. If more than a single extremal coordinate point is
needed, <b>CNearTreeFindInSphere</b> can be used to obtain a CVector result vector of
all the coordinate vectors that satisfy the constraint of being within a specified
radius, or <b>CNearTreeFindOutSphere</b> can be used to obtain a CVector result vector
of all the coordinates that satisfy the constraint of being outside a specified radius.
<b>CNearTreeFindIn Annulus</b> can be used to obtain a CVector result vector of all the
coordinates that satisfy the constraint of being between two specified radii from the probe.
<b>CNearTreeFindKNearest</b> can be used to obtain a CVector result vector of the <i>k</i>
coordinates closest to the probe point such that all results are within the specified
radius of the probe point, or <b>CNearTreeFindKFarthest</b> to obtain a CVector result vector
of the <i>k</i> coordinates farthest from the probe point such that all results are at or outside the specified
radius of the probe point.
The vectors themselves
are not copied into the result vector. If the parameter <i>resetcount</i> is true (non
zero) the result vector is cleared before the search. A CVector result vector
of the matching object pointers is returned if <i>objs</i> is not NULL.
Aternatively the forms <b>CNearTreeFindTreeInSphere</b>, <b>CNearTreeFindTreeOutSphere</b>,
<b>CNearTreeFindTreeInAnnulus</b>, <b>CNearTreeFindKTreeNearest</b>, <b>CNearTreeFindKTreeFarthest</b>
can be used to obtain CNearTrees rather than CVectors of results. The functions
<b>CNearTreeNearest</b> and <b>CNearTreeFindFarthest</b> implement <b>CNearTreeNearestNeighbor</b> and
<b>CNearTreeFarthestNeighbor</b>, respectively, adjusting the radius of the search while
the search is in progress and are not normally used by users.
<p>
<p>The size of the tree as a count of objects can be obtained using the function
<b>NearTreeGetSize</b> or the macro <b>NearTreeSize</b>. The size of the tree
as a count of nodes and the depth of the tree can be obtained using the functions
<b>CNearTreeCount</b> and <b>CNearTreeGetDepth</b>. Estimates of the Hausdorff
dimension, the esd of that estimate, the diameter, the spacing and the variance
of the spacing can be obtained with <b>CNearTreeGetDimEstimate</b>,
<b>CNearTreeGetDimEstimateEsd</b>,
<b>CNearTreeGetDiamEstimate</b>, <b>CNearTreeGetMeanSpacing</b> and
<b>CNearTreeGetVarSpacing</b>.
<P>
<h4>Returns</h4>
If <b>CNearTreeDist</b> fails, it returns -1. Except for <b>CNearTreeDist</b>, all the functions
in the API return 0 ( <b>CNEARTREE_SUCCESS</b> ) for
success. If dynamic memory allocation fails, <b>CNEARTREE_MALLOC_FAILED</b> is returned.
If a call is made with an improper argument, <b>CNEARTREE_BAD_ARGUMENT</b> is returned.
If a search fails to find anything, <b>CNEARTREE_NOT_FOUND</b> is returned. If there is
a failure in an attempt to free a CNearTree, <b>CNEARTREE_FREE_FAILED</b> is returned. If
any of the internal call to CVector fail, <b>CNEARTREE_CVECTOR_FAILED</b> is returned. For
convenience in debugging, the formerly negative values of these returns are now positive.
<p>
<h4>Examples</h4>
<P>
To create a neartree for 3-dimensional vectors of doubles:
<P>
<pre>
<tt>
#include <CNearTree.h>
CNearTreeHandle treehandle;
int bReturn;
...
bReturn = !CNearTreeCreate(&treehandle,3,CNEARTREE_TYPE_DOUBLE);
</tt>
</pre>
<p>
To insert a copy of a 3-dimensional vector of doubles into this tree,
with no associated object:
<P>
<pre>
<tt>
double v[3];
...
v[0] = 1.; v[1] = 2.; v[2] = 3.;
bReturn = !CNearTreeInsert(treehandle,&v[0],NULL);
</tt>
</pre>
<p>
To search for the nearest neighbor to a probe vector vSearch in a radius of 3.,
returning a pointer to the resulting vector in vBest:
<P>
<pre>
<tt>
double * vBest;
void * vvBest;
double vSearch[3];
double dRad = =3.;
...
if ( !CNearTreeNearestNeighbor(treehandle,dRad,&vvBest,NULL,vSearch))
{ vBest = (double *)vvBest; }
</tt>
</pre>
<p>
Note the use of a separate void * vvBest instead of a cast of &vBest to avoid compiler
type punning warnings.
<P>
For more examples of the use of CNearTree.c, see <a href="main.c">main.c</a> and <a href="CNearTreeTest.c">CNearTreeTest.c</a>
in the release kit.
<hr>
<hr>
<h4><a name="rhrand.h">A Portable pseudo-random number generator: rhrand.h</a></h4>
<p>
rhrand.h is a portable pseudo-random number generator based one by Rob Harrison, derived from
"one in J.M.Hammersley and D.C. Handscomb, 'Monte Carlo
Methods,' Methuen & Co., London and Wiley & Sons, New
York, 1964, p47". See also, D. E. Knuth "The Art of
Computer Programming", Volume 2, "Seminumerical
Alogorithms, Third Edition, Addison-Wesley, Reading MA, 1997.
<p>
rhrand.h is a header file in which a C++ class, RHrand, is defined, and a
C struct typedef CRHrand is defined.
<p>
The C++ interface is
<p>
<pre><tt>
static const int RHRAND_MAX = 32767; /* the integer range accessible as RHrand::RHRAND_MAX */
RHrand(void) /* the default constructor */
RHrand( const int iseed ) /* a constructor to start with the given seed */
~RHrand( void) /* a destructor */
void srandom( const int iseed) /* reset the generator based on the given seed */
double urand( void ) /* return a random double uniformly distributed in [0,1) */
int random ( void ) /* return a random integer uniformly distributed in [0, RHRAND_MAX-1] */
</tt></pre>
<p>
In C++ code, typical use is
<pre><tt>
#include <rhhand.h>
RHrand rhr;
...
x = rhr.urand();
</pre></tt>
<p>
The C interface is suppressed in RHRAND_NOCCODE is defined. Otherwise the C interface is based on defining
a struct of type CRHRrand and calling macros that refer to a handle of type RCRHrandHandle.
<p>
<pre><tt>
typedef struct CRHrand_ { /* the struct used in random number generattion */
double buffer[55];
int indx;
int jndx;
int kndx;
double dTemp;
} CRHrand;
typedef CRHrand * CRHrandHandle; /* the type to be used in maro calls */
#define CRHRAND_MAX 32767 /* the integer range */
#define CRHrandSrandom(randhandle,iseed) ...
/* a macro to call to initialize CHRrandHandle randhandle
using see int iseed */
#define CRHrandUrand(randhandle) ... /* a macro to return a random double uniformly distributed in [0,1) */
#define CRHrandRandom(randhandle) ((int)(CRHrandUrand(randhandle)*(double)CRHRAND_MAX))
/* a macro to return a random integer uniformly distributed in
[0, CRHRAND_MAX-1] */
</tt></pre>
<p>
Typical use is
<pre><tt>
#include <rhhand.h>
CRHrand rhr;
...
CRHrandSrandom(&rhr, 0 );
...
x = CRHrandUrand(&rhr);
</pre></tt>
<hr>
<hr>
<font size="-1">
Updated 25 April 2016<br />
<script language="javascript" type="text/javascript">
<!--
var name = "andrewsL@";
var domain = "ix.netcom.com";
var domext = ".com";
document.write ("<a href=\"mailto:" + name + domain + domext + "\">" + name + domain + domext+"</a>");
// -->
</script>
<noscript>
andrewsl@ix.netcom.com
</noscript>
</font>
</font>
</body>
</html>
|