g3dwindow.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <grass/gis.h>
  3. #include <grass/G3d.h>
  4. #include "G3d_intern.h"
  5. /*---------------------------------------------------------------------------*/
  6. G3D_Region g3d_window;
  7. /*---------------------------------------------------------------------------*/
  8. /*!
  9. * \brief
  10. *
  11. * Sets the window for <em>map</em> to <em>window</em>.
  12. * Can be used multiple times for the same map.
  13. *
  14. * \param map
  15. * \param window
  16. * \return void
  17. */
  18. void G3d_setWindowMap(G3D_Map * map, G3D_Region * window)
  19. {
  20. G3d_regionCopy(&(map->window), window);
  21. G3d_adjustRegion(&(map->window));
  22. }
  23. /*---------------------------------------------------------------------------*/
  24. /*!
  25. * \brief
  26. *
  27. * Sets the default window used for every map opened later in the program.
  28. * Can be used multiple times in the same program.
  29. *
  30. * \param window
  31. * \return void
  32. */
  33. void G3d_setWindow(G3D_Region * window)
  34. {
  35. G3d_regionCopy(&g3d_window, window);
  36. G3d_adjustRegion(&g3d_window);
  37. }
  38. /*---------------------------------------------------------------------------*/
  39. /*!
  40. * \brief
  41. *
  42. * Stores the current default window in <em>window</em>.
  43. *
  44. * \param window
  45. * \return void
  46. */
  47. void G3d_getWindow(G3D_Region * window)
  48. {
  49. G3d_regionCopy(window, &g3d_window);
  50. }
  51. /*---------------------------------------------------------------------------*/
  52. G3D_Region *G3d_windowPtr()
  53. {
  54. return &g3d_window;
  55. }
  56. /*---------------------------------------------------------------------------*/
  57. /*!
  58. * \brief
  59. *
  60. * Returns in <em>*value</em> the cell-value of the cell with
  61. * window-coordinate <em>(x, y, z)</em>. The value returned is of <em>type</em>.
  62. * This function invokes a fatal error if an error occurs.
  63. *
  64. * \param map
  65. * \param x
  66. * \param y
  67. * \param z
  68. * \param value
  69. * \param type
  70. * \return void
  71. */
  72. void G3d_getValue(G3D_Map * map, int x, int y, int z, void *value, int type)
  73. {
  74. double north, east, top;
  75. /*AV*/
  76. /* BEGIN OF ORIGINAL CODE */
  77. /*
  78. int row, col, depth;
  79. */
  80. /* END OF ORIGINAL CODE */
  81. /*AV*/
  82. /* BEGIN OF MY CODE */
  83. double row, col, depth;
  84. /* END OF MY CODE */
  85. /* convert (x, y, z) into (north, east, top) */
  86. /*AV*/
  87. /* BEGIN OF ORIGINAL CODE */
  88. /*
  89. north = ((double) map->window.rows - y - 0.5) / (double) map->window.rows *
  90. (map->window.north - map->window.south) + map->window.south;
  91. */
  92. /* END OF ORIGINAL CODE */
  93. /*AV*/
  94. /* BEGIN OF MY CODE */
  95. north = ((double)y + 0.5) / (double)map->window.rows *
  96. (map->window.north - map->window.south) + map->window.south;
  97. /* END OF MY CODE */
  98. east = ((double)x + 0.5) / (double)map->window.cols *
  99. (map->window.east - map->window.west) + map->window.west;
  100. top = ((double)z + 0.5) / (double)map->window.depths *
  101. (map->window.top - map->window.bottom) + map->window.bottom;
  102. /* convert (north, east, top) into (row, col, depth) */
  103. /*AV*/
  104. /* BEGIN OF ORIGINAL CODE */
  105. /*
  106. row = map->region.rows -
  107. (north - map->region.south) / (map->region.north - map->region.south) *
  108. map->region.rows;
  109. */
  110. /* END OF ORIGINAL CODE */
  111. /*AV*/
  112. /* BEGIN OF MY CODE */
  113. row =
  114. (north - map->region.south) / (map->region.north -
  115. map->region.south) * map->region.rows;
  116. /* END OF MY CODE */
  117. col = (east - map->region.west) / (map->region.east - map->region.west) *
  118. map->region.cols;
  119. depth =
  120. (top - map->region.bottom) / (map->region.top -
  121. map->region.bottom) *
  122. map->region.depths;
  123. /* if (row, col, depth) outside window return NULL value */
  124. if ((row < 0) || (row >= map->region.rows) ||
  125. (col < 0) || (col >= map->region.cols) ||
  126. (depth < 0) || (depth >= map->region.depths)) {
  127. G3d_setNullValue(value, 1, type);
  128. return;
  129. }
  130. /* get value */
  131. map->resampleFun(map, (int)row, (int)col, (int)depth, value, type);
  132. }
  133. /*---------------------------------------------------------------------------*/
  134. /*!
  135. * \brief
  136. *
  137. * Is equivalent to
  138. * <tt>G3d_getValue (map, x, y, z, &value, FCELL_TYPE);</tt> return value.
  139. *
  140. * \param map
  141. * \param x
  142. * \param y
  143. * \param z
  144. * \return float
  145. */
  146. float G3d_getFloat(G3D_Map * map, int x, int y, int z)
  147. {
  148. float value;
  149. G3d_getValue(map, x, y, z, &value, FCELL_TYPE);
  150. return value;
  151. }
  152. /*---------------------------------------------------------------------------*/
  153. /*!
  154. * \brief
  155. *
  156. * Is equivalent
  157. * to <tt>G3d_getValue (map, x, y, z, &value, DCELL_TYPE);</tt> return value.
  158. *
  159. * \param map
  160. * \param x
  161. * \param y
  162. * \param z
  163. * \return double
  164. */
  165. double G3d_getDouble(G3D_Map * map, int x, int y, int z)
  166. {
  167. double value;
  168. G3d_getValue(map, x, y, z, &value, DCELL_TYPE);
  169. return value;
  170. }