소스 검색

adjust diglib to changed rtree

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@51800 15284696-431f-4ddb-bdfa-cd5b030d7da7
Markus Metz 13 년 전
부모
커밋
b717cc630e
2개의 변경된 파일211개의 추가작업 그리고 49개의 파일을 삭제
  1. 132 18
      lib/vector/diglib/spindex.c
  2. 79 31
      lib/vector/diglib/spindex_rw.c

+ 132 - 18
lib/vector/diglib/spindex.c

@@ -38,7 +38,9 @@ int dig_spidx_init(struct Plus_head *Plus)
 
     ndims = (Plus->with_z != 0) ? 3 : 2;
     Plus->spidx_with_z = (Plus->with_z != 0);
-    
+
+    G_debug(1, "dig_spidx_init(), %d dims", ndims);
+
     if (Plus->Spidx_file) {
 	int fd;
 	char *filename;
@@ -253,7 +255,14 @@ int
 dig_spidx_add_node(struct Plus_head *Plus, int node,
 		   double x, double y, double z)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_add_node(): node = %d, x,y,z = %f, %f, %f", node, x,
 	    y, z);
@@ -280,7 +289,14 @@ dig_spidx_add_node(struct Plus_head *Plus, int node,
  */
 int dig_spidx_add_line(struct Plus_head *Plus, int line, struct bound_box * box)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_add_line(): line = %d", line);
 
@@ -306,7 +322,14 @@ int dig_spidx_add_line(struct Plus_head *Plus, int line, struct bound_box * box)
  */
 int dig_spidx_add_area(struct Plus_head *Plus, int area, struct bound_box * box)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_add_area(): area = %d", area);
 
@@ -333,7 +356,14 @@ int dig_spidx_add_area(struct Plus_head *Plus, int area, struct bound_box * box)
 
 int dig_spidx_add_isle(struct Plus_head *Plus, int isle, struct bound_box * box)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_add_isle(): isle = %d", isle);
 
@@ -362,7 +392,14 @@ int dig_spidx_del_node(struct Plus_head *Plus, int node)
 {
     int ret;
     struct P_node *Node;
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_del_node(): node = %d", node);
 
@@ -395,9 +432,16 @@ int dig_spidx_del_node(struct Plus_head *Plus, int node)
  */
 int dig_spidx_del_line(struct Plus_head *Plus, int line, double x, double y, double z)
 {
-    struct P_line *Line;
-    struct RTree_Rect rect;
     int ret;
+    struct P_line *Line;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_del_line(): line = %d", line);
 
@@ -437,7 +481,14 @@ int dig_spidx_del_area(struct Plus_head *Plus, int area)
     struct P_line *Line;
     struct P_node *Node;
     struct P_topo_b *topo;
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_del_area(): area = %d", area);
 
@@ -483,7 +534,14 @@ int dig_spidx_del_isle(struct Plus_head *Plus, int isle)
     struct P_line *Line;
     struct P_node *Node;
     struct P_topo_b *topo;
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_spidx_del_isle(): isle = %d", isle);
 
@@ -565,7 +623,14 @@ int
 dig_select_nodes(struct Plus_head *Plus, const struct bound_box * box,
 		 struct ilist *list)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_select_nodes()");
 
@@ -604,8 +669,15 @@ static int _add_node(int id, struct RTree_Rect rect, int *node)
  */
 int dig_find_node(struct Plus_head *Plus, double x, double y, double z)
 {
-    struct RTree_Rect rect;
     int node;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_find_node()");
 
@@ -638,7 +710,14 @@ int dig_find_node(struct Plus_head *Plus, double x, double y, double z)
 int dig_select_lines(struct Plus_head *Plus, const struct bound_box *box,
 		      struct boxlist *list)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_select_lines_with_box()");
 
@@ -670,8 +749,15 @@ int dig_select_lines(struct Plus_head *Plus, const struct bound_box *box,
  */
 int dig_find_line_box(struct Plus_head *Plus, struct boxlist *list)
 {
-    struct RTree_Rect rect;
     int ret;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_find_line_box()");
 
@@ -706,7 +792,14 @@ int
 dig_select_areas(struct Plus_head *Plus, const struct bound_box * box,
 		 struct boxlist *list)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_select_areas_with_box()");
 
@@ -738,8 +831,15 @@ dig_select_areas(struct Plus_head *Plus, const struct bound_box * box,
  */
 int dig_find_area_box(struct Plus_head *Plus, struct boxlist *list)
 {
-    struct RTree_Rect rect;
     int ret;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_find_line_box()");
 
@@ -774,7 +874,14 @@ int
 dig_select_isles(struct Plus_head *Plus, const struct bound_box * box,
 		 struct boxlist *list)
 {
-    struct RTree_Rect rect;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_select_areas_with_box()");
 
@@ -806,8 +913,15 @@ dig_select_isles(struct Plus_head *Plus, const struct bound_box * box,
  */
 int dig_find_isle_box(struct Plus_head *Plus, struct boxlist *list)
 {
-    struct RTree_Rect rect;
     int ret;
+    static struct RTree_Rect rect;
+    static int rect_init = 0;
+
+    if (!rect_init) {
+	/* always 6 sides for 3D */
+	rect.boundary = G_malloc(6 * sizeof(RectReal));
+	rect_init = 6;
+    }
 
     G_debug(3, "dig_find_line_box()");
 

+ 79 - 31
lib/vector/diglib/spindex_rw.c

@@ -22,6 +22,26 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+/* TODO: only write out actually used sides */
+#ifndef NUMSIDES
+#define NUMSIDES 6
+#endif
+
+/* TODO: merge these two */
+struct spidxstack
+{
+    off_t pos[MAXCARD];		/* file position of child node, object ID on level 0 */
+    struct RTree_Node sn;	/* stack node */
+    int branch_id;		/* branch no to follow down */
+};
+
+struct spidxpstack
+{
+    off_t pos[MAXCARD];		/* file position of child node, object ID on level 0 */
+    struct RTree_Node *sn;	/* stack node pointer */
+    int branch_id;		/* branch no to follow down */
+};
+
 /*!
    \brief Write spatial index header to file
 
@@ -590,7 +610,13 @@ static int rtree_dump_branch_file(FILE * fp, struct RTree_Branch *b, int with_z,
 int rtree_dump_node_file(FILE * fp, off_t pos, int with_z, struct RTree *t)
 {
     int i;
-    struct RTree_Node n;
+    static struct RTree_Node n;
+    static int node_init = 0;
+    
+    if (!node_init) {
+	for (i = 0; i < MAXCARD; i++)
+	    RTreeNewRect(&(n.branch[i].rect), t);
+    }
 
     /* recursive nearly-but-a-bit-messy depth-first pre-order traversal
      * potentially filling up memory */
@@ -660,12 +686,7 @@ static off_t rtree_write_from_memory(struct gvfile *fp, off_t startpos,
     int sidx_nodesize, sidx_leafsize;
     struct RTree_Node *n;
     int i, j, writeout, maxcard;
-    struct spidxstack
-    {
-	off_t pos[MAXCARD];	/* file position of child node, object ID on level 0 */
-	struct RTree_Node *sn;	/* stack node */
-	int branch_id;		/* branch no to follow down */
-    } s[50];
+    struct spidxpstack s[MAXLEVEL];
     int top = 0;
 
     /* should be foolproof */
@@ -765,14 +786,21 @@ static off_t rtree_write_from_file(struct gvfile *fp, off_t startpos,
     int sidx_nodesize, sidx_leafsize;
     struct RTree_Node *n;
     int i, j, writeout, maxcard;
-    struct spidxstack
-    {
-	off_t pos[MAXCARD];	/* file position of child node, object ID on level 0 */
-	struct RTree_Node sn;	/* stack node */
-	int branch_id;		/* branch no to follow down */
-    } s[MAXLEVEL];
+    static struct spidxstack s[MAXLEVEL];
+    static int stack_init = 0;
     int top = 0;
     
+    if (!stack_init) {
+	for (i = 0; i < MAXLEVEL; i++) {
+	    for (j = 0; j < MAXCARD; j++) {
+		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
+	    }
+	}
+	stack_init = 1;
+    }
+
+
+
     /* write pending changes to file */
     RTreeFlushBuffer(t);
 
@@ -879,14 +907,20 @@ static void rtree_load_to_memory(struct gvfile *fp, off_t rootpos,
 {
     struct RTree_Node *newnode = NULL;
     int i, j, loadnode, maxcard;
-    struct spidxstack
-    {
-	off_t pos[MAXCARD];	/* file position of child node, object ID on level 0 */
-	struct RTree_Node sn;	/* stack node */
-	int branch_id;		/* branch no to follow down */
-    } s[50], *last;
+    struct spidxstack *last;
+    static struct spidxstack s[MAXLEVEL];
+    static int stack_init = 0;
     int top = 0;
 
+    if (!stack_init) {
+	for (i = 0; i < MAXLEVEL; i++) {
+	    for (j = 0; j < MAXCARD; j++) {
+		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
+	    }
+	}
+	stack_init = 1;
+    }
+
     /* stack size of t->rootlevel + 1 would be enough because of
      * depth-first postorder traversal:
      * only one node per level on stack at any given time */
@@ -1005,14 +1039,20 @@ static void rtree_load_to_file(struct gvfile *fp, off_t rootpos,
     struct RTree_Node newnode;
     off_t newnode_pos = -1;
     int i, j, loadnode, maxcard;
-    struct spidxstack
-    {
-	off_t pos[MAXCARD];	/* file position of child node, object ID on level 0 */
-	struct RTree_Node sn;	/* stack node */
-	int branch_id;		/* branch no to follow down */
-    } s[MAXLEVEL], *last;
+    struct spidxstack *last;
+    static struct spidxstack s[MAXLEVEL];
+    static int stack_init = 0;
     int top = 0;
 
+    if (!stack_init) {
+	for (i = 0; i < MAXLEVEL; i++) {
+	    for (j = 0; j < MAXCARD; j++) {
+		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
+	    }
+	}
+	stack_init = 1;
+    }
+	
     /* stack size of t->rootlevel + 1 would be enough because of
      * depth-first postorder traversal:
      * only one node per level on stack at any given time */
@@ -1284,17 +1324,25 @@ int rtree_search(struct RTree *t, struct RTree_Rect *r, SearchHitCallback shcb,
 {
     int hitCount = 0, found, maxcard;
     int i, j;
-    struct spidxstack
-    {
-	off_t pos[MAXCARD];	/* file position of child node, object ID on level 0 */
-	struct RTree_Node sn;	        /* stack node */
-	int branch_id;		/* branch no to follow down */
-    } s[50], *last;
+    struct spidxstack *last;
+    static struct spidxstack s[MAXLEVEL];
+    static int stack_init = 0;
     int top = 0;
 
     assert(r);
     assert(t);
 
+    if (!stack_init) {
+	struct Rect_Real *r;
+
+	for (i = 0; i < MAXLEVEL; i++) {
+	    for (j = 0; j < MAXCARD; j++) {
+		s[i].sn.branch[j].rect.boundary = G_malloc(6 * sizeof(RectReal));
+	    }
+	}
+	stack_init = 1;
+    }
+
     /* stack size of t->rootlevel + 1 is enough because of depth first search */
     /* only one node per level on stack at any given time */