test_put_get_value.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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(G3D_Map *map, double north, double east, double
  24. top, int col, int row, int depth, int fact);
  25. static int test_resampling_fcell(G3D_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. G3D_Region region;
  54. G3D_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. G3d_getWindow(&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. G3d_adjustRegion(&region);
  68. map = G3d_openNewOptTileSize("test_put_get_value_dcell", G3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  69. /* The window is the same as the map region ... of course */
  70. G3d_setWindowMap(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. G3d_putValue(map, x, y, z, &value, DCELL_TYPE);
  91. }
  92. }
  93. }
  94. /* Write everything to the disk */
  95. G3d_flushAllTiles(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. G3d_getRegionValue(map, north, east, top, &value, DCELL_TYPE);
  133. G3d_getValue(map, col, row, depth, &value_ref, DCELL_TYPE);
  134. /* G3d_getValueRegion 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 G3d_getRegionValue");
  138. sum++;
  139. }
  140. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  141. G_message("Error in G3d_getValue");
  142. sum++;
  143. }
  144. G3d_closeCell(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. G3D_Region region;
  159. G3D_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. G3d_getWindow(&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. G3d_adjustRegion(&region);
  173. map = G3d_openNewOptTileSize("test_put_get_value_dcell", G3D_USE_CACHE_XY, &region, FCELL_TYPE, 32);
  174. /* The window is the same as the map region ... of course */
  175. G3d_setWindowMap(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. G3d_putValue(map, x, y, z, &value, FCELL_TYPE);
  182. }
  183. }
  184. }
  185. /* Write everything to the disk */
  186. G3d_flushAllTiles(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. G3d_getRegionValue(map, north, east, top, &value, FCELL_TYPE);
  224. G3d_getValue(map, 10, 15, 5, &value_ref, FCELL_TYPE);
  225. /* G3d_getValueRegion 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 G3d_getRegionValue");
  229. sum++;
  230. }
  231. if(value_ref == 0 || value_ref < 0 || value_ref > 0) {
  232. G_message("Error in G3d_getValue");
  233. sum++;
  234. }
  235. G3d_closeCell(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. G3D_Region region;
  249. G3D_Region window;
  250. G3D_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. G3d_getWindow(&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. G3d_adjustRegion(&region);
  264. map = G3d_openNewOptTileSize("test_put_get_value_resample", G3D_USE_CACHE_XY, &region, DCELL_TYPE, 32);
  265. /* We modify the window for resampling tests */
  266. G3d_regionCopy(&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. G3d_adjustRegion(&window);
  272. /* The window is the same as the map region ... of course */
  273. G3d_setWindowMap(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. G3d_putDouble(map, x, y, z, value);
  300. }
  301. }
  302. }
  303. /* Write everything to the disk */
  304. G3d_flushAllTiles(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. G3d_closeCell(map);
  335. G_remove("grid3", "test_put_get_value_dcell");
  336. return sum;
  337. }
  338. /* *************************************************************** */
  339. int test_resampling_dcell(G3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  340. {
  341. int sum = 0;
  342. DCELL value;
  343. DCELL value_ref;
  344. DCELL value_reg;
  345. DCELL value_win;
  346. G3d_getRegionValue(map, north, east, top, &value, DCELL_TYPE);
  347. G3d_getWindowValue(map, north, east, top, &value_win, DCELL_TYPE);
  348. G3d_getValue(map, col * fact, row * fact, depth * fact, &value_ref, DCELL_TYPE);
  349. G3d_getValueRegion(map, col, row, depth, &value_reg, DCELL_TYPE);
  350. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  351. if(value != col + row + depth) {
  352. G_message("Error in G3d_getRegionValue");
  353. sum++;
  354. }
  355. if(value != col + row + depth) {
  356. G_message("Error in G3d_getWindowValue");
  357. sum++;
  358. }
  359. if(value != col + row + depth) {
  360. G_message("Error in G3d_getValue");
  361. sum++;
  362. }
  363. if(value != col + row + depth) {
  364. G_message("Error in G3d_getValueRegion");
  365. sum++;
  366. }
  367. return sum;
  368. }
  369. /* *************************************************************** */
  370. int test_resampling_fcell(G3D_Map *map, double north, double east, double top, int col, int row, int depth, int fact)
  371. {
  372. int sum = 0;
  373. FCELL value;
  374. FCELL value_ref;
  375. FCELL value_reg;
  376. FCELL value_win;
  377. G3d_getRegionValue(map, north, east, top, &value, FCELL_TYPE);
  378. G3d_getWindowValue(map, north, east, top, &value_win, FCELL_TYPE);
  379. G3d_getValue(map, col * fact, row * fact, depth * fact, &value_ref, FCELL_TYPE);
  380. G3d_getValueRegion(map, col, row, depth, &value_reg, FCELL_TYPE);
  381. printf("Value %g == %g == %g == %g\n", value, value_win, value_ref, value_reg);
  382. if(value != col + row + depth) {
  383. G_message("Error in G3d_getRegionValue");
  384. sum++;
  385. }
  386. if(value != col + row + depth) {
  387. G_message("Error in G3d_getWindowValue");
  388. sum++;
  389. }
  390. if(value != col + row + depth) {
  391. G_message("Error in G3d_getValue");
  392. sum++;
  393. }
  394. if(value != col + row + depth) {
  395. G_message("Error in G3d_getValueRegion");
  396. sum++;
  397. }
  398. return sum;
  399. }