-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
test_put_get_value.c
578 lines (447 loc) · 17.8 KB
/
test_put_get_value.c
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*****************************************************************************
*
* MODULE: Grass raster3d Library
* AUTHOR(S): Soeren Gebbert, Braunschweig (GER) Jun 2011
* soerengebbert <at> googlemail <dot> com
*
* PURPOSE: Unit and Integration tests
*
* COPYRIGHT: (C) 2000 by the 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.
*
*****************************************************************************/
#include <stdlib.h>
#include <string.h>
#include "test_raster3d_lib.h"
#include "grass/interpf.h"
static int test_put_get_value_dcell(void);
static int test_put_get_value_fcell(void);
static int test_put_get_value_resampling(void);
static int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths);
static int test_resampling_dcell(RASTER3D_Map *map, double north, double east, double
top, int col, int row, int depth, int fact);
static int test_resampling_fcell(RASTER3D_Map *map, double north, double east, double
top, int col, int row, int depth, int fact);
/* *************************************************************** */
/* Perform the put-get value tests ******************************* */
/* *************************************************************** */
int unit_test_put_get_value()
{
int sum = 0;
G_message("\n++ Running raster3d put/get value unit tests ++");
sum += test_put_get_value_dcell();
sum += test_put_get_value_fcell();
sum += test_put_get_value_resampling();
if (sum > 0)
G_warning("\n-- raster3d put/get value unit tests failure --");
else
G_message("\n-- raster3d put/get value unit tests finished successfully --");
return sum;
}
/* *************************************************************** */
int test_put_get_value_dcell(void)
{
int sum = 0;
int x, y, z;
DCELL value;
DCELL value_ref;
G_message("Testing DCELL put get value functions");
double north, east, top;
int col, row, depth;
RASTER3D_Region region;
RASTER3D_Map *map = NULL;
/* We need to set up a specific region for the new raster3d map.
* First we safe the default region. */
Rast3d_get_window(®ion);
region.bottom = 0.0;
region.top = 1000;
region.south = 1000;
region.north = 8500;
region.west = 5000;
region.east = 10000;
region.rows = 15;
region.cols = 10;
region.depths = 5;
Rast3d_adjust_region(®ion);
map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell", RASTER3D_USE_CACHE_XY, ®ion, DCELL_TYPE, 32);
/* The window is the same as the map region ... of course */
Rast3d_set_window_map(map, ®ion);
/*
ROWS
1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 8000 8500 north
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 region
COLS
5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10 region
DEPTHS
0 200 400 600 800 1000 top
|....|....|....|....|....|
0 1 2 3 4 5 region
*/
for(z = 0; z < region.depths; z++) {
for(y = 0; y < region.rows; y++) { /* From the north to the south */
for(x = 0; x < region.cols; x++) {
value = -1;
Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
}
}
}
for(z = 0; z < region.depths; z++) {
for(y = 0; y < region.rows; y++) { /* From the north to the south */
for(x = 0; x < region.cols; x++) {
/* Add cols, rows and depths and put this in the map */
value = x + y + z;
Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
}
}
}
/* Reread the new open map and compare the expected results */
G_message("Get the value of the upper left corner -> 0");
col = row = depth = 0;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
col = row = depth = 1;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
col = 4;
row = 3;
depth = 2;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
/* Write everything to the disk and reopen the map */
Rast3d_flush_all_tiles(map);
Rast3d_close(map);
map = Rast3d_open_cell_old("test_put_get_value_dcell", G_mapset(), ®ion, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
col = 9;
row = 14;
depth = 4;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
col = 10;
row = 15;
depth = 5;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
Rast3d_get_value(map, col, row, depth, &value_ref, DCELL_TYPE);
/* Rast3d_get_value_region does not work with coordinates outside the region */
printf("Value %g == %g\n", value, value_ref);
if(value == 0 || value < 0 || value > 0) {
G_message("Error in Rast3d_get_region_value");
sum++;
}
if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
G_message("Error in Rast3d_get_value");
sum++;
}
Rast3d_close(map);
G_remove("grid3", "test_put_get_value_dcell");
return sum;
}
/* *************************************************************** */
int test_put_get_value_fcell(void)
{
int sum = 0;
int x, y, z;
FCELL value;
FCELL value_ref;
G_message("Testing FCELL put get value functions");
double north, east, top;
int col, row, depth;
RASTER3D_Region region;
RASTER3D_Map *map = NULL;
/* We need to set up a specific region for the new raster3d map.
* First we safe the default region. */
Rast3d_get_window(®ion);
region.bottom = 0.0;
region.top = 1000;
region.south = 1000;
region.north = 8500;
region.west = 5000;
region.east = 10000;
region.rows = 15;
region.cols = 10;
region.depths = 5;
Rast3d_adjust_region(®ion);
map = Rast3d_open_new_opt_tile_size("test_put_get_value_fcell", RASTER3D_USE_CACHE_XY, ®ion, FCELL_TYPE, 32);
/* The window is the same as the map region ... of course */
Rast3d_set_window_map(map, ®ion);
for(z = 0; z < region.depths; z++) {
for(y = 0; y < region.rows; y++) {
for(x = 0; x < region.cols; x++) {
/* Add cols, rows and depths and put this in the map */
value = x + y + z;
Rast3d_put_value(map, x, y, z, &value, FCELL_TYPE);
}
}
}
/* Write everything to the disk */
Rast3d_flush_all_tiles(map);
Rast3d_close(map);
map = Rast3d_open_cell_old("test_put_get_value_fcell", G_mapset(), ®ion, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
/* Reread the map and compare the expected results */
G_message("Get the value of the lower left corner -> 0");
col = row = depth = 0;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
col = row = depth = 1;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
col = 4;
row = 3;
depth = 2;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
col = 9;
row = 14;
depth = 4;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
col = 10;
row = 15;
depth = 5;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
Rast3d_get_value(map, 10, 15, 5, &value_ref, FCELL_TYPE);
/* Rast3d_get_value_region does not work with coordinates outside the region */
printf("Value %g == %g\n", value, value_ref);
if(value == 0 || value < 0 || value > 0) {
G_message("Error in Rast3d_get_region_value");
sum++;
}
if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
G_message("Error in Rast3d_get_value");
sum++;
}
Rast3d_close(map);
G_remove("grid3", "test_put_get_value_fcell");
return sum;
}
/* *************************************************************** */
int test_put_get_value_resampling(void)
{
int sum = 0;
int x, y, z;
DCELL value;
G_message("Testing put get resample value functions");
double north, east, top;
int col, row, depth;
RASTER3D_Region region;
RASTER3D_Region window;
RASTER3D_Map *map = NULL;
/* We need to set up a specific region for the new raster3d map.
* First we safe the default region. */
Rast3d_get_window(®ion);
region.bottom = 0.0;
region.top = 1000;
region.south = 1000;
region.north = 8500;
region.west = 5000;
region.east = 10000;
region.rows = 15;
region.cols = 10;
region.depths = 5;
Rast3d_adjust_region(®ion);
map = Rast3d_open_new_opt_tile_size("test_put_get_value_resample", RASTER3D_USE_CACHE_XY, ®ion, DCELL_TYPE, 32);
/*
ROWS
1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 8000 8500 north
|....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 region
| | | | | | | | | | | | | | | |
30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0 window
COLS
5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
|....|....|....|....|....|....|....|....|....|....|
0 1 2 3 4 5 6 7 8 9 10 region
| | | | | | | | | | |
0 2 4 6 8 10 12 14 16 18 20 window
DEPTHS
0 200 400 600 800 1000 top
|....|....|....|....|....|
0 1 2 3 4 5 region
| | | | | |
0 2 4 6 8 10 window
*/
for(z = 0; z < region.depths; z++) {
for(y = 0; y < region.rows; y++) { /* North to south */
for(x = 0; x < region.cols; x++) {
/* Add cols, rows and depths and put this in the map */
value = x + y + z;
Rast3d_put_double(map, x, y, z, value);
}
}
}
/* Write everything to the disk */
Rast3d_flush_all_tiles(map);
Rast3d_close(map);
/* We modify the window for resampling tests */
Rast3d_region_copy(&window, ®ion);
/* Double the cols, rows and depths -> 8x resolution window */
window.rows = 30;
window.cols = 20;
window.depths = 10;
Rast3d_adjust_region(&window);
map = Rast3d_open_cell_old("test_put_get_value_resample", G_mapset(), ®ion, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
/* The window has the 8x resolution as the map region */
Rast3d_set_window_map(map, &window);
/* Reread the map and compare the expected results */
G_message("Get the value of the upper left corner -> 0");
col = row = depth = 0;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
col = row = depth = 1;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
G_message("Get the value of x == 7 y == 9 z == 3 -> x + y + z == 19");
col = 7;
row = 9;
depth = 3;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z == 27");
col = 9;
row = 14;
depth = 4;
north = region.north - region.ns_res * row;
east = region.west + region.ew_res * col;
top = region.bottom + region.tb_res * depth;
sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
sum += test_get_value_region(map, region.cols, region.rows, region.depths);
Rast3d_close(map);
G_remove("grid3", "test_put_get_value_resample");
return sum;
}
/* *************************************************************** */
int test_resampling_dcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
{
int sum = 0;
DCELL value;
DCELL value_ref;
DCELL value_reg;
DCELL value_win;
Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
Rast3d_get_window_value(map, north, east, top, &value_win, DCELL_TYPE);
Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, DCELL_TYPE);
Rast3d_get_value_region(map, col, row, depth, &value_reg, DCELL_TYPE);
printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
if(value != col + row + depth) {
G_message("Error in Rast3d_get_region_value");
sum++;
}
if(value_win != col + row + depth) {
G_message("Error in Rast3d_get_window_value");
sum++;
}
if(value_ref != col + row + depth) {
G_message("Error in Rast3d_get_value");
sum++;
}
if(value_reg != col + row + depth) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
return sum;
}
/* *************************************************************** */
int test_resampling_fcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
{
int sum = 0;
FCELL value;
FCELL value_ref;
FCELL value_reg;
FCELL value_win;
Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
Rast3d_get_window_value(map, north, east, top, &value_win, FCELL_TYPE);
Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, FCELL_TYPE);
Rast3d_get_value_region(map, col, row, depth, &value_reg, FCELL_TYPE);
printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
if(value != col + row + depth) {
G_message("Error in Rast3d_get_region_value");
sum++;
}
if(value_win != col + row + depth) {
G_message("Error in Rast3d_get_window_value");
sum++;
}
if(value_ref != col + row + depth) {
G_message("Error in Rast3d_get_value");
sum++;
}
if(value_reg != col + row + depth) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
return sum;
}
/* *************************************************************** */
int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths)
{
int sum = 0;
FCELL fvalue1 = 0.0;
FCELL fvalue2 = 0.0;
DCELL dvalue1 = 0.0;
DCELL dvalue2 = 0.0;
/* Test for correct Null value */
Rast3d_get_value_region(map, -1, -1, -1, &fvalue1, FCELL_TYPE);
Rast3d_get_value_region(map, cols, rows, depths, &fvalue2, FCELL_TYPE);
Rast3d_get_value_region(map, -1, -1, -1, &dvalue1, DCELL_TYPE);
Rast3d_get_value_region(map, cols, rows, depths, &dvalue2, DCELL_TYPE);
printf("Value %g == %g == %g == %g\n", fvalue1, fvalue2, dvalue1, dvalue2);
if(!Rast_is_f_null_value(&fvalue1)) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
if(!Rast_is_f_null_value(&fvalue2)) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
if(!Rast_is_d_null_value(&dvalue1)) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
if(!Rast_is_d_null_value(&dvalue2)) {
G_message("Error in Rast3d_get_value_region");
sum++;
}
return sum;
}