runmat-plot 0.4.0

GPU-accelerated and static plotting for RunMat with WGPU and Plotters
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
//! Error bar plot implementation.

use crate::core::{
    marker_shape_code, vertex_utils, AlphaMode, BoundingBox, DrawCall, GpuVertexBuffer, Material,
    PipelineType, RenderData, Vertex,
};
use crate::plots::line::{LineMarkerAppearance, LineStyle};
use glam::{Vec3, Vec4};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorBarOrientation {
    Vertical,
    Horizontal,
    Both,
}

#[derive(Debug, Clone)]
pub struct ErrorBar {
    pub x: Vec<f64>,
    pub y: Vec<f64>,
    pub y_neg: Vec<f64>,
    pub y_pos: Vec<f64>,
    pub x_neg: Vec<f64>,
    pub x_pos: Vec<f64>,
    pub orientation: ErrorBarOrientation,

    pub color: Vec4,
    pub line_width: f32,
    pub line_style: LineStyle,
    pub cap_size: f32,
    pub marker: Option<LineMarkerAppearance>,

    pub label: Option<String>,
    pub visible: bool,

    vertices: Option<Vec<Vertex>>,
    bounds: Option<BoundingBox>,
    dirty: bool,
    gpu_vertices: Option<GpuVertexBuffer>,
    gpu_vertex_count: Option<usize>,
    gpu_bounds: Option<BoundingBox>,
    marker_vertices: Option<Vec<Vertex>>,
    marker_gpu_vertices: Option<GpuVertexBuffer>,
    marker_dirty: bool,
}

impl ErrorBar {
    pub fn new_vertical(
        x: Vec<f64>,
        y: Vec<f64>,
        y_neg: Vec<f64>,
        y_pos: Vec<f64>,
    ) -> Result<Self, String> {
        let n = x.len();
        if n == 0 || y.len() != n || y_neg.len() != n || y_pos.len() != n {
            return Err("errorbar: input vectors must have equal non-zero length".to_string());
        }
        Ok(Self {
            x,
            y,
            y_neg,
            y_pos,
            x_neg: vec![0.0; n],
            x_pos: vec![0.0; n],
            orientation: ErrorBarOrientation::Vertical,
            color: Vec4::new(0.0, 0.0, 0.0, 1.0),
            line_width: 1.0,
            line_style: LineStyle::Solid,
            cap_size: 6.0,
            marker: Some(LineMarkerAppearance {
                kind: crate::plots::scatter::MarkerStyle::Circle,
                size: 6.0,
                edge_color: Vec4::new(0.0, 0.0, 0.0, 1.0),
                face_color: Vec4::new(0.0, 0.0, 0.0, 1.0),
                filled: false,
            }),
            label: None,
            visible: true,
            vertices: None,
            bounds: None,
            dirty: true,
            gpu_vertices: None,
            gpu_vertex_count: None,
            gpu_bounds: None,
            marker_vertices: None,
            marker_gpu_vertices: None,
            marker_dirty: true,
        })
    }

    pub fn new_both(
        x: Vec<f64>,
        y: Vec<f64>,
        x_neg: Vec<f64>,
        x_pos: Vec<f64>,
        y_neg: Vec<f64>,
        y_pos: Vec<f64>,
    ) -> Result<Self, String> {
        let n = x.len();
        if n == 0
            || y.len() != n
            || x_neg.len() != n
            || x_pos.len() != n
            || y_neg.len() != n
            || y_pos.len() != n
        {
            return Err("errorbar: input vectors must have equal non-zero length".to_string());
        }
        let mut plot = Self::new_vertical(x, y, y_neg, y_pos)?;
        plot.x_neg = x_neg;
        plot.x_pos = x_pos;
        plot.orientation = ErrorBarOrientation::Both;
        Ok(plot)
    }

    pub fn with_style(
        mut self,
        color: Vec4,
        line_width: f32,
        line_style: LineStyle,
        cap_size: f32,
    ) -> Self {
        self.color = color;
        self.line_width = line_width.max(0.5);
        self.line_style = line_style;
        self.cap_size = cap_size.max(0.0);
        self.dirty = true;
        self.gpu_vertices = None;
        self.gpu_vertex_count = None;
        self.gpu_bounds = None;
        self.marker_gpu_vertices = None;
        self.marker_dirty = true;
        self
    }

