building_blocks_core 0.7.1

The core data types for defining 2D and 3D integer lattices.
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use crate::PointN;

use core::ops::{
    Add, AddAssign, BitAnd, BitOr, BitXor, Div, Mul, Neg, Not, Rem, Shl, Shr, Sub, SubAssign,
};
use num::Zero;

/// A trait that bundles op traits that all `PointN<N>` (and its components) should have.
pub trait Point:
    'static
    + Abs
    + Add<Output = Self>
    + AddAssign
    + Bounded
    + ConstZero
    + Copy
    + Div<<Self as Point>::Scalar, Output = Self>
    + Div<Self, Output = Self>
    + GetComponent<Scalar = <Self as Point>::Scalar>
    + MapComponents<Scalar = <Self as Point>::Scalar>
    + Mul<<Self as Point>::Scalar, Output = Self>
    + Mul<Self, Output = Self>
    + Ones
    + PartialEq
    + PartialOrd
    + Sized
    + Sub<Output = Self>
    + SubAssign
    + Neg
    + Zero
{
    type Scalar: Copy;

    fn fill(value: <Self as Point>::Scalar) -> Self;

    fn basis() -> Vec<Self>;

    fn volume(self) -> <Self as Point>::Scalar;
}

pub trait Abs {
    fn abs(self) -> Self;
}

pub trait GetComponent {
    type Scalar: Copy;

    /// Returns the component specified by index. I.e. X = 0, Y = 1, Z = 2.
    fn at(self, component_index: usize) -> Self::Scalar;
}

pub trait MapComponents {
    type Scalar;

    /// Returns the point after applying `f` component-wise.
    fn map_components_unary(self, f: impl Fn(Self::Scalar) -> Self::Scalar) -> Self;

    /// Returns the point after applying `f` component-wise to both `self` and `other` in parallel.
    fn map_components_binary(
        self,
        other: Self,
        f: impl Fn(Self::Scalar, Self::Scalar) -> Self::Scalar,
    ) -> Self;
}

pub trait MinMaxComponent {
    type Scalar;

    fn min_component(self) -> Self::Scalar;
    fn max_component(self) -> Self::Scalar;
}

pub trait Ones: Copy {
    /// A point of all ones.
    const ONES: Self;
}

pub trait Distance: Point {
    /// The L1 distance between points.
    fn l1_distance(self, other: Self) -> <Self as Point>::Scalar;

    /// The square of the L2 (Euclidean) distance between points.
    fn l2_distance_squared(self, other: Self) -> <Self as Point>::Scalar;
}

pub trait Norm: Sized {
    fn norm_squared(self) -> f32;

    #[inline]
    fn norm(self) -> f32 {
        self.norm_squared().sqrt()
    }
}

pub trait DotProduct {
    type Scalar: Copy;

    /// The vector dot product.
    fn dot(self, other: Self) -> Self::Scalar;
}

pub trait IntegerPoint<N>:
    BitAnd<Self, Output = Self>
    + BitOr<Self, Output = Self>
    + BitXor<Self, Output = Self>
    + BitAnd<i32, Output = Self>
    + BitOr<i32, Output = Self>
    + BitXor<i32, Output = Self>
    + Eq
    + IntegerDiv
    + IterExtent<N>
    + LatticeOrder
    + Neighborhoods
    + Not<Output = Self>
    + Point<Scalar = i32>
    + Rem<Self, Output = Self>
    + Shl<Self, Output = Self>
    + Shr<Self, Output = Self>
    + Rem<i32, Output = Self>
    + Shl<i32, Output = Self>
    + Shr<i32, Output = Self>
{
    /// Returns `true` iff all dimensions are powers of 2.
    fn dimensions_are_powers_of_2(self) -> bool;

    /// Returns `true` iff all dimensions are equal.
    fn is_cube(self) -> bool;
}

pub trait IntegerDiv {
    fn vector_div_floor(self, rhs: Self) -> Self;

    fn scalar_div_floor(self, rhs: i32) -> Self;

    fn vector_div_ceil(self, rhs: Self) -> Self;

    fn scalar_div_ceil(self, rhs: i32) -> Self;
}

pub trait LatticeOrder {
    /// Component-wise maximum.
    fn join(self, other: Self) -> Self;

    /// Component-wise minimum.
    fn meet(self, other: Self) -> Self;
}

pub trait FloatPoint<N>: IntoIntegerPoint + Point<Scalar = f32> {
    fn round(self) -> Self;

    fn floor(self) -> Self;

    fn ceil(self) -> Self;

    fn fract(self) -> Self;

    /// Ensures that you floor before casting to integers, since this is not the default behavior for negative integers.
    #[inline]
    fn floor_int(self) -> Self::IntPoint {
        self.floor().into_int()
    }
}

pub trait IntoIntegerPoint {
    type IntPoint;

    fn into_int(self) -> Self::IntPoint;
}

impl<N> FloatPoint<N> for PointN<N>
where
    Self: IntoIntegerPoint + Point<Scalar = f32>,
{
    #[inline]
    fn round(self) -> Self {
        self.map_components_unary(|c| c.round())
    }

    #[inline]
    fn floor(self) -> Self {
        self.map_components_unary(|c| c.floor())
    }

    #[inline]
    fn ceil(self) -> Self {
        self.map_components_unary(|c| c.ceil())
    }

    #[inline]
    fn fract(self) -> Self {
        self.map_components_unary(|c| c.fract())
    }
}

macro_rules! impl_unary_ops {
    ($t:ty, $scalar:ty) => {
        impl Mul<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn mul(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| rhs * c)
            }
        }

        impl Mul<$t> for $scalar {
            type Output = $t;

            #[inline]
            fn mul(self, rhs: $t) -> $t {
                rhs * self
            }
        }
    };
}

