[Liblas-commits] r1273 - in trunk: apps include/liblas src

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Jun 8 21:52:13 EDT 2009


Author: hobu
Date: Wed May 20 12:56:45 2009
New Revision: 1273
URL: http://liblas.org/changeset/1273

Log:
more refactoring

Modified:
   trunk/apps/lasindex.cpp
   trunk/include/liblas/lasindex.hpp
   trunk/src/lasindex.cpp

Modified: trunk/apps/lasindex.cpp
==============================================================================
--- trunk/apps/lasindex.cpp	(original)
+++ trunk/apps/lasindex.cpp	Wed May 20 12:56:45 2009
@@ -111,9 +111,10 @@
     LASReader* reader = new LASReader(*istrm);
     
     
-    LASDataStream* idxstrm = new LASDataStream(reader);
+    LASIndexDataStream* idxstrm = new LASIndexDataStream(reader);
 
-    LASIndex* index = new LASIndex(*idxstrm, input);
+    LASIndex* index = new LASIndex( input);
+    index->Initialize(*idxstrm);
     
     delete idxstrm;
     delete index;
@@ -121,6 +122,7 @@
     
     LASIndex* idx = new LASIndex(input);
     
+    idx->Initialize();
     std::vector<liblas::uint32_t>* ids = 0;
 
     try{
@@ -141,32 +143,34 @@
         
     
     
-    std::cout << "Vec length" << ids->size() << std::endl;  
-    delete idx;
-
-    LASDataStream* idxstrm = new LASDataStream(reader);
-
-    LASIndex* index = new LASIndex(*idxstrm, "MEMORY");
-    
-    //doing it in memory
-
-    std::vector<liblas::uint32_t>* ids = 0;
-
-    try{
-//        ids = idx->intersects(289815.12,4320979.06, 289818.01,4320982.59,46.83,170.65);
-
-// _zoom
-//        ids = idx->intersects(630355.0,4834609.0,630395.0,4834641.0,0.0,200.0);
-
-// _clip
-// 630346.830000,4834500.000000,55.260000
-        ids = index->intersects(630262.300000,4834500.000000,630346.830000,4834500.000000,50.900000,55.260000);
-//       ids = idx->intersects(630297.0,4834497.0,630302.0,4834501.0,0.0,200.0);
-
-    } catch (Tools::Exception& e) {
-        std::string s = e.what();
-        std::cout << "error querying index value" << s <<std::endl; exit(1);
-    }
+//     std::cout << "Vec length" << ids->size() << std::endl;  
+//     delete idx;
+// 
+//     LASIndexDataStream* idstrm = new LASIndexDataStream(reader);
+// 
+//     LASIndex* index = new LASIndex("MEMORY");
+//     
+//     in
+//     
+//     //doing it in memory
+// 
+//     std::vector<liblas::uint32_t>* ids = 0;
+// 
+//     try{
+// //        ids = idx->intersects(289815.12,4320979.06, 289818.01,4320982.59,46.83,170.65);
+// 
+// // _zoom
+// //        ids = idx->intersects(630355.0,4834609.0,630395.0,4834641.0,0.0,200.0);
+// 
+// // _clip
+// // 630346.830000,4834500.000000,55.260000
+//         ids = index->intersects(630262.300000,4834500.000000,630346.830000,4834500.000000,50.900000,55.260000);
+// //       ids = idx->intersects(630297.0,4834497.0,630302.0,4834501.0,0.0,200.0);
+// 
+//     } catch (Tools::Exception& e) {
+//         std::string s = e.what();
+//         std::cout << "error querying index value" << s <<std::endl; exit(1);
+//     }
     
 
     // LASPoint* p

Modified: trunk/include/liblas/lasindex.hpp
==============================================================================
--- trunk/include/liblas/lasindex.hpp	(original)
+++ trunk/include/liblas/lasindex.hpp	Wed May 20 12:56:45 2009
@@ -56,7 +56,7 @@
 
 namespace liblas {
 
-class LASDataStream;
+class LASIndexDataStream;
 
 class LASIndex
 {
@@ -70,12 +70,14 @@
     };
     
     LASIndex();
-    LASIndex(LASDataStream& strm, std::string& filename);
     LASIndex(std::string& filename);
     /// Copy constructor.
     LASIndex(LASIndex const& other);
     ~LASIndex();
     
+    void Initialize(LASIndexDataStream& strm);
+    void Initialize();
+    
     /// Assignment operator.
     LASIndex& operator=(LASIndex const& rhs);
     
@@ -83,7 +85,20 @@
     bool operator==(const LASIndex& other) const;
 
     
+    /// Inserts a LASPoint into the index with a given id
+    /// \param p the LASPoint to insert
+    /// \param id the id to associate with the point
     void insert(LASPoint& p, int64_t id);
+    
+    /// Intersects the index with the given cube and returns a 
+    /// std::vector of ids.  Caller has ownership of the vector.
+    /// \param minx minimum X value of the cube
+    /// \param maxx maximum X value of the cube
+    /// \param miny minimum Y value of the cube
+    /// \param maxy maximum Y value of the cube
+    /// \param minz minimum Z value of the cube
+    /// \param maxz maximum Z value of the cube
+    /// \return std::vector<uint32_t> that the caller owns containing the ids that intersect the query.
     std::vector<uint32_t>* intersects(double minx, double miny, double maxx, double maxy, double minz, double maxz);
 
     /// Sets the page size for the index when stored externally
@@ -95,7 +110,7 @@
     uint32_t GetPageSize() { return m_Pagesize; }
     
     /// Sets the index type
-    /// \param v - index type. 
+    /// \param v - index type.  Defaults to eExternalIndex.
     void SetIndexType(IndexType v) { m_idxType = v; }
 
     /// Gets the index type
@@ -117,14 +132,18 @@
     double m_idxFillFactor; 
     bool m_idxExists;
 
+    std::string m_idxFilename;
 
     uint16_t m_bufferCapacity;
     bool m_bufferWriteThrough;
     
-    void Init();
+    bool m_Initialized;
+
+    
+    void Setup();
     SpatialIndex::IStorageManager* CreateStorage(std::string& filename);
     SpatialIndex::StorageManager::IBuffer* CreateIndexBuffer(SpatialIndex::IStorageManager& storage);
-    SpatialIndex::ISpatialIndex* CreateIndex(LASDataStream& strm);
+    SpatialIndex::ISpatialIndex* CreateIndex(LASIndexDataStream& strm);
     SpatialIndex::ISpatialIndex* LoadIndex();
     bool ExternalIndexExists(std::string& filename);
 };
@@ -146,15 +165,11 @@
     void visitData(std::vector<const SpatialIndex::IData*>& v);
 };
 
