[GRASS-SVN] r34453 - grass/trunk/lib/gis

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Nov 23 14:29:14 EST 2008


Author: glynn
Date: 2008-11-23 14:29:14 -0500 (Sun, 23 Nov 2008)
New Revision: 34453

Modified:
   grass/trunk/lib/gis/area.c
   grass/trunk/lib/gis/area_ellipse.c
   grass/trunk/lib/gis/area_poly1.c
   grass/trunk/lib/gis/area_sphere.c
Log:
Bundle static variables into per-file structures


Modified: grass/trunk/lib/gis/area.c
===================================================================
--- grass/trunk/lib/gis/area.c	2008-11-23 19:08:29 UTC (rev 34452)
+++ grass/trunk/lib/gis/area.c	2008-11-23 19:29:14 UTC (rev 34453)
@@ -16,20 +16,22 @@
 
 #include <grass/gis.h>
 
+static struct state {
+    struct Cell_head window;
+    double square_meters;
+    int projection;
 
-static struct Cell_head window;
-static double square_meters;
-static int projection;
+    double units_to_meters_squared;
 
-static double units_to_meters_squared = 0.0;
+    /* these next are for lat-long only */
+    int next_row;
+    double north_value;
+    double north;
+    double (*darea0) (double);
+} state;
 
-/* these next are for lat-long only */
-static int next_row;
-static double north_value;
-static double north;
-static double (*darea0) (double);
+static struct state *st = &state;
 
