Update README file.
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
26 ///\brief A simple two dimensional vector and a bounding box implementation
28 /// The class \ref lemon::dim2::Point "dim2::Point" implements
29 /// a two dimensional vector with the usual operations.
31 /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
32 /// can be used to determine
33 /// the rectangular bounding box of a set of
34 /// \ref lemon::dim2::Point "dim2::Point"'s.
38 ///Tools for handling two dimensional coordinates
40 ///This namespace is a storage of several
41 ///tools for handling two dimensional coordinates
47 /// A simple two dimensional vector (plain vector) implementation
49 /// A simple two dimensional vector (plain vector) implementation
50 /// with the usual vector operations.
63 ///Default constructor
66 ///Construct an instance from coordinates
67 Point(T a, T b) : x(a), y(b) { }
69 ///Returns the dimension of the vector (i.e. returns 2).
71 ///The dimension of the vector.
72 ///This function always returns 2.
73 int size() const { return 2; }
75 ///Subscripting operator
77 ///\c p[0] is \c p.x and \c p[1] is \c p.y
79 T& operator[](int idx) { return idx == 0 ? x : y; }
81 ///Const subscripting operator
83 ///\c p[0] is \c p.x and \c p[1] is \c p.y
85 const T& operator[](int idx) const { return idx == 0 ? x : y; }
87 ///Conversion constructor
88 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
90 ///Give back the square of the norm of the vector
91 T normSquare() const {
95 ///Increment the left hand side by \c u
96 Point<T>& operator +=(const Point<T>& u) {
102 ///Decrement the left hand side by \c u
103 Point<T>& operator -=(const Point<T>& u) {
109 ///Multiply the left hand side with a scalar
110 Point<T>& operator *=(const T &u) {
116 ///Divide the left hand side by a scalar
117 Point<T>& operator /=(const T &u) {
123 ///Return the scalar product of two vectors
124 T operator *(const Point<T>& u) const {
128 ///Return the sum of two vectors
129 Point<T> operator+(const Point<T> &u) const {
134 ///Return the negative of the vector
135 Point<T> operator-() const {
141 ///Return the difference of two vectors
142 Point<T> operator-(const Point<T> &u) const {
147 ///Return a vector multiplied by a scalar
148 Point<T> operator*(const T &u) const {
153 ///Return a vector divided by a scalar
154 Point<T> operator/(const T &u) const {
160 bool operator==(const Point<T> &u) const {
161 return (x==u.x) && (y==u.y);
165 bool operator!=(Point u) const {
166 return (x!=u.x) || (y!=u.y);
175 template <typename T>
176 inline Point<T> makePoint(const T& x, const T& y) {
177 return Point<T>(x, y);
180 ///Return a vector multiplied by a scalar
182 ///Return a vector multiplied by a scalar.
184 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
188 ///Read a plain vector from a stream
190 ///Read a plain vector from a stream.
194 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
197 if (c != '(') is.putback(c);
201 if (!(is >> z.x)) return is;
203 if (c != ',') is.putback(c);
207 if (!(is >> z.y)) return is;
209 if (c != ')') is.putback(c);
216 ///Write a plain vector to a stream
218 ///Write a plain vector to a stream.
222 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
224 os << "(" << z.x << ", " << z.y << ")";
228 ///Rotate by 90 degrees
230 ///Returns the parameter rotated by 90 degrees in positive direction.
234 inline Point<T> rot90(const Point<T> &z)
236 return Point<T>(-z.y,z.x);
239 ///Rotate by 180 degrees
241 ///Returns the parameter rotated by 180 degrees.
245 inline Point<T> rot180(const Point<T> &z)
247 return Point<T>(-z.x,-z.y);
250 ///Rotate by 270 degrees
252 ///Returns the parameter rotated by 90 degrees in negative direction.
256 inline Point<T> rot270(const Point<T> &z)
258 return Point<T>(z.y,-z.x);
263 /// A class to calculate or store the bounding box of plain vectors.
265 /// A class to calculate or store the bounding box of plain vectors.
269 Point<T> _bottom_left, _top_right;
273 ///Default constructor: creates an empty bounding box
274 BoundingBox() { _empty = true; }
276 ///Construct an instance from one point
277 BoundingBox(Point<T> a) {
278 _bottom_left = _top_right = a;
282 ///Construct an instance from two points
284 ///Construct an instance from two points.
285 ///\param a The bottom left corner.
286 ///\param b The top right corner.
287 ///\warning The coordinates of the bottom left corner must be no more
288 ///than those of the top right one.
289 BoundingBox(Point<T> a,Point<T> b)
296 ///Construct an instance from four numbers
298 ///Construct an instance from four numbers.
299 ///\param l The left side of the box.
300 ///\param b The bottom of the box.
301 ///\param r The right side of the box.
302 ///\param t The top of the box.
303 ///\warning The left side must be no more than the right side and
304 ///bottom must be no more than the top.
305 BoundingBox(T l,T b,T r,T t)
307 _bottom_left=Point<T>(l,b);
308 _top_right=Point<T>(r,t);
312 ///Return \c true if the bounding box is empty.
314 ///Return \c true if the bounding box is empty (i.e. return \c false
315 ///if at least one point was added to the box or the coordinates of
316 ///the box were set).
318 ///The coordinates of an empty bounding box are not defined.
323 ///Make the BoundingBox empty
328 ///Give back the bottom left corner of the box
330 ///Give back the bottom left corner of the box.
331 ///If the bounding box is empty, then the return value is not defined.
332 Point<T> bottomLeft() const {
336 ///Set the bottom left corner of the box
338 ///Set the bottom left corner of the box.
339 ///\pre The box must not be empty.
340 void bottomLeft(Point<T> p) {
344 ///Give back the top right corner of the box
346 ///Give back the top right corner of the box.
347 ///If the bounding box is empty, then the return value is not defined.
348 Point<T> topRight() const {
352 ///Set the top right corner of the box
354 ///Set the top right corner of the box.
355 ///\pre The box must not be empty.
356 void topRight(Point<T> p) {
360 ///Give back the bottom right corner of the box
362 ///Give back the bottom right corner of the box.
363 ///If the bounding box is empty, then the return value is not defined.
364 Point<T> bottomRight() const {
365 return Point<T>(_top_right.x,_bottom_left.y);
368 ///Set the bottom right corner of the box
370 ///Set the bottom right corner of the box.
371 ///\pre The box must not be empty.
372 void bottomRight(Point<T> p) {
374 _bottom_left.y = p.y;
377 ///Give back the top left corner of the box
379 ///Give back the top left corner of the box.
380 ///If the bounding box is empty, then the return value is not defined.
381 Point<T> topLeft() const {
382 return Point<T>(_bottom_left.x,_top_right.y);
385 ///Set the top left corner of the box
387 ///Set the top left corner of the box.
388 ///\pre The box must not be empty.
389 void topLeft(Point<T> p) {
391 _bottom_left.x = p.x;
394 ///Give back the bottom of the box
396 ///Give back the bottom of the box.
397 ///If the bounding box is empty, then the return value is not defined.
399 return _bottom_left.y;
402 ///Set the bottom of the box
404 ///Set the bottom of the box.
405 ///\pre The box must not be empty.
410 ///Give back the top of the box
412 ///Give back the top of the box.
413 ///If the bounding box is empty, then the return value is not defined.
418 ///Set the top of the box
420 ///Set the top of the box.
421 ///\pre The box must not be empty.
426 ///Give back the left side of the box
428 ///Give back the left side of the box.
429 ///If the bounding box is empty, then the return value is not defined.
431 return _bottom_left.x;
434 ///Set the left side of the box
436 ///Set the left side of the box.
437 ///\pre The box must not be empty.
442 /// Give back the right side of the box
444 /// Give back the right side of the box.
445 ///If the bounding box is empty, then the return value is not defined.
450 ///Set the right side of the box
452 ///Set the right side of the box.
453 ///\pre The box must not be empty.
458 ///Give back the height of the box
460 ///Give back the height of the box.
461 ///If the bounding box is empty, then the return value is not defined.
463 return _top_right.y-_bottom_left.y;
466 ///Give back the width of the box
468 ///Give back the width of the box.
469 ///If the bounding box is empty, then the return value is not defined.
471 return _top_right.x-_bottom_left.x;
474 ///Checks whether a point is inside a bounding box
475 bool inside(const Point<T>& u) const {
479 return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
480 (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
484 ///Increments a bounding box with a point
486 ///Increments a bounding box with a point.
488 BoundingBox& add(const Point<T>& u){
490 _bottom_left = _top_right = u;
494 if (_bottom_left.x > u.x) _bottom_left.x = u.x;
495 if (_bottom_left.y > u.y) _bottom_left.y = u.y;
496 if (_top_right.x < u.x) _top_right.x = u.x;
497 if (_top_right.y < u.y) _top_right.y = u.y;
502 ///Increments a bounding box to contain another bounding box
504 ///Increments a bounding box to contain another bounding box.
506 BoundingBox& add(const BoundingBox &u){
514 ///Intersection of two bounding boxes
516 ///Intersection of two bounding boxes.
518 BoundingBox operator&(const BoundingBox& u) const {
520 if (_empty || u._empty) {
523 b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
524 b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
525 b._top_right.x = std::min(_top_right.x, u._top_right.x);
526 b._top_right.y = std::min(_top_right.y, u._top_right.y);
527 b._empty = b._bottom_left.x > b._top_right.x ||
528 b._bottom_left.y > b._top_right.y;
533 };//class Boundingbox
536 ///Map of x-coordinates of a \ref Point "Point"-map
539 ///Map of x-coordinates of a \ref Point "Point"-map.
547 typedef typename M::Value::Value Value;
548 typedef typename M::Key Key;
550 XMap(M& map) : _map(map) {}
551 Value operator[](Key k) const {return _map[k].x;}
552 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
555 ///Returns an \ref XMap class
557 ///This function just returns an \ref XMap class.
562 inline XMap<M> xMap(M &m)
568 inline XMap<M> xMap(const M &m)
573 ///Constant (read only) version of \ref XMap
576 ///Constant (read only) version of \ref XMap
584 typedef typename M::Value::Value Value;
585 typedef typename M::Key Key;
587 ConstXMap(const M &map) : _map(map) {}
588 Value operator[](Key k) const {return _map[k].x;}
591 ///Returns a \ref ConstXMap class
593 ///This function just returns a \ref ConstXMap class.
596 ///\relates ConstXMap
598 inline ConstXMap<M> xMap(const M &m)
600 return ConstXMap<M>(m);
603 ///Map of y-coordinates of a \ref Point "Point"-map
606 ///Map of y-coordinates of a \ref Point "Point"-map.
614 typedef typename M::Value::Value Value;
615 typedef typename M::Key Key;
617 YMap(M& map) : _map(map) {}
618 Value operator[](Key k) const {return _map[k].y;}
619 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
622 ///Returns a \ref YMap class
624 ///This function just returns a \ref YMap class.
629 inline YMap<M> yMap(M &m)
635 inline YMap<M> yMap(const M &m)
640 ///Constant (read only) version of \ref YMap
643 ///Constant (read only) version of \ref YMap
651 typedef typename M::Value::Value Value;
652 typedef typename M::Key Key;
654 ConstYMap(const M &map) : _map(map) {}
655 Value operator[](Key k) const {return _map[k].y;}
658 ///Returns a \ref ConstYMap class
660 ///This function just returns a \ref ConstYMap class.
663 ///\relates ConstYMap
665 inline ConstYMap<M> yMap(const M &m)
667 return ConstYMap<M>(m);
671 ///\brief Map of the \ref Point::normSquare() "normSquare()"
672 ///of a \ref Point "Point"-map
674 ///Map of the \ref Point::normSquare() "normSquare()"
675 ///of a \ref Point "Point"-map.
683 typedef typename M::Value::Value Value;
684 typedef typename M::Key Key;
686 NormSquareMap(const M &map) : _map(map) {}
687 Value operator[](Key k) const {return _map[k].normSquare();}
690 ///Returns a \ref NormSquareMap class
692 ///This function just returns a \ref NormSquareMap class.
695 ///\relates NormSquareMap
697 inline NormSquareMap<M> normSquareMap(const M &m)
699 return NormSquareMap<M>(m);
708 #endif //LEMON_DIM2_H