buffer2.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*!
  2. \file lib/vector/Vlib/buffer2.c
  3. \brief Vector library - nearest, adjust, parallel lines
  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
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Original author Radim Blazek (see buffer.c)
  11. \author Rewritten by Rosen Matev (Google Summer of Code 2008)
  12. */
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. #include "dgraph.h"
  18. #define LENGTH(DX, DY) (sqrt((DX*DX)+(DY*DY)))
  19. #ifndef MIN
  20. #define MIN(X,Y) ((X<Y)?X:Y)
  21. #endif
  22. #ifndef MAX
  23. #define MAX(X,Y) ((X>Y)?X:Y)
  24. #endif
  25. #define PI M_PI
  26. #define RIGHT_SIDE 1
  27. #define LEFT_SIDE -1
  28. #define LOOPED_LINE 1
  29. #define NON_LOOPED_LINE 0
  30. /* norm_vector() calculates normalized vector form two points */
  31. static void norm_vector(double x1, double y1, double x2, double y2, double *x,
  32. double *y)
  33. {
  34. double dx, dy, l;
  35. dx = x2 - x1;
  36. dy = y2 - y1;
  37. if ((dx == 0) && (dy == 0)) {
  38. /* assume that dx == dy == 0, which should give (NaN,NaN) */
  39. /* without this, very small dx or dy could result in Infinity */
  40. *x = 0;
  41. *y = 0;
  42. return;
  43. }
  44. l = LENGTH(dx, dy);
  45. *x = dx / l;
  46. *y = dy / l;
  47. return;
  48. }
  49. static void rotate_vector(double x, double y, double cosa, double sina,
  50. double *nx, double *ny)
  51. {
  52. *nx = x * cosa - y * sina;
  53. *ny = x * sina + y * cosa;
  54. return;
  55. }
  56. /*
  57. * (x,y) shoud be normalized vector for common transforms; This func transforms (x,y) to a vector corresponding to da, db, dalpha params
  58. * dalpha is in radians
  59. */
  60. static void elliptic_transform(double x, double y, double da, double db,
  61. double dalpha, double *nx, double *ny)
  62. {
  63. double cosa = cos(dalpha);
  64. double sina = sin(dalpha);
  65. /* double cc = cosa*cosa;
  66. double ss = sina*sina;
  67. double t = (da-db)*sina*cosa;
  68. *nx = (da*cc + db*ss)*x + t*y;
  69. *ny = (da*ss + db*cc)*y + t*x;
  70. return; */
  71. double va, vb;
  72. va = (x * cosa + y * sina) * da;
  73. vb = (x * (-sina) + y * cosa) * db;
  74. *nx = va * cosa + vb * (-sina);
  75. *ny = va * sina + vb * cosa;
  76. return;
  77. }
  78. /*
  79. * vect(x,y) must be normalized
  80. * gives the tangent point of the tangent to ellpise(da,db,dalpha) parallel to vect(x,y)
  81. * dalpha is in radians
  82. * ellipse center is in (0,0)
  83. */
  84. static void elliptic_tangent(double x, double y, double da, double db,
  85. double dalpha, double *px, double *py)
  86. {
  87. double cosa = cos(dalpha);
  88. double sina = sin(dalpha);
  89. double u, v, len;
  90. /* rotate (x,y) -dalpha radians */
  91. rotate_vector(x, y, cosa, -sina, &x, &y);
  92. /*u = (x + da*y/db)/2;
  93. v = (y - db*x/da)/2; */
  94. u = da * da * y;
  95. v = -db * db * x;
  96. len = da * db / sqrt(da * da * v * v + db * db * u * u);
  97. u *= len;
  98. v *= len;
  99. rotate_vector(u, v, cosa, sina, px, py);
  100. return;
  101. }
  102. /*
  103. * !!! This is not line in GRASS' sense. See http://en.wikipedia.org/wiki/Line_%28mathematics%29
  104. */
  105. static void line_coefficients(double x1, double y1, double x2, double y2,
  106. double *a, double *b, double *c)
  107. {
  108. *a = y2 - y1;
  109. *b = x1 - x2;
  110. *c = x2 * y1 - x1 * y2;
  111. return;
  112. }
  113. /*
  114. * Finds intersection of two straight lines. Returns 0 if the lines are parallel, 1 if they cross,
  115. * 2 if they are the same line.
  116. * !!!!!!!!!!!!!!!! FIX THIS TOLLERANCE CONSTANTS BAD (and UGLY) CODE !!!!!!!!!
  117. */
  118. static int line_intersection(double a1, double b1, double c1, double a2,
  119. double b2, double c2, double *x, double *y)
  120. {
  121. double d;
  122. if (fabs(a2 * b1 - a1 * b2) == 0) {
  123. if (fabs(a2 * c1 - a1 * c2) == 0)
  124. return 2;
  125. else
  126. return 0;
  127. }
  128. else {
  129. d = a1 * b2 - a2 * b1;
  130. *x = (b1 * c2 - b2 * c1) / d;
  131. *y = (c1 * a2 - c2 * a1) / d;
  132. return 1;
  133. }
  134. }
  135. static double angular_tolerance(double tol, double da, double db)
  136. {
  137. double a = MAX(da, db);
  138. if (tol > a)
  139. tol = a;
  140. return 2 * acos(1 - tol / a);
  141. }
  142. /*
  143. * This function generates parallel line (with loops, but not like the old ones).
  144. * It is not to be used directly for creating buffers.
  145. * + added elliptical buffers/par.lines support
  146. *
  147. * dalpha - direction of elliptical buffer major axis in degrees
  148. * da - distance along major axis
  149. * db: distance along minor (perp.) axis
  150. * side: side >= 0 - right side, side < 0 - left side
  151. * when (da == db) we have plain distances (old case)
  152. * round - 1 for round corners, 0 for sharp corners. (tol is used only if round == 1)
  153. */
  154. static void parallel_line(struct line_pnts *Points, double da, double db,
  155. double dalpha, int side, int round, int caps, int looped,
  156. double tol, struct line_pnts *nPoints)
  157. {
  158. int i, j, res, np;
  159. double *x, *y;
  160. double tx, ty, vx, vy, wx, wy, nx, ny, mx, my, rx, ry;
  161. double vx1, vy1, wx1, wy1;
  162. double a0, b0, c0, a1, b1, c1;
  163. double phi1, phi2, delta_phi;
  164. double nsegments, angular_tol, angular_step;
  165. int inner_corner, turns360;
  166. G_debug(3, "parallel_line()");
  167. if (looped && 0) {
  168. /* start point != end point */
  169. return;
  170. }
  171. Vect_reset_line(nPoints);
  172. if (looped) {
  173. Vect_append_point(Points, Points->x[1], Points->y[1], Points->z[1]);
  174. }
  175. np = Points->n_points;
  176. x = Points->x;
  177. y = Points->y;
  178. if ((np == 0) || (np == 1))
  179. return;
  180. if ((da == 0) || (db == 0)) {
  181. Vect_copy_xyz_to_pnts(nPoints, x, y, NULL, np);
  182. return;
  183. }
  184. side = (side >= 0) ? (1) : (-1); /* normalize variable */
  185. dalpha *= PI / 180; /* convert dalpha from degrees to radians */
  186. angular_tol = angular_tolerance(tol, da, db);
  187. for (i = 0; i < np - 1; i++) {
  188. /* save the old values */
  189. a0 = a1;
  190. b0 = b1;
  191. c0 = c1;
  192. wx = vx;
  193. wy = vy;
  194. norm_vector(x[i], y[i], x[i + 1], y[i + 1], &tx, &ty);
  195. if ((tx == 0) && (ty == 0))
  196. continue;
  197. elliptic_tangent(side * tx, side * ty, da, db, dalpha, &vx, &vy);
  198. nx = x[i] + vx;
  199. ny = y[i] + vy;
  200. mx = x[i + 1] + vx;
  201. my = y[i + 1] + vy;
  202. line_coefficients(nx, ny, mx, my, &a1, &b1, &c1);
  203. if (i == 0) {
  204. if (!looped)
  205. Vect_append_point(nPoints, nx, ny, 0);
  206. continue;
  207. }
  208. delta_phi = atan2(ty, tx) - atan2(y[i] - y[i - 1], x[i] - x[i - 1]);
  209. if (delta_phi > PI)
  210. delta_phi -= 2 * PI;
  211. else if (delta_phi <= -PI)
  212. delta_phi += 2 * PI;
  213. /* now delta_phi is in [-pi;pi] */
  214. turns360 = (fabs(fabs(delta_phi) - PI) < 1e-15);
  215. inner_corner = (side * delta_phi <= 0) && (!turns360);
  216. if ((turns360) && (!(caps && round))) {
  217. if (caps) {
  218. norm_vector(0, 0, vx, vy, &tx, &ty);
  219. elliptic_tangent(side * tx, side * ty, da, db, dalpha, &tx,
  220. &ty);
  221. }
  222. else {
  223. tx = 0;
  224. ty = 0;
  225. }
  226. Vect_append_point(nPoints, x[i] + wx + tx, y[i] + wy + ty, 0);
  227. Vect_append_point(nPoints, nx + tx, ny + ty, 0); /* nx == x[i] + vx, ny == y[i] + vy */
  228. }
  229. else if ((!round) || inner_corner) {
  230. res = line_intersection(a0, b0, c0, a1, b1, c1, &rx, &ry);
  231. /* if (res == 0) {
  232. G_debug(4, "a0=%.18f, b0=%.18f, c0=%.18f, a1=%.18f, b1=%.18f, c1=%.18f", a0, b0, c0, a1, b1, c1);
  233. G_fatal_error("Two consequtive line segments are parallel, but not on one straight line! This should never happen.");
  234. return;
  235. } */
  236. if (res == 1) {
  237. if (!round)
  238. Vect_append_point(nPoints, rx, ry, 0);
  239. else {
  240. /* d = dig_distance2_point_to_line(rx, ry, 0, x[i-1], y[i-1], 0, x[i], y[i], 0,
  241. 0, NULL, NULL, NULL, NULL, NULL);
  242. if ( */
  243. Vect_append_point(nPoints, rx, ry, 0);
  244. }
  245. }
  246. }
  247. else {
  248. /* we should draw elliptical arc for outside corner */
  249. /* inverse transforms */
  250. elliptic_transform(wx, wy, 1 / da, 1 / db, dalpha, &wx1, &wy1);
  251. elliptic_transform(vx, vy, 1 / da, 1 / db, dalpha, &vx1, &vy1);
  252. phi1 = atan2(wy1, wx1);
  253. phi2 = atan2(vy1, vx1);
  254. delta_phi = side * (phi2 - phi1);
  255. /* make delta_phi in [0, 2pi] */
  256. if (delta_phi < 0)
  257. delta_phi += 2 * PI;
  258. nsegments = (int)(delta_phi / angular_tol) + 1;
  259. angular_step = side * (delta_phi / nsegments);
  260. for (j = 0; j <= nsegments; j++) {
  261. elliptic_transform(cos(phi1), sin(phi1), da, db, dalpha, &tx,
  262. &ty);
  263. Vect_append_point(nPoints, x[i] + tx, y[i] + ty, 0);
  264. phi1 += angular_step;
  265. }
  266. }
  267. if ((!looped) && (i == np - 2)) {
  268. Vect_append_point(nPoints, mx, my, 0);
  269. }
  270. }
  271. if (looped) {
  272. Vect_append_point(nPoints, nPoints->x[0], nPoints->y[0],
  273. nPoints->z[0]);
  274. }
  275. Vect_line_prune(nPoints);
  276. if (looped) {
  277. Vect_line_delete_point(Points, Points->n_points - 1);
  278. }
  279. }
  280. /* input line must be looped */
  281. static void convolution_line(struct line_pnts *Points, double da, double db,
  282. double dalpha, int side, int round, int caps,
  283. double tol, struct line_pnts *nPoints)
  284. {
  285. int i, j, res, np;
  286. double *x, *y;
  287. double tx, ty, vx, vy, wx, wy, nx, ny, mx, my, rx, ry;
  288. double vx1, vy1, wx1, wy1;
  289. double a0, b0, c0, a1, b1, c1;
  290. double phi1, phi2, delta_phi;
  291. double nsegments, angular_tol, angular_step;
  292. double angle0, angle1;
  293. int inner_corner, turns360;
  294. G_debug(3, "convolution_line() side = %d", side);
  295. np = Points->n_points;
  296. x = Points->x;
  297. y = Points->y;
  298. if ((np == 0) || (np == 1))
  299. return;
  300. if ((x[0] != x[np - 1]) || (y[0] != y[np - 1])) {
  301. G_fatal_error(_("Line is not looped"));
  302. return;
  303. }
  304. Vect_reset_line(nPoints);
  305. if ((da == 0) || (db == 0)) {
  306. Vect_copy_xyz_to_pnts(nPoints, x, y, NULL, np);
  307. return;
  308. }
  309. side = (side >= 0) ? (1) : (-1); /* normalize variable */
  310. dalpha *= PI / 180; /* convert dalpha from degrees to radians */
  311. angular_tol = angular_tolerance(tol, da, db);
  312. i = np - 2;
  313. norm_vector(x[i], y[i], x[i + 1], y[i + 1], &tx, &ty);
  314. elliptic_tangent(side * tx, side * ty, da, db, dalpha, &vx, &vy);
  315. angle1 = atan2(ty, tx);
  316. nx = x[i] + vx;
  317. ny = y[i] + vy;
  318. mx = x[i + 1] + vx;
  319. my = y[i + 1] + vy;
  320. if (!round)
  321. line_coefficients(nx, ny, mx, my, &a1, &b1, &c1);
  322. for (i = 0; i <= np - 2; i++) {
  323. G_debug(4, "point %d, segment %d-%d", i, i, i + 1);
  324. /* save the old values */
  325. if (!round) {
  326. a0 = a1;
  327. b0 = b1;
  328. c0 = c1;
  329. }
  330. wx = vx;
  331. wy = vy;
  332. angle0 = angle1;
  333. norm_vector(x[i], y[i], x[i + 1], y[i + 1], &tx, &ty);
  334. if ((tx == 0) && (ty == 0))
  335. continue;
  336. elliptic_tangent(side * tx, side * ty, da, db, dalpha, &vx, &vy);
  337. angle1 = atan2(ty, tx);
  338. nx = x[i] + vx;
  339. ny = y[i] + vy;
  340. mx = x[i + 1] + vx;
  341. my = y[i + 1] + vy;
  342. if (!round)
  343. line_coefficients(nx, ny, mx, my, &a1, &b1, &c1);
  344. delta_phi = angle1 - angle0;
  345. if (delta_phi > PI)
  346. delta_phi -= 2 * PI;
  347. else if (delta_phi <= -PI)
  348. delta_phi += 2 * PI;
  349. /* now delta_phi is in [-pi;pi] */
  350. turns360 = (fabs(fabs(delta_phi) - PI) < 1e-15);
  351. inner_corner = (side * delta_phi <= 0) && (!turns360);
  352. /* if <line turns 360> and (<caps> and <not round>) */
  353. if (turns360 && caps && (!round)) {
  354. norm_vector(0, 0, vx, vy, &tx, &ty);
  355. elliptic_tangent(side * tx, side * ty, da, db, dalpha, &tx, &ty);
  356. Vect_append_point(nPoints, x[i] + wx + tx, y[i] + wy + ty, 0);
  357. G_debug(4, " append point (c) x=%.16f y=%.16f", x[i] + wx + tx,
  358. y[i] + wy + ty);
  359. Vect_append_point(nPoints, nx + tx, ny + ty, 0); /* nx == x[i] + vx, ny == y[i] + vy */
  360. G_debug(4, " append point (c) x=%.16f y=%.16f", nx + tx, ny + ty);
  361. }
  362. if ((!turns360) && (!round) && (!inner_corner)) {
  363. res = line_intersection(a0, b0, c0, a1, b1, c1, &rx, &ry);
  364. if (res == 1) {
  365. Vect_append_point(nPoints, rx, ry, 0);
  366. G_debug(4, " append point (o) x=%.16f y=%.16f", rx, ry);
  367. }
  368. else if (res == 2) {
  369. /* no need to append point in this case */
  370. }
  371. else
  372. G_fatal_error(_("Unexpected result of line_intersection() res = %d"),
  373. res);
  374. }
  375. if (round && (!inner_corner) && (!turns360 || caps)) {
  376. /* we should draw elliptical arc for outside corner */
  377. /* inverse transforms */
  378. elliptic_transform(wx, wy, 1 / da, 1 / db, dalpha, &wx1, &wy1);
  379. elliptic_transform(vx, vy, 1 / da, 1 / db, dalpha, &vx1, &vy1);
  380. phi1 = atan2(wy1, wx1);
  381. phi2 = atan2(vy1, vx1);
  382. delta_phi = side * (phi2 - phi1);
  383. /* make delta_phi in [0, 2pi] */
  384. if (delta_phi < 0)
  385. delta_phi += 2 * PI;
  386. nsegments = (int)(delta_phi / angular_tol) + 1;
  387. angular_step = side * (delta_phi / nsegments);
  388. phi1 += angular_step;
  389. for (j = 1; j <= nsegments - 1; j++) {
  390. elliptic_transform(cos(phi1), sin(phi1), da, db, dalpha, &tx,
  391. &ty);
  392. Vect_append_point(nPoints, x[i] + tx, y[i] + ty, 0);
  393. G_debug(4, " append point (r) x=%.16f y=%.16f", x[i] + tx,
  394. y[i] + ty);
  395. phi1 += angular_step;
  396. }
  397. }
  398. Vect_append_point(nPoints, nx, ny, 0);
  399. G_debug(4, " append point (s) x=%.16f y=%.16f", nx, ny);
  400. Vect_append_point(nPoints, mx, my, 0);
  401. G_debug(4, " append point (s) x=%.16f y=%.16f", mx, my);
  402. }
  403. /* close the output line */
  404. Vect_append_point(nPoints, nPoints->x[0], nPoints->y[0], nPoints->z[0]);
  405. /* Vect_line_prune ( nPoints ); */
  406. }
  407. /*
  408. * side: side >= 0 - extracts contour on right side of edge, side < 0 - extracts contour on left side of edge
  409. * if the extracted contour is the outer contour, it is returned in ccw order
  410. * else if it is inner contour, it is returned in cw order
  411. */
  412. static void extract_contour(struct planar_graph *pg, struct pg_edge *first,
  413. int side, int winding, int stop_at_line_end,
  414. struct line_pnts *nPoints)
  415. {
  416. int j;
  417. int v; /* current vertex number */
  418. int v0;
  419. int eside; /* side of the current edge */
  420. double eangle; /* current edge angle with Ox (according to the current direction) */
  421. struct pg_vertex *vert; /* current vertex */
  422. struct pg_vertex *vert0; /* last vertex */
  423. struct pg_edge *edge; /* current edge; must be edge of vert */
  424. /* int cs; *//* on which side are we turning along the contour */
  425. /* we will always turn right and dont need that one */
  426. double opt_angle, tangle;
  427. int opt_j, opt_side, opt_flag;
  428. G_debug(3, "extract_contour(): v1=%d, v2=%d, side=%d, stop_at_line_end=%d",
  429. first->v1, first->v2, side, stop_at_line_end);
  430. Vect_reset_line(nPoints);
  431. edge = first;
  432. if (side >= 0) {
  433. eside = 1;
  434. v0 = edge->v1;
  435. v = edge->v2;
  436. }
  437. else {
  438. eside = -1;
  439. v0 = edge->v2;
  440. v = edge->v1;
  441. }
  442. vert0 = &(pg->v[v0]);
  443. vert = &(pg->v[v]);
  444. eangle = atan2(vert->y - vert0->y, vert->x - vert0->x);
  445. while (1) {
  446. Vect_append_point(nPoints, vert0->x, vert0->y, 0);
  447. G_debug(4, "ec: v0=%d, v=%d, eside=%d, edge->v1=%d, edge->v2=%d", v0,
  448. v, eside, edge->v1, edge->v2);
  449. G_debug(4, "ec: append point x=%.18f y=%.18f", vert0->x, vert0->y);
  450. /* mark current edge as visited on the appropriate side */
  451. if (eside == 1) {
  452. edge->visited_right = 1;
  453. edge->winding_right = winding;
  454. }
  455. else {
  456. edge->visited_left = 1;
  457. edge->winding_left = winding;
  458. }
  459. opt_flag = 1;
  460. for (j = 0; j < vert->ecount; j++) {
  461. /* exclude current edge */
  462. if (vert->edges[j] != edge) {
  463. tangle = vert->angles[j] - eangle;
  464. if (tangle < -PI)
  465. tangle += 2 * PI;
  466. else if (tangle > PI)
  467. tangle -= 2 * PI;
  468. /* now tangle is in (-PI, PI) */
  469. if (opt_flag || (tangle < opt_angle)) {
  470. opt_j = j;
  471. opt_side = (vert->edges[j]->v1 == v) ? (1) : (-1);
  472. opt_angle = tangle;
  473. opt_flag = 0;
  474. }
  475. }
  476. }
  477. // G_debug(4, "ec: opt: side=%d opt_flag=%d opt_angle=%.18f opt_j=%d opt_step=%d", side, opt_flag, opt_angle, opt_j, opt_step);
  478. /* if line end is reached (no other edges at curr vertex) */
  479. if (opt_flag) {
  480. if (stop_at_line_end) {
  481. G_debug(3, " end has been reached, will stop here");
  482. break;
  483. }
  484. else {
  485. opt_j = 0; /* the only edge of vert is vert->edges[0] */
  486. opt_side = -eside; /* go to the other side of the current edge */
  487. G_debug(3, " end has been reached, turning around");
  488. }
  489. }
  490. /* break condition */
  491. if ((vert->edges[opt_j] == first) && (opt_side == side))
  492. break;
  493. if (opt_side == 1) {
  494. if (vert->edges[opt_j]->visited_right) {
  495. G_warning(_("Next edge was visited but it is not the first one !!! breaking loop"));
  496. G_debug(4,
  497. "ec: v0=%d, v=%d, eside=%d, edge->v1=%d, edge->v2=%d",
  498. v, (edge->v1 == v) ? (edge->v2) : (edge->v1),
  499. opt_side, vert->edges[opt_j]->v1,
  500. vert->edges[opt_j]->v2);
  501. break;
  502. }
  503. }
  504. else {
  505. if (vert->edges[opt_j]->visited_left) {
  506. G_warning(_("Next edge was visited but it is not the first one !!! breaking loop"));
  507. G_debug(4,
  508. "ec: v0=%d, v=%d, eside=%d, edge->v1=%d, edge->v2=%d",
  509. v, (edge->v1 == v) ? (edge->v2) : (edge->v1),
  510. opt_side, vert->edges[opt_j]->v1,
  511. vert->edges[opt_j]->v2);
  512. break;
  513. }
  514. }
  515. edge = vert->edges[opt_j];
  516. eside = opt_side;
  517. v0 = v;
  518. v = (edge->v1 == v) ? (edge->v2) : (edge->v1);
  519. vert0 = vert;
  520. vert = &(pg->v[v]);
  521. eangle = vert0->angles[opt_j];
  522. }
  523. Vect_append_point(nPoints, vert->x, vert->y, 0);
  524. G_debug(4, "ec: append point x=%.18f y=%.18f", vert->x, vert->y);
  525. return;
  526. }
  527. /*
  528. * This function extracts the outer contour of a (self crossing) line.
  529. * It can generate left/right contour if none of the line ends are in a loop.
  530. * If one or both of them is in a loop, then there's only one contour
  531. *
  532. * side: side > 0 - right contour, side < 0 - left contour, side = 0 - outer contour
  533. * if side != 0 and there's only one contour, the function returns it
  534. *
  535. * TODO: Implement side != 0 feature;
  536. */
  537. static void extract_outer_contour(struct planar_graph *pg, int side,
  538. struct line_pnts *nPoints)
  539. {
  540. int i;
  541. int flag;
  542. int v;
  543. struct pg_vertex *vert;
  544. struct pg_edge *edge;
  545. double min_x, min_angle;
  546. G_debug(3, "extract_outer_contour()");
  547. if (side != 0) {
  548. G_fatal_error(_("side != 0 feature not implemented"));
  549. return;
  550. }
  551. /* find a line segment which is on the outer contour */
  552. flag = 1;
  553. for (i = 0; i < pg->vcount; i++) {
  554. if (flag || (pg->v[i].x < min_x)) {
  555. v = i;
  556. min_x = pg->v[i].x;
  557. flag = 0;
  558. }
  559. }
  560. vert = &(pg->v[v]);
  561. flag = 1;
  562. for (i = 0; i < vert->ecount; i++) {
  563. if (flag || (vert->angles[i] < min_angle)) {
  564. edge = vert->edges[i];
  565. min_angle = vert->angles[i];
  566. flag = 0;
  567. }
  568. }
  569. /* the winding on the outer contour is 0 */
  570. extract_contour(pg, edge, (edge->v1 == v) ? RIGHT_SIDE : LEFT_SIDE, 0, 0,
  571. nPoints);
  572. return;
  573. }
  574. /*
  575. * Extracts contours which are not visited.
  576. * IMPORTANT: the outer contour must be visited (you should call extract_outer_contour() to do that),
  577. * so that extract_inner_contour() doesn't return it
  578. *
  579. * returns: 0 when there are no more inner contours; otherwise, 1
  580. */
  581. static int extract_inner_contour(struct planar_graph *pg, int *winding,
  582. struct line_pnts *nPoints)
  583. {
  584. int i, w;
  585. struct pg_edge *edge;
  586. G_debug(3, "extract_inner_contour()");
  587. for (i = 0; i < pg->ecount; i++) {
  588. edge = &(pg->e[i]);
  589. if (edge->visited_left) {
  590. if (!(pg->e[i].visited_right)) {
  591. w = edge->winding_left - 1;
  592. extract_contour(pg, &(pg->e[i]), RIGHT_SIDE, w, 0, nPoints);
  593. *winding = w;
  594. return 1;
  595. }
  596. }
  597. else {
  598. if (pg->e[i].visited_right) {
  599. w = edge->winding_right + 1;
  600. extract_contour(pg, &(pg->e[i]), LEFT_SIDE, w, 0, nPoints);
  601. *winding = w;
  602. return 1;
  603. }
  604. }
  605. }
  606. return 0;
  607. }
  608. /* point_in_buf - test if point px,py is in d buffer of Points
  609. ** dalpha is in degrees
  610. ** returns: 1 in buffer
  611. ** 0 not in buffer
  612. */
  613. static int point_in_buf(struct line_pnts *Points, double px, double py, double da,
  614. double db, double dalpha)
  615. {
  616. int i, np;
  617. double cx, cy;
  618. double delta, delta_k, k;
  619. double vx, vy, wx, wy, mx, my, nx, ny;
  620. double len, tx, ty, d, da2;
  621. G_debug(3, "point_in_buf()");
  622. dalpha *= PI / 180; /* convert dalpha from degrees to radians */
  623. np = Points->n_points;
  624. da2 = da * da;
  625. for (i = 0; i < np - 1; i++) {
  626. vx = Points->x[i];
  627. vy = Points->y[i];
  628. wx = Points->x[i + 1];
  629. wy = Points->y[i + 1];
  630. if (da != db) {
  631. mx = wx - vx;
  632. my = wy - vy;
  633. len = LENGTH(mx, my);
  634. elliptic_tangent(mx / len, my / len, da, db, dalpha, &cx, &cy);
  635. delta = mx * cy - my * cx;
  636. delta_k = (px - vx) * cy - (py - vy) * cx;
  637. k = delta_k / delta;
  638. /* G_debug(4, "k = %g, k1 = %g", k, (mx * (px - vx) + my * (py - vy)) / (mx * mx + my * my)); */
  639. if (k <= 0) {
  640. nx = vx;
  641. ny = vy;
  642. }
  643. else if (k >= 1) {
  644. nx = wx;
  645. ny = wy;
  646. }
  647. else {
  648. nx = vx + k * mx;
  649. ny = vy + k * my;
  650. }
  651. /* inverse transform */
  652. elliptic_transform(px - nx, py - ny, 1 / da, 1 / db, dalpha, &tx,
  653. &ty);
  654. d = dig_distance2_point_to_line(nx + tx, ny + ty, 0, vx, vy, 0,
  655. wx, wy, 0, 0, NULL, NULL, NULL,
  656. NULL, NULL);
  657. /* G_debug(4, "sqrt(d)*da = %g, len' = %g, olen = %g", sqrt(d)*da, da*LENGTH(tx,ty), LENGTH((px-nx),(py-ny))); */
  658. if (d <= 1) {
  659. //G_debug(1, "d=%g", d);
  660. return 1;
  661. }
  662. }
  663. else {
  664. d = dig_distance2_point_to_line(px, py, 0, vx, vy, 0, wx, wy, 0,
  665. 0, NULL, NULL, NULL, NULL, NULL);
  666. /* G_debug(4, "sqrt(d) = %g", sqrt(d)); */
  667. if (d <= da2) {
  668. return 1;
  669. }
  670. }
  671. }
  672. return 0;
  673. }
  674. /* returns 0 for ccw, non-zero for cw
  675. */
  676. static int get_polygon_orientation(const double *x, const double *y, int n)
  677. {
  678. double x1, y1, x2, y2;
  679. double area;
  680. x2 = x[n - 1];
  681. y2 = y[n - 1];
  682. area = 0;
  683. while (--n >= 0) {
  684. x1 = x2;
  685. y1 = y2;
  686. x2 = *x++;
  687. y2 = *y++;
  688. area += (y2 + y1) * (x2 - x1);
  689. }
  690. return (area > 0);
  691. }
  692. /* internal */
  693. static void add_line_to_array(struct line_pnts *Points,
  694. struct line_pnts ***arrPoints, int *count,
  695. int *allocated, int more)
  696. {
  697. if (*allocated == *count) {
  698. *allocated += more;
  699. *arrPoints =
  700. G_realloc(*arrPoints, (*allocated) * sizeof(struct line_pnts *));
  701. }
  702. (*arrPoints)[*count] = Points;
  703. (*count)++;
  704. return;
  705. }
  706. static void destroy_lines_array(struct line_pnts **arr, int count)
  707. {
  708. int i;
  709. for (i = 0; i < count; i++)
  710. Vect_destroy_line_struct(arr[i]);
  711. G_free(arr);
  712. }
  713. /* area_outer and area_isles[i] must be closed non self-intersecting lines
  714. side: 0 - auto, 1 - right, -1 left
  715. */
  716. static void buffer_lines(struct line_pnts *area_outer, struct line_pnts **area_isles,
  717. int isles_count, int side, double da, double db,
  718. double dalpha, int round, int caps, double tol,
  719. struct line_pnts **oPoints, struct line_pnts ***iPoints,
  720. int *inner_count)
  721. {
  722. struct planar_graph *pg2;
  723. struct line_pnts *sPoints, *cPoints;
  724. struct line_pnts **arrPoints;
  725. int i, count = 0;
  726. int res, winding;
  727. int auto_side;
  728. int more = 8;
  729. int allocated = 0;
  730. double px, py;
  731. G_debug(3, "buffer_lines()");
  732. auto_side = (side == 0);
  733. /* initializations */
  734. sPoints = Vect_new_line_struct();
  735. cPoints = Vect_new_line_struct();
  736. arrPoints = NULL;
  737. /* outer contour */
  738. G_debug(3, " processing outer contour");
  739. *oPoints = Vect_new_line_struct();
  740. if (auto_side)
  741. side =
  742. get_polygon_orientation(area_outer->x, area_outer->y,
  743. area_outer->n_points -
  744. 1) ? LEFT_SIDE : RIGHT_SIDE;
  745. convolution_line(area_outer, da, db, dalpha, side, round, caps, tol,
  746. sPoints);
  747. pg2 = pg_create(sPoints);
  748. extract_outer_contour(pg2, 0, *oPoints);
  749. res = extract_inner_contour(pg2, &winding, cPoints);
  750. while (res != 0) {
  751. if (winding == 0) {
  752. if (!Vect_point_in_poly(cPoints->x[0], cPoints->y[0], area_outer)) {
  753. if (Vect_get_point_in_poly(cPoints, &px, &py) != 0)
  754. G_fatal_error(_("Vect_get_point_in_poly() failed"));
  755. if (!point_in_buf(area_outer, px, py, da, db, dalpha)) {
  756. add_line_to_array(cPoints, &arrPoints, &count, &allocated,
  757. more);
  758. cPoints = Vect_new_line_struct();
  759. }
  760. }
  761. }
  762. res = extract_inner_contour(pg2, &winding, cPoints);
  763. }
  764. pg_destroy_struct(pg2);
  765. /* inner contours */
  766. G_debug(3, " processing inner contours");
  767. for (i = 0; i < isles_count; i++) {
  768. if (auto_side)
  769. side =
  770. get_polygon_orientation(area_isles[i]->x, area_isles[i]->y,
  771. area_isles[i]->n_points -
  772. 1) ? RIGHT_SIDE : LEFT_SIDE;
  773. convolution_line(area_isles[i], da, db, dalpha, side, round, caps,
  774. tol, sPoints);
  775. pg2 = pg_create(sPoints);
  776. extract_outer_contour(pg2, 0, cPoints);
  777. res = extract_inner_contour(pg2, &winding, cPoints);
  778. while (res != 0) {
  779. if (winding == -1) {
  780. /* we need to check if the area is in the buffer.
  781. I've simplfied convolution_line(), so that it runs faster,
  782. however that leads to ocasional problems */
  783. if (Vect_point_in_poly
  784. (cPoints->x[0], cPoints->y[0], area_isles[i])) {
  785. if (Vect_get_point_in_poly(cPoints, &px, &py) != 0)
  786. G_fatal_error(_("Vect_get_point_in_poly() failed"));
  787. if (!point_in_buf(area_isles[i], px, py, da, db, dalpha)) {
  788. add_line_to_array(cPoints, &arrPoints, &count,
  789. &allocated, more);
  790. cPoints = Vect_new_line_struct();
  791. }
  792. }
  793. }
  794. res = extract_inner_contour(pg2, &winding, cPoints);
  795. }
  796. pg_destroy_struct(pg2);
  797. }
  798. arrPoints = G_realloc(arrPoints, count * sizeof(struct line_pnts *));
  799. *inner_count = count;
  800. *iPoints = arrPoints;
  801. Vect_destroy_line_struct(sPoints);
  802. Vect_destroy_line_struct(cPoints);
  803. G_debug(3, "buffer_lines() ... done");
  804. return;
  805. }
  806. /*!
  807. \brief Creates buffer around line.
  808. See also Vect_line_buffer().
  809. \param InPoints input line geometry
  810. \param da distance along major axis
  811. \param db distance along minor axis
  812. \param dalpha angle between 0x and major axis
  813. \param round make corners round
  814. \param caps add caps at line ends
  815. \param tol maximum distance between theoretical arc and output segments
  816. \param[out] oPoints output polygon outer border (ccw order)
  817. \param[out] inner_count number of holes
  818. \param[out] iPoints array of output polygon's holes (cw order)
  819. */
  820. void Vect_line_buffer2(const struct line_pnts *Points, double da, double db,
  821. double dalpha, int round, int caps, double tol,
  822. struct line_pnts **oPoints,
  823. struct line_pnts ***iPoints, int *inner_count)
  824. {
  825. struct planar_graph *pg;
  826. struct line_pnts *tPoints, *outer;
  827. struct line_pnts **isles;
  828. int isles_count = 0;
  829. int res, winding;
  830. int more = 8;
  831. int isles_allocated = 0;
  832. G_debug(2, "Vect_line_buffer()");
  833. /* initializations */
  834. tPoints = Vect_new_line_struct();
  835. isles = NULL;
  836. pg = pg_create(Points);
  837. /* outer contour */
  838. outer = Vect_new_line_struct();
  839. extract_outer_contour(pg, 0, outer);
  840. /* inner contours */
  841. res = extract_inner_contour(pg, &winding, tPoints);
  842. while (res != 0) {
  843. add_line_to_array(tPoints, &isles, &isles_count, &isles_allocated,
  844. more);
  845. tPoints = Vect_new_line_struct();
  846. res = extract_inner_contour(pg, &winding, tPoints);
  847. }
  848. buffer_lines(outer, isles, isles_count, RIGHT_SIDE, da, db, dalpha, round,
  849. caps, tol, oPoints, iPoints, inner_count);
  850. Vect_destroy_line_struct(tPoints);
  851. Vect_destroy_line_struct(outer);
  852. destroy_lines_array(isles, isles_count);
  853. pg_destroy_struct(pg);
  854. return;
  855. }
  856. /*!
  857. \brief Creates buffer around area.
  858. \param Map vector map
  859. \param area area id
  860. \param da distance along major axis
  861. \param db distance along minor axis
  862. \param dalpha angle between 0x and major axis
  863. \param round make corners round
  864. \param caps add caps at line ends
  865. \param tol maximum distance between theoretical arc and output segments
  866. \param[out] oPoints output polygon outer border (ccw order)
  867. \param[out] inner_count number of holes
  868. \param[out] iPoints array of output polygon's holes (cw order)
  869. */
  870. void Vect_area_buffer2(const struct Map_info *Map, int area, double da, double db,
  871. double dalpha, int round, int caps, double tol,
  872. struct line_pnts **oPoints,
  873. struct line_pnts ***iPoints, int *inner_count)
  874. {
  875. struct line_pnts *tPoints, *outer;
  876. struct line_pnts **isles;
  877. int isles_count = 0;
  878. int i, isle;
  879. int more = 8;
  880. int isles_allocated = 0;
  881. G_debug(2, "Vect_area_buffer()");
  882. /* initializations */
  883. tPoints = Vect_new_line_struct();
  884. isles_count = Vect_get_area_num_isles(Map, area);
  885. isles_allocated = isles_count;
  886. isles = G_malloc(isles_allocated * sizeof(struct line_pnts *));
  887. /* outer contour */
  888. outer = Vect_new_line_struct();
  889. Vect_get_area_points(Map, area, outer);
  890. Vect_append_point(outer, outer->x[0], outer->y[0], outer->z[0]);
  891. /* inner contours */
  892. for (i = 0; i < isles_count; i++) {
  893. isle = Vect_get_area_isle(Map, area, i);
  894. Vect_get_isle_points(Map, isle, tPoints);
  895. /* Check if the isle is big enough */
  896. /*
  897. if (Vect_line_length(tPoints) < 2*PI*max)
  898. continue;
  899. */
  900. Vect_append_point(tPoints, tPoints->x[0], tPoints->y[0],
  901. tPoints->z[0]);
  902. add_line_to_array(tPoints, &isles, &isles_count, &isles_allocated,
  903. more);
  904. tPoints = Vect_new_line_struct();
  905. }
  906. buffer_lines(outer, isles, isles_count, 0, da, db, dalpha, round, caps,
  907. tol, oPoints, iPoints, inner_count);
  908. Vect_destroy_line_struct(tPoints);
  909. Vect_destroy_line_struct(outer);
  910. destroy_lines_array(isles, isles_count);
  911. return;
  912. }
  913. /*!
  914. \brief Creates buffer around the point (px, py).
  915. \param px input point x-coordinate
  916. \param py input point y-coordinate
  917. \param da distance along major axis
  918. \param da distance along minor axis
  919. \param dalpha angle between 0x and major axis
  920. \param round make corners round
  921. \param tol maximum distance between theoretical arc and output segments
  922. \param[out] nPoints output polygon outer border (ccw order)
  923. */
  924. void Vect_point_buffer2(double px, double py, double da, double db,
  925. double dalpha, int round, double tol,
  926. struct line_pnts **oPoints)
  927. {
  928. double tx, ty;
  929. double angular_tol, angular_step, phi1;
  930. int j, nsegments;
  931. G_debug(2, "Vect_point_buffer()");
  932. *oPoints = Vect_new_line_struct();
  933. dalpha *= PI / 180; /* convert dalpha from degrees to radians */
  934. if (round || (!round)) {
  935. angular_tol = angular_tolerance(tol, da, db);
  936. nsegments = (int)(2 * PI / angular_tol) + 1;
  937. angular_step = 2 * PI / nsegments;
  938. phi1 = 0;
  939. for (j = 0; j < nsegments; j++) {
  940. elliptic_transform(cos(phi1), sin(phi1), da, db, dalpha, &tx,
  941. &ty);
  942. Vect_append_point(*oPoints, px + tx, py + ty, 0);
  943. phi1 += angular_step;
  944. }
  945. }
  946. else {
  947. }
  948. /* close the output line */
  949. Vect_append_point(*oPoints, (*oPoints)->x[0], (*oPoints)->y[0],
  950. (*oPoints)->z[0]);
  951. return;
  952. }
  953. /*
  954. \brief Create parrallel line
  955. See also Vect_line_parallel().
  956. \param InPoints input line geometry
  957. \param da distance along major axis
  958. \param da distance along minor axis
  959. \param dalpha angle between 0x and major axis
  960. \param round make corners round
  961. \param tol maximum distance between theoretical arc and output segments
  962. \param[out] OutPoints output line
  963. */
  964. void Vect_line_parallel2(struct line_pnts *InPoints, double da, double db,
  965. double dalpha, int side, int round, double tol,
  966. struct line_pnts *OutPoints)
  967. {
  968. G_debug(2, "Vect_line_parallel(): npoints = %d, da = %f, "
  969. "db = %f, dalpha = %f, side = %d, round_corners = %d, tol = %f",
  970. InPoints->n_points, da, db, dalpha, side, round, tol);
  971. parallel_line(InPoints, da, db, dalpha, side, round, 1, NON_LOOPED_LINE,
  972. tol, OutPoints);
  973. /* if (!loops)
  974. clean_parallel(OutPoints, InPoints, distance, rm_end);
  975. */
  976. return;
  977. }