aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/bbox.hh
blob: 565e2648f8ea5188a510295c4f2891b4581ebe0c (plain)
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
// $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/bbox.hh,v 1.11 2003/01/03 15:49:36 schnetter Exp $

#ifndef BBOX_HH
#define BBOX_HH

#include <iostream>

#include "defs.hh"
#include "vect.hh"

using namespace std;



// Forward declaration
template<class T, int D> class bbox;

// Input/Output
template<class T, int D>
istream& operator>> (istream& is, bbox<T,D>& b);
template<class T, int D>
ostream& operator<< (ostream& os, const bbox<T,D>& b);



// Bounding box class
template<class T, int D>
class bbox {
  
  // Fields
  vect<T,D> _lower, _upper, _stride; // bounds are inclusive
  
public:
  
  // Constructors
  bbox ();
  bbox (const bbox& b);
  bbox& operator= (const bbox& b);
  bbox (const vect<T,D>& lower, const vect<T,D>& upper,
	const vect<T,D>& stride);
  
  // Accessors
  // (Don't return references; *this might be a temporary)
  vect<T,D> lower () const { return _lower; }
  vect<T,D> upper () const { return _upper; }
  vect<T,D> stride () const { return _stride; }
  vect<T,D> shape () const { return _upper - _lower + _stride; }
  
  bool empty() const {
    return any(lower()>upper());
  }
  
  T size () const;
  T num_points () const;
  
  // Queries
  bool contains (const vect<T,D>& x) const;
  
  // Operators
  bool operator== (const bbox& b) const;
  bool operator!= (const bbox& b) const;
  bool operator< (const bbox& b) const;
  bool operator> (const bbox& b) const;
  bool operator<= (const bbox& b) const;
  bool operator>= (const bbox& b) const;
  
  // Intersection
  bbox operator& (const bbox& b) const;
  
  // Containment
  bool is_contained_in (const bbox& b) const;
  
  // Alignment check
  bool is_aligned_with (const bbox& b) const;
  
  // Expand the bbox a little by multiples of the stride
  bbox expand (const vect<T,D>& lo, const vect<T,D>& hi) const;
  
  // Find the smallest b-compatible box around *this
  bbox expanded_for (const bbox& b) const;
  
  // Find the largest b-compatible box inside *this
  bbox contracted_for (const bbox& b) const;
  
  // Smallest bbox containing both boxes
  bbox expanded_containing (const bbox<T,D>& b) const;
  
  // Iterators
  class iterator {
  protected:
    const bbox& box;
    vect<T,D> pos;
  public:
    iterator (const bbox& box, const vect<T,D>& pos);
    const vect<T,D>& operator* () const { return pos; }
    bool operator!= (const iterator& i) const;
    iterator& operator++ ();
  };
  
  iterator begin () const;
  iterator end () const;
  
  // Input/Output
  void input (istream& is);
  void output (ostream& os) const;
};



// Input
template<class T,int D>
inline istream& operator>> (istream& is, bbox<T,D>& b) {
  b.input(is);
  return is;
}



// Output
template<class T,int D>
inline ostream& operator<< (ostream& os, const bbox<T,D>& b) {
  b.output(os);
  return os;
}



#endif // BBOX_HH