1 /******************************************************************************
2  *
3  * File:           nn.h
4  *
5  * Created:        04/08/2000
6  *
7  * Author:         Pavel Sakov
8  *                 CSIRO Marine Research
9  *
10  * Purpose:        Header file for nn library
11  *
12  * Description:    None
13  *
14  * Revisions:      None
15  *
16  *****************************************************************************/
17 
18 #if !defined(_NN_H)
19 #define _NN_H
20 
21 typedef enum { SIBSON, NON_SIBSONIAN } NN_RULE;
22 
23 /* "point" is a basic data structure in this package.
24  */
25 #if !defined(_POINT_STRUCT)
26 #define _POINT_STRUCT
27 typedef struct {
28     double x;
29     double y;
30     double z;
31 } point;
32 #endif
33 
34 /* Constructors for interpolators in this package require Delaunay
35  * triangulation of the input data.
36  */
37 #if !defined(_DELAUNAY_STRUCT)
38 #define _DELAUNAY_STRUCT
39 struct delaunay;
40 typedef struct delaunay delaunay;
41 #endif
42 
43 /** Builds Delaunay triangulation of the given array of points.
44  *
45  * @param np Number of points
46  * @param points Array of points [np] (input)
47  * @param ns Number of forced segments
48  * @param segments Array of (forced) segment endpoint indices [2*ns]
49  * @param nh Number of holes
50  * @param holes Array of hole (x,y) coordinates [2*nh]
51  * @return Delaunay triangulation structure with triangulation results
52  */
53 delaunay* delaunay_build(int np, point points[], int ns, int segments[], int nh, double holes[]);
54 
55 /** Destroys Delaunay triangulation.
56  *
57  * @param d Structure to be destroyed
58  */
59 void delaunay_destroy(delaunay* d);
60 
61 /** Smoothes the input point array by averaging the input x,y and z values
62  ** for each cell within virtual rectangular nx by ny grid. The corners of the
63  ** grid are created from min and max values of the input array. It also frees
64  ** the original array and returns results and new dimension via original
65  ** data and size pointers.
66  *
67  * @param n Pointer to number of points (input/output)
68  * @param p Pointer to array of points (input/output) [*n]
69  * @param nx Number of x nodes in decimation
70  * @param ny Number of y nodes in decimation
71  */
72 void points_thingrid(int* n, point** p, int nx, int ny);
73 
74 /** Smoothes the input point array by averaging the input data (X,Y and Z
75  ** values) until the sum of the distances between points does not exceed the
76  ** specified maximum value. It also frees the original array and returns
77  ** results and new dimension via original data and size pointers.
78  *
79  * @param n Pointer to number of points (input/output)
80  * @param p Pointer to array of points (input/output) [*n]
81  * @param rmax Maximum allowed accumulated distance
82  */
83 void points_thinlin(int* n, point** p, double rmax);
84 
85 /* Calculates X and/or Y ranges of the input array of points. If necessary,
86  * adjusts the range according to the zoom value.
87  *
88  * @param n Number of points
89  * @param points Array of points
90  * @param xmin Min X value if *xmin = NaN on input, not changed otherwise
91  * @param xmax Max X value if *xmax = NaN on input, not changed otherwise
92  * @param ymin Min Y value if *ymin = NaN on input, not changed otherwise
93  * @param ymax Max Y value if *ymax = NaN on input, not changed otherwise
94  */
95 void points_getrange(int n, point points[], double zoom, double* xmin, double* xmax, double* ymin, double* ymax);
96 
97 /** Generates rectangular grid nx by ny using specified min and max x and y
98  ** values. Allocates space for the output point array, be sure to free it
99  ** when necessary!
100  *
101  * @param xmin Min x value
102  * @param xmax Max x value
103  * @param ymin Min y value
104  * @param ymax Max y value
105  * @param nx Number of x nodes
106  * @param ny Number of y nodes
107  * @param zoom Zoom coefficient
108  * @param nout Pointer to number of output points
109  * @param pout Pointer to array of output points [*nout]
110  */
111 void points_generate(double xmin, double xmax, double ymin, double ymax, int nx, int ny, int* nout, point** pout);
112 
113 /** Reads array of points from a columnar file.
114  *
115  * @param fname File name (can be "stdin" dor stndard input)
116  * @param dim Number of dimensions (must be 2 or 3)
117  * @param n Pointer to number of points (output)
118  * @param points Pointer to array of points [*n] (output)
119  */
120 void points_read(char* fname, int dim, int* n, point** points);
121 
122 /** Scales Y coordinate so that the resulting set fits into square:
123  ** xmax - xmin = ymax - ymin
124  *
125  * @param n Number of points
126  * @param points The points to scale
127  * @return Y axis compression coefficient
128  */
129 double points_scaletosquare(int n, point* points);
130 
131 /** Compresses Y domain by a given multiple.
132  *
133  * @param n Number of points
134  * @param points The points to scale
135  * @param Y axis compression coefficient as returned by points_scaletosquare()
136  */
137 void points_scale(int n, point* points, double k);
138 
139 /** `lpi' -- "Linear Point Interpolator" is a structure for linear
140  ** interpolation of data on a "point-to-point" basis.
141  *
142  * `lpi' interpolates linearly within each triangle resulted from the Delaunay
143  * triangluation of the input data. `lpi' is much faster than all Natural
144  * Neighbours interpolators below.
145  */
146 struct lpi;
147 typedef struct lpi lpi;
148 
149 /** Builds linear interpolator.
150  *
151  * @param d Delaunay triangulation
152  * @return Linear interpolator
153  */
154 lpi* lpi_build(delaunay* d);
155 
156 /** Destroys linear interpolator.
157  *
158  * @param l Structure to be destroyed
159  */
160 void lpi_destroy(lpi* l);
161 
162 /** Finds linearly interpolated value in a point.
163  *
164  * @param l Linear point interpolator
165  * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output)
166  */
167 void lpi_interpolate_point(lpi* l, point* p);
168 
169 /** Linearly interpolates data in an array of points.
170  *
171  * @param nin Number of input points
172  * @param pin Array of input points [pin]
173  * @param nout Number of ouput points
174  * @param pout Array of output points [nout]
175  */
176 void lpi_interpolate_points(int nin, point pin[], int nout, point pout[]);
177 
178 /** `nnpi' -- "Natural Neighbours Point Interpolator" is a structure for
179  ** Natural Neighbours interpolation of data on a "point-to-point" basis.
180  *
181  * Because it involves weight calculation for each output point, it is not
182  * designed to take advantage of consequitive interpolations on the same
183  * sets of input and output points -- use `nnhpi' or `nnai' in these cases.
184  */
185 struct nnpi;
186 typedef struct nnpi nnpi;
187 
188 /** Creates Natural Neighbours point interpolator.
189  *
190  * @param d Delaunay triangulation
191  * @return Natural Neighbours interpolation
192  */
193 nnpi* nnpi_create(delaunay* d);
194 
195 /** Destroys Natural Neighbours point interpolation.
196  *
197  * @param nn Structure to be destroyed
198  */
199 void nnpi_destroy(nnpi* nn);
200 
201 /** Performs Natural Neighbours interpolation in a point.
202  *
203  * @param nn NN point interpolator
204  * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output)
205  */
206 void nnpi_interpolate_point(nnpi* nn, point* p);
207 
208 /** Performs Natural Neighbours interpolation in an array of points.
209  *
210  * @param nin Number of input points
211  * @param pin Array of input points [pin]
212  * @param wmin Minimal allowed weight
213  * @param nout Number of output points
214  * @param pout Array of output points [nout]
215  */
216 void nnpi_interpolate_points(int nin, point pin[], double wmin, int nout, point pout[]);
217 
218 /** Sets minimal allowed weight for Natural Neighbours interpolation.
219  *
220  * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating
221  * inside convex hall of the data only (returning NaNs otherwise).
222  *
223  * @param nn Natural Neighbours point interpolator
224  * @param wmin Minimal allowed weight
225  */
226 void nnpi_setwmin(nnpi* nn, double wmin);
227 
228 /** `nnhpi' -- "Natural Neighbours Hashing Point Interpolator" -- is a
229  ** structure for conducting consequitive Natural Neighbours interpolations
230  ** from the same set of observation points, designed to take advantage of
231  ** repeated interpolations in the same point. It allows modifying Z
232  ** coordinate of observed data between interpolations (because this does not
233  ** affect the interpolant weights).
234  */
235 struct nnhpi;
236 typedef struct nnhpi nnhpi;
237 
238 /** Creates Natural Neighbours hashing point interpolator.
239  *
240  * @param d Delaunay triangulation
241  * @param size Hash table size (should be of order of number of output points)
242  * @return Natural Neighbours interpolation
243  */
244 nnhpi* nnhpi_create(delaunay* d, int size);
245 
246 /** Destroys Natural Neighbours hashing point interpolation.
247  *
248  * @param nn Structure to be destroyed
249  */
250 void nnhpi_destroy(nnhpi* nn);
251 
252 /** Performs Natural Neighbours interpolation in a point.
253  *
254  * @param nnhpi NN hashing point interpolator
255  * @param p Point to be interpolated (p->x, p->y -- input; p->z -- output)
256  */
257 void nnhpi_interpolate(nnhpi* nn, point* p);
258 
259 /** Modifies interpolated data.
260  *
261  * Finds point* pd in the underlying Delaunay triangulation such that
262  * pd->x = p->x and pd->y = p->y, and copies p->z to pd->z. Exits with error
263  * if the point is not found.
264  *
265  * @param nn Natural Neighbours hashing point interpolator
266  * @param p New data
267  */
268 void nnhpi_modify_data(nnhpi* nn, point* p);
269 
270 /** Sets minimal allowed weight for Natural Neighbours interpolation.
271  *
272  * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating
273  * inside convex hall of the data only (returning NaNs otherwise).
274  *
275  * @param nn Natural Neighbours point hashing interpolator
276  * @param wmin Minimal allowed weight
277  */
278 void nnhpi_setwmin(nnhpi* nn, double wmin);
279 
280 /** `nnai' -- "Natural Neighbours Array Interpolator" is a structure for
281  ** conducting consequitive Natural Neighbours interpolations from the same
282  ** set of observation points in the same set of points. It allows modifying Z
283  ** coordinate of data between interpolations (because this does not
284  ** affect the interpolant weights).
285  *
286  * `nnai' is the fastest of the three Natural Neighbours interpolators in `nn'
287  * library.
288  */
289 struct nnai;
290 typedef struct nnai nnai;
291 
292 /** Builds Natural Neighbours array interpolator.
293  *
294  * This includes calculation of weights used in nnai_interpolate().
295  *
296  * @param d Delaunay triangulation
297  * @return Natural Neighbours interpolation
298  */
299 nnai* nnai_build(delaunay* d, int n, double* x, double* y);
300 
301 /** Destroys Natural Neighbours array interpolator.
302  *
303  * @param nn Structure to be destroyed
304  */
305 void nnai_destroy(nnai* nn);
306 
307 /** Conducts NN interpolation in a fixed array of output points using
308  ** data specified in a fixed array of input points. Uses pre-calculated
309  ** weights.
310  *
311  * @param nn NN array interpolator
312  * @param zin input data [nn->d->npoints]
313  * @param zout output data [nn->n]. Must be pre-allocated!
314  */
315 void nnai_interpolate(nnai* nn, double* zin, double* zout);
316 
317 /** Sets minimal allowed weight for Natural Neighbours interpolation.
318  *
319  * For Sibson interpolation, setting wmin = 0 is equivalent to interpolating
320  * inside convex hall of the input data only (returning NaNs otherwise).
321  *
322  * @param nn Natural Neighbours array interpolator
323  * @param wmin Minimal allowed weight
324  */
325 void nnai_setwmin(nnai* nn, double wmin);
326 
327 /* Sets the verbosity level within nn package.
328  * 0 (default) - silent
329  * 1 - verbose
330  * 2 - very verbose
331  */
332 extern int nn_verbose;
333 
334 /* Switches between different formulations for NN weights.
335  * SIBSON -- classic formulation by Sibson
336  * NON_SIBSONIAN -- alternative formulation by Belikov & Semenov
337  *
338  */
339 extern NN_RULE nn_rule;
340 
341 /* Contains version string for the nn package.
342  */
343 extern char* nn_version;
344 
345 /* Limits verbose information to a particular vertex (used mainly for
346  * debugging purposes).
347  */
348 extern int nn_test_vertice;
349 
350 #endif                          /* _NN_H */
351