buffer2.c 32 KB

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