poly.c 19 KB

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