[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 |
---|
[1359] | 5 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[906] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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; |
---|
[1391] | 91 | } |
---|
[207] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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; |
---|
[1391] | 116 | } |
---|
[1049] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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; |
---|
[1391] | 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); |
---|
[1391] | 139 | } |
---|
[201] | 140 | |
---|
[207] | 141 | ///Testing inequality |
---|
[1257] | 142 | bool operator!=(xy u) const { |
---|
[240] | 143 | return (x!=u.x) || (y!=u.y); |
---|
[1391] | 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; |
---|
[1391] | 154 | } |
---|
[1071] | 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> |
---|
[1392] | 162 | inline std::istream& operator>>(std::istream &is, xy<T> &z) { |
---|
| 163 | char c; |
---|
| 164 | if (is >> c) { |
---|
| 165 | if (c != '(') is.putback(c); |
---|
| 166 | } else { |
---|
| 167 | is.clear(); |
---|
| 168 | } |
---|
| 169 | if (!(is >> z.x)) return is; |
---|
| 170 | if (is >> c) { |
---|
| 171 | if (c != ',') is.putback(c); |
---|
| 172 | } else { |
---|
| 173 | is.clear(); |
---|
| 174 | } |
---|
| 175 | if (!(is >> z.y)) return is; |
---|
| 176 | if (is >> c) { |
---|
| 177 | if (c != ')') is.putback(c); |
---|
| 178 | } else { |
---|
| 179 | is.clear(); |
---|
| 180 | } |
---|
[207] | 181 | return is; |
---|
| 182 | } |
---|
[201] | 183 | |
---|
[814] | 184 | ///Write a plainvector to a stream |
---|
| 185 | |
---|
[967] | 186 | ///Write a plainvector to a stream |
---|
[814] | 187 | ///\relates xy |
---|
| 188 | /// |
---|
[207] | 189 | template<typename T> |
---|
[1392] | 190 | inline std::ostream& operator<<(std::ostream &os, const xy<T>& z) |
---|
[207] | 191 | { |
---|
[240] | 192 | os << "(" << z.x << ", " << z.y << ")"; |
---|
[207] | 193 | return os; |
---|
| 194 | } |
---|
| 195 | |
---|
[1202] | 196 | ///Rotate by 90 degrees |
---|
| 197 | |
---|
| 198 | ///Returns its parameter rotated by 90 degrees in positive direction. |
---|
| 199 | ///\relates xy |
---|
| 200 | /// |
---|
| 201 | template<typename T> |
---|
| 202 | inline xy<T> rot90(const xy<T> &z) |
---|
| 203 | { |
---|
| 204 | return xy<T>(-z.y,z.x); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | ///Rotate by 270 degrees |
---|
| 208 | |
---|
| 209 | ///Returns its parameter rotated by 90 degrees in negative direction. |
---|
| 210 | ///\relates xy |
---|
| 211 | /// |
---|
| 212 | template<typename T> |
---|
| 213 | inline xy<T> rot270(const xy<T> &z) |
---|
| 214 | { |
---|
| 215 | return xy<T>(z.y,-z.x); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | |
---|
[244] | 219 | |
---|
[458] | 220 | /// A class to calculate or store the bounding box of plainvectors. |
---|
| 221 | |
---|
| 222 | /// A class to calculate or store the bounding box of plainvectors. |
---|
| 223 | /// |
---|
| 224 | ///\author Attila Bernath |
---|
[244] | 225 | template<typename T> |
---|
| 226 | class BoundingBox { |
---|
| 227 | xy<T> bottom_left, top_right; |
---|
| 228 | bool _empty; |
---|
| 229 | public: |
---|
| 230 | |
---|
| 231 | ///Default constructor: an empty bounding box |
---|
| 232 | BoundingBox() { _empty = true; } |
---|
| 233 | |
---|
| 234 | ///Constructing the instance from one point |
---|
| 235 | BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; } |
---|
| 236 | |
---|
| 237 | ///Is there any point added |
---|
| 238 | bool empty() const { |
---|
| 239 | return _empty; |
---|
| 240 | } |
---|
| 241 | |
---|
[1391] | 242 | ///Makes the BoundingBox empty |
---|
| 243 | void clear() { |
---|
| 244 | _empty=1; |
---|
| 245 | } |
---|
| 246 | |
---|
[244] | 247 | ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined) |
---|
| 248 | xy<T> bottomLeft() const { |
---|
| 249 | return bottom_left; |
---|
[1391] | 250 | } |
---|
[244] | 251 | |
---|
| 252 | ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined) |
---|
| 253 | xy<T> topRight() const { |
---|
| 254 | return top_right; |
---|
[1391] | 255 | } |
---|
[244] | 256 | |
---|
[1045] | 257 | ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined) |
---|
| 258 | xy<T> bottomRight() const { |
---|
| 259 | return xy<T>(top_right.x,bottom_left.y); |
---|
[1391] | 260 | } |
---|
[1045] | 261 | |
---|
| 262 | ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined) |
---|
| 263 | xy<T> topLeft() const { |
---|
| 264 | return xy<T>(bottom_left.x,top_right.y); |
---|
[1391] | 265 | } |
---|
[1045] | 266 | |
---|
| 267 | ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 268 | T bottom() const { |
---|
| 269 | return bottom_left.y; |
---|
[1391] | 270 | } |
---|
[1045] | 271 | |
---|
| 272 | ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 273 | T top() const { |
---|
| 274 | return top_right.y; |
---|
[1391] | 275 | } |
---|
[1045] | 276 | |
---|
| 277 | ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 278 | T left() const { |
---|
| 279 | return bottom_left.x; |
---|
[1391] | 280 | } |
---|
[1045] | 281 | |
---|
| 282 | ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 283 | T right() const { |
---|
| 284 | return top_right.x; |
---|
[1391] | 285 | } |
---|
[1045] | 286 | |
---|
[1102] | 287 | ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 288 | T height() const { |
---|
| 289 | return top_right.y-bottom_left.y; |
---|
[1391] | 290 | } |
---|
[1102] | 291 | |
---|
| 292 | ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined) |
---|
| 293 | T width() const { |
---|
| 294 | return top_right.x-bottom_left.x; |
---|
[1391] | 295 | } |
---|
[1102] | 296 | |
---|
[244] | 297 | ///Checks whether a point is inside a bounding box |
---|
| 298 | bool inside(const xy<T>& u){ |
---|
| 299 | if (_empty) |
---|
| 300 | return false; |
---|
| 301 | else{ |
---|
| 302 | return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && |
---|
| 303 | (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); |
---|
| 304 | } |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | ///Increments a bounding box with a point |
---|
| 308 | BoundingBox& operator +=(const xy<T>& u){ |
---|
| 309 | if (_empty){ |
---|
| 310 | bottom_left=top_right=u; |
---|
| 311 | _empty = false; |
---|
| 312 | } |
---|
| 313 | else{ |
---|
| 314 | if (bottom_left.x > u.x) bottom_left.x = u.x; |
---|
| 315 | if (bottom_left.y > u.y) bottom_left.y = u.y; |
---|
| 316 | if (top_right.x < u.x) top_right.x = u.x; |
---|
| 317 | if (top_right.y < u.y) top_right.y = u.y; |
---|
| 318 | } |
---|
| 319 | return *this; |
---|
[1391] | 320 | } |
---|
[244] | 321 | |
---|
| 322 | ///Sums a bounding box and a point |
---|
| 323 | BoundingBox operator +(const xy<T>& u){ |
---|
| 324 | BoundingBox b = *this; |
---|
| 325 | return b += u; |
---|
[1391] | 326 | } |
---|
[244] | 327 | |
---|
| 328 | ///Increments a bounding box with an other bounding box |
---|
| 329 | BoundingBox& operator +=(const BoundingBox &u){ |
---|
| 330 | if ( !u.empty() ){ |
---|
| 331 | *this += u.bottomLeft(); |
---|
| 332 | *this += u.topRight(); |
---|
| 333 | } |
---|
| 334 | return *this; |
---|
[1391] | 335 | } |
---|
[244] | 336 | |
---|
| 337 | ///Sums two bounding boxes |
---|
| 338 | BoundingBox operator +(const BoundingBox& u){ |
---|
| 339 | BoundingBox b = *this; |
---|
| 340 | return b += u; |
---|
[1391] | 341 | } |
---|
[244] | 342 | |
---|
| 343 | };//class Boundingbox |
---|
| 344 | |
---|
| 345 | |
---|
[1317] | 346 | ///Map of x-coordinates of an xy<>-map |
---|
| 347 | |
---|
| 348 | ///\ingroup maps |
---|
| 349 | /// |
---|
| 350 | template<class M> |
---|
| 351 | class XMap |
---|
| 352 | { |
---|
| 353 | M &_map; |
---|
| 354 | public: |
---|
| 355 | typedef typename M::Value::Value Value; |
---|
| 356 | typedef typename M::Key Key; |
---|
| 357 | ///\e |
---|
| 358 | XMap(M &map) : _map(map) {} |
---|
| 359 | Value operator[](Key k) const {return _map[k].x;} |
---|
[1352] | 360 | void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
---|
[1317] | 361 | }; |
---|
| 362 | |
---|
| 363 | ///Returns an \ref XMap class |
---|
| 364 | |
---|
| 365 | ///This function just returns an \ref XMap class. |
---|
| 366 | /// |
---|
| 367 | ///\ingroup maps |
---|
| 368 | ///\relates XMap |
---|
| 369 | template<class M> |
---|
| 370 | inline XMap<M> xMap(M &m) |
---|
| 371 | { |
---|
| 372 | return XMap<M>(m); |
---|
| 373 | } |
---|
| 374 | |
---|
| 375 | ///Constant (read only) version of \ref XMap |
---|
| 376 | |
---|
| 377 | ///\ingroup maps |
---|
| 378 | /// |
---|
| 379 | template<class M> |
---|
| 380 | class ConstXMap |
---|
| 381 | { |
---|
| 382 | const M &_map; |
---|
| 383 | public: |
---|
| 384 | typedef typename M::Value::Value Value; |
---|
| 385 | typedef typename M::Key Key; |
---|
| 386 | ///\e |
---|
| 387 | ConstXMap(const M &map) : _map(map) {} |
---|
| 388 | Value operator[](Key k) const {return _map[k].x;} |
---|
| 389 | }; |
---|
| 390 | |
---|
| 391 | ///Returns a \ref ConstXMap class |
---|
| 392 | |
---|
| 393 | ///This function just returns an \ref ConstXMap class. |
---|
| 394 | /// |
---|
| 395 | ///\ingroup maps |
---|
| 396 | ///\relates ConstXMap |
---|
| 397 | template<class M> |
---|
| 398 | inline ConstXMap<M> xMap(const M &m) |
---|
| 399 | { |
---|
| 400 | return ConstXMap<M>(m); |
---|
| 401 | } |
---|
| 402 | |
---|
| 403 | ///Map of y-coordinates of an xy<>-map |
---|
| 404 | |
---|
| 405 | ///\ingroup maps |
---|
| 406 | /// |
---|
| 407 | template<class M> |
---|
| 408 | class YMap |
---|
| 409 | { |
---|
| 410 | M &_map; |
---|
| 411 | public: |
---|
| 412 | typedef typename M::Value::Value Value; |
---|
| 413 | typedef typename M::Key Key; |
---|
| 414 | ///\e |
---|
| 415 | YMap(M &map) : _map(map) {} |
---|
| 416 | Value operator[](Key k) const {return _map[k].y;} |
---|
[1352] | 417 | void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
---|
[1317] | 418 | }; |
---|
| 419 | |
---|
| 420 | ///Returns an \ref YMap class |
---|
| 421 | |
---|
| 422 | ///This function just returns an \ref YMap class. |
---|
| 423 | /// |
---|
| 424 | ///\ingroup maps |
---|
| 425 | ///\relates YMap |
---|
| 426 | template<class M> |
---|
| 427 | inline YMap<M> yMap(M &m) |
---|
| 428 | { |
---|
| 429 | return YMap<M>(m); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | ///Constant (read only) version of \ref YMap |
---|
| 433 | |
---|
| 434 | ///\ingroup maps |
---|
| 435 | /// |
---|
| 436 | template<class M> |
---|
| 437 | class ConstYMap |
---|
| 438 | { |
---|
| 439 | const M &_map; |
---|
| 440 | public: |
---|
| 441 | typedef typename M::Value::Value Value; |
---|
| 442 | typedef typename M::Key Key; |
---|
| 443 | ///\e |
---|
| 444 | ConstYMap(const M &map) : _map(map) {} |
---|
| 445 | Value operator[](Key k) const {return _map[k].y;} |
---|
| 446 | }; |
---|
| 447 | |
---|
| 448 | ///Returns a \ref ConstYMap class |
---|
| 449 | |
---|
| 450 | ///This function just returns an \ref ConstYMap class. |
---|
| 451 | /// |
---|
| 452 | ///\ingroup maps |
---|
| 453 | ///\relates ConstYMap |
---|
| 454 | template<class M> |
---|
| 455 | inline ConstYMap<M> yMap(const M &m) |
---|
| 456 | { |
---|
| 457 | return ConstYMap<M>(m); |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | |
---|
[1352] | 461 | ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map |
---|
| 462 | |
---|
| 463 | ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map |
---|
| 464 | ///\ingroup maps |
---|
| 465 | /// |
---|
| 466 | template<class M> |
---|
| 467 | class NormSquareMap |
---|
| 468 | { |
---|
| 469 | const M &_map; |
---|
| 470 | public: |
---|
| 471 | typedef typename M::Value::Value Value; |
---|
| 472 | typedef typename M::Key Key; |
---|
| 473 | ///\e |
---|
| 474 | NormSquareMap(const M &map) : _map(map) {} |
---|
| 475 | Value operator[](Key k) const {return _map[k].normSquare();} |
---|
| 476 | }; |
---|
| 477 | |
---|
| 478 | ///Returns a \ref NormSquareMap class |
---|
| 479 | |
---|
| 480 | ///This function just returns an \ref NormSquareMap class. |
---|
| 481 | /// |
---|
| 482 | ///\ingroup maps |
---|
| 483 | ///\relates NormSquareMap |
---|
| 484 | template<class M> |
---|
| 485 | inline NormSquareMap<M> normSquareMap(const M &m) |
---|
| 486 | { |
---|
| 487 | return NormSquareMap<M>(m); |
---|
| 488 | } |
---|
| 489 | |
---|
[431] | 490 | /// @} |
---|
[244] | 491 | |
---|
| 492 | |
---|
[921] | 493 | } //namespace lemon |
---|
[201] | 494 | |
---|
[921] | 495 | #endif //LEMON_XY_H |
---|