[GRASS-SVN] r43626 - grass/branches/releasebranch_6_4/vector/v.voronoi

svn_grass at osgeo.org svn_grass at osgeo.org
Wed Sep 22 14:38:01 EDT 2010


Author: neteler
Date: 2010-09-22 18:38:00 +0000 (Wed, 22 Sep 2010)
New Revision: 43626

Modified:
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_edgelist.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_geometry.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_heap.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_main.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_memory.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_output.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/sw_voronoi.c
   grass/branches/releasebranch_6_4/vector/v.voronoi/vo_main.c
Log:
backport: much less memory, a bit faster, backport from trunk r40286

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_edgelist.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_edgelist.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_edgelist.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <grass/gis.h>
 #include "sw_defs.h"
 
 int ntry, totalsearch;
@@ -7,10 +8,10 @@
 {
     int i;
 
-    freeinit(&hfl, sizeof **ELhash);
+    freeinit(&hfl, sizeof(struct Halfedge));
     ELhashsize = 2 * sqrt_nsites;
-    ELhash = (struct Halfedge **)myalloc(sizeof *ELhash * ELhashsize);
-    for (i = 0; i < ELhashsize; i += 1)
+    ELhash = (struct Halfedge **)G_malloc(ELhashsize * sizeof(struct Halfedge *));
+    for (i = 0; i < ELhashsize; i++)
 	ELhash[i] = (struct Halfedge *)NULL;
     ELleftend = HEcreate((struct Edge *)NULL, 0);
     ELrightend = HEcreate((struct Edge *)NULL, 0);
@@ -62,7 +63,7 @@
 
     /* Hash table points to deleted half edge.  Patch as necessary. */
     ELhash[b] = (struct Halfedge *)NULL;
-    if ((he->ELrefcnt -= 1) == 0)
+    if ((--(he->ELrefcnt)) == 0)
 	makefree((struct Freenode *)he, &hfl);
     return ((struct Halfedge *)NULL);
 }
@@ -80,15 +81,15 @@
 	bucket = ELhashsize - 1;
     he = ELgethash(bucket);
     if (he == (struct Halfedge *)NULL) {
-	for (i = 1; 1; i += 1) {
+	for (i = 1; 1; i++) {
 	    if ((he = ELgethash(bucket - i)) != (struct Halfedge *)NULL)
 		break;
 	    if ((he = ELgethash(bucket + i)) != (struct Halfedge *)NULL)
 		break;
-	};
+	}
 	totalsearch += i;
-    };
-    ntry += 1;
+    }
+    ntry++;
     /* Now search linear list of halfedges for the corect one */
     if (he == ELleftend || (he != ELrightend && right_of(he, p))) {
 	do {
@@ -104,10 +105,10 @@
     /* Update hash table and reference counts */
     if (bucket > 0 && bucket < ELhashsize - 1) {
 	if (ELhash[bucket] != (struct Halfedge *)NULL)
-	    ELhash[bucket]->ELrefcnt -= 1;
+	    ELhash[bucket]->ELrefcnt--;
 	ELhash[bucket] = he;
-	ELhash[bucket]->ELrefcnt += 1;
-    };
+	ELhash[bucket]->ELrefcnt++;
+    }
     return (he);
 }
 

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_geometry.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_geometry.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_geometry.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -4,10 +4,9 @@
 
 int geominit(void)
 {
-    struct Edge e;
     double sn;
 
-    freeinit(&efl, sizeof e);
+    freeinit(&efl, sizeof(struct Edge));
     nvertices = 0;
     nedges = 0;
     sn = nsites + 4;
@@ -48,11 +47,11 @@
 	newedge->b = 1.0;
 	newedge->a = dx / dy;
 	newedge->c /= dy;
-    };
+    }
 
     newedge->edgenbr = nedges;
     out_bisector(newedge);
-    nedges += 1;
+    nedges++;
     return (newedge);
 }
 