-class LASDataStream : public SpatialIndex::IDataStream
+class LASIndexDataStream : public SpatialIndex::IDataStream
 {
 public:
-    LASDataStream(LASReader* reader);
-
-    ~LASDataStream()
-    {
-        if (m_pNext != 0) delete m_pNext;
-    };
+    LASIndexDataStream(LASReader* reader);
+    ~LASIndexDataStream();
 
     SpatialIndex::IData* getNext();
     bool hasNext() throw (Tools::NotSupportedException);

Modified: trunk/src/lasindex.cpp
==============================================================================
--- trunk/src/lasindex.cpp	(original)
+++ trunk/src/lasindex.cpp	Wed May 20 12:56:45 2009
@@ -54,8 +54,12 @@
 namespace liblas
 {
 
-void LASIndex::Init()
+void LASIndex::Setup()
 {   
+    // set default values for member variables
+    // actual initialization of the index must happen externally with
+    // Initialize.  This allows the changing of the member variables 
+    // with the setters/getters.
 
     m_Pagesize = 4096;
     m_idxType = eExternalIndex;
@@ -71,15 +75,83 @@
     m_bufferWriteThrough = false;
 
     m_idxExists = false;
+    
+    m_idxFilename = std::string("");
+    m_Initialized = false;
 }
 
 LASIndex::LASIndex()
 {
     std::cout << "Blank Index Constructor called!" << std::endl;
-    Init();
+    Setup();
+}
+
+
+LASIndex::LASIndex(std::string& filename) 
+{
+    Setup();
+    m_idxFilename = filename;
+}
+
+
+
+
+LASIndex::LASIndex(LASIndex const& other) 
+{
+    std::cout << "Index copy called" << std::endl;
+}
+
+LASIndex::~LASIndex() 
+{
+    std::cout << "~LASIndex called" << std::endl;
+    
+    if (m_rtree != 0)
+        delete m_rtree;
+    if (m_buffer != 0)
+        delete m_buffer;
+    if (m_storage != 0)
+        delete m_storage;
+
+}
+
+void LASIndex::Initialize()
+{
+    m_storage = CreateStorage(m_idxFilename);
+    
+    m_buffer = CreateIndexBuffer(*m_storage);
+
+    if (m_idxExists == true) {
+        std::cout << "loading existing index from file " << std::endl;
+        m_rtree = LoadIndex();
+    }
+    else
+    {
+        throw std::runtime_error("can't create index with only a filename, must have LASDatastream");
+    }
+    
+    m_Initialized = true;
 }
 
-SpatialIndex::ISpatialIndex* LASIndex::CreateIndex(LASDataStream& strm) 
+
+
+void LASIndex::Initialize(LASIndexDataStream& strm)
+{
+    m_storage = CreateStorage(m_idxFilename);
+    m_buffer = CreateIndexBuffer(*m_storage);
+
+    if (m_idxExists == true) {
+        std::cout << "loading existing index from LASIndexDataStream " << std::endl;
+        m_rtree = LoadIndex();
+    }
+    else
+    {
+        std::cout << "Creating new index from LASIndexDataStream  ... " << std::endl;
+        m_rtree = CreateIndex(strm);
+    }
+    m_Initialized = true;
+
+}
+SpatialIndex::ISpatialIndex* LASIndex::CreateIndex(LASIndexDataStream& strm) 
 {
     using namespace SpatialIndex;
     
@@ -97,8 +169,8 @@
                                                       m_idxId);
         bool ret = index->isIndexValid();
         if (ret == false) 
-            throw std::runtime_error(   "Spatial index error: index is not"
-                                        " valid after createAndBulkLoadNewRTree");
+            throw std::runtime_error(   "Spatial index error: index is not "
+                                        "valid after createAndBulkLoadNewRTree");
 
         return index;
     } catch (Tools::Exception& e) {
@@ -128,26 +200,6 @@
         throw std::runtime_error(os.str());
     }    
 }
