//#####################################################################
// Copyright 2015
// This file is part of PhysBAM whose distribution is governed by the license contained in the accompanying file PHYSBAM_COPYRIGHT.txt.
//#####################################################################
#if USE_OPENVDB
#include <Core/Arrays/ARRAYS_ND.h>
#include <Core/Log/DEBUG_UTILITIES.h>
#include <Core/Log/LOG.h>
#include <Core/Matrices/MATRIX.h>
#include <Core/Parsing/PARSE_ARGS.h>
#include <Core/Read_Write/FILE_UTILITIES.h>
#include <Core/Vectors/VECTOR.h>
#include <openvdb/openvdb.h>
using namespace PhysBAM;
typedef float T;
typedef VECTOR<T,3> TV;
typedef VECTOR<int,TV::m> IV;

int main(int argc,char *argv[])
{
    std::string file_in,file_out;
    PARSE_ARGS parse_args(argc,argv);
    parse_args.Extra(&file_in,"in","input file (*.phi)");
    parse_args.Extra(&file_out,"out","output vdb");
    parse_args.Parse();

    ARRAY<T,IV> data; 
    Read_From_File(file_in,data);

    openvdb::initialize();
    openvdb::FloatGrid::Ptr grid=openvdb::FloatGrid::create(); 
    grid->setGridClass(openvdb::GRID_FOG_VOLUME);
    openvdb::FloatGrid::Accessor accessor=grid->getAccessor();
    openvdb::Coord xyz(0,0,0);
    IV start=data.domain.min_corner;
    IV counts(data.domain.Edge_Lengths());
    for(int i=0;i<counts.x;i++)
        for(int j=0;j<counts.y;j++)
            for(int k=0;k<counts.z;k++)
            {
                xyz.reset(i,j,k);
                accessor.setValue(xyz,data(IV(start.x+i,start.y+j,start.z+k)));
            }
    openvdb::io::File file(file_out);
    openvdb::GridPtrVec grids;
    grids.push_back(grid);
    file.write(grids);
    file.close();

    return 0;
}
#else
#include <Core/Log/DEBUG_UTILITIES.h>
using namespace PhysBAM;
int main(int argc,char *argv[])
{
    PHYSBAM_FATAL_ERROR("You must enable USE_OPENVDB to use this.");
    return 0;
}
#endif
