Added the function isFinite(), and replaced the calls to finite() with it.
This was necessary because finite() is not a standard function. Neither can
we use its standard counterpart isfinite(), because it was introduced only
in C99, and therefore it is not supplied by all C++ implementations.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2007
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) { }
76 ///The dimension of the vector.
78 ///This class give back always 2.
80 int size() const { return 2; }
82 ///Subscripting operator
84 ///\c p[0] is \c p.x and \c p[1] is \c p.y
86 T& operator[](int idx) { return idx == 0 ? x : y; }
88 ///Const subscripting operator
90 ///\c p[0] is \c p.x and \c p[1] is \c p.y
92 const T& operator[](int idx) const { return idx == 0 ? x : y; }
94 ///Conversion constructor
95 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
97 ///Give back the square of the norm of the vector
98 T normSquare() const {
102 ///Increment the left hand side by u
103 Point<T>& operator +=(const Point<T>& u) {
109 ///Decrement the left hand side by u
110 Point<T>& operator -=(const Point<T>& u) {
116 ///Multiply the left hand side with a scalar
117 Point<T>& operator *=(const T &u) {
123 ///Divide the left hand side by a scalar
124 Point<T>& operator /=(const T &u) {
130 ///Return the scalar product of two vectors
131 T operator *(const Point<T>& u) const {
135 ///Return the sum of two vectors
136 Point<T> operator+(const Point<T> &u) const {
141 ///Return the neg of the vectors
142 Point<T> operator-() const {
148 ///Return the difference of two vectors
149 Point<T> operator-(const Point<T> &u) const {
154 ///Return a vector multiplied by a scalar
155 Point<T> operator*(const T &u) const {
160 ///Return a vector divided by a scalar
161 Point<T> operator/(const T &u) const {
167 bool operator==(const Point<T> &u) const {
168 return (x==u.x) && (y==u.y);
172 bool operator!=(Point u) const {
173 return (x!=u.x) || (y!=u.y);
182 template <typename T>
183 inline Point<T> makePoint(const T& x, const T& y) {
184 return Point<T>(x, y);
187 ///Return a vector multiplied by a scalar
189 ///Return a vector multiplied by a scalar
191 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
195 ///Read a plainvector from a stream
197 ///Read a plainvector from a stream
201 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
204 if (c != '(') is.putback(c);
208 if (!(is >> z.x)) return is;
210 if (c != ',') is.putback(c);
214 if (!(is >> z.y)) return is;
216 if (c != ')') is.putback(c);
223 ///Write a plainvector to a stream
225 ///Write a plainvector to a stream
229 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
231 os << "(" << z.x << ", " << z.y << ")";
235 ///Rotate by 90 degrees
237 ///Returns its parameter rotated by 90 degrees in positive direction.
241 inline Point<T> rot90(const Point<T> &z)
243 return Point<T>(-z.y,z.x);
246 ///Rotate by 180 degrees
248 ///Returns its parameter rotated by 180 degrees.
252 inline Point<T> rot180(const Point<T> &z)
254 return Point<T>(-z.x,-z.y);
257 ///Rotate by 270 degrees
259 ///Returns its parameter rotated by 90 degrees in negative direction.
263 inline Point<T> rot270(const Point<T> &z)
265 return Point<T>(z.y,-z.x);
270 /// A class to calculate or store the bounding box of plainvectors.
272 /// A class to calculate or store the bounding box of plainvectors.
274 ///\author Attila Bernath
277 Point<T> bottom_left, top_right;
281 ///Default constructor: creates an empty bounding box
282 BoundingBox() { _empty = true; }
284 ///Construct an instance from one point
285 BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
287 ///Construct an instance from two points
289 ///Construct an instance from two points
290 ///\warning The coordinates of the bottom-left corner must be no more
291 ///than those of the top-right one
292 BoundingBox(Point<T> a,Point<T> b)
299 ///Construct an instance from four numbers
301 ///Construct an instance from four numbers
302 ///\warning The coordinates of the bottom-left corner must be no more
303 ///than those of the top-right one
304 BoundingBox(T l,T b,T r,T t)
306 bottom_left=Point<T>(l,b);
307 top_right=Point<T>(r,t);
311 ///Were any points added?
316 ///Make the BoundingBox empty
321 ///Give back the bottom left corner
323 ///Give back the bottom left corner.
324 ///If the bounding box is empty, then the return value is not defined.
325 Point<T> bottomLeft() const {
329 ///Set the bottom left corner
331 ///Set the bottom left corner.
332 ///It should only bee used for non-empty box.
333 void bottomLeft(Point<T> p) {
337 ///Give back the top right corner
339 ///Give back the top right corner.
340 ///If the bounding box is empty, then the return value is not defined.
341 Point<T> topRight() const {
345 ///Set the top right corner
347 ///Set the top right corner.
348 ///It should only bee used for non-empty box.
349 void topRight(Point<T> p) {
353 ///Give back the bottom right corner
355 ///Give back the bottom right corner.
356 ///If the bounding box is empty, then the return value is not defined.
357 Point<T> bottomRight() const {
358 return Point<T>(top_right.x,bottom_left.y);
361 ///Set the bottom right corner
363 ///Set the bottom right corner.
364 ///It should only bee used for non-empty box.
365 void bottomRight(Point<T> p) {
370 ///Give back the top left corner
372 ///Give back the top left corner.
373 ///If the bounding box is empty, then the return value is not defined.
374 Point<T> topLeft() const {
375 return Point<T>(bottom_left.x,top_right.y);
378 ///Set the top left corner
380 ///Set the top left corner.
381 ///It should only bee used for non-empty box.
382 void topLeft(Point<T> p) {
387 ///Give back the bottom of the box
389 ///Give back the bottom of the box.
390 ///If the bounding box is empty, then the return value is not defined.
392 return bottom_left.y;
395 ///Set the bottom of the box
397 ///Set the bottom of the box.
398 ///It should only bee used for non-empty box.
403 ///Give back the top of the box
405 ///Give back the top of the box.
406 ///If the bounding box is empty, then the return value is not defined.
411 ///Set the top of the box
413 ///Set the top of the box.
414 ///It should only bee used for non-empty box.
419 ///Give back the left side of the box
421 ///Give back the left side of the box.
422 ///If the bounding box is empty, then the return value is not defined.
424 return bottom_left.x;
427 ///Set the left side of the box
429 ///Set the left side of the box.
430 ///It should only bee used for non-empty box
435 /// Give back the right side of the box
437 /// Give back the right side of the box.
438 ///If the bounding box is empty, then the return value is not defined.
443 ///Set the right side of the box
445 ///Set the right side of the box.
446 ///It should only bee used for non-empty box
451 ///Give back the height of the box
453 ///Give back the height of the box.
454 ///If the bounding box is empty, then the return value is not defined.
456 return top_right.y-bottom_left.y;
459 ///Give back the width of the box
461 ///Give back the width of the box.
462 ///If the bounding box is empty, then the return value is not defined.
464 return top_right.x-bottom_left.x;
467 ///Checks whether a point is inside a bounding box
468 bool inside(const Point<T>& u){
472 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
473 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
477 ///Increments a bounding box with a point
478 BoundingBox& add(const Point<T>& u){
480 bottom_left=top_right=u;
484 if (bottom_left.x > u.x) bottom_left.x = u.x;
485 if (bottom_left.y > u.y) bottom_left.y = u.y;
486 if (top_right.x < u.x) top_right.x = u.x;
487 if (top_right.y < u.y) top_right.y = u.y;
492 ///Increments a bounding to contain another bounding box
493 BoundingBox& add(const BoundingBox &u){
495 this->add(u.bottomLeft());
496 this->add(u.topRight());
501 ///Intersection of two bounding boxes
502 BoundingBox operator &(const BoundingBox& u){
504 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
505 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
506 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
507 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
508 b._empty = this->_empty || u._empty ||
509 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
513 };//class Boundingbox
516 ///Map of x-coordinates of a dim2::Point<>-map
519 ///Map of x-coordinates of a dim2::Point<>-map
527 typedef typename M::Value::Value Value;
528 typedef typename M::Key Key;
530 XMap(M& map) : _map(map) {}
531 Value operator[](Key k) const {return _map[k].x;}
532 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
535 ///Returns an \ref XMap class
537 ///This function just returns an \ref XMap class.
542 inline XMap<M> xMap(M &m)
548 inline XMap<M> xMap(const M &m)
553 ///Constant (read only) version of \ref XMap
556 ///Constant (read only) version of \ref XMap
564 typedef typename M::Value::Value Value;
565 typedef typename M::Key Key;
567 ConstXMap(const M &map) : _map(map) {}
568 Value operator[](Key k) const {return _map[k].x;}
571 ///Returns a \ref ConstXMap class
573 ///This function just returns an \ref ConstXMap class.
576 ///\relates ConstXMap
578 inline ConstXMap<M> xMap(const M &m)
580 return ConstXMap<M>(m);
583 ///Map of y-coordinates of a dim2::Point<>-map
586 ///Map of y-coordinates of a dim2::Point<>-map
594 typedef typename M::Value::Value Value;
595 typedef typename M::Key Key;
597 YMap(M& map) : _map(map) {}
598 Value operator[](Key k) const {return _map[k].y;}
599 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
602 ///Returns an \ref YMap class
604 ///This function just returns an \ref YMap class.
609 inline YMap<M> yMap(M &m)
615 inline YMap<M> yMap(const M &m)
620 ///Constant (read only) version of \ref YMap
623 ///Constant (read only) version of \ref YMap
631 typedef typename M::Value::Value Value;
632 typedef typename M::Key Key;
634 ConstYMap(const M &map) : _map(map) {}
635 Value operator[](Key k) const {return _map[k].y;}
638 ///Returns a \ref ConstYMap class
640 ///This function just returns an \ref ConstYMap class.
643 ///\relates ConstYMap
645 inline ConstYMap<M> yMap(const M &m)
647 return ConstYMap<M>(m);
651 ///\brief Map of the \ref Point::normSquare() "normSquare()"
652 ///of an \ref Point "Point"-map
654 ///Map of the \ref Point::normSquare() "normSquare()"
655 ///of an \ref Point "Point"-map
664 typedef typename M::Value::Value Value;
665 typedef typename M::Key Key;
667 NormSquareMap(const M &map) : _map(map) {}
668 Value operator[](Key k) const {return _map[k].normSquare();}
671 ///Returns a \ref NormSquareMap class
673 ///This function just returns an \ref NormSquareMap class.
676 ///\relates NormSquareMap
678 inline NormSquareMap<M> normSquareMap(const M &m)
680 return NormSquareMap<M>(m);
689 #endif //LEMON_DIM2_H