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