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 ///Conversion constructor
78 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
80 ///Give back the square of the norm of the vector
81 T normSquare() const {
85 ///Increment the left hand side by u
86 Point<T>& operator +=(const Point<T>& u) {
92 ///Decrement the left hand side by u
93 Point<T>& operator -=(const Point<T>& u) {
99 ///Multiply the left hand side with a scalar
100 Point<T>& operator *=(const T &u) {
106 ///Divide the left hand side by a scalar
107 Point<T>& operator /=(const T &u) {
113 ///Return the scalar product of two vectors
114 T operator *(const Point<T>& u) const {
118 ///Return the sum of two vectors
119 Point<T> operator+(const Point<T> &u) const {
124 ///Return the neg of the vectors
125 Point<T> operator-() const {
131 ///Return the difference of two vectors
132 Point<T> operator-(const Point<T> &u) const {
137 ///Return a vector multiplied by a scalar
138 Point<T> operator*(const T &u) const {
143 ///Return a vector divided by a scalar
144 Point<T> operator/(const T &u) const {
150 bool operator==(const Point<T> &u) const {
151 return (x==u.x) && (y==u.y);
155 bool operator!=(Point u) const {
156 return (x!=u.x) || (y!=u.y);
165 template <typename T>
166 inline Point<T> make_Point(const T& x, const T& y) {
167 return Point<T>(x, y);
170 ///Return a vector multiplied by a scalar
172 ///Return a vector multiplied by a scalar
174 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
178 ///Read a plainvector from a stream
180 ///Read a plainvector from a stream
184 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
187 if (c != '(') is.putback(c);
191 if (!(is >> z.x)) return is;
193 if (c != ',') is.putback(c);
197 if (!(is >> z.y)) return is;
199 if (c != ')') is.putback(c);
206 ///Write a plainvector to a stream
208 ///Write a plainvector to a stream
212 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
214 os << "(" << z.x << ", " << z.y << ")";
218 ///Rotate by 90 degrees
220 ///Returns its parameter rotated by 90 degrees in positive direction.
224 inline Point<T> rot90(const Point<T> &z)
226 return Point<T>(-z.y,z.x);
229 ///Rotate by 180 degrees
231 ///Returns its parameter rotated by 180 degrees.
235 inline Point<T> rot180(const Point<T> &z)
237 return Point<T>(-z.x,-z.y);
240 ///Rotate by 270 degrees
242 ///Returns its parameter rotated by 90 degrees in negative direction.
246 inline Point<T> rot270(const Point<T> &z)
248 return Point<T>(z.y,-z.x);
253 /// A class to calculate or store the bounding box of plainvectors.
255 /// A class to calculate or store the bounding box of plainvectors.
257 ///\author Attila Bernath
260 Point<T> bottom_left, top_right;
264 ///Default constructor: creates an empty bounding box
265 BoundingBox() { _empty = true; }
267 ///Construct an instance from one point
268 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
270 ///Were any points added?
275 ///Make the BoundingBox empty
280 ///Give back the bottom left corner
282 ///Give back the bottom left corner.
283 ///If the bounding box is empty, then the return value is not defined.
284 Point<T> bottomLeft() const {
288 ///Set the bottom left corner
290 ///Set the bottom left corner.
291 ///It should only bee used for non-empty box.
292 void bottomLeft(Point<T> p) {
296 ///Give back the top right corner
298 ///Give back the top right corner.
299 ///If the bounding box is empty, then the return value is not defined.
300 Point<T> topRight() const {
304 ///Set the top right corner
306 ///Set the top right corner.
307 ///It should only bee used for non-empty box.
308 void topRight(Point<T> p) {
312 ///Give back the bottom right corner
314 ///Give back the bottom right corner.
315 ///If the bounding box is empty, then the return value is not defined.
316 Point<T> bottomRight() const {
317 return Point<T>(top_right.x,bottom_left.y);
320 ///Set the bottom right corner
322 ///Set the bottom right corner.
323 ///It should only bee used for non-empty box.
324 void bottomRight(Point<T> p) {
329 ///Give back the top left corner
331 ///Give back the top left corner.
332 ///If the bounding box is empty, then the return value is not defined.
333 Point<T> topLeft() const {
334 return Point<T>(bottom_left.x,top_right.y);
337 ///Set the top left corner
339 ///Set the top left corner.
340 ///It should only bee used for non-empty box.
341 void topLeft(Point<T> p) {
346 ///Give back the bottom of the box
348 ///Give back the bottom of the box.
349 ///If the bounding box is empty, then the return value is not defined.
351 return bottom_left.y;
354 ///Set the bottom of the box
356 ///Set the bottom of the box.
357 ///It should only bee used for non-empty box.
362 ///Give back the top of the box
364 ///Give back the top of the box.
365 ///If the bounding box is empty, then the return value is not defined.
370 ///Set the top of the box
372 ///Set the top of the box.
373 ///It should only bee used for non-empty box.
378 ///Give back the left side of the box
380 ///Give back the left side of the box.
381 ///If the bounding box is empty, then the return value is not defined.
383 return bottom_left.x;
386 ///Set the left side of the box
388 ///Set the left side of the box.
389 ///It should only bee used for non-empty box
394 /// Give back the right side of the box
396 /// Give back the right side of the box.
397 ///If the bounding box is empty, then the return value is not defined.
402 ///Set the right side of the box
404 ///Set the right side of the box.
405 ///It should only bee used for non-empty box
410 ///Give back the height of the box
412 ///Give back the height of the box.
413 ///If the bounding box is empty, then the return value is not defined.
415 return top_right.y-bottom_left.y;
418 ///Give back the width of the box
420 ///Give back the width of the box.
421 ///If the bounding box is empty, then the return value is not defined.
423 return top_right.x-bottom_left.x;
426 ///Checks whether a point is inside a bounding box
427 bool inside(const Point<T>& u){
431 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
432 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
436 ///Increments a bounding box with a point
437 BoundingBox& add(const Point<T>& u){
439 bottom_left=top_right=u;
443 if (bottom_left.x > u.x) bottom_left.x = u.x;
444 if (bottom_left.y > u.y) bottom_left.y = u.y;
445 if (top_right.x < u.x) top_right.x = u.x;
446 if (top_right.y < u.y) top_right.y = u.y;
451 // ///Sums a bounding box and a point
452 // BoundingBox operator +(const Point<T>& u){
453 // BoundingBox b = *this;
457 ///Increments a bounding box with another bounding box
458 BoundingBox& add(const BoundingBox &u){
460 this->add(u.bottomLeft());
461 this->add(u.topRight());
466 ///Sums two bounding boxes
467 BoundingBox operator +(const BoundingBox& u){
468 BoundingBox b = *this;
473 ///Intersection of two bounding boxes
474 BoundingBox operator &(const BoundingBox& u){
476 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
477 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
478 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
479 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
480 b._empty = this->_empty || u._empty ||
481 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
485 };//class Boundingbox
488 ///Map of x-coordinates of a dim2::Point<>-map
498 typedef typename M::Value::Value Value;
499 typedef typename M::Key Key;
501 XMap(M& map) : _map(map) {}
502 Value operator[](Key k) const {return _map[k].x;}
503 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
506 ///Returns an \ref XMap class
508 ///This function just returns an \ref XMap class.
513 inline XMap<M> xMap(M &m)
519 inline XMap<M> xMap(const M &m)
524 ///Constant (read only) version of \ref XMap
534 typedef typename M::Value::Value Value;
535 typedef typename M::Key Key;
537 ConstXMap(const M &map) : _map(map) {}
538 Value operator[](Key k) const {return _map[k].x;}
541 ///Returns a \ref ConstXMap class
543 ///This function just returns an \ref ConstXMap class.
546 ///\relates ConstXMap
548 inline ConstXMap<M> xMap(const M &m)
550 return ConstXMap<M>(m);
553 ///Map of y-coordinates of a dim2::Point<>-map
563 typedef typename M::Value::Value Value;
564 typedef typename M::Key Key;
566 YMap(M& map) : _map(map) {}
567 Value operator[](Key k) const {return _map[k].y;}
568 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
571 ///Returns an \ref YMap class
573 ///This function just returns an \ref YMap class.
578 inline YMap<M> yMap(M &m)
584 inline YMap<M> yMap(const M &m)
589 ///Constant (read only) version of \ref YMap
599 typedef typename M::Value::Value Value;
600 typedef typename M::Key Key;
602 ConstYMap(const M &map) : _map(map) {}
603 Value operator[](Key k) const {return _map[k].y;}
606 ///Returns a \ref ConstYMap class
608 ///This function just returns an \ref ConstYMap class.
611 ///\relates ConstYMap
613 inline ConstYMap<M> yMap(const M &m)
615 return ConstYMap<M>(m);
619 ///Map of the \ref Point::normSquare() "normSquare()" of an \ref Point "Point"-map
621 ///Map of the \ref Point::normSquare() "normSquare()" of an \ref Point "Point"-map
630 typedef typename M::Value::Value Value;
631 typedef typename M::Key Key;
633 NormSquareMap(const M &map) : _map(map) {}
634 Value operator[](Key k) const {return _map[k].normSquare();}
637 ///Returns a \ref NormSquareMap class
639 ///This function just returns an \ref NormSquareMap class.
642 ///\relates NormSquareMap
644 inline NormSquareMap<M> normSquareMap(const M &m)
646 return NormSquareMap<M>(m);
655 #endif //LEMON_DIM2_H