Debian Codegraph @

dwsys/Graphics_extensions.cpp

raw
1 /* Graphics_extensions.cpp
2 *
3 * Copyright (C) 2012-2022 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 djmw 20120727 latest modification
21 */
22
23 #include "NUM2.h"
24 #include "Permutation.h"
25 #include "Graphics_extensions.h"
26
27 #include "enums_getText.h"
28 #include "Graphics_extensions_enums.h"
29 #include "enums_getValue.h"
30 #include "Graphics_extensions_enums.h"
31
32 double Graphics_getFontSizeInsideBox (Graphics g, double widthWC, double heightWCx, double maxNumberOfCharacters_line, double maxNumberOfLines) {
33 const double pointsPerMillimetre = 72.0 / (10.0 * 2.54);
34 const double boxWidth_points = Graphics_dxWCtoMM (g, widthWC) * pointsPerMillimetre;
35 const double boxHeight_points = Graphics_dyWCtoMM (g, heightWCx) * pointsPerMillimetre;
36 return std::min (boxHeight_points / maxNumberOfLines , 2.0 * boxWidth_points / maxNumberOfCharacters_line);
37 }
38
39 /*
40 Draw a box plot of x [1..x.size]. The vertical center line of the plot
41 is at position 'x'. The rectangle box is 2*w wide, the whisker 2*r.
42 All drawing outside [ymin, ymax] is clipped.
43 */
44
45 void Graphics_boxAndWhiskerPlot (Graphics g, constVEC data, double x, double r, double w, double ymin, double ymax) {
46 const int lineType = Graphics_inqLineType (g);
47
48 Melder_assert (r > 0.0 && w > 0.0);
49 if (data.size < 3)
50 return;
51 /*
52 Sort the data (ascending: x [1] <= ... <= x [ndata]).
53 Get the median (q50) and the upper and lower quartile points
54 (q25 and q75).
55 Now q25 and q75 are the lower and upper hinges, respectively.
56 The fences can be calculated from q25 and q75.
57 The spread is defined as the interquartile range or midrange
58 |q75 - q25|.
59 The fences are defined as:
60 (lower/upper) innerfence = (lower/upper) hinge +/- 1.5 hspread
61 (lower/upper) outerfence = (lower/upper) hinge +/- 3.0 hspread
62 */
63
64 autoVEC sorted = sort_e_VEC (data);
65
66 if (ymax <= ymin) {
67 ymin = sorted [1];
68 ymax = sorted [sorted.size];
69 }
70 if (sorted [1] > ymax || sorted [sorted.size] < ymin)
71 return;
72
73 const double mean = NUMmean (sorted.get());
74 const double q25 = NUMquantile (sorted.get(), 0.25);
75 const double q50 = NUMquantile (sorted.get(), 0.5);
76 const double q75 = NUMquantile (sorted.get(), 0.75);
77
78 const double hspread = fabs (q75 - q25);
79 const double lowerOuterFence = q25 - 3.0 * hspread;
80 const double lowerInnerFence = q25 - 1.5 * hspread;
81 const double upperInnerFence = q75 + 1.5 * hspread;
82 const double upperOuterFence = q75 + 3.0 * hspread;
83 /*
84 Decide whether there are outliers that have to be drawn.
85 First process sorted from below.
86 */
87 integer i = 1;
88 while (i <= sorted.size && sorted [i] < ymin)
89 i ++;
90 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF);
91 while (i <= sorted.size && sorted [i] < lowerOuterFence)
92 Graphics_text (g, x, sorted [i ++], U"o");
93
94 while (i <= sorted.size && sorted [i] < lowerInnerFence)
95 Graphics_text (g, x, sorted [i ++], U"*");
96
97 const double lowerWhisker = sorted [i] < q25 ? sorted [i] : lowerInnerFence;
98 if (lowerWhisker > ymax)
99 return;
100 /*
101 Next process data from above.
102 */
103 const integer ilow = i;
104 i = sorted.size;
105 while (i >= ilow && sorted [i] > ymax)
106 i --;
107 while (i >= ilow && sorted [i] > upperOuterFence)
108 Graphics_text (g, x, sorted [i --], U"o");
109
110 while (i >= ilow && sorted [i] > upperInnerFence)
111 Graphics_text (g, x, sorted [i --], U"*");
112
113 const double upperWhisker = sorted [i] > q75 ? sorted [i] : upperInnerFence;
114 if (upperWhisker < ymin)
115 return;
116 /*
117 Determine what parts of the "box" have to be drawn within the
118 range [ymin, ymax].
119 Horizontal lines first.
120 */
121 double y1 = lowerWhisker;
122 if (ymax > y1 && ymin < y1)
123 Graphics_line (g, x - r, y1, x + r, y1);
124 y1 = q25;
125 if (ymax > y1 && ymin < y1)
126 Graphics_line (g, x - w, y1, x + w, y1);
127 y1 = q50;
128 if (ymax > y1 && ymin < y1)
129 Graphics_line (g, x - w, y1, x + w, y1);
130 y1 = q75;
131 if (ymax > y1 && ymin < y1)
132 Graphics_line (g, x - w, y1, x + w, y1);
133 y1 = upperWhisker;
134 if (ymax > y1 && ymin < y1)
135 Graphics_line (g, x - r, y1, x + r, y1);
136 /*
137 Extension: draw the mean too.
138 */
139 y1 = mean;
140 if (ymax > y1 && ymin < y1) {
141 Graphics_setLineType (g, Graphics_DOTTED);
142 Graphics_line (g, x - w, y1, x + w, y1);
143 Graphics_setLineType (g, lineType);
144 }
145 /*
146 Vertical lines.
147 */
148 y1 = lowerWhisker;
149 double y2 = q25;
150 if (ymax > y1 && ymin < y2) {
151 y1 = y1 > ymin ? y1 : ymin;
152 y2 = y2 < ymax ? y2 : ymax;
153 Graphics_line (g, x, y1, x, y2);
154 }
155 y1 = q25;
156 y2 = q75;
157 if (ymax > y1 && ymin < y2) {
158 y1 = y1 > ymin ? y1 : ymin;
159 y2 = y2 < ymax ? y2 : ymax;
160 Graphics_line (g, x - w, y1, x - w, y2);
161 Graphics_line (g, x + w, y1, x + w, y2);
162 }
163 y1 = q75;
164 y2 = upperWhisker;
165 if (ymax > y1 && ymin < y2) {
166 y1 = y1 > ymin ? y1 : ymin;
167 y2 = y2 < ymax ? y2 : ymax;
168 Graphics_line (g, x, y1, x, y2);
169 }
170 }
171
172 void Graphics_quantileQuantilePlot (Graphics g, integer numberOfQuantiles, constVEC x, constVEC y,
173 double xmin, double xmax, double ymin, double ymax, double labelSize, conststring32 plotLabel) {
174 const double fontSize = Graphics_inqFontSize (g);
175
176 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF);
177 Graphics_setFontSize (g, labelSize);
178 autoVEC xsorted = sort_e_VEC (x);
179 autoVEC ysorted = sort_e_VEC (y);
180
181 const integer numberOfData = std::min (x.size, y.size);
182 numberOfQuantiles = std::min (numberOfData, numberOfQuantiles);
183 const double un = pow (0.5, 1.0 / numberOfQuantiles);
184 const double u1 = 1.0 - un;
185 if (xmin == xmax) {
186 xmin = NUMquantile (xsorted.get(), u1);
187 xmax = NUMquantile (xsorted.get(), un);
188 }
189 if (ymin == ymax) {
190 ymin = NUMquantile (ysorted.get(), u1);
191 ymax = NUMquantile (ysorted.get(), un);
192 }
193 for (integer i = 1; i <= numberOfQuantiles; i++) {
194 const double ui = i == 1 ? u1 : (i == numberOfQuantiles ? un : (i - 0.3175) / (numberOfQuantiles + 0.365));
195 const double qx = NUMquantile (xsorted.get(), ui);
196 const double qy = NUMquantile (ysorted.get(), ui);
197 if (qx < xmin || qx > xmax || qy < ymin || qy > ymax)
198 continue; // outside area
199 Graphics_text (g, qx, qy, plotLabel);
200 }
201 Graphics_setLineType (g, Graphics_DOTTED);
202 Graphics_line (g, xmin, ymin, xmax, ymax);
203 Graphics_setLineType (g, Graphics_DRAWN);
204 Graphics_setFontSize (g, fontSize);
205 }
206
207 void Graphics_lagPlot (Graphics g, constVEC data, double xmin, double xmax, integer lag, double labelSize, conststring32 plotLabel)
208 {
209 if (lag < 0 || lag >= data.size)
210 return;
211 const double fontSize = Graphics_inqFontSize (g);
212 Graphics_setFontSize (g, labelSize);
213 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF);
214 /*
215 Plot x [i] vertically and x [i-lag] horizontally
216 */
217 for (integer i = 1; i <= data.size - lag; i ++) {
218 const double x = data [i + lag], y = data [i];
219 if (x >= xmin && x <= xmax && y >= xmin && y <= xmax)
220 Graphics_text (g, x, y, plotLabel);
221 }
222 Graphics_setLineType (g, Graphics_DRAWN);
223 Graphics_setFontSize (g, fontSize);
224 }
225
226 void getGridLayout (integer numberOfItems, integer *out_numberOfRows, integer *out_numberOfColumns) {
227 integer numberOfColumns = sqrt (numberOfItems);
228 integer numberOfRows = 1 + Melder_roundDown ((numberOfItems - 1) / numberOfColumns);
229 if (numberOfColumns > numberOfRows)
230 std::swap (numberOfRows, numberOfColumns);
231 if (out_numberOfRows)
232 *out_numberOfRows = numberOfRows;
233 if (out_numberOfColumns)
234 *out_numberOfColumns = numberOfColumns;
235 }
236
237 /*
238 Implicit:
239 Rectangular grid.
240 Origin (0,0) is at left-bottom, top-right is at (1, 1)
241 */
242 integer getGridCellIndex (double x, double y, integer numberOfRows, integer numberOfColumns) {
243 const integer icol = 1 + (int) (x * numberOfColumns);
244 if (icol < 1 || icol > numberOfColumns)
245 return 0;
246 const integer irow = 1 + (int) ((1.0 - y) * numberOfRows);
247 if (irow < 1 || irow > numberOfRows)
248 return 0;
249 return (irow - 1) * numberOfColumns + icol; // left-to-right, top-to-bottom
250 }
251
252 autoLineSegmentClipper LineSegmentClipper_create (double xL, double xR, double yB, double yT) {
253 try {
254 autoLineSegmentClipper me = std::make_unique <structLineSegmentClipper> ();
255 my init (xL, xR, yB, yT);
256 return me;
257 } catch (MelderError) {
258 Melder_throw (U"Cannot create LineSegmentClipper.");
259 }
260 }
261
262 bool LineSegmentClipper_clip (LineSegmentClipper me, double& x1, double& y1, double& x2, double& y2) {
263 return my clip (x1, y1, x2, y2);
264 }
265
266 struct structLineClipper {
267 private:
268 typedef struct structHPoint {
269 double x, y, w;
270 } *HPoint;
271 /*
272 The four corners of the rectangle in homogeneous coordinates (x, y, w)
273 Order BottomLeft, BottomRight, TopRight, TopLeft
274 The edges from c[1] to c[2], c[2] to c[3], c[3] to c[4], c[4] to c[1].
275 */
276 autovector<structHPoint> corners;
277 autovector<structHPoint> edges;
278 autoINTVEC tab1, tab2;
279 struct structHPoint from = {0.0, 0.0, 1.0}, to = {0.0, 0.0, 1.0};
280
281 void cross (HPoint a, HPoint b, HPoint p) { // p = a x b
282 p -> x = a -> y * b -> w - b -> y * a -> w;
283 p -> y = b -> x * a -> w - a -> x * b -> w;
284 p -> w = a -> x * b -> y - b -> x * a -> y;
285 }
286
287 double in (HPoint a, HPoint b) {
288 return a -> x * b -> x + a -> y * b -> y + a -> w * b -> w;
289 }
290
291 bool clipLine () {
292 struct structHPoint p;
293 cross (& from, & to, & p);
294 int c = 0;
295 for (int i = 1; i <= 4; i ++)
296 if (in (& p, & corners [i]) >= 0)
297 c |= 1 << (i - 1);
298 if (c == 0 || c == 15) // case 6, Fig 5:
299 return false;
300 Melder_assert (c % 5 != 0);
301 const int i = tab1 [c + 1], j = tab2 [c + 1]; // count from 1
302 cross (& p, & edges [i], & from);
303 cross (& p, & edges [j], & to);
304 return true;
305 }
306
307 public:
308
309 void init (double xL, double xR, double yB, double yT) {
310 Melder_assert (xL < xR && yB < yT);
311 corners = {{xL, yB, 1.0}, {xR, yB, 1.0}, {xR, yT, 1.0}, {xL, yT, 1.0}};
312 edges = newvectorzero<structHPoint> (4);
313 for (integer i = 1; i <= 4; i ++) {
314 const integer ip1 = i % 4 + 1;
315 cross (& corners [i], & corners[ip1], & edges[i]);
316 }
317 /*
318 Table 1 in Skala (2012)
319 */
320 //tab1 = {-1, 3, 0, 3, 1, -2, 0, 3, 2, 2, -2, 2, 1, 1, 0, -1};
321 tab1 = {-1, 4, 1, 4, 2, -2, 1, 4, 3, 3, -2, 3, 2, 2, 1, -1}; // count from 1
322 //tab2 = {-1, 0, 1, 1, 2, -2, 2, 2, 3, 0, -2, 1, 3, 0, 3, -1};
323 tab2 = {-1, 1, 2, 2, 3, -2, 3, 3, 4, 1, -2, 2, 4, 1, 4, -1}; // count from 1
324 }
325
326 bool clip (double& x1, double& y1, double& x2, double& y2) {
327 bool result = false;
328 from.x = x1; from.y = y1; from.w = 1.0;
329 to.x = x2; to.y = y2; to.w = 1.0;
330 if (clipLine ()) {
331 x1 = from.x / from.w; y1 = from.y / from.w;
332 x2 = to.x / to.w; y2 = to.y / to.w;
333 result = true;
334 }
335 return result;
336 }
337 };
338
339 /* End of file Graphics_extensions.cpp */