plotkit-core 0.5.0

Core types and logic for the plotkit plotting library
Documentation
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
//! Hexbin chart builder methods.
//!
//! Provides a fluent builder API for configuring [`HexbinArtist`] instances
//! and the hexagonal binning algorithm. Each builder method returns `&mut Self`,
//! allowing calls to be chained for concise, readable chart construction.

use crate::artist::HexbinArtist;
use crate::colormap::Colormap;
use crate::primitives::Color;
use std::collections::HashMap;

// ---------------------------------------------------------------------------
// Builder methods
// ---------------------------------------------------------------------------

impl HexbinArtist {
    /// Sets the number of hexagons across the x-axis.
    pub fn gridsize(&mut self, gridsize: usize) -> &mut Self {
        self.gridsize = gridsize;
        self
    }

    /// Sets the colormap used to map bin counts to colors.
    pub fn colormap(&mut self, cmap: Colormap) -> &mut Self {
        self.cmap = cmap;
        self
    }

    /// Sets the minimum count threshold; bins with fewer points are hidden.
    pub fn mincnt(&mut self, mincnt: usize) -> &mut Self {
        self.mincnt = mincnt;
        self
    }

    /// Sets the opacity from 0.0 (fully transparent) to 1.0 (fully opaque).
    pub fn alpha(&mut self, alpha: f64) -> &mut Self {
        self.alpha = alpha.clamp(0.0, 1.0);
        self
    }

    /// Sets the legend label.
    pub fn label(&mut self, label: &str) -> &mut Self {
        self.label = Some(label.to_string());
        self
    }

    /// Sets the hex edge (stroke) color.
    pub fn edgecolor(&mut self, color: Color) -> &mut Self {
        self.edgecolor = Some(color);
        self
    }

    /// Enables or disables auto-attaching a colorbar when this hexbin is drawn.
    pub fn colorbar(&mut self, show: bool) -> &mut Self {
        self.show_colorbar = show;
        self
    }
}

// ---------------------------------------------------------------------------
// Hexagonal binning algorithm
// ---------------------------------------------------------------------------

/// A hex cell key for the flat-top hexagonal grid.
///
/// We use axial coordinates (q, r) to identify each hexagon. Two `i64`
/// values give us exact, hash-friendly keys without floating-point
/// comparison issues.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct HexCell {
    /// Axial q-coordinate (column).
    pub q: i64,
    /// Axial r-coordinate (row).
    pub r: i64,
}

/// Result of binning data points into hexagonal cells.
#[derive(Debug, Clone)]
pub struct HexBinResult {
    /// Center (cx, cy) in data space and point count for each non-empty cell.
    pub cells: Vec<(f64, f64, usize)>,
    /// Maximum count across all cells.
    pub max_count: usize,
    /// Minimum count across all cells (among those >= mincnt).
    pub min_count: usize,
}

/// Computes the 6 vertices of a flat-top regular hexagon centered at
/// `(cx, cy)` with the given circumradius `size`.
///
/// Vertices are returned in counter-clockwise order starting from the
/// rightmost point (angle = 0).
pub fn hexagon_vertices(cx: f64, cy: f64, size: f64) -> [(f64, f64); 6] {
    let mut verts = [(0.0, 0.0); 6];
    for (i, vert) in verts.iter_mut().enumerate() {
        let angle = std::f64::consts::PI / 3.0 * i as f64;
        *vert = (cx + size * angle.cos(), cy + size * angle.sin());
    }
    verts
}

