1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/work/athos/xy/boundingbox.h Tue Mar 23 17:28:47 2004 +0000
1.3 @@ -0,0 +1,92 @@
1.4 +// -*- c++ -*-
1.5 +/**
1.6 +Implementation of a bounding box of plainvectors.
1.7 +
1.8 +*/
1.9 +#ifndef HUGO_BOUNDINGBOX_H
1.10 +#define HUGO_BOUNDINGBOX_H
1.11 +
1.12 +
1.13 +#include <xy.h>
1.14 +
1.15 +namespace hugo {
1.16 +
1.17 + template<typename T>
1.18 + class BoundingBox {
1.19 + xy<T> bottomleft, topright;
1.20 + bool _empty;
1.21 + public:
1.22 +
1.23 + ///Default constructor: an empty bounding box
1.24 + BoundingBox() { _empty = true; }
1.25 +
1.26 + ///Constructing the instance from one point
1.27 + BoundingBox(xy<T> a) { bottomleft=topright=a; _empty = false; }
1.28 +
1.29 + ///Is there any point added
1.30 + bool empty() const {
1.31 + return _empty;
1.32 + }
1.33 +
1.34 + ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
1.35 + xy<T> bottomLeft() const {
1.36 + return bottomleft;
1.37 + };
1.38 +
1.39 + ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
1.40 + xy<T> topRight() const {
1.41 + return topright;
1.42 + };
1.43 +
1.44 + ///Checks whether a point is inside a bounding box
1.45 + bool inside(const xy<T>& u){
1.46 + if (_empty)
1.47 + return false;
1.48 + else{
1.49 + return ((u.x-bottomleft.x)*(topright.x-u.x) >= 0 &&
1.50 + (u.y-bottomleft.y)*(topright.y-u.y) >= 0 );
1.51 + }
1.52 + }
1.53 +
1.54 + ///Increments a bounding box with a point
1.55 + BoundingBox& operator +=(const xy<T>& u){
1.56 + if (_empty){
1.57 + bottomleft=topright=u;
1.58 + _empty = false;
1.59 + }
1.60 + else{
1.61 + if (bottomleft.x > u.x) bottomleft.x = u.x;
1.62 + if (bottomleft.y > u.y) bottomleft.y = u.y;
1.63 + if (topright.x < u.x) topright.x = u.x;
1.64 + if (topright.y < u.y) topright.y = u.y;
1.65 + }
1.66 + return *this;
1.67 + };
1.68 +
1.69 + ///Sums a bounding box and a point
1.70 + BoundingBox operator +(const xy<T>& u){
1.71 + BoundingBox b = *this;
1.72 + return b += u;
1.73 + };
1.74 +
1.75 + ///Increments a bounding box with an other bounding box
1.76 + BoundingBox& operator +=(const BoundingBox &u){
1.77 + if ( !u.empty() ){
1.78 + *this += u.bottomLeft();
1.79 + *this += u.topRight();
1.80 + }
1.81 + return *this;
1.82 + };
1.83 +
1.84 + ///Sums two bounding boxes
1.85 + BoundingBox operator +(const BoundingBox& u){
1.86 + BoundingBox b = *this;
1.87 + return b += u;
1.88 + };
1.89 +
1.90 + };//class BoundingBox
1.91 +
1.92 +
1.93 +} //namespace hugo
1.94 +
1.95 +#endif //HUGO_BOUNDINGBOX_H