pbrt 0.1.5

Rust implementation of https://pbrt.org/
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
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Types and utilities for dealing with 2D and 3D, integer and float data types.
use std::fmt;

use crate::core::geometry::point::Point2;
use crate::core::geometry::point::Point2i;
use crate::core::geometry::point::Point3;
use crate::core::geometry::vector::Vector2;
use crate::core::geometry::Number;
use crate::Float;

/// Generic type for and 2D bounding boxes.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Bounds2<T>
where
    T: Number,
{
    /// point representing the minimum x,y value of the bounds.
    pub p_min: Point2<T>,
    /// point representing the maximum x,y value of the bounds.
    pub p_max: Point2<T>,
}

impl<T> Default for Bounds2<T>
where
    T: Number,
{
    fn default() -> Self {
        Self {
            p_min: Point2 {
                x: T::max_value(),
                y: T::max_value(),
            },
            p_max: Point2 {
                x: T::min_value(),
                y: T::min_value(),
            },
        }
    }
}

impl<T> fmt::Display for Bounds2<T>
where
    T: Number,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[ {} - {} ]", self.p_min, self.p_max)
    }
}

impl<T> From<[[T; 2]; 2]> for Bounds2<T>
where
    T: Number,
{
    /// Create `Bounds2<T>` from tuple of slices.  It also ensures min/max are correct, regardless of
    /// how they're arranged in the incoming slices.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2f;
    /// use pbrt::core::geometry::Point2f;
    ///
    /// let b = Bounds2f::from([[2., 3.], [4., 5.]]);
    /// assert_eq!(
    ///     b,
    ///     Bounds2f {
    ///         p_min: Point2f { x: 2., y: 3. },
    ///         p_max: Point2f { x: 4., y: 5. }
    ///     }
    /// );
    ///
    /// let b = Bounds2f::from([[5., 4.], [3., 2.]]);
    /// assert_eq!(b, Bounds2f::from([[3., 2.], [5., 4.]]));
    /// ```
    fn from(ps: [[T; 2]; 2]) -> Self {
        let p1 = Point2::from(ps[0]);
        let p2 = Point2::from(ps[1]);
        [p1, p2].into()
    }
}

impl<T> From<[Point2<T>; 2]> for Bounds2<T>
where
    T: Number,
{
    /// Create `Bounds2<T>` from slice of `Point2<t>`.  It also ensures min/max are correct, regardless of
    /// how they're arranged in the incoming `Point2<t>`.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2f;
    /// use pbrt::core::geometry::Point2f;
    ///
    /// let b = Bounds2f::from([Point2f::from([2., 3.]), Point2f::from([4., 5.])]);
    /// assert_eq!(
    ///     b,
    ///     Bounds2f {
    ///         p_min: Point2f { x: 2., y: 3. },
    ///         p_max: Point2f { x: 4., y: 5. }
    ///     }
    /// );
    ///
    /// let b = Bounds2f::from([Point2f::from([5., 4.]), Point2f::from([3., 2.])]);
    /// assert_eq!(b, Bounds2f::from([[3., 2.], [5., 4.]]));
    /// ```
    fn from(ps: [Point2<T>; 2]) -> Self {
        let (p1, p2) = (ps[0], ps[1]);
        let p_min = Point2::from((
            if p1.x < p2.x { p1.x } else { p2.x },
            if p1.y < p2.y { p1.y } else { p2.y },
        ));
        let p_max = Point2::from((
            if p1.x > p2.x { p1.x } else { p2.x },
            if p1.y > p2.y { p1.y } else { p2.y },
        ));
        Bounds2 { p_min, p_max }
    }
}

impl<T> From<(Point2<T>, Point2<T>)> for Bounds2<T>
where
    T: Number,
{
    /// Create `Bounds2<T>` from tuple of `Point2<t>`.  It also ensures min/max are correct, regardless of
    /// how they're arranged in the incoming `Point2<t>`.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2f;
    /// use pbrt::core::geometry::Point2f;
    ///
    /// let b = Bounds2f::from([Point2f::from([2., 3.]), Point2f::from([4., 5.])]);
    /// assert_eq!(
    ///     b,
    ///     Bounds2f {
    ///         p_min: Point2f { x: 2., y: 3. },
    ///         p_max: Point2f { x: 4., y: 5. }
    ///     }
    /// );
    ///
    /// let b = Bounds2f::from((Point2f::from([5., 4.]), Point2f::from([3., 2.])));
    /// assert_eq!(b, Bounds2f::from([[3., 2.], [5., 4.]]));
    /// ```
    fn from((p1, p2): (Point2<T>, Point2<T>)) -> Self {
        let p_min = Point2::from((
            if p1.x < p2.x { p1.x } else { p2.x },
            if p1.y < p2.y { p1.y } else { p2.y },
        ));
        let p_max = Point2::from((
            if p1.x > p2.x { p1.x } else { p2.x },
            if p1.y > p2.y { p1.y } else { p2.y },
        ));
        Bounds2 { p_min, p_max }
    }
}

