healpix 0.3.2

Rust implementation of the HEALPix tesselation.
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
//! Direction enumerations.

pub mod map;
pub mod set;

use std::ops::Mul;

/// A positive or negative value
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(i8)]
pub enum PosOrNeg {
    Neg = -1,
    Zero = 0,
    Pos = 1,
}
impl PosOrNeg {
    pub const fn from_val(val: i8) -> Self {
        if val < 0 {
            Self::Neg
        } else if val > 0 {
            Self::Pos
        } else {
            Self::Zero
        }
    }
}
impl Mul for PosOrNeg {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        if self == PosOrNeg::Zero || rhs == PosOrNeg::Zero {
            PosOrNeg::Zero
        } else if self == rhs {
            PosOrNeg::Pos
        } else {
            PosOrNeg::Neg
        }
    }
}
impl Mul<f64> for PosOrNeg {
    type Output = f64;

    fn mul(self, rhs: f64) -> Self::Output {
        match self {
            PosOrNeg::Pos => rhs,
            PosOrNeg::Neg => -rhs,
            PosOrNeg::Zero => 0.0,
        }
    }
}

/// A cardinal direction: North, East, South, or West.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Cardinal {
    N,
    E,
    S,
    W,
}
impl Cardinal {
    pub const VALUES: [Self; 4] = [Self::N, Self::E, Self::S, Self::W];
    /// Convert this to an index. This starts with 0 as north and rotates clockwise.
    #[inline(always)]
    pub const fn index(self) -> u8 {
        self as u8
    }
    /// Convert an index to a cardinal direction, using the ordering specified in [`index`](Self::index).
    #[inline(always)]
    pub const fn from_index(index: u8) -> Self {
        match index {
            0 => Self::N,
            1 => Self::E,
            2 => Self::S,
            3 => Self::W,
            _ => panic!("Cardinal index out of bounds"),
        }
    }
    /// Attempt to convert an index to a cardinal direction, using the ordering specified in [`index`](Self::index).
    #[inline(always)]
    pub const fn try_from_index(index: u8) -> Option<Self> {
        match index {
            0 => Some(Self::N),
            1 => Some(Self::E),
            2 => Some(Self::S),
            3 => Some(Self::W),
            _ => None,
        }
    }
    /// Get the direction of the X direction. East is positive.
    #[inline(always)]
    pub const fn x(self) -> PosOrNeg {
        match self {
            Self::W => PosOrNeg::Neg,
            Self::E => PosOrNeg::Pos,
            _ => PosOrNeg::Zero,
        }
    }
    /// Get the direction of the Y direction. North is positive.
    #[inline(always)]
    pub const fn y(self) -> PosOrNeg {
        match self {
            Self::S => PosOrNeg::Neg,
            Self::N => PosOrNeg::Pos,
            _ => PosOrNeg::Zero,
        }
    }
    /// Get the x and y directions of this coordinate. East is positive X and north is positive Y.
    #[inline(always)]
    pub const fn xy(self) -> [PosOrNeg; 2] {
        match self {
            Self::N => [PosOrNeg::Zero, PosOrNeg::Pos],
            Self::E => [PosOrNeg::Pos, PosOrNeg::Zero],
            Self::S => [PosOrNeg::Zero, PosOrNeg::Neg],
            Self::W => [PosOrNeg::Neg, PosOrNeg::Zero],
        }
    }
    /// Try to create find a cardinal direction from X- and Y- directions.
    #[inline(always)]
    pub const fn from_xy(x: PosOrNeg, y: PosOrNeg) -> Option<Self> {
        match [x, y] {
            [PosOrNeg::Zero, PosOrNeg::Pos] => Some(Self::N),
            [PosOrNeg::Pos, PosOrNeg::Zero] => Some(Self::E),
            [PosOrNeg::Zero, PosOrNeg::Neg] => Some(Self::S),
            [PosOrNeg::Neg, PosOrNeg::Zero] => Some(Self::W),
            _ => None,
        }
    }
}
impl Enum for Cardinal {
    fn from_enum_index(idx: usize) -> Self {
        Self::from_index(idx as _)
    }
    fn into_enum_index(self) -> usize {
        self.index() as _
    }
}

