/* 
* 
* Copyright (C) 2013 UMPC
* 
* This file is part of CLaRiNet.
*  
* CLaRiNet is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* CLaRiNet is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with CLaRiNet.  If not, see <http://www.gnu.org/licenses/>.
*/
#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 ;             // Nombre de fils directs dans l'arbre
    int ndescendants ;          // Nombre total de descendants dans l'arbre
    double TreeWeight ;         // Poids total élément + descendance

    // 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 );

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

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
