[Liblas-commits] hg: move filters into ReaderI and out of the liblas::Reader impl...

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Dec 6 17:28:33 EST 2010


changeset 2a87e6c492ac in /Volumes/Data/www/liblas.org/hg
details: http://hg.liblas.orghg?cmd=changeset;node=2a87e6c492ac
summary: move filters into ReaderI and out of the liblas::Reader implementation

diffstat:

 include/liblas/detail/reader/point.hpp  |    3 +-
 include/liblas/detail/reader/reader.hpp |    8 ++
 include/liblas/lasreader.hpp            |   10 +-
 include/liblas/liblas.hpp               |    3 +
 src/detail/reader/point.cpp             |   18 +----
 src/detail/reader/reader.cpp            |  108 ++++++++++++++++++++++++++---
 src/lasreader.cpp                       |  114 +++++++++++--------------------
 7 files changed, 157 insertions(+), 107 deletions(-)

diffs (truncated from 466 to 300 lines):

diff -r 19a9c7034517 -r 2a87e6c492ac include/liblas/detail/reader/point.hpp
--- a/include/liblas/detail/reader/point.hpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/include/liblas/detail/reader/point.hpp	Mon Dec 06 16:28:23 2010 -0600
@@ -62,8 +62,7 @@
     Point(std::istream& ifs, HeaderPtr header);
     virtual ~Point();
 
-    std::istream& GetStream() const;
-    const liblas::Point& GetPoint() const { return m_point; }
+    liblas::Point& GetPoint() { return m_point; }
     void read();
     
 protected:
diff -r 19a9c7034517 -r 2a87e6c492ac include/liblas/detail/reader/reader.hpp
--- a/include/liblas/detail/reader/reader.hpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/include/liblas/detail/reader/reader.hpp	Mon Dec 06 16:28:23 2010 -0600
@@ -71,8 +71,14 @@
     
     void Reset(HeaderPtr header);
 
+    void SetFilters(std::vector<liblas::FilterPtr> const& filters);
+    void SetTransforms(std::vector<liblas::TransformPtr> const& transforms);
+
+
 protected:
     void CreateTransform();
+    bool FilterPoint(liblas::Point const& p);
+    void TransformPoint(liblas::Point& p);
 
     typedef std::istream::off_type off_type;
     typedef std::istream::pos_type pos_type;
@@ -84,6 +90,8 @@
     PointReaderPtr m_point_reader;
     HeaderReaderPtr m_header_reader;
 
+    std::vector<liblas::FilterPtr> m_filters;
+    std::vector<liblas::TransformPtr> m_transforms;
 private:
 
     // Blocked copying operations, declared but not defined.
diff -r 19a9c7034517 -r 2a87e6c492ac include/liblas/lasreader.hpp
--- a/include/liblas/lasreader.hpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/include/liblas/lasreader.hpp	Mon Dec 06 16:28:23 2010 -0600
@@ -125,7 +125,7 @@
     /// effect for reading data at specific locations in the file.  
     /// They only affect reading ReadNextPoint-style operations
     /// Filters are applied *before* transforms.
-    void SetFilters(std::vector<liblas::FilterPtr> const& filters) {m_filters = filters;}
+    void SetFilters(std::vector<liblas::FilterPtr> const& filters);
 
     /// Sets transforms to apply to points.  Points are transformed in 
     /// place *in the order* of the transform list.
@@ -135,7 +135,7 @@
     /// special case.  You can define your own reprojection transforms and add 
     /// it to the list, but be sure to not issue a SetOutputSRS to trigger 
     /// the internal transform creation
-    void SetTransforms(std::vector<liblas::TransformPtr> const& transforms) {m_transforms = transforms;}
+    void SetTransforms(std::vector<liblas::TransformPtr> const& transforms);
 
 
 private:
@@ -145,7 +145,7 @@
     Reader& operator=(Reader const& rhs);
 
     void Init(); // throws on error
-    bool KeepPoint(liblas::Point const& p);
+    // bool KeepPoint(liblas::Point const& p);
 
     const std::auto_ptr<ReaderI> m_pimpl;
 
@@ -158,8 +158,8 @@
     // read from the istream
     bool bCustomHeader;
     
-    std::vector<liblas::FilterPtr> m_filters;
-    std::vector<liblas::TransformPtr> m_transforms;
+    // std::vector<liblas::FilterPtr> m_filters;
+    // std::vector<liblas::TransformPtr> m_transforms;
 
 };
 
diff -r 19a9c7034517 -r 2a87e6c492ac include/liblas/liblas.hpp
--- a/include/liblas/liblas.hpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/include/liblas/liblas.hpp	Mon Dec 06 16:28:23 2010 -0600
@@ -128,6 +128,9 @@
     virtual void Seek(std::size_t n, HeaderPtr header) = 0;
     
     virtual void Reset(HeaderPtr header) = 0;
+    
+    virtual void SetFilters(std::vector<liblas::FilterPtr> const& filters) = 0;
+    virtual void SetTransforms(std::vector<liblas::TransformPtr> const& transforms) = 0;
 
     virtual ~ReaderI() {}
 };
diff -r 19a9c7034517 -r 2a87e6c492ac src/detail/reader/point.cpp
--- a/src/detail/reader/point.cpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/src/detail/reader/point.cpp	Mon Dec 06 16:28:23 2010 -0600
@@ -67,27 +67,15 @@
 {
 }
 
