00001 //---------------------------------------------------------------------- 00002 // File: ANNx.h 00003 // Programmer: Sunil Arya and David Mount 00004 // Last modified: 03/04/98 (Release 0.1) 00005 // Description: Internal include file for ANN 00006 // 00007 // These declarations are of use in manipulating some of 00008 // the internal data objects appearing in ANN, but are not 00009 // needed for applications just using the nearest neighbor 00010 // search. 00011 // 00012 // Typical users of ANN should not need to access this file. 00013 //---------------------------------------------------------------------- 00014 // Copyright (c) 1997-2005 University of Maryland and Sunil Arya and 00015 // David Mount. All Rights Reserved. 00016 // 00017 // This software and related documentation is part of the Approximate 00018 // Nearest Neighbor Library (ANN). This software is provided under 00019 // the provisions of the Lesser GNU Public License (LGPL). See the 00020 // file ../ReadMe.txt for further information. 00021 // 00022 // The University of Maryland (U.M.) and the authors make no 00023 // representations about the suitability or fitness of this software for 00024 // any purpose. It is provided "as is" without express or implied 00025 // warranty. 00026 //---------------------------------------------------------------------- 00027 // History: 00028 // Revision 0.1 03/04/98 00029 // Initial release 00030 // Revision 1.0 04/01/05 00031 // Changed LO, HI, IN, OUT to ANN_LO, ANN_HI, etc. 00032 //---------------------------------------------------------------------- 00033 00034 #ifndef ANNx_H 00035 #define ANNx_H 00036 00037 #include <iomanip> // I/O manipulators 00038 #include <ANN/ANN.h> // ANN includes 00039 00040 //---------------------------------------------------------------------- 00041 // Global constants and types 00042 //---------------------------------------------------------------------- 00043 enum {ANN_LO=0, ANN_HI=1}; // splitting indices 00044 enum {ANN_IN=0, ANN_OUT=1}; // shrinking indices 00045 // what to do in case of error 00046 enum ANNerr {ANNwarn = 0, ANNabort = 1}; 00047 00048 //---------------------------------------------------------------------- 00049 // Maximum number of points to visit 00050 // We have an option for terminating the search early if the 00051 // number of points visited exceeds some threshold. If the 00052 // threshold is 0 (its default) this means there is no limit 00053 // and the algorithm applies its normal termination condition. 00054 //---------------------------------------------------------------------- 00055 00056 extern int ANNmaxPtsVisited; // maximum number of pts visited 00057 extern int ANNptsVisited; // number of pts visited in search 00058 00059 //---------------------------------------------------------------------- 00060 // Global function declarations 00061 //---------------------------------------------------------------------- 00062 00063 void annError( // ANN error routine 00064 char *msg, // error message 00065 ANNerr level); // level of error 00066 00067 void annPrintPt( // print a point 00068 ANNpoint pt, // the point 00069 int dim, // the dimension 00070 std::ostream &out); // output stream 00071 00072 //---------------------------------------------------------------------- 00073 // Orthogonal (axis aligned) rectangle 00074 // Orthogonal rectangles are represented by two points, one 00075 // for the lower left corner (min coordinates) and the other 00076 // for the upper right corner (max coordinates). 00077 // 00078 // The constructor initializes from either a pair of coordinates, 00079 // pair of points, or another rectangle. Note that all constructors 00080 // allocate new point storage. The destructor deallocates this 00081 // storage. 00082 // 00083 // BEWARE: Orthogonal rectangles should be passed ONLY BY REFERENCE. 00084 // (C++'s default copy constructor will not allocate new point 00085 // storage, then on return the destructor free's storage, and then 00086 // you get into big trouble in the calling procedure.) 00087 //---------------------------------------------------------------------- 00088 00089 class ANNorthRect { 00090 public: 00091 ANNpoint lo; // rectangle lower bounds 00092 ANNpoint hi; // rectangle upper bounds 00093 // 00094 ANNorthRect( // basic constructor 00095 int dd, // dimension of space 00096 ANNcoord l=0, // default is empty 00097 ANNcoord h=0) 00098 { lo = annAllocPt(dd, l); hi = annAllocPt(dd, h); } 00099 00100 ANNorthRect( // (almost a) copy constructor 00101 int dd, // dimension 00102 const ANNorthRect &r) // rectangle to copy 00103 { lo = annCopyPt(dd, r.lo); hi = annCopyPt(dd, r.hi); } 00104 00105 ANNorthRect( // construct from points 00106 int dd, // dimension 00107 ANNpoint l, // low point 00108 ANNpoint h) // hight point 00109 { lo = annCopyPt(dd, l); hi = annCopyPt(dd, h); } 00110 00111 ~ANNorthRect() // destructor 00112 { annDeallocPt(lo); annDeallocPt(hi); } 00113 00114 ANNbool inside(int dim, ANNpoint p);// is point p inside rectangle? 00115 }; 00116 00117 void annAssignRect( // assign one rect to another 00118 int dim, // dimension (both must be same) 00119 ANNorthRect &dest, // destination (modified) 00120 const ANNorthRect &source); // source 00121 00122 //---------------------------------------------------------------------- 00123 // Orthogonal (axis aligned) halfspace 00124 // An orthogonal halfspace is represented by an integer cutting 00125 // dimension cd, coordinate cutting value, cv, and side, sd, which is 00126 // either +1 or -1. Our convention is that point q lies in the (closed) 00127 // halfspace if (q[cd] - cv)*sd >= 0. 00128 //---------------------------------------------------------------------- 00129 00130 class ANNorthHalfSpace { 00131 public: 00132 int cd; // cutting dimension 00133 ANNcoord cv; // cutting value 00134 int sd; // which side 00135 // 00136 ANNorthHalfSpace() // default constructor 00137 { cd = 0; cv = 0; sd = 0; } 00138 00139 ANNorthHalfSpace( // basic constructor 00140 int cdd, // dimension of space 00141 ANNcoord cvv, // cutting value 00142 int sdd) // side 00143 { cd = cdd; cv = cvv; sd = sdd; } 00144 00145 ANNbool in(ANNpoint q) const // is q inside halfspace? 00146 { return (ANNbool) ((q[cd] - cv)*sd >= 0); } 00147 00148 ANNbool out(ANNpoint q) const // is q outside halfspace? 00149 { return (ANNbool) ((q[cd] - cv)*sd < 0); } 00150 00151 ANNdist dist(ANNpoint q) const // (squared) distance from q 00152 { return (ANNdist) ANN_POW(q[cd] - cv); } 00153 00154 void setLowerBound(int d, ANNpoint p)// set to lower bound at p[i] 00155 { cd = d; cv = p[d]; sd = +1; } 00156 00157 void setUpperBound(int d, ANNpoint p)// set to upper bound at p[i] 00158 { cd = d; cv = p[d]; sd = -1; } 00159 00160 void project(ANNpoint &q) // project q (modified) onto halfspace 00161 { if (out(q)) q[cd] = cv; } 00162 }; 00163 00164 // array of halfspaces 00165 typedef ANNorthHalfSpace *ANNorthHSArray; 00166 00167 #endif