File: interp.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (217 lines) | stat: -rw-r--r-- 6,264 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*!
   \file lib/raster/interp.c

   \brief Raster Library - Interpolation methods

   (C) 2001-2009,2013 GRASS Development Team

   This program is free software under the GNU General Public License
   (>=v2). Read the file COPYING that comes with GRASS for details.

   \author Original author CERL
 */

#include <math.h>
#include <string.h>

#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/glocale.h>

DCELL Rast_interp_linear(double u, DCELL c0, DCELL c1)
{
    return u * (c1 - c0) + c0;
}

DCELL Rast_interp_bilinear(double u, double v, DCELL c00, DCELL c01, DCELL c10,
                           DCELL c11)
{
    DCELL c0 = Rast_interp_linear(u, c00, c01);
    DCELL c1 = Rast_interp_linear(u, c10, c11);

    return Rast_interp_linear(v, c0, c1);
}

DCELL Rast_interp_cubic(double u, DCELL c0, DCELL c1, DCELL c2, DCELL c3)
{
    return (u * (u * (u * (c3 - 3 * c2 + 3 * c1 - c0) +
                      (-c3 + 4 * c2 - 5 * c1 + 2 * c0)) +
                 (c2 - c0)) +
            2 * c1) /
           2;
}

DCELL Rast_interp_bicubic(double u, double v, DCELL c00, DCELL c01, DCELL c02,
                          DCELL c03, DCELL c10, DCELL c11, DCELL c12, DCELL c13,
                          DCELL c20, DCELL c21, DCELL c22, DCELL c23, DCELL c30,
                          DCELL c31, DCELL c32, DCELL c33)
{
    DCELL c0 = Rast_interp_cubic(u, c00, c01, c02, c03);
    DCELL c1 = Rast_interp_cubic(u, c10, c11, c12, c13);
    DCELL c2 = Rast_interp_cubic(u, c20, c21, c22, c23);
    DCELL c3 = Rast_interp_cubic(u, c30, c31, c32, c33);

    return Rast_interp_cubic(v, c0, c1, c2, c3);
}

DCELL Rast_interp_lanczos(double u, double v, DCELL *c)
{
    double uweight[5], vweight[5], d, d_pi;
    double usum, vsum;
    DCELL c0, c1, c2, c3, c4;
    double sind, sincd1, sincd2;

    d_pi = u * M_PI;
    sind = 2 * sin(d_pi);
    sincd1 = sind * sin(d_pi / 2);
    uweight[2] = (u == 0 ? 1 : sincd1 / (d_pi * d_pi));
    usum = uweight[2];

    d = u + 2;
    d_pi = d * M_PI;
    if (d > 2)
        uweight[0] = 0.;
    else
        uweight[0] = (d == 0 ? 1 : -sincd1 / (d_pi * d_pi));
    usum += uweight[0];

    d = u + 1.;
    d_pi = d * M_PI;
    sincd2 = sind * sin(d_pi / 2);
    uweight[1] = (d == 0 ? 1 : -sincd2 / (d_pi * d_pi));
    usum += uweight[1];

    d = u - 1.;
    d_pi = d * M_PI;
    uweight[3] = (d == 0 ? 1 : sincd2 / (d_pi * d_pi));
    usum += uweight[3];

    d = u - 2.;
    d_pi = d * M_PI;
    if (d < -2)
        uweight[4] = 0.;
    else
        uweight[4] = (d == 0 ? 1 : -sincd1 / (d_pi * d_pi));
    usum += uweight[4];

    d_pi = v * M_PI;
    sind = 2 * sin(d_pi);
    sincd1 = sind * sin(d_pi / 2);
    vweight[2] = (v == 0 ? 1 : sincd1 / (d_pi * d_pi));
    vsum = vweight[2];

    d = v + 2;
    d_pi = d * M_PI;
    if (d > 2)
        vweight[0] = 0;
    else
        vweight[0] = (d == 0 ? 1 : -sincd1 / (d_pi * d_pi));
    vsum += vweight[0];

    d = v + 1.;
    d_pi = d * M_PI;
    sincd2 = sind * sin(d_pi / 2);
    vweight[1] = (d == 0 ? 1 : -sincd2 / (d_pi * d_pi));
    vsum += vweight[1];

    d = v - 1.;
    d_pi = d * M_PI;
    vweight[3] = (d == 0 ? 1 : sincd2 / (d_pi * d_pi));
    vsum += vweight[3];

    d = v - 2.;
    d_pi = d * M_PI;
    if (d < -2)
        vweight[4] = 0;
    else
        vweight[4] = (d == 0 ? 1 : -sincd1 / (d_pi * d_pi));
    vsum += vweight[4];

    c0 = (c[0] * uweight[0] + c[1] * uweight[1] + c[2] * uweight[2] +
          c[3] * uweight[3] + c[4] * uweight[4]);
    c1 = (c[5] * uweight[0] + c[6] * uweight[1] + c[7] * uweight[2] +
          c[8] * uweight[3] + c[9] * uweight[4]);
    c2 = (c[10] * uweight[0] + c[11] * uweight[1] + c[12] * uweight[2] +
          c[13] * uweight[3] + c[14] * uweight[4]);
    c3 = (c[15] * uweight[0] + c[16] * uweight[1] + c[17] * uweight[2] +
          c[18] * uweight[3] + c[19] * uweight[4]);
    c4 = (c[20] * uweight[0] + c[21] * uweight[1] + c[22] * uweight[2] +
          c[23] * uweight[3] + c[24] * uweight[4]);

    return ((c0 * vweight[0] + c1 * vweight[1] + c2 * vweight[2] +
             c3 * vweight[3] + c4 * vweight[4]) /
            (usum * vsum));
}