/// Bins `(x, y)` data points into a flat-top hexagonal grid.
///
/// The grid is sized so that roughly `gridsize` hexagons span the x-extent
/// of the data. Only cells with at least `mincnt` points are included in
/// the result.
///
/// # Algorithm
///
/// Uses the standard "nearest hex center" approach for flat-top hexagons:
///   1. Compute hex size (circumradius) from the data extent and `gridsize`.
///   2. For each point, convert to fractional axial coordinates then round
///      to the nearest hex cell using cube-coordinate rounding.
///   3. Accumulate counts in a hash map keyed by axial `(q, r)`.
///   4. Convert each cell's axial coordinates back to Cartesian centers.
pub fn bin_hexagonal(
    x: &[f64],
    y: &[f64],
    gridsize: usize,
    mincnt: usize,
) -> HexBinResult {
    if x.is_empty() || y.is_empty() {
        return HexBinResult {
            cells: Vec::new(),
            max_count: 0,
            min_count: 0,
        };
    }

    let (xmin, xmax, ymin, ymax) = data_extent(x, y);
    let x_range = (xmax - xmin).max(f64::EPSILON);
    let _y_range = (ymax - ymin).max(f64::EPSILON);

    // For flat-top hexagons:
    //   width  = 2 * size
    //   height = sqrt(3) * size
    // We want `gridsize` hexes across the x range.
    let size = x_range / (gridsize as f64 * 1.5 + 0.5);
    let size = size.max(f64::EPSILON);

    let sqrt3 = 3.0_f64.sqrt();

    // Bin each point.
    let mut counts: HashMap<HexCell, usize> = HashMap::new();

    let n = x.len().min(y.len());
    for i in 0..n {
        let px = x[i];
        let py = y[i];
        if !px.is_finite() || !py.is_finite() {
            continue;
        }

        // Convert Cartesian to fractional axial coordinates (flat-top).
        let fq = (2.0 / 3.0 * (px - xmin)) / size;
        let fr = (-(px - xmin) / 3.0 + sqrt3 / 3.0 * (py - ymin)) / size;

        // Round to nearest hex using cube-coordinate rounding.
        let (q, r) = axial_round(fq, fr);

        let cell = HexCell { q, r };
        *counts.entry(cell).or_insert(0) += 1;
    }

    // Filter by mincnt and compute centers.
    let mut cells = Vec::with_capacity(counts.len());
    let mut max_count = 0usize;
    let mut min_count = usize::MAX;

    for (cell, count) in &counts {
        if *count < mincnt {
            continue;
        }
        // Convert axial back to Cartesian.
        let cx = xmin + size * (3.0 / 2.0 * cell.q as f64);
        let cy = ymin + size * (sqrt3 / 2.0 * cell.q as f64 + sqrt3 * cell.r as f64);
        cells.push((cx, cy, *count));
        if *count > max_count {
            max_count = *count;
        }
        if *count < min_count {
            min_count = *count;
        }
    }

    if cells.is_empty() {
        min_count = 0;
    }

    // Sort cells for deterministic rendering order (bottom-left to top-right).
    cells.sort_by(|a, b| {
        a.1.partial_cmp(&b.1)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then(a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal))
    });

    HexBinResult {
        cells,
        max_count,
        min_count,
    }
}

