39 #include "metaprogramming.hxx" 40 #include "numerictraits.hxx" 41 #include "tinyvector.hxx" 48 template<
class VALUETYPE>
49 struct EndInsidePolicy
51 static inline bool isEmptyRange(VALUETYPE b, VALUETYPE e)
56 static inline VALUETYPE pointEnd(VALUETYPE p)
63 template<
class VALUETYPE>
64 struct EndOutsidePolicy
66 static inline bool isEmptyRange(VALUETYPE b, VALUETYPE e)
71 static inline VALUETYPE pointEnd(VALUETYPE p)
87 template<
class VALUETYPE,
unsigned int DIMENSION>
97 typedef typename NumericTraits<VALUETYPE>::Promote
VolumeType;
103 enum { Dimension = DIMENSION };
110 typedef typename If<typename NumericTraits<VALUETYPE>::isIntegral,
111 detail::EndOutsidePolicy<VALUETYPE>,
120 : begin_(NumericTraits<Vector>::one())
128 Box(Vector
const &begin, Vector
const &end)
129 : begin_(begin), end_(end)
135 explicit Box(Vector
const &size)
163 Vector
const &
end()
const 200 end_ += newBegin - begin_;
222 VolumeType result(end_[0] - begin_[0]);
223 for(
unsigned int i = 1; i < DIMENSION; ++i)
224 result *= end_[i] - begin_[i];
234 return end_ - begin_;
242 end_ = begin_ + size;
262 for(
unsigned int i = 0; i < DIMENSION; ++i)
264 begin_[i] -= borderWidth;
265 end_[i] += borderWidth;
276 begin_ -= borderWidth;
285 return (begin_ == r.begin_) && (end_ == r.end_);
291 return (begin_ != r.begin_) || (end_ != r.end_);
304 for(
unsigned int i = 0; i < DIMENSION; ++i)
305 if(RangePolicy::isEmptyRange(begin_[i], end_[i]))
316 for(
unsigned int i = 0; i < DIMENSION; ++i)
317 if((p[i] < begin_[i]) ||
318 RangePolicy::isEmptyRange(p[i], end_[i]))
333 if(!contains(r.begin_))
335 for(
unsigned int i = 0; i < DIMENSION; ++i)
336 if(r.end_[i] > end_[i])
350 for(
unsigned int i = 0; i < DIMENSION; ++i)
351 if(RangePolicy::isEmptyRange(r.begin_[i], end_[i]) ||
352 RangePolicy::isEmptyRange(begin_[i], r.end_[i]))
367 for(
unsigned int i = 0; i < DIMENSION; ++i)
368 end_[i] = RangePolicy::pointEnd(p[i]);
372 for(
unsigned int i = 0; i < DIMENSION; ++i)
376 if(RangePolicy::isEmptyRange(p[i], end_[i]))
377 end_[i] = RangePolicy::pointEnd(p[i]);
404 return this->
operator=(r);
406 for(
unsigned int i = 0; i < DIMENSION; ++i)
408 if(r.begin_[i] < begin_[i])
409 begin_[i] = r.begin_[i];
410 if(end_[i] < r.end_[i])
437 return this->
operator=(r);
439 for(
unsigned int i = 0; i < DIMENSION; ++i)
441 if(begin_[i] < r.begin_[i])
442 begin_[i] = r.begin_[i];
443 if(r.end_[i] < end_[i])
554 template<
class VALUETYPE,
unsigned int DIMENSION>
555 std::ostream& operator<< (std::ostream& stream, const Box<VALUETYPE, DIMENSION> & box) {
556 stream<<
"["<<box.
begin()<<
", "<<box.
end()<<
" ]";
564 #endif // VIGRA_BOX_HXX void moveBy(Vector const &offset)
Definition: box.hxx:207
Box operator &(Box const &r) const
Definition: box.hxx:454
Box(Vector const &size)
Definition: box.hxx:135
Box & operator/=(double scale)
Definition: box.hxx:489
bool contains(Vector const &p) const
Definition: box.hxx:314
Vector & end()
Definition: box.hxx:174
void setSize(Vector const &size)
Definition: box.hxx:240
Box operator|(Vector const &p) const
Definition: box.hxx:388
Box & operator|=(Vector const &p)
Definition: box.hxx:362
Box & operator*=(double scale)
Definition: box.hxx:466
bool intersects(Box const &r) const
Definition: box.hxx:346
Box operator+(const Vector &offset) const
Definition: box.hxx:523
bool isEmpty() const
Definition: box.hxx:302
VALUETYPE value_type
Definition: box.hxx:93
void moveTo(Vector const &newBegin)
Definition: box.hxx:198
NumericTraits< VALUETYPE >::Promote VolumeType
Definition: box.hxx:97
iterator end()
Definition: tinyvector.hxx:864
Vector & begin()
Definition: box.hxx:152
Box()
Definition: box.hxx:119
Box operator|(Box const &r) const
Definition: box.hxx:420
Box operator*(double scale) const
Definition: box.hxx:477
Box & operator-=(const Vector &offset)
Definition: box.hxx:535
bool contains(Box const &r) const
Definition: box.hxx:329
Box & operator+=(const Vector &offset)
Definition: box.hxx:512
void addBorder(const Vector &borderWidth)
Definition: box.hxx:274
Represent an n-dimensional box as a (begin, end) pair. Depending on the value type, end() is considered to be outside the box (as in the STL, for integer types), or inside (for floating point types). size() will always be end() - begin().
Definition: box.hxx:88
TinyVector< VALUETYPE, DIMENSION > Vector
Definition: box.hxx:101
Vector const & end() const
Definition: box.hxx:163
void setEnd(Vector const &end)
Definition: box.hxx:190
Box & operator &=(Box const &r)
Definition: box.hxx:432
Box(Vector const &begin, Vector const &end)
Definition: box.hxx:128
void addSize(Vector const &offset)
Definition: box.hxx:250
Box operator-(const Vector &offset) const
Definition: box.hxx:546
If< typename NumericTraits< VALUETYPE >::isIntegral, detail::EndOutsidePolicy< VALUETYPE >, detail::EndInsidePolicy< VALUETYPE > >::type RangePolicy
Definition: box.hxx:112
VolumeType volume() const
Definition: box.hxx:217
bool operator!=(Box const &r) const
inequality check
Definition: box.hxx:289
void setBegin(Vector const &begin)
Definition: box.hxx:182
Vector size() const
Definition: box.hxx:232
Vector const & begin() const
Definition: box.hxx:143
void addBorder(VALUETYPE borderWidth)
Definition: box.hxx:260
Box & operator|=(Box const &r)
Definition: box.hxx:399
Box operator/(double scale) const
Definition: box.hxx:500
bool operator==(Box const &r) const
equality check
Definition: box.hxx:283