@@ -88,7 +87,7 @@
     else {
 	el = el2;
 	e = e2;
-    };
+    }
     right_of_site = xint >= e->reg[1]->coord.x;
     if ((right_of_site && el->ELpm == le) ||
 	(!right_of_site && el->ELpm == re))
@@ -131,14 +130,14 @@
 		above = !above;
 	    if (!above)
 		fast = 1;
-	};
+	}
 	if (!fast) {
 	    dxs = topsite->coord.x - (e->reg[0])->coord.x;
 	    above = e->b * (dxp * dxp - dyp * dyp) <
 		dxs * dyp * (1.0 + 2.0 * dxp / dxs + e->b * e->b);
 	    if (e->b < 0.0)
 		above = !above;
-	};
+	}
     }
     else {			/*e->b==1.0 */
 	yl = e->c - e->a * p->x;
@@ -146,7 +145,7 @@
 	t2 = p->x - topsite->coord.x;
 	t3 = yl - topsite->coord.y;
 	above = t1 * t1 > t2 * t2 + t3 * t3;
-    };
+    }
     return (el->ELpm == le ? above : !above);
 }
 
@@ -177,7 +176,7 @@
 int makevertex(struct Site *v)
 {
     v->sitenbr = nvertices;
-    nvertices += 1;
+    nvertices++;
     out_vertex(v);
     return 0;
 }
@@ -185,7 +184,7 @@
 
 int deref(struct Site *v)
 {
-    v->refcnt -= 1;
+    v->refcnt--;
     if (v->refcnt == 0)
 	makefree((struct Freenode *)v, &sfl);
     return 0;
@@ -193,6 +192,6 @@
 
 int ref(struct Site *v)
 {
-    v->refcnt += 1;
+    v->refcnt++;
     return 0;
 }

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_heap.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_heap.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_heap.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -1,4 +1,5 @@
 #include <stdlib.h>
+#include <grass/gis.h>
 #include "sw_defs.h"
 
 
@@ -15,10 +16,10 @@
 	    (he->ystar == next->ystar && v->coord.x > next->vertex->coord.x)))
     {
 	last = next;
-    };
+    }
     he->PQnext = last->PQnext;
     last->PQnext = he;
-    PQcount += 1;
+    PQcount++;
     return 0;
 }
 
@@ -31,10 +32,10 @@
 	while (last->PQnext != he)
 	    last = last->PQnext;
 	last->PQnext = he->PQnext;
-	PQcount -= 1;
+	PQcount--;
 	deref(he->vertex);
 	he->vertex = (struct Site *)NULL;
-    };
+    }
     return 0;
 }
 
@@ -65,10 +66,11 @@
     struct Point answer;
 
     while (PQhash[PQmin].PQnext == (struct Halfedge *)NULL) {
-	PQmin += 1;
-    };
+	PQmin++;
+    }
     answer.x = PQhash[PQmin].PQnext->vertex->coord.x;
     answer.y = PQhash[PQmin].PQnext->ystar;
+    answer.z = PQhash[PQmin].PQnext->vertex->coord.z;
     return (answer);
 }
 
@@ -78,7 +80,7 @@
 
     curr = PQhash[PQmin].PQnext;
     PQhash[PQmin].PQnext = curr->PQnext;
-    PQcount -= 1;
+    PQcount--;
     return (curr);
 }
 
@@ -90,8 +92,8 @@
     PQcount = 0;
     PQmin = 0;
     PQhashsize = 4 * sqrt_nsites;
-    PQhash = (struct Halfedge *)myalloc(PQhashsize * sizeof *PQhash);
-    for (i = 0; i < PQhashsize; i += 1)
+    PQhash = (struct Halfedge *)G_malloc(PQhashsize * sizeof(struct Halfedge));
+    for (i = 0; i < PQhashsize; i++)
 	PQhash[i].PQnext = (struct Halfedge *)NULL;
 
     return 0;

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_main.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_main.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_main.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -30,7 +30,7 @@
 
     if (siteidx < nsites) {
 	s = &sites[siteidx];
-	siteidx += 1;
+	siteidx++;
 	return (s);
     }
     else