-LASIndex::LASIndex(LASDataStream& strm, std::string& filename)
-{
-    using namespace SpatialIndex;
-    
-    Init();
-    
-    m_storage = CreateStorage(filename);
-    m_buffer = CreateIndexBuffer(*m_storage);
-
-    if (m_idxExists == true) {
-        std::cout << "loading existing index from LASDataStream " << std::endl;
-        m_rtree = LoadIndex();
-    }
-    else
-    {
-        std::cout << "Creating new index from LASDataStream  ... " << std::endl;
-        m_rtree = CreateIndex(strm);
-    }        
-}
-
 
 SpatialIndex::IStorageManager* LASIndex::CreateStorage(std::string& filename)
 {
@@ -233,44 +285,6 @@
     return output;
 }
 
-LASIndex::LASIndex(std::string& filename) 
-{
-
-    Init();
-    
-    m_storage = CreateStorage(filename);
-    
-    m_buffer = CreateIndexBuffer(*m_storage);
-
-    if (m_idxExists == true) {
-        std::cout << "loading existing index from file " << std::endl;
-        m_rtree = LoadIndex();
-    }
-    else
-    {
-        throw std::runtime_error("can't create index with only a filename, must have LASDatastream");
-    }   
-    
-}
-
-LASIndex::LASIndex(LASIndex const& other) 
-{
-    std::cout << "Index copy called" << std::endl;
-
-}
-
-LASIndex::~LASIndex() 
-{
-    std::cout << "~LASIndex called" << std::endl;
-    
-    if (m_rtree != 0)
-        delete m_rtree;
-    if (m_buffer != 0)
-        delete m_buffer;
-    if (m_storage != 0)
-        delete m_storage;
-
-}
 
 
 LASIndex& LASIndex::operator=(LASIndex const& rhs)
@@ -302,11 +316,15 @@
     max[1] = p.GetY(); 
     max[2] = p.GetZ();
     
