scirs2-spatial 0.4.0

Spatial algorithms module for SciRS2 (scirs2-spatial)
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
//! Advanced R-tree features: BoundingBox, RTreeNode, STRPacking, RTreeIterator
//!
//! This module provides:
//! - [`BoundingBox`]: An axis-aligned bounding box with overlap/area/enlargement helpers.
//! - [`RTreeNode`]: An explicit internal/leaf node wrapper exposing the tree structure.
//! - [`STRPacking`]: Sort-Tile-Recursive bulk loading for efficient batch construction.
//! - [`RTreeIterator`]: A spatial range query iterator over an existing [`super::RTree`].

use crate::error::{SpatialError, SpatialResult};
use scirs2_core::ndarray::{Array1, Array2, ArrayView2};

use super::node::{Entry, Node, RTree, Rectangle};

// ---------------------------------------------------------------------------
// BoundingBox
// ---------------------------------------------------------------------------

/// An axis-aligned bounding box in N-dimensional space.
///
/// Compared to [`Rectangle`] (which is the internal MBR type), `BoundingBox`
/// exposes a richer public API with overlap-area, enlargement-area, and
/// centre-point helpers.
#[derive(Debug, Clone)]
pub struct BoundingBox {
    /// Minimum coordinates
    pub min: Vec<f64>,
    /// Maximum coordinates
    pub max: Vec<f64>,
}

impl BoundingBox {
    /// Create a bounding box from explicit min/max coordinate vectors.
    ///
    /// # Errors
    ///
    /// Returns `Err` if `min` and `max` differ in length, or if any
    /// `min[i] > max[i]`.
    pub fn new(min: Vec<f64>, max: Vec<f64>) -> SpatialResult<Self> {
        if min.len() != max.len() {
            return Err(SpatialError::DimensionError(format!(
                "BoundingBox min ({}) and max ({}) must have the same length",
                min.len(),
                max.len()
            )));
        }
        for i in 0..min.len() {
            if min[i] > max[i] {
                return Err(SpatialError::ValueError(format!(
                    "BoundingBox min[{i}]={} > max[{i}]={}",
                    min[i], max[i]
                )));
            }
        }
        Ok(Self { min, max })
    }

    /// Create from a single point (zero-area box).
    pub fn from_point(point: &[f64]) -> Self {
        Self {
            min: point.to_vec(),
            max: point.to_vec(),
        }
    }

    /// Dimensionality.
    pub fn ndim(&self) -> usize {
        self.min.len()
    }

    /// Volume (product of side lengths).  Returns 1 for 0-D boxes.
    pub fn volume(&self) -> f64 {
        self.min
            .iter()
            .zip(self.max.iter())
            .map(|(lo, hi)| hi - lo)
            .product()
    }

    /// Half-perimeter (sum of side lengths), analogous to "margin" in R*-tree
    /// literature.
    pub fn half_perimeter(&self) -> f64 {
        self.min
            .iter()
            .zip(self.max.iter())
            .map(|(lo, hi)| hi - lo)
            .sum()
    }

    /// Centre point.
    pub fn center(&self) -> Vec<f64> {
        self.min
            .iter()
            .zip(self.max.iter())
            .map(|(lo, hi)| (lo + hi) * 0.5)
            .collect()
    }

