[GRASS-SVN] r40246 - grass/trunk/vector/v.delaunay

svn_grass at osgeo.org svn_grass at osgeo.org
Tue Jan 5 08:17:42 EST 2010


Author: mmetz
Date: 2010-01-05 08:17:41 -0500 (Tue, 05 Jan 2010)
New Revision: 40246

Modified:
   grass/trunk/vector/v.delaunay/data_types.h
   grass/trunk/vector/v.delaunay/defs.h
   grass/trunk/vector/v.delaunay/edge.c
   grass/trunk/vector/v.delaunay/edge.h
   grass/trunk/vector/v.delaunay/geom_primitives.h
   grass/trunk/vector/v.delaunay/geometry.c
   grass/trunk/vector/v.delaunay/geometry.h
   grass/trunk/vector/v.delaunay/in_out.c
   grass/trunk/vector/v.delaunay/in_out.h
   grass/trunk/vector/v.delaunay/main.c
   grass/trunk/vector/v.delaunay/memory.c
Log:
fix for ticket #660

Modified: grass/trunk/vector/v.delaunay/data_types.h
===================================================================
--- grass/trunk/vector/v.delaunay/data_types.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/data_types.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,19 +1,6 @@
 #ifndef DATA_TYPES_H
 #define DATA_TYPES_H
 
-#include <stdlib.h>
-
-#ifndef MY_NULL
-#define MY_NULL  0
-#endif
-#define TRUE  1
-#define FALSE 0
-
-typedef enum
-{ left, right } side;
-
-typedef unsigned char boolean;
-
 struct vertex
 {
     double x, y, z;
@@ -30,5 +17,4 @@
     struct edge *dprev;
 };
 
-extern struct vertex *sites;
 #endif

Modified: grass/trunk/vector/v.delaunay/defs.h
===================================================================
--- grass/trunk/vector/v.delaunay/defs.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/defs.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,11 +1,5 @@
-#include <grass/gis.h>
-#include <grass/vector.h>
-#include <grass/glocale.h>
-
 #ifdef MAIN
-struct Cell_head Window;
-struct bound_box Box;
+struct vertex *sites;
 #else
-extern struct Cell_head Window;
-extern struct bound_box Box;
+extern struct vertex *sites;
 #endif