@@ -72,7 +72,7 @@
 
     if (j != nsites) {
 	nsites = j;
-	sites = (struct Site *)G_realloc(sites, nsites * sizeof(*sites));
+	sites = (struct Site *)G_realloc(sites, nsites * sizeof(struct Site));
     }
 
 }
@@ -80,7 +80,7 @@
 /* read all sites, sort, and compute xmin, xmax, ymin, ymax */
 int readsites(void)
 {
-    int i, nlines, line;
+    int nlines, line;
     struct line_pnts *Points;
 
     Points = Vect_new_line_struct();
@@ -88,7 +88,7 @@
     nlines = Vect_get_num_lines(&In);
 
     nsites = 0;
-    sites = (struct Site *)myalloc(nlines * sizeof(*sites));
+    sites = (struct Site *)G_malloc(nlines * sizeof(struct Site));
 
     for (line = 1; line <= nlines; line++) {
 	int type;
@@ -108,28 +108,37 @@
 	    G_debug(3, "Points->z[0]: %f", Points->z[0]);
 	    sites[nsites].coord.z = Points->z[0];
 	}
+	else
+	    sites[nsites].coord.z = 0.0;
 
 	sites[nsites].sitenbr = nsites;
 	sites[nsites].refcnt = 0;
-	nsites += 1;
-	if (nsites % 4000 == 0)
-	    sites =
-		(struct Site *)G_realloc(sites,
-					 (nsites + 4000) * sizeof(*sites));
+
+	if (nsites > 1) {
+	    if (xmin > sites[nsites].coord.x)
+		xmin = sites[nsites].coord.x;
+	    if (xmax < sites[nsites].coord.x)
+		xmax = sites[nsites].coord.x;
+	    if (ymin > sites[nsites].coord.y)
+		ymin = sites[nsites].coord.y;
+	    if (ymax < sites[nsites].coord.y)
+		ymax = sites[nsites].coord.y;
+	}
+	else {
+	    xmin = xmax = sites[nsites].coord.x;
+	    ymin = ymax = sites[nsites].coord.y;
+	}
+	nsites++;
     }
 
-    qsort(sites, nsites, sizeof(*sites), scomp);
+    if (nsites < nlines - 1)
+	sites =
+	    (struct Site *)G_realloc(sites,
+				     (nsites) * sizeof(struct Site));
+
+    qsort(sites, nsites, sizeof(struct Site), scomp);
     removeDuplicates();
-    xmin = sites[0].coord.x;
-    xmax = sites[0].coord.x;
-    for (i = 1; i < nsites; i += 1) {
-	if (sites[i].coord.x < xmin)
-	    xmin = sites[i].coord.x;
-	if (sites[i].coord.x > xmax)
-	    xmax = sites[i].coord.x;
-    }
-    ymin = sites[0].coord.y;
-    ymax = sites[nsites - 1].coord.y;
+
     return 0;
 }
 
@@ -141,7 +150,7 @@
     s = (struct Site *)getfree(&sfl);
     s->refcnt = 0;
     s->sitenbr = siteidx;
-    siteidx += 1;
+    siteidx++;
 
     if (scanf("%lf %lf", &(s->coord.x), &(s->coord.y)) == EOF)
 	return ((struct Site *)NULL);

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_memory.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_memory.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_memory.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -16,10 +16,10 @@
     struct Freenode *t;
 
     if (fl->head == (struct Freenode *)NULL) {
-	t = (struct Freenode *)myalloc(sqrt_nsites * fl->nodesize);
-	for (i = 0; i < sqrt_nsites; i += 1)
+	t = (struct Freenode *)G_malloc(sqrt_nsites * fl->nodesize);
+	for (i = 0; i < sqrt_nsites; i++)
 	    makefree((struct Freenode *)((char *)t + i * fl->nodesize), fl);
-    };
+    }
     t = fl->head;
     fl->head = (fl->head)->nextfree;
     return ((char *)t);
