/
geom.d
460 lines (382 loc) · 12.3 KB
/
geom.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
module ggplotd.geom;
import cairo = cairo.cairo;
import ggplotd.bounds;
import ggplotd.aes;
import ggplotd.colour : ColourID;
version (unittest)
{
import dunit.toolkit;
}
///
struct Geom
{
alias drawFunction = cairo.Context delegate(cairo.Context context);
drawFunction draw; ///
ColourID colour; ///
AdaptiveBounds bounds; ///
import std.typecons : Tuple;
Tuple!(double, string)[] xTickLabels; ///
Tuple!(double, string)[] yTickLabels; ///
}
///
auto geomPoint(AES)(AES aes)
{
alias CoordX = typeof(NumericLabel!(typeof(AES.x))(AES.x));
alias CoordY = typeof(NumericLabel!(typeof(AES.y))(AES.y));
alias CoordType = typeof(merge(aes, Aes!(CoordX, "x", CoordY,
"y")(CoordX(AES.x), CoordY(AES.y))));
struct GeomRange(T)
{
this(T aes)
{
_aes = merge(aes, Aes!(CoordX, "x", CoordY, "y")(CoordX(aes.x), CoordY(aes.y)));
}
@property auto front()
{
immutable tup = _aes.front;
auto f = delegate(cairo.Context context) {
auto devP = context.userToDevice(cairo.Point!double(tup.x[0], tup.y[0]));
context.save();
context.identityMatrix;
context.rectangle(devP.x - 0.5 * tup.size, devP.y - 0.5 * tup.size, tup.size, tup.size);
context.fill();
context.restore();
return context;
};
AdaptiveBounds bounds;
bounds.adapt(Point(tup.x[0], tup.y[0]));
return Geom(f, ColourID(tup.colour), bounds);
}
void popFront()
{
_aes.popFront();
}
@property bool empty()
{
return _aes.empty;
}
private:
CoordType _aes;
}
return GeomRange!AES(aes);
}
///
unittest
{
auto aes = Aes!(double[], "x", double[], "y")([1.0], [2.0]);
auto gl = geomPoint(aes);
assertEqual(gl.front.colour[1], "black");
gl.popFront;
assert(gl.empty);
}
///
auto geomLine(AES)(AES aes)
{
import std.algorithm : map;
import std.range : array, zip;
struct GeomRange(T)
{
this(T aes)
{
groupedAes = aes.group;
}
@property auto front()
{
auto xs = NumericLabel!(typeof(groupedAes.front.front.x)[])(
groupedAes.front.map!((t) => t.x).array);
auto ys = NumericLabel!(typeof(groupedAes.front.front.y)[])(
groupedAes.front.map!((t) => t.y).array);
auto coords = zip(xs, ys);
auto f = delegate(cairo.Context context) {
auto fr = coords.front;
context.moveTo(fr[0][0], fr[1][0]);
coords.popFront;
foreach (tup;
coords)
{
context.lineTo(tup[0][0], tup[1][0]);
}
return context;
};
auto geom = Geom(f, ColourID(groupedAes.front.front.colour));
AdaptiveBounds bounds;
coords = zip(xs, ys);
foreach (tup; coords)
{
bounds.adapt(Point(tup[0][0], tup[1][0]));
if (!xs.numeric)
geom.xTickLabels ~= tup[0];
if (!ys.numeric)
geom.yTickLabels ~= tup[0];
}
geom.bounds = bounds;
return geom;
}
void popFront()
{
groupedAes.popFront;
}
@property bool empty()
{
return groupedAes.empty;
}
private:
typeof(group(T.init)) groupedAes;
}
return GeomRange!AES(aes);
}
///
unittest
{
auto aes = Aes!(double[], "x", double[], "y", string[], "colour")([1.0,
2.0, 1.1, 3.0], [3.0, 1.5, 1.1, 1.8], ["a", "b", "a", "b"]);
auto gl = geomLine(aes);
import std.range : empty;
assert(gl.front.xTickLabels.empty);
assert(gl.front.yTickLabels.empty);
assertEqual(gl.front.colour[1], "a");
assertEqual(gl.front.bounds.min_x, 1.0);
assertEqual(gl.front.bounds.max_x, 1.1);
gl.popFront;
assertEqual(gl.front.colour[1], "b");
assertEqual(gl.front.bounds.max_x, 3.0);
gl.popFront;
assert(gl.empty);
}
unittest
{
auto aes = Aes!(string[], "x", string[], "y", string[], "colour")(["a",
"b", "c", "b"], ["a", "b", "b", "a"], ["b", "b", "b", "b"]);
auto gl = geomLine(aes);
assertEqual(gl.front.xTickLabels.length, 4);
assertEqual(gl.front.yTickLabels.length, 4);
}
unittest
{
auto aes = Aes!(string[], "x", string[], "y", string[], "colour")(["a",
"b", "c", "b"], ["a", "b", "b", "a"], ["b", "b", "b", "b"]);
auto gl = geomLine(aes);
auto aes2 = Aes!(string[], "x", string[], "y", double[], "colour")(["a",
"b", "c", "b"], ["a", "b", "b", "a"], [0, 1, 0, 0.1]);
auto gl2 = geomLine(aes2);
import std.range : chain, walkLength;
assertEqual(gl.chain(gl2).walkLength, 4);
}
// Bin a range of data
private auto bin(R)(R xs, size_t noBins = 10)
{
struct Bin
{
double[] range;
size_t count;
}
import std.typecons : Tuple;
import std.algorithm : group;
struct BinRange(Range)
{
this(Range xs, size_t noBins)
{
import std.math : floor;
import std.algorithm : min, max, reduce, sort, map;
import std.array : array;
import std.range : walkLength;
assert(xs.walkLength > 0);
// Find the min and max values
auto minmax = xs.reduce!((a, b) => min(a, b), (a, b) => max(a, b));
_width = (minmax[1] - minmax[0]) / (noBins - 1);
_noBins = noBins;
// If min == max we need to set a custom width
if (_width == 0)
_width = 0.1;
_min = minmax[0] - 0.5 * _width;
// Count the number of data points that fall in a
// bin. This is done by scaling them into whole numbers
counts = xs.map!((a) => floor((a - _min) / _width)).array.sort().array.group();
// Initialize our bins
if (counts.front[0] == _binID)
{
_cnt = counts.front[1];
counts.popFront;
}
}
/// Return a bin describing the range and number of data points (count) that fall within that range.
@property auto front()
{
return Bin([_min, _min + _width], _cnt);
}
void popFront()
{
_min += _width;
_cnt = 0;
++_binID;
if (!counts.empty && counts.front[0] == _binID)
{
_cnt = counts.front[1];
counts.popFront;
}
}
@property bool empty()
{
return _binID >= _noBins;
}
private:
double _min;
double _width;
size_t _noBins;
size_t _binID = 0;
typeof(group(Range.init)) counts;
size_t _cnt = 0;
}
return BinRange!R(xs, noBins);
}
unittest
{
import std.array : array;
import std.range : back, walkLength;
auto binR = bin!(double[])([0.5, 0.01, 0.0, 0.9, 1.0, 0.99], 11);
assertEqual(binR.walkLength, 11);
assertEqual(binR.front.range, [-0.05, 0.05]);
assertEqual(binR.front.count, 2);
assertLessThan(binR.array.back.range[0], 1);
assertGreaterThan(binR.array.back.range[1], 1);
assertEqual(binR.array.back.count, 2);
binR = bin!(double[])([0.01], 11);
assertEqual(binR.walkLength, 11);
assertEqual(binR.front.count, 1);
binR = bin!(double[])([-0.01, 0, 0, 0, 0.01], 11);
assertEqual(binR.walkLength, 11);
assertLessThan(binR.front.range[0], -0.01);
assertGreaterThan(binR.front.range[1], -0.01);
assertEqual(binR.front.count, 1);
assertLessThan(binR.array.back.range[0], 0.01);
assertGreaterThan(binR.array.back.range[1], 0.01);
assertEqual(binR.array.back.count, 1);
assertEqual(binR.array[5].count, 3);
assertLessThan(binR.array[5].range[0], 0.0);
assertGreaterThan(binR.array[5].range[1], 0.0);
}
/// Draw histograms based on the x coordinates of the data (aes)
auto geomHist(AES)(AES aes)
{
import std.algorithm : map;
import std.array : array;
import std.range : repeat;
// New aes to hold the lines for drawing histogram
auto aesLine = Aes!(double[], "x", double[], "y", typeof(aes.front.colour)[], "colour")();
foreach (grouped; group(aes)) // Split data by colour/id
{
auto bins = grouped.map!((t) => t.x) // Extract the x coordinates
.array.bin(11); // Bin the data
foreach (bin; bins)
{
// Convert data into line coordinates
aesLine.x ~= [bin.range[0], bin.range[0], bin.range[1], bin.range[1]];
aesLine.y ~= [0, bin.count, bin.count, 0];
// Each (new) line coordinate has the colour specified
// in the original data
aesLine.colour ~= grouped.front.colour.repeat(4).array;
}
}
// Use the xs/ys/colours to draw lines
return geomLine(aesLine);
}
/// Draw axis, first and last location are start/finish
/// others are ticks (perpendicular)
auto geomAxis(AES)(AES aes, double tickLength)
{
import std.array : array;
import std.range : chain, empty, repeat;
import std.math : sqrt, pow;
double[] xs;
double[] ys;
double[] lxs;
double[] lys;
double[] langles;
string[] lbls;
auto colour = aes.front.colour;
double[2] orig = [aes.front.x, aes.front.y];
double[2] direction;
while (!aes.empty)
{
auto tick = aes.front;
xs ~= tick.x;
ys ~= tick.y;
aes.popFront;
// Draw ticks perpendicular to main axis;
if (xs.length > 1 && !aes.empty)
{
if (xs.length == 2)
{
// Calculate tick direction and size
direction = [tick.x - orig[0], tick.y - orig[1]];
auto dirLength = sqrt(pow(direction[0], 2) + pow(direction[1], 2));
direction[0] *= tickLength / dirLength;
direction[1] *= tickLength / dirLength;
}
xs ~= [tick.x + direction[1], tick.x];
ys ~= [tick.y + direction[0], tick.y];
lxs ~= tick.x - direction[1];
lys ~= tick.y - direction[0];
lbls ~= tick.label;
langles ~= tick.angle;
}
}
return geomLine(Aes!(typeof(xs), "x", typeof(ys), "y")(xs, ys)).chain(
geomLabel(Aes!(double[], "x", double[], "y", string[], "label",
double[], "angle")(lxs, lys, lbls, langles)));
}
/// Draw Label at given x and y position
auto geomLabel(AES)(AES aes)
{
alias CoordX = typeof(NumericLabel!(typeof(AES.x))(AES.x));
alias CoordY = typeof(NumericLabel!(typeof(AES.y))(AES.y));
alias CoordType = typeof(merge(aes, Aes!(CoordX, "x", CoordY,
"y")(CoordX(AES.x), CoordY(AES.y))));
struct GeomRange(T)
{
size_t size = 6;
this(T aes)
{
_aes = merge(aes, Aes!(CoordX, "x", CoordY, "y")(CoordX(aes.x), CoordY(aes.y)));
}
@property auto front()
{
immutable tup = _aes.front;
auto f = delegate(cairo.Context context) {
context.setFontSize(14.0);
context.moveTo(tup.x[0], tup.y[0]);
context.save();
context.identityMatrix;
context.rotate(tup.angle);
auto extents = context.textExtents(tup.label);
auto textSize = cairo.Point!double(0.5 * extents.width, 0.5 * extents.height);
context.relMoveTo(-textSize.x, textSize.y);
context.showText(tup.label);
context.restore();
return context;
};
AdaptiveBounds bounds;
bounds.adapt(Point(tup.x[0], tup.y[0]));
return Geom(f, ColourID(tup.colour), bounds);
}
void popFront()
{
_aes.popFront();
}
@property bool empty()
{
return _aes.empty;
}
private:
CoordType _aes;
}
return GeomRange!AES(aes);
}
unittest
{
auto aes = Aes!(string[], "x", string[], "y", string[], "label")(["a", "b",
"c", "b"], ["a", "b", "b", "a"], ["b", "b", "b", "b"]);
auto gl = geomLabel(aes);
import std.range : walkLength;
assertEqual(gl.walkLength, 4);
}