misc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1989,
  2. 1990, 1991, 1995, 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free
  3. Software Foundation, Inc.
  4. The GNU plotutils package is free software. You may redistribute it
  5. and/or modify it under the terms of the GNU General Public License as
  6. published by the Free Software foundation; either version 2, or (at your
  7. option) any later version.
  8. The GNU plotutils package is distributed in the hope that it will be
  9. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with the GNU plotutils package; see the file COPYING. If not, write to
  14. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. /* This file contains miscellaneous subroutines for GNU graph. Currently,
  17. it contains only array_bounds(), which is called if the user fails to
  18. specify at least one of the bounds xmin,xmax,ymin,ymax.
  19. array_bounds() returns the unspecified bounds via pointers. I.e., it
  20. finishes the job of specifying a bounding box for the data points that
  21. will be plotted. The box may later be expanded so that its bounds are
  22. multiples of the tick spacing (see plotter.c).
  23. array_bounds() is called in graph.c, just before a graph is begun. */
  24. #include "sys-defines.h"
  25. #include "extern.h"
  26. /* bit fields for return value from Cohen-Sutherland clipper */
  27. enum { ACCEPTED = 0x1, CLIPPED_FIRST = 0x2, CLIPPED_SECOND = 0x4 };
  28. /* for internal clipper use */
  29. enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
  30. /* forward references */
  31. static int clip_line (double *x0_p, double *y0_p, double *x1_p, double *y1_p, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip, bool spec_min_x, bool spec_min_y, bool spec_max_x, bool spec_max_y);
  32. static int compute_relevant_points (double xx, double yy, double oldxx, double oldyy, int clip_mode, double user_min_x, double user_min_y, double user_max_x, double user_max_y, bool spec_min_x, bool spec_min_y, bool spec_max_x, bool spec_max_y, double xxr[2], double yyr[2]);
  33. static int compute_outcode (double x, double y, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip, bool spec_min_x, bool spec_min_y, bool spec_max_x, bool spec_max_y);
  34. void
  35. swapd(double * a, double * b)
  36. {
  37. double tmp = *a;
  38. *a = *b;
  39. *b = tmp;
  40. }
  41. void
  42. array_bounds (const Point *p, int length,
  43. bool transpose_axes, int clip_mode,
  44. double *min_x, double *min_y, double *max_x, double *max_y,
  45. const bool spec_min_x, const bool spec_min_y,
  46. const bool spec_max_x, const bool spec_max_y)
  47. {
  48. /* keep compilers happy */
  49. double user_min_x = 0.0, user_min_y = 0.0;
  50. double user_max_x = 0.0, user_max_y = 0.0;
  51. double local_min_x = 0.0, local_min_y = 0.0;
  52. double local_max_x = 0.0, local_max_y = 0.0;
  53. double xx, yy, oldxx, oldyy;
  54. bool point_seen = false;
  55. int i;
  56. if (length == 0)
  57. /* adopt a convention */
  58. {
  59. if (!spec_min_x)
  60. *min_x = 0.0;
  61. if (!spec_min_y)
  62. *min_y = 0.0;
  63. if (!spec_max_x)
  64. *max_x = *min_x;
  65. if (!spec_max_y)
  66. *max_y = *min_y;
  67. return;
  68. }
  69. if (spec_min_x)
  70. user_min_x = *min_x;
  71. else /* won't use user_min_x */
  72. local_min_x = DBL_MAX;
  73. if (spec_max_x)
  74. user_max_x = *max_x;
  75. else /* won't use user_max_x */
  76. local_max_x = -(DBL_MAX);
  77. /* special case: user specified both bounds, but min > max (reversed axis) */
  78. if (spec_min_x && spec_max_x && user_min_x > user_max_x)
  79. swapd (&user_min_x, &user_max_x);
  80. if (spec_min_y)
  81. user_min_y = *min_y;
  82. else
  83. local_min_y = DBL_MAX; /* won't use user_min_y */
  84. if (spec_max_y)
  85. user_max_y = *max_y;
  86. else /* won't use user_max_y */
  87. local_max_y = -(DBL_MAX);
  88. /* special case: user specified both bounds, but min > max (reversed axis) */
  89. if (spec_min_y && spec_max_y && user_min_y > user_max_y)
  90. swapd (&user_min_y, &user_max_y);
  91. /* loop through points in array; examine each line segment */
  92. oldxx = oldyy = 0.0; /* previous point */
  93. for (i = 0; i < length; i++)
  94. {
  95. double xxr[2], yyr[2]; /* storage for `relevant points' */
  96. int n, j;
  97. int effective_clip_mode;
  98. /* get new point */
  99. xx = (transpose_axes ? p[i].y : p[i].x);
  100. yy = (transpose_axes ? p[i].x : p[i].y);
  101. /* determine clipping mode (see compute_relevant_points() below) */
  102. if (i == 0 || p[i].pendown == false
  103. || (p[i].linemode <= 0 && p[i].fill_fraction < 0.0))
  104. /* no polyline or filling, each point is isolated */
  105. effective_clip_mode = 0;
  106. else if (p[i].fill_fraction >= 0.0)
  107. effective_clip_mode = 2;
  108. else
  109. effective_clip_mode = clip_mode;
  110. n = compute_relevant_points (xx, yy, oldxx, oldyy,
  111. effective_clip_mode,
  112. user_min_x, user_min_y,
  113. user_max_x, user_max_y,
  114. spec_min_x, spec_min_y,
  115. spec_max_x, spec_max_y,
  116. xxr, yyr);
  117. /* loop through relevant points, updating bounding box */
  118. for (j = 0; j < n; j++)
  119. {
  120. point_seen = true;
  121. if (!spec_min_x)
  122. local_min_x = DMIN(local_min_x, xxr[j]);
  123. if (!spec_min_y)
  124. local_min_y = DMIN(local_min_y, yyr[j]);
  125. if (!spec_max_x)
  126. local_max_x = DMAX(local_max_x, xxr[j]);
  127. if (!spec_max_y)
  128. local_max_y = DMAX(local_max_y, yyr[j]);
  129. }
  130. oldxx = xx;
  131. oldyy = yy;
  132. }
  133. if (!point_seen)
  134. /* a convention */
  135. local_min_x = local_min_y = local_max_x = local_max_y = 0.0;
  136. /* pass back bounds that user didn't specify */
  137. if (!spec_min_x)
  138. *min_x = local_min_x;
  139. if (!spec_min_y)
  140. *min_y = local_min_y;
  141. if (!spec_max_x)
  142. *max_x = local_max_x;
  143. if (!spec_max_y)
  144. *max_y = local_max_y;
  145. return;
  146. }
  147. /* For a new data point (xx,yy), compute the `relevant points', i.e. the
  148. ones that should be used in updating the bounding box. There may be 0,
  149. 1, or 2 of them. The number of relevant points is returned, and the
  150. relevant points themselves are returned via pointers.
  151. The relevant points are computed from the line segment extending from
  152. (oldxx,oldyy) to (xx,yy), via an algorithm parametrized by a
  153. gnuplot-style clip mode (0, 1, or 2).
  154. If clip mode=0 then the simplest algorithm is used: (xx,yy) is a
  155. relevant point iff it satisfies the user-specified bound(s), and there
  156. are no other relevant points, i.e., (oldxx,oldyy) is ignored.
  157. If clip mode=1 then if the line segment from (oldxx, oldyy) from (xx,yy)
  158. has at least one endpoint that satisfies the user-specified bounds, it
  159. generates two relevant points: the endpoints of the line segment,
  160. clipped to the bounds. If on the other hand neither endpoint of the
  161. line segment from (oldxx,oldyy) to (xx,yy) satisfies the user-specified
  162. bounds, no relevant points are generated even if the line segment
  163. contains points that satisfy the bounds.
  164. If clip mode=2 then the line segment, if it intersects the bounding box,
  165. is clipped on both ends, and both resulting endpoints are relevant. */
  166. static int
  167. compute_relevant_points (double xx, double yy,
  168. double oldxx, double oldyy,
  169. int clip_mode,
  170. double user_min_x, double user_min_y,
  171. double user_max_x, double user_max_y,
  172. bool spec_min_x, bool spec_min_y,
  173. bool spec_max_x, bool spec_max_y,
  174. double xxr[2], double yyr[2])
  175. {
  176. int clipval;
  177. switch (clip_mode)
  178. {
  179. case 0:
  180. if ((!spec_min_x || xx >= user_min_x)
  181. && (!spec_max_x || xx <= user_max_x)
  182. && (!spec_min_y || yy >= user_min_y)
  183. && (!spec_max_y || yy <= user_max_y))
  184. {
  185. xxr[0] = xx;
  186. yyr[0] = yy;
  187. return 1;
  188. }
  189. else
  190. return 0;
  191. break;
  192. case 1:
  193. default:
  194. clipval = clip_line (&oldxx, &oldyy, &xx, &yy, user_min_x, user_max_x, user_min_y, user_max_y, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  195. if ((clipval & ACCEPTED)
  196. && !((clipval & CLIPPED_FIRST) && (clipval & CLIPPED_SECOND)))
  197. {
  198. xxr[0] = oldxx;
  199. yyr[0] = oldyy;
  200. xxr[1] = xx;
  201. yyr[1] = yy;
  202. return 2;
  203. }
  204. else
  205. return 0;
  206. break;
  207. case 2:
  208. clipval = clip_line (&oldxx, &oldyy, &xx, &yy, user_min_x, user_max_x, user_min_y, user_max_y, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  209. if (clipval & ACCEPTED)
  210. {
  211. xxr[0] = oldxx;
  212. yyr[0] = oldyy;
  213. xxr[1] = xx;
  214. yyr[1] = yy;
  215. return 2;
  216. }
  217. else
  218. return 0;
  219. break;
  220. }
  221. }
  222. /* clip_line() takes two points, the endpoints of a line segment in the
  223. * device frame (expressed in terms of floating-point device coordinates),
  224. * and destructively passes back two points: the endpoints of the line
  225. * segment clipped by Cohen-Sutherland to the rectangular clipping area.
  226. * The return value contains bitfields ACCEPTED, CLIPPED_FIRST, and
  227. * CLIPPED_SECOND.
  228. *
  229. * This is a modified C-S clipper: the flags spec_{min,max}_{x,y} indicate
  230. * whether or not clipping is to be performed on each edge.
  231. TODO refactor with clip_line in plotter.c
  232. */
  233. static int
  234. clip_line (double *x0_p, double *y0_p, double *x1_p, double *y1_p, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip, bool spec_min_x, bool spec_min_y, bool spec_max_x, bool spec_max_y)
  235. {
  236. double x0 = *x0_p;
  237. double y0 = *y0_p;
  238. double x1 = *x1_p;
  239. double y1 = *y1_p;
  240. int outcode0, outcode1;
  241. bool accepted;
  242. int clipval = 0;
  243. outcode0 = compute_outcode (x0, y0, x_min_clip, x_max_clip, y_min_clip, y_max_clip, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  244. outcode1 = compute_outcode (x1, y1, x_min_clip, x_max_clip, y_min_clip, y_max_clip, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  245. for ( ; ; )
  246. {
  247. if (!(outcode0 | outcode1)) /* accept */
  248. {
  249. accepted = true;
  250. break;
  251. }
  252. else if (outcode0 & outcode1) /* reject */
  253. {
  254. accepted = false;
  255. break;
  256. }
  257. else
  258. {
  259. /* at least one endpoint is outside; choose one that is */
  260. int outcode_out = (outcode0 ? outcode0 : outcode1);
  261. double x, y; /* intersection with clip edge */
  262. if (outcode_out & RIGHT)
  263. {
  264. x = x_max_clip;
  265. y = isinf (x0) ? y1 : y0 + (y1 - y0) * (x - x0) / (x1 - x0);
  266. }
  267. else if (outcode_out & LEFT)
  268. {
  269. x = x_min_clip;
  270. y = isinf (x0) ? y1 : y0 + (y1 - y0) * (x - x0) / (x1 - x0);
  271. }
  272. else if (outcode_out & TOP)
  273. {
  274. y = y_max_clip;
  275. x = isinf (y0) ? x1 : x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  276. }
  277. else /* BOTTOM bit must be set */
  278. {
  279. y = y_min_clip;
  280. x = isinf (y0) ? x1 : x0 + (x1 - x0) * (y - y0) / (y1 - y0);
  281. }
  282. if (outcode_out == outcode0)
  283. {
  284. x0 = x;
  285. y0 = y;
  286. outcode0 = compute_outcode (x0, y0, x_min_clip, x_max_clip, y_min_clip, y_max_clip, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  287. }
  288. else
  289. {
  290. x1 = x;
  291. y1 = y;
  292. outcode1 = compute_outcode (x1, y1, x_min_clip, x_max_clip, y_min_clip, y_max_clip, spec_min_x, spec_min_y, spec_max_x, spec_max_y);
  293. }
  294. }
  295. }
  296. if (accepted)
  297. {
  298. clipval |= ACCEPTED;
  299. if ((x0 != *x0_p) || (y0 != *y0_p))
  300. clipval |= CLIPPED_FIRST;
  301. if ((x1 != *x1_p) || (y1 != *y1_p))
  302. clipval |= CLIPPED_SECOND;
  303. *x0_p = x0;
  304. *y0_p = y0;
  305. *x1_p = x1;
  306. *y1_p = y1;
  307. }
  308. return clipval;
  309. }
  310. /* TODO refactor with clip_line in plotter.c */
  311. static int
  312. compute_outcode (double x, double y, double x_min_clip, double x_max_clip, double y_min_clip, double y_max_clip, bool spec_min_x, bool spec_min_y, bool spec_max_x, bool spec_max_y)
  313. {
  314. return RIGHT * (spec_max_x && x > x_max_clip)
  315. + LEFT * (spec_min_x && x < x_min_clip)
  316. + TOP * (spec_max_y && y > y_max_clip)
  317. + BOTTOM * (spec_min_y && y < y_min_clip);
  318. }
  319. /*
  320. Local Variables:
  321. c-file-style: "gnu"
  322. tab-width: 8
  323. End:
  324. */