devotee 0.2.0-beta.32

Visualization engine
Documentation
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
436
437
438
439
440
441
442
443
444
445
446
447
use crate::util::vector::Vector;

use super::{Image, ImageMut};

#[derive(Clone, Copy, Debug)]
struct Zone {
    origin: Vector<i32>,
    start: Vector<i32>,
    dimensions: Vector<i32>,
    flip: Flip,
    rotation: Rotation,
    scale: i32,
}

impl Zone {
    fn limited_to(self, target_dimensions: Vector<i32>) -> Self {
        let dimensions = ((self.origin + self.dimensions).individual_min(target_dimensions)
            - self.origin)
            .individual_max(Vector::<i32>::zero());
        let start = -self.origin.individual_min(Vector::<i32>::zero());
        Self {
            dimensions,
            start,
            ..self
        }
    }

    fn deform_position(&self, position: Vector<i32>) -> Vector<i32> {
        self.flip.apply(
            self.rotation.apply(position / self.scale, self.dimensions),
            self.dimensions,
        ) + self.origin
    }

    fn position_if_in_bounds(&self, position: Vector<i32>) -> Option<Vector<i32>> {
        let position = self.deform_position(position);
        if position.x() < self.origin.x() || position.y() < self.origin.y() {
            return None;
        }
        if position.x() >= self.origin.x() + self.dimensions.x()
            || position.y() >= self.origin.y() + self.dimensions.y()
        {
            return None;
        }
        Some(position)
    }

    fn calculate_external_start(&self) -> Vector<i32> {
        match self.rotation {
            Rotation::None | Rotation::Half => self.start,
            Rotation::CW | Rotation::CCW => self.start.swapped(),
        }
    }
}

/// A view into an `Image`.
#[derive(Clone, Copy)]
pub struct View<'target, P> {
    target: &'target dyn Image<P>,
    zone: Zone,
}

impl<P> View<'_, P> {
    /// Get start hint.
    /// It represents the offset from the top-left corner to the actual data start position.
    pub fn get_start(&self) -> Vector<i32> {
        self.zone.calculate_external_start()
    }

    /// Get current flip value.
    pub fn get_flip(&self) -> Flip {
        self.zone.flip
    }

    /// Get mutable reference to the current flip value.
    pub fn flip_mut(&mut self) -> &mut Flip {
        &mut self.zone.flip
    }

    /// Consume this `View` and get another one with the flip value provided.
    pub fn with_flip(self, flip: Flip) -> Self {
        let zone = Zone { flip, ..self.zone };
        Self { zone, ..self }
    }

    /// Get current rotation value.
    pub fn get_rotation(&self) -> Rotation {
        self.zone.rotation
    }

    /// Get mutable reference to the current rotation value.
    pub fn rotation_mut(&mut self) -> &mut Rotation {
        &mut self.zone.rotation
    }

    /// Consume this `View` and get another one with the rotation value provided.
    pub fn with_rotation(self, rotation: Rotation) -> Self {
        let zone = Zone {
            rotation,
            ..self.zone
        };
        Self { zone, ..self }
    }

    /// Get current scale value.
    pub fn get_scale(&self) -> i32 {
        self.zone.scale
    }

    /// Consume this `View` and create another one with the scale value provided.
    ///
    /// # Panics
    /// Panics if `scale` is less or equal to 0.
    pub fn with_scale(self, scale: i32) -> Self {
        assert_ne!(scale, 0, "Scale can't be zero");
        assert!(scale > 0, "Scale can't be negative");
        let zone = Zone { scale, ..self.zone };
        Self { zone, ..self }
    }
}

impl<'target, P> View<'target, P> {
    /// Create new immutable `View` into provided `target`.
    pub fn new(
        target: &'target dyn Image<P>,
        origin: Vector<i32>,
        dimensions: Vector<i32>,
    ) -> Self {
        let target_dimensions = Vector::new(target.width(), target.height());
        let start = Vector::<i32>::zero();
        let flip = Flip::None;
        let rotation = Rotation::None;
        let scale = 1;
        let zone = Zone {
            origin,
            start,
            dimensions,
            flip,
            rotation,
            scale,
        }
        .limited_to(target_dimensions);

        Self { target, zone }
    }
}

/// A mutable view into an `Image`.
pub struct ViewMut<'target, P> {
    target: &'target mut dyn ImageMut<P>,
    zone: Zone,
}

