1.1 --- a/lemon/xy.h Wed Sep 06 11:39:22 2006 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,654 +0,0 @@
1.4 -/* -*- C++ -*-
1.5 - *
1.6 - * This file is a part of LEMON, a generic C++ optimization library
1.7 - *
1.8 - * Copyright (C) 2003-2006
1.9 - * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.10 - * (Egervary Research Group on Combinatorial Optimization, EGRES).
1.11 - *
1.12 - * Permission to use, modify and distribute this software is granted
1.13 - * provided that this copyright notice appears in all copies. For
1.14 - * precise terms see the accompanying LICENSE file.
1.15 - *
1.16 - * This software is provided "AS IS" with no warranty of any kind,
1.17 - * express or implied, and with no claim as to its suitability for any
1.18 - * purpose.
1.19 - *
1.20 - */
1.21 -
1.22 -#ifndef LEMON_XY_H
1.23 -#define LEMON_XY_H
1.24 -
1.25 -#include <iostream>
1.26 -#include <lemon/bits/utility.h>
1.27 -
1.28 -///\ingroup misc
1.29 -///\file
1.30 -///\brief A simple two dimensional vector and a bounding box implementation
1.31 -///
1.32 -/// The class \ref lemon::xy "xy" implements
1.33 -///a two dimensional vector with the usual
1.34 -/// operations.
1.35 -///
1.36 -/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
1.37 -/// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
1.38 -///
1.39 -///\author Attila Bernath
1.40 -
1.41 -
1.42 -namespace lemon {
1.43 -
1.44 - /// \addtogroup misc
1.45 - /// @{
1.46 -
1.47 - /// A simple two dimensional vector (plainvector) implementation
1.48 -
1.49 - /// A simple two dimensional vector (plainvector) implementation
1.50 - ///with the usual vector
1.51 - /// operators.
1.52 - ///
1.53 - ///\note As you might have noticed, this class does not follow the
1.54 - ///\ref naming_conv "LEMON Coding Style" (it should be called \c Xy
1.55 - ///according to it). There is a stupid Hungarian proverb, "A kivétel
1.56 - ///erõsíti a szabályt" ("An exception
1.57 - ///reinforces a rule", which is
1.58 - ///actually a mistranslation of the Latin proverb "Exceptio probat regulam").
1.59 - ///This class is an example for that.
1.60 - ///\author Attila Bernath
1.61 - template<typename T>
1.62 - class xy {
1.63 -
1.64 - public:
1.65 -
1.66 - typedef T Value;
1.67 -
1.68 - ///First co-ordinate
1.69 - T x;
1.70 - ///Second co-ordinate
1.71 - T y;
1.72 -
1.73 - ///Default constructor
1.74 - xy() {}
1.75 -
1.76 - ///Construct an instance from coordinates
1.77 - xy(T a, T b) : x(a), y(b) { }
1.78 -
1.79 -
1.80 - ///Conversion constructor
1.81 - template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
1.82 -
1.83 - ///Give back the square of the norm of the vector
1.84 - T normSquare() const {
1.85 - return x*x+y*y;
1.86 - }
1.87 -
1.88 - ///Increment the left hand side by u
1.89 - xy<T>& operator +=(const xy<T>& u) {
1.90 - x += u.x;
1.91 - y += u.y;
1.92 - return *this;
1.93 - }
1.94 -
1.95 - ///Decrement the left hand side by u
1.96 - xy<T>& operator -=(const xy<T>& u) {
1.97 - x -= u.x;
1.98 - y -= u.y;
1.99 - return *this;
1.100 - }
1.101 -
1.102 - ///Multiply the left hand side with a scalar
1.103 - xy<T>& operator *=(const T &u) {
1.104 - x *= u;
1.105 - y *= u;
1.106 - return *this;
1.107 - }
1.108 -
1.109 - ///Divide the left hand side by a scalar
1.110 - xy<T>& operator /=(const T &u) {
1.111 - x /= u;
1.112 - y /= u;
1.113 - return *this;
1.114 - }
1.115 -
1.116 - ///Return the scalar product of two vectors
1.117 - T operator *(const xy<T>& u) const {
1.118 - return x*u.x+y*u.y;
1.119 - }
1.120 -
1.121 - ///Return the sum of two vectors
1.122 - xy<T> operator+(const xy<T> &u) const {
1.123 - xy<T> b=*this;
1.124 - return b+=u;
1.125 - }
1.126 -
1.127 - ///Return the neg of the vectors
1.128 - xy<T> operator-() const {
1.129 - xy<T> b=*this;
1.130 - b.x=-b.x; b.y=-b.y;
1.131 - return b;
1.132 - }
1.133 -
1.134 - ///Return the difference of two vectors
1.135 - xy<T> operator-(const xy<T> &u) const {
1.136 - xy<T> b=*this;
1.137 - return b-=u;
1.138 - }
1.139 -
1.140 - ///Return a vector multiplied by a scalar
1.141 - xy<T> operator*(const T &u) const {
1.142 - xy<T> b=*this;
1.143 - return b*=u;
1.144 - }
1.145 -
1.146 - ///Return a vector divided by a scalar
1.147 - xy<T> operator/(const T &u) const {
1.148 - xy<T> b=*this;
1.149 - return b/=u;
1.150 - }
1.151 -
1.152 - ///Test equality
1.153 - bool operator==(const xy<T> &u) const {
1.154 - return (x==u.x) && (y==u.y);
1.155 - }
1.156 -
1.157 - ///Test inequality
1.158 - bool operator!=(xy u) const {
1.159 - return (x!=u.x) || (y!=u.y);
1.160 - }
1.161 -
1.162 - };
1.163 -
1.164 - ///Return an xy
1.165 -
1.166 - ///Return an xy
1.167 - ///\relates xy
1.168 - template <typename T>
1.169 - inline xy<T> make_xy(const T& x, const T& y) {
1.170 - return xy<T>(x, y);
1.171 - }
1.172 -
1.173 - ///Return a vector multiplied by a scalar
1.174 -
1.175 - ///Return a vector multiplied by a scalar
1.176 - ///\relates xy
1.177 - template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
1.178 - return x*u;
1.179 - }
1.180 -
1.181 - ///Read a plainvector from a stream
1.182 -
1.183 - ///Read a plainvector from a stream
1.184 - ///\relates xy
1.185 - ///
1.186 - template<typename T>
1.187 - inline std::istream& operator>>(std::istream &is, xy<T> &z) {
1.188 - char c;
1.189 - if (is >> c) {
1.190 - if (c != '(') is.putback(c);
1.191 - } else {
1.192 - is.clear();
1.193 - }
1.194 - if (!(is >> z.x)) return is;
1.195 - if (is >> c) {
1.196 - if (c != ',') is.putback(c);
1.197 - } else {
1.198 - is.clear();
1.199 - }
1.200 - if (!(is >> z.y)) return is;
1.201 - if (is >> c) {
1.202 - if (c != ')') is.putback(c);
1.203 - } else {
1.204 - is.clear();
1.205 - }
1.206 - return is;
1.207 - }
1.208 -
1.209 - ///Write a plainvector to a stream
1.210 -
1.211 - ///Write a plainvector to a stream
1.212 - ///\relates xy
1.213 - ///
1.214 - template<typename T>
1.215 - inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
1.216 - {
1.217 - os << "(" << z.x << ", " << z.y << ")";
1.218 - return os;
1.219 - }
1.220 -
1.221 - ///Rotate by 90 degrees
1.222 -
1.223 - ///Returns its parameter rotated by 90 degrees in positive direction.
1.224 - ///\relates xy
1.225 - ///
1.226 - template<typename T>
1.227 - inline xy<T> rot90(const xy<T> &z)
1.228 - {
1.229 - return xy<T>(-z.y,z.x);
1.230 - }
1.231 -
1.232 - ///Rotate by 180 degrees
1.233 -
1.234 - ///Returns its parameter rotated by 180 degrees.
1.235 - ///\relates xy
1.236 - ///
1.237 - template<typename T>
1.238 - inline xy<T> rot180(const xy<T> &z)
1.239 - {
1.240 - return xy<T>(-z.x,-z.y);
1.241 - }
1.242 -
1.243 - ///Rotate by 270 degrees
1.244 -
1.245 - ///Returns its parameter rotated by 90 degrees in negative direction.
1.246 - ///\relates xy
1.247 - ///
1.248 - template<typename T>
1.249 - inline xy<T> rot270(const xy<T> &z)
1.250 - {
1.251 - return xy<T>(z.y,-z.x);
1.252 - }
1.253 -
1.254 -
1.255 -
1.256 - /// A class to calculate or store the bounding box of plainvectors.
1.257 -
1.258 - /// A class to calculate or store the bounding box of plainvectors.
1.259 - ///
1.260 - ///\author Attila Bernath
1.261 - template<typename T>
1.262 - class BoundingBox {
1.263 - xy<T> bottom_left, top_right;
1.264 - bool _empty;
1.265 - public:
1.266 -
1.267 - ///Default constructor: creates an empty bounding box
1.268 - BoundingBox() { _empty = true; }
1.269 -
1.270 - ///Construct an instance from one point
1.271 - BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
1.272 -
1.273 - ///Were any points added?
1.274 - bool empty() const {
1.275 - return _empty;
1.276 - }
1.277 -
1.278 - ///Make the BoundingBox empty
1.279 - void clear() {
1.280 - _empty=1;
1.281 - }
1.282 -
1.283 - ///Give back the bottom left corner
1.284 -
1.285 - ///Give back the bottom left corner.
1.286 - ///If the bounding box is empty, then the return value is not defined.
1.287 - xy<T> bottomLeft() const {
1.288 - return bottom_left;
1.289 - }
1.290 -
1.291 - ///Set the bottom left corner
1.292 -
1.293 - ///Set the bottom left corner.
1.294 - ///It should only bee used for non-empty box.
1.295 - void bottomLeft(xy<T> p) {
1.296 - bottom_left = p;
1.297 - }
1.298 -
1.299 - ///Give back the top right corner
1.300 -
1.301 - ///Give back the top right corner.
1.302 - ///If the bounding box is empty, then the return value is not defined.
1.303 - xy<T> topRight() const {
1.304 - return top_right;
1.305 - }
1.306 -
1.307 - ///Set the top right corner
1.308 -
1.309 - ///Set the top right corner.
1.310 - ///It should only bee used for non-empty box.
1.311 - void topRight(xy<T> p) {
1.312 - top_right = p;
1.313 - }
1.314 -
1.315 - ///Give back the bottom right corner
1.316 -
1.317 - ///Give back the bottom right corner.
1.318 - ///If the bounding box is empty, then the return value is not defined.
1.319 - xy<T> bottomRight() const {
1.320 - return xy<T>(top_right.x,bottom_left.y);
1.321 - }
1.322 -
1.323 - ///Set the bottom right corner
1.324 -
1.325 - ///Set the bottom right corner.
1.326 - ///It should only bee used for non-empty box.
1.327 - void bottomRight(xy<T> p) {
1.328 - top_right.x = p.x;
1.329 - bottom_left.y = p.y;
1.330 - }
1.331 -
1.332 - ///Give back the top left corner
1.333 -
1.334 - ///Give back the top left corner.
1.335 - ///If the bounding box is empty, then the return value is not defined.
1.336 - xy<T> topLeft() const {
1.337 - return xy<T>(bottom_left.x,top_right.y);
1.338 - }
1.339 -
1.340 - ///Set the top left corner
1.341 -
1.342 - ///Set the top left corner.
1.343 - ///It should only bee used for non-empty box.
1.344 - void topLeft(xy<T> p) {
1.345 - top_right.y = p.y;
1.346 - bottom_left.x = p.x;
1.347 - }
1.348 -
1.349 - ///Give back the bottom of the box
1.350 -
1.351 - ///Give back the bottom of the box.
1.352 - ///If the bounding box is empty, then the return value is not defined.
1.353 - T bottom() const {
1.354 - return bottom_left.y;
1.355 - }
1.356 -
1.357 - ///Set the bottom of the box
1.358 -
1.359 - ///Set the bottom of the box.
1.360 - ///It should only bee used for non-empty box.
1.361 - void bottom(T t) {
1.362 - bottom_left.y = t;
1.363 - }
1.364 -
1.365 - ///Give back the top of the box
1.366 -
1.367 - ///Give back the top of the box.
1.368 - ///If the bounding box is empty, then the return value is not defined.
1.369 - T top() const {
1.370 - return top_right.y;
1.371 - }
1.372 -
1.373 - ///Set the top of the box
1.374 -
1.375 - ///Set the top of the box.
1.376 - ///It should only bee used for non-empty box.
1.377 - void top(T t) {
1.378 - top_right.y = t;
1.379 - }
1.380 -
1.381 - ///Give back the left side of the box
1.382 -
1.383 - ///Give back the left side of the box.
1.384 - ///If the bounding box is empty, then the return value is not defined.
1.385 - T left() const {
1.386 - return bottom_left.x;
1.387 - }
1.388 -
1.389 - ///Set the left side of the box
1.390 -
1.391 - ///Set the left side of the box.
1.392 - ///It should only bee used for non-empty box
1.393 - void left(T t) {
1.394 - bottom_left.x = t;
1.395 - }
1.396 -
1.397 - /// Give back the right side of the box
1.398 -
1.399 - /// Give back the right side of the box.
1.400 - ///If the bounding box is empty, then the return value is not defined.
1.401 - T right() const {
1.402 - return top_right.x;
1.403 - }
1.404 -
1.405 - ///Set the right side of the box
1.406 -
1.407 - ///Set the right side of the box.
1.408 - ///It should only bee used for non-empty box
1.409 - void right(T t) {
1.410 - top_right.x = t;
1.411 - }
1.412 -
1.413 - ///Give back the height of the box
1.414 -
1.415 - ///Give back the height of the box.
1.416 - ///If the bounding box is empty, then the return value is not defined.
1.417 - T height() const {
1.418 - return top_right.y-bottom_left.y;
1.419 - }
1.420 -
1.421 - ///Give back the width of the box
1.422 -
1.423 - ///Give back the width of the box.
1.424 - ///If the bounding box is empty, then the return value is not defined.
1.425 - T width() const {
1.426 - return top_right.x-bottom_left.x;
1.427 - }
1.428 -
1.429 - ///Checks whether a point is inside a bounding box
1.430 - bool inside(const xy<T>& u){
1.431 - if (_empty)
1.432 - return false;
1.433 - else{
1.434 - return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
1.435 - (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
1.436 - }
1.437 - }
1.438 -
1.439 - ///Increments a bounding box with a point
1.440 - BoundingBox& add(const xy<T>& u){
1.441 - if (_empty){
1.442 - bottom_left=top_right=u;
1.443 - _empty = false;
1.444 - }
1.445 - else{
1.446 - if (bottom_left.x > u.x) bottom_left.x = u.x;
1.447 - if (bottom_left.y > u.y) bottom_left.y = u.y;
1.448 - if (top_right.x < u.x) top_right.x = u.x;
1.449 - if (top_right.y < u.y) top_right.y = u.y;
1.450 - }
1.451 - return *this;
1.452 - }
1.453 -
1.454 -// ///Sums a bounding box and a point
1.455 -// BoundingBox operator +(const xy<T>& u){
1.456 -// BoundingBox b = *this;
1.457 -// return b += u;
1.458 -// }
1.459 -
1.460 - ///Increments a bounding box with another bounding box
1.461 - BoundingBox& add(const BoundingBox &u){
1.462 - if ( !u.empty() ){
1.463 - this->add(u.bottomLeft());
1.464 - this->add(u.topRight());
1.465 - }
1.466 - return *this;
1.467 - }
1.468 -
1.469 - ///Sums two bounding boxes
1.470 - BoundingBox operator +(const BoundingBox& u){
1.471 - BoundingBox b = *this;
1.472 - return b.add(u);
1.473 - }
1.474 -
1.475 -
1.476 - ///Intersection of two bounding boxes
1.477 - BoundingBox operator &(const BoundingBox& u){
1.478 - BoundingBox b;
1.479 - b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
1.480 - b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
1.481 - b.top_right.x=std::min(this->top_right.x,u.top_right.x);
1.482 - b.top_right.y=std::min(this->top_right.y,u.top_right.y);
1.483 - b._empty = this->_empty || u._empty ||
1.484 - b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
1.485 - return b;
1.486 - }
1.487 -
1.488 - };//class Boundingbox
1.489 -
1.490 -
1.491 - ///Map of x-coordinates of an xy<>-map
1.492 -
1.493 - ///\ingroup maps
1.494 - ///
1.495 - template<class M>
1.496 - class XMap
1.497 - {
1.498 - M& _map;
1.499 - public:
1.500 -
1.501 - typedef typename M::Value::Value Value;
1.502 - typedef typename M::Key Key;
1.503 - ///\e
1.504 - XMap(M& map) : _map(map) {}
1.505 - Value operator[](Key k) const {return _map[k].x;}
1.506 - void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
1.507 - };
1.508 -
1.509 - ///Returns an \ref XMap class
1.510 -
1.511 - ///This function just returns an \ref XMap class.
1.512 - ///
1.513 - ///\ingroup maps
1.514 - ///\relates XMap
1.515 - template<class M>
1.516 - inline XMap<M> xMap(M &m)
1.517 - {
1.518 - return XMap<M>(m);
1.519 - }
1.520 -
1.521 - template<class M>
1.522 - inline XMap<M> xMap(const M &m)
1.523 - {
1.524 - return XMap<M>(m);
1.525 - }
1.526 -
1.527 - ///Constant (read only) version of \ref XMap
1.528 -
1.529 - ///\ingroup maps
1.530 - ///
1.531 - template<class M>
1.532 - class ConstXMap
1.533 - {
1.534 - const M& _map;
1.535 - public:
1.536 -
1.537 - typedef typename M::Value::Value Value;
1.538 - typedef typename M::Key Key;
1.539 - ///\e
1.540 - ConstXMap(const M &map) : _map(map) {}
1.541 - Value operator[](Key k) const {return _map[k].x;}
1.542 - };
1.543 -
1.544 - ///Returns a \ref ConstXMap class
1.545 -
1.546 - ///This function just returns an \ref ConstXMap class.
1.547 - ///
1.548 - ///\ingroup maps
1.549 - ///\relates ConstXMap
1.550 - template<class M>
1.551 - inline ConstXMap<M> xMap(const M &m)
1.552 - {
1.553 - return ConstXMap<M>(m);
1.554 - }
1.555 -
1.556 - ///Map of y-coordinates of an xy<>-map
1.557 -
1.558 - ///\ingroup maps
1.559 - ///
1.560 - template<class M>
1.561 - class YMap
1.562 - {
1.563 - M& _map;
1.564 - public:
1.565 -
1.566 - typedef typename M::Value::Value Value;
1.567 - typedef typename M::Key Key;
1.568 - ///\e
1.569 - YMap(M& map) : _map(map) {}
1.570 - Value operator[](Key k) const {return _map[k].y;}
1.571 - void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
1.572 - };
1.573 -
1.574 - ///Returns an \ref YMap class
1.575 -
1.576 - ///This function just returns an \ref YMap class.
1.577 - ///
1.578 - ///\ingroup maps
1.579 - ///\relates YMap
1.580 - template<class M>
1.581 - inline YMap<M> yMap(M &m)
1.582 - {
1.583 - return YMap<M>(m);
1.584 - }
1.585 -
1.586 - template<class M>
1.587 - inline YMap<M> yMap(const M &m)
1.588 - {
1.589 - return YMap<M>(m);
1.590 - }
1.591 -
1.592 - ///Constant (read only) version of \ref YMap
1.593 -
1.594 - ///\ingroup maps
1.595 - ///
1.596 - template<class M>
1.597 - class ConstYMap
1.598 - {
1.599 - const M& _map;
1.600 - public:
1.601 -
1.602 - typedef typename M::Value::Value Value;
1.603 - typedef typename M::Key Key;
1.604 - ///\e
1.605 - ConstYMap(const M &map) : _map(map) {}
1.606 - Value operator[](Key k) const {return _map[k].y;}
1.607 - };
1.608 -
1.609 - ///Returns a \ref ConstYMap class
1.610 -
1.611 - ///This function just returns an \ref ConstYMap class.
1.612 - ///
1.613 - ///\ingroup maps
1.614 - ///\relates ConstYMap
1.615 - template<class M>
1.616 - inline ConstYMap<M> yMap(const M &m)
1.617 - {
1.618 - return ConstYMap<M>(m);
1.619 - }
1.620 -
1.621 -
1.622 - ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
1.623 -
1.624 - ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
1.625 - ///\ingroup maps
1.626 - ///
1.627 - template<class M>
1.628 - class NormSquareMap
1.629 - {
1.630 - const M& _map;
1.631 - public:
1.632 -
1.633 - typedef typename M::Value::Value Value;
1.634 - typedef typename M::Key Key;
1.635 - ///\e
1.636 - NormSquareMap(const M &map) : _map(map) {}
1.637 - Value operator[](Key k) const {return _map[k].normSquare();}
1.638 - };
1.639 -
1.640 - ///Returns a \ref NormSquareMap class
1.641 -
1.642 - ///This function just returns an \ref NormSquareMap class.
1.643 - ///
1.644 - ///\ingroup maps
1.645 - ///\relates NormSquareMap
1.646 - template<class M>
1.647 - inline NormSquareMap<M> normSquareMap(const M &m)
1.648 - {
1.649 - return NormSquareMap<M>(m);
1.650 - }
1.651 -
1.652 - /// @}
1.653 -
1.654 -
1.655 -} //namespace lemon
1.656 -
1.657 -#endif //LEMON_XY_H