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
//! A crate providing data structures for square-tiled grids wrapped around the surface of certain objects.
//! This create was intended to be used for the creation of cellular automata on non-flat grids.
//! The crate provides a trait `SurfaceGrid` with an associated type `Point` which can be used to traverse the grid squares.
//! Additionally, for grids that wrap a sphere the `Point` type implements the `SpherePoint` trait providing conversions
//! between geographic and surface grid coordinates.
//! 
//! ## Available Surfaces
//! ### Spheres
//! - `RectangleSphereGrid` - Uses an equirectangular projection to wrap a rectangle around the sphere.
//! - `CubeSphereGrid` - Projects a cube over the sphere with each face being a square grid.

use std::ops::{IndexMut, Index};

use rayon::iter::ParallelIterator;

pub mod sphere;

/// A grid wrapped around a surface.
pub trait SurfaceGrid<T> : IndexMut<Self::Point> + Index<Self::Point, Output = T> + IntoIterator<Item = (Self::Point, T)> {
    /// The type of a point on this grid.
    type Point: GridPoint + Send;

    /// Creates a new surface grid by calling the specified function for each point in the grid.
    ///
    /// - `f` - The function to apply.
    fn from_fn<F: FnMut(&Self::Point) -> T>(f: F) -> Self;

    /// Creates a new surface grid by calling the specified function in parallel for each point in
    /// the grid.
    ///
    /// - `f` - The function to apply.
    fn from_fn_par<F: Fn(&Self::Point) -> T + Send + Sync>(f: F) -> Self where T: Send + Sync;

