aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/bboxset.hh
blob: 23b2dc18c54c74d51261cc255216836d0101711a (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
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
#ifndef BBOXSET_HH
#define BBOXSET_HH

#include <cassert>
#include <iostream>
#include <list>
#include <set>
#include <vector>

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

using namespace std;



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

// template<typename T,int D>
// bboxset<T,D> operator+ (const bbox<T,D>& b1, const bbox<T,D>& b2);
// template<typename T,int D>
// bboxset<T,D> operator+ (const bbox<T,D>& b, const bboxset<T,D>& s);

// template<typename T,int D>
// bboxset<T,D> operator- (const bbox<T,D>& b1, const bbox<T,D>& b2);
// template<typename T,int D>
// bboxset<T,D> operator- (const bbox<T,D>& b, const bboxset<T,D>& s);

// Input
template<typename T,int D>
istream& operator>> (istream& is, bboxset<T,D>& s);

// Output
template<typename T,int D>
ostream& operator<< (ostream& os, const bboxset<T,D>& s);



// Bounding box class
template<typename T, int D>
class bboxset {
  
  // Types
  typedef bbox<T,D> box;
  //S typedef set<box> bset;
  typedef list<box> bset;
  
  // Fields
  bset bs;
  // Invariant:
  // All bboxes have the same stride.
  // No bbox is empty.
  // The bboxes don't overlap.
  
public:
  
  // Constructors
  bboxset ();
  bboxset (const box& b);
  bboxset (const bboxset& s);
  
  bboxset (const list<box>& lb);
  bboxset (const vector<list<box> >& vlb);
  template<typename U>
  bboxset (const vector<U>& vb, const bbox<T,D> U::* const v);
  template<typename U>
  bboxset (const vector<U>& vb, const bboxset U::* const v);
  
  static bboxset poison ();
  
  // Invariant
  bool invariant () const;
  
private:
  // Normalisation
  void normalize ();
  
public:
  // Accessors
  bool empty () const { return bs.empty(); }
  // T size () const;
  typedef typename box::size_type size_type;
  size_type size () const;
  int setsize () const { return bs.size(); }
  
  // Find out whether this bboxset intersects the bbox b
  bool intersects (const box& b) const;
  
  // Add (bboxes that don't overlap)
  bboxset& operator+= (const box& b)
  {
    if (b.empty()) return *this;
    // This is very slow when there are many bboxes
#if 0 && ! defined(CARPET_OPTIMISE)
    // check for overlap
    for (const_iterator bi=begin(); bi!=end(); ++bi) {
      assert (not (*bi).intersects(b));
    }
#endif
    //S bs.insert(b);
    bs.push_back(b);
    assert (invariant());
    return *this;
  }
  
  bboxset& operator+= (const bboxset& s);
  bboxset& add_transfer (bboxset& s);
  bboxset operator+ (const box& b) const;
  bboxset operator+ (const bboxset& s) const;
  
  // Union
  bboxset& operator|= (const box& b);
  bboxset& operator|= (const bboxset& s);
  bboxset operator| (const box& b) const;
  bboxset operator| (const bboxset& s) const;
  
  // Intersection
  bboxset operator& (const box& b) const;
  bboxset operator& (const bboxset& s) const;
  bboxset& operator&= (const box& b);
  bboxset& operator&= (const bboxset& s);
  
  // Difference
  // friend bboxset operator- <T,D>(const box& b1, const box& b2);
  static bboxset minus (const box& b1, const box& b2);
  bboxset operator- (const box& b) const;
  bboxset& operator-= (const box& b)
  {
    *this = *this - b;
    assert (invariant());
    return *this;
  }
  bboxset& operator-= (const bboxset& s);
  bboxset operator- (const bboxset& s) const;
  // friend bboxset operator- <T,D>(const box& b, const bboxset& s);
  static bboxset minus (const box& b, const bboxset& s);
  
  /** Find a bbox containing the whole set.  */
  box container () const;
  /** Find the pseudo-inverse.  */
  bboxset pseudo_inverse (const int n) const;
  
  /** Expand (enlarge) the bboxset by multiples of the stride.  */
  bboxset expand (const vect<T,D>& lo, const vect<T,D>& hi) const;
  bboxset expand (const vect<vect<T,D>,2>& lohi) const
  { return expand (lohi[0], lohi[1]); }
  
  /** Shift the bboxset by multiples of the stride.  */
  bboxset shift (const vect<T,D>& v) const
  { return expand (-v, v); }
  
  /** Expand (enlarge) the bboxset by multiples of a fraction of the
      stride.  */
  bboxset expand (const vect<T,D>& lo, const vect<T,D>& hi,
                  const vect<T,D>& denom) const;
  bboxset expand (const vect<vect<T,D>,2>& lohi, const vect<T,D>& denom) const
  { return expand (lohi[0], lohi[1], denom); }
  
  /** Shift the bboxset by multiples of a fraction of the stride.  */
  bboxset shift (const vect<T,D>& v, const vect<T,D>& denom) const
  { return expand (-v, v, denom); }
  
  /** Find the smallest b-compatible box around this bbox.
      ("compatible" means having the same stride.)  */
  bboxset expanded_for (const box& b) const;
  
  /** Find the largest b-compatible box inside this bbox.  */
  bboxset contracted_for (const box& b) const;
  
  // Equality
  bool operator== (const bboxset& s) const;
  bool operator!= (const bboxset& s) const;
  bool operator< (const bboxset& s) const;
  bool operator<= (const bboxset& s) const;
  bool operator> (const bboxset& s) const;
  bool operator>= (const bboxset& s) const;
  
  // Iterators
  typedef typename bset::const_iterator const_iterator;
  typedef typename bset::iterator       iterator;
  
  const_iterator begin () const { return bs.begin(); }
  const_iterator end () const   { return bs.end(); }
//   iterator begin () const { return bs.begin(); }
//   iterator end () const   { return bs.end(); }
  
  // Memory usage
  size_t memory () const CCTK_ATTRIBUTE_PURE { return memoryof (bs); }
  
  // Input
  istream& input (istream& is);
  
  // Output
  ostream& output (ostream& os) const;
};



template<typename T,int D>
inline bboxset<T,D> operator+ (const bbox<T,D>& b1, const bbox<T,D>& b2) {
  return bboxset<T,D>(b1) + bboxset<T,D>(b2);
}

template<typename T,int D>
inline bboxset<T,D> operator+ (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return bboxset<T,D>(b) + s;
}

template<typename T,int D>
inline bboxset<T,D> operator- (const bbox<T,D>& b1, const bbox<T,D>& b2) {
  return bboxset<T,D>::minus(b1,b2);
}

template<typename T,int D>
inline bboxset<T,D> operator- (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return bboxset<T,D>::minus(b,s);
}



template<typename T,int D>
inline bboxset<T,D> operator| (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return s | b;
}

template<typename T,int D>
inline bboxset<T,D> operator& (const bbox<T,D>& b, const bboxset<T,D>& s) {
  return s & b;
}



template<typename T,int D>
inline bool operator== (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator== (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) == s;
}

template<typename T,int D>
inline bool operator!= (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator!= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) != s;
}

