test_put_get_value.c 18 KB

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