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++) {
182 RectReal increase, bestIncr = -1, area, bestArea = 0;
183 int best = 0, bestoverlap;
186 bestoverlap =
t->nodecard + 1;
190 for (i = 0; i <
t->nodecard; i++) {
198 for (j = 0; j <
t->leafcard; j++) {
205 if (overlap < bestoverlap) {
207 bestoverlap = overlap;
211 else if (overlap == bestoverlap) {
213 if (increase < bestIncr) {
218 else if (increase == bestIncr && area < bestArea) {
239 int i, first_time = 1;
246 return RTreePickLeafBranch(
r, n,
t);
248 for (i = 0; i <
t->nodecard; i++) {
254 if (increase < bestIncr || first_time) {
260 else if (increase == bestIncr && area < bestArea) {
272 if ((n)->level > 0) {
273 assert(n && i >= 0 && i < t->nodecard);
276 RTreeInitNodeBranchM(&(n->
branch[i]),
t);
278 RTreeInitNodeBranchF(&(n->
branch[i]),
t);
281 assert(n && i >= 0 && i < t->leafcard);
283 RTreeInitLeafBranch(&(n->
branch[i]),
t);
296 for (i = 0; i < nodes; i++) {
318 static void RTreeSwapDist(
struct dist *a,
struct dist *
b)
330 static int RTreeDistIsSorted(
struct dist *d,
int first,
int last)
334 for (i = first; i < last; i++) {
335 if (d[i].distance > d[i + 1].distance)
345 static int RTreePartitionDist(
struct dist *d,
int first,
int last)
347 int pivot, mid = ((first + last) >> 1);
350 if (last - first == 1) {
351 if (d[first].distance > d[last].distance) {
352 RTreeSwapDist(&(d[first]), &(d[last]));
358 larger = pivot = mid;
360 if (d[first].distance > d[mid].distance) {
361 larger = pivot = first;
365 if (d[larger].distance > d[last].distance) {
368 if (d[smaller].distance > d[last].distance) {
374 RTreeSwapDist(&(d[pivot]), &(d[last]));
379 while (first < last) {
380 if (d[first].distance <= d[last].distance) {
381 if (pivot != first) {
382 RTreeSwapDist(&(d[pivot]), &(d[first]));
390 RTreeSwapDist(&(d[pivot]), &(d[last]));
400 static void RTreeQuicksortDist(
struct dist *d,
int n)
402 int pivot, first, last;
413 first = s_first[stacksize];
414 last = s_last[stacksize];
416 if (!RTreeDistIsSorted(d, first, last)) {
418 pivot = RTreePartitionDist(d, first, last);
420 s_first[stacksize] = first;
421 s_last[stacksize] = pivot - 1;
424 s_first[stacksize] = pivot + 1;
425 s_last[stacksize] = last;
456 l = RTreeNewListBranch(
t);
472 int i, j, maxkids, type;
474 struct dist rdist[
MAXCARD + 1];
489 for (j = 0; j <
t->ndims; j++) {
496 for (i = 0; i < maxkids; i++) {
498 rdist[i].distance = 0;
500 for (j = 0; j <
t->ndims; j++) {
501 center_r = (
t->BranchBuf[i].rect.boundary[j +
t->ndims_alloc] +
502 t->BranchBuf[i].rect.boundary[j]) /
504 delta = center_n[j] - center_r;
505 rdist[i].distance += delta * delta;
508 RTreeInitBranch[type](&(n->
branch[i]),
t);
513 rdist[maxkids].distance = 0;
514 for (j = 0; j <
t->ndims; j++) {
516 (
b->rect.boundary[j +
t->ndims_alloc] +
b->rect.boundary[j]) / 2;
517 delta = center_n[j] - center_r;
518 rdist[maxkids].distance += delta * delta;
520 rdist[maxkids].id = maxkids;
523 RTreeQuicksortDist(rdist, maxkids);
527 RTreeReInsertBranch(
t->BranchBuf[rdist[maxkids - i].id], n->
level, ee,
531 for (i = 0; i < maxkids -
FORCECARD + 1; i++) {
552 if (n->
count < maxkids) {
553 if ((n)->level > 0) {
554 for (i = 0; i < maxkids; i++) {
565 else if ((n)->level == 0) {
566 for (i = 0; i < maxkids; i++) {
579 if (n->
level <
t->rootlevel && overflow[n->
level]) {
581 RTreeRemoveBranches(n,
b, ee, cover,
t);
582 overflow[n->
level] = 0;
608 for (i = 0; i < depth; i++)
625 maxkids = (n->
level > 0 ?
t->nodecard :
t->leafcard);
627 fprintf(stdout,
"node");
629 fprintf(stdout,
" LEAF");
630 else if (n->
level > 0)
631 fprintf(stdout,
" NONLEAF");
633 fprintf(stdout,
" TYPE=?");
634 fprintf(stdout,
" level=%d count=%d", n->
level, n->
count);
636 for (i = 0; i < maxkids; i++) {
640 fprintf(stdout,
"\t%d: data id = %d\n", i, n->
branch[i].
child.
id);
644 fprintf(stdout,
"branch %d\n", i);
645 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