-
 /**
  * \brief Begin cell area calculations.
  *
@@ -52,26 +54,27 @@
     double a, e2;
     double factor;
 
-    G_get_set_window(&window);
-    switch (projection = window.proj) {
+    G_get_set_window(&st->window);
+    switch (st->projection = st->window.proj) {
     case PROJECTION_LL:
 	G_get_ellipsoid_parameters(&a, &e2);
 	if (e2) {
-	    G_begin_zone_area_on_ellipsoid(a, e2, window.ew_res / 360.0);
-	    darea0 = G_darea0_on_ellipsoid;
+	    G_begin_zone_area_on_ellipsoid(a, e2, st->window.ew_res / 360.0);
+	    st->darea0 = G_darea0_on_ellipsoid;
 	}
 	else {
-	    G_begin_zone_area_on_sphere(a, window.ew_res / 360.0);
-	    darea0 = G_darea0_on_sphere;
+	    G_begin_zone_area_on_sphere(a, st->window.ew_res / 360.0);
+	    st->darea0 = G_darea0_on_sphere;
 	}
-	next_row = 0;
-	north_value = darea0(north = window.north);
+	st->next_row = 0;
+	st->north = st->window.north;
+	st->north_value = st->darea0(st->north);
 	return 2;
     default:
-	square_meters = window.ns_res * window.ew_res;
+	st->square_meters = st->window.ns_res * st->window.ew_res;
 	factor = G_database_units_to_meters_factor();
 	if (factor > 0.0)
-	    square_meters *= (factor * factor);
+	    st->square_meters *= (factor * factor);
 	return (factor > 0.0);
     }
 }
@@ -93,17 +96,20 @@
     double south_value;
     double cell_area;
 
-    if (projection != PROJECTION_LL)
-	return square_meters;
+    if (st->projection != PROJECTION_LL)
+	return st->square_meters;
 
-    if (row != next_row)
-	north_value = darea0(north = window.north - row * window.ns_res);
+    if (row != st->next_row) {
+	st->north = st->window.north - row * st->window.ns_res;
+	st->north_value = st->darea0(st->north);
+    }
 
-    south_value = darea0(north -= window.ns_res);
-    cell_area = north_value - south_value;
+    st->north -= st->window.ns_res;
+    south_value = st->darea0(st->north);
+    cell_area = st->north_value - south_value;
 
-    next_row = row + 1;
-    north_value = south_value;
+    st->next_row = row + 1;
+    st->north_value = south_value;
 
     return cell_area;
 }
@@ -125,17 +131,17 @@
     double a, e2;
     double factor;
 
-    if ((projection = G_projection()) == PROJECTION_LL) {
+    if ((st->projection = G_projection()) == PROJECTION_LL) {
 	G_get_ellipsoid_parameters(&a, &e2);
 	G_begin_ellipsoid_polygon_area(a, e2);
 	return 2;
     }
     factor = G_database_units_to_meters_factor();
     if (factor > 0.0) {
-	units_to_meters_squared = factor * factor;
+	st->units_to_meters_squared = factor * factor;
 	return 1;
     }
-    units_to_meters_squared = 1.0;
+    st->units_to_meters_squared = 1.0;
     return 0;
 }
 
@@ -166,10 +172,10 @@
 {
     double area;
 
-    if (projection == PROJECTION_LL)
+    if (st->projection == PROJECTION_LL)
 	area = G_ellipsoid_polygon_area(x, y, n);
     else
-	area = G_planimetric_polygon_area(x, y, n) * units_to_meters_squared;
+	area = G_planimetric_polygon_area(x, y, n) * st->units_to_meters_squared;
 
     return area;
 }

Modified: grass/trunk/lib/gis/area_ellipse.c
===================================================================
--- grass/trunk/lib/gis/area_ellipse.c	2008-11-23 19:08:29 UTC (rev 34452)
+++ grass/trunk/lib/gis/area_ellipse.c	2008-11-23 19:29:14 UTC (rev 34453)
@@ -18,11 +18,13 @@
 #include <grass/gis.h>
 #include "pi.h"
 
+static struct state {
+    double E;
+    double M;
+} state;
 
-static double E;
-static double M;
+static struct state *st = &state;
 
-
 /*
  * a is semi-major axis, e2 is eccentricity squared, s is a scale factor
  * code will fail if e2==0 (sphere)
@@ -49,8 +51,8 @@
 
 int G_begin_zone_area_on_ellipsoid(double a, double e2, double s)
 {
-    E = sqrt(e2);
-    M = s * a * a * M_PI * (1 - e2) / E;
+    st->E = sqrt(e2);
+    st->M = s * a * a * M_PI * (1 - e2) / st->E;
 
     return 0;
 }
@@ -70,9 +72,9 @@
 {
     double x;
 
-    x = E * sin(Radians(lat));
+    x = st->E * sin(Radians(lat));
 
-    return (M * (x / (1.0 - x * x) + 0.5 * log((1.0 + x) / (1.0 - x))));
+    return (st->M * (x / (1.0 - x * x) + 0.5 * log((1.0 + x) / (1.0 - x))));
 }
 
 

Modified: grass/trunk/lib/gis/area_poly1.c
===================================================================
--- grass/trunk/lib/gis/area_poly1.c	2008-11-23 19:08:29 UTC (rev 34452)
+++ grass/trunk/lib/gis/area_poly1.c	2008-11-23 19:29:14 UTC (rev 34453)
@@ -21,16 +21,16 @@
 
 #define TWOPI M_PI + M_PI
 
-static double QA, QB, QC;
-static double QbarA, QbarB, QbarC, QbarD;
+static struct state {
+    double QA, QB, QC;
+    double QbarA, QbarB, QbarC, QbarD;
+    double AE;  /** a^2(1-e^2) */
+    double Qp;  /** Q at the north pole */
+    double E;   /** Area of the earth */
+} state;
 
-static double AE;  /** a^2(1-e^2) */
+static struct state *st = &state;
 
-static double Qp;  /** Q at the north pole */
-
-static double E;   /** Area of the earth */
-
-
 static double Q(double x)
 {
     double sinx, sinx2;
@@ -38,7 +38,7 @@
     sinx = sin(x);
     sinx2 = sinx * sinx;
 
-    return sinx * (1 + sinx2 * (QA + sinx2 * (QB + sinx2 * QC)));
+    return sinx * (1 + sinx2 * (st->QA + sinx2 * (st->QB + sinx2 * st->QC)));
 }
 
 static double Qbar(double x)
@@ -48,7 +48,7 @@
     cosx = cos(x);
     cosx2 = cosx * cosx;
 
