| 1 |
#ifndef _Graphics_extensions_h_ |
| 2 |
#define _Graphics_extensions_h_ |
| 3 |
/* Graphics_extensions.h |
| 4 |
* |
| 5 |
* Copyright (C) 2012-2022 David Weenink |
| 6 |
* |
| 7 |
* This code is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 10 |
* your option) any later version. |
| 11 |
* |
| 12 |
* This code is distributed in the hope that it will be useful, but |
| 13 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "Graphics.h" |
| 22 |
#include "Graphics_extensions_enums.h" |
| 23 |
|
| 24 |
/* |
| 25 |
Get a font size to display at least 'maxNumberOfLines' of text that consistes of |
| 26 |
'maxNumberOfCharacters_line' characters per line. |
| 27 |
*/ |
| 28 |
double Graphics_getFontSizeInsideBox (Graphics g, double widthWC, double heightWC, double maxNumberOfCharacters_line, double maxNumberOfLines); |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
Draw a box plot of data [1..ndata]. The vertical center line of the plot |
| 33 |
is at position 'x'. The rectangle box is 2*w wide, the whisker 2*r. |
| 34 |
All drawing outside [ymin, ymax] is clipped. |
| 35 |
*/ |
| 36 |
void Graphics_boxAndWhiskerPlot (Graphics g, constVEC data, double x, double r, double w, double ymin, double ymax); |
| 37 |
|
| 38 |
void Graphics_quantileQuantilePlot (Graphics g, integer numberOfQuantiles, constVEC xdata, constVEC ydata, |
| 39 |
double xmin, double xmax, double ymin, double ymax, double labelSize, conststring32 plotLabel); |
| 40 |
|
| 41 |
void Graphics_lagPlot (Graphics g, constVEC x, double xmin, double xmax, integer lag, double labelSize, conststring32 plotLabel); |
| 42 |
|
| 43 |
void getGridLayout (integer numberOfItems, integer *out_numberOfRows, integer *out_numberOfColumns); |
| 44 |
/* |
| 45 |
Get dimensions of a two-dimensional grid to layout n elements as nrow x ncol, |
| 46 |
where nrow x ncol >= n and nrow >= ncol and nrow - ncol <= 1. |
| 47 |
*/ |
| 48 |
|
| 49 |
integer getGridCellIndex (double x, double y, integer numberOfRows, integer numberOfColumns); |
| 50 |
/* |
| 51 |
Implicit: |
| 52 |
Rectangular grid numberOfRows x numberOfColumns |
| 53 |
Origin (0,0) is at left-bottom, top-right is at (1, 1) |
| 54 |
*/ |
| 55 |
|
| 56 |
|
| 57 |
/* |
| 58 |
Clipline segments from (x1,y1) to (x2, y2) against a rectangular clipping window |
| 59 |
whose bottom-left point is (xL,yB) anf top-right point is (xR,yT). |
| 60 |
*/ |
| 61 |
typedef struct structLineSegmentClipper *LineSegmentClipper; |
| 62 |
using autoLineSegmentClipper = std::unique_ptr<structLineSegmentClipper>; |
| 63 |
|
| 64 |
autoLineSegmentClipper LineSegmentClipper_create (double xL, double xR, double yB, double yT); |
| 65 |
|
| 66 |
bool LineSegmentClipper_clip (LineSegmentClipper me, double& x1, double& y1, double& x2, double& y2); |
| 67 |
|
| 68 |
struct structLineSegmentClipper { |
| 69 |
|
| 70 |
private: |
| 71 |
|
| 72 |
typedef struct structHPoint { |
| 73 |
double x, y, w; |
| 74 |
} *HPoint; |
| 75 |
/* |
| 76 |
The four corners of the rectangle in homogeneous coordinates (x, y, w) |
| 77 |
Order BottomLeft, BottomRight, TopRight, TopLeft |
| 78 |
The edges from c[1] to c[2], c[2] to c[3], c[3] to c[4], c[4] to c[1]. |
| 79 |
*/ |
| 80 |
autovector<structHPoint> corners; |
| 81 |
autovector<structHPoint> edges; |
| 82 |
struct structHPoint from, to, p; // working storage |
| 83 |
autoINTVEC tab1, tab2, mask; |
| 84 |
|
| 85 |
const int INSIDE = 0, LEFT = 1, RIGHT = 1<<1, BOTTOM = 1<<2, TOP = 1<<3; |
| 86 |
|
| 87 |
int getCohenSutherlandCode (HPoint p) { |
| 88 |
int result = INSIDE; |
| 89 |
if (p -> x < corners [1].x) |
| 90 |
result |= LEFT; |
| 91 |
else if (p -> x > corners[3].x) // to the right of clip window |
| 92 |
result |= RIGHT; |
| 93 |
if (p -> y < corners[1].y) // below the clip window |
| 94 |
result |= BOTTOM; |
| 95 |
else if (p -> y > corners[3].y) // above the clip window |
| 96 |
result |= TOP; |
| 97 |
return result; |
| 98 |
} |
| 99 |
|
| 100 |
void cross (HPoint a, HPoint b, HPoint p) { // p = a x b |
| 101 |
p -> x = a -> y * b -> w - b -> y * a -> w; |
| 102 |
p -> y = b -> x * a -> w - a -> x * b -> w; |
| 103 |
p -> w = a -> x * b -> y - b -> x * a -> y; |
| 104 |
} |
| 105 |
|
| 106 |
double in (HPoint a, HPoint b) { // sum a[i]*b[i] |
| 107 |
return a -> x * b -> x + a -> y * b -> y + a -> w * b -> w; |
| 108 |
} |
| 109 |
|
| 110 |
bool clipLine () { |
| 111 |
const int fromCode = getCohenSutherlandCode (& from); |
| 112 |
const int toCode = getCohenSutherlandCode (& to); |
| 113 |
|
| 114 |
if ((fromCode | toCode) == 0) // case 1, Fig 5: all inside? |
| 115 |
return true; |
| 116 |
|
| 117 |
if ((fromCode & toCode) != 0) // case 2, Fig 5: all outside? |
| 118 |
return false; |
| 119 |
/* |
| 120 |
trivial cases are done |
| 121 |
*/ |
| 122 |
|
| 123 |
cross (& from, & to, & p); |
| 124 |
int c = 0; |
| 125 |
for (int i = 1; i <= 4; i ++) |
| 126 |
if (in (& p, & corners [i]) >= 0) |
| 127 |
c |= 1 << (i - 1); |
| 128 |
c += 1;// we count from 1 |
| 129 |
if (c == 1 || c == 16) // case 6, Fig 5: |
| 130 |
return false; |
| 131 |
Melder_assert (c % 5 != 1); |
| 132 |
const int i = tab1 [c], j = tab2 [c]; |
| 133 |
if (fromCode != 0 && toCode != 0) { // two intersections? |
| 134 |
cross (& p, & edges [i], & from); |
| 135 |
cross (& p, & edges [j], & to); |
| 136 |
} else { // only one intersection |
| 137 |
if (fromCode == 0) { // to is outside |
| 138 |
if ((toCode & mask [c]) != 0) |
| 139 |
cross (& p, & edges [i], & to); |
| 140 |
else |
| 141 |
cross (& p, & edges [j], & to); |
| 142 |
} else if (toCode == 0){ // from is outside |
| 143 |
if ((fromCode & mask [c]) != 0) |
| 144 |
cross (& p, & edges [i], & from); |
| 145 |
else |
| 146 |
cross (& p, & edges [j], & from); |
| 147 |
} |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |
| 151 |
public: |
| 152 |
|
| 153 |
/* |
| 154 |
For rectangular window: |
| 155 |
*/ |
| 156 |
void init (double x1, double x2, double y1, double y2) { |
| 157 |
/* |
| 158 |
We want bottomLeft as (x1,y1) and topRight as (x2,y2), |
| 159 |
corners anti clockwise |
| 160 |
*/ |
| 161 |
if (x1 > x2) |
| 162 |
std::swap (x1, x2); |
| 163 |
if (y1 > y2) |
| 164 |
std::swap (y1, y2); |
| 165 |
// BL BR TR TL |
| 166 |
corners = {{x1, y1, 1.0}, {x2, y1, 1.0}, {x2, y2, 1.0}, {x1, y2, 1.0}}; |
| 167 |
edges = newvectorzero<structHPoint> (4); |
| 168 |
for (integer i = 1; i <= 4; i ++) { |
| 169 |
const integer ip1 = i % 4 + 1; |
| 170 |
cross (& corners [i], & corners[ip1], & edges[i]); |
| 171 |
} |
| 172 |
/* |
| 173 |
Table 1 in Skala (2005) for indices 0,1,2,3 |
| 174 |
tab1 = {-1, 0, 0, 1, 1, -2, 0, 2, 2, 0, -2, 1, 1, 0, 0, -1}; |
| 175 |
tab2 = {-1, 3, 1, 3, 2, -2, 2, 3, 3, 2, -2, 2, 3, 1, 3, -1}; |
| 176 |
Our indices are 1,2,3,4 so we add 1 to these tables. |
| 177 |
*/ |
| 178 |
tab1 = {-1, 1, 1, 2, 2, -2, 1, 3, 3, 1, -2, 2, 2, 1, 1, -1}; // count from 1 |
| 179 |
tab2 = {-1, 4, 2, 4, 3, -2, 3, 4, 4, 3, -2, 3, 4, 2, 4, -1}; // count from 1 |
| 180 |
mask = {-1, 4, 4, 2, 2, -2, 4, 8, 8, 4, -2, 2, 2, 4, 4, -1}; |
| 181 |
} |
| 182 |
|
| 183 |
bool clip (double& x1, double& y1, double& x2, double& y2) { |
| 184 |
bool result = false; |
| 185 |
from = { x1, y1, 1.0 }; |
| 186 |
to = { x2, y2, 1.0 }; |
| 187 |
if (clipLine ()) { |
| 188 |
x1 = from.x / from.w; y1 = from.y / from.w; |
| 189 |
x2 = to.x / to.w; y2 = to.y / to.w; |
| 190 |
result = true; |
| 191 |
} |
| 192 |
return result; |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
#endif /* _Graphics_extensions_h_ */ |