mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 16:59:43 +01:00
The selection outline and the hover highlight drew a symbol instance as the box square to the page around it, so a turned, skewed or perspective instance showed a box larger than its frame. Type in perspective had the same box. Node::drawn_corners gives the four corners of an instance's art box placed by its transform, and of type's flat bounds, both through the projection of an object in perspective. node_outline joins them, as it already does for a placed document's corners.
100 lines
4.6 KiB
Rust
100 lines
4.6 KiB
Rust
//! Perspective Grid attachments (Object › Perspective).
|
||
//!
|
||
//! The grid itself is document data (`Document.unknown["perspectiveGrid"]`, modelled by the tools
|
||
//! crate). Which plane an object lies on is kept on the object, so copies, duplicates and pastes
|
||
//! stay attached and deleted objects leave nothing stale behind.
|
||
|
||
use serde::{Deserialize, Serialize};
|
||
use vectorcraft_geom::{Homography, Point, Rect};
|
||
|
||
use crate::symbols::SYMBOL_SQUARE;
|
||
use crate::{Node, NodeKind, SymbolBoxes};
|
||
|
||
/// The plane an object in perspective lies on.
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
|
||
#[serde(rename_all = "camelCase")]
|
||
pub struct PerspectiveAttachment {
|
||
/// `left`, `right` or `ground`.
|
||
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||
pub plane: String,
|
||
/// Where along the plane's normal (points) the parallel plane the object lies on is: 0 is the
|
||
/// grid plane in its original place.
|
||
#[serde(default, skip_serializing_if = "crate::skip::is_default")]
|
||
pub depth: f64,
|
||
/// Type and symbol instances: the projective map (row-major 3 × 3) that draws their flat art
|
||
/// in perspective. They stay type and symbol instances; the renderer and exporters project
|
||
/// their outlines.
|
||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||
pub projection: Option<[f64; 9]>,
|
||
/// Object › Perspective › Edit Text in progress: the type is drawn flat to be edited (not
|
||
/// saved).
|
||
#[serde(skip)]
|
||
pub editing: bool,
|
||
}
|
||
|
||
impl PerspectiveAttachment {
|
||
pub fn new(plane: &str, depth: f64) -> Self {
|
||
Self { plane: plane.to_string(), depth: if depth.is_finite() { depth } else { 0.0 }, ..Self::default() }
|
||
}
|
||
|
||
/// The projection, when it is a valid one.
|
||
pub fn homography(&self) -> Option<Homography> {
|
||
self.projection.and_then(Homography::from_array)
|
||
}
|
||
}
|
||
|
||
impl Node {
|
||
/// The projective map type or a symbol instance in perspective is drawn through (not while
|
||
/// Edit Text shows it flat).
|
||
pub fn projection(&self) -> Option<Homography> {
|
||
let a = self.extras().perspective.as_deref().filter(|a| !a.editing)?;
|
||
matches!(self.kind, NodeKind::Text(_) | NodeKind::SymbolInstance { .. }).then(|| a.homography()).flatten()
|
||
}
|
||
|
||
/// `b` (flat bounds) as drawn: the box of its projection for type and symbols in perspective.
|
||
pub(crate) fn projected(&self, b: Option<Rect>) -> Option<Rect> {
|
||
match (self.projection(), b) {
|
||
(Some(h), Some(b)) => h.map_rect_bbox(b).or(Some(b)),
|
||
(_, b) => b,
|
||
}
|
||
}
|
||
|
||
/// The corners of the box a symbol instance or type is drawn in, on the page (top left, top
|
||
/// right, bottom right, bottom left): an instance's art box (measured from `symbols`, or
|
||
/// `SYMBOL_SQUARE` when it has none) placed by its transform, and type's flat bounds, both
|
||
/// through the projection of type or an instance in perspective. Joined by straight lines,
|
||
/// they outline the box as drawn. When the projection sends a corner to infinity the corners
|
||
/// stay flat, as the flat box is kept for [`Node::geometric_bounds_with`]. `None` for other
|
||
/// objects and for type without bounds.
|
||
pub fn drawn_corners(&self, symbols: &SymbolBoxes) -> Option<[Point; 4]> {
|
||
let flat = match &self.kind {
|
||
NodeKind::SymbolInstance { symbol, xf } => {
|
||
let art = symbols.art(symbol).map_or(SYMBOL_SQUARE, |a| a.geometric);
|
||
[(art.x0, art.y0), (art.x1, art.y0), (art.x1, art.y1), (art.x0, art.y1)].map(|(x, y)| *xf * Point::new(x, y))
|
||
}
|
||
NodeKind::Text(t) => {
|
||
let b = t.bounds()?;
|
||
[(b.x0, b.y0), (b.x1, b.y0), (b.x1, b.y1), (b.x0, b.y1)].map(|(x, y)| Point::new(x, y))
|
||
}
|
||
_ => return None,
|
||
};
|
||
let Some(h) = self.projection() else { return Some(flat) };
|
||
let [a, b, c, d] = flat.map(|p| h.apply(p));
|
||
Some(match (a, b, c, d) {
|
||
(Some(a), Some(b), Some(c), Some(d)) => [a, b, c, d],
|
||
_ => flat,
|
||
})
|
||
}
|
||
|
||
/// Keep a projection drawing the same picture moved by `a` (the flat art moves by `a` too). An
|
||
/// invertible `a` that the projection cannot follow removes it, and the art is drawn flat.
|
||
pub(crate) fn transform_projection(&mut self, a: vectorcraft_geom::Affine) {
|
||
if let Some(p) = self.extras_if_any().and_then(|e| e.perspective.as_deref_mut())
|
||
&& let Some(h) = p.homography()
|
||
&& Homography::affine_is_invertible(a)
|
||
{
|
||
p.projection = h.conjugated(a).map(|h| h.to_array());
|
||
}
|
||
}
|
||
}
|