Modified: grass/trunk/vector/v.delaunay/edge.c
===================================================================
--- grass/trunk/vector/v.delaunay/edge.c	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/edge.c	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,12 +1,13 @@
-#include  "data_types.h"
-#include  "memory.h"
-#include  "edge.h"
+#include <stdlib.h>
+#include "data_types.h"
+#include "memory.h"
+#include "edge.h"
 
 /* 
  *  Construct an edge from vertices v1, v2 and add it to rings of edges e1, e2
  */
 struct edge *join(struct edge *e1, struct vertex *v1,
-		  struct edge *e2, struct vertex *v2, side s)
+		  struct edge *e2, struct vertex *v2, int side)
 {
 
     struct edge *new_edge;
@@ -16,7 +17,7 @@
 
     new_edge = create_edge(v1, v2);
 
-    if (s == left) {
+    if (side == LEFT) {
 	if (ORG(e1) == v1)
 	    splice(OPREV(e1), new_edge, v1);
 	else
@@ -118,9 +119,9 @@
 	new_edge;
     ORG(new_edge) = v1;
     DEST(new_edge) = v2;
-    if (v1->entry_pt == MY_NULL)
+    if (v1->entry_pt == NULL)
 	v1->entry_pt = new_edge;
-    if (v2->entry_pt == MY_NULL)
+    if (v2->entry_pt == NULL)
 	v2->entry_pt = new_edge;
     return new_edge;
 }

Modified: grass/trunk/vector/v.delaunay/edge.h
===================================================================
--- grass/trunk/vector/v.delaunay/edge.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/edge.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -8,14 +8,17 @@
 #define DNEXT(e) ((e)->dnext)
 #define DPREV(e) ((e)->dprev)
 
-#define OTHER_VERTEX(e,p) (ORG(e) == p ? DEST(e) : ORG(e))
-#define NEXT(e,p)         (ORG(e) == p ? ONEXT(e) : DNEXT(e))
-#define PREV(e,p)         (ORG(e) == p ? OPREV(e) : DPREV(e))
+#define OTHER_VERTEX(e,p) (ORG(e) == (p) ? DEST(e) : ORG(e))
+#define NEXT(e,p)         (ORG(e) == (p) ? ONEXT(e) : DNEXT(e))
+#define PREV(e,p)         (ORG(e) == (p) ? OPREV(e) : DPREV(e))
 
-#define SAME_EDGE(e1,e2) (e1 == e2)
+#define SAME_EDGE(e1,e2) ((e1) == (e2))
 
+#define LEFT  0
+#define RIGHT 1
+
 struct edge *join(struct edge *e1, struct vertex *v1,
-		  struct edge *e2, struct vertex *v2, side s);
+		  struct edge *e2, struct vertex *v2, int side);
 void delete_edge(struct edge *e);
 void splice(struct edge *a, struct edge *b, struct vertex *v);
 struct edge *create_edge(struct vertex *v1, struct vertex *v2);

Modified: grass/trunk/vector/v.delaunay/geom_primitives.h
===================================================================
--- grass/trunk/vector/v.delaunay/geom_primitives.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/geom_primitives.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,18 +1,25 @@
 #ifndef GEOM_PRIMITIVES_H
 #define GEOM_PRIMITIVES_H
 
-#define CREATE_VECTOR(p1, p2, dx, dy) (dx = p2->x - p1->x, dy = p2->y - p1->y)
+#ifndef TRUE
+#define TRUE  1
+#endif
+#ifndef FALSE
+#define FALSE 0
+#endif
 
-#define DOT_PRODUCT_2V(a1, a2, b1, b2) (a1 * b1 + a2 * b2)
+#define CREATE_VECTOR(p1, p2, dx, dy) ((dx) = (p2)->x - (p1)->x, (dy) = (p2)->y - (p1)->y)
 
-#define CROSS_PRODUCT_2V(a1, a2, b1, b2) (a1 * b2 - a2 * b1)
+#define DOT_PRODUCT_2V(a1, a2, b1, b2) ((a1) * (b1) + (a2) * (b2))
+
+#define CROSS_PRODUCT_2V(a1, a2, b1, b2) ((a1) * (b2) - (a2) * (b1))
 /*
    cross-product around p2
    ((p2->x - p1->x) * (p3->y - p2->y) - (p2->y - p1->y) * (p3->x - p2->x))
 
    around p1
  */
-#define CROSS_PRODUCT_3P(p1, p2, p3) ((p2->x - p1->x) * (p3->y - p1->y) - (p2->y - p1->y) * (p3->x - p1->x))
+#define CROSS_PRODUCT_3P(p1, p2, p3) (((p2)->x - (p1)->x) * ((p3)->y - (p1)->y) - ((p2)->y - (p1)->y) * ((p3)->x - (p1)->x))
 
 /* predicate testing if p3 is to the left of the line through p1 and p2 */
 #define LEFT_OF(p1, p2, p3) (CROSS_PRODUCT_3P(p1, p2, p3) > 0)

Modified: grass/trunk/vector/v.delaunay/geometry.c
===================================================================
--- grass/trunk/vector/v.delaunay/geometry.c	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/geometry.c	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,4 +1,5 @@
 #include <stddef.h>
+#include "defs.h"
 #include "data_types.h"
 #include "memory.h"
 #include "geometry.h"
@@ -16,7 +17,7 @@
 		  struct edge *l_ccw_r, struct vertex *u,
 		  struct edge **l_tangent);
 
