All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends

interval.h

00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2011, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00038 #ifndef FCL_INTERVAL_H
00039 #define FCL_INTERVAL_H
00040 
00041 #include "fcl/data_types.h"
00042 
00043 namespace fcl
00044 {
00045 
00047 struct Interval
00048 {
00049   FCL_REAL i_[2];
00050 
00051   Interval() { i_[0] = i_[1] = 0; }
00052 
00053   explicit Interval(FCL_REAL v)
00054   {
00055     i_[0] = i_[1] = v;
00056   }
00057 
00059   Interval(FCL_REAL left, FCL_REAL right)
00060   {
00061     i_[0] = left; i_[1] = right;
00062   }
00063 
00065   inline void setValue(FCL_REAL a, FCL_REAL b)
00066   {
00067     i_[0] = a; i_[1] = b;
00068   }
00069 
00071   inline void setValue(FCL_REAL x)
00072   {
00073     i_[0] = i_[1] = x;
00074   }
00075 
00077   inline FCL_REAL operator [] (size_t i) const
00078   {
00079     return i_[i];
00080   }
00081 
00083   inline FCL_REAL& operator [] (size_t i)
00084   {
00085     return i_[i];
00086   }
00087 
00089   inline bool operator == (const Interval& other) const
00090   {
00091     if(i_[0] != other.i_[0]) return false;
00092     if(i_[1] != other.i_[1]) return false;
00093     return true;
00094   }
00095 
00097   inline Interval operator + (const Interval& other) const
00098   {
00099     return Interval(i_[0] + other.i_[0], i_[1] + other.i_[1]);
00100   }
00101 
00103   inline Interval operator - (const Interval& other) const
00104   {
00105     return Interval(i_[0] - other.i_[1], i_[1] - other.i_[0]);
00106   }
00107 
00108   inline Interval& operator += (const Interval& other)
00109   {
00110     i_[0] += other.i_[0];
00111     i_[1] += other.i_[1];
00112     return *this;
00113   }
00114 
00115   inline Interval& operator -= (const Interval& other)
00116   {
00117     i_[0] -= other.i_[1];
00118     i_[1] -= other.i_[0];
00119     return *this;
00120   }
00121 
00122   Interval operator * (const Interval& other) const;
00123 
00124   Interval& operator *= (const Interval& other);
00125 
00126   inline Interval operator * (FCL_REAL d) const
00127   {
00128     if(d >= 0) return Interval(i_[0] * d, i_[1] * d);
00129     return Interval(i_[1] * d, i_[0] * d);
00130   }
00131 
00132   inline Interval& operator *= (FCL_REAL d)
00133   {
00134     if(d >= 0)
00135     {
00136       i_[0] *= d;
00137       i_[1] *= d;
00138     }
00139     else
00140     {
00141       FCL_REAL tmp = i_[0];
00142       i_[0] = i_[1] * d;
00143       i_[1] = tmp * d;
00144     }
00145 
00146     return *this;
00147   }
00148 
00150   Interval operator / (const Interval& other) const;
00151 
00152   Interval& operator /= (const Interval& other);
00153 
00155   inline bool overlap(const Interval& other) const
00156   {
00157     if(i_[1] < other.i_[0]) return false;
00158     if(i_[0] > other.i_[1]) return false;
00159     return true;
00160   }
00161 
00162   inline bool intersect(const Interval& other)
00163   {
00164     if(i_[1] < other.i_[0]) return false;
00165     if(i_[0] > other.i_[1]) return false;
00166     if(i_[1] > other.i_[1]) i_[1] = other.i_[1];
00167     if(i_[0] < other.i_[0]) i_[0] = other.i_[0];
00168     return true;
00169   }
00170 
00171   inline Interval operator - () const
00172   {
00173     return Interval(-i_[1], -i_[0]);
00174   }
00175 
00177   inline FCL_REAL getAbsLower() const
00178   {
00179     if(i_[0] >= 0) return i_[0];
00180     if(i_[1] >= 0) return 0;
00181     return -i_[1];
00182   }
00183 
00185   inline FCL_REAL getAbsUpper() const
00186   {
00187     if(i_[0] + i_[1] >= 0) return i_[1];
00188     return i_[0];
00189   }
00190 
00191 
00192   inline bool contains(FCL_REAL v) const
00193   {
00194     if(v < i_[0]) return false;
00195     if(v > i_[1]) return false;
00196     return true;
00197   }
00198 
00200   inline Interval& bound(FCL_REAL v)
00201   {
00202     if(v < i_[0]) i_[0] = v;
00203     if(v > i_[1]) i_[1] = v;
00204     return *this;
00205   }
00206 
00207 
00209   inline Interval& bound(const Interval& other)
00210   {
00211     if(other.i_[0] < i_[0]) i_[0] = other.i_[0];
00212     if(other.i_[1] > i_[1]) i_[1] = other.i_[1];
00213     return *this;
00214   }
00215 
00216 
00217   void print() const;
00218   inline FCL_REAL center() const { return 0.5 * (i_[0] + i_[1]); }
00219   inline FCL_REAL diameter() const { return i_[1] -i_[0]; }
00220 };
00221 
00222 Interval bound(const Interval& i, FCL_REAL v);
00223 
00224 Interval bound(const Interval& i, const Interval& other);
00225 
00226 }
00227 #endif