/// An ordinal direction: Northeast, Southeast, Southwest, or Northwest.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Ordinal {
    NE,
    SE,
    SW,
    NW,
}
impl Ordinal {
    pub const VALUES: [Self; 4] = [Self::NE, Self::SE, Self::SW, Self::NW];
    /// Convert this to an index. This starts with 0 as northeast and rotates clockwise.
    /// The value is 45 degrees to the right of the cardinal value with the same index.
    #[inline(always)]
    pub const fn index(self) -> u8 {
        self as u8
    }
    /// Convert an index to a cardinal direction, using the ordering specified in [`index`](Self::index).
    #[inline(always)]
    pub const fn from_index(index: u8) -> Self {
        match index {
            0 => Self::NE,
            1 => Self::SE,
            2 => Self::SW,
            3 => Self::NW,
            _ => panic!("Ordinal index out of bounds"),
        }
    }
    /// Attempt to convert an index to a cardinal direction, using the ordering specified in [`index`](Self::index).
    #[inline(always)]
    pub const fn try_from_index(index: u8) -> Option<Self> {
        match index {
            0 => Some(Self::NE),
            1 => Some(Self::SE),
            2 => Some(Self::SW),
            3 => Some(Self::NW),
            _ => None,
        }
    }
    /// Get the direction of the X direction. East is positive.
    #[inline(always)]
    pub const fn x(self) -> PosOrNeg {
        match self {
            Self::NW | Self::SW => PosOrNeg::Neg,
            Self::NE | Self::SE => PosOrNeg::Pos,
        }
    }
    /// Get the direction of the Y direction. North is positive.
    #[inline(always)]
    pub const fn y(self) -> PosOrNeg {
        match self {
            Self::SW | Self::SE => PosOrNeg::Neg,
            Self::NW | Self::NE => PosOrNeg::Pos,
        }
    }
    /// Get the x and y directions of this coordinate. East is positive X and north is positive Y.
    #[inline(always)]
    pub const fn xy(self) -> [PosOrNeg; 2] {
        match self {
            Self::NE => [PosOrNeg::Pos, PosOrNeg::Pos],
            Self::SE => [PosOrNeg::Pos, PosOrNeg::Neg],
            Self::SW => [PosOrNeg::Neg, PosOrNeg::Neg],
            Self::NW => [PosOrNeg::Neg, PosOrNeg::Pos],
        }
    }
    /// Try to create find a ordinal direction from X- and Y- directions.
    #[inline(always)]
    pub const fn from_xy(x: PosOrNeg, y: PosOrNeg) -> Option<Self> {
        match [x, y] {
            [PosOrNeg::Pos, PosOrNeg::Pos] => Some(Self::NE),
            [PosOrNeg::Pos, PosOrNeg::Neg] => Some(Self::SE),
            [PosOrNeg::Neg, PosOrNeg::Neg] => Some(Self::SW),
            [PosOrNeg::Neg, PosOrNeg::Pos] => Some(Self::NW),
            _ => None,
        }
    }

    /// Rotate clockwise by a given amount.
    #[inline(always)]
    pub const fn rot_cw(self, dist: u8) -> Self {
        Self::from_index(self.index().wrapping_add(dist) % 4)
    }
    /// Rotate counter-clockwise by a given amount.
    #[inline(always)]
    pub const fn rot_ccw(self, dist: u8) -> Self {
        Self::from_index(self.index().wrapping_sub(dist) % 4)
    }
    /// Clockwise rotation distance to rotate self to the other direction.
    #[inline(always)]
    pub const fn dist_cw(self, other: Self) -> u8 {
        other.index().wrapping_sub(self.index()) % 4
    }
    /// Counter-clockwise rotation distance to rotate self to the other direction.
    #[inline(always)]
    pub const fn dist_ccw(self, other: Self) -> u8 {
        self.index().wrapping_sub(other.index()) % 4
    }
}
impl Enum for Ordinal {
    fn from_enum_index(idx: usize) -> Self {
        Self::from_index(idx as _)
    }
    fn into_enum_index(self) -> usize {
        self.index() as _
    }
}

