[Liblas-commits] hg: rework bounds to use new liblas::Range<T> and add a bunch mo...

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Sep 9 11:57:27 EDT 2010


changeset cc8916dc511c in /Volumes/Data/www/liblas.org/hg
details: http://hg.liblas.orghg?cmd=changeset;node=cc8916dc511c
summary: rework bounds to use new liblas::Range<T> and add a bunch more functionality to it as a result

diffstat:

 include/liblas/lasbounds.hpp |  410 +++++++++++++++++++++++++++++++++---------
 test/unit/CMakeLists.txt     |    1 +
 test/unit/lasbounds_test.cpp |   90 +++++++++
 3 files changed, 407 insertions(+), 94 deletions(-)

diffs (truncated from 639 to 300 lines):

diff -r acd7cbc2f615 -r cc8916dc511c include/liblas/lasbounds.hpp
--- a/include/liblas/lasbounds.hpp	Wed Sep 08 14:51:48 2010 -0500
+++ b/include/liblas/lasbounds.hpp	Thu Sep 09 10:57:18 2010 -0500
@@ -61,30 +61,137 @@
 
 namespace liblas {
 
+template <typename T>
+class Range
+{
+public:
+    T min;
+    T max;
+    
+    Range(T mmin=std::numeric_limits<T>::max(), T mmax=std::numeric_limits<T>::min())
+        : min(mmin), max(mmax) {};
+    
+
+    Range(Range const& other)
+        : min(other.min)
+        , max(other.max)
+    {
+    }
+
+    Range& operator=(Range<T> const& rhs)
+    {
+        if (&rhs != this)
+        {
+            min = rhs.min;
+            max = rhs.max;
+        }
+        return *this;
+    }
+    
+    bool operator==(Range<T> const& rhs) const
+    {
+        return equal(rhs);
+    }
+    
+    bool operator!=(Range const& rhs) const
+    {
+        return !(equal(rhs));
+    }
+    
+    bool equal(Range const& other) const
+    {
+        
+        if    (!(detail::compare_distance(min, other.min))  
+            || !(detail::compare_distance(max, other.max))) 
+        {
+            return false;
+        }
+    
+        return true;
+    }
+    
+    bool overlaps(Range const& r) const 
+    {
+        return min <= r.max && max >= r.min;
+    }
+
+    bool contains(Range const& r) const
+    {
+        return min <= r.min && r.max <= max;
+    }
+    
+    bool contains(T v) const
+    {
+        return min <= v && v <= max;
+    }
+    
+    bool empty(void) const 
+    {
+        return min==std::numeric_limits<T>::max() && max==std::numeric_limits<T>::min();
+    }
+    
+    void shift(T v) 
+    {
+        min += v;
+        max += v;
+    }
+    
+    void scale(T v) 
+    {
+        min *= v;
+        max *= v;
+    }
+    
+    void clip(Range const& r)
+    {
+        if (r.min > min)
+            min = r.min;
+        if (r.max < max)
+            max = r.max;
+    }
+    
+    void grow(T v) 
+    {
+        if (v < min) 
+            min = v;
+        if (v > max)
+            max = v;
+    }
+    T length() const
+    {
+        return max - min;
+    }
+};
     
 template <typename T>
 class Bounds
 {
 public:
 
-    typedef typename std::vector<T> Vector;
-    typedef typename std::vector<T>::size_type size_type;
-        
+
+    typedef typename std::vector< Range<T> >::size_type size_type;
+    
+    typedef typename std::vector< Range<T> > RangeVec;
 private:
-    Vector mins;
-    Vector maxs;
 
+    RangeVec ranges;
+    
 public:
 
 Bounds<T>()
 {
-    mins.resize(0);
-    maxs.resize(0);
+    ranges.resize(0);
 }
 
 Bounds(Bounds const& other)
-    : mins(other.mins)
-    , maxs(other.maxs)
+    : 
+    ranges(other.ranges)
+{
+}
+
+Bounds(RangeVec const& rngs)
+    : 
+    ranges(rngs)
 {
 }
 
@@ -95,15 +202,15 @@
         T maxy, 
         T maxz)
 {
-    mins.resize(3);
-    maxs.resize(3);
+    ranges.resize(3);
     
-    mins[0] = minx;
-    mins[1] = miny;
-    mins[2] = minz;
-    maxs[0] = maxx;
-    maxs[1] = maxy;
-    maxs[2] = maxz;
+    ranges[0].min = minx;
+    ranges[1].min = miny;
+    ranges[2].min = minz;
+
+    ranges[0].max = maxx;
+    ranges[1].max = maxy;
+    ranges[2].max = maxz;
     
 #ifdef DEBUG
     verify();
@@ -116,12 +223,14 @@
         T maxx, 
         T maxy)
 {
-    mins.resize(2);
-    maxs.resize(2);
-    mins[0] = minx;
-    mins[1] = miny;
-    maxs[0] = maxx;
-    maxs[1] = maxy;
+
+    ranges.resize(2);
+
+    ranges[0].min = minx;
+    ranges[1].min = miny;
+
+    ranges[0].max = maxx;
+    ranges[1].max = maxy;
     
 #ifdef DEBUG
     verify();
@@ -131,15 +240,15 @@
 
 Bounds( const Point& min, const Point& max)
 {
-    mins.resize(3);
-    maxs.resize(3);
+    ranges.resize(3);
     
-    mins[0] = min.GetX();
-    mins[1] = min.GetY();
-    mins[2] = min.GetZ();
-    maxs[0] = max.GetX();
-    maxs[1] = max.GetY();
-    maxs[2] = max.GetZ();
+    ranges[0].min = min.GetX();
+    ranges[1].min = min.GetY();
+    ranges[2].min = min.GetZ();
+
+    ranges[0].max = max.GetX();
+    ranges[1].max = max.GetY();
+    ranges[2].max = max.GetZ();
     
 #ifdef DEBUG
     verify();
@@ -148,77 +257,102 @@
 }
 
 
-Bounds( Vector const& low, Vector const& high)
-{
-    if (low.size() != high.size() ) {
-        std::ostringstream msg; 
-        msg << "Bounds dimensions are not equal.  Low bounds dimensions are " << low.size()
-            << " and the high bounds are " << high.size();
-        throw std::runtime_error(msg.str());                
-    }
-    mins.resize(low.size());
-    
-    mins = low;
-    maxs = high;
-    
-#ifdef DEBUG
-    verify();
-#endif
-
-}
+// Bounds( Vector const& low, Vector const& high)
+// {
+//     if (low.size() != high.size() ) {
+//         std::ostringstream msg; 
+//         msg << "Bounds dimensions are not equal.  Low bounds dimensions are " << low.size()
+//             << " and the high bounds are " << high.size();
+//         throw std::runtime_error(msg.str());                
+//     }
+//     mins.resize(low.size());
+//     
+//     mins = low;
+//     maxs = high;
+//     
+// #ifdef DEBUG
+//     verify();
+// #endif
+// 
+// }
 
 T min(std::size_t const& index) const
 {
-    if (mins.size() <= index) {
+    if (ranges.size() <= index) {
+        // std::ostringstream msg; 
+        // msg << "Bounds dimensions, " << ranges.size() <<", is less "
+        //     << "than the given index, " << index;
+        // throw std::runtime_error(msg.str());                
         return 0;
     }
-    return mins[index];
+    return ranges[index].min;
 }
 
 void min(std::size_t const& index, T v)
 {
-    if (mins.size() <= index) {
-        mins.resize(index + 1);
-        maxs.resize(index + 1);
+    if (ranges.size() <= index) {
+        ranges.resize(index + 1);
     }
-    mins[index] = v;
+    ranges[index].min = v;
 }
 
 T max(std::size_t const& index) const
 {
-    if (maxs.size() <= index) {
-        return 0.0;
+    if (ranges.size() <= index) {
+        // std::ostringstream msg; 
+        // msg << "Bounds dimensions, " << ranges.size() <<", is less "
+        //     << "than the given index, " << index;
+        // throw std::runtime_error(msg.str());    
+        return 0;
     }
-        return maxs[index];
+    return ranges[index].max;
 }


More information about the Liblas-commits mailing list