1########################################################################
2##
3## Copyright (C) 2007-2021 The Octave Project Developers
4##
5## See the file COPYRIGHT.md in the top-level directory of this
6## distribution or <https://octave.org/copyright/>.
7##
8## This file is part of Octave.
9##
10## Octave is free software: you can redistribute it and/or modify it
11## under the terms of the GNU General Public License as published by
12## the Free Software Foundation, either version 3 of the License, or
13## (at your option) any later version.
14##
15## Octave is distributed in the hope that it will be useful, but
16## WITHOUT ANY WARRANTY; without even the implied warranty of
17## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18## GNU General Public License for more details.
19##
20## You should have received a copy of the GNU General Public License
21## along with Octave; see the file COPYING.  If not, see
22## <https://www.gnu.org/licenses/>.
23##
24########################################################################
25
26function geometryimages (d, nm, typ)
27
28  if (strcmp (typ, "txt"))
29    image_as_txt (d, nm);
30    return;
31  endif
32
33  set_graphics_toolkit ();
34  set_print_size ();
35  hide_output ();
36  outfile = fullfile (d, [nm "." typ]);
37  if (strcmp (typ, "png"))
38    set (groot, "defaulttextfontname", "*");
39  endif
40  if (strcmp (typ, "eps"))
41    d_typ = "-depsc2";
42  else
43    d_typ = ["-d" typ];
44  endif
45
46  if (! __have_feature__ ("QHULL")
47      && any (strcmp (nm, {"voronoi", "griddata", "convhull", "delaunay", ...
48                           "triplot"})))
49    sombreroimage (outfile, typ, d_typ);
50  elseif (strcmp (nm, "voronoi"))
51    rand ("state", 9);
52    x = rand (10, 1);
53    y = rand (10, 1);
54    tri = delaunay (x, y);
55    [vx, vy] = voronoi (x, y, tri);
56    triplot (tri, x, y, "b");
57    hold on;
58    plot (vx, vy, "r");
59    [r, c] = tri2circ (tri(end,:), x, y);
60    pc = [-1:0.01:1];
61    xc = r * sin (pi*pc) + c(1);
62    yc = r * cos (pi*pc) + c(2);
63    plot (xc, yc, "g-", "LineWidth", 3);
64    axis ([0, 1, 0, 1]);
65    set (gca, "dataaspectratio", [1, 1, 1],
66              "plotboxaspectratio", [1, 1, 1]);
67    print (outfile, d_typ);
68  elseif (strcmp (nm, "triplot"))
69    rand ("state", 2)
70    x = rand (20, 1);
71    y = rand (20, 1);
72    tri = delaunay (x, y);
73    triplot (tri, x, y);
74    print (outfile, d_typ);
75  elseif (strcmp (nm, "griddata"))
76    rand ("state", 1);
77    x = 2 * rand (1000,1) - 1;
78    y = 2 * rand (size (x)) - 1;
79    z = sin (2 * (x.^2 + y.^2));
80    [xx,yy] = meshgrid (linspace (-1,1,32));
81    zz = griddata (x, y, z, xx, yy);
82    mesh (xx, yy, zz);
83    print (outfile, d_typ);
84  elseif (strcmp (nm, "convhull"))
85    x = -3:0.05:3;
86    y = abs (sin (x));
87    k = convhull (x, y);
88    plot (x(k),y(k),'r-', x,y,'b+');
89    axis ([-3.05, 3.05, -0.05, 1.05]);
90    print (outfile, d_typ);
91  elseif (strcmp (nm, "delaunay"))
92    rand ("state", 1);
93    x = rand (1, 10);
94    y = rand (1, 10);
95    T = delaunay (x, y);
96    X = [ x(T(:,1)); x(T(:,2)); x(T(:,3)); x(T(:,1)) ];
97    Y = [ y(T(:,1)); y(T(:,2)); y(T(:,3)); y(T(:,1)) ];
98    axis ([0, 1, 0, 1]);
99    plot (X,Y,"b", x,y,"r*");
100    print (outfile, d_typ);
101  elseif (strcmp (nm, "inpolygon"))
102    randn ("state", 2);
103    x = randn (100, 1);
104    y = randn (100, 1);
105    vx = cos (pi * [-1 : 0.1: 1]);
106    vy = sin (pi * [-1 : 0.1 : 1]);
107    in = inpolygon (x, y, vx, vy);
108    plot (vx, vy, x(in), y(in), "r+", x(!in), y(!in), "bo");
109    axis ([-2, 2, -2, 2]);
110    print (outfile, d_typ);
111  else
112    error ("unrecognized plot requested");
113  endif
114  hide_output ();
115endfunction
116
117function [r, c] = tri2circ (tri, xx, yy)
118  x = xx(tri);
119  y = yy(tri);
120  m = (y(1:end-1) - y(2:end)) ./ (x(1:end-1) - x(2:end));
121  xc = (prod(m) .* (y(1) - y(end)) + m(end)*(x(1)+x(2)) - m(1)*(x(2)+x(3))) ...
122        ./ (2 * (m(end) - m(1)));
123  yc = - (xc - (x(2) + x(3))./2) ./ m(end) + (y(2) + y(3)) / 2;
124  c = [xc, yc];
125  r = sqrt ((xc - x(1)).^2 + (yc - y(1)).^2);
126endfunction
127
128function sombreroimage (outfile, typ, d_typ)
129  if (strcmp (typ, "txt"))
130    fid = fopen (outfile, "wt");
131    fputs (fid, "+-----------------------------+\n");
132    fputs (fid, "| Image unavailable because   |\n");
133    fputs (fid, "| of a missing QHULL library. |\n");
134    fputs (fid, "+-----------------------------+\n");
135    fclose (fid);
136    return;
137  else
138    [x, y, z] = sombrero ();
139    unwind_protect
140      mesh (x, y, z);
141      title ({"Sorry, graphics are unavailable because Octave was",
142              "compiled without the QHULL library."});
143    unwind_protect_cleanup
144      print (outfile, d_typ);
145      hide_output ();
146    end_unwind_protect
147  endif
148endfunction
149
150## This function no longer sets the graphics toolkit; That is now done
151## automatically by C++ code which will ordinarily choose 'qt', but might
152## choose gnuplot on older systems.  Only a complete lack of plotting is a
153## problem.
154function set_graphics_toolkit ()
155  if (isempty (available_graphics_toolkits ()))
156    error ("no graphics toolkit available for plotting");
157  elseif (strcmp ("qt", graphics_toolkit ())
158          && __have_feature__ ("QT_OFFSCREEN"))
159    ## Use qt with QOffscreenSurface for plot
160  elseif (! strcmp ("gnuplot", graphics_toolkit ()))
161    if (! any (strcmp ("gnuplot", available_graphics_toolkits ())))
162      error ("no graphics toolkit available for offscreen plotting");
163    else
164      graphics_toolkit ("gnuplot");
165    endif
166  endif
167endfunction
168
169function set_print_size ()
170  image_size = [5.0, 3.5]; # in inches, 16:9 format
171  border = 0;              # For postscript use 50/72
172  set (groot, "defaultfigurepapertype", "<custom>");
173  set (groot, "defaultfigurepaperorientation", "landscape");
174  set (groot, "defaultfigurepapersize", image_size + 2*border);
175  set (groot, "defaultfigurepaperposition", [border, border, image_size]);
176  ## FIXME: Required until listener for legend exists (bug #39697)
177  set (groot, "defaultfigureposition", [ 72*[border, border, image_size] ]);
178endfunction
179
180## Use this function before plotting commands and after every call to print
181## since print() resets output to stdout (unfortunately, gnuplot can't pop
182## output as it can the terminal type).
183function hide_output ()
184  hf = figure (1, "visible", "off");
185endfunction
186
187## generate something for the texinfo @image command to process
188function image_as_txt (d, nm)
189  fid = fopen (fullfile (d, [nm ".txt"]), "wt");
190  fputs (fid, "\n");
191  fputs (fid, "+---------------------------------+\n");
192  fputs (fid, "| Image unavailable in text mode. |\n");
193  fputs (fid, "+---------------------------------+\n");
194  fclose (fid);
195endfunction
196