1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
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
26 ///\brief A simple two dimensional vector and a bounding box implementation
30 ///Tools for handling two dimensional coordinates
32 ///This namespace is a storage of several
33 ///tools for handling two dimensional coordinates
36 /// \addtogroup geomdat
39 /// Two dimensional vector (plain vector)
41 /// A simple two dimensional vector (plain vector) implementation
42 /// with the usual vector operations.
55 ///Default constructor
58 ///Construct an instance from coordinates
59 Point(T a, T b) : x(a), y(b) { }
61 ///Returns the dimension of the vector (i.e. returns 2).
63 ///The dimension of the vector.
64 ///This function always returns 2.
65 int size() const { return 2; }
67 ///Subscripting operator
69 ///\c p[0] is \c p.x and \c p[1] is \c p.y
71 T& operator[](int idx) { return idx == 0 ? x : y; }
73 ///Const subscripting operator
75 ///\c p[0] is \c p.x and \c p[1] is \c p.y
77 const T& operator[](int idx) const { return idx == 0 ? x : y; }
79 ///Conversion constructor
80 template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
82 ///Give back the square of the norm of the vector
83 T normSquare() const {
87 ///Increment the left hand side by \c u
88 Point<T>& operator +=(const Point<T>& u) {
94 ///Decrement the left hand side by \c u
95 Point<T>& operator -=(const Point<T>& u) {
101 ///Multiply the left hand side with a scalar
102 Point<T>& operator *=(const T &u) {
108 ///Divide the left hand side by a scalar
109 Point<T>& operator /=(const T &u) {
115 ///Return the scalar product of two vectors
116 T operator *(const Point<T>& u) const {
120 ///Return the sum of two vectors
121 Point<T> operator+(const Point<T> &u) const {
126 ///Return the negative of the vector
127 Point<T> operator-() const {
133 ///Return the difference of two vectors
134 Point<T> operator-(const Point<T> &u) const {
139 ///Return a vector multiplied by a scalar
140 Point<T> operator*(const T &u) const {
145 ///Return a vector divided by a scalar
146 Point<T> operator/(const T &u) const {
152 bool operator==(const Point<T> &u) const {
153 return (x==u.x) && (y==u.y);
157 bool operator!=(Point u) const {
158 return (x!=u.x) || (y!=u.y);
167 template <typename T>
168 inline Point<T> makePoint(const T& x, const T& y) {
169 return Point<T>(x, y);
172 ///Return a vector multiplied by a scalar
174 ///Return a vector multiplied by a scalar.
176 template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
180 ///Read a plain vector from a stream
182 ///Read a plain vector from a stream.
186 inline std::istream& operator>>(std::istream &is, Point<T> &z) {
189 if (c != '(') is.putback(c);
193 if (!(is >> z.x)) return is;
195 if (c != ',') is.putback(c);
199 if (!(is >> z.y)) return is;
201 if (c != ')') is.putback(c);
208 ///Write a plain vector to a stream
210 ///Write a plain vector to a stream.
214 inline std::ostream& operator<<(std::ostream &os, const Point<T>& z)
216 os << "(" << z.x << "," << z.y << ")";
220 ///Rotate by 90 degrees
222 ///Returns the parameter rotated by 90 degrees in positive direction.
226 inline Point<T> rot90(const Point<T> &z)
228 return Point<T>(-z.y,z.x);
231 ///Rotate by 180 degrees
233 ///Returns the parameter rotated by 180 degrees.
237 inline Point<T> rot180(const Point<T> &z)
239 return Point<T>(-z.x,-z.y);
242 ///Rotate by 270 degrees
244 ///Returns the parameter rotated by 90 degrees in negative direction.
248 inline Point<T> rot270(const Point<T> &z)
250 return Point<T>(z.y,-z.x);
255 /// Bounding box of plain vectors (points).
257 /// A class to calculate or store the bounding box of plain vectors
258 /// (\ref Point "points").
261 Point<T> _bottom_left, _top_right;
265 ///Default constructor: creates an empty box
266 Box() { _empty = true; }
268 ///Construct a box from one point
270 _bottom_left = _top_right = a;
274 ///Construct a box from two points
276 ///Construct a box from two points.
277 ///\param a The bottom left corner.
278 ///\param b The top right corner.
279 ///\warning The coordinates of the bottom left corner must be no more
280 ///than those of the top right one.
281 Box(Point<T> a,Point<T> b)
288 ///Construct a box from four numbers
290 ///Construct a box from four numbers.
291 ///\param l The left side of the box.
292 ///\param b The bottom of the box.
293 ///\param r The right side of the box.
294 ///\param t The top of the box.
295 ///\warning The left side must be no more than the right side and
296 ///bottom must be no more than the top.
299 _bottom_left=Point<T>(l,b);
300 _top_right=Point<T>(r,t);
304 ///Return \c true if the box is empty.
306 ///Return \c true if the box is empty (i.e. return \c false
307 ///if at least one point was added to the box or the coordinates of
308 ///the box were set).
310 ///The coordinates of an empty box are not defined.
315 ///Make the box empty
320 ///Give back the bottom left corner of the box
322 ///Give back the bottom left corner of the box.
323 ///If the box is empty, then the return value is not defined.
324 Point<T> bottomLeft() const {
328 ///Set the bottom left corner of the box
330 ///Set the bottom left corner of the box.
331 ///\pre The box must not be empty.
332 void bottomLeft(Point<T> p) {
336 ///Give back the top right corner of the box
338 ///Give back the top right corner of the box.
339 ///If the box is empty, then the return value is not defined.
340 Point<T> topRight() const {
344 ///Set the top right corner of the box
346 ///Set the top right corner of the box.
347 ///\pre The box must not be empty.
348 void topRight(Point<T> p) {
352 ///Give back the bottom right corner of the box
354 ///Give back the bottom right corner of the box.
355 ///If the box is empty, then the return value is not defined.
356 Point<T> bottomRight() const {
357 return Point<T>(_top_right.x,_bottom_left.y);
360 ///Set the bottom right corner of the box
362 ///Set the bottom right corner of the box.
363 ///\pre The box must not be empty.
364 void bottomRight(Point<T> p) {
366 _bottom_left.y = p.y;
369 ///Give back the top left corner of the box
371 ///Give back the top left corner of the box.
372 ///If the box is empty, then the return value is not defined.
373 Point<T> topLeft() const {
374 return Point<T>(_bottom_left.x,_top_right.y);
377 ///Set the top left corner of the box
379 ///Set the top left corner of the box.
380 ///\pre The box must not be empty.
381 void topLeft(Point<T> p) {
383 _bottom_left.x = p.x;
386 ///Give back the bottom of the box
388 ///Give back the bottom of the box.
389 ///If the box is empty, then the return value is not defined.
391 return _bottom_left.y;
394 ///Set the bottom of the box
396 ///Set the bottom of the box.
397 ///\pre The box must not be empty.
402 ///Give back the top of the box
404 ///Give back the top of the box.
405 ///If the box is empty, then the return value is not defined.
410 ///Set the top of the box
412 ///Set the top of the box.
413 ///\pre The box must not be empty.
418 ///Give back the left side of the box
420 ///Give back the left side of the box.
421 ///If the box is empty, then the return value is not defined.
423 return _bottom_left.x;
426 ///Set the left side of the box
428 ///Set the left side of the box.
429 ///\pre The box must not be empty.
434 /// Give back the right side of the box
436 /// Give back the right side of the box.
437 ///If the box is empty, then the return value is not defined.
442 ///Set the right side of the box
444 ///Set the right side of the box.
445 ///\pre The box must not be empty.
450 ///Give back the height of the box
452 ///Give back the height of the box.
453 ///If the box is empty, then the return value is not defined.
455 return _top_right.y-_bottom_left.y;
458 ///Give back the width of the box
460 ///Give back the width of the box.
461 ///If the box is empty, then the return value is not defined.
463 return _top_right.x-_bottom_left.x;
466 ///Checks whether a point is inside the box
467 bool inside(const Point<T>& u) const {
471 return ( (u.x-_bottom_left.x)*(_top_right.x-u.x) >= 0 &&
472 (u.y-_bottom_left.y)*(_top_right.y-u.y) >= 0 );
476 ///Increments the box with a point
478 ///Increments the box with a point.
480 Box& add(const Point<T>& u){
482 _bottom_left = _top_right = u;
486 if (_bottom_left.x > u.x) _bottom_left.x = u.x;
487 if (_bottom_left.y > u.y) _bottom_left.y = u.y;
488 if (_top_right.x < u.x) _top_right.x = u.x;
489 if (_top_right.y < u.y) _top_right.y = u.y;
494 ///Increments the box to contain another box
496 ///Increments the box to contain another box.
498 Box& add(const Box &u){
506 ///Intersection of two boxes
508 ///Intersection of two boxes.
510 Box operator&(const Box& u) const {
512 if (_empty || u._empty) {
515 b._bottom_left.x = std::max(_bottom_left.x, u._bottom_left.x);
516 b._bottom_left.y = std::max(_bottom_left.y, u._bottom_left.y);
517 b._top_right.x = std::min(_top_right.x, u._top_right.x);
518 b._top_right.y = std::min(_top_right.y, u._top_right.y);
519 b._empty = b._bottom_left.x > b._top_right.x ||
520 b._bottom_left.y > b._top_right.y;
528 ///Read a box from a stream
530 ///Read a box from a stream.
533 inline std::istream& operator>>(std::istream &is, Box<T>& b) {
537 if (c != '(') is.putback(c);
541 if (!(is >> p)) return is;
544 if (c != ',') is.putback(c);
548 if (!(is >> p)) return is;
551 if (c != ')') is.putback(c);
558 ///Write a box to a stream
560 ///Write a box to a stream.
563 inline std::ostream& operator<<(std::ostream &os, const Box<T>& b)
565 os << "(" << b.bottomLeft() << "," << b.topRight() << ")";
569 ///Map of x-coordinates of a <tt>Point</tt>-map
571 ///Map of x-coordinates of a \ref Point "Point"-map.
579 typedef typename M::Value::Value Value;
580 typedef typename M::Key Key;
582 XMap(M& map) : _map(map) {}
583 Value operator[](Key k) const {return _map[k].x;}
584 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
587 ///Returns an XMap class
589 ///This function just returns an XMap class.
592 inline XMap<M> xMap(M &m)
598 inline XMap<M> xMap(const M &m)
603 ///Constant (read only) version of XMap
605 ///Constant (read only) version of XMap.
613 typedef typename M::Value::Value Value;
614 typedef typename M::Key Key;
616 ConstXMap(const M &map) : _map(map) {}
617 Value operator[](Key k) const {return _map[k].x;}
620 ///Returns a ConstXMap class
622 ///This function just returns a ConstXMap class.
623 ///\relates ConstXMap
625 inline ConstXMap<M> xMap(const M &m)
627 return ConstXMap<M>(m);
630 ///Map of y-coordinates of a <tt>Point</tt>-map
632 ///Map of y-coordinates of a \ref Point "Point"-map.
640 typedef typename M::Value::Value Value;
641 typedef typename M::Key Key;
643 YMap(M& map) : _map(map) {}
644 Value operator[](Key k) const {return _map[k].y;}
645 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
648 ///Returns a YMap class
650 ///This function just returns a YMap class.
653 inline YMap<M> yMap(M &m)
659 inline YMap<M> yMap(const M &m)
664 ///Constant (read only) version of YMap
666 ///Constant (read only) version of YMap.
674 typedef typename M::Value::Value Value;
675 typedef typename M::Key Key;
677 ConstYMap(const M &map) : _map(map) {}
678 Value operator[](Key k) const {return _map[k].y;}
681 ///Returns a ConstYMap class
683 ///This function just returns a ConstYMap class.
684 ///\relates ConstYMap
686 inline ConstYMap<M> yMap(const M &m)
688 return ConstYMap<M>(m);
692 ///\brief Map of the normSquare() of a <tt>Point</tt>-map
694 ///Map of the \ref Point::normSquare() "normSquare()"
695 ///of a \ref Point "Point"-map.
702 typedef typename M::Value::Value Value;
703 typedef typename M::Key Key;
705 NormSquareMap(const M &map) : _map(map) {}
706 Value operator[](Key k) const {return _map[k].normSquare();}
709 ///Returns a NormSquareMap class
711 ///This function just returns a NormSquareMap class.
712 ///\relates NormSquareMap
714 inline NormSquareMap<M> normSquareMap(const M &m)
716 return NormSquareMap<M>(m);
725 #endif //LEMON_DIM2_H