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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use crate::Dimension;

#[cfg(feature = "serde_support")]
use serde::{Deserialize, Serialize};

/// 3D position
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Position {
    pub x: i32,
    pub y: i32,
    pub z: i32,
}

/// 2D position
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
pub struct Position2D {
    pub x: i32,
    pub y: i32,
}

///
/// Two dimensional boundary consisting of `Position2D` and `Dimension`
/// Bounds can be either free standing or binding
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Bounds {
    /// Binding bounds constrain movement to their area
    Binding(Position2D, Dimension),
    /// Free standing bounds specify an area but do not constrain
    Free(Position2D, Dimension),
}

impl std::iter::IntoIterator for Bounds {
    type Item = Position2D;
    type IntoIter = BoundsIntoIterator;

    fn into_iter(self) -> Self::IntoIter {
        BoundsIntoIterator {
            bounds: self,
            index: 0,
        }
    }
}

///
/// Bounds iterator that returns Position2D elements contained in given area
///
pub struct BoundsIntoIterator {
    bounds: Bounds,
    index: usize,
}

impl std::iter::Iterator for BoundsIntoIterator {
    type Item = Position2D;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index >= self.bounds.size() {
            return None;
        }

        let old_index = self.index;
        self.index += 1;

        if let Some(pos) = coords_from_index(old_index, *self.bounds.dimension()) {
            Some(pos + *self.bounds.position())
        } else {
            None
        }
    }
}

impl std::ops::Sub<Position2D> for Bounds {
    type Output = Bounds;

    fn sub(self, other: Position2D) -> Self::Output {
        match self {
            Bounds::Binding(pos, dim) => Bounds::Binding(pos - other, dim),
            Bounds::Free(pos, dim) => Bounds::Free(pos - other, dim),
        }
    }
}

impl Bounds {
    /// Empty bounds constructor, sets `Position` to 0, 0
    pub fn empty() -> Self {
        Bounds::Free(Position2D { x: 0, y: 0 }, Dimension::default())
    }

    /// Single point bounds constructor with unit size for given position
    pub fn point(pos: Position2D) -> Self {
        Bounds::Binding(pos, Dimension::unit())
    }

    /// Position component accessor
    pub fn position(&self) -> &Position2D {
        match self {
            Bounds::Binding(p, _) => p,
            Bounds::Free(p, _) => p,
        }
    }

    /// Dimension component accessor
    pub fn dimension(&self) -> &Dimension {
        match self {
            Bounds::Binding(_, d) => d,
            Bounds::Free(_, d) => d,
        }
    }

    /// Area size calculation as dimension.w * dimension.h
    pub fn size(&self) -> usize {
        self.dimension().size()
    }

    /// Right side point for given bounds area
    pub fn right(&self) -> i32 {
        self.position().x + i32::from(self.dimension().w) - 1
    }

    /// Bottom side point for given bounds area
    pub fn bottom(&self) -> i32 {
        self.position().y + i32::from(self.dimension().h) - 1
    }

    /// Checks if given coordinates are inside this bounded area
    pub fn contains(&self, other: Position2D) -> bool {
        let pos = self.position();
        let dim = self.dimension();

        other.x >= pos.x
            && other.x < pos.x + i32::from(dim.w)
            && other.y >= pos.y
            && other.y < pos.y + i32::from(dim.h)
    }

    /// Calculates rectangular intersection
    pub fn intersects(&self, pos: Position2D, dim: Dimension) -> bool {
        let top_edge1 = self.position().y + i32::from(self.dimension().h);
        let right_edge1 = self.position().x + i32::from(self.dimension().w);
        let left_edge1 = self.position().x;
        let bottom_edge1 = self.position().y;
        let top_edge2 = pos.y + i32::from(dim.h);
        let right_edge2 = pos.x + i32::from(dim.w);
        let left_edge2 = pos.x;
        let bottom_edge2 = pos.y;

        left_edge1 < right_edge2
            && right_edge1 > left_edge2
            && bottom_edge1 < top_edge2
            && top_edge1 > bottom_edge2
    }
}

impl std::fmt::Display for Position {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.z != 0 {
            write!(f, "{},{},{}", self.x, self.y, self.z)
        } else {
            write!(f, "{},{}", self.x, self.y)
        }
    }
}

impl std::fmt::Display for Position2D {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{},{}", self.x, self.y)
    }
}

impl From<&mut Position> for Position2D {
    fn from(pos: &mut Position) -> Position2D {
        Position2D { x: pos.x, y: pos.y }
    }
}

impl From<Position> for Position2D {
    fn from(pos: Position) -> Position2D {
        Position2D { x: pos.x, y: pos.y }
    }
}

impl From<&Position> for Position2D {
    fn from(pos: &Position) -> Position2D {
        Position2D { x: pos.x, y: pos.y }
    }
}

impl std::ops::Add<Position2D> for Position2D {
    type Output = Position2D;

    fn add(self, other: Position2D) -> Self::Output {
        Position2D {
            x: self.x + other.x,
            y: self.y + other.y,
        }
    }
}

impl std::ops::Add<Position2D> for Position {
    type Output = Position;

    fn add(self, other: Position2D) -> Self::Output {
        Position {
            x: self.x + other.x,
            y: self.y + other.y,
            z: self.z,
        }
    }
}

