Commit 4fb8f536 authored by berenger-bramas's avatar berenger-bramas

ScalFMM Octree & Utils Sources

git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/scalfmm/scalfmm/trunk@3 2616d619-271b-44dc-8df4-d4a8f33a7222
parent 2b8d584a
#ignore building directory
build/
#ignore generated doc
Doc/html/
#ignore qt project file
*.pro
*.pro.*
#ignore executables
*.exe
#ignore temporary files
tmp/
*.tmp
*~
*TAGS
*#*#
This diff is collapsed.
#ifndef FLIST_HPP
#define FLIST_HPP
// /!\ Please, you must read the license at the bottom of this page
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FList
* Please read the license
*
* This class is a linked list container.
*
* Please refere to unit test flistUTest.cpp
*/
template< class Object >
class FList {
/** A list node */
struct Node {
Object target; //< Object of the node
Node* next; //< Next node
};
Node* root; //< Root node, NULL if size is 0
int size; //< Elements in the list
/**
* Copy a list into current object
* The current list has to be empty when this function is called
*/
void copy(const FList& other){
const Node* otherRoot = other.root;
Node ** myRoot = &this->root;
while(otherRoot){
(*myRoot) = new Node;
(*myRoot)->target = otherRoot->target;
myRoot = &(*myRoot)->next;
otherRoot = otherRoot->next;
}
*myRoot = 0;
this->size = other.size;
}
public:
/** Constructor (of an empty list) */
FList() : root(0) , size(0) {
}
/** Desctructor */
virtual ~FList(){
clear();
}
/**
* Copy operator
* This will clear the current list before copying
* @param other the source list
* @return the current list as a reference
*/
FList& operator=(const FList& other){
clear();
copy(other);
return *this;
}
/**
* Copy constructor
* @param other the source list
*/
FList(const FList& other): root(0) , size(0) {
copy(other);
}
/**
* To clear the list
* Size is 0 after calling this function
*/
void clear(){
while(this->root){
Node* next(this->root->next);
delete this->root;
this->root = next;
}
this->size = 0;
}
/**
* Push an element in the front of the list
*/
void pushFront(Object& inObject){
Node* newNode = new Node;
newNode->target = inObject;
newNode->next = this->root;
this->root = newNode;
++size;
}
/**
* Push an element in the front of the list
* used when type is pointer and like [TYPE* const] object
*/
void pushFront(Object const inObject){
Node* newNode = new Node;
newNode->target = inObject;
newNode->next = this->root;
this->root = newNode;
++size;
}
/**
* To get front value (last pushed value)
* if size == 0 then defaultValue is returned
* @param defaultValue as the returned value in case size == 0, equal Object() if no param as passed
* @return first value if exists or defaultValue otherwise
*/
Object& front(Object& defaultValue = Object()){
if(size) return this->root->target;
else return defaultValue;
}
/**
* To get front value as const
* if size == 0 then defaultValue is return
* @param defaultValue as the returned value in case size == 0, equal Object() if no param as passed
* @return first value if exists or defaultValue otherwise
*/
const Object& front(const Object& defaultValue = Object()) const {
if(size) return this->root->target;
else return defaultValue;
}
/**
* To get the front value and remove it from the list
* @return first value
* @warning you must check the list's size before calling this function!
*/
Object popFront(){
--size;
Node* newNode = this->root;
this->root = this->root->next;
Object value = newNode->target;
delete newNode;
return value;
}
/**
* To get the number of elements in the list
* @return size
*/
int getSize() const{
return size;
}
/**
* This iterator allow accessing list's elements
* If you change the target list pointed by an iterator
* you cannot used the iterator any more.
* <code>
* FList<int> values; <br>
* // inserting ... <br>
* FList<int>::BasicIterator iter(values); <br>
* while( iter.isValide() ){ <br>
* iter.value() += 1; <br>
* iter.progress(); <br>
* } <br>
* </code>
*/
class BasicIterator {
private:
Node* iter; //< current node on the list
public:
/**
* Constructor needs the target list
* @param the list to iterate on
*/
BasicIterator(FList& list) : iter(list.root){
}
/** to progress on the list */
void progress(){
if(iter) iter = iter->next;
}
/**
* Current pointed value
* current iterator must be valide (isValide()) to use this function
*/
Object& value(){
return iter->target;
}
/**
* Current pointed value
* current iterator must be valide (isValide()) to use this function
*/
const Object& value() const{
return iter->target;
}
/**
* To know if an iterator is at the end of the list
* @return true if the current iterator can progress and access to value, else false
*/
bool isValide() const{
return iter;
}
};
};
#endif //FLIST_HPP
// [--LICENSE--]
This diff is collapsed.
This diff is collapsed.
#ifndef FTREECOORDINATE_HPP
#define FTREECOORDINATE_HPP
// /!\ Please, you must read the license at the bottom of this page
#include <cstring>
/** Morton index */
typedef long long MortonIndex;
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FTreeCoordinate
* Please read the license
*
* This class represents tree coordinate. It is used to save
* the position in "box unit" (not system unit!).
*/
class FTreeCoordinate{
private:
long x; //< x box-th position
long y; //< y box-th position
long z; //< z box-th position
public:
/** Default constructor */
FTreeCoordinate() : x(0), y(0), z(0){
}
/**
* Default constructor
* @param inX the x
* @param inY the y
* @param inZ the z
*/
FTreeCoordinate(const long inX,const long inY,const long inZ)
: x(inX), y(inY), z(inZ){
}
/** Default destructor */
virtual ~FTreeCoordinate(){
}
/**
* Copy constructor
* @param other the source class to copy
*/
FTreeCoordinate(const FTreeCoordinate& other):x(other.x), y(other.y), z(other.z){
}
/**
* Copy constructor
* @param other the source class to copy
* @return this a reference to the current object
*/
FTreeCoordinate& operator=(const FTreeCoordinate& other){
memcpy(this, &other, sizeof(other));
return *this;
}
/**
* Position setter
* @param inX the new x
* @param inY the new y
* @param inZ the new z
*/
void setPosition(const long inX,const long inY,const long inZ){
this->x = inX;
this->y = inY;
this->z = inZ;
}
/**
* X Getter
* @return this->x
*/
long getX() const{
return this->x;
}
/**
* Y Getter
* @return this->y
*/
long getY() const{
return this->y;
}
/**
* Z Getter
* @return this->z
*/
long getZ() const{
return this->z;
}
/**
* X Setter, simply change x position
* @param the new x
*/
void setX(const long inX){
this->x = inX;
}
/**
* Y Setter, simply change y position
* @param the new y
*/
void setY(const long inY){
this->y = inY;
}
/**
* Z Setter, simply change z position
* @param the new z
*/
void setZ(const long inZ){
this->z = inZ;
}
/**
* To get the morton index of the current position
* @complexity inLevel
* @param inLevel the level of the component
* @return morton index
*/
MortonIndex getMortonIndex(const int inLevel) const{
MortonIndex index = 0x0LL;
MortonIndex mask = 0x1LL;
MortonIndex mx = this->x << 2;
MortonIndex my = this->y << 1;
MortonIndex mz = this->z;
for(int indexLevel = 0; indexLevel < inLevel ; ++indexLevel){
index |= (mz & mask);
mask <<= 1;
index |= (my & mask);
mask <<= 1;
index |= (mx & mask);
mask <<= 1;
mz <<= 2;
my <<= 2;
mx <<= 2;
}
return index;
}
};
#endif //FTREECOORDINATE_HPP
// [--LICENSE--]
#ifndef FABSTRACTPARTICULE_HPP
#define FABSTRACTPARTICULE_HPP
// /!\ Please, you must read the license at the bottom of this page
/* forward declaration to avoid include */
class F3DPosition;
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FAbstractBody
* @brief
* Please read the license
*
* This class define the method that every particule class
* has to implement.
* @warning Inherite from this class when implement a specific particule type
*/
class FAbstractParticule{
public:
/** Default destructor */
virtual ~FAbstractParticule(){
}
/**
* Must be implemented by each user Particule class
* @return the position of the current cell
*/
virtual F3DPosition getPosition() const = 0;
};
#endif //FABSTRACTPARTICULE_HPP
// [--LICENSE--]
#ifndef FBASICPARTICULE_HPP
#define FBASICPARTICULE_HPP
// /!\ Please, you must read the license at the bottom of this page
#include "FAbstractParticule.hpp"
#include "../Utils/F3DPosition.hpp"
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FBasicParticule
* Please read the license
*
* This class defines a basic particule used for examples.
*/
class FBasicParticule : public FAbstractParticule{
protected:
F3DPosition pos; //< Particule's position
public:
/**
* Constructor with a position
* @param inX x position
* @param inY y position
* @param inZ z position
*/
FBasicParticule(const double inX, const double inY, const double inZ) : pos(inX,inY,inZ) {
}
/**
* Constructor with a position object
* @param inPos particule position
*/
FBasicParticule(const F3DPosition& inPos) : pos(inPos) {
}
/** Default constructor */
FBasicParticule(){
}
/** Default destructor */
virtual ~FBasicParticule(){
}
/**
* From the FAbstractParticule definition
* @return the position of the current cell
*/
F3DPosition getPosition() const {
return pos;
}
/**
* This function is needed by the basic loader to fill the current particule
* @param inPos the position given by the basic loader
*/
void setPosition(const F3DPosition& inPos) {
pos = inPos;
}
};
#endif //FBASICPARTICULE_HPP
// [--LICENSE--]
#ifndef FABSTRACTLOADER_HPP
#define FABSTRACTLOADER_HPP
// /!\ Please, you must read the license at the bottom of this page
class F3DPosition;
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FAbstractLoader
* Please read the license
*
* This class defined the FMB usual loader. A loader is the component
* that fills an octree.
*
* If you want to use a specific file format you then need to inherite from this loader
* and implemente several methods.
*
* @warning Inherite from this class when defining a loader class
*/
template <class ParticuleClass>
class FAbstractLoader {
public:
/** Default destructor */
virtual ~FAbstractLoader(){
}
/**
* Get the number of particules for this simulation
* @return number of particules that the loader can fill
*/
virtual long getNumberOfParticules() const = 0;
/**
* Get the center of the simulation box
* @return box center needed by the octree
*/
virtual F3DPosition getCenterOfBox() const = 0;
/**
* Get the simulation box width
* @return box width needed by the octree
*/
virtual double getBoxWidth() const = 0;
/**
* To know if the loader is valide (file opened, etc.)
* @return true if file is open
*/
virtual bool isValide() const = 0;
/**
* Fill the next particule
* @param inParticule the particule to fill
*/
virtual void fillParticule(ParticuleClass* const inParticule) = 0;
};
#endif //FABSTRACTLOADER_HPP
// [--LICENSE--]
#ifndef FBASICLOADER_HPP
#define FBASICLOADER_HPP
// /!\ Please, you must read the license at the bottom of this page
#include <iostream>
#include <fstream>
#include "FAbstractLoader.hpp"
#include "../Utils/F3DPosition.hpp"
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FBasicLoader
* Please read the license
*
* Load a file with a format like :
* NB_particules Box_width Box_X Box_Y Box_Z // init
* X Y Z // one particule by line
* ....
* <code>
* FBasicLoader<FBasicParticule> loader("../FMB++/Tests/particules.basic.txt"); <br>
* if(!loader.isValide()){ <br>
* std::cout << "Loader Error\n"; <br>
* return 1; <br>
* } <br>
* <br>
* FOctree<FBasicParticule, TestCell, 10, 3> tree(loader.getBoxWidth(),loader.getCenterOfBox()); <br>
* <br>
* for(int idx = 0 ; idx < loader.getNumberOfParticules() ; ++idx){ <br>
* FBasicParticule* const part = new FBasicParticule(); <br>
* loader.fillParticule(part); <br>
* tree.insert(part); <br>
* } <br>
* </code>
*/
template <class ParticuleClass>
class FBasicLoader : public FAbstractLoader<ParticuleClass> {
protected:
std::ifstream file; //< The file to read
F3DPosition centerOfBox; //< The center of box read from file
double boxWidth; //< the box width read from file
int nbParticules; //< the number of particules read from file
public:
/**
* The constructor need the file name
* @param filename the name of the file to open
* you can test if file is successfuly open by calling isValide()
*/
FBasicLoader(const char* const filename): file(filename,std::ifstream::in){
// test if open
if(this->file.is_open()){
double x,y,z;
this->file >> this->nbParticules >> this->boxWidth >> x >> y >> z;
this->centerOfBox.setPosition(x,y,z);
}
else {
this->boxWidth = 0;
this->nbParticules = 0;
}
}
/**
* Default destructor, simply close the file
*/
virtual ~FBasicLoader(){
file.close();
}
/**
* To know if file is open and ready to read
* @return true if loader can work
*/
bool isValide() const{
return this->file.is_open() && !this->file.eof();
}
/**
* To get the number of particules from this loader
* @param the number of particules the loader can fill
*/
long getNumberOfParticules() const{
return this->nbParticules;
}
/**
* The center of the box from the simulation file opened by the loader
* @return box center
*/
F3DPosition getCenterOfBox() const{
return this->centerOfBox;
}
/**
* The box width from the simulation file opened by the loader
* @return box width
*/
double getBoxWidth() const{
return this->boxWidth;
}
/**
* Fill a particule
* @warning to work with the loader, particules has to expose a setPosition method
* @param the particule to fill
*/
void fillParticule(ParticuleClass* const inParticule){
double x,y,z;
this->file >> x >> y >> z;
inParticule->setPosition(F3DPosition(x,y,z));
}
};
#endif //FBASICLOADER_HPP
// [--LICENSE--]
#ifndef F3DPOSITION_HPP
#define F3DPOSITION_HPP
// /!\ Please, you must read the license at the bottom of this page
// To get memcpy
#include <cstring>
/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class F3DPosition
* Please read the license
*
*/
class F3DPosition{
private:
double x; //< x position
double y; //< y position
double z; //< z position
public:
/** Default constructor */
F3DPosition() : x(0.0), y(0.0), z(0.0){
}
/** Default constructor */
F3DPosition(const double inX,const double inY,const double inZ)
: x(inX), y(inY), z(inZ){
}
/** Default destructor */
virtual ~F3DPosition(){
}
/**
* Copy constructor
* @param other the source class to copy
*/
F3DPosition(const F3DPosition& other):x(other.x), y(other.y), z(other.z){
}
/**
* Copy constructor
* @param other the source class to copy
* @return this a reference to the current class
*/
F3DPosition& operator=(const F3DPosition& other){
memcpy(this, &other, sizeof(F3DPosition));
return *this;
}
/**
* Position setter
* @param other the source class to copy
* @return this a reference to the current class
*/