src/lemon/xy.h
author alpar
Tue, 11 Jan 2005 09:04:08 +0000
changeset 1069 7b81a36809c6
parent 1045 1bf336c63f25
child 1071 7c70fc1b2d8b
permissions -rw-r--r--
- Minor correction in time_measure.h
- A bit more meaningful test in time_measure_test.cc
     1 /* -*- C++ -*-
     2  * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Combinatorial Optimization Research Group, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_XY_H
    18 #define LEMON_XY_H
    19 
    20 #include <iostream>
    21 
    22 ///\ingroup misc
    23 ///\file
    24 ///\brief A simple two dimensional vector and a bounding box implementation 
    25 ///
    26 /// The class \ref lemon::xy "xy" implements
    27 ///a two dimensional vector with the usual
    28 /// operations.
    29 ///
    30 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
    31 /// the rectangular bounding box a set of \ref lemon::xy "xy"'s.
    32 ///
    33 ///\author Attila Bernath
    34 
    35 
    36 namespace lemon {
    37 
    38   /// \addtogroup misc
    39   /// @{
    40 
    41   /// A two dimensional vector (plainvector) implementation
    42 
    43   /// A two dimensional vector (plainvector) implementation
    44   ///with the usual vector
    45   /// operators.
    46   ///
    47   ///\author Attila Bernath
    48   template<typename T>
    49     class xy {
    50 
    51     public:
    52 
    53       typedef T Value;
    54 
    55       T x,y;     
    56       
    57       ///Default constructor: both coordinates become 0
    58       xy() : x(0), y(0) {}
    59 
    60       ///Constructing the instance from coordinates
    61       xy(T a, T b) : x(a), y(b) { }
    62 
    63 
    64       ///Conversion constructor
    65       template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
    66 
    67       ///Gives back the square of the norm of the vector
    68       T normSquare(){
    69 	return x*x+y*y;
    70       };
    71   
    72       ///Increments the left hand side by u
    73       xy<T>& operator +=(const xy<T>& u){
    74 	x += u.x;
    75 	y += u.y;
    76 	return *this;
    77       };
    78   
    79       ///Decrements the left hand side by u
    80       xy<T>& operator -=(const xy<T>& u){
    81 	x -= u.x;
    82 	y -= u.y;
    83 	return *this;
    84       };
    85 
    86       ///Multiplying the left hand side with a scalar
    87       xy<T>& operator *=(const T &u){
    88 	x *= u;
    89 	y *= u;
    90 	return *this;
    91       };
    92 
    93       ///Dividing the left hand side by a scalar
    94       xy<T>& operator /=(const T &u){
    95 	x /= u;
    96 	y /= u;
    97 	return *this;
    98       };
    99   
   100       ///Returns the scalar product of two vectors
   101       T operator *(const xy<T>& u){
   102 	return x*u.x+y*u.y;
   103       };
   104   
   105       ///Returns the sum of two vectors
   106       xy<T> operator+(const xy<T> &u) const {
   107 	xy<T> b=*this;
   108 	return b+=u;
   109       };
   110 
   111       ///Returns the neg of the vectors
   112       xy<T> operator-() const {
   113 	xy<T> b=*this;
   114 	b.x=-b.x; b.y=-b.y;
   115 	return b;
   116       };
   117 
   118       ///Returns the difference of two vectors
   119       xy<T> operator-(const xy<T> &u) const {
   120 	xy<T> b=*this;
   121 	return b-=u;
   122       };
   123 
   124       ///Returns a vector multiplied by a scalar
   125       xy<T> operator*(const T &u) const {
   126 	xy<T> b=*this;
   127 	return b*=u;
   128       };
   129 
   130       ///Returns a vector divided by a scalar
   131       xy<T> operator/(const T &u) const {
   132 	xy<T> b=*this;
   133 	return b/=u;
   134       };
   135 
   136       ///Testing equality
   137       bool operator==(const xy<T> &u){
   138 	return (x==u.x) && (y==u.y);
   139       };
   140 
   141       ///Testing inequality
   142       bool operator!=(xy u){
   143 	return  (x!=u.x) || (y!=u.y);
   144       };
   145 
   146     };
   147 
   148   ///Read a plainvector from a stream
   149 
   150   ///Read a plainvector from a stream
   151   ///\relates xy
   152   ///
   153   template<typename T>
   154   inline
   155   std::istream& operator>>(std::istream &is, xy<T> &z)
   156   {
   157 
   158     is >> z.x >> z.y;
   159     return is;
   160   }
   161 
   162   ///Write a plainvector to a stream
   163 
   164   ///Write a plainvector to a stream
   165   ///\relates xy
   166   ///
   167   template<typename T>
   168   inline
   169   std::ostream& operator<<(std::ostream &os, xy<T> z)
   170   {
   171     os << "(" << z.x << ", " << z.y << ")";
   172     return os;
   173   }
   174 
   175 
   176   /// A class to calculate or store the bounding box of plainvectors.
   177 
   178   /// A class to calculate or store the bounding box of plainvectors.
   179   ///
   180   ///\author Attila Bernath
   181   template<typename T>
   182     class BoundingBox {
   183       xy<T> bottom_left, top_right;
   184       bool _empty;
   185     public:
   186       
   187       ///Default constructor: an empty bounding box
   188       BoundingBox() { _empty = true; }
   189 
   190       ///Constructing the instance from one point
   191       BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
   192 
   193       ///Is there any point added
   194       bool empty() const {
   195 	return _empty;
   196       }
   197 
   198       ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) 
   199       xy<T> bottomLeft() const {
   200 	return bottom_left;
   201       };
   202 
   203       ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) 
   204       xy<T> topRight() const {
   205 	return top_right;
   206       };
   207 
   208       ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) 
   209       xy<T> bottomRight() const {
   210 	return xy<T>(top_right.x,bottom_left.y);
   211       };
   212 
   213       ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) 
   214       xy<T> topLeft() const {
   215 	return xy<T>(bottom_left.x,top_right.y);
   216       };
   217 
   218       ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) 
   219       T bottom() const {
   220 	return bottom_left.y;
   221       };
   222 
   223       ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) 
   224       T top() const {
   225 	return top_right.y;
   226       };
   227 
   228       ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) 
   229       T left() const {
   230 	return bottom_left.x;
   231       };
   232 
   233       ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) 
   234       T right() const {
   235 	return top_right.x;
   236       };
   237 
   238       ///Checks whether a point is inside a bounding box
   239       bool inside(const xy<T>& u){
   240 	if (_empty)
   241 	  return false;
   242 	else{
   243 	  return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
   244 		  (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
   245 	}
   246       }
   247   
   248       ///Increments a bounding box with a point
   249       BoundingBox& operator +=(const xy<T>& u){
   250 	if (_empty){
   251 	  bottom_left=top_right=u;
   252 	  _empty = false;
   253 	}
   254 	else{
   255 	  if (bottom_left.x > u.x) bottom_left.x = u.x;
   256 	  if (bottom_left.y > u.y) bottom_left.y = u.y;
   257 	  if (top_right.x < u.x) top_right.x = u.x;
   258 	  if (top_right.y < u.y) top_right.y = u.y;
   259 	}
   260 	return *this;
   261       };
   262   
   263       ///Sums a bounding box and a point
   264       BoundingBox operator +(const xy<T>& u){
   265 	BoundingBox b = *this;
   266 	return b += u;
   267       };
   268 
   269       ///Increments a bounding box with an other bounding box
   270       BoundingBox& operator +=(const BoundingBox &u){
   271 	if ( !u.empty() ){
   272 	  *this += u.bottomLeft();
   273 	  *this += u.topRight();
   274 	}
   275 	return *this;
   276       };
   277   
   278       ///Sums two bounding boxes
   279       BoundingBox operator +(const BoundingBox& u){
   280 	BoundingBox b = *this;
   281 	return b += u;
   282       };
   283 
   284     };//class Boundingbox
   285 
   286 
   287   /// @}
   288 
   289 
   290 } //namespace lemon
   291 
   292 #endif //LEMON_XY_H