    pub fn with_label<S: Into<String>>(mut self, label: S) -> Self {
        self.label = Some(label.into());
        self
    }

    pub fn set_visible(&mut self, visible: bool) {
        self.visible = visible;
    }

    pub fn set_marker(&mut self, marker: Option<LineMarkerAppearance>) {
        self.marker = marker;
        self.marker_dirty = true;
        if self.marker.is_none() {
            self.marker_vertices = None;
            self.marker_gpu_vertices = None;
        }
    }

    pub fn set_marker_gpu_vertices(&mut self, buffer: Option<GpuVertexBuffer>) {
        let has_gpu = buffer.is_some();
        self.marker_gpu_vertices = buffer;
        if has_gpu {
            self.marker_vertices = None;
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn from_gpu_buffer(
        color: Vec4,
        line_width: f32,
        line_style: LineStyle,
        cap_size: f32,
        orientation: ErrorBarOrientation,
        buffer: GpuVertexBuffer,
        vertex_count: usize,
        bounds: BoundingBox,
    ) -> Self {
        Self {
            x: Vec::new(),
            y: Vec::new(),
            y_neg: Vec::new(),
            y_pos: Vec::new(),
            x_neg: Vec::new(),
            x_pos: Vec::new(),
            orientation,
            color,
            line_width,
            line_style,
            cap_size,
            marker: None,
            label: None,
            visible: true,
            vertices: None,
            bounds: None,
            dirty: false,
            gpu_vertices: Some(buffer),
            gpu_vertex_count: Some(vertex_count),
            gpu_bounds: Some(bounds),
            marker_vertices: None,
            marker_gpu_vertices: None,
            marker_dirty: true,
        }
    }

    pub fn generate_vertices(&mut self) -> &Vec<Vertex> {
        if self.gpu_vertices.is_some() {
            if self.vertices.is_none() {
                self.vertices = Some(Vec::new());
            }
            return self.vertices.as_ref().unwrap();
        }
        if self.dirty || self.vertices.is_none() {
            let mut verts = Vec::new();
            for i in 0..self.x.len() {
                let xi = self.x[i] as f32;
                let yi = self.y[i] as f32;
                if !xi.is_finite() || !yi.is_finite() {
                    continue;
                }
                if matches!(
                    self.orientation,
                    ErrorBarOrientation::Vertical | ErrorBarOrientation::Both
                ) {
                    let y0 = (self.y[i] - self.y_neg[i]) as f32;
                    let y1 = (self.y[i] + self.y_pos[i]) as f32;
                    if y0.is_finite() && y1.is_finite() && include_segment(i, self.line_style) {
                        verts.push(Vertex::new(Vec3::new(xi, y0, 0.0), self.color));
                        verts.push(Vertex::new(Vec3::new(xi, y1, 0.0), self.color));
                        if self.cap_size > 0.0 {
                            let half = self.cap_size * 0.005;
                            verts.push(Vertex::new(Vec3::new(xi - half, y0, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(xi + half, y0, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(xi - half, y1, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(xi + half, y1, 0.0), self.color));
                        }
                    }
                }
                if matches!(
                    self.orientation,
                    ErrorBarOrientation::Horizontal | ErrorBarOrientation::Both
                ) {
                    let x0 = (self.x[i] - self.x_neg[i]) as f32;
                    let x1 = (self.x[i] + self.x_pos[i]) as f32;
                    if x0.is_finite() && x1.is_finite() && include_segment(i, self.line_style) {
                        verts.push(Vertex::new(Vec3::new(x0, yi, 0.0), self.color));
                        verts.push(Vertex::new(Vec3::new(x1, yi, 0.0), self.color));
                        if self.cap_size > 0.0 {
                            let half = self.cap_size * 0.005;
                            verts.push(Vertex::new(Vec3::new(x0, yi - half, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(x0, yi + half, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(x1, yi - half, 0.0), self.color));
                            verts.push(Vertex::new(Vec3::new(x1, yi + half, 0.0), self.color));
                        }
                    }
                }
            }
            self.vertices = Some(verts);
            self.dirty = false;
        }
        self.vertices.as_ref().unwrap()
    }

    pub fn marker_render_data(&mut self) -> Option<RenderData> {
        let marker = self.marker.clone()?;
        if let Some(gpu_vertices) = self.marker_gpu_vertices.clone() {
            let vertex_count = gpu_vertices.vertex_count;
            if vertex_count == 0 {
                return None;
            }
            return Some(RenderData {
                pipeline_type: PipelineType::Points,
                vertices: Vec::new(),
                indices: None,
                gpu_vertices: Some(gpu_vertices),
                bounds: None,
                material: Material {
                    albedo: marker.face_color,
                    emissive: marker.edge_color,
                    roughness: 1.0,
                    metallic: marker_shape_code(marker.kind) as f32,
                    alpha_mode: if marker.face_color.w < 0.999 {
                        AlphaMode::Blend
                    } else {
                        AlphaMode::Opaque
                    },
                    ..Default::default()
                },
                draw_calls: vec![DrawCall {
                    vertex_offset: 0,
                    vertex_count,
                    index_offset: None,
                    index_count: None,
                    instance_count: 1,
                }],
                image: None,
            });
        }
        if self.marker_dirty || self.marker_vertices.is_none() {
            let mut vertices = Vec::new();
            for (&x, &y) in self.x.iter().zip(self.y.iter()) {
                let x = x as f32;
                let y = y as f32;
                if !x.is_finite() || !y.is_finite() {
                    continue;
                }
                vertices.push(Vertex {
                    position: [x, y, 0.0],
                    color: marker.face_color.to_array(),
                    normal: [0.0, 0.0, marker.size],
                    tex_coords: [0.0, 0.0],
                });
            }
            self.marker_vertices = Some(vertices);
            self.marker_dirty = false;
        }
        let vertices = self.marker_vertices.as_ref()?;
        if vertices.is_empty() {
            return None;
        }
        Some(RenderData {
            pipeline_type: PipelineType::Points,
            vertices: vertices.clone(),
            indices: None,
            gpu_vertices: None,
            bounds: None,
            material: Material {
                albedo: marker.face_color,
                emissive: marker.edge_color,
                roughness: 1.0,
                metallic: marker_shape_code(marker.kind) as f32,
                alpha_mode: if marker.face_color.w < 0.999 {
                    AlphaMode::Blend
                } else {
                    AlphaMode::Opaque
                },
                ..Default::default()
            },
            draw_calls: vec![DrawCall {
                vertex_offset: 0,
                vertex_count: vertices.len(),
                index_offset: None,
                index_count: None,
                instance_count: 1,
            }],
            image: None,
        })
    }

    pub fn bounds(&mut self) -> BoundingBox {
        if let Some(bounds) = self.gpu_bounds {
            return bounds;
        }
        if self.dirty || self.bounds.is_none() {
            let mut min = Vec3::new(f32::INFINITY, f32::INFINITY, 0.0);
            let mut max = Vec3::new(f32::NEG_INFINITY, f32::NEG_INFINITY, 0.0);
            for i in 0..self.x.len() {
                let xi = self.x[i] as f32;
                let yi = self.y[i] as f32;
                if !xi.is_finite() || !yi.is_finite() {
                    continue;
                }
                min.x = min
                    .x
                    .min(xi - self.x_neg.get(i).copied().unwrap_or(0.0) as f32);
                max.x = max
                    .x
                    .max(xi + self.x_pos.get(i).copied().unwrap_or(0.0) as f32);
                min.y = min
                    .y
                    .min(yi - self.y_neg.get(i).copied().unwrap_or(0.0) as f32);
                max.y = max
                    .y
                    .max(yi + self.y_pos.get(i).copied().unwrap_or(0.0) as f32);
            }
            if self.cap_size > 0.0 {
                let half = self.cap_size * 0.005;
                min.x -= half;
                max.x += half;
                min.y -= half;
                max.y += half;
            }
            if !min.x.is_finite() {
                min = Vec3::ZERO;
                max = Vec3::ZERO;
            }
            self.bounds = Some(BoundingBox::new(min, max));
        }
        self.bounds.unwrap()
    }

    pub fn render_data(&mut self) -> RenderData {
        let bounds = self.bounds();
        let (vertices, vertex_count, gpu_vertices) = if self.gpu_vertices.is_some() {
            (
                Vec::new(),
                self.gpu_vertex_count.unwrap_or(0),
                self.gpu_vertices.clone(),
            )
        } else {
            let vertices = self.generate_vertices().clone();
            let count = vertices.len();
            (vertices, count, None)
        };
        RenderData {
            pipeline_type: PipelineType::Lines,
            vertices,
            indices: None,
            gpu_vertices,
            bounds: Some(bounds),
            material: Material {
                albedo: self.color,
                roughness: self.line_width,
                ..Default::default()
            },
            draw_calls: vec![DrawCall {
                vertex_offset: 0,
                vertex_count,
                index_offset: None,
                index_count: None,
                instance_count: 1,
            }],
            image: None,
        }
    }

    pub fn render_data_with_viewport(&mut self, viewport_px: Option<(u32, u32)>) -> RenderData {
        if self.gpu_vertices.is_some() {
            return self.render_data();
        }

        let bounds = self.bounds();
        let (vertices, vertex_count, pipeline_type) = if self.line_width > 1.0 {
            let viewport_px = viewport_px.unwrap_or((600, 400));
            let data_per_px = crate::core::data_units_per_px(&bounds, viewport_px);
            let width_data = self.line_width.max(0.1) * data_per_px;
            let verts = self.generate_vertices().clone();
            let mut thick = Vec::new();
            for segment in verts.chunks_exact(2) {
                let x = [segment[0].position[0] as f64, segment[1].position[0] as f64];
                let y = [segment[0].position[1] as f64, segment[1].position[1] as f64];
                let color = Vec4::from_array(segment[0].color);
                thick.extend(vertex_utils::create_thick_polyline(
                    &x, &y, color, width_data,
                ));
            }
            let count = thick.len();
            (thick, count, PipelineType::Triangles)
        } else {
            let verts = self.generate_vertices().clone();
            let count = verts.len();
            (verts, count, PipelineType::Lines)
        };
        RenderData {
            pipeline_type,
            vertices,
            indices: None,
            gpu_vertices: None,
            bounds: Some(bounds),
            material: Material {
                albedo: self.color,
                roughness: self.line_width.max(0.0),
                ..Default::default()
            },
            draw_calls: vec![DrawCall {
                vertex_offset: 0,
                vertex_count,
                index_offset: None,
                index_count: None,
                instance_count: 1,
            }],
            image: None,
        }
    }

    pub fn estimated_memory_usage(&self) -> usize {
        self.vertices
            .as_ref()
            .map_or(0, |v| v.len() * std::mem::size_of::<Vertex>())
            + self
                .marker_vertices
                .as_ref()
                .map_or(0, |v| v.len() * std::mem::size_of::<Vertex>())
    }
}

fn include_segment(index: usize, style: LineStyle) -> bool {
    match style {
        LineStyle::Solid => true,
        LineStyle::Dashed => (index % 4) < 2,
        LineStyle::Dotted => index.is_multiple_of(4),
        LineStyle::DashDot => {
            let m = index % 6;
            m < 2 || m == 3
        }
    }
}

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

    #[test]
    fn errorbar_bounds_include_caps() {
        let mut plot = ErrorBar::new_vertical(
            vec![0.0, 1.0],
            vec![1.0, 2.0],
            vec![0.5, 0.5],
            vec![0.5, 0.5],
        )
        .unwrap()
        .with_style(Vec4::ONE, 1.0, LineStyle::Solid, 10.0);
        let bounds = plot.bounds();
        assert!(bounds.max.x > 1.0);
        assert!(bounds.min.x < 0.0);
    }

    #[test]
    fn thick_errorbar_use_viewport_aware_triangles() {
        let mut plot = ErrorBar::new_vertical(
            vec![0.0, 1.0],
            vec![1.0, 2.0],
            vec![0.5, 0.5],
            vec![0.5, 0.5],
        )
        .unwrap()
        .with_style(Vec4::ONE, 2.0, LineStyle::Solid, 6.0);
        let render = plot.render_data_with_viewport(Some((600, 400)));
        assert_eq!(render.pipeline_type, PipelineType::Triangles);
        assert!(!render.vertices.is_empty());
    }
}