box.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*!
  2. \file lib/vector/Vlib/box.c
  3. \brief Vector library - bounding box
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. */
  12. #include <stdlib.h>
  13. #include <grass/vector.h>
  14. /*!
  15. \brief Tests for point in box
  16. \param x,y,z coordinates
  17. \param Box boundary box
  18. \return 1 point is in box
  19. \return 0 point is not in box
  20. */
  21. int Vect_point_in_box(double x, double y, double z, const struct bound_box * Box)
  22. {
  23. if (x >= Box->W && x <= Box->E &&
  24. y >= Box->S && y <= Box->N && z >= Box->B && z <= Box->T) {
  25. return 1;
  26. }
  27. return 0;
  28. }
  29. /*!
  30. \brief Tests for overlap of two boxes
  31. \param A boundary box A
  32. \param B boundary box B
  33. \return 1 boxes overlap
  34. \return 0 boxes do not overlap
  35. */
  36. int Vect_box_overlap(const struct bound_box * A, const struct bound_box * B)
  37. {
  38. if (A->E < B->W || A->W > B->E ||
  39. A->N < B->S || A->S > B->N || A->T < B->B || A->B > B->T) {
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. /*!
  45. \brief Copy box B to box A
  46. \param A boundary A
  47. \param B boundary B
  48. \return 1
  49. */
  50. int Vect_box_copy(struct bound_box * A, const struct bound_box * B)
  51. {
  52. A->N = B->N;
  53. A->S = B->S;
  54. A->E = B->E;
  55. A->W = B->W;
  56. A->T = B->T;
  57. A->B = B->B;
  58. return 1;
  59. }
  60. /*!
  61. \brief Extend box A by box B
  62. \param A boundary A
  63. \param B boundary B
  64. \return 1
  65. */
  66. int Vect_box_extend(struct bound_box * A, const struct bound_box * B)
  67. {
  68. if (B->N > A->N)
  69. A->N = B->N;
  70. if (B->S < A->S)
  71. A->S = B->S;
  72. if (B->E > A->E)
  73. A->E = B->E;
  74. if (B->W < A->W)
  75. A->W = B->W;
  76. if (B->T > A->T)
  77. A->T = B->T;
  78. if (B->B < A->B)
  79. A->B = B->B;
  80. return 1;
  81. }
  82. /*!
  83. * \brief Clip coordinates to box, if necessary, lines extending outside of a box.
  84. *
  85. * A line represented by the coordinates <em>x, y</em> and <em>c_x, c_y</em> is clipped to
  86. * the window defined by <em>s</em> (south), <em>n</em> (north), <em>w</em>
  87. * (west), and <em>e</em> (east). Note that the following constraints must be
  88. * true:
  89. * w <e
  90. * s <n
  91. * The <em>x</em> and <em>c_x</em> are values to be compared to <em>w</em> and
  92. * <em>e.</em> The <em>y</em> and <em>c_y</em> are values to be compared to
  93. * <em>s</em> and <em>n.</em>
  94. * The <em>x</em> and <em>c_x</em> values returned lie between <em>w</em> and
  95. * <em>e.</em> The <em>y</em> and <em>c_y</em> values returned lie between
  96. * <em>s</em> and <em>n.</em>
  97. *
  98. * \param x, y coordinates (w, e)
  99. * \param c_x,c_y coordinates (s, n)
  100. * \param Box boundary box
  101. *
  102. * \return 1 if any clipping occured
  103. * \return 0 otherwise
  104. */
  105. int
  106. Vect_box_clip(double *x, double *y, double *c_x, double *c_y, const struct bound_box * Box)
  107. {
  108. int mod;
  109. mod = 0;
  110. if (*x < Box->W) {
  111. if (*c_x != *x)
  112. *y = *y + (Box->W - *x) / (*c_x - *x) * (*c_y - *y);
  113. *x = Box->W;
  114. mod = 1;
  115. }
  116. if (*x > Box->E) {
  117. if (*c_x != *x)
  118. *y = *y + (Box->E - *x) / (*c_x - *x) * (*c_y - *y);
  119. *x = Box->E;
  120. mod = 1;
  121. }
  122. if (*c_x < Box->W) {
  123. if (*c_x != *x)
  124. *c_y = *c_y + (Box->W - *c_x) / (*x - *c_x) * (*y - *c_y);
  125. *c_x = Box->W;
  126. mod = 1;
  127. }
  128. if (*c_x > Box->E) {
  129. if (*c_x != *x)
  130. *c_y = *c_y + (Box->E - *c_x) / (*x - *c_x) * (*y - *c_y);
  131. *c_x = Box->E;
  132. mod = 1;
  133. }
  134. if (*y < Box->S) {
  135. if (*c_y != *y)
  136. *x = *x + (Box->S - *y) / (*c_y - *y) * (*c_x - *x);
  137. *y = Box->S;
  138. mod = 1;
  139. }
  140. if (*y > Box->N) {
  141. if (*c_y != *y)
  142. *x = *x + (Box->N - *y) / (*c_y - *y) * (*c_x - *x);
  143. *y = Box->N;
  144. mod = 1;
  145. }
  146. if (*c_y < Box->S) {
  147. if (*c_y != *y)
  148. *c_x = *c_x + (Box->S - *c_y) / (*y - *c_y) * (*x - *c_x);
  149. *c_y = Box->S;
  150. mod = 1;
  151. }
  152. if (*c_y > Box->N) {
  153. if (*c_y != *y)
  154. *c_x = *c_x + (Box->N - *c_y) / (*y - *c_y) * (*x - *c_x);
  155. *c_y = Box->N;
  156. mod = 1;
  157. }
  158. return (mod);
  159. }
  160. /*!
  161. \brief Get bounding box of given feature
  162. \param Map vector map
  163. \param line feature id
  164. \param[out] Box bounding box
  165. \return 1 on success
  166. \return 0 line is dead
  167. */
  168. int Vect_get_line_box(const struct Map_info *Map, int line, struct bound_box * Box)
  169. {
  170. const struct Plus_head *Plus;
  171. struct P_line *Line;
  172. Plus = &(Map->plus);
  173. Line = Plus->Line[line];
  174. if (Line == NULL) { /* dead */
  175. Box->N = 0;
  176. Box->S = 0;
  177. Box->E = 0;
  178. Box->W = 0;
  179. Box->T = 0;
  180. Box->B = 0;
  181. return 0;
  182. }
  183. else {
  184. Box->N = Line->N;
  185. Box->S = Line->S;
  186. Box->E = Line->E;
  187. Box->W = Line->W;
  188. Box->T = Line->T;
  189. Box->B = Line->B;
  190. }
  191. return 1;
  192. }
  193. /*!
  194. \brief Get bounding box of area
  195. \param Map vector map
  196. \param area area id
  197. \param[out] Box bounding box
  198. \return 1 on success
  199. \return 0 area is dead
  200. */
  201. int Vect_get_area_box(const struct Map_info *Map, int area, struct bound_box * Box)
  202. {
  203. const struct Plus_head *Plus;
  204. struct P_area *Area;
  205. Plus = &(Map->plus);
  206. Area = Plus->Area[area];
  207. if (Area == NULL) { /* dead */
  208. Box->N = 0;
  209. Box->S = 0;
  210. Box->E = 0;
  211. Box->W = 0;
  212. Box->T = 0;
  213. Box->B = 0;
  214. return 0;
  215. }
  216. else {
  217. Box->N = Area->N;
  218. Box->S = Area->S;
  219. Box->E = Area->E;
  220. Box->W = Area->W;
  221. Box->T = Area->T;
  222. Box->B = Area->B;
  223. }
  224. return 1;
  225. }
  226. /*!
  227. \brief Get bounding box of isle
  228. \param Map vector map
  229. \param isle isle id
  230. \param[out] Box bounding box
  231. \return 1 on success
  232. \return 0 isle is dead
  233. */
  234. int Vect_get_isle_box(const struct Map_info *Map, int isle, struct bound_box * Box)
  235. {
  236. const struct Plus_head *Plus;
  237. struct P_isle *Isle;
  238. Plus = &(Map->plus);
  239. Isle = Plus->Isle[isle];
  240. if (Isle == NULL) { /* dead */
  241. Box->N = 0;
  242. Box->S = 0;
  243. Box->E = 0;
  244. Box->W = 0;
  245. Box->T = 0;
  246. Box->B = 0;
  247. return 0;
  248. }
  249. else {
  250. Box->N = Isle->N;
  251. Box->S = Isle->S;
  252. Box->E = Isle->E;
  253. Box->W = Isle->W;
  254. Box->T = Isle->T;
  255. Box->B = Isle->B;
  256. }
  257. return 1;
  258. }
  259. /*!
  260. \brief Get bounding box of map (all features in the map)
  261. \param Map vector map
  262. \param[out] Box bouding box
  263. \return 1 on success
  264. \return 0 on error
  265. */
  266. int Vect_get_map_box(const struct Map_info *Map, struct bound_box * Box)
  267. {
  268. const struct Plus_head *Plus;
  269. Plus = &(Map->plus);
  270. Box->N = Plus->box.N;
  271. Box->S = Plus->box.S;
  272. Box->E = Plus->box.E;
  273. Box->W = Plus->box.W;
  274. Box->T = Plus->box.T;
  275. Box->B = Plus->box.B;
  276. return 1;
  277. }
  278. /*!
  279. \brief Copy region window to bounding box
  280. \param Window region structure (raster-based)
  281. \param[out] Box boundary box (vector-based)
  282. \return 1 on success
  283. \return 0 on error
  284. */
  285. int Vect_region_box(const struct Cell_head *Window, struct bound_box * Box)
  286. {
  287. Box->N = Window->north;
  288. Box->S = Window->south;
  289. Box->E = Window->east;
  290. Box->W = Window->west;
  291. Box->T = PORT_DOUBLE_MAX;
  292. Box->B = -PORT_DOUBLE_MAX;
  293. return 1;
  294. }