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
use euclid::Point3D;

use crate::block::{
    self, Evoxel, Evoxels, MinEval, Modifier,
    Resolution::{self, R1},
};
use crate::math::{Cube, GridAab, GridCoordinate, GridPoint, Vol};
use crate::universe;

/// Data for [`Modifier::Zoom`], describing a portion of the original block that is scaled
/// up to become the whole block.
///
/// Design note: This is a struct separate from [`Modifier`] so that it can have a
/// constructor accepting only valid bounds.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct Zoom {
    /// Scale factor to zoom in by.
    scale: Resolution,

    /// Which portion of the block/space will be used, specified in terms of an offset
    /// in the grid of zoomed blocks (that is, this should have coordinates between `0`
    /// and `scale - 1`).
    offset: Point3D<u8, Cube>,
    // /// If present, a space to extract voxels from _instead of_ the underlying
    // /// [`Primitive`]. This may be used so that the before-zooming block can be a
    // /// custom preview rather than an exact miniature of the multi-block
    // /// structure.
    // space: Option<Handle<Space>>,
}

impl Zoom {
    /// Construct a [`Zoom`] which enlarges the original block's voxels by `scale` and
    /// selects the region of them whose lower corner is `offset * scale`.
    ///
    /// Panics if any of `offset`'s components are out of bounds, i.e. less than 0 or
    /// greater than `scale - 1`.
    #[track_caller]
    pub fn new(scale: Resolution, offset: GridPoint) -> Self {
        if !GridAab::for_block(scale).contains_cube(Cube::from(offset)) {
            panic!("Zoom offset {offset:?} out of bounds for {scale}");
        }

        Self {
            scale,
            offset: offset.map(|c| c as u8),
        }
    }

    /// Decompose into parts, for serialization.
    #[cfg(feature = "save")]
    pub(crate) fn to_serial_schema(&self) -> crate::save::schema::ModifierSer<'static> {
        let Zoom { scale, offset } = *self;
        crate::save::schema::ModifierSer::ZoomV1 {
            scale,
            offset: offset.into(),
        }
    }

    pub(super) fn evaluate(
        &self,
        input: MinEval,
        filter: &block::EvalFilter,
    ) -> Result<MinEval, block::InEvalError> {
        let Zoom {
            offset: offset_in_zoomed_blocks,
            scale,
        } = *self;

        // TODO: respect filter.skip_eval

        // TODO: To efficiently implement this, we should be able to run in a phase
        // *before* the `Primitive` evaluation, which allows us to reduce how many
        // of the primitive voxels are evaluated. (Modifier::Move will also benefit.)

        let original_resolution = input.resolution();
        let MinEval { attributes, voxels } = input;

        // TODO: write test cases for what happens if the division fails
        // (this is probably wrong in that we need to duplicate voxels if it happens)
        let zoom_resolution = (original_resolution / scale).unwrap_or(R1);

        Ok(match voxels {
            Evoxels::One(_) => {
                // Block has resolution 1.
                // Zoom::new() checks that the region is not outside the block's unit cube,
                // so we can just unconditionally return the original color.
                MinEval { attributes, voxels }
            }
            Evoxels::Many(_, voxels) => {
                let voxel_offset = offset_in_zoomed_blocks
                    .map(GridCoordinate::from)
                    .to_vector()
                    * GridCoordinate::from(zoom_resolution);
                match GridAab::for_block(zoom_resolution)
                    .intersection_cubes(voxels.bounds().translate(-voxel_offset))
                {
                    // This case occurs when the voxels' actual bounds (which may be smaller
                    // than the block bounding box) don't intersect the zoom region.
                    None => MinEval {
                        attributes,
                        voxels: Evoxels::One(Evoxel::AIR),
                    },
                    Some(intersected_bounds) => {
                        block::Budget::decrement_voxels(
                            &filter.budget,
                            intersected_bounds.volume().unwrap(),
                        )?;
                        MinEval {
                            attributes,
                            voxels: Evoxels::Many(
                                zoom_resolution,
                                Vol::from_fn(intersected_bounds, |p| voxels[p + voxel_offset]),
                            ),
                        }
                    }
                }
            }
        })
    }

    /// Scale factor to zoom in by.
    pub fn scale(&self) -> Resolution {
        self.scale
    }

    /// Which portion of the block/space will be used, specified in terms of an offset
    /// in the grid of zoomed blocks (that is, this will have coordinates between `0`
    /// and `scale - 1`).
    pub fn offset(&self) -> GridPoint {
        self.offset.map(i32::from)
    }
}

