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