ferrochart-core 0.3.0

Platform-independent core of the FerroChart financial charting engine
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! CPU implementation of the series transforms that the wgpu shaders perform.
//!
//! **Consistency rule**: the step from
//! `XTransform`/`YTransform` to pixels is done in `f32`, with exactly the operation
//! order the shaders use (no FMA), so that both backends round identically at
//! pixel boundaries.
//!
//! Shader compilers may fuse `a * b + c` into an FMA (Metal compiles with
//! fast-math by default, and WGSL cannot forbid it), which rounds differently
//! from a separate multiply and add. The per-element transforms are therefore
//! written **add first, multiply last** — `x = (d + shift) * step`,
//! `y = (v_rel + bias) * a` — which no compiler can contract. The folded
//! constants `shift` and `bias` are computed here, in f64, for both backends. Each function names its shader counterpart; the shaders are
//! in `crates/ferrochart-render-wgpu/src/shaders/series.wgsl` and must
//! mirror these functions line by line. Change both together.
//!

use crate::data::OhlcLevelView;
use crate::data::lod::{f32_down, f32_up};
use crate::frame::{LineDash, PriceMapping, XTransform, YTransform};
use crate::geom::{PointPx, Px, RectPx};

/// log2 of the number of elements per GPU chunk.
pub const CHUNK_SHIFT: u32 = 12;
/// Elements per GPU chunk. GPU buffers hold one more (the junction element).
pub const CHUNK_LEN: i64 = 1 << CHUNK_SHIFT;

/// Chunk number of element `e` of LOD `level`. Level 0 elements are
/// logical indices; level `k >= 2` elements are bucket numbers, which already
/// count from the anchor.
///
/// # Examples
///
/// ```
/// use ferrochart_core::data::ANCHOR;
/// use ferrochart_core::transform_helpers::{chunk_of, chunk_start, CHUNK_LEN};
///
/// assert_eq!(chunk_of(0, ANCHOR + CHUNK_LEN), 1);
/// assert_eq!(chunk_start(0, 1), ANCHOR + CHUNK_LEN);
/// assert_eq!(chunk_of(3, 5000), 1);
/// assert_eq!(chunk_start(3, 1), CHUNK_LEN);
/// ```
pub fn chunk_of(level: u8, e: i64) -> i64 {
    let from_anchor = if level == 0 {
        e - crate::data::ANCHOR
    } else {
        e
    };
    from_anchor >> CHUNK_SHIFT
}

/// First element of chunk `c` of LOD `level`.
pub fn chunk_start(level: u8, c: i64) -> i64 {
    let from_anchor = c << CHUNK_SHIFT;
    if level == 0 {
        from_anchor + crate::data::ANCHOR
    } else {
        from_anchor
    }
}

/// The reference price a renderer assigns to chunk `c` when it first writes it:
/// the first finite, positive open in the chunk. The GPU backend freezes
/// it; the stateless CPU backend recomputes it, which gives the same value
/// unless the chunk's first bars changed after the GPU first saw them.
pub fn initial_chunk_ref(view: &OhlcLevelView<'_>, c: i64) -> Option<f64> {
    let start = chunk_start(view.level, c).max(view.first);
    let end = (chunk_start(view.level, c) + CHUNK_LEN).min(view.end());
    let opens = || (start..end).filter_map(|j| view.open.at(view.first, j));
    // Value series may be zero or negative (oscillators), and a value bucket's
    // first element may be a gap: fall back to the first finite open, then the
    // first finite high. In log mode such a chunk only holds gaps anyway.
    opens()
        .find(|p| p.is_finite() && *p > 0.0)
        .or_else(|| opens().find(|p| p.is_finite()))
        .or_else(|| {
            (start..end)
                .filter_map(|j| view.high.at(view.first, j))
                .find(|p| p.is_finite())
        })
}

