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
63 ///Default constructor
66 ///Constructing the instance from coordinates
67 xy(T a, T b) : x(a), y(b) { }
70 ///Conversion constructor
71 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
73 ///Gives back the square of the norm of the vector
74 T normSquare() const {
78 ///Increments the left hand side by u
79 xy<T>& operator +=(const xy<T>& u) {
85 ///Decrements the left hand side by u
86 xy<T>& operator -=(const xy<T>& u) {
92 ///Multiplying the left hand side with a scalar
93 xy<T>& operator *=(const T &u) {
99 ///Dividing the left hand side by a scalar
100 xy<T>& operator /=(const T &u) {
106 ///Returns the scalar product of two vectors
107 T operator *(const xy<T>& u) const {
111 ///Returns the sum of two vectors
112 xy<T> operator+(const xy<T> &u) const {
117 ///Returns the neg of the vectors
118 xy<T> operator-() const {
124 ///Returns the difference of two vectors
125 xy<T> operator-(const xy<T> &u) const {
130 ///Returns a vector multiplied by a scalar
131 xy<T> operator*(const T &u) const {
136 ///Returns a vector divided by a scalar
137 xy<T> operator/(const T &u) const {
143 bool operator==(const xy<T> &u) const {
144 return (x==u.x) && (y==u.y);
147 ///Testing inequality
148 bool operator!=(xy u) const {
149 return (x!=u.x) || (y!=u.y);
154 ///Returns a vector multiplied by a scalar
156 ///Returns a vector multiplied by a scalar
158 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
162 ///Read a plainvector from a stream
164 ///Read a plainvector from a stream
168 inline std::istream& operator>>(std::istream &is, xy<T> &z) {
171 if (c != '(') is.putback(c);
175 if (!(is >> z.x)) return is;
177 if (c != ',') is.putback(c);
181 if (!(is >> z.y)) return is;
183 if (c != ')') is.putback(c);
190 ///Write a plainvector to a stream
192 ///Write a plainvector to a stream
196 inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
198 os << "(" << z.x << ", " << z.y << ")";
202 ///Rotate by 90 degrees
204 ///Returns its parameter rotated by 90 degrees in positive direction.
208 inline xy<T> rot90(const xy<T> &z)
210 return xy<T>(-z.y,z.x);
213 ///Rotate by 270 degrees
215 ///Returns its parameter rotated by 90 degrees in negative direction.
219 inline xy<T> rot270(const xy<T> &z)
221 return xy<T>(z.y,-z.x);
226 /// A class to calculate or store the bounding box of plainvectors.
228 /// A class to calculate or store the bounding box of plainvectors.
230 ///\author Attila Bernath
233 xy<T> bottom_left, top_right;
237 ///Default constructor: creates an empty bounding box
238 BoundingBox() { _empty = true; }
240 ///Constructing the instance from one point
241 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
243 ///Were any points added?
248 ///Makes the BoundingBox empty
253 ///\brief Gives back the bottom left corner
254 ///(if the bounding box is empty, then the return value is not defined)
255 xy<T> bottomLeft() const {
259 ///\brief Sets the bottom left corner
260 ///(should only bee used for non-empty box)
261 void bottomLeft(xy<T> p) {
265 ///\brief Gives back the top right corner
266 ///(if the bounding box is empty, then the return value is not defined)
267 xy<T> topRight() const {
271 ///\brief Sets the top right corner
272 ///(should only bee used for non-empty box)
273 void topRight(xy<T> p) {
277 ///\brief Gives back the bottom right corner
278 ///(if the bounding box is empty, then the return value is not defined)
279 xy<T> bottomRight() const {
280 return xy<T>(top_right.x,bottom_left.y);
283 ///\brief Sets the bottom right corner
284 ///(should only bee used for non-empty box)
285 void bottomRight(xy<T> p) {
290 ///\brief Gives back the top left corner
291 ///(if the bounding box is empty, then the return value is not defined)
292 xy<T> topLeft() const {
293 return xy<T>(bottom_left.x,top_right.y);
296 ///\brief Sets the top left corner
297 ///(should only bee used for non-empty box)
298 void topLeft(xy<T> p) {
303 ///\brief Gives back the bottom of the box
304 ///(if the bounding box is empty, then the return value is not defined)
306 return bottom_left.y;
309 ///\brief Sets the bottom of the box
310 ///(should only bee used for non-empty box)
315 ///\brief Gives back the top of the box
316 ///(if the bounding box is empty, then the return value is not defined)
321 ///\brief Sets the top of the box
322 ///(should only bee used for non-empty box)
327 ///\brief Gives back the left side of the box
328 ///(if the bounding box is empty, then the return value is not defined)
330 return bottom_left.x;
333 ///\brief Sets the left side of the box
334 ///(should only bee used for non-empty box)
339 ///\brief Gives back the right side of the box
340 ///(if the bounding box is empty, then the return value is not defined)
345 ///\brief Sets the right side of the box
346 ///(should only bee used for non-empty box)
351 ///\brief Gives back the height of the box
352 ///(if the bounding box is empty, then the return value is not defined)
354 return top_right.y-bottom_left.y;
357 ///\brief Gives back the width of the box
358 ///(if the bounding box is empty, then the return value is not defined)
360 return top_right.x-bottom_left.x;
363 ///Checks whether a point is inside a bounding box
364 bool inside(const xy<T>& u){
368 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
369 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
373 ///Increments a bounding box with a point
374 BoundingBox& add(const xy<T>& u){
376 bottom_left=top_right=u;
380 if (bottom_left.x > u.x) bottom_left.x = u.x;
381 if (bottom_left.y > u.y) bottom_left.y = u.y;
382 if (top_right.x < u.x) top_right.x = u.x;
383 if (top_right.y < u.y) top_right.y = u.y;
388 // ///Sums a bounding box and a point
389 // BoundingBox operator +(const xy<T>& u){
390 // BoundingBox b = *this;
394 ///Increments a bounding box with an other bounding box
395 BoundingBox& add(const BoundingBox &u){
397 this->add(u.bottomLeft());
398 this->add(u.topRight());
403 ///Sums two bounding boxes
404 BoundingBox operator +(const BoundingBox& u){
405 BoundingBox b = *this;
410 ///Intersection of two bounding boxes
411 BoundingBox operator &(const BoundingBox& u){
413 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
414 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
415 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
416 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
417 b._empty = this->_empty || u._empty ||
418 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
422 };//class Boundingbox
425 ///Map of x-coordinates of an xy<>-map
435 typedef typename M::Value::Value Value;
436 typedef typename M::Key Key;
438 XMap(M& map) : _map(map) {}
439 Value operator[](Key k) const {return _map[k].x;}
440 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
443 ///Returns an \ref XMap class
445 ///This function just returns an \ref XMap class.
450 inline XMap<M> xMap(M &m)
456 inline XMap<M> xMap(const M &m)
461 ///Constant (read only) version of \ref XMap
471 typedef typename M::Value::Value Value;
472 typedef typename M::Key Key;
474 ConstXMap(const M &map) : _map(map) {}
475 Value operator[](Key k) const {return _map[k].x;}
478 ///Returns a \ref ConstXMap class
480 ///This function just returns an \ref ConstXMap class.
483 ///\relates ConstXMap
485 inline ConstXMap<M> xMap(const M &m)
487 return ConstXMap<M>(m);
490 ///Map of y-coordinates of an xy<>-map
500 typedef typename M::Value::Value Value;
501 typedef typename M::Key Key;
503 YMap(M& map) : _map(map) {}
504 Value operator[](Key k) const {return _map[k].y;}
505 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
508 ///Returns an \ref YMap class
510 ///This function just returns an \ref YMap class.
515 inline YMap<M> yMap(M &m)
521 inline YMap<M> yMap(const M &m)
526 ///Constant (read only) version of \ref YMap
536 typedef typename M::Value::Value Value;
537 typedef typename M::Key Key;
539 ConstYMap(const M &map) : _map(map) {}
540 Value operator[](Key k) const {return _map[k].y;}
543 ///Returns a \ref ConstYMap class
545 ///This function just returns an \ref ConstYMap class.
548 ///\relates ConstYMap
550 inline ConstYMap<M> yMap(const M &m)
552 return ConstYMap<M>(m);
556 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
558 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
567 typedef typename M::Value::Value Value;
568 typedef typename M::Key Key;
570 NormSquareMap(const M &map) : _map(map) {}
571 Value operator[](Key k) const {return _map[k].normSquare();}
574 ///Returns a \ref NormSquareMap class
576 ///This function just returns an \ref NormSquareMap class.
579 ///\relates NormSquareMap
581 inline NormSquareMap<M> normSquareMap(const M &m)
583 return NormSquareMap<M>(m);