简体   繁体   English

试图实现一个 C++ 库,需要一些关于如何与之交互的指针

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

I'm relatively inexperienced in C++, and am trying to compile a library to DLL for use in Unity.我在 C++ 方面相对缺乏经验,正在尝试将库编译为 DLL 以在 Unity 中使用。 As far as generating the DLL and interfacing with it, I've had success in getting my below function to call and return a dummy value in Unity, no worries.至于生成 DLL 并与之交互,我已经成功地让我的下面的函数在 Unity 中调用并返回一个虚拟值,不用担心。

Where I'm struggling is working out how to interface with this Approximate Nearest Neighbour library, and could use some pointers.我正在努力解决如何与这个Approximate Nearest Neighbor库交互,并且可以使用一些指针。 (This may be a hard problem to define without downloading the above-linked source and having a look yourself, but I will attempt to make it simple) (如果不下载上面链接的源代码并自己查看,这可能是一个很难定义的问题,但我会尽量简化它)

In Unity, I have a three-dimensional array (points[] = float[x,y,z]), and a query point in space (float[x,y,z]).在 Unity 中,我有一个三维数组(points[] = float[x,y,z])和一个空间中的查询点(float[x,y,z])。 I wish to feed this data into something like the below function (which I have modified from the ann_sample.cpp - original linked at the bottom), and return the nearest neighbour to this point.我希望将这些数据输入到类似于下面的函数中(我已经从 ann_sample.cpp 中修改了 - 原始链接在底部),并将最近的邻居返回到这一点。

My function我的功能

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  
}

Original Function (This is expecting a command-line input - ie ann_sample [-d dim] [-max mpts] [-nn k] [-e eps] [-df data] [-qf query])原始函数(这是一个命令行输入 - 即 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;
}

As you can see in my comments, I am having trouble determining where and how I might actually feed the input data in, that is - my point array mentioned above.正如您在我的评论中看到的那样,我无法确定实际输入数据的位置和方式,即 - 上面提到的我的点数组。

It appears the data is being populated (in the reference function) with the below code, by utilizing an "istream" class, but I do not understand how this is working, or how I would emulate it.通过使用“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;
}

Update:更新:
Writing this all out was evidently the push I needed in the right direction!把这一切写出来显然是我在正确方向上需要的推动力! In case it's relevant for anyone in the future, the manner through which I interfaced with it was fairly straightforward.如果它与未来的任何人相关,我与它交互的方式是相当简单的。

I passed a few 2D arrays into the C++ plugin, which I then converted into ANNpointArrays.我将一些 2D 数组传递到 C++ 插件中,然后将其转换为 ANNpointArrays。 Given it's C++, I needed to first instantiate the arrays of the appropriate size, then populate as the below:鉴于它是 C++,我需要首先实例化适当大小的数组,然后填充如下:

    float qPoints[][3]

    .....

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

Once I had this, it was relatively simple to run an annKSearch on each of my query points, returning an array of integers for Nearest Neighbour, and floats for distances.一旦我有了这个,在我的每个查询点上运行一个 annKSearch 就相对简单了,返回一个最近邻的整数数组,并浮动距离。 I needed to learn a few things about how to pass and instantiate arrays in C++, but it was a lot easier than I thought!我需要学习一些关于如何在 C++ 中传递和实例化数组的知识,但这比我想象的要容易得多!

Not that anyone's reading it - but this has been a useful exercise for me, and I'm leaving the question up just in case someone else is trying to implement this library in C#!并不是说有人在读它——但这对我来说是一个有用的练习,我把这个问题留了下来,以防其他人试图用 C# 实现这个库!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM