draw2.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  1. #include <math.h>
  2. #include <string.h>
  3. #include <grass/gis.h>
  4. #include <grass/raster.h>
  5. #include <grass/display.h>
  6. #include <grass/glocale.h>
  7. struct rectangle
  8. {
  9. double left;
  10. double rite;
  11. double bot;
  12. double top;
  13. };
  14. struct vector
  15. {
  16. double x, y;
  17. };
  18. struct plane
  19. {
  20. double x, y, k;
  21. };
  22. static struct vector cur;
  23. static struct rectangle clip;
  24. static struct plane pl_left = { -1, 0, 0 };
  25. static struct plane pl_rite = { 1, 0, 0 };
  26. static struct plane pl_bot = { 0, -1, 0 };
  27. static struct plane pl_top = { 0, 1, 0 };
  28. static int window_set;
  29. #define min(x,y) ((x) < (y) ? (x) : (y))
  30. #define max(x,y) ((x) > (y) ? (x) : (y))
  31. static double *xi, *yi;
  32. static int nalloc_i;
  33. static double *xf, *yf;
  34. static int nalloc_f;
  35. struct functions {
  36. void (*line)(double x1, double y1, double x2, double y2);
  37. void (*polydots)(const double *x, const double *y, int n);
  38. void (*polyline)(const double *x, const double *y, int n);
  39. void (*polygon)(const double *x, const double *y, int n);
  40. void (*box)(double x1, double y1, double x2, double y2);
  41. };
  42. static struct functions raw_functions, cull_functions, clip_functions;
  43. static struct functions *fns = &cull_functions;
  44. /******************************************************************************/
  45. static void alloc_dst(int n)
  46. {
  47. if (nalloc_i >= n)
  48. return;
  49. nalloc_i = n;
  50. xi = G_realloc(xi, nalloc_i * sizeof(double));
  51. yi = G_realloc(yi, nalloc_i * sizeof(double));
  52. }
  53. static void alloc_src(int n)
  54. {
  55. if (nalloc_f >= n)
  56. return;
  57. nalloc_f = n + 10;
  58. xf = G_realloc(xf, nalloc_f * sizeof(double));
  59. yf = G_realloc(yf, nalloc_f * sizeof(double));
  60. }
  61. static void dealloc_src(const double **x, const double **y, int release)
  62. {
  63. if (release) {
  64. G_free(*(double **)x);
  65. G_free(*(double **)y);
  66. }
  67. *x = xf;
  68. *y = yf;
  69. nalloc_f = 0;
  70. xf = NULL;
  71. yf = NULL;
  72. }
  73. static int do_reduce(double *x, double *y, int n)
  74. {
  75. static double eps = 0.5;
  76. int i, j;
  77. for (i = 0, j = 1; j < n; j++) {
  78. if (fabs(x[j] - x[i]) < eps && fabs(y[j] - y[i]) < eps)
  79. continue;
  80. i++;
  81. if (i == j)
  82. continue;
  83. x[i] = x[j];
  84. y[i] = y[j];
  85. }
  86. return i + 1;
  87. }
  88. static int do_convert(const double *x, const double *y, int n)
  89. {
  90. int i;
  91. alloc_dst(n);
  92. for (i = 0; i < n; i++) {
  93. xi[i] = D_u_to_d_col(x[i]);
  94. yi[i] = D_u_to_d_row(y[i]);
  95. }
  96. return do_reduce(xi, yi, n);
  97. }
  98. static void rel_to_abs(const double **px, const double **py, int n)
  99. {
  100. const double *x = *px;
  101. const double *y = *py;
  102. int i;
  103. alloc_src(n);
  104. xf[0] = cur.x + x[0];
  105. yf[0] = cur.y + y[0];
  106. for (i = 1; i < n; i++) {
  107. xf[i] = xf[i-1] + x[i];
  108. yf[i] = yf[i-1] + y[i];
  109. }
  110. dealloc_src(px, py, 0);
  111. }
  112. static double dist_plane(double x, double y, const struct plane *p)
  113. {
  114. return x * p->x + y * p->y + p->k;
  115. }
  116. static int shift_count(double dx)
  117. {
  118. return (int)floor(dx / 360);
  119. }
  120. static double shift_angle(double dx)
  121. {
  122. return shift_count(dx) * 360;
  123. }
  124. static double coerce(double x)
  125. {
  126. x += 180;
  127. x -= shift_angle(x);
  128. x -= 180;
  129. return x;
  130. }
  131. static int euclidify(double *x, const double *y, int n, int no_pole)
  132. {
  133. double ux0 = clip.left;
  134. double ux1 = clip.rite;
  135. double x0, x1;
  136. int lo, hi, count;
  137. int i;
  138. x0 = x1 = x[0];
  139. for (i = 1; i < n; i++) {
  140. if (fabs(y[i]) < 89.9)
  141. x[i] = x[i - 1] + coerce(x[i] - x[i - 1]);
  142. x0 = min(x0, x[i]);
  143. x1 = max(x1, x[i]);
  144. }
  145. if (no_pole && fabs(x[n - 1] - x[0]) > 180)
  146. return 0;
  147. lo = -shift_count(ux1 - x0);
  148. hi = shift_count(x1 - ux0);
  149. count = hi - lo + 1;
  150. for (i = 0; i < n; i++)
  151. x[i] -= lo * 360;
  152. return count;
  153. }
  154. static void do_ll_wrap(const double *x, const double *y, int n,
  155. void (*func) (const double *, const double *, int))
  156. {
  157. double *xx = G_malloc(n * sizeof(double));
  158. int count, i;
  159. memcpy(xx, x, n * sizeof(double));
  160. count = euclidify(xx, y, n, 0);
  161. for (i = 0; i < count; i++) {
  162. int j;
  163. (*func) (xx, y, n);
  164. for (j = 0; j < n; j++)
  165. xx[j] -= 360;
  166. }
  167. G_free(xx);
  168. }
  169. /******************************************************************************/
  170. static double interpolate(double a, double b, double ka, double kb)
  171. {
  172. return (a * kb - b * ka) / (kb - ka);
  173. }
  174. static int clip_plane(struct vector *a, struct vector *b,
  175. const struct plane *p, int *clipped)
  176. {
  177. double ka = dist_plane(a->x, a->y, p);
  178. double kb = dist_plane(b->x, b->y, p);
  179. double kab;
  180. /* both outside */
  181. if (ka > 0 && kb > 0)
  182. return 1;
  183. /* both inside */
  184. if (ka <= 0 && kb <= 0)
  185. return 0;
  186. *clipped = 1;
  187. /* a outside - swap a and b */
  188. if (ka >= 0) {
  189. struct vector *t;
  190. double kt;
  191. t = a;
  192. a = b;
  193. b = t;
  194. kt = ka;
  195. ka = kb;
  196. kb = kt;
  197. }
  198. kab = kb - ka;
  199. b->x = interpolate(a->x, b->x, ka, kb);
  200. b->y = interpolate(a->y, b->y, ka, kb);
  201. return 0;
  202. }
  203. static int do_clip(struct vector *a, struct vector *b)
  204. {
  205. int clipped = 0;
  206. if (a->x < clip.left && b->x < clip.left)
  207. return -1;
  208. if (a->x > clip.rite && b->x > clip.rite)
  209. return -1;
  210. if (a->y < clip.bot && b->y < clip.bot)
  211. return -1;
  212. if (a->y > clip.top && b->y > clip.top)
  213. return -1;
  214. if (clip_plane(a, b, &pl_left, &clipped))
  215. return -1;
  216. if (clip_plane(a, b, &pl_rite, &clipped))
  217. return -1;
  218. if (clip_plane(a, b, &pl_bot, &clipped))
  219. return -1;
  220. if (clip_plane(a, b, &pl_top, &clipped))
  221. return -1;
  222. return clipped;
  223. }
  224. static int line_clip(double x1, double y1, double x2, double y2)
  225. {
  226. struct vector a, b;
  227. int clipped;
  228. a.x = x1;
  229. a.y = y1;
  230. b.x = x2;
  231. b.y = y2;
  232. clipped = do_clip(&a, &b);
  233. if (clipped >= 0) {
  234. double x1 = D_u_to_d_col(a.x);
  235. double y1 = D_u_to_d_row(a.y);
  236. double x2 = D_u_to_d_col(b.x);
  237. double y2 = D_u_to_d_row(b.y);
  238. R_move_abs(x1, y1);
  239. R_cont_abs(x2, y2);
  240. }
  241. return clipped;
  242. }
  243. static int line_clip_ll(double ax, double ay, double bx, double by)
  244. {
  245. double ux0 = clip.left;
  246. double ux1 = clip.rite;
  247. double x0, x1;
  248. int lo, hi, i;
  249. int ret;
  250. bx = ax + coerce(bx - ax);
  251. x0 = min(ax, bx);
  252. x1 = max(ax, bx);
  253. lo = -shift_count(ux1 - x0);
  254. hi = shift_count(x1 - ux0);
  255. ret = 0;
  256. for (i = lo; i <= hi; i++)
  257. ret |= line_clip(ax - i * 360, ay, bx - i * 360, by);
  258. return ret;
  259. }
  260. static void polyline_clip(const double *x, const double *y, int n)
  261. {
  262. int i;
  263. for (i = 1; i < n; i++)
  264. line_clip(x[i - 1], y[i - 1], x[i], y[i]);
  265. }
  266. static int clip_polygon_plane(int *pn, const double *x, const double *y,
  267. const struct plane *p)
  268. {
  269. int n = *pn;
  270. double x0 = x[n - 1];
  271. double y0 = y[n - 1];
  272. double d0 = dist_plane(x0, y0, p);
  273. int i, j;
  274. for (i = j = 0; i < n; i++) {
  275. double x1 = x[i];
  276. double y1 = y[i];
  277. double d1 = dist_plane(x1, y1, p);
  278. int in0 = d0 <= 0;
  279. int in1 = d1 <= 0;
  280. if (in0 != in1) { /* edge crossing */
  281. alloc_src(j + 1);
  282. xf[j] = interpolate(x0, x1, d0, d1);
  283. yf[j] = interpolate(y0, y1, d0, d1);
  284. j++;
  285. }
  286. if (in1) { /* point inside */
  287. alloc_src(j + 1);
  288. xf[j] = x[i];
  289. yf[j] = y[i];
  290. j++;
  291. }
  292. x0 = x1;
  293. y0 = y1;
  294. d0 = d1;
  295. }
  296. *pn = j;
  297. return (j == 0);
  298. }
  299. static void polygon_clip(const double *x, const double *y, int n)
  300. {
  301. alloc_src(n + 10);
  302. if (clip_polygon_plane(&n, x, y, &pl_left))
  303. return;
  304. dealloc_src(&x, &y, 0);
  305. if (clip_polygon_plane(&n, x, y, &pl_rite))
  306. return;
  307. dealloc_src(&x, &y, 1);
  308. if (clip_polygon_plane(&n, x, y, &pl_bot))
  309. return;
  310. dealloc_src(&x, &y, 1);
  311. if (clip_polygon_plane(&n, x, y, &pl_top))
  312. return;
  313. dealloc_src(&x, &y, 1);
  314. n = do_convert(x, y, n);
  315. R_polygon_abs(xi, yi, n);
  316. }
  317. static void polydots_clip(const double *x, const double *y, int n)
  318. {
  319. double ux0 = clip.left;
  320. int i, j;
  321. alloc_src(n);
  322. for (i = j = 0; i < n; i++) {
  323. double xx = x[i];
  324. double yy = y[i];
  325. if (D_is_lat_lon())
  326. xx -= shift_angle(x[i] - ux0);
  327. if (xx < clip.left || xx > clip.rite)
  328. continue;
  329. if (yy < clip.bot || yy > clip.top)
  330. continue;
  331. xf[j] = xx;
  332. yf[j] = yy;
  333. j++;
  334. }
  335. n = do_convert(xf, yf, n);
  336. R_polydots_abs(xi, yi, j);
  337. }
  338. static void box_clip(double x1, double y1, double x2, double y2)
  339. {
  340. x1 = max(clip.left, min(clip.rite, x1));
  341. x2 = max(clip.left, min(clip.rite, x2));
  342. y1 = max(clip.bot, min(clip.top, y1));
  343. y2 = max(clip.bot, min(clip.top, y2));
  344. x1 = D_u_to_d_col(x1);
  345. x2 = D_u_to_d_col(x2);
  346. y1 = D_u_to_d_row(y1);
  347. y2 = D_u_to_d_row(y2);
  348. R_box_abs(x1, y1, x2, y2);
  349. }
  350. static void box_clip_ll(double x1, double y1, double x2, double y2)
  351. {
  352. double ux0 = clip.left;
  353. double ux1 = clip.rite;
  354. int lo, hi, i;
  355. x2 = x1 + coerce(x2 - x1);
  356. lo = -shift_count(ux1 - x1);
  357. hi = shift_count(x2 - ux0);
  358. for (i = lo; i <= hi; i++)
  359. box_clip(x1 - i * 360, y1, x2 - i * 360, y2);
  360. }
  361. /******************************************************************************/
  362. static int cull_polyline_plane(int *pn, const double *x, const double *y,
  363. const struct plane *p)
  364. {
  365. int n = *pn;
  366. int last = -1;
  367. int prev = 0;
  368. double x0 = x[prev];
  369. double y0 = y[prev];
  370. double d0 = dist_plane(x0, y0, p);
  371. int i, j;
  372. for (i = 0, j = 0; i < n; i++) {
  373. double x1 = x[i];
  374. double y1 = y[i];
  375. double d1 = dist_plane(x1, y1, p);
  376. int in0 = d0 <= 0;
  377. int in1 = d1 <= 0;
  378. if (!in0 && in1 && last != prev) { /* entering */
  379. alloc_src(j + 1);
  380. xf[j] = x0;
  381. yf[j] = y0;
  382. j++;
  383. last = prev;
  384. }
  385. if (in1 || in0) { /* inside or leaving */
  386. alloc_src(j + 1);
  387. xf[j] = x1;
  388. yf[j] = y1;
  389. j++;
  390. last = i;
  391. }
  392. x0 = x1;
  393. y0 = y1;
  394. d0 = d1;
  395. prev = i;
  396. }
  397. *pn = j;
  398. return (j == 0);
  399. }
  400. static void polyline_cull(const double *x, const double *y, int n)
  401. {
  402. alloc_src(n + 10);
  403. if (cull_polyline_plane(&n, x, y, &pl_left))
  404. return;
  405. dealloc_src(&x, &y, 0);
  406. if (cull_polyline_plane(&n, x, y, &pl_rite))
  407. return;
  408. dealloc_src(&x, &y, 1);
  409. if (cull_polyline_plane(&n, x, y, &pl_bot))
  410. return;
  411. dealloc_src(&x, &y, 1);
  412. if (cull_polyline_plane(&n, x, y, &pl_top))
  413. return;
  414. dealloc_src(&x, &y, 1);
  415. n = do_convert(x, y, n);
  416. R_polyline_abs(xi, yi, n);
  417. }
  418. static int cull_polygon_plane(int *pn, const double *x, const double *y,
  419. const struct plane *p)
  420. {
  421. int n = *pn;
  422. int last = -1;
  423. int prev = n - 1;
  424. double x0 = x[prev];
  425. double y0 = y[prev];
  426. double d0 = dist_plane(x0, y0, p);
  427. int i, j;
  428. for (i = j = 0; i < n; i++) {
  429. double x1 = x[i];
  430. double y1 = y[i];
  431. double d1 = dist_plane(x1, y1, p);
  432. int in0 = d0 <= 0;
  433. int in1 = d1 <= 0;
  434. if (!in0 && in1 && last != prev) { /* entering */
  435. alloc_src(j + 1);
  436. xf[j] = x0;
  437. yf[j] = y0;
  438. j++;
  439. last = prev;
  440. }
  441. if (in1 || in0) { /* inside or leaving */
  442. alloc_src(j + 1);
  443. xf[j] = x1;
  444. yf[j] = y1;
  445. j++;
  446. last = i;
  447. }
  448. x0 = x1;
  449. y0 = y1;
  450. d0 = d1;
  451. prev = i;
  452. }
  453. *pn = j;
  454. return (j == 0);
  455. }
  456. static void polygon_cull(const double *x, const double *y, int n)
  457. {
  458. alloc_src(n + 10);
  459. if (cull_polygon_plane(&n, x, y, &pl_left))
  460. return;
  461. dealloc_src(&x, &y, 0);
  462. if (cull_polygon_plane(&n, x, y, &pl_rite))
  463. return;
  464. dealloc_src(&x, &y, 1);
  465. if (cull_polygon_plane(&n, x, y, &pl_bot))
  466. return;
  467. dealloc_src(&x, &y, 1);
  468. if (cull_polygon_plane(&n, x, y, &pl_top))
  469. return;
  470. dealloc_src(&x, &y, 1);
  471. n = do_convert(x, y, n);
  472. R_polygon_abs(xi, yi, n);
  473. }
  474. static void box_cull(double x1, double y1, double x2, double y2)
  475. {
  476. if (x1 > clip.rite && x2 > clip.rite)
  477. return;
  478. if (x1 < clip.left && x2 < clip.left)
  479. return;
  480. if (y1 > clip.top && y2 > clip.top)
  481. return;
  482. if (y1 < clip.bot && y2 < clip.bot)
  483. return;
  484. x1 = D_u_to_d_col(x1);
  485. y1 = D_u_to_d_row(y1);
  486. x2 = D_u_to_d_col(x2);
  487. y2 = D_u_to_d_row(y2);
  488. R_box_abs(x1, y1, x2, y2);
  489. }
  490. static void box_cull_ll(double x1, double y1, double x2, double y2)
  491. {
  492. double ux0 = clip.left;
  493. double ux1 = clip.rite;
  494. int lo, hi, i;
  495. x2 = x1 + coerce(x2 - x1);
  496. lo = -shift_count(ux1 - x1);
  497. hi = shift_count(x2 - ux0);
  498. for (i = lo; i <= hi; i++)
  499. box_clip(x1 - i * 360, y1, x2 - i * 360, y2);
  500. }
  501. static int line_cull(double x1, double y1, double x2, double y2)
  502. {
  503. if (x1 > clip.rite && x2 > clip.rite)
  504. return 1;
  505. if (x1 < clip.left && x2 < clip.left)
  506. return 1;
  507. if (y1 > clip.top && y2 > clip.top)
  508. return 1;
  509. if (y1 < clip.bot && y2 < clip.bot)
  510. return 1;
  511. x1 = D_u_to_d_col(x1);
  512. y1 = D_u_to_d_row(y1);
  513. x2 = D_u_to_d_col(x2);
  514. y2 = D_u_to_d_row(y2);
  515. R_move_abs(x1, y1);
  516. R_cont_abs(x2, y2);
  517. return 0;
  518. }
  519. static int line_cull_ll(double ax, double ay, double bx, double by)
  520. {
  521. double ux0 = clip.left;
  522. double ux1 = clip.rite;
  523. double x0, x1;
  524. int lo, hi, i;
  525. int ret;
  526. bx = ax + coerce(bx - ax);
  527. x0 = min(ax, bx);
  528. x1 = max(ax, bx);
  529. lo = -shift_count(ux1 - x0);
  530. hi = shift_count(x1 - ux0);
  531. ret = 1;
  532. for (i = lo; i <= hi; i++)
  533. ret &= line_cull(ax - i * 360, ay, bx - i * 360, by);
  534. return ret;
  535. }
  536. /******************************************************************************/
  537. static void D_line_raw(double x1, double y1, double x2, double y2)
  538. {
  539. x1 = D_u_to_d_col(x1);
  540. y1 = D_u_to_d_row(y1);
  541. R_move_abs(x1, y1);
  542. x2 = D_u_to_d_col(x2);
  543. y2 = D_u_to_d_row(y2);
  544. R_cont_abs(x2, y2);
  545. }
  546. static void D_polydots_raw(const double *x, const double *y, int n)
  547. {
  548. n = do_convert(x, y, n);
  549. R_polydots_abs(xi, yi, n);
  550. }
  551. static void D_polyline_raw(const double *x, const double *y, int n)
  552. {
  553. n = do_convert(x, y, n);
  554. R_polyline_abs(xi, yi, n);
  555. }
  556. static void D_polygon_raw(const double *x, const double *y, int n)
  557. {
  558. n = do_convert(x, y, n);
  559. R_polygon_abs(xi, yi, n);
  560. }
  561. static void D_box_raw(double x1, double y1, double x2, double y2)
  562. {
  563. x1 = D_u_to_d_col(x1);
  564. x2 = D_u_to_d_col(x2);
  565. y1 = D_u_to_d_row(y1);
  566. y2 = D_u_to_d_row(y2);
  567. R_box_abs(x1, y1, x2, y2);
  568. }
  569. static struct functions raw_functions = {
  570. D_line_raw,
  571. D_polydots_raw,
  572. D_polyline_raw,
  573. D_polygon_raw,
  574. D_box_raw,
  575. };
  576. /******************************************************************************/
  577. static void D_line_cull(double x1, double y1, double x2, double y2)
  578. {
  579. if (!window_set)
  580. D_clip_to_map();
  581. if (D_is_lat_lon())
  582. line_cull_ll(x1, y1, x2, y2);
  583. else
  584. line_cull(x1, y1, x2, y2);
  585. }
  586. static void D_polydots_cull(const double *x, const double *y, int n)
  587. {
  588. if (!window_set)
  589. D_clip_to_map();
  590. polydots_clip(x, y, n);
  591. }
  592. static void D_polyline_cull(const double *x, const double *y, int n)
  593. {
  594. if (n < 2)
  595. return;
  596. if (!window_set)
  597. D_clip_to_map();
  598. if (D_is_lat_lon())
  599. do_ll_wrap(x, y, n, polyline_cull);
  600. else
  601. polyline_cull(x, y, n);
  602. }
  603. static void D_polygon_cull(const double *x, const double *y, int n)
  604. {
  605. if (!window_set)
  606. D_clip_to_map();
  607. if (D_is_lat_lon())
  608. do_ll_wrap(x, y, n, polygon_cull);
  609. else
  610. polygon_cull(x, y, n);
  611. }
  612. static void D_box_cull(double x1, double y1, double x2, double y2)
  613. {
  614. if (!window_set)
  615. D_clip_to_map();
  616. if (D_is_lat_lon())
  617. box_cull_ll(x1, y1, x2, y2);
  618. else
  619. box_cull(x1, y1, x2, y2);
  620. }
  621. static struct functions cull_functions = {
  622. D_line_cull,
  623. D_polydots_cull,
  624. D_polyline_cull,
  625. D_polygon_cull,
  626. D_box_cull,
  627. };
  628. /******************************************************************************/
  629. static void D_line_clip(double x1, double y1, double x2, double y2)
  630. {
  631. if (!window_set)
  632. D_clip_to_map();
  633. if (D_is_lat_lon())
  634. line_clip_ll(x1, y1, x2, y2);
  635. else
  636. line_clip(x1, y1, x2, y2);
  637. }
  638. static void D_polydots_clip(const double *x, const double *y, int n)
  639. {
  640. if (!window_set)
  641. D_clip_to_map();
  642. polydots_clip(x, y, n);
  643. }
  644. static void D_polyline_clip(const double *x, const double *y, int n)
  645. {
  646. if (!window_set)
  647. D_clip_to_map();
  648. if (n < 2)
  649. return;
  650. if (D_is_lat_lon())
  651. do_ll_wrap(x, y, n, polyline_clip);
  652. else
  653. polyline_clip(x, y, n);
  654. }
  655. static void D_polygon_clip(const double *x, const double *y, int n)
  656. {
  657. if (!window_set)
  658. D_clip_to_map();
  659. if (D_is_lat_lon())
  660. do_ll_wrap(x, y, n, polygon_clip);
  661. else
  662. polygon_clip(x, y, n);
  663. }
  664. static void D_box_clip(double x1, double y1, double x2, double y2)
  665. {
  666. if (!window_set)
  667. D_clip_to_map();
  668. if (D_is_lat_lon())
  669. box_clip_ll(x1, y1, x2, y2);
  670. else
  671. box_clip(x1, y1, x2, y2);
  672. }
  673. static struct functions clip_functions = {
  674. D_line_clip,
  675. D_polydots_clip,
  676. D_polyline_clip,
  677. D_polygon_clip,
  678. D_box_clip,
  679. };
  680. /******************************************************************************/
  681. /*!
  682. * \brief set clipping window
  683. *
  684. * Sets the clipping window to the pixel window that corresponds
  685. * to the current database region. This is the default.
  686. *
  687. * \param top
  688. * \param bottom
  689. * \param left
  690. * \param right
  691. */
  692. void D_set_clip(double t, double b, double l, double r)
  693. {
  694. clip.left = min(l, r);
  695. clip.rite = max(l, r);
  696. clip.bot = min(b, t);
  697. clip.top = max(b, t);
  698. pl_left.k = clip.left;
  699. pl_rite.k = -clip.rite;
  700. pl_bot.k = clip.bot;
  701. pl_top.k = -clip.top;
  702. window_set = 1;
  703. }
  704. /*!
  705. * \brief set clipping window to map window
  706. *
  707. * Sets the clipping window to the pixel window that corresponds to the
  708. * current database region. This is the default.
  709. *
  710. * \param ~
  711. */
  712. void D_clip_to_map(void)
  713. {
  714. double t, b, l, r;
  715. D_get_src(&t, &b, &l, &r);
  716. D_set_clip(t, b, l, r);
  717. }
  718. void D_line_width(double d)
  719. {
  720. R_line_width(d > 0 ? d : 0);
  721. }
  722. void D_get_text_box(const char *text, double *t, double *b, double *l, double *r)
  723. {
  724. double T, B, L, R;
  725. R_get_text_box(text, &T, &B, &L, &R);
  726. *t = D_d_to_u_row(T);
  727. *b = D_d_to_u_row(B);
  728. *l = D_d_to_u_col(L);
  729. *r = D_d_to_u_col(R);
  730. if (*t < *b) {
  731. double tmp = *t; *t = *b; *b = tmp;
  732. }
  733. if (*r < *l) {
  734. double tmp = *r; *r = *l; *l = tmp;
  735. }
  736. }
  737. /******************************************************************************/
  738. void D_line_abs(double x1, double y1, double x2, double y2)
  739. {
  740. (*fns->line)(x1, y1, x2, y2);
  741. }
  742. void D_polydots_abs(const double *x, const double *y, int n)
  743. {
  744. (*fns->polydots)(x, y, n);
  745. }
  746. void D_polyline_abs(const double *x, const double *y, int n)
  747. {
  748. (*fns->polyline)(x, y, n);
  749. }
  750. void D_polygon_abs(const double *x, const double *y, int n)
  751. {
  752. (*fns->polygon)(x, y, n);
  753. }
  754. void D_box_abs(double x1, double y1, double x2, double y2)
  755. {
  756. (*fns->box)(x1, y1, x2, y2);
  757. }
  758. /******************************************************************************/
  759. void D_line_rel(double x1, double y1, double x2, double y2)
  760. {
  761. cur.x += x1;
  762. cur.y += y1;
  763. x1 = cur.x;
  764. y1 = cur.y;
  765. cur.x += x2;
  766. cur.y += y2;
  767. x2 = cur.x;
  768. y2 = cur.y;
  769. D_line_abs(x1, y1, x2, y2);
  770. }
  771. void D_polydots_rel(const double *x, const double *y, int n)
  772. {
  773. rel_to_abs(&x, &y, n);
  774. D_polydots_abs(x, y, n);
  775. dealloc_src(&x, &y, 1);
  776. }
  777. void D_polyline_rel(const double *x, const double *y, int n)
  778. {
  779. rel_to_abs(&x, &y, n);
  780. D_polyline_abs(x, y, n);
  781. dealloc_src(&x, &y, 1);
  782. }
  783. void D_polygon_rel(const double *x, const double *y, int n)
  784. {
  785. rel_to_abs(&x, &y, n);
  786. D_polygon_abs(x, y, n);
  787. dealloc_src(&x, &y, 1);
  788. }
  789. void D_box_rel(double x2, double y2)
  790. {
  791. D_box_abs(cur.x, cur.y, cur.x + x2, cur.y + y2);
  792. }
  793. /******************************************************************************/
  794. void D_set_clip_mode(int mode)
  795. {
  796. switch (mode) {
  797. case D_MODE_NONE: fns = &raw_functions; break;
  798. case D_MODE_CULL: fns = &cull_functions; break;
  799. case D_MODE_CLIP: fns = &clip_functions; break;
  800. }
  801. }
  802. /******************************************************************************/
  803. static int path_point_max;
  804. static int path_point_num;
  805. static double *path_x, *path_y;
  806. static int path_segment_max;
  807. static int path_segment_num;
  808. static int *path_segments;
  809. static int in_path;
  810. static int cur_seg;
  811. static void path_alloc_segment(void)
  812. {
  813. if (path_segment_num >= path_segment_max) {
  814. path_segment_max = path_segment_num + 10;
  815. path_segments = G_realloc(path_segments, path_segment_max * sizeof(int));
  816. }
  817. }
  818. static void path_alloc_point(void)
  819. {
  820. if (path_point_num >= path_point_max) {
  821. path_point_max = path_point_num + 100;
  822. path_x = G_realloc(path_x, path_point_max * sizeof(double));
  823. path_y = G_realloc(path_y, path_point_max * sizeof(double));
  824. }
  825. }
  826. static void new_point(double x, double y)
  827. {
  828. path_alloc_point();
  829. path_x[path_point_num] = x;
  830. path_y[path_point_num] = y;
  831. path_point_num++;
  832. }
  833. static void new_segment(void)
  834. {
  835. cur_seg = path_segment_num;
  836. path_alloc_segment();
  837. path_segments[path_segment_num] = path_point_num;
  838. path_segment_num++;
  839. }
  840. /******************************************************************************/
  841. void D_move_abs(double x, double y)
  842. {
  843. cur.x = x;
  844. cur.y = y;
  845. if (in_path) {
  846. new_segment();
  847. new_point(x, y);
  848. }
  849. else {
  850. x = D_u_to_d_col(x);
  851. y = D_u_to_d_row(y);
  852. R_move_abs(x, y);
  853. }
  854. }
  855. void D_move_rel(double x, double y)
  856. {
  857. D_move_abs(cur.x + x, cur.y + y);
  858. }
  859. void D_cont_abs(double x, double y)
  860. {
  861. if (in_path)
  862. new_point(x, y);
  863. else
  864. (*fns->line)(cur.x, cur.y, x, y);
  865. cur.x = x;
  866. cur.y = y;
  867. }
  868. void D_cont_rel(double x, double y)
  869. {
  870. D_cont_abs(cur.x + x, cur.y + y);
  871. }
  872. /******************************************************************************/
  873. void D_begin(void)
  874. {
  875. if (in_path)
  876. G_warning(_("D_begin() called while path active"));
  877. path_segment_num = 0;
  878. path_point_num = 0;
  879. in_path = 1;
  880. }
  881. void D_end(void)
  882. {
  883. if (!in_path)
  884. G_warning(_("D_end() called while path not active"));
  885. new_segment();
  886. in_path = 0;
  887. }
  888. void D_close(void)
  889. {
  890. int i;
  891. if (!in_path) {
  892. G_warning(_("D_close() called while path not active"));
  893. return;
  894. }
  895. i = path_segments[cur_seg];
  896. new_point(path_x[i], path_y[i]);
  897. }
  898. void D_stroke(void)
  899. {
  900. int i;
  901. for (i = 0; i < path_segment_num - 1; i++) {
  902. int i0 = path_segments[i];
  903. int i1 = path_segments[i+1];
  904. D_polyline_abs(&path_x[i0], &path_y[i0], i1 - i0);
  905. }
  906. }
  907. void D_fill(void)
  908. {
  909. int i;
  910. for (i = 0; i < path_segment_num - 1; i++) {
  911. int i0 = path_segments[i];
  912. int i1 = path_segments[i+1];
  913. D_polygon_abs(&path_x[i0], &path_y[i0], i1 - i0);
  914. }
  915. }
  916. /******************************************************************************/