-std::istream& Point::GetStream() const
-{
-    return m_ifs;
-}
-
 void Point::read()
 {
     assert(m_header);
     assert(m_point.GetHeaderPtr());
     assert(m_raw_data.size() > 0);
+    
+    m_point.SetHeaderPtr(m_header);
+    detail::read_n(m_raw_data.front(), m_ifs, m_raw_data.size());
 
-    // try
-    // {
-        detail::read_n(m_raw_data.front(), m_ifs, m_raw_data.size());
-    // }
-    // catch (std::out_of_range const& e) // we reached the end of the file
-    // {
-    //     // FIXME: Why do we kill the error? It cause setting m_point with invali data, doesn't it? --mloskot
-    //     std::cerr << e.what() << std::endl;
-    // }    
-    
     m_point.SetData(m_raw_data);
 }
 
diff -r 19a9c7034517 -r 2a87e6c492ac src/detail/reader/reader.cpp
--- a/src/detail/reader/reader.cpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/src/detail/reader/reader.cpp	Mon Dec 06 16:28:23 2010 -0600
@@ -89,8 +89,48 @@
     } 
 }
 
+void ReaderImpl::TransformPoint(liblas::Point& p)
+{    
+
+    // Apply the transforms to each point
+    std::vector<liblas::TransformPtr>::const_iterator ti;
+
+    for (ti = m_transforms.begin(); ti != m_transforms.end(); ++ti)
+    {
+        liblas::TransformPtr transform = *ti;
+        transform->transform(p);
+    }            
+}
+
+
+bool ReaderImpl::FilterPoint(liblas::Point const& p)
+{    
+    // If there's no filters on this reader, we keep 
+    // the point no matter what.
+    if (m_filters.empty() ) {
+        return true;
+    }
+
+    std::vector<liblas::FilterPtr>::const_iterator fi;
+    for (fi = m_filters.begin(); fi != m_filters.end(); ++fi)
+    {
+        liblas::FilterPtr filter = *fi;
+        if (!filter->filter(p))
+        {
+            return false;
+        }
+    }
+    return true;
+}
+
+
+    
 HeaderPtr ReaderImpl::ReadHeader()
 {
+    // If we're eof, we need to reset the state
+    if (m_ifs.eof())
+        m_ifs.clear();
+    
     m_header_reader->read();
     HeaderPtr h = m_header_reader->GetHeader();
     
@@ -105,25 +145,53 @@
     {
         m_ifs.clear();
         m_ifs.seekg(header->GetDataOffset(), std::ios::beg);
-        
-        m_point_reader->read();
-        ++m_current;
-        return m_point_reader->GetPoint();        
-
     }
 
-    if (m_current < m_size)
+    if (m_current >= m_size ){
+        throw std::out_of_range("ReadNextPoint: file has no more points to read, end of file reached");
+    } 
+
+    m_point_reader->read();
+    ++m_current;
+    liblas::Point& p = m_point_reader->GetPoint();
+
+    // Filter the points and continue reading until we either find 
+    // one to keep or throw an exception.
+
+    bool bLastPoint = false;
+    if (!FilterPoint(p))
     {
         m_point_reader->read();
         ++m_current;
-        return m_point_reader->GetPoint();
+        p = m_point_reader->GetPoint();
 
-    } else if (m_current == m_size ){
-        throw std::out_of_range("file has no more points to read, end of file reached");
-    } else {
-        throw std::runtime_error("ReadNextPoint: m_current > m_size, something has gone extremely awry");
+        while (!FilterPoint(p))
+        {
+            m_point_reader->read();
+            ++m_current;
+            p = m_point_reader->GetPoint();
+            if (m_current == m_size) 
+            {
+                bLastPoint = true;
+                break;
+            }
+        }
     }
 
+    if (!bLastPoint)
+    {
+        if (!m_transforms.empty())
+        {
+            TransformPoint(p);
+        }
+        return p;
+    }
+    else
+        throw std::out_of_range("ReadNextPoint: file has no more points to read, end of file reached");
+
+
+
+
 }
 
 liblas::Point const& ReaderImpl::ReadPointAt(std::size_t n, HeaderPtr header)
@@ -142,8 +210,12 @@
     m_ifs.seekg(pos, std::ios::beg);
 
     m_point_reader->read();
-    const liblas::Point& point = m_point_reader->GetPoint();
-    
+    liblas::Point& point = m_point_reader->GetPoint();
+
+    if (!m_transforms.empty())
+    {
+        TransformPoint(point);
+    }
     return point;
 }
 
@@ -165,6 +237,16 @@
     m_current = n;
 }
 
+void ReaderImpl::SetFilters(std::vector<liblas::FilterPtr> const& filters)
+{
+    m_filters = filters;
+}
+
+void ReaderImpl::SetTransforms(std::vector<liblas::TransformPtr> const& transforms)
+{
+    m_transforms = transforms;
+}
+    
 // ReaderImpl* ReaderFactory::Create(std::istream& ifs)
 // {
 //     if (!ifs)
diff -r 19a9c7034517 -r 2a87e6c492ac src/lasreader.cpp
--- a/src/lasreader.cpp	Mon Dec 06 14:49:12 2010 -0600
+++ b/src/lasreader.cpp	Mon Dec 06 16:28:23 2010 -0600
@@ -63,35 +63,29 @@
 {
 
 Reader::Reader(std::istream& ifs) :
-    m_pimpl(new detail::CachedReaderImpl(ifs,3)),
+    m_pimpl(new detail::ReaderImpl(ifs)),
     m_point(0),
     m_empty_point(new Point()),
-    bCustomHeader(false),
-    m_filters(0),
-    m_transforms(0)
+    bCustomHeader(false)
 {
     Init();
 }
 
 Reader::Reader(std::istream& ifs, uint32_t cache_size) :
-    m_pimpl(new detail::CachedReaderImpl(ifs, cache_size)),
+    m_pimpl(new detail::ReaderImpl(ifs)),
     m_point(0),


More information about the Liblas-commits mailing list