1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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
27 ///\brief A simple two dimensional vector and a bounding box implementation
31 ///Tools for handling two dimensional coordinates
33 ///This namespace is a storage of several
34 ///tools for handling two dimensional coordinates
37 /// \addtogroup geomdat
40 /// Two dimensional vector (plain vector)
42 /// A simple two dimensional vector (plain vector) implementation
43 /// with the usual vector operations.
56 ///Default constructor
59 ///Construct an instance from coordinates
60 Point(T a, T b) : x(a), y(b) { }
62 ///Returns the dimension of the vector (i.e. returns 2).
64 ///The dimension of the vector.
65 ///This function always returns 2.
66 int size() const { return 2; }
68 ///Subscripting operator
70 ///\c p[0] is \c p.x and \c p[1] is \c p.y
72 T& operator[](int idx) { return idx == 0 ? x : y; }
74 ///Const subscripting operator
76 ///\c p[0] is \c p.x and \c p[1] is \c p.y
78 const T& operator[](int idx) const { return idx == 0 ? x : y; }
80 ///Conversion constructor
81 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
83 ///Give back the square of the norm of the vector
84 T normSquare() const {
88 ///Increment the left hand side by \c u
89 Point<T>& operator +=(const Point<T>& u) {
95 ///Decrement the left hand side by \c u
96 Point<T>& operator -=(const Point<T>& u) {
102 ///Multiply the left hand side with a scalar
103 Point<T>& operator *=(const T &u) {
109 ///Divide the left hand side by a scalar
110 Point<T>& operator /=(const T &u) {
116 ///Return the scalar product of two vectors
117 T operator *(const Point<T>& u) const {
121 ///Return the sum of two vectors
122 Point<T> operator+(const Point<T> &u) const {
127 ///Return the negative of the vector
128 Point<T> operator-() const {
134 ///Return the difference of two vectors
135 Point<T> operator-(const Point<T> &u) const {
140 ///Return a vector multiplied by a scalar
141 Point<T> operator*(const T &u) const {
146 ///Return a vector divided by a scalar
147 Point<T> operator/(const T &u) const {
153 bool operator==(const Point<T> &u) const {
154 return (x==u.x) && (y==u.y);
158 bool operator!=(Point u) const {
159 return (x!=u.x) || (y!=u.y);
168 template <typename T>
169 inline Point<T> makePoint(const T& x, const T& y) {
170 return Point<T>(x, y);
173 ///Return a vector multiplied by a scalar
175 ///Return a vector multiplied by a scalar.
177 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
181 ///Read a plain vector from a stream
183 ///Read a plain vector from a stream.
187 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
190 if (c != '(') is.putback(c);
194 if (!(is >> z.x)) return is;
196 if (c != ',') is.putback(c);
200 if (!(is >> z.y)) return is;
202 if (c != ')') is.putback(c);
209 ///Write a plain vector to a stream
211 ///Write a plain vector to a stream.
215 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
217 os << "(" << z.x << "," << z.y << ")";
221 ///Rotate by 90 degrees
223 ///Returns the parameter rotated by 90 degrees in positive direction.
227 inline Point<T> rot90(const Point<T> &z)
229 return Point<T>(-z.y,z.x);
232 ///Rotate by 180 degrees
234 ///Returns the parameter rotated by 180 degrees.
238 inline Point<T> rot180(const Point<T> &z)
240 return Point<T>(-z.x,-z.y);
243 ///Rotate by 270 degrees
245 ///Returns the parameter rotated by 90 degrees in negative direction.
249 inline Point<T> rot270(const Point<T> &z)
251 return Point<T>(z.y,-z.x);
256 /// Bounding box of plain vectors (points).
258 /// A class to calculate or store the bounding box of plain vectors
259 /// (\ref Point "points").
262 Point<T> _bottom_left, _top_right;
266 ///Default constructor: creates an empty box
267 Box() { _empty = true; }
269 ///Construct a box from one point
271 _bottom_left = _top_right = a;
275 ///Construct a box from two points
277 ///Construct a box from two points.
278 ///\param a The bottom left corner.
279 ///\param b The top right corner.
280 ///\warning The coordinates of the bottom left corner must be no more
281 ///than those of the top right one.
282 Box(Point<T> a,Point<T> b)
289 ///Construct a box from four numbers
291 ///Construct a box from four numbers.
292 ///\param l The left side of the box.
293 ///\param b The bottom of the box.
294 ///\param r The right side of the box.
295 ///\param t The top of the box.
296 ///\warning The left side must be no more than the right side and
297 ///bottom must be no more than the top.
300 _bottom_left=Point<T>(l,b);
301 _top_right=Point<T>(r,t);
305 ///Return \c true if the box is empty.
307 ///Return \c true if the box is empty (i.e. return \c false
308 ///if at least one point was added to the box or the coordinates of
309 ///the box were set).
311 ///The coordinates of an empty box are not defined.
316 ///Make the box empty
321 ///Give back the bottom left corner of the box
323 ///Give back the bottom left corner of the box.
324 ///If the box is empty, then the return value is not defined.
325 Point<T> bottomLeft() const {
329 ///Set the bottom left corner of the box
331 ///Set the bottom left corner of the box.
332 ///\pre The box must not be empty.
333 void bottomLeft(Point<T> p) {
337 ///Give back the top right corner of the box
339 ///Give back the top right corner of the box.
340 ///If the box is empty, then the return value is not defined.
341 Point<T> topRight() const {
345 ///Set the top right corner of the box
347 ///Set the top right corner of the box.
348 ///\pre The box must not be empty.
349 void topRight(Point<T> p) {
353 ///Give back the bottom right corner of the box
355 ///Give back the bottom right corner of the box.
356 ///If the box is empty, then the return value is not defined.
357 Point<T> bottomRight() const {
358 return Point<T>(_top_right.x,_bottom_left.y);
361 ///Set the bottom right corner of the box
363 ///Set the bottom right corner of the box.
364 ///\pre The box must not be empty.
365 void bottomRight(Point<T> p) {
367 _bottom_left.y = p.y;
370 ///Give back the top left corner of the box
372 ///Give back the top left corner of the box.
373 ///If the box is empty, then the return value is not defined.
374 Point<T> topLeft() const {
375 return Point<T>(_bottom_left.x,_top_right.y);
378 ///Set the top left corner of the box
380 ///Set the top left corner of the box.
381 ///\pre The box must not be empty.
382 void topLeft(Point<T> p) {
384 _bottom_left.x = p.x;
387 ///Give back the bottom of the box
389 ///Give back the bottom of the box.
390 ///If the box is empty, then the return value is not defined.
392 return _bottom_left.y;
395 ///Set the bottom of the box
397 ///Set the bottom of the box.
398 ///\pre The box must not be empty.
403 ///Give back the top of the box
405 ///Give back the top of the box.
406 ///If the box is empty, then the return value is not defined.
411 ///Set the top of the box
413 ///Set the top of the box.
414 ///\pre The box must not be empty.
419 ///Give back the left side of the box
421 ///Give back the left side of the box.
422 ///If the box is empty, then the return value is not defined.
424 return _bottom_left.x;
427 ///Set the left side of the box
429 ///Set the left side of the box.
430 ///\pre The box must not be empty.
435 /// Give back the right side of the box
437 /// Give back the right side of the box.
438 ///If the box is empty, then the return value is not defined.
443 ///Set the right side of the box
445 ///Set the right side of the box.
446 ///\pre The box must not be empty.
451 ///Give back the height of the box
453 ///Give back the height of the box.
454 ///If the box is empty, then the return value is not defined.
456 return _top_right.y-_bottom_left.y;
459 ///Give back the width of the box
461 ///Give back the width of the box.
462 ///If the box is empty, then the return value is not defined.
464 return _top_right.x-_bottom_left.x;
467 ///Checks whether a point is inside the box
468 bool inside(const Point<T>& u) const {
472 return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
473 (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
477 ///Increments the box with a point
479 ///Increments the box with a point.
481 Box& add(const Point<T>& u){
483 _bottom_left = _top_right = u;
487 if (_bottom_left.x > u.x) _bottom_left.x = u.x;
488 if (_bottom_left.y > u.y) _bottom_left.y = u.y;
489 if (_top_right.x < u.x) _top_right.x = u.x;
490 if (_top_right.y < u.y) _top_right.y = u.y;
495 ///Increments the box to contain another box
497 ///Increments the box to contain another box.
499 Box& add(const Box &u){
507 ///Intersection of two boxes
509 ///Intersection of two boxes.
511 Box operator&(const Box& u) const {
513 if (_empty || u._empty) {
516 b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
517 b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
518 b._top_right.x = std::min(_top_right.x, u._top_right.x);
519 b._top_right.y = std::min(_top_right.y, u._top_right.y);
520 b._empty = b._bottom_left.x > b._top_right.x ||
521 b._bottom_left.y > b._top_right.y;
529 ///Read a box from a stream
531 ///Read a box from a stream.
534 inline std::istream& operator>>(std::istream &is, Box<T>& b) {
538 if (c != '(') is.putback(c);
542 if (!(is >> p)) return is;
545 if (c != ',') is.putback(c);
549 if (!(is >> p)) return is;
552 if (c != ')') is.putback(c);
559 ///Write a box to a stream
561 ///Write a box to a stream.
564 inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
566 os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
570 ///Map of x-coordinates of a <tt>Point</tt>-map
572 ///Map of x-coordinates of a \ref Point "Point"-map.
580 typedef typename M::Value::Value Value;
581 typedef typename M::Key Key;
583 XMap(M& map) : _map(map) {}
584 Value operator[](Key k) const {return _map[k].x;}
585 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
588 ///Returns an XMap class
590 ///This function just returns an XMap class.
593 inline XMap<M> xMap(M &m)
599 inline XMap<M> xMap(const M &m)
604 ///Constant (read only) version of XMap
606 ///Constant (read only) version of XMap.
614 typedef typename M::Value::Value Value;
615 typedef typename M::Key Key;
617 ConstXMap(const M &map) : _map(map) {}
618 Value operator[](Key k) const {return _map[k].x;}
621 ///Returns a ConstXMap class
623 ///This function just returns a ConstXMap class.
624 ///\relates ConstXMap
626 inline ConstXMap<M> xMap(const M &m)
628 return ConstXMap<M>(m);
631 ///Map of y-coordinates of a <tt>Point</tt>-map
633 ///Map of y-coordinates of a \ref Point "Point"-map.
641 typedef typename M::Value::Value Value;
642 typedef typename M::Key Key;
644 YMap(M& map) : _map(map) {}
645 Value operator[](Key k) const {return _map[k].y;}
646 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
649 ///Returns a YMap class
651 ///This function just returns a YMap class.
654 inline YMap<M> yMap(M &m)
660 inline YMap<M> yMap(const M &m)
665 ///Constant (read only) version of YMap
667 ///Constant (read only) version of YMap.
675 typedef typename M::Value::Value Value;
676 typedef typename M::Key Key;
678 ConstYMap(const M &map) : _map(map) {}
679 Value operator[](Key k) const {return _map[k].y;}
682 ///Returns a ConstYMap class
684 ///This function just returns a ConstYMap class.
685 ///\relates ConstYMap
687 inline ConstYMap<M> yMap(const M &m)
689 return ConstYMap<M>(m);
693 ///\brief Map of the normSquare() of a <tt>Point</tt>-map
695 ///Map of the \ref Point::normSquare() "normSquare()"
696 ///of a \ref Point "Point"-map.
703 typedef typename M::Value::Value Value;
704 typedef typename M::Key Key;
706 NormSquareMap(const M &map) : _map(map) {}
707 Value operator[](Key k) const {return _map[k].normSquare();}
710 ///Returns a NormSquareMap class
712 ///This function just returns a NormSquareMap class.
713 ///\relates NormSquareMap
715 inline NormSquareMap<M> normSquareMap(const M &m)
717 return NormSquareMap<M>(m);
726 #endif //LEMON_DIM2_H