1 2 3 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 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 100 101
|
template <typename T, typename U> class Terrain
{
protected:
unsigned int Width;
unsigned int Height;
std::vector<std::vector<T> > Sol;
T CoefficientDeDiffusion;
T CoefficientDEvaporation;
public:
Terrain ( const unsigned int l, const unsigned int h );
Terrain ( const Terrain<T,U>& ter );
~Terrain ();
Terrain<T,U>& operator = ( Terrain<T,U> &ter );
void affiche () const;
void SetSol ( const unsigned int l, const unsigned int h, T valeur );
void SetCoefficientDeDiffusion ( T coeffDiff );
void SetCoefficientDEvaporation ( T coeffEvap );
void Diffuse ( U dT );
};
template <typename T, typename U> void Terrain<T,U>::Diffuse ( U dT )
{
std::vector<std::vector<T> > flux;
std::vector<std::vector<T> > pertes;
T courant; //Valeur du pixel courant
T vh, vb, vg, vd; //Valeurs des pixels voisins
T p, pTempo; //Variables auxiliaires
const T zero = static_cast<T>( 0 );
//flux et pertes sont initialisée pour faire sx de large et sy de haut
for ( unsigned int y = 0; y < Height; y++ )
{
flux.push_back ( std::vector<T> ( Width, zero ) );
pertes.push_back ( std::vector<T> ( Width, zero ) );
}
//Calcule de diffusion
for ( unsigned int y = 0; y < Height; ++y )
{
for ( unsigned int x = 0; x < Width; ++x )
{
vh = zero; vb = zero; vg = zero; vd = zero; p = zero;
courant = Sol[y][x];
//Calcul de flux : ce que gagne chaques pixels voisins
if ( y ) //Si on est pas à la première ligne
{
vh = Sol[y-1][x];
if ( courant > vh )
{
pTempo = dT * CoefficientDeDiffusion * ( courant - vh );
flux[y-1][x] = pTempo;
p += pTempo;
}
}
if ( y < Height-1 ) //Si on est pas à la dernière ligne
{
vb = Sol[y+1][x];
if ( courant > vb )
{
pTempo = dT * CoefficientDeDiffusion * ( courant - vb );
flux[y+1][x] = pTempo;
p += pTempo;
}
}
if ( x ) //Si on est pas à la première colonne
{
vg = Sol[y][x-1];
if ( courant > vg )
{
pTempo = dT * CoefficientDeDiffusion * ( courant - vg );
flux[y][x-1] = pTempo;
p += pTempo;
}
}
if ( x < Width-1 ) //Si on est pas à la dernière colonne
{
vd = Sol[y][x+1];
if ( courant > vd )
{
pTempo = dT * CoefficientDeDiffusion * ( courant - vd );
flux[y][x+1] = pTempo;
p += pTempo;
}
}
//Calcul de pertes : ce que perd ce pixel
pertes[y][x] = p + dT * CoefficientDEvaporation;
}
}
//Calcul final : On additionne au tableau d'origine le tableau des flux
// et on y soustrait celui des pertes
for ( unsigned int y = 0; y < Height; ++y )
for ( unsigned int x = 0; x < Width; ++x )
Sol[y][x] += flux[y][x] - pertes[y][x];
} |
Partager