athos@240: // -*- c++ -*-
athos@240: /**
athos@240: Implementation of a bounding box of plainvectors.
athos@240: 
athos@240: */
athos@240: #ifndef HUGO_BOUNDINGBOX_H
athos@240: #define HUGO_BOUNDINGBOX_H
athos@240: 
athos@240: 
athos@240: #include <xy.h>
athos@240: 
athos@240: namespace hugo {
athos@240: 
athos@240:   template<typename T>
athos@240:     class BoundingBox {
athos@240:       xy<T> bottomleft, topright;
athos@240:       bool _empty;
athos@240:     public:
athos@240:       
athos@240:       ///Default constructor: an empty bounding box
athos@240:       BoundingBox() { _empty = true; }
athos@240: 
athos@240:       ///Constructing the instance from one point
athos@240:       BoundingBox(xy<T> a) { bottomleft=topright=a; _empty = false; }
athos@240: 
athos@240:       ///Is there any point added
athos@240:       bool empty() const {
athos@240: 	return _empty;
athos@240:       }
athos@240: 
athos@240:       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
athos@240:       xy<T> bottomLeft() const {
athos@240: 	return bottomleft;
athos@240:       };
athos@240: 
athos@240:       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
athos@240:       xy<T> topRight() const {
athos@240: 	return topright;
athos@240:       };
athos@240: 
athos@240:       ///Checks whether a point is inside a bounding box
athos@240:       bool inside(const xy<T>& u){
athos@240: 	if (_empty)
athos@240: 	  return false;
athos@240: 	else{
athos@240: 	  return ((u.x-bottomleft.x)*(topright.x-u.x) >= 0 &&
athos@240: 		  (u.y-bottomleft.y)*(topright.y-u.y) >= 0 );
athos@240: 	}
athos@240:       }
athos@240:   
athos@240:       ///Increments a bounding box with a point
athos@240:       BoundingBox& operator +=(const xy<T>& u){
athos@240: 	if (_empty){
athos@240: 	  bottomleft=topright=u;
athos@240: 	  _empty = false;
athos@240: 	}
athos@240: 	else{
athos@240: 	  if (bottomleft.x > u.x) bottomleft.x = u.x;
athos@240: 	  if (bottomleft.y > u.y) bottomleft.y = u.y;
athos@240: 	  if (topright.x < u.x) topright.x = u.x;
athos@240: 	  if (topright.y < u.y) topright.y = u.y;
athos@240: 	}
athos@240: 	return *this;
athos@240:       };
athos@240:   
athos@240:       ///Sums a bounding box and a point
athos@240:       BoundingBox operator +(const xy<T>& u){
athos@240: 	BoundingBox b = *this;
athos@240: 	return b += u;
athos@240:       };
athos@240: 
athos@240:       ///Increments a bounding box with an other bounding box
athos@240:       BoundingBox& operator +=(const BoundingBox &u){
athos@240: 	if ( !u.empty() ){
athos@240: 	  *this += u.bottomLeft();
athos@240: 	  *this += u.topRight();
athos@240: 	}
athos@240: 	return *this;
athos@240:       };
athos@240:   
athos@240:       ///Sums two bounding boxes
athos@240:       BoundingBox operator +(const BoundingBox& u){
athos@240: 	BoundingBox b = *this;
athos@240: 	return b += u;
athos@240:       };
athos@240: 
athos@240:     };//class BoundingBox
athos@240: 
athos@240: 
athos@240: } //namespace hugo
athos@240: 
athos@240: #endif //HUGO_BOUNDINGBOX_H