ann-kd-tree.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu, 18 Jun 2009 13:03:18 -0700 (2009-06-18)
changeset 4 d88e548a9aeb
parent 2 cbe1fa5e2993
child 5 4aed2049d9cc
permissions -rw-r--r--
Removed unnecessary priority parameter from fixed radius search
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     1
#include <boost/python.hpp>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     2
#include <boost/python/stl_iterator.hpp>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     3
#include <boost/shared_ptr.hpp>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     4
#include <boost/functional/hash.hpp>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     5
namespace bp = boost::python;
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     6
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     7
#include <ANN/ANN.h>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     8
#include <iostream>
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
     9
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    10
// Constructor from list        TODO: change to iterator
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    11
boost::shared_ptr<ANNkd_tree>       init_from_list(bp::list lst)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    12
{ 
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    13
    int             dimension   = bp::len(lst[0]);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    14
    int             npts        = bp::len(lst);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    15
    ANNpointArray   dataPts     = annAllocPts(npts, dimension);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    16
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    17
    // Convert points from Python list to ANNpointArray
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    18
    for (unsigned p = 0; p < bp::len(lst); ++p)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    19
    {
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    20
        ANNpoint& pt = dataPts[p];
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    21
        for (unsigned c = 0; c < dimension; ++c)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    22
            pt[c] = bp::extract<ANNcoord>(lst[p][c]);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    23
    }
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    24
    
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    25
    boost::shared_ptr<ANNkd_tree>   p(new ANNkd_tree(dataPts, npts, dimension));
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    26
    return p;
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    27
}
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    28
1
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    29
bp::tuple                           search(ANNkd_tree& kdtree, bp::list q, int k, double eps, bool priority = false)
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    30
{
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    31
    ANNpoint annq = annAllocPt(bp::len(q));
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    32
    for (unsigned c = 0; c < bp::len(q); ++c)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    33
        annq[c] = bp::extract<ANNcoord>(q[c]);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    34
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    35
    ANNidxArray     nn_idx  = new ANNidx[k];
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    36
    ANNdistArray    dists   = new ANNdist[k];
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    37
1
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    38
    if (priority)
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    39
        kdtree.annkPriSearch(annq, k, nn_idx, dists, eps);
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    40
    else
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    41
        kdtree.annkSearch(annq, k, nn_idx, dists, eps);
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    42
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    43
    bp::list indices, distances;
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    44
    for (unsigned i = 0; i < k; ++i)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    45
    {
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    46
        indices.append(nn_idx[i]);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    47
        distances.append(dists[i]);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    48
    }
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    49
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    50
    delete nn_idx; delete dists;
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    51
    return bp::make_tuple(indices, distances);
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    52
}
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    53
4
d88e548a9aeb Removed unnecessary priority parameter from fixed radius search
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 2
diff changeset
    54
bp::tuple                           k_fixed_radius_search(ANNkd_tree& kdtree, bp::list q, double sqRad, int k, double eps)
2
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    55
{
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    56
    ANNpoint annq = annAllocPt(bp::len(q));
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    57
    for (unsigned c = 0; c < bp::len(q); ++c)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    58
        annq[c] = bp::extract<ANNcoord>(q[c]);
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    59
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    60
    ANNidxArray     nn_idx  = new ANNidx[k];
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    61
    ANNdistArray    dists   = new ANNdist[k];
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    62
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    63
    int kball = kdtree.annkFRSearch(annq, k, sqRad, nn_idx, dists, eps);
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    64
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    65
    bp::list indices, distances;
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    66
    for (unsigned i = 0; i < k; ++i)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    67
    {
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    68
        if (nn_idx[i] != ANN_NULL_IDX)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    69
            indices.append(nn_idx[i]);
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    70
        if (dists[i] != ANN_DIST_INF)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    71
            distances.append(dists[i]);
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    72
    }
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    73
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    74
    delete nn_idx; delete dists;
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    75
    return bp::make_tuple(indices, distances, kball);
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    76
}
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    77
1
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    78
bp::tuple                           ksearch(ANNkd_tree& kdtree, bp::list q, int k, double eps)
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    79
{
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    80
    return search(kdtree, q, k, eps, false);
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    81
}
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    82
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    83
bp::tuple                           k_priority_search(ANNkd_tree& kdtree, bp::list q, int k, double eps)
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    84
{
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    85
    return search(kdtree, q, k, eps, true);
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    86
}
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    87
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    88
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    89
void export_ann_kd_tree()
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    90
{
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    91
    bp::class_<ANNkd_tree>("KDTree")
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    92
        .def("__init__",            bp::make_constructor(&init_from_list))
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    93
2
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    94
        .def("kSearch",             &ksearch)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    95
        .def("kPriSearch",          &k_priority_search)
cbe1fa5e2993 Added fixed radius search, and renamed methods to match C++ names
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 1
diff changeset
    96
        .def("kFRSearch",           &k_fixed_radius_search)
1
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
    97
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    98
        .def("__len__",             &ANNkd_tree::nPoints)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
    99
        .def("dim",                 &ANNkd_tree::theDim)
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
   100
    ;
1
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
   101
d6060e9b11c4 Added k_priority_search and max_pts_visit
Dmitriy Morozov <dmitriy@mrzv.org>
parents: 0
diff changeset
   102
    bp::def("max_pts_visit",        &annMaxPtsVisit);
0
dc27a515a0a3 Initial commit
Dmitriy Morozov <dmitriy@mrzv.org>
parents:
diff changeset
   103
}