draw2.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. }
  101. /******************************************************************************/
  102. /*!
  103. * \brief set clipping window
  104. *
  105. * Sets the clipping window to the pixel window that corresponds
  106. * to the current database region. This is the default.
  107. *
  108. * \param top
  109. * \param bottom
  110. * \param left
  111. * \param right
  112. */
  113. void D_set_clip(double t, double b, double l, double r)
  114. {
  115. clip.left = min(l, r);
  116. clip.rite = max(l, r);
  117. clip.bot = min(b, t);
  118. clip.top = max(b, t);
  119. window_set = 1;
  120. }
  121. /*!
  122. * \brief set clipping window to map window
  123. *
  124. * Sets the clipping window to the pixel window that corresponds to the
  125. * current database region. This is the default.
  126. *
  127. * \param ~
  128. */
  129. void D_clip_to_map(void)
  130. {
  131. double t, b, l, r;
  132. D_get_src(&t, &b, &l, &r);
  133. D_set_clip(t, b, l, r);
  134. }
  135. void D_set_clip_mode(int mode)
  136. {
  137. clip_mode = mode;
  138. }
  139. void D_set_reduction(double e)
  140. {
  141. epsilon = e;
  142. }
  143. void D_line_width(double d)
  144. {
  145. COM_Line_width(d > 0 ? d : 0);
  146. }
  147. void D_get_text_box(const char *text, double *t, double *b, double *l, double *r)
  148. {
  149. double T, B, L, R;
  150. COM_Get_text_box(text, &T, &B, &L, &R);
  151. *t = D_d_to_u_row(T);
  152. *b = D_d_to_u_row(B);
  153. *l = D_d_to_u_col(L);
  154. *r = D_d_to_u_col(R);
  155. if (*t < *b) {
  156. double tmp = *t; *t = *b; *b = tmp;
  157. }
  158. if (*r < *l) {
  159. double tmp = *r; *r = *l; *l = tmp;
  160. }
  161. }
  162. /******************************************************************************/
  163. void D_pos_abs(double x, double y)
  164. {
  165. cur.x = x;
  166. cur.y = y;
  167. x = D_u_to_d_col(x);
  168. y = D_u_to_d_row(y);
  169. COM_Pos_abs(x, y);
  170. }
  171. void D_pos_rel(double x, double y)
  172. {
  173. D_pos_abs(cur.x + x, cur.y + y);
  174. }
  175. /******************************************************************************/
  176. static void do_path(int no_pole)
  177. {
  178. struct path *p = &path;
  179. struct clip planes;
  180. int i;
  181. if (!window_set)
  182. D_clip_to_map();
  183. if (D_is_lat_lon()) {
  184. ll_wrap_path(&ll_path, p, no_pole);
  185. p = &ll_path;
  186. }
  187. switch (clip_mode) {
  188. case M_NONE:
  189. break;
  190. case M_CULL:
  191. D__set_clip_planes(&planes, &clip);
  192. D__cull_path(&clip_path, p, &planes);
  193. p = &clip_path;
  194. break;
  195. case M_CLIP:
  196. D__set_clip_planes(&planes, &clip);
  197. D__clip_path(&clip_path, p, &planes);
  198. p = &clip_path;
  199. break;
  200. }
  201. conv_path(&raw_path, p);
  202. p = &raw_path;
  203. if (epsilon > 0) {
  204. reduce_path(&eps_path, p, epsilon);
  205. p = &eps_path;
  206. }
  207. COM_Begin();
  208. for (i = 0; i < p->count; i++) {
  209. struct vertex *v = &p->vertices[i];
  210. switch (v->mode)
  211. {
  212. case P_MOVE:
  213. COM_Move(v->x, v->y);
  214. break;
  215. case P_CONT:
  216. COM_Cont(v->x, v->y);
  217. break;
  218. case P_CLOSE:
  219. COM_Close();
  220. break;
  221. }
  222. }
  223. }
  224. void D_begin(void)
  225. {
  226. path_begin(&path);
  227. }
  228. void D_end(void)
  229. {
  230. }
  231. void D_move_abs(double x, double y)
  232. {
  233. path_move(&path, x, y);
  234. cur.x = x;
  235. cur.y = y;
  236. }
  237. void D_cont_abs(double x, double y)
  238. {
  239. path_cont(&path, x, y);
  240. cur.x = x;
  241. cur.y = y;
  242. }
  243. void D_close(void)
  244. {
  245. path_close(&path);
  246. }
  247. void D_stroke(void)
  248. {
  249. do_path(0);
  250. COM_Stroke();
  251. }
  252. void D_fill(void)
  253. {
  254. do_path(1);
  255. COM_Fill();
  256. }
  257. void D_dots(void)
  258. {
  259. struct path *p = &path;
  260. int i;
  261. if (!window_set)
  262. D_clip_to_map();
  263. for (i = 0; i < p->count; i++) {
  264. struct vertex *v = &p->vertices[i];
  265. double x = v->x;
  266. double y = v->y;
  267. if (D_is_lat_lon())
  268. x = coerce(x);
  269. if (clip_mode != M_NONE) {
  270. if (x < clip.left || x > clip.rite)
  271. continue;
  272. if (y < clip.bot || y > clip.top)
  273. continue;
  274. }
  275. x = D_u_to_d_col(x);
  276. y = D_u_to_d_row(y);
  277. COM_Point(x, y);
  278. }
  279. }
  280. /******************************************************************************/
  281. static void poly_abs(const double *x, const double *y, int n)
  282. {
  283. int i;
  284. if (n < 2)
  285. return;
  286. D_begin();
  287. D_move_abs(x[0], y[0]);
  288. for (i = 1; i < n; i++)
  289. D_cont_abs(x[i], y[i]);
  290. }
  291. void D_polyline_abs(const double *x, const double *y, int n)
  292. {
  293. poly_abs(x, y, n);
  294. D_stroke();
  295. }
  296. void D_polygon_abs(const double *x, const double *y, int n)
  297. {
  298. poly_abs(x, y, n);
  299. D_close();
  300. D_fill();
  301. }
  302. void D_polydots_abs(const double *x, const double *y, int n)
  303. {
  304. poly_abs(x, y, n);
  305. D_dots();
  306. }
  307. void D_line_abs(double x1, double y1, double x2, double y2)
  308. {
  309. D_begin();
  310. D_move_abs(x1, y1);
  311. D_cont_abs(x2, y2);
  312. D_end();
  313. D_stroke();
  314. }
  315. void D_box_abs(double x1, double y1, double x2, double y2)
  316. {
  317. struct vector save = cur;
  318. D_begin();
  319. D_move_abs(x1, y1);
  320. D_cont_abs(x2, y1);
  321. D_cont_abs(x2, y2);
  322. D_cont_abs(x1, y2);
  323. D_close();
  324. D_end();
  325. D_fill();
  326. cur = save;
  327. }
  328. /******************************************************************************/
  329. static void poly_rel(const double *x, const double *y, int n)
  330. {
  331. int i;
  332. if (n < 2)
  333. return;
  334. D_begin();
  335. D_move_rel(x[0], y[0]);
  336. for (i = 1; i < n; i++)
  337. D_cont_rel(x[i], y[i]);
  338. }
  339. void D_move_rel(double x, double y)
  340. {
  341. D_move_abs(cur.x + x, cur.y + y);
  342. }
  343. void D_cont_rel(double x, double y)
  344. {
  345. D_cont_abs(cur.x + x, cur.y + y);
  346. }
  347. void D_polydots_rel(const double *x, const double *y, int n)
  348. {
  349. poly_rel(x, y, n);
  350. D_dots();
  351. }
  352. void D_polyline_rel(const double *x, const double *y, int n)
  353. {
  354. poly_rel(x, y, n);
  355. D_stroke();
  356. }
  357. void D_polygon_rel(const double *x, const double *y, int n)
  358. {
  359. poly_rel(x, y, n);
  360. D_close();
  361. D_fill();
  362. }
  363. void D_line_rel(double x1, double y1, double x2, double y2)
  364. {
  365. cur.x += x1;
  366. cur.y += y1;
  367. x1 = cur.x;
  368. y1 = cur.y;
  369. cur.x += x2;
  370. cur.y += y2;
  371. x2 = cur.x;
  372. y2 = cur.y;
  373. D_line_abs(x1, y1, x2, y2);
  374. }
  375. void D_box_rel(double x2, double y2)
  376. {
  377. D_box_abs(cur.x, cur.y, cur.x + x2, cur.y + y2);
  378. }
  379. /******************************************************************************/