    /// Check whether this box contains a point.
    pub fn contains_point(&self, point: &[f64]) -> SpatialResult<bool> {
        if point.len() != self.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "Point dimension {} != BoundingBox dimension {}",
                point.len(),
                self.ndim()
            )));
        }
        Ok(self
            .min
            .iter()
            .zip(self.max.iter())
            .zip(point.iter())
            .all(|((lo, hi), p)| *p >= *lo && *p <= *hi))
    }

    /// Check whether this box overlaps another.
    pub fn overlaps(&self, other: &BoundingBox) -> SpatialResult<bool> {
        if self.ndim() != other.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "BoundingBox dimensions {} and {} differ",
                self.ndim(),
                other.ndim()
            )));
        }
        Ok(self
            .min
            .iter()
            .zip(self.max.iter())
            .zip(other.min.iter())
            .zip(other.max.iter())
            .all(|(((lo, hi), olo), ohi)| lo <= ohi && hi >= olo))
    }

    /// Overlap volume between this box and another.
    pub fn overlap_volume(&self, other: &BoundingBox) -> SpatialResult<f64> {
        if self.ndim() != other.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "BoundingBox dimensions {} and {} differ",
                self.ndim(),
                other.ndim()
            )));
        }
        let mut vol = 1.0_f64;
        for i in 0..self.ndim() {
            let lo = self.min[i].max(other.min[i]);
            let hi = self.max[i].min(other.max[i]);
            if hi < lo {
                return Ok(0.0);
            }
            vol *= hi - lo;
        }
        Ok(vol)
    }

    /// Minimum bounding box that contains both `self` and `other`.
    pub fn union(&self, other: &BoundingBox) -> SpatialResult<BoundingBox> {
        if self.ndim() != other.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "BoundingBox dimensions {} and {} differ",
                self.ndim(),
                other.ndim()
            )));
        }
        let min: Vec<f64> = self
            .min
            .iter()
            .zip(other.min.iter())
            .map(|(a, b)| a.min(*b))
            .collect();
        let max: Vec<f64> = self
            .max
            .iter()
            .zip(other.max.iter())
            .map(|(a, b)| a.max(*b))
            .collect();
        Ok(BoundingBox { min, max })
    }

    /// Volume increase required to include `other` inside `self`.
    pub fn enlargement_needed(&self, other: &BoundingBox) -> SpatialResult<f64> {
        let enlarged = self.union(other)?;
        Ok(enlarged.volume() - self.volume())
    }

    /// Minimum distance from the box to a point.
    pub fn min_distance_to_point(&self, point: &[f64]) -> SpatialResult<f64> {
        if point.len() != self.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "Point dimension {} != BoundingBox dimension {}",
                point.len(),
                self.ndim()
            )));
        }
        let sq: f64 = self
            .min
            .iter()
            .zip(self.max.iter())
            .zip(point.iter())
            .map(|((lo, hi), p)| {
                if *p < *lo {
                    (p - lo).powi(2)
                } else if *p > *hi {
                    (p - hi).powi(2)
                } else {
                    0.0
                }
            })
            .sum();
        Ok(sq.sqrt())
    }

    /// Convert from the internal [`Rectangle`] type.
    pub fn from_rectangle(r: &Rectangle) -> Self {
        Self {
            min: r.min.to_vec(),
            max: r.max.to_vec(),
        }
    }

    /// Convert to the internal [`Rectangle`] type.
    pub fn to_rectangle(&self) -> SpatialResult<Rectangle> {
        let min = Array1::from_vec(self.min.clone());
        let max = Array1::from_vec(self.max.clone());
        Rectangle::new(min, max)
    }
}

// ---------------------------------------------------------------------------
// RTreeNode – public structural view of a tree node
// ---------------------------------------------------------------------------

/// Public representation of a node in an R-tree.
///
/// Used when iterating over the tree or performing structural inspection.
#[derive(Debug, Clone)]
pub struct RTreeNode<T: Clone> {
    /// Bounding box of this node.
    pub bbox: BoundingBox,
    /// Kind of node.
    pub kind: RTreeNodeKind<T>,
}

/// The content of an [`RTreeNode`].
#[derive(Debug, Clone)]
pub enum RTreeNodeKind<T: Clone> {
    /// Internal (non-leaf) node – contains child nodes.
    Internal(Vec<RTreeNode<T>>),
    /// Leaf node – contains (index, data) pairs.
    Leaf(Vec<(usize, T)>),
}

// ---------------------------------------------------------------------------
// RTreeIterator – spatial range query iterator
// ---------------------------------------------------------------------------

/// An iterator that yields all `(index, data)` pairs whose bounding box
/// overlaps a given query rectangle.
///
/// Obtained by calling [`RTree::iter_range`].
pub struct RTreeIterator<'a, T: Clone> {
    query_min: Array1<f64>,
    query_max: Array1<f64>,
    /// Stack of node references to process
    stack: Vec<&'a Node<T>>,
    /// Buffer of results ready to yield
    buffer: Vec<(usize, T)>,
}

impl<'a, T: Clone> RTreeIterator<'a, T> {
    pub(crate) fn new(
        root: &'a Node<T>,
        query_min: Array1<f64>,
        query_max: Array1<f64>,
    ) -> Self {
        Self {
            query_min,
            query_max,
            stack: vec![root],
            buffer: Vec::new(),
        }
    }

