g_range.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* This file is part of the GNU plotutils package. Copyright (C) 1995,
  2. 1996, 1997, 1998, 1999, 2000, 2005, 2008, Free Software Foundation, Inc.
  3. The GNU plotutils package is free software. You may redistribute it
  4. and/or modify it under the terms of the GNU General Public License as
  5. published by the Free Software foundation; either version 2, or (at your
  6. option) any later version.
  7. The GNU plotutils package is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. You should have received a copy of the GNU General Public License along
  12. with the GNU plotutils package; see the file COPYING. If not, write to
  13. the Free Software Foundation, Inc., 51 Franklin St., Fifth Floor,
  14. Boston, MA 02110-1301, USA. */
  15. /* This file contains functions that update the bounding box information
  16. for a page whenever a new object (ellipse, line segment, or Bezier
  17. segment) is plotted. Updating takes the line width into account, more
  18. or less. The bounding box information is stored in terms of device
  19. units, in the page's plOutbuf structure. */
  20. #include "sys-defines.h"
  21. #include "extern.h"
  22. #define VLENGTH(v) sqrt( (v).x * (v).x + (v).y * (v).y )
  23. /* update bounding box due to drawing of ellipse (args are in user coors) */
  24. /* WARNING: This is not completely accurate, due to the nonzero width of
  25. the pen used to draw the ellipse. Notoriously, the outer boundary of a
  26. `wide ellipse' isn't an ellipse at all: in general it's an eighth-order
  27. curve (see Foley and van Damm), though it's a fourth-order curve if the
  28. axes are aligned with the coordinate axes. Here we approximate it as an
  29. ellipse, with semimajor and semiminor axes in the user frame increased
  30. by one-half of the line width. This approximation is good unless the
  31. line width is large. */
  32. void
  33. _set_ellipse_bbox (plOutbuf *bufp, double x, double y, double rx, double ry, double costheta, double sintheta, double linewidth, double m[6])
  34. {
  35. double ux, uy, vx, vy;
  36. double mixing_angle;
  37. double semi_axis_1_x, semi_axis_1_y, semi_axis_2_x, semi_axis_2_y;
  38. double rx_device, ry_device;
  39. double theta_device, costheta_device, sintheta_device;
  40. double xdeviation, ydeviation;
  41. /* take user-frame line width into account (approximately! see above) */
  42. rx += 0.5 * linewidth;
  43. ry += 0.5 * linewidth;
  44. /* perform affine user->device coor transformation; (ux,uy) and (vx,vy)
  45. are forward images of the semiaxes, i.e. they are conjugate radial
  46. vectors in the device frame */
  47. ux = XDV_INTERNAL(rx * costheta, rx * sintheta, m);
  48. uy = YDV_INTERNAL(rx * costheta, rx * sintheta, m);
  49. vx = XDV_INTERNAL(-ry * sintheta, ry * costheta, m);
  50. vy = YDV_INTERNAL(-ry * sintheta, ry * costheta, m);
  51. /* angle by which the conjugate radial vectors should be mixed, in order
  52. to yield vectors along the major and minor axes in the device frame */
  53. mixing_angle = 0.5 * _xatan2 (2.0 * (ux * vx + uy * vy),
  54. ux * ux + uy * uy - vx * vx + vy * vy);
  55. /* semi-axis vectors in device coordinates */
  56. semi_axis_1_x = ux * cos(mixing_angle) + vx * sin(mixing_angle);
  57. semi_axis_1_y = uy * cos(mixing_angle) + vy * sin(mixing_angle);
  58. semi_axis_2_x = ux * cos(mixing_angle + M_PI_2)
  59. + vx * sin(mixing_angle + M_PI_2);
  60. semi_axis_2_y = uy * cos(mixing_angle + M_PI_2)
  61. + vy * sin(mixing_angle + M_PI_2);
  62. /* semi-axis lengths in device coordinates */
  63. rx_device = sqrt (semi_axis_1_x * semi_axis_1_x
  64. + semi_axis_1_y * semi_axis_1_y);
  65. ry_device = sqrt (semi_axis_2_x * semi_axis_2_x
  66. + semi_axis_2_y * semi_axis_2_y);
  67. /* angle of inclination of the first semi-axis, in device frame */
  68. theta_device = - _xatan2 (semi_axis_1_y, semi_axis_1_x);
  69. costheta_device = cos (theta_device);
  70. sintheta_device = sin (theta_device);
  71. /* maximum displacement in horizontal and vertical directions
  72. while drawing ellipse, in device frame */
  73. xdeviation = sqrt (rx_device * rx_device * costheta_device * costheta_device
  74. + ry_device * ry_device * sintheta_device * sintheta_device);
  75. ydeviation = sqrt (rx_device * rx_device * sintheta_device * sintheta_device
  76. + ry_device * ry_device * costheta_device * costheta_device);
  77. /* record these displacements, for bounding box */
  78. _update_bbox (bufp,
  79. XD_INTERNAL(x,y,m) + xdeviation,
  80. YD_INTERNAL(x,y,m) + ydeviation);
  81. _update_bbox (bufp,
  82. XD_INTERNAL(x,y,m) + xdeviation,
  83. YD_INTERNAL(x,y,m) - ydeviation);
  84. _update_bbox (bufp,
  85. XD_INTERNAL(x,y,m) - xdeviation,
  86. YD_INTERNAL(x,y,m) + ydeviation);
  87. _update_bbox (bufp,
  88. XD_INTERNAL(x,y,m) - xdeviation,
  89. YD_INTERNAL(x,y,m) - ydeviation);
  90. }
  91. /* update bounding box due to drawing of a line end (args are in user coors) */
  92. void
  93. _set_line_end_bbox (plOutbuf *bufp, double x, double y, double xother, double yother, double linewidth, int capstyle, double m[6])
  94. {
  95. plVector v, vrot;
  96. double xs, ys;
  97. double halfwidth = 0.5 * linewidth;
  98. switch (capstyle)
  99. {
  100. case PL_CAP_BUTT:
  101. default:
  102. vrot.x = yother - y;
  103. vrot.y = x - xother;
  104. _vscale (&vrot, halfwidth);
  105. xs = x + vrot.x;
  106. ys = y + vrot.y;
  107. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  108. xs = x - vrot.x;
  109. ys = y - vrot.y;
  110. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  111. break;
  112. case PL_CAP_PROJECT:
  113. v.x = xother - x;
  114. v.y = yother - y;
  115. _vscale (&v, halfwidth);
  116. vrot.x = yother - y;
  117. vrot.y = x - xother;
  118. _vscale (&vrot, halfwidth);
  119. xs = x - v.x + vrot.x;
  120. ys = y - v.y + vrot.y;
  121. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  122. xs = x - v.x - vrot.x;
  123. ys = y - v.y - vrot.y;
  124. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  125. break;
  126. case PL_CAP_ROUND:
  127. _set_ellipse_bbox (bufp, x, y, halfwidth, halfwidth, 1.0, 0.0, 0.0, m);
  128. break;
  129. case PL_CAP_TRIANGULAR:
  130. /* add projecting vertex */
  131. v.x = xother - x;
  132. v.y = yother - y;
  133. _vscale (&v, halfwidth);
  134. xs = x + v.x;
  135. ys = y + v.y;
  136. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  137. /* add other two vertices */
  138. vrot.x = yother - y;
  139. vrot.y = x - xother;
  140. _vscale (&vrot, halfwidth);
  141. xs = x + vrot.x;
  142. ys = y + vrot.y;
  143. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  144. xs = x - vrot.x;
  145. ys = y - vrot.y;
  146. _update_bbox (bufp, XD_INTERNAL(xs,ys,m), YD_INTERNAL(xs,ys,m));
  147. break;
  148. }
  149. }
  150. /* update bounding box due to drawing of a line join (args are in user coors)*/
  151. void
  152. _set_line_join_bbox (plOutbuf *bufp, double xleft, double yleft, double x, double y, double xright, double yright, double linewidth, int joinstyle, double miterlimit, double m[6])
  153. {
  154. plVector v1, v2, vsum;
  155. double v1len, v2len;
  156. double halfwidth;
  157. double mitrelen;
  158. switch (joinstyle)
  159. {
  160. case PL_JOIN_MITER:
  161. default:
  162. v1.x = xleft - x;
  163. v1.y = yleft - y;
  164. v2.x = xright - x;
  165. v2.y = yright - y;
  166. v1len = VLENGTH(v1);
  167. v2len = VLENGTH(v2);
  168. if (v1len == 0.0 || v2len == 0.0)
  169. _update_bbox (bufp, XD_INTERNAL(x,y,m), YD_INTERNAL(x,y,m));
  170. else
  171. {
  172. double cosphi;
  173. /* The maximum value the cosine of the angle between two joining
  174. lines may have, if the join is to be mitered rather than
  175. beveled, is 1-2/(M*M), where M is the mitrelimit. This is
  176. because M equals the cosecant of one-half the minimum angle. */
  177. cosphi = ((v1.x * v2.x + v1.y * v2.y) / v1len) / v2len;
  178. if (miterlimit <= 1.0
  179. || (cosphi > (1.0 - 2.0 / (miterlimit * miterlimit))))
  180. /* bevel rather than miter */
  181. {
  182. _set_line_end_bbox (bufp, x, y, xleft, yleft, linewidth, PL_CAP_BUTT, m);
  183. _set_line_end_bbox (bufp,x, y, xright, yright, linewidth, PL_CAP_BUTT, m);
  184. }
  185. else
  186. {
  187. mitrelen = sqrt (1.0 / (2.0 - 2.0 * cosphi)) * linewidth;
  188. vsum.x = v1.x + v2.x;
  189. vsum.y = v1.y + v2.y;
  190. _vscale (&vsum, mitrelen);
  191. x -= vsum.x;
  192. y -= vsum.y;
  193. _update_bbox (bufp, XD_INTERNAL(x,y,m), YD_INTERNAL(x,y,m));
  194. }
  195. }
  196. break;
  197. case PL_JOIN_TRIANGULAR:
  198. /* add a miter vertex, and same vertices as when bevelling */
  199. v1.x = xleft - x;
  200. v1.y = yleft - y;
  201. v2.x = xright - x;
  202. v2.y = yright - y;
  203. vsum.x = v1.x + v2.x;
  204. vsum.y = v1.y + v2.y;
  205. _vscale (&vsum, 0.5 * linewidth);
  206. x -= vsum.x;
  207. y -= vsum.y;
  208. _update_bbox (bufp, XD_INTERNAL(x,y,m), YD_INTERNAL(x,y,m));
  209. x += vsum.x;
  210. y += vsum.y;
  211. /* fall through */
  212. case PL_JOIN_BEVEL:
  213. _set_line_end_bbox (bufp, x, y, xleft, yleft, linewidth, PL_CAP_BUTT, m);
  214. _set_line_end_bbox (bufp, x, y, xright, yright, linewidth, PL_CAP_BUTT, m);
  215. break;
  216. case PL_JOIN_ROUND:
  217. halfwidth = 0.5 * linewidth;
  218. _set_ellipse_bbox (bufp, x, y, halfwidth, halfwidth, 1.0, 0.0, 0.0, m);
  219. break;
  220. }
  221. }
  222. /* Update bounding box due to drawing of a quadratic Bezier segment. This
  223. takes into account only extremal x/y values in the interior of the
  224. segment, i.e. it doesn't take the endpoints into account. */
  225. /* WARNING: Like _set_ellipse_bbox above, this does not properly take line
  226. width into account. The boundary of a `thick Bezier' is not a nice
  227. curve at all. */
  228. #define QUAD_COOR(t,x0,x1,x2) (((x0)-2*(x1)+(x2))*t*t + 2*((x1)-(x2))*t + (x2))
  229. void
  230. _set_bezier2_bbox (plOutbuf *bufp, double x0, double y0, double x1, double y1, double x2, double y2, double device_line_width, double m[6])
  231. {
  232. double a_x, b_x, t_x;
  233. double a_y, b_y, t_y;
  234. double x, y, xdevice, ydevice;
  235. double device_halfwidth = 0.5 * device_line_width;
  236. /* compute coeffs of linear equation at+b=0, for both x and y coors */
  237. a_x = x0 - 2 * x1 + x2;
  238. b_x = (x1 - x2);
  239. a_y = y0 - 2 * y1 + y2;
  240. b_y = (y1 - y2);
  241. if (a_x != 0.0) /* can solve the linear eqn. */
  242. {
  243. t_x = -b_x / a_x;
  244. if (t_x > 0.0 && t_x < 1.0) /* root is in meaningful range */
  245. {
  246. x = QUAD_COOR(t_x, x0, x1, x2);
  247. y = QUAD_COOR(t_x, y0, y1, y2);
  248. xdevice = XD_INTERNAL(x,y,m);
  249. ydevice = YD_INTERNAL(x,y,m);
  250. _update_bbox (bufp, xdevice + device_halfwidth, ydevice);
  251. _update_bbox (bufp, xdevice - device_halfwidth, ydevice);
  252. }
  253. }
  254. if (a_y != 0.0) /* can solve the linear eqn. */
  255. {
  256. t_y = -b_y / a_y;
  257. if (t_y > 0.0 && t_y < 1.0) /* root is in meaningful range */
  258. {
  259. x = QUAD_COOR(t_y, x0, x1, x2);
  260. y = QUAD_COOR(t_y, y0, y1, y2);
  261. xdevice = XD_INTERNAL(x,y,m);
  262. ydevice = YD_INTERNAL(x,y,m);
  263. _update_bbox (bufp, xdevice, ydevice + device_halfwidth);
  264. _update_bbox (bufp, xdevice, ydevice - device_halfwidth);
  265. }
  266. }
  267. }
  268. /* Update bounding box due to drawing of a cubic Bezier segment. This
  269. takes into account only extremal x/y values in the interior of the
  270. segment, i.e. it doesn't take the endpoints into account. */
  271. /* WARNING: Like _set_ellipse_bbox above, this does not properly take line
  272. width into account. The boundary of a `thick Bezier' is not a nice
  273. curve at all. */
  274. #define CUBIC_COOR(t,x0,x1,x2,x3) (((x0)-3*(x1)+3*(x2)-(x3))*t*t*t + 3*((x1)-2*(x2)+(x3))*t*t + 3*((x2)-(x3))*t + (x3))
  275. void
  276. _set_bezier3_bbox (plOutbuf *bufp, double x0, double y0, double x1, double y1, double x2, double y2, double x3, double y3, double device_line_width, double m[6])
  277. {
  278. double a_x, b_x, c_x, s_x, t_x;
  279. double a_y, b_y, c_y, s_y, t_y;
  280. double x, y, xdevice, ydevice;
  281. double device_halfwidth = 0.5 * device_line_width;
  282. double sqrt_disc;
  283. /* compute coeffs of quad. equation at^2+bt+c=0, for both x and y coors */
  284. a_x = x0 - 3 * x1 + 3 * x2 - x3;
  285. b_x = 2 * (x1 - 2 * x2 + x3);
  286. c_x = x2 - x3;
  287. a_y = y0 - 3 * y1 + 3 * y2 - y3;
  288. b_y = 2 * (y1 - 2 * y2 + y3);
  289. c_y = y2 - y3;
  290. if (a_x != 0.0) /* can solve the quadratic */
  291. {
  292. sqrt_disc = sqrt (b_x * b_x - 4 * a_x * c_x);
  293. s_x = (- b_x + sqrt_disc) / (2 * a_x);
  294. t_x = (- b_x - sqrt_disc) / (2 * a_x);
  295. if (s_x > 0.0 && s_x < 1.0) /* root is in meaningful range */
  296. {
  297. x = CUBIC_COOR(s_x, x0, x1, x2, x3);
  298. y = CUBIC_COOR(s_x, y0, y1, y2, y3);
  299. xdevice = XD_INTERNAL(x,y,m);
  300. ydevice = YD_INTERNAL(x,y,m);
  301. _update_bbox (bufp, xdevice + device_halfwidth, ydevice);
  302. _update_bbox (bufp, xdevice - device_halfwidth, ydevice);
  303. }
  304. if (t_x > 0.0 && t_x < 1.0) /* root is in meaningful range */
  305. {
  306. x = CUBIC_COOR(t_x, x0, x1, x2, x3);
  307. y = CUBIC_COOR(t_x, y0, y1, y2, y3);
  308. xdevice = XD_INTERNAL(x,y,m);
  309. ydevice = YD_INTERNAL(x,y,m);
  310. _update_bbox (bufp, xdevice + device_halfwidth, ydevice);
  311. _update_bbox (bufp, xdevice - device_halfwidth, ydevice);
  312. }
  313. }
  314. if (a_y != 0.0) /* can solve the quadratic */
  315. {
  316. sqrt_disc = sqrt (b_y * b_y - 4 * a_y * c_y);
  317. s_y = (- b_y + sqrt_disc) / (2 * a_y);
  318. t_y = (- b_y - sqrt_disc) / (2 * a_y);
  319. if (s_y > 0.0 && s_y < 1.0) /* root is in meaningful range */
  320. {
  321. x = CUBIC_COOR(s_y, x0, x1, x2, x3);
  322. y = CUBIC_COOR(s_y, y0, y1, y2, y3);
  323. xdevice = XD_INTERNAL(x,y,m);
  324. ydevice = YD_INTERNAL(x,y,m);
  325. _update_bbox (bufp, xdevice, ydevice + device_halfwidth);
  326. _update_bbox (bufp, xdevice, ydevice - device_halfwidth);
  327. }
  328. if (t_y > 0.0 && t_y < 1.0) /* root is in meaningful range */
  329. {
  330. x = CUBIC_COOR(t_y, x0, x1, x2, x3);
  331. y = CUBIC_COOR(t_y, y0, y1, y2, y3);
  332. xdevice = XD_INTERNAL(x,y,m);
  333. ydevice = YD_INTERNAL(x,y,m);
  334. _update_bbox (bufp, xdevice, ydevice + device_halfwidth);
  335. _update_bbox (bufp, xdevice, ydevice - device_halfwidth);
  336. }
  337. }
  338. }