File: nnai.c

package info (click to toggle)
plplot 5.10.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 26,280 kB
  • ctags: 13,512
  • sloc: ansic: 83,001; xml: 27,081; ada: 18,878; cpp: 15,966; tcl: 11,651; python: 7,075; f90: 7,058; ml: 6,974; java: 6,665; perl: 5,029; sh: 2,210; makefile: 199; lisp: 75; sed: 25; fortran: 7
file content (431 lines) | stat: -rw-r--r-- 11,665 bytes parent folder | download | duplicates (6)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//--------------------------------------------------------------------------
//
// File:           nnai.c
//
// Created:        15/11/2002
//
// Author:         Pavel Sakov
//                 CSIRO Marine Research
//
// Purpose:        Code for:
//                 -- Natural Neighbours Array Interpolator
//
// Description:    `nnai' is a tructure for conducting
//                 consequitive Natural Neighbours interpolations on a given
//                 spatial data set in a given array of points. It allows to
//                 modify Z coordinate of data in between interpolations.
//                 `nnai' is the fastest of the three Natural
//                 Neighbours interpolators in `nn' library.
//
// Revisions:      None
//
//--------------------------------------------------------------------------

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "nn.h"
#include "delaunay.h"
#include "nan.h"

typedef struct
{
    int   nvertices;
    int   * vertices;           // vertex indices [nvertices]
    double* weights;            // vertex weights [nvertices]
} nn_weights;

struct nnai
{
    delaunay  * d;
    double    wmin;
    double    n;                // number of output points
    double    * x;              // [n]
    double    * y;              // [n]
    nn_weights* weights;
};

void nn_quit( const char* format, ... );
void nnpi_calculate_weights( nnpi* nn );
int nnpi_get_nvertices( nnpi* nn );
int* nnpi_get_vertices( nnpi* nn );
double* nnpi_get_weights( nnpi* nn );
void nnpi_normalize_weights( nnpi* nn );
void nnpi_reset( nnpi* nn );
void nnpi_set_point( nnpi* nn, point* p );

// Builds Natural Neighbours array interpolator. This includes calculation of
// weights used in nnai_interpolate().
//
// @param d Delaunay triangulation
// @return Natural Neighbours interpolation
//
nnai* nnai_build( delaunay* d, int n, double* x, double* y )
{
    nnai  * nn  = malloc( sizeof ( nnai ) );
    nnpi  * nnp = nnpi_create( d );
    int   * vertices;
    double* weights;
    int   i;

    if ( n <= 0 )
        nn_quit( "nnai_create(): n = %d\n", n );

    nn->d = d;
    nn->n = n;
    nn->x = malloc( (size_t) n * sizeof ( double ) );
    memcpy( nn->x, x, (size_t) n * sizeof ( double ) );
    nn->y = malloc( (size_t) n * sizeof ( double ) );
    memcpy( nn->y, y, (size_t) n * sizeof ( double ) );
    nn->weights = malloc( (size_t) n * sizeof ( nn_weights ) );

    for ( i = 0; i < n; ++i )
    {
        nn_weights* w = &nn->weights[i];
        point     p;

        p.x = x[i];
        p.y = y[i];

        nnpi_reset( nnp );
        nnpi_set_point( nnp, &p );
        nnpi_calculate_weights( nnp );
        nnpi_normalize_weights( nnp );

        vertices = nnpi_get_vertices( nnp );
        weights  = nnpi_get_weights( nnp );

        w->nvertices = nnpi_get_nvertices( nnp );
        w->vertices  = malloc( (size_t) ( w->nvertices ) * sizeof ( int ) );
        memcpy( w->vertices, vertices, (size_t) ( w->nvertices ) * sizeof ( int ) );
        w->weights = malloc( (size_t) ( w->nvertices ) * sizeof ( double ) );
        memcpy( w->weights, weights, (size_t) ( w->nvertices ) * sizeof ( double ) );
    }

    nnpi_destroy( nnp );

    return nn;
}

// Destroys Natural Neighbours array interpolator.
//
// @param nn Structure to be destroyed
//
void nnai_destroy( nnai* nn )
{
    int i;

    for ( i = 0; i < nn->n; ++i )
    {
        nn_weights* w = &nn->weights[i];

        free( w->vertices );
        free( w->weights );
    }

    free( nn->x );
    free( nn->y );
    free( nn->weights );
    free( nn );
}

