| [207] | 1 | // -*- c++ -*- | 
|---|
| [201] | 2 | #ifndef HUGO_XY_H | 
|---|
|  | 3 | #define HUGO_XY_H | 
|---|
|  | 4 |  | 
|---|
|  | 5 | #include <iostream> | 
|---|
|  | 6 |  | 
|---|
| [491] | 7 | ///\ingroup misc | 
|---|
| [249] | 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. | 
|---|
| [458] | 17 | /// | 
|---|
|  | 18 | ///\author Attila Bernath | 
|---|
| [249] | 19 |  | 
|---|
|  | 20 |  | 
|---|
| [431] | 21 | namespace hugo { | 
|---|
|  | 22 |  | 
|---|
|  | 23 | /// \addtogroup misc | 
|---|
|  | 24 | /// @{ | 
|---|
|  | 25 |  | 
|---|
| [458] | 26 | /// A two dimensional vector (plainvector) implementation | 
|---|
| [242] | 27 |  | 
|---|
| [458] | 28 | /// A two dimensional vector (plainvector) implementation | 
|---|
|  | 29 | ///with the usual vector | 
|---|
|  | 30 | /// operators. | 
|---|
|  | 31 | /// | 
|---|
|  | 32 | ///\author Attila Bernath | 
|---|
| [207] | 33 | template<typename T> | 
|---|
|  | 34 | class xy { | 
|---|
| [201] | 35 |  | 
|---|
| [207] | 36 | public: | 
|---|
| [240] | 37 |  | 
|---|
|  | 38 | T x,y; | 
|---|
| [207] | 39 |  | 
|---|
|  | 40 | ///Default constructor: both coordinates become 0 | 
|---|
| [240] | 41 | xy() : x(0), y(0) {} | 
|---|
| [201] | 42 |  | 
|---|
| [240] | 43 | ///Constructing the instance from coordinates | 
|---|
| [514] | 44 | xy(T a, T b) : x(a), y(b) { } | 
|---|
| [201] | 45 |  | 
|---|
|  | 46 |  | 
|---|
| [207] | 47 | ///Gives back the square of the norm of the vector | 
|---|
|  | 48 | T normSquare(){ | 
|---|
| [240] | 49 | return x*x+y*y; | 
|---|
| [207] | 50 | }; | 
|---|
| [201] | 51 |  | 
|---|
| [207] | 52 | ///Increments the left hand side by u | 
|---|
|  | 53 | xy<T>& operator +=(const xy<T>& u){ | 
|---|
| [240] | 54 | x += u.x; | 
|---|
|  | 55 | y += u.y; | 
|---|
| [207] | 56 | return *this; | 
|---|
|  | 57 | }; | 
|---|
| [201] | 58 |  | 
|---|
| [207] | 59 | ///Decrements the left hand side by u | 
|---|
|  | 60 | xy<T>& operator -=(const xy<T>& u){ | 
|---|
| [240] | 61 | x -= u.x; | 
|---|
|  | 62 | y -= u.y; | 
|---|
| [207] | 63 | return *this; | 
|---|
|  | 64 | }; | 
|---|
| [201] | 65 |  | 
|---|
| [207] | 66 | ///Multiplying the left hand side with a scalar | 
|---|
|  | 67 | xy<T>& operator *=(const T &u){ | 
|---|
| [240] | 68 | x *= u; | 
|---|
|  | 69 | y *= u; | 
|---|
| [207] | 70 | return *this; | 
|---|
|  | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 | ///Dividing the left hand side by a scalar | 
|---|
|  | 74 | xy<T>& operator /=(const T &u){ | 
|---|
| [240] | 75 | x /= u; | 
|---|
|  | 76 | y /= u; | 
|---|
| [207] | 77 | return *this; | 
|---|
|  | 78 | }; | 
|---|
| [201] | 79 |  | 
|---|
| [207] | 80 | ///Returns the scalar product of two vectors | 
|---|
|  | 81 | T operator *(const xy<T>& u){ | 
|---|
| [240] | 82 | return x*u.x+y*u.y; | 
|---|
| [207] | 83 | }; | 
|---|
| [201] | 84 |  | 
|---|
| [207] | 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 | }; | 
|---|
| [201] | 90 |  | 
|---|
| [207] | 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 | }; | 
|---|
| [201] | 96 |  | 
|---|
| [207] | 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 | }; | 
|---|
| [201] | 102 |  | 
|---|
| [207] | 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 | }; | 
|---|
| [201] | 108 |  | 
|---|
| [207] | 109 | ///Testing equality | 
|---|
|  | 110 | bool operator==(const xy<T> &u){ | 
|---|
| [240] | 111 | return (x==u.x) && (y==u.y); | 
|---|
| [207] | 112 | }; | 
|---|
| [201] | 113 |  | 
|---|
| [207] | 114 | ///Testing inequality | 
|---|
|  | 115 | bool operator!=(xy u){ | 
|---|
| [240] | 116 | return  (x!=u.x) || (y!=u.y); | 
|---|
| [207] | 117 | }; | 
|---|
| [201] | 118 |  | 
|---|
| [207] | 119 | }; | 
|---|
| [201] | 120 |  | 
|---|
| [814] | 121 | ///Read a plainvector from a stream | 
|---|
|  | 122 |  | 
|---|
|  | 123 | ///\relates xy | 
|---|
|  | 124 | /// | 
|---|
| [207] | 125 | template<typename T> | 
|---|
|  | 126 | inline | 
|---|
|  | 127 | std::istream& operator>>(std::istream &is, xy<T> &z) | 
|---|
|  | 128 | { | 
|---|
| [240] | 129 |  | 
|---|
|  | 130 | is >> z.x >> z.y; | 
|---|
| [207] | 131 | return is; | 
|---|
|  | 132 | } | 
|---|
| [201] | 133 |  | 
|---|
| [814] | 134 | ///Write a plainvector to a stream | 
|---|
|  | 135 |  | 
|---|
|  | 136 | ///\relates xy | 
|---|
|  | 137 | /// | 
|---|
| [207] | 138 | template<typename T> | 
|---|
|  | 139 | inline | 
|---|
|  | 140 | std::ostream& operator<<(std::ostream &os, xy<T> z) | 
|---|
|  | 141 | { | 
|---|
| [240] | 142 | os << "(" << z.x << ", " << z.y << ")"; | 
|---|
| [207] | 143 | return os; | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
| [244] | 146 |  | 
|---|
| [458] | 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 | 
|---|
| [244] | 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 |  | 
|---|
| [431] | 228 | /// @} | 
|---|
| [244] | 229 |  | 
|---|
|  | 230 |  | 
|---|
| [207] | 231 | } //namespace hugo | 
|---|
| [201] | 232 |  | 
|---|
|  | 233 | #endif //HUGO_XY_H | 
|---|