FFmaBinLoader.hpp 4.2 KB
Newer Older
1 2
#ifndef FFMABINLOADER_HPP
#define FFMABINLOADER_HPP
3
// [--License--]
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

#include <cstdio>

#include "../Utils/FGlobal.hpp"
#include "FAbstractLoader.hpp"
#include "../Utils/F3DPosition.hpp"
#include "../Utils/FDebug.hpp"

/**
* @author Berenger Bramas (berenger.bramas@inria.fr)
* @class FFmaBinLoader
* Please read the license
*
* Load a file with a format like :
* NB_particles Box_width Box_X Box_Y Box_Z // init
* X Y Z // one particle by line
* ....
* <code>
*    FFmaBinLoader<FBasicParticle> loader("../FMB++/Tests/particles.basic.txt"); <br>
*    if(!loader.isOpen()){ <br>
*        std::cout << "Loader Error\n"; <br>
*        return 1; <br>
*    } <br>
* <br>
*    FOctree<FBasicParticle, TestCell, FSimpleLeaf> tree(loader.getBoxWidth(),loader.getCenterOfBox()); <br>
* <br>
*    for(int idx = 0 ; idx < loader.getNumberOfParticles() ; ++idx){ <br>
*        FBasicParticle* const part = new FBasicParticle(); <br>
*        loader.fillParticle(part); <br>
*        tree.insert(part); <br>
*    } <br>
* </code>
*
* Particle has to extend {FExtendPhysicalValue,FExtendPosition}
*/
template <class ParticleClass>
class FFmaBinLoader : public FAbstractLoader<ParticleClass> {
protected:
    FILE* const file;            //< The file to read
    F3DPosition centerOfBox;     //< The center of box read from file
    FReal boxWidth;              //< the box width read from file
45
    FSize nbParticles;             //< the number of particles read from file
46

berenger-bramas's avatar
berenger-bramas committed
47 48
    int removeWarning;

49 50 51 52 53 54
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 hasNotFinished()
    */
berenger-bramas's avatar
berenger-bramas committed
55
    FFmaBinLoader(const char* const filename): file(fopen(filename, "rb")), removeWarning(0) {
56 57 58
        // test if open
        if(this->file != NULL) {
            int sizeOfElement(0);
berenger-bramas's avatar
berenger-bramas committed
59
            removeWarning += fread(&sizeOfElement, sizeof(int), 1, file);
60 61 62 63
            FDEBUG(if(sizeOfElement != int(sizeof(FReal)) ){)
                FDEBUG( FDebug::Controller.writeFromLine("Warning type size between file and FReal are differents\n", __LINE__, __FILE__); )
                    printf("%d sizeofelement\n",sizeOfElement);
            FDEBUG(})
64
            removeWarning += fread(&this->nbParticles, sizeof(FSize), 1, file);
65

berenger-bramas's avatar
berenger-bramas committed
66
            removeWarning += fread(&this->boxWidth, sizeof(FReal), 1, file);
67 68 69
            this->boxWidth *= 2;

            FReal x,y,z;
berenger-bramas's avatar
berenger-bramas committed
70 71 72
            removeWarning += fread(&x, sizeof(FReal), 1, file);
            removeWarning += fread(&y, sizeof(FReal), 1, file);
            removeWarning += fread(&z, sizeof(FReal), 1, file);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            this->centerOfBox.setPosition(x,y,z);
        }
        else {
             this->boxWidth = 0;
             this->nbParticles = 0;
        }
    }

    /**
    * Default destructor, simply close the file
    */
    virtual ~FFmaBinLoader(){
        fclose(file);
    }

    /**
      * To know if file is open and ready to read
      * @return true if loader can work
      */
    bool isOpen() const{
        return this->file != NULL;
    }

    /**
      * To get the number of particles from this loader
      * @param the number of particles the loader can fill
      */
100
    FSize getNumberOfParticles() const{
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
        return this->nbParticles;
    }

    /**
      * 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
      */
    FReal getBoxWidth() const{
        return this->boxWidth;
    }

    /**
      * Fill a particle
      * @warning to work with the loader, particles has to expose a setPosition method
      * @param the particle to fill
      */
    void fillParticle(ParticleClass& inParticle){
        FReal x,y,z,data;

berenger-bramas's avatar
berenger-bramas committed
128 129 130 131
        removeWarning += fread(&x, sizeof(FReal), 1, file);
        removeWarning += fread(&y, sizeof(FReal), 1, file);
        removeWarning += fread(&z, sizeof(FReal), 1, file);
        removeWarning += fread(&data, sizeof(FReal), 1, file);
132 133 134 135 136 137 138 139 140 141

        inParticle.setPosition(x,y,z);
        inParticle.setPhysicalValue(data);
    }

};


#endif //FFmaBinLoader_HPP

142
// [--END--]