lemon/dim2.h
changeset 161 2c999941b871
parent 42 3a98515e9bc3
child 209 765619b7cbb2
equal deleted inserted replaced
3:41c9d131f25e 4:0ec0a16fabe7
    25 ///\ingroup misc
    25 ///\ingroup misc
    26 ///\file
    26 ///\file
    27 ///\brief A simple two dimensional vector and a bounding box implementation 
    27 ///\brief A simple two dimensional vector and a bounding box implementation 
    28 ///
    28 ///
    29 /// The class \ref lemon::dim2::Point "dim2::Point" implements
    29 /// The class \ref lemon::dim2::Point "dim2::Point" implements
    30 ///a two dimensional vector with the usual
    30 /// a two dimensional vector with the usual operations.
    31 /// operations.
       
    32 ///
    31 ///
    33 /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
    32 /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
    34 /// can be used to determine
    33 /// can be used to determine
    35 /// the rectangular bounding box of a set of
    34 /// the rectangular bounding box of a set of
    36 /// \ref lemon::dim2::Point "dim2::Point"'s.
    35 /// \ref lemon::dim2::Point "dim2::Point"'s.
    47   /// @{
    46   /// @{
    48 
    47 
    49   /// A simple two dimensional vector (plainvector) implementation
    48   /// A simple two dimensional vector (plainvector) implementation
    50 
    49 
    51   /// A simple two dimensional vector (plainvector) implementation
    50   /// A simple two dimensional vector (plainvector) implementation
    52   ///with the usual vector
    51   /// with the usual vector operations.
    53   /// operators.
       
    54   ///
       
    55   template<typename T>
    52   template<typename T>
    56     class Point {
    53     class Point {
    57 
    54 
    58     public:
    55     public:
    59 
    56 
    68       Point() {}
    65       Point() {}
    69 
    66 
    70       ///Construct an instance from coordinates
    67       ///Construct an instance from coordinates
    71       Point(T a, T b) : x(a), y(b) { }
    68       Point(T a, T b) : x(a), y(b) { }
    72 
    69 
    73       ///The dimension of the vector.
    70       ///Returns the dimension of the vector (i.e. returns 2).
    74 
    71 
    75       ///The dimension of the vector.
    72       ///The dimension of the vector.
    76       ///This function always returns 2. 
    73       ///This function always returns 2. 
    77       int size() const { return 2; }
    74       int size() const { return 2; }
    78 
    75 
    94       ///Give back the square of the norm of the vector
    91       ///Give back the square of the norm of the vector
    95       T normSquare() const {
    92       T normSquare() const {
    96         return x*x+y*y;
    93         return x*x+y*y;
    97       }
    94       }
    98   
    95   
    99       ///Increment the left hand side by u
    96       ///Increment the left hand side by \c u
   100       Point<T>& operator +=(const Point<T>& u) {
    97       Point<T>& operator +=(const Point<T>& u) {
   101         x += u.x;
    98         x += u.x;
   102         y += u.y;
    99         y += u.y;
   103         return *this;
   100         return *this;
   104       }
   101       }
   105   
   102   
   106       ///Decrement the left hand side by u
   103       ///Decrement the left hand side by \c u
   107       Point<T>& operator -=(const Point<T>& u) {
   104       Point<T>& operator -=(const Point<T>& u) {
   108         x -= u.x;
   105         x -= u.x;
   109         y -= u.y;
   106         y -= u.y;
   110         return *this;
   107         return *this;
   111       }
   108       }
   313       ///Return \c true if the bounding box is empty.
   310       ///Return \c true if the bounding box is empty.
   314       
   311       
   315       ///Return \c true if the bounding box is empty (i.e. return \c false
   312       ///Return \c true if the bounding box is empty (i.e. return \c false
   316       ///if at least one point was added to the box or the coordinates of
   313       ///if at least one point was added to the box or the coordinates of
   317       ///the box were set).
   314       ///the box were set).
       
   315       ///
   318       ///The coordinates of an empty bounding box are not defined. 
   316       ///The coordinates of an empty bounding box are not defined. 
   319       bool empty() const {
   317       bool empty() const {
   320         return _empty;
   318         return _empty;
   321       }
   319       }
   322       
   320       
   323       ///Make the BoundingBox empty
   321       ///Make the BoundingBox empty
   324       void clear() {
   322       void clear() {
   325         _empty=1;
   323         _empty=1;
   326       }
   324       }
   327 
   325 
   328       ///Give back the bottom left corner
   326       ///Give back the bottom left corner of the box
   329 
   327 
   330       ///Give back the bottom left corner.
   328       ///Give back the bottom left corner of the box.
   331       ///If the bounding box is empty, then the return value is not defined.
   329       ///If the bounding box is empty, then the return value is not defined.
   332       Point<T> bottomLeft() const {
   330       Point<T> bottomLeft() const {
   333         return bottom_left;
   331         return bottom_left;
   334       }
   332       }
   335 
   333 
   336       ///Set the bottom left corner
   334       ///Set the bottom left corner of the box
   337 
   335 
   338       ///Set the bottom left corner.
   336       ///Set the bottom left corner of the box.
   339       ///It should only be used for non-empty box.
   337       ///It should only be used for non-empty box.
   340       void bottomLeft(Point<T> p) {
   338       void bottomLeft(Point<T> p) {
   341 	bottom_left = p;
   339 	bottom_left = p;
   342       }
   340       }
   343 
   341 
   344       ///Give back the top right corner
   342       ///Give back the top right corner of the box
   345 
   343 
   346       ///Give back the top right corner.
   344       ///Give back the top right corner of the box.
   347       ///If the bounding box is empty, then the return value is not defined.
   345       ///If the bounding box is empty, then the return value is not defined.
   348       Point<T> topRight() const {
   346       Point<T> topRight() const {
   349         return top_right;
   347         return top_right;
   350       }
   348       }
   351 
   349 
   352       ///Set the top right corner
   350       ///Set the top right corner of the box
   353 
   351 
   354       ///Set the top right corner.
   352       ///Set the top right corner of the box.
   355       ///It should only be used for non-empty box.
   353       ///It should only be used for non-empty box.
   356       void topRight(Point<T> p) {
   354       void topRight(Point<T> p) {
   357 	top_right = p;
   355 	top_right = p;
   358       }
   356       }
   359 
   357 
   360       ///Give back the bottom right corner
   358       ///Give back the bottom right corner of the box
   361 
   359 
   362       ///Give back the bottom right corner.
   360       ///Give back the bottom right corner of the box.
   363       ///If the bounding box is empty, then the return value is not defined.
   361       ///If the bounding box is empty, then the return value is not defined.
   364       Point<T> bottomRight() const {
   362       Point<T> bottomRight() const {
   365         return Point<T>(top_right.x,bottom_left.y);
   363         return Point<T>(top_right.x,bottom_left.y);
   366       }
   364       }
   367 
   365 
   368       ///Set the bottom right corner
   366       ///Set the bottom right corner of the box
   369 
   367 
   370       ///Set the bottom right corner.
   368       ///Set the bottom right corner of the box.
   371       ///It should only be used for non-empty box.
   369       ///It should only be used for non-empty box.
   372       void bottomRight(Point<T> p) {
   370       void bottomRight(Point<T> p) {
   373 	top_right.x = p.x;
   371 	top_right.x = p.x;
   374 	bottom_left.y = p.y;
   372 	bottom_left.y = p.y;
   375       }
   373       }
   376  
   374  
   377       ///Give back the top left corner
   375       ///Give back the top left corner of the box
   378 
   376 
   379       ///Give back the top left corner.
   377       ///Give back the top left corner of the box.
   380       ///If the bounding box is empty, then the return value is not defined.
   378       ///If the bounding box is empty, then the return value is not defined.
   381       Point<T> topLeft() const {
   379       Point<T> topLeft() const {
   382         return Point<T>(bottom_left.x,top_right.y);
   380         return Point<T>(bottom_left.x,top_right.y);
   383       }
   381       }
   384 
   382 
   385       ///Set the top left corner
   383       ///Set the top left corner of the box
   386 
   384 
   387       ///Set the top left corner.
   385       ///Set the top left corner of the box.
   388       ///It should only be used for non-empty box.
   386       ///It should only be used for non-empty box.
   389       void topLeft(Point<T> p) {
   387       void topLeft(Point<T> p) {
   390 	top_right.y = p.y;
   388 	top_right.y = p.y;
   391 	bottom_left.x = p.x;
   389 	bottom_left.x = p.x;
   392       }
   390       }
   531       }
   529       }
   532 
   530 
   533     };//class Boundingbox
   531     };//class Boundingbox
   534 
   532 
   535 
   533 
   536   ///Map of x-coordinates of a Point map
   534   ///Map of x-coordinates of a \ref Point "Point"-map
   537 
   535 
   538   ///\ingroup maps
   536   ///\ingroup maps
   539   ///Map of x-coordinates of a \ref dim2::Point "Point"-map.
   537   ///Map of x-coordinates of a \ref Point "Point"-map.
   540   ///
   538   ///
   541   template<class M>
   539   template<class M>
   542   class XMap 
   540   class XMap 
   543   {
   541   {
   544     M& _map;
   542     M& _map;
   568   inline XMap<M> xMap(const M &m) 
   566   inline XMap<M> xMap(const M &m) 
   569   {
   567   {
   570     return XMap<M>(m);
   568     return XMap<M>(m);
   571   }
   569   }
   572 
   570 
   573   ///Constant (read only) version of XMap
   571   ///Constant (read only) version of \ref XMap
   574 
   572 
   575   ///\ingroup maps
   573   ///\ingroup maps
   576   ///Constant (read only) version of \ref XMap
   574   ///Constant (read only) version of \ref XMap
   577   ///
   575   ///
   578   template<class M>
   576   template<class M>
   598   inline ConstXMap<M> xMap(const M &m) 
   596   inline ConstXMap<M> xMap(const M &m) 
   599   {
   597   {
   600     return ConstXMap<M>(m);
   598     return ConstXMap<M>(m);
   601   }
   599   }
   602 
   600 
   603   ///Map of y-coordinates of a Point map
   601   ///Map of y-coordinates of a \ref Point "Point"-map
   604     
   602     
   605   ///\ingroup maps
   603   ///\ingroup maps
   606   ///Map of y-coordinates of a \ref Point "Point"-map.
   604   ///Map of y-coordinates of a \ref Point "Point"-map.
   607   ///
   605   ///
   608   template<class M>
   606   template<class M>
   635   inline YMap<M> yMap(const M &m) 
   633   inline YMap<M> yMap(const M &m) 
   636   {
   634   {
   637     return YMap<M>(m);
   635     return YMap<M>(m);
   638   }
   636   }
   639 
   637 
   640   ///Constant (read only) version of YMap
   638   ///Constant (read only) version of \ref YMap
   641 
   639 
   642   ///\ingroup maps
   640   ///\ingroup maps
   643   ///Constant (read only) version of \ref YMap
   641   ///Constant (read only) version of \ref YMap
   644   ///
   642   ///
   645   template<class M>
   643   template<class M>
   666   {
   664   {
   667     return ConstYMap<M>(m);
   665     return ConstYMap<M>(m);
   668   }
   666   }
   669 
   667 
   670 
   668 
   671     ///\brief Map of the normSquare()
   669   ///\brief Map of the \ref Point::normSquare() "normSquare()"
   672     ///of a Point map
   670   ///of a \ref Point "Point"-map
   673     ///
   671   ///
   674     ///Map of the \ref Point::normSquare() "normSquare()"
   672   ///Map of the \ref Point::normSquare() "normSquare()"
   675     ///of a \ref Point "Point"-map.
   673   ///of a \ref Point "Point"-map.
   676     ///\ingroup maps
   674   ///\ingroup maps
   677     ///
       
   678   template<class M>
   675   template<class M>
   679   class NormSquareMap 
   676   class NormSquareMap 
   680   {
   677   {
   681     const M& _map;
   678     const M& _map;
   682   public:
   679   public: