span.h

Go to the documentation of this file.
00001 
00004 
00005 //
00006 // Copyright (c) 2004-2007 University of Southern California
00007 // Rob Peters <rjpeters at usc dot edu>
00008 //
00009 // created: Mon Oct 25 12:49:03 2004
00010 // commit: $Id: span.h 10065 2007-04-12 05:54:56Z rjpeters $
00011 // $HeadURL: file:///lab/rjpeters/svnrepo/code/trunk/groovx/src/geom/span.h $
00012 //
00013 // --------------------------------------------------------------------
00014 //
00015 // This file is part of GroovX.
00016 //   [http://ilab.usc.edu/rjpeters/groovx/]
00017 //
00018 // GroovX is free software; you can redistribute it and/or modify it
00019 // under the terms of the GNU General Public License as published by
00020 // the Free Software Foundation; either version 2 of the License, or
00021 // (at your option) any later version.
00022 //
00023 // GroovX is distributed in the hope that it will be useful, but
00024 // WITHOUT ANY WARRANTY; without even the implied warranty of
00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 // General Public License for more details.
00027 //
00028 // You should have received a copy of the GNU General Public License
00029 // along with GroovX; if not, write to the Free Software Foundation,
00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00031 //
00033 
00034 #ifndef GROOVX_GEOM_SPAN_H_UTC20050626084023_DEFINED
00035 #define GROOVX_GEOM_SPAN_H_UTC20050626084023_DEFINED
00036 
00037 #include "rutz/algo.h"
00038 #include "rutz/error.h"
00039 
00040 #include "rutz/debug.h"
00041 
00042 namespace geom
00043 {
00044   template <class V>
00045   class span
00046   {
00047   private:
00048 
00049   public:
00050     span()         : lo(),  hi()  {} // init to zero
00051     span(V l, V h) : lo(l), hi(h)
00052     {
00053       if (lo > hi)
00054         throw rutz::error("invalid span (lo > hi)", SRC_POS);
00055     }
00056 
00058     template <class U>
00059     explicit span(const span<U>& other) : lo(other.lo), hi(other.hi) {}
00060 
00061     static span<V> from_any(V v1, V v2)
00062     {
00063       return span<V>(rutz::min(v1, v2), rutz::max(v1, v2));
00064     }
00065 
00066     span<V>& operator=(const span<V>& i)
00067     {
00068       const_cast<V&>(lo) = i.lo;
00069       const_cast<V&>(hi) = i.hi;
00070       return *this;
00071     }
00072 
00073     template <class U>
00074     bool contains(U val) const
00075     { return val >= lo && val <= hi; }
00076 
00077     V center() const
00078     { return (lo+hi)/V(2); }
00079 
00080     V width() const
00081     { return (hi-lo); }
00082 
00083     span<V> incr_width(V w) const
00084     {
00085       const V half = V(0.5*w);
00086       return span<V>(lo-half,
00087                      hi+(w-half)); // in case V(0.5*h) != 0.5*h (e.g. int math)
00088     }
00089 
00090     span<V> with_width(V w) const
00091     {
00092       return incr_width(w - width());
00093     }
00094 
00095     span<V> scaled_by(V factor) const
00096     {
00097       return with_width(width() * factor);
00098     }
00099 
00100     span<V> shifted_by(V shift) const
00101     {
00102       return span<V>(lo+shift, hi+shift);
00103     }
00104 
00105     bool is_void() const
00106     {
00107       GVX_ASSERT(!(lo > hi));
00108       return (lo == hi);
00109     }
00110 
00111     span<V> union_with(const span<V>& other) const
00112     {
00113       if (other.is_void()) return *this;
00114       if (this->is_void()) return other;
00115       return span<V>(rutz::min(this->lo, other.lo),
00116                      rutz::max(this->hi, other.hi));
00117     }
00118 
00119     span<V> including(V val) const
00120     {
00121       if      (val < lo) return span<V>(val, hi);
00122       else if (val > hi) return span<V>(lo, val);
00123 
00124       /* else */         return *this;
00125     }
00126 
00127     const V lo;
00128     const V hi;
00129   };
00130 }
00131 
00132 static const char __attribute__((used)) vcid_groovx_geom_span_h_utc20050626084023[] = "$Id: span.h 10065 2007-04-12 05:54:56Z rjpeters $ $HeadURL: file:
00133 #endif // !GROOVX_GEOM_SPAN_H_UTC20050626084023_DEFINED

The software described here is Copyright (c) 1998-2005, Rob Peters.
This page was generated Wed Dec 3 06:49:38 2008 by Doxygen version 1.5.5.