impl<'image, P> ViewMut<'image, P> {
    /// Create new mutable `View` into provided `target`.
    pub fn new_mut(
        target: &'image mut dyn ImageMut<P>,
        origin: Vector<i32>,
        dimensions: Vector<i32>,
    ) -> Self {
        let target_dimensions = Vector::new(target.width(), target.height());
        let start = Vector::<i32>::zero();
        let flip = Flip::None;
        let rotation = Rotation::None;
        let scale = 1;
        let zone = Zone {
            origin,
            start,
            dimensions,
            flip,
            rotation,
            scale,
        }
        .limited_to(target_dimensions);

        Self { target, zone }
    }
}

impl<P> ViewMut<'_, P> {
    /// Get start hint.
    /// It represents the offset from the top-left corner to the actual data start position.
    pub fn get_start(&self) -> Vector<i32> {
        self.zone.calculate_external_start()
    }

    /// Get current flip value.
    pub fn get_flip(&self) -> Flip {
        self.zone.flip
    }

    /// Get mutable reference to the current flip value.
    pub fn flip_mut(&mut self) -> &mut Flip {
        &mut self.zone.flip
    }

    /// Consume this `View` and get another one with the flip value provided.
    pub fn with_flip(self, flip: Flip) -> Self {
        let zone = Zone { flip, ..self.zone };
        Self { zone, ..self }
    }

    /// Get current rotation value.
    pub fn get_rotation(&self) -> Rotation {
        self.zone.rotation
    }

    /// Get mutable reference to the current rotation value.
    pub fn rotation_mut(&mut self) -> &mut Rotation {
        &mut self.zone.rotation
    }

    /// Consume this `View` and get another one with the rotation value provided.
    pub fn with_rotation(self, rotation: Rotation) -> Self {
        let zone = Zone {
            rotation,
            ..self.zone
        };
        Self { zone, ..self }
    }

    /// Get current scale value.
    pub fn get_scale(&self) -> i32 {
        self.zone.scale
    }

    /// Consume this `View` and create another one with the scale value provided.
    ///
    /// # Panics
    /// Panics if `scale` is less or equal to 0.
    pub fn with_scale(self, scale: i32) -> Self {
        assert_ne!(scale, 0, "Scale can't be zero");
        assert!(scale > 0, "Scale can't be negative");
        let zone = Zone { scale, ..self.zone };
        Self { zone, ..self }
    }
}

impl<P> Image<P> for View<'_, P> {
    fn pixel(&self, position: Vector<i32>) -> Option<P> {
        self.target
            .pixel(self.zone.position_if_in_bounds(position)?)
    }

    unsafe fn pixel_unchecked(&self, position: Vector<i32>) -> P {
        unsafe {
            self.target
                .pixel_unchecked(self.zone.deform_position(position))
        }
    }

    fn width(&self) -> i32 {
        let dimension = match self.zone.rotation {
            Rotation::None | Rotation::Half => self.zone.dimensions.x(),
            Rotation::CCW | Rotation::CW => self.zone.dimensions.y(),
        };
        dimension * self.zone.scale
    }

    fn height(&self) -> i32 {
        let dimension = match self.zone.rotation {
            Rotation::None | Rotation::Half => self.zone.dimensions.y(),
            Rotation::CCW | Rotation::CW => self.zone.dimensions.x(),
        };
        dimension * self.zone.scale
    }
}

impl<P> Image<P> for ViewMut<'_, P> {
    fn pixel(&self, position: Vector<i32>) -> Option<P> {
        self.target
            .pixel(self.zone.position_if_in_bounds(position)?)
    }

    unsafe fn pixel_unchecked(&self, position: Vector<i32>) -> P {
        unsafe {
            self.target
                .pixel_unchecked(self.zone.deform_position(position))
        }
    }

    fn width(&self) -> i32 {
        let dimension = match self.zone.rotation {
            Rotation::None | Rotation::Half => self.zone.dimensions.x(),
            Rotation::CCW | Rotation::CW => self.zone.dimensions.y(),
        };
        dimension * self.zone.scale
    }

    fn height(&self) -> i32 {
        let dimension = match self.zone.rotation {
            Rotation::None | Rotation::Half => self.zone.dimensions.y(),
            Rotation::CCW | Rotation::CW => self.zone.dimensions.x(),
        };
        dimension * self.zone.scale
    }
}

