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::Box "dim2::Box" can be used to determine
32 /// the rectangular bounding box of a set of
33 /// \ref lemon::dim2::Point "dim2::Point"'s.
37 ///Tools for handling two dimensional coordinates
39 ///This namespace is a storage of several
40 ///tools for handling two dimensional coordinates
46 /// Two dimensional vector (plain vector)
48 /// A simple two dimensional vector (plain vector) implementation
49 /// with the usual vector operations.
62 ///Default constructor
65 ///Construct an instance from coordinates
66 Point(T a, T b) : x(a), y(b) { }
68 ///Returns the dimension of the vector (i.e. returns 2).
70 ///The dimension of the vector.
71 ///This function always returns 2.
72 int size() const { return 2; }
74 ///Subscripting operator
76 ///\c p[0] is \c p.x and \c p[1] is \c p.y
78 T& operator[](int idx) { return idx == 0 ? x : y; }
80 ///Const subscripting operator
82 ///\c p[0] is \c p.x and \c p[1] is \c p.y
84 const T& operator[](int idx) const { return idx == 0 ? x : y; }
86 ///Conversion constructor
87 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
89 ///Give back the square of the norm of the vector
90 T normSquare() const {
94 ///Increment the left hand side by \c u
95 Point<T>& operator +=(const Point<T>& u) {
101 ///Decrement the left hand side by \c u
102 Point<T>& operator -=(const Point<T>& u) {
108 ///Multiply the left hand side with a scalar
109 Point<T>& operator *=(const T &u) {
115 ///Divide the left hand side by a scalar
116 Point<T>& operator /=(const T &u) {
122 ///Return the scalar product of two vectors
123 T operator *(const Point<T>& u) const {
127 ///Return the sum of two vectors
128 Point<T> operator+(const Point<T> &u) const {
133 ///Return the negative of the vector
134 Point<T> operator-() const {
140 ///Return the difference of two vectors
141 Point<T> operator-(const Point<T> &u) const {
146 ///Return a vector multiplied by a scalar
147 Point<T> operator*(const T &u) const {
152 ///Return a vector divided by a scalar
153 Point<T> operator/(const T &u) const {
159 bool operator==(const Point<T> &u) const {
160 return (x==u.x) && (y==u.y);
164 bool operator!=(Point u) const {
165 return (x!=u.x) || (y!=u.y);
174 template <typename T>
175 inline Point<T> makePoint(const T& x, const T& y) {
176 return Point<T>(x, y);
179 ///Return a vector multiplied by a scalar
181 ///Return a vector multiplied by a scalar.
183 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
187 ///Read a plain vector from a stream
189 ///Read a plain vector from a stream.
193 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
196 if (c != '(') is.putback(c);
200 if (!(is >> z.x)) return is;
202 if (c != ',') is.putback(c);
206 if (!(is >> z.y)) return is;
208 if (c != ')') is.putback(c);
215 ///Write a plain vector to a stream
217 ///Write a plain vector to a stream.
221 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
223 os << "(" << z.x << "," << z.y << ")";
227 ///Rotate by 90 degrees
229 ///Returns the parameter rotated by 90 degrees in positive direction.
233 inline Point<T> rot90(const Point<T> &z)
235 return Point<T>(-z.y,z.x);
238 ///Rotate by 180 degrees
240 ///Returns the parameter rotated by 180 degrees.
244 inline Point<T> rot180(const Point<T> &z)
246 return Point<T>(-z.x,-z.y);
249 ///Rotate by 270 degrees
251 ///Returns the parameter rotated by 90 degrees in negative direction.
255 inline Point<T> rot270(const Point<T> &z)
257 return Point<T>(z.y,-z.x);
262 /// Bounding box of plain vectors (points).
264 /// A class to calculate or store the bounding box of plain vectors
265 /// (\ref Point "points").
268 Point<T> _bottom_left, _top_right;
272 ///Default constructor: creates an empty box
273 Box() { _empty = true; }
275 ///Construct a box from one point
277 _bottom_left = _top_right = a;
281 ///Construct a box from two points
283 ///Construct a box from two points.
284 ///\param a The bottom left corner.
285 ///\param b The top right corner.
286 ///\warning The coordinates of the bottom left corner must be no more
287 ///than those of the top right one.
288 Box(Point<T> a,Point<T> b)
295 ///Construct a box from four numbers
297 ///Construct a box from four numbers.
298 ///\param l The left side of the box.
299 ///\param b The bottom of the box.
300 ///\param r The right side of the box.
301 ///\param t The top of the box.
302 ///\warning The left side must be no more than the right side and
303 ///bottom must be no more than the top.
306 _bottom_left=Point<T>(l,b);
307 _top_right=Point<T>(r,t);
311 ///Return \c true if the box is empty.
313 ///Return \c true if the box is empty (i.e. return \c false
314 ///if at least one point was added to the box or the coordinates of
315 ///the box were set).
317 ///The coordinates of an empty box are not defined.
322 ///Make the box empty
327 ///Give back the bottom left corner of the box
329 ///Give back the bottom left corner of the box.
330 ///If the box is empty, then the return value is not defined.
331 Point<T> bottomLeft() const {
335 ///Set the bottom left corner of the box
337 ///Set the bottom left corner of the box.
338 ///\pre The box must not be empty.
339 void bottomLeft(Point<T> p) {
343 ///Give back the top right corner of the box
345 ///Give back the top right corner of the box.
346 ///If the box is empty, then the return value is not defined.
347 Point<T> topRight() const {
351 ///Set the top right corner of the box
353 ///Set the top right corner of the box.
354 ///\pre The box must not be empty.
355 void topRight(Point<T> p) {
359 ///Give back the bottom right corner of the box
361 ///Give back the bottom right corner of the box.
362 ///If the box is empty, then the return value is not defined.
363 Point<T> bottomRight() const {
364 return Point<T>(_top_right.x,_bottom_left.y);
367 ///Set the bottom right corner of the box
369 ///Set the bottom right corner of the box.
370 ///\pre The box must not be empty.
371 void bottomRight(Point<T> p) {
373 _bottom_left.y = p.y;
376 ///Give back the top left corner of the box
378 ///Give back the top left corner of the box.
379 ///If the box is empty, then the return value is not defined.
380 Point<T> topLeft() const {
381 return Point<T>(_bottom_left.x,_top_right.y);
384 ///Set the top left corner of the box
386 ///Set the top left corner of the box.
387 ///\pre The box must not be empty.
388 void topLeft(Point<T> p) {
390 _bottom_left.x = p.x;
393 ///Give back the bottom of the box
395 ///Give back the bottom of the box.
396 ///If the box is empty, then the return value is not defined.
398 return _bottom_left.y;
401 ///Set the bottom of the box
403 ///Set the bottom of the box.
404 ///\pre The box must not be empty.
409 ///Give back the top of the box
411 ///Give back the top of the box.
412 ///If the box is empty, then the return value is not defined.
417 ///Set the top of the box
419 ///Set the top of the box.
420 ///\pre The box must not be empty.
425 ///Give back the left side of the box
427 ///Give back the left side of the box.
428 ///If the box is empty, then the return value is not defined.
430 return _bottom_left.x;
433 ///Set the left side of the box
435 ///Set the left side of the box.
436 ///\pre The box must not be empty.
441 /// Give back the right side of the box
443 /// Give back the right side of the box.
444 ///If the box is empty, then the return value is not defined.
449 ///Set the right side of the box
451 ///Set the right side of the box.
452 ///\pre The box must not be empty.
457 ///Give back the height of the box
459 ///Give back the height of the box.
460 ///If the box is empty, then the return value is not defined.
462 return _top_right.y-_bottom_left.y;
465 ///Give back the width of the box
467 ///Give back the width of the box.
468 ///If the box is empty, then the return value is not defined.
470 return _top_right.x-_bottom_left.x;
473 ///Checks whether a point is inside the box
474 bool inside(const Point<T>& u) const {
478 return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
479 (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
483 ///Increments the box with a point
485 ///Increments the box with a point.
487 Box& add(const Point<T>& u){
489 _bottom_left = _top_right = u;
493 if (_bottom_left.x > u.x) _bottom_left.x = u.x;
494 if (_bottom_left.y > u.y) _bottom_left.y = u.y;
495 if (_top_right.x < u.x) _top_right.x = u.x;
496 if (_top_right.y < u.y) _top_right.y = u.y;
501 ///Increments the box to contain another box
503 ///Increments the box to contain another box.
505 Box& add(const Box &u){
513 ///Intersection of two boxes
515 ///Intersection of two boxes.
517 Box operator&(const Box& u) const {
519 if (_empty || u._empty) {
522 b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
523 b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
524 b._top_right.x = std::min(_top_right.x, u._top_right.x);
525 b._top_right.y = std::min(_top_right.y, u._top_right.y);
526 b._empty = b._bottom_left.x > b._top_right.x ||
527 b._bottom_left.y > b._top_right.y;
535 ///Read a box from a stream
537 ///Read a box from a stream.
540 inline std::istream& operator>>(std::istream &is, Box<T>& b) {
544 if (c != '(') is.putback(c);
548 if (!(is >> p)) return is;
551 if (c != ',') is.putback(c);
555 if (!(is >> p)) return is;
558 if (c != ')') is.putback(c);
565 ///Write a box to a stream
567 ///Write a box to a stream.
570 inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
572 os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
576 ///Map of x-coordinates of a <tt>Point</tt>-map
578 ///Map of x-coordinates of a \ref Point "Point"-map.
586 typedef typename M::Value::Value Value;
587 typedef typename M::Key Key;
589 XMap(M& map) : _map(map) {}
590 Value operator[](Key k) const {return _map[k].x;}
591 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
594 ///Returns an XMap class
596 ///This function just returns an XMap class.
599 inline XMap<M> xMap(M &m)
605 inline XMap<M> xMap(const M &m)
610 ///Constant (read only) version of XMap
612 ///Constant (read only) version of XMap.
620 typedef typename M::Value::Value Value;
621 typedef typename M::Key Key;
623 ConstXMap(const M &map) : _map(map) {}
624 Value operator[](Key k) const {return _map[k].x;}
627 ///Returns a ConstXMap class
629 ///This function just returns a ConstXMap class.
630 ///\relates ConstXMap
632 inline ConstXMap<M> xMap(const M &m)
634 return ConstXMap<M>(m);
637 ///Map of y-coordinates of a <tt>Point</tt>-map
639 ///Map of y-coordinates of a \ref Point "Point"-map.
647 typedef typename M::Value::Value Value;
648 typedef typename M::Key Key;
650 YMap(M& map) : _map(map) {}
651 Value operator[](Key k) const {return _map[k].y;}
652 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
655 ///Returns a YMap class
657 ///This function just returns a YMap class.
660 inline YMap<M> yMap(M &m)
666 inline YMap<M> yMap(const M &m)
671 ///Constant (read only) version of YMap
673 ///Constant (read only) version of YMap.
681 typedef typename M::Value::Value Value;
682 typedef typename M::Key Key;
684 ConstYMap(const M &map) : _map(map) {}
685 Value operator[](Key k) const {return _map[k].y;}
688 ///Returns a ConstYMap class
690 ///This function just returns a ConstYMap class.
691 ///\relates ConstYMap
693 inline ConstYMap<M> yMap(const M &m)
695 return ConstYMap<M>(m);
699 ///\brief Map of the normSquare() of a <tt>Point</tt>-map
701 ///Map of the \ref Point::normSquare() "normSquare()"
702 ///of a \ref Point "Point"-map.
709 typedef typename M::Value::Value Value;
710 typedef typename M::Key Key;
712 NormSquareMap(const M &map) : _map(map) {}
713 Value operator[](Key k) const {return _map[k].normSquare();}
716 ///Returns a NormSquareMap class
718 ///This function just returns a NormSquareMap class.
719 ///\relates NormSquareMap
721 inline NormSquareMap<M> normSquareMap(const M &m)
723 return NormSquareMap<M>(m);
732 #endif //LEMON_DIM2_H