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