[GRASS-SVN] r42455 - grass-addons/raster/r.stream.angle

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jun 3 07:30:27 EDT 2010


Author: mmetz
Date: 2010-06-03 07:30:25 -0400 (Thu, 03 Jun 2010)
New Revision: 42455

Modified:
   grass-addons/raster/r.stream.angle/global.h
   grass-addons/raster/r.stream.angle/init.c
   grass-addons/raster/r.stream.angle/io.c
   grass-addons/raster/r.stream.angle/main.c
   grass-addons/raster/r.stream.angle/order.c
   grass-addons/raster/r.stream.angle/seg_line.c
   grass-addons/raster/r.stream.angle/tangent.c
Log:
format and clean up code

Modified: grass-addons/raster/r.stream.angle/global.h
===================================================================
--- grass-addons/raster/r.stream.angle/global.h	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/global.h	2010-06-03 11:30:25 UTC (rev 42455)
@@ -47,7 +47,7 @@
  1 | 2 | 3 
  */
 
-#define STREAM struct _stream //zostaje
+#define STREAM struct _stream
 STREAM {
     int stream;			/* index */
     int strahler, shreeve, horton, hack;	/* orders */
@@ -61,34 +61,34 @@
     int out_r, out_c;
     float tangent_dir; /* tangent direction at init */
     float stream_dir;  /* final stream  direction */
-    };
+};
 
 
 #define DIRCELLS struct _dircells
 DIRCELLS {
-	int r;
-	int c;
-	float dir_diff;
-	float small_dir_diff;
-	int candidate;
-	int tunning;
-	int decision;
-	int category;
+    int r;
+    int c;
+    float dir_diff;
+    float small_dir_diff;
+    int candidate;
+    int tunning;
+    int decision;
+    int category;
 };
 
-#define SEGMENTS struct _segments //modyfikacja
+#define SEGMENTS struct _segments
 SEGMENTS {
-	int out_stream; /* for streams == stream */
-	int init_stream; /* for streams == stream */
-	int cell_num;
-	int seg_num;
-	float dir_angle; /* final direction */
-	float dir_init, dir_middle, dir_last, dir_full;
-	float *angles;
-	float *lengths;
-	float *drops;
-	int *cellnums;
-	int *cats;
+    int out_stream; /* for streams == stream */
+    int init_stream; /* for streams == stream */
+    int cell_num;
+    int seg_num;
+    float dir_angle; /* final direction */
+    float dir_init, dir_middle, dir_last, dir_full;
+    float *angles;
+    float *lengths;
+    float *drops;
+    int *cellnums;
+    int *cats;
 };
 
 	  /* functions.c */
@@ -162,8 +162,8 @@
 int springs_num, outlets_num;
 int stream_num;
 
-struct line_pnts *Segments; //
-struct line_cats *Cats; //
+struct line_pnts *Segments;
+struct line_cats *Cats;
 
 
 

Modified: grass-addons/raster/r.stream.angle/init.c
===================================================================
--- grass-addons/raster/r.stream.angle/init.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/init.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -3,8 +3,6 @@
 
 int trib_nums(int r, int c)
 {				/* calcualte number of tributuaries */
-
-
     int trib = 0;
     int i, j;
     int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
@@ -20,82 +18,83 @@
 	    trib++;
     }
     if (trib > 5)
-	G_fatal_error("Error finding nodes. Stream and direction maps probably do not match...");
+	G_fatal_error
+	    ("Error finding nodes. Stream and direction maps probably do not match...");
     if (trib > 3)
 	G_warning("Stream network may be too dense...");
 
-   return trib;
+    return trib;
 }				/* end trib_num */
 
 int init_streams(void)
 {
-  int i;
-  s_streams = (STREAM *) G_malloc((stream_num + 1) * sizeof(STREAM));
-  seg_common=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
+    int i;
 
- /*
-  seg_strahler=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
-  seg_horton=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
-  seg_hack=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
- */ 
-  
-  for (i = 0; i <= stream_num; ++i) {
-		s_streams[i].stream = -1;
-		s_streams[i].next_stream = -1;
-		s_streams[i].out_stream = -1;
-		s_streams[i].strahler = -1;
-		s_streams[i].shreeve = -1;
-		s_streams[i].hack = -1;
-		s_streams[i].horton = -1;
-		s_streams[i].init = -1;
-		s_streams[i].trib_num = 0;
-		s_streams[i].cell_num = 0;
-		s_streams[i].accum = 0.;
-		s_streams[i].trib[0] = 0;
-		s_streams[i].trib[1] = 0;
-		s_streams[i].trib[2] = 0;
-		s_streams[i].trib[3] = 0;
-		s_streams[i].trib[4] = 0;
-		s_streams[i].init_r = -1;
-		s_streams[i].init_c = -1;
-		s_streams[i].out_r = -1;
-		s_streams[i].out_c = -1;
-		s_streams[i].stream_dir=-999;
-		s_streams[i].tangent_dir=-999;
-		
-		seg_common[i].init_stream=-1;
-		seg_common[i].out_stream=-1;
-		seg_common[i].cell_num=-1;
-		seg_common[i].seg_num=-1;
-		seg_common[i].dir_angle=-1; 
-		seg_common[i].dir_init=-1;
-		seg_common[i].dir_middle=-1; 
-		seg_common[i].dir_last=-1; 
-		seg_common[i].dir_full=-1;
-		seg_common[i].angles=NULL;
-		seg_common[i].lengths=NULL;
-		seg_common[i].drops=NULL;
-		seg_common[i].cellnums=NULL;
-		seg_common[i].cats=NULL;
+    s_streams = (STREAM *) G_malloc((stream_num + 1) * sizeof(STREAM));
+    seg_common = (SEGMENTS *) G_malloc((stream_num + 1) * sizeof(SEGMENTS));
 
-	}
-	return 0;
+    /*
+       seg_strahler=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
+       seg_horton=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
+       seg_hack=(SEGMENTS *)G_malloc((stream_num + 1) *sizeof(SEGMENTS));
+     */
+
+    for (i = 0; i <= stream_num; ++i) {
+	s_streams[i].stream = -1;
+	s_streams[i].next_stream = -1;
+	s_streams[i].out_stream = -1;
+	s_streams[i].strahler = -1;
+	s_streams[i].shreeve = -1;
+	s_streams[i].hack = -1;
+	s_streams[i].horton = -1;
+	s_streams[i].init = -1;
+	s_streams[i].trib_num = 0;
+	s_streams[i].cell_num = 0;
+	s_streams[i].accum = 0.;
+	s_streams[i].trib[0] = 0;
+	s_streams[i].trib[1] = 0;
+	s_streams[i].trib[2] = 0;
+	s_streams[i].trib[3] = 0;
+	s_streams[i].trib[4] = 0;
+	s_streams[i].init_r = -1;
+	s_streams[i].init_c = -1;
+	s_streams[i].out_r = -1;
+	s_streams[i].out_c = -1;
+	s_streams[i].stream_dir = -999;
+	s_streams[i].tangent_dir = -999;
+
+	seg_common[i].init_stream = -1;
+	seg_common[i].out_stream = -1;
+	seg_common[i].cell_num = -1;
+	seg_common[i].seg_num = -1;
+	seg_common[i].dir_angle = -1;
+	seg_common[i].dir_init = -1;
+	seg_common[i].dir_middle = -1;
+	seg_common[i].dir_last = -1;
+	seg_common[i].dir_full = -1;
+	seg_common[i].angles = NULL;
+	seg_common[i].lengths = NULL;
+	seg_common[i].drops = NULL;
+	seg_common[i].cellnums = NULL;
+	seg_common[i].cats = NULL;
+
+    }
+    return 0;
 }
 
 int find_nodes(void)
 {
-
     int d, i, j;		/* d: direction, i: iteration */
     int r, c;
     int trib_num, trib = 0;
     int next_stream = -1, cur_stream;
-
-    springs_num = 0, outlets_num = 0;
     int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
     int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
 
     G_message(_("Finding nodes..."));
 
+    springs_num = 0, outlets_num = 0;
+
     outlets = (int *)G_malloc((stream_num) * sizeof(int));
     springs = (int *)G_malloc((stream_num) * sizeof(int));
 
@@ -105,7 +104,7 @@
 		trib_num = trib_nums(r, c);
 		trib = 0;
 		d = abs(dirs[r][c]);	/* abs */
-			if (r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||
+		if (r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||
 		    c + nextc[d] < 0 || c + nextc[d] > (ncols - 1)) {
 		    next_stream = -1;
 		}
@@ -114,14 +113,15 @@
 		    if (next_stream < 1)
 			next_stream = -1;
 		}
-		if (dirs[r][c]==0) 
-		    next_stream=-1;
+		if (dirs[r][c] == 0)
+		    next_stream = -1;
 		cur_stream = streams[r][c];
 
 		if (cur_stream != next_stream) {	/* building hierarchy */
 
 		    if (outlets_num > (stream_num - 1))
-			G_fatal_error("Error finding nodes. Stream and direction maps probably do not match...");
+			G_fatal_error
+			    ("Error finding nodes. Stream and direction maps probably do not match...");
 
 		    s_streams[cur_stream].stream = cur_stream;
 		    s_streams[cur_stream].next_stream = next_stream;
@@ -130,24 +130,26 @@
 		}
 
 		if (trib_num == 0) {	/* adding springs */
-		
+
 		    if (springs_num > (stream_num - 1))
-			G_fatal_error("Error finding nodes. Stream and direction maps probably do not match...");
+			G_fatal_error
+			    ("Error finding nodes. Stream and direction maps probably do not match...");
 
 		    s_streams[cur_stream].trib_num = 0;
 		    springs[springs_num++] = cur_stream;	/* collecting springs */
 		    s_streams[cur_stream].init_r = r;
-				s_streams[cur_stream].init_c = c;
+		    s_streams[cur_stream].init_c = c;
 		}
 
 		if (trib_num > 1) {	/* adding tributuaries */
 		    s_streams[cur_stream].trib_num = trib_num;
 		    s_streams[cur_stream].init_r = r;
-				s_streams[cur_stream].init_c = c;
-		    
+		    s_streams[cur_stream].init_c = c;
+
 		    for (i = 1; i < 9; ++i) {
 			if (trib > 4)
-			    G_fatal_error("Error finding nodes. Stream and direction maps probably do not match...");
+			    G_fatal_error
+				("Error finding nodes. Stream and direction maps probably do not match...");
 
 			if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) ||
 			    c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
@@ -168,100 +170,106 @@
     return 0;
 }
 
-int do_cum_length (void) {
-	
-	int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
-  int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+int do_cum_length(void)
+{
+    int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+    int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+    int i, s, d;		/* s - streams index; d - direction */
+    int done = 1;
+    int r, c;
+    int next_r, next_c;
+    int cur_stream;
+    float cur_northing, cur_easting;
+    float next_northing, next_easting;
+    double cur_length = 0.;
+    double cur_accum = 0.;
 
-  int i, s, d;		/* s - streams index; d - direction */
-  int done = 1;
-  int r, c;
-  int next_r, next_c;
-  int cur_stream;
-  float cur_northing, cur_easting;
-  float next_northing, next_easting;
-  double cur_length=0.;
-  double cur_accum=0.;
-	G_message("Finding longests streams...");
-		
-	for (s=0; s<springs_num; ++s) { /* main loop on springs */
-		r = s_streams[springs[s]].init_r;
-		c = s_streams[springs[s]].init_c;
-		cur_stream=s_streams[springs[s]].stream;
-		cur_length=0;
-		done=1;
-		
-		while(done) {
+    G_message("Finding longests streams...");
 
-		 cur_northing = window.north - (r + .5) * window.ns_res;
-	   cur_easting = window.west + (c + .5) * window.ew_res;
-		 d=dirs[r][c];
-		 next_r=r+nextr[d];
-	   next_c=c+nextc[d];
-			
-		if (d<1|| /* end of route: sink */
-		r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) || /*border*/
-		c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) ||
-		streams[next_r][next_c]<1) { /* mask */
-			
-			cur_length=(window.ns_res+window.ew_res)/2;
-			s_streams[cur_stream].accum += cur_length;
-			cur_length=G_distance(next_easting, next_northing, cur_easting,  cur_northing);
-			s_streams[cur_stream].accum += cur_length;
-			s_streams[cur_stream].cell_num++;
-			s_streams[cur_stream].out_r=r;
-			s_streams[cur_stream].out_c=c;
-			break;
-		}
- 
+    for (s = 0; s < springs_num; ++s) {	/* main loop on springs */
+	r = s_streams[springs[s]].init_r;
+	c = s_streams[springs[s]].init_c;
+	cur_stream = s_streams[springs[s]].stream;
+	cur_length = 0;
+	done = 1;
 
-		 next_northing = window.north - (next_r + .5) * window.ns_res;
-		 next_easting = 	window.west + (next_c + .5) * window.ew_res;
-		 cur_length =	G_distance(next_easting, next_northing, cur_easting,  cur_northing);
-		 s_streams[cur_stream].accum += cur_length;
-		 s_streams[cur_stream].cell_num++;
-		 
-			if (streams[next_r][next_c] != cur_stream) {
-				s_streams[cur_stream].out_r=r;
-				s_streams[cur_stream].out_c=c;
-				cur_stream=streams[next_r][next_c];
-				cur_accum=0;
-			
-				for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
-					if (s_streams[s_streams[cur_stream].trib[i]].accum == 0) {
-						done=0;
-						cur_accum=0;
-						break; /* do not pass accum*/
-					} 
-					if(s_streams[s_streams[cur_stream].trib[i]].accum>cur_accum) 
-						cur_accum=s_streams[s_streams[cur_stream].trib[i]].accum;
-				} /* end for i */
-			s_streams[cur_stream].accum=cur_accum;
-			}/* end if */
-		
-		 r=next_r;
-		 c=next_c;		
-			
-		} /* end while */
-	} /* end for s*/
-return 0;
+	while (done) {
+
+	    cur_northing = window.north - (r + .5) * window.ns_res;
+	    cur_easting = window.west + (c + .5) * window.ew_res;
+	    d = dirs[r][c];
+	    next_r = r + nextr[d];
+	    next_c = c + nextc[d];
+	    next_northing = window.north - (next_r + .5) * window.ns_res;
+	    next_easting = window.west + (next_c + .5) * window.ew_res;
+
+	    if (d < 1 ||	/* end of route: sink */
+		r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||	/*border */
+		c + nextc[d] < 0 || c + nextc[d] > (ncols - 1) || streams[next_r][next_c] < 1) {	/* mask */
+
+		cur_length = (window.ns_res + window.ew_res) / 2;
+		s_streams[cur_stream].accum += cur_length;
+		cur_length =
+		    G_distance(next_easting, next_northing, cur_easting,
+			       cur_northing);
+		s_streams[cur_stream].accum += cur_length;
+		s_streams[cur_stream].cell_num++;
+		s_streams[cur_stream].out_r = r;
+		s_streams[cur_stream].out_c = c;
+		break;
+	    }
+
+	    cur_length =
+		G_distance(next_easting, next_northing, cur_easting,
+			   cur_northing);
+	    s_streams[cur_stream].accum += cur_length;
+	    s_streams[cur_stream].cell_num++;
+
+	    if (streams[next_r][next_c] != cur_stream) {
+		s_streams[cur_stream].out_r = r;
+		s_streams[cur_stream].out_c = c;
+		cur_stream = streams[next_r][next_c];
+		cur_accum = 0;
+
+		for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
+		    if (s_streams[s_streams[cur_stream].trib[i]].accum == 0) {
+			done = 0;
+			cur_accum = 0;
+			break;	/* do not pass accum */
+		    }
+		    if (s_streams[s_streams[cur_stream].trib[i]].accum >
+			cur_accum)
+			cur_accum =
+			    s_streams[s_streams[cur_stream].trib[i]].accum;
+		}		/* end for i */
+		s_streams[cur_stream].accum = cur_accum;
+	    }			/* end if */
+
+	    r = next_r;
+	    c = next_c;
+
+	}			/* end while */
+    }				/* end for s */
+    return 0;
 }
 
