1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lemon/dim2.h Thu Dec 20 16:11:56 2007 +0000
1.3 @@ -0,0 +1,689 @@
1.4 +/* -*- C++ -*-
1.5 + *
1.6 + * This file is a part of LEMON, a generic C++ optimization library
1.7 + *
1.8 + * Copyright (C) 2003-2007
1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 + *
1.12 + * Permission to use, modify and distribute this software is granted
1.13 + * provided that this copyright notice appears in all copies. For
1.14 + * precise terms see the accompanying LICENSE file.
1.15 + *
1.16 + * This software is provided "AS IS" with no warranty of any kind,
1.17 + * express or implied, and with no claim as to its suitability for any
1.18 + * purpose.
1.19 + *
1.20 + */
1.21 +
1.22 +#ifndef LEMON_DIM2_H
1.23 +#define LEMON_DIM2_H
1.24 +
1.25 +#include <iostream>
1.26 +#include <lemon/bits/utility.h>
1.27 +
1.28 +///\ingroup misc
1.29 +///\file
1.30 +///\brief A simple two dimensional vector and a bounding box implementation
1.31 +///
1.32 +/// The class \ref lemon::dim2::Point "dim2::Point" implements
1.33 +///a two dimensional vector with the usual
1.34 +/// operations.
1.35 +///
1.36 +/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
1.37 +/// can be used to determine
1.38 +/// the rectangular bounding box of a set of
1.39 +/// \ref lemon::dim2::Point "dim2::Point"'s.
1.40 +///
1.41 +///\author Attila Bernath
1.42 +
1.43 +
1.44 +namespace lemon {
1.45 +
1.46 + ///Tools for handling two dimensional coordinates
1.47 +
1.48 + ///This namespace is a storage of several
1.49 + ///tools for handling two dimensional coordinates
1.50 + namespace dim2 {
1.51 +
1.52 + /// \addtogroup misc
1.53 + /// @{
1.54 +
1.55 + /// A simple two dimensional vector (plainvector) implementation
1.56 +
1.57 + /// A simple two dimensional vector (plainvector) implementation
1.58 + ///with the usual vector
1.59 + /// operators.
1.60 + ///
1.61 + template<typename T>
1.62 + class Point {
1.63 +
1.64 + public:
1.65 +
1.66 + typedef T Value;
1.67 +
1.68 + ///First co-ordinate
1.69 + T x;
1.70 + ///Second co-ordinate
1.71 + T y;
1.72 +
1.73 + ///Default constructor
1.74 + Point() {}
1.75 +
1.76 + ///Construct an instance from coordinates
1.77 + Point(T a, T b) : x(a), y(b) { }
1.78 +
1.79 + ///The dimension of the vector.
1.80 +
1.81 + ///This class give back always 2.
1.82 + ///
1.83 + int size() const { return 2; }
1.84 +
1.85 + ///Subscripting operator
1.86 +
1.87 + ///\c p[0] is \c p.x and \c p[1] is \c p.y
1.88 + ///
1.89 + T& operator[](int idx) { return idx == 0 ? x : y; }
1.90 +
1.91 + ///Const subscripting operator
1.92 +
1.93 + ///\c p[0] is \c p.x and \c p[1] is \c p.y
1.94 + ///
1.95 + const T& operator[](int idx) const { return idx == 0 ? x : y; }
1.96 +
1.97 + ///Conversion constructor
1.98 + template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
1.99 +
1.100 + ///Give back the square of the norm of the vector
1.101 + T normSquare() const {
1.102 + return x*x+y*y;
1.103 + }
1.104 +
1.105 + ///Increment the left hand side by u
1.106 + Point<T>& operator +=(const Point<T>& u) {
1.107 + x += u.x;
1.108 + y += u.y;
1.109 + return *this;
1.110 + }
1.111 +
1.112 + ///Decrement the left hand side by u
1.113 + Point<T>& operator -=(const Point<T>& u) {
1.114 + x -= u.x;
1.115 + y -= u.y;
1.116 + return *this;
1.117 + }
1.118 +
1.119 + ///Multiply the left hand side with a scalar
1.120 + Point<T>& operator *=(const T &u) {
1.121 + x *= u;
1.122 + y *= u;
1.123 + return *this;
1.124 + }
1.125 +
1.126 + ///Divide the left hand side by a scalar
1.127 + Point<T>& operator /=(const T &u) {
1.128 + x /= u;
1.129 + y /= u;
1.130 + return *this;
1.131 + }
1.132 +
1.133 + ///Return the scalar product of two vectors
1.134 + T operator *(const Point<T>& u) const {
1.135 + return x*u.x+y*u.y;
1.136 + }
1.137 +
1.138 + ///Return the sum of two vectors
1.139 + Point<T> operator+(const Point<T> &u) const {
1.140 + Point<T> b=*this;
1.141 + return b+=u;
1.142 + }
1.143 +
1.144 + ///Return the neg of the vectors
1.145 + Point<T> operator-() const {
1.146 + Point<T> b=*this;
1.147 + b.x=-b.x; b.y=-b.y;
1.148 + return b;
1.149 + }
1.150 +
1.151 + ///Return the difference of two vectors
1.152 + Point<T> operator-(const Point<T> &u) const {
1.153 + Point<T> b=*this;
1.154 + return b-=u;
1.155 + }
1.156 +
1.157 + ///Return a vector multiplied by a scalar
1.158 + Point<T> operator*(const T &u) const {
1.159 + Point<T> b=*this;
1.160 + return b*=u;
1.161 + }
1.162 +
1.163 + ///Return a vector divided by a scalar
1.164 + Point<T> operator/(const T &u) const {
1.165 + Point<T> b=*this;
1.166 + return b/=u;
1.167 + }
1.168 +
1.169 + ///Test equality
1.170 + bool operator==(const Point<T> &u) const {
1.171 + return (x==u.x) && (y==u.y);
1.172 + }
1.173 +
1.174 + ///Test inequality
1.175 + bool operator!=(Point u) const {
1.176 + return (x!=u.x) || (y!=u.y);
1.177 + }
1.178 +
1.179 + };
1.180 +
1.181 + ///Return an Point
1.182 +
1.183 + ///Return an Point
1.184 + ///\relates Point
1.185 + template <typename T>
1.186 + inline Point<T> makePoint(const T& x, const T& y) {
1.187 + return Point<T>(x, y);
1.188 + }
1.189 +
1.190 + ///Return a vector multiplied by a scalar
1.191 +
1.192 + ///Return a vector multiplied by a scalar
1.193 + ///\relates Point
1.194 + template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
1.195 + return x*u;
1.196 + }
1.197 +
1.198 + ///Read a plainvector from a stream
1.199 +
1.200 + ///Read a plainvector from a stream
1.201 + ///\relates Point
1.202 + ///
1.203 + template<typename T>
1.204 + inline std::istream& operator>>(std::istream &is, Point<T> &z) {
1.205 + char c;
1.206 + if (is >> c) {
1.207 + if (c != '(') is.putback(c);
1.208 + } else {
1.209 + is.clear();
1.210 + }
1.211 + if (!(is >> z.x)) return is;
1.212 + if (is >> c) {
1.213 + if (c != ',') is.putback(c);
1.214 + } else {
1.215 + is.clear();
1.216 + }
1.217 + if (!(is >> z.y)) return is;
1.218 + if (is >> c) {
1.219 + if (c != ')') is.putback(c);
1.220 + } else {
1.221 + is.clear();
1.222 + }
1.223 + return is;
1.224 + }
1.225 +
1.226 + ///Write a plainvector to a stream
1.227 +
1.228 + ///Write a plainvector to a stream
1.229 + ///\relates Point
1.230 + ///
1.231 + template<typename T>
1.232 + inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
1.233 + {
1.234 + os << "(" << z.x << ", " << z.y << ")";
1.235 + return os;
1.236 + }
1.237 +
1.238 + ///Rotate by 90 degrees
1.239 +
1.240 + ///Returns its parameter rotated by 90 degrees in positive direction.
1.241 + ///\relates Point
1.242 + ///
1.243 + template<typename T>
1.244 + inline Point<T> rot90(const Point<T> &z)
1.245 + {
1.246 + return Point<T>(-z.y,z.x);
1.247 + }
1.248 +
1.249 + ///Rotate by 180 degrees
1.250 +
1.251 + ///Returns its parameter rotated by 180 degrees.
1.252 + ///\relates Point
1.253 + ///
1.254 + template<typename T>
1.255 + inline Point<T> rot180(const Point<T> &z)
1.256 + {
1.257 + return Point<T>(-z.x,-z.y);
1.258 + }
1.259 +
1.260 + ///Rotate by 270 degrees
1.261 +
1.262 + ///Returns its parameter rotated by 90 degrees in negative direction.
1.263 + ///\relates Point
1.264 + ///
1.265 + template<typename T>
1.266 + inline Point<T> rot270(const Point<T> &z)
1.267 + {
1.268 + return Point<T>(z.y,-z.x);
1.269 + }
1.270 +
1.271 +
1.272 +
1.273 + /// A class to calculate or store the bounding box of plainvectors.
1.274 +
1.275 + /// A class to calculate or store the bounding box of plainvectors.
1.276 + ///
1.277 + ///\author Attila Bernath
1.278 + template<typename T>
1.279 + class BoundingBox {
1.280 + Point<T> bottom_left, top_right;
1.281 + bool _empty;
1.282 + public:
1.283 +
1.284 + ///Default constructor: creates an empty bounding box
1.285 + BoundingBox() { _empty = true; }
1.286 +
1.287 + ///Construct an instance from one point
1.288 + BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
1.289 +
1.290 + ///Construct an instance from two points
1.291 +
1.292 + ///Construct an instance from two points
1.293 + ///\warning The coordinates of the bottom-left corner must be no more
1.294 + ///than those of the top-right one
1.295 + BoundingBox(Point<T> a,Point<T> b)
1.296 + {
1.297 + bottom_left=a;
1.298 + top_right=b;
1.299 + _empty = false;
1.300 + }
1.301 +
1.302 + ///Construct an instance from four numbers
1.303 +
1.304 + ///Construct an instance from four numbers
1.305 + ///\warning The coordinates of the bottom-left corner must be no more
1.306 + ///than those of the top-right one
1.307 + BoundingBox(T l,T b,T r,T t)
1.308 + {
1.309 + bottom_left=Point<T>(l,b);
1.310 + top_right=Point<T>(r,t);
1.311 + _empty = false;
1.312 + }
1.313 +
1.314 + ///Were any points added?
1.315 + bool empty() const {
1.316 + return _empty;
1.317 + }
1.318 +
1.319 + ///Make the BoundingBox empty
1.320 + void clear() {
1.321 + _empty=1;
1.322 + }
1.323 +
1.324 + ///Give back the bottom left corner
1.325 +
1.326 + ///Give back the bottom left corner.
1.327 + ///If the bounding box is empty, then the return value is not defined.
1.328 + Point<T> bottomLeft() const {
1.329 + return bottom_left;
1.330 + }
1.331 +
1.332 + ///Set the bottom left corner
1.333 +
1.334 + ///Set the bottom left corner.
1.335 + ///It should only bee used for non-empty box.
1.336 + void bottomLeft(Point<T> p) {
1.337 + bottom_left = p;
1.338 + }
1.339 +
1.340 + ///Give back the top right corner
1.341 +
1.342 + ///Give back the top right corner.
1.343 + ///If the bounding box is empty, then the return value is not defined.
1.344 + Point<T> topRight() const {
1.345 + return top_right;
1.346 + }
1.347 +
1.348 + ///Set the top right corner
1.349 +
1.350 + ///Set the top right corner.
1.351 + ///It should only bee used for non-empty box.
1.352 + void topRight(Point<T> p) {
1.353 + top_right = p;
1.354 + }
1.355 +
1.356 + ///Give back the bottom right corner
1.357 +
1.358 + ///Give back the bottom right corner.
1.359 + ///If the bounding box is empty, then the return value is not defined.
1.360 + Point<T> bottomRight() const {
1.361 + return Point<T>(top_right.x,bottom_left.y);
1.362 + }
1.363 +
1.364 + ///Set the bottom right corner
1.365 +
1.366 + ///Set the bottom right corner.
1.367 + ///It should only bee used for non-empty box.
1.368 + void bottomRight(Point<T> p) {
1.369 + top_right.x = p.x;
1.370 + bottom_left.y = p.y;
1.371 + }
1.372 +
1.373 + ///Give back the top left corner
1.374 +
1.375 + ///Give back the top left corner.
1.376 + ///If the bounding box is empty, then the return value is not defined.
1.377 + Point<T> topLeft() const {
1.378 + return Point<T>(bottom_left.x,top_right.y);
1.379 + }
1.380 +
1.381 + ///Set the top left corner
1.382 +
1.383 + ///Set the top left corner.
1.384 + ///It should only bee used for non-empty box.
1.385 + void topLeft(Point<T> p) {
1.386 + top_right.y = p.y;
1.387 + bottom_left.x = p.x;
1.388 + }
1.389 +
1.390 + ///Give back the bottom of the box
1.391 +
1.392 + ///Give back the bottom of the box.
1.393 + ///If the bounding box is empty, then the return value is not defined.
1.394 + T bottom() const {
1.395 + return bottom_left.y;
1.396 + }
1.397 +
1.398 + ///Set the bottom of the box
1.399 +
1.400 + ///Set the bottom of the box.
1.401 + ///It should only bee used for non-empty box.
1.402 + void bottom(T t) {
1.403 + bottom_left.y = t;
1.404 + }
1.405 +
1.406 + ///Give back the top of the box
1.407 +
1.408 + ///Give back the top of the box.
1.409 + ///If the bounding box is empty, then the return value is not defined.
1.410 + T top() const {
1.411 + return top_right.y;
1.412 + }
1.413 +
1.414 + ///Set the top of the box
1.415 +
1.416 + ///Set the top of the box.
1.417 + ///It should only bee used for non-empty box.
1.418 + void top(T t) {
1.419 + top_right.y = t;
1.420 + }
1.421 +
1.422 + ///Give back the left side of the box
1.423 +
1.424 + ///Give back the left side of the box.
1.425 + ///If the bounding box is empty, then the return value is not defined.
1.426 + T left() const {
1.427 + return bottom_left.x;
1.428 + }
1.429 +
1.430 + ///Set the left side of the box
1.431 +
1.432 + ///Set the left side of the box.
1.433 + ///It should only bee used for non-empty box
1.434 + void left(T t) {
1.435 + bottom_left.x = t;
1.436 + }
1.437 +
1.438 + /// Give back the right side of the box
1.439 +
1.440 + /// Give back the right side of the box.
1.441 + ///If the bounding box is empty, then the return value is not defined.
1.442 + T right() const {
1.443 + return top_right.x;
1.444 + }
1.445 +
1.446 + ///Set the right side of the box
1.447 +
1.448 + ///Set the right side of the box.
1.449 + ///It should only bee used for non-empty box
1.450 + void right(T t) {
1.451 + top_right.x = t;
1.452 + }
1.453 +
1.454 + ///Give back the height of the box
1.455 +
1.456 + ///Give back the height of the box.
1.457 + ///If the bounding box is empty, then the return value is not defined.
1.458 + T height() const {
1.459 + return top_right.y-bottom_left.y;
1.460 + }
1.461 +
1.462 + ///Give back the width of the box
1.463 +
1.464 + ///Give back the width of the box.
1.465 + ///If the bounding box is empty, then the return value is not defined.
1.466 + T width() const {
1.467 + return top_right.x-bottom_left.x;
1.468 + }
1.469 +
1.470 + ///Checks whether a point is inside a bounding box
1.471 + bool inside(const Point<T>& u){
1.472 + if (_empty)
1.473 + return false;
1.474 + else{
1.475 + return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
1.476 + (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
1.477 + }
1.478 + }
1.479 +
1.480 + ///Increments a bounding box with a point
1.481 + BoundingBox& add(const Point<T>& u){
1.482 + if (_empty){
1.483 + bottom_left=top_right=u;
1.484 + _empty = false;
1.485 + }
1.486 + else{
1.487 + if (bottom_left.x > u.x) bottom_left.x = u.x;
1.488 + if (bottom_left.y > u.y) bottom_left.y = u.y;
1.489 + if (top_right.x < u.x) top_right.x = u.x;
1.490 + if (top_right.y < u.y) top_right.y = u.y;
1.491 + }
1.492 + return *this;
1.493 + }
1.494 +
1.495 + ///Increments a bounding to contain another bounding box
1.496 + BoundingBox& add(const BoundingBox &u){
1.497 + if ( !u.empty() ){
1.498 + this->add(u.bottomLeft());
1.499 + this->add(u.topRight());
1.500 + }
1.501 + return *this;
1.502 + }
1.503 +
1.504 + ///Intersection of two bounding boxes
1.505 + BoundingBox operator &(const BoundingBox& u){
1.506 + BoundingBox b;
1.507 + b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
1.508 + b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
1.509 + b.top_right.x=std::min(this->top_right.x,u.top_right.x);
1.510 + b.top_right.y=std::min(this->top_right.y,u.top_right.y);
1.511 + b._empty = this->_empty || u._empty ||
1.512 + b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
1.513 + return b;
1.514 + }
1.515 +
1.516 + };//class Boundingbox
1.517 +
1.518 +
1.519 + ///Map of x-coordinates of a dim2::Point<>-map
1.520 +
1.521 + ///\ingroup maps
1.522 + ///Map of x-coordinates of a dim2::Point<>-map
1.523 + ///
1.524 + template<class M>
1.525 + class XMap
1.526 + {
1.527 + M& _map;
1.528 + public:
1.529 +
1.530 + typedef typename M::Value::Value Value;
1.531 + typedef typename M::Key Key;
1.532 + ///\e
1.533 + XMap(M& map) : _map(map) {}
1.534 + Value operator[](Key k) const {return _map[k].x;}
1.535 + void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
1.536 + };
1.537 +
1.538 + ///Returns an \ref XMap class
1.539 +
1.540 + ///This function just returns an \ref XMap class.
1.541 + ///
1.542 + ///\ingroup maps
1.543 + ///\relates XMap
1.544 + template<class M>
1.545 + inline XMap<M> xMap(M &m)
1.546 + {
1.547 + return XMap<M>(m);
1.548 + }
1.549 +
1.550 + template<class M>
1.551 + inline XMap<M> xMap(const M &m)
1.552 + {
1.553 + return XMap<M>(m);
1.554 + }
1.555 +
1.556 + ///Constant (read only) version of \ref XMap
1.557 +
1.558 + ///\ingroup maps
1.559 + ///Constant (read only) version of \ref XMap
1.560 + ///
1.561 + template<class M>
1.562 + class ConstXMap
1.563 + {
1.564 + const M& _map;
1.565 + public:
1.566 +
1.567 + typedef typename M::Value::Value Value;
1.568 + typedef typename M::Key Key;
1.569 + ///\e
1.570 + ConstXMap(const M &map) : _map(map) {}
1.571 + Value operator[](Key k) const {return _map[k].x;}
1.572 + };
1.573 +
1.574 + ///Returns a \ref ConstXMap class
1.575 +
1.576 + ///This function just returns an \ref ConstXMap class.
1.577 + ///
1.578 + ///\ingroup maps
1.579 + ///\relates ConstXMap
1.580 + template<class M>
1.581 + inline ConstXMap<M> xMap(const M &m)
1.582 + {
1.583 + return ConstXMap<M>(m);
1.584 + }
1.585 +
1.586 + ///Map of y-coordinates of a dim2::Point<>-map
1.587 +
1.588 + ///\ingroup maps
1.589 + ///Map of y-coordinates of a dim2::Point<>-map
1.590 + ///
1.591 + template<class M>
1.592 + class YMap
1.593 + {
1.594 + M& _map;
1.595 + public:
1.596 +
1.597 + typedef typename M::Value::Value Value;
1.598 + typedef typename M::Key Key;
1.599 + ///\e
1.600 + YMap(M& map) : _map(map) {}
1.601 + Value operator[](Key k) const {return _map[k].y;}
1.602 + void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
1.603 + };
1.604 +
1.605 + ///Returns an \ref YMap class
1.606 +
1.607 + ///This function just returns an \ref YMap class.
1.608 + ///
1.609 + ///\ingroup maps
1.610 + ///\relates YMap
1.611 + template<class M>
1.612 + inline YMap<M> yMap(M &m)
1.613 + {
1.614 + return YMap<M>(m);
1.615 + }
1.616 +
1.617 + template<class M>
1.618 + inline YMap<M> yMap(const M &m)
1.619 + {
1.620 + return YMap<M>(m);
1.621 + }
1.622 +
1.623 + ///Constant (read only) version of \ref YMap
1.624 +
1.625 + ///\ingroup maps
1.626 + ///Constant (read only) version of \ref YMap
1.627 + ///
1.628 + template<class M>
1.629 + class ConstYMap
1.630 + {
1.631 + const M& _map;
1.632 + public:
1.633 +
1.634 + typedef typename M::Value::Value Value;
1.635 + typedef typename M::Key Key;
1.636 + ///\e
1.637 + ConstYMap(const M &map) : _map(map) {}
1.638 + Value operator[](Key k) const {return _map[k].y;}
1.639 + };
1.640 +
1.641 + ///Returns a \ref ConstYMap class
1.642 +
1.643 + ///This function just returns an \ref ConstYMap class.
1.644 + ///
1.645 + ///\ingroup maps
1.646 + ///\relates ConstYMap
1.647 + template<class M>
1.648 + inline ConstYMap<M> yMap(const M &m)
1.649 + {
1.650 + return ConstYMap<M>(m);
1.651 + }
1.652 +
1.653 +
1.654 + ///\brief Map of the \ref Point::normSquare() "normSquare()"
1.655 + ///of an \ref Point "Point"-map
1.656 + ///
1.657 + ///Map of the \ref Point::normSquare() "normSquare()"
1.658 + ///of an \ref Point "Point"-map
1.659 + ///\ingroup maps
1.660 + ///
1.661 + template<class M>
1.662 + class NormSquareMap
1.663 + {
1.664 + const M& _map;
1.665 + public:
1.666 +
1.667 + typedef typename M::Value::Value Value;
1.668 + typedef typename M::Key Key;
1.669 + ///\e
1.670 + NormSquareMap(const M &map) : _map(map) {}
1.671 + Value operator[](Key k) const {return _map[k].normSquare();}
1.672 + };
1.673 +
1.674 + ///Returns a \ref NormSquareMap class
1.675 +
1.676 + ///This function just returns an \ref NormSquareMap class.
1.677 + ///
1.678 + ///\ingroup maps
1.679 + ///\relates NormSquareMap
1.680 + template<class M>
1.681 + inline NormSquareMap<M> normSquareMap(const M &m)
1.682 + {
1.683 + return NormSquareMap<M>(m);
1.684 + }
1.685 +
1.686 + /// @}
1.687 +
1.688 + } //namespce dim2
1.689 +
1.690 +} //namespace lemon
1.691 +
1.692 +#endif //LEMON_DIM2_H