poly.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*!
  2. \file lib/vector/Vlib/poly.c
  3. \brief Vector library - polygon related fns
  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 GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. */
  11. #include <math.h>
  12. #include <stdlib.h>
  13. #include <grass/vector.h>
  14. #include <grass/linkm.h>
  15. #include <grass/glocale.h>
  16. struct Slink
  17. {
  18. struct Slink *next;
  19. double x;
  20. };
  21. /* function prototypes */
  22. static int comp_double(double *, double *);
  23. static int V__within(double, double, double);
  24. int Vect__intersect_line_with_poly();
  25. static void destroy_links(struct link_head *, struct Slink *);
  26. static int Vect__divide_and_conquer(struct Slink *, const struct line_pnts *,
  27. struct link_head *, double *, double *,
  28. int);
  29. /*!
  30. \brief Get point inside area and outside all islands.
  31. Take a line and intersect it with the polygon and any islands.
  32. sort the list of X values from these intersections. This will
  33. be a list of segments alternating IN/OUT/IN/OUT of the polygon.
  34. Pick the largest IN segment and take the midpoint.
  35. \param Map vector map
  36. \param area area id
  37. \param[out] X,Y point coordinateds
  38. \return 0 on success
  39. \return -1 on error
  40. */
  41. int
  42. Vect_get_point_in_area(const struct Map_info *Map, int area, double *X, double *Y)
  43. {
  44. static struct line_pnts *Points;
  45. static struct line_pnts **IPoints;
  46. static int first_time = 1;
  47. static int isl_allocated = 0;
  48. int i, n_isles;
  49. G_debug(3, "Vect_get_point_in_area()");
  50. if (first_time) {
  51. Points = Vect_new_line_struct();
  52. IPoints = NULL;
  53. first_time = 0;
  54. }
  55. n_isles = Vect_get_area_num_isles(Map, area);
  56. if (n_isles > isl_allocated) {
  57. IPoints = (struct line_pnts **)
  58. G_realloc(IPoints, (1 + n_isles) * sizeof(struct line_pnts *));
  59. for (i = isl_allocated; i < n_isles; i++)
  60. IPoints[i] = Vect_new_line_struct();
  61. isl_allocated = n_isles;
  62. }
  63. if (0 > Vect_get_area_points(Map, area, Points))
  64. return -1;
  65. for (i = 0; i < n_isles; i++) {
  66. IPoints[i]->alloc_points = 0;
  67. if (0 >
  68. Vect_get_isle_points(Map, Vect_get_area_isle(Map, area, i),
  69. IPoints[i]))
  70. return -1;
  71. }
  72. return (Vect_get_point_in_poly_isl((const struct line_pnts*) Points,
  73. (const struct line_pnts**) IPoints, n_isles, X, Y));
  74. return -1;
  75. }
  76. static int comp_double(double *i, double *j)
  77. {
  78. if (*i < *j)
  79. return -1;
  80. return (*i > *j);
  81. }
  82. static int V__within(double a, double x, double b)
  83. {
  84. if (a < b)
  85. return (x >= a && x <= b);
  86. return (x >= b && x <= a);
  87. }
  88. /*
  89. \brief Intersects line with polygon
  90. For each intersection of a polygon w/ a line, stuff the X value in
  91. the Inter Points array. I used line_pnts, just cuz the memory
  92. management was already there. I am getting real tired of managing
  93. realloc stuff. Assumes that no vertex of polygon lies on Y This is
  94. taken care of by functions calling this function
  95. \param Points line
  96. \param y ?
  97. \param Iter intersection ?
  98. \return 0 on success
  99. \return -1 on error
  100. */
  101. int
  102. Vect__intersect_line_with_poly(const struct line_pnts *Points,
  103. double y, struct line_pnts *Inter)
  104. {
  105. int i;
  106. double a, b, c, d, x;
  107. double perc;
  108. for (i = 1; i < Points->n_points; i++) {
  109. a = Points->y[i - 1];
  110. b = Points->y[i];
  111. c = Points->x[i - 1];
  112. d = Points->x[i];
  113. if (V__within(a, y, b)) {
  114. if (a == b)
  115. continue;
  116. perc = (y - a) / (b - a);
  117. x = perc * (d - c) + c; /* interp X */
  118. if (0 > Vect_append_point(Inter, x, y, 0))
  119. return -1;
  120. }
  121. }
  122. return 0;
  123. }
  124. /*!
  125. \brief Get point inside polygon.
  126. This does NOT consider ISLANDS!
  127. \param Points polygon
  128. \param[out] X,Y point coordinates
  129. \return 0 on success
  130. \return -1 on error
  131. */
  132. int Vect_get_point_in_poly(const struct line_pnts *Points, double *X, double *Y)
  133. {
  134. double cent_x, cent_y;
  135. struct Slink *Head;
  136. static struct link_head *Token;
  137. struct Slink *tmp;
  138. static int first_time = 1;
  139. register int i;
  140. double x_max, x_min;
  141. int ret;
  142. /* get centroid */
  143. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  144. /* is it w/in poly? */
  145. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1) {
  146. *X = cent_x;
  147. *Y = cent_y;
  148. return 0;
  149. }
  150. /* guess we have to do it the hard way... */
  151. G_debug(3, "Vect_get_point_in_poly(): divide and conquer");
  152. /* get min and max x values */
  153. x_max = x_min = Points->x[0];
  154. for (i = 0; i < Points->n_points; i++) {
  155. if (x_min > Points->x[i])
  156. x_min = Points->x[i];
  157. if (x_max < Points->x[i])
  158. x_max = Points->x[i];
  159. }
  160. /* init the linked list */
  161. if (first_time) {
  162. /* will never call link_cleanup () */
  163. link_exit_on_error(1); /* kill program if out of memory */
  164. Token = (struct link_head *)link_init(sizeof(struct Slink));
  165. first_time = 0;
  166. }
  167. Head = (struct Slink *)link_new(Token);
  168. tmp = (struct Slink *)link_new(Token);
  169. Head->next = tmp;
  170. tmp->next = NULL;
  171. Head->x = x_min;
  172. tmp->x = x_max;
  173. *Y = cent_y; /* pick line segment (x_min, cent_y) - (x_max, cent_y) */
  174. ret = Vect__divide_and_conquer(Head, Points, Token, X, Y, 10);
  175. destroy_links(Token, Head);
  176. if (ret < 0) {
  177. G_warning("Vect_get_point_in_poly(): %s",
  178. _("Unable to find point in polygon"));
  179. return -1;
  180. }
  181. G_debug(3, "Found point in %d iterations", 10 - ret);
  182. return 0;
  183. }
  184. /*
  185. \brief Provide a breadth first binary division of real space along line segment.
  186. Looking for a point w/in the polygon.
  187. This routine walks along the list of points on line segment
  188. and divides each pair in half. It sticks that new point right into
  189. the list, and then checks to see if it is inside the poly.
  190. After going through the whole list, it calls itself. The list
  191. now has a whole extra set of points to divide again.
  192. \param Head
  193. \param Points
  194. \param Token
  195. \param X,Y
  196. \param levels
  197. \return # levels it took
  198. \return -1 if exceeded # of levels
  199. */
  200. static int
  201. Vect__divide_and_conquer(struct Slink *Head,
  202. const struct line_pnts *Points,
  203. struct link_head *Token,
  204. double *X, double *Y, int levels)
  205. {
  206. struct Slink *A, *B, *C;
  207. G_debug(3, "Vect__divide_and_conquer(): LEVEL %d", levels);
  208. A = Head;
  209. B = Head->next;
  210. do {
  211. C = (struct Slink *)link_new(Token);
  212. A->next = C;
  213. C->next = B;
  214. C->x = (A->x + B->x) / 2.;
  215. if (Vect_point_in_poly(C->x, *Y, Points) == 1) {
  216. *X = C->x;
  217. return levels;
  218. }
  219. A = B;
  220. B = B->next;
  221. }
  222. while (B != NULL);
  223. /*
  224. ** If it got through the entire loop and still no hits,
  225. ** then lets go a level deeper and divide again.
  226. */
  227. if (levels <= 0)
  228. return -1;
  229. return Vect__divide_and_conquer(Head, Points, Token, X, Y, --levels);
  230. }
  231. static void destroy_links(struct link_head *Token, struct Slink *Head)
  232. {
  233. struct Slink *p, *tmp;
  234. p = Head;
  235. while (p != NULL) {
  236. tmp = p->next;
  237. link_dispose(Token, (VOID_T *) p);
  238. p = tmp;
  239. }
  240. }
  241. /*!
  242. \brief Get centroid of polygon
  243. \param points polygon
  244. \param[out] cent_x,cent_y centroid coordinates
  245. \return 0 on success
  246. \return -1 on error
  247. */
  248. int
  249. Vect_find_poly_centroid(const struct line_pnts *points,
  250. double *cent_x, double *cent_y)
  251. {
  252. int i;
  253. double *xptr1, *yptr1;
  254. double *xptr2, *yptr2;
  255. double cent_weight_x, cent_weight_y;
  256. double len, tot_len;
  257. tot_len = 0.0;
  258. cent_weight_x = 0.0;
  259. cent_weight_y = 0.0;
  260. xptr1 = points->x;
  261. yptr1 = points->y;
  262. xptr2 = points->x + 1;
  263. yptr2 = points->y + 1;
  264. for (i = 1; i < points->n_points; i++) {
  265. len = hypot(*xptr1 - *xptr2, *yptr1 - *yptr2);
  266. cent_weight_x += len * ((*xptr1 + *xptr2) / 2.);
  267. cent_weight_y += len * ((*yptr1 + *yptr2) / 2.);
  268. tot_len += len;
  269. xptr1++;
  270. xptr2++;
  271. yptr1++;
  272. yptr2++;
  273. }
  274. if (tot_len == 0.0)
  275. return -1;
  276. *cent_x = cent_weight_x / tot_len;
  277. *cent_y = cent_weight_y / tot_len;
  278. return 0;
  279. }
  280. /*
  281. ** returns true if point is in any of islands /w in area
  282. ** returns 0 if not
  283. ** returns -1 on error
  284. */
  285. /*
  286. int
  287. Vect_point_in_islands (
  288. struct Map_info *Map,
  289. int area,
  290. double cent_x, double cent_y)
  291. {
  292. struct P_area *Area;
  293. static struct line_pnts *TPoints;
  294. static int first_time = 1;
  295. int isle;
  296. if (first_time == 1)
  297. {
  298. TPoints = Vect_new_line_struct ();
  299. first_time = 0;
  300. }
  301. Area = &(Map->plus.Area[area]);
  302. for (isle = 0; isle < Area->n_isles; isle++)
  303. {
  304. if (0 > Vect_get_isle_points (Map, Area->isles[isle], TPoints))
  305. return -1;
  306. if ( Vect_point_in_poly (cent_x, cent_y, TPoints) == 1 )
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. */
  312. /*!
  313. \brief Get point inside polygon but outside the islands specifiled in IPoints.
  314. Take a line and intersect it with the polygon and any islands.
  315. sort the list of X values from these intersections. This will be a
  316. list of segments alternating IN/OUT/IN/OUT of the polygon. Pick the
  317. largest IN segment and take the midpoint.
  318. \param Points polygon (boundary)
  319. \param IPoints isles (list of isle boundaries)
  320. \param n_isles number of isles
  321. \param[out] att_x,att_y point coordinates
  322. \return 0 on success
  323. \return -1 on error
  324. */
  325. int Vect_get_point_in_poly_isl(const struct line_pnts *Points,
  326. const struct line_pnts **IPoints, int n_isles,
  327. double *att_x, double *att_y)
  328. {
  329. static struct line_pnts *Intersects;
  330. static int first_time = 1;
  331. double cent_x, cent_y;
  332. register int i, j;
  333. double max, hi_y, lo_y;
  334. int maxpos;
  335. int point_in_sles = 0;
  336. double diff;
  337. G_debug(3, "Vect_get_point_in_poly_isl(): n_isles = %d", n_isles);
  338. if (first_time) {
  339. Intersects = Vect_new_line_struct();
  340. first_time = 0;
  341. }
  342. if (Points->n_points < 3) { /* test */
  343. if (Points->n_points > 0) {
  344. *att_x = Points->x[0];
  345. *att_y = Points->y[0];
  346. return 0;
  347. }
  348. return -1;
  349. }
  350. /* get centroid */
  351. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  352. /* is it w/in poly? */
  353. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1) {
  354. /* if the point is inside the polygon */
  355. for (i = 0; i < n_isles; i++) {
  356. if (Vect_point_in_poly(cent_x, cent_y, IPoints[i]) >= 1) {
  357. point_in_sles = 1;
  358. break;
  359. }
  360. }
  361. if (!point_in_sles) {
  362. *att_x = cent_x;
  363. *att_y = cent_y;
  364. return 0;
  365. }
  366. }
  367. /* guess we have to do it the hard way... */
  368. /* first find att_y close to cent_y so that no points lie on the line */
  369. /* find the point closest to line from below, and point close to line
  370. from above and take average of their y-coordinates */
  371. /* first initializing lo_y,hi_y to be any 2 pnts on either side of cent_y */
  372. hi_y = cent_y - 1;
  373. lo_y = cent_y + 1;
  374. for (i = 0; i < Points->n_points; i++) {
  375. if ((lo_y < cent_y) && (hi_y >= cent_y))
  376. break; /* already initialized */
  377. if (Points->y[i] < cent_y)
  378. lo_y = Points->y[i];
  379. if (Points->y[i] >= cent_y)
  380. hi_y = Points->y[i];
  381. }
  382. /* first going through boundary points */
  383. for (i = 0; i < Points->n_points; i++) {
  384. if ((Points->y[i] < cent_y) &&
  385. ((cent_y - Points->y[i]) < (cent_y - lo_y)))
  386. lo_y = Points->y[i];
  387. if ((Points->y[i] >= cent_y) &&
  388. ((Points->y[i] - cent_y) < (hi_y - cent_y)))
  389. hi_y = Points->y[i];
  390. }
  391. for (i = 0; i < n_isles; i++)
  392. for (j = 0; j < IPoints[i]->n_points; j++) {
  393. if ((IPoints[i]->y[j] < cent_y) &&
  394. ((cent_y - IPoints[i]->y[j]) < (cent_y - lo_y)))
  395. lo_y = IPoints[i]->y[j];
  396. if ((IPoints[i]->y[j] >= cent_y) &&
  397. ((IPoints[i]->y[j] - cent_y) < (hi_y - cent_y)))
  398. hi_y = IPoints[i]->y[j];
  399. }
  400. if (lo_y == hi_y)
  401. return (-1); /* area is empty */
  402. else
  403. *att_y = (hi_y + lo_y) / 2.0;
  404. Intersects->n_points = 0;
  405. Vect__intersect_line_with_poly(Points, *att_y, Intersects);
  406. /* add in intersections w/ holes */
  407. for (i = 0; i < n_isles; i++) {
  408. if (0 >
  409. Vect__intersect_line_with_poly(IPoints[i], *att_y, Intersects))
  410. return -1;
  411. }
  412. if (Intersects->n_points < 2) /* test */
  413. return -1;
  414. qsort(Intersects->x, (size_t) Intersects->n_points, sizeof(double),
  415. (void *)comp_double);
  416. max = 0;
  417. maxpos = 0;
  418. /* find area of MAX distance */
  419. for (i = 0; i < Intersects->n_points; i += 2) {
  420. diff = Intersects->x[i + 1] - Intersects->x[i];
  421. if (diff > max) {
  422. max = diff;
  423. maxpos = i;
  424. }
  425. }
  426. if (max == 0.0) /* area was empty: example ((x1,y1), (x2,y2), (x1,y1)) */
  427. return -1;
  428. *att_x = (Intersects->x[maxpos] + Intersects->x[maxpos + 1]) / 2.;
  429. return 0;
  430. }
  431. /* Intersect segments of Points with ray from point X,Y to the right.
  432. * Returns: -1 point exactly on segment
  433. * number of intersections
  434. */
  435. static int segments_x_ray(double X, double Y, const struct line_pnts *Points)
  436. {
  437. double x1, x2, y1, y2;
  438. double x_inter;
  439. int n_intersects;
  440. int n;
  441. G_debug(3, "segments_x_ray(): x = %f y = %f n_points = %d", X, Y,
  442. Points->n_points);
  443. /* Follow the ray from X,Y along positive x and find number of intersections.
  444. * Coordinates exactly on ray are considered to be slightly above. */
  445. n_intersects = 0;
  446. for (n = 1; n < Points->n_points; n++) {
  447. x1 = Points->x[n - 1];
  448. y1 = Points->y[n - 1];
  449. x2 = Points->x[n];
  450. y2 = Points->y[n];
  451. /* G_debug() is slow, avoid it in loops over points,
  452. * activate when needed */
  453. /*
  454. G_debug(3, "X = %f Y = %f x1 = %f y1 = %f x2 = %f y2 = %f", X, Y, x1,
  455. y1, x2, y2);
  456. */
  457. /* I know, it should be possible to do that with less conditions,
  458. * but it should be enough readable also! */
  459. /* first, skip segments that obviously do not intersect with test ray */
  460. /* segment above (X is not important) */
  461. if (y1 > Y && y2 > Y)
  462. continue;
  463. /* segment below (X is not important) */
  464. if (y1 < Y && y2 < Y)
  465. continue;
  466. /* segment left from X -> no intersection */
  467. if (x1 < X && x2 < X)
  468. continue;
  469. /* point on vertex */
  470. if ((x1 == X && y1 == Y) || (x2 == X && y2 == Y))
  471. return -1;
  472. /* on vertical boundary */
  473. if (x1 == x2 && x1 == X) {
  474. if ((y1 <= Y && y2 >= Y) || (y1 >= Y && y2 <= Y))
  475. return -1;
  476. }
  477. /* on horizontal boundary */
  478. if (y1 == y2 && y1 == Y) {
  479. if ((x1 <= X && x2 >= X) || (x1 >= X && x2 <= X))
  480. return -1;
  481. else
  482. continue; /* segment on ray (X is not important) */
  483. }
  484. /* segment on ray (X is not important) */
  485. /* if (y1 == Y && y2 == Y)
  486. continue; */
  487. /* one end on Y second above (X is not important) */
  488. if ((y1 == Y && y2 > Y) || (y2 == Y && y1 > Y))
  489. continue;
  490. /* For following cases we know that at least one of x1 and x2 is >= X */
  491. /* one end of segment on Y second below Y */
  492. if (y1 == Y && y2 < Y) {
  493. if (x1 >= X) /* x of the end on the ray is >= X */
  494. n_intersects++;
  495. continue;
  496. }
  497. if (y2 == Y && y1 < Y) {
  498. if (x2 >= X)
  499. n_intersects++;
  500. continue;
  501. }
  502. /* one end of segment above Y second below Y */
  503. if ((y1 < Y && y2 > Y) || (y1 > Y && y2 < Y)) {
  504. if (x1 >= X && x2 >= X) {
  505. n_intersects++;
  506. continue;
  507. }
  508. /* now either x1 < X && x2 > X or x1 > X && x2 < X -> calculate intersection */
  509. x_inter = dig_x_intersect(x1, x2, y1, y2, Y);
  510. G_debug(3, "x_inter = %f", x_inter);
  511. if (x_inter == X)
  512. return -1; /* point on segment, do not assume inside/outside */
  513. else if (x_inter > X)
  514. n_intersects++;
  515. continue; /* would not be necessary, just to check, see below */
  516. }
  517. /* should not be reached (one condition is not necessary, but it is maybe better readable
  518. * and it is a check) */
  519. G_warning
  520. ("segments_x_ray() %s: X = %f Y = %f x1 = %f y1 = %f x2 = %f y2 = %f",
  521. _("conditions failed"), X, Y, x1, y1, x2, y2);
  522. }
  523. return n_intersects;
  524. }
  525. /*!
  526. \brief Determines if a point (X,Y) is inside a polygon.
  527. \param X,Y point coordinates
  528. \param Points polygon
  529. \return 0 - outside
  530. \return 1 - inside
  531. \return 2 - on the boundary
  532. */
  533. int Vect_point_in_poly(double X, double Y, const struct line_pnts *Points)
  534. {
  535. int n_intersects;
  536. G_debug(3, "Vect_point_in_poly(): x = %f y = %f n_points = %d", X, Y,
  537. Points->n_points);
  538. n_intersects = segments_x_ray(X, Y, Points);
  539. if (n_intersects == -1)
  540. return 2;
  541. /* odd number of intersections: inside, return 1
  542. * even number of intersections: outside, return 0 */
  543. return (n_intersects & 1);
  544. }
  545. /*!
  546. \brief Determines if a point (X,Y) is inside an area outer ring. Islands are not considered.
  547. \param X,Y point coordinates
  548. \param Map vector map
  549. \param area area id
  550. \param box area bounding box
  551. \return 0 - outside
  552. \return 1 - inside
  553. \return 2 - on the boundary
  554. */
  555. int
  556. Vect_point_in_area_outer_ring(double X, double Y, const struct Map_info *Map,
  557. int area, struct bound_box *box)
  558. {
  559. static int first = 1;
  560. int n_intersects, inter;
  561. int i, line;
  562. static struct line_pnts *Points;
  563. const struct Plus_head *Plus;
  564. struct P_area *Area;
  565. /* keep in sync with Vect_point_in_island() */
  566. G_debug(3, "Vect_point_in_area_outer_ring(): x = %f y = %f area = %d",
  567. X, Y, area);
  568. if (first == 1) {
  569. Points = Vect_new_line_struct();
  570. first = 0;
  571. }
  572. Plus = &(Map->plus);
  573. Area = Plus->Area[area];
  574. /* First it must be in box */
  575. if (X < box->W || X > box->E || Y > box->N || Y < box->S)
  576. return 0;
  577. n_intersects = 0;
  578. for (i = 0; i < Area->n_lines; i++) {
  579. line = abs(Area->lines[i]);
  580. Vect_read_line(Map, Points, NULL, line);
  581. /* if the bbox of the line would be available,
  582. * the bbox could be used for a first check: */
  583. /* Vect_line_box(Points, &lbox);
  584. * do not check lines that obviously do not intersect with test ray */
  585. /* if ((lbox.N < Y) || (lbox.S > Y) || (lbox.E < X))
  586. continue;
  587. */
  588. /* retrieving the bbox from the spatial index or
  589. * calculating the box from the vertices is slower than
  590. * just feeding the line to segments_x_ray() */
  591. inter = segments_x_ray(X, Y, Points);
  592. if (inter == -1)
  593. return 2;
  594. n_intersects += inter;
  595. }
  596. /* odd number of intersections: inside, return 1
  597. * even number of intersections: outside, return 0 */
  598. return (n_intersects & 1);
  599. }
  600. /*!
  601. \brief Determines if a point (X,Y) is inside an island.
  602. \param X,Y point coordinates
  603. \param Map vector map
  604. \param isle isle id
  605. \param box isle bounding box
  606. \return 0 - outside
  607. \return 1 - inside
  608. \return 2 - on the boundary
  609. */
  610. int Vect_point_in_island(double X, double Y, const struct Map_info *Map,
  611. int isle, struct bound_box *box)
  612. {
  613. static int first = 1;
  614. int n_intersects, inter;
  615. int i, line;
  616. static struct line_pnts *Points;
  617. const struct Plus_head *Plus;
  618. struct P_isle *Isle;
  619. /* keep in sync with Vect_point_in_area_outer_ring() */
  620. G_debug(3, "Vect_point_in_island(): x = %f y = %f isle = %d",
  621. X, Y, isle);
  622. if (first == 1) {
  623. Points = Vect_new_line_struct();
  624. first = 0;
  625. }
  626. Plus = &(Map->plus);
  627. Isle = Plus->Isle[isle];
  628. /* First it must be in box */
  629. if (X < box->W || X > box->E || Y > box->N || Y < box->S)
  630. return 0;
  631. n_intersects = 0;
  632. for (i = 0; i < Isle->n_lines; i++) {
  633. line = abs(Isle->lines[i]);
  634. Vect_read_line(Map, Points, NULL, line);
  635. /* if the bbox of the line would be available,
  636. * the bbox could be used for a first check: */
  637. /* Vect_line_box(Points, &lbox);
  638. * dont check lines that obviously do not intersect with test ray */
  639. /* if ((lbox.N < Y) || (lbox.S > Y) || (lbox.E < X))
  640. continue;
  641. */
  642. /* retrieving the bbox from the spatial index or
  643. * calculating the box from the vertices is slower than
  644. * just feeding the line to segments_x_ray() */
  645. inter = segments_x_ray(X, Y, Points);
  646. if (inter == -1)
  647. return 2;
  648. n_intersects += inter;
  649. }
  650. /* odd number of intersections: inside, return 1
  651. * even number of intersections: outside, return 0 */
  652. return (n_intersects & 1);
  653. }