/// `[open, high, low, close]` relative to `chunk_ref`, exactly as stored in the
/// GPU buffers: open/close rounded to nearest, high rounded up and low rounded
/// down, so the drawn range always contains the true range.
pub fn ohlc_rel(yt: &YTransform, ohlc: [f64; 4], chunk_ref: f64) -> [f32; 4] {
    let r = map(yt.mapping, chunk_ref);
    let [o, h, l, c] = ohlc.map(|v| map(yt.mapping, v) - r);
    [o as f32, f32_up(h), f32_down(l), c as f32].map(gap_or)
}

/// Stored instead of a relative value that is not finite (a NaN gap of a value
/// series, the log of a non-positive price). Shaders cannot rely on NaN
/// checks under fast-math, so gaps are this finite sentinel. Shader: `GAP`.
pub const GAP: f32 = 3.0e38;

/// `v` if finite, else [`GAP`].
fn gap_or(v: f32) -> f32 {
    if v.is_finite() { v } else { GAP }
}

/// Whether a stored relative value is a gap. Shader: `is_gap()`.
pub fn is_gap(v_rel: f32) -> bool {
    // NaN compares false, so it counts as a gap too.
    v_rel.abs() >= 1.0e38 || v_rel.is_nan()
}

/// Up/down decision on the relative values. Shader: `is_up_rel()`.
pub fn is_up_rel(open_rel: f32, close_rel: f32) -> bool {
    close_rel >= open_rel
}

/// Pixel snapping used everywhere: round half up, `floor(v + 0.5)`.
/// Shader: `snap()`.
pub fn snap(v: f32) -> f32 {
    (v + 0.5).floor()
}

/// `offset / step`: the x offset in elements, so that
/// `x = (d + shift) * step` (no multiply-add to fuse). The per-draw uniform.
pub fn x_shift(xt: &XTransform) -> f32 {
    if xt.step == 0.0 {
        return 0.0;
    }
    (f64::from(xt.offset) / f64::from(xt.step)) as f32
}

/// x of the center of element `j`: `(d + shift) * step` with `d = j - base`.
/// Shader: `x_center()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::frame::XTransform;
/// use ferrochart_core::transform_helpers::x_center;
///
/// let xt = XTransform { base: 100, step: 4.0, offset: 10.0 };
/// assert_eq!(x_center(&xt, 102), 18.0);
/// ```
pub fn x_center(xt: &XTransform, j: i64) -> f32 {
    let d = (j - xt.base) as f32;
    let shifted = d + x_shift(xt);
    shifted * xt.step
}

fn map(mapping: PriceMapping, v: f64) -> f64 {
    match mapping {
        PriceMapping::Linear => v,
        PriceMapping::Log => v.ln(),
    }
}

/// `f(value) - f(chunk_ref)` as f32: what the GPU buffers store. Computed
/// on the CPU in both backends.
pub fn v_rel(yt: &YTransform, value: f64, chunk_ref: f64) -> f32 {
    (map(yt.mapping, value) - map(yt.mapping, chunk_ref)) as f32
}

/// The per-chunk uniform: `f(chunk_ref) - f(view_ref) + b / a`, so that
/// `y = (v_rel + bias) * a` equals `a * (f(value) - f(view_ref)) + b`.
pub fn y_bias(yt: &YTransform, chunk_ref: f64) -> f32 {
    if yt.a == 0.0 {
        return 0.0;
    }
    let delta = map(yt.mapping, chunk_ref) - map(yt.mapping, yt.view_ref);
    (delta + f64::from(yt.b) / f64::from(yt.a)) as f32
}

/// `y = (v_rel + bias) * a` in f32, in this order. Shader: `y_of()`.
pub fn y(yt: &YTransform, v_rel: f32, bias: f32) -> f32 {
    let t = v_rel + bias;
    t * yt.a
}

/// y of a single price, with `view_ref` as the chunk reference.
///
/// # Examples
///
/// ```
/// use ferrochart_core::frame::{PriceMapping, YTransform};
/// use ferrochart_core::transform_helpers::y_of_price;
///
/// let yt = YTransform { a: -2.0, b: 50.0, view_ref: 100.0, mapping: PriceMapping::Linear };
/// assert_eq!(y_of_price(&yt, 110.0), 30.0);
/// ```
pub fn y_of_price(yt: &YTransform, price: f64) -> f32 {
    y(yt, v_rel(yt, price, yt.view_ref), y_bias(yt, yt.view_ref))
}