-void divide(struct vertex *sites_sorted[], unsigned int l, unsigned int r,
+void divide(unsigned int l, unsigned int r,
 	    struct edge **l_ccw, struct edge **r_cw)
 {
 
@@ -30,26 +31,25 @@
     if (n == 2) {
 	/* Base case #1 - 2 sites in region. Construct an edge from 
 	   two sites in the region       */
-	*l_ccw = *r_cw = create_edge(sites_sorted[l], sites_sorted[r]);
+	*l_ccw = *r_cw = create_edge(&(sites[l]), &(sites[r]));
     }
     else if (n == 3) {
 	/* Base case #2 - 3 sites. Construct a triangle or two edges */
-	a = create_edge(sites_sorted[l], sites_sorted[l + 1]);
-	b = create_edge(sites_sorted[l + 1], sites_sorted[r]);
-	splice(a, b, sites_sorted[l + 1]);
+	a = create_edge(&(sites[l]), &(sites[l + 1]));
+	b = create_edge(&(sites[l + 1]), &(sites[r]));
+	splice(a, b, &(sites[l + 1]));
 	c_p =
-	    CROSS_PRODUCT_3P(sites_sorted[l], sites_sorted[l + 1],
-			     sites_sorted[r]);
+	    CROSS_PRODUCT_3P(&(sites[l]), &(sites[l + 1]), &(sites[r]));
 
 	if (c_p > 0.0) {
 	    /* Create a triangle */
-	    c = join(a, sites_sorted[l], b, sites_sorted[r], right);
+	    c = join(a, &(sites[l]), b, &(sites[r]), RIGHT);
 	    *l_ccw = a;
 	    *r_cw = b;
 	}
 	else if (c_p < 0.0) {
 	    /* Create a triangle */
-	    c = join(a, sites_sorted[l], b, sites_sorted[r], left);
+	    c = join(a, &(sites[l]), b, &(sites[r]), LEFT);
 	    *l_ccw = c;
 	    *r_cw = c;
 	}
@@ -67,18 +67,18 @@
 	split = (l + r) / 2;
 
 	/* Divide into two halves */
-	divide(sites_sorted, l, split, &l_ccw_l, &r_cw_l);
-	divide(sites_sorted, split + 1, r, &l_ccw_r, &r_cw_r);
+	divide(l, split, &l_ccw_l, &r_cw_l);
+	divide(split + 1, r, &l_ccw_r, &r_cw_r);
 
 	/* Merge the two triangulations */
-	merge(r_cw_l, sites_sorted[split], l_ccw_r, sites_sorted[split + 1],
+	merge(r_cw_l, &(sites[split]), l_ccw_r, &(sites[split + 1]),
 	      &l_tangent);
 
 	/* The lower tangent added by merge may have invalidated 
 	   l_ccw_l or r_cw_r. Update them if necessary. */
-	if (ORG(l_tangent) == sites_sorted[l])
+	if (ORG(l_tangent) == &(sites[l]))
 	    l_ccw_l = l_tangent;
-	if (DEST(l_tangent) == sites_sorted[r])
+	if (DEST(l_tangent) == &(sites[r]))
 	    r_cw_r = l_tangent;
 
 	/* Update leftmost ccw edge and rightmost cw edge */
@@ -99,7 +99,7 @@
 {
     struct edge *l, *r;
     struct vertex *o_l, *o_r, *d_l, *d_r;
-    boolean ready;
+    unsigned char ready;
 
     l = r_cw_l;
     r = l_ccw_r;
@@ -109,7 +109,7 @@
     d_r = OTHER_VERTEX(r, u);
     ready = FALSE;
 
-    while (!ready)
+    while (ready == FALSE)
 	/* left_of */
 	if (LEFT_OF(o_l, d_l, o_r)) {
 	    l = PREV(l, d_l);
@@ -140,24 +140,24 @@
 {
     struct edge *base, *l_cand, *r_cand;
     struct vertex *org_base, *dest_base;
-    long double u_l_c_o_b, v_l_c_o_b, u_l_c_d_b, v_l_c_d_b;
-    long double u_r_c_o_b, v_r_c_o_b, u_r_c_d_b, v_r_c_d_b;
+    double u_l_c_o_b, v_l_c_o_b, u_l_c_d_b, v_l_c_d_b;
+    double u_r_c_o_b, v_r_c_o_b, u_r_c_d_b, v_r_c_d_b;
 
     /* cross product */
-    long double c_p_l_cand, c_p_r_cand;
+    double c_p_l_cand, c_p_r_cand;
 
     /* dot product */
-    long double d_p_l_cand, d_p_r_cand;
-    boolean above_l_cand, above_r_cand, above_next, above_prev;
+    double d_p_l_cand, d_p_r_cand;
+    unsigned char above_l_cand, above_r_cand, above_next, above_prev;
     struct vertex *dest_l_cand, *dest_r_cand;
-    long double cot_l_cand, cot_r_cand;
+    double cot_l_cand, cot_r_cand;
     struct edge *l_lower, *r_lower;
     struct vertex *org_r_lower, *org_l_lower;
 
     /* Create first cross edge by joining lower common tangent */
     find_lowest_cross_edge(r_cw_l, s, l_ccw_r, u, &l_lower, &org_l_lower,
 			   &r_lower, &org_r_lower);
-    base = join(l_lower, org_l_lower, r_lower, org_r_lower, right);
+    base = join(l_lower, org_l_lower, r_lower, org_r_lower, RIGHT);
     org_base = org_l_lower;
     dest_base = org_r_lower;
 
@@ -275,12 +275,12 @@
 	if (!above_l_cand ||
 	    (above_l_cand && above_r_cand && cot_r_cand < cot_l_cand)) {
 	    /* Connect to the right */
-	    base = join(base, org_base, r_cand, dest_r_cand, right);
+	    base = join(base, org_base, r_cand, dest_r_cand, RIGHT);
 	    dest_base = dest_r_cand;
 	}
 	else {
 	    /* Connect to the left */
-	    base = join(l_cand, dest_l_cand, base, dest_base, right);
+	    base = join(l_cand, dest_l_cand, base, dest_base, RIGHT);
 	    org_base = dest_l_cand;
 	}
     }

Modified: grass/trunk/vector/v.delaunay/geometry.h
===================================================================
--- grass/trunk/vector/v.delaunay/geometry.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/geometry.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,6 +1,6 @@
 #ifndef GEOMETRY_H
 #define GEOMETRY_H
 
-void divide(struct vertex *sites_sorted[], unsigned int l, unsigned int r,
+void divide(unsigned int l, unsigned int r,
 	    struct edge **l_ccw, struct edge **r_cw);
 #endif

Modified: grass/trunk/vector/v.delaunay/in_out.c
===================================================================
--- grass/trunk/vector/v.delaunay/in_out.c	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/in_out.c	2010-01-05 13:17:41 UTC (rev 40246)
@@ -5,17 +5,38 @@
 #include <grass/gis.h>
 #include <grass/vector.h>
 #include <grass/glocale.h>
+#include "defs.h"
 #include "data_types.h"
 #include "memory.h"
 #include "edge.h"
-#include "geom_primitives.h"
 
-void output_edges(struct vertex *sites_sorted[], unsigned int n, int mode3d,
-		  int Type, struct Map_info map_out)
+/* compare first according to x-coordinate, if equal then y-coordinate */
+int cmp(const void *a, const void *b)
 {
+    struct vertex *p1 = (struct vertex *)a;
+    struct vertex *p2 = (struct vertex *)b;
+
+    if (p1->x < p2->x)
+	return 1;
+    else if (p1->x > p2->x)
+	return -1;
+    else {
+	if (p1->y < p2->y)
+	    return 1;
+	else if (p1->y > p2->y)
+	    return -1;
+    }
+    
+    return 0;
+}
+
+void output_edges(unsigned int n, int mode3d, int Type,
+		  struct Map_info map_out)
+{
     struct edge *e_start, *e;
     struct vertex *u, *v;
     unsigned int i;
+    double x1, y1, z1, x2, y2, z2;
 
     static struct line_pnts *Points = NULL;
     static struct line_cats *Cats = NULL;
@@ -24,33 +45,26 @@
 	Points = Vect_new_line_struct();
 	Cats = Vect_new_cats_struct();
     }
-    double x1, y1, z1, x2, y2, z2;
 
-    G_message(_("Creating edges..."));
+    G_message(_("Writing edges..."));
     for (i = 0; i < n; i++) {
 	G_percent(i, n, 2);
-	u = sites_sorted[i];
+	u = &(sites[i]);
 	e_start = e = u->entry_pt;
 	do {
 	    v = OTHER_VERTEX(e, u);
-	    if (u < v) {
-		x1 = sites[u - sites].x;
-		y1 = sites[u - sites].y;
-		x2 = sites[v - sites].x;
-		y2 = sites[v - sites].y;
+	    if (cmp(u, v) == 1) {
+		x1 = u->x;
+		y1 = u->y;
+		z1 = u->z;
+		x2 = v->x;
+		y2 = v->y;
+		z2 = v->z;
 
 		Vect_reset_line(Points);
 
-		if (mode3d) {
-		    z1 = sites[u - sites].z;
-		    z2 = sites[v - sites].z;
-		    Vect_append_point(Points, x1, y1, z1);
-		    Vect_append_point(Points, x2, y2, z2);
-		}
-		else {
-		    Vect_append_point(Points, x1, y1, 0.0);
-		    Vect_append_point(Points, x2, y2, 0.0);
-		}
+		Vect_append_point(Points, x1, y1, z1);
+		Vect_append_point(Points, x2, y2, z2);
 		Vect_write_line(&map_out, Type, Points, Cats);
 	    }
 	    e = NEXT(e, u);
@@ -61,7 +75,7 @@
 
 /* Print the ring of triangles about each vertex. */
 
-void output_triangles(struct vertex *sites_sorted[], unsigned int n,
+void output_triangles(unsigned int n,
 		      int mode3d, int Type, struct Map_info map_out)
 {
     struct edge *e_start, *e, *next;
@@ -75,63 +89,45 @@
     double x1, y1, z1, x2, y2, z2, x3, y3, z3;
 
     for (i = 0; i < n; i++) {
-	u = sites_sorted[i];
+	u = &(sites[i]);
 	e_start = e = u->entry_pt;
 	do {
 	    v = OTHER_VERTEX(e, u);
-	    if (u < v) {
+	    if (cmp(u, v) == 1) {
 		next = NEXT(e, u);
 		w = OTHER_VERTEX(next, u);
-		if (u < w)
+		if (cmp(u, w) == 1)
 		    if (SAME_EDGE(NEXT(next, w), PREV(e, v))) {
 			/* Triangle. */
-			if (v > w) {
+			if (cmp(w, v) == 1) {
 			    temp = v;
 			    v = w;
 			    w = temp;
 			}
-			x1 = sites[u - sites].x;
-			y1 = sites[u - sites].y;
-			x2 = sites[v - sites].x;
-			y2 = sites[v - sites].y;
-			x3 = sites[w - sites].x;
-			y3 = sites[w - sites].y;
+			x1 = u->x;
+			y1 = u->y;
+			x2 = v->x;
+			y2 = v->y;
+			x3 = w->x;
+			y3 = w->y;
+			z1 = u->z;
+			z2 = v->z;
+			z3 = w->z;
 
-			if (mode3d) {
-			    z1 = sites[u - sites].z;
-			    z2 = sites[v - sites].z;
-			    z3 = sites[w - sites].z;
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x1, y1, z1);
-			    Vect_append_point(Points, x2, y2, z2);
-			    Vect_write_line(&map_out, Type, Points, Cats);
+			Vect_reset_line(Points);
+			Vect_append_point(Points, x1, y1, z1);
+			Vect_append_point(Points, x2, y2, z2);
+			Vect_write_line(&map_out, Type, Points, Cats);
 
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x2, y2, z2);
-			    Vect_append_point(Points, x3, y3, z3);
-			    Vect_write_line(&map_out, Type, Points, Cats);
+			Vect_reset_line(Points);
+			Vect_append_point(Points, x2, y2, z2);
+			Vect_append_point(Points, x3, y3, z3);
+			Vect_write_line(&map_out, Type, Points, Cats);
 
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x3, y3, z3);
-			    Vect_append_point(Points, x1, y1, z1);
-			    Vect_write_line(&map_out, Type, Points, Cats);
-			}
-			else {
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x1, y1, 0.0);
-			    Vect_append_point(Points, x2, y2, 0.0);
-			    Vect_write_line(&map_out, Type, Points, Cats);
-
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x2, y2, 0.0);
-			    Vect_append_point(Points, x3, y3, 0.0);
-			    Vect_write_line(&map_out, Type, Points, Cats);
-
-			    Vect_reset_line(Points);
-			    Vect_append_point(Points, x3, y3, 0.0);
-			    Vect_append_point(Points, x1, y1, 0.0);
-			    Vect_write_line(&map_out, Type, Points, Cats);
-			}
+			Vect_reset_line(Points);
+			Vect_append_point(Points, x3, y3, z3);
+			Vect_append_point(Points, x1, y1, z1);
+			Vect_write_line(&map_out, Type, Points, Cats);
 		    }
 	    }
 	    /* Next edge around u. */
@@ -140,26 +136,22 @@
     }
 }
 
-void remove_duplicates(struct vertex *list[], unsigned int *size)
+void remove_duplicates(unsigned int *size)
 {
-    int n = *size - 1;
-    int left = 0;
-    int right = 1;
-    int shift = 0;
-    int empty = 0;
+    unsigned int n = *size;
+    unsigned int prev = 0;
+    unsigned int next;
 
-    if (n > 0)
-	for (; right < n; left++, right++)
-	    if (list[left]->x == list[right]->x &&
-		list[left]->y == list[right]->y) {
-		if (shift == 0) {
-		    shift = 1;
-		    empty = right;
-		}
-		(*size)--;
-	    }
-	    else if (shift == 1)
-		list[empty++] = list[right];
+    if (n > 0) {
+	for (next = 1; next < n; next++) {
+	    if (sites[prev].x != sites[next].x ||
+		sites[prev].y != sites[next].y) {
+		sites[++prev].x = sites[next].x;
+		sites[prev].y = sites[next].y;
+	    }	
+	}
+	*size = prev + 1;
+    }
 }
 
 /* returns number of sites read */
@@ -200,12 +192,15 @@
 	    G_debug(3, "Points->z[0]: %f", Points->z[0]);
 	    sites[nsites].z = Points->z[0];
 	}
+	else {
+	    sites[nsites].z = 0.0;
+	}
 	/* Initialise entry edge vertices. */
-	sites[nsites].entry_pt = MY_NULL;
+	sites[nsites].entry_pt = NULL;
 
-	nsites += 1;
-	/* number 100 was arbitrarily chosen */
+	nsites++;
 #if 0
+	/* number 100 was arbitrarily chosen */
 	if (nsites == allocated && line != nlines){
 	    allocated += 100;
 	    realloc_sites(allocated);

Modified: grass/trunk/vector/v.delaunay/in_out.h
===================================================================
--- grass/trunk/vector/v.delaunay/in_out.h	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/in_out.h	2010-01-05 13:17:41 UTC (rev 40246)
@@ -3,9 +3,10 @@
 
 int read_sites(int mode3d, int complete_map, struct Map_info* map_in,
 	       struct bound_box Box, int);
-void output_edges(struct vertex *sites_sorted[], unsigned int n, int mode3d,
-		  int Type, struct Map_info map_out);
-void output_triangles(struct vertex *sites_sorted[], unsigned int n,
-		      int mode3d, int Type, struct Map_info map_out);
-void remove_duplicates(struct vertex *list[], unsigned int *size);
+void output_edges(unsigned int n, int mode3d, int Type,
+                  struct Map_info map_out);
+void output_triangles(unsigned int n, int mode3d, int Type,
+                      struct Map_info map_out);
+void remove_duplicates(unsigned int *size);
+int cmp(const void *a, const void *b);
 #endif

Modified: grass/trunk/vector/v.delaunay/main.c
===================================================================
--- grass/trunk/vector/v.delaunay/main.c	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/main.c	2010-01-05 13:17:41 UTC (rev 40246)
@@ -14,6 +14,7 @@
  *
  **************************************************************/
 
+#define MAIN
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -22,19 +23,17 @@
 #include <grass/vector.h>
 #include <grass/glocale.h>
 
+#include "defs.h"
 #include "data_types.h"
 #include "memory.h"
 #include "geometry.h"
 #include "edge.h"
 #include "in_out.h"
 
-int compare(const struct vertex **p1, const struct vertex **p2);
-
 int main(int argc, char *argv[])
 {
-
     /* GRASS related variables */
-    struct Map_info map_in, map_out;
+    struct Map_info In, Out;
     struct Cell_head Window;
     struct bound_box Box;
     struct GModule *module;
@@ -49,10 +48,8 @@
     int complete_map;
     int mode3d;
     
-    unsigned int i;
     unsigned int n;
     struct edge *l_cw, *r_ccw;
-    struct vertex **sites_sorted;
 
     /* GRASS related manipulations */
     G_gisinit(argv[0]);
@@ -65,6 +62,7 @@
 
     in_opt = G_define_standard_option(G_OPT_V_INPUT);
     field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
+    field_opt->answer = "-1";
     out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
 
     reg_flag = G_define_flag();
@@ -90,63 +88,49 @@
     Cats = Vect_new_cats_struct();
     
     Vect_set_open_level(2);
-    Vect_open_old2(&map_in, in_opt->answer, "", field_opt->answer);
+    Vect_open_old2(&In, in_opt->answer, "", field_opt->answer);
 
     /* check if we have a 3D input points map */
-    mode3d = Vect_is_3d(&map_in);
+    mode3d = Vect_is_3d(&In);
 
-    if (mode3d) {
-	if (0 > Vect_open_new(&map_out, out_opt->answer, 1))
-	    G_fatal_error(_("Unable to create vector map <%s>"),
-			  out_opt->answer);
-    }
-    else if (0 > Vect_open_new(&map_out, out_opt->answer, 0))
+    if (0 > Vect_open_new(&Out, out_opt->answer, mode3d))
 	G_fatal_error(_("Unable to create vector map <%s>"), out_opt->answer);
 
-    Vect_hist_copy(&map_in, &map_out);
-    Vect_hist_command(&map_out);
+    Vect_hist_copy(&In, &Out);
+    Vect_hist_command(&Out);
 
     /* initialize working region */
     G_get_window(&Window);
     Vect_region_box(&Window, &Box);
 
-    n = read_sites(mode3d, complete_map, &map_in, Box,
-		   Vect_get_field_number(&map_in, field_opt->answer));
+    n = read_sites(mode3d, complete_map, &In, Box,
+		   Vect_get_field_number(&In, field_opt->answer));
 
-    /* Sort. */
-    sites_sorted =
-	(struct vertex **)G_malloc((unsigned)n * sizeof(struct vertex *));
-    if (sites_sorted == MY_NULL)
-	G_fatal_error(_("Not enough memory"));
-    for (i = 0; i < n; i++)
-	sites_sorted[i] = sites + i;
-    qsort(sites_sorted, n, sizeof(struct vertex *), (void *)compare);
+    Vect_set_release_support(&In);
+    Vect_close(&In);
 
+    /* sort sites */
+    qsort(sites, n, sizeof(struct vertex), cmp);
+
     G_verbose_message(_("Removing duplicates..."));
-    remove_duplicates(sites_sorted, &n);
+    remove_duplicates(&n);
+    if (n < 3)
+	G_fatal_error(_("no points to triangulate"));
+	
+    /* triangulate */
+    G_message(_("Delaunay triangulation..."));
+    divide(0, n - 1, &l_cw, &r_ccw);
 
-    /* Triangulate. */
-    divide(sites_sorted, 0, n - 1, &l_cw, &r_ccw);
+    output_edges(n, mode3d, Type, Out);
 
-    output_edges(sites_sorted, n, mode3d, Type, map_out);
-
-    G_free((char *)sites_sorted);
     free_memory();
 
-    Vect_close(&map_in);
-
     if (Type == GV_BOUNDARY) {
-	int verbose = G_verbose();
-	if (verbose < G_verbose_max()) {
-	    G_message(_("Building topology..."));
-	    G_set_verbose(0);
-	}
-	Vect_build_partial(&map_out, GV_BUILD_AREAS);
-	G_set_verbose(verbose);
-	nareas = Vect_get_num_areas(&map_out);
+	Vect_build_partial(&Out, GV_BUILD_AREAS);
+	nareas = Vect_get_num_areas(&Out);
 	G_debug(3, "nareas = %d", nareas);
 	/*  Assign centroid to each area */
-	G_message(_("Writing areas..."));
+	G_message(_("Calculating area centroids..."));
 	for (area = 1; area <= nareas; area++) {
 	    double x, y, z, angle, slope;
 	    int ret;
@@ -154,12 +138,12 @@
 	    G_percent(area, nareas, 2);
 	    Vect_reset_line(Points);
 	    Vect_reset_cats(Cats);
-	    ret = Vect_get_point_in_area(&map_out, area, &x, &y);
+	    ret = Vect_get_point_in_area(&Out, area, &x, &y);
 	    if (ret < 0) {
 		G_warning(_("Unable to calculate area centroid"));
 		continue;
 	    }
-	    ret = Vect_tin_get_z(&map_out, x, y, &z, &angle, &slope);
+	    ret = Vect_tin_get_z(&Out, x, y, &z, &angle, &slope);
 	    G_debug(3, "area centroid z: %f", z);
 	    if (ret < 0) {
 		G_warning(_("Unable to calculate area centroid z coordinate"));
@@ -167,19 +151,11 @@
 	    }
 	    Vect_append_point(Points, x, y, z);
 	    Vect_cat_set(Cats, 1, area);
-	    Vect_write_line(&map_out, GV_CENTROID, Points, Cats);
+	    Vect_write_line(&Out, GV_CENTROID, Points, Cats);
 	}
     }
-    Vect_build(&map_out);
-    Vect_close(&map_out);
+    Vect_build(&Out);
+    Vect_close(&Out);
 
     exit(EXIT_SUCCESS);
 }
-
-/* compare first according to x-coordinate, if equal then y-coordinate */
-int compare(const struct vertex **p1, const struct vertex **p2)
-{
-    if ((*p1)->x == (*p2)->x)
-	return ((*p1)->y < (*p2)->y);
-    return ((*p1)->x < (*p2)->x);
-}

Modified: grass/trunk/vector/v.delaunay/memory.c
===================================================================
--- grass/trunk/vector/v.delaunay/memory.c	2010-01-05 10:40:49 UTC (rev 40245)
+++ grass/trunk/vector/v.delaunay/memory.c	2010-01-05 13:17:41 UTC (rev 40246)
@@ -1,10 +1,9 @@
 #include <stdlib.h>
 #include <grass/gis.h>
-#include <grass/vector.h>
 #include <grass/glocale.h>
+#include "defs.h"
 #include "data_types.h"
 
-struct vertex *sites;
 static struct edge *edges;
 static struct edge **free_list_e;
 
@@ -17,17 +16,17 @@
 
     /* Sites storage. */
     sites = (struct vertex *)G_calloc(n, sizeof(struct vertex));
-    if (sites == MY_NULL)
+    if (sites == NULL)
 	G_fatal_error(_("Not enough memory."));
 
     /* Edges. Euler's formula - at most 3n edges on a set of n sites */
     n_free_e = 3 * n;
     edges = e = (struct edge *)G_calloc(n_free_e, sizeof(struct edge));
-    if (edges == MY_NULL)
+    if (edges == NULL)
 	G_fatal_error(_("Not enough memory."));
 
     free_list_e = (struct edge **)G_calloc(n_free_e, sizeof(struct edge *));
-    if (free_list_e == MY_NULL)
+    if (free_list_e == NULL)
 	G_fatal_error(_("Not enough memory."));
     for (i = 0; i < n_free_e; i++, e++)
 	free_list_e[i] = e;
@@ -37,7 +36,7 @@
 {
     /* Sites storage. */
     sites = (struct vertex *)G_calloc(n, sizeof(struct vertex));
-    if (sites == MY_NULL)
+    if (sites == NULL)
 	G_fatal_error(_("Not enough memory."));
 }
 
@@ -45,7 +44,7 @@
 {
     /* Sites storage. */
     sites = (struct vertex *)G_realloc(sites, n * sizeof(struct vertex));
-    if (sites == MY_NULL)
+    if (sites == NULL)
 	G_fatal_error(_("Not enough memory."));
 }
 
@@ -57,11 +56,11 @@
     /* Edges. Euler's formula - at most 3n edges on a set of n sites */
     n_free_e = 3 * n;
     edges = e = (struct edge *)G_calloc(n_free_e, sizeof(struct edge));
-    if (edges == MY_NULL)
+    if (edges == NULL)
 	G_fatal_error(_("Not enough memory."));
 
     free_list_e = (struct edge **)G_calloc(n_free_e, sizeof(struct edge *));
-    if (free_list_e == MY_NULL)
+    if (free_list_e == NULL)
 	G_fatal_error(_("Not enough memory."));
     for (i = 0; i < n_free_e; i++, e++)
 	free_list_e[i] = e;



More information about the grass-commit mailing list