30 #define DBL_MAX 1.797693E308
42 maxkids =
t->nodecard;
44 for (i = 0; i < maxkids; i++) {
51 maxkids =
t->leafcard;
53 for (i = 0; i < maxkids; i++) {
60 t->BranchCount = maxkids + 1;
65 for (i = 1; i < maxkids + 1; i++) {
86 if (p->
count[group] == 0)
109 int i, j, seed0 = 0, seed1 = 0;
112 for (i = 0; i < p->
total; i++)
115 worst = -CoverSplitArea - 1;
116 for (i = 0; i < p->
total - 1; i++) {
117 for (j = i + 1; j < p->
total; j++) {
130 RTreeClassify(seed0, 0, p,
t);
131 RTreeClassify(seed1, 1, p,
t);
143 for (i = 0; i < p->
total; i++) {
168 for (i = 0; i < maxrects; i++) {
185 fprintf(stdout,
"\npartition:\n");
186 for (i = 0; i < p->
total; i++) {
187 fprintf(stdout,
"%3d\t", i);
189 fprintf(stdout,
"\n");
190 for (i = 0; i < p->
total; i++) {
192 fprintf(stdout,
" t\t");
194 fprintf(stdout,
"\t");
196 fprintf(stdout,
"\n");
197 for (i = 0; i < p->
total; i++) {
198 fprintf(stdout,
"%3d\t", p->
partition[i]);
200 fprintf(stdout,
"\n");
202 fprintf(stdout,
"count[0] = %d area = %f\n", p->
count[0], p->
area[0]);
203 fprintf(stdout,
"count[1] = %d area = %f\n", p->
count[1], p->
area[1]);
205 fprintf(stdout,
"total area = %f effectiveness = %3.2f\n",
207 (
float)CoverSplitArea / (p->
area[0] + p->
area[1]));
209 fprintf(stdout,
"cover[0]:\n");
212 fprintf(stdout,
"cover[1]:\n");
236 int group, chosen = 0, betterGroup = 0;
240 RTreePickSeeds(p, CoverSplitArea,
t);
242 rect_0 = &(
t->rect_0);
243 rect_1 = &(
t->rect_1);
249 for (i = 0; i < p->
total; i++) {
253 r = &(
t->BranchBuf[i].rect);
258 diff = growth1 - growth0;
266 if (diff > biggestDiff) {
271 else if (diff == biggestDiff &&
278 RTreeClassify(chosen, betterGroup, p,
t);
287 for (i = 0; i < p->
total; i++) {
289 RTreeClassify(i, group, p,
t);
332 static int RTreeBranchBufIsSorted(
int first,
int last,
int side,
337 for (i = first; i < last; i++) {
338 if (RTreeCompareBranches(&(
t->BranchBuf[i]), &(
t->BranchBuf[i + 1]),
349 static int RTreePartitionBranchBuf(
int first,
int last,
int side,
352 int pivot, mid = ((first + last) >> 1);
355 if (last - first == 1) {
356 if (RTreeCompareBranches(&(
t->BranchBuf[first]), &(
t->BranchBuf[last]),
358 RTreeSwapBranches(&(
t->BranchBuf[first]), &(
t->BranchBuf[last]),
t);
364 larger = pivot = mid;
366 if (RTreeCompareBranches(&(
t->BranchBuf[first]), &(
t->BranchBuf[mid]),
368 larger = pivot = first;
372 if (RTreeCompareBranches(&(
t->BranchBuf[larger]), &(
t->BranchBuf[last]),
376 if (RTreeCompareBranches(&(
t->BranchBuf[smaller]),
377 &(
t->BranchBuf[last]), side) == 1) {
383 RTreeSwapBranches(&(
t->BranchBuf[pivot]), &(
t->BranchBuf[last]),
t);
388 while (first < last) {
389 if (RTreeCompareBranches(&(
t->BranchBuf[first]), &(
t->BranchBuf[last]),
391 if (pivot != first) {
392 RTreeSwapBranches(&(
t->BranchBuf[pivot]),
393 &(
t->BranchBuf[first]),
t);
401 RTreeSwapBranches(&(
t->BranchBuf[pivot]), &(
t->BranchBuf[last]),
t);
410 static void RTreeQuicksortBranchBuf(
int side,
struct RTree *
t)
412 int pivot, first, last;
416 s_last[0] =
t->BranchCount - 1;
423 first = s_first[stacksize];
424 last = s_last[stacksize];
426 if (!RTreeBranchBufIsSorted(first, last, side,
t)) {
428 pivot = RTreePartitionBranchBuf(first, last, side,
t);
430 s_first[stacksize] = first;
431 s_last[stacksize] = pivot - 1;
434 s_first[stacksize] = pivot + 1;
435 s_last[stacksize] = last;
455 int maxkids,
struct RTree *
t)
458 int axis = 0, best_axis = 0, side = 0;
459 RectReal margin, smallest_margin = 0;
461 struct RTree_Rect *rect_0, *rect_1, *orect, *upperrect;
462 int minfill1 = minfill - 1;
463 RectReal overlap, vol, smallest_overlap = -1, smallest_vol = -1;
465 static int *best_cut =
NULL, *best_side =
NULL;
466 static int one_init = 0;
474 rect_0 = &(
t->rect_0);
475 rect_1 = &(
t->rect_1);
477 upperrect = &(
t->upperrect);
487 for (i = 0; i <
t->ndims; i++) {
498 RTreeQuicksortBranchBuf(i + s *
t->ndims_alloc,
t);
505 for (j = 1; j < minfill1; j++) {
506 r1 = &(
t->BranchBuf[j].rect);
508 r2 = &(
t->BranchBuf[maxkids - j].rect);
511 r2 = &(
t->BranchBuf[maxkids - minfill1].rect);
516 for (j = minfill1; j <
t->BranchCount - minfill; j++) {
518 r1 = &(
t->BranchBuf[j].rect);
522 for (k = j + 1; k <
t->BranchCount - minfill; k++) {
523 r2 = &(
t->BranchBuf[k].rect);
533 if (margin <= smallest_margin) {
534 smallest_margin = margin;
543 for (k = 0; k <
t->ndims; k++) {
559 l = k +
t->ndims_alloc;
572 if (overlap <= smallest_overlap) {
573 smallest_overlap = overlap;
578 else if (overlap == smallest_overlap) {
580 if (vol <= smallest_vol) {
591 if (best_axis != axis || best_side[best_axis] != side)
592 RTreeQuicksortBranchBuf(
593 best_axis + best_side[best_axis] *
t->ndims_alloc,
t);
595 best_cut[best_axis]++;
597 for (i = 0; i < best_cut[best_axis]; i++)
598 RTreeClassify(i, 0, p,
t);
600 for (i = best_cut[best_axis]; i <
t->BranchCount; i++)
601 RTreeClassify(i, 1, p,
t);
622 RTreeGetBranches(n,
b, &CoverSplitArea,
t);
630 RTreeMethodZero(&(
t->p),
MINFILL(level,
t), CoverSplitArea,
t);
636 (nn)->level = n->
level = level;
637 RTreeLoadNodes(n, nn, p,
t);
#define MAXKIDS(level, t)
#define MINFILL(level, t)
int RTreeAddBranch(struct RTree_Branch *, struct RTree_Node *, struct RTree_Node **, struct RTree_ListBranch **, struct RTree_Rect *, char *, struct RTree *)
RectReal RTreeRectSphericalVolume(struct RTree_Rect *, struct RTree *)
void RTreeNullRect(struct RTree_Rect *, struct RTree *)
void RTreeCopyBranch(struct RTree_Branch *, struct RTree_Branch *, struct RTree *)
RectReal RTreeRectVolume(struct RTree_Rect *, struct RTree *)
RectReal RTreeRectMargin(struct RTree_Rect *, 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 RTreeInitNode(struct RTree *t, struct RTree_Node *n, int type)
void RTreePrintRect(struct RTree_Rect *R, int depth, struct RTree *t)
void RTreeInitPVars(struct RTree_PartitionVars *p, int maxrects, int minfill, struct RTree *t)
void RTreeSplitNode(struct RTree_Node *n, struct RTree_Branch *b, struct RTree_Node *nn, struct RTree *t)
struct RTree_Branch * branch
struct RTree_Rect cover[2]
#define MAXLEVEL
Maximum verbosity level.