3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
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
23 #include <lemon/bits/utility.h>
27 ///\brief A simple two dimensional vector and a bounding box implementation
29 /// The class \ref lemon::dim2::Point "dim2::Point" implements
30 ///a two dimensional vector with the usual
33 /// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox"
34 /// can be used to determine
35 /// the rectangular bounding box of a set of
36 /// \ref lemon::dim2::Point "dim2::Point"'s.
38 ///\author Attila Bernath
43 ///Tools for handling two dimensional coordinates
45 ///This namespace is a storage of several
46 ///tools for handling two dimensional coordinates
52 /// A simple two dimensional vector (plainvector) implementation
54 /// A simple two dimensional vector (plainvector) implementation
55 ///with the usual vector
70 ///Default constructor
73 ///Construct an instance from coordinates
74 Point(T a, T b) : x(a), y(b) { }
76 ///The dimension of the vector.
78 ///This class give back always 2.
80 int size() const { return 2; }
82 ///Subscripting operator
84 ///\c p[0] is \c p.x and \c p[1] is \c p.y
86 T& operator[](int idx) { return idx == 0 ? x : y; }
88 ///Const subscripting operator
90 ///\c p[0] is \c p.x and \c p[1] is \c p.y
92 const T& operator[](int idx) const { return idx == 0 ? x : y; }
94 ///Conversion constructor
95 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
97 ///Give back the square of the norm of the vector
98 T normSquare() const {
102 ///Increment the left hand side by u
103 Point<T>& operator +=(const Point<T>& u) {
109 ///Decrement the left hand side by u
110 Point<T>& operator -=(const Point<T>& u) {
116 ///Multiply the left hand side with a scalar
117 Point<T>& operator *=(const T &u) {
123 ///Divide the left hand side by a scalar
124 Point<T>& operator /=(const T &u) {
130 ///Return the scalar product of two vectors
131 T operator *(const Point<T>& u) const {
135 ///Return the sum of two vectors
136 Point<T> operator+(const Point<T> &u) const {
141 ///Return the neg of the vectors
142 Point<T> operator-() const {
148 ///Return the difference of two vectors
149 Point<T> operator-(const Point<T> &u) const {
154 ///Return a vector multiplied by a scalar
155 Point<T> operator*(const T &u) const {
160 ///Return a vector divided by a scalar
161 Point<T> operator/(const T &u) const {
167 bool operator==(const Point<T> &u) const {
168 return (x==u.x) && (y==u.y);
172 bool operator!=(Point u) const {
173 return (x!=u.x) || (y!=u.y);
182 template <typename T>
183 inline Point<T> makePoint(const T& x, const T& y) {
184 return Point<T>(x, y);
187 ///Return a vector multiplied by a scalar
189 ///Return a vector multiplied by a scalar
191 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
195 ///Read a plainvector from a stream
197 ///Read a plainvector from a stream
201 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
204 if (c != '(') is.putback(c);
208 if (!(is >> z.x)) return is;
210 if (c != ',') is.putback(c);
214 if (!(is >> z.y)) return is;
216 if (c != ')') is.putback(c);
223 ///Write a plainvector to a stream
225 ///Write a plainvector to a stream
229 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
231 os << "(" << z.x << ", " << z.y << ")";
235 ///Rotate by 90 degrees
237 ///Returns its parameter rotated by 90 degrees in positive direction.
241 inline Point<T> rot90(const Point<T> &z)
243 return Point<T>(-z.y,z.x);
246 ///Rotate by 180 degrees
248 ///Returns its parameter rotated by 180 degrees.
252 inline Point<T> rot180(const Point<T> &z)
254 return Point<T>(-z.x,-z.y);
257 ///Rotate by 270 degrees
259 ///Returns its parameter rotated by 90 degrees in negative direction.
263 inline Point<T> rot270(const Point<T> &z)
265 return Point<T>(z.y,-z.x);
270 /// A class to calculate or store the bounding box of plainvectors.
272 /// A class to calculate or store the bounding box of plainvectors.
274 ///\author Attila Bernath
277 Point<T> bottom_left, top_right;
281 ///Default constructor: creates an empty bounding box
282 BoundingBox() { _empty = true; }
284 ///Construct an instance from one point
285 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
287 ///Were any points added?
292 ///Make the BoundingBox empty
297 ///Give back the bottom left corner
299 ///Give back the bottom left corner.
300 ///If the bounding box is empty, then the return value is not defined.
301 Point<T> bottomLeft() const {
305 ///Set the bottom left corner
307 ///Set the bottom left corner.
308 ///It should only bee used for non-empty box.
309 void bottomLeft(Point<T> p) {
313 ///Give back the top right corner
315 ///Give back the top right corner.
316 ///If the bounding box is empty, then the return value is not defined.
317 Point<T> topRight() const {
321 ///Set the top right corner
323 ///Set the top right corner.
324 ///It should only bee used for non-empty box.
325 void topRight(Point<T> p) {
329 ///Give back the bottom right corner
331 ///Give back the bottom right corner.
332 ///If the bounding box is empty, then the return value is not defined.
333 Point<T> bottomRight() const {
334 return Point<T>(top_right.x,bottom_left.y);
337 ///Set the bottom right corner
339 ///Set the bottom right corner.
340 ///It should only bee used for non-empty box.
341 void bottomRight(Point<T> p) {
346 ///Give back the top left corner
348 ///Give back the top left corner.
349 ///If the bounding box is empty, then the return value is not defined.
350 Point<T> topLeft() const {
351 return Point<T>(bottom_left.x,top_right.y);
354 ///Set the top left corner
356 ///Set the top left corner.
357 ///It should only bee used for non-empty box.
358 void topLeft(Point<T> p) {
363 ///Give back the bottom of the box
365 ///Give back the bottom of the box.
366 ///If the bounding box is empty, then the return value is not defined.
368 return bottom_left.y;
371 ///Set the bottom of the box
373 ///Set the bottom of the box.
374 ///It should only bee used for non-empty box.
379 ///Give back the top of the box
381 ///Give back the top of the box.
382 ///If the bounding box is empty, then the return value is not defined.
387 ///Set the top of the box
389 ///Set the top of the box.
390 ///It should only bee used for non-empty box.
395 ///Give back the left side of the box
397 ///Give back the left side of the box.
398 ///If the bounding box is empty, then the return value is not defined.
400 return bottom_left.x;
403 ///Set the left side of the box
405 ///Set the left side of the box.
406 ///It should only bee used for non-empty box
411 /// Give back the right side of the box
413 /// Give back the right side of the box.
414 ///If the bounding box is empty, then the return value is not defined.
419 ///Set the right side of the box
421 ///Set the right side of the box.
422 ///It should only bee used for non-empty box
427 ///Give back the height of the box
429 ///Give back the height of the box.
430 ///If the bounding box is empty, then the return value is not defined.
432 return top_right.y-bottom_left.y;
435 ///Give back the width of the box
437 ///Give back the width of the box.
438 ///If the bounding box is empty, then the return value is not defined.
440 return top_right.x-bottom_left.x;
443 ///Checks whether a point is inside a bounding box
444 bool inside(const Point<T>& u){
448 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
449 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
453 ///Increments a bounding box with a point
454 BoundingBox& add(const Point<T>& u){
456 bottom_left=top_right=u;
460 if (bottom_left.x > u.x) bottom_left.x = u.x;
461 if (bottom_left.y > u.y) bottom_left.y = u.y;
462 if (top_right.x < u.x) top_right.x = u.x;
463 if (top_right.y < u.y) top_right.y = u.y;
468 ///Increments a bounding to contain another bounding box
469 BoundingBox& add(const BoundingBox &u){
471 this->add(u.bottomLeft());
472 this->add(u.topRight());
477 ///Intersection of two bounding boxes
478 BoundingBox operator &(const BoundingBox& u){
480 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
481 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
482 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
483 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
484 b._empty = this->_empty || u._empty ||
485 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
489 };//class Boundingbox
492 ///Map of x-coordinates of a dim2::Point<>-map
495 ///Map of x-coordinates of a dim2::Point<>-map
503 typedef typename M::Value::Value Value;
504 typedef typename M::Key Key;
506 XMap(M& map) : _map(map) {}
507 Value operator[](Key k) const {return _map[k].x;}
508 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
511 ///Returns an \ref XMap class
513 ///This function just returns an \ref XMap class.
518 inline XMap<M> xMap(M &m)
524 inline XMap<M> xMap(const M &m)
529 ///Constant (read only) version of \ref XMap
532 ///Constant (read only) version of \ref XMap
540 typedef typename M::Value::Value Value;
541 typedef typename M::Key Key;
543 ConstXMap(const M &map) : _map(map) {}
544 Value operator[](Key k) const {return _map[k].x;}
547 ///Returns a \ref ConstXMap class
549 ///This function just returns an \ref ConstXMap class.
552 ///\relates ConstXMap
554 inline ConstXMap<M> xMap(const M &m)
556 return ConstXMap<M>(m);
559 ///Map of y-coordinates of a dim2::Point<>-map
562 ///Map of y-coordinates of a dim2::Point<>-map
570 typedef typename M::Value::Value Value;
571 typedef typename M::Key Key;
573 YMap(M& map) : _map(map) {}
574 Value operator[](Key k) const {return _map[k].y;}
575 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
578 ///Returns an \ref YMap class
580 ///This function just returns an \ref YMap class.
585 inline YMap<M> yMap(M &m)
591 inline YMap<M> yMap(const M &m)
596 ///Constant (read only) version of \ref YMap
599 ///Constant (read only) version of \ref YMap
607 typedef typename M::Value::Value Value;
608 typedef typename M::Key Key;
610 ConstYMap(const M &map) : _map(map) {}
611 Value operator[](Key k) const {return _map[k].y;}
614 ///Returns a \ref ConstYMap class
616 ///This function just returns an \ref ConstYMap class.
619 ///\relates ConstYMap
621 inline ConstYMap<M> yMap(const M &m)
623 return ConstYMap<M>(m);
627 ///\brief Map of the \ref Point::normSquare() "normSquare()"
628 ///of an \ref Point "Point"-map
630 ///Map of the \ref Point::normSquare() "normSquare()"
631 ///of an \ref Point "Point"-map
640 typedef typename M::Value::Value Value;
641 typedef typename M::Key Key;
643 NormSquareMap(const M &map) : _map(map) {}
644 Value operator[](Key k) const {return _map[k].normSquare();}
647 ///Returns a \ref NormSquareMap class
649 ///This function just returns an \ref NormSquareMap class.
652 ///\relates NormSquareMap
654 inline NormSquareMap<M> normSquareMap(const M &m)
656 return NormSquareMap<M>(m);
665 #endif //LEMON_DIM2_H