瀏覽代碼

Bundle static variables into per-file structures

git-svn-id: https://svn.osgeo.org/grass/grass/trunk@34453 15284696-431f-4ddb-bdfa-cd5b030d7da7
Glynn Clements 16 年之前
父節點
當前提交
0e0697431b
共有 4 個文件被更改,包括 82 次插入71 次删除
  1. 39 33
      lib/gis/area.c
  2. 9 7
      lib/gis/area_ellipse.c
  3. 28 28
      lib/gis/area_poly1.c
  4. 6 3
      lib/gis/area_sphere.c

+ 39 - 33
lib/gis/area.c

@@ -16,19 +16,21 @@
 
 
 #include <grass/gis.h>
 #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 */
-static int next_row;
-static double north_value;
-static double north;
-static double (*darea0) (double);
+    /* these next are for lat-long only */
+    int next_row;
+    double north_value;
+    double north;
+    double (*darea0) (double);
+} state;
 
 
+static struct state *st = &state;
 
 
 /**
 /**
  * \brief Begin cell area calculations.
  * \brief Begin cell area calculations.
@@ -52,26 +54,27 @@ int G_begin_cell_area_calculations(void)
     double a, e2;
     double a, e2;
     double factor;
     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:
     case PROJECTION_LL:
 	G_get_ellipsoid_parameters(&a, &e2);
 	G_get_ellipsoid_parameters(&a, &e2);
 	if (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 {
 	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;
 	return 2;
     default:
     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();
 	factor = G_database_units_to_meters_factor();
 	if (factor > 0.0)
 	if (factor > 0.0)
-	    square_meters *= (factor * factor);
+	    st->square_meters *= (factor * factor);
 	return (factor > 0.0);
 	return (factor > 0.0);
     }
     }
 }
 }
@@ -93,17 +96,20 @@ double G_area_of_cell_at_row(int row)
     double south_value;
     double south_value;
     double cell_area;
     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;
     return cell_area;
 }
 }
@@ -125,17 +131,17 @@ int G_begin_polygon_area_calculations(void)
     double a, e2;
     double a, e2;
     double factor;
     double factor;
 
 
-    if ((projection = G_projection()) == PROJECTION_LL) {
+    if ((st->projection = G_projection()) == PROJECTION_LL) {
 	G_get_ellipsoid_parameters(&a, &e2);
 	G_get_ellipsoid_parameters(&a, &e2);
 	G_begin_ellipsoid_polygon_area(a, e2);
 	G_begin_ellipsoid_polygon_area(a, e2);
 	return 2;
 	return 2;
     }
     }
     factor = G_database_units_to_meters_factor();
     factor = G_database_units_to_meters_factor();
     if (factor > 0.0) {
     if (factor > 0.0) {
-	units_to_meters_squared = factor * factor;
+	st->units_to_meters_squared = factor * factor;
 	return 1;
 	return 1;
     }
     }
-    units_to_meters_squared = 1.0;
+    st->units_to_meters_squared = 1.0;
     return 0;
     return 0;
 }
 }
 
 
@@ -166,10 +172,10 @@ double G_area_of_polygon(const double *x, const double *y, int n)
 {
 {
     double area;
     double area;
 
 
-    if (projection == PROJECTION_LL)
+    if (st->projection == PROJECTION_LL)
 	area = G_ellipsoid_polygon_area(x, y, n);
 	area = G_ellipsoid_polygon_area(x, y, n);
     else
     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;
     return area;
 }
 }

+ 9 - 7
lib/gis/area_ellipse.c

@@ -18,10 +18,12 @@
 #include <grass/gis.h>
 #include <grass/gis.h>
 #include "pi.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
  * a is semi-major axis, e2 is eccentricity squared, s is a scale factor
@@ -49,8 +51,8 @@ static double M;
 
 
 int G_begin_zone_area_on_ellipsoid(double a, double e2, double s)
 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;
     return 0;
 }
 }
@@ -70,9 +72,9 @@ double G_darea0_on_ellipsoid(double lat)
 {
 {
     double x;
     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))));
 }
 }
 
 
 
 

+ 28 - 28
lib/gis/area_poly1.c

@@ -21,15 +21,15 @@
 
 
 #define TWOPI M_PI + M_PI
 #define TWOPI M_PI + M_PI
 
 
-static double QA, QB, QC;
-static double QbarA, QbarB, QbarC, QbarD;
-
-static double AE;  /** a^2(1-e^2) */
-
-static double Qp;  /** Q at the north pole */
-
-static double E;   /** Area of the earth */
+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 struct state *st = &state;
 
 
 static double Q(double x)
 static double Q(double x)
 {
 {
@@ -38,7 +38,7 @@ static double Q(double x)
     sinx = sin(x);
     sinx = sin(x);
     sinx2 = sinx * sinx;
     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)
 static double Qbar(double x)
@@ -48,7 +48,7 @@ static double Qbar(double x)
     cosx = cos(x);
     cosx = cos(x);
     cosx2 = cosx * cosx;
     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 @@ int G_begin_ellipsoid_polygon_area(double a, double e2)
     e4 = e2 * e2;
     e4 = e2 * e2;
     e6 = e4 * 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;
     return 0;
 }
 }
@@ -136,12 +136,12 @@ double G_ellipsoid_polygon_area(const double *lon, const double *lat, int n)
 		x1 += TWOPI;
 		x1 += TWOPI;
 
 
 	dx = x2 - x1;
 	dx = x2 - x1;
-	area += dx * (Qp - Q(y2));
+	area += dx * (st->Qp - Q(y2));
 
 
 	if ((dy = y2 - y1) != 0.0)
 	if ((dy = y2 - y1) != 0.0)
 	    area += dx * Q(y2) - (dx / dy) * (Qbar2 - Qbar1);
 	    area += dx * Q(y2) - (dx / dy) * (Qbar2 - Qbar1);
     }
     }
-    if ((area *= AE) < 0.0)
+    if ((area *= st->AE) < 0.0)
 	area = -area;
 	area = -area;
 
 
     /* kludge - if polygon circles the south pole the area will be
     /* kludge - if polygon circles the south pole the area will be
@@ -149,10 +149,10 @@ double G_ellipsoid_polygon_area(const double *lon, const double *lat, int n)
      * the difference between total surface area of the earth and
      * the difference between total surface area of the earth and
      * the "north pole" area.
      * 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;
     return area;
 }
 }

+ 6 - 3
lib/gis/area_sphere.c

@@ -19,8 +19,11 @@
 #include "pi.h"
 #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
  * r is radius of sphere, s is a scaling factor
@@ -41,7 +44,7 @@ static double M;
 
 
 int G_begin_zone_area_on_sphere(double r, double s)
 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 @@ int G_begin_zone_area_on_sphere(double r, double s)
 
 
 double G_darea0_on_sphere(double lat)
 double G_darea0_on_sphere(double lat)
 {
 {
-    return (M * sin(Radians(lat)));
+    return (st->M * sin(Radians(lat)));
 }
 }