@@ -31,18 +31,3 @@
     fl->head = curr;
     return 0;
 }
-
-int total_alloc;
-char *myalloc(unsigned n)
-{
-    char *t;
-
-    if ((t = G_malloc(n)) == (char *)0) {
-	fprintf(stderr,
-		"Insufficient memory processing site %d (%d bytes in use)\n",
-		siteidx, total_alloc);
-	exit(0);
-    };
-    total_alloc += n;
-    return (t);
-}

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_output.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_output.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_output.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -146,7 +146,7 @@
     else {
 	s1 = e->ep[0];
 	s2 = e->ep[1];
-    };
+    }
 
     if (e->a == 1.0) {
 	y1 = pymin;
@@ -167,19 +167,19 @@
 	if (x1 > pxmax) {
 	    x1 = pxmax;
 	    y1 = (e->c - x1) / e->b;
-	};
+	}
 	if (x1 < pxmin) {
 	    x1 = pxmin;
 	    y1 = (e->c - x1) / e->b;
-	};
+	}
 	if (x2 > pxmax) {
 	    x2 = pxmax;
 	    y2 = (e->c - x2) / e->b;
-	};
+	}
 	if (x2 < pxmin) {
 	    x2 = pxmin;
 	    y2 = (e->c - x2) / e->b;
-	};
+	}
     }
     else {
 	x1 = pxmin;
@@ -200,20 +200,20 @@
 	if (y1 > pymax) {
 	    y1 = pymax;
 	    x1 = (e->c - y1) / e->a;
-	};
+	}
 	if (y1 < pymin) {
 	    y1 = pymin;
 	    x1 = (e->c - y1) / e->a;
-	};
+	}
 	if (y2 > pymax) {
 	    y2 = pymax;
 	    x2 = (e->c - y2) / e->a;
-	};
+	}
 	if (y2 < pymin) {
 	    y2 = pymin;
 	    x2 = (e->c - y2) / e->a;
-	};
-    };
+	}
+    }
 
     line(x1, y1, x2, y2);
 

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/sw_voronoi.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/sw_voronoi.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/sw_voronoi.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -37,13 +37,13 @@
 	    if ((p = intersect(lbnd, bisector)) != (struct Site *)NULL) {
 		PQdelete(lbnd);
 		PQinsert(lbnd, p, dist(p, newsite));
-	    };
+	    }
 	    lbnd = bisector;
 	    bisector = HEcreate(e, re);
 	    ELinsert(lbnd, bisector);
 	    if ((p = intersect(bisector, rbnd)) != (struct Site *)NULL) {
 		PQinsert(bisector, p, dist(p, newsite));
-	    };
+	    }
 	    /* get next site, but ensure that it doesn't have the same
 	       coordinates as the previous. If so, step over to the following
 	       site. Andrea Aime 4/7/2001 */
@@ -87,19 +87,19 @@
 	    if ((p = intersect(llbnd, bisector)) != (struct Site *)NULL) {
 		PQdelete(llbnd);
 		PQinsert(llbnd, p, dist(p, bot));
-	    };
+	    }
 	    if ((p = intersect(bisector, rrbnd)) != (struct Site *)NULL) {
 		PQinsert(bisector, p, dist(p, bot));
-	    };
+	    }
 	}
 	else
 	    break;
-    };
+    }
 
     for (lbnd = ELright(ELleftend); lbnd != ELrightend; lbnd = ELright(lbnd)) {
 	e = lbnd->ELedge;
 	out_ep(e);
-    };
+    }
 
     return 0;
 }

