GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
interp2d.c
Go to the documentation of this file.
1 
2 /*!
3  * \file interp2d.c
4  *
5  * \author
6  * Lubos Mitas (original program and various modifications)
7  *
8  * \author
9  * H. Mitasova,
10  * I. Kosinovsky, D. Gerdes,
11  * D. McCauley
12  * (GRASS4.1 version of the program and GRASS4.2 modifications)
13  *
14  * \author
15  * L. Mitas,
16  * H. Mitasova,
17  * I. Kosinovsky,
18  * D.Gerdes,
19  * D. McCauley
20  * (1993, 1995)
21  *
22  * \author modified by McCauley in August 1995
23  * \author modified by Mitasova in August 1995, Nov. 1996
24  * \author
25  * bug fixes(mask) and modification for variable smoothing
26  * Mitasova (Jan 1997)
27  *
28  * \copyright
29  * (C) 1993-1999 by Lubos Mitas and the GRASS Development Team
30  *
31  * \copyright
32  * This program is free software under the
33  * GNU General Public License (>=v2).
34  * Read the file COPYING that comes with GRASS
35  * for details.
36  */
37 
38 
39 #include <stdio.h>
40 #include <math.h>
41 #include <unistd.h>
42 
43 #include <grass/gis.h>
44 #include <grass/raster.h>
45 #include <grass/glocale.h>
46 #include <grass/bitmap.h>
47 
48 #include <grass/interpf.h>
49 
50 
51 #define CEULER .57721566
52 
53 
54 /*!
55  * Calculates grid values for a given segment
56  *
57  * Calculates grid for the given segment represented by data (contains
58  * n_rows, n_cols, ew_res,ns_res, and all points inside + overlap) using
59  * solutions of system of linear equations and interpolating functions
60  * interp() and interpder(). Also calls secpar() to compute slope, aspect
61  * and curvatures if required.
62  *
63  * *ertot* can be also called *RMS deviation of the interpolated surface*
64  */
65 int IL_grid_calc_2d(struct interp_params *params,
66  struct quaddata *data, /*!< given segment */
67  struct BM *bitmask, /*!< bitmask */
68  double zmin, double zmax, /*!< min and max input z-values */
69  double *zminac, double *zmaxac, /*!< min and max interp. z-values */
70  double *gmin, double *gmax, /*!< min and max interp. slope val. */
71  double *c1min, double *c1max, /*!< min and max interp. curv. val. */
72  double *c2min, double *c2max, /*!< min and max interp. curv. val. */
73  double *ertot, /*!< total interpolating func. error */
74  double *b, /*!< solutions of linear equations */
75  off_t offset1, /*!< offset for temp file writing */
76  double dnorm)
77 {
78 
79  /*
80  * C C INTERPOLATION BY FUNCTIONAL METHOD : TPS + complete regul.
81  * c
82  */
83  double x_or = data->x_orig;
84  double y_or = data->y_orig;
85  int n_rows = data->n_rows;
86  int n_cols = data->n_cols;
87  int n_points = data->n_points;
88  struct triple *points;
89  static double *w2 = NULL;
90  static double *w = NULL;
91  int cond1, cond2;
92  double r;
93  double stepix, stepiy, xx, xg, yg, xx2;
94  double rfsta2, /* cons, cons1, */ wm, dx, dy, dxx, dyy, dxy, h, bmgd1,
95  bmgd2;
96  double r2, gd1, gd2; /* for interpder() */
97  int n1, k, l, m;
98  int ngstc, nszc, ngstr, nszr;
99  double zz;
100  int bmask = 1;
101  static int first_time_z = 1;
102  off_t offset, offset2;
103  double fstar2 = params->fi * params->fi / 4.;
104  double tfsta2, tfstad;
105  double ns_res, ew_res;
106  double rsin = 0, rcos = 0, teta, scale = 0; /*anisotropy parameters - added by JH 2002 */
107  double xxr, yyr;
108 
109  if (params->theta) {
110  teta = params->theta / M_R2D; /* deg to rad */
111  rsin = sin(teta);
112  rcos = cos(teta);
113  }
114  if (params->scalex)
115  scale = params->scalex;
116 
117  ns_res = (((struct quaddata *)(data))->ymax -
118  ((struct quaddata *)(data))->y_orig) / data->n_rows;
119  ew_res = (((struct quaddata *)(data))->xmax -
120  ((struct quaddata *)(data))->x_orig) / data->n_cols;
121 
122  /* tfsta2 = fstar2 * 2.; modified after removing normalization of z */
123  tfsta2 = (fstar2 * 2.) / dnorm;
124  tfstad = tfsta2 / dnorm;
125  points = data->points;
126 
127  /*
128  * normalization
129  */
130  stepix = ew_res / dnorm;
131  stepiy = ns_res / dnorm;
132 
133  cond2 = ((params->adxx != NULL) || (params->adyy != NULL) ||
134  (params->adxy != NULL));
135  cond1 = ((params->adx != NULL) || (params->ady != NULL) || cond2);
136 
137  if (!w) {
138  if (!(w = (double *)G_malloc(sizeof(double) * (params->KMAX2 + 9)))) {
139  G_warning(_("Out of memory"));
140  return -1;
141  }
142  }
143  if (!w2) {
144  if (!(w2 = (double *)G_malloc(sizeof(double) * (params->KMAX2 + 9)))) {
145  G_warning(_("Out of memory"));
146  return -1;
147  }
148  }
149  n1 = n_points + 1;
150  /*
151  * C C INTERPOLATION * MOST INNER LOOPS ! C
152  */
153  ngstc = (int)(x_or / ew_res + 0.5) + 1;
154  nszc = ngstc + n_cols - 1;
155  ngstr = (int)(y_or / ns_res + 0.5) + 1;
156  nszr = ngstr + n_rows - 1;
157 
158 
159  for (k = ngstr; k <= nszr; k++) {
160  offset = offset1 * (k - 1); /* rows offset */
161  yg = (k - ngstr) * stepiy + stepiy / 2.; /* fixed by J.H. in July 01 */
162  for (m = 1; m <= n_points; m++) {
163  wm = yg - points[m - 1].y;
164  w[m] = wm;
165  w2[m] = wm * wm;
166  }
167  for (l = ngstc; l <= nszc; l++) {
168  if (bitmask != NULL)
169  /* if(params->maskmap != NULL) PK Apr 03 MASK support */
170  bmask = BM_get(bitmask, l - 1, k - 1); /*fixed by helena jan 97 */
171  /* if(bmask==0 || bmask==-1) fprintf(stderr, "bmask=%d, at (%d,%d)\n", bmask, l, k); */
172  xg = (l - ngstc) * stepix + stepix / 2.; /*fixed by J.H. in July 01 */
173  dx = 0.;
174  dy = 0.;
175  dxx = 0.;
176  dyy = 0.;
177  dxy = 0.;
178  zz = 0.;
179  if (bmask == 1) { /* compute everything for area which is
180  * not masked out */
181  h = b[0];
182  for (m = 1; m <= n_points; m++) {
183  xx = xg - points[m - 1].x;
184  if ((params->theta) && (params->scalex)) {
185  /* we run anisotropy */
186  xxr = xx * rcos + w[m] * rsin;
187  yyr = w[m] * rcos - xx * rsin;
188  xx2 = xxr * xxr;
189  w2[m] = yyr * yyr;
190  r2 = scale * xx2 + w2[m];
191  r = r2;
192  rfsta2 = scale * xx2 + w2[m];
193  }
194  else {
195  xx2 = xx * xx;
196  r2 = xx2 + w2[m];
197  r = r2;
198  rfsta2 = xx2 + w2[m];
199  }
200 
201  h = h + b[m] * params->interp(r, params->fi);
202  if (cond1) {
203  if (!params->interpder(r, params->fi, &gd1, &gd2))
204  return -1;
205  bmgd1 = b[m] * gd1;
206  dx = dx + bmgd1 * xx;
207  dy = dy + bmgd1 * w[m];
208  if (cond2) {
209  bmgd2 = b[m] * gd2;
210  dxx = dxx + bmgd2 * xx2 + bmgd1;
211  dyy = dyy + bmgd2 * w2[m] + bmgd1;
212  dxy = dxy + bmgd2 * xx * w[m];
213  }
214  }
215  }
216 
217  /* zz = (h * dnorm) + zmin; replaced by helena jan. 97 due to removing norma
218  lization of z and zm in segmen2d.c */
219  zz = h + zmin;
220  if (first_time_z) {
221  first_time_z = 0;
222  *zmaxac = *zminac = zz;
223  }
224  *zmaxac = amax1(zz, *zmaxac);
225  *zminac = amin1(zz, *zminac);
226  if ((zz > zmax + 0.1 * (zmax - zmin))
227  || (zz < zmin - 0.1 * (zmax - zmin))) {
228  static int once = 0;
229 
230  if (!once) {
231  once = 1;
232  G_warning(_("Overshoot - increase in tension suggested. "
233  "Overshoot occurs at (%d,%d) cell. "
234  "Z-value %f, zmin %f, zmax %f."),
235  l, k, zz, zmin, zmax);
236  }
237  }
238 
239  params->az[l] = (FCELL) zz;
240 
241  if (cond1) {
242  params->adx[l] = (FCELL) (-dx * tfsta2);
243  params->ady[l] = (FCELL) (-dy * tfsta2);
244  if (cond2) {
245  params->adxx[l] = (FCELL) (-dxx * tfstad);
246  params->adyy[l] = (FCELL) (-dyy * tfstad);
247  params->adxy[l] = (FCELL) (-dxy * tfstad);
248  }
249  }
250 
251  }
252  else {
253  Rast_set_d_null_value(params->az + l, 1);
254  /* fprintf (stderr, "zz=%f, az[l]=%f, c=%d\n", zz, params->az[l], l); */
255 
256  if (cond1) {
257  Rast_set_d_null_value(params->adx + l, 1);
258  Rast_set_d_null_value(params->ady + l, 1);
259  if (cond2) {
260  Rast_set_d_null_value(params->adxx + l, 1);
261  Rast_set_d_null_value(params->adyy + l, 1);
262  Rast_set_d_null_value(params->adxy + l, 1);
263  }
264  }
265  }
266 
267  }
268  if (cond1 && (params->deriv != 1)) {
269  if (params->secpar(params, ngstc, nszc, k, bitmask,
270  gmin, gmax, c1min, c1max, c2min, c2max, cond1,
271  cond2) < 0)
272  return -1;
273  }
274 
275  offset2 = (offset + ngstc - 1) * sizeof(FCELL);
276  if (params->wr_temp(params, ngstc, nszc, offset2) < 0)
277  return -1;
278  }
279  return 1;
280 }
#define G_malloc(n)
Definition: defs/gis.h:112
interp_fn * interp
Definition: interpf.h:101
double y_orig
Definition: dataquad.h:51
interpder_fn * interpder
Definition: interpf.h:102
double scalex
Definition: interpf.h:90
Definition: bitmap.h:17
double theta
Definition: interpf.h:89
double amax1(double, double)
Definition: minmax.c:54
DCELL * adxx
Definition: interpf.h:78
int IL_grid_calc_2d(struct interp_params *params, struct quaddata *data, struct BM *bitmask, double zmin, double zmax, double *zminac, double *zmaxac, double *gmin, double *gmax, double *c1min, double *c1max, double *c2min, double *c2max, double *ertot, double *b, off_t offset1, double dnorm)
Definition: interp2d.c:65
#define M_R2D
Definition: gis.h:153
#define NULL
Definition: ccmath.h:32
double x_orig
Definition: dataquad.h:50
DCELL * adx
Definition: interpf.h:78
secpar_fn * secpar
Definition: interpf.h:100
struct triple * points
Definition: dataquad.h:57
double l
Definition: r_raster.c:39
double b
Definition: r_raster.c:39
DCELL * az
Definition: interpf.h:78
double x
Definition: dataquad.h:42
int BM_get(struct BM *, int, int)
Gets &#39;val&#39; from the bitmap.
Definition: bitmap.c:223
double fi
Definition: interpf.h:80
double ymax
Definition: dataquad.h:53
float FCELL
Definition: gis.h:615
double amin1(double, double)
Definition: minmax.c:67
DCELL * ady
Definition: interpf.h:78
wr_temp_fn * wr_temp
Definition: interpf.h:103
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
int n_rows
Definition: dataquad.h:54
DCELL * adxy
Definition: interpf.h:78
int n_cols
Definition: dataquad.h:55
double y
Definition: dataquad.h:43
double r
Definition: r_raster.c:39
void Rast_set_d_null_value(DCELL *, int)
To set a number of DCELL raster values to NULL.
Definition: null_val.c:155
int n_points
Definition: dataquad.h:56
DCELL * adyy
Definition: interpf.h:78