macro_rules! impl_binary_ops {
    ($t:ty, $scalar:ty) => {
        impl LatticeOrder for $t {
            #[inline]
            fn join(self, other: Self) -> Self {
                self.map_components_binary(other, <$scalar>::max)
            }

            #[inline]
            fn meet(self, other: Self) -> Self {
                self.map_components_binary(other, <$scalar>::min)
            }
        }

        impl Mul<Self> for $t {
            type Output = Self;

            #[inline]
            fn mul(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 * c2)
            }
        }
    };
}

macro_rules! impl_unary_integer_ops {
    ($t:ty, $scalar:ty) => {
        impl BitAnd<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn bitand(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c & rhs)
            }
        }

        impl BitOr<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn bitor(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c | rhs)
            }
        }

        impl BitXor<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn bitxor(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c ^ rhs)
            }
        }

        impl Not for $t {
            type Output = Self;

            #[inline]
            fn not(self) -> Self {
                self.map_components_unary(|c| !c)
            }
        }

        impl Rem<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn rem(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c.rem_euclid(rhs))
            }
        }

        impl Shl<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn shl(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c << rhs)
            }
        }

        impl Shr<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn shr(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c >> rhs)
            }
        }
    };
}

macro_rules! impl_binary_integer_ops {
    ($t:ty) => {
        impl BitAnd<Self> for $t {
            type Output = Self;

            #[inline]
            fn bitand(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 & c2)
            }
        }

        impl BitOr<Self> for $t {
            type Output = Self;

            #[inline]
            fn bitor(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 | c2)
            }
        }

        impl BitXor<Self> for $t {
            type Output = Self;

            #[inline]
            fn bitxor(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 ^ c2)
            }
        }

        impl Rem<Self> for $t {
            type Output = Self;

            #[inline]
            fn rem(self, other: Self) -> Self {
                self.map_components_binary(other, |c1, c2| c1.rem_euclid(c2))
            }
        }

        impl Shl<Self> for $t {
            type Output = Self;

            #[inline]
            fn shl(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 << c2)
            }
        }

        impl Shr<Self> for $t {
            type Output = Self;

            #[inline]
            fn shr(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 >> c2)
            }
        }
    };
}

macro_rules! impl_float_div {
    ($t:ty, $scalar:ty) => {
        impl Div<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn div(self, rhs: $scalar) -> Self {
                self.map_components_unary(|c| c / rhs)
            }
        }

        impl Div<Self> for $t {
            type Output = Self;

            #[inline]
            fn div(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| c1 / c2)
            }
        }
    };
}

macro_rules! impl_integer_div {
    ($t:ty, $scalar:ty) => {
        // Use specialized implementation for integers because the default Div impl rounds towards zero, which is not what we
        // want.
        impl Div<$scalar> for $t {
            type Output = Self;

            #[inline]
            fn div(self, rhs: $scalar) -> Self {
                self.scalar_div_floor(rhs)
            }
        }

        // Use specialized implementation for integers because the default Div impl rounds towards zero,
        // which is not what we want.
        impl Div<Self> for $t {
            type Output = Self;

            #[inline]
            fn div(self, rhs: Self) -> Self {
                self.vector_div_floor(rhs)
            }
        }

        impl IntegerDiv for $t {
            #[inline]
            fn vector_div_floor(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| Integer::div_floor(&c1,&c2))
            }

            #[inline]
            fn scalar_div_floor(self, rhs: i32) -> Self {
                self.map_components_unary(|c| Integer::div_floor(&c,&rhs))
            }

            #[inline]
            fn vector_div_ceil(self, rhs: Self) -> Self {
                self.map_components_binary(rhs, |c1, c2| Integer::div_ceil(&c1,&c2))
            }

            #[inline]
            fn scalar_div_ceil(self, rhs: i32) -> Self {
                self.map_components_unary(|c| Integer::div_ceil(&c,&rhs))
            }
        }
    };
}

pub trait Neighborhoods: Sized {
    /// All corners of an N-dimensional unit cube.
    fn corner_offsets() -> Vec<Self>;

    /// [Von Neumann Neighborhood](https://en.wikipedia.org/wiki/Von_Neumann_neighborhood)
    fn von_neumann_offsets() -> Vec<Self>;

    /// [Moore Neighborhood](https://en.wikipedia.org/wiki/Moore_neighborhood)
    fn moore_offsets() -> Vec<Self>;
}

pub trait IterExtent<N> {
    type PointIter: Iterator<Item = PointN<N>>;

    fn iter_extent(min: PointN<N>, max: PointN<N>) -> Self::PointIter;
}

// `Zero` trait doesn't allow associated constants for zero because of bignums.
pub trait ConstZero: Copy {
    const ZERO: Self;
}

// `One` trait doesn't allow associated constants for one because of bignums.
pub trait ConstOne: Copy {
    const ONE: Self;
}

impl ConstZero for i32 {
    const ZERO: i32 = 0;
}
impl ConstOne for i32 {
    const ONE: i32 = 1;
}

impl ConstZero for f32 {
    const ZERO: f32 = 0.0;
}
impl ConstOne for f32 {
    const ONE: f32 = 1.0;
}

pub trait Bounded: Copy {
    const MIN: Self;
    const MAX: Self;
}

impl Bounded for i32 {
    const MIN: Self = std::i32::MIN;
    const MAX: Self = std::i32::MAX;
}

impl Bounded for f32 {
    const MIN: Self = std::f32::MIN;
    const MAX: Self = std::f32::MAX;
}