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/utility.h>
27 ///\brief A simple two dimensional vector and a bounding box implementation
29 /// The class \ref lemon::xy "xy" implements
30 ///a two dimensional vector with the usual
33 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
34 /// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
36 ///\author Attila Bernath
44 /// A simple two dimensional vector (plainvector) implementation
46 /// A simple two dimensional vector (plainvector) implementation
47 ///with the usual vector
50 ///\author Attila Bernath
60 ///Default constructor
63 ///Constructing the instance from coordinates
64 xy(T a, T b) : x(a), y(b) { }
67 ///Conversion constructor
68 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
70 ///Gives back the square of the norm of the vector
71 T normSquare() const {
75 ///Increments the left hand side by u
76 xy<T>& operator +=(const xy<T>& u) {
82 ///Decrements the left hand side by u
83 xy<T>& operator -=(const xy<T>& u) {
89 ///Multiplying the left hand side with a scalar
90 xy<T>& operator *=(const T &u) {
96 ///Dividing the left hand side by a scalar
97 xy<T>& operator /=(const T &u) {
103 ///Returns the scalar product of two vectors
104 T operator *(const xy<T>& u) const {
108 ///Returns the sum of two vectors
109 xy<T> operator+(const xy<T> &u) const {
114 ///Returns the neg of the vectors
115 xy<T> operator-() const {
121 ///Returns the difference of two vectors
122 xy<T> operator-(const xy<T> &u) const {
127 ///Returns a vector multiplied by a scalar
128 xy<T> operator*(const T &u) const {
133 ///Returns a vector divided by a scalar
134 xy<T> operator/(const T &u) const {
140 bool operator==(const xy<T> &u) const {
141 return (x==u.x) && (y==u.y);
144 ///Testing inequality
145 bool operator!=(xy u) const {
146 return (x!=u.x) || (y!=u.y);
151 ///Returns a vector multiplied by a scalar
153 ///Returns a vector multiplied by a scalar
155 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
159 ///Read a plainvector from a stream
161 ///Read a plainvector from a stream
165 inline std::istream& operator>>(std::istream &is, xy<T> &z) {
168 if (c != '(') is.putback(c);
172 if (!(is >> z.x)) return is;
174 if (c != ',') is.putback(c);
178 if (!(is >> z.y)) return is;
180 if (c != ')') is.putback(c);
187 ///Write a plainvector to a stream
189 ///Write a plainvector to a stream
193 inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
195 os << "(" << z.x << ", " << z.y << ")";
199 ///Rotate by 90 degrees
201 ///Returns its parameter rotated by 90 degrees in positive direction.
205 inline xy<T> rot90(const xy<T> &z)
207 return xy<T>(-z.y,z.x);
210 ///Rotate by 270 degrees
212 ///Returns its parameter rotated by 90 degrees in negative direction.
216 inline xy<T> rot270(const xy<T> &z)
218 return xy<T>(z.y,-z.x);
223 /// A class to calculate or store the bounding box of plainvectors.
225 /// A class to calculate or store the bounding box of plainvectors.
227 ///\author Attila Bernath
230 xy<T> bottom_left, top_right;
234 ///Default constructor: creates an empty bounding box
235 BoundingBox() { _empty = true; }
237 ///Constructing the instance from one point
238 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
240 ///Were any points added?
245 ///Makes the BoundingBox empty
250 ///\brief Gives back the bottom left corner
251 ///(if the bounding box is empty, then the return value is not defined)
252 xy<T> bottomLeft() const {
256 ///\brief Sets the bottom left corner
257 ///(should only bee used for non-empty box)
258 void bottomLeft(xy<T> p) {
262 ///\brief Gives back the top right corner
263 ///(if the bounding box is empty, then the return value is not defined)
264 xy<T> topRight() const {
268 ///\brief Sets the top right corner
269 ///(should only bee used for non-empty box)
270 void topRight(xy<T> p) {
274 ///\brief Gives back the bottom right corner
275 ///(if the bounding box is empty, then the return value is not defined)
276 xy<T> bottomRight() const {
277 return xy<T>(top_right.x,bottom_left.y);
280 ///\brief Sets the bottom right corner
281 ///(should only bee used for non-empty box)
282 void bottomRight(xy<T> p) {
287 ///\brief Gives back the top left corner
288 ///(if the bounding box is empty, then the return value is not defined)
289 xy<T> topLeft() const {
290 return xy<T>(bottom_left.x,top_right.y);
293 ///\brief Sets the top left corner
294 ///(should only bee used for non-empty box)
295 void topLeft(xy<T> p) {
300 ///\brief Gives back the bottom of the box
301 ///(if the bounding box is empty, then the return value is not defined)
303 return bottom_left.y;
306 ///\brief Sets the bottom of the box
307 ///(should only bee used for non-empty box)
312 ///\brief Gives back the top of the box
313 ///(if the bounding box is empty, then the return value is not defined)
318 ///\brief Sets the top of the box
319 ///(should only bee used for non-empty box)
324 ///\brief Gives back the left side of the box
325 ///(if the bounding box is empty, then the return value is not defined)
327 return bottom_left.x;
330 ///\brief Sets the left side of the box
331 ///(should only bee used for non-empty box)
336 ///\brief Gives back the right side of the box
337 ///(if the bounding box is empty, then the return value is not defined)
342 ///\brief Sets the right side of the box
343 ///(should only bee used for non-empty box)
348 ///\brief Gives back the height of the box
349 ///(if the bounding box is empty, then the return value is not defined)
351 return top_right.y-bottom_left.y;
354 ///\brief Gives back the width of the box
355 ///(if the bounding box is empty, then the return value is not defined)
357 return top_right.x-bottom_left.x;
360 ///Checks whether a point is inside a bounding box
361 bool inside(const xy<T>& u){
365 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
366 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
370 ///Increments a bounding box with a point
371 BoundingBox& add(const xy<T>& u){
373 bottom_left=top_right=u;
377 if (bottom_left.x > u.x) bottom_left.x = u.x;
378 if (bottom_left.y > u.y) bottom_left.y = u.y;
379 if (top_right.x < u.x) top_right.x = u.x;
380 if (top_right.y < u.y) top_right.y = u.y;
385 // ///Sums a bounding box and a point
386 // BoundingBox operator +(const xy<T>& u){
387 // BoundingBox b = *this;
391 ///Increments a bounding box with an other bounding box
392 BoundingBox& add(const BoundingBox &u){
394 this->add(u.bottomLeft());
395 this->add(u.topRight());
400 ///Sums two bounding boxes
401 BoundingBox operator +(const BoundingBox& u){
402 BoundingBox b = *this;
407 ///Intersection of two bounding boxes
408 BoundingBox operator &(const BoundingBox& u){
410 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
411 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
412 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
413 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
414 b._empty = this->_empty || u._empty ||
415 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
419 };//class Boundingbox
422 ///Map of x-coordinates of an xy<>-map
432 typedef typename M::Value::Value Value;
433 typedef typename M::Key Key;
435 XMap(M& map) : _map(map) {}
436 Value operator[](Key k) const {return _map[k].x;}
437 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
440 ///Returns an \ref XMap class
442 ///This function just returns an \ref XMap class.
447 inline XMap<M> xMap(M &m)
453 inline XMap<M> xMap(const M &m)
458 ///Constant (read only) version of \ref XMap
468 typedef typename M::Value::Value Value;
469 typedef typename M::Key Key;
471 ConstXMap(const M &map) : _map(map) {}
472 Value operator[](Key k) const {return _map[k].x;}
475 ///Returns a \ref ConstXMap class
477 ///This function just returns an \ref ConstXMap class.
480 ///\relates ConstXMap
482 inline ConstXMap<M> xMap(const M &m)
484 return ConstXMap<M>(m);
487 ///Map of y-coordinates of an xy<>-map
497 typedef typename M::Value::Value Value;
498 typedef typename M::Key Key;
500 YMap(M& map) : _map(map) {}
501 Value operator[](Key k) const {return _map[k].y;}
502 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
505 ///Returns an \ref YMap class
507 ///This function just returns an \ref YMap class.
512 inline YMap<M> yMap(M &m)
518 inline YMap<M> yMap(const M &m)
523 ///Constant (read only) version of \ref YMap
533 typedef typename M::Value::Value Value;
534 typedef typename M::Key Key;
536 ConstYMap(const M &map) : _map(map) {}
537 Value operator[](Key k) const {return _map[k].y;}
540 ///Returns a \ref ConstYMap class
542 ///This function just returns an \ref ConstYMap class.
545 ///\relates ConstYMap
547 inline ConstYMap<M> yMap(const M &m)
549 return ConstYMap<M>(m);
553 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
555 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
564 typedef typename M::Value::Value Value;
565 typedef typename M::Key Key;
567 NormSquareMap(const M &map) : _map(map) {}
568 Value operator[](Key k) const {return _map[k].normSquare();}
571 ///Returns a \ref NormSquareMap class
573 ///This function just returns an \ref NormSquareMap class.
576 ///\relates NormSquareMap
578 inline NormSquareMap<M> normSquareMap(const M &m)
580 return NormSquareMap<M>(m);