// Conducts NN interpolation in a fixed array of output points using
// data specified for a fixed array of input points. Uses pre-calculated
// weights.
//
// @param nn NN array interpolator
// @param zin input data [nn->d->npoints]
// @param zout output data [nn->n]. Must be pre-allocated!
//
void nnai_interpolate( nnai* nn, double* zin, double* zout )
{
    int i;

    for ( i = 0; i < nn->n; ++i )
    {
        nn_weights* w = &nn->weights[i];
        double    z   = 0.0;
        int       j;

        for ( j = 0; j < w->nvertices; ++j )
        {
            double weight = w->weights[j];

            if ( weight < nn->wmin )
            {
                z = NaN;
                break;
            }
            z += weight * zin[w->vertices[j]];
        }

        zout[i] = z;
    }
}

//* Sets minimal allowed weight for Natural Neighbours interpolation.
// @param nn Natural Neighbours array interpolator
// @param wmin Minimal allowed weight
//
void nnai_setwmin( nnai* nn, double wmin )
{
    nn->wmin = wmin;
}

// The rest of this file contains a number of test programs.
//
#if defined ( NNAI_TEST )

#include <sys/time.h>

#define NPOINTSIN    10000
#define NMIN         10
#define NX           101
#define NXMIN        1

#define SQ( x )    ( ( x ) * ( x ) )

static double franke( double x, double y )
{
    x *= 9.0;
    y *= 9.0;
    return 0.75 * exp( ( -SQ( x - 2.0 ) - SQ( y - 2.0 ) ) / 4.0 )
           + 0.75 * exp( -SQ( x - 2.0 ) / 49.0 - ( y - 2.0 ) / 10.0 )
           + 0.5 * exp( ( -SQ( x - 7.0 ) - SQ( y - 3.0 ) ) / 4.0 )
           - 0.2 * exp( -SQ( x - 4.0 ) - SQ( y - 7.0 ) );
}

static void usage()
{
    printf(
        "Usage: nn_test [-v|-V] [-n <nin> <nxout>]\n"
        "Options:\n"
        "  -a              -- use non-Sibsonian interpolation rule\n"
        "  -n <nin> <nout>:\n"
        "            <nin> -- number of input points (default = 10000)\n"
        "           <nout> -- number of output points per side (default = 64)\n"
        "  -v              -- verbose\n"
        "  -V              -- very verbose\n"
        );
}

