poly.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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_y_line_with_poly();
  25. int Vect__intersect_x_line_with_poly();
  26. static void destroy_links(struct link_head *, 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. return (*i > *j);
  82. }
  83. static int V__within(double a, double x, double b)
  84. {
  85. if (a < b)
  86. return (x >= a && x < b);
  87. return (x > b && x <= a);
  88. }
  89. /*
  90. \brief Intersects line with polygon
  91. For each intersection of a polygon w/ a line, stuff the X value in
  92. the Inter Points array. I used line_pnts, just cuz the memory
  93. management was already there. I am getting real tired of managing
  94. realloc stuff.
  95. \param Points line
  96. \param y y coordinate of horizontal line
  97. \param Inter intersections of horizontal line with points line
  98. \return 0 on success
  99. \return -1 on error
  100. */
  101. int
  102. Vect__intersect_y_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 Intersects line with polygon
  126. For each intersection of a polygon w/ a line, stuff the Y value in
  127. the Inter Points array. I used line_pnts, just cuz the memory
  128. management was already there. I am getting real tired of managing
  129. realloc stuff.
  130. \param Points line
  131. \param x x coordinate of vertical line
  132. \param Inter intersections of horizontal line with points line
  133. \return 0 on success
  134. \return -1 on error
  135. */
  136. int
  137. Vect__intersect_x_line_with_poly(const struct line_pnts *Points,
  138. double x, struct line_pnts *Inter)
  139. {
  140. int i;
  141. double a, b, c, d, y;
  142. double perc;
  143. for (i = 1; i < Points->n_points; i++) {
  144. a = Points->x[i - 1];
  145. b = Points->x[i];
  146. c = Points->y[i - 1];
  147. d = Points->y[i];
  148. if (V__within(a, x, b)) {
  149. if (a == b)
  150. continue;
  151. perc = (x - a) / (b - a);
  152. y = perc * (d - c) + c; /* interp Y */
  153. if (0 > Vect_append_point(Inter, x, y, 0))
  154. return -1;
  155. }
  156. }
  157. return 0;
  158. }
  159. /*!
  160. \brief Get point inside polygon.
  161. This does NOT consider ISLANDS!
  162. \param Points polygon
  163. \param[out] X,Y point coordinates
  164. \return 0 on success
  165. \return -1 on error
  166. */
  167. int Vect_get_point_in_poly(const struct line_pnts *Points, double *X, double *Y)
  168. {
  169. double cent_x, cent_y;
  170. struct Slink *Head;
  171. static struct link_head *Token;
  172. struct Slink *tmp;
  173. static int first_time = 1;
  174. register int i;
  175. double x_max, x_min;
  176. int ret;
  177. /* get centroid */
  178. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  179. /* is it w/in poly? */
  180. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1) {
  181. *X = cent_x;
  182. *Y = cent_y;
  183. return 0;
  184. }
  185. /* guess we have to do it the hard way... */
  186. G_debug(3, "Vect_get_point_in_poly(): divide and conquer");
  187. /* get min and max x values */
  188. x_max = x_min = Points->x[0];
  189. for (i = 0; i < Points->n_points; i++) {
  190. if (x_min > Points->x[i])
  191. x_min = Points->x[i];
  192. if (x_max < Points->x[i])
  193. x_max = Points->x[i];
  194. }
  195. /* init the linked list */
  196. if (first_time) {
  197. /* will never call link_cleanup () */
  198. link_exit_on_error(1); /* kill program if out of memory */
  199. Token = (struct link_head *)link_init(sizeof(struct Slink));
  200. first_time = 0;
  201. }
  202. Head = (struct Slink *)link_new(Token);
  203. tmp = (struct Slink *)link_new(Token);
  204. Head->next = tmp;
  205. tmp->next = NULL;
  206. Head->x = x_min;
  207. tmp->x = x_max;
  208. *Y = cent_y; /* pick line segment (x_min, cent_y) - (x_max, cent_y) */
  209. ret = Vect__divide_and_conquer(Head, Points, Token, X, Y, 10);
  210. destroy_links(Token, Head);
  211. if (ret < 0) {
  212. G_warning("Vect_get_point_in_poly(): %s",
  213. _("Unable to find point in polygon"));
  214. return -1;
  215. }
  216. G_debug(3, "Found point in %d iterations", 10 - ret);
  217. return 0;
  218. }
  219. /*
  220. \brief Provide a breadth first binary division of real space along line segment.
  221. Looking for a point w/in the polygon.
  222. This routine walks along the list of points on line segment
  223. and divides each pair in half. It sticks that new point right into
  224. the list, and then checks to see if it is inside the poly.
  225. After going through the whole list, it calls itself. The list
  226. now has a whole extra set of points to divide again.
  227. \param Head
  228. \param Points
  229. \param Token
  230. \param X,Y
  231. \param levels
  232. \return # levels it took
  233. \return -1 if exceeded # of levels
  234. */
  235. static int
  236. Vect__divide_and_conquer(struct Slink *Head,
  237. const struct line_pnts *Points,
  238. struct link_head *Token,
  239. double *X, double *Y, int levels)
  240. {
  241. struct Slink *A, *B, *C;
  242. G_debug(3, "Vect__divide_and_conquer(): LEVEL %d", levels);
  243. A = Head;
  244. B = Head->next;
  245. do {
  246. C = (struct Slink *)link_new(Token);
  247. A->next = C;
  248. C->next = B;
  249. C->x = (A->x + B->x) / 2.;
  250. if (Vect_point_in_poly(C->x, *Y, Points) == 1) {
  251. *X = C->x;
  252. return levels;
  253. }
  254. A = B;
  255. B = B->next;
  256. }
  257. while (B != NULL);
  258. /*
  259. ** If it got through the entire loop and still no hits,
  260. ** then lets go a level deeper and divide again.
  261. */
  262. if (levels <= 0)
  263. return -1;
  264. return Vect__divide_and_conquer(Head, Points, Token, X, Y, --levels);
  265. }
  266. static void destroy_links(struct link_head *Token, struct Slink *Head)
  267. {
  268. struct Slink *p, *tmp;
  269. p = Head;
  270. while (p != NULL) {
  271. tmp = p->next;
  272. link_dispose(Token, (VOID_T *) p);
  273. p = tmp;
  274. }
  275. }
  276. /*!
  277. \brief Get centroid of polygon
  278. \param points polygon
  279. \param[out] cent_x,cent_y centroid coordinates
  280. \return 0 on success
  281. \return -1 on error
  282. */
  283. int
  284. Vect_find_poly_centroid(const struct line_pnts *points,
  285. double *cent_x, double *cent_y)
  286. {
  287. int i;
  288. double *xptr1, *yptr1;
  289. double *xptr2, *yptr2;
  290. double cent_weight_x, cent_weight_y;
  291. double len, tot_len;
  292. tot_len = 0.0;
  293. cent_weight_x = 0.0;
  294. cent_weight_y = 0.0;
  295. xptr1 = points->x;
  296. yptr1 = points->y;
  297. xptr2 = points->x + 1;
  298. yptr2 = points->y + 1;
  299. /* center of gravity of the polygon line, not area */
  300. for (i = 1; i < points->n_points; i++) {
  301. len = hypot(*xptr1 - *xptr2, *yptr1 - *yptr2);
  302. cent_weight_x += len * ((*xptr1 + *xptr2) / 2.);
  303. cent_weight_y += len * ((*yptr1 + *yptr2) / 2.);
  304. tot_len += len;
  305. xptr1++;
  306. xptr2++;
  307. yptr1++;
  308. yptr2++;
  309. }
  310. if (tot_len == 0.0)
  311. return -1;
  312. *cent_x = cent_weight_x / tot_len;
  313. *cent_y = cent_weight_y / tot_len;
  314. return 0;
  315. }
  316. /*
  317. ** returns true if point is in any of islands /w in area
  318. ** returns 0 if not
  319. ** returns -1 on error
  320. */
  321. /*
  322. int
  323. Vect_point_in_islands (
  324. struct Map_info *Map,
  325. int area,
  326. double cent_x, double cent_y)
  327. {
  328. struct P_area *Area;
  329. static struct line_pnts *TPoints;
  330. static int first_time = 1;
  331. int isle;
  332. if (first_time == 1)
  333. {
  334. TPoints = Vect_new_line_struct ();
  335. first_time = 0;
  336. }
  337. Area = &(Map->plus.Area[area]);
  338. for (isle = 0; isle < Area->n_isles; isle++)
  339. {
  340. if (0 > Vect_get_isle_points (Map, Area->isles[isle], TPoints))
  341. return -1;
  342. if ( Vect_point_in_poly (cent_x, cent_y, TPoints) == 1 )
  343. return 1;
  344. }
  345. return 0;
  346. }
  347. */
  348. /*!
  349. \brief Get point inside polygon but outside the islands specifiled in IPoints.
  350. Take a line and intersect it with the polygon and any islands.
  351. sort the list of X values from these intersections. This will be a
  352. list of segments alternating IN/OUT/IN/OUT of the polygon. Pick the
  353. largest IN segment and take the midpoint.
  354. \param Points polygon (boundary)
  355. \param IPoints isles (list of isle boundaries)
  356. \param n_isles number of isles
  357. \param[out] att_x,att_y point coordinates
  358. \return 0 on success
  359. \return -1 on error
  360. */
  361. int Vect_get_point_in_poly_isl(const struct line_pnts *Points,
  362. const struct line_pnts **IPoints, int n_isles,
  363. double *att_x, double *att_y)
  364. {
  365. static struct line_pnts *Intersects;
  366. static int first_time = 1;
  367. double cent_x, cent_y;
  368. register int i, j;
  369. double max, hi_x, lo_x, hi_y, lo_y;
  370. double fa, fb, dmax;
  371. int exp;
  372. int maxpos;
  373. int point_in_sles = 0;
  374. double diff;
  375. int ret;
  376. G_debug(3, "Vect_get_point_in_poly_isl(): n_isles = %d", n_isles);
  377. if (first_time) {
  378. Intersects = Vect_new_line_struct();
  379. first_time = 0;
  380. }
  381. if (Points->n_points < 3) { /* test */
  382. if (Points->n_points > 0) {
  383. *att_x = Points->x[0];
  384. *att_y = Points->y[0];
  385. return 0;
  386. }
  387. return -1;
  388. }
  389. /* get centroid */
  390. Vect_find_poly_centroid(Points, &cent_x, &cent_y);
  391. /* is it w/in poly? */
  392. if (Vect_point_in_poly(cent_x, cent_y, Points) == 1) {
  393. /* if the point is inside the polygon */
  394. for (i = 0; i < n_isles; i++) {
  395. if (Vect_point_in_poly(cent_x, cent_y, IPoints[i]) >= 1) {
  396. point_in_sles = 1;
  397. break;
  398. }
  399. }
  400. if (!point_in_sles) {
  401. *att_x = cent_x;
  402. *att_y = cent_y;
  403. return 0;
  404. }
  405. }
  406. /* guess we have to do it the hard way... */
  407. /* first find att_y close to cent_y so that no points lie on the line */
  408. /* find the point closest to line from below, and point close to line
  409. from above and take average of their y-coordinates */
  410. /* same for x */
  411. /* first initializing lo_x,hi_x and lo_y,hi_y
  412. * to be any 2 pnts on either side of cent_x and cent_y */
  413. hi_y = cent_y - 1;
  414. lo_y = cent_y + 1;
  415. hi_x = cent_x - 1;
  416. lo_x = cent_x + 1;
  417. for (i = 0; i < Points->n_points; i++) {
  418. if ((lo_y < cent_y) && (hi_y >= cent_y) &&
  419. (lo_x < cent_x) && (hi_x >= cent_x))
  420. break; /* already initialized */
  421. if (Points->y[i] < cent_y)
  422. lo_y = Points->y[i];
  423. if (Points->y[i] >= cent_y)
  424. hi_y = Points->y[i];
  425. if (Points->x[i] < cent_x)
  426. lo_x = Points->x[i];
  427. if (Points->x[i] >= cent_x)
  428. hi_x = Points->x[i];
  429. }
  430. /* first going through boundary points */
  431. for (i = 0; i < Points->n_points; i++) {
  432. if ((Points->y[i] < cent_y) &&
  433. ((cent_y - Points->y[i]) < (cent_y - lo_y)))
  434. lo_y = Points->y[i];
  435. if ((Points->y[i] >= cent_y) &&
  436. ((Points->y[i] - cent_y) < (hi_y - cent_y)))
  437. hi_y = Points->y[i];
  438. if ((Points->x[i] < cent_x) &&
  439. ((cent_x - Points->x[i]) < (cent_x - lo_x)))
  440. lo_x = Points->x[i];
  441. if ((Points->x[i] >= cent_x) &&
  442. ((Points->x[i] - cent_x) < (hi_x - cent_x)))
  443. hi_x = Points->x[i];
  444. }
  445. for (i = 0; i < n_isles; i++) {
  446. for (j = 0; j < IPoints[i]->n_points; j++) {
  447. if ((IPoints[i]->y[j] < cent_y) &&
  448. ((cent_y - IPoints[i]->y[j]) < (cent_y - lo_y)))
  449. lo_y = IPoints[i]->y[j];
  450. if ((IPoints[i]->y[j] >= cent_y) &&
  451. ((IPoints[i]->y[j] - cent_y) < (hi_y - cent_y)))
  452. hi_y = IPoints[i]->y[j];
  453. if ((IPoints[i]->x[j] < cent_x) &&
  454. ((cent_x - IPoints[i]->x[j]) < (cent_x - lo_x)))
  455. lo_x = IPoints[i]->x[j];
  456. if ((IPoints[i]->x[j] >= cent_x) &&
  457. ((IPoints[i]->x[j] - cent_x) < (hi_x - cent_x)))
  458. hi_x = IPoints[i]->x[j];
  459. }
  460. }
  461. if (lo_y == hi_y)
  462. return (-1); /* area is empty */
  463. *att_y = (hi_y + lo_y) / 2.0;
  464. Intersects->n_points = 0;
  465. Vect__intersect_y_line_with_poly(Points, *att_y, Intersects);
  466. /* add in intersections w/ holes */
  467. for (i = 0; i < n_isles; i++) {
  468. if (0 >
  469. Vect__intersect_y_line_with_poly(IPoints[i], *att_y, Intersects))
  470. return -1;
  471. }
  472. if (Intersects->n_points < 2) /* test */
  473. return -1;
  474. qsort(Intersects->x, (size_t) Intersects->n_points, sizeof(double),
  475. (void *)comp_double);
  476. max = 0;
  477. maxpos = 0;
  478. /* find area of MAX distance */
  479. for (i = 0; i < Intersects->n_points; i += 2) {
  480. diff = Intersects->x[i + 1] - Intersects->x[i];
  481. if (diff > max) {
  482. max = diff;
  483. maxpos = i;
  484. }
  485. }
  486. /* ULP single precision 23, double 52 bits, here 42 */
  487. /* if the difference is too small, the point will be on a line
  488. * ULP double is too small, ULP single too large */
  489. fa = fabs(Intersects->x[maxpos]);
  490. fb = fabs(Intersects->x[maxpos + 1]);
  491. if (fa > fb)
  492. dmax = frexp(fa, &exp);
  493. else
  494. dmax = frexp(fb, &exp);
  495. exp -= 42;
  496. dmax = ldexp(dmax, exp);
  497. if (max > dmax) {
  498. *att_x = (Intersects->x[maxpos] + Intersects->x[maxpos + 1]) / 2.;
  499. }
  500. else {
  501. /* try x intersect */
  502. G_debug(3, "Vect_get_point_in_poly_isl(): trying x intersect");
  503. if (lo_x == hi_x)
  504. return (-1); /* area is empty */
  505. *att_x = (hi_x + lo_x) / 2.0;
  506. Intersects->n_points = 0;
  507. Vect__intersect_x_line_with_poly(Points, *att_x, Intersects);
  508. /* add in intersections w/ holes */
  509. for (i = 0; i < n_isles; i++) {
  510. if (0 >
  511. Vect__intersect_x_line_with_poly(IPoints[i], *att_x, Intersects))
  512. return -1;
  513. }
  514. if (Intersects->n_points < 2) /* test */
  515. return -1;
  516. qsort(Intersects->y, (size_t) Intersects->n_points, sizeof(double),
  517. (void *)comp_double);
  518. max = 0;
  519. maxpos = 0;
  520. /* find area of MAX distance */
  521. for (i = 0; i < Intersects->n_points; i += 2) {
  522. diff = Intersects->y[i + 1] - Intersects->y[i];
  523. if (diff > max) {
  524. max = diff;
  525. maxpos = i;
  526. }
  527. }
  528. /* ULP single precision 23, double 52 bits, here 42 */
  529. fa = fabs(Intersects->y[maxpos]);
  530. fb = fabs(Intersects->y[maxpos + 1]);
  531. if (fa > fb)
  532. dmax = frexp(fa, &exp);
  533. else
  534. dmax = frexp(fb, &exp);
  535. exp -= 42;
  536. dmax = ldexp(dmax, exp);
  537. if (max > dmax) {
  538. *att_y = (Intersects->y[maxpos] + Intersects->y[maxpos + 1]) / 2.;
  539. }
  540. else {
  541. /* area was (nearly) empty: example ((x1,y1), (x2,y2), (x1,y1)) */
  542. G_warning("Vect_get_point_in_poly_isl(): collapsed area");
  543. return -1;
  544. }
  545. }
  546. /* is it now w/in poly? */
  547. cent_x = *att_x;
  548. cent_y = *att_y;
  549. point_in_sles = 0;
  550. ret = Vect_point_in_poly(cent_x, cent_y, Points);
  551. if (ret == 2) {
  552. /* point on outer ring, should not happen because of ULP test above */
  553. G_warning("Vect_get_point_in_poly_isl(), the hard way: centroid is on outer ring, max dist is %g", max);
  554. return -1;
  555. }
  556. if (ret == 1) {
  557. /* if the point is inside the polygon, should not happen because of ULP test above */
  558. for (i = 0; i < n_isles; i++) {
  559. if (Vect_point_in_poly(cent_x, cent_y, IPoints[i]) >= 1) {
  560. point_in_sles = 1;
  561. G_warning("Vect_get_point_in_poly_isl(), the hard way: centroid is in isle, max dist is %g", max);
  562. break;
  563. }
  564. }
  565. if (!point_in_sles) {
  566. return 0;
  567. }
  568. }
  569. return -1;
  570. }
  571. /* Intersect segments of Points with ray from point X,Y to the right.
  572. * Returns: -1 point exactly on segment
  573. * number of intersections
  574. */
  575. static int segments_x_ray(double X, double Y, const struct line_pnts *Points)
  576. {
  577. double x1, x2, y1, y2;
  578. double x_inter;
  579. int n_intersects;
  580. int n;
  581. G_debug(3, "segments_x_ray(): x = %f y = %f n_points = %d", X, Y,
  582. Points->n_points);
  583. /* Follow the ray from X,Y along positive x and find number of intersections.
  584. * Coordinates exactly on ray are considered to be slightly above. */
  585. n_intersects = 0;
  586. for (n = 1; n < Points->n_points; n++) {
  587. x1 = Points->x[n - 1];
  588. y1 = Points->y[n - 1];
  589. x2 = Points->x[n];
  590. y2 = Points->y[n];
  591. /* G_debug() is slow, avoid it in loops over points,
  592. * activate when needed */
  593. /*
  594. G_debug(3, "X = %f Y = %f x1 = %f y1 = %f x2 = %f y2 = %f", X, Y, x1,
  595. y1, x2, y2);
  596. */
  597. /* I know, it should be possible to do that with less conditions,
  598. * but it should be enough readable also! */
  599. /* first, skip segments that obviously do not intersect with test ray */
  600. /* segment above (X is not important) */
  601. if (y1 > Y && y2 > Y)
  602. continue;
  603. /* segment below (X is not important) */
  604. if (y1 < Y && y2 < Y)
  605. continue;
  606. /* segment left from X -> no intersection */
  607. if (x1 < X && x2 < X)
  608. continue;
  609. /* point on vertex */
  610. if ((x1 == X && y1 == Y) || (x2 == X && y2 == Y))
  611. return -1;
  612. /* on vertical boundary */
  613. if (x1 == x2 && x1 == X) {
  614. if ((y1 <= Y && y2 >= Y) || (y1 >= Y && y2 <= Y))
  615. return -1;
  616. }
  617. /* on horizontal boundary */
  618. if (y1 == y2 && y1 == Y) {
  619. if ((x1 <= X && x2 >= X) || (x1 >= X && x2 <= X))
  620. return -1;
  621. else
  622. continue; /* segment on ray (X is not important) */
  623. }
  624. /* segment on ray (X is not important) */
  625. /* if (y1 == Y && y2 == Y)
  626. continue; */
  627. /* one end on Y second above (X is not important) */
  628. if ((y1 == Y && y2 > Y) || (y2 == Y && y1 > Y))
  629. continue;
  630. /* For following cases we know that at least one of x1 and x2 is >= X */
  631. /* one end of segment on Y second below Y */
  632. if (y1 == Y && y2 < Y) {
  633. if (x1 >= X) /* x of the end on the ray is >= X */
  634. n_intersects++;
  635. continue;
  636. }
  637. if (y2 == Y && y1 < Y) {
  638. if (x2 >= X)
  639. n_intersects++;
  640. continue;
  641. }
  642. /* one end of segment above Y second below Y */
  643. if ((y1 < Y && y2 > Y) || (y1 > Y && y2 < Y)) {
  644. if (x1 >= X && x2 >= X) {
  645. n_intersects++;
  646. continue;
  647. }
  648. /* now either x1 < X && x2 > X or x1 > X && x2 < X -> calculate intersection */
  649. x_inter = dig_x_intersect(x1, x2, y1, y2, Y);
  650. G_debug(3, "x_inter = %f", x_inter);
  651. if (x_inter == X)
  652. return -1; /* point on segment, do not assume inside/outside */
  653. else if (x_inter > X)
  654. n_intersects++;
  655. continue; /* would not be necessary, just to check, see below */
  656. }
  657. /* should not be reached (one condition is not necessary, but it is maybe better readable
  658. * and it is a check) */
  659. G_warning
  660. ("segments_x_ray() %s: X = %f Y = %f x1 = %f y1 = %f x2 = %f y2 = %f",
  661. _("conditions failed"), X, Y, x1, y1, x2, y2);
  662. }
  663. return n_intersects;
  664. }
  665. /*!
  666. \brief Determines if a point (X,Y) is inside a polygon.
  667. \param X,Y point coordinates
  668. \param Points polygon
  669. \return 0 - outside
  670. \return 1 - inside
  671. \return 2 - on the boundary
  672. */
  673. int Vect_point_in_poly(double X, double Y, const struct line_pnts *Points)
  674. {
  675. int n_intersects;
  676. G_debug(3, "Vect_point_in_poly(): x = %f y = %f n_points = %d", X, Y,
  677. Points->n_points);
  678. n_intersects = segments_x_ray(X, Y, Points);
  679. if (n_intersects == -1)
  680. return 2;
  681. /* odd number of intersections: inside, return 1
  682. * even number of intersections: outside, return 0 */
  683. return (n_intersects & 1);
  684. }
  685. /*!
  686. \brief Determines if a point (X,Y) is inside an area outer ring. Islands are not considered.
  687. \param X,Y point coordinates
  688. \param Map vector map
  689. \param area area id
  690. \param box area bounding box
  691. \return 0 - outside
  692. \return 1 - inside
  693. \return 2 - on the boundary
  694. */
  695. int
  696. Vect_point_in_area_outer_ring(double X, double Y, const struct Map_info *Map,
  697. int area, struct bound_box *box)
  698. {
  699. static int first = 1;
  700. int n_intersects, inter;
  701. int i, line;
  702. static struct line_pnts *Points;
  703. const struct Plus_head *Plus;
  704. struct P_area *Area;
  705. /* keep in sync with Vect_point_in_island() */
  706. G_debug(3, "Vect_point_in_area_outer_ring(): x = %f y = %f area = %d",
  707. X, Y, area);
  708. if (first == 1) {
  709. Points = Vect_new_line_struct();
  710. first = 0;
  711. }
  712. Plus = &(Map->plus);
  713. Area = Plus->Area[area];
  714. /* First it must be in box */
  715. if (X < box->W || X > box->E || Y > box->N || Y < box->S)
  716. return 0;
  717. n_intersects = 0;
  718. for (i = 0; i < Area->n_lines; i++) {
  719. line = abs(Area->lines[i]);
  720. Vect_read_line(Map, Points, NULL, line);
  721. /* if the bbox of the line would be available,
  722. * the bbox could be used for a first check: */
  723. /* Vect_line_box(Points, &lbox);
  724. * do not check lines that obviously do not intersect with test ray */
  725. /* if ((lbox.N < Y) || (lbox.S > Y) || (lbox.E < X))
  726. continue;
  727. */
  728. /* retrieving the bbox from the spatial index or
  729. * calculating the box from the vertices is slower than
  730. * just feeding the line to segments_x_ray() */
  731. inter = segments_x_ray(X, Y, Points);
  732. if (inter == -1)
  733. return 2;
  734. n_intersects += inter;
  735. }
  736. /* odd number of intersections: inside, return 1
  737. * even number of intersections: outside, return 0 */
  738. return (n_intersects & 1);
  739. }
  740. /*!
  741. \brief Determines if a point (X,Y) is inside an island.
  742. \param X,Y point coordinates
  743. \param Map vector map
  744. \param isle isle id
  745. \param box isle bounding box
  746. \return 0 - outside
  747. \return 1 - inside
  748. \return 2 - on the boundary
  749. */
  750. int Vect_point_in_island(double X, double Y, const struct Map_info *Map,
  751. int isle, struct bound_box *box)
  752. {
  753. static int first = 1;
  754. int n_intersects, inter;
  755. int i, line;
  756. static struct line_pnts *Points;
  757. const struct Plus_head *Plus;
  758. struct P_isle *Isle;
  759. /* keep in sync with Vect_point_in_area_outer_ring() */
  760. G_debug(3, "Vect_point_in_island(): x = %f y = %f isle = %d",
  761. X, Y, isle);
  762. if (first == 1) {
  763. Points = Vect_new_line_struct();
  764. first = 0;
  765. }
  766. Plus = &(Map->plus);
  767. Isle = Plus->Isle[isle];
  768. /* First it must be in box */
  769. if (X < box->W || X > box->E || Y > box->N || Y < box->S)
  770. return 0;
  771. n_intersects = 0;
  772. for (i = 0; i < Isle->n_lines; i++) {
  773. line = abs(Isle->lines[i]);
  774. Vect_read_line(Map, Points, NULL, line);
  775. /* if the bbox of the line would be available,
  776. * the bbox could be used for a first check: */
  777. /* Vect_line_box(Points, &lbox);
  778. * don't check lines that obviously do not intersect with test ray */
  779. /* if ((lbox.N < Y) || (lbox.S > Y) || (lbox.E < X))
  780. continue;
  781. */
  782. /* retrieving the bbox from the spatial index or
  783. * calculating the box from the vertices is slower than
  784. * just feeding the line to segments_x_ray() */
  785. inter = segments_x_ray(X, Y, Points);
  786. if (inter == -1)
  787. return 2;
  788. n_intersects += inter;
  789. }
  790. /* odd number of intersections: inside, return 1
  791. * even number of intersections: outside, return 0 */
  792. return (n_intersects & 1);
  793. }