test_put_get_value.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 coordinate transformation 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 6500 7000 7500 8000 8500 9000 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. /* Add cols, rows and depths and put this in the map */
  90. value = x + y + z;
  91. Rast3d_put_value(map, x, y, z, &value, DCELL_TYPE);
  92. }
  93. }
  94. }
  95. /* Write everything to the disk */
  96. Rast3d_flush_all_tiles(map);
  97. Rast3d_close(map);
  98. map = Rast3d_open_cell_old("test_put_get_value_dcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  99. /* Reread the map and compare the expected results */
  100. G_message("Get the value of the upper left corner -> 0");
  101. col = row = depth = 0;
  102. north = region.north - 0.1; /* north would be out of bounds therefor -0.1 */
  103. east = region.west + region.ew_res * col;
  104. top = region.bottom + region.tb_res * depth;
  105. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  106. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  107. col = row = depth = 1;
  108. north = region.north - region.ns_res * (row + 1);
  109. east = region.west + region.ew_res * col;
  110. top = region.bottom + region.tb_res * depth;
  111. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  112. G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
  113. col = 4;
  114. row = 3;
  115. depth = 2;
  116. north = region.north - region.ns_res * (row + 1);
  117. east = region.west + region.ew_res * col;
  118. top = region.bottom + region.tb_res * depth;
  119. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  120. G_message("Get the value of x == 9 y == 14 z == 4 -> x + y + z = 27");
  121. col = 9;
  122. row = 14;
  123. depth = 4;
  124. north = region.north - region.ns_res * (row + 1);
  125. east = region.west + region.ew_res * col;
  126. top = region.bottom + region.tb_res * depth;
  127. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 1);
  128. G_message("Get the value of x == 10 y == 15 z == 5 -> x + y + z = NAN");
  129. col = 10;
  130. row = 15;
  131. depth = 5;
  132. north = region.north - region.ns_res * (row + 1);
  133. east = region.west + region.ew_res * col;
  134. top = region.bottom + region.tb_res * depth;
  135. Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
  136. Rast3d_get_value(map, col, row, depth, &value_ref, DCELL_TYPE);
  137. /* Rast3d_get_value_region does not work with coordinates outside the region */
  138. printf("Value %g == %g\n", value, value_ref);
  139. if(value == 0 || value < 0 || value > 0) {
  140. G_message("Error in Rast3d_get_region_value");
  141. sum++;
  142. }
  143. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  144. G_message("Error in Rast3d_get_value");
  145. sum++;
  146. }
  147. Rast3d_close(map);
  148. G_remove("grid3", "test_put_get_value_dcell");
  149. return sum;
  150. }
  151. /* *************************************************************** */
  152. int test_put_get_value_fcell(void)
  153. {
  154. int sum = 0;
  155. int x, y, z;
  156. FCELL value;
  157. FCELL value_ref;
  158. G_message("Testing FCELL put get value functions");
  159. double north, east, top;
  160. int col, row, depth;
  161. RASTER3D_Region region;
  162. RASTER3D_Map *map = NULL;
  163. /* We need to set up a specific region for the new raster3d map.
  164. * First we safe the default region. */
  165. Rast3d_get_window(&region);
  166. region.bottom = 0.0;
  167. region.top = 1000;
  168. region.south = 1000;
  169. region.north = 8500;
  170. region.west = 5000;
  171. region.east = 10000;
  172. region.rows = 15;
  173. region.cols = 10;
  174. region.depths = 5;
  175. Rast3d_adjust_region(&region);
  176. map = Rast3d_open_new_opt_tile_size("test_put_get_value_fcell", RASTER3D_USE_CACHE_XY, &region, FCELL_TYPE, 32);
  177. /* The window is the same as the map region ... of course */
  178. Rast3d_set_window_map(map, &region);
  179. for(z = 0; z < region.depths; z++) {
  180. for(y = 0; y < region.rows; y++) {
  181. for(x = 0; x < region.cols; x++) {
  182. /* Add cols, rows and depths and put this in the map */
  183. value = x + y + z;
  184. Rast3d_put_value(map, x, y, z, &value, FCELL_TYPE);
  185. }
  186. }
  187. }
  188. /* Write everything to the disk */
  189. Rast3d_flush_all_tiles(map);
  190. Rast3d_close(map);
  191. map = Rast3d_open_cell_old("test_put_get_value_fcell", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  192. /* Reread the map and compare the expected results */
  193. G_message("Get the value of the lower left corner -> 0");
  194. col = row = depth = 0;
  195. north = region.north - region.ns_res * (row + 1);
  196. east = region.west + region.ew_res * col;
  197. top = region.bottom + region.tb_res * depth;
  198. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  199. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  200. col = row = depth = 1;
  201. north = region.north - region.ns_res * (row + 1);
  202. east = region.west + region.ew_res * col;
  203. top = region.bottom + region.tb_res * depth;
  204. sum += test_resampling_fcell(map, north, east, top, col, row, depth, 1);
  205. G_message("Get the value of x == 4 y == 3 z == 2 -> x + y + z = 9");
  206. col = 4;
  207. row = 3;
  208. depth = 2;
  209. north = region.north - region.ns_res * (row + 1);
  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 == 9 y == 14 z == 4 -> x + y + z = 27");
  214. col = 9;
  215. row = 14;
  216. depth = 4;
  217. north = region.north - region.ns_res * (row + 1);
  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 == 10 y == 15 z == 5 -> x + y + z = NAN");
  222. col = 10;
  223. row = 15;
  224. depth = 5;
  225. north = region.north - region.ns_res * (row + 1);
  226. east = region.west + region.ew_res * col;
  227. top = region.bottom + region.tb_res * depth;
  228. Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
  229. Rast3d_get_value(map, 10, 15, 5, &value_ref, FCELL_TYPE);
  230. /* Rast3d_get_value_region does not work with coordinates outside the region */
  231. printf("Value %g == %g\n", value, value_ref);
  232. if(value == 0 || value < 0 || value > 0) {
  233. G_message("Error in Rast3d_get_region_value");
  234. sum++;
  235. }
  236. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  237. G_message("Error in Rast3d_get_value");
  238. sum++;
  239. }
  240. Rast3d_close(map);
  241. G_remove("grid3", "test_put_get_value_fcell");
  242. return sum;
  243. }
  244. /* *************************************************************** */
  245. int test_put_get_value_resampling(void)
  246. {
  247. int sum = 0;
  248. int x, y, z;
  249. DCELL value;
  250. G_message("Testing put get resample value functions");
  251. double north, east, top;
  252. int col, row, depth;
  253. RASTER3D_Region region;
  254. RASTER3D_Region window;
  255. RASTER3D_Map *map = NULL;
  256. /* We need to set up a specific region for the new raster3d map.
  257. * First we safe the default region. */
  258. Rast3d_get_window(&region);
  259. region.bottom = 0.0;
  260. region.top = 1000;
  261. region.south = 1000;
  262. region.north = 8500;
  263. region.west = 5000;
  264. region.east = 10000;
  265. region.rows = 15;
  266. region.cols = 10;
  267. region.depths = 5;
  268. Rast3d_adjust_region(&region);
  269. map = Rast3d_open_new_opt_tile_size("test_put_get_value_resample", RASTER3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  270. /*
  271. ROWS
  272. 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6500 7000 7500 8000 8500 9000 north
  273. |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
  274. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 region
  275. | | | | | | | | | | | | | | | |
  276. 30 28 26 24 22 20 18 16 14 12 10 8 6 4 2 0 window
  277. COLS
  278. 5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000 east
  279. |....|....|....|....|....|....|....|....|....|....|
  280. 0 1 2 3 4 5 6 7 8 9 10 region
  281. | | | | | | | | | | |
  282. 0 2 4 6 8 10 12 14 16 18 20 window
  283. DEPTHS
  284. 0 200 400 600 800 1000 top
  285. |....|....|....|....|....|
  286. 0 1 2 3 4 5 region
  287. | | | | | |
  288. 0 2 4 6 8 10 window
  289. */
  290. for(z = 0; z < region.depths; z++) {
  291. for(y = 0; y < region.rows; y++) { /* North to south */
  292. for(x = 0; x < region.cols; x++) {
  293. /* Add cols, rows and depths and put this in the map */
  294. value = x + y + z;
  295. Rast3d_put_double(map, x, y, z, value);
  296. }
  297. }
  298. }
  299. /* Write everything to the disk */
  300. Rast3d_flush_all_tiles(map);
  301. Rast3d_close(map);
  302. /* We modify the window for resampling tests */
  303. Rast3d_region_copy(&window, &region);
  304. /* Double the cols, rows and depths -> 8x resolution window */
  305. window.rows = 30;
  306. window.cols = 20;
  307. window.depths = 10;
  308. Rast3d_adjust_region(&window);
  309. map = Rast3d_open_cell_old("test_put_get_value_resample", G_mapset(), &region, DCELL_TYPE, RASTER3D_USE_CACHE_XY);
  310. /* The window has the 8x resolution as the map region */
  311. Rast3d_set_window_map(map, &window);
  312. /* Reread the map and compare the expected results */
  313. G_message("Get the value of the upper left corner -> 0");
  314. col = row = depth = 0;
  315. north = region.north - region.ns_res * (row + 1);
  316. east = region.west + region.ew_res * col;
  317. top = region.bottom + region.tb_res * depth;
  318. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  319. G_message("Get the value of x == y == z == 1 -> x + y + z == 3");
  320. col = row = depth = 1;
  321. north = region.north - region.ns_res * (row + 1);
  322. east = region.west + region.ew_res * col;
  323. top = region.bottom + region.tb_res * depth;
  324. sum += test_resampling_dcell(map, north, east, top, col, row, depth, 2);
  325. G_message("Get the value of x == 7 y == 9 z == 3 -> x + y + z == 19");
  326. col = 7;
  327. row = 9;
  328. depth = 3;
  329. north = region.north - region.ns_res * (row + 1);
  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 == 9 y == 14 z == 4 -> x + y + z == 27");
  334. col = 9;
  335. row = 14;
  336. depth = 4;
  337. north = region.north - region.ns_res * (row + 1);
  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. sum += test_get_value_region(map, region.cols, region.rows, region.depths);
  342. Rast3d_close(map);
  343. G_remove("grid3", "test_put_get_value_resample");
  344. return sum;
  345. }
  346. /* *************************************************************** */
  347. int test_resampling_dcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  348. {
  349. int sum = 0;
  350. DCELL value;
  351. DCELL value_ref;
  352. DCELL value_reg;
  353. DCELL value_win;
  354. Rast3d_get_region_value(map, north, east, top, &value, DCELL_TYPE);
  355. Rast3d_get_window_value(map, north, east, top, &value_win, DCELL_TYPE);
  356. Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, DCELL_TYPE);
  357. Rast3d_get_value_region(map, col, row, depth, &value_reg, DCELL_TYPE);
  358. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  359. if(value != col + row + depth) {
  360. G_message("Error in Rast3d_get_region_value");
  361. sum++;
  362. }
  363. if(value_win != col + row + depth) {
  364. G_message("Error in Rast3d_get_window_value");
  365. sum++;
  366. }
  367. if(value_ref != col + row + depth) {
  368. G_message("Error in Rast3d_get_value");
  369. sum++;
  370. }
  371. if(value_reg != col + row + depth) {
  372. G_message("Error in Rast3d_get_value_region");
  373. sum++;
  374. }
  375. return sum;
  376. }
  377. /* *************************************************************** */
  378. int test_resampling_fcell(RASTER3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  379. {
  380. int sum = 0;
  381. FCELL value;
  382. FCELL value_ref;
  383. FCELL value_reg;
  384. FCELL value_win;
  385. Rast3d_get_region_value(map, north, east, top, &value, FCELL_TYPE);
  386. Rast3d_get_window_value(map, north, east, top, &value_win, FCELL_TYPE);
  387. Rast3d_get_value(map, col * fact, row * fact, depth * fact, &value_ref, FCELL_TYPE);
  388. Rast3d_get_value_region(map, col, row, depth, &value_reg, FCELL_TYPE);
  389. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  390. if(value != col + row + depth) {
  391. G_message("Error in Rast3d_get_region_value");
  392. sum++;
  393. }
  394. if(value_win != col + row + depth) {
  395. G_message("Error in Rast3d_get_window_value");
  396. sum++;
  397. }
  398. if(value_ref != col + row + depth) {
  399. G_message("Error in Rast3d_get_value");
  400. sum++;
  401. }
  402. if(value_reg != col + row + depth) {
  403. G_message("Error in Rast3d_get_value_region");
  404. sum++;
  405. }
  406. return sum;
  407. }
  408. /* *************************************************************** */
  409. int test_get_value_region(RASTER3D_Map *map, int cols, int rows, int depths)
  410. {
  411. int sum = 0;
  412. FCELL fvalue1 = 0.0;
  413. FCELL fvalue2 = 0.0;
  414. DCELL dvalue1 = 0.0;
  415. DCELL dvalue2 = 0.0;
  416. /* Test for correct Null value */
  417. Rast3d_get_value_region(map, -1, -1, -1, &fvalue1, FCELL_TYPE);
  418. Rast3d_get_value_region(map, cols, rows, depths, &fvalue2, FCELL_TYPE);
  419. Rast3d_get_value_region(map, -1, -1, -1, &dvalue1, DCELL_TYPE);
  420. Rast3d_get_value_region(map, cols, rows, depths, &dvalue2, DCELL_TYPE);
  421. printf("Value %g == %g == %g == %g\n", fvalue1, fvalue2, dvalue1, dvalue2);
  422. if(!Rast_is_f_null_value(&fvalue1)) {
  423. G_message("Error in Rast3d_get_value_region");
  424. sum++;
  425. }
  426. if(!Rast_is_f_null_value(&fvalue2)) {
  427. G_message("Error in Rast3d_get_value_region");
  428. sum++;
  429. }
  430. if(!Rast_is_d_null_value(&dvalue1)) {
  431. G_message("Error in Rast3d_get_value_region");
  432. sum++;
  433. }
  434. if(!Rast_is_d_null_value(&dvalue2)) {
  435. G_message("Error in Rast3d_get_value_region");
  436. sum++;
  437. }
  438. return sum;
  439. }