瀏覽代碼

rtree minor bug fixes

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@39148 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 15 年之前
父節點
當前提交
888b110b13
共有 3 個文件被更改,包括 26 次插入10 次删除
  1. 9 2
      lib/vector/rtree/index.c
  2. 16 6
      lib/vector/rtree/node.c
  3. 1 2
      lib/vector/rtree/split.c

+ 9 - 2
lib/vector/rtree/index.c

@@ -189,7 +189,10 @@ static int RTreeInsertRect2(struct Rect *r, union Child child, int level,
 	/* child field of leaves contains tid of data record */
 	/* child field of leaves contains tid of data record */
 	b.child = child;
 	b.child = child;
 	/* add branch, may split node or remove branches */
 	/* add branch, may split node or remove branches */
-	cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
+	if (top)
+	    cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
+	else 
+	    cover = NULL;
 	result = RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
 	result = RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
 	/* update node count */
 	/* update node count */
 	if (result == 1) {
 	if (result == 1) {
@@ -222,7 +225,10 @@ static int RTreeInsertRect2(struct Rect *r, union Child child, int level,
 	    b.rect = RTreeNodeCover(b.child.ptr, t);
 	    b.rect = RTreeNodeCover(b.child.ptr, t);
 
 
 	    /* add branch, may split node or remove branches */
 	    /* add branch, may split node or remove branches */
-	    cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
+	    if (top)
+		cover = &(s[top - 1].sn->branch[s[top - 1].branch_id].rect);
+	    else
+		cover = NULL;
 	    result =
 	    result =
 		RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
 		RTreeAddBranch(&b, s[top].sn, &n2, ee, cover, overflow, t);
 
 
@@ -510,6 +516,7 @@ static int RTreeDeleteRect1(struct Rect *r, union Child child,
 	    }
 	    }
 	    t->root = n->branch[i].child.ptr;
 	    t->root = n->branch[i].child.ptr;
 	    RTreeFreeNode(n);
 	    RTreeFreeNode(n);
+	    t->n_levels--;
 	}
 	}
 	return 0;
 	return 0;
     }
     }

+ 16 - 6
lib/vector/rtree/node.c

@@ -22,7 +22,6 @@
 #include "index.h"
 #include "index.h"
 #include "card.h"
 #include "card.h"
 
 
-
 /* rectangle distances for forced removal */
 /* rectangle distances for forced removal */
 struct dist
 struct dist
 {
 {
@@ -433,6 +432,9 @@ static void RTreeRemoveBranches(struct Node *n, struct Branch *b,
     RectReal center_n[NUMDIMS], center_r, delta;
     RectReal center_n[NUMDIMS], center_r, delta;
     struct Branch branchbuf[MAXCARD + 1];
     struct Branch branchbuf[MAXCARD + 1];
     struct dist rdist[MAXCARD + 1];
     struct dist rdist[MAXCARD + 1];
+    struct Rect new_cover;
+
+    assert(cover);
 
 
     if ((n)->level > 0) {
     if ((n)->level > 0) {
 	maxkids = t->nodecard;
 	maxkids = t->nodecard;
@@ -442,13 +444,14 @@ static void RTreeRemoveBranches(struct Node *n, struct Branch *b,
 	maxkids = t->leafcard;
 	maxkids = t->leafcard;
 	is_node = 0;
 	is_node = 0;
     }
     }
-    maxkids = (n)->level > 0 ? t->nodecard : t->leafcard;
 
 
     assert(n->count == maxkids);	/* must be full */
     assert(n->count == maxkids);	/* must be full */
 
 
+    new_cover = RTreeCombineRect(cover, &(b->rect), t);
+
     /* center coords of node cover */
     /* center coords of node cover */
     for (j = 0; j < t->ndims; j++) {
     for (j = 0; j < t->ndims; j++) {
-	center_n[j] = (cover->boundary[j + NUMDIMS] + cover->boundary[j]) / 2;
+	center_n[j] = (new_cover.boundary[j + NUMDIMS] + new_cover.boundary[j]) / 2;
     }
     }
 
 
     /* compute distances of child rectangle centers to node cover center */
     /* compute distances of child rectangle centers to node cover center */
@@ -458,8 +461,8 @@ static void RTreeRemoveBranches(struct Node *n, struct Branch *b,
 	rdist[i].id = i;
 	rdist[i].id = i;
 	for (j = 0; j < t->ndims; j++) {
 	for (j = 0; j < t->ndims; j++) {
 	    center_r =
 	    center_r =
-		(n->branch[i].rect.boundary[j + NUMDIMS] +
-		 n->branch[i].rect.boundary[j]) / 2;
+		(branchbuf[i].rect.boundary[j + NUMDIMS] +
+		 branchbuf[i].rect.boundary[j]) / 2;
 	    delta = center_n[j] - center_r;
 	    delta = center_n[j] - center_r;
 	    rdist[i].distance += delta * delta;
 	    rdist[i].distance += delta * delta;
 	}
 	}
@@ -470,7 +473,14 @@ static void RTreeRemoveBranches(struct Node *n, struct Branch *b,
     /* new branch */
     /* new branch */
     branchbuf[maxkids] = *b;
     branchbuf[maxkids] = *b;
     rdist[maxkids].distance = 0;
     rdist[maxkids].distance = 0;
-    rdist[maxkids].id = i;
+    for (j = 0; j < t->ndims; j++) {
+	center_r =
+	    (b->rect.boundary[j + NUMDIMS] +
+	     b->rect.boundary[j]) / 2;
+	delta = center_n[j] - center_r;
+	rdist[maxkids].distance += delta * delta;
+    }
+    rdist[maxkids].id = maxkids;
 
 
     /* quicksort dist */
     /* quicksort dist */
     RTreeQuicksortDist(rdist, maxkids);
     RTreeQuicksortDist(rdist, maxkids);

+ 1 - 2
lib/vector/rtree/split.c

@@ -464,7 +464,7 @@ static void RTreeQuicksortBranchBuf(int side)
 static void RTreeMethodOne(struct PartitionVars *p, int minfill,
 static void RTreeMethodOne(struct PartitionVars *p, int minfill,
 			   struct RTree *t)
 			   struct RTree *t)
 {
 {
-    int i, j, k, l, s, maxkids, first_time = 1;
+    int i, j, k, l, s, maxkids;
     int axis = 0, best_axis = 0, side = 0, best_side[NUMDIMS];
     int axis = 0, best_axis = 0, side = 0, best_side[NUMDIMS];
     int best_cut[NUMDIMS];
     int best_cut[NUMDIMS];
     RectReal margin, smallest_margin = 0;
     RectReal margin, smallest_margin = 0;
@@ -530,7 +530,6 @@ static void RTreeMethodOne(struct PartitionVars *p, int minfill,
 		if (margin <= smallest_margin) {
 		if (margin <= smallest_margin) {
 		    smallest_margin = margin;
 		    smallest_margin = margin;
 		    best_axis = i;
 		    best_axis = i;
-		    first_time = 0;
 		}
 		}
 
 
 		/* remember best distribution for this axis */
 		/* remember best distribution for this axis */