簡體   English   中英

試圖實現一個 C++ 庫,需要一些關於如何與之交互的指針

[英]Attempting to implement a C++ library, need some pointers on how to interface with it

我在 C++ 方面相對缺乏經驗,正在嘗試將庫編譯為 DLL 以在 Unity 中使用。 至於生成 DLL 並與之交互,我已經成功地讓我的下面的函數在 Unity 中調用並返回一個虛擬值,不用擔心。

我正在努力解決如何與這個Approximate Nearest Neighbor庫交互,並且可以使用一些指針。 (如果不下載上面鏈接的源代碼並自己查看,這可能是一個很難定義的問題,但我會盡量簡化它)

在 Unity 中,我有一個三維數組(points[] = float[x,y,z])和一個空間中的查詢點(float[x,y,z])。 我希望將這些數據輸入到類似於下面的函數中(我已經從 ann_sample.cpp 中修改了 - 原始鏈接在底部),並將最近的鄰居返回到這一點。

我的功能

int myInit(float x, float y, float z, int numPoints, int maxPoints, int nn)
{
    int                 nPts;                   
    ANNpointArray       dataPts;                
    ANNpoint            queryPt;                
    ANNidxArray         nnIdx;                  
    ANNdistArray        dists;                  
    ANNkd_tree* kdTree;                         

    int dimension = 3;
    
    queryPt = annAllocPt(dimension , x);                        //This only expects the dimension, and a coord - I assume this is pre-allocation? 
    dataPts = annAllocPts(maxPoints, dimension);    //Again, this does not expect the coordinate (xyz) data, is pre-allocating the array?
    nnIdx = new ANNidx[nn];                         // more pre-allocating?
    dists = new ANNdist[nn];                        // more pre-allocating?

    nPts = numPoints;           
                        
    //Should we be populating the actual queryPt and dataPts here?
    //at the moment, these are still empty?

    kdTree = new ANNkd_tree(                    // build search structure
        dataPts,                                // the data points
        nPts,                                   // number of points
        dimension);                             // dimension of space


    kdTree->annkSearch(                     // search
            queryPt,                        // query point
            nn,                             // number of near neighbors
            nnIdx,                          // nearest neighbors (returned)
            dists,                          // distance (returned)
            eps);                           // error bound
    
    annClose();                                 // done with ANN

    return nnIdx[0];  //this is the nearest neighbor  
}

原始函數(這是一個命令行輸入 - 即 ann_sample [-d dim] [-max mpts] [-nn k] [-e eps] [-df data] [-qf query])

int main(int argc, char **argv)
{
    int                 nPts;                   // actual number of data points
    ANNpointArray       dataPts;                // data points
    ANNpoint            queryPt;                // query point
    ANNidxArray         nnIdx;                  // near neighbor indices
    ANNdistArray        dists;                  // near neighbor distances
    ANNkd_tree*         kdTree;                 // search structure

    getArgs(argc, argv);                        // read command-line arguments

    queryPt = annAllocPt(dim);                  // allocate query point
    dataPts = annAllocPts(maxPts, dim);         // allocate data points
    nnIdx = new ANNidx[k];                      // allocate near neigh indices
    dists = new ANNdist[k];                     // allocate near neighbor dists

    nPts = 0;                                   // read data points

    cout << "Data Points:\n";
    while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) {
        printPt(cout, dataPts[nPts]);
        nPts++;
    }

    kdTree = new ANNkd_tree(                    // build search structure
                    dataPts,                    // the data points
                    nPts,                       // number of points
                    dim);                       // dimension of space

    while (readPt(*queryIn, queryPt)) {         // read query points
        cout << "Query point: ";                // echo query point
        printPt(cout, queryPt);

        kdTree->annkSearch(                     // search
                queryPt,                        // query point
                k,                              // number of near neighbors
                nnIdx,                          // nearest neighbors (returned)
                dists,                          // distance (returned)
                eps);                           // error bound

        cout << "\tNN:\tIndex\tDistance\n";
        for (int i = 0; i < k; i++) {           // print summary
            dists[i] = sqrt(dists[i]);          // unsquare distance
            cout << "\t" << i << "\t" << nnIdx[i] << "\t" << dists[i] << "\n";
        }
    }
    delete [] nnIdx;                            // clean things up
    delete [] dists;
    delete kdTree;
    annClose();                                 // done with ANN

    return EXIT_SUCCESS;
}

正如您在我的評論中看到的那樣,我無法確定實際輸入數據的位置和方式,即 - 上面提到的我的點數組。

通過使用“istream”類,似乎正在使用以下代碼填充數據(在參考函數中),但我不明白這是如何工作的,或者我將如何模擬它。

    while (nPts < maxPts && readPt(*dataIn, dataPts[nPts])) {
        printPt(cout, dataPts[nPts]);
        nPts++;
    }

... 

bool readPt(istream &in, ANNpoint p)            // read point (false on EOF)
{
    for (int i = 0; i < dim; i++) {
        if(!(in >> p[i])) return false;
    }
    return true;
}

更新:
把這一切寫出來顯然是我在正確方向上需要的推動力! 如果它與未來的任何人相關,我與它交互的方式是相當簡單的。

我將一些 2D 數組傳遞到 C++ 插件中,然后將其轉換為 ANNpointArrays。 鑒於它是 C++,我需要首先實例化適當大小的數組,然后填充如下:

    float qPoints[][3]

    .....

    for (int i = 0; i < nQueryPts; i++) {
        for (int j = 0; j < dimension; j++)
        {
            queryPts[i][j] = qPoints[i][j];
        }
    }

一旦我有了這個,在我的每個查詢點上運行一個 annKSearch 就相對簡單了,返回一個最近鄰的整數數組,並浮動距離。 我需要學習一些關於如何在 C++ 中傳遞和實例化數組的知識,但這比我想象的要容易得多!

並不是說有人在讀它——但這對我來說是一個有用的練習,我把這個問題留了下來,以防其他人試圖用 C# 實現這個庫!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM