buffer2.c 31 KB

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