//#####################################################################
// Copyright 2009, Jon Gretarsson.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
// Namespace SIGNED_DISTANCE
//##################################################################### 
#include <Core/Grids_Uniform_Computations/SEGMENTED_CURVE_2D_SIGNED_DISTANCE.h>
#include <Core/Arrays/ARRAYS_ND.h>
#include <Core/Grids/GRID.h>
#include <Core/Log/LOG.h>
#include <Core/Math_Tools/RANGE.h>
#include <Core/Meshes/SEGMENTED_CURVE.h>
namespace PhysBAM{

namespace SIGNED_DISTANCE{
//#####################################################################
// Function Calculate
//#####################################################################
template<class T> void Calculate(SEGMENTED_CURVE<VECTOR<T,2> >& curve,const GRID<VECTOR<T,2> >& grid,AT_NODES,ARRAY<T,VECTOR<int,2> >& phi,bool print_progress)
{
    bool bounding_box_defined=curve.bounding_box!=0;if(!bounding_box_defined) curve.Update_Bounding_Box();

    phi.Resize(grid.Numbers_Of_Nodes());

    T epsilon=(T)1e-8*grid.dX.Min();
    int total_cells=grid.Numbers_Of_Nodes().x*grid.Numbers_Of_Nodes().y,cells_done=0,progress=-1;
    for(int i=0;i<grid.Numbers_Of_Nodes().x;i++) for(int j=0;j<grid.Numbers_Of_Nodes().y;j++){
        phi(i,j)=curve.Calculate_Signed_Distance(grid.Node_X(VECTOR<int,2>(i,j)),epsilon);
        if(print_progress){
            cells_done++;int new_progress=(int)((T)100*cells_done/total_cells);
            if(new_progress > progress){
                LOG::cout<<new_progress<<"% "<<std::flush;
                progress=new_progress;}}}
    if(print_progress) LOG::cout<<std::endl;

    if(!bounding_box_defined){delete curve.bounding_box;curve.bounding_box=0;}
}
//####################################################################
template void Calculate(SEGMENTED_CURVE<VECTOR<float,2> >&,const GRID<VECTOR<float,2> >&,AT_NODES,ARRAY<float,VECTOR<int,2> >&,bool);
template void Calculate(SEGMENTED_CURVE<VECTOR<double,2> >&,const GRID<VECTOR<double,2> >&,AT_NODES,ARRAY<double,VECTOR<int,2> >&,bool);
};
};