/// How bars are drawn at a given element step.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BarMetrics {
    /// Body width in pixels: `max(1, floor(step * 0.8))`, made odd by subtracting 1.
    pub body: f32,
    /// Whether bodies are drawn (`step >= 1.5`); otherwise only high-low wicks.
    pub show_body: bool,
    /// Whether body borders are drawn (`step >= 3`).
    pub show_border: bool,
}

/// Bar metrics for element spacing `step`. Shader: `bar_metrics()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::transform_helpers::bar_metrics;
///
/// assert_eq!(bar_metrics(10.0).body, 7.0); // floor(8) = 8, even -> 7
/// assert_eq!(bar_metrics(6.0).body, 3.0);  // floor(4.8) = 4 -> 3
/// assert!(!bar_metrics(1.2).show_body);
/// ```
pub fn bar_metrics(step: f32) -> BarMetrics {
    let mut body = (step * 0.8).floor().max(1.0);
    if body % 2.0 == 0.0 {
        body -= 1.0;
    }
    BarMetrics {
        body,
        show_body: step >= 1.5,
        show_border: step >= 3.0,
    }
}

/// Up/down rule: a bar is "up" when `close >= open`. Shader: `is_up()`.
pub fn is_up(open: f64, close: f64) -> bool {
    close >= open
}

/// Pixel geometry of one candle.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct CandleGeom {
    /// 1-px wide high-low line.
    pub wick: RectPx,
    /// Body, if bodies are drawn at this step.
    pub body: Option<RectPx>,
}

/// Vertical pixel span `[snap(min), snap(max))`, at least 1 px tall.
fn v_span(y0: f32, y1: f32) -> (f32, f32) {
    let top = snap(y0.min(y1));
    let bottom = snap(y0.max(y1));
    (top, (bottom - top).max(1.0))
}

/// Candle geometry from the element's center x and the y of its prices.
/// Shader: `candle_vertex()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::transform_helpers::{bar_metrics, candle};
///
/// let g = candle(10.3, bar_metrics(6.0), [20.0, 10.0, 40.0, 30.0]);
/// assert_eq!((g.wick.x, g.wick.w, g.wick.y, g.wick.h), (10.0, 1.0, 10.0, 30.0));
/// let body = g.body.unwrap();
/// assert_eq!((body.x, body.w, body.y, body.h), (9.0, 3.0, 20.0, 10.0));
/// ```
pub fn candle(xc: f32, m: BarMetrics, ys: [f32; 4]) -> CandleGeom {
    let [y_open, y_high, y_low, y_close] = ys;
    let column = xc.floor();
    let (wy, wh) = v_span(y_high, y_low);
    let wick = RectPx {
        x: column,
        y: wy,
        w: 1.0,
        h: wh,
    };
    let body = m.show_body.then(|| {
        let (by, bh) = v_span(y_open, y_close);
        RectPx {
            x: column - (m.body - 1.0) * 0.5,
            y: by,
            w: m.body,
            h: bh,
        }
    });
    CandleGeom { wick, body }
}

/// A histogram column from `y_value` down (or up) to `y_base`. Shader:
/// `histogram_vertex()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::transform_helpers::{bar_metrics, histogram};
///
/// let r = histogram(10.3, bar_metrics(6.0), 80.2, 100.0);
/// assert_eq!((r.x, r.w, r.y, r.h), (9.0, 3.0, 80.0, 20.0));
/// ```
pub fn histogram(xc: f32, m: BarMetrics, y_value: f32, y_base: f32) -> RectPx {
    let column = xc.floor();
    let width = if m.show_body { m.body } else { 1.0 };
    let (y, h) = v_span(y_value, y_base);
    RectPx {
        x: column - (width - 1.0) * 0.5,
        y,
        w: width,
        h,
    }
}

