1use num_traits::ToPrimitive;
2use winit::dpi::PhysicalSize;
3
4use super::{Vector2, Vector2I};
5
6#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
7pub struct Point2 {
8 pub x: i32,
9 pub y: i32,
10}
11
12impl Point2 {
13 pub fn new<T: ToPrimitive>(x: T, y: T) -> Self {
14 Self {
15 x: x.to_i32().unwrap_or(0),
16 y: y.to_i32().unwrap_or(0),
17 }
18 }
19
20 pub const ZERO: Self = Self { x: 0, y: 0 };
21 pub const ONE: Self = Self { x: 1, y: 1 };
22}
23
24impl Default for Point2 {
25 fn default() -> Self {
26 Self::ZERO
27 }
28}
29
30impl Into<wgpu::Extent3d> for Point2 {
31 fn into(self) -> wgpu::Extent3d {
32 wgpu::Extent3d {
33 width: self.x as u32,
34 height: self.y as u32,
35 depth_or_array_layers: 1,
36 }
37 }
38}
39
40impl Into<PhysicalSize<u32>> for Point2 {
41 fn into(self) -> PhysicalSize<u32> {
42 PhysicalSize {
43 width: self.x as u32,
44 height: self.y as u32,
45 }
46 }
47}
48
49impl From<PhysicalSize<u32>> for Point2 {
50 fn from(size: PhysicalSize<u32>) -> Self {
51 Self {
52 x: size.width as i32,
53 y: size.height as i32,
54 }
55 }
56}
57
58impl Into<PhysicalSize<i32>> for Point2 {
59 fn into(self) -> PhysicalSize<i32> {
60 PhysicalSize {
61 width: self.x,
62 height: self.y,
63 }
64 }
65}
66
67impl From<PhysicalSize<i32>> for Point2 {
68 fn from(size: PhysicalSize<i32>) -> Self {
69 Self {
70 x: size.width,
71 y: size.height,
72 }
73 }
74}
75
76impl From<(i32, i32)> for Point2 {
77 fn from(tuple: (i32, i32)) -> Self {
78 Self {
79 x: tuple.0,
80 y: tuple.1,
81 }
82 }
83}
84
85impl From<(u32, u32)> for Point2 {
86 fn from(tuple: (u32, u32)) -> Self {
87 Self {
88 x: tuple.0 as i32,
89 y: tuple.1 as i32,
90 }
91 }
92}
93
94impl From<Vector2> for Point2 {
95 fn from(vector: Vector2) -> Self {
96 Self {
97 x: vector.x.floor() as i32,
98 y: vector.y.floor() as i32,
99 }
100 }
101}
102
103impl From<Vector2I> for Point2 {
104 fn from(vector: Vector2I) -> Self {
105 Self {
106 x: vector.x,
107 y: vector.y,
108 }
109 }
110}
111
112#[derive(Clone, Copy, Debug, PartialEq, Eq)]
113pub struct Point3 {
114 pub x: i32,
115 pub y: i32,
116 pub z: i32,
117}
118
119impl Point3 {
120 pub fn new<T: ToPrimitive>(x: T, y: T, z: T) -> Self {
121 Self {
122 x: x.to_i32().unwrap_or(0),
123 y: y.to_i32().unwrap_or(0),
124 z: z.to_i32().unwrap_or(0),
125 }
126 }
127
128 pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
129 pub const ONE: Self = Self { x: 1, y: 1, z: 1 };
130}
131
132impl Default for Point3 {
133 fn default() -> Self {
134 Self::ZERO
135 }
136}
137
138impl From<(i32, i32, i32)> for Point3 {
139 fn from(tuple: (i32, i32, i32)) -> Self {
140 Self {
141 x: tuple.0,
142 y: tuple.1,
143 z: tuple.2,
144 }
145 }
146}
147
148impl From<(u32, u32, u32)> for Point3 {
149 fn from(tuple: (u32, u32, u32)) -> Self {
150 Self {
151 x: tuple.0 as i32,
152 y: tuple.1 as i32,
153 z: tuple.2 as i32,
154 }
155 }
156}
157
158impl From<Vector2> for Point3 {
159 fn from(vector: Vector2) -> Self {
160 Self {
161 x: vector.x.floor() as i32,
162 y: vector.y.floor() as i32,
163 z: 0, }
165 }
166}
167
168impl From<Vector2I> for Point3 {
169 fn from(vector: Vector2I) -> Self {
170 Self {
171 x: vector.x,
172 y: vector.y,
173 z: 0, }
175 }
176}
177
178impl Into<wgpu::Extent3d> for Point3 {
179 fn into(self) -> wgpu::Extent3d {
180 wgpu::Extent3d {
181 width: self.x as u32,
182 height: self.y as u32,
183 depth_or_array_layers: self.z as u32,
184 }
185 }
186}
187
188impl From<Point2> for Point3 {
189 fn from(point: Point2) -> Self {
190 Self {
191 x: point.x,
192 y: point.y,
193 z: 0, }
195 }
196}
197
198impl From<Point3> for Point2 {
199 fn from(point: Point3) -> Self {
200 Self {
201 x: point.x,
202 y: point.y,
203 }
204 }
205}
206
207impl From<Point2> for Vector2 {
208 fn from(point: Point2) -> Self {
209 Self {
210 x: point.x as f32,
211 y: point.y as f32,
212 }
213 }
214}
215
216impl From<Point3> for Vector2 {
217 fn from(point: Point3) -> Self {
218 Self {
219 x: point.x as f32,
220 y: point.y as f32,
221 }
222 }
223}
224
225impl From<Point2> for Vector2I {
226 fn from(point: Point2) -> Self {
227 Self {
228 x: point.x,
229 y: point.y,
230 }
231 }
232}
233
234impl From<Point3> for Vector2I {
235 fn from(point: Point3) -> Self {
236 Self {
237 x: point.x,
238 y: point.y,
239 }
240 }
241}