Image obtained by using color PostScript driver (psc) and then converted to animated gif using ImageMagick convert tool.

1 2 #pragma _ipath "./" 3 #include "plcdemos.h" 4 #include <time.h> 5 6 #if !defined(HAVE_ISNAN) 7 # define isnan(x) ((x) != (x)) 8 #endif 9 10 11 static PLINT pts = 500; 12 static PLINT xp = 25; 13 static PLINT yp = 20; 14 static PLINT nl = 15; 15 static int knn_order = 20; 16 static PLFLT threshold = 1.001; 17 static PLFLT wmin = -1e3; 18 static int randn = 0; 19 static int rosen = 0; 20 21 static PLOptionTable options[] = { 22 { 23 "npts", 24 NULL, 25 NULL, 26 &pts, 27 PL_OPT_INT, 28 "-npts points", 29 "Specify number of random points to generate [500]" }, 30 { 31 "randn", 32 NULL, 33 NULL, 34 &randn, 35 PL_OPT_BOOL, 36 "-randn", 37 "Normal instead of uniform sampling -- the effective \n\ 38 \t\t\t number of points will be smaller than the specified." }, 39 { 40 "rosen", 41 NULL, 42 NULL, 43 &rosen, 44 PL_OPT_BOOL, 45 "-rosen", 46 "Generate points from the Rosenbrock function."}, 47 { 48 "nx", 49 NULL, 50 NULL, 51 &xp, 52 PL_OPT_INT, 53 "-nx points", 54 "Specify grid x dimension [25]" }, 55 { 56 "ny", 57 NULL, 58 NULL, 59 &yp, 60 PL_OPT_INT, 61 "-ny points", 62 "Specify grid y dimension [20]" }, 63 { 64 "nlevel", 65 NULL, 66 NULL, 67 &nl, 68 PL_OPT_INT, 69 "-nlevel ", 70 "Specify number of contour levels [15]" }, 71 { 72 "knn_order", 73 NULL, 74 NULL, 75 &knn_order, 76 PL_OPT_INT, 77 "-knn_order order", 78 "Specify the number of neighbors [20]" }, 79 { 80 "threshold", 81 NULL, 82 NULL, 83 &threshold, 84 PL_OPT_FLOAT, 85 "-threshold float", 86 "Specify what a thin triangle is [1. < [1.001] < 2.]" }, 87 88 { 89 NULL, NULL, NULL, NULL, 0, NULL, NULL 90 ++++} }; 91 92 void create_data(PLFLT **xi, PLFLT **yi, PLFLT **zi, int pts); 93 void free_data(PLFLT *x, PLFLT *y, PLFLT *z); 94 void create_grid(PLFLT **xi, int px, PLFLT **yi, int py); 95 void free_grid(PLFLT *x, PLFLT *y); 96 97 static void 98 cmap1_init() 99 { 100 PLFLT i[2], h[2], l[2], s[2]; 101 102 i[0] = 0.0; i[1] = 1.0; 103 h[0] = 240; h[1] = 0; 104 l[0] = 0.6; 105 l[1] = 0.6; 106 107 s[0] = 0.8; 108 s[1] = 0.8; 109 110 plscmap1n(256); 111 c_plscmap1l(0, 2, i, h, l, s, NULL); 112 } 113 114 PLFLT xm, xM, ym, yM; 115 116 int 117 main(int argc, char *argv[]) 118 { 119 PLFLT *x, *y, *z, *clev; 120 PLFLT *xg, *yg, **zg, **szg; 121 PLFLT zmin, zmax, lzm, lzM; 122 long ct; 123 int i, j, k; 124 PLINT alg; 125 char ylab[40], xlab[40]; 126 char *title[] = {"Cubic Spline Approximation", 127 "Delaunay Linear Interpolation", 128 "Natural Neighbors Interpolation", 129 "KNN Inv. Distance Weighted", 130 "3NN Linear Interpolation", 131 "4NN Around Inv. Dist. Weighted"}; 132 133 PLFLT opt[] = {0., 0., 0., 0., 0., 0.}; 134 135 xm = ym = -0.2; 136 xM = yM = 0.8; 137 138 plMergeOpts(options, "x21c options", NULL); 139 plParseOpts(&argc, argv, PL_PARSE_FULL); 140 141 opt[2] = wmin; 142 opt[3] = (PLFLT) knn_order; 143 opt[4] = threshold; 144 145 146 plinit(); 147 148 create_data(&x, &y, &z, pts); zmin = z[0]; 149 zmax = z[0]; 150 for (i=1; i<pts; i++) { 151 if (z[i] > zmax) 152 zmax = z[i]; 153 if (z[i] < zmin) 154 zmin = z[i]; 155 } 156 157 create_grid(&xg, xp, &yg, yp); plAlloc2dGrid(&zg, xp, yp); clev = (PLFLT 158 ++++*) malloc(nl * sizeof(PLFLT)); 159 160 sprintf(xlab, "Npts=%d gridx=%d gridy=%d", pts, xp, yp); 161 plcol0(1); 162 plenv(xm, xM, ym, yM, 2, 0); 163 plcol0(15); 164 pllab(xlab, "", "The original data"); 165 plcol0(2); 166 plpoin(pts, x, y, 5); 167 pladv(0); 168 169 plssub(3,2); 170 171 for (k=0; k<2; k++) { 172 pladv(0); 173 for (alg=1; alg<7; alg++) { 174 175 ct = clock(); 176 plgriddata(x, y, z, pts, xg, xp, yg, yp, zg, alg, opt[alg-1]); 177 sprintf(xlab, "time=%d ms", (clock() - ct)/1000); 178 sprintf(ylab, "opt=%.3f", opt[alg-1]); 179 180 181 if (alg == GRID_CSA || alg == GRID_DTLI || alg == GRID_NNLI || alg == 182 ++++GRID_NNI) { 183 int ii, jj; 184 PLFLT dist, d; 185 186 for (i=0; i<xp; i++) { 187 for (j=0; j<yp; j++) { 188 if (isnan(zg[i][j])) { 189 zg[i][j] = 0.; dist = 0.; 190 191 for (ii=i-1; ii<=i+1 && ii<xp; ii++) { 192 for (jj=j-1; jj<=j+1 && jj<yp; jj++) { 193 if (ii >= 0 && jj >= 0 && !isnan(zg[ii][jj])) { 194 d = (abs(ii-i) + abs(jj-j)) == 1 ? 1. : 1.4142; 195 zg[i][j] += zg[ii][jj]/(d*d); 196 dist += d; 197 } 198 } 199 } 200 if (dist != 0.) 201 zg[i][j] /= dist; 202 else 203 zg[i][j] = zmin; 204 205 } 206 } 207 } 208 } 209 210 plMinMax2dGrid(zg, xp, yp, &lzM, &lzm); 211 212 plcol0(1); 213 pladv(alg); 214 215 if (k == 0) { 216 217 lzm = MIN(lzm, zmin); 218 lzM = MAX(lzM, zmax); 219 for (i=0; i<nl; i++) 220 clev[i] = lzm + (lzM-lzm)/(nl-1)*i; 221 222 plenv0(xm, xM, ym, yM, 2, 0); 223 plcol0(15); 224 pllab(xlab, ylab, title[alg-1]); 225 plshades(zg, xp, yp, NULL, xm, xM, ym, yM, 226 clev, nl, 1, 0, 1, plfill, 1, NULL, NULL); 227 plcol0(2); 228 } else { 229 230 for (i=0; i<nl; i++) 231 clev[i] = lzm + (lzM-lzm)/(nl-1)*i; 232 233 cmap1_init(); 234 plvpor(0.0, 1.0, 0.0, 0.9); 235 plwind(-1.0, 1.0, -1.0, 1.5); 236 237 plw3d(1., 1., 1., xm, xM, ym, yM, lzm, lzM, 30, -60); 238 plbox3("bnstu", ylab, 0.0, 0, 239 "bnstu", xlab, 0.0, 0, 240 "bcdmnstuv", "", 0.0, 4); 241 plcol0(15); 242 pllab("", "", title[alg-1]); 243 plot3dc(xg, yg, zg, xp, yp, DRAW_LINEXY | MAG_COLOR | BASE_CONT, clev, nl); 244 } 245 } 246 } 247 248 plend(); 249 250 free_data(x, y, z); 251 free_grid(xg, yg); 252 free((void *)clev); 253 plFree2dGrid(zg, xp, yp); 254 } 255 256 257 void 258 create_grid(PLFLT **xi, int px, PLFLT **yi, int py) 259 { 260 261 PLFLT *x, *y; 262 int i; 263 264 x = *xi = (PLFLT *) malloc(px * sizeof(PLFLT)); 265 y = *yi = (PLFLT *) malloc(py * sizeof(PLFLT)); 266 267 for (i=0; i<px; i++) 268 *x++ = xm + (xM-xm)*i/(px-1.); 269 270 for (i=0; i<py; i++) 271 *y++ = ym + (yM-ym)*i/(py-1.); 272 } 273 274 void 275 free_grid(PLFLT *xi, PLFLT *yi) 276 { 277 free((void *)xi); 278 free((void *)yi); 279 } 280 281 void 282 create_data(PLFLT **xi, PLFLT **yi, PLFLT **zi, int pts) 283 { 284 int i; 285 PLFLT *x, *y, *z, r; 286 PLFLT xt, yt; 287 288 *xi = x = (PLFLT *) malloc(pts * sizeof(PLFLT)); 289 *yi = y = (PLFLT *) malloc(pts * sizeof(PLFLT)); 290 *zi = z = (PLFLT *) malloc(pts * sizeof(PLFLT)); 291 292 for(i=0; i<pts; i++) { 293 xt = drand48(); 294 yt = drand48(); 295 if (!randn) { 296 *x = xt + xm; 297 *y = yt + ym; 298 } else { *x = sqrt(-2.*log(xt)) * cos(2.*PI*yt) + xm; 299 *y = sqrt(-2.*log(xt)) * sin(2.*PI*yt) + ym; 300 } 301 if (!rosen) { 302 r = sqrt((*x) * (*x) + (*y) * (*y)); 303 *z = exp(-r * r) * cos(2.0 * PI * r); 304 } else { 305 *z = log(pow(1. - *x, 2.) + 100. * pow(*y - pow(*x, 2.), 2.)); 306 } 307 x++; y++; z++; 308 } 309 } 310 311 void 312 free_data(PLFLT *x, PLFLT *y, PLFLT *z) 313 { 314 free((void *)x); 315 free((void *)y); 316 free((void *)z); 317 }
Code description
- 1
- $Id: x21c.c,v 1.12 2004/01/20 02:25:59 airwin Exp $ Grid data demo Copyright (C) 2004 Joao Cardoso This file is part of PLplot. PLplot is free software; you can redistribute it and/or modify it under the terms of the GNU General Library Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. PLplot is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with PLplot; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- 10
- Options data structure definition.
- 89
- option
- 89
- handler
- 89
- client data
- 89
- address of variable to set
- 89
- mode flag
- 89
- short syntax
- 89
- long syntax
- 101
- left boundary
- 101
- right boundary
- 102
- blue -> green -> yellow ->
- 102
- -> red
- 144
- Initialize plplot
- 147
- the sampled data
- 156
- grid the data at
- 156
- the output grided data
- 178
- - CSA can generate NaNs (only interpolates?!). * - DTLI and NNI can generate NaNs for points outside the convex hull * of the data points. * - NNLI can generate NaNs if a sufficiently thick triangle is not found * * PLplot should be NaN/Inf aware, but changing it now is quite a job... * so, instead of not plotting the NaN regions, a weighted average over * the neighbors is done.
- 185
- average (IDW) over the 8 neighbors
- 233
- * For the comparition to be fair, all plots should have the * same z values, but to get the max/min of the data generated * by all algorithms would imply two passes. Keep it simple. * * plw3d(1., 1., 1., xm, xM, ym, yM, zmin, zmax, 30, -60);
- 295
- std=1, meaning that many points are outside the plot range