-    try{
+    if (m_Initialized = false)
+        throw std::runtime_error("Spatial index has not been initialized");
+        
+    try {
         m_rtree->insertData(0, 0, SpatialIndex::Region(min, max, 3), id);
     } catch (Tools::Exception& e) {
-        std::string s = e.what();
-        std::cout << "error inserting index value" << s <<std::endl; exit(1);
+        std::ostringstream os;
+        os << "Spatial Index Error: " << e.what();
+        throw std::runtime_error(os.str());
     }
 }
 
@@ -322,18 +340,26 @@
     max[0] = maxx; 
     max[1] = maxy; 
     max[2] = maxz;
+
+    if (m_Initialized = false)
+        throw std::runtime_error("Spatial index has not been initialized");
     
-    std::cout.setf(std::ios_base::fixed);
-    
-    // std::cout << "minx: " << min[0] << " miny: "<<min[1] << " maxx: " <<max[0] << " maxy: " << max[1] << " minz: " << min[2] << " maxz: " << max[2] <<std::endl;
-    if (min[0] > max[0] || min[1] > max[1]) {std::cout << "epic fail!" << std::endl;};
+    // std::cout.setf(std::ios_base::fixed);
+    // std::cout << "Query bounds" << SpatialIndex::Region(min, max, 3) << std::endl;
+
     std::vector<uint32_t>* vect = new std::vector<uint32_t>;
     LASVisitor* visitor = new LASVisitor(vect);
-    
-    const SpatialIndex::Region *region = new SpatialIndex::Region(min, max, 3);
-    // std::cout << *region << std::endl;
-    m_rtree->intersectsWithQuery(*region, *visitor);
-    // std::cout << index() <<std::endl;
+
+    try {
+        m_rtree->intersectsWithQuery(SpatialIndex::Region(min, max, 3), *visitor);
+    } catch (Tools::Exception& e) {
+        std::ostringstream os;
+        os << "Spatial Index Error: " << e.what();
+        delete visitor;
+        throw std::runtime_error(os.str());
+    }
+
+    delete visitor;
     return vect;
     
 }
@@ -441,18 +467,18 @@
 
 
 
-LASDataStream::LASDataStream(LASReader *reader) : m_reader(reader), m_pNext(0), m_id(0)
+LASIndexDataStream::LASIndexDataStream(LASReader *reader) : m_reader(reader), m_pNext(0), m_id(0)
 {
-    bool read = readPoint();
-    
-    if (read)
-        m_id = 0;
+    m_id = 0;
+    readPoint();
+}
 
-    std::cout<<"Initiating LASDataStream... read=" << read <<std::endl;
-    
+LASIndexDataStream::~LASIndexDataStream()
+{
+    if (m_pNext != 0) delete m_pNext;
 }
 
-bool LASDataStream::readPoint()
+bool LASIndexDataStream::readPoint()
 {
     double min[3], max[3];
     
@@ -479,7 +505,7 @@
 }
 
 
-SpatialIndex::IData* LASDataStream::getNext()
+SpatialIndex::IData* LASIndexDataStream::getNext()
 {
     if (m_pNext == 0) return 0;
 
@@ -490,20 +516,20 @@
     return ret;
 }
 
-bool LASDataStream::hasNext() throw (Tools::NotSupportedException)
+bool LASIndexDataStream::hasNext() throw (Tools::NotSupportedException)
 {
-    // std::cout << "LASDataStream::hasNext called ..." << std::endl;
+    // std::cout << "LASIndexDataStream::hasNext called ..." << std::endl;
     return (m_pNext != 0);
 }
 
-size_t LASDataStream::size() throw (Tools::NotSupportedException)
+size_t LASIndexDataStream::size() throw (Tools::NotSupportedException)
 {
     throw Tools::NotSupportedException("Operation not supported.");
 }
 
-void LASDataStream::rewind() throw (Tools::NotSupportedException)
+void LASIndexDataStream::rewind() throw (Tools::NotSupportedException)
 {
-    // std::cout << "LASDataStream::rewind called..." << std::endl;
+    // std::cout << "LASIndexDataStream::rewind called..." << std::endl;
 
     if (m_pNext != 0)
     {


More information about the Liblas-commits mailing list