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
//!
//! Geometry helper functionality.
use crate::{Vec3, Vec3i, Vec3u};

/// A plane which can be intersected by a ray.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct Plane {
    /// plane described as x,y,z normal
    pub normal: Vec3,

    /// dot product of the point and normal, representing the plane position
    pub bias: f32,
}

/// A Ray represents an infinite half-line starting at `origin` and going in specified unit length `direction`.
#[derive(Debug, Copy, Clone)]
#[repr(C)]
pub struct Ray {
    /// origin point of the ray
    pub origin: Vec3,

    /// normalized direction vector of the ray
    pub direction: Vec3,
}

/// A plane which can be intersected by a ray.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Planeu {
    /// plane described as x,y,z normal
    pub normal: Vec3u,

    /// dot product of the point and normal, representing the plane position
    pub bias: u32,
}

/// A Ray represents an infinite half-line starting at `origin` and going in specified unit length `direction`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Rayu {
    /// origin point of the ray
    pub origin: Vec3u,

    /// normalized direction vector of the ray
    pub direction: Vec3u,
}

/// A plane which can be intersected by a ray.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Planei {
    /// plane described as x,y,z normal
    pub normal: Vec3i,

    /// dot product of the point and normal, representing the plane position
    pub bias: i32,
}

/// A Ray represents an infinite half-line starting at `origin` and going in specified unit length `direction`.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Rayi {
    /// origin point of the ray
    pub origin: Vec3i,

    /// normalized direction vector of the ray
    pub direction: Vec3i,
}

macro_rules! impl_plane_ray {
    ($($pn:ident, $rn:ident, $v3t:ident => $t:ident),+) => {
        $(
            impl $rn {
                /// Returns the distance along the ray which intersects with the provided `Plane`
                #[inline]
                pub fn intersect_plane(&self, plane: $pn) -> Option<$t> {
                    plane.intersect_ray(*self)
                }

                /// Returns a `Vec3` along the ray at a distance `t` from it's origin.
                #[inline]
                pub fn at_distance(&self, z: $t) -> $v3t {
                    self.direction.mul_add($v3t::broadcast(z), self.origin)
                }
            }

            impl $pn {
                /// Create a new `Plane`.
                #[inline]
                pub fn new(normal: $v3t, bias: $t) -> Self {
                    $pn { normal, bias }
                }

                /// Create a new `Plane` from a point normal representation. The normal parameter must already be normalized.
                #[inline]
                pub fn from_point_normal(point: $v3t, normal: $v3t) -> Self {
                    Self {
                        normal,
                        bias: point.dot(normal),
                    }
                }

                /// Create a new `Plane` from a point normal representation
                #[inline]
                pub fn from_point_vectors(point: $v3t, v1: $v3t, v2: $v3t) -> Self {
                    Self::from_point_normal(point, v1.cross(v2))
                }

                /// Create a `Plane` which is facing along the X-Axis at the provided coordinate.
                #[inline]
                pub fn with_x(x: $t) -> Self {
                    Self::from_point_normal($v3t::new(x, 0 as $t, 0 as $t), $v3t::new(1 as $t, 0 as $t, 0 as $t,))
                }

                /// Create a `Plane` which is facing along the Y-Axis at the provided coordinate.
                #[inline]
                pub fn with_y(y: $t) -> Self {
                    Self::from_point_normal($v3t::new(0 as $t, y, 0 as $t), $v3t::new(0 as $t, 1 as $t, 0 as $t))
                }

                /// Create a `Plane` which is facing along the Z-Axis at the provided coordinate.
                #[inline]
                pub fn with_z(z: $t) -> Self {
                    Self::from_point_normal($v3t::new(0 as $t, 0 as $t, z), $v3t::new(0 as $t, 0 as $t, 1 as $t))
                }

                /// f32his `Plane` normal
                #[inline]
                pub fn normal(&self) -> $v3t {
                    self.normal
                }

                /// Normalized representation of this `Plane`
                #[inline]
                pub fn normalize(&mut self)  {
                    let distance = self.normal.mag();
                    self.normal /= distance;
                    self.bias /= distance;
                }

                /// Normalized representation of this `Plane`
                #[inline]
                pub fn normalized(&self) -> Self {
                    let distance = self.normal.mag();
                    Self {
                        normal: self.normal / distance,
                        bias: self.bias / distance,
                    }
                }

                /// Returns the dot product of this `Plane` and a provided `Vec3`
                #[inline]
                pub fn dot_point(&self, point: $v3t) -> $t {
                    self.normal.x * point.x + self.normal.y * point.y + self.normal.z * point.z + self.bias
                }

                /// Returns the dot product of this `Plane` and a provided `Vec3`, assumed to be a normal, computed with this planes normal.
                #[inline]
                pub fn dot(&self, point: $v3t) -> $t {
                    self.normal.x * point.x + self.normal.y * point.y + self.normal.z * point.z
                }

                /// Returns the dot product of this `Plane` with another `Plane`. This is computed against the two plane normals.
                #[inline]
                pub fn dot_plane(&self, plane: $pn) -> $t {
                    self.normal.x * plane.normal.x
                        + self.normal.y * plane.normal.y
                        + self.normal.z * plane.normal.z
                        + self.bias * plane.bias
                }

                /// Returns the intersection distance of the provided line given a point and direction, or `None` if none occurs.
                ///
                /// Warning: These intersection methods do not check for the ray never intersecting. This is up to the user to confirm.
                #[inline]
                pub fn intersect_line(&self, point: $v3t, direction: $v3t) -> Option<$t> {
                    let fv = self.dot(direction);
                    let distance = self.dot_point(point) / fv;

                    Some(distance)
                }

                /// Returns the intersection distance of the provided `Ray`, or `None` if none occurs.
                ///
                /// Warning: These intersection methods do not check for the ray never intersecting. This is up to the user to confirm.
                #[inline]
                pub fn intersect_ray(&self, ray: $rn) -> Option<$t> {
                    self.intersect_line(ray.origin, ray.direction)
                }
            }
        )+
    }
}