impl<P> ImageMut<P> for ViewMut<'_, P> {
    fn set_pixel(&mut self, position: Vector<i32>, value: &P) {
        if let Some(position) = self.zone.position_if_in_bounds(position) {
            self.target.set_pixel(position, value);
        }
    }

    fn modify_pixel(
        &mut self,
        position: Vector<i32>,
        function: &mut dyn FnMut((i32, i32), P) -> P,
    ) {
        if let Some(position) = self.zone.position_if_in_bounds(position) {
            self.target.modify_pixel(position, function);
        }
    }

    unsafe fn set_pixel_unchecked(&mut self, position: Vector<i32>, value: &P) {
        unsafe {
            self.target.set_pixel_unchecked(
                self.zone.origin + self.zone.deform_position(position),
                value,
            );
        }
    }

    fn clear(&mut self, color: P) {
        // We do believe that we are in a proper range.
        // By this time we should have already recalculated origin and dimensions to be in bounds.
        unsafe {
            for y in 0..self.zone.dimensions.y() {
                for x in 0..self.zone.dimensions.x() {
                    self.target
                        .set_pixel_unchecked(self.zone.origin + (x, y), &color);
                }
            }
        }
    }
}

/// Flip transform applied to a view.
#[derive(Clone, Copy, Debug)]
pub enum Flip {
    /// No flip occurs.
    None,
    /// There is a horizontal flip.
    Horizontal,
    /// There is a vertical flip.
    Vertical,
    /// Both flips occur, effectively rotating by 180 degrees.
    Both,
}

impl From<u8> for Flip {
    fn from(value: u8) -> Self {
        match value % 4 {
            0 => Flip::None,
            1 => Flip::Horizontal,
            2 => Flip::Vertical,
            3 => Flip::Both,
            _ => unreachable!(),
        }
    }
}

impl Flip {
    fn apply(&self, position: Vector<i32>, dimensions: Vector<i32>) -> Vector<i32> {
        match self {
            Flip::None => position,
            Flip::Horizontal => (dimensions.x() - position.x() - 1, position.y()).into(),
            Flip::Vertical => (position.x(), dimensions.y() - position.y() - 1).into(),
            Flip::Both => dimensions - position - (1, 1),
        }
    }
}

/// Rotation transform applied to a view.
#[derive(Clone, Copy, Debug)]
pub enum Rotation {
    /// No rotation occurs.
    None,
    /// Counterclockwise rotation by 90 degrees occurs.
    CCW,
    /// Rotation by 180 degrees occurs.
    Half,
    /// Clockwise rotation by 90 degrees occurs.
    CW,
}

impl From<u8> for Rotation {
    fn from(value: u8) -> Self {
        match value % 4 {
            0 => Rotation::None,
            1 => Rotation::CCW,
            2 => Rotation::Half,
            3 => Rotation::CW,
            _ => unreachable!(),
        }
    }
}

impl Rotation {
    fn apply(&self, position: Vector<i32>, dimensions: Vector<i32>) -> Vector<i32> {
        match self {
            Rotation::None => position,
            Rotation::CW => (position.y(), dimensions.y() - position.x() - 1).into(),
            Rotation::Half => dimensions - position - (1, 1),
            Rotation::CCW => (dimensions.x() - position.y() - 1, position.x()).into(),
        }
    }
}

impl<'a, I, P> From<&'a I> for View<'a, P>
where
    I: Image<P>,
{
    fn from(value: &'a I) -> Self {
        let dimensions = Vector::new(value.width(), value.height());
        View::new(value, Vector::<i32>::zero(), dimensions)
    }
}

impl<'a, P> From<&'a dyn Image<P>> for View<'a, P> {
    fn from(value: &'a dyn Image<P>) -> Self {
        let dimensions = Vector::new(value.width(), value.height());
        View::new(value, Vector::<i32>::zero(), dimensions)
    }
}

impl<'a, 'b, I, P> From<&'a mut I> for ViewMut<'b, P>
where
    'a: 'b,
    I: ImageMut<P>,
{
    fn from(value: &'a mut I) -> Self {
        let dimensions = Vector::new(value.width(), value.height());
        ViewMut::new_mut(value, Vector::<i32>::zero(), dimensions)
    }
}

impl<'a, 'b, P> From<&'a mut (dyn ImageMut<P> + 'a)> for ViewMut<'b, P>
where
    'a: 'b,
{
    fn from(value: &'a mut (dyn ImageMut<P> + 'a)) -> Self {
        let dimensions = Vector::new(value.width(), value.height());
        ViewMut::new_mut(value, Vector::<i32>::zero(), dimensions)
    }
}