poly.c 19 KB

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