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::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 ///\note As you might have noticed, this class does not follow the
51 ///\ref naming_conv "LEMON Coding Style" (it should be called \c Xy
52 ///according to it). There is a stupid Hungarian proverb, "A kivétel
53 ///erõsíti a szabályt" ("An exception
54 ///reinforces a rule", which is
55 ///actually a mistranslation of the Latin proverb "Exceptio probat regulam").
56 ///This class is an example for that.
57 ///\author Attila Bernath
70 ///Default constructor
73 ///Construct an instance from coordinates
74 xy(T a, T b) : x(a), y(b) { }
77 ///Conversion constructor
78 template<class TT> xy(const xy<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 xy<T>& operator +=(const xy<T>& u) {
92 ///Decrement the left hand side by u
93 xy<T>& operator -=(const xy<T>& u) {
99 ///Multiply the left hand side with a scalar
100 xy<T>& operator *=(const T &u) {
106 ///Divide the left hand side by a scalar
107 xy<T>& operator /=(const T &u) {
113 ///Return the scalar product of two vectors
114 T operator *(const xy<T>& u) const {
118 ///Return the sum of two vectors
119 xy<T> operator+(const xy<T> &u) const {
124 ///Return the neg of the vectors
125 xy<T> operator-() const {
131 ///Return the difference of two vectors
132 xy<T> operator-(const xy<T> &u) const {
137 ///Return a vector multiplied by a scalar
138 xy<T> operator*(const T &u) const {
143 ///Return a vector divided by a scalar
144 xy<T> operator/(const T &u) const {
150 bool operator==(const xy<T> &u) const {
151 return (x==u.x) && (y==u.y);
155 bool operator!=(xy u) const {
156 return (x!=u.x) || (y!=u.y);
165 template <typename T>
166 inline xy<T> make_xy(const T& x, const T& y) {
170 ///Return a vector multiplied by a scalar
172 ///Return a vector multiplied by a scalar
174 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
178 ///Read a plainvector from a stream
180 ///Read a plainvector from a stream
184 inline std::istream& operator>>(std::istream &is, xy<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 xy<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 xy<T> rot90(const xy<T> &z)
226 return xy<T>(-z.y,z.x);
229 ///Rotate by 180 degrees
231 ///Returns its parameter rotated by 180 degrees.
235 inline xy<T> rot180(const xy<T> &z)
237 return xy<T>(-z.x,-z.y);
240 ///Rotate by 270 degrees
242 ///Returns its parameter rotated by 90 degrees in negative direction.
246 inline xy<T> rot270(const xy<T> &z)
248 return xy<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 xy<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(xy<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 xy<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(xy<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 xy<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(xy<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 xy<T> bottomRight() const {
317 return xy<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(xy<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 xy<T> topLeft() const {
334 return xy<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(xy<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 xy<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 xy<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 xy<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 an xy<>-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 an xy<>-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 xy::normSquare() "normSquare()" of an \ref xy "xy"-map
621 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-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);