2013-03-03 00:42:56 +01:00
|
|
|
/*+
|
2014-05-22 09:38:59 +02:00
|
|
|
This is CosmoTool (./src/kdtree_splitters.hpp) -- Copyright (C) Guilhem Lavaux (2007-2014)
|
2013-03-03 00:42:56 +01:00
|
|
|
|
|
|
|
guilhem.lavaux@gmail.com
|
|
|
|
|
|
|
|
This software is a computer program whose purpose is to provide a toolbox for cosmological
|
|
|
|
data analysis (e.g. filters, generalized Fourier transforms, power spectra, ...)
|
|
|
|
|
|
|
|
This software is governed by the CeCILL license under French law and
|
|
|
|
abiding by the rules of distribution of free software. You can use,
|
|
|
|
modify and/ or redistribute the software under the terms of the CeCILL
|
|
|
|
license as circulated by CEA, CNRS and INRIA at the following URL
|
|
|
|
"http://www.cecill.info".
|
|
|
|
|
|
|
|
As a counterpart to the access to the source code and rights to copy,
|
|
|
|
modify and redistribute granted by the license, users are provided only
|
|
|
|
with a limited warranty and the software's author, the holder of the
|
|
|
|
economic rights, and the successive licensors have only limited
|
|
|
|
liability.
|
|
|
|
|
|
|
|
In this respect, the user's attention is drawn to the risks associated
|
|
|
|
with loading, using, modifying and/or developing or reproducing the
|
|
|
|
software by the user in light of its specific status of free software,
|
|
|
|
that may mean that it is complicated to manipulate, and that also
|
|
|
|
therefore means that it is reserved for developers and experienced
|
|
|
|
professionals having in-depth computer knowledge. Users are therefore
|
|
|
|
encouraged to load and test the software's suitability as regards their
|
|
|
|
requirements in conditions enabling the security of their systems and/or
|
|
|
|
data to be ensured and, more generally, to use and operate it in the
|
|
|
|
same conditions as regards security.
|
|
|
|
|
|
|
|
The fact that you are presently reading this means that you have had
|
|
|
|
knowledge of the CeCILL license and that you accept its terms.
|
|
|
|
+*/
|
2013-03-06 04:39:33 +01:00
|
|
|
|
2012-05-20 15:22:51 +02:00
|
|
|
#ifndef __KDTREE_SPLITTERS_HPP
|
|
|
|
#define __KDTREE_SPLITTERS_HPP
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace CosmoTool
|
|
|
|
{
|
|
|
|
|
|
|
|
template<int N, typename ValType, typename CType = ComputePrecision>
|
|
|
|
struct KD_homogeneous_cell_splitter
|
|
|
|
{
|
|
|
|
typedef typename KDDef<N,CType>::KDCoordinates coords;
|
|
|
|
typedef typename KDDef<N,CType>::CoordType ctype;
|
|
|
|
|
2012-05-21 20:04:21 +02:00
|
|
|
|
2015-06-09 20:01:56 +02:00
|
|
|
void check_splitting(KDCell<N,ValType,CType> **cells, NodeIntType Ncells, int axis, NodeIntType split_index, ctype midCoord)
|
2012-05-21 20:04:21 +02:00
|
|
|
{
|
|
|
|
ctype delta = std::numeric_limits<ctype>::max();
|
|
|
|
assert(split_index < Ncells);
|
|
|
|
assert(axis < N);
|
2015-06-09 20:01:56 +02:00
|
|
|
for (NodeIntType i = 0; i < split_index; i++)
|
2012-05-21 20:04:21 +02:00
|
|
|
{
|
|
|
|
assert(cells[i]->coord[axis] <= midCoord);
|
|
|
|
delta = min(midCoord-cells[i]->coord[axis], delta);
|
|
|
|
}
|
2015-06-09 20:01:56 +02:00
|
|
|
for (NodeIntType i = split_index+1; i < Ncells; i++)
|
2012-05-21 20:04:21 +02:00
|
|
|
{
|
|
|
|
assert(cells[i]->coord[axis] > midCoord);
|
|
|
|
delta = min(cells[i]->coord[axis]-midCoord, delta);
|
|
|
|
}
|
|
|
|
assert(delta >= 0);
|
|
|
|
assert (std::abs(cells[split_index]->coord[axis]-midCoord) <= delta);
|
|
|
|
}
|
|
|
|
|
2015-06-09 20:01:56 +02:00
|
|
|
void operator()(KDCell<N,ValType,CType> **cells, NodeIntType Ncells, NodeIntType& split_index, int axis, coords minBound, coords maxBound)
|
2012-05-20 15:22:51 +02:00
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
if (Ncells == 1)
|
|
|
|
{
|
|
|
|
split_index = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-05-20 15:22:51 +02:00
|
|
|
ctype midCoord = 0.5*(maxBound[axis]+minBound[axis]);
|
2015-06-09 20:01:56 +02:00
|
|
|
NodeIntType below = 0, above = Ncells-1;
|
2012-05-21 20:04:21 +02:00
|
|
|
ctype delta_min = std::numeric_limits<ctype>::max();
|
2015-06-09 20:01:56 +02:00
|
|
|
NodeIntType idx_min = std::numeric_limits<NodeIntType>::max();
|
2012-05-20 15:22:51 +02:00
|
|
|
|
|
|
|
while (below < above)
|
|
|
|
{
|
|
|
|
ctype delta = cells[below]->coord[axis]-midCoord;
|
|
|
|
if (delta > 0)
|
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
if (delta < delta_min)
|
2012-05-20 15:22:51 +02:00
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
delta_min = delta;
|
|
|
|
idx_min = above;
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
std::swap(cells[below], cells[above--]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
if (-delta < delta_min)
|
2012-05-20 15:22:51 +02:00
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
delta_min = -delta;
|
|
|
|
idx_min = below;
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
below++;
|
|
|
|
}
|
|
|
|
}
|
2012-05-21 20:04:21 +02:00
|
|
|
// Last iteration
|
|
|
|
{
|
|
|
|
ctype delta = cells[below]->coord[axis]-midCoord;
|
|
|
|
if (delta > 0)
|
|
|
|
{
|
|
|
|
if (delta < delta_min)
|
|
|
|
{
|
|
|
|
delta_min = delta;
|
|
|
|
idx_min = above;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (-delta < delta_min)
|
|
|
|
{
|
|
|
|
delta_min = -delta;
|
|
|
|
idx_min = above;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (idx_min != above)
|
2012-05-20 15:22:51 +02:00
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
bool cond1 = cells[idx_min]->coord[axis] > midCoord;
|
2012-05-20 15:22:51 +02:00
|
|
|
bool cond2 = cells[above]->coord[axis] > midCoord;
|
|
|
|
if ((cond1 && cond2) || (!cond1 && !cond2))
|
|
|
|
{
|
|
|
|
split_index = above;
|
2012-05-21 20:04:21 +02:00
|
|
|
std::swap(cells[above], cells[idx_min]);
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
else if (cond2)
|
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
if (above >= 1)
|
|
|
|
{
|
|
|
|
split_index = above-1;
|
|
|
|
std::swap(cells[above-1], cells[idx_min]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
split_index = 0;
|
|
|
|
assert(split_index >= 0);
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-21 20:04:21 +02:00
|
|
|
if (above+1 < Ncells)
|
|
|
|
{
|
|
|
|
split_index = above+1;
|
|
|
|
std::swap(cells[above+1], cells[idx_min]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
split_index = Ncells-1;
|
|
|
|
|
|
|
|
assert(split_index < Ncells);
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else split_index = above;
|
2012-05-21 20:04:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
// check_splitting(cells, Ncells, axis, split_index, midCoord);
|
2012-05-20 15:22:51 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|