6 template<
class Po
int,
class Base >
9 memset(_children, 0,
sizeof(_children));
12 template<
class Po
int,
class Base >
15 , _splitValue(cell._splitValue)
17 if (cell._children[0])
25 if (cell._children[1])
27 _children[1] =
new AAKdCell< Point, Base >(*cell._children[1]);
35 template<
class Po
int,
class Base >
38 for (
unsigned int i = 0; i < 2; ++i)
45 template<
class Po
int,
class Base >
49 return _children[
index];
52 template<
class Po
int,
class Base >
56 return _children[
index];
59 template<
class Po
int,
class Base >
62 _children[
index] = child;
65 template<
class Po
int,
class Base >
72 template<
class Po
int,
class Base >
78 template<
class Po
int,
class Base >
84 template<
class Po
int,
class Base >
92 template<
class Strategies >
101 std::list< CellType* > stack;
102 stack.push_back(root);
107 if (ShouldSubdivide(
c))
114 for (
unsigned int i = 0; i < CellType::NChildren; ++i)
116 stack.push_back((*
c)[i]);
122 template<
class Strategies >
124 ScalarType radius, std::vector< size_t >* points)
const
131 PointsInSphere(Root(), center, radius, points);
134 template<
class Strategies >
137 std::vector< size_t >* points)
const
146 PointsInAACube(Root(), center, cube.
Width() / 2, points);
149 template<
class Strategies >
151 ScalarType width, std::vector< size_t >* points)
const
158 PointsInAACube(Root(), center, width / 2, points);
161 template<
class Strategies >
173 std::list< CellType* > stack;
181 if (!ShouldSubdivide(
c))
183 for (
unsigned int i = 0; i < CellType::NChildren; ++i)
192 for (
unsigned int i = 0; i < CellType::NChildren; ++i)
194 stack.push_back((*
c)[i]);
198 else if (ShouldSubdivide(
c))
201 for (
unsigned int i = 0; i < CellType::NChildren; ++i)
203 stack.push_back((*
c)[i]);
207 StrategyBaseType::RefreshWithNewTreeData(bc);
210 template<
class Strategies >
214 *dist = std::numeric_limits< ScalarType >::infinity();
222 if (*dist < std::numeric_limits< ScalarType >::infinity())
228 template<
class Strategies >
230 unsigned int k, std::vector< NN >* neighbors,
233 *dist = std::numeric_limits< ScalarType >::infinity();
242 neighbors->reserve(k + 1);
243 KNearestNeighbors(*Root(), rootBox, p, k, neighbors, &worstIdx, dist);
244 if (*dist < std::numeric_limits< ScalarType >::infinity())
250 template<
class Strategies >
253 size_t* neighbor, ScalarType* dist2)
const
258 for (
size_t i = 0; i < cell.Size(); ++i)
260 size_t idx = Translate(cell.Points() + i);
261 PointType diff = PointTranslated(idx) - p;
262 ScalarType d2 = diff * diff;
272 unsigned int nearerChild, fartherChild;
273 if (p[cell.Axis()] <= cell.Split())
283 AABox< PointType > subBoxes[2];
284 box.
Split(cell.Axis(), cell.Split(), &subBoxes[0], &subBoxes[1]);
285 NearestNeighbor(*cell[nearerChild], subBoxes[nearerChild],
289 if (Intersect(subBoxes[fartherChild], p, *dist2))
290 NearestNeighbor(*cell[fartherChild], subBoxes[fartherChild],
294 template<
class Strategies >
296 const AABox< PointType >& box,
const PointType& p,
unsigned int k,
297 std::vector< NN >* neighbors,
size_t* worstIdx,
298 ScalarType* dist2)
const
303 for (
size_t i = 0; i < cell.Size(); ++i)
305 size_t idx = Translate(cell.Points() + i);
306 PointType diff = PointTranslated(idx) - p;
307 ScalarType d2 = diff * diff;
308 if (neighbors->size() < k)
310 neighbors->push_back(NN(idx, d2));
314 else if (d2 < *dist2)
316 (*neighbors)[*worstIdx] = NN(idx, d2);
324 unsigned int nearerChild, fartherChild;
325 if (p[cell.Axis()] <= cell.Split())
335 AABox< PointType > subBoxes[2];
336 box.Split(cell.Axis(), cell.Split(), &subBoxes[0], &subBoxes[1]);
338 KNearestNeighbors(*cell[nearerChild], subBoxes[nearerChild], p, k,
339 neighbors, worstIdx, dist2);
342 if (neighbors->size() < k || Intersect(subBoxes[fartherChild],
344 KNearestNeighbors(*cell[fartherChild], subBoxes[fartherChild],
345 p, k, neighbors, worstIdx, dist2);
348 template<
class Strategies >
349 void AAKdTree< Strategies >::ReadjustData(CellType* cell)
356 pmax = pmin = PointTranslated(Translate(cell->Points()));
357 for (
size_t i = 1; i < cell->Size(); ++i)
360 PointTranslated(Translate(cell->Points() + i));
361 for (
unsigned int j = 0; j < Dim; ++j)
367 else if (pmax[j] < p[j])
375 unsigned int axis = 0;
376 ScalarType length = pdiff[0];
377 for (
unsigned int j = 1; j < Dim; ++j)
379 if (pdiff[j] > length)
386 ScalarType
split = (pmax[axis] + pmin[axis]) / 2;
388 CellType* left = (*cell)[0],
390 SplitAlongAxis(*cell, axis,
split, left, right);
395 template<
class Strategies >
397 const PointType& center, ScalarType radius,
398 std::vector< size_t >* points)
const
402 ScalarType radius2 = radius * radius;
403 for (
size_t i = 0; i < cell->Size(); ++i)
405 size_t ind = Translate(cell->Points() + i);
406 const PointType& p = PointTranslated(ind);
408 if ((d * d) <= radius2)
410 points->push_back(ind);
416 ScalarType d = center[cell->Axis()] - cell->Split();
419 PointsInSphere((*cell)[1], center, radius, points);
423 PointsInSphere((*cell)[0], center, radius, points);
428 template<
class Strategies >
430 const PointType& center, ScalarType radius,
431 std::vector< HandleType >* points)
const
435 for (
size_t i = 0; i < cell->Size(); ++i)
437 size_t ind = Translate(cell->Points() + i);
438 const PointType& p = PointTranslated(ind);
440 bool isInside =
true;
441 for (
unsigned int j = 0; j < PointType::Dim; ++j)
451 points->push_back(ind);
457 ScalarType d = center[cell->Axis()] - cell->Split();
460 PointsInAACube((*cell)[1], center, radius, points);
464 PointsInAACube((*cell)[0], center, radius, points);
469 template<
class Strategies >
470 bool AAKdTree< Strategies >::ShouldSubdivide(
const CellType* cell)
const
472 if (cell->Size() > 10)
479 template<
class Strategies >
480 void AAKdTree< Strategies >::Subdivide(CellType* cell)
483 pmax = pmin = PointTranslated(Translate(cell->Points()));
484 for (
size_t i = 1; i < cell->Size(); ++i)
487 PointTranslated(Translate(cell->Points() + i));
488 for (
unsigned int j = 0; j < Dim; ++j)
494 else if (pmax[j] < p[j])
502 unsigned int axis = 0;
503 ScalarType length = pdiff[0];
504 for (
unsigned int j = 1; j < Dim; ++j)
506 if (pdiff[j] > length)
513 ScalarType
split = (pmax[axis] + pmin[axis]) / 2;
515 CellType* left, *right;
517 right =
new CellType;
518 SplitAlongAxis(*cell, axis,
split, left, right);
520 if (left->Size() == cell->Size() || right->Size() == cell->Size())
528 cell->Child(0, left);
529 cell->Child(1, right);
532 template<
class Strategies >
534 AAKdTree< Strategies >::Absolute(ScalarType f)
const