e_intersect.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. /*!
  2. \file lib/vector/Vlib/e_intersect.c
  3. \brief Vector library - intersection (lower level functions)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2008-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 Rewritten by Rosen Matev (Google Summer of Code 2008)
  9. */
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <grass/gis.h>
  13. #include "e_intersect.h"
  14. #define SWAP(a,b) {double t = a; a = b; b = t;}
  15. #define D ((ax2-ax1)*(by1-by2) - (ay2-ay1)*(bx1-bx2))
  16. #define DA ((bx1-ax1)*(by1-by2) - (by1-ay1)*(bx1-bx2))
  17. #define DB ((ax2-ax1)*(by1-ay1) - (ay2-ay1)*(bx1-ax1))
  18. #ifdef ASDASDASFDSAFFDAS
  19. mpf_t p11, p12, p21, p22, t1, t2;
  20. mpf_t dd, dda, ddb, delta;
  21. mpf_t rra, rrb;
  22. int initialized = 0;
  23. void initialize_mpf_vars()
  24. {
  25. mpf_set_default_prec(2048);
  26. mpf_init(p11);
  27. mpf_init(p12);
  28. mpf_init(p21);
  29. mpf_init(p22);
  30. mpf_init(t1);
  31. mpf_init(t2);
  32. mpf_init(dd);
  33. mpf_init(dda);
  34. mpf_init(ddb);
  35. mpf_init(delta);
  36. mpf_init(rra);
  37. mpf_init(rrb);
  38. initialized = 1;
  39. }
  40. /*
  41. Caclulates:
  42. |a11-b11 a12-b12|
  43. |a21-b21 a22-b22|
  44. */
  45. void det22(mpf_t rop, double a11, double b11, double a12, double b12,
  46. double a21, double b21, double a22, double b22)
  47. {
  48. mpf_set_d(t1, a11);
  49. mpf_set_d(t2, b11);
  50. mpf_sub(p11, t1, t2);
  51. mpf_set_d(t1, a12);
  52. mpf_set_d(t2, b12);
  53. mpf_sub(p12, t1, t2);
  54. mpf_set_d(t1, a21);
  55. mpf_set_d(t2, b21);
  56. mpf_sub(p21, t1, t2);
  57. mpf_set_d(t1, a22);
  58. mpf_set_d(t2, b22);
  59. mpf_sub(p22, t1, t2);
  60. mpf_mul(t1, p11, p22);
  61. mpf_mul(t2, p12, p21);
  62. mpf_sub(rop, t1, t2);
  63. return;
  64. }
  65. void swap(double *a, double *b)
  66. {
  67. double t = *a;
  68. *a = *b;
  69. *b = t;
  70. return;
  71. }
  72. /* multi-precision version */
  73. int segment_intersection_2d_e(double ax1, double ay1, double ax2, double ay2,
  74. double bx1, double by1, double bx2, double by2,
  75. double *x1, double *y1, double *x2, double *y2)
  76. {
  77. double t;
  78. double max_ax, min_ax, max_ay, min_ay;
  79. double max_bx, min_bx, max_by, min_by;
  80. int sgn_d, sgn_da, sgn_db;
  81. int vertical;
  82. int f11, f12, f21, f22;
  83. mp_exp_t exp;
  84. char *s;
  85. if (!initialized)
  86. initialize_mpf_vars();
  87. /* TODO: Works for points ? */
  88. G_debug(3, "segment_intersection_2d_e()");
  89. G_debug(4, " ax1 = %.18f, ay1 = %.18f", ax1, ay1);
  90. G_debug(4, " ax2 = %.18f, ay2 = %.18f", ax2, ay2);
  91. G_debug(4, " bx1 = %.18f, by1 = %.18f", bx1, by1);
  92. G_debug(4, " bx2 = %.18f, by2 = %.18f", bx2, by2);
  93. f11 = ((ax1 == bx1) && (ay1 == by1));
  94. f12 = ((ax1 == bx2) && (ay1 == by2));
  95. f21 = ((ax2 == bx1) && (ay2 == by1));
  96. f22 = ((ax2 == bx2) && (ay2 == by2));
  97. /* Check for identical segments */
  98. if ((f11 && f22) || (f12 && f21)) {
  99. G_debug(3, " identical segments");
  100. *x1 = ax1;
  101. *y1 = ay1;
  102. *x2 = ax2;
  103. *y2 = ay2;
  104. return 5;
  105. }
  106. /* Check for identical endpoints */
  107. if (f11 || f12) {
  108. G_debug(3, " connected by endpoints");
  109. *x1 = ax1;
  110. *y1 = ay1;
  111. return 1;
  112. }
  113. if (f21 || f22) {
  114. G_debug(3, " connected by endpoints");
  115. *x1 = ax2;
  116. *y1 = ay2;
  117. return 1;
  118. }
  119. if ((MAX(ax1, ax2) < MIN(bx1, bx2)) || (MAX(bx1, bx2) < MIN(ax1, ax2))) {
  120. G_debug(3, " no intersection (disjoint bounding boxes)");
  121. return 0;
  122. }
  123. if ((MAX(ay1, ay2) < MIN(by1, by2)) || (MAX(by1, by2) < MIN(ay1, ay2))) {
  124. G_debug(3, " no intersection (disjoint bounding boxes)");
  125. return 0;
  126. }
  127. det22(dd, ax2, ax1, bx1, bx2, ay2, ay1, by1, by2);
  128. sgn_d = mpf_sgn(dd);
  129. if (sgn_d != 0) {
  130. G_debug(3, " general position");
  131. det22(dda, bx1, ax1, bx1, bx2, by1, ay1, by1, by2);
  132. sgn_da = mpf_sgn(dda);
  133. /*mpf_div(rra, dda, dd);
  134. mpf_div(rrb, ddb, dd);
  135. s = mpf_get_str(NULL, &exp, 10, 40, rra);
  136. G_debug(4, " ra = %sE%d", (s[0]==0)?"0":s, exp);
  137. s = mpf_get_str(NULL, &exp, 10, 24, rrb);
  138. G_debug(4, " rb = %sE%d", (s[0]==0)?"0":s, exp);
  139. */
  140. if (sgn_d > 0) {
  141. if ((sgn_da < 0) || (mpf_cmp(dda, dd) > 0)) {
  142. G_debug(3, " no intersection");
  143. return 0;
  144. }
  145. det22(ddb, ax2, ax1, bx1, ax1, ay2, ay1, by1, ay1);
  146. sgn_db = mpf_sgn(ddb);
  147. if ((sgn_db < 0) || (mpf_cmp(ddb, dd) > 0)) {
  148. G_debug(3, " no intersection");
  149. return 0;
  150. }
  151. }
  152. else { /* if sgn_d < 0 */
  153. if ((sgn_da > 0) || (mpf_cmp(dda, dd) < 0)) {
  154. G_debug(3, " no intersection");
  155. return 0;
  156. }
  157. det22(ddb, ax2, ax1, bx1, ax1, ay2, ay1, by1, ay1);
  158. sgn_db = mpf_sgn(ddb);
  159. if ((sgn_db > 0) || (mpf_cmp(ddb, dd) < 0)) {
  160. G_debug(3, " no intersection");
  161. return 0;
  162. }
  163. }
  164. /*G_debug(3, " ra=%.17g rb=%.17g", mpf_get_d(dda)/mpf_get_d(dd), mpf_get_d(ddb)/mpf_get_d(dd)); */
  165. /*G_debug(3, " sgn_d=%d sgn_da=%d sgn_db=%d cmp(dda,dd)=%d cmp(ddb,dd)=%d", sgn_d, sgn_da, sgn_db, mpf_cmp(dda, dd), mpf_cmp(ddb, dd)); */
  166. mpf_set_d(delta, ax2 - ax1);
  167. mpf_mul(t1, dda, delta);
  168. mpf_div(t2, t1, dd);
  169. *x1 = ax1 + mpf_get_d(t2);
  170. mpf_set_d(delta, ay2 - ay1);
  171. mpf_mul(t1, dda, delta);
  172. mpf_div(t2, t1, dd);
  173. *y1 = ay1 + mpf_get_d(t2);
  174. G_debug(3, " intersection %.16g, %.16g", *x1, *y1);
  175. return 1;
  176. }
  177. /* segments are parallel or collinear */
  178. det22(dda, bx1, ax1, bx1, bx2, by1, ay1, by1, by2);
  179. sgn_da = mpf_sgn(dda);
  180. if (sgn_da != 0) {
  181. /* segments are parallel */
  182. G_debug(3, " parallel segments");
  183. return 0;
  184. }
  185. /* segments are colinear. check for overlap */
  186. /* swap endpoints if needed */
  187. /* if segments are vertical, we swap x-coords with y-coords */
  188. vertical = 0;
  189. if (ax1 > ax2) {
  190. SWAP(ax1, ax2);
  191. SWAP(ay1, ay2);
  192. }
  193. else if (ax1 == ax2) {
  194. vertical = 1;
  195. if (ay1 > ay2)
  196. SWAP(ay1, ay2);
  197. SWAP(ax1, ay1);
  198. SWAP(ax2, ay2);
  199. }
  200. if (bx1 > bx2) {
  201. SWAP(bx1, bx2);
  202. SWAP(by1, by2);
  203. }
  204. else if (bx1 == bx2) {
  205. if (by1 > by2)
  206. SWAP(by1, by2);
  207. SWAP(bx1, by1);
  208. SWAP(bx2, by2);
  209. }
  210. G_debug(3, " collinear segments");
  211. if ((bx2 < ax1) || (bx1 > ax2)) {
  212. G_debug(3, " no intersection");
  213. return 0;
  214. }
  215. /* there is overlap or connected end points */
  216. G_debug(3, " overlap");
  217. /* a contains b */
  218. if ((ax1 < bx1) && (ax2 > bx2)) {
  219. G_debug(3, " a contains b");
  220. if (!vertical) {
  221. *x1 = bx1;
  222. *y1 = by1;
  223. *x2 = bx2;
  224. *y2 = by2;
  225. }
  226. else {
  227. *x1 = by1;
  228. *y1 = bx1;
  229. *x2 = by2;
  230. *y2 = bx2;
  231. }
  232. return 3;
  233. }
  234. /* b contains a */
  235. if ((ax1 > bx1) && (ax2 < bx2)) {
  236. G_debug(3, " b contains a");
  237. if (!vertical) {
  238. *x1 = bx1;
  239. *y1 = by1;
  240. *x2 = bx2;
  241. *y2 = by2;
  242. }
  243. else {
  244. *x1 = by1;
  245. *y1 = bx1;
  246. *x2 = by2;
  247. *y2 = bx2;
  248. }
  249. return 4;
  250. }
  251. /* general overlap, 2 intersection points */
  252. G_debug(3, " partial overlap");
  253. if ((bx1 > ax1) && (bx1 < ax2)) { /* b1 is in a */
  254. if (!vertical) {
  255. *x1 = bx1;
  256. *y1 = by1;
  257. *x2 = ax2;
  258. *y2 = ay2;
  259. }
  260. else {
  261. *x1 = by1;
  262. *y1 = bx1;
  263. *x2 = ay2;
  264. *y2 = ax2;
  265. }
  266. return 2;
  267. }
  268. if ((bx2 > ax1) && (bx2 < ax2)) { /* b2 is in a */
  269. if (!vertical) {
  270. *x1 = bx2;
  271. *y1 = by2;
  272. *x2 = ax1;
  273. *y2 = ay1;
  274. }
  275. else {
  276. *x1 = by2;
  277. *y1 = bx2;
  278. *x2 = ay1;
  279. *y2 = ax1;
  280. }
  281. return 2;
  282. }
  283. /* should not be reached */
  284. G_warning(("segment_intersection_2d() ERROR (should not be reached)"));
  285. G_warning("%.16g %.16g", ax1, ay1);
  286. G_warning("%.16g %.16g", ax2, ay2);
  287. G_warning("x");
  288. G_warning("%.16g %.16g", bx1, by1);
  289. G_warning("%.16g %.16g", bx2, by2);
  290. return 0;
  291. }
  292. #endif
  293. /* OLD */
  294. /* tollerance aware version */
  295. /* TODO: fix all ==s left */
  296. int segment_intersection_2d_tol(double ax1, double ay1, double ax2,
  297. double ay2, double bx1, double by1,
  298. double bx2, double by2, double *x1,
  299. double *y1, double *x2, double *y2,
  300. double tol)
  301. {
  302. double tola, tolb;
  303. double d, d1, d2, ra, rb, t;
  304. int switched = 0;
  305. /* TODO: Works for points ? */
  306. G_debug(4, "segment_intersection_2d()");
  307. G_debug(4, " ax1 = %.18f, ay1 = %.18f", ax1, ay1);
  308. G_debug(4, " ax2 = %.18f, ay2 = %.18f", ax2, ay2);
  309. G_debug(4, " bx1 = %.18f, by1 = %.18f", bx1, by1);
  310. G_debug(4, " bx2 = %.18f, by2 = %.18f", bx2, by2);
  311. /* Check identical lines */
  312. if ((FEQUAL(ax1, bx1, tol) && FEQUAL(ay1, by1, tol) &&
  313. FEQUAL(ax2, bx2, tol) && FEQUAL(ay2, by2, tol)) ||
  314. (FEQUAL(ax1, bx2, tol) && FEQUAL(ay1, by2, tol) &&
  315. FEQUAL(ax2, bx1, tol) && FEQUAL(ay2, by1, tol))) {
  316. G_debug(2, " -> identical segments");
  317. *x1 = ax1;
  318. *y1 = ay1;
  319. *x2 = ax2;
  320. *y2 = ay2;
  321. return 5;
  322. }
  323. /* 'Sort' lines by x1, x2, y1, y2 */
  324. if (bx1 < ax1)
  325. switched = 1;
  326. else if (bx1 == ax1) {
  327. if (bx2 < ax2)
  328. switched = 1;
  329. else if (bx2 == ax2) {
  330. if (by1 < ay1)
  331. switched = 1;
  332. else if (by1 == ay1) {
  333. if (by2 < ay2)
  334. switched = 1; /* by2 != ay2 (would be identical */
  335. }
  336. }
  337. }
  338. if (switched) {
  339. t = ax1;
  340. ax1 = bx1;
  341. bx1 = t;
  342. t = ay1;
  343. ay1 = by1;
  344. by1 = t;
  345. t = ax2;
  346. ax2 = bx2;
  347. bx2 = t;
  348. t = ay2;
  349. ay2 = by2;
  350. by2 = t;
  351. }
  352. d = (ax2 - ax1) * (by1 - by2) - (ay2 - ay1) * (bx1 - bx2);
  353. d1 = (bx1 - ax1) * (by1 - by2) - (by1 - ay1) * (bx1 - bx2);
  354. d2 = (ax2 - ax1) * (by1 - ay1) - (ay2 - ay1) * (bx1 - ax1);
  355. G_debug(2, " d = %.18g", d);
  356. G_debug(2, " d1 = %.18g", d1);
  357. G_debug(2, " d2 = %.18g", d2);
  358. tola = tol / MAX(fabs(ax2 - ax1), fabs(ay2 - ay1));
  359. tolb = tol / MAX(fabs(bx2 - bx1), fabs(by2 - by1));
  360. G_debug(2, " tol = %.18g", tol);
  361. G_debug(2, " tola = %.18g", tola);
  362. G_debug(2, " tolb = %.18g", tolb);
  363. if (!FZERO(d, tol)) {
  364. ra = d1 / d;
  365. rb = d2 / d;
  366. G_debug(2, " not parallel/collinear: ra = %.18g", ra);
  367. G_debug(2, " rb = %.18g", rb);
  368. if ((ra <= -tola) || (ra >= 1 + tola) || (rb <= -tolb) ||
  369. (rb >= 1 + tolb)) {
  370. G_debug(2, " no intersection");
  371. return 0;
  372. }
  373. ra = MIN(MAX(ra, 0), 1);
  374. *x1 = ax1 + ra * (ax2 - ax1);
  375. *y1 = ay1 + ra * (ay2 - ay1);
  376. G_debug(2, " intersection %.18f, %.18f", *x1, *y1);
  377. return 1;
  378. }
  379. /* segments are parallel or collinear */
  380. G_debug(3, " -> parallel/collinear");
  381. if ((!FZERO(d1, tol)) || (!FZERO(d2, tol))) { /* lines are parallel */
  382. G_debug(2, " -> parallel");
  383. return 0;
  384. }
  385. /* segments are colinear. check for overlap */
  386. /* aa = adx*adx + ady*ady;
  387. bb = bdx*bdx + bdy*bdy;
  388. t = (ax1-bx1)*bdx + (ay1-by1)*bdy; */
  389. /* Collinear vertical */
  390. /* original code assumed lines were not both vertical
  391. * so there is a special case if they are */
  392. if (FEQUAL(ax1, ax2, tol) && FEQUAL(bx1, bx2, tol) &&
  393. FEQUAL(ax1, bx1, tol)) {
  394. G_debug(2, " -> collinear vertical");
  395. if (ay1 > ay2) {
  396. t = ay1;
  397. ay1 = ay2;
  398. ay2 = t;
  399. } /* to be sure that ay1 < ay2 */
  400. if (by1 > by2) {
  401. t = by1;
  402. by1 = by2;
  403. by2 = t;
  404. } /* to be sure that by1 < by2 */
  405. if (ay1 > by2 || ay2 < by1) {
  406. G_debug(2, " -> no intersection");
  407. return 0;
  408. }
  409. /* end points */
  410. if (FEQUAL(ay1, by2, tol)) {
  411. *x1 = ax1;
  412. *y1 = ay1;
  413. G_debug(2, " -> connected by end points");
  414. return 1; /* endpoints only */
  415. }
  416. if (FEQUAL(ay2, by1, tol)) {
  417. *x1 = ax2;
  418. *y1 = ay2;
  419. G_debug(2, " -> connected by end points");
  420. return 1; /* endpoints only */
  421. }
  422. /* general overlap */
  423. G_debug(3, " -> vertical overlap");
  424. /* a contains b */
  425. if (ay1 <= by1 && ay2 >= by2) {
  426. G_debug(2, " -> a contains b");
  427. *x1 = bx1;
  428. *y1 = by1;
  429. *x2 = bx2;
  430. *y2 = by2;
  431. if (!switched)
  432. return 3;
  433. else
  434. return 4;
  435. }
  436. /* b contains a */
  437. if (ay1 >= by1 && ay2 <= by2) {
  438. G_debug(2, " -> b contains a");
  439. *x1 = ax1;
  440. *y1 = ay1;
  441. *x2 = ax2;
  442. *y2 = ay2;
  443. if (!switched)
  444. return 4;
  445. else
  446. return 3;
  447. }
  448. /* general overlap, 2 intersection points */
  449. G_debug(2, " -> partial overlap");
  450. if (by1 > ay1 && by1 < ay2) { /* b1 in a */
  451. if (!switched) {
  452. *x1 = bx1;
  453. *y1 = by1;
  454. *x2 = ax2;
  455. *y2 = ay2;
  456. }
  457. else {
  458. *x1 = ax2;
  459. *y1 = ay2;
  460. *x2 = bx1;
  461. *y2 = by1;
  462. }
  463. return 2;
  464. }
  465. if (by2 > ay1 && by2 < ay2) { /* b2 in a */
  466. if (!switched) {
  467. *x1 = bx2;
  468. *y1 = by2;
  469. *x2 = ax1;
  470. *y2 = ay1;
  471. }
  472. else {
  473. *x1 = ax1;
  474. *y1 = ay1;
  475. *x2 = bx2;
  476. *y2 = by2;
  477. }
  478. return 2;
  479. }
  480. /* should not be reached */
  481. G_warning(("Vect_segment_intersection() ERROR (collinear vertical segments)"));
  482. G_warning("%.15g %.15g", ax1, ay1);
  483. G_warning("%.15g %.15g", ax2, ay2);
  484. G_warning("x");
  485. G_warning("%.15g %.15g", bx1, by1);
  486. G_warning("%.15g %.15g", bx2, by2);
  487. return 0;
  488. }
  489. G_debug(2, " -> collinear non vertical");
  490. /* Collinear non vertical */
  491. if ((bx1 > ax1 && bx2 > ax1 && bx1 > ax2 && bx2 > ax2) ||
  492. (bx1 < ax1 && bx2 < ax1 && bx1 < ax2 && bx2 < ax2)) {
  493. G_debug(2, " -> no intersection");
  494. return 0;
  495. }
  496. /* there is overlap or connected end points */
  497. G_debug(2, " -> overlap/connected end points");
  498. /* end points */
  499. if ((ax1 == bx1 && ay1 == by1) || (ax1 == bx2 && ay1 == by2)) {
  500. *x1 = ax1;
  501. *y1 = ay1;
  502. G_debug(2, " -> connected by end points");
  503. return 1;
  504. }
  505. if ((ax2 == bx1 && ay2 == by1) || (ax2 == bx2 && ay2 == by2)) {
  506. *x1 = ax2;
  507. *y1 = ay2;
  508. G_debug(2, " -> connected by end points");
  509. return 1;
  510. }
  511. if (ax1 > ax2) {
  512. t = ax1;
  513. ax1 = ax2;
  514. ax2 = t;
  515. t = ay1;
  516. ay1 = ay2;
  517. ay2 = t;
  518. } /* to be sure that ax1 < ax2 */
  519. if (bx1 > bx2) {
  520. t = bx1;
  521. bx1 = bx2;
  522. bx2 = t;
  523. t = by1;
  524. by1 = by2;
  525. by2 = t;
  526. } /* to be sure that bx1 < bx2 */
  527. /* a contains b */
  528. if (ax1 <= bx1 && ax2 >= bx2) {
  529. G_debug(2, " -> a contains b");
  530. *x1 = bx1;
  531. *y1 = by1;
  532. *x2 = bx2;
  533. *y2 = by2;
  534. if (!switched)
  535. return 3;
  536. else
  537. return 4;
  538. }
  539. /* b contains a */
  540. if (ax1 >= bx1 && ax2 <= bx2) {
  541. G_debug(2, " -> b contains a");
  542. *x1 = ax1;
  543. *y1 = ay1;
  544. *x2 = ax2;
  545. *y2 = ay2;
  546. if (!switched)
  547. return 4;
  548. else
  549. return 3;
  550. }
  551. /* general overlap, 2 intersection points (lines are not vertical) */
  552. G_debug(2, " -> partial overlap");
  553. if (bx1 > ax1 && bx1 < ax2) { /* b1 is in a */
  554. if (!switched) {
  555. *x1 = bx1;
  556. *y1 = by1;
  557. *x2 = ax2;
  558. *y2 = ay2;
  559. }
  560. else {
  561. *x1 = ax2;
  562. *y1 = ay2;
  563. *x2 = bx1;
  564. *y2 = by1;
  565. }
  566. return 2;
  567. }
  568. if (bx2 > ax1 && bx2 < ax2) { /* b2 is in a */
  569. if (!switched) {
  570. *x1 = bx2;
  571. *y1 = by2;
  572. *x2 = ax1;
  573. *y2 = ay1;
  574. }
  575. else {
  576. *x1 = ax1;
  577. *y1 = ay1;
  578. *x2 = bx2;
  579. *y2 = by2;
  580. }
  581. return 2;
  582. }
  583. /* should not be reached */
  584. G_warning(("segment_intersection_2d() ERROR (collinear non vertical segments)"));
  585. G_warning("%.15g %.15g", ax1, ay1);
  586. G_warning("%.15g %.15g", ax2, ay2);
  587. G_warning("x");
  588. G_warning("%.15g %.15g", bx1, by1);
  589. G_warning("%.15g %.15g", bx2, by2);
  590. return 0;
  591. }
  592. int segment_intersection_2d(double ax1, double ay1, double ax2, double ay2,
  593. double bx1, double by1, double bx2, double by2,
  594. double *x1, double *y1, double *x2, double *y2)
  595. {
  596. const int DLEVEL = 4;
  597. int vertical;
  598. int f11, f12, f21, f22;
  599. double d, da, db;
  600. /* TODO: Works for points ? */
  601. G_debug(DLEVEL, "segment_intersection_2d()");
  602. G_debug(4, " ax1 = %.18f, ay1 = %.18f", ax1, ay1);
  603. G_debug(4, " ax2 = %.18f, ay2 = %.18f", ax2, ay2);
  604. G_debug(4, " bx1 = %.18f, by1 = %.18f", bx1, by1);
  605. G_debug(4, " bx2 = %.18f, by2 = %.18f", bx2, by2);
  606. f11 = ((ax1 == bx1) && (ay1 == by1));
  607. f12 = ((ax1 == bx2) && (ay1 == by2));
  608. f21 = ((ax2 == bx1) && (ay2 == by1));
  609. f22 = ((ax2 == bx2) && (ay2 == by2));
  610. /* Check for identical segments */
  611. if ((f11 && f22) || (f12 && f21)) {
  612. G_debug(DLEVEL, " identical segments");
  613. *x1 = ax1;
  614. *y1 = ay1;
  615. *x2 = ax2;
  616. *y2 = ay2;
  617. return 5;
  618. }
  619. /* Check for identical endpoints */
  620. if (f11 || f12) {
  621. G_debug(DLEVEL, " connected by endpoints");
  622. *x1 = ax1;
  623. *y1 = ay1;
  624. return 1;
  625. }
  626. if (f21 || f22) {
  627. G_debug(DLEVEL, " connected by endpoints");
  628. *x1 = ax2;
  629. *y1 = ay2;
  630. return 1;
  631. }
  632. if ((MAX(ax1, ax2) < MIN(bx1, bx2)) || (MAX(bx1, bx2) < MIN(ax1, ax2))) {
  633. G_debug(DLEVEL, " no intersection (disjoint bounding boxes)");
  634. return 0;
  635. }
  636. if ((MAX(ay1, ay2) < MIN(by1, by2)) || (MAX(by1, by2) < MIN(ay1, ay2))) {
  637. G_debug(DLEVEL, " no intersection (disjoint bounding boxes)");
  638. return 0;
  639. }
  640. /* swap endpoints if needed */
  641. /* if segments are vertical, we swap x-coords with y-coords */
  642. vertical = 0;
  643. if (ax1 > ax2) {
  644. SWAP(ax1, ax2);
  645. SWAP(ay1, ay2);
  646. }
  647. else if (ax1 == ax2) {
  648. vertical = 1;
  649. if (ay1 > ay2)
  650. SWAP(ay1, ay2);
  651. SWAP(ax1, ay1);
  652. SWAP(ax2, ay2);
  653. }
  654. if (bx1 > bx2) {
  655. SWAP(bx1, bx2);
  656. SWAP(by1, by2);
  657. }
  658. else if (bx1 == bx2) {
  659. if (by1 > by2)
  660. SWAP(by1, by2);
  661. SWAP(bx1, by1);
  662. SWAP(bx2, by2);
  663. }
  664. d = D;
  665. if (d != 0) {
  666. G_debug(DLEVEL, " general position");
  667. da = DA;
  668. /*mpf_div(rra, dda, dd);
  669. mpf_div(rrb, ddb, dd);
  670. s = mpf_get_str(NULL, &exp, 10, 40, rra);
  671. G_debug(4, " ra = %sE%d", (s[0]==0)?"0":s, exp);
  672. s = mpf_get_str(NULL, &exp, 10, 24, rrb);
  673. G_debug(4, " rb = %sE%d", (s[0]==0)?"0":s, exp);
  674. */
  675. if (d > 0) {
  676. if ((da < 0) || (da > d)) {
  677. G_debug(DLEVEL, " no intersection");
  678. return 0;
  679. }
  680. db = DB;
  681. if ((db < 0) || (db > d)) {
  682. G_debug(DLEVEL, " no intersection");
  683. return 0;
  684. }
  685. }
  686. else { /* if d < 0 */
  687. if ((da > 0) || (da < d)) {
  688. G_debug(DLEVEL, " no intersection");
  689. return 0;
  690. }
  691. db = DB;
  692. if ((db > 0) || (db < d)) {
  693. G_debug(DLEVEL, " no intersection");
  694. return 0;
  695. }
  696. }
  697. /*G_debug(DLEVEL, " ra=%.17g rb=%.17g", mpf_get_d(dda)/mpf_get_d(dd), mpf_get_d(ddb)/mpf_get_d(dd)); */
  698. /*G_debug(DLEVEL, " sgn_d=%d sgn_da=%d sgn_db=%d cmp(dda,dd)=%d cmp(ddb,dd)=%d", sgn_d, sgn_da, sgn_db, mpf_cmp(dda, dd), mpf_cmp(ddb, dd)); */
  699. *x1 = ax1 + (ax2 - ax1) * da / d;
  700. *y1 = ay1 + (ay2 - ay1) * da / d;
  701. G_debug(DLEVEL, " intersection %.16g, %.16g", *x1, *y1);
  702. return 1;
  703. }
  704. /* segments are parallel or collinear */
  705. da = DA;
  706. db = DB;
  707. if ((da != 0) || (db != 0)) {
  708. /* segments are parallel */
  709. G_debug(DLEVEL, " parallel segments");
  710. return 0;
  711. }
  712. /* segments are colinear. check for overlap */
  713. G_debug(DLEVEL, " collinear segments");
  714. if ((bx2 < ax1) || (bx1 > ax2)) {
  715. G_debug(DLEVEL, " no intersection");
  716. return 0;
  717. }
  718. /* there is overlap or connected end points */
  719. G_debug(DLEVEL, " overlap");
  720. /* a contains b */
  721. if ((ax1 < bx1) && (ax2 > bx2)) {
  722. G_debug(DLEVEL, " a contains b");
  723. if (!vertical) {
  724. *x1 = bx1;
  725. *y1 = by1;
  726. *x2 = bx2;
  727. *y2 = by2;
  728. }
  729. else {
  730. *x1 = by1;
  731. *y1 = bx1;
  732. *x2 = by2;
  733. *y2 = bx2;
  734. }
  735. return 3;
  736. }
  737. /* b contains a */
  738. if ((ax1 > bx1) && (ax2 < bx2)) {
  739. G_debug(DLEVEL, " b contains a");
  740. if (!vertical) {
  741. *x1 = bx1;
  742. *y1 = by1;
  743. *x2 = bx2;
  744. *y2 = by2;
  745. }
  746. else {
  747. *x1 = by1;
  748. *y1 = bx1;
  749. *x2 = by2;
  750. *y2 = bx2;
  751. }
  752. return 4;
  753. }
  754. /* general overlap, 2 intersection points */
  755. G_debug(DLEVEL, " partial overlap");
  756. if ((bx1 > ax1) && (bx1 < ax2)) { /* b1 is in a */
  757. if (!vertical) {
  758. *x1 = bx1;
  759. *y1 = by1;
  760. *x2 = ax2;
  761. *y2 = ay2;
  762. }
  763. else {
  764. *x1 = by1;
  765. *y1 = bx1;
  766. *x2 = ay2;
  767. *y2 = ax2;
  768. }
  769. return 2;
  770. }
  771. if ((bx2 > ax1) && (bx2 < ax2)) { /* b2 is in a */
  772. if (!vertical) {
  773. *x1 = bx2;
  774. *y1 = by2;
  775. *x2 = ax1;
  776. *y2 = ay1;
  777. }
  778. else {
  779. *x1 = by2;
  780. *y1 = bx2;
  781. *x2 = ay1;
  782. *y2 = ax1;
  783. }
  784. return 2;
  785. }
  786. /* should not be reached */
  787. G_warning(("segment_intersection_2d() ERROR (should not be reached)"));
  788. G_warning("%.16g %.16g", ax1, ay1);
  789. G_warning("%.16g %.16g", ax2, ay2);
  790. G_warning("x");
  791. G_warning("%.16g %.16g", bx1, by1);
  792. G_warning("%.16g %.16g", bx2, by2);
  793. return 0;
  794. }
  795. #define N 52 /* double's mantisa size in bits */
  796. /* a and b are different in at most <bits> significant digits */
  797. int almost_equal(double a, double b, int bits)
  798. {
  799. int ea, eb, e;
  800. if (a == b)
  801. return 1;
  802. if (a == 0 || b == 0) {
  803. /* return (0 < -N+bits); */
  804. return (bits > N);
  805. }
  806. frexp(a, &ea);
  807. frexp(b, &eb);
  808. if (ea != eb)
  809. return (bits > N + abs(ea - eb));
  810. frexp(a - b, &e);
  811. return (e < ea - N + bits);
  812. }
  813. #ifdef ASDASDFASFEAS
  814. int segment_intersection_2d_test(double ax1, double ay1, double ax2,
  815. double ay2, double bx1, double by1,
  816. double bx2, double by2, double *x1,
  817. double *y1, double *x2, double *y2)
  818. {
  819. double t;
  820. double max_ax, min_ax, max_ay, min_ay;
  821. double max_bx, min_bx, max_by, min_by;
  822. int sgn_d, sgn_da, sgn_db;
  823. int vertical;
  824. int f11, f12, f21, f22;
  825. mp_exp_t exp;
  826. char *s;
  827. double d, da, db, ra, rb;
  828. if (!initialized)
  829. initialize_mpf_vars();
  830. /* TODO: Works for points ? */
  831. G_debug(3, "segment_intersection_2d_test()");
  832. G_debug(3, " ax1 = %.18e, ay1 = %.18e", ax1, ay1);
  833. G_debug(3, " ax2 = %.18e, ay2 = %.18e", ax2, ay2);
  834. G_debug(3, " bx1 = %.18e, by1 = %.18e", bx1, by1);
  835. G_debug(3, " bx2 = %.18e, by2 = %.18e", bx2, by2);
  836. f11 = ((ax1 == bx1) && (ay1 == by1));
  837. f12 = ((ax1 == bx2) && (ay1 == by2));
  838. f21 = ((ax2 == bx1) && (ay2 == by1));
  839. f22 = ((ax2 == bx2) && (ay2 == by2));
  840. /* Check for identical segments */
  841. if ((f11 && f22) || (f12 && f21)) {
  842. G_debug(4, " identical segments");
  843. *x1 = ax1;
  844. *y1 = ay1;
  845. *x2 = ax2;
  846. *y2 = ay2;
  847. return 5;
  848. }
  849. /* Check for identical endpoints */
  850. if (f11 || f12) {
  851. G_debug(4, " connected by endpoints");
  852. *x1 = ax1;
  853. *y1 = ay1;
  854. return 1;
  855. }
  856. if (f21 || f22) {
  857. G_debug(4, " connected by endpoints");
  858. *x1 = ax2;
  859. *y1 = ay2;
  860. return 1;
  861. }
  862. if ((MAX(ax1, ax2) < MIN(bx1, bx2)) || (MAX(bx1, bx2) < MIN(ax1, ax2))) {
  863. G_debug(4, " no intersection (disjoint bounding boxes)");
  864. return 0;
  865. }
  866. if ((MAX(ay1, ay2) < MIN(by1, by2)) || (MAX(by1, by2) < MIN(ay1, ay2))) {
  867. G_debug(4, " no intersection (disjoint bounding boxes)");
  868. return 0;
  869. }
  870. d = (ax2 - ax1) * (by1 - by2) - (ay2 - ay1) * (bx1 - bx2);
  871. da = (bx1 - ax1) * (by1 - by2) - (by1 - ay1) * (bx1 - bx2);
  872. db = (ax2 - ax1) * (by1 - ay1) - (ay2 - ay1) * (bx1 - ax1);
  873. det22(dd, ax2, ax1, bx1, bx2, ay2, ay1, by1, by2);
  874. sgn_d = mpf_sgn(dd);
  875. s = mpf_get_str(NULL, &exp, 10, 40, dd);
  876. G_debug(3, " dd = %sE%d", (s[0] == 0) ? "0" : s, exp);
  877. G_debug(3, " d = %.18E", d);
  878. if (sgn_d != 0) {
  879. G_debug(3, " general position");
  880. det22(dda, bx1, ax1, bx1, bx2, by1, ay1, by1, by2);
  881. det22(ddb, ax2, ax1, bx1, ax1, ay2, ay1, by1, ay1);
  882. sgn_da = mpf_sgn(dda);
  883. sgn_db = mpf_sgn(ddb);
  884. ra = da / d;
  885. rb = db / d;
  886. mpf_div(rra, dda, dd);
  887. mpf_div(rrb, ddb, dd);
  888. s = mpf_get_str(NULL, &exp, 10, 40, rra);
  889. G_debug(4, " rra = %sE%d", (s[0] == 0) ? "0" : s, exp);
  890. G_debug(4, " ra = %.18E", ra);
  891. s = mpf_get_str(NULL, &exp, 10, 40, rrb);
  892. G_debug(4, " rrb = %sE%d", (s[0] == 0) ? "0" : s, exp);
  893. G_debug(4, " rb = %.18E", rb);
  894. if (sgn_d > 0) {
  895. if ((sgn_da < 0) || (mpf_cmp(dda, dd) > 0)) {
  896. G_debug(DLEVEL, " no intersection");
  897. return 0;
  898. }
  899. if ((sgn_db < 0) || (mpf_cmp(ddb, dd) > 0)) {
  900. G_debug(DLEVEL, " no intersection");
  901. return 0;
  902. }
  903. }
  904. else { /* if sgn_d < 0 */
  905. if ((sgn_da > 0) || (mpf_cmp(dda, dd) < 0)) {
  906. G_debug(DLEVEL, " no intersection");
  907. return 0;
  908. }
  909. if ((sgn_db > 0) || (mpf_cmp(ddb, dd) < 0)) {
  910. G_debug(DLEVEL, " no intersection");
  911. return 0;
  912. }
  913. }
  914. mpf_set_d(delta, ax2 - ax1);
  915. mpf_mul(t1, dda, delta);
  916. mpf_div(t2, t1, dd);
  917. *x1 = ax1 + mpf_get_d(t2);
  918. mpf_set_d(delta, ay2 - ay1);
  919. mpf_mul(t1, dda, delta);
  920. mpf_div(t2, t1, dd);
  921. *y1 = ay1 + mpf_get_d(t2);
  922. G_debug(2, " intersection at:");
  923. G_debug(2, " xx = %.18e", *x1);
  924. G_debug(2, " x = %.18e", ax1 + ra * (ax2 - ax1));
  925. G_debug(2, " yy = %.18e", *y1);
  926. G_debug(2, " y = %.18e", ay1 + ra * (ay2 - ay1));
  927. return 1;
  928. }
  929. G_debug(3, " parallel/collinear...");
  930. return -1;
  931. }
  932. #endif