poly.c 19 KB

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