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) { }
77 int size() const { return 2; }
79 ///Subscripting operator
80 T& operator[](int idx) { return idx == 0 ? x : y; }
82 ///Const subscripting operator
83 const T& operator[](int idx) const { return idx == 0 ? x : y; }
85 ///Conversion constructor
86 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
88 ///Give back the square of the norm of the vector
89 T normSquare() const {
93 ///Increment the left hand side by u
94 Point<T>& operator +=(const Point<T>& u) {
100 ///Decrement the left hand side by u
101 Point<T>& operator -=(const Point<T>& u) {
107 ///Multiply the left hand side with a scalar
108 Point<T>& operator *=(const T &u) {
114 ///Divide the left hand side by a scalar
115 Point<T>& operator /=(const T &u) {
121 ///Return the scalar product of two vectors
122 T operator *(const Point<T>& u) const {
126 ///Return the sum of two vectors
127 Point<T> operator+(const Point<T> &u) const {
132 ///Return the neg of the vectors
133 Point<T> operator-() const {
139 ///Return the difference of two vectors
140 Point<T> operator-(const Point<T> &u) const {
145 ///Return a vector multiplied by a scalar
146 Point<T> operator*(const T &u) const {
151 ///Return a vector divided by a scalar
152 Point<T> operator/(const T &u) const {
158 bool operator==(const Point<T> &u) const {
159 return (x==u.x) && (y==u.y);
163 bool operator!=(Point u) const {
164 return (x!=u.x) || (y!=u.y);
173 template <typename T>
174 inline Point<T> makePoint(const T& x, const T& y) {
175 return Point<T>(x, y);
178 ///Return a vector multiplied by a scalar
180 ///Return a vector multiplied by a scalar
182 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
186 ///Read a plainvector from a stream
188 ///Read a plainvector from a stream
192 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
195 if (c != '(') is.putback(c);
199 if (!(is >> z.x)) return is;
201 if (c != ',') is.putback(c);
205 if (!(is >> z.y)) return is;
207 if (c != ')') is.putback(c);
214 ///Write a plainvector to a stream
216 ///Write a plainvector to a stream
220 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
222 os << "(" << z.x << ", " << z.y << ")";
226 ///Rotate by 90 degrees
228 ///Returns its parameter rotated by 90 degrees in positive direction.
232 inline Point<T> rot90(const Point<T> &z)
234 return Point<T>(-z.y,z.x);
237 ///Rotate by 180 degrees
239 ///Returns its parameter rotated by 180 degrees.
243 inline Point<T> rot180(const Point<T> &z)
245 return Point<T>(-z.x,-z.y);
248 ///Rotate by 270 degrees
250 ///Returns its parameter rotated by 90 degrees in negative direction.
254 inline Point<T> rot270(const Point<T> &z)
256 return Point<T>(z.y,-z.x);
261 /// A class to calculate or store the bounding box of plainvectors.
263 /// A class to calculate or store the bounding box of plainvectors.
265 ///\author Attila Bernath
268 Point<T> bottom_left, top_right;
272 ///Default constructor: creates an empty bounding box
273 BoundingBox() { _empty = true; }
275 ///Construct an instance from one point
276 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
278 ///Were any points added?
283 ///Make the BoundingBox empty
288 ///Give back the bottom left corner
290 ///Give back the bottom left corner.
291 ///If the bounding box is empty, then the return value is not defined.
292 Point<T> bottomLeft() const {
296 ///Set the bottom left corner
298 ///Set the bottom left corner.
299 ///It should only bee used for non-empty box.
300 void bottomLeft(Point<T> p) {
304 ///Give back the top right corner
306 ///Give back the top right corner.
307 ///If the bounding box is empty, then the return value is not defined.
308 Point<T> topRight() const {
312 ///Set the top right corner
314 ///Set the top right corner.
315 ///It should only bee used for non-empty box.
316 void topRight(Point<T> p) {
320 ///Give back the bottom right corner
322 ///Give back the bottom right corner.
323 ///If the bounding box is empty, then the return value is not defined.
324 Point<T> bottomRight() const {
325 return Point<T>(top_right.x,bottom_left.y);
328 ///Set the bottom right corner
330 ///Set the bottom right corner.
331 ///It should only bee used for non-empty box.
332 void bottomRight(Point<T> p) {
337 ///Give back the top left corner
339 ///Give back the top left corner.
340 ///If the bounding box is empty, then the return value is not defined.
341 Point<T> topLeft() const {
342 return Point<T>(bottom_left.x,top_right.y);
345 ///Set the top left corner
347 ///Set the top left corner.
348 ///It should only bee used for non-empty box.
349 void topLeft(Point<T> p) {
354 ///Give back the bottom of the box
356 ///Give back the bottom of the box.
357 ///If the bounding box is empty, then the return value is not defined.
359 return bottom_left.y;
362 ///Set the bottom of the box
364 ///Set the bottom of the box.
365 ///It should only bee used for non-empty box.
370 ///Give back the top of the box
372 ///Give back the top of the box.
373 ///If the bounding box is empty, then the return value is not defined.
378 ///Set the top of the box
380 ///Set the top of the box.
381 ///It should only bee used for non-empty box.
386 ///Give back the left side of the box
388 ///Give back the left side of the box.
389 ///If the bounding box is empty, then the return value is not defined.
391 return bottom_left.x;
394 ///Set the left side of the box
396 ///Set the left side of the box.
397 ///It should only bee used for non-empty box
402 /// Give back the right side of the box
404 /// Give back the right side of the box.
405 ///If the bounding box is empty, then the return value is not defined.
410 ///Set the right side of the box
412 ///Set the right side of the box.
413 ///It should only bee used for non-empty box
418 ///Give back the height of the box
420 ///Give back the height of the box.
421 ///If the bounding box is empty, then the return value is not defined.
423 return top_right.y-bottom_left.y;
426 ///Give back the width of the box
428 ///Give back the width of the box.
429 ///If the bounding box is empty, then the return value is not defined.
431 return top_right.x-bottom_left.x;
434 ///Checks whether a point is inside a bounding box
435 bool inside(const Point<T>& u){
439 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
440 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
444 ///Increments a bounding box with a point
445 BoundingBox& add(const Point<T>& u){
447 bottom_left=top_right=u;
451 if (bottom_left.x > u.x) bottom_left.x = u.x;
452 if (bottom_left.y > u.y) bottom_left.y = u.y;
453 if (top_right.x < u.x) top_right.x = u.x;
454 if (top_right.y < u.y) top_right.y = u.y;
459 // ///Sums a bounding box and a point
460 // BoundingBox operator +(const Point<T>& u){
461 // BoundingBox b = *this;
465 ///Increments a bounding box with another bounding box
466 BoundingBox& add(const BoundingBox &u){
468 this->add(u.bottomLeft());
469 this->add(u.topRight());
474 ///Sums two bounding boxes
475 BoundingBox operator +(const BoundingBox& u){
476 BoundingBox b = *this;
481 ///Intersection of two bounding boxes
482 BoundingBox operator &(const BoundingBox& u){
484 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
485 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
486 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
487 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
488 b._empty = this->_empty || u._empty ||
489 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
493 };//class Boundingbox
496 ///Map of x-coordinates of a dim2::Point<>-map
506 typedef typename M::Value::Value Value;
507 typedef typename M::Key Key;
509 XMap(M& map) : _map(map) {}
510 Value operator[](Key k) const {return _map[k].x;}
511 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
514 ///Returns an \ref XMap class
516 ///This function just returns an \ref XMap class.
521 inline XMap<M> xMap(M &m)
527 inline XMap<M> xMap(const M &m)
532 ///Constant (read only) version of \ref XMap
542 typedef typename M::Value::Value Value;
543 typedef typename M::Key Key;
545 ConstXMap(const M &map) : _map(map) {}
546 Value operator[](Key k) const {return _map[k].x;}
549 ///Returns a \ref ConstXMap class
551 ///This function just returns an \ref ConstXMap class.
554 ///\relates ConstXMap
556 inline ConstXMap<M> xMap(const M &m)
558 return ConstXMap<M>(m);
561 ///Map of y-coordinates of a dim2::Point<>-map
571 typedef typename M::Value::Value Value;
572 typedef typename M::Key Key;
574 YMap(M& map) : _map(map) {}
575 Value operator[](Key k) const {return _map[k].y;}
576 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
579 ///Returns an \ref YMap class
581 ///This function just returns an \ref YMap class.
586 inline YMap<M> yMap(M &m)
592 inline YMap<M> yMap(const M &m)
597 ///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 ///Map of the \ref Point::normSquare() "normSquare()" of an \ref Point "Point"-map
629 ///Map of the \ref Point::normSquare() "normSquare()" of an \ref Point "Point"-map
638 typedef typename M::Value::Value Value;
639 typedef typename M::Key Key;
641 NormSquareMap(const M &map) : _map(map) {}
642 Value operator[](Key k) const {return _map[k].normSquare();}
645 ///Returns a \ref NormSquareMap class
647 ///This function just returns an \ref NormSquareMap class.
650 ///\relates NormSquareMap
652 inline NormSquareMap<M> normSquareMap(const M &m)
654 return NormSquareMap<M>(m);
663 #endif //LEMON_DIM2_H