#include "FVLib.h"
int main(int argc, char *argv[])
{
	if (argc < 4) 
	{
        cout<<"Wrong format"<<endl;    
        cout<<"deform mesh_in.xml mesh_out.xml percentage angle"<<endl;
        exit(0);
	}
	size_t code_in=NULL_FORMAT,code_out=NULL_FORMAT;        
	string    file_in=argv[1];
	string    file_out=argv[2];
	string    percent=argv[3];
	
	
	size_t l;
	l=file_in.length();
	string label_in;label_in.insert(0,file_in,l-4,4);
	l=file_out.length();
	string label_out;label_out.insert(0,file_out,l-4,4);
	if(!label_in.compare(".xml"))  code_in=XML_FORMAT;
	if(!label_out.compare(".xml"))  code_out=XML_FORMAT;
	//cout<<"code_in="<<code_in<<", code out="<<code_out<<endl;
	if(!code_in) {cout<<"ERROR: input file format must be .xml"<<endl;exit(0);}
	if(!code_out) {cout<<"ERROR: output file format must be .xml"<<endl;exit(0);}
	double deform=atof(percent.c_str())*0.01;
	double h=0.,theta,ale,ble,phi;
	FVMesh1D m1;
	FVMesh2D m2;
	FVMesh3D m3;
	if(m3.read(file_in.c_str())==FVOK) 
    {
		cout<<"3D mesh, ";
		FVVertex3D *ptr_v;
		FVFace3D *ptr_f;
		m3.beginVertex();
		while((ptr_v=m3.nextVertex())) ptr_v->code=0;
		m3.beginFace();
		while((ptr_f=m3.nextFace()))
        {
			ptr_f->beginVertex();
			while((ptr_v=ptr_f->nextVertex()))
				ptr_v->code|=ptr_f->code;
			h=sqrt(ptr_f->area);
        }
		cout<<"deform factor ="<<deform<<endl;fflush(NULL);
		m3.beginVertex();
		while((ptr_v=m3.nextVertex())) 
        {
			if(!ptr_v->code)    
            {
				ale=(rand()*1.0)/(1.0*RAND_MAX);  
				ble=(rand()*1.0)/(1.0*RAND_MAX);
				theta=2.*FVLIB_PI*ale; 
				phi=2.*FVLIB_PI*ble;
				ptr_v->coord.x+=deform*h*cos(theta)*sin(phi);
				ptr_v->coord.y+=deform*h*sin(theta)*sin(phi);
				ptr_v->coord.z+=deform*h*sin(phi);        
            }
        }
		m3.write(file_out.c_str());        
    }
	if(m2.read(file_in.c_str())==FVOK) 
    {
		cout<<"2D mesh, ";
		FVVertex2D *ptr_v;
		FVEdge2D *ptr_e;
		m2.beginVertex();
		while((ptr_v=m2.nextVertex())) ptr_v->code=0;
		m2.beginEdge();
		while((ptr_e=m2.nextEdge()))
        {
			ptr_e->firstVertex->code|=ptr_e->code;
			ptr_e->secondVertex->code|=ptr_e->code;
			h=ptr_e->length;
        }
		cout<<"deform factor ="<<deform<<endl;fflush(NULL);
		m2.beginVertex();
		while((ptr_v=m2.nextVertex())) 
        {
			if(!ptr_v->code)    
            {
				ale=(rand()*1.0)/(1.0*RAND_MAX);    
				theta=2.*FVLIB_PI*ale; 
				ptr_v->coord.x+=deform*h*cos(theta);
				ptr_v->coord.y+=deform*h*sin(theta);
            }
        }
		m2.write(file_out.c_str());         
    }      
	if(m1.read(file_in.c_str())==FVOK) 
    {
		cout<<"1D mesh, "<<endl;
		FVVertex1D *ptr_v;
		FVCell1D *ptr_c;
		m1.beginVertex();
		while((ptr_v=m1.nextVertex())) ptr_v->code=0;
		m1.beginCell();
		while((ptr_c=m1.nextCell()))
        {
			ptr_c->firstVertex->code|=ptr_c->code;
			ptr_c->secondVertex->code|=ptr_c->code;
			h=ptr_c->length;
        }
		cout<<"deform factor ="<<deform<<endl;fflush(NULL);
		m1.beginVertex();
		while((ptr_v=m1.nextVertex())) 
        {
			if(!ptr_v->code)    
            {
				ale=(rand()*1.0)/(1.0*RAND_MAX);    
				theta=2.*FVLIB_PI*ale; 
				ptr_v->coord.x+=deform*h*cos(theta);
            }
        }
		// the work to do
		m1.write(file_out.c_str());  
    }
}