/// OHLC bar ("American line"): the high-low line, and when bodies are shown at
/// this step, the open tick on the left and the close tick on the right
/// (1 px thick, `(body - 1) / 2` long). Shader: `vs_ohlc_bar`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::transform_helpers::{bar_metrics, ohlc_bar};
///
/// let [wick, open, close] = ohlc_bar(10.3, bar_metrics(10.0), [20.0, 10.0, 40.0, 30.0]);
/// let (wick, open, close) = (wick.unwrap(), open.unwrap(), close.unwrap());
/// assert_eq!((wick.x, wick.y, wick.h), (10.0, 10.0, 30.0));
/// assert_eq!((open.x, open.w, open.y, open.h), (7.0, 3.0, 20.0, 1.0));
/// assert_eq!((close.x, close.w, close.y), (11.0, 3.0, 30.0));
/// ```
pub fn ohlc_bar(xc: f32, m: BarMetrics, ys: [f32; 4]) -> [Option<RectPx>; 3] {
    let [y_open, y_high, y_low, y_close] = ys;
    let column = xc.floor();
    let (wy, wh) = v_span(y_high, y_low);
    let wick = RectPx {
        x: column,
        y: wy,
        w: 1.0,
        h: wh,
    };
    let half = (m.body - 1.0) * 0.5;
    if !m.show_body || half < 1.0 {
        return [Some(wick), None, None];
    }
    let (oy, _) = v_span(y_open, y_open);
    let (cy, _) = v_span(y_close, y_close);
    let open = RectPx {
        x: column - half,
        y: oy,
        w: half,
        h: 1.0,
    };
    let close = RectPx {
        x: column + 1.0,
        y: cy,
        w: half,
        h: 1.0,
    };
    [Some(wick), Some(open), Some(close)]
}

/// Alpha coverage in 1/255 steps, as both backends blend it:
/// `floor(coverage * 255 + 0.5)`. Shader: `coverage_8()`.
pub fn coverage_8(coverage: f32) -> u8 {
    (coverage * 255.0 + 0.5).floor().clamp(0.0, 255.0) as u8
}

/// Coverage of the pixel centered at `p` by the segment `a`→`b` drawn
/// `2 × half_width` wide with round caps (consecutive segments of a polyline
/// join without gaps). Anti-aliased over one pixel. Shader:
/// `segment_coverage()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::geom::{PointPx, Px};
/// use ferrochart_core::transform_helpers::segment_coverage;
///
/// let (a, b) = (PointPx { x: 0.0, y: 10.0 }, PointPx { x: 20.0, y: 10.0 });
/// assert_eq!(segment_coverage(PointPx { x: 5.5, y: 10.5 }, a, b, Px(1.0)), 1.0);
/// assert_eq!(segment_coverage(PointPx { x: 5.5, y: 13.5 }, a, b, Px(1.0)), 0.0);
/// ```
pub fn segment_coverage(p: PointPx, a: PointPx, b: PointPx, half_width: Px) -> f32 {
    let pax = p.x - a.x;
    let pay = p.y - a.y;
    let bax = b.x - a.x;
    let bay = b.y - a.y;
    let len2 = bax * bax + bay * bay;
    let mut h = 0.0;
    if len2 > 0.0 {
        h = ((pax * bax + pay * bay) / len2).clamp(0.0, 1.0);
    }
    let dx = pax - bax * h;
    let dy = pay - bay * h;
    let d = (dx * dx + dy * dy).sqrt();
    (half_width.0 + 0.5 - d).clamp(0.0, 1.0)
}

/// Direction and length of the line `a`→`b` (the length is at least 1e-4,
/// so the direction is always defined). Shader: `vs_line()`.
pub fn line_axis(a: PointPx, b: PointPx) -> (PointPx, f32) {
    let dx = b.x - a.x;
    let dy = b.y - a.y;
    let len = (dx * dx + dy * dy).sqrt().max(1e-4);
    (
        PointPx {
            x: dx / len,
            y: dy / len,
        },
        len,
    )
}

