Debian Codegraph @

src/vendor/ann/ANN.cpp

raw
1 //----------------------------------------------------------------------
2 // File: ANN.cpp
3 // Programmer: Sunil Arya and David Mount
4 // Description: Methods for ANN.h and ANNx.h
5 // Last modified: 01/27/10 (Version 1.1.2)
6 //----------------------------------------------------------------------
7 // Copyright (c) 1997-2010 University of Maryland and Sunil Arya and
8 // David Mount. All Rights Reserved.
9 //
10 // This software and related documentation is part of the Approximate
11 // Nearest Neighbor Library (ANN). This software is provided under
12 // the provisions of the Lesser GNU Public License (LGPL). See the
13 // file ../ReadMe.txt for further information.
14 //
15 // The University of Maryland (U.M.) and the authors make no
16 // representations about the suitability or fitness of this software for
17 // any purpose. It is provided "as is" without express or implied
18 // warranty.
19 //----------------------------------------------------------------------
20 // History:
21 // Revision 0.1 03/04/98
22 // Initial release
23 // Revision 1.0 04/01/05
24 // Added performance counting to annDist()
25 // Revision 1.1.2 01/27/10
26 // Fixed minor compilation bugs for new versions of gcc
27 //----------------------------------------------------------------------
28
29 #include <cstdlib> // C standard lib defs
30 #include <ANN/ANNx.h> // all ANN includes
31 #include <ANN/ANNperf.h> // ANN performance
32 #ifdef RANN
33 #define R_NO_REMAP
34 #include <R.h> // R headers for error handling
35 #endif
36
37 using namespace std; // make std:: accessible
38
39 //----------------------------------------------------------------------
40 // Point methods
41 //----------------------------------------------------------------------
42
43 //----------------------------------------------------------------------
44 // Distance utility.
45 // (Note: In the nearest neighbor search, most distances are
46 // computed using partial distance calculations, not this
47 // procedure.)
48 //----------------------------------------------------------------------
49
50 ANNdist annDist( // interpoint squared distance
51 int dim,
52 ANNpoint p,
53 ANNpoint q)
54 {
55 int d;
56 ANNcoord diff;
57 ANNcoord dist;
58
59 dist = 0;
60 for (d = 0; d < dim; d++) {
61 diff = p[d] - q[d];
62 dist = ANN_SUM(dist, ANN_POW(diff));
63 }
64 ANN_FLOP(3*dim) // performance counts
65 ANN_PTS(1)
66 ANN_COORD(dim)
67 return dist;
68 }
69
70 //----------------------------------------------------------------------
71 // annPrintPoint() prints a point to a given output stream.
72 //----------------------------------------------------------------------
73
74 void annPrintPt( // print a point
75 ANNpoint pt, // the point
76 int dim, // the dimension
77 std::ostream &out) // output stream
78 {
79 for (int j = 0; j < dim; j++) {
80 out << pt[j];
81 if (j < dim-1) out << " ";
82 }
83 }
84
85 //----------------------------------------------------------------------
86 // Point allocation/deallocation:
87 //
88 // Because points (somewhat like strings in C) are stored
89 // as pointers. Consequently, creating and destroying
90 // copies of points may require storage allocation. These
91 // procedures do this.
92 //
93 // annAllocPt() and annDeallocPt() allocate a deallocate
94 // storage for a single point, and return a pointer to it.
95 //
96 // annAllocPts() allocates an array of points as well a place
97 // to store their coordinates, and initializes the points to
98 // point to their respective coordinates. It allocates point
99 // storage in a contiguous block large enough to store all the
100 // points. It performs no initialization.
101 //
102 // annDeallocPts() should only be used on point arrays allocated
103 // by annAllocPts since it assumes that points are allocated in
104 // a block.
105 //
106 // annCopyPt() copies a point taking care to allocate storage
107 // for the new point.
108 //
109 // annAssignRect() assigns the coordinates of one rectangle to
110 // another. The two rectangles must have the same dimension
111 // (and it is not possible to test this here).
112 //----------------------------------------------------------------------
113
114 ANNpoint annAllocPt(int dim, ANNcoord c) // allocate 1 point
115 {
116 ANNpoint p = new ANNcoord[dim];
117 for (int i = 0; i < dim; i++) p[i] = c;
118 return p;
119 }
120
121 ANNpointArray annAllocPts(int n, int dim) // allocate n pts in dim
122 {
123 ANNpointArray pa = new ANNpoint[n]; // allocate points
124 ANNpoint p = new ANNcoord[n*dim]; // allocate space for coords
125 for (int i = 0; i < n; i++) {
126 pa[i] = &(p[i*dim]);
127 }
128 return pa;
129 }
130
131 void annDeallocPt(ANNpoint &p) // deallocate 1 point
132 {
133 delete [] p;
134 p = NULL;
135 }
136
137 void annDeallocPts(ANNpointArray &pa) // deallocate points
138 {
139 delete [] pa[0]; // dealloc coordinate storage
140 delete [] pa; // dealloc points
141 pa = NULL;
142 }
143
144 ANNpoint annCopyPt(int dim, ANNpoint source) // copy point
145 {
146 ANNpoint p = new ANNcoord[dim];
147 for (int i = 0; i < dim; i++) p[i] = source[i];
148 return p;
149 }
150
151 // assign one rect to another
152 void annAssignRect(int dim, ANNorthRect &dest, const ANNorthRect &source)
153 {
154 for (int i = 0; i < dim; i++) {
155 dest.lo[i] = source.lo[i];
156 dest.hi[i] = source.hi[i];
157 }
158 }
159
160 // is point inside rectangle?
161 ANNbool ANNorthRect::inside(int dim, ANNpoint p)
162 {
163 for (int i = 0; i < dim; i++) {
164 if (p[i] < lo[i] || p[i] > hi[i]) return ANNfalse;
165 }
166 return ANNtrue;
167 }
168
169 //----------------------------------------------------------------------
170 // Error handler
171 //----------------------------------------------------------------------
172
173 void annError(const char* msg, ANNerr level)
174 {
175 if (level == ANNabort) {
176 #ifdef RANN
177 Rf_error("RANN: %s",msg);
178 #else
179 cerr << "ANN: ERROR------->" << msg << "<-------------ERROR\n";
180 exit(1);
181 #endif
182 }
183 else {
184 #ifdef RANN
185 Rf_warning("RANN: %s",msg);
186 #else
187 cerr << "ANN: WARNING----->" << msg << "<-------------WARNING\n";
188 #endif
189 }
190 }
191
192 //----------------------------------------------------------------------
193 // Limit on number of points visited
194 // We have an option for terminating the search early if the
195 // number of points visited exceeds some threshold. If the
196 // threshold is 0 (its default) this means there is no limit
197 // and the algorithm applies its normal termination condition.
198 // This is for applications where there are real time constraints
199 // on the running time of the algorithm.
200 //----------------------------------------------------------------------
201
202 int ANNmaxPtsVisited = 0; // maximum number of pts visited
203 int ANNptsVisited; // number of pts visited in search
204
205 //----------------------------------------------------------------------
206 // Global function declarations
207 //----------------------------------------------------------------------
208
209 void annMaxPtsVisit( // set limit on max. pts to visit in search
210 int maxPts) // the limit
211 {
212 ANNmaxPtsVisited = maxPts;
213 }