/// A direction, either cardinal or ordinal.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Direction {
    N,
    NE,
    E,
    SE,
    S,
    SW,
    W,
    NW,
}
impl Direction {
    pub const VALUES: [Self; 8] = [
        Self::N,
        Self::NE,
        Self::E,
        Self::SE,
        Self::S,
        Self::SW,
        Self::W,
        Self::NW,
    ];
    #[inline(always)]
    pub const fn index(self) -> u8 {
        self as u8
    }
    #[inline(always)]
    pub const fn from_index(index: u8) -> Self {
        match index {
            0 => Self::N,
            1 => Self::NE,
            2 => Self::E,
            3 => Self::SE,
            4 => Self::S,
            5 => Self::SW,
            6 => Self::W,
            7 => Self::NW,
            _ => panic!("Direction index out of bounds"),
        }
    }
    #[inline(always)]
    pub const fn try_from_index(index: u8) -> Option<Self> {
        match index {
            0 => Some(Self::N),
            1 => Some(Self::NE),
            2 => Some(Self::E),
            3 => Some(Self::SE),
            4 => Some(Self::S),
            5 => Some(Self::SW),
            6 => Some(Self::W),
            7 => Some(Self::NW),
            _ => None,
        }
    }
    /// Get the direction of the X direction. East is positive.
    #[inline(always)]
    pub const fn x(self) -> PosOrNeg {
        match self {
            Self::NW | Self::SW | Self::W => PosOrNeg::Neg,
            Self::NE | Self::SE | Self::E => PosOrNeg::Pos,
            Self::N | Self::S => PosOrNeg::Zero,
        }
    }
    /// Get the direction of the Y direction. North is positive.
    #[inline(always)]
    pub const fn y(self) -> PosOrNeg {
        match self {
            Self::SW | Self::SE | Self::S => PosOrNeg::Neg,
            Self::NW | Self::NE | Self::N => PosOrNeg::Pos,
            Self::W | Self::E => PosOrNeg::Zero,
        }
    }
    /// Get the x and y directions of this coordinate. East is positive X and north is positive Y.
    #[inline(always)]
    pub const fn xy(self) -> [PosOrNeg; 2] {
        match self {
            Self::N => [PosOrNeg::Zero, PosOrNeg::Pos],
            Self::NE => [PosOrNeg::Pos, PosOrNeg::Pos],
            Self::E => [PosOrNeg::Pos, PosOrNeg::Zero],
            Self::SE => [PosOrNeg::Pos, PosOrNeg::Neg],
            Self::S => [PosOrNeg::Zero, PosOrNeg::Neg],
            Self::SW => [PosOrNeg::Neg, PosOrNeg::Neg],
            Self::W => [PosOrNeg::Neg, PosOrNeg::Zero],
            Self::NW => [PosOrNeg::Neg, PosOrNeg::Pos],
        }
    }
    /// Get the offset to the southeast.
    #[inline(always)]
    pub const fn se(self) -> PosOrNeg {
        match self {
            Self::S | Self::W | Self::SW => PosOrNeg::Neg,
            Self::N | Self::E | Self::NE => PosOrNeg::Pos,
            Self::NW | Self::SE => PosOrNeg::Zero,
        }
    }
    /// Get the offset to the southwest.
    #[inline(always)]
    pub const fn sw(self) -> PosOrNeg {
        match self {
            Self::S | Self::E | Self::SE => PosOrNeg::Neg,
            Self::N | Self::W | Self::NW => PosOrNeg::Pos,
            Self::NE | Self::SW => PosOrNeg::Zero,
        }
    }
    /// Try to create find a direction from X- and Y- directions.
    #[inline(always)]
    pub const fn from_xy(x: PosOrNeg, y: PosOrNeg) -> Option<Self> {
        match [x, y] {
            [PosOrNeg::Zero, PosOrNeg::Pos] => Some(Self::N),
            [PosOrNeg::Pos, PosOrNeg::Pos] => Some(Self::NE),
            [PosOrNeg::Pos, PosOrNeg::Zero] => Some(Self::E),
            [PosOrNeg::Pos, PosOrNeg::Neg] => Some(Self::SE),
            [PosOrNeg::Zero, PosOrNeg::Neg] => Some(Self::S),
            [PosOrNeg::Neg, PosOrNeg::Neg] => Some(Self::SW),
            [PosOrNeg::Neg, PosOrNeg::Zero] => Some(Self::W),
            [PosOrNeg::Neg, PosOrNeg::Pos] => Some(Self::NW),
            _ => None,
        }
    }
    /// Try to find a direction from SE- and SW- offsets.
    #[inline(always)]
    pub const fn from_sesw(se: PosOrNeg, sw: PosOrNeg) -> Option<Self> {
        match [se, sw] {
            [PosOrNeg::Zero, PosOrNeg::Pos] => Some(Self::NW),
            [PosOrNeg::Pos, PosOrNeg::Pos] => Some(Self::N),
            [PosOrNeg::Pos, PosOrNeg::Zero] => Some(Self::NE),
            [PosOrNeg::Pos, PosOrNeg::Neg] => Some(Self::E),
            [PosOrNeg::Zero, PosOrNeg::Neg] => Some(Self::SE),
            [PosOrNeg::Neg, PosOrNeg::Neg] => Some(Self::S),
            [PosOrNeg::Neg, PosOrNeg::Zero] => Some(Self::SW),
            [PosOrNeg::Neg, PosOrNeg::Pos] => Some(Self::W),
            _ => None,
        }
    }
    #[inline(always)]
    pub const fn opposite(self) -> Self {
        Self::from_index((self.index() + 4) % 8)
    }
    #[inline(always)]
    pub const fn is_cardinal(self) -> bool {
        self.index() & 1 == 0
    }
    #[inline(always)]
    pub const fn is_ordinal(self) -> bool {
        self.index() & 1 == 1
    }
    #[inline(always)]
    pub const fn unwrap_cardinal(self) -> Cardinal {
        debug_assert!(self.is_cardinal());
        Cardinal::from_index(self.index() >> 1)
    }
    #[inline(always)]
    pub const fn unwrap_ordinal(self) -> Ordinal {
        debug_assert!(self.is_ordinal());
        Ordinal::from_index(self.index() >> 1)
    }
}

impl Enum for Direction {
    fn from_enum_index(idx: usize) -> Self {
        Self::from_index(idx as _)
    }
    fn into_enum_index(self) -> usize {
        self.index() as _
    }
}
impl From<Cardinal> for Direction {
    fn from(value: Cardinal) -> Self {
        match value {
            Cardinal::N => Self::N,
            Cardinal::E => Self::E,
            Cardinal::S => Self::S,
            Cardinal::W => Self::W,
        }
    }
}

impl From<Ordinal> for Direction {
    fn from(value: Ordinal) -> Self {
        match value {
            Ordinal::NE => Self::NE,
            Ordinal::SE => Self::SE,
            Ordinal::SW => Self::SW,
            Ordinal::NW => Self::NW,
        }
    }
}

/// A type that can be converted to and from indices.
pub trait Enum {
    /// Convert this value into an index.
    fn into_enum_index(self) -> usize;
    /// Take an index from [`into_enum_index`](Self::into_enum_index) and convert it back to this.
    fn from_enum_index(idx: usize) -> Self;
}