last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrInterval.h 00044 * Defines the template type cvr::interval<T> and all shortcuts like 00045 * cvr::iinterval. 00046 * \author Pablo Alvarado 00047 * \date 01.11.2002 00048 * 00049 * $Id: cvrInterval.h,v 1.2 2007/10/07 04:21:40 alvarado Exp $ 00050 */ 00051 00052 #ifndef _CVR_INTERVAL_H_ 00053 #define _CVR_INTERVAL_H_ 00054 00055 #include "cvrAssert.h" 00056 #include "cvrIoHandler.h" 00057 #include "cvrMath.h" 00058 #include "cvrRound.h" 00059 00060 #include <iostream> 00061 00062 namespace cvr { 00063 00064 /** 00065 * A one dimensional interval, giving the from and to values. 00066 * 00067 * The template type T will be the one used for each limit value. For 00068 * example interval<float> uses float for \a from and \a to. 00069 * 00070 * This data structure simplifies the manipulation of one 00071 * dimensional intervals providing simple interfaces for adding, 00072 * substracting, distance (L2), and more. 00073 * 00074 * There are some alias to shorten the notation: 00075 * - cvr::iinterval is equivalent to cvr::interval<int> 00076 * - cvr::finterval is equivalent to cvr::interval<sreal> 00077 * - cvr::dinterval is equivalent to cvr::interval<dreal> 00078 * 00079 * An inteval with a \c from value greater than the \c to value is 00080 * considered as invalid. So, methods which return such 00081 * configuration can be interpreted as empty intervals. 00082 * 00083 * @ingroup gGeomData 00084 */ 00085 template <typename T> 00086 class interval { 00087 public: 00088 /** 00089 * Used for the template-based interface for pixels as vectors. 00090 */ 00091 typedef T value_type; 00092 00093 /** 00094 * Return type of the size() member 00095 */ 00096 typedef int size_type; 00097 00098 /** 00099 * Anonymous union to allow the fastes access to the elements as 00100 * semantic tokes (x and y) and as array as well 00101 */ 00102 union { 00103 __extension__ struct { 00104 /** 00105 * From value 00106 */ 00107 value_type from; 00108 00109 /** 00110 * To value 00111 */ 00112 value_type to; 00113 }; 00114 00115 /** 00116 * Array that shares the same memory with from and to 00117 */ 00118 value_type data[2]; 00119 }; 00120 00121 /** 00122 * Default constructor 00123 */ 00124 explicit interval(const T newx=T(),const T newy=T()); 00125 00126 /** 00127 * Copy constructor 00128 */ 00129 template <typename U> 00130 interval(const interval<U>& p); 00131 00132 /** 00133 * Copy constructor 00134 */ 00135 template <typename U> 00136 interval<T>& castFrom(const interval<U>& p); 00137 00138 /** 00139 * Set the coordinate values and return a reference to this interval 00140 */ 00141 inline interval<T>& set(const T tx,const T ty); 00142 00143 /** 00144 * Get the limit values 00145 */ 00146 inline void get(T& tx,T& ty) const; 00147 00148 /** 00149 * Scale an interval, multiplying each limit by the given scalar. 00150 * 00151 * The type U has to be compatible with the type T in the sense that 00152 * they can be multiplied and casted to T. This applies to most build 00153 * in type like int, float, etc. 00154 */ 00155 template<typename U> 00156 inline interval<T>& multiply(const U c); 00157 00158 /** 00159 * Scale an interval, multiplying each limit by the given scalar. 00160 * 00161 * The type U has to be compatible with the type T in the sense that 00162 * they can be multiplied and casted to T. This applies to most build 00163 * in type like int, float, etc. 00164 */ 00165 template<typename U> 00166 inline interval<T> operator*(const U c) const; 00167 00168 /** 00169 * Multiply the other interval interval<T> with a given scale factor 00170 * 00171 * The type U has to be compatible with the type T in the sense that 00172 * they can be multiplied and casted to T. This applies to most build 00173 * in type like int, float, etc. 00174 */ 00175 template<typename U> 00176 inline interval<T>& multiply(const interval<T>& other,const U c); 00177 00178 /** 00179 * Multiply interval<T> with a given factor 00180 * 00181 * The type U has to be compatible with the type T in the sense that 00182 * they can be multiplied and casted to T. This applies to most build 00183 * in type like int, float, etc. 00184 */ 00185 template<typename U> 00186 inline interval<T>& operator*=(const U c); 00187 00188 /** 00189 * Multiplies elementwise the components of this and the interval c, and 00190 * leave the result here. 00191 */ 00192 inline interval<T>& emultiply(const interval<T>& c); 00193 00194 /** 00195 * Multiplies elementwise the components of \a a and \a b and leave the 00196 * result here. 00197 */ 00198 inline interval<T>& emultiply(const interval<T>& a,const interval<T>& b); 00199 00200 /** 00201 * Divide each component of interval<T> with a given factor 00202 */ 00203 template<typename U> 00204 inline interval<T>& divide(const U c); 00205 00206 /** 00207 * Divide each component of other other interval<T> with a given factor 00208 */ 00209 template<typename U> 00210 inline interval<T>& divide(const interval<T>& other,const U c); 00211 00212 /** 00213 * Divide each component of interval<T> by a given factor 00214 */ 00215 template <typename U> 00216 inline interval<T> operator/(const U c) const; 00217 00218 /** 00219 * Divide each component of interval<T> by a given factor 00220 */ 00221 template <typename U> 00222 inline interval<T>& operator/=(const U c); 00223 00224 /** 00225 * Elementwise division of each component of the intervals 00226 */ 00227 inline interval<T>& edivide(const interval<T>& c); 00228 00229 /** 00230 * Elementwise division of each component of the intervals 00231 */ 00232 inline interval<T>& edivide(const interval<T>& a,const interval<T>& b); 00233 00234 /** 00235 * Modulo c of the integer part of each component of the interval 00236 */ 00237 inline interval<T> operator%(const int c) const; 00238 00239 /** 00240 * Find the smallest interval which contains all points of both, 00241 * this interval and the given one. 00242 * 00243 * Since the intervals may not overlap, the result 00244 * 00245 * @param p the other interval to be added to this one 00246 * @return a reference to this interval 00247 */ 00248 inline interval<T>& join(const interval<T>& p); 00249 00250 /** 00251 * Add the two other intervals and leave the result here 00252 * @param a first interval to be added 00253 * @param b second interval to be added 00254 * @return a reference to this interval, which will contain a+b 00255 */ 00256 inline interval<T>& join(const interval<T>& a, 00257 const interval<T>& b); 00258 00259 /** 00260 * Set subtraction of this and the given interval. 00261 * 00262 * Here, the subtraction means the smallest interval which 00263 * contains all the points of the set-subtraction of this interval 00264 * minus all the elements of the given one. 00265 */ 00266 inline interval<T>& subtract(const interval<T>& p); 00267 00268 00269 /** 00270 * Subtract the two other intervals and leave the result here 00271 * 00272 * Here, the subtraction means the smallest interval which 00273 * contains all the points of the set-subtraction of this interval 00274 * minus all the elements of the given one. 00275 * 00276 * @param a first interval 00277 * @param b interval to be subtracted from the first one 00278 * @return a reference to this interval, which will contain a-b 00279 */ 00280 inline interval<T>& subtract(const interval<T>& a, 00281 const interval<T>& b); 00282 00283 /** 00284 * Operator - 00285 */ 00286 inline interval<T> operator-(const interval<T>& p) const; 00287 00288 /** 00289 * Operator -= 00290 */ 00291 inline interval<T>& operator-=(const interval<T>& p); 00292 00293 /** 00294 * Copy operator 00295 */ 00296 inline interval<T>& copy(const interval<T>& p); 00297 00298 /** 00299 * Operator = 00300 */ 00301 inline interval<T>& operator=(const interval<T>& p); 00302 00303 /** 00304 * Operator == 00305 */ 00306 inline bool operator==(const interval<T>& p) const; 00307 00308 /** 00309 * Operator != 00310 */ 00311 inline bool operator!=(const interval<T>& p) const; 00312 00313 /** 00314 * Length of the interval 00315 * 00316 * For this method, the intervals are considered closed. This is 00317 * specially important for integer types, which will return 00318 * from-to+1, while for floating-point types it just return from-to. 00319 * 00320 */ 00321 inline T length() const; 00322 00323 /** 00324 * Check if element is contained in the interval 00325 * 00326 * Return true if the given value is contained within the 00327 * container. This method assumes that the intervall represents a closed 00328 * interval, so that the boundaries are considered as part of it. 00329 */ 00330 inline bool contains(const T val) const; 00331 00332 /** 00333 * Check if the given interval is fully contained in the interval 00334 * 00335 * Return true if the given value is contained within the 00336 * container. This method assumes that the intervall represents a closed 00337 * interval, so that the boundaries are considered as part of it. 00338 */ 00339 inline bool contains(const interval<T>& val) const; 00340 00341 /** 00342 * Return the closest value of type T which lies on the interval. 00343 * 00344 * This method returns the same value of the argument iff that 00345 * value lies within the interval, \a from if the given value is 00346 * less than it, or \a to if it is greater. 00347 */ 00348 inline T closest(const T val) const; 00349 00350 /** 00351 * @name Access as vector 00352 */ 00353 //@{ 00354 /** 00355 * Used to simulate vector access. It allows the use of interval 00356 * in templates expecting a vector-like structure. 00357 * 00358 * The correspondence between the elements of the vector and 00359 * the components will be [0] for x and [1] for y 00360 */ 00361 inline T& operator[](const int i); 00362 00363 /** 00364 * Used to simulate read-only vector access. It allows the use of interval 00365 * in templates expecting a vector-like structure. 00366 * 00367 * The correspondence between the elements of the vector and 00368 * the components will be [0] for x and [1] for y 00369 */ 00370 inline const T& operator[](const int i) const; 00371 00372 /** 00373 * Used to simulate the vector size. 00374 */ 00375 inline size_type size() const; 00376 //@} 00377 00378 }; 00379 00380 /** 00381 * Read the vector from the given ioHandler. The complete flag indicates 00382 * if the enclosing begin and end should be also be readed 00383 * 00384 * @ingroup gStorable 00385 */ 00386 template <typename T> 00387 bool read(ioHandler& handler,interval<T>& p,const bool complete=true); 00388 00389 /** 00390 * Write the vector in the given ioHandler. The complete flag indicates 00391 * if the enclosing begin and end should be also be written or not 00392 * 00393 * @ingroup gStorable 00394 */ 00395 template<typename T> 00396 bool write(ioHandler& handler,const interval<T>& p,const bool complete=true); 00397 00398 /** 00399 * A interval with integer coordinates 00400 */ 00401 typedef interval<int> iinterval; 00402 00403 /** 00404 * A interval with unsigned integer coordinates 00405 */ 00406 typedef interval<unsigned int> uiinterval; 00407 00408 /** 00409 * A interval with double coordinates 00410 */ 00411 typedef interval<double> dinterval; 00412 00413 /** 00414 * A interval with float coordinates 00415 */ 00416 typedef interval<float> finterval; 00417 00418 } 00419 00420 namespace std { 00421 00422 //inline ostream& operator<<(ostream& s,const cvr::interval& p); 00423 template <typename T> 00424 inline ostream& operator<<(ostream& s,const cvr::interval<T>& p); 00425 00426 //inline ostream& operator>>(istream& s,const cvr::interval& p); 00427 template <typename T> 00428 inline istream& operator>>(istream& s,cvr::interval<T>& p); 00429 00430 } 00431 00432 #include "cvrInterval_template.h" 00433 00434 #endif 00435 00436 00437