2 * lemon/xy.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
21 #include <lemon/utility.h>
25 ///\brief A simple two dimensional vector and a bounding box implementation
27 /// The class \ref lemon::xy "xy" implements
28 ///a two dimensional vector with the usual
31 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
32 /// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
34 ///\author Attila Bernath
42 /// A simple two dimensional vector (plainvector) implementation
44 /// A simple two dimensional vector (plainvector) implementation
45 ///with the usual vector
48 ///\author Attila Bernath
58 ///Default constructor
61 ///Constructing the instance from coordinates
62 xy(T a, T b) : x(a), y(b) { }
65 ///Conversion constructor
66 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
68 ///Gives back the square of the norm of the vector
69 T normSquare() const {
73 ///Increments the left hand side by u
74 xy<T>& operator +=(const xy<T>& u) {
80 ///Decrements the left hand side by u
81 xy<T>& operator -=(const xy<T>& u) {
87 ///Multiplying the left hand side with a scalar
88 xy<T>& operator *=(const T &u) {
94 ///Dividing the left hand side by a scalar
95 xy<T>& operator /=(const T &u) {
101 ///Returns the scalar product of two vectors
102 T operator *(const xy<T>& u) const {
106 ///Returns the sum of two vectors
107 xy<T> operator+(const xy<T> &u) const {
112 ///Returns the neg of the vectors
113 xy<T> operator-() const {
119 ///Returns the difference of two vectors
120 xy<T> operator-(const xy<T> &u) const {
125 ///Returns a vector multiplied by a scalar
126 xy<T> operator*(const T &u) const {
131 ///Returns a vector divided by a scalar
132 xy<T> operator/(const T &u) const {
138 bool operator==(const xy<T> &u) const {
139 return (x==u.x) && (y==u.y);
142 ///Testing inequality
143 bool operator!=(xy u) const {
144 return (x!=u.x) || (y!=u.y);
149 ///Returns a vector multiplied by a scalar
151 ///Returns a vector multiplied by a scalar
153 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
157 ///Read a plainvector from a stream
159 ///Read a plainvector from a stream
163 inline std::istream& operator>>(std::istream &is, xy<T> &z) {
166 if (c != '(') is.putback(c);
170 if (!(is >> z.x)) return is;
172 if (c != ',') is.putback(c);
176 if (!(is >> z.y)) return is;
178 if (c != ')') is.putback(c);
185 ///Write a plainvector to a stream
187 ///Write a plainvector to a stream
191 inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
193 os << "(" << z.x << ", " << z.y << ")";
197 ///Rotate by 90 degrees
199 ///Returns its parameter rotated by 90 degrees in positive direction.
203 inline xy<T> rot90(const xy<T> &z)
205 return xy<T>(-z.y,z.x);
208 ///Rotate by 270 degrees
210 ///Returns its parameter rotated by 90 degrees in negative direction.
214 inline xy<T> rot270(const xy<T> &z)
216 return xy<T>(z.y,-z.x);
221 /// A class to calculate or store the bounding box of plainvectors.
223 /// A class to calculate or store the bounding box of plainvectors.
225 ///\author Attila Bernath
228 xy<T> bottom_left, top_right;
232 ///Default constructor: creates an empty bounding box
233 BoundingBox() { _empty = true; }
235 ///Constructing the instance from one point
236 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
238 ///Were any points added?
243 ///Makes the BoundingBox empty
248 ///Gives back the bottom left corner (if the bounding box is empty, then the return value is not defined)
249 xy<T> bottomLeft() const {
253 ///Gives back the top right corner (if the bounding box is empty, then the return value is not defined)
254 xy<T> topRight() const {
258 ///Gives back the bottom right corner (if the bounding box is empty, then the return value is not defined)
259 xy<T> bottomRight() const {
260 return xy<T>(top_right.x,bottom_left.y);
263 ///Gives back the top left corner (if the bounding box is empty, then the return value is not defined)
264 xy<T> topLeft() const {
265 return xy<T>(bottom_left.x,top_right.y);
268 ///Gives back the bottom of the box (if the bounding box is empty, then the return value is not defined)
270 return bottom_left.y;
273 ///Gives back the top of the box (if the bounding box is empty, then the return value is not defined)
278 ///Gives back the left side of the box (if the bounding box is empty, then the return value is not defined)
280 return bottom_left.x;
283 ///Gives back the right side of the box (if the bounding box is empty, then the return value is not defined)
288 ///Gives back the height of the box (if the bounding box is empty, then the return value is not defined)
290 return top_right.y-bottom_left.y;
293 ///Gives back the width of the box (if the bounding box is empty, then the return value is not defined)
295 return top_right.x-bottom_left.x;
298 ///Checks whether a point is inside a bounding box
299 bool inside(const xy<T>& u){
303 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
304 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
308 ///Increments a bounding box with a point
309 BoundingBox& add(const xy<T>& u){
311 bottom_left=top_right=u;
315 if (bottom_left.x > u.x) bottom_left.x = u.x;
316 if (bottom_left.y > u.y) bottom_left.y = u.y;
317 if (top_right.x < u.x) top_right.x = u.x;
318 if (top_right.y < u.y) top_right.y = u.y;
323 // ///Sums a bounding box and a point
324 // BoundingBox operator +(const xy<T>& u){
325 // BoundingBox b = *this;
329 ///Increments a bounding box with an other bounding box
330 BoundingBox& add(const BoundingBox &u){
332 this->add(u.bottomLeft());
333 this->add(u.topRight());
338 ///Sums two bounding boxes
339 BoundingBox operator +(const BoundingBox& u){
340 BoundingBox b = *this;
345 ///Intersection of two bounding boxes
346 BoundingBox operator &(const BoundingBox& u){
348 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
349 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
350 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
351 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
352 b._empty = this->_empty || u._empty ||
353 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
357 };//class Boundingbox
360 ///Map of x-coordinates of an xy<>-map
367 typename SmartReference<M>::Type _map;
369 typedef True NeedCopy;
371 typedef typename M::Value::Value Value;
372 typedef typename M::Key Key;
374 XMap(typename SmartParameter<M>::Type map) : _map(map) {}
375 Value operator[](Key k) const {return _map[k].x;}
376 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
379 ///Returns an \ref XMap class
381 ///This function just returns an \ref XMap class.
386 inline XMap<M> xMap(M &m)
392 inline XMap<M> xMap(const M &m)
397 ///Constant (read only) version of \ref XMap
404 typename SmartConstReference<M>::Type _map;
406 typedef True NeedCopy;
408 typedef typename M::Value::Value Value;
409 typedef typename M::Key Key;
411 ConstXMap(const M &map) : _map(map) {}
412 Value operator[](Key k) const {return _map[k].x;}
415 ///Returns a \ref ConstXMap class
417 ///This function just returns an \ref ConstXMap class.
420 ///\relates ConstXMap
422 inline ConstXMap<M> xMap(const M &m)
424 return ConstXMap<M>(m);
427 ///Map of y-coordinates of an xy<>-map
434 typename SmartReference<M>::Type _map;
436 typedef True NeedCopy;
438 typedef typename M::Value::Value Value;
439 typedef typename M::Key Key;
441 YMap(typename SmartParameter<M>::Type map) : _map(map) {}
442 Value operator[](Key k) const {return _map[k].y;}
443 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
446 ///Returns an \ref YMap class
448 ///This function just returns an \ref YMap class.
453 inline YMap<M> yMap(M &m)
459 inline YMap<M> yMap(const M &m)
464 ///Constant (read only) version of \ref YMap
471 typename SmartConstReference<M>::Type _map;
473 typedef True NeedCopy;
475 typedef typename M::Value::Value Value;
476 typedef typename M::Key Key;
478 ConstYMap(const M &map) : _map(map) {}
479 Value operator[](Key k) const {return _map[k].y;}
482 ///Returns a \ref ConstYMap class
484 ///This function just returns an \ref ConstYMap class.
487 ///\relates ConstYMap
489 inline ConstYMap<M> yMap(const M &m)
491 return ConstYMap<M>(m);
495 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
497 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
503 typename SmartConstReference<M>::Type _map;
505 typedef True NeedCopy;
507 typedef typename M::Value::Value Value;
508 typedef typename M::Key Key;
510 NormSquareMap(const M &map) : _map(map) {}
511 Value operator[](Key k) const {return _map[k].normSquare();}
514 ///Returns a \ref NormSquareMap class
516 ///This function just returns an \ref NormSquareMap class.
519 ///\relates NormSquareMap
521 inline NormSquareMap<M> normSquareMap(const M &m)
523 return NormSquareMap<M>(m);