00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "rtc/rtcPoint3DKdTree.h"
00022
00023
00024
00025 namespace rtc {
00026
00027 Point3DKdTree::Point3DKdTree(float* x, float* y, float* z, int num_points)
00028 {
00029
00030 dataPts = annAllocPts(num_points,3);
00031 for(int i = 0; i<num_points; ++i) {
00032 dataPts[i][0] = x[i];
00033 dataPts[i][1] = y[i];
00034 dataPts[i][2] = z[i];
00035 }
00036
00037
00038 ann_tree = new ANNkd_tree(dataPts, num_points, 3);
00039 }
00040
00041 Point3DKdTree::~Point3DKdTree()
00042 {
00043 delete ann_tree;
00044 annDeallocPts(dataPts);
00045 annClose();
00046 }
00047
00048 int Point3DKdTree::findNearest(const Vec3f& p)
00049 {
00050 return findNearest(p(0),p(1),p(2));
00051 }
00052
00053 int Point3DKdTree::findNearest(float x, float y, float z)
00054 {
00055
00056 ANNpoint queryPt = annAllocPt(3);
00057 queryPt[0] = x;
00058 queryPt[1] = y;
00059 queryPt[2] = z;
00060
00061 ANNidx ann_index;
00062 ANNdist dist;
00063
00064 ann_tree->annkSearch(queryPt, 1, &ann_index, &dist, 0.0);
00065
00066 annDeallocPt(queryPt);
00067
00068 return ann_index;
00069 }
00070
00071
00072 void Point3DKdTree::findNearest(float x, float y, float z, int k, int* points)
00073 {
00074
00075 ANNpoint queryPt=annAllocPt(3);
00076 queryPt[0] = x;
00077 queryPt[1] = y;
00078 queryPt[2] = z;
00079
00080 ANNdist* dist = new ANNdist[k];
00081
00082 ann_tree->annkSearch(queryPt, k, points, dist, 0.0);
00083
00084 annDeallocPt(queryPt);
00085
00086 delete [] dist;
00087 }
00088
00089
00090 void Point3DKdTree::findNearest(float x, float y, float z, int k, std::vector<int>& points)
00091 {
00092 points.clear();
00093
00094 ANNpoint queryPt=annAllocPt(3);
00095 queryPt[0] = x;
00096 queryPt[1] = y;
00097 queryPt[2] = z;
00098
00099
00100 ANNidx* ann_index = new ANNidx[k];
00101 ANNdist* dist = new ANNdist[k];
00102
00103 ann_tree->annkSearch(queryPt, k, ann_index, dist, 0.0);
00104
00105 for(int i=0;i<k; i++) points.push_back(ann_index[i]);
00106
00107 annDeallocPt(queryPt);
00108
00109 delete [] ann_index;
00110 delete [] dist;
00111 }
00112
00113 void Point3DKdTree::findNearest(const Vec3f& p, int k, int* points)
00114 {
00115 findNearest(p(0),p(1),p(2),k,points);
00116 }
00117
00118 void Point3DKdTree::findNearest(const Vec3f& p, int k, std::vector<int>& points)
00119 {
00120 findNearest(p(0),p(1),p(2),k,points);
00121 }
00122
00123 int Point3DKdTree::findWithinRange(float x, float y, float z, float range, std::vector<int>& points)
00124 {
00125 points.clear();
00126
00127 ANNpoint queryPt=annAllocPt(3);
00128 queryPt[0] = x;
00129 queryPt[1] = y;
00130 queryPt[2] = z;
00131
00132
00133 int k = ann_tree->annkFRSearch(queryPt,rtc_sqr(range),0,NULL,NULL,0.0);
00134
00135 ANNidx *index=new ANNidx[k];
00136 ANNdist *dist=new ANNdist[k];
00137
00138 k = ann_tree->annkFRSearch(queryPt,rtc_sqr(range),k,index,dist,0.0);
00139
00140 for(int i=0;i<k;++i)
00141 points.push_back(index[i]);
00142
00143 annDeallocPt(queryPt);
00144 delete[] dist;
00145 delete[] index;
00146 return k;
00147 }
00148
00149 int Point3DKdTree::findWithinRange(const Vec3f& p, float range, std::vector<int>& points)
00150 {
00151 return findWithinRange(p[0],p[1],p[2],range,points);
00152 }
00153
00154 void Point3DKdTree::findNearest(float* ann_sample, ANNidx* i,ANNdist* d)
00155 {
00156 ANNpoint queryPt=annAllocPt(3);
00157 queryPt[0] = ann_sample[0];
00158 queryPt[1] = ann_sample[1];
00159 queryPt[2] = ann_sample[2];
00160 ann_tree->annkSearch(queryPt, 1, i, d, 0.0);
00161
00162 annDeallocPt(queryPt);
00163 }
00164
00165
00166 }
00167