g_space.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 the space method, which is a standard part of
  16. libplot. It sets the mapping from user coordinates to display
  17. coordinates. On the display device, the drawing region is a fixed
  18. rectangle (usually a square). The arguments to the space method are the
  19. lower left and upper right vertices of a `window' (a drawing rectangle),
  20. in user coordinates. This window, whose axes are aligned with the
  21. coordinate axes, will be mapped affinely onto the drawing region on the
  22. display device.
  23. Equivalently, the space method sets the transformation matrix attribute
  24. that will be used for graphical objects that are subsequently drawn.
  25. Any transformation matrix produced by invoking space() will necessarily
  26. preserve coordinate axes.
  27. This file also contains the space2 method, which is a GNU extension to
  28. libplot. The arguments to the space2 method are the vertices of an
  29. `affine window' (a drawing parallelogram), in user coordinates. (The
  30. specified vertices are the lower left, the lower right, and the upper
  31. left.) This window will be mapped affinely onto the drawing region on
  32. the display device. Transformation matrices produced by invoking
  33. space() do not necessarily preserve coordinate axes.
  34. space and space2 are simply wrappers around the fsetmatrix() method. */
  35. /* This file also contains the fsetmatrix method, which is a GNU extension
  36. to libplot. Much as in Postscript, it sets the transformation matrix
  37. from user coordinates to NDC (normalized device coordinates). This, in
  38. turn, determines the map from user coordinates to device coordinates.
  39. The resulting transformation matrix will be used as an attribute of
  40. objects that are subsequently drawn on the graphics display. */
  41. /* This file also contains the fconcat method, which is a GNU extension to
  42. libplot. fconcat is simply a wrapper around fsetmatrix. As in
  43. Postscript, it left-multiplies the transformation matrix from user
  44. coordinates to NDC coordinates by a specified matrix. That is, it
  45. modifies the affine transformation from user coordinates to NDC and
  46. hence to device coordinates, by requiring that the transformation
  47. currently in effect be be preceded by a specified affine
  48. transformation. */
  49. /* N.B. Invoking fsetmatrix causes the default line width and default font
  50. size, which are expressed in user units, to be recomputed. That is
  51. because those two quantities are specified as a fraction of the size of
  52. the display: in device terms, rather than in terms of user units. The
  53. idea is that no matter what the arguments of fsetmatrix are, switching
  54. later to the default line width or default font size, by passing an
  55. out-of-bounds argument to linewidth() or fontsize(), should yield a
  56. reasonable result. */
  57. #include "sys-defines.h"
  58. #include "extern.h"
  59. /* potential roundoff error (absolute, for defining boundary of display) */
  60. #define ROUNDING_FUZZ 0.0000001
  61. /* potential roundoff error (relative, used for checking isotropy etc.) */
  62. #define OTHER_FUZZ 0.0000001
  63. /* The vertices of the parallelogram in user space have coordinates (going
  64. counterclockwise) (x0,y0), (x1,y1), (x1,y1)+(x2,y2)-(x0,y0), and
  65. (x2,y2). */
  66. int
  67. _API_fspace2 (R___(Plotter *_plotter) double x0, double y0, double x1, double y1, double x2, double y2)
  68. {
  69. double s[6];
  70. double v0x, v0y, v1x, v1y, v2x, v2y;
  71. double cross;
  72. if (!_plotter->data->open)
  73. {
  74. _plotter->error (R___(_plotter)
  75. "fspace2: invalid operation");
  76. return -1;
  77. }
  78. /* Compute affine transformation from user frame to NDC [normalized
  79. device coordinates] frame. The parallelogram in the user frame is
  80. mapped to the square [0,1]x[0,1] in the NDC frame. */
  81. v0x = x0;
  82. v0y = y0;
  83. v1x = x1 - x0;
  84. v1y = y1 - y0;
  85. v2x = x2 - x0;
  86. v2y = y2 - y0;
  87. cross = v1x * v2y - v1y * v2x;
  88. if (cross == 0.0)
  89. {
  90. _plotter->error (R___(_plotter) "the requested singular affine transformation cannot be performed");
  91. return -1;
  92. }
  93. /* linear transformation */
  94. s[0] = v2y / cross;
  95. s[1] = -v1y / cross;
  96. s[2] = -v2x / cross;
  97. s[3] = v1x / cross;
  98. /* translation */
  99. s[4] = - (v0x * v2y - v0y * v2x) / cross;
  100. s[5] = (v0x * v1y - v0y * v1x) / cross;
  101. return _API_fsetmatrix (R___(_plotter)
  102. s[0], s[1], s[2], s[3], s[4], s[5]);
  103. }
  104. int
  105. _API_fspace (R___(Plotter *_plotter) double x0, double y0, double x1, double y1)
  106. {
  107. return _API_fspace2 (R___(_plotter) x0, y0, x1, y0, x0, y1);
  108. }
  109. int
  110. _API_fsetmatrix (R___(Plotter *_plotter) double m0, double m1, double m2, double m3, double m4, double m5)
  111. {
  112. int i;
  113. double s[6], t[6];
  114. double norm, min_sing_val, max_sing_val;
  115. if (!_plotter->data->open)
  116. {
  117. _plotter->error (R___(_plotter)
  118. "fsetmatrix: invalid operation");
  119. return -1;
  120. }
  121. /* linear transformation */
  122. s[0] = m0;
  123. s[1] = m1;
  124. s[2] = m2;
  125. s[3] = m3;
  126. /* translation */
  127. s[4] = m4;
  128. s[5] = m5;
  129. /* store new user_frame->NDC_frame map in drawing state */
  130. for (i = 0; i < 6; i++)
  131. _plotter->drawstate->transform.m_user_to_ndc[i] = s[i];
  132. /* compute the user_frame -> device_frame map, as product of this map
  133. with the following NDC_frame->device_frame map: store in drawing state */
  134. _matrix_product (s, _plotter->data->m_ndc_to_device, t);
  135. for (i = 0; i < 6; i++)
  136. _plotter->drawstate->transform.m[i] = t[i];
  137. /* for convenience, precompute boolean properties of the
  138. user_frame->device_frame map: store in drawing state */
  139. /* Does the user_frame->device_frame map preserve axis directions? */
  140. _plotter->drawstate->transform.axes_preserved =
  141. (t[1] == 0.0 && t[2] == 0.0) ? true : false;
  142. /* Is the user_frame->device_frame map a uniform scaling (possibly
  143. involving a rotation or reflection)? We need to know this because
  144. it's only uniform maps that map circles to circles, and circular arcs
  145. to circular arcs. Also some Plotters, e.g. Fig Plotters, don't
  146. support non-uniformly transformed fonts. */
  147. #define IS_ZERO(arg) (IS_ZERO1(arg) && IS_ZERO2(arg))
  148. #define IS_ZERO1(arg) (FABS(arg) < OTHER_FUZZ * DMAX(t[0] * t[0], t[1] * t[1]))
  149. #define IS_ZERO2(arg) (FABS(arg) < OTHER_FUZZ * DMAX(t[2] * t[2], t[3] * t[3]))
  150. /* if row vectors are of equal length and orthogonal... */
  151. if (IS_ZERO(t[0] * t[0] + t[1] * t[1] - t[2] * t[2] - t[3] * t[3])
  152. &&
  153. IS_ZERO(t[0] * t[2] + t[1] * t[3]))
  154. /* map's scaling is uniform */
  155. _plotter->drawstate->transform.uniform = true;
  156. else
  157. /* map's scaling not uniform */
  158. _plotter->drawstate->transform.uniform = false;
  159. /* Does the user_frame->physical_frame map involve a reflection? This is
  160. useful to know because some Plotters, e.g. Fig Plotters, don't support
  161. reflected fonts, even if they're uniformly transformed.
  162. This is a tricky question, because it isn't a question about the
  163. user_frame->device_frame map alone. There's a sequence of maps:
  164. user_frame -> NDC_frame -> device_frame -> physical_frame
  165. If the device_frame uses `flipped y' coordinates, then by definition,
  166. the default NDC_frame->device_frame map and the
  167. device_frame->physical_frame map both include a reflection, so they
  168. cancel each other out.
  169. (Though depending on the Plotter, non-default behavior could obtain.
  170. For example, the PAGESIZE parameter allows the specification of xsize
  171. and ysize, and if exactly one of these two is negative, the
  172. NDC_frame->device_frame map will include an extra reflection.)
  173. What we do is look at the `sign' or orientation-preservingness of the
  174. user_frame->device_frame map, and flip it if the
  175. device_frame->physical_frame map is flagged as `flipped y'. */
  176. {
  177. double det;
  178. det = t[0] * t[3] - t[1] * t[2];
  179. _plotter->drawstate->transform.nonreflection
  180. = ((_plotter->data->flipped_y ? -1 : 1) * det >= 0) ? true : false;
  181. }
  182. /* DO SOME OTHER STUFF, ALL RELATED TO LINE WIDTHS AND FONT SIZES */
  183. /* For scaling purposes, compute matrix norm of linear transformation
  184. appearing in the affine map from the user frame to the NDC frame. */
  185. /* This minimum singular value isn't really the norm. But it's close
  186. enough. */
  187. _matrix_sing_vals (s, &min_sing_val, &max_sing_val);
  188. norm = min_sing_val;
  189. /* Set new default line width in user frame. This default value will be
  190. switched to, later, if the user calls linewidth() with a negative
  191. (i.e. out-of-bound) argument. */
  192. if (_plotter->data->display_coors_type
  193. == (int)DISP_DEVICE_COORS_INTEGER_LIBXMI)
  194. /* using libxmi or a compatible rendering algorithm; so set default
  195. line width to zero (interpreted as specifying a Bresenham line) */
  196. _plotter->drawstate->default_line_width = 0.0;
  197. else
  198. /* not using libxmi or a compatible rendering algorithm; so set default
  199. line width to a nonzero fraction of the display size */
  200. {
  201. if (norm == 0.0) /* avoid division by 0 */
  202. _plotter->drawstate->default_line_width = 0.0;
  203. else
  204. _plotter->drawstate->default_line_width
  205. = PL_DEFAULT_LINE_WIDTH_AS_FRACTION_OF_DISPLAY_SIZE / norm;
  206. }
  207. if (_plotter->data->linewidth_invoked == false)
  208. /* help out lusers who rely on us to initialize the linewidth to a
  209. reasonable value, as if this were plot(3) rather than GNU libplot */
  210. {
  211. /* invoke API function flinewidth(), which computes a nominal
  212. device-frame line width, using the transformation matrix;
  213. specifying a negative linewidth switches to the default */
  214. _API_flinewidth (R___(_plotter) -1.0);
  215. /* pretend we haven't invoked flinewidth() yet, so that the luser can
  216. invoke space() and/or fsetmatrix() additional times, each time
  217. automatically resetting the linewidth */
  218. _plotter->data->linewidth_invoked = false;
  219. }
  220. else
  221. /* invoke API function merely to compute a new nominal device-frame
  222. line width, from the current user-frame line width */
  223. _API_flinewidth (R___(_plotter) _plotter->drawstate->line_width);
  224. /* Similarly, set new default font size in user frame. This default
  225. value will be switched to, later, if the user calls fontsize() with
  226. out-of-bound arguments. */
  227. if (norm == 0.0) /* avoid division by 0 */
  228. _plotter->drawstate->default_font_size = 0.0;
  229. else
  230. _plotter->drawstate->default_font_size
  231. = PL_DEFAULT_FONT_SIZE_AS_FRACTION_OF_DISPLAY_SIZE / norm;
  232. /* Help out users who rely on us to choose a reasonable font size, as if
  233. this were Unix plot(3) rather than GNU libplot. We don't wish to
  234. retrieve an actual font here, so we don't invoke _API_fontsize().
  235. However, this size will be used by the Plotter-specific method
  236. _paint_text(), which will first do the retrieval. */
  237. if (_plotter->data->fontsize_invoked == false)
  238. _plotter->drawstate->font_size = _plotter->drawstate->default_font_size;
  239. return 0;
  240. }
  241. int
  242. _API_fconcat (R___(Plotter *_plotter) double m0, double m1, double m2, double m3, double m4, double m5)
  243. {
  244. double m[6], s[6];
  245. if (!_plotter->data->open)
  246. {
  247. _plotter->error (R___(_plotter)
  248. "fconcat: invalid operation");
  249. return -1;
  250. }
  251. m[0] = m0;
  252. m[1] = m1;
  253. m[2] = m2;
  254. m[3] = m3;
  255. m[4] = m4;
  256. m[5] = m5;
  257. /* compute new user->NDC affine map */
  258. _matrix_product (m, _plotter->drawstate->transform.m_user_to_ndc, s);
  259. /* set it in drawing state */
  260. return _API_fsetmatrix (R___(_plotter)
  261. s[0], s[1], s[2], s[3], s[4], s[5]);
  262. }
  263. /* Compute the affine transformation from the NDC frame to the device
  264. frame. This is an internal method, called by any Plotter at
  265. initialization time, or at latest during the first invocation of
  266. openpl().
  267. The square [0,1]x[0,1] in the NDC frame is mapped to the viewport in the
  268. device frame (a square or rectangular region). So, the
  269. NDC_frame->device_frame map preserves coordinate axes. (Though either
  270. the x or y axis may be flipped, the latter being more common, because
  271. some devices' native coordinate system has a flipped-y convention, which
  272. means that the final device_frame->physical_frame map flips in the y
  273. direction.)
  274. There is support for the ROTATION Plotter parameter, which allows the
  275. NDC frame to be rotated by 90, 180, or 270 degrees, before it is mapped
  276. to the device frame. */
  277. bool
  278. _compute_ndc_to_device_map (plPlotterData *data)
  279. {
  280. double t[6];
  281. double map_1[6], map_2[6], map_1a[6], map_1b[6], map_1ab[6], map_1c[6];
  282. double device_x_left, device_x_right, device_y_bottom, device_y_top;
  283. const char *rotation_s;
  284. double rotation_angle;
  285. int i;
  286. /* begin by computing device coordinate ranges */
  287. switch (data->display_model_type)
  288. {
  289. case (int)DISP_MODEL_PHYSICAL:
  290. /* Plotter has a physical display, ranges in device coordinates of
  291. the viewport are known (they're expressed in inches, and are
  292. computed from the PAGESIZE parameter when the Plotter is created,
  293. see ?_defplot.c). E.g., AI, Fig, HPGL, PCL, and PS Plotters. */
  294. {
  295. device_x_left = data->xmin;
  296. device_x_right = data->xmax;
  297. device_y_bottom = data->ymin;
  298. device_y_top = data->ymax;
  299. }
  300. break;
  301. case (int)DISP_MODEL_VIRTUAL:
  302. default:
  303. /* Plotter has a display, but its size isn't specified in physical
  304. units such as inches. E.g., CGM, SVG, GIF, PNM, Tektronix, X, and
  305. X Drawable Plotters. CGM and SVG Plotters are hybrids of a sort:
  306. the PAGESIZE parameter is meaningful for them, as far as nominal
  307. viewport size goes, but we treat a CGM or SVG display as `virtual'
  308. because a CGM or SVG viewer or interpreter is free to ignore the
  309. requested viewport size. */
  310. {
  311. switch ((int)data->display_coors_type)
  312. {
  313. case (int)DISP_DEVICE_COORS_REAL:
  314. default:
  315. /* Real-coordinate virtual display device. E.g., generic and
  316. Metafile Plotters; also SVG Plotters. */
  317. device_x_left = data->xmin;
  318. device_x_right = data->xmax;
  319. device_y_bottom = data->ymin;
  320. device_y_top = data->ymax;
  321. break;
  322. case (int)DISP_DEVICE_COORS_INTEGER_LIBXMI:
  323. case (int)DISP_DEVICE_COORS_INTEGER_NON_LIBXMI:
  324. /* Integer-coordinate virtual display device, in the sense that
  325. we emit integer coordinates only (sometimes by choice).
  326. Of the Plotters that have virtual displays (see above), GIF,
  327. PNM, X, and X Drawable Plotters use libxmi-compatible scan
  328. conversion; Tektronix Plotters and CGM Plotters do not.
  329. In both cases, compute device coordinate ranges from imin,
  330. imax, jmin, jmax, which are already available (see
  331. ?_defplot.c; e.g., for Plotters with adjustable-size
  332. displays, they are taken from the BITMAPSIZE parameter).
  333. The subtraction/addition of 0.5-ROUNDING_FUZZ, which widens
  334. the rectangle by nearly 0.5 pixel on each side, is magic. */
  335. {
  336. /* test whether NCD_frame->device_frame map reflects in the x
  337. and/or y direction */
  338. double x_sign = (data->imin < data->imax ? 1.0 : -1.0);
  339. double y_sign = (data->jmin < data->jmax ? 1.0 : -1.0);
  340. device_x_left = ((double)(data->imin)
  341. + x_sign * (- 0.5 + ROUNDING_FUZZ));
  342. device_x_right = ((double)(data->imax)
  343. + x_sign * (0.5 - ROUNDING_FUZZ));
  344. device_y_bottom = ((double)(data->jmin)
  345. + y_sign * (- 0.5 + ROUNDING_FUZZ));
  346. device_y_top = ((double)(data->jmax)
  347. + y_sign * (0.5 - ROUNDING_FUZZ));
  348. }
  349. break;
  350. }
  351. }
  352. break;
  353. }
  354. /* Device coordinate ranges now known, so work out transformation from
  355. NDC frame to device frame; take ROTATION parameter into account.
  356. The (NDC_frame)->(device_frame) map is the composition of two maps:
  357. (1) a preliminary rotation about (0.5,0.5) in the NDC frame,
  358. (2) the default (NDC_frame)->(device frame) map,
  359. in that order. And the first of these is the composition of three:
  360. (1a) translate by -(0.5,0.5)
  361. (1b) rotate by ROTATION degrees about (0,0)
  362. (1c) translate by +(0.5,0.5).
  363. */
  364. /* compute map #1 as product of maps 1a, 1b, 1c */
  365. rotation_s = (const char *)_get_plot_param (data, "ROTATION");
  366. if (rotation_s == NULL)
  367. rotation_s = (const char *)_get_default_plot_param ("ROTATION");
  368. if (strcmp (rotation_s, "no") == 0)
  369. rotation_angle = 0.0; /* "no" means 0 degrees */
  370. else if (strcmp (rotation_s, "yes") == 0)
  371. rotation_angle = 90.0; /* "yes" means 90 degrees */
  372. else if (sscanf (rotation_s, "%lf", &rotation_angle) <= 0)
  373. rotation_angle = 0.0; /* default */
  374. rotation_angle *= (M_PI / 180.0); /* convert to radians */
  375. map_1a[0] = map_1a[3] = 1.0;
  376. map_1a[1] = map_1a[2] = 0.0;
  377. map_1a[4] = map_1a[5] = -0.5;
  378. map_1b[0] = cos (rotation_angle);
  379. map_1b[1] = sin (rotation_angle);
  380. map_1b[2] = - sin (rotation_angle);
  381. map_1b[3] = cos (rotation_angle);
  382. map_1b[4] = map_1b[5] = 0.0;
  383. map_1c[0] = map_1c[3] = 1.0;
  384. map_1c[1] = map_1c[2] = 0.0;
  385. map_1c[4] = map_1c[5] = 0.5;
  386. _matrix_product (map_1a, map_1b, map_1ab);
  387. _matrix_product (map_1ab, map_1c, map_1);
  388. /* compute map #2: the default (NDC frame)->(device frame) map */
  389. /* NDC point (0,0) [lower left corner] gets mapped into this */
  390. map_2[4] = device_x_left;
  391. map_2[5] = device_y_bottom;
  392. /* NDC vector (1,0) gets mapped into this */
  393. map_2[0] = device_x_right - device_x_left;
  394. map_2[1] = 0.0;
  395. /* NDC vector (0,1) gets mapped into this */
  396. map_2[2] = 0.0;
  397. map_2[3] = device_y_top - device_y_bottom;
  398. /* compute (NDC_frame)->(device frame) map as a product of maps 1,2 */
  399. _matrix_product (map_1, map_2, t);
  400. /* set affine transformation in Plotter */
  401. for (i = 0; i < 6; i++)
  402. data->m_ndc_to_device[i] = t[i];
  403. return true;
  404. }