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