box.c 7.4 KB

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