Skip to main content

runmat_plot/plots/
contour_fill.rs

1//! Filled contour plot implementation (triangles on the base plane).
2
3use crate::core::{
4    BoundingBox, DrawCall, GpuVertexBuffer, Material, PipelineType, RenderData, Vertex,
5};
6use glam::Vec4;
7
8#[derive(Debug, Clone)]
9pub struct ContourFillPlot {
10    pub label: Option<String>,
11    pub visible: bool,
12    vertices: Option<Vec<Vertex>>,
13    gpu_vertices: Option<GpuVertexBuffer>,
14    vertex_count: usize,
15    bounds: BoundingBox,
16}
17
18impl ContourFillPlot {
19    pub fn from_vertices(vertices: Vec<Vertex>, bounds: BoundingBox) -> Self {
20        let vertex_count = vertices.len();
21        Self {
22            label: None,
23            visible: true,
24            vertices: Some(vertices),
25            gpu_vertices: None,
26            vertex_count,
27            bounds,
28        }
29    }
30
31    pub fn from_gpu_buffer(
32        buffer: GpuVertexBuffer,
33        vertex_count: usize,
34        bounds: BoundingBox,
35    ) -> Self {
36        Self {
37            label: None,
38            visible: true,
39            vertices: None,
40            gpu_vertices: Some(buffer),
41            vertex_count,
42            bounds,
43        }
44    }
45
46    pub fn with_label<S: Into<String>>(mut self, label: S) -> Self {
47        self.label = Some(label.into());
48        self
49    }
50
51    pub fn set_visible(&mut self, visible: bool) {
52        self.visible = visible;
53    }
54
55    pub fn bounds(&self) -> BoundingBox {
56        self.bounds
57    }
58
59    pub fn cpu_vertices(&self) -> Option<&[Vertex]> {
60        self.vertices.as_deref()
61    }
62
63    pub fn render_data(&mut self) -> RenderData {
64        let bounds = self.bounds();
65        let material = Material {
66            albedo: Vec4::ONE,
67            ..Default::default()
68        };
69
70        let (vertices, gpu_vertices) = if let Some(buffer) = &self.gpu_vertices {
71            (Vec::new(), Some(buffer.clone()))
72        } else {
73            (self.vertices.clone().unwrap_or_default(), None)
74        };
75
76        let draw_call = DrawCall {
77            vertex_offset: 0,
78            vertex_count: self.vertex_count,
79            index_offset: None,
80            index_count: None,
81            instance_count: 1,
82        };
83
84        RenderData {
85            pipeline_type: PipelineType::Triangles,
86            vertices,
87            indices: None,
88            gpu_vertices,
89            bounds: Some(bounds),
90            material,
91            draw_calls: vec![draw_call],
92            image: None,
93        }
94    }
95
96    pub fn estimated_memory_usage(&self) -> usize {
97        self.vertices
98            .as_ref()
99            .map(|v| v.len() * std::mem::size_of::<Vertex>())
100            .unwrap_or(0)
101    }
102}