Skip to main content

ink_rs/
geometry.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Plain geometric values.
6//!
7//! These are ordinary Rust types rather than the generated ones. The generated
8//! structs are `!Send`/`!Sync` because Crubit cannot know what the C++ side
9//! does with them, which would spread to everything holding a point. Converting
10//! at the boundary costs a couple of float copies and keeps the public API
11//! movable between threads.
12
13use crate::ffi;
14
15/// A position in stroke units.
16#[derive(Debug, Clone, Copy, PartialEq, Default)]
17pub struct Point {
18    pub x: f32,
19    pub y: f32,
20}
21
22impl Point {
23    pub const ORIGIN: Point = Point { x: 0.0, y: 0.0 };
24
25    pub const fn new(x: f32, y: f32) -> Self {
26        Self { x, y }
27    }
28
29    pub(crate) fn from_ffi(point: ffi::Point) -> Self {
30        Self {
31            x: point.x,
32            y: point.y,
33        }
34    }
35
36    pub(crate) fn to_ffi(self) -> ffi::Point {
37        ffi::Point {
38            x: self.x,
39            y: self.y,
40        }
41    }
42}
43
44impl From<(f32, f32)> for Point {
45    fn from((x, y): (f32, f32)) -> Self {
46        Self::new(x, y)
47    }
48}
49
50/// A displacement in stroke units.
51#[derive(Debug, Clone, Copy, PartialEq, Default)]
52pub struct Vec2 {
53    pub x: f32,
54    pub y: f32,
55}
56
57impl Vec2 {
58    pub const fn new(x: f32, y: f32) -> Self {
59        Self { x, y }
60    }
61
62    pub fn magnitude(self) -> f32 {
63        self.x.hypot(self.y)
64    }
65}
66
67impl From<(f32, f32)> for Vec2 {
68    fn from((x, y): (f32, f32)) -> Self {
69        Self::new(x, y)
70    }
71}
72
73/// An axis-aligned bounding box.
74#[derive(Debug, Clone, Copy, PartialEq)]
75pub struct Rect {
76    pub x_min: f32,
77    pub y_min: f32,
78    pub x_max: f32,
79    pub y_max: f32,
80}
81
82impl Rect {
83    pub const fn new(x_min: f32, y_min: f32, x_max: f32, y_max: f32) -> Self {
84        Self {
85            x_min,
86            y_min,
87            x_max,
88            y_max,
89        }
90    }
91
92    pub fn width(&self) -> f32 {
93        self.x_max - self.x_min
94    }
95
96    pub fn height(&self) -> f32 {
97        self.y_max - self.y_min
98    }
99
100    pub fn center(&self) -> Point {
101        Point::new(
102            (self.x_min + self.x_max) * 0.5,
103            (self.y_min + self.y_max) * 0.5,
104        )
105    }
106
107    pub(crate) fn from_ffi(rect: ffi::Rect) -> Self {
108        Self {
109            x_min: rect.x_min,
110            y_min: rect.y_min,
111            x_max: rect.x_max,
112            y_max: rect.y_max,
113        }
114    }
115}
116
117/// A snapshot of triangle geometry, ready to hand to a renderer.
118///
119/// Positions and indices are copied out of Ink rather than borrowed, because
120/// Ink rebuilds its meshes in place as a stroke grows and any borrow would be
121/// invalidated by the next call to
122/// [`InProgressStroke::update_shape`](crate::InProgressStroke::update_shape).
123#[derive(Debug, Clone, Default, PartialEq)]
124pub struct Mesh {
125    /// Vertex positions, one per vertex.
126    pub positions: Vec<Point>,
127    /// Triangle vertex indices, three per triangle, indexing into `positions`.
128    pub indices: Vec<u32>,
129}
130
131impl Mesh {
132    pub fn is_empty(&self) -> bool {
133        self.indices.is_empty()
134    }
135
136    /// Number of triangles, which is always `indices.len() / 3`.
137    pub fn triangle_count(&self) -> usize {
138        self.indices.len() / 3
139    }
140
141    /// The three positions making up triangle `index`, if it exists.
142    pub fn triangle(&self, index: usize) -> Option<[Point; 3]> {
143        let base = index.checked_mul(3)?;
144        let slice = self.indices.get(base..base + 3)?;
145        Some([
146            *self.positions.get(slice[0] as usize)?,
147            *self.positions.get(slice[1] as usize)?,
148            *self.positions.get(slice[2] as usize)?,
149        ])
150    }
151}