src/hugo/xy.h
author alpar
Mon, 13 Sep 2004 11:24:35 +0000
changeset 835 eb9587f09b42
parent 539 fb261e3a9a0f
child 906 17f31d280385
permissions -rw-r--r--
Remove one remaining range checking.
     1 // -*- c++ -*-
     2 #ifndef HUGO_XY_H
     3 #define HUGO_XY_H
     4 
     5 #include <iostream>
     6 
     7 ///\ingroup misc
     8 ///\file
     9 ///\brief A simple two dimensional vector and a bounding box implementation 
    10 ///
    11 /// The class \ref hugo::xy "xy" implements
    12 ///a two dimensional vector with the usual
    13 /// operations.
    14 ///
    15 /// The class \ref hugo::BoundingBox "BoundingBox" can be used to determine
    16 /// the rectangular bounding box a set of \ref hugo::xy "xy"'s.
    17 ///
    18 ///\author Attila Bernath
    19 
    20 
    21 namespace hugo {
    22 
    23   /// \addtogroup misc
    24   /// @{
    25 
    26   /// A two dimensional vector (plainvector) implementation
    27 
    28   /// A two dimensional vector (plainvector) implementation
    29   ///with the usual vector
    30   /// operators.
    31   ///
    32   ///\author Attila Bernath
    33   template<typename T>
    34     class xy {
    35 
    36     public:
    37 
    38       T x,y;     
    39       
    40       ///Default constructor: both coordinates become 0
    41       xy() : x(0), y(0) {}
    42 
    43       ///Constructing the instance from coordinates
    44       xy(T a, T b) : x(a), y(b) { }
    45 
    46 
    47       ///Gives back the square of the norm of the vector
    48       T normSquare(){
    49 	return x*x+y*y;
    50       };
    51   
    52       ///Increments the left hand side by u
    53       xy<T>& operator +=(const xy<T>& u){
    54 	x += u.x;
    55 	y += u.y;
    56 	return *this;
    57       };
    58   
    59       ///Decrements the left hand side by u
    60       xy<T>& operator -=(const xy<T>& u){
    61 	x -= u.x;
    62 	y -= u.y;
    63 	return *this;
    64       };
    65 
    66       ///Multiplying the left hand side with a scalar
    67       xy<T>& operator *=(const T &u){
    68 	x *= u;
    69 	y *= u;
    70 	return *this;
    71       };
    72 
    73       ///Dividing the left hand side by a scalar
    74       xy<T>& operator /=(const T &u){
    75 	x /= u;
    76 	y /= u;
    77 	return *this;
    78       };
    79   
    80       ///Returns the scalar product of two vectors
    81       T operator *(const xy<T>& u){
    82 	return x*u.x+y*u.y;
    83       };
    84   
    85       ///Returns the sum of two vectors
    86       xy<T> operator+(const xy<T> &u) const {
    87 	xy<T> b=*this;
    88 	return b+=u;
    89       };
    90 
    91       ///Returns the difference of two vectors
    92       xy<T> operator-(const xy<T> &u) const {
    93 	xy<T> b=*this;
    94 	return b-=u;
    95       };
    96 
    97       ///Returns a vector multiplied by a scalar
    98       xy<T> operator*(const T &u) const {
    99 	xy<T> b=*this;
   100 	return b*=u;
   101       };
   102 
   103       ///Returns a vector divided by a scalar
   104       xy<T> operator/(const T &u) const {
   105 	xy<T> b=*this;
   106 	return b/=u;
   107       };
   108 
   109       ///Testing equality
   110       bool operator==(const xy<T> &u){
   111 	return (x==u.x) && (y==u.y);
   112       };
   113 
   114       ///Testing inequality
   115       bool operator!=(xy u){
   116 	return  (x!=u.x) || (y!=u.y);
   117       };
   118 
   119     };
   120 
   121   ///Read a plainvector from a stream
   122 
   123   ///\relates xy
   124   ///
   125   template<typename T>
   126   inline
   127   std::istream& operator>>(std::istream &is, xy<T> &z)
   128   {
   129 
   130     is >> z.x >> z.y;
   131     return is;
   132   }
   133 
   134   ///Write a plainvector to a stream
   135 
   136   ///\relates xy
   137   ///
   138   template<typename T>
   139   inline
   140   std::ostream& operator<<(std::ostream &os, xy<T> z)
   141   {
   142     os << "(" << z.x << ", " << z.y << ")";
   143     return os;
   144   }
   145 
   146 
   147   /// A class to calculate or store the bounding box of plainvectors.
   148 
   149   /// A class to calculate or store the bounding box of plainvectors.
   150   ///
   151   ///\author Attila Bernath
   152   template<typename T>
   153     class BoundingBox {
   154       xy<T> bottom_left, top_right;
   155       bool _empty;
   156     public:
   157       
   158       ///Default constructor: an empty bounding box
   159       BoundingBox() { _empty = true; }
   160 
   161       ///Constructing the instance from one point
   162       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   163 
   164       ///Is there any point added
   165       bool empty() const {
   166 	return _empty;
   167       }
   168 
   169       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   170       xy<T> bottomLeft() const {
   171 	return bottom_left;
   172       };
   173 
   174       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   175       xy<T> topRight() const {
   176 	return top_right;
   177       };
   178 
   179       ///Checks whether a point is inside a bounding box
   180       bool inside(const xy<T>& u){
   181 	if (_empty)
   182 	  return false;
   183 	else{
   184 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   185 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   186 	}
   187       }
   188   
   189       ///Increments a bounding box with a point
   190       BoundingBox& operator +=(const xy<T>& u){
   191 	if (_empty){
   192 	  bottom_left=top_right=u;
   193 	  _empty = false;
   194 	}
   195 	else{
   196 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   197 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   198 	  if (top_right.x < u.x) top_right.x = u.x;
   199 	  if (top_right.y < u.y) top_right.y = u.y;
   200 	}
   201 	return *this;
   202       };
   203   
   204       ///Sums a bounding box and a point
   205       BoundingBox operator +(const xy<T>& u){
   206 	BoundingBox b = *this;
   207 	return b += u;
   208       };
   209 
   210       ///Increments a bounding box with an other bounding box
   211       BoundingBox& operator +=(const BoundingBox &u){
   212 	if ( !u.empty() ){
   213 	  *this += u.bottomLeft();
   214 	  *this += u.topRight();
   215 	}
   216 	return *this;
   217       };
   218   
   219       ///Sums two bounding boxes
   220       BoundingBox operator +(const BoundingBox& u){
   221 	BoundingBox b = *this;
   222 	return b += u;
   223       };
   224 
   225     };//class Boundingbox
   226 
   227 
   228   /// @}
   229 
   230 
   231 } //namespace hugo
   232 
   233 #endif //HUGO_XY_H