alpar@2658: /* -*- C++ -*- alpar@2658: * alpar@2658: * This file is a part of LEMON, a generic C++ optimization library alpar@2658: * alpar@2658: * Copyright (C) 2003-2006 alpar@2658: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@2658: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@2658: * alpar@2658: * Permission to use, modify and distribute this software is granted alpar@2658: * provided that this copyright notice appears in all copies. For alpar@2658: * precise terms see the accompanying LICENSE file. alpar@2658: * alpar@2658: * This software is provided "AS IS" with no warranty of any kind, alpar@2658: * express or implied, and with no claim as to its suitability for any alpar@2658: * purpose. alpar@2658: * alpar@2658: */ alpar@2658: alpar@2658: #ifndef LEMON_DIM2_H alpar@2658: #define LEMON_DIM2_H alpar@2658: alpar@2658: #include alpar@2658: #include alpar@2658: alpar@2658: ///\ingroup misc alpar@2658: ///\file alpar@2658: ///\brief A simple two dimensional vector and a bounding box implementation alpar@2658: /// alpar@2658: /// The class \ref lemon::dim2::Point "dim2::Point" implements alpar@2658: ///a two dimensional vector with the usual alpar@2658: /// operations. alpar@2658: /// alpar@2658: /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox" alpar@2658: /// can be used to determine alpar@2658: /// the rectangular bounding box of a set of alpar@2658: /// \ref lemon::dim2::Point "dim2::Point"'s. alpar@2658: /// alpar@2658: ///\author Attila Bernath alpar@2658: alpar@2658: alpar@2658: namespace lemon { alpar@2658: alpar@2658: ///Tools for handling two dimensional coordinates alpar@2658: alpar@2658: ///This namespace is a storage of several alpar@2658: ///tools for handling two dimensional coordinates alpar@2658: namespace dim2 { alpar@2658: alpar@2658: /// \addtogroup misc alpar@2658: /// @{ alpar@2658: alpar@2658: /// A simple two dimensional vector (plainvector) implementation alpar@2658: alpar@2658: /// A simple two dimensional vector (plainvector) implementation alpar@2658: ///with the usual vector alpar@2658: /// operators. alpar@2658: /// alpar@2658: template alpar@2658: class Point { alpar@2658: alpar@2658: public: alpar@2658: alpar@2658: typedef T Value; alpar@2658: alpar@2658: ///First co-ordinate alpar@2658: T x; alpar@2658: ///Second co-ordinate alpar@2658: T y; alpar@2658: alpar@2658: ///Default constructor alpar@2658: Point() {} alpar@2658: alpar@2658: ///Construct an instance from coordinates alpar@2658: Point(T a, T b) : x(a), y(b) { } alpar@2658: alpar@2658: ///The dimension of the vector. alpar@2658: alpar@2658: ///This class give back always 2. alpar@2658: /// alpar@2658: int size() const { return 2; } alpar@2658: alpar@2658: ///Subscripting operator alpar@2658: alpar@2658: ///\c p[0] is \c p.x and \c p[1] is \c p.y alpar@2658: /// alpar@2658: T& operator[](int idx) { return idx == 0 ? x : y; } alpar@2658: alpar@2658: ///Const subscripting operator alpar@2658: alpar@2658: ///\c p[0] is \c p.x and \c p[1] is \c p.y alpar@2658: /// alpar@2658: const T& operator[](int idx) const { return idx == 0 ? x : y; } alpar@2658: alpar@2658: ///Conversion constructor alpar@2658: template Point(const Point &p) : x(p.x), y(p.y) {} alpar@2658: alpar@2658: ///Give back the square of the norm of the vector alpar@2658: T normSquare() const { alpar@2658: return x*x+y*y; alpar@2658: } alpar@2658: alpar@2658: ///Increment the left hand side by u alpar@2658: Point& operator +=(const Point& u) { alpar@2658: x += u.x; alpar@2658: y += u.y; alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Decrement the left hand side by u alpar@2658: Point& operator -=(const Point& u) { alpar@2658: x -= u.x; alpar@2658: y -= u.y; alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Multiply the left hand side with a scalar alpar@2658: Point& operator *=(const T &u) { alpar@2658: x *= u; alpar@2658: y *= u; alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Divide the left hand side by a scalar alpar@2658: Point& operator /=(const T &u) { alpar@2658: x /= u; alpar@2658: y /= u; alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Return the scalar product of two vectors alpar@2658: T operator *(const Point& u) const { alpar@2658: return x*u.x+y*u.y; alpar@2658: } alpar@2658: alpar@2658: ///Return the sum of two vectors alpar@2658: Point operator+(const Point &u) const { alpar@2658: Point b=*this; alpar@2658: return b+=u; alpar@2658: } alpar@2658: alpar@2658: ///Return the neg of the vectors alpar@2658: Point operator-() const { alpar@2658: Point b=*this; alpar@2658: b.x=-b.x; b.y=-b.y; alpar@2658: return b; alpar@2658: } alpar@2658: alpar@2658: ///Return the difference of two vectors alpar@2658: Point operator-(const Point &u) const { alpar@2658: Point b=*this; alpar@2658: return b-=u; alpar@2658: } alpar@2658: alpar@2658: ///Return a vector multiplied by a scalar alpar@2658: Point operator*(const T &u) const { alpar@2658: Point b=*this; alpar@2658: return b*=u; alpar@2658: } alpar@2658: alpar@2658: ///Return a vector divided by a scalar alpar@2658: Point operator/(const T &u) const { alpar@2658: Point b=*this; alpar@2658: return b/=u; alpar@2658: } alpar@2658: alpar@2658: ///Test equality alpar@2658: bool operator==(const Point &u) const { alpar@2658: return (x==u.x) && (y==u.y); alpar@2658: } alpar@2658: alpar@2658: ///Test inequality alpar@2658: bool operator!=(Point u) const { alpar@2658: return (x!=u.x) || (y!=u.y); alpar@2658: } alpar@2658: alpar@2658: }; alpar@2658: alpar@2658: ///Return an Point alpar@2658: alpar@2658: ///Return an Point alpar@2658: ///\relates Point alpar@2658: template alpar@2658: inline Point makePoint(const T& x, const T& y) { alpar@2658: return Point(x, y); alpar@2658: } alpar@2658: alpar@2658: ///Return a vector multiplied by a scalar alpar@2658: alpar@2658: ///Return a vector multiplied by a scalar alpar@2658: ///\relates Point alpar@2658: template Point operator*(const T &u,const Point &x) { alpar@2658: return x*u; alpar@2658: } alpar@2658: alpar@2658: ///Read a plainvector from a stream alpar@2658: alpar@2658: ///Read a plainvector from a stream alpar@2658: ///\relates Point alpar@2658: /// alpar@2658: template alpar@2658: inline std::istream& operator>>(std::istream &is, Point &z) { alpar@2658: char c; alpar@2658: if (is >> c) { alpar@2658: if (c != '(') is.putback(c); alpar@2658: } else { alpar@2658: is.clear(); alpar@2658: } alpar@2658: if (!(is >> z.x)) return is; alpar@2658: if (is >> c) { alpar@2658: if (c != ',') is.putback(c); alpar@2658: } else { alpar@2658: is.clear(); alpar@2658: } alpar@2658: if (!(is >> z.y)) return is; alpar@2658: if (is >> c) { alpar@2658: if (c != ')') is.putback(c); alpar@2658: } else { alpar@2658: is.clear(); alpar@2658: } alpar@2658: return is; alpar@2658: } alpar@2658: alpar@2658: ///Write a plainvector to a stream alpar@2658: alpar@2658: ///Write a plainvector to a stream alpar@2658: ///\relates Point alpar@2658: /// alpar@2658: template alpar@2658: inline std::ostream& operator<<(std::ostream &os, const Point& z) alpar@2658: { alpar@2658: os << "(" << z.x << ", " << z.y << ")"; alpar@2658: return os; alpar@2658: } alpar@2658: alpar@2658: ///Rotate by 90 degrees alpar@2658: alpar@2658: ///Returns its parameter rotated by 90 degrees in positive direction. alpar@2658: ///\relates Point alpar@2658: /// alpar@2658: template alpar@2658: inline Point rot90(const Point &z) alpar@2658: { alpar@2658: return Point(-z.y,z.x); alpar@2658: } alpar@2658: alpar@2658: ///Rotate by 180 degrees alpar@2658: alpar@2658: ///Returns its parameter rotated by 180 degrees. alpar@2658: ///\relates Point alpar@2658: /// alpar@2658: template alpar@2658: inline Point rot180(const Point &z) alpar@2658: { alpar@2658: return Point(-z.x,-z.y); alpar@2658: } alpar@2658: alpar@2658: ///Rotate by 270 degrees alpar@2658: alpar@2658: ///Returns its parameter rotated by 90 degrees in negative direction. alpar@2658: ///\relates Point alpar@2658: /// alpar@2658: template alpar@2658: inline Point rot270(const Point &z) alpar@2658: { alpar@2658: return Point(z.y,-z.x); alpar@2658: } alpar@2658: alpar@2658: alpar@2658: alpar@2658: /// A class to calculate or store the bounding box of plainvectors. alpar@2658: alpar@2658: /// A class to calculate or store the bounding box of plainvectors. alpar@2658: /// alpar@2658: ///\author Attila Bernath alpar@2658: template alpar@2658: class BoundingBox { alpar@2658: Point bottom_left, top_right; alpar@2658: bool _empty; alpar@2658: public: alpar@2658: alpar@2658: ///Default constructor: creates an empty bounding box alpar@2658: BoundingBox() { _empty = true; } alpar@2658: alpar@2658: ///Construct an instance from one point alpar@2658: BoundingBox(Point a) { bottom_left=top_right=a; _empty = false; } alpar@2658: alpar@2658: ///Were any points added? alpar@2658: bool empty() const { alpar@2658: return _empty; alpar@2658: } alpar@2658: alpar@2658: ///Make the BoundingBox empty alpar@2658: void clear() { alpar@2658: _empty=1; alpar@2658: } alpar@2658: alpar@2658: ///Give back the bottom left corner alpar@2658: alpar@2658: ///Give back the bottom left corner. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: Point bottomLeft() const { alpar@2658: return bottom_left; alpar@2658: } alpar@2658: alpar@2658: ///Set the bottom left corner alpar@2658: alpar@2658: ///Set the bottom left corner. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void bottomLeft(Point p) { alpar@2658: bottom_left = p; alpar@2658: } alpar@2658: alpar@2658: ///Give back the top right corner alpar@2658: alpar@2658: ///Give back the top right corner. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: Point topRight() const { alpar@2658: return top_right; alpar@2658: } alpar@2658: alpar@2658: ///Set the top right corner alpar@2658: alpar@2658: ///Set the top right corner. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void topRight(Point p) { alpar@2658: top_right = p; alpar@2658: } alpar@2658: alpar@2658: ///Give back the bottom right corner alpar@2658: alpar@2658: ///Give back the bottom right corner. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: Point bottomRight() const { alpar@2658: return Point(top_right.x,bottom_left.y); alpar@2658: } alpar@2658: alpar@2658: ///Set the bottom right corner alpar@2658: alpar@2658: ///Set the bottom right corner. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void bottomRight(Point p) { alpar@2658: top_right.x = p.x; alpar@2658: bottom_left.y = p.y; alpar@2658: } alpar@2658: alpar@2658: ///Give back the top left corner alpar@2658: alpar@2658: ///Give back the top left corner. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: Point topLeft() const { alpar@2658: return Point(bottom_left.x,top_right.y); alpar@2658: } alpar@2658: alpar@2658: ///Set the top left corner alpar@2658: alpar@2658: ///Set the top left corner. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void topLeft(Point p) { alpar@2658: top_right.y = p.y; alpar@2658: bottom_left.x = p.x; alpar@2658: } alpar@2658: alpar@2658: ///Give back the bottom of the box alpar@2658: alpar@2658: ///Give back the bottom of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T bottom() const { alpar@2658: return bottom_left.y; alpar@2658: } alpar@2658: alpar@2658: ///Set the bottom of the box alpar@2658: alpar@2658: ///Set the bottom of the box. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void bottom(T t) { alpar@2658: bottom_left.y = t; alpar@2658: } alpar@2658: alpar@2658: ///Give back the top of the box alpar@2658: alpar@2658: ///Give back the top of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T top() const { alpar@2658: return top_right.y; alpar@2658: } alpar@2658: alpar@2658: ///Set the top of the box alpar@2658: alpar@2658: ///Set the top of the box. alpar@2658: ///It should only bee used for non-empty box. alpar@2658: void top(T t) { alpar@2658: top_right.y = t; alpar@2658: } alpar@2658: alpar@2658: ///Give back the left side of the box alpar@2658: alpar@2658: ///Give back the left side of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T left() const { alpar@2658: return bottom_left.x; alpar@2658: } alpar@2658: alpar@2658: ///Set the left side of the box alpar@2658: alpar@2658: ///Set the left side of the box. alpar@2658: ///It should only bee used for non-empty box alpar@2658: void left(T t) { alpar@2658: bottom_left.x = t; alpar@2658: } alpar@2658: alpar@2658: /// Give back the right side of the box alpar@2658: alpar@2658: /// Give back the right side of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T right() const { alpar@2658: return top_right.x; alpar@2658: } alpar@2658: alpar@2658: ///Set the right side of the box alpar@2658: alpar@2658: ///Set the right side of the box. alpar@2658: ///It should only bee used for non-empty box alpar@2658: void right(T t) { alpar@2658: top_right.x = t; alpar@2658: } alpar@2658: alpar@2658: ///Give back the height of the box alpar@2658: alpar@2658: ///Give back the height of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T height() const { alpar@2658: return top_right.y-bottom_left.y; alpar@2658: } alpar@2658: alpar@2658: ///Give back the width of the box alpar@2658: alpar@2658: ///Give back the width of the box. alpar@2658: ///If the bounding box is empty, then the return value is not defined. alpar@2658: T width() const { alpar@2658: return top_right.x-bottom_left.x; alpar@2658: } alpar@2658: alpar@2658: ///Checks whether a point is inside a bounding box alpar@2658: bool inside(const Point& u){ alpar@2658: if (_empty) alpar@2658: return false; alpar@2658: else{ alpar@2658: return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && alpar@2658: (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); alpar@2658: } alpar@2658: } alpar@2658: alpar@2658: ///Increments a bounding box with a point alpar@2658: BoundingBox& add(const Point& u){ alpar@2658: if (_empty){ alpar@2658: bottom_left=top_right=u; alpar@2658: _empty = false; alpar@2658: } alpar@2658: else{ alpar@2658: if (bottom_left.x > u.x) bottom_left.x = u.x; alpar@2658: if (bottom_left.y > u.y) bottom_left.y = u.y; alpar@2658: if (top_right.x < u.x) top_right.x = u.x; alpar@2658: if (top_right.y < u.y) top_right.y = u.y; alpar@2658: } alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Increments a bounding to contain another bounding box alpar@2658: BoundingBox& add(const BoundingBox &u){ alpar@2658: if ( !u.empty() ){ alpar@2658: this->add(u.bottomLeft()); alpar@2658: this->add(u.topRight()); alpar@2658: } alpar@2658: return *this; alpar@2658: } alpar@2658: alpar@2658: ///Intersection of two bounding boxes alpar@2658: BoundingBox operator &(const BoundingBox& u){ alpar@2658: BoundingBox b; alpar@2658: b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x); alpar@2658: b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y); alpar@2658: b.top_right.x=std::min(this->top_right.x,u.top_right.x); alpar@2658: b.top_right.y=std::min(this->top_right.y,u.top_right.y); alpar@2658: b._empty = this->_empty || u._empty || alpar@2658: b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y; alpar@2658: return b; alpar@2658: } alpar@2658: alpar@2658: };//class Boundingbox alpar@2658: alpar@2658: alpar@2658: ///Map of x-coordinates of a dim2::Point<>-map alpar@2658: alpar@2658: ///\ingroup maps alpar@2658: ///Map of x-coordinates of a dim2::Point<>-map alpar@2658: /// alpar@2658: template alpar@2658: class XMap alpar@2658: { alpar@2658: M& _map; alpar@2658: public: alpar@2658: alpar@2658: typedef typename M::Value::Value Value; alpar@2658: typedef typename M::Key Key; alpar@2658: ///\e alpar@2658: XMap(M& map) : _map(map) {} alpar@2658: Value operator[](Key k) const {return _map[k].x;} alpar@2658: void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));} alpar@2658: }; alpar@2658: alpar@2658: ///Returns an \ref XMap class alpar@2658: alpar@2658: ///This function just returns an \ref XMap class. alpar@2658: /// alpar@2658: ///\ingroup maps alpar@2658: ///\relates XMap alpar@2658: template alpar@2658: inline XMap xMap(M &m) alpar@2658: { alpar@2658: return XMap(m); alpar@2658: } alpar@2658: alpar@2658: template alpar@2658: inline XMap xMap(const M &m) alpar@2658: { alpar@2658: return XMap(m); alpar@2658: } alpar@2658: alpar@2658: ///Constant (read only) version of \ref XMap alpar@2658: alpar@2658: ///\ingroup maps alpar@2658: ///Constant (read only) version of \ref XMap alpar@2658: /// alpar@2658: template alpar@2658: class ConstXMap alpar@2658: { alpar@2658: const M& _map; alpar@2658: public: alpar@2658: alpar@2658: typedef typename M::Value::Value Value; alpar@2658: typedef typename M::Key Key; alpar@2658: ///\e alpar@2658: ConstXMap(const M &map) : _map(map) {} alpar@2658: Value operator[](Key k) const {return _map[k].x;} alpar@2658: }; alpar@2658: alpar@2658: ///Returns a \ref ConstXMap class alpar@2658: alpar@2658: ///This function just returns an \ref ConstXMap class. alpar@2658: /// alpar@2658: ///\ingroup maps alpar@2658: ///\relates ConstXMap alpar@2658: template alpar@2658: inline ConstXMap xMap(const M &m) alpar@2658: { alpar@2658: return ConstXMap(m); alpar@2658: } alpar@2658: alpar@2658: ///Map of y-coordinates of a dim2::Point<>-map alpar@2658: alpar@2658: ///\ingroup maps alpar@2658: ///Map of y-coordinates of a dim2::Point<>-map alpar@2658: /// alpar@2658: template alpar@2658: class YMap alpar@2658: { alpar@2658: M& _map; alpar@2658: public: alpar@2658: alpar@2658: typedef typename M::Value::Value Value; alpar@2658: typedef typename M::Key Key; alpar@2658: ///\e alpar@2658: YMap(M& map) : _map(map) {} alpar@2658: Value operator[](Key k) const {return _map[k].y;} alpar@2658: void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));} alpar@2658: }; alpar@2658: alpar@2658: ///Returns an \ref YMap class alpar@2658: alpar@2658: ///This function just returns an \ref YMap class. alpar@2658: /// alpar@2658: ///\ingroup maps alpar@2658: ///\relates YMap alpar@2658: template alpar@2658: inline YMap yMap(M &m) alpar@2658: { alpar@2658: return YMap(m); alpar@2658: } alpar@2658: alpar@2658: template alpar@2658: inline YMap yMap(const M &m) alpar@2658: { alpar@2658: return YMap(m); alpar@2658: } alpar@2658: alpar@2658: ///Constant (read only) version of \ref YMap alpar@2658: alpar@2658: ///\ingroup maps alpar@2658: ///Constant (read only) version of \ref YMap alpar@2658: /// alpar@2658: template alpar@2658: class ConstYMap alpar@2658: { alpar@2658: const M& _map; alpar@2658: public: alpar@2658: alpar@2658: typedef typename M::Value::Value Value; alpar@2658: typedef typename M::Key Key; alpar@2658: ///\e alpar@2658: ConstYMap(const M &map) : _map(map) {} alpar@2658: Value operator[](Key k) const {return _map[k].y;} alpar@2658: }; alpar@2658: alpar@2658: ///Returns a \ref ConstYMap class alpar@2658: alpar@2658: ///This function just returns an \ref ConstYMap class. alpar@2658: /// alpar@2658: ///\ingroup maps alpar@2658: ///\relates ConstYMap alpar@2658: template alpar@2658: inline ConstYMap yMap(const M &m) alpar@2658: { alpar@2658: return ConstYMap(m); alpar@2658: } alpar@2658: alpar@2658: alpar@2658: ///\brief Map of the \ref Point::normSquare() "normSquare()" alpar@2658: ///of an \ref Point "Point"-map alpar@2658: /// alpar@2658: ///Map of the \ref Point::normSquare() "normSquare()" alpar@2658: ///of an \ref Point "Point"-map alpar@2658: ///\ingroup maps alpar@2658: /// alpar@2658: template alpar@2658: class NormSquareMap alpar@2658: { alpar@2658: const M& _map; alpar@2658: public: alpar@2658: alpar@2658: typedef typename M::Value::Value Value; alpar@2658: typedef typename M::Key Key; alpar@2658: ///\e alpar@2658: NormSquareMap(const M &map) : _map(map) {} alpar@2658: Value operator[](Key k) const {return _map[k].normSquare();} alpar@2658: }; alpar@2658: alpar@2658: ///Returns a \ref NormSquareMap class alpar@2658: alpar@2658: ///This function just returns an \ref NormSquareMap class. alpar@2658: /// alpar@2658: ///\ingroup maps alpar@2658: ///\relates NormSquareMap alpar@2658: template alpar@2658: inline NormSquareMap normSquareMap(const M &m) alpar@2658: { alpar@2658: return NormSquareMap(m); alpar@2658: } alpar@2658: alpar@2658: /// @} alpar@2658: alpar@2658: } //namespce dim2 alpar@2658: alpar@2658: } //namespace lemon alpar@2658: alpar@2658: #endif //LEMON_DIM2_H