Modified: grass/branches/releasebranch_6_4/vector/v.voronoi/vo_main.c
===================================================================
--- grass/branches/releasebranch_6_4/vector/v.voronoi/vo_main.c	2010-09-22 18:36:25 UTC (rev 43625)
+++ grass/branches/releasebranch_6_4/vector/v.voronoi/vo_main.c	2010-09-22 18:38:00 UTC (rev 43626)
@@ -56,19 +56,18 @@
 {
     COOR *ca = (COOR *) a;
     COOR *cb = (COOR *) b;
-    double thresh = 1e-10;
     double ma, mb;
 
     /* calculate measure */
     ma = mb = 0.0;
 
-    if (fabs(ca->y - Box.S) < thresh) {	/* bottom */
+    if (fabs(ca->y - Box.S) < GRASS_EPSILON) {	/* bottom */
 	ma = ca->x - Box.W;
     }
-    else if (fabs(ca->x - Box.E) < thresh) {	/* right */
+    else if (fabs(ca->x - Box.E) < GRASS_EPSILON) {	/* right */
 	ma = (Box.E - Box.W) + (ca->y - Box.S);
     }
-    else if (fabs(ca->y - Box.N) < thresh) {	/* top */
+    else if (fabs(ca->y - Box.N) < GRASS_EPSILON) {	/* top */
 	ma = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - ca->x);
     }
     else {			/* left */
@@ -76,13 +75,13 @@
     }
 
 
-    if (fabs(cb->y - Box.S) < thresh) {	/* bottom */
+    if (fabs(cb->y - Box.S) < GRASS_EPSILON) {	/* bottom */
 	mb = cb->x - Box.W;
     }
-    else if (fabs(cb->x - Box.E) < thresh) {	/* right */
+    else if (fabs(cb->x - Box.E) < GRASS_EPSILON) {	/* right */
 	mb = (Box.E - Box.W) + (cb->y - Box.S);
     }
-    else if (fabs(cb->y - Box.N) < thresh) {	/* top */
+    else if (fabs(cb->y - Box.N) < GRASS_EPSILON) {	/* top */
 	mb = (Box.E - Box.W) + (Box.N - Box.S) + (Box.E - cb->x);
     }
     else {			/* left */
@@ -171,8 +170,9 @@
     G_get_window(&Window);
     Vect_region_box(&Window, &Box);
 
-    freeinit(&sfl, sizeof *sites);
+    freeinit(&sfl, sizeof(struct Site));
 
+    G_message(_("Reading sites..."));
     readsites();
 
     siteidx = 0;
@@ -181,9 +181,11 @@
     triangulate = 0;
     plot = 0;
     debug = 0;
+
+    G_message(_("Voronoi triangulation..."));
     voronoi(triangulate, nextone);
 
-    /* Close free ends by cyrrent region */
+    /* Close free ends by current region */
     Vect_build_partial(&Out, GV_BUILD_BASE);
 
     ncoor = 0;
@@ -256,12 +258,21 @@
     }
 
     if (1) {
-	int line, nlines, type;
+	int line, nlines, type, ctype;
 
+	if (line_flag->answer)
+	    ctype = GV_POINT;
+	else
+	    ctype = GV_CENTROID;
+
 	nlines = Vect_get_num_lines(&In);
 
+	G_message(_("Writing sites to output..."));
+
 	for (line = 1; line <= nlines; line++) {
 
+	    G_percent(line, nlines, 2);
+
 	    type = Vect_read_line(&In, Points, Cats, line);
 	    if (!(type & GV_POINTS))
 		continue;
@@ -269,7 +280,7 @@
 	    if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
 		continue;
 
-	    Vect_write_line(&Out, GV_CENTROID, Points, Cats);
+	    Vect_write_line(&Out, ctype, Points, Cats);
 
 
 	    for (i = 0; i < Cats->n_cats; i++) {
@@ -346,7 +357,6 @@
 		Vect_map_add_dblink(&Out, OFi->number, OFi->name, OFi->table,
 				    IFi->key, OFi->database, OFi->driver);
 	    }
-	    G_done_msg(" ");
 	}
     }
 
@@ -357,5 +367,6 @@
     Vect_build(&Out);
     Vect_close(&Out);
 
+    G_done_msg(" ");
     exit(EXIT_SUCCESS);
 }



More information about the grass-commit mailing list