Algorithm-KNN-XS

 view release on metacpan or  search on metacpan

lib_ann_interface.cpp  view on Meta::CPAN

#include "lib_ann_interface.h"

using namespace std;

LibANNInterface::LibANNInterface(std::vector< std::vector<double> >& points, string& dump, bool use_bd_tree, int bucket_size, int split_rule, int shrink_rule) {
    if (!points.size() && !dump.size())
        throw InvalidParameterValueException("either points or a tree dump must be given");

    kd_tree  = NULL;
    bd_tree  = NULL;
    data_pts = NULL;

    if (points.size()) {
        if (bucket_size < 1)
            throw InvalidParameterValueException("bucket_size must be >= 1");

        // the first element sets the dimension and set the number of availble data points
        count_data_pts = points.size();

        if (count_data_pts > 0) {
            dim = points.front().size();
        }
        else {
            dim = 0;
        }

lib_ann_interface.cpp  view on Meta::CPAN

        delete kd_tree;

    if (data_pts != NULL)
        annDeallocPts(data_pts);

    annClose();
}

void LibANNInterface::set_annMaxPtsVisit(int max_points) {
    if (max_points < 0)
        throw InvalidParameterValueException("max_points must be >= 0");

    annMaxPtsVisit(max_points);
}

std::vector< std::vector<double> > LibANNInterface::annkSearch(std::vector<double>& query_point, int limit_neighbors, double epsilon) {
    return ann_search(query_point, limit_neighbors, epsilon, false);
}

std::vector< std::vector<double> > LibANNInterface::annkPriSearch(std::vector<double>& query_point, int limit_neighbors, double epsilon) {
    return ann_search(query_point, limit_neighbors, epsilon, true);
}

std::vector< std::vector<double> > LibANNInterface::ann_search(std::vector<double>& query_point, int limit_neighbors, double epsilon, bool use_prio_search) {
    if (limit_neighbors < 0)
        throw InvalidParameterValueException("limit_neighbors must be >= 0");

    if (limit_neighbors > count_data_pts)
        throw InvalidParameterValueException("limit_neighbors must be <= the number of points in the current tree");

    if (epsilon < 0)
        throw InvalidParameterValueException("epsilon must be >= 0");

    if (query_point.size() != dim)
        throw InvalidParameterValueException("query_point must have the same dimension as the current tree");

    if (limit_neighbors == 0)
        limit_neighbors = count_data_pts;

    std::vector< std::vector<double> > result;

    ANNidxArray nn_idx = new ANNidx[limit_neighbors];
    ANNdistArray dists = new ANNdist[limit_neighbors];
    ANNpoint query_pt  = annAllocPt(dim);

lib_ann_interface.cpp  view on Meta::CPAN


    annDeallocPt(query_pt);
    delete [] nn_idx;
    delete [] dists;

    return result;
}

std::vector< std::vector<double> > LibANNInterface::annkFRSearch(std::vector<double>& query_point, int limit_neighbors, double epsilon, double radius) {
    if (limit_neighbors < 0)
        throw InvalidParameterValueException("limit_neighbors must be >= 0");

    if (limit_neighbors > count_data_pts)
        throw InvalidParameterValueException("limit_neighbors must be <= the number of points in the current tree");

    if (epsilon < 0)
        throw InvalidParameterValueException("epsilon must be >= 0");

    if (query_point.size() != dim)
        throw InvalidParameterValueException("query_point must have the same dimension as the current tree");

    if (limit_neighbors == 0)
        limit_neighbors = count_data_pts;

    std::vector< std::vector<double> > result;

    ANNidxArray nn_idx = new ANNidx[limit_neighbors];
    ANNdistArray dists = new ANNdist[limit_neighbors];
    ANNpoint query_pt  = annAllocPt(dim);

lib_ann_interface.cpp  view on Meta::CPAN


    annDeallocPt(query_pt);
    delete [] nn_idx;
    delete [] dists;

    return result;
}

int LibANNInterface::annCntNeighbours(std::vector<double>& query_point, double epsilon, double radius) {
    if (epsilon < 0)
        throw InvalidParameterValueException("epsilon must be >= 0");

    if (query_point.size() != dim)
        throw InvalidParameterValueException("query_point must have the same dimension as the current tree");

    ANNpoint query_pt  = annAllocPt(dim);

    int i = 0;
    std::vector<double>::iterator iter;

    for(iter = query_point.begin(); iter != query_point.end(); iter++) {
        query_pt[i] = *iter;
        i++;
    }

lib_ann_interface.h  view on Meta::CPAN

#include <vector>

#include <ANN/ANN.h> // http://www.cs.umd.edu/~mount/ANN/
#include <ANN/ANNperf.h> // http://www.cs.umd.edu/~mount/ANN/

using namespace std;

class InvalidParameterValueException : public std::exception {
    public:
        explicit InvalidParameterValueException(const std::string& what) : m_what(what) {}
        virtual ~InvalidParameterValueException() throw() {}
        virtual const char * what() const throw() {
            return m_what.c_str();
        }

    private:
        std::string m_what;
};

// libANN can do a exit(1) if a a given dump file is incorrect

class LibANNInterface {



( run in 0.492 second using v1.01-cache-2.11-cpan-496ff517765 )