/// Coverage of a pixel by an immediate line (`LineInst`) of length
/// `len` and `width`, butt ends, anti-aliased over one pixel: `along` is the
/// pixel center's distance along the line from its start, `across` its
/// signed distance from the axis. Dashes are `4w` on, `4w` off and dots `w`
/// on, `2w` off, measured along the line. Shader: `line_coverage()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::frame::LineDash;
/// use ferrochart_core::geom::Px;
/// use ferrochart_core::transform_helpers::line_coverage;
///
/// assert_eq!(line_coverage(5.0, 0.0, 10.0, Px(1.0), LineDash::Solid), 1.0);
/// assert_eq!(line_coverage(5.0, 2.0, 10.0, Px(1.0), LineDash::Solid), 0.0);
/// assert_eq!(line_coverage(5.0, 0.0, 10.0, Px(1.0), LineDash::Dashed), 0.0);
/// ```
pub fn line_coverage(along: f32, across: f32, len: f32, width: Px, dash: LineDash) -> f32 {
    let half = width.0 * 0.5;
    let cov_across = (half + 0.5 - across.abs()).clamp(0.0, 1.0);
    let cov_along = (along.min(len - along) + 0.5).clamp(0.0, 1.0);
    let on = match dash {
        LineDash::Solid => true,
        LineDash::Dashed => {
            let t = along / (8.0 * width.0);
            t - t.floor() < 0.5
        }
        LineDash::Dotted => {
            let t = along / (3.0 * width.0);
            t - t.floor() < 1.0 / 3.0
        }
    };
    if on { cov_across * cov_along } else { 0.0 }
}

/// Built-in icon: filled circle (markers).
pub const ICON_CIRCLE: u16 = 0;
/// Built-in icon: filled square.
pub const ICON_SQUARE: u16 = 1;
/// Built-in icon: arrow pointing up.
pub const ICON_ARROW_UP: u16 = 2;
/// Built-in icon: arrow pointing down.
pub const ICON_ARROW_DOWN: u16 = 3;

/// Coverage of the pixel whose center is `(dx, dy)` from the center of a
/// built-in icon `size` pixels across (the "atlas" is these signed
/// distance functions), anti-aliased over one pixel; 0 for unknown icons.
/// Shader: `icon_coverage()` in `prims.wgsl`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::transform_helpers::{ICON_ARROW_UP, ICON_CIRCLE, icon_coverage};
///
/// assert_eq!(icon_coverage(ICON_CIRCLE, 0.5, 0.5, 10.0), 1.0);
/// assert_eq!(icon_coverage(ICON_CIRCLE, 6.5, 0.5, 10.0), 0.0);
/// // The tip of an up arrow is at the top (y grows downwards).
/// assert!(icon_coverage(ICON_ARROW_UP, 0.0, -4.0, 10.0) > 0.0);
/// assert_eq!(icon_coverage(ICON_ARROW_UP, 4.0, -4.0, 10.0), 0.0);
/// ```
pub fn icon_coverage(icon: u16, dx: f32, dy: f32, size: f32) -> f32 {
    let h = size * 0.5;
    let ax = dx.abs();
    let d = match icon {
        ICON_CIRCLE => (dx * dx + dy * dy).sqrt() - h,
        ICON_SQUARE => {
            let s = h * 0.8;
            (ax - s).max(dy.abs() - s)
        }
        ICON_ARROW_UP | ICON_ARROW_DOWN => {
            let ty = if icon == ICON_ARROW_UP { dy } else { -dy };
            // Head: the triangle (0, -h), (±h, 0); shaft: |x| ≤ 0.35h, 0 ≤ y ≤ h.
            let head = ty.max((ax - ty - h) * 0.707_106_77);
            let q = h * 0.5;
            let shaft = (ax - h * 0.35).max((ty - q).abs() - q);
            head.min(shaft)
        }
        _ => return 0.0,
    };
    (0.5 - d).clamp(0.0, 1.0)
}

/// Whether the dash pattern is "on" at pixel center `x` for a line of the
/// given width (patterns follow the x axis, which suits the mostly horizontal
/// series lines). Dashes: `4w` on, `4w` off; dots: `w` on, `w` off. Shader:
/// `dash_on()`.
pub fn dash_on(x: Px, width: Px, dash: LineDash) -> bool {
    let period = match dash {
        LineDash::Solid => return true,
        LineDash::Dashed => 4.0 * width.0,
        LineDash::Dotted => width.0,
    };
    let k = (x.0 / period).floor();
    k - 2.0 * (k * 0.5).floor() < 0.5
}

/// Coverage of the pixel centered at `p` by the fill between the edge `a`→`b`
/// and the horizontal line `base_y`, within the column range `[a.x, b.x)`
/// (half-open, so adjacent columns never overlap). Both horizontal edges are
/// anti-aliased. Shader: `fill_coverage()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::geom::{PointPx, Px};
/// use ferrochart_core::transform_helpers::fill_coverage;
///
/// let (a, b) = (PointPx { x: 0.0, y: 10.0 }, PointPx { x: 10.0, y: 10.0 });
/// assert_eq!(fill_coverage(PointPx { x: 2.5, y: 15.5 }, a, b, Px(20.0)), 1.0);
/// assert_eq!(fill_coverage(PointPx { x: 2.5, y: 5.5 }, a, b, Px(20.0)), 0.0);
/// assert_eq!(fill_coverage(PointPx { x: 10.5, y: 15.5 }, a, b, Px(20.0)), 0.0);
/// ```
pub fn fill_coverage(p: PointPx, a: PointPx, b: PointPx, base_y: Px) -> f32 {
    if !(p.x >= a.x && p.x < b.x) {
        return 0.0;
    }
    let t = (p.x - a.x) / (b.x - a.x);
    let edge = a.y + (b.y - a.y) * t;
    let lo = edge.min(base_y.0);
    let hi = edge.max(base_y.0);
    ((p.y - lo).min(hi - p.y) + 0.5).clamp(0.0, 1.0)
}

/// Coverage of the pixel centered at `p` by a band between the upper edge
/// `a`→`b` and the lower edge from `(a.x, lower_a)` to `(b.x, lower_b)`,
/// within `[a.x, b.x)` like [`fill_coverage`], and whether the upper series
/// is below the lower one there (`crossed`). `scale` is the y transform's
/// `a`: its sign says which way values grow on screen. Shader:
/// `band_coverage()`.
///
/// # Examples
///
/// ```
/// use ferrochart_core::geom::PointPx;
/// use ferrochart_core::transform_helpers::band_coverage;
///
/// let (a, b) = (PointPx { x: 0.0, y: 10.0 }, PointPx { x: 10.0, y: 10.0 });
/// // Lower edge at y = 20: inside between 10 and 20 (y grows downwards, a < 0).
/// assert_eq!(band_coverage(PointPx { x: 5.5, y: 15.5 }, a, b, 20.0, 20.0, -1.0), (1.0, false));
/// assert_eq!(band_coverage(PointPx { x: 5.5, y: 25.5 }, a, b, 20.0, 20.0, -1.0).0, 0.0);
/// // The "lower" series drawn above it: crossed.
/// assert!(band_coverage(PointPx { x: 5.5, y: 7.5 }, a, b, 5.0, 5.0, -1.0).1);
/// ```
pub fn band_coverage(
    p: PointPx,
    a: PointPx,
    b: PointPx,
    lower_a: f32,
    lower_b: f32,
    scale: f32,
) -> (f32, bool) {
    if !(p.x >= a.x && p.x < b.x) {
        return (0.0, false);
    }
    let t = (p.x - a.x) / (b.x - a.x);
    let upper = a.y + (b.y - a.y) * t;
    let lower = lower_a + (lower_b - lower_a) * t;
    let lo = upper.min(lower);
    let hi = upper.max(lower);
    let coverage = ((p.y - lo).min(hi - p.y) + 0.5).clamp(0.0, 1.0);
    (coverage, (lower - upper) * scale > 0.0)
}

