author | jacint |
Wed, 24 Mar 2004 09:36:21 +0000 | |
changeset 241 | 4acba8684811 |
child 242 | b255f25ad394 |
permissions | -rw-r--r-- |
athos@240 | 1 |
// -*- c++ -*- |
athos@240 | 2 |
/** |
athos@240 | 3 |
Implementation of a bounding box of plainvectors. |
athos@240 | 4 |
|
athos@240 | 5 |
*/ |
athos@240 | 6 |
#ifndef HUGO_BOUNDINGBOX_H |
athos@240 | 7 |
#define HUGO_BOUNDINGBOX_H |
athos@240 | 8 |
|
athos@240 | 9 |
|
athos@240 | 10 |
#include <xy.h> |
athos@240 | 11 |
|
athos@240 | 12 |
namespace hugo { |
athos@240 | 13 |
|
athos@240 | 14 |
template<typename T> |
athos@240 | 15 |
class BoundingBox { |
athos@240 | 16 |
xy<T> bottomleft, topright; |
athos@240 | 17 |
bool _empty; |
athos@240 | 18 |
public: |
athos@240 | 19 |
|
athos@240 | 20 |
///Default constructor: an empty bounding box |
athos@240 | 21 |
BoundingBox() { _empty = true; } |
athos@240 | 22 |
|
athos@240 | 23 |
///Constructing the instance from one point |
athos@240 | 24 |
BoundingBox(xy<T> a) { bottomleft=topright=a; _empty = false; } |
athos@240 | 25 |
|
athos@240 | 26 |
///Is there any point added |
athos@240 | 27 |
bool empty() const { |
athos@240 | 28 |
return _empty; |
athos@240 | 29 |
} |
athos@240 | 30 |
|
athos@240 | 31 |
///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) |
athos@240 | 32 |
xy<T> bottomLeft() const { |
athos@240 | 33 |
return bottomleft; |
athos@240 | 34 |
}; |
athos@240 | 35 |
|
athos@240 | 36 |
///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) |
athos@240 | 37 |
xy<T> topRight() const { |
athos@240 | 38 |
return topright; |
athos@240 | 39 |
}; |
athos@240 | 40 |
|
athos@240 | 41 |
///Checks whether a point is inside a bounding box |
athos@240 | 42 |
bool inside(const xy<T>& u){ |
athos@240 | 43 |
if (_empty) |
athos@240 | 44 |
return false; |
athos@240 | 45 |
else{ |
athos@240 | 46 |
return ((u.x-bottomleft.x)*(topright.x-u.x) >= 0 && |
athos@240 | 47 |
(u.y-bottomleft.y)*(topright.y-u.y) >= 0 ); |
athos@240 | 48 |
} |
athos@240 | 49 |
} |
athos@240 | 50 |
|
athos@240 | 51 |
///Increments a bounding box with a point |
athos@240 | 52 |
BoundingBox& operator +=(const xy<T>& u){ |
athos@240 | 53 |
if (_empty){ |
athos@240 | 54 |
bottomleft=topright=u; |
athos@240 | 55 |
_empty = false; |
athos@240 | 56 |
} |
athos@240 | 57 |
else{ |
athos@240 | 58 |
if (bottomleft.x > u.x) bottomleft.x = u.x; |
athos@240 | 59 |
if (bottomleft.y > u.y) bottomleft.y = u.y; |
athos@240 | 60 |
if (topright.x < u.x) topright.x = u.x; |
athos@240 | 61 |
if (topright.y < u.y) topright.y = u.y; |
athos@240 | 62 |
} |
athos@240 | 63 |
return *this; |
athos@240 | 64 |
}; |
athos@240 | 65 |
|
athos@240 | 66 |
///Sums a bounding box and a point |
athos@240 | 67 |
BoundingBox operator +(const xy<T>& u){ |
athos@240 | 68 |
BoundingBox b = *this; |
athos@240 | 69 |
return b += u; |
athos@240 | 70 |
}; |
athos@240 | 71 |
|
athos@240 | 72 |
///Increments a bounding box with an other bounding box |
athos@240 | 73 |
BoundingBox& operator +=(const BoundingBox &u){ |
athos@240 | 74 |
if ( !u.empty() ){ |
athos@240 | 75 |
*this += u.bottomLeft(); |
athos@240 | 76 |
*this += u.topRight(); |
athos@240 | 77 |
} |
athos@240 | 78 |
return *this; |
athos@240 | 79 |
}; |
athos@240 | 80 |
|
athos@240 | 81 |
///Sums two bounding boxes |
athos@240 | 82 |
BoundingBox operator +(const BoundingBox& u){ |
athos@240 | 83 |
BoundingBox b = *this; |
athos@240 | 84 |
return b += u; |
athos@240 | 85 |
}; |
athos@240 | 86 |
|
athos@240 | 87 |
};//class BoundingBox |
athos@240 | 88 |
|
athos@240 | 89 |
|
athos@240 | 90 |
} //namespace hugo |
athos@240 | 91 |
|
athos@240 | 92 |
#endif //HUGO_BOUNDINGBOX_H |