#![allow(missing_docs)]
#![allow(dead_code)]
use serde::{Deserialize, Serialize};
pub type DrawColor = [f32; 4];
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DrawDuration {
Single,
Persistent,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DrawCommand {
Line {
start: [f64; 3],
end: [f64; 3],
color: DrawColor,
duration: DrawDuration,
},
Sphere {
center: [f64; 3],
radius: f64,
color: DrawColor,
duration: DrawDuration,
},
Aabb {
min: [f64; 3],
max: [f64; 3],
color: DrawColor,
duration: DrawDuration,
},
Arrow {
from: [f64; 3],
to: [f64; 3],
head_size: f64,
color: DrawColor,
duration: DrawDuration,
},
Cross {
center: [f64; 3],
size: f64,
color: DrawColor,
duration: DrawDuration,
},
Text {
position: [f64; 3],
label: String,
color: DrawColor,
duration: DrawDuration,
},
}
impl DrawCommand {
pub fn duration(&self) -> &DrawDuration {
match self {
DrawCommand::Line { duration, .. } => duration,
DrawCommand::Sphere { duration, .. } => duration,
DrawCommand::Aabb { duration, .. } => duration,
DrawCommand::Arrow { duration, .. } => duration,
DrawCommand::Cross { duration, .. } => duration,
DrawCommand::Text { duration, .. } => duration,
}
}
pub fn is_single(&self) -> bool {
*self.duration() == DrawDuration::Single
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct DrawList {
pub commands: Vec<DrawCommand>,
}
impl DrawList {
pub fn new() -> Self {
Self::default()
}
pub fn push(&mut self, cmd: DrawCommand) {
self.commands.push(cmd);
}
pub fn add_line(
&mut self,
start: [f64; 3],
end: [f64; 3],
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Line {
start,
end,
color,
duration,
});
}
pub fn add_sphere(
&mut self,
center: [f64; 3],
radius: f64,
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Sphere {
center,
radius,
color,
duration,
});
}
pub fn add_aabb(
&mut self,
min: [f64; 3],
max: [f64; 3],
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Aabb {
min,
max,
color,
duration,
});
}
pub fn add_arrow(
&mut self,
from: [f64; 3],
to: [f64; 3],
head_size: f64,
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Arrow {
from,
to,
head_size,
color,
duration,
});
}
pub fn add_cross(
&mut self,
center: [f64; 3],
size: f64,
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Cross {
center,
size,
color,
duration,
});
}
pub fn add_text(
&mut self,
position: [f64; 3],
label: impl Into<String>,
color: DrawColor,
duration: DrawDuration,
) {
self.commands.push(DrawCommand::Text {
position,
label: label.into(),
color,
duration,
});
}
pub fn drain_single(&mut self) -> Vec<DrawCommand> {
let mut singles = Vec::new();
let mut persistent = Vec::new();
for cmd in self.commands.drain(..) {
if cmd.is_single() {
singles.push(cmd);
} else {
persistent.push(cmd);
}
}
self.commands = persistent;
singles
}
pub fn iter(&self) -> impl Iterator<Item = &DrawCommand> {
self.commands.iter()
}
pub fn clear(&mut self) {
self.commands.clear();
}
pub fn len(&self) -> usize {
self.commands.len()
}
pub fn is_empty(&self) -> bool {
self.commands.is_empty()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DrawnFrame {
pub step: u64,
pub commands: Vec<DrawCommand>,
}
pub struct DebugDrawSession {
pub list: DrawList,
step: u64,
}
impl DebugDrawSession {
pub fn new() -> Self {
Self {
list: DrawList::new(),
step: 0,
}
}
pub fn begin_step(&mut self, step: u64) {
self.step = step;
}
pub fn end_step(&mut self) -> DrawnFrame {
let single_cmds = self.list.drain_single();
DrawnFrame {
step: self.step,
commands: single_cmds,
}
}
pub fn line(&mut self, start: [f64; 3], end: [f64; 3], color: DrawColor) {
self.list.add_line(start, end, color, DrawDuration::Single);
}
pub fn sphere(&mut self, center: [f64; 3], radius: f64, color: DrawColor) {
self.list
.add_sphere(center, radius, color, DrawDuration::Single);
}
pub fn aabb(&mut self, min: [f64; 3], max: [f64; 3], color: DrawColor) {
self.list.add_aabb(min, max, color, DrawDuration::Single);
}
pub fn arrow(&mut self, from: [f64; 3], to: [f64; 3], head_size: f64, color: DrawColor) {
self.list
.add_arrow(from, to, head_size, color, DrawDuration::Single);
}
pub fn cross(&mut self, center: [f64; 3], size: f64, color: DrawColor) {
self.list
.add_cross(center, size, color, DrawDuration::Single);
}
pub fn text(&mut self, position: [f64; 3], label: impl Into<String>, color: DrawColor) {
self.list
.add_text(position, label, color, DrawDuration::Single);
}
pub fn step(&self) -> u64 {
self.step
}
}
impl Default for DebugDrawSession {
fn default() -> Self {
Self::new()
}
}
pub const RED: DrawColor = [1.0, 0.0, 0.0, 1.0];
pub const GREEN: DrawColor = [0.0, 1.0, 0.0, 1.0];
pub const BLUE: DrawColor = [0.0, 0.0, 1.0, 1.0];
pub const YELLOW: DrawColor = [1.0, 1.0, 0.0, 1.0];
pub const CYAN: DrawColor = [0.0, 1.0, 1.0, 1.0];
pub const MAGENTA: DrawColor = [1.0, 0.0, 1.0, 1.0];
pub const WHITE: DrawColor = [1.0, 1.0, 1.0, 1.0];
pub const ORANGE: DrawColor = [1.0, 0.5, 0.0, 1.0];