2013-03-03 00:42:56 +01:00
|
|
|
/*+
|
2014-05-22 09:38:59 +02:00
|
|
|
This is CosmoTool (./src/octTree.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
|
|
|
|
2009-11-01 18:20:50 +01:00
|
|
|
#ifndef __COSMOTOOL_AMR_HPP
|
|
|
|
#define __COSMOTOOL_AMR_HPP
|
|
|
|
|
|
|
|
#include "config.hpp"
|
|
|
|
|
|
|
|
namespace CosmoTool
|
|
|
|
{
|
|
|
|
|
|
|
|
typedef uint32_t octPtr;
|
|
|
|
typedef uint16_t octCoordType;
|
|
|
|
static const uint16_t octCoordTypeNorm = 0xffff;
|
|
|
|
static const uint16_t octCoordCenter = 0x8000;
|
|
|
|
|
|
|
|
// This is also the root cell, but this one
|
|
|
|
// is never referenced (really ??).
|
|
|
|
static const octPtr invalidOctCell = 0xffffffff;
|
|
|
|
static const octPtr emptyOctCell = 0;
|
|
|
|
static const octPtr octParticleMarker = 0x80000000;
|
|
|
|
static const octPtr octParticleMask = 0x7fffffff;
|
|
|
|
|
|
|
|
typedef octCoordType OctCoords[3];
|
|
|
|
|
2015-01-26 18:16:43 +01:00
|
|
|
template<class T = void>
|
2009-11-01 18:20:50 +01:00
|
|
|
struct OctCell
|
|
|
|
{
|
|
|
|
octPtr numberLeaves;
|
|
|
|
octPtr children[8];
|
2015-01-26 18:16:43 +01:00
|
|
|
T data;
|
2009-11-01 18:20:50 +01:00
|
|
|
};
|
|
|
|
|
2015-01-26 18:16:43 +01:00
|
|
|
template<typename T>
|
|
|
|
struct OctTree_defaultUpdater {
|
|
|
|
void operator()(T& d) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T_dataUpdater = OctTree_defaultUpdater<void>, class T = void>
|
2009-11-01 18:20:50 +01:00
|
|
|
class OctTree
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
OctTree(const FCoordinates *particles, octPtr numParticles,
|
|
|
|
uint32_t maxTreeDepth, uint32_t maxAbsoluteDepth,
|
|
|
|
uint32_t threshold = 1);
|
|
|
|
~OctTree();
|
|
|
|
|
|
|
|
void buildTree(uint32_t maxAbsoluteDepth);
|
|
|
|
void insertParticle(octPtr node,
|
|
|
|
const OctCoords& icoord,
|
|
|
|
octCoordType halfNodeLength,
|
|
|
|
octPtr particleId,
|
|
|
|
uint32_t maxAbsoluteDepth);
|
|
|
|
|
|
|
|
|
|
|
|
octPtr getNumberLeaves() const {
|
|
|
|
return cells[0].numberLeaves;
|
|
|
|
}
|
|
|
|
|
2009-11-27 18:06:04 +01:00
|
|
|
static bool unconditioned(const FCoordinates&, octPtr, float, bool)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-11-01 18:20:50 +01:00
|
|
|
template<typename FunT>
|
|
|
|
void walkTree(FunT f)
|
2009-11-07 05:05:00 +01:00
|
|
|
{
|
|
|
|
walkTree(f, unconditioned);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename FunT, typename CondT>
|
|
|
|
void walkTree(FunT f, CondT condition)
|
2009-11-01 18:20:50 +01:00
|
|
|
{
|
|
|
|
OctCoords rootCenter = { octCoordCenter, octCoordCenter, octCoordCenter };
|
|
|
|
|
2009-11-07 05:05:00 +01:00
|
|
|
walkTreeElements(f, condition, 0, rootCenter, octCoordCenter);
|
2009-11-01 18:20:50 +01:00
|
|
|
}
|
|
|
|
|
2009-12-07 15:46:59 +01:00
|
|
|
|
|
|
|
|
2009-11-01 18:20:50 +01:00
|
|
|
protected:
|
2015-01-26 18:16:43 +01:00
|
|
|
T_dataUpdater updater;
|
2009-11-01 18:20:50 +01:00
|
|
|
const FCoordinates *particles;
|
|
|
|
octPtr numParticles;
|
2015-01-26 18:16:43 +01:00
|
|
|
OctCell<T> *cells;
|
2009-11-01 18:20:50 +01:00
|
|
|
float Lbox;
|
|
|
|
octPtr lastNode;
|
|
|
|
octPtr numCells;
|
2009-11-02 01:56:10 +01:00
|
|
|
float lenNorm;
|
|
|
|
float xMin[3];
|
2009-11-01 18:20:50 +01:00
|
|
|
|
|
|
|
|
2009-11-07 05:05:00 +01:00
|
|
|
static bool unconditioned()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename FunT, typename CondT>
|
|
|
|
void walkTreeElements(FunT f, CondT condition,
|
|
|
|
octPtr node,
|
2009-11-01 18:20:50 +01:00
|
|
|
const OctCoords& icoord,
|
|
|
|
octCoordType halfNodeLength)
|
|
|
|
{
|
|
|
|
OctCoords newCoord;
|
2009-11-02 01:56:10 +01:00
|
|
|
FCoordinates center, realCenter;
|
2009-11-01 18:20:50 +01:00
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++)
|
2015-01-26 18:16:43 +01:00
|
|
|
{
|
|
|
|
center[j] = icoord[j]/(2.*octCoordCenter);
|
|
|
|
realCenter[j] = xMin[j] + center[j]*lenNorm;
|
|
|
|
}
|
2009-11-27 18:06:04 +01:00
|
|
|
|
2009-11-02 14:27:00 +01:00
|
|
|
f(realCenter, cells[node].numberLeaves, lenNorm*halfNodeLength/(float)octCoordCenter,
|
2015-01-26 18:16:43 +01:00
|
|
|
cells[node].children[0] == invalidOctCell, // True if this is a meta-node
|
|
|
|
false);
|
2009-11-27 18:06:04 +01:00
|
|
|
|
|
|
|
if (!condition(realCenter, cells[node].numberLeaves,
|
|
|
|
lenNorm*halfNodeLength/(float)octCoordCenter,
|
|
|
|
cells[node].children[0] == invalidOctCell))
|
2015-01-26 18:16:43 +01:00
|
|
|
return;
|
2009-11-27 18:06:04 +01:00
|
|
|
|
2009-11-01 18:20:50 +01:00
|
|
|
for (int i = 0; i < 8; i++)
|
2015-01-26 18:16:43 +01:00
|
|
|
{
|
|
|
|
octPtr newNode = cells[node].children[i];
|
|
|
|
int ipos[3] = { (i&1), (i&2)>>1, (i&4)>>2 };
|
|
|
|
|
|
|
|
if (newNode == emptyOctCell || newNode == invalidOctCell)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
newCoord[j] = icoord[j]+(2*ipos[j]-1)*halfNodeLength/2;
|
|
|
|
|
|
|
|
if (newNode & octParticleMarker)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < 3; j++)
|
|
|
|
{
|
|
|
|
center[j] = newCoord[j]/(2.*octCoordCenter);
|
|
|
|
realCenter[j] = xMin[j] + lenNorm*center[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
f(realCenter,
|
|
|
|
1, lenNorm*halfNodeLength/(2.*octCoordCenter),
|
|
|
|
false, true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
walkTreeElements(f, condition, cells[node].children[i], newCoord, halfNodeLength/2);
|
|
|
|
}
|
2009-11-01 18:20:50 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|