/// The line segments of one element of a value series. `cur` and `next` are
/// the stored relative `[first, max, min, last]` of elements `j` and `j + 1`
/// (both relative to element `j`'s chunk reference), `xj`/`xn` their center
/// x. Segment 0 is the max-min range of an LOD bucket (absent at level 0,
/// where max == min); segment 1 connects `last(j)` to `first(j + 1)`, or for a
/// step line runs horizontally to `xn`, and segment 2 is then the vertical
/// step. Gaps break the line. Shader: `vs_line_series`.
pub fn line_segments(
    yt: &YTransform,
    bias: f32,
    xj: f32,
    xn: f32,
    cur: [f32; 4],
    next: [f32; 4],
    step: bool,
) -> [Option<(PointPx, PointPx)>; 3] {
    let [_, hi, lo, last] = cur;
    let first_n = next[0];
    let p = |x: f32, v: f32| PointPx {
        x,
        y: y(yt, v, bias),
    };
    let mut out = [None; 3];
    if !is_gap(hi) && !is_gap(lo) && hi.to_bits() != lo.to_bits() {
        out[0] = Some((p(xj, hi), p(xj, lo)));
    }
    if !is_gap(last) && !is_gap(first_n) {
        if step {
            out[1] = Some((p(xj, last), p(xn, last)));
            out[2] = Some((p(xn, last), p(xn, first_n)));
        } else {
            out[1] = Some((p(xj, last), p(xn, first_n)));
        }
    }
    out
}

/// The top edge of the fill under one element of an area or baseline series
/// (see [`line_segments`]); `None` across gaps. Shader: `vs_fill_series`.
pub fn fill_edge(
    yt: &YTransform,
    bias: f32,
    xj: f32,
    xn: f32,
    cur: [f32; 4],
    next: [f32; 4],
    step: bool,
) -> Option<(PointPx, PointPx)> {
    let (last, first_n) = (cur[3], next[0]);
    if is_gap(last) || is_gap(first_n) {
        return None;
    }
    let y0 = y(yt, last, bias);
    let y1 = if step { y0 } else { y(yt, first_n, bias) };
    Some((PointPx { x: xj, y: y0 }, PointPx { x: xn, y: y1 }))
}

/// The y a histogram column of a value series reaches: of the element's max
/// and min, the one farther from the base (so LOD buckets keep their spikes).
/// `None` for a gap. Shader: `vs_value_histogram`.
pub fn histogram_value_y(yt: &YTransform, bias: f32, cur: [f32; 4], base_y: f32) -> Option<f32> {
    let (hi, lo) = (cur[1], cur[2]);
    if is_gap(hi) || is_gap(lo) {
        return None;
    }
    let (y_hi, y_lo) = (y(yt, hi, bias), y(yt, lo, bias));
    Some(if (y_hi - base_y).abs() >= (y_lo - base_y).abs() {
        y_hi
    } else {
        y_lo
    })
}

/// Position of `y` in a vertical gradient from `top` (0) to `bottom` (1).
/// Shader: `gradient_t()`.
pub fn gradient_t(y: Px, top: Px, bottom: Px) -> f32 {
    let h = bottom.0 - top.0;
    if h <= 0.0 {
        return 0.0;
    }
    ((y.0 - top.0) / h).clamp(0.0, 1.0)
}

#[cfg(test)]
#[allow(clippy::float_cmp)] // exact values are the point of these tests
mod tests {
    use super::*;

    #[test]
    fn body_is_odd_and_fits_in_step() {
        for i in 1..2000 {
            let step = i as f32 * 0.05;
            let m = bar_metrics(step);
            assert!(m.body >= 1.0);
            assert_eq!(m.body % 2.0, 1.0, "step {step}");
            assert!(m.body <= step.max(1.0), "step {step}");
        }
    }

    #[test]
    fn log_mapping_is_relative() {
        let yt = YTransform {
            a: -100.0,
            b: 0.0,
            view_ref: 10.0,
            mapping: PriceMapping::Log,
        };
        // One e-fold above the reference moves 100 px up.
        assert!((y_of_price(&yt, 10.0 * core::f64::consts::E) + 100.0).abs() < 1e-3);
    }
}