impl From<Zoom> for Modifier {
    fn from(value: Zoom) -> Self {
        Modifier::Zoom(value)
    }
}

impl universe::VisitHandles for Zoom {
    fn visit_handles(&self, _visitor: &mut dyn universe::HandleVisitor) {
        let Zoom {
            scale: _,
            offset: _,
        } = self;
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Zoom {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let scale = u.arbitrary()?;
        let max_offset = GridCoordinate::from(scale) - 1;
        Ok(Self::new(
            scale,
            GridPoint::new(
                u.int_in_range(0..=max_offset)?,
                u.int_in_range(0..=max_offset)?,
                u.int_in_range(0..=max_offset)?,
            ),
        ))
    }

    fn size_hint(depth: usize) -> (usize, Option<usize>) {
        use arbitrary::{size_hint::and_all, Arbitrary};
        and_all(&[
            <Resolution as Arbitrary>::size_hint(depth),
            <[GridCoordinate; 3] as Arbitrary>::size_hint(depth),
        ])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::block::{EvaluatedBlock, Resolution::R2};
    use crate::content::{make_some_blocks, make_some_voxel_blocks};
    use crate::math::{GridVector, Rgba};
    use crate::universe::Universe;
    use euclid::point3;
    use pretty_assertions::assert_eq;

    #[test]
    #[should_panic(expected = "Zoom offset (2, 1, 1) out of bounds for 2")]
    fn construction_out_of_range_high() {
        Zoom::new(R2, point3(2, 1, 1));
    }

    #[test]
    #[should_panic(expected = "Zoom offset (-1, 1, 1) out of bounds for 2")]
    fn construction_out_of_range_low() {
        Zoom::new(R2, point3(-1, 1, 1));
    }

    #[test]
    fn evaluation() {
        let mut universe = Universe::new();
        let [original_block] = make_some_voxel_blocks(&mut universe);

        let ev_original = original_block.evaluate().unwrap();
        let zoom_resolution = ev_original.resolution().halve().unwrap();
        let original_voxels = &ev_original.voxels;

        // Try zoom at multiple offset steps.
        for x in 0i32..2 {
            dbg!(x);
            let zoomed = original_block.clone().with_modifier(Zoom::new(
                R2, // scale up by two = divide resolution by two
                point3(x, 0, 0),
            ));
            let ev_zoomed = zoomed.evaluate().unwrap();
            assert_eq!(
                ev_zoomed,
                if x >= 2 {
                    // out of range
                    EvaluatedBlock::from_voxels(
                        ev_original.attributes.clone(),
                        Evoxels::One(Evoxel::from_color(Rgba::TRANSPARENT)),
                        block::Cost {
                            components: 2,
                            voxels: 16u32.pow(3), // counts evaluation of Recur
                            recursion: 0,
                        },
                    )
                } else {
                    EvaluatedBlock::from_voxels(
                        ev_original.attributes.clone(),
                        Evoxels::Many(
                            zoom_resolution,
                            Vol::from_fn(GridAab::for_block(zoom_resolution), |p| {
                                original_voxels[p + GridVector::new(
                                    GridCoordinate::from(zoom_resolution) * x,
                                    0,
                                    0,
                                )]
                            }),
                        ),
                        block::Cost {
                            components: 2,
                            voxels: 16u32.pow(3) + 8u32.pow(3), // Recur + Zoom
                            recursion: 0,
                        },
                    )
                }
            );
        }
    }

    #[test]
    fn atom_in_bounds() {
        let [original] = make_some_blocks();
        let mut zoomed = original.clone();
        zoomed.modifiers_mut().push(Modifier::Zoom(Zoom {
            scale: R2,
            offset: point3(1, 0, 0),
        }));
        assert_eq!(zoomed.evaluate().unwrap().color, original.color());
    }
}