58 RTreeInitLeafBranch, RTreeInitNodeBranchM, RTreeInitNodeBranchF};
70 RTreeInitBranch[type](&(n->
branch[i]),
t);
118 for (i = 0; i <
MAXCARD; i++) {
137 int i, first_time = 1;
139 if ((n)->
level > 0) {
140 for (i = 0; i <
t->nodecard; i++) {
152 for (i = 0; i <
t->leafcard; i++) {
181 RectReal increase, bestIncr = -1, area, bestArea = 0;
182 int best = 0, bestoverlap;
185 bestoverlap =
t->nodecard + 1;
189 for (i = 0; i <
t->nodecard; i++) {
197 for (j = 0; j <
t->leafcard; j++) {
204 if (overlap < bestoverlap) {
206 bestoverlap = overlap;
210 else if (overlap == bestoverlap) {
212 if (increase < bestIncr) {
217 else if (increase == bestIncr && area < bestArea) {
238 int i, first_time = 1;
245 return RTreePickLeafBranch(
r, n,
t);
247 for (i = 0; i <
t->nodecard; i++) {
253 if (increase < bestIncr || first_time) {
259 else if (increase == bestIncr && area < bestArea) {
271 if ((n)->level > 0) {
272 assert(n && i >= 0 && i < t->nodecard);
275 RTreeInitNodeBranchM(&(n->
branch[i]),
t);
277 RTreeInitNodeBranchF(&(n->
branch[i]),
t);
280 assert(n && i >= 0 && i < t->leafcard);
282 RTreeInitLeafBranch(&(n->
branch[i]),
t);
295 for (i = 0; i < nodes; i++) {
317 static void RTreeSwapDist(
struct dist *a,
struct dist *
b)
329 static int RTreeDistIsSorted(
struct dist *d,
int first,
int last)
333 for (i = first; i < last; i++) {
334 if (d[i].distance > d[i + 1].distance)
344 static int RTreePartitionDist(
struct dist *d,
int first,
int last)
346 int pivot, mid = ((first + last) >> 1);
349 if (last - first == 1) {
350 if (d[first].distance > d[last].distance) {
351 RTreeSwapDist(&(d[first]), &(d[last]));
357 larger = pivot = mid;
359 if (d[first].distance > d[mid].distance) {
360 larger = pivot = first;
364 if (d[larger].distance > d[last].distance) {
367 if (d[smaller].distance > d[last].distance) {
373 RTreeSwapDist(&(d[pivot]), &(d[last]));
378 while (first < last) {
379 if (d[first].distance <= d[last].distance) {
380 if (pivot != first) {
381 RTreeSwapDist(&(d[pivot]), &(d[first]));
389 RTreeSwapDist(&(d[pivot]), &(d[last]));
399 static void RTreeQuicksortDist(
struct dist *d,
int n)
401 int pivot, first, last;
412 first = s_first[stacksize];
413 last = s_last[stacksize];
415 if (!RTreeDistIsSorted(d, first, last)) {
417 pivot = RTreePartitionDist(d, first, last);
419 s_first[stacksize] = first;
420 s_last[stacksize] = pivot - 1;
423 s_first[stacksize] = pivot + 1;
424 s_last[stacksize] = last;
455 l = RTreeNewListBranch(
t);
471 int i, j, maxkids, type;
473 struct dist rdist[
MAXCARD + 1];
488 for (j = 0; j <
t->ndims; j++) {
495 for (i = 0; i < maxkids; i++) {
497 rdist[i].distance = 0;
499 for (j = 0; j <
t->ndims; j++) {
500 center_r = (
t->BranchBuf[i].rect.boundary[j +
t->ndims_alloc] +
501 t->BranchBuf[i].rect.boundary[j]) /
503 delta = center_n[j] - center_r;
504 rdist[i].distance += delta * delta;
507 RTreeInitBranch[type](&(n->
branch[i]),
t);
512 rdist[maxkids].distance = 0;
513 for (j = 0; j <
t->ndims; j++) {
515 (
b->rect.boundary[j +
t->ndims_alloc] +
b->rect.boundary[j]) / 2;
516 delta = center_n[j] - center_r;
517 rdist[maxkids].distance += delta * delta;
519 rdist[maxkids].id = maxkids;
522 RTreeQuicksortDist(rdist, maxkids);
526 RTreeReInsertBranch(
t->BranchBuf[rdist[maxkids - i].id], n->
level, ee,
530 for (i = 0; i < maxkids -
FORCECARD + 1; i++) {
551 if (n->
count < maxkids) {
552 if ((n)->level > 0) {
553 for (i = 0; i < maxkids; i++) {
564 else if ((n)->level == 0) {
565 for (i = 0; i < maxkids; i++) {
578 if (n->
level <
t->rootlevel && overflow[n->
level]) {
580 RTreeRemoveBranches(n,
b, ee, cover,
t);
581 overflow[n->
level] = 0;
606 for (i = 0; i < depth; i++)
623 maxkids = (n->
level > 0 ?
t->nodecard :
t->leafcard);
625 fprintf(stdout,
"node");
627 fprintf(stdout,
" LEAF");
628 else if (n->
level > 0)
629 fprintf(stdout,
" NONLEAF");
631 fprintf(stdout,
" TYPE=?");
632 fprintf(stdout,
" level=%d count=%d", n->
level, n->
count);
634 for (i = 0; i < maxkids; i++) {
638 fprintf(stdout,
"\t%d: data id = %d\n", i, n->
branch[i].
child.
id);
642 fprintf(stdout,
"branch %d\n", i);
643 RTreePrintBranch(&(n->
branch[i]), depth + 1,
t);
#define MAXKIDS(level, t)
RectReal RTreeRectSphericalVolume(struct RTree_Rect *, struct RTree *)
void RTreeSplitNode(struct RTree_Node *, struct RTree_Branch *, struct RTree_Node *, struct RTree *)
void RTreeCombineRect(struct RTree_Rect *, struct RTree_Rect *, struct RTree_Rect *, struct RTree *)
int RTreeExpandRect(struct RTree_Rect *, struct RTree_Rect *, struct RTree *)
void RTreeInitRect(struct RTree_Rect *, struct RTree *)
Initialize a rectangle to have all 0 coordinates.
#define RTreeCopyRect(r1, r2, t)
#define assert(condition)
void RTreePrintNode(struct RTree_Node *n, int depth, struct RTree *t)
void RTreeCopyNode(struct RTree_Node *n1, struct RTree_Node *n2, struct RTree *t)
void RTreeCopyBranch(struct RTree_Branch *b1, struct RTree_Branch *b2, struct RTree *t)
void RTreeFreeNode(struct RTree_Node *n)
void RTreeDisconnectBranch(struct RTree_Node *n, int i, struct RTree *t)
void RTreeNodeCover(struct RTree_Node *n, struct RTree_Rect *r, struct RTree *t)
struct RTree_Node * RTreeAllocNode(struct RTree *t, int level)
void RTreeDestroyNode(struct RTree_Node *n, int nodes)
void RTreeTabIn(int depth)
int RTreeAddBranch(struct RTree_Branch *b, struct RTree_Node *n, struct RTree_Node **newnode, struct RTree_ListBranch **ee, struct RTree_Rect *cover, char *overflow, struct RTree *t)
int RTreePickBranch(struct RTree_Rect *r, struct RTree_Node *n, struct RTree *t)
void RTreeInitNode(struct RTree *t, struct RTree_Node *n, int type)
RectReal * RTreeAllocBoundary(struct RTree *t)
Allocate the boundary array of a rectangle for a given tree.
void RTreeFreeBoundary(struct RTree_Rect *r)
Delete the boundary of a rectangle.
int RTreeOverlap(struct RTree_Rect *r, struct RTree_Rect *s, struct RTree *t)
void RTreePrintRect(struct RTree_Rect *R, int depth, struct RTree *t)
struct RTree_Branch * branch