wjinan hai 9 meses
achega
113e2e0592
Modificáronse 4 ficheiros con 277 adicións e 0 borrados
  1. 16 0
      README.md
  2. 71 0
      example/example.cpp
  3. 190 0
      example/libspatialindex.cpp
  4. BIN=BIN
      spatialindex-src-1.9.3.tar.gz

+ 16 - 0
README.md

@@ -0,0 +1,16 @@
+1. 安装sptialindex
+下载源码(可直接解压仓库里的tar压缩包 1.9.3版本)
+https://libspatialindex.org/en/latest/index.html
+按以下步骤进行编译安装
+https://libspatialindex.org/en/latest/install.html
+
+2. 编译cpp文件
+./example 下的libspatialindex.cpp
+命令为:
+g++ libspatialindex.cpp -o libspatialindex -I/home/marioh/usr/include -L/home/marioh/usr/lib -lspatialindex
+
+若找不到库,进行软连接
+ln -s /your_install_path/xxx.so /usr/lib
+sudo ldconfig
+
+可编译运行./example/example.cpp测试库是否安装成功,是否可用。

+ 71 - 0
example/example.cpp

@@ -0,0 +1,71 @@
+#include <stdio.h>
+#include <cstdlib>
+#include <spatialindex/capi/sidx_api.h> 
+#include <spatialindex/capi/sidx_impl.h>
+#include <spatialindex/capi/sidx_config.h>
+
+using namespace std;
+using namespace SpatialIndex;
+
+
+void load(IndexH idx);
+void query(IndexH idx);
+void bounds(IndexH idx);
+
+int main()
+{
+   char* pszVersion = SIDX_Version();
+   fprintf(stdout, "libspatialindex version: %s\n", pszVersion);
+   fflush(stdout);
+   free(pszVersion);
+
+   IndexPropertyH props = IndexProperty_Create();
+ 
+   // create an in-memory r*-tree index
+   IndexProperty_SetIndexType(props, RT_RTree);
+   IndexProperty_SetIndexStorage(props, RT_Memory);
+   IndexH idx = Index_Create(props);
+   IndexProperty_Destroy(props); 
+   if (Index_IsValid(idx))
+   {
+       load(idx);
+       bounds(idx);
+       query(idx);
+       Index_Destroy(idx); 
+       }
+   else
+   {
+       printf("Failed to create valid index\n");
+   }
+   return 0; 
+}
+void load(IndexH idx)
+{
+    double min[] = {0.5, 0.5};
+    double max[] = {0.5, 0.5};
+    Index_InsertData(idx, 1, min, max, 2, 0, 0);
+}
+void query(IndexH idx)
+{
+    double min[] = {0.0, 0.0};
+    double max[] = {1.0, 1.0};
+    uint64_t nResults;
+    Index_Intersects_count(idx, min, max, 2, &nResults);
+    if (nResults == 1)
+         printf("Successful Query\n");
+    else
+         printf("Failed to execute query\n");
+}
+void bounds(IndexH idx)
+{
+    uint32_t dims;
+    double* pMins;
+    double* pMaxs;
+    Index_GetBounds(idx, &pMins, &pMaxs, &dims);
+    free(pMins);
+    free(pMaxs);
+    if (dims == 2)
+        printf("Successful bounds query\n");
+    else
+        printf("Failed to execute bounds query\n");
+}

+ 190 - 0
example/libspatialindex.cpp

