aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/bbox.cc
blob: b4f6523f563e31e5e06fd3b3fc5cf581737c6794 (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
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
#include <cctk.h>
#include <cctk_Parameters.h>

#include <algorithm>
#include <cassert>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
#include <typeinfo>

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

#include "bbox.hh"

using namespace std;



// Consistency checks
template<typename T, int D>
void bbox<T,D>::assert_bbox_limits () const
{
  ASSERT_BBOX (all(_stride>T(0)));
  ASSERT_BBOX (all((_upper-_lower)%_stride == T(0)));
  if (numeric_limits<T>::is_integer) {
    // prevent accidental wrap-around
    if (any (_lower >= numeric_limits<T>::max() / 2) or
        any (_lower <= numeric_limits<T>::min() / 2) or
        any (_upper >= numeric_limits<T>::max() / 2) or
        any (_upper <= numeric_limits<T>::min() / 2))
    {
      ostringstream buf;
      T dummy;
      buf << "Tried to create a very large bbox [" << _lower << "," << _upper << "," << _stride << "] for the type " << typeid(dummy).name() << " -- it is likely that this would lead to an integer overflow";
      CCTK_WARN (CCTK_WARN_ABORT, buf.str().c_str());
    }
  }
}



// Poison
template<typename T, int D>
bbox<T,D> bbox<T,D>::poison ()
{
  DECLARE_CCTK_PARAMETERS;
  
  vect<T,D> const v (deadbeef);
  return bbox (v, v, v);
}

template<typename T, int D>
bool bbox<T,D>::is_poison () const
{
  return D > 0 and equal_to<bbox> () (*this, poison());
}



// Accessors
template<typename T, int D>
typename bbox<T,D>::size_type bbox<T,D>::size () const {
  if (empty()) return 0;
  const vect<T,D> sh(shape()/stride());
#ifndef CARPET_DEBUG
  return prod(vect<size_type,D>(sh));
#else
  size_type sz = 1, max = numeric_limits<size_type>::max();
  for (int d=0; d<D; ++d) {
    if (sh[d] > max) {
      CCTK_VWarn (CCTK_WARN_ABORT, __LINE__, __FILE__, CCTK_THORNSTRING,
                  "size of bbox of type %s is too large -- integer overflow",
                  typeid(*this).name());
    }
    sz *= sh[d];
    max /= sh[d];
  }
  return sz;
#endif
}

// Queries

// Containment
template<typename T, int D>
bool bbox<T,D>::contains (const vect<T,D>& x) const {
  if (empty()) return false;
  // no alignment check
  return all(x>=lower() and x<=upper());
}

template<typename T, int D>
bool bbox<T,D>::is_contained_in (const bbox& b) const {
  if (empty()) return true;
  // no alignment check
  return all(lower()>=b.lower() and upper()<=b.upper());
}

// Intersection
template<typename T, int D>
bool bbox<T,D>::intersects (const bbox& b) const {
  if (empty()) return false;
  if (b.empty()) return false;
  // no alignment check
  return all(upper()>=b.lower() and lower()<=b.upper());
}

// Alignment check
template<typename T, int D>
bool bbox<T,D>::is_aligned_with (const bbox& b) const {
  return all(stride()==b.stride() and (lower()-b.lower()) % stride() == T(0));
}

// Operators
template<typename T, int D>
bool bbox<T,D>::operator== (const bbox& b) const {
  if (empty() and b.empty()) return true;
  ASSERT_BBOX (all(stride()==b.stride()));
  return all(lower()==b.lower() and upper()==b.upper());
}

template<typename T, int D>
bool bbox<T,D>::operator!= (const bbox& b) const {
  return not (*this == b);
}

#if 0
// Introduce an ordering on bboxes
template<typename T, int D>
bool bbox<T,D>::operator< (const bbox& b) const {
  // An arbitraty order: empty boxes come first, then sorted by lower
  // bound, then by upper bound, then by coarseness
  if (b.empty()) return false;
  if (empty()) return true;
  for (int d=D-1; d>=0; --d) {
    if (lower()[d] < b.lower()[d]) return true;
    if (lower()[d] > b.lower()[d]) return false;
  }
  for (int d=D-1; d>=0; --d) {
    if (upper()[d] < b.upper()[d]) return true;
    if (upper()[d] > b.upper()[d]) return false;
  }
  for (int d=D-1; d>=0; --d) {
    if (stride()[d] > b.stride()[d]) return true;
    if (stride()[d] < b.stride()[d]) return false;
  }
  return false;
}
#endif

template<typename T, int D>
bool bbox<T,D>::operator<= (const bbox& b) const {
  return is_contained_in (b);
}

template<typename T, int D>
bool bbox<T,D>::operator>= (const bbox& b) const {
  return b <= *this;
}

template<typename T, int D>
bool bbox<T,D>::operator< (const bbox& b) const {
  return *this <= b and *this != b;
}

template<typename T, int D>
bool bbox<T,D>::operator> (const bbox& b) const {
  return b < *this;
}

// Expand the bbox a little by multiples of the stride
template<typename T, int D>
bbox<T,D> bbox<T,D>::expand (const vect<T,D>& lo, const vect<T,D>& hi) const {
  // Allow expansion only into directions where the extent is not negative
  // ASSERT_BBOX (all(lower()<=upper() or (lo==T(0) and hi==T(0))));
  ASSERT_BBOX (all(shape()>=vect<T,D>(0) or (lo==T(0) and hi==T(0))));
  const vect<T,D> str = stride();
  const vect<T,D> lb = lower() - lo * str;
  const vect<T,D> ub = upper() + hi * str;
  return bbox(lb,ub,str);
}

// Expand the bbox a little by multiples of a fraction of the stride
template<typename T, int D>
bbox<T,D> bbox<T,D>::expand (const vect<T,D>& lo, const vect<T,D>& hi,
                             const vect<T,D>& denom) const
{
  // Allow expansion only into directions where the extent is not negative
  // ASSERT_BBOX (all(lower()<=upper() or (lo==T(0) and hi==T(0))));
  ASSERT_BBOX (all(shape()>=vect<T,D>(0) or (lo==T(0) and hi==T(0))));
  ASSERT_BBOX (all(denom>vect<T,D>(0)));
  const vect<T,D> str = stride();
  ASSERT_BBOX (all (str%denom==vect<T,D>(0) or (lo==T(0) and hi==T(0))));
  const vect<T,D> lb = lower() - lo * str / denom;
  const vect<T,D> ub = upper() + hi * str / denom;
  return bbox(lb,ub,str);
}

// Find the smallest b-compatible box around *this
template<typename T, int D>
bbox<T,D> bbox<T,D>::expanded_for (const bbox& b) const {
  if (empty()) return bbox(b.lower(), b.lower()-b.stride(), b.stride());
  const vect<T,D> str = b.stride();
  const vect<T,D> loff = imod (lower() - b.lower(), str);
  const vect<T,D> uoff = imod (upper() - b.lower(), str);
  const vect<T,D> lo = lower() - loff; // go outwards
  const vect<T,D> up = upper() + (str - uoff) % str;
  return bbox(lo,up,str);
}

// Find the largest b-compatible box inside *this
template<typename T, int D>
bbox<T,D> bbox<T,D>::contracted_for (const bbox& b) const {
  if (empty()) return bbox(b.lower(), b.lower()-b.stride(), b.stride());
  const vect<T,D> str = b.stride();
  const vect<T,D> loff = imod (lower() - b.lower(), str);
  const vect<T,D> uoff = imod (upper() - b.lower(), str);
  const vect<T,D> lo = lower() + (str - loff) % str; // go inwards
  const vect<T,D> up = upper() - uoff;
  return bbox(lo,up,str);
}

// Find the smallest open b-compatible box around *this:
// This routine is similar to expanded_for. However, it returns a box
// that is possibly larger than the box returned by expanded_for.
template<typename T, int D>
bbox<T,D> bbox<T,D>::anti_contracted_for (const bbox& b) const {
  if (empty()) return bbox(b.lower(), b.lower()-b.stride(), b.stride());
  return expand(1, 1).expanded_for(b).expand(-1, -1);
  // if (all(stride() <= b.stride())) {
  //   // New stride is larger or equal to current stride: forward call
  //   // to expanded_for
  //   return expanded_for(b);
  // }
  // if (all(stride() > b.stride())) {
  //   // New stride is smaller than current stride: expand box by one
  //   // stride, call expanded_for, and shrink box by on stride again
  //   return expand(1, 1).expanded_for(b).expand(-1, -1);
  // }
  // CCTK_BUILTIN_UNREACHABLE();
}

// Smallest bbox containing both boxes
template<typename T, int D>
bbox<T,D> bbox<T,D>::expanded_containing (const bbox& b) const {
  if (empty()) return b;
  if (b.empty()) return *this;
  ASSERT_BBOX (is_aligned_with(b));
  const vect<T,D> lo = min(lower(), b.lower());
  const vect<T,D> up = max(upper(), b.upper());
  const vect<T,D> str = min(stride(), b.stride());
  return bbox(lo,up,str);
}

// Iterators
template<typename T, int D>
bbox<T,D>::iterator::iterator (const bbox& box_, const vect<T,D>& pos_)
  : box(box_), pos(pos_) {
  if (box.empty()) pos=box.upper();
}

template<typename T, int D>
bool bbox<T,D>::iterator::operator!= (const iterator& i) const {
  return any(pos!=i.pos);
}

template<typename T, int D>
typename bbox<T,D>::iterator& bbox<T,D>::iterator::operator++ () {
  for (int d=0; d<D; ++d) {
    pos[d]+=box.stride()[d];
    if (pos[d]<=box.upper()[d]) break;
    pos[d]=box.lower()[d];
  }
  return *this;
}

template<typename T, int D>
typename bbox<T,D>::iterator bbox<T,D>::begin () const {
  return iterator(*this, lower());
}

template<typename T, int D>
typename bbox<T,D>::iterator bbox<T,D>::end () const {
  return iterator(*this, lower());
}



// Input
template<typename T,int D>
void bbox<T,D>::input (istream& is) {
  try {
    skipws (is);
    consume (is, '(');
    is >> _lower;
    skipws (is);
    consume (is, ':');
    is >> _upper;
    skipws (is);
    consume (is, ':');
    is >> _stride;
    skipws (is);
    if (is.peek() == '/') {
      consume (is, '/');
      vect<T,D> lower_dummy;
      is >> lower_dummy;
      skipws (is);
      consume (is, ':');
      vect<T,D> upper_dummy;
      is >> upper_dummy;
      skipws (is);
      consume (is, '/');
      vect<T,D> shape_dummy;
      is >> shape_dummy;
      skipws (is);
      consume (is, '/');
      size_type size_dummy;
      is >> size_dummy;
      ASSERT_BBOX (is.good());
      skipws (is);
    }
    consume (is, ')');
  } catch (input_error &err) {
    T Tdummy;
    cout << "Input error while reading a bbox<" << typestring(Tdummy) << "," << D << ">" << endl;
    throw err;
  }
  if (any(_stride<=T(0))) {
    cout << "While reading the bbox " << *this << ":" << endl
         << "   The stride is not positive." << endl;
    throw input_error();
  }
  if (any((_upper-_lower)%_stride != T(0))) {
    cout << "While reading the bbox " << *this << ":" << endl
         << "   The stride does not evenly divide the extent." << endl;
    throw input_error();
  }
  ASSERT_BBOX (all(_stride>T(0)));
  ASSERT_BBOX (all((_upper-_lower)%_stride == T(0)));
}



// Output
template<typename T,int D>
void bbox<T,D>::output (ostream& os) const {
  os << "(" << lower() << ":" << upper() << ":" << stride()
     << "/" << idiv(lower(), stride()) << ":" << idiv(upper(), stride())
     << "/" << shape() / stride()
     << "/" << size() << ")";
}



// Comparison

namespace std {
  // ==
  template<typename T, int D>
  bool equal_to<bbox<T,D> >::operator()(const bbox<T,D>& x, const bbox<T,D>& y)
    const
  {
    if (x.empty() and y.empty()) return true;
    if (x.empty() or y.empty()) return false;
    /*const*/ equal_to<vect<T,D> > vect_equal_to;
    if (not vect_equal_to(x.stride(), y.stride())) return false;
    if (not vect_equal_to(x.lower(), y.lower())) return false;
    if (not vect_equal_to(x.upper(), y.upper())) return false;
    return true;
  }
  
  // <
  template<typename T, int D>
  bool less<bbox<T,D> >::operator()(const bbox<T,D>& x, const bbox<T,D>& y)
    const
  {
    // Empty bboxes compare less than any non-empty bbox
    if (y.empty()) return false;
    if (x.empty()) return true;
    /*const*/ less<vect<T,D> > vect_less;
    if (vect_less(x.stride(), y.stride())) return true;
    if (vect_less(y.stride(), x.stride())) return false;
    if (vect_less(x.lower(), y.lower())) return true;
    if (vect_less(y.lower(), x.lower())) return false;
    if (vect_less(x.upper(), y.upper())) return true;
    if (vect_less(y.upper(), x.upper())) return false;
    return false;
  }
}



// Note: We need all dimensions all the time.
template class bbox<int,0>;
template class bbox<int,1>;
template class bbox<int,2>;
template class bbox<int,3>;
template class bbox<int,4>;
template class bbox<CCTK_REAL,dim>;

namespace std {
  template struct less<bbox<int,0> >;
  template struct less<bbox<int,1> >;
  template struct less<bbox<int,2> >;
  template struct less<bbox<int,3> >;
  template struct less<bbox<int,4> >;
}