poly.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  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. double tmp;
  90. if (a > b) {
  91. tmp = a;
  92. a = b;
  93. b = tmp;
  94. }
  95. return (x >= a && x <= b);
  96. }
  97. /*
  98. \brief Intersects line with polygon
  99. For each intersection of a polygon w/ a line, stuff the X value in
  100. the Inter Points array. I used line_pnts, just cuz the memory
  101. management was already there. I am getting real tired of managing
  102. realloc stuff. Assumes that no vertex of polygon lies on Y This is
  103. taken care of by functions calling this function
  104. \param Points line
  105. \param y ?
  106. \param Iter intersection ?
  107. \return 0 on success
  108. \return -1 on error
  109. */
  110. int
  111. Vect__intersect_line_with_poly(struct line_pnts *Points,
  112. double y, struct line_pnts *Inter)
  113. {
  114. int i;
  115. double a, b, c, d, x;
  116. double perc;
  117. for (i = 1; i < Points->n_points; i++) {
  118. a = Points->y[i - 1];
  119. b = Points->y[i];
  120. c = Points->x[i - 1];
  121. d = Points->x[i];
  122. if (V__within(a, y, b)) {
  123. if (a == b)
  124. continue;
  125. perc = (y - a) / (b - a);
  126. x = perc * (d - c) + c; /* interp X */
  127. if (0 > Vect_append_point(Inter, x, y, 0))
  128. return -1;
  129. }
  130. }
  131. return 0;
  132. }
  133. /*!
  134. \brief Get point inside polygon.
  135. This does NOT consider ISLANDS!
  136. \param Points polygon
  137. \param[out] X,Y point coordinates
  138. \return 0 on success
  139. \return -1 on error
  140. */
  141. int Vect_get_point_in_poly(struct line_pnts *Points, double *X, double *Y)
  142. {
  143. double cent_x, cent_y;
  144. struct Slink *Head;
  145. static struct link_head *Token;
  146. struct Slink *tmp;
  147. static int first_time = 1;
  148. register int i;
  149. double x_max, x_min;
  150. int ret;
  151. /* get centroid */
  152. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  153. /* is it w/in poly? */
  154. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1) {
  155. *X = cent_x;
  156. *Y = cent_y;
  157. return 0;
  158. }
  159. /* guess we have to do it the hard way... */
  160. /* get min and max x values */
  161. x_max = x_min = Points->x[0];
  162. for (i = 0; i < Points->n_points; i++) {
  163. if (x_min > Points->x[i])
  164. x_min = Points->x[i];
  165. if (x_max < Points->x[i])
  166. x_max = Points->x[i];
  167. }
  168. /* init the linked list */
  169. if (first_time) {
  170. /* will never call link_cleanup () */
  171. link_exit_on_error(1); /* kill program if out of memory */
  172. Token = (struct link_head *)link_init(sizeof(struct Slink));
  173. first_time = 0;
  174. }
  175. Head = (struct Slink *)link_new(Token);
  176. tmp = (struct Slink *)link_new(Token);
  177. Head->next = tmp;
  178. tmp->next = NULL;
  179. Head->x = x_min;
  180. tmp->x = x_max;
  181. *Y = cent_y; /* pick line segment (x_min, cent_y) - (x_max, cent_y) */
  182. ret = Vect__divide_and_conquer(Head, Points, Token, X, Y, 10);
  183. destroy_links(Head);
  184. if (ret < 0) {
  185. G_warning("Vect_get_point_in_poly(): %s",
  186. _("Unable to find point in polygon"));
  187. return -1;
  188. }
  189. G_debug(3, "Found point in %d iterations", 10 - ret);
  190. return 0;
  191. }
  192. /*
  193. \brief Provide a breadth first binary division of real space along line segment.
  194. Looking for a point w/in the polygon.
  195. This routine walks along the list of points on line segment
  196. and divides each pair in half. It sticks that new point right into
  197. the list, and then checks to see if it is inside the poly.
  198. After going through the whole list, it calls itself. The list
  199. now has a whole extra set of points to divide again.
  200. \param Head
  201. \param Points
  202. \param Token
  203. \param X,Y
  204. \param levels
  205. \return # levels it took
  206. \return -1 if exceeded # of levels
  207. */
  208. static int
  209. Vect__divide_and_conquer(struct Slink *Head,
  210. struct line_pnts *Points,
  211. struct link_head *Token,
  212. double *X, double *Y, int levels)
  213. {
  214. struct Slink *A, *B, *C;
  215. G_debug(3, "Vect__divide_and_conquer(): LEVEL %d", levels);
  216. A = Head;
  217. B = Head->next;
  218. do {
  219. C = (struct Slink *)link_new(Token);
  220. A->next = C;
  221. C->next = B;
  222. C->x = (A->x + B->x) / 2.;
  223. if (Vect_point_in_poly(C->x, *Y, Points) == 1) {
  224. *X = C->x;
  225. return levels;
  226. }
  227. A = B;
  228. B = B->next;
  229. }
  230. while (B != NULL);
  231. /*
  232. ** If it got through the entire loop and still no hits,
  233. ** then lets go a level deeper and divide again.
  234. */
  235. if (levels <= 0)
  236. return -1;
  237. return Vect__divide_and_conquer(Head, Points, Token, X, Y, --levels);
  238. }
  239. static void destroy_links(struct Slink *Head)
  240. {
  241. struct Slink *p, *tmp;
  242. p = Head;
  243. while (p != NULL) {
  244. tmp = p->next;
  245. link_dispose((struct link_head *)Head, (VOID_T *) p);
  246. p = tmp;
  247. }
  248. }
  249. /*!
  250. \brief Get centroid of polygon
  251. \param points polygon
  252. \param[out] cent_x,cent_y centroid coordinates
  253. \return 0 on success
  254. \return -1 on error
  255. */
  256. int
  257. Vect_find_poly_centroid(struct line_pnts *points,
  258. double *cent_x, double *cent_y)
  259. {
  260. int i;
  261. double *xptr1, *yptr1;
  262. double *xptr2, *yptr2;
  263. double cent_weight_x, cent_weight_y;
  264. double len, tot_len;
  265. tot_len = 0.0;
  266. cent_weight_x = 0.0;
  267. cent_weight_y = 0.0;
  268. xptr1 = points->x;
  269. yptr1 = points->y;
  270. xptr2 = points->x + 1;
  271. yptr2 = points->y + 1;
  272. for (i = 1; i < points->n_points; i++) {
  273. len = hypot(*xptr1 - *xptr2, *yptr1 - *yptr2);
  274. cent_weight_x += len * ((*xptr1 + *xptr2) / 2.);
  275. cent_weight_y += len * ((*yptr1 + *yptr2) / 2.);
  276. tot_len += len;
  277. xptr1++;
  278. xptr2++;
  279. yptr1++;
  280. yptr2++;
  281. }
  282. if (tot_len == 0.0)
  283. return -1;
  284. *cent_x = cent_weight_x / tot_len;
  285. *cent_y = cent_weight_y / tot_len;
  286. return 0;
  287. }
  288. /*
  289. ** returns true if point is in any of islands /w in area
  290. ** returns 0 if not
  291. ** returns -1 on error
  292. */
  293. /*
  294. int
  295. Vect_point_in_islands (
  296. struct Map_info *Map,
  297. int area,
  298. double cent_x, double cent_y)
  299. {
  300. P_AREA *Area;
  301. static struct line_pnts *TPoints;
  302. static int first_time = 1;
  303. int isle;
  304. if (first_time == 1)
  305. {
  306. TPoints = Vect_new_line_struct ();
  307. first_time = 0;
  308. }
  309. Area = &(Map->plus.Area[area]);
  310. for (isle = 0; isle < Area->n_isles; isle++)
  311. {
  312. if (0 > Vect_get_isle_points (Map, Area->isles[isle], TPoints))
  313. return -1;
  314. if ( Vect_point_in_poly (cent_x, cent_y, TPoints) == 1 )
  315. return 1;
  316. }
  317. return 0;
  318. }
  319. */
  320. /*!
  321. \brief Get point inside polygon but outside the islands specifiled in IPoints.
  322. Take a line and intersect it with the polygon and any islands.
  323. sort the list of X values from these intersections. This will
  324. be a list of segments alternating IN/OUT/IN/OUt of the polygon.
  325. Pick the largest IN segment and take the midpoint.
  326. \param Points polygon
  327. \param IPoints isles
  328. \param n_isles number of isles
  329. \param[out] att_x,att_y point coordinates
  330. \return 0 on success
  331. \return -1 on error
  332. */
  333. int
  334. Vect_get_point_in_poly_isl(struct line_pnts *Points,
  335. struct line_pnts **IPoints, int n_isles,
  336. double *att_x, double *att_y)
  337. {
  338. static struct line_pnts *Intersects;
  339. static int first_time = 1;
  340. double cent_x, cent_y;
  341. register int i, j;
  342. double max, hi_y, lo_y;
  343. int maxpos;
  344. int point_in_sles = 0;
  345. double diff;
  346. G_debug(3, "Vect_get_point_in_poly_isl(): n_isles = %d", n_isles);
  347. if (first_time) {
  348. Intersects = Vect_new_line_struct();
  349. first_time = 0;
  350. }
  351. if (Points->n_points < 3) { /* test */
  352. if (Points->n_points > 0) {
  353. *att_x = Points->x[0];
  354. *att_y = Points->y[0];
  355. return 0;
  356. }
  357. return -1;
  358. }
  359. /* get centroid */
  360. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  361. /* is it w/in poly? */
  362. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1)
  363. /* if the point is iside the polygon */
  364. {
  365. for (i = 0; i < n_isles; i++) {
  366. if (Vect_point_in_poly(cent_x, cent_y, IPoints[i]) >= 1) {
  367. point_in_sles = 1;
  368. break;
  369. }
  370. }
  371. if (!point_in_sles) {
  372. *att_x = cent_x;
  373. *att_y = cent_y;
  374. return 0;
  375. }
  376. }
  377. /* guess we have to do it the hard way... */
  378. /* first find att_y close to cent_y so that no points lie on the line */
  379. /* find the point closest to line from below, and point close to line
  380. from above and take average of their y-coordinates */
  381. /* first initializing lo_y,hi_y to be any 2 pnts on either side of cent_y */
  382. hi_y = cent_y - 1;
  383. lo_y = cent_y + 1;
  384. for (i = 0; i < Points->n_points; i++) {
  385. if ((lo_y < cent_y) && (hi_y >= cent_y))
  386. break; /* already initialized */
  387. if (Points->y[i] < cent_y)
  388. lo_y = Points->y[i];
  389. if (Points->y[i] >= cent_y)
  390. hi_y = Points->y[i];
  391. }
  392. /* first going throught boundary points */
  393. for (i = 0; i < Points->n_points; i++) {
  394. if ((Points->y[i] < cent_y) &&
  395. ((cent_y - Points->y[i]) < (cent_y - lo_y)))
  396. lo_y = Points->y[i];
  397. if ((Points->y[i] >= cent_y) &&
  398. ((Points->y[i] - cent_y) < (hi_y - cent_y)))
  399. hi_y = Points->y[i];
  400. }
  401. for (i = 0; i < n_isles; i++)
  402. for (j = 0; j < IPoints[i]->n_points; j++) {
  403. if ((IPoints[i]->y[j] < cent_y) &&
  404. ((cent_y - IPoints[i]->y[j]) < (cent_y - lo_y)))
  405. lo_y = IPoints[i]->y[j];
  406. if ((IPoints[i]->y[j] >= cent_y) &&
  407. ((IPoints[i]->y[j] - cent_y) < (hi_y - cent_y)))
  408. hi_y = IPoints[i]->y[j];
  409. }
  410. if (lo_y == hi_y)
  411. return (-1); /* area is empty */
  412. else
  413. *att_y = (hi_y + lo_y) / 2.0;
  414. Intersects->n_points = 0;
  415. Vect__intersect_line_with_poly(Points, *att_y, Intersects);
  416. /* add in intersections w/ holes */
  417. for (i = 0; i < n_isles; i++) {
  418. if (0 >
  419. Vect__intersect_line_with_poly(IPoints[i], *att_y, Intersects))
  420. return -1;
  421. }
  422. if (Intersects->n_points < 2) /* test */
  423. return -1;
  424. qsort(Intersects->x, (size_t) Intersects->n_points, sizeof(double),
  425. (void *)comp_double);
  426. max = 0;
  427. maxpos = 0;
  428. /* find area of MAX distance */
  429. for (i = 0; i < Intersects->n_points; i += 2) {
  430. diff = Intersects->x[i + 1] - Intersects->x[i];
  431. if (diff > max) {
  432. max = diff;
  433. maxpos = i;
  434. }
  435. }
  436. if (max == 0.0) /* area was empty: example ((x1,y1), (x2,y2), (x1,y1)) */
  437. return -1;
  438. *att_x = (Intersects->x[maxpos] + Intersects->x[maxpos + 1]) / 2.;
  439. return 0;
  440. }
  441. /* Intersect segments of Points with ray from point X,Y to the right.
  442. * Returns: -1 point exactly on segment
  443. * number of intersections
  444. */
  445. static int segments_x_ray(double X, double Y, struct line_pnts *Points)
  446. {
  447. double x1, x2, y1, y2;
  448. double x_inter;
  449. int n_intersects;
  450. int n;
  451. G_debug(3, "segments_x_ray(): x = %f y = %f n_points = %d", X, Y,
  452. Points->n_points);
  453. /* Follow the ray from X,Y along positive x and find number of intersections.
  454. * Coordinates exactly on ray are considered to be slightly above. */
  455. n_intersects = 0;
  456. for (n = 0; n < Points->n_points - 1; n++) {
  457. x1 = Points->x[n];
  458. y1 = Points->y[n];
  459. x2 = Points->x[n + 1];
  460. y2 = Points->y[n + 1];
  461. G_debug(3, "X = %f Y = %f x1 = %f y1 = %f x2 = %f y2 = %f", X, Y, x1,
  462. y1, x2, y2);
  463. /* I know, it should be possible to do that with less conditions, but it should be
  464. * enough readable also! */
  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. ((y1 <= Y && y2 >= Y) || (y1 >= Y && y2 <= Y)))
  474. return -1;
  475. /* on horizontal boundary */
  476. if ((y1 == y2 && y1 == Y) &&
  477. ((x1 <= X && x2 >= X) || (x1 >= X && x2 <= X)))
  478. return -1;
  479. /* segment on ray (X is not important) */
  480. if (y1 == Y && y2 == Y)
  481. continue;
  482. /* segment above (X is not important) */
  483. if (y1 > Y && y2 > Y)
  484. continue;
  485. /* segment below (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;
  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 may be 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, 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, 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. 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, 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. 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. }