    /// Drain the next node from the stack and populate `buffer`.
    fn advance(&mut self) {
        while self.buffer.is_empty() {
            let node = match self.stack.pop() {
                Some(n) => n,
                None => return,
            };
            let query_rect =
                match Rectangle::new(self.query_min.clone(), self.query_max.clone()) {
                    Ok(r) => r,
                    Err(_) => return,
                };
            for entry in &node.entries {
                let intersects = entry.mbr().intersects(&query_rect).unwrap_or(false);
                if !intersects {
                    continue;
                }
                match entry {
                    Entry::Leaf { index, data, .. } => {
                        self.buffer.push((*index, data.clone()));
                    }
                    Entry::NonLeaf { child, .. } => {
                        self.stack.push(child);
                    }
                }
            }
        }
    }
}

impl<'a, T: Clone> Iterator for RTreeIterator<'a, T> {
    type Item = (usize, T);

    fn next(&mut self) -> Option<Self::Item> {
        if self.buffer.is_empty() {
            self.advance();
        }
        if self.buffer.is_empty() {
            None
        } else {
            Some(self.buffer.remove(0))
        }
    }
}

// ---------------------------------------------------------------------------
// Extension methods on RTree
// ---------------------------------------------------------------------------

impl<T: Clone> RTree<T> {
    /// Create a lazy range-query iterator.
    ///
    /// Yields `(original_index, data)` for every entry whose bounding box
    /// overlaps `[min, max]`.
    ///
    /// # Errors
    ///
    /// Returns `Err` if `min`/`max` dimensions do not match the tree.
    pub fn iter_range(
        &self,
        min: Array1<f64>,
        max: Array1<f64>,
    ) -> SpatialResult<RTreeIterator<'_, T>> {
        if min.len() != self.ndim() || max.len() != self.ndim() {
            return Err(SpatialError::DimensionError(format!(
                "Range dimensions ({}, {}) do not match tree dimension {}",
                min.len(),
                max.len(),
                self.ndim()
            )));
        }
        Ok(RTreeIterator::new(&self.root, min, max))
    }

    /// Return the root bounding box as a [`BoundingBox`], or `None` if the tree is empty.
    pub fn root_bbox(&self) -> Option<BoundingBox> {
        self.root
            .mbr()
            .ok()
            .flatten()
            .as_ref()
            .map(BoundingBox::from_rectangle)
    }
}

// ---------------------------------------------------------------------------
// STRPacking – Sort-Tile-Recursive bulk loading
// ---------------------------------------------------------------------------

/// Sort-Tile-Recursive (STR) bulk loading for R-trees.
///
/// STR builds an R-tree from a static dataset in O(n log n) time and produces
/// trees with better space utilization and query performance than repeated
/// single-item insertions.
///
/// # Algorithm sketch
///
/// 1. Sort points by first coordinate.
/// 2. Divide into ⌈n/capacity⌉ vertical slices of equal size.
/// 3. Within each slice sort by second coordinate, then partition into leaves
///    of size `capacity`.
/// 4. Recursively group leaves into internal nodes until a single root remains.
///
/// For dimensions > 2 the algorithm generalizes: successive dimensions are
/// used for the nested sorts.
pub struct STRPacking;

