cellular_raza_concepts/
plotting.rs

1use plotters::backend::DrawingBackend;
2use plotters::coord::cartesian::Cartesian2d;
3use plotters::coord::types::RangedCoordf64;
4use plotters::prelude::BitMapBackend;
5use plotters::prelude::DrawingArea;
6use plotters::prelude::SVGBackend;
7
8use crate::errors::DrawingError;
9
10/// Creates a new plotting root which can then be drawn upon.
11pub trait CreatePlottingRoot //, E>
12// E: std::error::Error + std::marker::Sync + std::marker::Send,
13{
14    /// Creates a bitmap plotting root.
15    fn create_bitmap_root<'a, T>(
16        &self,
17        image_size: u32,
18        filename: &'a T,
19    ) -> Result<
20        DrawingArea<BitMapBackend<'a>, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
21        DrawingError,
22    >
23    where
24        T: AsRef<std::path::Path> + ?Sized;
25    // TODO implement this as well
26    /* fn create_svg_root<'a>(
27        &self,
28        image_size: u32,
29        filename: &'a String,
30    ) -> Result<
31        DrawingArea<SVGBackend<'a>, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
32        DrawingError,
33    >;*/
34}
35
36/// Allows elements of the simulation such as cells and voxels to draw themselves onto a plotting root.
37/// Typically, voxels will draw first and cells afterwards.
38pub trait PlotSelf {
39    /// Define which elements to draw when plotting the element itself.
40    fn plot_self<Db>(
41        &self,
42        root: &mut DrawingArea<Db, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
43    ) -> Result<(), DrawingError>
44    where
45        Db: DrawingBackend;
46
47    /// Overload for backend to have a purely bitmap function.
48    /// User are not expected to change this function.
49    fn plot_self_bitmap(
50        &self,
51        root: &mut DrawingArea<BitMapBackend, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
52    ) -> Result<(), DrawingError> {
53        self.plot_self(root)
54    }
55
56    /// Overload for backend to have a purely bitmap function.
57    /// User are not expected to change this function.
58    fn plot_self_svg(
59        &self,
60        root: &mut DrawingArea<SVGBackend, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
61    ) -> Result<(), DrawingError> {
62        self.plot_self(root)
63    }
64}
65
66use crate::cell::CellBox;
67use serde::{Deserialize, Serialize};
68
69impl<Cel> PlotSelf for CellBox<Cel>
70where
71    Cel: PlotSelf + Serialize + for<'a> Deserialize<'a>,
72{
73    fn plot_self<Db>(
74        &self,
75        root: &mut DrawingArea<Db, Cartesian2d<RangedCoordf64, RangedCoordf64>>,
76    ) -> Result<(), DrawingError>
77    where
78        Db: DrawingBackend,
79    {
80        self.cell.plot_self(root)
81    }
82}