draw2.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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. #include "driverlib.h"
  8. struct rectangle
  9. {
  10. double left;
  11. double rite;
  12. double bot;
  13. double top;
  14. };
  15. struct vector
  16. {
  17. double x, y;
  18. };
  19. static struct vector cur;
  20. static struct rectangle clip;
  21. static int window_set;
  22. #define min(x,y) ((x) < (y) ? (x) : (y))
  23. #define max(x,y) ((x) > (y) ? (x) : (y))
  24. static double *xi, *yi;
  25. static int nalloc_i;
  26. static double *xf, *yf;
  27. static int nalloc_f;
  28. /******************************************************************************/
  29. static void alloc_dst(int n)
  30. {
  31. if (nalloc_i >= n)
  32. return;
  33. nalloc_i = n;
  34. xi = G_realloc(xi, nalloc_i * sizeof(double));
  35. yi = G_realloc(yi, nalloc_i * sizeof(double));
  36. }
  37. static void alloc_src(int n)
  38. {
  39. if (nalloc_f >= n)
  40. return;
  41. nalloc_f = n + 10;
  42. xf = G_realloc(xf, nalloc_f * sizeof(double));
  43. yf = G_realloc(yf, nalloc_f * sizeof(double));
  44. }
  45. static void dealloc_src(const double **x, const double **y, int release)
  46. {
  47. if (release) {
  48. G_free(*(double **)x);
  49. G_free(*(double **)y);
  50. }
  51. *x = xf;
  52. *y = yf;
  53. nalloc_f = 0;
  54. xf = NULL;
  55. yf = NULL;
  56. }
  57. static int do_reduce(double *x, double *y, int n)
  58. {
  59. static double eps = 0.5;
  60. int i, j;
  61. for (i = 0, j = 1; j < n; j++) {
  62. if (fabs(x[j] - x[i]) < eps && fabs(y[j] - y[i]) < eps)
  63. continue;
  64. i++;
  65. if (i == j)
  66. continue;
  67. x[i] = x[j];
  68. y[i] = y[j];
  69. }
  70. return i + 1;
  71. }
  72. static int do_convert(const double *x, const double *y, int n)
  73. {
  74. int i;
  75. alloc_dst(n);
  76. for (i = 0; i < n; i++) {
  77. xi[i] = D_u_to_d_col(x[i]);
  78. yi[i] = D_u_to_d_row(y[i]);
  79. }
  80. return do_reduce(xi, yi, n);
  81. }
  82. static void rel_to_abs(const double **px, const double **py, int n)
  83. {
  84. const double *x = *px;
  85. const double *y = *py;
  86. int i;
  87. alloc_src(n);
  88. xf[0] = cur.x + x[0];
  89. yf[0] = cur.y + y[0];
  90. for (i = 1; i < n; i++) {
  91. xf[i] = xf[i-1] + x[i];
  92. yf[i] = yf[i-1] + y[i];
  93. }
  94. dealloc_src(px, py, 0);
  95. }
  96. static int shift_count(double dx)
  97. {
  98. return (int)floor(dx / 360);
  99. }
  100. static double shift_angle(double dx)
  101. {
  102. return shift_count(dx) * 360;
  103. }
  104. static double coerce(double x)
  105. {
  106. x += 180;
  107. x -= shift_angle(x);
  108. x -= 180;
  109. return x;
  110. }
  111. static int euclidify(double *x, const double *y, int n, int no_pole)
  112. {
  113. double ux0 = clip.left;
  114. double ux1 = clip.rite;
  115. double x0, x1;
  116. int lo, hi, count;
  117. int i;
  118. x0 = x1 = x[0];
  119. for (i = 1; i < n; i++) {
  120. if (fabs(y[i]) < 89.9)
  121. x[i] = x[i - 1] + coerce(x[i] - x[i - 1]);
  122. x0 = min(x0, x[i]);
  123. x1 = max(x1, x[i]);
  124. }
  125. if (no_pole && fabs(x[n - 1] - x[0]) > 180)
  126. return 0;
  127. lo = -shift_count(ux1 - x0);
  128. hi = shift_count(x1 - ux0);
  129. count = hi - lo + 1;
  130. for (i = 0; i < n; i++)
  131. x[i] -= lo * 360;
  132. return count;
  133. }
  134. static void ll_wrap_path(const double *x, const double *y, int n,
  135. void (*func) (const double *, const double *, int))
  136. {
  137. double *xx = G_malloc(n * sizeof(double));
  138. int count, i;
  139. memcpy(xx, x, n * sizeof(double));
  140. count = euclidify(xx, y, n, 0);
  141. for (i = 0; i < count; i++) {
  142. int j;
  143. (*func) (xx, y, n);
  144. for (j = 0; j < n; j++)
  145. xx[j] -= 360;
  146. }
  147. G_free(xx);
  148. }
  149. static void ll_wrap_line(double ax, double ay, double bx, double by,
  150. void (*func)(double, double, double, double))
  151. {
  152. double ux0 = clip.left;
  153. double ux1 = clip.rite;
  154. double x0, x1;
  155. int lo, hi, i;
  156. int ret;
  157. bx = ax + coerce(bx - ax);
  158. x0 = min(ax, bx);
  159. x1 = max(ax, bx);
  160. lo = -shift_count(ux1 - x0);
  161. hi = shift_count(x1 - ux0);
  162. ret = 0;
  163. for (i = lo; i <= hi; i++)
  164. (*func)(ax - i * 360, ay, bx - i * 360, by);
  165. }
  166. /******************************************************************************/
  167. static void D_polydots_raw(const double *x, const double *y, int n)
  168. {
  169. n = do_convert(x, y, n);
  170. R_polydots_abs(xi, yi, n);
  171. }
  172. static void D_polyline_raw(const double *x, const double *y, int n)
  173. {
  174. n = do_convert(x, y, n);
  175. R_polyline_abs(xi, yi, n);
  176. }
  177. static void D_polygon_raw(const double *x, const double *y, int n)
  178. {
  179. n = do_convert(x, y, n);
  180. R_polygon_abs(xi, yi, n);
  181. }
  182. static void D_line_raw(double x1, double y1, double x2, double y2)
  183. {
  184. x1 = D_u_to_d_col(x1);
  185. y1 = D_u_to_d_row(y1);
  186. x2 = D_u_to_d_col(x2);
  187. y2 = D_u_to_d_row(y2);
  188. R_line_abs(x1, y1, x2, y2);
  189. }
  190. static void D_box_raw(double x1, double y1, double x2, double y2)
  191. {
  192. x1 = D_u_to_d_col(x1);
  193. x2 = D_u_to_d_col(x2);
  194. y1 = D_u_to_d_row(y1);
  195. y2 = D_u_to_d_row(y2);
  196. R_box_abs(x1, y1, x2, y2);
  197. }
  198. /******************************************************************************/
  199. /*!
  200. * \brief set clipping window
  201. *
  202. * Sets the clipping window to the pixel window that corresponds
  203. * to the current database region. This is the default.
  204. *
  205. * \param top
  206. * \param bottom
  207. * \param left
  208. * \param right
  209. */
  210. void D_set_clip(double t, double b, double l, double r)
  211. {
  212. clip.left = min(l, r);
  213. clip.rite = max(l, r);
  214. clip.bot = min(b, t);
  215. clip.top = max(b, t);
  216. window_set = 1;
  217. }
  218. /*!
  219. * \brief set clipping window to map window
  220. *
  221. * Sets the clipping window to the pixel window that corresponds to the
  222. * current database region. This is the default.
  223. *
  224. * \param ~
  225. */
  226. void D_clip_to_map(void)
  227. {
  228. double t, b, l, r;
  229. D_get_src(&t, &b, &l, &r);
  230. D_set_clip(t, b, l, r);
  231. }
  232. void D_line_width(double d)
  233. {
  234. R_line_width(d > 0 ? d : 0);
  235. }
  236. void D_get_text_box(const char *text, double *t, double *b, double *l, double *r)
  237. {
  238. double T, B, L, R;
  239. R_get_text_box(text, &T, &B, &L, &R);
  240. *t = D_d_to_u_row(T);
  241. *b = D_d_to_u_row(B);
  242. *l = D_d_to_u_col(L);
  243. *r = D_d_to_u_col(R);
  244. if (*t < *b) {
  245. double tmp = *t; *t = *b; *b = tmp;
  246. }
  247. if (*r < *l) {
  248. double tmp = *r; *r = *l; *l = tmp;
  249. }
  250. }
  251. /******************************************************************************/
  252. void D_polydots_abs(const double *x, const double *y, int n)
  253. {
  254. if (!window_set)
  255. D_clip_to_map();
  256. if (D_is_lat_lon())
  257. ll_wrap_path(x, y, n, D_polydots_raw);
  258. else
  259. D_polydots_raw(x, y, n);
  260. }
  261. void D_polyline_abs(const double *x, const double *y, int n)
  262. {
  263. if (!window_set)
  264. D_clip_to_map();
  265. if (n < 2)
  266. return;
  267. if (D_is_lat_lon())
  268. ll_wrap_path(x, y, n, D_polyline_raw);
  269. else
  270. D_polyline_raw(x, y, n);
  271. }
  272. void D_polygon_abs(const double *x, const double *y, int n)
  273. {
  274. if (!window_set)
  275. D_clip_to_map();
  276. if (D_is_lat_lon())
  277. ll_wrap_path(x, y, n, D_polygon_raw);
  278. else
  279. D_polygon_raw(x, y, n);
  280. }
  281. void D_line_abs(double x1, double y1, double x2, double y2)
  282. {
  283. if (!window_set)
  284. D_clip_to_map();
  285. if (D_is_lat_lon())
  286. ll_wrap_line(x1, y1, x2, y2, D_line_raw);
  287. else
  288. D_line_raw(x1, y1, x2, y2);
  289. }
  290. void D_box_abs(double x1, double y1, double x2, double y2)
  291. {
  292. if (!window_set)
  293. D_clip_to_map();
  294. if (D_is_lat_lon())
  295. ll_wrap_line(x1, y1, x2, y2, D_box_raw);
  296. else
  297. D_box_raw(x1, y1, x2, y2);
  298. }
  299. /******************************************************************************/
  300. void D_line_rel(double x1, double y1, double x2, double y2)
  301. {
  302. cur.x += x1;
  303. cur.y += y1;
  304. x1 = cur.x;
  305. y1 = cur.y;
  306. cur.x += x2;
  307. cur.y += y2;
  308. x2 = cur.x;
  309. y2 = cur.y;
  310. D_line_abs(x1, y1, x2, y2);
  311. }
  312. void D_polydots_rel(const double *x, const double *y, int n)
  313. {
  314. rel_to_abs(&x, &y, n);
  315. D_polydots_abs(x, y, n);
  316. dealloc_src(&x, &y, 1);
  317. }
  318. void D_polyline_rel(const double *x, const double *y, int n)
  319. {
  320. rel_to_abs(&x, &y, n);
  321. D_polyline_abs(x, y, n);
  322. dealloc_src(&x, &y, 1);
  323. }
  324. void D_polygon_rel(const double *x, const double *y, int n)
  325. {
  326. rel_to_abs(&x, &y, n);
  327. D_polygon_abs(x, y, n);
  328. dealloc_src(&x, &y, 1);
  329. }
  330. void D_box_rel(double x2, double y2)
  331. {
  332. D_box_abs(cur.x, cur.y, cur.x + x2, cur.y + y2);
  333. }
  334. /******************************************************************************/
  335. static struct path path;
  336. void D_pos_abs(double x, double y)
  337. {
  338. cur.x = x;
  339. cur.y = y;
  340. x = D_u_to_d_col(x);
  341. y = D_u_to_d_row(y);
  342. R_pos_abs(x, y);
  343. }
  344. void D_pos_rel(double x, double y)
  345. {
  346. D_pos_abs(cur.x + x, cur.y + y);
  347. }
  348. void D_move_abs(double x, double y)
  349. {
  350. path_move(&path, x, y);
  351. cur.x = x;
  352. cur.y = y;
  353. }
  354. void D_move_rel(double x, double y)
  355. {
  356. D_move_abs(cur.x + x, cur.y + y);
  357. }
  358. void D_cont_abs(double x, double y)
  359. {
  360. path_cont(&path, x, y);
  361. cur.x = x;
  362. cur.y = y;
  363. }
  364. void D_cont_rel(double x, double y)
  365. {
  366. D_cont_abs(cur.x + x, cur.y + y);
  367. }
  368. /******************************************************************************/
  369. void D_begin(void)
  370. {
  371. path_begin(&path);
  372. }
  373. void D_end(void)
  374. {
  375. }
  376. void D_close(void)
  377. {
  378. path_close(&path);
  379. }
  380. void D_stroke(void)
  381. {
  382. path_fill(&path, D_polyline_abs);
  383. }
  384. void D_fill(void)
  385. {
  386. path_fill(&path, D_polygon_abs);
  387. }
  388. /******************************************************************************/