 #if !defined(AFX_VMDOutput_CPP_7F4A21B7_C13C_1223_TF2E_124095086234__INCLUDED_)
#define AFX_VMDOutput_CPP_7F4A21B7_C13C_1223_TF2E_124095086234__INCLUDED_


#include "VMDOutput.h"


VMDOutput::VMDOutput()
{

}

VMDOutput::~VMDOutput()
{
    
}
void VMDOutput::Write(std::string filename , MeshBluePrint blueprint)
{

    Nfunction f;
    std::vector<Vertex_Map> bvertex = blueprint.bvertex;
    std::vector<Triangle_Map> btriangle = blueprint.btriangle;
    std::vector<Inclusion_Map> binclusion = blueprint.binclusion;
    Vec3D Box = blueprint.simbox;
    
    std::vector<int> incstype;
    for (std::vector<Vertex_Map>::iterator it = bvertex.begin() ; it != bvertex.end(); ++it)
        incstype.push_back(0);
    
    
    for (std::vector<Inclusion_Map>::iterator it = binclusion.begin() ; it != binclusion.end(); ++it)
        incstype.at(it->vid) = it->tid;

	FILE *fgro;
    fgro = fopen(filename.c_str(), "w");

     const char* Title="generated by DTS";
     int Size=bvertex.size();
     fprintf(fgro,  "%s\n",Title);
     fprintf(fgro, "%5d\n",Size);
     int i=0;
     double resize = 1;
    
    for (std::vector<Vertex_Map>::iterator it = bvertex.begin() ; it != bvertex.end(); ++it)
    {
        std::string type;
        if(incstype.at(i)==0)
            type = "C";
        else if(incstype.at(i)==1)
            type = "O";
        else if(incstype.at(i)==2)
            type = "N";
        else if(incstype.at(i)>2)
            type = "C"+f.Int_to_String(incstype.at(i));
    const char* B=type.c_str();
    const char* A="Ver";
    int resid=0;
        
    fprintf(fgro, "%5d%5s%5s%5d%8.3f%8.3f%8.3f%8.4f%8.4f%8.4f\n",incstype.at(i),A,B,i%20000,it->x/resize,it->y/resize,it->z/resize,0.0,0.0,0.0 );
        i++;
    }
   fprintf(fgro,  "%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f%10.5f\n",Box(0)/resize,Box(1)/resize,Box(2)/resize,0.0,0.0,0.0,0.0,0.0,0.0 );
   fclose(fgro);
//=================================== 
//========= make the topology file
//=======================
std::string topfile=filename+".top";
std::ofstream Topfile;
Topfile.open(topfile.c_str());


Topfile<<"[ defaults ] \n";
Topfile<<"1 1 \n";
Topfile<<"[ atomtypes ] \n";
Topfile<<"CT1 72.0 0.000 A 0.0 0.0  \n";
Topfile<<"[ moleculetype ] \n";
Topfile<<" surface    1 \n";
Topfile<<" [atoms] \n";
i=0;
    for (std::vector<Vertex_Map>::iterator it = bvertex.begin() ; it != bvertex.end(); ++it)
    {
		i++;
		Topfile<<i<<" 	CT1 	"<<incstype.at(i-1) <<"	Sur 	C 	"<<i<<" 	0  \n";
    }
Topfile<<" [bonds] \n";

    // make the bonds
    for (std::vector<Triangle_Map>::iterator it = btriangle.begin() ; it != btriangle.end(); ++it)
    {
		i++;
        Vec3D x1((bvertex.at(it->v1)).x,(bvertex.at(it->v1)).y,(bvertex.at(it->v1)).z);
        Vec3D x2((bvertex.at(it->v2)).x,(bvertex.at(it->v2)).y,(bvertex.at(it->v2)).z);
        Vec3D x3((bvertex.at(it->v3)).x,(bvertex.at(it->v3)).y,(bvertex.at(it->v3)).z);
        
        if((bvertex.at(it->v1)).id>(bvertex.at(it->v2)).id)
        {
            Vec3D X=x2-x1;
            bool good = true;
            for (int i=0;i<3;i++)
            {
                if(fabs(X(i))>Box(i)/2)
                good = false;
            }
            if(good==true)
            {
                Topfile<<(bvertex.at(it->v1)).id+1<<"   "<<(bvertex.at(it->v2)).id+1<<"     1     1      20000\n";
            }
        }
        if((bvertex.at(it->v2)).id>(bvertex.at(it->v3)).id)
        {
            Vec3D X=x2-x3;
            bool good = true;
            for (int i=0;i<3;i++)
            {
                if(fabs(X(i))>Box(i)/2)
                    good = false;
            }
            if(good==true)
            {
                Topfile<<(bvertex.at(it->v2)).id+1<<"   "<<(bvertex.at(it->v3)).id+1<<"     1     1      20000\n";
            }
        }
        if((bvertex.at(it->v3)).id>(bvertex.at(it->v1)).id)
        {
            Vec3D X=x1-x3;
            bool good = true;
            for (int i=0;i<3;i++)
            {
                if(fabs(X(i))>Box(i)/2)
                    good = false;
            }
            if(good==true)
            {
                Topfile<<(bvertex.at(it->v3)).id+1<<"   "<<(bvertex.at(it->v1)).id+1<<"     1     1      20000\n";
            }
        }
    }
    

Topfile<<"[ system ] \n";
Topfile<<" triangluated surface \n";

Topfile<<"[ molecules ] \n";
Topfile<<" surface 1 \n";


}

/////////////////////////

#endif