impl_plane_ray!(Plane, Ray, Vec3 => f32);
impl_plane_ray!(Planeu, Rayu, Vec3u => u32);
impl_plane_ray!(Planei, Rayi, Vec3i => i32);

/// An axis-aligned bounding box
#[derive(Default, Debug, Copy, Clone)]
#[repr(C)]
pub struct Aabb {
    pub min: Vec3,
    pub max: Vec3,
}

/// An axis-aligned bounding box
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Aabbu {
    pub min: Vec3u,
    pub max: Vec3u,
}

/// An axis-aligned bounding box
#[derive(Default, Debug, Copy, Clone, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct Aabbi {
    pub min: Vec3i,
    pub max: Vec3i,
}

macro_rules! impl_aabb {
    ($($n:ident, $iter:ident, $v3t:ident => $t:ident),+) => {
        $(
        impl $n {
            /// Creates a new axis-aligned bounding box.
            ///
            /// `min` **must** be less than or equal to `max`. This is not checked by the library, but will result in
            /// bad results and/or unsigned integer underflow if it is not held.
            #[must_use]
            pub fn new(min: $v3t, max: $v3t) -> Self {
                Self { min, max }
            }

            #[inline]
            #[must_use]
            pub fn contains(&self, target: $v3t) -> bool {
                target.x >= self.min.x
                    && target.x <= self.max.x
                    && target.y >= self.min.y
                    && target.y <= self.max.y
                    && target.z >= self.min.z
                    && target.z <= self.max.z
            }

            #[inline]
            #[must_use]
            pub fn intersects(&self, other: &Self) -> bool {
                (self.min.x <= other.max.x && self.max.x >= other.min.x)
                    && (self.min.y <= other.max.y && self.max.y >= other.min.y)
                    && (self.min.z <= other.max.z && self.max.z >= other.min.z)
            }

            #[inline]
            #[must_use]
            pub fn size(&self) -> $v3t {
                self.max - self.min
            }

            #[inline]
            #[must_use]
            pub fn volume(&self) -> $t {
                self.size().x * self.size().y * self.size().z
            }

            #[inline]
            #[must_use]
            pub fn iter_stride(&self, stride: $t) -> $iter {
                $iter::new(*self, stride)
            }
        }

        /// Linear iterator across a 3D coordinate space with the provided stride.
        /// This iterator is inclusive of minimum coordinates, and exclusive of maximum.
        pub struct $iter {
            stride: $t,
            track: $v3t,
            region: $n,
        }
        impl $iter {
            /// Create a new iterator.
            #[must_use]
            pub fn new(region: $n, stride: $t) -> Self {
                Self {
                    track: region.min,
                    region,
                    stride,
                }
            }
        }
        impl Iterator for $iter {
            type Item = $v3t;

            fn next(&mut self) -> Option<Self::Item> {
                let ret = self.track;

                if self.track.z >= self.region.max.z {
                    return None;
                }

                if self.track.x >= self.region.max.x - (1 as $t) {
                    self.track.y += self.stride;
                    self.track.x = self.region.min.x;
                } else {
                    self.track.x += self.stride;
                    return Some(ret);
                }

                if self.track.y >= self.region.max.y {
                    self.track.z += self.stride;

                    self.track.y = self.region.min.y;
                }

                Some(ret)
            }

            #[inline]
            fn size_hint(&self) -> (usize, Option<usize>) {
                let cur_volume = ($n::new(self.track, self.region.max).volume() / self.stride / self.stride / self.stride) as usize;
                let volume = (self.region.volume() / self.stride / self.stride / self.stride) as usize;
                (volume - cur_volume, Some(volume))
            }
        }
        impl ExactSizeIterator for $iter {}
        )+
    }
}

impl Aabb {
    /// Same as iter_stride, but calls it with a stride of 1.0
    #[inline]
    #[must_use]
    pub fn iter(&self) -> AabbLinearIterator {
        self.iter_stride(1.0)
    }
}

impl Aabbu {
    /// Same as iter_stride, but calls it with a stride of 1.0
    #[inline]
    #[must_use]
    pub fn iter(&self) -> AabbuLinearIterator {
        self.iter_stride(1)
    }
}

impl Aabbi {
    /// Same as iter_stride, but calls it with a stride of 1.0
    #[inline]
    #[must_use]
    pub fn iter(&self) -> AabbiLinearIterator {
        self.iter_stride(1)
    }
}

impl_aabb!(Aabb, AabbLinearIterator, Vec3 => f32, Aabbu, AabbuLinearIterator, Vec3u => u32, Aabbi, AabbiLinearIterator, Vec3i => i32);