#ifndef MESHTREE_H
#define MESHTREE_H

#include "GeoMorph.h"
#include <map>

using namespace std;

/* MeshTree : arbre de mailles  topo */

typedef struct MeshTree {

    // Attributs :

    int RootCell ;
    int cnt ;                   // 0 si libre, 1 si contraint
    double Weight ;
    double SSE ;                // Erreur quadratique totale à l'amont
    int nchildren ;

    // Topologie :

    struct MeshTree ** Child ;       // Child[i] pointe vers le i-ème élément fils

    struct MeshTree * Parent;        // Pointer towards parent (downstream) mesh element

    // Position de l'exutoire sur le "rail" autorisé :

//    map<float,int> PositionRange ;
//    map<float,int>::iterator CurrentPosition ;

     int * PositionRange ;
     double * CWeight ;
     int RangeLength ;
     int CurrentPosition ;

} MeshTree ;

int MakeMeshTree ( FlowDirGrid * F , FloatRaster * CW , FloatRaster * PK , int nnodes , int * NODES , int * CNT , MeshTree ** _MTree , double * _meanweight );

int MakeMeshTree_ ( MeshTree * MTree , double * _meanweight , double * _SSE );

int OptimMeshVariance_ ( MeshTree * MTree , double * _meanweight );

int test_Up ( MeshTree * MTree );

int test_Down ( MeshTree * MTree );

int BuildPositionRange_ ( FlowDirGrid * F , FloatRaster * CW , FloatRaster * PK , MeshTree * MTree );

int UpdateMeshTree_ ( MeshTree * MTreeDst , MeshTree * MTreeSrc );

int CopyMeshTree_ ( MeshTree * MTree , MeshTree ** _MTreeCopy );

int DestroyMeshTree_ ( MeshTree ** _MTree );


#endif