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
|
/****************************************************************************
* MODULE: R-Tree library
*
* AUTHOR(S): Antonin Guttman - original code
* Daniel Green (green@superliminal.com) - major clean-up
* and implementation of bounding spheres
*
* PURPOSE: Multidimensional index
*
* COPYRIGHT: (C) 2001 by the GRASS Development Team
*
* This program is free software under the GNU General Public
* License (>=v2). Read the file COPYING that comes with GRASS
* for details.
*****************************************************************************/
#define SPLIT_QC
#include <stdio.h>
#include "assert.h"
#include "index.h"
#include "card.h"
#include "split_q.h"
/*-----------------------------------------------------------------------------
| Load branch buffer with branches from full node plus the extra branch.
-----------------------------------------------------------------------------*/
static void RTreeGetBranches(struct Node *n, struct Branch *b)
{
register int i;
assert(n);
assert(b);
/* load the branch buffer */
for (i=0; i<MAXKIDS(n); i++)
{
assert(n->branch[i].child); /* n should have every entry full */
BranchBuf[i] = n->branch[i];
}
BranchBuf[MAXKIDS(n)] = *b;
BranchCount = MAXKIDS(n) + 1;
/* calculate rect containing all in the set */
CoverSplit = BranchBuf[0].rect;
for (i=1; i<MAXKIDS(n)+1; i++)
{
CoverSplit = RTreeCombineRect(&CoverSplit, &BranchBuf[i].rect);
}
CoverSplitArea = RTreeRectSphericalVolume(&CoverSplit);
RTreeInitNode(n);
}
/*-----------------------------------------------------------------------------
| Put a branch in one of the groups.
-----------------------------------------------------------------------------*/
static void RTreeClassify(int i, int group, struct PartitionVars *p)
{
assert(p);
assert(!p->taken[i]);
p->partition[i] = group;
p->taken[i] = TRUE;
if (p->count[group] == 0)
p->cover[group] = BranchBuf[i].rect;
else
p->cover[group] =
RTreeCombineRect(&BranchBuf[i].rect, &p->cover[group]);
p->area[group] = RTreeRectSphericalVolume(&p->cover[group]);
p->count[group]++;
}
/*-----------------------------------------------------------------------------
| Pick two rects from set to be the first elements of the two groups.
| Pick the two that waste the most area if covered by a single rectangle.
-----------------------------------------------------------------------------*/
static void RTreePickSeeds(struct PartitionVars *p)
{
register int i, j, seed0, seed1;
RectReal worst, waste, area[MAXCARD+1];
for (i=0; i<p->total; i++)
area[i] = RTreeRectSphericalVolume(&BranchBuf[i].rect);
worst = -CoverSplitArea - 1;
for (i=0; i<p->total-1; i++)
{
for (j=i+1; j<p->total; j++)
{
struct Rect one_rect;
one_rect = RTreeCombineRect(
&BranchBuf[i].rect,
&BranchBuf[j].rect);
waste = RTreeRectSphericalVolume(&one_rect) -
area[i] - area[j];
if (waste > worst)
{
worst = waste;
seed0 = i;
seed1 = j;
}
}
}
RTreeClassify(seed0, 0, p);
RTreeClassify(seed1, 1, p);
}
/*-----------------------------------------------------------------------------
| Copy branches from the buffer into two nodes according to the partition.
-----------------------------------------------------------------------------*/
static void RTreeLoadNodes(struct Node *n, struct Node *q,
struct PartitionVars *p)
{
register int i;
assert(n);
assert(q);
assert(p);
for (i=0; i<p->total; i++)
{
assert(p->partition[i] == 0 || p->partition[i] == 1);
if (p->partition[i] == 0)
RTreeAddBranch(&BranchBuf[i], n, NULL);
else if (p->partition[i] == 1)
RTreeAddBranch(&BranchBuf[i], q, NULL);
}
}
/*-----------------------------------------------------------------------------
| Initialize a PartitionVars structure.
-----------------------------------------------------------------------------*/
static void RTreeInitPVars(struct PartitionVars *p, int maxrects, int minfill)
{
register int i;
assert(p);
p->count[0] = p->count[1] = 0;
p->cover[0] = p->cover[1] = RTreeNullRect();
p->area[0] = p->area[1] = (RectReal)0;
p->total = maxrects;
p->minfill = minfill;
for (i=0; i<maxrects; i++)
{
p->taken[i] = FALSE;
p->partition[i] = -1;
}
}
/*-----------------------------------------------------------------------------
| Print out data for a partition from PartitionVars struct.
-----------------------------------------------------------------------------*/
static void RTreePrintPVars(struct PartitionVars *p)
{
register int i;
assert(p);
fprintf (stdout, "\npartition:\n");
for (i=0; i<p->total; i++)
{
fprintf (stdout, "%3d\t", i);
}
fprintf (stdout, "\n");
for (i=0; i<p->total; i++)
{
if (p->taken[i])
fprintf (stdout, " t\t");
else
fprintf (stdout, "\t");
}
fprintf (stdout, "\n");
for (i=0; i<p->total; i++)
{
fprintf (stdout, "%3d\t", p->partition[i]);
}
fprintf (stdout, "\n");
fprintf (stdout, "count[0] = %d area = %f\n", p->count[0], p->area[0]);
fprintf (stdout, "count[1] = %d area = %f\n", p->count[1], p->area[1]);
if (p->area[0] + p->area[1] > 0)
{
fprintf (stdout, "total area = %f effectiveness = %3.2f\n",
p->area[0] + p->area[1],
(float)CoverSplitArea / (p->area[0] + p->area[1]));
}
fprintf (stdout, "cover[0]:\n");
RTreePrintRect(&p->cover[0], 0);
fprintf (stdout, "cover[1]:\n");
RTreePrintRect(&p->cover[1], 0);
}
/*-----------------------------------------------------------------------------
| Method #0 for choosing a partition:
| As the seeds for the two groups, pick the two rects that would waste the
| most area if covered by a single rectangle, i.e. evidently the worst pair
| to have in the same group.
| Of the remaining, one at a time is chosen to be put in one of the two groups.
| The one chosen is the one with the greatest difference in area expansion
| depending on which group - the rect most strongly attracted to one group
| and repelled from the other.
| If one group gets too full (more would force other group to violate min
| fill requirement) then other group gets the rest.
| These last are the ones that can go in either group most easily.
-----------------------------------------------------------------------------*/
static void RTreeMethodZero(struct PartitionVars *p, int minfill)
{
register int i;
RectReal biggestDiff;
register int group, chosen, betterGroup;
assert(p);
RTreeInitPVars(p, BranchCount, minfill);
RTreePickSeeds(p);
while (p->count[0] + p->count[1] < p->total
&& p->count[0] < p->total - p->minfill
&& p->count[1] < p->total - p->minfill)
{
biggestDiff = (RectReal)-1.;
for (i=0; i<p->total; i++)
{
if (!p->taken[i])
{
struct Rect *r, rect_0, rect_1;
RectReal growth0, growth1, diff;
r = &BranchBuf[i].rect;
rect_0 = RTreeCombineRect(r, &p->cover[0]);
rect_1 = RTreeCombineRect(r, &p->cover[1]);
growth0 = RTreeRectSphericalVolume(
&rect_0)-p->area[0];
growth1 = RTreeRectSphericalVolume(
&rect_1)-p->area[1];
diff = growth1 - growth0;
if (diff >= 0)
group = 0;
else
{
group = 1;
diff = -diff;
}
if (diff > biggestDiff)
{
biggestDiff = diff;
chosen = i;
betterGroup = group;
}
else if (diff==biggestDiff &&
p->count[group]<p->count[betterGroup])
{
chosen = i;
betterGroup = group;
}
}
}
RTreeClassify(chosen, betterGroup, p);
}
/* if one group too full, put remaining rects in the other */
if (p->count[0] + p->count[1] < p->total)
{
if (p->count[0] >= p->total - p->minfill)
group = 1;
else
group = 0;
for (i=0; i<p->total; i++)
{
if (!p->taken[i])
RTreeClassify(i, group, p);
}
}
assert(p->count[0] + p->count[1] == p->total);
assert(p->count[0] >= p->minfill && p->count[1] >= p->minfill);
}
/*-----------------------------------------------------------------------------
| Split a node.
| Divides the nodes branches and the extra one between two nodes.
| Old node is one of the new ones, and one really new one is created.
| Tries more than one method for choosing a partition, uses best result.
-----------------------------------------------------------------------------*/
extern void RTreeSplitNode(struct Node *n, struct Branch *b, struct Node **nn)
{
register struct PartitionVars *p;
register int level;
assert(n);
assert(b);
/* load all the branches into a buffer, initialize old node */
level = n->level;
RTreeGetBranches(n, b);
/* find partition */
p = &Partitions[0];
/* Note: can't use MINFILL(n) below since n was cleared by GetBranches() */
RTreeMethodZero(p, level>0 ? MinNodeFill : MinLeafFill);
/*
* put branches from buffer into 2 nodes
* according to chosen partition
*/
*nn = RTreeNewNode();
(*nn)->level = n->level = level;
RTreeLoadNodes(n, *nn, p);
assert(n->count+(*nn)->count == p->total);
}
|