use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::ids::{DataId, NodeId};
use crate::style::{Color, ColorSpec, LineStyle, MarkerShape, MarkerStyle};
use crate::text::Text;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Artist {
Line(Line),
Scatter(Scatter),
Contour(Contour),
Quiver(Quiver),
Surface(Surface),
}
impl Artist {
pub fn id(&self) -> NodeId {
match self {
Artist::Line(a) => a.id,
Artist::Scatter(a) => a.id,
Artist::Contour(a) => a.id,
Artist::Quiver(a) => a.id,
Artist::Surface(a) => a.id,
}
}
pub fn display_name(&self) -> Option<&Text> {
match self {
Artist::Line(a) => a.display_name.as_ref(),
Artist::Scatter(a) => a.display_name.as_ref(),
Artist::Contour(a) => a.display_name.as_ref(),
Artist::Quiver(a) => a.display_name.as_ref(),
Artist::Surface(a) => a.display_name.as_ref(),
}
}
pub fn visible(&self) -> bool {
match self {
Artist::Line(a) => a.visible,
Artist::Scatter(a) => a.visible,
Artist::Contour(a) => a.visible,
Artist::Quiver(a) => a.visible,
Artist::Surface(a) => a.visible,
}
}
pub fn set_visible(&mut self, visible: bool) {
match self {
Artist::Line(a) => a.visible = visible,
Artist::Scatter(a) => a.visible = visible,
Artist::Contour(a) => a.visible = visible,
Artist::Quiver(a) => a.visible = visible,
Artist::Surface(a) => a.visible = visible,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Line {
pub id: NodeId,
pub display_name: Option<Text>,
pub visible: bool,
pub x: DataId,
pub y: DataId,
pub z: Option<DataId>,
pub line: LineStyle,
pub marker: MarkerStyle,
}
impl Default for Line {
fn default() -> Self {
Self {
id: NodeId::default(),
display_name: None,
visible: true,
x: DataId::default(),
y: DataId::default(),
z: None,
line: LineStyle::default(),
marker: MarkerStyle::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Scatter {
pub id: NodeId,
pub display_name: Option<Text>,
pub visible: bool,
pub x: DataId,
pub y: DataId,
pub z: Option<DataId>,
pub size: ScatterSize,
pub color: ScatterColor,
pub marker: MarkerStyle,
}
impl Default for Scatter {
fn default() -> Self {
Self {
id: NodeId::default(),
display_name: None,
visible: true,
x: DataId::default(),
y: DataId::default(),
z: None,
size: ScatterSize::default(),
color: ScatterColor::default(),
marker: MarkerStyle {
shape: MarkerShape::Circle,
..MarkerStyle::default()
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ScatterSize {
Scalar {
value: f64,
},
Data {
data: DataId,
},
}
impl Default for ScatterSize {
fn default() -> Self {
ScatterSize::Scalar { value: 4.0 }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ScatterColor {
Spec {
spec: ColorSpec,
},
Data {
data: DataId,
},
}
impl Default for ScatterColor {
fn default() -> Self {
ScatterColor::Spec {
spec: ColorSpec::Auto,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Contour {
pub id: NodeId,
pub display_name: Option<Text>,
pub visible: bool,
pub grid: Grid,
pub z: DataId,
pub levels: Levels,
pub fill: bool,
pub placement: ContourPlacement,
pub line: LineStyle,
}
impl Default for Contour {
fn default() -> Self {
Self {
id: NodeId::default(),
display_name: None,
visible: true,
grid: Grid::default(),
z: DataId::default(),
levels: Levels::default(),
fill: false,
placement: ContourPlacement::default(),
line: LineStyle {
color: ColorSpec::Colormapped,
..LineStyle::default()
},
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Grid {
Rectilinear {
x: DataId,
y: DataId,
},
Curvilinear {
x: DataId,
y: DataId,
},
}
impl Default for Grid {
fn default() -> Self {
Grid::Rectilinear {
x: DataId::default(),
y: DataId::default(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum Levels {
Auto {
count: u32,
},
Explicit {
values: Vec<f64>,
},
}
impl Default for Levels {
fn default() -> Self {
Levels::Auto { count: 10 }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContourPlacement {
Plane {
z: Option<f64>,
},
AtLevel,
}
impl Default for ContourPlacement {
fn default() -> Self {
ContourPlacement::Plane { z: None }
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Quiver {
pub id: NodeId,
pub display_name: Option<Text>,
pub visible: bool,
pub x: DataId,
pub y: DataId,
pub z: Option<DataId>,
pub u: DataId,
pub v: DataId,
pub w: Option<DataId>,
pub scale: QuiverScale,
pub line: LineStyle,
pub head_size: f64,
}
impl Default for Quiver {
fn default() -> Self {
Self {
id: NodeId::default(),
display_name: None,
visible: true,
x: DataId::default(),
y: DataId::default(),
z: None,
u: DataId::default(),
v: DataId::default(),
w: None,
scale: QuiverScale::default(),
line: LineStyle::default(),
head_size: 0.3,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum QuiverScale {
#[default]
Auto,
Factor {
value: f64,
},
Off,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct Surface {
pub id: NodeId,
pub display_name: Option<Text>,
pub visible: bool,
pub grid: Grid,
pub z: DataId,
pub c: Option<DataId>,
pub face: ColorSpec,
pub edge: ColorSpec,
pub edge_width_pt: f64,
}
impl Default for Surface {
fn default() -> Self {
Self {
id: NodeId::default(),
display_name: None,
visible: true,
grid: Grid::default(),
z: DataId::default(),
c: None,
face: ColorSpec::Colormapped,
edge: ColorSpec::Rgba {
color: Color::BLACK,
},
edge_width_pt: 0.5,
}
}
}