use lievisual::geometry::{Point, Rect, Size};
use super::common::*;
use super::shape::*;
use super::unigraph::EdgeKind;
#[derive(Debug, Clone, Copy)]
pub enum RouteSegment {
Line { from: Point, to: Point },
CubicBezier {
p0: Point,
p1: Point,
p2: Point,
p3: Point,
},
}
impl RouteSegment {
pub fn start(&self) -> Point {
match self {
RouteSegment::Line { from, .. } => *from,
RouteSegment::CubicBezier { p0, .. } => *p0,
}
}
pub fn end(&self) -> Point {
match self {
RouteSegment::Line { to, .. } => *to,
RouteSegment::CubicBezier { p3, .. } => *p3,
}
}
pub fn length(&self) -> f64 {
match self {
RouteSegment::Line { from, to } => {
let dx = to.x - from.x;
let dy = to.y - from.y;
(dx * dx + dy * dy).sqrt()
}
RouteSegment::CubicBezier { p0, p1, p2, p3 } => {
let d01 = ((p1.x - p0.x).powi(2) + (p1.y - p0.y).powi(2)).sqrt();
let d12 = ((p2.x - p1.x).powi(2) + (p2.y - p1.y).powi(2)).sqrt();
let d23 = ((p3.x - p2.x).powi(2) + (p3.y - p2.y).powi(2)).sqrt();
d01 + d12 + d23
}
}
}
pub fn point_at(&self, t: f64) -> Point {
match self {
RouteSegment::Line { from, to } => {
Point::new(from.x + (to.x - from.x) * t, from.y + (to.y - from.y) * t)
}
RouteSegment::CubicBezier { p0, p1, p2, p3 } => {
let mt = 1.0 - t;
Point::new(
mt * mt * mt * p0.x
+ 3.0 * mt * mt * t * p1.x
+ 3.0 * mt * t * t * p2.x
+ t * t * t * p3.x,
mt * mt * mt * p0.y
+ 3.0 * mt * mt * t * p1.y
+ 3.0 * mt * t * t * p2.y
+ t * t * t * p3.y,
)
}
}
}
}
#[derive(Debug, Clone, Default)]
pub struct RoutePath(pub Vec<RouteSegment>);
impl RoutePath {
pub fn new() -> Self {
RoutePath(Vec::new())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn first(&self) -> Option<&RouteSegment> {
self.0.first()
}
pub fn last(&self) -> Option<&RouteSegment> {
self.0.last()
}
pub fn iter(&self) -> std::slice::Iter<'_, RouteSegment> {
self.0.iter()
}
pub fn push(&mut self, seg: RouteSegment) {
self.0.push(seg);
}
pub fn extend(&mut self, segs: impl IntoIterator<Item = RouteSegment>) {
self.0.extend(segs);
}
pub fn start(&self) -> Point {
self.first()
.map(RouteSegment::start)
.unwrap_or(Point::new(0.0, 0.0))
}
pub fn end(&self) -> Point {
self.last()
.map(RouteSegment::end)
.unwrap_or(Point::new(0.0, 0.0))
}
pub fn first_direction(&self) -> Point {
let Some(seg) = self.first() else {
return Point::new(0.0, 0.0);
};
match seg {
RouteSegment::Line { from, to } => norm(from, to),
RouteSegment::CubicBezier { p0, p1, .. } => norm(p0, p1),
}
}
pub fn last_direction(&self) -> Point {
let Some(seg) = self.last() else {
return Point::new(0.0, 0.0);
};
match seg {
RouteSegment::Line { from, to } => norm(from, to),
RouteSegment::CubicBezier { p2, p3, .. } => norm(p2, p3),
}
}
pub fn midpoint(&self) -> Point {
if self.is_empty() {
return Point::new(0.0, 0.0);
}
let total: f64 = self.0.iter().map(RouteSegment::length).sum();
if total <= 1e-9 {
return self.end();
}
let half = total / 2.0;
let mut acc = 0.0;
for seg in &self.0 {
let l = seg.length();
if acc + l >= half {
let t = if l > 1e-9 { (half - acc) / l } else { 0.0 };
return seg.point_at(t.clamp(0.0, 1.0));
}
acc += l;
}
self.end()
}
pub fn anchors(&self) -> Vec<Point> {
let mut out = Vec::new();
for seg in &self.0 {
out.push(seg.start());
}
if let Some(last) = self.last() {
out.push(last.end());
}
out
}
}
fn norm(a: &Point, b: &Point) -> Point {
let (dx, dy) = (b.x - a.x, b.y - a.y);
let len = (dx * dx + dy * dy).sqrt();
if len < 1e-9 {
Point::new(0.0, 0.0)
} else {
Point::new(dx / len, dy / len)
}
}
pub fn line_route(points: &[Point]) -> RoutePath {
let mut r = RoutePath::new();
for i in 0..points.len().saturating_sub(1) {
r.push(RouteSegment::Line {
from: points[i],
to: points[i + 1],
});
}
r
}
#[derive(Debug, Clone, Default)]
pub struct Geograph {
pub size: Size,
pub background: lievisual::geometry::Color,
pub nodes: Vec<GGNode>,
pub edges: Vec<GGEdge>,
pub containers: Vec<GGContainer>,
pub title: Option<String>,
pub show_data: bool,
pub activations: Vec<GGActivation>,
pub sequence_dividers: Vec<GGSequenceDivider>,
}
#[derive(Debug, Clone)]
pub struct GGActivation {
pub actor: NodeId,
pub x: f64,
pub y0: f64,
pub y1: f64,
}
#[derive(Debug, Clone)]
pub struct GGSequenceDivider {
pub block_id: ContainerId,
pub label: String,
pub y: f64,
}
#[derive(Debug, Clone)]
pub struct GGNode {
pub id: NodeId,
pub role: NodeRole,
pub center: Point,
pub size: Size,
pub shape: ShapeKind,
pub ports: ResolvedPorts,
pub label: Option<super::common::MeasuredLabel>,
pub detail: NodeDetail,
}
#[derive(Debug, Clone)]
pub struct GGEdge {
pub id: EdgeId,
pub source: NodeId,
pub target: NodeId,
pub route: RoutePath,
pub label_text: Option<String>,
pub label_anchor: Option<Point>,
pub kind: EdgeKind,
pub arrow: ArrowSpec,
pub routing_hint: RoutingHint,
pub line_kind: LineKind,
pub cardinality: (Option<ErCardinality>, Option<ErCardinality>),
pub cardinality_text: (Option<String>, Option<String>),
}
#[derive(Debug, Clone)]
pub struct GGContainer {
pub id: ContainerId,
pub bounds: Rect,
pub title: Option<String>,
pub kind: ContainerKind,
pub member_ids: Vec<NodeId>,
}