@@ -0,0 +1,190 @@
+#include <stdio.h>
+#include <cstdlib>
+#include <random>
+#include <chrono>
+#include <vector>
+#include <spatialindex/capi/sidx_api.h> 
+#include <spatialindex/capi/sidx_impl.h>
+#include <spatialindex/capi/sidx_config.h>
+
+using namespace std;
+using namespace SpatialIndex;
+
+struct MyPoint{
+   double x;
+   double y;
+   double z;
+   void init(double newX, double newY, double newZ) {
+       x = newX;
+       y = newY;
+       z = newZ;
+   }
+   double distance(){
+       return x * x + y * y + z * z ;
+   }
+};
+const long long pointNum = 1000000; //节点数;
+const int knn = 100; //k近邻;
+long long queryNum = 1;
+
+vector<MyPoint> pointVector;
+
+double* generateRandomPoint() {
+    std::random_device rd;
+    std::mt19937_64 gen(rd());
+    //范围
+    std::uniform_real_distribution<double> dis(0, 10); 
+    
+    double* coords = new double[3];
+    coords[0] = dis(gen);
+    coords[1] = dis(gen);
+    coords[2] = dis(gen);
+    //可注释
+    MyPoint myPoint ;
+    myPoint.init(coords[0], coords[1], coords[2]);
+    pointVector.push_back(myPoint);
+    //Print
+    //for(int i = 0; i < 3; i++) cout<<coords[i]<<" ";  printf("\n");
+    return coords;
+}
+
+Index* createIndex()
+{
+    // create a property set with default values.
+    // see utility.cc for all defaults  http://libspatialindex.github.io/doxygen/Utility_8cc_source.html#l00031
+    Tools::PropertySet* ps = GetDefaults();
+    Tools::Variant var;
+
+    // set index type to R*-Tree
+    var.m_varType = Tools::VT_ULONG;
+    var.m_val.ulVal = RT_RTree;
+    ps->setProperty("IndexType", var);
+
+    // Set index to store in memory (default is disk)
+    var.m_varType = Tools::VT_ULONG;
+    var.m_val.ulVal = RT_Memory;
+    ps->setProperty("IndexStorageType", var);
+    
+    var.m_varType = Tools::VT_ULONG;
+    var.m_val.ulVal = 3;
+    ps->setProperty("Dimension", var);
+    // initialize index
+    Index* idx = new Index(*ps);
+    delete ps;
+
+    // check index is ok
+    if (!idx->index().isIndexValid())
+        throw "Failed to create a valid index";
+    else
+        cout << "created index" << endl;
+
+    return idx;
+}
+
+void addPoint(Index* idx, double x, double y, double z, int64_t id)
+{
+    // create array with x, y, z coordinates
+    //double coords[] = {x, y, z};
+    double* coords = generateRandomPoint();
+    // shapes can also have an object associated with them but we'll leave that for the moment.
+    uint8_t* pData = 0;
+    uint32_t nDataLength = 0;
+
+    // create shape - three-dimensional
+    SpatialIndex::IShape* shape = 0;
+    shape = new SpatialIndex::Point(coords, 3);
+
+    // insert into the index along with an object and an ID
+    idx->index().insertData(nDataLength, pData, *shape, id);
+    //Print
+    //cout << "Point " << id << " inserted into the index.";
+
+    delete shape;
+    delete[] coords;
+}
+
+std::vector<SpatialIndex::IData*>* getNearest(Index* idx, double x, double y, double z, long long k)
+{
+    double coords[] = {x, y, z};
+
+    // get a visitor object and a point from which to search
+    ObjVisitor* visitor = new ObjVisitor;
+    // make the point from x, y, z coordinates
+    SpatialIndex::Point* r = new SpatialIndex::Point(coords, 3);
+
+    // get nearest maxResults shapes from the index
+    idx->index().nearestNeighborQuery(k, *r, *visitor);
+
+    // get the count of results
+    int64_t nResultCount;
+    nResultCount = visitor->GetResultCount();
+
+    // get the actual results
+    std::vector<SpatialIndex::IData*>& results = visitor->GetResults();
+    // an empty vector that we will copy the results to
+    vector<SpatialIndex::IData*>* resultsCopy = new vector<SpatialIndex::IData*>();
+
+    // copy the items into the newly allocated vector array
+    // we need to make sure to clone the actual item instead
+    // of just the pointers, as the visitor will nuke them
+    // upon destroy
+    for (int64_t i = 0; i < nResultCount; ++i)
+    {
+        resultsCopy->push_back(dynamic_cast<SpatialIndex::IData*>(results[i]->clone()));
+    }
+
+    delete r;
+    delete visitor;
+    //Print
+    //cout << "found " << nResultCount << " results." << endl;
+
+    return resultsCopy;
+}
+
+int main(int argc, char* argv[])
+{
+    cout<<"knn = "<< knn<<endl;
+    try {
+        // initialize the Index pointer
+        Index* idx = createIndex();
+        // add some points
+        auto insertStart = std::chrono::high_resolution_clock::now();
+        for(int i = 0; i < pointNum; i++) {
+	    addPoint(idx, 0,0,0, i);
+	    //Print
+	    //cout<<"distance:"<<pointVector[i].distance()<<endl;
+	}	
+	auto insertEnd = std::chrono::high_resolution_clock::now();
+	std::chrono::duration<double> duration = insertEnd - insertStart;
+        double insertTime = duration.count();
+        cout<< "插入数据花费的时间为"<< insertTime << "秒"<<"("<<pointNum<<")"<<endl;
+        
+        // get the nearest two locations to the royal albert hall
+        auto queryStart = std::chrono::high_resolution_clock::now();
+        for(int i = 0 ; i<queryNum; i++){
+        	double* coords = generateRandomPoint();
+        	std::vector<SpatialIndex::IData*>* results = getNearest(idx, coords[0], coords[1], coords[2], knn);
+        	delete[] coords;
+        }
+        auto queryEnd = std::chrono::high_resolution_clock::now();
+        std::chrono::duration<double> duration2 = queryEnd - queryStart;
+        double queryTime = duration2.count();
+        cout<< "查询数据花费的时间为"<< queryTime << "秒"<<"("<<queryNum<<")"<<endl;
+        /*//Print
+        if(knn <= pointNum){
+           for(SpatialIndex::IData* point : *results){
+           	int id = point->getIdentifier();
+        	cout<<"最近的n个点:"<<id;
+    	  	cout<<"("<<pointVector[id].x<<", "<<pointVector[id].y<<", "<<pointVector[id].z<<")"<<endl;
+          }//endfor
+        }//endif
+        */
+    }//end try
+    catch (const char* ex) {
+        cout << "Exception: " << ex << endl;
+    }
+
+    return 0;
+}
+//g++ xxx.cpp -o xxx -I/home/marioh/usr/include -L/home/marioh/usr/lib -lspatialindex
+

BIN=BIN
spatialindex-src-1.9.3.tar.gz