poly.c 19 KB

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