test_put_get_value.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*****************************************************************************
  2. *
  3. * MODULE: Grass raster3d Library
  4. * AUTHOR(S): Soeren Gebbert, Braunschweig (GER) Jun 2011
  5. * soerengebbert <at> googlemail <dot> com
  6. *
  7. * PURPOSE: Unit and Integration tests
  8. *
  9. * COPYRIGHT: (C) 2000 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "test_raster3d_lib.h"
  19. #include "grass/interpf.h"
  20. static int test_put_get_value_dcell(void);
  21. static int test_put_get_value_fcell(void);
  22. static int test_put_get_value_resampling(void);
  23. static int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths);
  24. static int test_resampling_dcell(RASTER3D_Map *map, double north, double east, double
  25. top, int col, int row, int depth, int fact);
  26. static int test_resampling_fcell(RASTER3D_Map *map, double north, double east, double
  27. top, int col, int row, int depth, int fact);
  28. /* *************************************************************** */
  29. /* Perform the put-get value tests ******************************* */
  30. /* *************************************************************** */
  31. int unit_test_put_get_value()
  32. {
  33. int sum = 0;
  34. G_message("\n++ Running raster3d put/get value unit tests ++");
  35. sum += test_put_get_value_dcell();
  36. sum += test_put_get_value_fcell();
  37. sum += test_put_get_value_resampling();
  38. if (sum > 0)
  39. G_warning("\n-- raster3d put/get value unit tests failure --");
  40. else
  41. G_message("\n-- raster3d put/get value unit tests finished successfully --");
  42. return sum;
  43. }
  44. /* *************************************************************** */
  45. int test_put_get_value_dcell(void)
  46. {
  47. int sum = 0;
  48. int x, y, z;
  49. DCELL value;
  50. DCELL value_ref;
  51. G_message("Testing DCELL put get value functions");
  52. double north, east, top;
  53. int col, row, depth;
  54. RASTER3D_Region region;
  55. RASTER3D_Map *map = NULL;
  56. /* We need to set up a specific region for the new raster3d map.
  57. * First we safe the default region. */
  58. Rast3d_get_window(&region);
  59. region.bottom = 0.0;
  60. region.top = 1000;
  61. region.south = 1000;
  62. region.north = 8500;
  63. region.west = 5000;
  64. region.east = 10000;
  65. region.rows = 15;
  66. region.cols = 10;
  67. region.depths = 5;
  68. Rast3d_adjust_region(&region);
  69. map = Rast3d_open_new_opt_tile_size("test_put_get_value_dcell", RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  70. /* The window is the same as the map region ... of course */
  71. Rast3d_set_window_map(map, &region);
  72. /*
  73. ROWS
  74. 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 8000 8500 north
  75. |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
  76. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 region
  77. COLS
  78. 5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
  79. |....|....|....|....|....|....|....|....|....|....|
  80. 0 1 2 3 4 5 6 7 8 9 10 region
  81. DEPTHS
  82. 0 200 400 600 800 1000 top
  83. |....|....|....|....|....|
  84. 0 1 2 3 4 5 region
  85. */
  86. for(z = 0; z < region.depths; z++) {
  87. for(y = 0; y < region.rows; y++) { /* From the north to the south */
  88. for(x = 0; x < region.cols; x++) {
  89. value = -1;
  90. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  91. }
  92. }
  93. }
  94. for(z = 0; z < region.depths; z++) {
  95. for(y = 0; y < region.rows; y++) { /* From the north to the south */
  96. for(x = 0; x < region.cols; x++) {
  97. /* Add cols, rows and depths and put this in the map */
  98. value = x + y + z;
  99. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  100. }
  101. }
  102. }
  103. /* Reread the new open map and compare the expected results */
  104. G_message("Get the value of the upper left corner -> 0");
  105. col = row = depth = 0;
  106. north = region.north - region.ns_res * row;
  107. east = region.west + region.ew_res * col;
  108. top = region.bottom + region.tb_res * depth;
  109. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  110. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  111. col = row = depth = 1;
  112. north = region.north - region.ns_res * row;
  113. east = region.west + region.ew_res * col;
  114. top = region.bottom + region.tb_res * depth;
  115. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  116. G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
  117. col = 4;
  118. row = 3;
  119. depth = 2;
  120. north = region.north - region.ns_res * row;
  121. east = region.west + region.ew_res * col;
  122. top = region.bottom + region.tb_res * depth;
  123. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  124. /* Write everything to the disk and reopen the map */
  125. Rast3d_flush_all_tiles(map);
  126. Rast3d_close(map);
  127. map = Rast3d_open_cell_old("test_put_get_value_dcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  128. G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
  129. col = 9;
  130. row = 14;
  131. depth = 4;
  132. north = region.north - region.ns_res * row;
  133. east = region.west + region.ew_res * col;
  134. top = region.bottom + region.tb_res * depth;
  135. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  136. G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
  137. col = 10;
  138. row = 15;
  139. depth = 5;
  140. north = region.north - region.ns_res * row;
  141. east = region.west + region.ew_res * col;
  142. top = region.bottom + region.tb_res * depth;
  143. Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
  144. Rast3d_get_value(map, col, row, depth, &value_ref, DCELL_TYPE);
  145. /* Rast3d_get_value_region does not work with coordinates outside the region */
  146. printf("Value %g == %g\n", value, value_ref);
  147. if(value == 0 || value < 0 || value > 0) {
  148. G_message("Error in Rast3d_get_region_value");
  149. sum++;
  150. }
  151. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  152. G_message("Error in Rast3d_get_value");
  153. sum++;
  154. }
  155. Rast3d_close(map);
  156. G_remove("grid3", "test_put_get_value_dcell");
  157. return sum;
  158. }
  159. /* *************************************************************** */
  160. int test_put_get_value_fcell(void)
  161. {
  162. int sum = 0;
  163. int x, y, z;
  164. FCELL value;
  165. FCELL value_ref;
  166. G_message("Testing FCELL put get value functions");
  167. double north, east, top;
  168. int col, row, depth;
  169. RASTER3D_Region region;
  170. RASTER3D_Map *map = NULL;
  171. /* We need to set up a specific region for the new raster3d map.
  172. * First we safe the default region. */
  173. Rast3d_get_window(&region);
  174. region.bottom = 0.0;
  175. region.top = 1000;
  176. region.south = 1000;
  177. region.north = 8500;
  178. region.west = 5000;
  179. region.east = 10000;
  180. region.rows = 15;
  181. region.cols = 10;
  182. region.depths = 5;
  183. Rast3d_adjust_region(&region);
  184. map = Rast3d_open_new_opt_tile_size("test_put_get_value_fcell", RASTER3D_USE_CACHE_XY, &region, FCELL_TYPE, 32);
  185. /* The window is the same as the map region ... of course */
  186. Rast3d_set_window_map(map, &region);
  187. for(z = 0; z < region.depths; z++) {
  188. for(y = 0; y < region.rows; y++) {
  189. for(x = 0; x < region.cols; x++) {
  190. /* Add cols, rows and depths and put this in the map */
  191. value = x + y + z;
  192. Rast3d_put_value(map, x, y, z, &value, FCELL_TYPE);
  193. }
  194. }
  195. }
  196. /* Write everything to the disk */
  197. Rast3d_flush_all_tiles(map);
  198. Rast3d_close(map);
  199. map = Rast3d_open_cell_old("test_put_get_value_fcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  200. /* Reread the map and compare the expected results */
  201. G_message("Get the value of the lower left corner -> 0");
  202. col = row = depth = 0;
  203. north = region.north - region.ns_res * row;
  204. east = region.west + region.ew_res * col;
  205. top = region.bottom + region.tb_res * depth;
  206. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  207. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  208. col = row = depth = 1;
  209. north = region.north - region.ns_res * row;
  210. east = region.west + region.ew_res * col;
  211. top = region.bottom + region.tb_res * depth;
  212. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  213. G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
  214. col = 4;
  215. row = 3;
  216. depth = 2;
  217. north = region.north - region.ns_res * row;
  218. east = region.west + region.ew_res * col;
  219. top = region.bottom + region.tb_res * depth;
  220. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  221. G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
  222. col = 9;
  223. row = 14;
  224. depth = 4;
  225. north = region.north - region.ns_res * row;
  226. east = region.west + region.ew_res * col;
  227. top = region.bottom + region.tb_res * depth;
  228. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  229. G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
  230. col = 10;
  231. row = 15;
  232. depth = 5;
  233. north = region.north - region.ns_res * row;
  234. east = region.west + region.ew_res * col;
  235. top = region.bottom + region.tb_res * depth;
  236. Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
  237. Rast3d_get_value(map, 10, 15, 5, &value_ref, FCELL_TYPE);
  238. /* Rast3d_get_value_region does not work with coordinates outside the region */
  239. printf("Value %g == %g\n", value, value_ref);
  240. if(value == 0 || value < 0 || value > 0) {
  241. G_message("Error in Rast3d_get_region_value");
  242. sum++;
  243. }
  244. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  245. G_message("Error in Rast3d_get_value");
  246. sum++;
  247. }
  248. Rast3d_close(map);
  249. G_remove("grid3", "test_put_get_value_fcell");
  250. return sum;
  251. }
  252. /* *************************************************************** */
  253. int test_put_get_value_resampling(void)
  254. {
  255. int sum = 0;
  256. int x, y, z;
  257. DCELL value;
  258. G_message("Testing put get resample value functions");
  259. double north, east, top;
  260. int col, row, depth;
  261. RASTER3D_Region region;
  262. RASTER3D_Region window;
  263. RASTER3D_Map *map = NULL;
  264. /* We need to set up a specific region for the new raster3d map.
  265. * First we safe the default region. */
  266. Rast3d_get_window(&region);
  267. region.bottom = 0.0;
  268. region.top = 1000;
  269. region.south = 1000;
  270. region.north = 8500;
  271. region.west = 5000;
  272. region.east = 10000;
  273. region.rows = 15;
  274. region.cols = 10;
  275. region.depths = 5;
  276. Rast3d_adjust_region(&region);
  277. map = Rast3d_open_new_opt_tile_size("test_put_get_value_resample", RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  278. /*
  279. ROWS
  280. 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6000 6500 7000 7500 8000 8500 north
  281. |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
  282. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 region
  283. | | | | | | | | | | | | | | | |
  284. 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0 window
  285. COLS
  286. 5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
  287. |....|....|....|....|....|....|....|....|....|....|
  288. 0 1 2 3 4 5 6 7 8 9 10 region
  289. | | | | | | | | | | |
  290. 0 2 4 6 8 10 12 14 16 18 20 window
  291. DEPTHS
  292. 0 200 400 600 800 1000 top
  293. |....|....|....|....|....|
  294. 0 1 2 3 4 5 region
  295. | | | | | |
  296. 0 2 4 6 8 10 window
  297. */
  298. for(z = 0; z < region.depths; z++) {
  299. for(y = 0; y < region.rows; y++) { /* North to south */
  300. for(x = 0; x < region.cols; x++) {
  301. /* Add cols, rows and depths and put this in the map */
  302. value = x + y + z;
  303. Rast3d_put_double(map, x, y, z, value);
  304. }
  305. }
  306. }
  307. /* Write everything to the disk */
  308. Rast3d_flush_all_tiles(map);
  309. Rast3d_close(map);
  310. /* We modify the window for resampling tests */
  311. Rast3d_region_copy(&window, &region);
  312. /* Double the cols, rows and depths -> 8x resolution window */
  313. window.rows = 30;
  314. window.cols = 20;
  315. window.depths = 10;
  316. Rast3d_adjust_region(&window);
  317. map = Rast3d_open_cell_old("test_put_get_value_resample", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  318. /* The window has the 8x resolution as the map region */
  319. Rast3d_set_window_map(map, &window);
  320. /* Reread the map and compare the expected results */
  321. G_message("Get the value of the upper left corner -> 0");
  322. col = row = depth = 0;
  323. north = region.north - region.ns_res * row;
  324. east = region.west + region.ew_res * col;
  325. top = region.bottom + region.tb_res * depth;
  326. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  327. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  328. col = row = depth = 1;
  329. north = region.north - region.ns_res * row;
  330. east = region.west + region.ew_res * col;
  331. top = region.bottom + region.tb_res * depth;
  332. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  333. G_message("Get the value of x == 7 y == 9 z == 3 -> x + y + z == 19");
  334. col = 7;
  335. row = 9;
  336. depth = 3;
  337. north = region.north - region.ns_res * row;
  338. east = region.west + region.ew_res * col;
  339. top = region.bottom + region.tb_res * depth;
  340. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  341. G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z == 27");
  342. col = 9;
  343. row = 14;
  344. depth = 4;
  345. north = region.north - region.ns_res * row;
  346. east = region.west + region.ew_res * col;
  347. top = region.bottom + region.tb_res * depth;
  348. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  349. sum += test_get_value_region(map, region.cols, region.rows, region.depths);
  350. Rast3d_close(map);
  351. G_remove("grid3", "test_put_get_value_resample");
  352. return sum;
  353. }
  354. /* *************************************************************** */
  355. int test_resampling_dcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  356. {
  357. int sum = 0;
  358. DCELL value;
  359. DCELL value_ref;
  360. DCELL value_reg;
  361. DCELL value_win;
  362. Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
  363. Rast3d_get_window_value(map, north, east, top, &value_win, DCELL_TYPE);
  364. Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, DCELL_TYPE);
  365. Rast3d_get_value_region(map, col, row, depth, &value_reg, DCELL_TYPE);
  366. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  367. if(value != col + row + depth) {
  368. G_message("Error in Rast3d_get_region_value");
  369. sum++;
  370. }
  371. if(value_win != col + row + depth) {
  372. G_message("Error in Rast3d_get_window_value");
  373. sum++;
  374. }
  375. if(value_ref != col + row + depth) {
  376. G_message("Error in Rast3d_get_value");
  377. sum++;
  378. }
  379. if(value_reg != col + row + depth) {
  380. G_message("Error in Rast3d_get_value_region");
  381. sum++;
  382. }
  383. return sum;
  384. }
  385. /* *************************************************************** */
  386. int test_resampling_fcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  387. {
  388. int sum = 0;
  389. FCELL value;
  390. FCELL value_ref;
  391. FCELL value_reg;
  392. FCELL value_win;
  393. Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
  394. Rast3d_get_window_value(map, north, east, top, &value_win, FCELL_TYPE);
  395. Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, FCELL_TYPE);
  396. Rast3d_get_value_region(map, col, row, depth, &value_reg, FCELL_TYPE);
  397. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  398. if(value != col + row + depth) {
  399. G_message("Error in Rast3d_get_region_value");
  400. sum++;
  401. }
  402. if(value_win != col + row + depth) {
  403. G_message("Error in Rast3d_get_window_value");
  404. sum++;
  405. }
  406. if(value_ref != col + row + depth) {
  407. G_message("Error in Rast3d_get_value");
  408. sum++;
  409. }
  410. if(value_reg != col + row + depth) {
  411. G_message("Error in Rast3d_get_value_region");
  412. sum++;
  413. }
  414. return sum;
  415. }
  416. /* *************************************************************** */
  417. int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths)
  418. {
  419. int sum = 0;
  420. FCELL fvalue1 = 0.0;
  421. FCELL fvalue2 = 0.0;
  422. DCELL dvalue1 = 0.0;
  423. DCELL dvalue2 = 0.0;
  424. /* Test for correct Null value */
  425. Rast3d_get_value_region(map, -1, -1, -1, &fvalue1, FCELL_TYPE);
  426. Rast3d_get_value_region(map, cols, rows, depths, &fvalue2, FCELL_TYPE);
  427. Rast3d_get_value_region(map, -1, -1, -1, &dvalue1, DCELL_TYPE);
  428. Rast3d_get_value_region(map, cols, rows, depths, &dvalue2, DCELL_TYPE);
  429. printf("Value %g == %g == %g == %g\n", fvalue1, fvalue2, dvalue1, dvalue2);
  430. if(!Rast_is_f_null_value(&fvalue1)) {
  431. G_message("Error in Rast3d_get_value_region");
  432. sum++;
  433. }
  434. if(!Rast_is_f_null_value(&fvalue2)) {
  435. G_message("Error in Rast3d_get_value_region");
  436. sum++;
  437. }
  438. if(!Rast_is_d_null_value(&dvalue1)) {
  439. G_message("Error in Rast3d_get_value_region");
  440. sum++;
  441. }
  442. if(!Rast_is_d_null_value(&dvalue2)) {
  443. G_message("Error in Rast3d_get_value_region");
  444. sum++;
  445. }
  446. return sum;
  447. }