/// Returns the hex circumradius for a given gridsize and x-extent.
///
/// This is exposed so the rendering pipeline can compute hex size from
/// the same parameters as the binning algorithm.
pub fn hex_size_for_gridsize(x_range: f64, gridsize: usize) -> f64 {
    let x_range = x_range.max(f64::EPSILON);
    let size = x_range / (gridsize as f64 * 1.5 + 0.5);
    size.max(f64::EPSILON)
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// Rounds fractional axial coordinates to the nearest hex cell using
/// cube-coordinate rounding.
fn axial_round(fq: f64, fr: f64) -> (i64, i64) {
    // Convert axial (q, r) to cube (x, y, z) where x=q, z=r, y=-x-z.
    let fx = fq;
    let fz = fr;
    let fy = -fx - fz;

    let mut rx = fx.round();
    let mut ry = fy.round();
    let mut rz = fz.round();

    let dx = (rx - fx).abs();
    let dy = (ry - fy).abs();
    let dz = (rz - fz).abs();

    if dx > dy && dx > dz {
        rx = -ry - rz;
    } else if dy > dz {
        ry = -rx - rz;
    } else {
        rz = -rx - ry;
    }

    let _ = ry; // y is implicit in axial coordinates
    (rx as i64, rz as i64)
}

/// Computes (xmin, xmax, ymin, ymax) for finite values in the data.
fn data_extent(x: &[f64], y: &[f64]) -> (f64, f64, f64, f64) {
    let mut xmin = f64::INFINITY;
    let mut xmax = f64::NEG_INFINITY;
    let mut ymin = f64::INFINITY;
    let mut ymax = f64::NEG_INFINITY;

    for &v in x {
        if v.is_finite() {
            if v < xmin { xmin = v; }
            if v > xmax { xmax = v; }
        }
    }
    for &v in y {
        if v.is_finite() {
            if v < ymin { ymin = v; }
            if v > ymax { ymax = v; }
        }
    }

    if !xmin.is_finite() { xmin = 0.0; }
    if !xmax.is_finite() { xmax = 1.0; }
    if !ymin.is_finite() { ymin = 0.0; }
    if !ymax.is_finite() { ymax = 1.0; }
    if (xmax - xmin).abs() < f64::EPSILON {
        xmin -= 0.5;
        xmax += 0.5;
    }
    if (ymax - ymin).abs() < f64::EPSILON {
        ymin -= 0.5;
        ymax += 0.5;
    }

    (xmin, xmax, ymin, ymax)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::artist::HexbinArtist;

    fn sample_hexbin() -> HexbinArtist {
        HexbinArtist {
            x: vec![0.0, 1.0, 2.0, 3.0, 4.0],
            y: vec![0.0, 1.0, 2.0, 3.0, 4.0],
            gridsize: 10,
            cmap: Colormap::Viridis,
            mincnt: 1,
            alpha: 1.0,
            color: Color::TAB_BLUE,
            label: None,
            edgecolor: None,
            show_colorbar: false,
        }
    }

    // -- Hexagon vertex tests --

    #[test]
    fn hexagon_vertices_count() {
        let verts = hexagon_vertices(0.0, 0.0, 1.0);
        assert_eq!(verts.len(), 6);
    }

    #[test]
    fn hexagon_vertices_symmetry() {
        let verts = hexagon_vertices(0.0, 0.0, 1.0);
        // First vertex should be at (1, 0) for flat-top hex at origin with size 1.
        assert!((verts[0].0 - 1.0).abs() < 1e-10);
        assert!((verts[0].1 - 0.0).abs() < 1e-10);
        // Opposite vertex (index 3) should be at (-1, 0).
        assert!((verts[3].0 - (-1.0)).abs() < 1e-10);
        assert!((verts[3].1 - 0.0).abs() < 1e-10);
    }

    #[test]
    fn hexagon_vertices_at_center() {
        let cx = 5.0;
        let cy = 3.0;
        let size = 2.0;
        let verts = hexagon_vertices(cx, cy, size);
        // First vertex: (cx + size, cy)
        assert!((verts[0].0 - (cx + size)).abs() < 1e-10);
        assert!((verts[0].1 - cy).abs() < 1e-10);
    }

    #[test]
    fn hexagon_vertices_equidistant_from_center() {
        let cx = 1.0;
        let cy = 2.0;
        let size = 3.0;
        let verts = hexagon_vertices(cx, cy, size);
        for (vx, vy) in &verts {
            let dist = ((vx - cx).powi(2) + (vy - cy).powi(2)).sqrt();
            assert!(
                (dist - size).abs() < 1e-10,
                "vertex ({vx}, {vy}) distance {dist} should equal size {size}"
            );
        }
    }

    // -- Binning algorithm tests --

    #[test]
    fn bin_empty_data() {
        let result = bin_hexagonal(&[], &[], 10, 1);
        assert!(result.cells.is_empty());
        assert_eq!(result.max_count, 0);
    }

    #[test]
    fn bin_single_point() {
        let result = bin_hexagonal(&[1.0], &[1.0], 10, 1);
        assert_eq!(result.cells.len(), 1);
        assert_eq!(result.cells[0].2, 1); // count = 1
    }

    #[test]
    fn bin_identical_points_same_cell() {
        // All 100 points at the same location should land in one cell.
        let x = vec![5.0; 100];
        let y = vec![5.0; 100];
        let result = bin_hexagonal(&x, &y, 10, 1);
        assert_eq!(result.cells.len(), 1);
        assert_eq!(result.cells[0].2, 100);
        assert_eq!(result.max_count, 100);
    }

    #[test]
    fn bin_count_accuracy() {
        // Place 50 points at (0,0) and 30 points at (100,100).
        let mut x = vec![0.0; 50];
        let mut y = vec![0.0; 50];
        x.extend(vec![100.0; 30]);
        y.extend(vec![100.0; 30]);
        let result = bin_hexagonal(&x, &y, 5, 1);

        // Total points across all cells should be 80.
        let total: usize = result.cells.iter().map(|c| c.2).sum();
        assert_eq!(total, 80);
        assert_eq!(result.max_count, 50);
    }

    #[test]
    fn bin_mincnt_filtering() {
        // Place 10 points at (0,0) and 1 point at (100,100) with mincnt=2.
        let mut x = vec![0.0; 10];
        let mut y = vec![0.0; 10];
        x.push(100.0);
        y.push(100.0);
        let result = bin_hexagonal(&x, &y, 5, 2);

        // The single-point cell should be filtered out.
        for cell in &result.cells {
            assert!(cell.2 >= 2, "all cells should have count >= 2");
        }
    }

    #[test]
    fn bin_gridsize_effect() {
        // With a larger gridsize, we should get more cells (finer grid).
        let x: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();
        let y: Vec<f64> = (0..100).map(|i| i as f64 * 0.1).collect();

        let result_coarse = bin_hexagonal(&x, &y, 5, 1);
        let result_fine = bin_hexagonal(&x, &y, 20, 1);

        assert!(
            result_fine.cells.len() >= result_coarse.cells.len(),
            "finer grid ({}) should produce >= cells than coarse grid ({})",
            result_fine.cells.len(),
            result_coarse.cells.len()
        );
    }

    #[test]
    fn bin_nan_points_skipped() {
        let x = vec![1.0, f64::NAN, 3.0];
        let y = vec![1.0, 2.0, f64::NAN];
        let result = bin_hexagonal(&x, &y, 10, 1);
        // Only (1.0, 1.0) is valid.
        let total: usize = result.cells.iter().map(|c| c.2).sum();
        assert_eq!(total, 1);
    }

    // -- Data bounds tests --

    #[test]
    fn data_bounds_basic() {
        let h = HexbinArtist {
            x: vec![1.0, 2.0, 3.0],
            y: vec![10.0, 20.0, 30.0],
            gridsize: 10,
            cmap: Colormap::Viridis,
            mincnt: 1,
            alpha: 1.0,
            color: Color::TAB_BLUE,
            label: None,
            edgecolor: None,
            show_colorbar: false,
        };
        let (xmin, xmax, ymin, ymax) = h.data_bounds();
        assert!((xmin - 1.0).abs() < f64::EPSILON);
        assert!((xmax - 3.0).abs() < f64::EPSILON);
        assert!((ymin - 10.0).abs() < f64::EPSILON);
        assert!((ymax - 30.0).abs() < f64::EPSILON);
    }

    #[test]
    fn data_bounds_empty() {
        let h = HexbinArtist {
            x: vec![],
            y: vec![],
            gridsize: 10,
            cmap: Colormap::Viridis,
            mincnt: 1,
            alpha: 1.0,
            color: Color::TAB_BLUE,
            label: None,
            edgecolor: None,
            show_colorbar: false,
        };
        assert_eq!(h.data_bounds(), (0.0, 1.0, 0.0, 1.0));
    }

    // -- Builder method tests --

    #[test]
    fn builder_gridsize() {
        let mut h = sample_hexbin();
        h.gridsize(30);
        assert_eq!(h.gridsize, 30);
    }

    #[test]
    fn builder_colormap() {
        let mut h = sample_hexbin();
        h.colormap(Colormap::Plasma);
        assert_eq!(h.cmap, Colormap::Plasma);
    }

    #[test]
    fn builder_mincnt() {
        let mut h = sample_hexbin();
        h.mincnt(5);
        assert_eq!(h.mincnt, 5);
    }

    #[test]
    fn builder_alpha() {
        let mut h = sample_hexbin();
        h.alpha(0.5);
        assert!((h.alpha - 0.5).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_alpha_clamp() {
        let mut h = sample_hexbin();
        h.alpha(2.0);
        assert!((h.alpha - 1.0).abs() < f64::EPSILON);
        h.alpha(-0.5);
        assert!((h.alpha - 0.0).abs() < f64::EPSILON);
    }

    #[test]
    fn builder_label() {
        let mut h = sample_hexbin();
        h.label("hexbin test");
        assert_eq!(h.label.as_deref(), Some("hexbin test"));
    }

    #[test]
    fn builder_edgecolor() {
        let mut h = sample_hexbin();
        h.edgecolor(Color::BLACK);
        assert_eq!(h.edgecolor, Some(Color::BLACK));
    }

    #[test]
    fn builder_chaining() {
        let mut h = sample_hexbin();
        h.gridsize(15)
            .colormap(Colormap::Inferno)
            .mincnt(3)
            .alpha(0.7)
            .label("chained")
            .edgecolor(Color::WHITE);
        assert_eq!(h.gridsize, 15);
        assert_eq!(h.cmap, Colormap::Inferno);
        assert_eq!(h.mincnt, 3);
        assert!((h.alpha - 0.7).abs() < f64::EPSILON);
        assert_eq!(h.label.as_deref(), Some("chained"));
        assert_eq!(h.edgecolor, Some(Color::WHITE));
    }

    // -- Axial rounding tests --

    #[test]
    fn axial_round_at_origin() {
        let (q, r) = axial_round(0.0, 0.0);
        assert_eq!(q, 0);
        assert_eq!(r, 0);
    }

    #[test]
    fn axial_round_exact_integer() {
        let (q, r) = axial_round(3.0, -2.0);
        assert_eq!(q, 3);
        assert_eq!(r, -2);
    }

    #[test]
    fn axial_round_near_boundary() {
        // Values very close to 0.5 should round deterministically.
        let (q1, r1) = axial_round(0.49, 0.0);
        let (q2, r2) = axial_round(0.51, 0.0);
        // Both should resolve to valid hex cells.
        assert!(q1 == 0 || q1 == 1);
        assert!(q2 == 0 || q2 == 1);
        let _ = (r1, r2); // suppress unused warnings
    }

    // -- hex_size_for_gridsize tests --

    #[test]
    fn hex_size_positive() {
        let size = hex_size_for_gridsize(10.0, 20);
        assert!(size > 0.0);
    }

    #[test]
    fn hex_size_increases_with_range() {
        let s1 = hex_size_for_gridsize(5.0, 10);
        let s2 = hex_size_for_gridsize(10.0, 10);
        assert!(s2 > s1);
    }

    #[test]
    fn hex_size_decreases_with_gridsize() {
        let s1 = hex_size_for_gridsize(10.0, 5);
        let s2 = hex_size_for_gridsize(10.0, 20);
        assert!(s2 < s1);
    }
}