impl<T> Bounds2<T>
where
    T: Number,
{
    /// `diagonal` computes the `Vector2` representing the diagonal of this `Bounds2`.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2f;
    /// use pbrt::core::geometry::Vector2f;
    ///
    /// let b = Bounds2f::from([[1., 1.], [3., 3.]]);
    /// assert_eq!(b.diagonal(), Vector2f::from([2.,2.]));
    /// ```
    pub fn diagonal(&self) -> Vector2<T> {
        self.p_max - self.p_min
    }

    /// Computes the area covered by this bounding box.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2f;
    /// use pbrt::core::geometry::Point2f;
    ///
    /// let b = Bounds2f::from([[1., 1.], [3., 3.]]);
    /// assert_eq!(b.area(), 4.);
    /// ```
    pub fn area(&self) -> T {
        let d = self.p_max - self.p_min;
        d.x * d.y
    }

    /// Determine if `p` inside `self` excluding upper-bounds.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2i;
    /// use pbrt::core::geometry::Point2i;
    ///
    /// let b = Bounds2i::from([[2, 2], [4, 4]]);
    /// assert!(b.inside_exclusive(Point2i::from([2, 2])));
    /// assert!(!b.inside_exclusive(Point2i::from([4, 4])));
    /// ```
    pub fn inside_exclusive(&self, p: Point2<T>) -> bool {
        p.x >= self.p_min.x && p.x < self.p_max.x && p.y >= self.p_min.y && p.y < self.p_max.y
    }
}

impl<T> Bounds2<T>
where
    T: Number,
{
    /// Returns the intersection of of the two given bounds.  Note, the returned bounds may be
    /// invalid if the bounds do not overlap.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2i;
    ///
    /// let b1 = Bounds2i::from([[1, 1], [3, 3]]);
    /// let b2 = Bounds2i::from([[2, 2], [4, 4]]);
    /// assert_eq!(
    ///     Bounds2i::intersect(&b1, &b2),
    ///     Bounds2i::from([[2, 2], [3, 3]])
    /// );
    ///
    /// let b3 = Bounds2i::from([[1, 1], [2, 2]]);
    /// let b4 = Bounds2i::from([[3, 3], [4, 4]]);
    /// assert_eq!(
    ///     Bounds2i::intersect(&b3, &b4),
    ///     // Explicitly construct Bounds2i to get invalid p_min/p_max.
    ///     Bounds2i {
    ///         p_min: [3, 3].into(),
    ///         p_max: [2, 2].into()
    ///     }
    /// );
    /// ```
    pub fn intersect(b1: &Bounds2<T>, b2: &Bounds2<T>) -> Self {
        // Important: assign to p_min/p_max directly and don't run the Bounds2() constructor, since
        // it takes min/max of the points passed to it.  In turn, that breaks returning an invalid
        // bound for the case where we intersect non-overlapping bounds (as we'd like to happen).
        Self {
            p_min: Point2::max(b1.p_min, b2.p_min),
            p_max: Point2::min(b1.p_max, b2.p_max),
        }
    }
}

/// 2D bounding box type with `Float` members.
pub type Bounds2f = Bounds2<Float>;

impl From<Bounds2i> for Bounds2f {
    fn from(b: Bounds2i) -> Self {
        Self {
            p_min: b.p_min.into(),
            p_max: b.p_max.into(),
        }
    }
}

/// 2D bounding box type with `isize` members.
pub type Bounds2i = Bounds2<isize>;

impl Bounds2i {
    /// Returns and iterator that visits each `Point2i` within the `Bound2i`.  
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds2i;
    /// use pbrt::core::geometry::Point2i;
    ///
    /// let b = Bounds2i::from([[2, 2], [4, 4]]);
    /// let mut it = b.iter();
    /// assert_eq!(it.next(), Some(Point2i::from([2, 2])));
    /// assert_eq!(it.next(), Some(Point2i::from([3, 2])));
    /// assert_eq!(it.next(), Some(Point2i::from([2, 3])));
    /// assert_eq!(it.next(), Some(Point2i::from([3, 3])));
    /// assert_eq!(it.next(), None);
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = Point2i> {
        let x_range = self.p_min.x..self.p_max.x;
        let y_range = self.p_min.y..self.p_max.y;
        y_range.flat_map(move |y| x_range.clone().map(move |x| [x, y].into()))
    }
}

impl From<Bounds2f> for Bounds2i {
    fn from(b: Bounds2f) -> Self {
        Self {
            p_min: b.p_min.into(),
            p_max: b.p_max.into(),
        }
    }
}

