test_coordinate_transform.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "test_g3d_lib.h"
  4. #include "grass/interpf.h"
  5. static int test_coordinate_transform(void);
  6. static int test_region(void);
  7. /* *************************************************************** */
  8. /* Perfrome the coordinate transformation tests ****************** */
  9. /* *************************************************************** */
  10. int unit_test_coordinate_transform(void)
  11. {
  12. int sum = 0;
  13. G_message(_("\n++ Running g3d coordinate transform unit tests ++"));
  14. sum += test_coordinate_transform();
  15. sum += test_region();
  16. if (sum > 0)
  17. G_warning(_("\n-- g3d coordinate transform unit tests failure --"));
  18. else
  19. G_message(_("\n-- g3d coordinate transform unit tests finished successfully --"));
  20. return sum;
  21. }
  22. /* *************************************************************** */
  23. int test_coordinate_transform(void)
  24. {
  25. int sum = 0;
  26. double north, east, top;
  27. int col = 0, row = 0, depth = 0;
  28. G3D_Region region, default_region;
  29. G3D_Map *map = NULL;
  30. /* We need to set up a specific region for the new g3d map.
  31. * First we safe the default region. */
  32. G3d_getWindow(&default_region);
  33. G3d_regionCopy(&region, &default_region);
  34. region.bottom = 0.0;
  35. region.top = 1000;
  36. region.south = 1000;
  37. region.north = 8500;
  38. region.west = 5000;
  39. region.east = 10000;
  40. region.rows = 15;
  41. region.cols = 10;
  42. region.depths = 5;
  43. G3d_adjustRegion(&region);
  44. map = G3d_openNewOptTileSize("test_coordinate_transform", G3D_USE_CACHE_XYZ, &region, FCELL_TYPE, 32);
  45. /* The window is the same as the map region ... of course */
  46. G3d_setWindowMap(map, &region);
  47. G_message("Test the upper right corner, coordinates must be col = 9, row = 0, depth = 4");
  48. /*
  49. ROWS
  50. 1000 1500 2000 2500 3000 3500 4000 4500 5000 5500 6500 7000 7500 8000 8500 9000
  51. |....|....|....|....|....|....|....|....|....|....|....|....|....|....|....|
  52. 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
  53. COLS
  54. 5000 5500 6000 6500 7000 7500 8000 8500 9000 9500 10000
  55. |....|....|....|....|....|....|....|....|....|....|
  56. 0 1 2 3 4 5 6 7 8 9 10
  57. DEPTHS
  58. 0 200 400 600 800 1000
  59. |....|....|....|....|....|
  60. 0 1 2 3 4 5
  61. */
  62. north = 8499.9;
  63. east= 9999.9;
  64. top = 999.9;
  65. G3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  66. printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
  67. if(region.cols - 1 != col || 0 != row || region.depths - 1 != depth) {
  68. G_message("Error in G3d_location2coord");
  69. sum++;
  70. }
  71. G_message("Test the lower left corner, coordinates must be col = 0 row = 14 depth = 0");
  72. north = 1000.0;
  73. east= 5000.0;
  74. top = 0.0;
  75. G3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  76. printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
  77. if(0 != col || 14 != row || 0 != depth) {
  78. G_message("Error in G3d_location2coord");
  79. sum++;
  80. }
  81. G_message("Test the center, coordinates must be col = 4 row = 7 depth = 2");
  82. north = 4750.0;
  83. east= 7499.9;
  84. top = 500.0;
  85. G3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  86. printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
  87. if((region.cols - 1)/2 != col || (region.rows - 1)/2 != row || (region.depths - 1)/2 != depth) {
  88. G_message("Error in G3d_location2coord");
  89. sum++;
  90. }
  91. G_message("Test the n=3000.1, e=7000.1 and t=800.1, coordinates must be col = 4 row = 10 depth = 4");
  92. north = 3000.1;
  93. east= 7000.1;
  94. top = 800.1;
  95. G3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  96. printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
  97. if(4 != col || map->region.rows - 5 != row || 4 != depth) {
  98. G_message("Error in G3d_location2coord");
  99. sum++;
  100. }
  101. G_message("Test the n=2999.9, e=6999.9 and t=799.9, coordinates must be col = 3 row = 11 depth = 3");
  102. north = 2999.9;
  103. east= 6999.9;
  104. top = 799.9;
  105. G3d_location2coord(&(map->region), north, east, top, &col, &row, &depth);
  106. printf("G3d_location2coord col %i row %i depth %i\n", col, row, depth);
  107. if(3 != col || map->region.rows - 4 != row || 3 != depth) {
  108. G_message("Error in G3d_location2coord");
  109. sum++;
  110. }
  111. G3d_closeCell(map);
  112. G_remove("grid3", "test_coordinate_transform");
  113. return sum;
  114. }
  115. /* *************************************************************** */
  116. int test_region(void)
  117. {
  118. int sum = 0;
  119. G3D_Region region, new_region;
  120. G3d_getWindow(&region);
  121. region.bottom = 0.0;
  122. region.top = 1000;
  123. region.south = 10000;
  124. region.north = 20000;
  125. region.west = 5000;
  126. region.east = 10000;
  127. region.rows = 20;
  128. region.cols = 10;
  129. region.depths = 5;
  130. region.ew_res = 0;
  131. region.ns_res = 0;
  132. region.tb_res = 0;
  133. /* Test region adjustment */
  134. G3d_adjustRegion(&region);
  135. if(region.ew_res != 500) {
  136. G_message("Error in G3d_adjustRegion: region.ew_res != 500");
  137. sum++;
  138. }
  139. if(region.ns_res != 500) {
  140. G_message("Error in G3d_adjustRegion: region.ew_res != 500");
  141. sum++;
  142. }
  143. if(region.tb_res != 200) {
  144. G_message("Error in G3d_adjustRegion: region.ew_res != 500");
  145. sum++;
  146. }
  147. /* Test the region copy */
  148. G3d_regionCopy(&new_region, &region);
  149. if(region.bottom != new_region.bottom) {
  150. G_message("Error in G3d_regionCopy: region.bottom != new_region.bottom");
  151. sum++;
  152. }
  153. if(region.cols != new_region.cols) {
  154. G_message("Error in G3d_regionCopy: region.cols != new_region.cols");
  155. sum++;
  156. }
  157. if(region.depths != new_region.depths) {
  158. G_message("Error in G3d_regionCopy: region.depths != new_region.depths");
  159. sum++;
  160. }
  161. if(region.east != new_region.east) {
  162. G_message("Error in G3d_regionCopy: region.east != new_region.east");
  163. sum++;
  164. }
  165. if(region.ew_res != new_region.ew_res) {
  166. G_message("Error in G3d_regionCopy: region.ew_res != new_region.ew_res");
  167. sum++;
  168. }
  169. if(region.north != new_region.north) {
  170. G_message("Error in G3d_regionCopy: region.north != new_region.north");
  171. sum++;
  172. }
  173. if(region.ns_res != new_region.ns_res) {
  174. G_message("Error in G3d_regionCopy: region.ns_res != new_region.ns_res");
  175. sum++;
  176. }
  177. if(region.proj != new_region.proj) {
  178. G_message("Error in G3d_regionCopy: region.proj != new_region.proj");
  179. sum++;
  180. }
  181. if(region.rows != new_region.rows) {
  182. G_message("Error in G3d_regionCopy: region.rows != new_region.rows");
  183. sum++;
  184. }
  185. if(region.south != new_region.south) {
  186. G_message("Error in G3d_regionCopy: region.south != new_region.south");
  187. sum++;
  188. }
  189. if(region.tb_res != new_region.tb_res) {
  190. G_message("Error in G3d_regionCopy: region.tb_res != new_region.tb_res");
  191. sum++;
  192. }
  193. if(region.top != new_region.top) {
  194. G_message("Error in G3d_regionCopy: region.top != new_region.top");
  195. sum++;
  196. }
  197. if(region.west != new_region.west) {
  198. G_message("Error in G3d_regionCopy: region.west != new_region.west");
  199. sum++;
  200. }
  201. if(region.zone != new_region.zone) {
  202. G_message("Error in G3d_regionCopy: region.zone != new_region.zone");
  203. sum++;
  204. }
  205. return sum;
  206. }