[GRASS-SVN] r71772 - grass/trunk/lib/vector/diglib

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 19 09:24:51 PST 2017


Author: mmetz
Date: 2017-11-19 09:24:51 -0800 (Sun, 19 Nov 2017)
New Revision: 71772

Modified:
   grass/trunk/lib/vector/diglib/plus_area.c
Log:
diglib: fix bug caused by compiler optimizations, optimize code

Modified: grass/trunk/lib/vector/diglib/plus_area.c
===================================================================
--- grass/trunk/lib/vector/diglib/plus_area.c	2017-11-19 17:22:27 UTC (rev 71771)
+++ grass/trunk/lib/vector/diglib/plus_area.c	2017-11-19 17:24:51 UTC (rev 71772)
@@ -106,8 +106,11 @@
 	    dig_angle_next_line(plus, prev_line, GV_RIGHT, GV_BOUNDARY, NULL);
 	G_debug(3, "next_line = %d", next_line);
 
-	if (next_line == 0)
+	if (next_line == 0) {
+	    G_debug(3, "Cannot build area, no next line for line %d",
+	            prev_line);
 	    return (-1);	/* Not found */
+	}
 
 	/* Check if adjacent lines do not have the same angle */
 	if (!dig_node_angle_check(plus, next_line, GV_BOUNDARY)) {
@@ -210,7 +213,7 @@
 	Area->lines[i] = line;
 	Line = plus->Line[abs(line)];
 	topo = (struct P_topo_b *)Line->topo;
-	if (line < 0) {		/* revers direction -> area on left */
+	if (line < 0) {		/* reverse direction -> area on left */
 	    if (topo->left != 0) {
 		G_warning(_("Line %d already has area/isle %d to left"), line,
 			  topo->left);
@@ -303,7 +306,7 @@
  */
 int dig_area_del_isle(struct Plus_head *plus, int area, int isle)
 {
-    int i, mv;
+    int i;
     struct P_area *Area;
 
     G_debug(3, "dig_area_del_isle(): area = %d isle = %d", area, isle);
@@ -312,25 +315,24 @@
     if (Area == NULL)
 	G_fatal_error(_("Attempt to delete isle from dead area"));
 
-    mv = 0;
-    for (i = 0; i < Area->n_isles; i++) {
-	if (mv) {
-	    Area->isles[i - 1] = Area->isles[i];
-	}
-	else {
-	    if (Area->isles[i] == isle)
-		mv = 1;
-	}
-    }
+    /* find index of the isle */
+    i = 0;
+    while (i < Area->n_isles && Area->isles[i] != isle)
+	i++;
 
-    if (mv) {
-	Area->n_isles--;
-    }
-    else {
+    if (i == Area->n_isles) {
 	G_fatal_error(_("Attempt to delete not registered isle %d from area %d"),
 		      isle, area);
     }
 
+    i++;
+    while (i < Area->n_isles) {
+	Area->isles[i - 1] = Area->isles[i];
+	i++;
+    }
+
+    Area->n_isles--;
+
     return 0;
 }
 
@@ -464,8 +466,7 @@
 dig_angle_next_line(struct Plus_head *plus, plus_t current_line, int side,
 		    int type, float *angle)
 {
-    int i, next;
-    int current;
+    int next;
     int line;
     plus_t node;
     struct P_node *Node;
@@ -519,6 +520,8 @@
     G_debug(3, "  n_lines = %d", Node->n_lines);
     /* avoid loop when not debugging */
     if (debug_level > 2) {
+	int i;
+
 	for (i = 0; i < Node->n_lines; i++) {
 	    G_debug(3, "  i = %d line = %d angle = %f", i, Node->lines[i],
 		    Node->angles[i]);
@@ -526,12 +529,15 @@
     }
 
     /* first find index for that line */
-    next = -1;
-    for (current = 0; current < Node->n_lines; current++) {
-	if (Node->lines[current] == current_line)
-	    next = current;
+    next = Node->n_lines - 1;
+    while (next >= 0 && Node->lines[next] != current_line) {
+	next--;
     }
+
     if (next == -1) {
+	/* internal error, should not happen */
+	G_fatal_error("dig_angle_next_line(): line %d not found at its own node %d",
+	              current_line, node);
 	if (angle)
 	    *angle = -9.;
 	return 0;		/* not found */
@@ -562,21 +568,22 @@
 		continue;
 	}
 
-	line = abs(Node->lines[next]);
-	Line = plus->Line[line];
+	line = Node->lines[next];
+	Line = plus->Line[abs(line)];
         
 	if (Line->type & type) {	/* line found */
 	    G_debug(3, "  this one");
 	    if (angle)
 		*angle = Node->angles[next];
-	    return (Node->lines[next]);
+	    return line;
 	}
 
 	/* input line reached, this must be last, because current_line may be correct return value (dangle) */
-	if (Node->lines[next] == current_line)
+	if (line == current_line)
 	    break;
     }
-    G_debug(3, "  Line NOT found at node %d", (int)node);
+    G_debug(3, "  No next line for line %d at node %d",
+            current_line, (int)node);
     if (angle)
 	*angle = -9.;
 



More information about the grass-commit mailing list