draw2.c 8.7 KB

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