[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[8] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[8] | 4 | * |
---|
[39] | 5 | * Copyright (C) 2003-2008 |
---|
[8] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef LEMON_DIM2_H |
---|
| 20 | #define LEMON_DIM2_H |
---|
| 21 | |
---|
| 22 | #include <iostream> |
---|
| 23 | |
---|
| 24 | ///\ingroup misc |
---|
| 25 | ///\file |
---|
[209] | 26 | ///\brief A simple two dimensional vector and a bounding box implementation |
---|
[8] | 27 | /// |
---|
| 28 | /// The class \ref lemon::dim2::Point "dim2::Point" implements |
---|
[49] | 29 | /// a two dimensional vector with the usual operations. |
---|
[8] | 30 | /// |
---|
| 31 | /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox" |
---|
| 32 | /// can be used to determine |
---|
| 33 | /// the rectangular bounding box of a set of |
---|
| 34 | /// \ref lemon::dim2::Point "dim2::Point"'s. |
---|
| 35 | |
---|
| 36 | namespace lemon { |
---|
| 37 | |
---|
| 38 | ///Tools for handling two dimensional coordinates |
---|
| 39 | |
---|
| 40 | ///This namespace is a storage of several |
---|
| 41 | ///tools for handling two dimensional coordinates |
---|
| 42 | namespace dim2 { |
---|
| 43 | |
---|
| 44 | /// \addtogroup misc |
---|
| 45 | /// @{ |
---|
| 46 | |
---|
[241] | 47 | /// A simple two dimensional vector (plain vector) implementation |
---|
[8] | 48 | |
---|
[241] | 49 | /// A simple two dimensional vector (plain vector) implementation |
---|
[49] | 50 | /// with the usual vector operations. |
---|
[8] | 51 | template<typename T> |
---|
| 52 | class Point { |
---|
| 53 | |
---|
| 54 | public: |
---|
| 55 | |
---|
| 56 | typedef T Value; |
---|
| 57 | |
---|
[15] | 58 | ///First coordinate |
---|
[8] | 59 | T x; |
---|
[15] | 60 | ///Second coordinate |
---|
[209] | 61 | T y; |
---|
| 62 | |
---|
[8] | 63 | ///Default constructor |
---|
| 64 | Point() {} |
---|
| 65 | |
---|
| 66 | ///Construct an instance from coordinates |
---|
| 67 | Point(T a, T b) : x(a), y(b) { } |
---|
| 68 | |
---|
[49] | 69 | ///Returns the dimension of the vector (i.e. returns 2). |
---|
[8] | 70 | |
---|
[15] | 71 | ///The dimension of the vector. |
---|
[209] | 72 | ///This function always returns 2. |
---|
[8] | 73 | int size() const { return 2; } |
---|
| 74 | |
---|
| 75 | ///Subscripting operator |
---|
| 76 | |
---|
| 77 | ///\c p[0] is \c p.x and \c p[1] is \c p.y |
---|
| 78 | /// |
---|
| 79 | T& operator[](int idx) { return idx == 0 ? x : y; } |
---|
| 80 | |
---|
| 81 | ///Const subscripting operator |
---|
| 82 | |
---|
| 83 | ///\c p[0] is \c p.x and \c p[1] is \c p.y |
---|
| 84 | /// |
---|
| 85 | const T& operator[](int idx) const { return idx == 0 ? x : y; } |
---|
| 86 | |
---|
| 87 | ///Conversion constructor |
---|
| 88 | template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {} |
---|
| 89 | |
---|
| 90 | ///Give back the square of the norm of the vector |
---|
| 91 | T normSquare() const { |
---|
| 92 | return x*x+y*y; |
---|
| 93 | } |
---|
[209] | 94 | |
---|
[49] | 95 | ///Increment the left hand side by \c u |
---|
[8] | 96 | Point<T>& operator +=(const Point<T>& u) { |
---|
| 97 | x += u.x; |
---|
| 98 | y += u.y; |
---|
| 99 | return *this; |
---|
| 100 | } |
---|
[209] | 101 | |
---|
[49] | 102 | ///Decrement the left hand side by \c u |
---|
[8] | 103 | Point<T>& operator -=(const Point<T>& u) { |
---|
| 104 | x -= u.x; |
---|
| 105 | y -= u.y; |
---|
| 106 | return *this; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | ///Multiply the left hand side with a scalar |
---|
| 110 | Point<T>& operator *=(const T &u) { |
---|
| 111 | x *= u; |
---|
| 112 | y *= u; |
---|
| 113 | return *this; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | ///Divide the left hand side by a scalar |
---|
| 117 | Point<T>& operator /=(const T &u) { |
---|
| 118 | x /= u; |
---|
| 119 | y /= u; |
---|
| 120 | return *this; |
---|
| 121 | } |
---|
[209] | 122 | |
---|
[8] | 123 | ///Return the scalar product of two vectors |
---|
| 124 | T operator *(const Point<T>& u) const { |
---|
| 125 | return x*u.x+y*u.y; |
---|
| 126 | } |
---|
[209] | 127 | |
---|
[8] | 128 | ///Return the sum of two vectors |
---|
| 129 | Point<T> operator+(const Point<T> &u) const { |
---|
| 130 | Point<T> b=*this; |
---|
| 131 | return b+=u; |
---|
| 132 | } |
---|
| 133 | |
---|
[15] | 134 | ///Return the negative of the vector |
---|
[8] | 135 | Point<T> operator-() const { |
---|
| 136 | Point<T> b=*this; |
---|
| 137 | b.x=-b.x; b.y=-b.y; |
---|
| 138 | return b; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | ///Return the difference of two vectors |
---|
| 142 | Point<T> operator-(const Point<T> &u) const { |
---|
| 143 | Point<T> b=*this; |
---|
| 144 | return b-=u; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | ///Return a vector multiplied by a scalar |
---|
| 148 | Point<T> operator*(const T &u) const { |
---|
| 149 | Point<T> b=*this; |
---|
| 150 | return b*=u; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | ///Return a vector divided by a scalar |
---|
| 154 | Point<T> operator/(const T &u) const { |
---|
| 155 | Point<T> b=*this; |
---|
| 156 | return b/=u; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | ///Test equality |
---|
| 160 | bool operator==(const Point<T> &u) const { |
---|
| 161 | return (x==u.x) && (y==u.y); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | ///Test inequality |
---|
| 165 | bool operator!=(Point u) const { |
---|
| 166 | return (x!=u.x) || (y!=u.y); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | }; |
---|
| 170 | |
---|
[209] | 171 | ///Return a Point |
---|
[8] | 172 | |
---|
[15] | 173 | ///Return a Point. |
---|
[8] | 174 | ///\relates Point |
---|
| 175 | template <typename T> |
---|
| 176 | inline Point<T> makePoint(const T& x, const T& y) { |
---|
| 177 | return Point<T>(x, y); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | ///Return a vector multiplied by a scalar |
---|
| 181 | |
---|
[15] | 182 | ///Return a vector multiplied by a scalar. |
---|
[8] | 183 | ///\relates Point |
---|
| 184 | template<typename T> Point<T> operator*(const T &u,const Point<T> &x) { |
---|
| 185 | return x*u; |
---|
| 186 | } |
---|
| 187 | |
---|
[241] | 188 | ///Read a plain vector from a stream |
---|
[8] | 189 | |
---|
[241] | 190 | ///Read a plain vector from a stream. |
---|
[8] | 191 | ///\relates Point |
---|
| 192 | /// |
---|
| 193 | template<typename T> |
---|
| 194 | inline std::istream& operator>>(std::istream &is, Point<T> &z) { |
---|
| 195 | char c; |
---|
| 196 | if (is >> c) { |
---|
| 197 | if (c != '(') is.putback(c); |
---|
| 198 | } else { |
---|
| 199 | is.clear(); |
---|
| 200 | } |
---|
| 201 | if (!(is >> z.x)) return is; |
---|
| 202 | if (is >> c) { |
---|
| 203 | if (c != ',') is.putback(c); |
---|
| 204 | } else { |
---|
| 205 | is.clear(); |
---|
| 206 | } |
---|
| 207 | if (!(is >> z.y)) return is; |
---|
| 208 | if (is >> c) { |
---|
| 209 | if (c != ')') is.putback(c); |
---|
| 210 | } else { |
---|
| 211 | is.clear(); |
---|
| 212 | } |
---|
| 213 | return is; |
---|
| 214 | } |
---|
| 215 | |
---|
[241] | 216 | ///Write a plain vector to a stream |
---|
[8] | 217 | |
---|
[241] | 218 | ///Write a plain vector to a stream. |
---|
[8] | 219 | ///\relates Point |
---|
| 220 | /// |
---|
| 221 | template<typename T> |
---|
| 222 | inline std::ostream& operator<<(std::ostream &os, const Point<T>& z) |
---|
| 223 | { |
---|
| 224 | os << "(" << z.x << ", " << z.y << ")"; |
---|
| 225 | return os; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | ///Rotate by 90 degrees |
---|
| 229 | |
---|
[15] | 230 | ///Returns the parameter rotated by 90 degrees in positive direction. |
---|
[8] | 231 | ///\relates Point |
---|
| 232 | /// |
---|
| 233 | template<typename T> |
---|
| 234 | inline Point<T> rot90(const Point<T> &z) |
---|
| 235 | { |
---|
| 236 | return Point<T>(-z.y,z.x); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | ///Rotate by 180 degrees |
---|
| 240 | |
---|
[15] | 241 | ///Returns the parameter rotated by 180 degrees. |
---|
[8] | 242 | ///\relates Point |
---|
| 243 | /// |
---|
| 244 | template<typename T> |
---|
| 245 | inline Point<T> rot180(const Point<T> &z) |
---|
| 246 | { |
---|
| 247 | return Point<T>(-z.x,-z.y); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | ///Rotate by 270 degrees |
---|
| 251 | |
---|
[15] | 252 | ///Returns the parameter rotated by 90 degrees in negative direction. |
---|
[8] | 253 | ///\relates Point |
---|
| 254 | /// |
---|
| 255 | template<typename T> |
---|
| 256 | inline Point<T> rot270(const Point<T> &z) |
---|
| 257 | { |
---|
| 258 | return Point<T>(z.y,-z.x); |
---|
| 259 | } |
---|
| 260 | |
---|
[209] | 261 | |
---|
[8] | 262 | |
---|
[241] | 263 | /// A class to calculate or store the bounding box of plain vectors. |
---|
[8] | 264 | |
---|
[241] | 265 | /// A class to calculate or store the bounding box of plain vectors. |
---|
| 266 | /// |
---|
[8] | 267 | template<typename T> |
---|
| 268 | class BoundingBox { |
---|
[241] | 269 | Point<T> _bottom_left, _top_right; |
---|
[8] | 270 | bool _empty; |
---|
| 271 | public: |
---|
[209] | 272 | |
---|
[8] | 273 | ///Default constructor: creates an empty bounding box |
---|
| 274 | BoundingBox() { _empty = true; } |
---|
| 275 | |
---|
| 276 | ///Construct an instance from one point |
---|
[241] | 277 | BoundingBox(Point<T> a) { |
---|
| 278 | _bottom_left = _top_right = a; |
---|
| 279 | _empty = false; |
---|
| 280 | } |
---|
[209] | 281 | |
---|
[8] | 282 | ///Construct an instance from two points |
---|
[209] | 283 | |
---|
[15] | 284 | ///Construct an instance from two points. |
---|
| 285 | ///\param a The bottom left corner. |
---|
| 286 | ///\param b The top right corner. |
---|
| 287 | ///\warning The coordinates of the bottom left corner must be no more |
---|
| 288 | ///than those of the top right one. |
---|
[8] | 289 | BoundingBox(Point<T> a,Point<T> b) |
---|
| 290 | { |
---|
[241] | 291 | _bottom_left = a; |
---|
| 292 | _top_right = b; |
---|
[209] | 293 | _empty = false; |
---|
[8] | 294 | } |
---|
[209] | 295 | |
---|
[8] | 296 | ///Construct an instance from four numbers |
---|
| 297 | |
---|
[15] | 298 | ///Construct an instance from four numbers. |
---|
| 299 | ///\param l The left side of the box. |
---|
| 300 | ///\param b The bottom of the box. |
---|
| 301 | ///\param r The right side of the box. |
---|
| 302 | ///\param t The top of the box. |
---|
| 303 | ///\warning The left side must be no more than the right side and |
---|
[209] | 304 | ///bottom must be no more than the top. |
---|
[8] | 305 | BoundingBox(T l,T b,T r,T t) |
---|
| 306 | { |
---|
[241] | 307 | _bottom_left=Point<T>(l,b); |
---|
| 308 | _top_right=Point<T>(r,t); |
---|
[209] | 309 | _empty = false; |
---|
[8] | 310 | } |
---|
[209] | 311 | |
---|
[15] | 312 | ///Return \c true if the bounding box is empty. |
---|
[209] | 313 | |
---|
[15] | 314 | ///Return \c true if the bounding box is empty (i.e. return \c false |
---|
| 315 | ///if at least one point was added to the box or the coordinates of |
---|
| 316 | ///the box were set). |
---|
[49] | 317 | /// |
---|
[209] | 318 | ///The coordinates of an empty bounding box are not defined. |
---|
[8] | 319 | bool empty() const { |
---|
| 320 | return _empty; |
---|
| 321 | } |
---|
[209] | 322 | |
---|
[8] | 323 | ///Make the BoundingBox empty |
---|
| 324 | void clear() { |
---|
[241] | 325 | _empty = true; |
---|
[8] | 326 | } |
---|
| 327 | |
---|
[49] | 328 | ///Give back the bottom left corner of the box |
---|
[8] | 329 | |
---|
[49] | 330 | ///Give back the bottom left corner of the box. |
---|
[8] | 331 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 332 | Point<T> bottomLeft() const { |
---|
[241] | 333 | return _bottom_left; |
---|
[8] | 334 | } |
---|
| 335 | |
---|
[49] | 336 | ///Set the bottom left corner of the box |
---|
[8] | 337 | |
---|
[49] | 338 | ///Set the bottom left corner of the box. |
---|
[241] | 339 | ///\pre The box must not be empty. |
---|
[8] | 340 | void bottomLeft(Point<T> p) { |
---|
[241] | 341 | _bottom_left = p; |
---|
[8] | 342 | } |
---|
| 343 | |
---|
[49] | 344 | ///Give back the top right corner of the box |
---|
[8] | 345 | |
---|
[49] | 346 | ///Give back the top right corner of the box. |
---|
[8] | 347 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 348 | Point<T> topRight() const { |
---|
[241] | 349 | return _top_right; |
---|
[8] | 350 | } |
---|
| 351 | |
---|
[49] | 352 | ///Set the top right corner of the box |
---|
[8] | 353 | |
---|
[49] | 354 | ///Set the top right corner of the box. |
---|
[241] | 355 | ///\pre The box must not be empty. |
---|
[8] | 356 | void topRight(Point<T> p) { |
---|
[241] | 357 | _top_right = p; |
---|
[8] | 358 | } |
---|
| 359 | |
---|
[49] | 360 | ///Give back the bottom right corner of the box |
---|
[8] | 361 | |
---|
[49] | 362 | ///Give back the bottom right corner of the box. |
---|
[8] | 363 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 364 | Point<T> bottomRight() const { |
---|
[241] | 365 | return Point<T>(_top_right.x,_bottom_left.y); |
---|
[8] | 366 | } |
---|
| 367 | |
---|
[49] | 368 | ///Set the bottom right corner of the box |
---|
[8] | 369 | |
---|
[49] | 370 | ///Set the bottom right corner of the box. |
---|
[241] | 371 | ///\pre The box must not be empty. |
---|
[8] | 372 | void bottomRight(Point<T> p) { |
---|
[241] | 373 | _top_right.x = p.x; |
---|
| 374 | _bottom_left.y = p.y; |
---|
[8] | 375 | } |
---|
[209] | 376 | |
---|
[49] | 377 | ///Give back the top left corner of the box |
---|
[8] | 378 | |
---|
[49] | 379 | ///Give back the top left corner of the box. |
---|
[8] | 380 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 381 | Point<T> topLeft() const { |
---|
[241] | 382 | return Point<T>(_bottom_left.x,_top_right.y); |
---|
[8] | 383 | } |
---|
| 384 | |
---|
[49] | 385 | ///Set the top left corner of the box |
---|
[8] | 386 | |
---|
[49] | 387 | ///Set the top left corner of the box. |
---|
[241] | 388 | ///\pre The box must not be empty. |
---|
[8] | 389 | void topLeft(Point<T> p) { |
---|
[241] | 390 | _top_right.y = p.y; |
---|
| 391 | _bottom_left.x = p.x; |
---|
[8] | 392 | } |
---|
| 393 | |
---|
| 394 | ///Give back the bottom of the box |
---|
| 395 | |
---|
| 396 | ///Give back the bottom of the box. |
---|
| 397 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 398 | T bottom() const { |
---|
[241] | 399 | return _bottom_left.y; |
---|
[8] | 400 | } |
---|
| 401 | |
---|
| 402 | ///Set the bottom of the box |
---|
| 403 | |
---|
| 404 | ///Set the bottom of the box. |
---|
[241] | 405 | ///\pre The box must not be empty. |
---|
[8] | 406 | void bottom(T t) { |
---|
[241] | 407 | _bottom_left.y = t; |
---|
[8] | 408 | } |
---|
| 409 | |
---|
| 410 | ///Give back the top of the box |
---|
| 411 | |
---|
| 412 | ///Give back the top of the box. |
---|
| 413 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 414 | T top() const { |
---|
[241] | 415 | return _top_right.y; |
---|
[8] | 416 | } |
---|
| 417 | |
---|
| 418 | ///Set the top of the box |
---|
| 419 | |
---|
| 420 | ///Set the top of the box. |
---|
[241] | 421 | ///\pre The box must not be empty. |
---|
[8] | 422 | void top(T t) { |
---|
[241] | 423 | _top_right.y = t; |
---|
[8] | 424 | } |
---|
| 425 | |
---|
| 426 | ///Give back the left side of the box |
---|
| 427 | |
---|
| 428 | ///Give back the left side of the box. |
---|
| 429 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 430 | T left() const { |
---|
[241] | 431 | return _bottom_left.x; |
---|
[8] | 432 | } |
---|
[209] | 433 | |
---|
[8] | 434 | ///Set the left side of the box |
---|
| 435 | |
---|
| 436 | ///Set the left side of the box. |
---|
[241] | 437 | ///\pre The box must not be empty. |
---|
[8] | 438 | void left(T t) { |
---|
[241] | 439 | _bottom_left.x = t; |
---|
[8] | 440 | } |
---|
| 441 | |
---|
| 442 | /// Give back the right side of the box |
---|
| 443 | |
---|
| 444 | /// Give back the right side of the box. |
---|
| 445 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 446 | T right() const { |
---|
[241] | 447 | return _top_right.x; |
---|
[8] | 448 | } |
---|
| 449 | |
---|
| 450 | ///Set the right side of the box |
---|
| 451 | |
---|
| 452 | ///Set the right side of the box. |
---|
[241] | 453 | ///\pre The box must not be empty. |
---|
[8] | 454 | void right(T t) { |
---|
[241] | 455 | _top_right.x = t; |
---|
[8] | 456 | } |
---|
| 457 | |
---|
| 458 | ///Give back the height of the box |
---|
| 459 | |
---|
| 460 | ///Give back the height of the box. |
---|
| 461 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 462 | T height() const { |
---|
[241] | 463 | return _top_right.y-_bottom_left.y; |
---|
[8] | 464 | } |
---|
| 465 | |
---|
| 466 | ///Give back the width of the box |
---|
| 467 | |
---|
| 468 | ///Give back the width of the box. |
---|
| 469 | ///If the bounding box is empty, then the return value is not defined. |
---|
| 470 | T width() const { |
---|
[241] | 471 | return _top_right.x-_bottom_left.x; |
---|
[8] | 472 | } |
---|
| 473 | |
---|
| 474 | ///Checks whether a point is inside a bounding box |
---|
[15] | 475 | bool inside(const Point<T>& u) const { |
---|
[8] | 476 | if (_empty) |
---|
| 477 | return false; |
---|
[241] | 478 | else { |
---|
| 479 | return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 && |
---|
| 480 | (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 ); |
---|
[8] | 481 | } |
---|
| 482 | } |
---|
[209] | 483 | |
---|
[8] | 484 | ///Increments a bounding box with a point |
---|
[15] | 485 | |
---|
| 486 | ///Increments a bounding box with a point. |
---|
| 487 | /// |
---|
[8] | 488 | BoundingBox& add(const Point<T>& u){ |
---|
[241] | 489 | if (_empty) { |
---|
| 490 | _bottom_left = _top_right = u; |
---|
[8] | 491 | _empty = false; |
---|
| 492 | } |
---|
[241] | 493 | else { |
---|
| 494 | if (_bottom_left.x > u.x) _bottom_left.x = u.x; |
---|
| 495 | if (_bottom_left.y > u.y) _bottom_left.y = u.y; |
---|
| 496 | if (_top_right.x < u.x) _top_right.x = u.x; |
---|
| 497 | if (_top_right.y < u.y) _top_right.y = u.y; |
---|
[8] | 498 | } |
---|
| 499 | return *this; |
---|
| 500 | } |
---|
[209] | 501 | |
---|
[15] | 502 | ///Increments a bounding box to contain another bounding box |
---|
[209] | 503 | |
---|
[15] | 504 | ///Increments a bounding box to contain another bounding box. |
---|
| 505 | /// |
---|
[8] | 506 | BoundingBox& add(const BoundingBox &u){ |
---|
| 507 | if ( !u.empty() ){ |
---|
[241] | 508 | add(u._bottom_left); |
---|
| 509 | add(u._top_right); |
---|
[8] | 510 | } |
---|
| 511 | return *this; |
---|
| 512 | } |
---|
[209] | 513 | |
---|
[8] | 514 | ///Intersection of two bounding boxes |
---|
[15] | 515 | |
---|
| 516 | ///Intersection of two bounding boxes. |
---|
| 517 | /// |
---|
| 518 | BoundingBox operator&(const BoundingBox& u) const { |
---|
[8] | 519 | BoundingBox b; |
---|
[241] | 520 | if (_empty || u._empty) { |
---|
[209] | 521 | b._empty = true; |
---|
| 522 | } else { |
---|
[241] | 523 | b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x); |
---|
| 524 | b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y); |
---|
| 525 | b._top_right.x = std::min(_top_right.x, u._top_right.x); |
---|
| 526 | b._top_right.y = std::min(_top_right.y, u._top_right.y); |
---|
| 527 | b._empty = b._bottom_left.x > b._top_right.x || |
---|
| 528 | b._bottom_left.y > b._top_right.y; |
---|
[209] | 529 | } |
---|
[8] | 530 | return b; |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | };//class Boundingbox |
---|
| 534 | |
---|
| 535 | |
---|
[49] | 536 | ///Map of x-coordinates of a \ref Point "Point"-map |
---|
[8] | 537 | |
---|
| 538 | ///\ingroup maps |
---|
[49] | 539 | ///Map of x-coordinates of a \ref Point "Point"-map. |
---|
[8] | 540 | /// |
---|
| 541 | template<class M> |
---|
[209] | 542 | class XMap |
---|
[8] | 543 | { |
---|
| 544 | M& _map; |
---|
| 545 | public: |
---|
| 546 | |
---|
| 547 | typedef typename M::Value::Value Value; |
---|
| 548 | typedef typename M::Key Key; |
---|
| 549 | ///\e |
---|
| 550 | XMap(M& map) : _map(map) {} |
---|
| 551 | Value operator[](Key k) const {return _map[k].x;} |
---|
| 552 | void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} |
---|
| 553 | }; |
---|
[209] | 554 | |
---|
[8] | 555 | ///Returns an \ref XMap class |
---|
| 556 | |
---|
| 557 | ///This function just returns an \ref XMap class. |
---|
| 558 | /// |
---|
| 559 | ///\ingroup maps |
---|
| 560 | ///\relates XMap |
---|
[209] | 561 | template<class M> |
---|
| 562 | inline XMap<M> xMap(M &m) |
---|
[8] | 563 | { |
---|
| 564 | return XMap<M>(m); |
---|
| 565 | } |
---|
| 566 | |
---|
[209] | 567 | template<class M> |
---|
| 568 | inline XMap<M> xMap(const M &m) |
---|
[8] | 569 | { |
---|
| 570 | return XMap<M>(m); |
---|
| 571 | } |
---|
| 572 | |
---|
[49] | 573 | ///Constant (read only) version of \ref XMap |
---|
[8] | 574 | |
---|
| 575 | ///\ingroup maps |
---|
| 576 | ///Constant (read only) version of \ref XMap |
---|
| 577 | /// |
---|
| 578 | template<class M> |
---|
[209] | 579 | class ConstXMap |
---|
[8] | 580 | { |
---|
| 581 | const M& _map; |
---|
| 582 | public: |
---|
| 583 | |
---|
| 584 | typedef typename M::Value::Value Value; |
---|
| 585 | typedef typename M::Key Key; |
---|
| 586 | ///\e |
---|
| 587 | ConstXMap(const M &map) : _map(map) {} |
---|
| 588 | Value operator[](Key k) const {return _map[k].x;} |
---|
| 589 | }; |
---|
[209] | 590 | |
---|
[8] | 591 | ///Returns a \ref ConstXMap class |
---|
| 592 | |
---|
[15] | 593 | ///This function just returns a \ref ConstXMap class. |
---|
[8] | 594 | /// |
---|
| 595 | ///\ingroup maps |
---|
| 596 | ///\relates ConstXMap |
---|
[209] | 597 | template<class M> |
---|
| 598 | inline ConstXMap<M> xMap(const M &m) |
---|
[8] | 599 | { |
---|
| 600 | return ConstXMap<M>(m); |
---|
| 601 | } |
---|
| 602 | |
---|
[49] | 603 | ///Map of y-coordinates of a \ref Point "Point"-map |
---|
[209] | 604 | |
---|
[8] | 605 | ///\ingroup maps |
---|
[15] | 606 | ///Map of y-coordinates of a \ref Point "Point"-map. |
---|
[8] | 607 | /// |
---|
| 608 | template<class M> |
---|
[209] | 609 | class YMap |
---|
[8] | 610 | { |
---|
| 611 | M& _map; |
---|
| 612 | public: |
---|
| 613 | |
---|
| 614 | typedef typename M::Value::Value Value; |
---|
| 615 | typedef typename M::Key Key; |
---|
| 616 | ///\e |
---|
| 617 | YMap(M& map) : _map(map) {} |
---|
| 618 | Value operator[](Key k) const {return _map[k].y;} |
---|
| 619 | void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} |
---|
| 620 | }; |
---|
| 621 | |
---|
[15] | 622 | ///Returns a \ref YMap class |
---|
[8] | 623 | |
---|
[15] | 624 | ///This function just returns a \ref YMap class. |
---|
[8] | 625 | /// |
---|
| 626 | ///\ingroup maps |
---|
| 627 | ///\relates YMap |
---|
[209] | 628 | template<class M> |
---|
| 629 | inline YMap<M> yMap(M &m) |
---|
[8] | 630 | { |
---|
| 631 | return YMap<M>(m); |
---|
| 632 | } |
---|
| 633 | |
---|
[209] | 634 | template<class M> |
---|
| 635 | inline YMap<M> yMap(const M &m) |
---|
[8] | 636 | { |
---|
| 637 | return YMap<M>(m); |
---|
| 638 | } |
---|
| 639 | |
---|
[49] | 640 | ///Constant (read only) version of \ref YMap |
---|
[8] | 641 | |
---|
| 642 | ///\ingroup maps |
---|
| 643 | ///Constant (read only) version of \ref YMap |
---|
| 644 | /// |
---|
| 645 | template<class M> |
---|
[209] | 646 | class ConstYMap |
---|
[8] | 647 | { |
---|
| 648 | const M& _map; |
---|
| 649 | public: |
---|
| 650 | |
---|
| 651 | typedef typename M::Value::Value Value; |
---|
| 652 | typedef typename M::Key Key; |
---|
| 653 | ///\e |
---|
| 654 | ConstYMap(const M &map) : _map(map) {} |
---|
| 655 | Value operator[](Key k) const {return _map[k].y;} |
---|
| 656 | }; |
---|
[209] | 657 | |
---|
[8] | 658 | ///Returns a \ref ConstYMap class |
---|
| 659 | |
---|
[15] | 660 | ///This function just returns a \ref ConstYMap class. |
---|
[8] | 661 | /// |
---|
| 662 | ///\ingroup maps |
---|
| 663 | ///\relates ConstYMap |
---|
[209] | 664 | template<class M> |
---|
| 665 | inline ConstYMap<M> yMap(const M &m) |
---|
[8] | 666 | { |
---|
| 667 | return ConstYMap<M>(m); |
---|
| 668 | } |
---|
| 669 | |
---|
| 670 | |
---|
[49] | 671 | ///\brief Map of the \ref Point::normSquare() "normSquare()" |
---|
| 672 | ///of a \ref Point "Point"-map |
---|
| 673 | /// |
---|
| 674 | ///Map of the \ref Point::normSquare() "normSquare()" |
---|
| 675 | ///of a \ref Point "Point"-map. |
---|
| 676 | ///\ingroup maps |
---|
[8] | 677 | template<class M> |
---|
[209] | 678 | class NormSquareMap |
---|
[8] | 679 | { |
---|
| 680 | const M& _map; |
---|
| 681 | public: |
---|
| 682 | |
---|
| 683 | typedef typename M::Value::Value Value; |
---|
| 684 | typedef typename M::Key Key; |
---|
| 685 | ///\e |
---|
| 686 | NormSquareMap(const M &map) : _map(map) {} |
---|
| 687 | Value operator[](Key k) const {return _map[k].normSquare();} |
---|
| 688 | }; |
---|
[209] | 689 | |
---|
[8] | 690 | ///Returns a \ref NormSquareMap class |
---|
| 691 | |
---|
[15] | 692 | ///This function just returns a \ref NormSquareMap class. |
---|
[8] | 693 | /// |
---|
| 694 | ///\ingroup maps |
---|
| 695 | ///\relates NormSquareMap |
---|
[209] | 696 | template<class M> |
---|
| 697 | inline NormSquareMap<M> normSquareMap(const M &m) |
---|
[8] | 698 | { |
---|
| 699 | return NormSquareMap<M>(m); |
---|
| 700 | } |
---|
| 701 | |
---|
| 702 | /// @} |
---|
| 703 | |
---|
| 704 | } //namespce dim2 |
---|
[209] | 705 | |
---|
[8] | 706 | } //namespace lemon |
---|
| 707 | |
---|
| 708 | #endif //LEMON_DIM2_H |
---|