box.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 occurred
  102. * \return 0 otherwise
  103. */
  104. int Vect_box_clip(double *x, double *y, double *c_x, double *c_y, const struct bound_box *Box)
  105. {
  106. int mod;
  107. mod = 0;
  108. if (*x < Box->W) {
  109. if (*c_x != *x)
  110. *y = *y + (Box->W - *x) / (*c_x - *x) * (*c_y - *y);
  111. *x = Box->W;
  112. mod = 1;
  113. }
  114. if (*x > Box->E) {
  115. if (*c_x != *x)
  116. *y = *y + (Box->E - *x) / (*c_x - *x) * (*c_y - *y);
  117. *x = Box->E;
  118. mod = 1;
  119. }
  120. if (*c_x < Box->W) {
  121. if (*c_x != *x)
  122. *c_y = *c_y + (Box->W - *c_x) / (*x - *c_x) * (*y - *c_y);
  123. *c_x = Box->W;
  124. mod = 1;
  125. }
  126. if (*c_x > Box->E) {
  127. if (*c_x != *x)
  128. *c_y = *c_y + (Box->E - *c_x) / (*x - *c_x) * (*y - *c_y);
  129. *c_x = Box->E;
  130. mod = 1;
  131. }
  132. if (*y < Box->S) {
  133. if (*c_y != *y)
  134. *x = *x + (Box->S - *y) / (*c_y - *y) * (*c_x - *x);
  135. *y = Box->S;
  136. mod = 1;
  137. }
  138. if (*y > Box->N) {
  139. if (*c_y != *y)
  140. *x = *x + (Box->N - *y) / (*c_y - *y) * (*c_x - *x);
  141. *y = Box->N;
  142. mod = 1;
  143. }
  144. if (*c_y < Box->S) {
  145. if (*c_y != *y)
  146. *c_x = *c_x + (Box->S - *c_y) / (*y - *c_y) * (*x - *c_x);
  147. *c_y = Box->S;
  148. mod = 1;
  149. }
  150. if (*c_y > Box->N) {
  151. if (*c_y != *y)
  152. *c_x = *c_x + (Box->N - *c_y) / (*y - *c_y) * (*x - *c_x);
  153. *c_y = Box->N;
  154. mod = 1;
  155. }
  156. return (mod);
  157. }
  158. /*!
  159. \brief Get bounding box of given feature
  160. Vector map must be open at topological level and built with level
  161. >= GV_BUILD_BASE.
  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. \return -1 on error
  168. */
  169. int Vect_get_line_box(const struct Map_info *Map, int line, struct bound_box *Box)
  170. {
  171. struct Plus_head *Plus;
  172. struct P_line *Line;
  173. int type;
  174. static struct line_pnts *Points = NULL;
  175. Plus = (struct Plus_head *) &(Map->plus);
  176. if (line < 1 || line > Plus->n_lines) {
  177. G_warning(_("Attempt to access feature with invalid id (%d)"), line);
  178. return -1;
  179. }
  180. Line = Plus->Line[line];
  181. if (Line == NULL) { /* dead */
  182. Box->N = Box->S = Box->E = Box->W = Box->T = Box->B = 0. / 0.;
  183. return 0;
  184. }
  185. type = Line->type;
  186. /* GV_LINES: retrieve box from spatial index */
  187. if (type & GV_LINES) {
  188. if (dig_find_line_box(Plus, line, Box) == 0) {
  189. G_warning(_("Unable to determine bbox for feature %d"), line);
  190. return -1;
  191. }
  192. if (!Vect_is_3d(Map)) {
  193. Box->T = PORT_DOUBLE_MAX;
  194. Box->B = -PORT_DOUBLE_MAX;
  195. }
  196. return 1;
  197. }
  198. /* all other: read line */
  199. if (Points == NULL)
  200. Points = Vect_new_line_struct();
  201. Vect_read_line(Map, Points, NULL, line);
  202. dig_line_box(Points, Box);
  203. if (!Vect_is_3d(Map)) {
  204. Box->T = PORT_DOUBLE_MAX;
  205. Box->B = -PORT_DOUBLE_MAX;
  206. }
  207. return 1;
  208. }
  209. /*!
  210. \brief Get bounding box of area
  211. Vector map must be open at topological level and built with level
  212. >= GV_BUILD_AREAS.
  213. \param Map vector map
  214. \param area area id
  215. \param[out] Box bounding box
  216. \return 1 on success
  217. \return 0 area is dead
  218. \return -1 on error
  219. */
  220. int Vect_get_area_box(const struct Map_info *Map, int area, struct bound_box *Box)
  221. {
  222. struct Plus_head *Plus;
  223. struct P_area *Area;
  224. Plus = (struct Plus_head *) &(Map->plus);
  225. if (area < 1 || area > Plus->n_areas) {
  226. G_warning(_("Attempt to access area with invalid id (%d)"), area);
  227. return -1;
  228. }
  229. Area = Plus->Area[area];
  230. if (Area == NULL) { /* dead */
  231. Box->N = Box->S = Box->E = Box->W = Box->T = Box->B = 0. / 0.;
  232. return 0;
  233. }
  234. if (dig_find_area_box(Plus, area, Box) == 0) {
  235. G_warning(_("Unable to determine bbox for area %d"), area);
  236. return -1;
  237. }
  238. if (!Vect_is_3d(Map)) {
  239. Box->T = PORT_DOUBLE_MAX;
  240. Box->B = -PORT_DOUBLE_MAX;
  241. }
  242. return 1;
  243. }
  244. /*!
  245. \brief Get bounding box of isle
  246. Vector map must be open at topological level and built with level
  247. >= GV_BUILD_AREAS.
  248. \param Map vector map
  249. \param isle isle id
  250. \param[out] Box bounding box
  251. \return 1 on success
  252. \return 0 isle is dead / bounding box not found
  253. \return -1 on error
  254. */
  255. int Vect_get_isle_box(const struct Map_info *Map, int isle, struct bound_box *Box)
  256. {
  257. struct Plus_head *Plus;
  258. struct P_isle *Isle;
  259. Plus = (struct Plus_head *) &(Map->plus);
  260. if (isle < 1 || isle > Plus->n_isles) {
  261. G_warning(_("Attempt to access area with invalid id (%d)"), isle);
  262. return -1;
  263. }
  264. Isle = Plus->Isle[isle];
  265. if (Isle == NULL) { /* dead */
  266. Box->N = Box->S = Box->E = Box->W = Box->T = Box->B = 0. / 0.;
  267. return 0;
  268. }
  269. if (dig_find_isle_box(Plus, isle, Box) == 0) {
  270. G_warning(_("Unable to determine bbox for isle %d"), isle);
  271. return -1;
  272. }
  273. if (!Vect_is_3d(Map)) {
  274. Box->T = PORT_DOUBLE_MAX;
  275. Box->B = -PORT_DOUBLE_MAX;
  276. }
  277. return 1;
  278. }
  279. /*!
  280. \brief Get bounding box of map (all features in the map)
  281. \param Map vector map
  282. \param[out] Box bouding box
  283. \return 1 on success
  284. \return 0 on error
  285. */
  286. int Vect_get_map_box(const struct Map_info *Map, struct bound_box *Box)
  287. {
  288. const struct Plus_head *Plus;
  289. Plus = &(Map->plus);
  290. Box->N = Plus->box.N;
  291. Box->S = Plus->box.S;
  292. Box->E = Plus->box.E;
  293. Box->W = Plus->box.W;
  294. Box->T = Plus->box.T;
  295. Box->B = Plus->box.B;
  296. return 1;
  297. }
  298. /*!
  299. \brief Copy region window to bounding box
  300. \param Window region structure (raster-based)
  301. \param[out] Box boundary box (vector-based)
  302. \return 1 on success
  303. \return 0 on error
  304. */
  305. int Vect_region_box(const struct Cell_head *Window, struct bound_box *Box)
  306. {
  307. Box->N = Window->north;
  308. Box->S = Window->south;
  309. Box->E = Window->east;
  310. Box->W = Window->west;
  311. Box->T = PORT_DOUBLE_MAX;
  312. Box->B = -PORT_DOUBLE_MAX;
  313. return 1;
  314. }