//#####################################################################
// Copyright 2003-2009, Eran Guendelman, Geoffrey Irving, Michael Lentine, Andrew Selle.
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
#include <Core/Meshes/STRUCTURE_LIST.h>
#include <Core/Log/LOG.h>
#include <Core/Read_Write/FILE_UTILITIES.h>
namespace PhysBAM{
template<class TV,class ID> void STRUCTURE_LIST<TV,ID>::
Read(const VIEWER_DIR& viewer_dir,const std::string& list_name)
{
    ARRAY<ID> needs_init;
    BASE::Read(viewer_dir,list_name,needs_init);
    Read_From_File(viewer_dir.Common_File(list_name+"_key"),names);
    for(int i=0;i<needs_init.m;i++){
        ID id=needs_init(i),index=Element_Index(id);
        Set_Active_Element(index,STRUCTURE<TV>::Create_From_Name(names(id)));
        Read_From_File(
            viewer_dir.Common_File(LOG::sprintf("%s_%d.%s",list_name,id,
                    Active_Element(index)->Extension())),*Active_Element(index));}

    for(ID id=0;id<Size();id++)
        if(Is_Active(id)){
            STRUCTURE<TV>& structure=*Active_Element(Element_Index(id));
            std::string filename=viewer_dir.Frame_File(LOG::sprintf("%s_%d.%s",list_name,id,structure.Extension()));
            if(File_Exists(filename)) {
                Read_From_File(filename,structure);
                structure.update_every_frame=true;}
            else structure.update_every_frame=false;}
}
template<class TV,class ID> void STRUCTURE_LIST<TV,ID>::
Write(STREAM_TYPE stream_type,const VIEWER_DIR& viewer_dir, const std::string& list_name) const
{
    BASE::Write(stream_type,viewer_dir,list_name);
    names.Resize(Value(Size()));
    ARRAY<ID>& needs_write=Needs_Write();
    for(int i=0;i<needs_write.Size();i++){
        ID id=needs_write(i);
        int index=Element_Index(id);
        assert(index>=0);
        names(id)=Active_Element(index)->Name();
        Write_To_File(stream_type,viewer_dir.Common_File(LOG::sprintf("%s_%d.%s",
                    list_name,id,Active_Element(index)->Extension())),*Active_Element(index));}
    if(viewer_dir.First_Frame() || needs_write.Size())
        Write_To_File(stream_type,viewer_dir.Common_File(list_name+"_key"),names);
    needs_write.Remove_All();

    for(ID id=0;id<Size();id++)
        if(Is_Active(id)){
            STRUCTURE<TV>& structure=*Active_Element(Element_Index(id));
            if(structure.update_every_frame){
                std::string filename=viewer_dir.Frame_File(LOG::sprintf("%s_%d.%s",list_name,id,structure.Extension()));
                Write_To_File(stream_type,filename,structure);}}
}
template void STRUCTURE_LIST<VECTOR<double,1>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<double,1>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
template void STRUCTURE_LIST<VECTOR<double,2>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<double,2>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
template void STRUCTURE_LIST<VECTOR<double,3>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<double,3>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
template void STRUCTURE_LIST<VECTOR<float,1>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<float,1>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
template void STRUCTURE_LIST<VECTOR<float,2>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<float,2>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
template void STRUCTURE_LIST<VECTOR<float,3>,int>::Read(VIEWER_DIR const&,std::string const&);
template void STRUCTURE_LIST<VECTOR<float,3>,int>::Write(STREAM_TYPE,VIEWER_DIR const&,std::string const&) const;
}