impl std::ops::Add<i32> for Position {
    type Output = Position;

    fn add(self, value: i32) -> Self::Output {
        Position {
            x: self.x + value,
            y: self.y + value,
            z: self.z, // keep z
        }
    }
}

impl std::ops::AddAssign<Position2D> for Position {
    fn add_assign(&mut self, other: Position2D) {
        self.x += other.x;
        self.y += other.y;
    }
}

impl std::ops::AddAssign<i32> for Position {
    fn add_assign(&mut self, value: i32) {
        self.x += value;
        self.y += value;
    }
}

impl std::ops::AddAssign<Position2D> for Position2D {
    fn add_assign(&mut self, other: Position2D) {
        self.x += other.x;
        self.y += other.y;
    }
}

impl std::ops::Sub for Position {
    type Output = Position;

    fn sub(self, other: Self) -> Self {
        Position {
            x: self.x - other.x,
            y: self.y - other.y,
            z: self.z - other.z,
        }
    }
}

impl std::ops::Sub for Position2D {
    type Output = Position2D;

    fn sub(self, other: Self) -> Self {
        Position2D {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

impl std::ops::Sub<Position> for Position2D {
    type Output = Position2D;

    fn sub(self, other: Position) -> Self {
        Position2D {
            x: self.x - other.x,
            y: self.y - other.y,
        }
    }
}

impl std::ops::SubAssign<Position2D> for Position2D {
    fn sub_assign(&mut self, other: Position2D) {
        self.x -= other.x;
        self.y -= other.y;
    }
}

impl Position {
    ///
    /// Applies given `Translation` to this `Position` with regards to the provided
    /// `Bounds` area. If `Bounds` is binding ensures position does not reach outside.
    ///
    pub fn apply(&mut self, translation: Translation, bounds: Bounds) -> bool {
        match translation {
            Translation::None => {}
            Translation::Relative(x, y, z) => {
                self.x += x;
                self.y += y;
                self.z += z;
            }
            Translation::Absolute(x, y, z) => {
                self.x = x;
                self.y = y;
                if let Some(z) = z {
                    self.z = z;
                }
            }
            Translation::ToEdge(dir) => match dir {
                Direction::Left => self.x = bounds.position().x,
                Direction::Top => self.y = bounds.position().y,
                Direction::Bottom => self.y = bounds.bottom(),
                Direction::Right => self.x = bounds.right(),
            },
        }

        match bounds {
            Bounds::Binding(p, _) => {
                if self.x < p.x {
                    self.x = p.x;
                    false
                } else if self.y < p.y {
                    self.y = p.y;
                    false
                } else if self.x > bounds.right() {
                    self.x = bounds.right();
                    false
                } else if self.y > bounds.bottom() {
                    self.y = bounds.bottom();
                    false
                } else {
                    true
                }
            }
            _ => true,
        }
    }
}

impl Position2D {
    pub fn from_xy(x: i32, y: i32) -> Self {
        Self { x, y }
    }

    ///
    /// Applies given `Translation` to this `Position2D` with regards to the provided
    /// `Bounds` area. If `Bounds` is binding ensures position does not reach outside.
    ///
    pub fn apply(&mut self, translation: Translation, bounds: Bounds) -> bool {
        let mut pos3d = Position {
            x: self.x,
            y: self.y,
            z: 0,
        };

        if pos3d.apply(translation, bounds) {
            self.x = pos3d.x;
            self.y = pos3d.y;
            true
        } else {
            false
        }
    }

    /// Create bounds from two points
    pub fn area(self, other: Position2D) -> Bounds {
        let top_left = Position2D {
            x: std::cmp::min(self.x, other.x),
            y: std::cmp::min(self.y, other.y),
        };
        let bottom_right = Position2D {
            x: std::cmp::max(self.x, other.x),
            y: std::cmp::max(self.y, other.y),
        };

        let dim = Dimension::for_area(top_left, bottom_right);

        Bounds::Binding(top_left, dim)
    }

    /// Create the list of all positions in given area from point with given dimension
    pub fn area_texels(self, dim: Dimension) -> Vec<Position2D> {
        let mut result = Vec::with_capacity(dim.size());

        for x in self.x..self.x + i32::from(dim.w) {
            for y in self.y..self.y + i32::from(dim.h) {
                result.push(Position2D { x, y });
            }
        }

        result
    }
}

///
/// Describes a direction
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
    Left,
    Top,
    Bottom,
    Right,
}

///
/// Describes the translation operation
///
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Translation {
    /// None for avoiding the need for Option<>
    None,
    /// Relative to current `Position`
    Relative(i32, i32, i32),
    /// Absolute with optional Z coordinate
    Absolute(i32, i32, Option<i32>),
    /// To edge of constrained area in given direction
    ToEdge(Direction),
}

impl Default for Translation {
    fn default() -> Self {
        Translation::None
    }
}

fn coords_from_index(index: usize, dim: Dimension) -> Option<Position2D> {
    let i = index as i32;
    let w = i32::from(dim.w);
    let h = i32::from(dim.h);

    if i < w * h {
        Some(Position2D { x: i % w, y: i / w })
    } else {
        None
    }
}