-    return cosx * (QbarA + cosx2 * (QbarB + cosx2 * (QbarC + cosx2 * QbarD)));
+    return cosx * (st->QbarA + cosx2 * (st->QbarB + cosx2 * (st->QbarC + cosx2 * st->QbarD)));
 }
 
 
@@ -71,21 +71,21 @@
     e4 = e2 * e2;
     e6 = e4 * e2;
 
-    AE = a * a * (1 - e2);
+    st->AE = a * a * (1 - e2);
 
-    QA = (2.0 / 3.0) * e2;
-    QB = (3.0 / 5.0) * e4;
-    QC = (4.0 / 7.0) * e6;
+    st->QA = (2.0 / 3.0) * e2;
+    st->QB = (3.0 / 5.0) * e4;
+    st->QC = (4.0 / 7.0) * e6;
 
-    QbarA = -1.0 - (2.0 / 3.0) * e2 - (3.0 / 5.0) * e4 - (4.0 / 7.0) * e6;
-    QbarB = (2.0 / 9.0) * e2 + (2.0 / 5.0) * e4 + (4.0 / 7.0) * e6;
-    QbarC = -(3.0 / 25.0) * e4 - (12.0 / 35.0) * e6;
-    QbarD = (4.0 / 49.0) * e6;
+    st->QbarA = -1.0 - (2.0 / 3.0) * e2 - (3.0 / 5.0) * e4 - (4.0 / 7.0) * e6;
+    st->QbarB = (2.0 / 9.0) * e2 + (2.0 / 5.0) * e4 + (4.0 / 7.0) * e6;
+    st->QbarC = -(3.0 / 25.0) * e4 - (12.0 / 35.0) * e6;
+    st->QbarD = (4.0 / 49.0) * e6;
 
-    Qp = Q(M_PI_2);
-    E = 4 * M_PI * Qp * AE;
-    if (E < 0.0)
-	E = -E;
+    st->Qp = Q(M_PI_2);
+    st->E = 4 * M_PI * st->Qp * st->AE;
+    if (st->E < 0.0)
+	st->E = -st->E;
 
     return 0;
 }
@@ -136,12 +136,12 @@
 		x1 += TWOPI;
 
 	dx = x2 - x1;
-	area += dx * (Qp - Q(y2));
+	area += dx * (st->Qp - Q(y2));
 
 	if ((dy = y2 - y1) != 0.0)
 	    area += dx * Q(y2) - (dx / dy) * (Qbar2 - Qbar1);
     }
-    if ((area *= AE) < 0.0)
+    if ((area *= st->AE) < 0.0)
 	area = -area;
 
     /* kludge - if polygon circles the south pole the area will be
@@ -149,10 +149,10 @@
      * the difference between total surface area of the earth and
      * the "north pole" area.
      */
-    if (area > E)
-	area = E;
-    if (area > E / 2)
-	area = E - area;
+    if (area > st->E)
+	area = st->E;
+    if (area > st->E / 2)
+	area = st->E - area;
 
     return area;
 }

Modified: grass/trunk/lib/gis/area_sphere.c
===================================================================
--- grass/trunk/lib/gis/area_sphere.c	2008-11-23 19:08:29 UTC (rev 34452)
+++ grass/trunk/lib/gis/area_sphere.c	2008-11-23 19:29:14 UTC (rev 34453)
@@ -19,8 +19,11 @@
 #include "pi.h"
 
 
-static double M;
+static struct state {
+    double M;
+} state;
 
+static struct state *st = &state;
 
 /*
  * r is radius of sphere, s is a scaling factor
@@ -41,7 +44,7 @@
 
 int G_begin_zone_area_on_sphere(double r, double s)
 {
-    return (M = s * 2.0 * r * r * M_PI);
+    return (st->M = s * 2.0 * r * r * M_PI);
 }
 
 
@@ -54,7 +57,7 @@
 
 double G_darea0_on_sphere(double lat)
 {
-    return (M * sin(Radians(lat)));
+    return (st->M * sin(Radians(lat)));
 }
 
 



More information about the grass-commit mailing list