    /// Applies a function to each cell and its direct neighbours.
    ///
    /// The provided function is called with the arguments: current, up, down, left, right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours<F: FnMut(&T, &T, &T, &T, &T) -> T>(&self, mut f: F) -> Self where Self: Sized {
        Self::from_fn(|current| {
            f(&self[current.clone()], &self[current.up()], &self[current.down()], &self[current.left()], &self[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals.
    ///
    /// The provided function is called with the arguments: up_left, up, up_right,
    /// left, current, right, down_left, down, down_right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_diagonals<
                F: FnMut(&T, &T, &T, &T, &T, &T, &T, &T, &T) -> T
            >(&self, mut f: F) -> Self where Self: Sized {
        Self::from_fn(|current| {
            f(
                &self[current.up().left()], &self[current.up()], &self[current.up().right()],
                &self[current.left()], &self[current.clone()], &self[current.right()],
                &self[current.down().left()], &self[current.down()], &self[current.down().right()]
                )
        })
    }
    
    /// Applies a function in parallel to each cell and its direct neighbours.
    ///
    /// The provided function is called with the arguments: current, up, down, left, right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_par<
                F: Fn(&T, &T, &T, &T, &T) -> T + Send + Sync
            >(&self, f: F) -> Self where Self: Sized + Sync, T: Send + Sync {
        Self::from_fn_par(|current| {
            f(&self[current.clone()], &self[current.up()], &self[current.down()], &self[current.left()], &self[current.right()])
        })
    }
    
    /// Applies a function in parallel to each cell and its direct neighbours including diagonals.
    ///
    /// The provided function is called with the arguments: up_left, up, up_right,
    /// left, current, right, down_left, down, down_right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_diagonals_par<
                F: Fn(&T, &T, &T, &T, &T, &T, &T, &T, &T) -> T + Send + Sync
            >(&self, f: F) -> Self where Self: Sized + Sync, T: Send + Sync {
        Self::from_fn_par(|current| {
            f(
                &self[current.up().left()], &self[current.up()], &self[current.up().right()],
                &self[current.left()], &self[current.clone()], &self[current.right()],
                &self[current.down().left()], &self[current.down()], &self[current.down().right()]
                )
        })
    }
    
    /// Applies a function to each cell and its direct neighbours providing the current point.
    ///
    /// The provided function is called with the arguments: current, position, up, down, left, right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_with_position<F: FnMut(&T, &Self::Point, &T, &T, &T, &T) -> T>(&self, mut f: F) -> Self where Self: Sized {
        Self::from_fn(|current| {
            f(&self[current.clone()], current, &self[current.up()], &self[current.down()], &self[current.left()], &self[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals providing the
    /// current point.
    ///
    /// The provided function is called with the arguments: position
    /// up_left, up, up_right,
    /// left, current, right,
    /// down_left, down, down_right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_diagonals_with_position<
                F: FnMut(&Self::Point, &T, &T, &T, &T, &T, &T, &T, &T, &T) -> T
            >(&self, mut f: F) -> Self where Self: Sized {
        Self::from_fn(|current| {
            f(current,
                &self[current.up().left()], &self[current.up()], &self[current.up().right()],
                &self[current.left()], &self[current.clone()], &self[current.right()],
                &self[current.down().left()], &self[current.down()], &self[current.down().right()]
                )
        })
    }
    
    /// Applies a function in parallel to each cell and its direct neighbours providing the current
    /// point.
    ///
    /// The provided function is called with the arguments: current, position, up, down, left, right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_par_with_position<
                F: Fn(&T, &Self::Point, &T, &T, &T, &T) -> T + Send + Sync
            >(&self, f: F) -> Self where Self: Sized + Sync, T: Send + Sync {
        Self::from_fn_par(|current| {
            f(&self[current.clone()], current, &self[current.up()], &self[current.down()], &self[current.left()], &self[current.right()])
        })
    }
    
    /// Applies a function in parallel to each cell and its direct neighbours including diagonals
    /// providing the current point.
    ///
    /// The provided function is called with the arguments: position,
    /// up_left, up, up_right,
    /// left, current, right, 
    /// down_left, down, down_right.
    ///
    /// `f` - The function to apply.
    fn map_neighbours_diagonals_par_with_position<
                F: Fn(&Self::Point, &T, &T, &T, &T, &T, &T, &T, &T, &T) -> T + Send + Sync
            >(&self, f: F) -> Self where Self: Sized + Sync, T: Send + Sync {
        Self::from_fn_par(|current| {
            f(current,
                &self[current.up().left()], &self[current.up()], &self[current.up().right()],
                &self[current.left()], &self[current.clone()], &self[current.right()],
                &self[current.down().left()], &self[current.down()], &self[current.down().right()]
                )
        })
    }
    
    /// Updates this surface grid by calling the specified function for each point in the grid.
    ///
    /// - `f` - The function to apply.
    fn set_from_fn<F: FnMut(&Self::Point) -> T>(&mut self, f: F);
    
    /// Updates this surface grid by calling the specified function for each point in the grid in
    /// parallel.
    ///
    /// - `f` - The function to apply.
    fn set_from_fn_par<F: Fn(&Self::Point) -> T + Send + Sync>(&mut self, f: F) where T: Send + Sync;

    /// Applies a function to each cell and its direct neighbours.
    ///
    /// The provided function is called with the arguments: current, up, down, left, right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours<
                U,
                G: SurfaceGrid<U, Point = Self::Point>,
                F: FnMut(&U, &U, &U, &U, &U) -> T
            >(&mut self, source: &G, mut f: F) {
        self.set_from_fn(|current| {
            f(&source[current.clone()], &source[current.up()], &source[current.down()], &source[current.left()], &source[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals.
    ///
    /// The provided function is called with the arguments: up_left, up, up_right,
    /// left, current, right, down_left, down, down_right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_diagonals<
                U,
                G: SurfaceGrid<U, Point = Self::Point>,
                F: FnMut(&U, &U, &U, &U, &U, &U, &U, &U, &U) -> T
            >(&mut self, source: &G, mut f: F) {
        self.set_from_fn(|current| {
            f(
                &source[current.up().left()], &source[current.up()], &source[current.up().right()],
                &source[current.left()], &source[current.clone()], &source[current.right()],
                &source[current.down().left()], &source[current.down()], &source[current.down().right()]
                )
        })
    }
    
    /// Applies a function to each cell and its direct neighbours in parallel.
    ///
    /// The provided function is called with the arguments: current, up, down, left, right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_par<
                U,
                G: SurfaceGrid<U, Point = Self::Point> + Sync,
                F: Fn(&U, &U, &U, &U, &U) -> T + Send + Sync
            >(&mut self, source: &G, f: F) where T: Send + Sync {
        self.set_from_fn(|current| {
            f(&source[current.clone()], &source[current.up()], &source[current.down()], &source[current.left()], &source[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals in parallel.
    ///
    /// The provided function is called with the arguments: up_left, up, up_right,
    /// left, current, right, down_left, down, down_right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_diagonals_par<
                U,
                G: SurfaceGrid<U, Point = Self::Point> + Sync,
                F: Fn(&U, &U, &U, &U, &U, &U, &U, &U, &U) -> T + Send + Sync
            >(&mut self, source: &G, f: F) where T: Send + Sync {
        self.set_from_fn(|current| {
            f(
                &source[current.up().left()], &source[current.up()], &source[current.up().right()],
                &source[current.left()], &source[current.clone()], &source[current.right()],
                &source[current.down().left()], &source[current.down()], &source[current.down().right()]
                )
        })
    }
    
    /// Applies a function to each cell and its direct neighbours providing the position.
    ///
    /// The provided function is called with the arguments: current, position, up, down, left, right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_with_position<
                U,
                G: SurfaceGrid<U, Point = Self::Point>,
                F: FnMut(&U, &Self::Point, &U, &U, &U, &U) -> T
            >(&mut self, source: &G, mut f: F) {
        self.set_from_fn(|current| {
            f(&source[current.clone()], current, &source[current.up()], &source[current.down()], &source[current.left()], &source[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals.
    ///
    /// The provided function is called with the arguments: position,
    /// up_left, up, up_right,
    /// left, current, right,
    /// down_left, down, down_right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_diagonals_with_position<
                U,
                G: SurfaceGrid<U, Point = Self::Point>,
                F: FnMut(&Self::Point, &U, &U, &U, &U, &U, &U, &U, &U, &U) -> T
            >(&mut self, source: &G, mut f: F) {
        self.set_from_fn(|current| {
            f(current,
                &source[current.up().left()], &source[current.up()], &source[current.up().right()],
                &source[current.left()], &source[current.clone()], &source[current.right()],
                &source[current.down().left()], &source[current.down()], &source[current.down().right()]
                )
        })
    }
    
    /// Applies a function to each cell and its direct neighbours in parallel.
    ///
    /// The provided function is called with the arguments: current, position, up, down, left, right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_par_with_position<
                U,
                G: SurfaceGrid<U, Point = Self::Point> + Sync,
                F: Fn(&U, &Self::Point, &U, &U, &U, &U) -> T + Send + Sync
            >(&mut self, source: &G, f: F) where T: Send + Sync {
        self.set_from_fn(|current| {
            f(&source[current.clone()], current, &source[current.up()], &source[current.down()], &source[current.left()], &source[current.right()])
        })
    }
    
    /// Applies a function to each cell and its direct neighbours including diagonals in parallel.
    ///
    /// The provided function is called with the arguments: position,
    /// up_left, up, up_right,
    /// left, current, right,
    /// down_left, down, down_right.
    ///
    /// `source` - The source grid from which to read data.
    /// `f` - The function to apply.
    fn set_from_neighbours_diagonals_par_with_position<
                U,
                G: SurfaceGrid<U, Point = Self::Point> + Sync,
                F: Fn(&Self::Point, &U, &U, &U, &U, &U, &U, &U, &U, &U) -> T + Send + Sync
            >(&mut self, source: &G, f: F) where T: Send + Sync {
        self.set_from_fn(|current| {
            f(current,
                &source[current.up().left()], &source[current.up()], &source[current.up().right()],
                &source[current.left()], &source[current.clone()], &source[current.right()],
                &source[current.down().left()], &source[current.down()], &source[current.down().right()]
                )
        })
    }

    /// Iterates over the points in this grid and their values.
    fn iter<'a>(&'a self) -> impl Iterator<Item = (Self::Point, &'a T)> where T: 'a;

    /// Iterates over the points in this grid and their values in parallel.
    fn par_iter<'a>(&'a self) -> impl ParallelIterator<Item = (Self::Point, &'a T)> where T: 'a + Send + Sync;

    /// Iterates over the points in this grid.
    fn points(&self) -> impl Iterator<Item = Self::Point>;

    /// Iterates over the points in this grid in parallel.
    fn par_points(&self) -> impl ParallelIterator<Item = Self::Point>;
}

/// A point on a surface grid.
/// 
/// A type implementing this trait should ensure that the following conditions are met:
/// - Two points are equal if they point to the same physical location on the grid.
///
/// A point on the grid must also be associated with some direction.
/// For surfaces that loop this should work such that moving in the same direction will eventually
/// result in reaching the point at which you started moving.
pub trait GridPoint : Eq + PartialEq + Clone {
    /// Gets the point that is immediately above this grid point.
    fn up(&self) -> Self;
    
    /// Gets the point that is immediately below this grid point.
    fn down(&self) -> Self;

    /// Gets the point that is immediately to the left of this grid point.
    fn left(&self) -> Self;

    /// Gets the point that is immediately to the right of this grid point.
    fn right(&self) -> Self;

    /// Gets the position of the point in 3D space.
    ///
    /// - `scale` - The scale of the 3D object.
    fn position(&self, scale: f64) -> (f64, f64, f64);
}