pub struct Bounds2iIterator {
    b: Bounds2i,
    p: Point2i,
}

// For inspiration see:
// https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=775d8823d2a9c97707be44b74ed77f2b
impl Iterator for Bounds2iIterator {
    type Item = Point2i;

    fn next(&mut self) -> Option<Self::Item> {
        self.p.x += 1;
        if self.p.x == self.b.p_max.x {
            self.p.x = self.b.p_min.x;
            self.p.y += 1;
            if self.p.x == self.b.p_max.y {
                return None;
            }
        }
        Some(self.p)
    }
}

/// Generic type for 3D bounding boxes.
#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct Bounds3<T> {
    /// point representing the minimum x,y,z value of the bounds.
    pub p_min: Point3<T>,
    /// point representing the maximum x,y,z value of the bounds.
    pub p_max: Point3<T>,
}

/// 3D bounding box type with `Float` members.
pub type Bounds3f = Bounds3<Float>;
/// 3D bounding box type with `isize` members.
pub type Bounds3i = Bounds3<isize>;

impl<T> From<[[T; 3]; 2]> for Bounds3<T>
where
    T: Number,
{
    /// Create `Bounds3<T>` from tuple of slices.  It also ensures min/max are correct, regardless of
    /// how they're arranged in the incoming slices.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds3f;
    /// use pbrt::core::geometry::Point3f;
    ///
    /// let b = Bounds3f::from([[2., 3., 4.], [4., 5.,6.]]);
    /// assert_eq!(
    ///     b,
    ///     Bounds3f {
    ///         p_min: Point3f { x: 2., y: 3.,z:4. },
    ///         p_max: Point3f { x: 4., y: 5.,z:6. }
    ///     }
    /// );
    ///
    /// let b = Bounds3f::from([[5., 4., 1.], [3., 2.,3.]]);
    /// assert_eq!(b, Bounds3f::from([[3., 2.,1.], [5., 4.,3.]]));
    /// ```
    fn from(ps: [[T; 3]; 2]) -> Self {
        let p1 = Point3::from(ps[0]);
        let p2 = Point3::from(ps[1]);
        [p1, p2].into()
    }
}

impl<T> From<[Point3<T>; 2]> for Bounds3<T>
where
    T: Number,
{
    /// Create `Bounds3<T>` from slice of `Point3<t>`.  It also ensures min/max are correct, regardless of
    /// how they're arranged in the incoming `Point3<t>`.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds3f;
    /// use pbrt::core::geometry::Point3f;
    ///
    /// let b = Bounds3f::from([Point3f::from([2., 3.,4.]), Point3f::from([4., 5.,6.])]);
    /// assert_eq!(
    ///     b,
    ///     Bounds3f {
    ///         p_min: Point3f { x: 2., y: 3. ,z:4.},
    ///         p_max: Point3f { x: 4., y: 5.,z:6. }
    ///     }
    /// );
    ///
    /// let b = Bounds3f::from([Point3f::from([5., 4.,1.]), Point3f::from([3., 2.,3.])]);
    /// assert_eq!(b, Bounds3f::from([[3., 2.,1.], [5., 4.,3.]]));
    /// ```
    fn from(ps: [Point3<T>; 2]) -> Self {
        let (p1, p2) = (ps[0], ps[1]);
        let p_min = Point3::from([
            if p1.x < p2.x { p1.x } else { p2.x },
            if p1.y < p2.y { p1.y } else { p2.y },
            if p1.z < p2.z { p1.z } else { p2.z },
        ]);
        let p_max = Point3::from([
            if p1.x > p2.x { p1.x } else { p2.x },
            if p1.y > p2.y { p1.y } else { p2.y },
            if p1.z > p2.z { p1.z } else { p2.z },
        ]);
        Bounds3 { p_min, p_max }
    }
}

impl<T> Bounds3<T>
where
    T: Number,
{
    /// Determine if `p` inside `self` excluding upper-bounds.
    ///
    /// # Examples
    /// ```
    /// use pbrt::core::geometry::Bounds3i;
    /// use pbrt::core::geometry::Point3i;
    ///
    /// let b = Bounds3i::from([[2, 2, 2], [4, 4, 4]]);
    /// assert!(b.inside_exclusive(Point3i::from([2, 2, 2])));
    /// assert!(!b.inside_exclusive(Point3i::from([4, 4, 4])));
    /// ```
    pub fn inside_exclusive(&self, p: Point3<T>) -> bool {
        p.x >= self.p_min.x
            && p.x < self.p_max.x
            && p.y >= self.p_min.y
            && p.y < self.p_max.y
            && p.z >= self.p_min.z
            && p.z < self.p_max.z
    }
}