int main( int argc, char* argv[] )
{
    int nin  = NPOINTSIN;
    int nx   = NX;
    int nout = 0;
    point           * pin  = NULL;
    delaunay        * d    = NULL;
    point           * pout = NULL;
    nnai            * nn   = NULL;
    double          * zin  = NULL;
    double          * xout = NULL;
    double          * yout = NULL;
    double          * zout = NULL;
    int             cpi    = -1; // control point index
    struct timeval  tv0, tv1, tv2;
    struct timezone tz;
    int             i;

    i = 1;
    while ( i < argc )
    {
        switch ( argv[i][1] )
        {
        case 'a':
            i++;
            nn_rule = NON_SIBSONIAN;
            break;
        case 'n':
            i++;
            if ( i >= argc )
                nn_quit( "no number of data points found after -i\n" );
            nin = atoi( argv[i] );
            i++;
            if ( i >= argc )
                nn_quit( "no number of ouput points per side found after -i\n" );
            nx = atoi( argv[i] );
            i++;
            break;
        case 'v':
            i++;
            nn_verbose = 1;
            break;
        case 'V':
            i++;
            nn_verbose = 2;
            break;
        default:
            usage();
            break;
        }
    }

    if ( nin < NMIN )
        nin = NMIN;
    if ( nx < NXMIN )
        nx = NXMIN;

    printf( "\nTest of Natural Neighbours array interpolator:\n\n" );
    printf( "  %d data points\n", nin );
    printf( "  %d output points\n", nx * nx );

    //
    // generate data
    //
    printf( "  generating data:\n" );
    fflush( stdout );
    pin = malloc( nin * sizeof ( point ) );
    zin = malloc( nin * sizeof ( double ) );
    for ( i = 0; i < nin; ++i )
    {
        point* p = &pin[i];

        p->x   = (double) random() / RAND_MAX;
        p->y   = (double) random() / RAND_MAX;
        p->z   = franke( p->x, p->y );
        zin[i] = p->z;
        if ( nn_verbose )
            printf( "    (%f, %f, %f)\n", p->x, p->y, p->z );
    }

    //
    // triangulate
    //
    printf( "  triangulating:\n" );
    fflush( stdout );
    d = delaunay_build( nin, pin, 0, NULL, 0, NULL );

    //
    // generate output points
    //
    points_generate2( -0.1, 1.1, -0.1, 1.1, nx, nx, &nout, &pout );
    xout = malloc( nout * sizeof ( double ) );
    yout = malloc( nout * sizeof ( double ) );
    zout = malloc( nout * sizeof ( double ) );
    for ( i = 0; i < nout; ++i )
    {
        point* p = &pout[i];

        xout[i] = p->x;
        yout[i] = p->y;
        zout[i] = NaN;
    }
    cpi = ( nx / 2 ) * ( nx + 1 );

    gettimeofday( &tv0, &tz );

    //
    // create interpolator
    //
    printf( "  creating interpolator:\n" );
    fflush( stdout );
    nn = nnai_build( d, nout, xout, yout );

    fflush( stdout );
    gettimeofday( &tv1, &tz );
    {
        long dt = 1000000 * ( tv1.tv_sec - tv0.tv_sec ) + tv1.tv_usec - tv0.tv_usec;

        printf( "    interpolator creation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout );
    }

    //
    // interpolate
    //
    printf( "  interpolating:\n" );
    fflush( stdout );
    nnai_interpolate( nn, zin, zout );
    if ( nn_verbose )
        for ( i = 0; i < nout; ++i )
            printf( "    (%f, %f, %f)\n", xout[i], yout[i], zout[i] );

    fflush( stdout );
    gettimeofday( &tv2, &tz );
    {
        long dt = 1000000.0 * ( tv2.tv_sec - tv1.tv_sec ) + tv2.tv_usec - tv1.tv_usec;

        printf( "    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout );
    }

    if ( !nn_verbose )
        printf( "    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke( xout[cpi], yout[cpi] ) );

    printf( "  interpolating one more time:\n" );
    fflush( stdout );
    nnai_interpolate( nn, zin, zout );
    if ( nn_verbose )
        for ( i = 0; i < nout; ++i )
            printf( "    (%f, %f, %f)\n", xout[i], yout[i], zout[i] );

    fflush( stdout );
    gettimeofday( &tv0, &tz );
    {
        long dt = 1000000.0 * ( tv0.tv_sec - tv2.tv_sec ) + tv0.tv_usec - tv2.tv_usec;

        printf( "    interpolation time = %ld us (%.2f us / point)\n", dt, (double) dt / nout );
    }

    if ( !nn_verbose )
        printf( "    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke( xout[cpi], yout[cpi] ) );

    printf( "  entering new data:\n" );
    fflush( stdout );
    for ( i = 0; i < nin; ++i )
    {
        point* p = &pin[i];

        p->z   = p->x * p->x - p->y * p->y;
        zin[i] = p->z;
        if ( nn_verbose )
            printf( "    (%f, %f, %f)\n", p->x, p->y, p->z );
    }

    printf( "  interpolating:\n" );
    fflush( stdout );
    nnai_interpolate( nn, zin, zout );
    if ( nn_verbose )
        for ( i = 0; i < nout; ++i )
            printf( "    (%f, %f, %f)\n", xout[i], yout[i], zout[i] );

    if ( !nn_verbose )
        printf( "    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], xout[cpi] * xout[cpi] - yout[cpi] * yout[cpi] );

    printf( "  restoring data:\n" );
    fflush( stdout );
    for ( i = 0; i < nin; ++i )
    {
        point* p = &pin[i];

        p->z   = franke( p->x, p->y );
        zin[i] = p->z;
        if ( nn_verbose )
            printf( "    (%f, %f, %f)\n", p->x, p->y, p->z );
    }

    printf( "  interpolating:\n" );
    fflush( stdout );
    nnai_interpolate( nn, zin, zout );
    if ( nn_verbose )
        for ( i = 0; i < nout; ++i )
            printf( "    (%f, %f, %f)\n", xout[i], yout[i], zout[i] );

    if ( !nn_verbose )
        printf( "    control point: (%f, %f, %f) (expected z = %f)\n", xout[cpi], yout[cpi], zout[cpi], franke( xout[cpi], yout[cpi] ) );

    printf( "\n" );

    nnai_destroy( nn );
    free( zin );
    free( xout );
    free( yout );
    free( zout );
    free( pout );
    delaunay_destroy( d );
    free( pin );

    return 0;
}

#endif