template<typename T,int D>
inline bool operator< (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator< (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) < s;
}

template<typename T,int D>
inline bool operator<= (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator<= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) <= s;
}

template<typename T,int D>
inline bool operator> (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator> (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) > s;
}

template<typename T,int D>
inline bool operator>= (const bbox<T,D>& b, const bboxset<T,D>& s)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator>= (const bbox<T,D>& b, const bboxset<T,D>& s)
{
  return bboxset<T,D>(b) >= s;
}



template<typename T,int D>
inline bool operator== (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator== (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s == bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator!= (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator!= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s != bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator< (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator< (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s < bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator<= (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator<= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s <= bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator> (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator> (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s > bboxset<T,D>(b);
}

template<typename T,int D>
inline bool operator>= (const bboxset<T,D>& s, const bbox<T,D>& b)
  CCTK_ATTRIBUTE_PURE;
template<typename T,int D>
inline bool operator>= (const bboxset<T,D>& s, const bbox<T,D>& b)
{
  return s >= bboxset<T,D>(b);
}



// Memory usage
template<typename T, int D>
inline size_t memoryof (bboxset<T,D> const & s)
  CCTK_ATTRIBUTE_PURE;
template<typename T, int D>
inline size_t memoryof (bboxset<T,D> const & s)
{ return s.memory(); }



// Input
template<typename T,int D>
inline istream& operator>> (istream& is, bboxset<T,D>& s) {
  return s.input(is);
}



// Output
template<typename T,int D>
inline ostream& operator<< (ostream& os, const bboxset<T,D>& s) {
  return s.output(os);
}



#endif // BBOXSET_HH