1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
use crate::{result::RvResult, rverr};
use image::GenericImage;
use serde::{Deserialize, Serialize};
use std::ops::{Add, Div, Mul, Sub};

pub trait Calc:
    Mul<Output = Self> + Div<Output = Self> + Add<Output = Self> + Sub<Output = Self>
where
    Self: Sized,
{
}
impl Calc for i32 {}
impl Calc for u32 {}
impl Calc for f32 {}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Shape {
    pub w: u32,
    pub h: u32,
}
impl Shape {
    pub fn new(w: u32, h: u32) -> Self {
        Self { w, h }
    }
    pub fn from_im<I>(im: &I) -> Self
    where
        I: GenericImage,
    {
        Self {
            w: im.width(),
            h: im.height(),
        }
    }
}

impl From<[usize; 2]> for Shape {
    fn from(value: [usize; 2]) -> Self {
        Self::new(value[0] as u32, value[1] as u32)
    }
}

#[derive(Clone, Copy)]
pub enum OutOfBoundsMode {
    Deny,
    Resize(Shape), // minimal area the box needs to keep
}

pub fn max_squaredist<'a, I1, I2>(points1: I1, points2: I2) -> (PtI, PtI, i64)
where
    I1: Iterator<Item = PtI> + 'a + Clone,
    I2: Iterator<Item = PtI> + 'a + Clone,
{
    points1
        .map(|p1| {
            points2
                .clone()
                .map(|p2| {
                    let d = (p2.x as i64 - p1.x as i64).pow(2) + (p2.y as i64 - p1.y as i64).pow(2);
                    (p1, p2, d)
                })
                .max_by_key(|(_, _, d)| *d)
                .unwrap()
        })
        .max_by_key(|(_, _, d)| *d)
        .unwrap()
}

pub trait IsSignedInt {}

impl IsSignedInt for i32 {}
impl IsSignedInt for i64 {}

#[cfg(test)]
#[macro_export]
macro_rules! point {
    ($x:literal, $y:literal) => {{
        if $x < 0.0 || $y < 0.0 {
            panic!("cannot create point from negative coords, {}, {}", $x, $y);
        }
        crate::domain::PtF { x: $x, y: $y }
    }};
}
#[cfg(test)]
#[macro_export]
macro_rules! point_i {
    ($x:literal, $y:literal) => {{
        if $x < 0 || $y < 0 {
            panic!("cannot create point from negative coords, {}, {}", $x, $y);
        }
        crate::domain::PtI { x: $x, y: $y }
    }};
}

#[macro_export]
macro_rules! impl_point_into {
    ($T:ty) => {
        impl From<PtI> for ($T, $T) {
            fn from(p: PtI) -> Self {
                (p.x as $T, p.y as $T)
            }
        }
        impl From<PtF> for ($T, $T) {
            fn from(p: PtF) -> Self {
                (p.x as $T, p.y as $T)
            }
        }
        impl From<($T, $T)> for PtF {
            fn from((x, y): ($T, $T)) -> Self {
                Self {
                    x: x as f32,
                    y: y as f32,
                }
            }
        }
        impl From<($T, $T)> for PtI {
            fn from((x, y): ($T, $T)) -> Self {
                Self {
                    x: x as u32,
                    y: y as u32,
                }
            }
        }
    };
}

#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct Point<T> {
    pub x: T,
    pub y: T,
}

impl<T> Point<T>
where
    T: Calc + Copy,
{
    pub fn len_square(&self) -> T {
        self.x * self.x + self.y * self.y
    }
    pub fn dist_square(&self, other: &Self) -> T {
        <(T, T) as Into<Point<T>>>::into((self.x - other.x, self.y - other.y)).len_square()
    }
    pub fn dot(&self, rhs: &Self) -> T {
        self.x * rhs.x + self.y * rhs.y
    }
}

impl<T> Mul<T> for Point<T>
where
    T: Calc + Copy,
{
    type Output = Self;
    fn mul(self, rhs: T) -> Self::Output {
        Point {
            x: self.x * rhs,
            y: self.y * rhs,
        }
    }
}
impl<T> Mul for Point<T>
where
    T: Calc + Copy,
{
    type Output = Self;
    fn mul(self, rhs: Self) -> Self::Output {
        Point {
            x: self.x * rhs.x,
            y: self.y * rhs.y,
        }
    }
}
impl<T> Div<T> for Point<T>
where
    T: Calc + Copy,
{
    type Output = Self;
    fn div(self, rhs: T) -> Self::Output {
        Point {
            x: self.x / rhs,
            y: self.y / rhs,
        }
    }
}
impl<T> Div for Point<T>
where
    T: Calc + Copy,
{
    type Output = Self;
    fn div(self, rhs: Self) -> Self::Output {
        Point {
            x: self.x / rhs.x,
            y: self.y / rhs.y,
        }
    }
}

impl<T> Sub for Point<T>
where
    T: Calc + Copy,
{
    type Output = Point<T>;
    fn sub(self, rhs: Self) -> Self::Output {
        Point {
            x: self.x - rhs.x,
            y: self.y - rhs.y,
        }
    }
}
impl<T> Add for Point<T>
where
    T: Calc + Copy,
{
    type Output = Point<T>;
    fn add(self, rhs: Self) -> Self::Output {
        Point {
            x: self.x + rhs.x,
            y: self.y + rhs.y,
        }
    }
}

impl<T> From<(T, T)> for Point<T>
where
    T: Calc,
{
    fn from(value: (T, T)) -> Self {
        Self {
            x: value.0,
            y: value.1,
        }
    }
}
impl<T> From<Point<T>> for (T, T)
where
    T: Calc,
{
    fn from(p: Point<T>) -> (T, T) {
        (p.x, p.y)
    }
}
impl_point_into!(i64);
impl_point_into!(i32);
pub type PtF = Point<f32>;
pub type PtI = Point<u32>;

impl PtI {
    pub fn from_signed(p: (i32, i32)) -> RvResult<Self> {
        if p.0 < 0 || p.1 < 0 {
            Err(rverr!(
                "cannot create point with negative coordinates, {:?}",
                p
            ))
        } else {
            Ok(Self {
                x: p.0 as u32,
                y: p.1 as u32,
            })
        }
    }
    pub fn equals<U>(&self, other: (U, U)) -> bool
    where
        U: PartialEq,
        PtI: Into<(U, U)>,
    {
        <Self as Into<(U, U)>>::into(*self) == other
    }
}

impl From<PtI> for PtF {
    fn from(p: PtI) -> Self {
        ((p.x as f32), (p.y as f32)).into()
    }
}
impl From<PtF> for PtI {
    fn from(p: PtF) -> Self {
        ((p.x as u32), (p.y as u32)).into()
    }
}
impl From<(u32, u32)> for PtF {
    fn from(x: (u32, u32)) -> Self {
        ((x.0 as f32), (x.1 as f32)).into()
    }
}
impl From<(f32, f32)> for PtI {
    fn from(x: (f32, f32)) -> Self {
        ((x.0 as u32), (x.1 as u32)).into()
    }
}
impl From<(usize, usize)> for PtI {
    fn from(x: (usize, usize)) -> Self {
        ((x.0 as u32), (x.1 as u32)).into()
    }
}

impl From<PtI> for (usize, usize) {
    fn from(p: PtI) -> Self {
        (p.x as usize, p.y as usize)
    }
}