[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/xy.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
[1164] | 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[906] | 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 | |
---|
[921] | 17 | #ifndef LEMON_XY_H |
---|
| 18 | #define LEMON_XY_H |
---|
[201] | 19 | |
---|
| 20 | #include <iostream> |
---|
| 21 | |
---|
[491] | 22 | ///\ingroup misc |
---|
[249] | 23 | ///\file |
---|
| 24 | ///\brief A simple two dimensional vector and a bounding box implementation |
---|
| 25 | /// |
---|
[921] | 26 | /// The class \ref lemon::xy "xy" implements |
---|
[249] | 27 | ///a two dimensional vector with the usual |
---|
| 28 | /// operations. |
---|
| 29 | /// |
---|
[921] | 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. |
---|
[458] | 32 | /// |
---|
| 33 | ///\author Attila Bernath |
---|
[249] | 34 | |
---|
| 35 | |
---|
[921] | 36 | namespace lemon { |
---|
[431] | 37 | |
---|
| 38 | /// \addtogroup misc |
---|
| 39 | /// @{ |
---|
| 40 | |
---|
[1257] | 41 | /// A simple two dimensional vector (plainvector) implementation |
---|
[242] | 42 | |
---|
[1257] | 43 | /// A simple two dimensional vector (plainvector) implementation |
---|
[458] | 44 | ///with the usual vector |
---|
| 45 | /// operators. |
---|
| 46 | /// |
---|
| 47 | ///\author Attila Bernath |
---|
[207] | 48 | template<typename T> |
---|
| 49 | class xy { |
---|
[201] | 50 | |
---|
[207] | 51 | public: |
---|
[240] | 52 | |
---|
[987] | 53 | typedef T Value; |
---|
[964] | 54 | |
---|
[240] | 55 | T x,y; |
---|
[207] | 56 | |
---|
[1257] | 57 | ///Default constructor |
---|
| 58 | xy() {} |
---|
[201] | 59 | |
---|
[240] | 60 | ///Constructing the instance from coordinates |
---|
[514] | 61 | xy(T a, T b) : x(a), y(b) { } |
---|
[201] | 62 | |
---|
| 63 | |
---|
[1049] | 64 | ///Conversion constructor |
---|
| 65 | template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {} |
---|
| 66 | |
---|
[207] | 67 | ///Gives back the square of the norm of the vector |
---|
[1257] | 68 | T normSquare() const { |
---|
[240] | 69 | return x*x+y*y; |
---|
[207] | 70 | }; |
---|
[201] | 71 | |
---|
[207] | 72 | ///Increments the left hand side by u |
---|
[1257] | 73 | xy<T>& operator +=(const xy<T>& u) { |
---|
[240] | 74 | x += u.x; |
---|
| 75 | y += u.y; |
---|
[207] | 76 | return *this; |
---|
| 77 | }; |
---|
[201] | 78 | |
---|
[207] | 79 | ///Decrements the left hand side by u |
---|
[1257] | 80 | xy<T>& operator -=(const xy<T>& u) { |
---|
[240] | 81 | x -= u.x; |
---|
| 82 | y -= u.y; |
---|
[207] | 83 | return *this; |
---|
| 84 | }; |
---|
[201] | 85 | |
---|
[207] | 86 | ///Multiplying the left hand side with a scalar |
---|
[1257] | 87 | xy<T>& operator *=(const T &u) { |
---|
[240] | 88 | x *= u; |
---|
| 89 | y *= u; |
---|
[207] | 90 | return *this; |
---|
| 91 | }; |
---|
| 92 | |
---|
| 93 | ///Dividing the left hand side by a scalar |
---|
[1257] | 94 | xy<T>& operator /=(const T &u) { |
---|
[240] | 95 | x /= u; |
---|
| 96 | y /= u; |
---|
[207] | 97 | return *this; |
---|
| 98 | }; |
---|
[201] | 99 | |
---|
[207] | 100 | ///Returns the scalar product of two vectors |
---|
[1257] | 101 | T operator *(const xy<T>& u) const { |
---|
[240] | 102 | return x*u.x+y*u.y; |
---|
[207] | 103 | }; |
---|
[201] | 104 | |
---|
[207] | 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 | }; |
---|
[201] | 110 | |
---|
[1049] | 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 | |
---|
[207] | 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 | }; |
---|
[201] | 123 | |
---|
[207] | 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 | }; |
---|
[201] | 129 | |
---|
[207] | 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 | }; |
---|
[201] | 135 | |
---|
[207] | 136 | ///Testing equality |
---|
[1257] | 137 | bool operator==(const xy<T> &u) const { |
---|
[240] | 138 | return (x==u.x) && (y==u.y); |
---|
[207] | 139 | }; |
---|
[201] | 140 | |
---|
[207] | 141 | ///Testing inequality |
---|
[1257] | 142 | bool operator!=(xy u) const { |
---|
[240] | 143 | return (x!=u.x) || (y!=u.y); |
---|
[207] | 144 | }; |
---|
[201] | 145 | |
---|
[207] | 146 | }; |
---|
[201] | 147 | |
---|
[1071] | 148 | ///Returns a vector multiplied by a scalar |
---|
[1083] | 149 | |
---|
| 150 | ///Returns a vector multiplied by a scalar |
---|
| 151 | ///\relates xy |
---|
[1071] | 152 | template<typename T> xy<T> operator*(const T &u,const xy<T> &x) { |
---|
| 153 | return x*u; |
---|
| 154 | }; |
---|
| 155 | |
---|
[814] | 156 | ///Read a plainvector from a stream |
---|
| 157 | |
---|
[967] | 158 | ///Read a plainvector from a stream |
---|
[814] | 159 | ///\relates xy |
---|
| 160 | /// |
---|
[207] | 161 | template<typename T> |
---|
| 162 | inline |
---|
| 163 | std::istream& operator>>(std::istream &is, xy<T> &z) |
---|
| 164 | { |
---|
[240] | 165 | |
---|
| 166 | is >> z.x >> z.y; |
---|
[207] | 167 | return is; |
---|
| 168 | } |
---|
[201] | 169 | |
---|
[814] | 170 | ///Write a plainvector to a stream |
---|
| 171 | |
---|
[967] | 172 | ///Write a plainvector to a stream |
---|
[814] | 173 | ///\relates xy |
---|
| 174 | /// |
---|
[207] | 175 | template<typename T> |
---|
| 176 | inline |
---|
| 177 | std::ostream& operator<<(std::ostream &os, xy<T> z) |
---|
| 178 | { |
---|
[240] | 179 | os << "(" << z.x << ", " << z.y << ")"; |
---|
[207] | 180 | return os; |
---|
| 181 | } |
---|
| 182 | |
---|
[1202] | 183 | ///Rotate by 90 degrees |
---|
| 184 | |
---|
| 185 | ///Returns its parameter rotated by 90 degrees in positive direction. |
---|
| 186 | ///\relates xy |
---|
| 187 | /// |
---|
| 188 | template<typename T> |
---|
| 189 | inline xy<T> rot90(const xy<T> &z) |
---|
| 190 | { |
---|
| 191 | return xy<T>(-z.y,z.x); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | ///Rotate by 270 degrees |
---|
| 195 | |
---|
| 196 | ///Returns its parameter rotated by 90 degrees in negative direction. |
---|
| 197 | ///\relates xy |
---|
| 198 | /// |
---|
| 199 | template<typename T> |
---|
| 200 | inline xy<T> rot270(const xy<T> &z) |
---|
| 201 | { |
---|
| 202 | return xy<T>(z.y,-z.x); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | |
---|
[244] | 206 | |
---|
[458] | 207 | /// A class to calculate or store the bounding box of plainvectors. |
---|
| 208 | |
---|
| 209 | /// A class to calculate or store the bounding box of plainvectors. |
---|
| 210 | /// |
---|
| 211 | ///\author Attila Bernath |
---|
[244] | 212 | template<typename T> |
---|
| 213 | class BoundingBox { |
---|
| 214 | xy<T> bottom_left, top_right; |
---|
| 215 | bool _empty; |
---|
| 216 | public: |
---|
| 217 | |
---|
| 218 | ///Default constructor: an empty bounding box |
---|
| 219 | BoundingBox() { _empty = true; } |
---|
| 220 | |
---|
| 221 | ///Constructing the instance from one point |
---|
| 222 | BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; } |
---|
| 223 | |
---|
| 224 | ///Is there any point added |
---|
| 225 | bool empty() const { |
---|
| 226 | return _empty; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) |
---|
| 230 | xy<T> bottomLeft() const { |
---|
| 231 | return bottom_left; |
---|
| 232 | }; |
---|
| 233 | |
---|
| 234 | ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) |
---|
| 235 | xy<T> topRight() const { |
---|
| 236 | return top_right; |
---|
| 237 | }; |
---|
| 238 | |
---|
[1045] | 239 | ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) |
---|
| 240 | xy<T> bottomRight() const { |
---|
| 241 | return xy<T>(top_right.x,bottom_left.y); |
---|
| 242 | }; |
---|
| 243 | |
---|
| 244 | ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) |
---|
| 245 | xy<T> topLeft() const { |
---|
| 246 | return xy<T>(bottom_left.x,top_right.y); |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 250 | T bottom() const { |
---|
| 251 | return bottom_left.y; |
---|
| 252 | }; |
---|
| 253 | |
---|
| 254 | ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 255 | T top() const { |
---|
| 256 | return top_right.y; |
---|
| 257 | }; |
---|
| 258 | |
---|
| 259 | ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 260 | T left() const { |
---|
| 261 | return bottom_left.x; |
---|
| 262 | }; |
---|
| 263 | |
---|
| 264 | ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 265 | T right() const { |
---|
| 266 | return top_right.x; |
---|
| 267 | }; |
---|
| 268 | |
---|
[1102] | 269 | ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 270 | T height() const { |
---|
| 271 | return top_right.y-bottom_left.y; |
---|
| 272 | }; |
---|
| 273 | |
---|
| 274 | ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 275 | T width() const { |
---|
| 276 | return top_right.x-bottom_left.x; |
---|
| 277 | }; |
---|
| 278 | |
---|
[244] | 279 | ///Checks whether a point is inside a bounding box |
---|
| 280 | bool inside(const xy<T>& u){ |
---|
| 281 | if (_empty) |
---|
| 282 | return false; |
---|
| 283 | else{ |
---|
| 284 | return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && |
---|
| 285 | (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); |
---|
| 286 | } |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | ///Increments a bounding box with a point |
---|
| 290 | BoundingBox& operator +=(const xy<T>& u){ |
---|
| 291 | if (_empty){ |
---|
| 292 | bottom_left=top_right=u; |
---|
| 293 | _empty = false; |
---|
| 294 | } |
---|
| 295 | else{ |
---|
| 296 | if (bottom_left.x > u.x) bottom_left.x = u.x; |
---|
| 297 | if (bottom_left.y > u.y) bottom_left.y = u.y; |
---|
| 298 | if (top_right.x < u.x) top_right.x = u.x; |
---|
| 299 | if (top_right.y < u.y) top_right.y = u.y; |
---|
| 300 | } |
---|
| 301 | return *this; |
---|
| 302 | }; |
---|
| 303 | |
---|
| 304 | ///Sums a bounding box and a point |
---|
| 305 | BoundingBox operator +(const xy<T>& u){ |
---|
| 306 | BoundingBox b = *this; |
---|
| 307 | return b += u; |
---|
| 308 | }; |
---|
| 309 | |
---|
| 310 | ///Increments a bounding box with an other bounding box |
---|
| 311 | BoundingBox& operator +=(const BoundingBox &u){ |
---|
| 312 | if ( !u.empty() ){ |
---|
| 313 | *this += u.bottomLeft(); |
---|
| 314 | *this += u.topRight(); |
---|
| 315 | } |
---|
| 316 | return *this; |
---|
| 317 | }; |
---|
| 318 | |
---|
| 319 | ///Sums two bounding boxes |
---|
| 320 | BoundingBox operator +(const BoundingBox& u){ |
---|
| 321 | BoundingBox b = *this; |
---|
| 322 | return b += u; |
---|
| 323 | }; |
---|
| 324 | |
---|
| 325 | };//class Boundingbox |
---|
| 326 | |
---|
| 327 | |
---|
[1317] | 328 | ///Map of x-coordinates of an xy<>-map |
---|
| 329 | |
---|
| 330 | ///\ingroup maps |
---|
| 331 | /// |
---|
| 332 | template<class M> |
---|
| 333 | class XMap |
---|
| 334 | { |
---|
| 335 | M &_map; |
---|
| 336 | public: |
---|
| 337 | typedef typename M::Value::Value Value; |
---|
| 338 | typedef typename M::Key Key; |
---|
| 339 | ///\e |
---|
| 340 | XMap(M &map) : _map(map) {} |
---|
| 341 | Value operator[](Key k) const {return _map[k].x;} |
---|
| 342 | Value set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
---|
| 343 | }; |
---|
| 344 | |
---|
| 345 | ///Returns an \ref XMap class |
---|
| 346 | |
---|
| 347 | ///This function just returns an \ref XMap class. |
---|
| 348 | /// |
---|
| 349 | ///\ingroup maps |
---|
| 350 | ///\relates XMap |
---|
| 351 | template<class M> |
---|
| 352 | inline XMap<M> xMap(M &m) |
---|
| 353 | { |
---|
| 354 | return XMap<M>(m); |
---|
| 355 | } |
---|
| 356 | |
---|
| 357 | ///Constant (read only) version of \ref XMap |
---|
| 358 | |
---|
| 359 | ///\ingroup maps |
---|
| 360 | /// |
---|
| 361 | template<class M> |
---|
| 362 | class ConstXMap |
---|
| 363 | { |
---|
| 364 | const M &_map; |
---|
| 365 | public: |
---|
| 366 | typedef typename M::Value::Value Value; |
---|
| 367 | typedef typename M::Key Key; |
---|
| 368 | ///\e |
---|
| 369 | ConstXMap(const M &map) : _map(map) {} |
---|
| 370 | Value operator[](Key k) const {return _map[k].x;} |
---|
| 371 | }; |
---|
| 372 | |
---|
| 373 | ///Returns a \ref ConstXMap class |
---|
| 374 | |
---|
| 375 | ///This function just returns an \ref ConstXMap class. |
---|
| 376 | /// |
---|
| 377 | ///\ingroup maps |
---|
| 378 | ///\relates ConstXMap |
---|
| 379 | template<class M> |
---|
| 380 | inline ConstXMap<M> xMap(const M &m) |
---|
| 381 | { |
---|
| 382 | return ConstXMap<M>(m); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | ///Map of y-coordinates of an xy<>-map |
---|
| 386 | |
---|
| 387 | ///\ingroup maps |
---|
| 388 | /// |
---|
| 389 | template<class M> |
---|
| 390 | class YMap |
---|
| 391 | { |
---|
| 392 | M &_map; |
---|
| 393 | public: |
---|
| 394 | typedef typename M::Value::Value Value; |
---|
| 395 | typedef typename M::Key Key; |
---|
| 396 | ///\e |
---|
| 397 | YMap(M &map) : _map(map) {} |
---|
| 398 | Value operator[](Key k) const {return _map[k].y;} |
---|
| 399 | Value set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
---|
| 400 | }; |
---|
| 401 | |
---|
| 402 | ///Returns an \ref YMap class |
---|
| 403 | |
---|
| 404 | ///This function just returns an \ref YMap class. |
---|
| 405 | /// |
---|
| 406 | ///\ingroup maps |
---|
| 407 | ///\relates YMap |
---|
| 408 | template<class M> |
---|
| 409 | inline YMap<M> yMap(M &m) |
---|
| 410 | { |
---|
| 411 | return YMap<M>(m); |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | ///Constant (read only) version of \ref YMap |
---|
| 415 | |
---|
| 416 | ///\ingroup maps |
---|
| 417 | /// |
---|
| 418 | template<class M> |
---|
| 419 | class ConstYMap |
---|
| 420 | { |
---|
| 421 | const M &_map; |
---|
| 422 | public: |
---|
| 423 | typedef typename M::Value::Value Value; |
---|
| 424 | typedef typename M::Key Key; |
---|
| 425 | ///\e |
---|
| 426 | ConstYMap(const M &map) : _map(map) {} |
---|
| 427 | Value operator[](Key k) const {return _map[k].y;} |
---|
| 428 | }; |
---|
| 429 | |
---|
| 430 | ///Returns a \ref ConstYMap class |
---|
| 431 | |
---|
| 432 | ///This function just returns an \ref ConstYMap class. |
---|
| 433 | /// |
---|
| 434 | ///\ingroup maps |
---|
| 435 | ///\relates ConstYMap |
---|
| 436 | template<class M> |
---|
| 437 | inline ConstYMap<M> yMap(const M &m) |
---|
| 438 | { |
---|
| 439 | return ConstYMap<M>(m); |
---|
| 440 | } |
---|
| 441 | |
---|
| 442 | |
---|
[431] | 443 | /// @} |
---|
[244] | 444 | |
---|
| 445 | |
---|
[921] | 446 | } //namespace lemon |
---|
[201] | 447 | |
---|
[921] | 448 | #endif //LEMON_XY_H |
---|