Debian Codegraph @

src/NN.cc

raw
1 #include <cstdlib> // C standard lib defs
2 #include <iomanip> // I/O manipulators
3 #include <cmath> // math includes
4 #include <iostream> // I/O streams
5 #include <cstring> // C-style strings
6
7 #include <math.h> // math routines
8 #define R_NO_REMAP
9 #include <R.h> // R header
10
11 namespace ann2 {
12
13 #include "ANN/ANN.h" // ANN library header
14
15 }
16
17 //------------------------------------------------------------------------------------------------
18 // Near Neighbours Program
19 //------------------------------------------------------------------------------------------------
20 extern "C"
21 {
22 void get_NN_2Set(double *data, double *query, int *D, int *ND, int *NQ, int *K, double *EPS,
23 int *SEARCHTYPE, int *USEBDTREE, double *SQRAD, int *nn_index, double *distances)
24 {
25 using namespace ann2;
26
27 const int d = *D; // Number of Dimensions for points
28 const int nd = *ND; // Number of Data points
29 const int nq= *NQ; // Number of Query points
30 const int k = *K; // Maximum number of Nearest Neighbours
31
32 const int searchtype = *SEARCHTYPE;
33 const bool usebdtree = *USEBDTREE?true:false;
34
35 const double error_bound = *EPS; // enough said!
36 const double sqRad = *SQRAD; // Squared Radius for rad search
37
38 ANNkd_tree *the_tree; // Search structure
39
40 ANNpointArray data_pts = annAllocPts(nd,d); // Allocate data points
41 ANNidxArray nn_idx = new ANNidx[k]; // Allocate near neigh indices
42 ANNdistArray dists = new ANNdist[k]; // Allocate near neighbor dists
43
44 int *d_ptr = new int[d];
45 int ptr = 0;
46
47 // set up column offsets for query point matrix (to convert Row/Col major)
48 for(int i = 0; i < d; i++)
49 {
50 d_ptr[i] = i*nd;
51 }
52
53 for(int i = 0; i < nd; i++) // now construct the points
54 {
55 for(int j = 0; j < d; j++)
56 {
57 data_pts[i][j]=data[ d_ptr[j]++ ];
58 }
59 }
60
61 if(usebdtree){
62 the_tree = new ANNbd_tree( // Build search structure
63 data_pts, // The data points
64 nd, // Number of data points
65 d); // Dimension of space
66 } else {
67 the_tree = new ANNkd_tree( data_pts, nd, d);
68 }
69
70 // set up offsets for query point matrix (to convert Row / Col major)
71 for(int i = 0; i < d; i++)
72 {
73 d_ptr[i] = i*nq;
74 }
75
76 ANNpoint pq = annAllocPt(d);
77 for(int i = 0; i < nq; i++) // Run all query points against tree
78 {
79 // read coords of current query point
80 for(int j = 0; j < d; j++)
81 {
82 pq[j]=query[ d_ptr[j]++ ];
83 }
84
85 switch(searchtype){
86 case 1:
87 the_tree->annkSearch( // search
88 pq, // query point
89 k, // number of near neighbors
90 nn_idx, // nearest neighbors (returned)
91 dists, // distance (returned)
92 error_bound); // error bound
93 break;
94
95 case 2: // Priority search
96 the_tree->annkPriSearch(pq, k, nn_idx, dists, error_bound);
97 break;
98
99 case 3: // Fixed radius search
100 the_tree->annkFRSearch( pq, sqRad, k, nn_idx, dists,error_bound);
101 break;
102 }
103
104 for (int j = 0; j < k; j++)
105 {
106 distances[ptr] = ANN_ROOT(dists[j]); // unsquare distance
107 nn_index[ptr++] = nn_idx[j] + 1; // put indices in returned array
108 }
109 }
110
111 // Do a little bit of memory management......
112 annDeallocPt(pq);
113 annDeallocPts(data_pts);
114 delete [] nn_idx;
115 delete [] dists;
116 delete [] d_ptr;
117 delete the_tree;
118 }
119 }
120