1use crate::ffi;
14
15#[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#[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#[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#[derive(Debug, Clone, Default, PartialEq)]
124pub struct Mesh {
125 pub positions: Vec<Point>,
127 pub indices: Vec<u32>,
129}
130
131impl Mesh {
132 pub fn is_empty(&self) -> bool {
133 self.indices.is_empty()
134 }
135
136 pub fn triangle_count(&self) -> usize {
138 self.indices.len() / 3
139 }
140
141 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}