DCELL Rast_interp_cubic_bspline(double u, DCELL c0, DCELL c1, DCELL c2,
                                DCELL c3)
{
    return (u * (u * (u * (c3 - 3 * c2 + 3 * c1 - c0) +
                      (3 * c2 - 6 * c1 + 3 * c0)) +
                 (3 * c2 - 3 * c0)) +
            c2 + 4 * c1 + c0) /
           6;
}

DCELL Rast_interp_bicubic_bspline(double u, double v, DCELL c00, DCELL c01,
                                  DCELL c02, DCELL c03, DCELL c10, DCELL c11,
                                  DCELL c12, DCELL c13, DCELL c20, DCELL c21,
                                  DCELL c22, DCELL c23, DCELL c30, DCELL c31,
                                  DCELL c32, DCELL c33)
{
    DCELL c0 = Rast_interp_cubic_bspline(u, c00, c01, c02, c03);
    DCELL c1 = Rast_interp_cubic_bspline(u, c10, c11, c12, c13);
    DCELL c2 = Rast_interp_cubic_bspline(u, c20, c21, c22, c23);
    DCELL c3 = Rast_interp_cubic_bspline(u, c30, c31, c32, c33);

    return Rast_interp_cubic_bspline(v, c0, c1, c2, c3);
}

/*!
   \brief Get interpolation method from the option.

   Calls G_fatal_error() on unknown interpolation method.

   Supported methods:
   - NEAREST
   - BILINEAR
   - CUBIC

   \code
   int interp_method
   struct Option *opt_method;

   opt_method = G_define_standard_option(G_OPT_R_INTERP_TYPE);

   if (G_parser(argc, argv))
   exit(EXIT_FAILURE);

   interp_method = G_option_to_interp_type(opt_method);
   \endcode

   \param option pointer to interpolation option

   \return interpolation method code
 */
int Rast_option_to_interp_type(const struct Option *option)
{
    int interp_type;

    interp_type = INTERP_UNKNOWN;
    if (option->answer) {
        if (strcmp(option->answer, "nearest") == 0) {
            interp_type = INTERP_NEAREST;
        }
        else if (strcmp(option->answer, "bilinear") == 0) {
            interp_type = INTERP_BILINEAR;
        }
        else if (strcmp(option->answer, "bicubic") == 0) {
            interp_type = INTERP_BICUBIC;
        }
    }

    if (interp_type == INTERP_UNKNOWN)
        G_fatal_error(_("Unknown interpolation method: %s"), option->answer);

    return interp_type;
}