ANNx.h
上传用户:chinafayin
上传日期:2022-04-05
资源大小:153k
文件大小:6k
源码类别:

并行计算

开发平台:

Visual C++

  1. //----------------------------------------------------------------------
  2. // File: ANNx.h
  3. // Programmer:  Sunil Arya and David Mount
  4. // Last modified: 03/04/98 (Release 0.1)
  5. // Description: Internal include file for ANN
  6. //
  7. // These declarations are of use in manipulating some of
  8. // the internal data objects appearing in ANN, but are not
  9. // needed for applications just using the nearest neighbor
  10. // search.
  11. //
  12. // Typical users of ANN should not need to access this file.
  13. //----------------------------------------------------------------------
  14. // Copyright (c) 1997-2005 University of Maryland and Sunil Arya and
  15. // David Mount.  All Rights Reserved.
  16. // 
  17. // This software and related documentation is part of the Approximate
  18. // Nearest Neighbor Library (ANN).  This software is provided under
  19. // the provisions of the Lesser GNU Public License (LGPL).  See the
  20. // file ../ReadMe.txt for further information.
  21. // 
  22. // The University of Maryland (U.M.) and the authors make no
  23. // representations about the suitability or fitness of this software for
  24. // any purpose.  It is provided "as is" without express or implied
  25. // warranty.
  26. //----------------------------------------------------------------------
  27. // History:
  28. // Revision 0.1  03/04/98
  29. //     Initial release
  30. // Revision 1.0  04/01/05
  31. //     Changed LO, HI, IN, OUT to ANN_LO, ANN_HI, etc.
  32. //----------------------------------------------------------------------
  33. #ifndef ANNx_H
  34. #define ANNx_H
  35. #include <iomanip> // I/O manipulators
  36. #include <cstring>
  37. #include <cstdlib>
  38. #include "ANN.h" // ANN includes
  39. //----------------------------------------------------------------------
  40. // Global constants and types
  41. //----------------------------------------------------------------------
  42. enum {ANN_LO=0, ANN_HI=1}; // splitting indices
  43. enum {ANN_IN=0, ANN_OUT=1}; // shrinking indices
  44. // what to do in case of error
  45. enum ANNerr {ANNwarn = 0, ANNabort = 1};
  46. //----------------------------------------------------------------------
  47. // Maximum number of points to visit
  48. // We have an option for terminating the search early if the
  49. // number of points visited exceeds some threshold.  If the
  50. // threshold is 0 (its default)  this means there is no limit
  51. // and the algorithm applies its normal termination condition.
  52. //----------------------------------------------------------------------
  53. extern ANN_THREAD_LOCAL int ANNmaxPtsVisited; // maximum number of pts visited
  54. extern ANN_THREAD_LOCAL int ANNptsVisited; // number of pts visited in search
  55. //----------------------------------------------------------------------
  56. // Global function declarations
  57. //----------------------------------------------------------------------
  58. void annError( // ANN error routine
  59. char *msg, // error message
  60. ANNerr level); // level of error
  61. void annPrintPt( // print a point
  62. ANNpoint pt, // the point
  63. int dim, // the dimension
  64. std::ostream &out); // output stream
  65. //----------------------------------------------------------------------
  66. // Orthogonal (axis aligned) rectangle
  67. // Orthogonal rectangles are represented by two points, one
  68. // for the lower left corner (min coordinates) and the other
  69. // for the upper right corner (max coordinates).
  70. //
  71. // The constructor initializes from either a pair of coordinates,
  72. // pair of points, or another rectangle.  Note that all constructors
  73. // allocate new point storage. The destructor deallocates this
  74. // storage.
  75. //
  76. // BEWARE: Orthogonal rectangles should be passed ONLY BY REFERENCE.
  77. // (C++'s default copy constructor will not allocate new point
  78. // storage, then on return the destructor free's storage, and then
  79. // you get into big trouble in the calling procedure.)
  80. //----------------------------------------------------------------------
  81. class ANNorthRect {
  82. public:
  83. ANNpoint lo; // rectangle lower bounds
  84. ANNpoint hi; // rectangle upper bounds
  85. //
  86. ANNorthRect( // basic constructor
  87. int dd, // dimension of space
  88. ANNcoord l=0, // default is empty
  89. ANNcoord h=0)
  90. {  lo = annAllocPt(dd, l);  hi = annAllocPt(dd, h); }
  91. ANNorthRect( // (almost a) copy constructor
  92. int dd, // dimension
  93. const ANNorthRect &r) // rectangle to copy
  94. {  lo = annCopyPt(dd, r.lo);  hi = annCopyPt(dd, r.hi);  }
  95. ANNorthRect( // construct from points
  96. int dd, // dimension
  97. ANNpoint l, // low point
  98. ANNpoint h) // hight point
  99. {  lo = annCopyPt(dd, l);  hi = annCopyPt(dd, h);  }
  100. ~ANNorthRect() // destructor
  101.     {  annDeallocPt(lo);  annDeallocPt(hi);  }
  102. ANNbool inside(int dim, ANNpoint p);// is point p inside rectangle?
  103. };
  104. void annAssignRect( // assign one rect to another
  105. int dim, // dimension (both must be same)
  106. ANNorthRect &dest, // destination (modified)
  107. const ANNorthRect &source); // source
  108. //----------------------------------------------------------------------
  109. // Orthogonal (axis aligned) halfspace
  110. // An orthogonal halfspace is represented by an integer cutting
  111. // dimension cd, coordinate cutting value, cv, and side, sd, which is
  112. // either +1 or -1. Our convention is that point q lies in the (closed)
  113. // halfspace if (q[cd] - cv)*sd >= 0.
  114. //----------------------------------------------------------------------
  115. class ANNorthHalfSpace {
  116. public:
  117. int cd; // cutting dimension
  118. ANNcoord cv; // cutting value
  119. int sd; // which side
  120. //
  121. ANNorthHalfSpace() // default constructor
  122. {  cd = 0; cv = 0;  sd = 0;  }
  123. ANNorthHalfSpace( // basic constructor
  124. int cdd, // dimension of space
  125. ANNcoord cvv, // cutting value
  126. int sdd) // side
  127. {  cd = cdd;  cv = cvv;  sd = sdd;  }
  128. ANNbool in(ANNpoint q) const // is q inside halfspace?
  129. {  return  (ANNbool) ((q[cd] - cv)*sd >= 0);  }
  130. ANNbool out(ANNpoint q) const // is q outside halfspace?
  131. {  return  (ANNbool) ((q[cd] - cv)*sd < 0);  }
  132. ANNdist dist(ANNpoint q) const // (squared) distance from q
  133. {  return  (ANNdist) ANN_POW(q[cd] - cv);  }
  134. void setLowerBound(int d, ANNpoint p)// set to lower bound at p[i]
  135. {  cd = d;  cv = p[d];  sd = +1;  }
  136. void setUpperBound(int d, ANNpoint p)// set to upper bound at p[i]
  137. {  cd = d;  cv = p[d];  sd = -1;  }
  138. void project(ANNpoint &q) // project q (modified) onto halfspace
  139. {  if (out(q)) q[cd] = cv;  }
  140. };
  141. // array of halfspaces
  142. typedef ANNorthHalfSpace *ANNorthHSArray;
  143. #endif