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
|
/******************************************************************************
*
* Project: CPL - Common Portability Library
* Purpose: Implementation of quadtree building and searching functions.
* Derived from shapelib and mapserver implementations
* Author: Frank Warmerdam, warmerdam@pobox.com
* Even Rouault, <even dot rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 1999-2008, Frank Warmerdam
* Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
******************************************************************************
*/
#include "cpl_port.h"
#include "cpl_quad_tree.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include "cpl_conv.h"
#include "cpl_error.h"
constexpr int MAX_DEFAULT_TREE_DEPTH = 12;
constexpr int MAX_SUBNODES = 4;
typedef struct _QuadTreeNode QuadTreeNode;
struct _QuadTreeNode
{
/* area covered by this psNode */
CPLRectObj rect;
int nFeatures; /* number of shapes stored at this psNode. */
int nNumSubNodes; /* number of active subnodes */
void **pahFeatures; /* list of shapes stored at this psNode. */
CPLRectObj *pasBounds;
QuadTreeNode *apSubNode[MAX_SUBNODES];
};
struct _CPLQuadTree
{
QuadTreeNode *psRoot;
CPLQuadTreeGetBoundsFunc pfnGetBounds;
CPLQuadTreeGetBoundsExFunc pfnGetBoundsEx;
void *pUserData;
int nFeatures;
int nMaxDepth;
int nBucketCapacity;
double dfSplitRatio;
bool bForceUseOfSubNodes;
};
static void CPLQuadTreeAddFeatureInternal(CPLQuadTree *hQuadTree,
void *hFeature,
const CPLRectObj *pRect);
static void CPLQuadTreeNodeDestroy(QuadTreeNode *psNode);
/* -------------------------------------------------------------------- */
/* If the following is 0.5, psNodes will be split in half. If it */
/* is 0.6 then each apSubNode will contain 60% of the parent */
/* psNode, with 20% representing overlap. This can be help to */
/* prevent small objects on a boundary from shifting too high */
/* up the hQuadTree. */
/* -------------------------------------------------------------------- */
constexpr double DEFAULT_SPLIT_RATIO = 0.55;
/*
** Returns TRUE if rectangle a is contained in rectangle b
*/
static CPL_INLINE bool CPL_RectContained(const CPLRectObj *a,
const CPLRectObj *b)
{
return a->minx >= b->minx && a->maxx <= b->maxx && a->miny >= b->miny &&
a->maxy <= b->maxy;
}
/*
** Returns TRUE if rectangles a and b overlap
*/
static CPL_INLINE bool CPL_RectOverlap(const CPLRectObj *a, const CPLRectObj *b)
{
if (a->minx > b->maxx)
return false;
if (a->maxx < b->minx)
return false;
if (a->miny > b->maxy)
return false;
if (a->maxy < b->miny)
return false;
return true;
}
/************************************************************************/
/* CPLQuadTreeNodeCreate() */
/************************************************************************/
static QuadTreeNode *CPLQuadTreeNodeCreate(const CPLRectObj *pRect)
{
QuadTreeNode *psNode =
static_cast<QuadTreeNode *>(CPLMalloc(sizeof(QuadTreeNode)));
psNode->nFeatures = 0;
psNode->pahFeatures = nullptr;
psNode->pasBounds = nullptr;
psNode->nNumSubNodes = 0;
memcpy(&(psNode->rect), pRect, sizeof(CPLRectObj));
return psNode;
}
/************************************************************************/
/* CPLQuadTreeCreate() */
/************************************************************************/
/**
* Create a new quadtree
*
* @param pGlobalBounds a pointer to the global extent of all
* the elements that will be inserted
* @param pfnGetBounds a user provided function to get the bounding box of
* the inserted elements. If it is set to NULL, then
* CPLQuadTreeInsertWithBounds() must be used, and
* extra memory will be used to keep features bounds in the
* quad tree.
*
* @return a newly allocated quadtree
*/
CPLQuadTree *CPLQuadTreeCreate(const CPLRectObj *pGlobalBounds,
CPLQuadTreeGetBoundsFunc pfnGetBounds)
{
CPLAssert(pGlobalBounds);
/* -------------------------------------------------------------------- */
/* Allocate the hQuadTree object */
/* -------------------------------------------------------------------- */
CPLQuadTree *hQuadTree =
static_cast<CPLQuadTree *>(CPLMalloc(sizeof(CPLQuadTree)));
hQuadTree->nFeatures = 0;
hQuadTree->pfnGetBounds = pfnGetBounds;
hQuadTree->pfnGetBoundsEx = nullptr;
hQuadTree->nMaxDepth = 0;
hQuadTree->nBucketCapacity = 8;
hQuadTree->dfSplitRatio = DEFAULT_SPLIT_RATIO;
hQuadTree->bForceUseOfSubNodes = false;
/* -------------------------------------------------------------------- */
/* Allocate the psRoot psNode. */
/* -------------------------------------------------------------------- */
hQuadTree->psRoot = CPLQuadTreeNodeCreate(pGlobalBounds);
hQuadTree->pUserData = nullptr;
return hQuadTree;
}
/************************************************************************/
/* CPLQuadTreeCreateEx() */
/************************************************************************/
/**
* Create a new quadtree
*
* @param pGlobalBounds a pointer to the global extent of all
* the elements that will be inserted
* @param pfnGetBoundsEx a user provided function to get the bounding box of
* the inserted elements. If it is set to NULL, then
* CPLQuadTreeInsertWithBounds() must be used, and
* extra memory will be used to keep features bounds in the
* quad tree.
* @param pUserData user data passed to pfnGetBoundsEx
*
* @return a newly allocated quadtree
*/
CPLQuadTree *CPLQuadTreeCreateEx(const CPLRectObj *pGlobalBounds,
CPLQuadTreeGetBoundsExFunc pfnGetBoundsEx,
void *pUserData)
{
CPLAssert(pGlobalBounds);
/* -------------------------------------------------------------------- */
/* Allocate the hQuadTree object */
/* -------------------------------------------------------------------- */
CPLQuadTree *hQuadTree =
static_cast<CPLQuadTree *>(CPLMalloc(sizeof(CPLQuadTree)));
hQuadTree->nFeatures = 0;
hQuadTree->pfnGetBounds = nullptr;
hQuadTree->pfnGetBoundsEx = pfnGetBoundsEx;
hQuadTree->nMaxDepth = 0;
hQuadTree->nBucketCapacity = 8;
hQuadTree->dfSplitRatio = DEFAULT_SPLIT_RATIO;
hQuadTree->bForceUseOfSubNodes = false;
/* -------------------------------------------------------------------- */
/* Allocate the psRoot psNode. */
/* -------------------------------------------------------------------- */
hQuadTree->psRoot = CPLQuadTreeNodeCreate(pGlobalBounds);
hQuadTree->pUserData = pUserData;
return hQuadTree;
}
/************************************************************************/
/* CPLQuadTreeGetAdvisedMaxDepth() */
/************************************************************************/
/**
* Returns the optimal depth of a quadtree to hold nExpectedFeatures
*
* @param nExpectedFeatures the expected maximum number of elements to be
* inserted.
*
* @return the optimal depth of a quadtree to hold nExpectedFeatures
*/
int CPLQuadTreeGetAdvisedMaxDepth(int nExpectedFeatures)
{
/* -------------------------------------------------------------------- */
/* Try to select a reasonable one */
/* that implies approximately 8 shapes per node. */
/* -------------------------------------------------------------------- */
int nMaxDepth = 0;
int nMaxNodeCount = 1;
while (nMaxNodeCount < nExpectedFeatures / 4)
{
nMaxDepth += 1;
nMaxNodeCount = nMaxNodeCount * 2;
}
CPLDebug("CPLQuadTree", "Estimated spatial index tree depth: %d",
nMaxDepth);
/* NOTE: Due to problems with memory allocation for deep trees,
* automatically estimated depth is limited up to 12 levels.
* See Ticket #1594 for detailed discussion.
*/
if (nMaxDepth > MAX_DEFAULT_TREE_DEPTH)
{
nMaxDepth = MAX_DEFAULT_TREE_DEPTH;
CPLDebug("CPLQuadTree",
"Falling back to max number of allowed index tree "
"levels (%d).",
MAX_DEFAULT_TREE_DEPTH);
}
return nMaxDepth;
}
/************************************************************************/
/* CPLQuadTreeSetMaxDepth() */
/************************************************************************/
/**
* Set the maximum depth of a quadtree. By default, quad trees have
* no maximum depth, but a maximum bucket capacity.
*
* @param hQuadTree the quad tree
* @param nMaxDepth the maximum depth allowed
*/
void CPLQuadTreeSetMaxDepth(CPLQuadTree *hQuadTree, int nMaxDepth)
{
hQuadTree->nMaxDepth = nMaxDepth;
}
/************************************************************************/
/* CPLQuadTreeSetBucketCapacity() */
/************************************************************************/
/**
* Set the maximum capacity of a node of a quadtree. The default value is 8.
* Note that the maximum capacity will only be honoured if the features
* inserted have a point geometry. Otherwise it may be exceeded.
*
* @param hQuadTree the quad tree
* @param nBucketCapacity the maximum capacity of a node of a quadtree
*/
void CPLQuadTreeSetBucketCapacity(CPLQuadTree *hQuadTree, int nBucketCapacity)
{
if (nBucketCapacity > 0)
hQuadTree->nBucketCapacity = nBucketCapacity;
}
/***********************************************************************/
/* CPLQuadTreeForceUseOfSubNodes() */
/************************************************************************/
/**
* Force the quadtree to insert as much as possible a feature whose bbox
* spread over multiple subnodes into those subnodes, rather than in the
* list of features attached to the node.
*
* @param hQuadTree the quad tree
*/
void CPLQuadTreeForceUseOfSubNodes(CPLQuadTree *hQuadTree)
{
hQuadTree->bForceUseOfSubNodes = true;
}
/************************************************************************/
/* CPLQuadTreeInsert() */
/************************************************************************/
/**
* Insert a feature into a quadtree
*
* @param hQuadTree the quad tree
* @param hFeature the feature to insert
*/
void CPLQuadTreeInsert(CPLQuadTree *hQuadTree, void *hFeature)
{
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"hQuadTree->pfnGetBounds == NULL");
return;
}
hQuadTree->nFeatures++;
CPLRectObj bounds;
if (hQuadTree->pfnGetBoundsEx)
hQuadTree->pfnGetBoundsEx(hFeature, hQuadTree->pUserData, &bounds);
else
hQuadTree->pfnGetBounds(hFeature, &bounds);
CPLQuadTreeAddFeatureInternal(hQuadTree, hFeature, &bounds);
}
/************************************************************************/
/* CPLQuadTreeInsertWithBounds() */
/************************************************************************/
/**
* Insert a feature into a quadtree
*
* @param hQuadTree the quad tree
* @param hFeature the feature to insert
* @param psBounds bounds of the feature
*/
void CPLQuadTreeInsertWithBounds(CPLQuadTree *hQuadTree, void *hFeature,
const CPLRectObj *psBounds)
{
hQuadTree->nFeatures++;
CPLQuadTreeAddFeatureInternal(hQuadTree, hFeature, psBounds);
}
/************************************************************************/
/* CPLQuadTreeRemove() */
/************************************************************************/
static bool CPLQuadTreeRemoveInternal(QuadTreeNode *psNode, void *hFeature,
const CPLRectObj *psBounds)
{
bool bRemoved = false;
for (int i = 0; i < psNode->nFeatures; i++)
{
if (psNode->pahFeatures[i] == hFeature)
{
if (i < psNode->nFeatures - 1)
{
memmove(psNode->pahFeatures + i, psNode->pahFeatures + i + 1,
(psNode->nFeatures - 1 - i) * sizeof(void *));
if (psNode->pasBounds)
{
memmove(psNode->pasBounds + i, psNode->pasBounds + i + 1,
(psNode->nFeatures - 1 - i) * sizeof(CPLRectObj));
}
}
bRemoved = true;
psNode->nFeatures--;
break;
}
}
if (psNode->nFeatures == 0 && psNode->pahFeatures != nullptr)
{
CPLFree(psNode->pahFeatures);
CPLFree(psNode->pasBounds);
psNode->pahFeatures = nullptr;
psNode->pasBounds = nullptr;
}
/* -------------------------------------------------------------------- */
/* Recurse to subnodes if they exist. */
/* -------------------------------------------------------------------- */
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (psNode->apSubNode[i] &&
CPL_RectOverlap(&(psNode->apSubNode[i]->rect), psBounds))
{
bRemoved |= CPLQuadTreeRemoveInternal(psNode->apSubNode[i],
hFeature, psBounds);
if (psNode->apSubNode[i]->nFeatures == 0 &&
psNode->apSubNode[i]->nNumSubNodes == 0)
{
CPLQuadTreeNodeDestroy(psNode->apSubNode[i]);
if (i < psNode->nNumSubNodes - 1)
{
memmove(psNode->apSubNode + i, psNode->apSubNode + i + 1,
(psNode->nNumSubNodes - 1 - i) *
sizeof(QuadTreeNode *));
}
i--;
psNode->nNumSubNodes--;
}
}
}
return bRemoved;
}
/**
* Remove a feature from a quadtree.
*
* Currently the quadtree is not re-balanced.
*
* @param hQuadTree the quad tree
* @param hFeature the feature to remove
* @param psBounds bounds of the feature (or NULL if pfnGetBounds has been
* filled)
*/
void CPLQuadTreeRemove(CPLQuadTree *hQuadTree, void *hFeature,
const CPLRectObj *psBounds)
{
if (psBounds == nullptr && hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
{
CPLError(CE_Failure, CPLE_AppDefined,
"hQuadTree->pfnGetBounds == NULL");
return;
}
CPLRectObj bounds; // keep variable in this outer scope
if (psBounds == nullptr)
{
if (hQuadTree->pfnGetBoundsEx)
hQuadTree->pfnGetBoundsEx(hFeature, hQuadTree->pUserData, &bounds);
else
hQuadTree->pfnGetBounds(hFeature, &bounds);
psBounds = &bounds;
}
if (CPLQuadTreeRemoveInternal(hQuadTree->psRoot, hFeature, psBounds))
{
hQuadTree->nFeatures--;
}
}
/************************************************************************/
/* CPLQuadTreeNodeDestroy() */
/************************************************************************/
static void CPLQuadTreeNodeDestroy(QuadTreeNode *psNode)
{
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (psNode->apSubNode[i])
CPLQuadTreeNodeDestroy(psNode->apSubNode[i]);
}
if (psNode->pahFeatures)
{
CPLFree(psNode->pahFeatures);
CPLFree(psNode->pasBounds);
}
CPLFree(psNode);
}
/************************************************************************/
/* CPLQuadTreeDestroy() */
/************************************************************************/
/**
* Destroy a quadtree
*
* @param hQuadTree the quad tree to destroy
*/
void CPLQuadTreeDestroy(CPLQuadTree *hQuadTree)
{
CPLAssert(hQuadTree);
CPLQuadTreeNodeDestroy(hQuadTree->psRoot);
CPLFree(hQuadTree);
}
/************************************************************************/
/* CPLQuadTreeSplitBounds() */
/************************************************************************/
static void CPLQuadTreeSplitBounds(double dfSplitRatio, const CPLRectObj *in,
CPLRectObj *out1, CPLRectObj *out2)
{
/* -------------------------------------------------------------------- */
/* The output bounds will be very similar to the input bounds, */
/* so just copy over to start. */
/* -------------------------------------------------------------------- */
memcpy(out1, in, sizeof(CPLRectObj));
memcpy(out2, in, sizeof(CPLRectObj));
/* -------------------------------------------------------------------- */
/* Split in X direction. */
/* -------------------------------------------------------------------- */
if ((in->maxx - in->minx) > (in->maxy - in->miny))
{
const double range = in->maxx - in->minx;
out1->maxx = in->minx + range * dfSplitRatio;
out2->minx = in->maxx - range * dfSplitRatio;
}
/* -------------------------------------------------------------------- */
/* Otherwise split in Y direction. */
/* -------------------------------------------------------------------- */
else
{
const double range = in->maxy - in->miny;
out1->maxy = in->miny + range * dfSplitRatio;
out2->miny = in->maxy - range * dfSplitRatio;
}
}
/************************************************************************/
/* CPLQuadTreeNodeAddFeatureAlg1() */
/************************************************************************/
static void CPLQuadTreeNodeAddFeatureAlg1(CPLQuadTree *hQuadTree,
QuadTreeNode *psNode, void *hFeature,
const CPLRectObj *pRect)
{
if (psNode->nNumSubNodes == 0)
{
// If we have reached the max bucket capacity, try to insert
// in a subnode if possible.
if (psNode->nFeatures >= hQuadTree->nBucketCapacity)
{
CPLRectObj half1 = {0.0, 0.0, 0.0, 0.0};
CPLRectObj half2 = {0.0, 0.0, 0.0, 0.0};
CPLRectObj quad1 = {0.0, 0.0, 0.0, 0.0};
CPLRectObj quad2 = {0.0, 0.0, 0.0, 0.0};
CPLRectObj quad3 = {0.0, 0.0, 0.0, 0.0};
CPLRectObj quad4 = {0.0, 0.0, 0.0, 0.0};
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &psNode->rect,
&half1, &half2);
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &half1, &quad1,
&quad2);
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &half2, &quad3,
&quad4);
if (memcmp(&psNode->rect, &quad1, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad2, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad3, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad4, sizeof(CPLRectObj)) != 0 &&
(hQuadTree->bForceUseOfSubNodes ||
CPL_RectContained(pRect, &quad1) ||
CPL_RectContained(pRect, &quad2) ||
CPL_RectContained(pRect, &quad3) ||
CPL_RectContained(pRect, &quad4)))
{
psNode->nNumSubNodes = 4;
psNode->apSubNode[0] = CPLQuadTreeNodeCreate(&quad1);
psNode->apSubNode[1] = CPLQuadTreeNodeCreate(&quad2);
psNode->apSubNode[2] = CPLQuadTreeNodeCreate(&quad3);
psNode->apSubNode[3] = CPLQuadTreeNodeCreate(&quad4);
const int oldNumFeatures = psNode->nFeatures;
void **oldFeatures = psNode->pahFeatures;
CPLRectObj *pasOldBounds = psNode->pasBounds;
psNode->nFeatures = 0;
psNode->pahFeatures = nullptr;
psNode->pasBounds = nullptr;
// Redispatch existing pahFeatures in apSubNodes.
for (int i = 0; i < oldNumFeatures; i++)
{
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree, psNode,
oldFeatures[i],
&pasOldBounds[i]);
else
{
CPLRectObj bounds;
if (hQuadTree->pfnGetBoundsEx)
hQuadTree->pfnGetBoundsEx(
oldFeatures[i], hQuadTree->pUserData, &bounds);
else
hQuadTree->pfnGetBounds(oldFeatures[i], &bounds);
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree, psNode,
oldFeatures[i], &bounds);
}
}
CPLFree(oldFeatures);
CPLFree(pasOldBounds);
/* recurse back on this psNode now that it has apSubNodes */
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree, psNode, hFeature,
pRect);
return;
}
}
}
else
{
/* --------------------------------------------------------------------
*/
/* If there are apSubNodes, then consider whether this object */
/* will fit in them. */
/* --------------------------------------------------------------------
*/
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (CPL_RectContained(pRect, &psNode->apSubNode[i]->rect))
{
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree, psNode->apSubNode[i],
hFeature, pRect);
return;
}
}
if (hQuadTree->bForceUseOfSubNodes)
{
bool overlaps[4];
bool overlapAll = true;
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
overlaps[i] =
CPL_RectOverlap(pRect, &psNode->apSubNode[i]->rect);
if (!overlaps[i])
overlapAll = false;
}
if (!overlapAll)
{
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (overlaps[i])
{
CPLRectObj intersection;
intersection.minx = std::max(
pRect->minx, psNode->apSubNode[i]->rect.minx);
intersection.miny = std::max(
pRect->miny, psNode->apSubNode[i]->rect.miny);
intersection.maxx = std::min(
pRect->maxx, psNode->apSubNode[i]->rect.maxx);
intersection.maxy = std::min(
pRect->maxy, psNode->apSubNode[i]->rect.maxy);
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree,
psNode->apSubNode[i],
hFeature, &intersection);
}
}
return;
}
}
}
/* -------------------------------------------------------------------- */
/* If none of that worked, just add it to this psNodes list. */
/* -------------------------------------------------------------------- */
psNode->nFeatures++;
if (psNode->nFeatures == 1)
{
CPLAssert(psNode->pahFeatures == nullptr);
psNode->pahFeatures = static_cast<void **>(
CPLMalloc(hQuadTree->nBucketCapacity * sizeof(void *)));
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
psNode->pasBounds = static_cast<CPLRectObj *>(
CPLMalloc(hQuadTree->nBucketCapacity * sizeof(CPLRectObj)));
}
else if (psNode->nFeatures > hQuadTree->nBucketCapacity)
{
psNode->pahFeatures = static_cast<void **>(CPLRealloc(
psNode->pahFeatures, sizeof(void *) * psNode->nFeatures));
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
psNode->pasBounds = static_cast<CPLRectObj *>(CPLRealloc(
psNode->pasBounds, sizeof(CPLRectObj) * psNode->nFeatures));
}
psNode->pahFeatures[psNode->nFeatures - 1] = hFeature;
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
psNode->pasBounds[psNode->nFeatures - 1] = *pRect;
return;
}
/************************************************************************/
/* CPLQuadTreeNodeAddFeatureAlg2() */
/************************************************************************/
static void CPLQuadTreeNodeAddFeatureAlg2(CPLQuadTree *hQuadTree,
QuadTreeNode *psNode, void *hFeature,
const CPLRectObj *pRect,
int nMaxDepth)
{
/* -------------------------------------------------------------------- */
/* If there are apSubNodes, then consider whether this object */
/* will fit in them. */
/* -------------------------------------------------------------------- */
if (nMaxDepth > 1 && psNode->nNumSubNodes > 0)
{
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (CPL_RectContained(pRect, &psNode->apSubNode[i]->rect))
{
CPLQuadTreeNodeAddFeatureAlg2(hQuadTree, psNode->apSubNode[i],
hFeature, pRect, nMaxDepth - 1);
return;
}
}
}
/* -------------------------------------------------------------------- */
/* Otherwise, consider creating four apSubNodes if could fit into */
/* them, and adding to the appropriate apSubNode. */
/* -------------------------------------------------------------------- */
else if (nMaxDepth > 1 && psNode->nNumSubNodes == 0)
{
CPLRectObj half1, half2, quad1, quad2, quad3, quad4;
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &psNode->rect, &half1,
&half2);
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &half1, &quad1, &quad2);
CPLQuadTreeSplitBounds(hQuadTree->dfSplitRatio, &half2, &quad3, &quad4);
if (memcmp(&psNode->rect, &quad1, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad2, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad3, sizeof(CPLRectObj)) != 0 &&
memcmp(&psNode->rect, &quad4, sizeof(CPLRectObj)) != 0 &&
(CPL_RectContained(pRect, &quad1) ||
CPL_RectContained(pRect, &quad2) ||
CPL_RectContained(pRect, &quad3) ||
CPL_RectContained(pRect, &quad4)))
{
psNode->nNumSubNodes = 4;
psNode->apSubNode[0] = CPLQuadTreeNodeCreate(&quad1);
psNode->apSubNode[1] = CPLQuadTreeNodeCreate(&quad2);
psNode->apSubNode[2] = CPLQuadTreeNodeCreate(&quad3);
psNode->apSubNode[3] = CPLQuadTreeNodeCreate(&quad4);
/* recurse back on this psNode now that it has apSubNodes */
CPLQuadTreeNodeAddFeatureAlg2(hQuadTree, psNode, hFeature, pRect,
nMaxDepth);
return;
}
}
/* -------------------------------------------------------------------- */
/* If none of that worked, just add it to this psNodes list. */
/* -------------------------------------------------------------------- */
psNode->nFeatures++;
psNode->pahFeatures = static_cast<void **>(
CPLRealloc(psNode->pahFeatures, sizeof(void *) * psNode->nFeatures));
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
{
psNode->pasBounds = static_cast<CPLRectObj *>(CPLRealloc(
psNode->pasBounds, sizeof(CPLRectObj) * psNode->nFeatures));
}
psNode->pahFeatures[psNode->nFeatures - 1] = hFeature;
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
{
psNode->pasBounds[psNode->nFeatures - 1] = *pRect;
}
}
/************************************************************************/
/* CPLQuadTreeAddFeatureInternal() */
/************************************************************************/
static void CPLQuadTreeAddFeatureInternal(CPLQuadTree *hQuadTree,
void *hFeature,
const CPLRectObj *pRect)
{
if (hQuadTree->nMaxDepth == 0)
{
CPLQuadTreeNodeAddFeatureAlg1(hQuadTree, hQuadTree->psRoot, hFeature,
pRect);
}
else
{
CPLQuadTreeNodeAddFeatureAlg2(hQuadTree, hQuadTree->psRoot, hFeature,
pRect, hQuadTree->nMaxDepth);
}
}
/************************************************************************/
/* CPLQuadTreeCollectFeatures() */
/************************************************************************/
static void CPLQuadTreeCollectFeatures(const CPLQuadTree *hQuadTree,
const QuadTreeNode *psNode,
const CPLRectObj *pAoi,
int *pnFeatureCount, int *pnMaxFeatures,
void ***pppFeatureList)
{
/* -------------------------------------------------------------------- */
/* Does this psNode overlap the area of interest at all? If not, */
/* return without adding to the list at all. */
/* -------------------------------------------------------------------- */
if (!CPL_RectOverlap(&psNode->rect, pAoi))
return;
/* -------------------------------------------------------------------- */
/* Grow the list to hold the features on this psNode. */
/* -------------------------------------------------------------------- */
if (*pnFeatureCount + psNode->nFeatures > *pnMaxFeatures)
{
// TODO(schwehr): Symbolic constant.
*pnMaxFeatures = (*pnFeatureCount + psNode->nFeatures) * 2 + 20;
*pppFeatureList = static_cast<void **>(
CPLRealloc(*pppFeatureList, sizeof(void *) * *pnMaxFeatures));
}
/* -------------------------------------------------------------------- */
/* Add the local features to the list. */
/* -------------------------------------------------------------------- */
for (int i = 0; i < psNode->nFeatures; i++)
{
if (hQuadTree->pfnGetBounds == nullptr &&
hQuadTree->pfnGetBoundsEx == nullptr)
{
if (CPL_RectOverlap(&psNode->pasBounds[i], pAoi))
(*pppFeatureList)[(*pnFeatureCount)++] = psNode->pahFeatures[i];
}
else
{
CPLRectObj bounds;
if (hQuadTree->pfnGetBoundsEx)
hQuadTree->pfnGetBoundsEx(psNode->pahFeatures[i],
hQuadTree->pUserData, &bounds);
else
hQuadTree->pfnGetBounds(psNode->pahFeatures[i], &bounds);
if (CPL_RectOverlap(&bounds, pAoi))
(*pppFeatureList)[(*pnFeatureCount)++] = psNode->pahFeatures[i];
}
}
/* -------------------------------------------------------------------- */
/* Recurse to subnodes if they exist. */
/* -------------------------------------------------------------------- */
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (psNode->apSubNode[i])
CPLQuadTreeCollectFeatures(hQuadTree, psNode->apSubNode[i], pAoi,
pnFeatureCount, pnMaxFeatures,
pppFeatureList);
}
}
/************************************************************************/
/* CPLQuadTreeSearch() */
/************************************************************************/
/**
* Returns all the elements inserted whose bounding box intersects the
* provided area of interest
*
* @param hQuadTree the quad tree
* @param pAoi the pointer to the area of interest
* @param pnFeatureCount the user data provided to the function.
*
* @return an array of features that must be freed with CPLFree
*/
void **CPLQuadTreeSearch(const CPLQuadTree *hQuadTree, const CPLRectObj *pAoi,
int *pnFeatureCount)
{
CPLAssert(hQuadTree);
CPLAssert(pAoi);
int nFeatureCount = 0;
if (pnFeatureCount == nullptr)
pnFeatureCount = &nFeatureCount;
*pnFeatureCount = 0;
int nMaxFeatures = 0;
void **ppFeatureList = nullptr;
CPLQuadTreeCollectFeatures(hQuadTree, hQuadTree->psRoot, pAoi,
pnFeatureCount, &nMaxFeatures, &ppFeatureList);
return ppFeatureList;
}
/************************************************************************/
/* CPLQuadTreeNodeForeach() */
/************************************************************************/
static bool CPLQuadTreeNodeForeach(const QuadTreeNode *psNode,
CPLQuadTreeForeachFunc pfnForeach,
void *pUserData)
{
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
if (!CPLQuadTreeNodeForeach(psNode->apSubNode[i], pfnForeach,
pUserData))
return false;
}
for (int i = 0; i < psNode->nFeatures; i++)
{
if (pfnForeach(psNode->pahFeatures[i], pUserData) == FALSE)
return false;
}
return true;
}
/************************************************************************/
/* CPLQuadTreeForeach() */
/************************************************************************/
/**
* Walk through the quadtree and runs the provided function on all the
* elements
*
* This function is provided with the user_data argument of pfnForeach.
* It must return TRUE to go on the walk through the hash set, or FALSE to
* make it stop.
*
* Note : the structure of the quadtree must *NOT* be modified during the
* walk.
*
* @param hQuadTree the quad tree
* @param pfnForeach the function called on each element.
* @param pUserData the user data provided to the function.
*/
void CPLQuadTreeForeach(const CPLQuadTree *hQuadTree,
CPLQuadTreeForeachFunc pfnForeach, void *pUserData)
{
CPLAssert(hQuadTree);
CPLAssert(pfnForeach);
CPLQuadTreeNodeForeach(hQuadTree->psRoot, pfnForeach, pUserData);
}
/************************************************************************/
/* CPLQuadTreeDumpNode() */
/************************************************************************/
static void CPLQuadTreeDumpNode(const QuadTreeNode *psNode, int nIndentLevel,
CPLQuadTreeDumpFeatureFunc pfnDumpFeatureFunc,
void *pUserData)
{
if (psNode->nNumSubNodes)
{
for (int count = nIndentLevel; --count >= 0;)
{
printf(" "); /*ok*/
}
printf("SubhQuadTrees :\n"); /*ok*/
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
for (int count = nIndentLevel + 1; --count >= 0;)
{
printf(" "); /*ok*/
}
printf("SubhQuadTree %d :\n", i + 1); /*ok*/
CPLQuadTreeDumpNode(psNode->apSubNode[i], nIndentLevel + 2,
pfnDumpFeatureFunc, pUserData);
}
}
if (psNode->nFeatures)
{
for (int count = nIndentLevel; --count >= 0;)
printf(" "); /*ok*/
printf("Leaves (%d):\n", psNode->nFeatures); /*ok*/
for (int i = 0; i < psNode->nFeatures; i++)
{
if (pfnDumpFeatureFunc)
{
pfnDumpFeatureFunc(psNode->pahFeatures[i], nIndentLevel + 2,
pUserData);
}
else
{
for (int count = nIndentLevel + 1; --count >= 0;)
{
printf(" "); /*ok*/
}
printf("%p\n", psNode->pahFeatures[i]); /*ok*/
}
}
}
}
/************************************************************************/
/* CPLQuadTreeDump() */
/************************************************************************/
/** Dump quad tree */
void CPLQuadTreeDump(const CPLQuadTree *hQuadTree,
CPLQuadTreeDumpFeatureFunc pfnDumpFeatureFunc,
void *pUserData)
{
CPLQuadTreeDumpNode(hQuadTree->psRoot, 0, pfnDumpFeatureFunc, pUserData);
}
/************************************************************************/
/* CPLQuadTreeGetStatsNode() */
/************************************************************************/
static void CPLQuadTreeGetStatsNode(const QuadTreeNode *psNode, int nDepthLevel,
int *pnNodeCount, int *pnMaxDepth,
int *pnMaxBucketCapacity)
{
(*pnNodeCount)++;
if (nDepthLevel > *pnMaxDepth)
*pnMaxDepth = nDepthLevel;
if (psNode->nFeatures > *pnMaxBucketCapacity)
*pnMaxBucketCapacity = psNode->nFeatures;
for (int i = 0; i < psNode->nNumSubNodes; i++)
{
CPLQuadTreeGetStatsNode(psNode->apSubNode[i], nDepthLevel + 1,
pnNodeCount, pnMaxDepth, pnMaxBucketCapacity);
}
}
/************************************************************************/
/* CPLQuadTreeGetStats() */
/************************************************************************/
/** Get stats */
void CPLQuadTreeGetStats(const CPLQuadTree *hQuadTree, int *pnFeatureCount,
int *pnNodeCount, int *pnMaxDepth,
int *pnMaxBucketCapacity)
{
CPLAssert(hQuadTree);
int nFeatureCount = 0;
if (pnFeatureCount == nullptr)
pnFeatureCount = &nFeatureCount;
int nNodeCount = 0;
if (pnNodeCount == nullptr)
pnNodeCount = &nNodeCount;
int nMaxDepth = 0;
if (pnMaxDepth == nullptr)
pnMaxDepth = &nMaxDepth;
int nMaxBucketCapacity = 0;
if (pnMaxBucketCapacity == nullptr)
pnMaxBucketCapacity = &nMaxBucketCapacity;
*pnFeatureCount = hQuadTree->nFeatures;
*pnNodeCount = 0;
*pnMaxDepth = 1;
*pnMaxBucketCapacity = 0;
CPLQuadTreeGetStatsNode(hQuadTree->psRoot, 0, pnNodeCount, pnMaxDepth,
pnMaxBucketCapacity);
// TODO(schwehr): If any of the pointers were set to local vars,
// do they need to be reset to a nullptr?
}
|