poly.c 20 KB

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