-int cur_orders(int cur_stream, int ordering) {
-		switch (ordering) {
-			case NONE:
-				return s_streams[cur_stream].stream;
-				break;
-			case HACK:
-				return s_streams[cur_stream].hack;
-				break;
-			case STRAHLER:
-				return s_streams[cur_stream].strahler;
-				break;
-			case HORTON:
-				return s_streams[cur_stream].horton;
-				break;
-		}
+int cur_orders(int cur_stream, int ordering)
+{
+    switch (ordering) {
+    case NONE:
+	return s_streams[cur_stream].stream;
+	break;
+    case HACK:
+	return s_streams[cur_stream].hack;
+	break;
+    case STRAHLER:
+	return s_streams[cur_stream].strahler;
+	break;
+    case HORTON:
+	return s_streams[cur_stream].horton;
+	break;
+    }
+    return 0;
 }
 
 /*
@@ -271,69 +279,75 @@
  * nc: next col (most downstream col)
  */
 
-float calc_dir(int rp,int cp,int rn,int cn) {
-	return
-	(cp-cn) == 0 ?
-		(rp-rn) > 0 ? 0 : PI :
-			(cp-cn) < 0 ? 
-				PI/2+atan((rp-rn)/(float)(cp-cn)) : 
-				3*PI/2+atan((rp-rn)/(float)(cp-cn));
+float calc_dir(int rp, int cp, int rn, int cn)
+{
+    return
+	(cp - cn) == 0 ?
+	(rp - rn) > 0 ? 0 : PI :
+	(cp - cn) < 0 ?
+	PI / 2 + atan((rp - rn) / (float)(cp - cn)) :
+	3 * PI / 2 + atan((rp - rn) / (float)(cp - cn));
 }
 
-float calc_drop(int rp,int cp,int rn,int cn) {
-		if ((elevation[rp][cp]-elevation[rn][cn])<0.)
+float calc_drop(int rp, int cp, int rn, int cn)
+{
+    if ((elevation[rp][cp] - elevation[rn][cn]) < 0.)
 	return 0.;
-		else
-	return elevation[rp][cp]-elevation[rn][cn];
+    else
+	return elevation[rp][cp] - elevation[rn][cn];
 }
 
-float calc_length(int rp,int cp,int rn,int cn) {
-	
-	int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
-  int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+float calc_length(int rp, int cp, int rn, int cn)
+{
 
-  int i, d;		/* s - streams index; d - direction */
-  int done = 1;
-  int r, c;
-  int next_r, next_c;
-  float cur_northing, cur_easting;
-  float next_northing, next_easting;
-	float length;
-	
-		r=rp;
-		c=cp;
-		length=0.;
-		done=1;
+    int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+    int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+    int d;			/* s - streams index; d - direction */
+    int done = 1;
+    int r, c;
+    int next_r, next_c;
+    float cur_northing, cur_easting;
+    float next_northing, next_easting;
+    float length;
 
-		while(done) {
+    r = rp;
+    c = cp;
+    length = 0.;
+    done = 1;
 
-		 cur_northing = window.north - (r + .5) * window.ns_res;
-	   cur_easting = window.west + (c + .5) * window.ew_res;
-		 d=dirs[r][c];
-		 next_r=r+nextr[d];
-	   next_c=c+nextc[d];
-			
-		if (d<1|| /* end of route: sink */
-		r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) || /*border*/
-		c + nextc[d] < 0 || c + nextc[d] > (ncols - 1))
-		{ 
-			length += (window.ns_res+window.ew_res)/2;	   
-			return length;
-		}
+    while (done) {
 
+	cur_northing = window.north - (r + .5) * window.ns_res;
+	cur_easting = window.west + (c + .5) * window.ew_res;
+	d = dirs[r][c];
+	next_r = r + nextr[d];
+	next_c = c + nextc[d];
 
-			if (r==rn && c==cn) {
-				length += (dirs[r][c]%2) ? 1.41 * (window.ns_res+window.ew_res)/2 : (window.ns_res+window.ew_res)/2;
-				return length;
-			}	
-		 next_northing = window.north - (next_r + .5) * window.ns_res;
-		 next_easting = 	window.west + (next_c + .5) * window.ew_res;
-		 length +=	G_distance(next_easting, next_northing, cur_easting,  cur_northing);
-		 
-		 r=next_r;
-		 c=next_c;
-		
-		
-		} /* end while */
+	if (d < 1 ||		/* end of route: sink */
+	    r + nextr[d] < 0 || r + nextr[d] > (nrows - 1) ||	/*border */
+	    c + nextc[d] < 0 || c + nextc[d] > (ncols - 1)) {
+	    length += (window.ns_res + window.ew_res) / 2;
+	    return length;
+	}
 
+
+	if (r == rn && c == cn) {
+	    length +=
+		(dirs[r][c] % 2) ? 1.41 * (window.ns_res +
+					   window.ew_res) /
+		2 : (window.ns_res + window.ew_res) / 2;
+	    return length;
+	}
+	next_northing = window.north - (next_r + .5) * window.ns_res;
+	next_easting = window.west + (next_c + .5) * window.ew_res;
+	length +=
+	    G_distance(next_easting, next_northing, cur_easting,
+		       cur_northing);
+
+	r = next_r;
+	c = next_c;
+
+
+    }				/* end while */
+    return length;
 }

Modified: grass-addons/raster/r.stream.angle/io.c
===================================================================
--- grass-addons/raster/r.stream.angle/io.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/io.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -53,7 +53,6 @@
     dirs = (CELL **) G_malloc(sizeof(CELL *) * nrows);
     streams = (CELL **) G_malloc(sizeof(CELL *) * nrows);
 
-    
     if (extended) {
 	in_dem_fd = open_raster(in_elev);
 
@@ -71,9 +70,7 @@
 	}
 	elevation = (FCELL **) G_malloc(sizeof(FCELL *) * nrows);
     }
-    
-    
-    
+
     G_message(_("Reading maps..."));
 
     for (r = 0; r < nrows; ++r) {
@@ -90,7 +87,7 @@
 	    G_fatal_error(_("Unable to read raster maps at row <%d>"), r);
 	}
 
-if (extended) {
+	if (extended) {
 	    elevation[r] = (FCELL *) G_malloc(sizeof(FCELL) * ncols);
 
 	    switch (elev_type) {
@@ -138,7 +135,6 @@
 	    }			/* end switch */
 	}			/* end if elev */
 
-
 	for (c = 0; c < ncols; ++c) {
 	    if (G_is_c_null_value(&r_dirs[c])) {
 		dirs[r][c] = 0;
@@ -158,8 +154,8 @@
 	/* END dirs & streams  & accums */
 
     }				/*end for r */
-    
-   switch (elev_type) {
+
+    switch (elev_type) {
     case CELL_TYPE:
 	G_free(r_dem_c);
 	break;
@@ -167,7 +163,7 @@
 	G_free(r_dem_d);
 	break;
     }
- 
+
     G_free(r_streams);
     G_free(r_dirs);
     G_percent(r, nrows, 2);
@@ -180,44 +176,45 @@
 
 int stream_number(void)
 {
-	char *cur_mapset;
-	CELL c_min, c_max;
-	struct Range stream_range;
-	G_init_range(&stream_range);
-	cur_mapset = G_find_cell2(in_streams, "");
-	G_read_range(in_streams,cur_mapset,&stream_range);
-	G_get_range_min_max(&stream_range, &c_min, &c_max);
-	stream_num=c_max;
-	return 0;
+    char *cur_mapset;
+    CELL c_min, c_max;
+    struct Range stream_range;
+
+    G_init_range(&stream_range);
+    cur_mapset = G_find_cell2(in_streams, "");
+    G_read_range(in_streams, cur_mapset, &stream_range);
+    G_get_range_min_max(&stream_range, &c_min, &c_max);
+    stream_num = c_max;
+    return 0;
 }
 
 
-int close_vector () {
-	
-	int i,j;
-	int cur_order;
-	int index_cat=0;
-	float angle, tangent;
-	struct field_info *Fi;
-	dbDriver *driver;
-	dbHandle handle;
-	dbString table_name, db_sql, val_string;
-	char *cat_col_name="cat";
-	char buf[1000];
-	
-	db_init_string(&db_sql);
-	db_init_string(&val_string);
-	db_init_string(&table_name);
-	db_init_handle(&handle);
-	
-	 Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
+int close_vector()
+{
+    int i, j;
+    int cur_order;
+    int index_cat = 0;
+    float angle, tangent;
+    struct field_info *Fi;
+    dbDriver *driver;
+    dbHandle handle;
+    dbString table_name, db_sql, val_string;
+    char *cat_col_name = "cat";
+    char buf[1000];
+
+    db_init_string(&db_sql);
+    db_init_string(&val_string);
+    db_init_string(&table_name);
+    db_init_handle(&handle);
+
+    Fi = Vect_default_field_info(&Out, 1, NULL, GV_1TABLE);
     driver = db_start_driver_open_database(Fi->driver, Fi->database);
     if (driver == NULL) {
 	G_fatal_error(_("Unable to start driver <%s>"), Fi->driver);
     }
-   
-			if (extended) /* calculate extended topology */
-  sprintf(buf,"create table %s (%s integer, \
+
+    if (extended)		/* calculate extended topology */
+	sprintf(buf, "create table %s (%s integer, \
 		c_stream integer, \
 		segment integer, \
 		c_order integer, \
@@ -227,88 +224,84 @@
 		ncells integer, \
 		n_stream integer, \
 		n_tangent double precision\
-		)",
-		Fi->table,cat_col_name);
-			else /* only simple results */
-	sprintf(buf,"create table %s (%s integer, \
+		)", Fi->table, cat_col_name);
+    else			/* only simple results */
+	sprintf(buf, "create table %s (%s integer, \
 		c_stream integer, \
 		c_order integer, \
 		direction double precision, \
 		length double precision \
-		)",
-		Fi->table,cat_col_name);	
-		
-	
-	db_set_string(&db_sql, buf);
+		)", Fi->table, cat_col_name);
 
-	if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
-		db_close_database(driver);
-		db_shutdown_driver(driver);
-		G_fatal_error("Cannot create table %s", db_get_string(&db_sql));
-	}
-	
-		if (db_create_index2(driver,Fi->table,cat_col_name) !=DB_OK)
+
+    db_set_string(&db_sql, buf);
+
+    if (db_execute_immediate(driver, &db_sql) != DB_OK) {
+	db_close_database(driver);
+	db_shutdown_driver(driver);
+	G_fatal_error("Cannot create table %s", db_get_string(&db_sql));
+    }
+
+    if (db_create_index2(driver, Fi->table, cat_col_name) != DB_OK)
 	G_warning("cannot create index");
-	
-		if (db_grant_on_table(driver,Fi->table,
-			DB_PRIV_SELECT, DB_GROUP|DB_PUBLIC) !=DB_OK)
+
+    if (db_grant_on_table(driver, Fi->table,
+			  DB_PRIV_SELECT, DB_GROUP | DB_PUBLIC) != DB_OK)
 	G_fatal_error("cannot grant privileges on table %s", Fi->table);
 
-	db_begin_transaction(driver);
+    db_begin_transaction(driver);
 
-	for (i=1;i<stream_num+1;++i) {
+    for (i = 1; i < stream_num + 1; ++i) {
 
-		for (j=0;j<seg_common[i].seg_num;++j) {
-			index_cat=seg_common[i].cats[j];
-			
-			angle=(radians) ? seg_common[i].angles[j] : rad2deg(seg_common[i].angles[j]); 
-			tangent=(radians) ? 
-				s_streams[s_streams[i].next_stream].tangent_dir :
-				rad2deg(s_streams[s_streams[i].next_stream].tangent_dir);
-			
-			cur_order=(ordering) ? cur_orders(s_streams[i].stream,ordering) : 0;
-			
-					if(extended)
-			sprintf(buf,"insert into %s  values (%d, %d, %d, %d, %f, %f, %f, %d, %d, %f)",
-				Fi->table, 
-				index_cat,
-				s_streams[i].stream,
-				j,
-				cur_order, 
-				angle,
-	 			seg_common[i].lengths[j],
-	 			seg_common[i].drops[j],
-				seg_common[i].cellnums[j],
-				s_streams[i].next_stream,
-				tangent
-			);
-					else
-						sprintf(buf,"insert into %s  values (%d, %d, %d, %f, %f)",
-				Fi->table, 
-				index_cat,
-				s_streams[i].stream,
-				cur_order, 
-				angle,
-				seg_common[i].lengths[j]
-			);		
+	for (j = 0; j < seg_common[i].seg_num; ++j) {
+	    index_cat = seg_common[i].cats[j];
 
-			db_set_string(&db_sql,buf);
+	    angle =
+		(radians) ? seg_common[i].angles[j] : rad2deg(seg_common[i].
+							      angles[j]);
+	    tangent =
+		(radians) ? s_streams[s_streams[i].next_stream].
+		tangent_dir : rad2deg(s_streams[s_streams[i].next_stream].
+				      tangent_dir);
 
-			if(db_execute_immediate(driver,&db_sql) !=DB_OK) {
-				db_close_database(driver);
-				db_shutdown_driver(driver);
-				G_fatal_error("Cannot inset new row: %s", db_get_string(&db_sql));
-			}
- 
-		}
+	    cur_order =
+		(ordering) ? cur_orders(s_streams[i].stream, ordering) : 0;
+
+	    if (extended)
+		sprintf(buf,
+			"insert into %s  values (%d, %d, %d, %d, %f, %f, %f, %d, %d, %f)",
+			Fi->table, index_cat, s_streams[i].stream, j,
+			cur_order, angle, seg_common[i].lengths[j],
+			seg_common[i].drops[j], seg_common[i].cellnums[j],
+			s_streams[i].next_stream, tangent);
+	    else
+		sprintf(buf, "insert into %s  values (%d, %d, %d, %f, %f)",
+			Fi->table,
+			index_cat,
+			s_streams[i].stream,
+			cur_order, angle, seg_common[i].lengths[j]
+		    );
+
+	    db_set_string(&db_sql, buf);
+
+	    if (db_execute_immediate(driver, &db_sql) != DB_OK) {
+		db_close_database(driver);
+		db_shutdown_driver(driver);
+		G_fatal_error("Cannot inset new row: %s",
+			      db_get_string(&db_sql));
+	    }
+
 	}
-	db_commit_transaction(driver);
-	db_close_database_shutdown_driver(driver); 
-	
-	Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
-		cat_col_name, Fi->database, Fi->driver);
-   
-	  Vect_hist_command(&Out);
+    }
+    db_commit_transaction(driver);
+    db_close_database_shutdown_driver(driver);
+
+    Vect_map_add_dblink(&Out, 1, NULL, Fi->table,
+			cat_col_name, Fi->database, Fi->driver);
+
+    Vect_hist_command(&Out);
     Vect_build(&Out);
     Vect_close(&Out);
+
+    return 0;
 }

Modified: grass-addons/raster/r.stream.angle/main.c
===================================================================
--- grass-addons/raster/r.stream.angle/main.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/main.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -27,173 +27,164 @@
  */
 int main(int argc, char *argv[])
 {
-
     struct GModule *module;	/* GRASS module for parsing arguments */
-    struct Option *in_dir_opt, 
-									*in_stm_opt,  /* streams */
-									*in_elev_opt,  /* streams */
-									*in_length,
-									*in_outlet,
-									*in_threshold,
-									*in_ordering,
-									*out_segments; /* options */
+    struct Option *in_dir_opt, *in_stm_opt,	/* streams */
+     *in_elev_opt,		/* streams */
+     *in_length, *in_outlet, *in_threshold, *in_ordering, *out_segments;	/* options */
+    struct Flag *out_rad, *out_ext;
 
-    struct Flag *out_rad,
-								*out_ext;
     /* initialize GIS environment */
     G_gisinit(argv[0]);		/* reads grass env, stores program name to G_program_name() */
 
     /* initialize module */
     module = G_define_module();
-    module->keywords =
-	_("stream topology, route azimuth, route direction");
+    module->keywords = _("stream topology, route azimuth, route direction");
     module->description =
 	_("Route azimuth, direction and relation to streams of higher order");
 
     /*input option direction is reqired, acummulation is optional */
 
     in_stm_opt = G_define_option();
-    in_stm_opt->key = "stream";	
+    in_stm_opt->key = "stream";
     in_stm_opt->type = TYPE_STRING;
     in_stm_opt->required = YES;
     in_stm_opt->gisprompt = "old,cell,raster";
     in_stm_opt->description =
 	"Name of stream mask input map (r.watershed or r.stream.extract)";
 
-		in_dir_opt = G_define_option();	/* input directon file */
+    in_dir_opt = G_define_option();	/* input directon file */
     in_dir_opt->key = "dirs";
     in_dir_opt->type = TYPE_STRING;
     in_dir_opt->required = YES;
     in_dir_opt->gisprompt = "old,cell,raster";
     in_dir_opt->description =
 	"Name of direction input map (r.watershed or r.stream.extract)";
-	
+
     in_elev_opt = G_define_option();	/* input directon file */
     in_elev_opt->key = "elev";
     in_elev_opt->type = TYPE_STRING;
     in_elev_opt->required = NO;
     in_elev_opt->gisprompt = "old,cell,raster";
     in_elev_opt->answer = NULL;
-    in_elev_opt->description =
-	"Name of elevation input map";
+    in_elev_opt->description = "Name of elevation input map";
 
     in_ordering = G_define_option();
-		in_ordering->key = "order";
-    in_ordering->description ="Stream ordering method";
+    in_ordering->key = "order";
+    in_ordering->description = "Stream ordering method";
     in_ordering->type = TYPE_STRING;
     in_ordering->required = YES;
-    in_ordering->options ="none,hack,horton,strahler";
+    in_ordering->options = "none,hack,horton,strahler";
     in_ordering->answer = "none";
-    
+
     in_length = G_define_option();
     in_length->key = "length";
     in_length->label = _("Search length to calculat direction");
     in_length->description = _("Must be > 0");
-		in_length->required = YES;
-		in_length->answer = "15";
-		in_length->type = TYPE_INTEGER;
-		
-		in_outlet = G_define_option();
+    in_length->required = YES;
+    in_length->answer = "15";
+    in_length->type = TYPE_INTEGER;
+
+    in_outlet = G_define_option();
     in_outlet->key = "skip";
     in_outlet->label = _("Skip segments shorter than");
     in_outlet->description = _("Must be >= 0");
-		in_outlet->required = YES;
-		in_outlet->answer = "5";
-		in_outlet->type = TYPE_INTEGER;
-		
-		in_threshold = G_define_option();
+    in_outlet->required = YES;
+    in_outlet->answer = "5";
+    in_outlet->type = TYPE_INTEGER;
+
+    in_threshold = G_define_option();
     in_threshold->key = "treshold";
-    in_threshold->label = _("Max angle (degrees) beetwen stream segments to ");
+    in_threshold->label =
+	_("Max angle (degrees) beetwen stream segments to ");
     in_threshold->description = _("Must be > 0");
-		in_threshold->required = YES;
-		in_threshold->answer = "150";
-		in_threshold->type = TYPE_DOUBLE;
-		
-		out_segments = G_define_option();
+    in_threshold->required = YES;
+    in_threshold->answer = "150";
+    in_threshold->type = TYPE_DOUBLE;
+
+    out_segments = G_define_option();
     out_segments->key = "seg_vector";
     out_segments->label = _("Vector to store new network with segments");
     out_segments->required = YES;
-		out_segments->answer = NULL;
-		out_segments->gisprompt = "new,vector,vector";
-     
+    out_segments->answer = NULL;
+    out_segments->gisprompt = "new,vector,vector";
+
     out_rad = G_define_flag();
     out_rad->key = 'r';
     out_rad->description = _("Output angles in radians (default: degrees)");
-    
+
     out_ext = G_define_flag();
     out_ext->key = 'e';
-    out_ext->description = _("Extended topology (default: calculate direction only)");
-      
+    out_ext->description =
+	_("Extended topology (default: calculate direction only)");
+
     if (G_parser(argc, argv))	/* parser */
 	exit(EXIT_FAILURE);
 
     G_get_window(&window);
-    
+
     /* stores input options to variables */
     in_dirs = in_dir_opt->answer;
     in_streams = in_stm_opt->answer;
     in_elev = in_elev_opt->answer;
-   
-    out_vector=out_segments->answer;
 
+    out_vector = out_segments->answer;
+
     seg_length = atoi(in_length->answer);
     seg_treshold = atof(in_threshold->answer);
     seg_outlet = atoi(in_outlet->answer);
 
-		radians = (out_rad->answer != 0);
-		extended = (out_ext->answer != 0);
-		
-      if (seg_length <= 0)
-		G_fatal_error("Search's length must be > 0");
-	    if (seg_treshold < 0 || seg_treshold > 180)
-		G_fatal_error("Treshold must be between 0 and 180");
-		  if (seg_outlet < 0)
-		G_fatal_error("Segment's length must be >= 0");
-			if (extended && !in_elev)
-		G_fatal_error("For extended topology elevation map is required");	
-		
-		seg_treshold=deg2rad(seg_treshold);
-		
+    radians = (out_rad->answer != 0);
+    extended = (out_ext->answer != 0);
 
-    
+    if (seg_length <= 0)
+	G_fatal_error("Search's length must be > 0");
+    if (seg_treshold < 0 || seg_treshold > 180)
+	G_fatal_error("Treshold must be between 0 and 180");
+    if (seg_outlet < 0)
+	G_fatal_error("Segment's length must be >= 0");
+    if (extended && !in_elev)
+	G_fatal_error("For extended topology elevation map is required");
+
+    seg_treshold = deg2rad(seg_treshold);
+
     /* stores output options to variables */
- 	
-			if (!strcmp(in_ordering->answer, "none"))
-		ordering = NONE;
-			else if (!strcmp(in_ordering->answer, "hack"))
-		ordering = HACK;
-			else if (!strcmp(in_ordering->answer, "horton"))
-		ordering = HORTON;
-			else if (!strcmp(in_ordering->answer, "strahler"))
-		ordering = STRAHLER;
-    
+
+    if (!strcmp(in_ordering->answer, "none"))
+	ordering = NONE;
+    else if (!strcmp(in_ordering->answer, "hack"))
+	ordering = HACK;
+    else if (!strcmp(in_ordering->answer, "horton"))
+	ordering = HORTON;
+    else if (!strcmp(in_ordering->answer, "strahler"))
+	ordering = STRAHLER;
+
     nrows = G_window_rows();
     ncols = G_window_cols();
 
     create_base_maps();
-	  stream_number();
+    stream_number();
 
-	stack_max = stream_num;	/* stack's size depends on number of streams */
-		G_begin_distance_calculations();
+    stack_max = stream_num;	/* stack's size depends on number of streams */
+    G_begin_distance_calculations();
     init_streams();
     find_nodes();
-		do_cum_length();
-		strahler();
-			if (ordering==HORTON)
-		horton();
-			if (ordering==HACK)
-		hack();
-	
-		if(extended) {
-			G_message("Calculate tangents at stream joins...");
-			calc_tangent(ordering);
-		}
-		
-		G_message("Calculate segments...");
-		do_segments(seg_common,ordering);
-			if (extended)
-		add_missing_dirs(ordering);
-		close_vector();
+    do_cum_length();
+    strahler();
+    if (ordering == HORTON)
+	horton();
+    if (ordering == HACK)
+	hack();
 
+    if (extended) {
+	G_message("Calculate tangents at stream joins...");
+	calc_tangent(ordering);
+    }
+
+    G_message("Calculate segments...");
+    do_segments(seg_common, ordering);
+    if (extended)
+	add_missing_dirs(ordering);
+    close_vector();
+
     exit(EXIT_SUCCESS);
 }

Modified: grass-addons/raster/r.stream.angle/order.c
===================================================================
--- grass-addons/raster/r.stream.angle/order.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/order.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -5,7 +5,6 @@
 
 int strahler(void)
 {
-
     int i, j, done = 1;
     int cur_stream, next_stream;
     int max_strahler = 0, max_strahler_num;
@@ -33,14 +32,14 @@
 			done = 0;
 			break;	/* strahler is not determined, break for loop */
 		    }
-		    else if (s_streams[s_streams[cur_stream].trib[i]].
-			     strahler > max_strahler) {
+		    else if (s_streams[s_streams[cur_stream].trib[i]].strahler
+			     > max_strahler) {
 			max_strahler =
 			    s_streams[s_streams[cur_stream].trib[i]].strahler;
 			max_strahler_num = 1;
 		    }
-		    else if (s_streams[s_streams[cur_stream].trib[i]].
-			     strahler == max_strahler) {
+		    else if (s_streams[s_streams[cur_stream].trib[i]].strahler
+			     == max_strahler) {
 			++max_strahler_num;
 		    }
 		}		/* end determining strahler */
@@ -60,7 +59,6 @@
 
 int shreeve(void)
 {
-
     int i, j, done = 1;
     int cur_stream, next_stream;
     int max_shreeve = 0;
@@ -109,7 +107,6 @@
 
 int horton(void)
 {
-
     int *stack;
     int top, i, j;
     int cur_stream, cur_horton;
@@ -143,25 +140,26 @@
 		for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
 		    if (s_streams[s_streams[cur_stream].trib[i]].horton < 0) {
 
-			if (s_streams[s_streams[cur_stream].trib[i]].
-			    strahler > max_strahler) {
+			if (s_streams[s_streams[cur_stream].trib[i]].strahler
+			    > max_strahler) {
 			    max_strahler =
-				s_streams[s_streams[cur_stream].trib[i]].
-				strahler;
+				s_streams[s_streams[cur_stream].
+					  trib[i]].strahler;
 			    max_accum =
-				s_streams[s_streams[cur_stream].trib[i]].
-				accum;
+				s_streams[s_streams[cur_stream].
+					  trib[i]].accum;
 			    up_stream = s_streams[cur_stream].trib[i];
 
 			}
-			else if (s_streams[s_streams[cur_stream].trib[i]].
-				 strahler == max_strahler) {
+			else if (s_streams
+				 [s_streams[cur_stream].trib[i]].strahler ==
+				 max_strahler) {
 
-			    if (s_streams[s_streams[cur_stream].trib[i]].
-				accum > max_accum) {
+			    if (s_streams[s_streams[cur_stream].trib[i]].accum
+				> max_accum) {
 				max_accum =
-				    s_streams[s_streams[cur_stream].trib[i]].
-				    accum;
+				    s_streams[s_streams[cur_stream].
+					      trib[i]].accum;
 				up_stream = s_streams[cur_stream].trib[i];
 			    }
 			}
@@ -192,7 +190,6 @@
 
 int hack(void)
 {
-
     int *stack;
     int top, i, j;
     int cur_stream, cur_hack;
@@ -226,8 +223,8 @@
 			if (s_streams[s_streams[cur_stream].trib[i]].accum >
 			    max_accum) {
 			    max_accum =
-				s_streams[s_streams[cur_stream].trib[i]].
-				accum;
+				s_streams[s_streams[cur_stream].
+					  trib[i]].accum;
 			    up_stream = s_streams[cur_stream].trib[i];
 			}
 		    }

Modified: grass-addons/raster/r.stream.angle/seg_line.c
===================================================================
--- grass-addons/raster/r.stream.angle/seg_line.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/seg_line.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -5,364 +5,356 @@
  * 
  */
 
-int do_segments(SEGMENTS *segments, int ordering) {
+int do_segments(SEGMENTS * segments, int ordering)
+{
+    int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+    int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+    int i, j, k, s;
+    int done, in_loop;		/*flags */
+    int r, c;
+    int init, out, cur_stream, next_stream, cell_num;
+    int cur_order, next_order;
+    DIRCELLS *cells;
+    /* variables for line segmentation */
+    int small_seg_length;
+    int cell_down, cell_up, small_cell_down, small_cell_up;
+    float dir_down, dir_up, small_dir_down, small_dir_up;
+    int out_segs, cat_num;
+    int local_minimum_point;
+    float local_minimum = PI;
+    int seg_nums = 0;
+    int prev_index = 0;
+    int cells_in_segment = 1;
+    int seg_cats = 0;
+    double north_offset, west_offset, ns_res, ew_res;
 
-	int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
-	int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
-	
-	int i,j,k,s;
-	int done, in_loop; /*flags */
-	int r,c;
-	int init, out, cur_stream, next_stream, cell_num;
-	int strahler_springs;
-	int cur_order, next_order;
-	int init_stream,out_stream;
-	DIRCELLS* cells;
-	int tmp=0;
-	
-	/* variables for line segmentation */
-	int small_seg_length; 
-	int cell_down, cell_up, small_cell_down, small_cell_up;
-	float dir_down, dir_up, small_dir_down, small_dir_up;
-	int out_segs, cat_num;
-	int local_minimum_point;
-	float local_minimum=PI;
-	float local_minimum_tunning=PI;
-	int seg_nums=0;
-	int prev_index=0;
-	int cells_in_segment=1;
-	int seg_cats=0;
-	
-	double north_offset, west_offset, ns_res, ew_res;
-	
-	ns_res = window.ns_res;
-  ew_res = window.ew_res;
-  north_offset = window.north - 0.5 * ns_res;
-  west_offset = window.west + 0.5 * ew_res;
+    ns_res = window.ns_res;
+    ew_res = window.ew_res;
+    north_offset = window.north - 0.5 * ns_res;
+    west_offset = window.west + 0.5 * ew_res;
 
-	/* the rest of vector code is in function close_vect in io.c file */
-	
-	Segments = Vect_new_line_struct();
-  Cats = Vect_new_cats_struct();
-  
-    if (Vect_open_new(&Out, out_vector, 0)<0) 
+    /* the rest of vector code is in function close_vect in io.c file */
+
+    Segments = Vect_new_line_struct();
+    Cats = Vect_new_cats_struct();
+
+    if (Vect_open_new(&Out, out_vector, 0) < 0)
 	G_fatal_error(_("Unable to create new vector map <%s>"), out_vector);
 
-	/* global variables: 
-	 * seg_length
-	 * seg_outlet
-	 * seg_treshold
-	 */
-		
-	/* for STRAHLER ordering only*/
-	if (ordering==STRAHLER) {
-		for (i=1;i<stream_num+1;++i) {
-			if (s_streams[i].strahler>1) {
-				done=1;
-				for (j=0;j<s_streams[i].trib_num;++j) {
-					if (s_streams[s_streams[i].trib[j]].strahler==s_streams[i].strahler) {
-						done=0;
-						break;
-					}		
-				}
-					if(done)
-				springs[springs_num++]=s_streams[i].stream;
-			}		
-		}	
+    /* global variables: 
+     * seg_length
+     * seg_outlet
+     * seg_treshold
+     */
+
+    /* for STRAHLER ordering only */
+    if (ordering == STRAHLER) {
+	for (i = 1; i < stream_num + 1; ++i) {
+	    if (s_streams[i].strahler > 1) {
+		done = 1;
+		for (j = 0; j < s_streams[i].trib_num; ++j) {
+		    if (s_streams[s_streams[i].trib[j]].strahler ==
+			s_streams[i].strahler) {
+			done = 0;
+			break;
+		    }
+		}
+		if (done)
+		    springs[springs_num++] = s_streams[i].stream;
+	    }
 	}
-	
-	/* for NONE ordering only*/
-	if (ordering==NONE) {
-		for (i=1;i<stream_num+1;++i) {
-			if (s_streams[i].strahler>1) 
-				springs[springs_num++]=s_streams[i].stream;
-		}	
+    }
+
+    /* for NONE ordering only */
+    if (ordering == NONE) {
+	for (i = 1; i < stream_num + 1; ++i) {
+	    if (s_streams[i].strahler > 1)
+		springs[springs_num++] = s_streams[i].stream;
 	}
-	
-	/* end for strahler and none ordering only*/
-		
-	for(s=0;s<springs_num;++s) {
-		
-		init=springs[s];
-		cur_stream=init;
-		next_stream=s_streams[cur_stream].next_stream;
-		cell_num=s_streams[cur_stream].cell_num;
-		
-		/* find outlet stream */
-		while (cur_orders(cur_stream,ordering)==cur_orders(next_stream,ordering)) {
-			cur_stream = next_stream;
-			next_stream = s_streams[cur_stream].next_stream;
-			cell_num += s_streams[cur_stream].cell_num;
+    }
+
+    /* end for strahler and none ordering only */
+
+    for (s = 0; s < springs_num; ++s) {
+
+	init = springs[s];
+	cur_stream = init;
+	next_stream = s_streams[cur_stream].next_stream;
+	cell_num = s_streams[cur_stream].cell_num;
+
+	/* find outlet stream */
+	while (cur_orders(cur_stream, ordering) ==
+	       cur_orders(next_stream, ordering)) {
+	    cur_stream = next_stream;
+	    next_stream = s_streams[cur_stream].next_stream;
+	    cell_num += s_streams[cur_stream].cell_num;
+	}
+
+	/* reset */
+
+	out = cur_stream;
+	/* s_streams[init].out_stream=out; */
+	cur_stream = init;
+	next_stream = s_streams[cur_stream].next_stream;
+
+	while (cur_orders(cur_stream, ordering) ==
+	       cur_orders(next_stream, ordering)) {
+	    segments[cur_stream].init_stream = init;
+	    segments[cur_stream].out_stream = out;
+	    cur_stream = next_stream;
+	    next_stream = s_streams[cur_stream].next_stream;
+	}
+
+	segments[cur_stream].init_stream = init;
+	segments[cur_stream].out_stream = cur_stream;
+	segments[cur_stream].cell_num = cell_num;
+
+	r = s_streams[cur_stream].out_r;
+	c = s_streams[cur_stream].out_c;
+
+	cells = (DIRCELLS *) G_malloc((cell_num + 1) * sizeof(DIRCELLS));
+
+	for (j = 0; j < cell_num; ++j) {
+
+	    cells[j].r = r;
+	    cells[j].c = c;
+	    cells[j].dir_diff = -1;
+	    cells[j].small_dir_diff = -1;
+	    cells[j].candidate = 0;
+	    cells[j].tunning = 0;
+	    cells[j].decision = 0;
+
+
+	    for (i = 1; i < 9; ++i) {
+		if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1)
+		    || c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
+		    continue;
+
+		k = (i + 4) > 8 ? i - 4 : i + 4;
+
+		if (streams[r + nextr[i]][c + nextc[i]] > 0 &&
+		    dirs[r + nextr[i]][c + nextc[i]] == k) {
+
+		    cur_order = cur_orders(streams[r][c], ordering);
+		    next_order =
+			cur_orders(streams[r + nextr[i]][c + nextc[i]],
+				   ordering);
+
+		    if (next_order == cur_order) {
+
+			r += nextr[i];
+			c += nextc[i];
+			break;
+		    }
 		}
-		
-		/* reset */
-		
-		out=cur_stream;
-		//s_streams[init].out_stream=out;
-		cur_stream=init;
-		next_stream=s_streams[cur_stream].next_stream;
-		
-		
-		while (cur_orders(cur_stream,ordering)==cur_orders(next_stream,ordering)) {
-			segments[cur_stream].init_stream=init;
-			segments[cur_stream].out_stream=out;
-			cur_stream = next_stream;
-			next_stream = s_streams[cur_stream].next_stream;
-		}
+	    }
+	}			/* end for j, cells initialized! */
 
-		segments[cur_stream].init_stream=init;
-		segments[cur_stream].out_stream=cur_stream;
-		segments[cur_stream].cell_num=cell_num;
-	
-	
-		r=s_streams[cur_stream].out_r;
-		c=s_streams[cur_stream].out_c;
-	
-		cells=(DIRCELLS *) G_malloc((cell_num+1) * sizeof(DIRCELLS));
-		
-		
-		for (j=0; j<cell_num; ++j) {
-	
-			cells[j].r=r;
-			cells[j].c=c;
-			cells[j].dir_diff=-1;
-			cells[j].small_dir_diff=-1;
-			cells[j].candidate=0;
-			cells[j].tunning=0;
-			cells[j].decision=0;	
+	if (cell_num < seg_outlet + 1) {
 
+	    segments[cur_stream].dir_full =
+		calc_dir(cells[cell_num - 1].r, cells[cell_num - 1].c,
+			 cells[0].r, cells[0].c);
+	}
+	else {
 
-			for (i = 1; i < 9; ++i) {
-					if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) 
-						|| c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
-				continue;
-			
-				k = (i + 4) > 8 ? i - 4 : i + 4;
-			
-				if (streams[r + nextr[i]][c + nextc[i]] > 0 &&
-					dirs[r + nextr[i]][c + nextc[i]] == k) {
-						
-					cur_order=cur_orders(streams[r][c],ordering);
-					next_order=cur_orders(streams[r + nextr[i]][c + nextc[i]],ordering);
-				
-					if (next_order==cur_order) {
-						
-						r+=nextr[i];
-						c+=nextc[i];
-						break; 
-					}
-				} 
-			}
-		}  /* end for j, cells initialized! */
+	    segments[cur_stream].dir_init =
+		calc_dir(cells[seg_outlet].r, cells[seg_outlet].c,
+			 cells[0].r, cells[0].c);
 
-		if(cell_num<seg_outlet+1) {
-	
-			segments[cur_stream].dir_full=
-				calc_dir(cells[cell_num-1].r, cells[cell_num-1].c,
-				cells[0].r,cells[0].c);
-			
-		}	else {
-		
-			segments[cur_stream].dir_init=
-				calc_dir(cells[seg_outlet].r, cells[seg_outlet].c,
-				cells[0].r,cells[0].c);
-			
-			segments[cur_stream].dir_full=
-				calc_dir(cells[cell_num-1].r, cells[cell_num-1].c,
-				cells[0].r,cells[0].c);
-			
-			segments[cur_stream].dir_middle=
-				calc_dir(cells[(cell_num/2)+(seg_outlet/2)].r, 
-				cells[(cell_num/2)+(seg_outlet/2)].c,
-				cells[(cell_num/2)-(seg_outlet/2)].r,
-				cells[(cell_num/2)-(seg_outlet/2)].c);
-			
-			segments[cur_stream].dir_last=
-				calc_dir(cells[cell_num-1].r, cells[cell_num-1].c,
-				cells[(cell_num-1)-seg_outlet].r,cells[(cell_num-1)-seg_outlet].c);
-	
+	    segments[cur_stream].dir_full =
+		calc_dir(cells[cell_num - 1].r, cells[cell_num - 1].c,
+			 cells[0].r, cells[0].c);
+
+	    segments[cur_stream].dir_middle =
+		calc_dir(cells[(cell_num / 2) + (seg_outlet / 2)].r,
+			 cells[(cell_num / 2) + (seg_outlet / 2)].c,
+			 cells[(cell_num / 2) - (seg_outlet / 2)].r,
+			 cells[(cell_num / 2) - (seg_outlet / 2)].c);
+
+	    segments[cur_stream].dir_last =
+		calc_dir(cells[cell_num - 1].r, cells[cell_num - 1].c,
+			 cells[(cell_num - 1) - seg_outlet].r,
+			 cells[(cell_num - 1) - seg_outlet].c);
+	}
+
+	small_seg_length = seg_length / 3;
+
+	for (i = (seg_outlet + 1); i < (cell_num - seg_outlet); ++i) {
+
+	    cell_down = (i < seg_length) ? i : seg_length;
+	    cell_up =
+		(i >
+		 ((cell_num - 1) - seg_length)) ? ((cell_num - 1) -
+						   i) : seg_length;
+
+	    dir_down =
+		calc_dir(cells[i].r, cells[i].c,
+			 cells[i - cell_down].r, cells[i - cell_down].c);
+
+	    dir_up =
+		calc_dir(cells[i + cell_up].r, cells[i + cell_up].c,
+			 cells[i].r, cells[i].c);
+
+	    dir_up = (dir_up >= PI) ? dir_up - PI : dir_up + PI;	/* dir_up: direction upstream */
+
+	    cells[i].dir_diff =
+		fabs(dir_down - dir_up) > PI ?
+		(PI * 2) - fabs(dir_down - dir_up) : fabs(dir_down - dir_up);
+
+	    small_cell_down = (i < small_seg_length) ? i : small_seg_length;
+
+	    small_cell_up = (i > ((cell_num - 1) - small_seg_length)) ?
+		((cell_num - 1) - i) : small_seg_length;
+
+	    small_dir_down =
+		calc_dir(cells[i].r, cells[i].c,
+			 cells[i - small_cell_down].r,
+			 cells[i - small_cell_down].c);
+
+	    small_dir_up =
+		calc_dir(cells[i + small_cell_up].r,
+			 cells[i + small_cell_up].c, cells[i].r, cells[i].c);
+
+	    small_dir_up = (small_dir_up >= PI) ? small_dir_up - PI : small_dir_up + PI;	/* small_dir_up: direction upstream */
+
+	    cells[i].small_dir_diff =
+		fabs(small_dir_down - small_dir_up) > PI ?
+		(PI * 2) - fabs(small_dir_down - small_dir_up) :
+		fabs(small_dir_down - small_dir_up);
+
+	    cells[i].candidate = (cells[i].dir_diff < seg_treshold) ? 1 : 0;
+	    cells[i].tunning =
+		(cells[i].small_dir_diff < (seg_treshold * 0.8)) ? 1 : 0;
+	}			/* end for i */
+
+	/* decision system: prototype */
+
+	/* fine tunning */
+	local_minimum = PI;
+	cat_num = 0;
+	in_loop = 0;
+	out_segs = 0;
+
+	for (i = 0; i < cell_num; ++i) {
+	    if (cells[i].candidate) {
+
+		out_segs = 0;
+		if (local_minimum > cells[i].small_dir_diff) {
+		    local_minimum = cells[i].small_dir_diff;
+		    local_minimum_point = i;
+		    in_loop = 1;
+		}		/* end local minimum */
+
+	    }
+	    else if (!cells[i].candidate && in_loop) {
+
+		out_segs++;
+		if (out_segs == (seg_length / 5)) {
+		    cells[local_minimum_point].decision = 1;
+		    local_minimum = PI;
+		    in_loop = 0;
 		}
+	    }
+	}			/* end for: fine tunning */
 
-		small_seg_length = seg_length / 3;
+	/* cleaning */
+	for (i = 0, out_segs = 0; i < cell_num; ++i, out_segs++) {
 
-			for (i=(seg_outlet+1); i<(cell_num-seg_outlet);++i) {
-		
-				cell_down = (i<seg_length) ? i : seg_length;
-				cell_up = (i>((cell_num-1)-seg_length)) ? ((cell_num-1)-i) : seg_length;
-				
-				dir_down =
-					calc_dir(cells[i].r, cells[i].c,
-					cells[i-cell_down].r,cells[i-cell_down].c);
-					
-				dir_up = 
-					calc_dir(cells[i+cell_up].r,cells[i+cell_up].c,
-					cells[i].r,cells[i].c);
-					
-				dir_up = (dir_up >= PI) ? 
-					dir_up - PI : dir_up + PI; /* dir_up: direction upstream */
-				
-				cells[i].dir_diff =
-					fabs(dir_down-dir_up) > PI ? 
-					(PI * 2)-fabs(dir_down-dir_up) : fabs(dir_down-dir_up);
-				
-				small_cell_down = 
-					(i<small_seg_length) ? i : small_seg_length;
-				
-				small_cell_up = (i>((cell_num-1)-small_seg_length)) ? 
-					((cell_num-1)-i) : small_seg_length;
-				
-				small_dir_down =
-					calc_dir(cells[i].r, cells[i].c,
-					cells[i-small_cell_down].r,cells[i-small_cell_down].c);
-					
-				small_dir_up = 
-					calc_dir(cells[i+small_cell_up].r,cells[i+small_cell_up].c,
-					cells[i].r,cells[i].c);
-					
-				small_dir_up = (small_dir_up >= PI) ? 
-					small_dir_up - PI : 
-					small_dir_up + PI; /* small_dir_up: direction upstream */
-					
-				cells[i].small_dir_diff=
-					fabs(small_dir_down-small_dir_up) > PI ? 
-					(PI * 2)-fabs(small_dir_down-small_dir_up) : 
-					fabs(small_dir_down-small_dir_up);
-		
-				cells[i].candidate = (cells[i].dir_diff<seg_treshold) ? 1 : 0;
-				cells[i].tunning = (cells[i].small_dir_diff<(seg_treshold*0.8)) ? 1 : 0;
-			
-			} /* end for i */
-	
-			/* decision system: prototype */
-			
-		
-			/* fine tunning */
-			local_minimum=PI;
-			cat_num=0;
-			in_loop=0;
-			
-			for (i=0;i<cell_num;++i) { 
-				if(cells[i].candidate) {
-				
-					out_segs=0;
-					if (local_minimum>cells[i].small_dir_diff) {
-						local_minimum=cells[i].small_dir_diff;
-						local_minimum_point=i;
-						in_loop=1;
-					} /* end local minimum */
-				
-				}	else if (!cells[i].candidate && in_loop) {
-				
-					out_segs++;
-					if (out_segs==(seg_length/5)) {
-						cells[local_minimum_point].decision=1;
-						local_minimum=PI;
-						in_loop=0;
-					}
-				}
-			} /* end for: fine tunning */
-	
-			/* cleaning */
-			for (i=0,out_segs=0;i<cell_num;++i,out_segs++) { 
-				
-				if(cells[i].decision) {
-	
-					if(out_segs<seg_outlet && i>seg_outlet) { /* was: seg_length/5 */
-						cells[i].decision=0;
-						i=local_minimum_point;
-					}	else {
-						local_minimum_point=i;
-					}	
-					out_segs=0;
-				}
-			}
-			
-			seg_nums=0;
-			prev_index=0;
-			cells_in_segment=1;		
-			
-			for (i=0;i<cell_num;++i) {
-					if (cells[i].decision==1 || i==(cell_num-1))
-				seg_nums++;
-			}
+	    if (cells[i].decision) {
 
-			segments[cur_stream].seg_num=seg_nums;
-			segments[cur_stream].angles=(float*)G_malloc(seg_nums*sizeof(float));
-			segments[cur_stream].lengths=(float*)G_malloc(seg_nums*sizeof(float));
-			segments[cur_stream].drops=(float*)G_malloc(seg_nums*sizeof(float));
-			segments[cur_stream].cellnums=(int*)G_malloc(seg_nums*sizeof(int));
-			segments[cur_stream].cats=(int*)G_malloc(seg_nums*sizeof(int));
-			
-			seg_nums=0;
-			
-			/* we go from segment outlet upstream */
-			for (i=0;i<cell_num;++i) {
-				if (cells[i].decision==1 || i==(cell_num-1)) {
-					
-					segments[cur_stream].angles[seg_nums]=
-						calc_dir(cells[i].r,cells[i].c,
-						cells[prev_index].r,cells[prev_index].c);
+		if (out_segs < seg_outlet && i > seg_outlet) {	/* was: seg_length/5 */
+		    cells[i].decision = 0;
+		    i = local_minimum_point;
+		}
+		else {
+		    local_minimum_point = i;
+		}
+		out_segs = 0;
+	    }
+	}
 
-					segments[cur_stream].lengths[seg_nums]=
-						calc_length(cells[i].r,cells[i].c,
-						cells[prev_index].r,cells[prev_index].c);	
+	seg_nums = 0;
+	prev_index = 0;
+	cells_in_segment = 1;
 
-							if(extended) {
-					segments[cur_stream].drops[seg_nums]=
-						calc_drop(cells[i].r,cells[i].c,
-						cells[prev_index].r,cells[prev_index].c);	
-											
-					segments[cur_stream].cellnums[seg_nums]=
-						cells_in_segment;
-					}					
-					segments[cur_stream].cats[seg_nums++]=
-						++seg_cats;
+	for (i = 0; i < cell_num; ++i) {
+	    if (cells[i].decision == 1 || i == (cell_num - 1))
+		seg_nums++;
+	}
 
-					cells_in_segment=0;
-			
-						if (i<(cell_num-1)) 
-					prev_index=i+1;
-				}	
-				cells_in_segment++;
-			
-			}
+	segments[cur_stream].seg_num = seg_nums;
+	segments[cur_stream].angles =
+	    (float *)G_malloc(seg_nums * sizeof(float));
+	segments[cur_stream].lengths =
+	    (float *)G_malloc(seg_nums * sizeof(float));
+	segments[cur_stream].drops =
+	    (float *)G_malloc(seg_nums * sizeof(float));
+	segments[cur_stream].cellnums =
+	    (int *)G_malloc(seg_nums * sizeof(int));
+	segments[cur_stream].cats = (int *)G_malloc(seg_nums * sizeof(int));
 
-		Vect_reset_line(Segments);
-		Vect_reset_cats(Cats);
-		
-		for(i=cell_num-1;i>-1;--i) {
-			Vect_append_point(Segments, west_offset + cells[i].c * ew_res,
-				  north_offset - cells[i].r * ns_res, 0);
-									
-			if(cells[i].decision==1 || i==0) {/* start new line */
-								
-					if(dirs[cells[i].r][cells[i].c]>0) /*add point to create network */
-				
-				Vect_append_point(Segments, west_offset + 
-				(cells[i].c+nextc[dirs[cells[i].r][cells[i].c]]) * ew_res,
-				  north_offset - 
-				(cells[i].r+nextr[dirs[cells[i].r][cells[i].c]]) * ns_res, 0);
-				
-				Vect_cat_set(Cats, 1, segments[cur_stream].cats[--seg_nums]);
-				
-				Vect_write_line(&Out, GV_LINE, Segments, Cats);
-				Vect_reset_line(Segments);
-				Vect_reset_cats(Cats);
-			}
+	seg_nums = 0;
+
+	/* we go from segment outlet upstream */
+	for (i = 0; i < cell_num; ++i) {
+	    if (cells[i].decision == 1 || i == (cell_num - 1)) {
+
+		segments[cur_stream].angles[seg_nums] =
+		    calc_dir(cells[i].r, cells[i].c,
+			     cells[prev_index].r, cells[prev_index].c);
+
+		segments[cur_stream].lengths[seg_nums] =
+		    calc_length(cells[i].r, cells[i].c,
+				cells[prev_index].r, cells[prev_index].c);
+
+		if (extended) {
+		    segments[cur_stream].drops[seg_nums] =
+			calc_drop(cells[i].r, cells[i].c,
+				  cells[prev_index].r, cells[prev_index].c);
+
+		    segments[cur_stream].cellnums[seg_nums] =
+			cells_in_segment;
 		}
+		segments[cur_stream].cats[seg_nums++] = ++seg_cats;
 
-		G_free(cells);	
+		cells_in_segment = 0;
 
+		if (i < (cell_num - 1))
+		    prev_index = i + 1;
+	    }
+	    cells_in_segment++;
 	}
-	
-return 0;
-}
 
+	Vect_reset_line(Segments);
+	Vect_reset_cats(Cats);
 
-	
-	
+	for (i = cell_num - 1; i > -1; --i) {
+	    Vect_append_point(Segments, west_offset + cells[i].c * ew_res,
+			      north_offset - cells[i].r * ns_res, 0);
 
+	    if (cells[i].decision == 1 || i == 0) {	/* start new line */
 
-	
+		if (dirs[cells[i].r][cells[i].c] > 0)
+		    /*add point to create network */
+		    Vect_append_point(Segments, west_offset +
+				      (cells[i].c +
+				       nextc[dirs[cells[i].r][cells[i].c]]) *
+				      ew_res,
+				      north_offset - (cells[i].r +
+						      nextr[dirs[cells[i].r]
+							    [cells[i].c]]) *
+				      ns_res, 0);
 
+		Vect_cat_set(Cats, 1, segments[cur_stream].cats[--seg_nums]);
 
+		Vect_write_line(&Out, GV_LINE, Segments, Cats);
+		Vect_reset_line(Segments);
+		Vect_reset_cats(Cats);
+	    }
+	}
+	G_free(cells);
+    }
+
+    return 0;
+}

Modified: grass-addons/raster/r.stream.angle/tangent.c
===================================================================
--- grass-addons/raster/r.stream.angle/tangent.c	2010-06-03 11:20:21 UTC (rev 42454)
+++ grass-addons/raster/r.stream.angle/tangent.c	2010-06-03 11:30:25 UTC (rev 42455)
@@ -10,183 +10,185 @@
  * avaraged direction  of current segment and previous segment of the same order 
  * the number of cell used for this can be determinde by user by parameter step_num */
 
-int calc_tangent(int ordering) {
-	int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
-	int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
-	
-	int cur_stream; 
-	int prev_stream=0;
-	int cur_order, prev_order;
-	int r, c, d;
-	int rn, cn; /* row, col cur stream */
-	int next_rn, next_cn;
-	int rp, cp; /* row, col prev stream */
-	int next_rp, next_cp;
-	int init_stream;
-	int i, j, k;
-	int step_num;
-	int *tribstack;
-	int top=1;
-	int done;
-	
-	int out_dir;
+int calc_tangent(int ordering)
+{
+    int nextr[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
+    int nextc[9] = { 0, 1, 0, -1, -1, -1, 0, 1, 1 };
+    int cur_stream;
+    int prev_stream = 0;
+    int cur_order, prev_order;
+    int r, c, d;
+    int rn, cn;			/* row, col cur stream */
+    int next_rn, next_cn;
+    int rp, cp;			/* row, col prev stream */
+    int i, j, k;
+    int step_num;
+    int *tribstack;
+    int top = 1;
+    int done;
 
-	tribstack=(int *)G_malloc(stack_max * sizeof(int));
-		
-	for (j=0;j<outlets_num;++j) {
-		cur_stream = s_streams[outlets[j]].stream;
-		tribstack[0]=0;
-		top=1;
-			
-		do {
-			/* prev streams and tribs */
-			prev_stream=0;
-			cur_order=cur_orders(cur_stream,ordering);
-						
-			for (i=0;i<s_streams[cur_stream].trib_num;++i) {
-				prev_order=cur_orders(s_streams[cur_stream].trib[i],ordering);
-				if (prev_order==cur_order) {
-					prev_stream=s_streams[cur_stream].trib[i]; /* is continuation */
-					}	else {
-						tribstack[top++]=s_streams[cur_stream].trib[i];
+    tribstack = (int *)G_malloc(stack_max * sizeof(int));
 
-					} /* end else */
-			
-			} /* for i */
+    for (j = 0; j < outlets_num; ++j) {
+	cur_stream = s_streams[outlets[j]].stream;
+	tribstack[0] = 0;
+	top = 1;
 
-				if (!prev_stream) {
-					cur_stream=tribstack[--top];
-						if(cur_stream) /* what to do with the stack */
-					continue;
-						else
-					break;	
-				}
+	do {
+	    /* prev streams and tribs */
+	    prev_stream = 0;
+	    cur_order = cur_orders(cur_stream, ordering);
 
-				rn = r = s_streams[cur_stream].init_r;
-				cn = c = s_streams[cur_stream].init_c;
+	    for (i = 0; i < s_streams[cur_stream].trib_num; ++i) {
+		prev_order =
+		    cur_orders(s_streams[cur_stream].trib[i], ordering);
+		if (prev_order == cur_order) {
+		    prev_stream = s_streams[cur_stream].trib[i];	/* is continuation */
+		}
+		else {
+		    tribstack[top++] = s_streams[cur_stream].trib[i];
 
-			for (i = 1; i < 9; ++i) {
-				if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1) 
-				 || c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
+		}		/* end else */
+
+	    }			/* for i */
+
+	    if (!prev_stream) {
+		cur_stream = tribstack[--top];
+		if (cur_stream)	/* what to do with the stack */
+		    continue;
+		else
+		    break;
+	    }
+
+	    rn = r = s_streams[cur_stream].init_r;
+	    cn = c = s_streams[cur_stream].init_c;
+
+	    for (i = 1; i < 9; ++i) {
+		if (r + nextr[i] < 0 || r + nextr[i] > (nrows - 1)
+		    || c + nextc[i] < 0 || c + nextc[i] > (ncols - 1))
+		    continue;
+		if (streams[r + nextr[i]][c + nextc[i]] == prev_stream) {
+		    rp = r + nextr[i];
+		    cp = c + nextc[i];
+		}
+	    }			/* for i */
+
+	    /* calculate direction for cur_stream */
+	    step_num = seg_length;
+
+	    while (step_num) {
+
+		step_num--;
+
+		/* downstream */
+		d = dirs[rn][cn];
+		next_rn = rn + nextr[d];
+		next_cn = cn + nextc[d];
+
+		if (d < 1 ||	/* no more cells */
+		    next_rn < 0 || next_rn > (nrows - 1) ||
+		    next_cn < 0 || next_cn > (ncols - 1))
+
+		    break;	/* leave internal while */
+
+		rn = next_rn;
+		cn = next_cn;
+
+		/* upstream */
+		prev_order = 0;
+		done = 0;
+
+		for (i = 1; i < 9; ++i) {
+		    if (rp + nextr[i] < 0 || rp + nextr[i] > (nrows - 1)
+			|| cp + nextc[i] < 0 || cp + nextc[i] > (ncols - 1))
 			continue;
-				if (streams[r + nextr[i]][c + nextc[i]] == prev_stream) {
-					rp=r + nextr[i];
-					cp=c + nextc[i];
-				}
-			}	 /* for i */
-			
-			/* calculate direction for cur_stream */
-			step_num=seg_length;
 
-			while(step_num) {
-				
-				step_num--;
-				
-				//downstream
-				d=dirs[rn][cn];
-				next_rn=rn+nextr[d];
-				next_cn=cn+nextc[d];
-				
-					if (d<1|| //no more cells 
-				next_rn < 0 || next_rn > (nrows - 1) ||
-				next_cn < 0 || next_cn > (ncols - 1))
-				
-				break; //leave internal while
-				
-				rn=next_rn;
-				cn=next_cn;
-								
-				//upstream 
-				prev_order=0;
-				done=0;
-				
-				for (i = 1; i < 9; ++i) {
-  					if (rp + nextr[i] < 0 || rp + nextr[i] > (nrows - 1) 
-						|| cp + nextc[i] < 0 || cp + nextc[i] > (ncols - 1))
-					continue;
-					
-					k = (i + 4) > 8 ? i - 4 : i + 4;
-					
-					if (streams[rp + nextr[i]][cp + nextc[i]] > 0 &&
-						dirs[rp + nextr[i]][cp + nextc[i]] == k) {
-						prev_order=cur_orders(streams[rp + nextr[i]][cp + nextc[i]],ordering);
-						
-						if (prev_order==cur_order) {
-							rp+=nextr[i];
-							cp+=nextc[i];
-							done=1;
-							break; /* leave for */
-						}
-					} 
-				} /* end for i */
-	
-				if (!done) 
-			break; /* leave internal while */
-			} /* end internal while */
-		
-		s_streams[cur_stream].tangent_dir = calc_dir(rp,cp,rn,cn);
-		
-		cur_stream=prev_stream;
-		} while (top);	
-		
-	}	/* for j */
-G_free(tribstack);
-return 0;
+		    k = (i + 4) > 8 ? i - 4 : i + 4;
+
+		    if (streams[rp + nextr[i]][cp + nextc[i]] > 0 &&
+			dirs[rp + nextr[i]][cp + nextc[i]] == k) {
+			prev_order =
+			    cur_orders(streams[rp + nextr[i]][cp + nextc[i]],
+				       ordering);
+
+			if (prev_order == cur_order) {
+			    rp += nextr[i];
+			    cp += nextc[i];
+			    done = 1;
+			    break;	/* leave for */
+			}
+		    }
+		}		/* end for i */
+
+		if (!done)
+		    break;	/* leave internal while */
+	    }			/* end internal while */
+
+	    s_streams[cur_stream].tangent_dir = calc_dir(rp, cp, rn, cn);
+
+	    cur_stream = prev_stream;
+	} while (top);
+
+    }				/* for j */
+    G_free(tribstack);
+    return 0;
 }
 
 /* for strahler and none ordering 
  * add missing dirs for streams of higher order
  */
 
-int add_missing_dirs(int ordering) {
-	int i, j;
-	int out;
-	int seg_num;
-	float last_angle, pre_last_angle;
-	float last_angle_diff;
-	int last_length, pre_last_length;
-	
-	for (j = 1; j <= stream_num; ++j) {
-			
-			if (s_streams[j].strahler==1 || seg_common[j].init_stream!=j) 
-		continue;	
-			
-		out=seg_common[j].out_stream;
-		seg_num=seg_common[out].seg_num;
-		
-			if(seg_num==1) {
-		s_streams[j].tangent_dir=seg_common[out].angles[0];
-		continue;
-			}
-			
-		last_angle=seg_common[out].angles[seg_num-1];
-		last_length=seg_common[out].lengths[seg_num-1];
-		
-			if(last_length>seg_outlet) {
-		s_streams[j].tangent_dir=seg_common[out].angles[seg_num-1];
-		continue;
-			}	
-				
-		pre_last_angle=seg_common[out].angles[seg_num-2];
-		
-		last_angle_diff=(abs(pre_last_angle-last_angle)>PI) ? 
-			2*PI-abs(pre_last_angle-last_angle) : abs(pre_last_angle-last_angle);
-		
-		if(last_angle_diff>PI-seg_outlet) {
-			s_streams[j].tangent_dir=seg_common[out].angles[seg_num-2];
-		}	else {
-		s_streams[j].tangent_dir=		
-			calc_dir(s_streams[j].init_r,
-						s_streams[j].init_c,
-						s_streams[s_streams[j].next_stream].out_r,
-						s_streams[s_streams[j].next_stream].out_c);
-			
-		
-		}
-		/* calc stream direction using coordinates 
-		 * of two last segments of current stream segment */
+int add_missing_dirs(int ordering)
+{
+    int j;
+    int out;
+    int seg_num;
+    float last_angle, pre_last_angle;
+    float last_angle_diff;
+    int last_length;
 
-	} /* for j */
-return 0;	
+    for (j = 1; j <= stream_num; ++j) {
+
+	if (s_streams[j].strahler == 1 || seg_common[j].init_stream != j)
+	    continue;
+
+	out = seg_common[j].out_stream;
+	seg_num = seg_common[out].seg_num;
+
+	if (seg_num == 1) {
+	    s_streams[j].tangent_dir = seg_common[out].angles[0];
+	    continue;
+	}
+
+	last_angle = seg_common[out].angles[seg_num - 1];
+	last_length = seg_common[out].lengths[seg_num - 1];
+
+	if (last_length > seg_outlet) {
+	    s_streams[j].tangent_dir = seg_common[out].angles[seg_num - 1];
+	    continue;
+	}
+
+	pre_last_angle = seg_common[out].angles[seg_num - 2];
+
+	last_angle_diff = (abs(pre_last_angle - last_angle) > PI) ?
+	    2 * PI - abs(pre_last_angle - last_angle) : abs(pre_last_angle -
+							    last_angle);
+
+	if (last_angle_diff > PI - seg_outlet) {
+	    s_streams[j].tangent_dir = seg_common[out].angles[seg_num - 2];
+	}
+	else {
+	    s_streams[j].tangent_dir =
+		calc_dir(s_streams[j].init_r,
+			 s_streams[j].init_c,
+			 s_streams[s_streams[j].next_stream].out_r,
+			 s_streams[s_streams[j].next_stream].out_c);
+
+
+	}
+	/* calc stream direction using coordinates 
+	 * of two last segments of current stream segment */
+
+    }				/* for j */
+    return 0;
 }
-



More information about the grass-commit mailing list