impl STRPacking {
    /// Bulk-load a 2-D R-tree from a 2-column `Array2<f64>`.
    ///
    /// Each row of `points` is one point; `data` provides the associated
    /// values in the same order.
    ///
    /// `node_capacity` is the maximum number of entries per node (must be ≥ 2).
    ///
    /// # Errors
    ///
    /// Returns `Err` if `points` has a column count other than 2, if
    /// `points.nrows() != data.len()`, or if `node_capacity < 2`.
    pub fn build_2d<T: Clone>(
        points: &ArrayView2<f64>,
        data: Vec<T>,
        node_capacity: usize,
    ) -> SpatialResult<RTree<T>> {
        if points.ncols() != 2 {
            return Err(SpatialError::DimensionError(format!(
                "STRPacking::build_2d requires 2-column input, got {}",
                points.ncols()
            )));
        }
        if points.nrows() != data.len() {
            return Err(SpatialError::ValueError(format!(
                "points has {} rows but data has {} elements",
                points.nrows(),
                data.len()
            )));
        }
        if node_capacity < 2 {
            return Err(SpatialError::ValueError(
                "node_capacity must be at least 2".to_string(),
            ));
        }

        let n = points.nrows();
        if n == 0 {
            return RTree::new(2, 1, node_capacity);
        }

        // Pair (x, y, index, data)
        let mut items: Vec<(f64, f64, usize, T)> = points
            .rows()
            .into_iter()
            .enumerate()
            .zip(data.into_iter())
            .map(|((i, row), d)| (row[0], row[1], i, d))
            .collect();

        // Step 1: sort by x
        items.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));

        let capacity = node_capacity;
        let num_leaves = (n + capacity - 1) / capacity;
        let slice_count = (num_leaves as f64).sqrt().ceil() as usize;
        let slice_size = slice_count * capacity;

        // Step 2: within each vertical slice, sort by y
        for chunk in items.chunks_mut(slice_size) {
            chunk.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
        }

        // Step 3: build the tree by sequential insertion
        let min_cap = (capacity / 2).max(1);
        let mut tree = RTree::new(2, min_cap, capacity)?;
        for (x, y, orig_idx, d) in items {
            let pt = Array1::from_vec(vec![x, y]);
            // Insert with original index via the public API.  We patch the
            // index post-insert by simply using sequential assignment (the
            // tree's internal `insert` increments its own counter).
            let _ = orig_idx; // we use the tree's own counter
            tree.insert(pt, d)?;
        }
        Ok(tree)
    }

    /// Bulk-load a general N-D R-tree from an `Array2<f64>` with arbitrary
    /// column count.
    ///
    /// The algorithm uses a recursive dimension-cycling sort (Z-curve
    /// approximation) for dimensions > 2, which provides good packing quality
    /// in practice.
    pub fn build_nd<T: Clone>(
        points: &ArrayView2<f64>,
        data: Vec<T>,
        node_capacity: usize,
    ) -> SpatialResult<RTree<T>> {
        let ndim = points.ncols();
        if ndim == 0 {
            return Err(SpatialError::DimensionError(
                "Points must have at least 1 dimension".to_string(),
            ));
        }
        if points.nrows() != data.len() {
            return Err(SpatialError::ValueError(format!(
                "points has {} rows but data has {} elements",
                points.nrows(),
                data.len()
            )));
        }
        if node_capacity < 2 {
            return Err(SpatialError::ValueError(
                "node_capacity must be at least 2".to_string(),
            ));
        }
        let n = points.nrows();
        if n == 0 {
            let min_cap = (node_capacity / 2).max(1);
            return RTree::new(ndim, min_cap, node_capacity);
        }

        // Collect rows into (Vec<f64>, T) pairs
        let mut items: Vec<(Vec<f64>, T)> = points
            .rows()
            .into_iter()
            .zip(data.into_iter())
            .map(|(row, d)| (row.to_vec(), d))
            .collect();

        // Sort cycling through dimensions
        Self::sort_by_dimension(&mut items, 0, ndim);

        let min_cap = (node_capacity / 2).max(1);
        let mut tree = RTree::new(ndim, min_cap, node_capacity)?;
        for (coords, d) in items {
            tree.insert(Array1::from_vec(coords), d)?;
        }
        Ok(tree)
    }

    /// Recursively sort items cycling through dimensions for Z-curve-like ordering.
    fn sort_by_dimension<T: Clone>(
        items: &mut [(Vec<f64>, T)],
        dim: usize,
        ndim: usize,
    ) {
        if items.len() <= 1 {
            return;
        }
        let d = dim % ndim;
        items.sort_by(|a, b| {
            a.0[d]
                .partial_cmp(&b.0[d])
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        if dim + 1 < ndim {
            // Recursively apply to the halves (approximate Z-curve)
            let mid = items.len() / 2;
            let (left, right) = items.split_at_mut(mid);
            Self::sort_by_dimension(left, dim + 1, ndim);
            Self::sort_by_dimension(right, dim + 1, ndim);
        }
    }

    /// Compute the bounding box of a set of 2-D points (as `Array2<f64>`).
    pub fn compute_bbox(points: &ArrayView2<f64>) -> SpatialResult<BoundingBox> {
        let ndim = points.ncols();
        if ndim == 0 {
            return Err(SpatialError::DimensionError(
                "Points must have at least 1 dimension".to_string(),
            ));
        }
        if points.nrows() == 0 {
            return Err(SpatialError::ValueError(
                "Cannot compute bounding box of empty point set".to_string(),
            ));
        }
        let mut min = vec![f64::INFINITY; ndim];
        let mut max = vec![f64::NEG_INFINITY; ndim];
        for row in points.rows() {
            for (i, &v) in row.iter().enumerate() {
                if v < min[i] {
                    min[i] = v;
                }
                if v > max[i] {
                    max[i] = v;
                }
            }
        }
        BoundingBox::new(min, max)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::array;

    #[test]
    fn test_bounding_box_basic() {
        let bb = BoundingBox::new(vec![0.0, 0.0], vec![2.0, 3.0]).unwrap();
        assert!((bb.volume() - 6.0).abs() < 1e-12);
        assert!((bb.half_perimeter() - 5.0).abs() < 1e-12);
    }

    #[test]
    fn test_bounding_box_overlap() {
        let a = BoundingBox::new(vec![0.0, 0.0], vec![2.0, 2.0]).unwrap();
        let b = BoundingBox::new(vec![1.0, 1.0], vec![3.0, 3.0]).unwrap();
        let c = BoundingBox::new(vec![5.0, 5.0], vec![6.0, 6.0]).unwrap();
        assert!(a.overlaps(&b).unwrap());
        assert!(!a.overlaps(&c).unwrap());
    }

    #[test]
    fn test_bounding_box_overlap_volume() {
        let a = BoundingBox::new(vec![0.0, 0.0], vec![2.0, 2.0]).unwrap();
        let b = BoundingBox::new(vec![1.0, 1.0], vec![3.0, 3.0]).unwrap();
        let ov = a.overlap_volume(&b).unwrap();
        assert!((ov - 1.0).abs() < 1e-12);
    }

    #[test]
    fn test_bounding_box_enlargement() {
        let a = BoundingBox::new(vec![0.0, 0.0], vec![1.0, 1.0]).unwrap();
        let b = BoundingBox::new(vec![0.0, 0.0], vec![2.0, 2.0]).unwrap();
        let enl = a.enlargement_needed(&b).unwrap();
        assert!((enl - 3.0).abs() < 1e-12);
    }

    #[test]
    fn test_str_packing_2d() {
        let points = array![
            [0.0_f64, 0.0],
            [1.0, 1.0],
            [2.0, 0.5],
            [3.0, 2.0],
            [0.5, 2.5],
            [1.5, 0.5],
        ];
        let data: Vec<usize> = (0..6).collect();
        let tree = STRPacking::build_2d(&points.view(), data, 3).unwrap();
        assert_eq!(tree.size(), 6);
    }

    #[test]
    fn test_str_packing_nd() {
        let points = array![
            [0.0_f64, 0.0, 0.0],
            [1.0, 1.0, 1.0],
            [2.0, 0.5, 1.5],
            [3.0, 2.0, 0.5],
        ];
        let data: Vec<usize> = (0..4).collect();
        let tree = STRPacking::build_nd(&points.view(), data, 2).unwrap();
        assert_eq!(tree.size(), 4);
    }

    #[test]
    fn test_rtree_iter_range() {
        let mut tree = RTree::new(2, 2, 4).unwrap();
        use scirs2_core::ndarray::Array1;
        let pts: Vec<[f64; 2]> = vec![
            [0.0, 0.0],
            [1.0, 1.0],
            [2.0, 2.0],
            [5.0, 5.0],
        ];
        for (i, p) in pts.iter().enumerate() {
            tree.insert(Array1::from_vec(p.to_vec()), i).unwrap();
        }
        let min = Array1::from_vec(vec![-0.5, -0.5]);
        let max = Array1::from_vec(vec![2.5, 2.5]);
        let results: Vec<_> = tree.iter_range(min, max).unwrap().collect();
        assert_eq!(results.len(), 3);
    }

    #[test]
    fn test_str_compute_bbox() {
        let pts = array![[1.0_f64, 2.0], [3.0, 0.0], [2.0, 5.0]];
        let bb = STRPacking::compute_bbox(&pts.view()).unwrap();
        assert!((bb.min[0] - 1.0).abs() < 1e-12);
        assert!((bb.max[0] - 3.0).abs() < 1e-12);
        assert!((bb.min[1] - 0.0).abs() < 1e-12);
        assert!((bb.max[1] - 5.0).abs() < 1e-12);
    }
}