<?

ini_set("memory_limit","128M");
set_time_limit(0);

$filename = $_FILES["myfile"]["name"];

print_r($_FILES);return;

$tmp=explode(".",$filename);

$extension=$tmp[count($tmp)-1];

$fileobj="";
for ($i=0;$i<count($tmp)-1;$i++) $fileobj.=$tmp[$i];
$fileobj.=".obj";

if ($extension!="fe") 
	{
	print "<br><BR><BR><center><font color=\"crimson\"><small><b>Please enter a valid .fe file and try again</b></small></font></center>";
	return;
	}


header ("Content-type:text/plain");

$FILE=$filename;
$TOFILE=$fileobj;

print $filename." ".$fileobj;

$handle=fopen($FILE,"r") or die ("Error");
while (!feof($handle))
{
$content=fgets($handle,8192);
print $content."<bR>";
}
fclose($handle);

return;

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//GET POINTS

$point=(array) null;
$vertices=false;
$handle=fopen($FILE,"r") or die ("Error reading file !!! Please try again !!");
while (!feof($handle)) 
{
$content=trim(fgets($handle,8192));
if (($content{0}=="/") && ($content{1}=="/")) continue;
if (strlen($content)==0) continue;
$content=str_replace("                     "," ",$content);
$content=str_replace("                    "," ",$content);
$content=str_replace("                   "," ",$content);
$content=str_replace("                  "," ",$content);
$content=str_replace("                 "," ",$content);
$content=str_replace("                "," ",$content);
$content=str_replace("               "," ",$content);
$content=str_replace("              "," ",$content);
$content=str_replace("             "," ",$content);
$content=str_replace("            "," ",$content);
$content=str_replace("           "," ",$content);
$content=str_replace("          "," ",$content);
$content=str_replace("         "," ",$content);
$content=str_replace("        "," ",$content);
$content=str_replace("       "," ",$content);
$content=str_replace("      "," ",$content);
$content=str_replace("     "," ",$content);
$content=str_replace("    "," ",$content);
$content=str_replace("   "," ",$content);
$content=str_replace("  "," ",$content);
$content=str_replace("\t"," ",$content);

$tmp=explode(" ",$content);
$kw=trim($tmp[0]);
if ($kw=="edges") {$vertices=false;break;}

if ($vertices)
	{
	$point[trim($tmp[0])]["X"]=trim($tmp[1]);
	$point[trim($tmp[0])]["Y"]=trim($tmp[2]);
	$point[trim($tmp[0])]["Z"]=trim($tmp[3]);
	}

if ($kw=="vertices") {$vertices=true;continue;}

}
fclose($handle);
ksort($point);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//GET EDGES

$edge=(array) null;
$edge_bool=false;

$handle=fopen($FILE,"r");
while (!feof($handle))
{
$content=trim(fgets($handle,8192));
if (($content{0}=="/") && ($content{1}=="/")) continue;
if (strlen($content)==0) continue;
$content=str_replace("    "," ",$content);
$content=str_replace("   "," ",$content);
$content=str_replace("  "," ",$content);
$tmp=explode(" ",$content);
$kw=trim($tmp[0]);
if ($kw=="faces") {$edge_bool=false;break;}

if ($edge_bool)
	{
	$edge[trim($tmp[0])]["E1"]=trim($tmp[1]);
	$edge[trim($tmp[0])]["E2"]=trim($tmp[2]);
	}

if ($kw=="edges") {$edge_bool=true;continue;}
}
fclose($handle);

ksort($edge);

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

//GET FACES

$face=(array) null;
$face_bool=false;

$handle=fopen($FILE,"r");
while (!feof($handle))
{
$content=trim(fgets($handle,8192));
if (($content{0}=="/") && ($content{1}=="/")) continue;
if (strlen($content)==0) continue;
$content=str_replace("    "," ",$content);
$content=str_replace("   "," ",$content);
$content=str_replace("  "," ",$content);
$tmp=explode(" ",$content);
$kw=trim($tmp[0]);
if ($kw=="bodies") {$face_bool=false;break;}

if ($face_bool)
	{
	$face[trim($tmp[0])]["F1"]=trim($tmp[1]);
	$face[trim($tmp[0])]["F2"]=trim($tmp[2]);
	$face[trim($tmp[0])]["F3"]=trim($tmp[3]);
	}

if ($kw=="faces") {$face_bool=true;continue;}
}
fclose($handle);

ksort($face);

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//GET BODIES

$body=(array) null;
$body_bool=false;

$handle=fopen($FILE,"r");
while (!feof($handle))
{
$content=trim(fgets($handle,163840));
if (($content{0}=="/") && ($content{1}=="/")) continue;
if (strlen($content)==0) continue;
$content=str_replace("        "," ",$content);
$content=str_replace("       "," ",$content);
$content=str_replace("      "," ",$content);
$content=str_replace("     "," ",$content);
$content=str_replace("    "," ",$content);
$content=str_replace("   "," ",$content);
$content=str_replace("  "," ",$content);
$tmp=explode(" ",$content);
$kw=trim($tmp[0]);
if ($kw=="read") {$body_bool=false;break;}

if ($body_bool)
	{
	for ($i=1;$i<count($tmp);$i++)
		{
		if (trim($tmp[$i])=="volume") break;
	$body[trim($tmp[0])]["B$i"]=trim($tmp[$i]);
		}
	}

if ($kw=="bodies") {$body_bool=true;continue;}
}
fclose($handle);

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



function getPointX($point,$index) { return $point[$index]["X"];}
function getPointY($point,$index) { return $point[$index]["Y"];}
function getPointZ($point,$index) { return $point[$index]["Z"];}

$POINT=(array) null;
$TRIANGLEPOINT=(array) null;
$TMP_TRIANGLE=(array) null;


foreach ($body as $key => $val)
{

$my_triangle_tab=$val;
$my_body=$key;

foreach ($my_triangle_tab as $key2 => $val2)
	{
	$my_triangle=$val2;

	if ($my_triangle>0)
		{
		$my_edge_tab=$face[$val2];

		foreach ($my_edge_tab as $key3 => $val3)
			{
			$my_edge=$val3;
					
			if ($my_edge>0)
				{
				//Arete positive
				$my_point_tab=$edge[$val3];
				if (!strstr($POINT[$my_point_tab["E1"]],"X".$my_body."X")) $POINT[$my_point_tab["E1"]].="X".$my_body."X";
				if (!strstr($POINT[$my_point_tab["E2"]],"X".$my_body."X")) $POINT[$my_point_tab["E2"]].="X".$my_body."X";
				if (!strstr($TRIANGLEPOINT[$my_triangle],"@".$my_point_tab["E1"]."@")) $TRIANGLEPOINT[$my_triangle].="@".$my_point_tab["E1"]."@";
				if (!strstr($TRIANGLEPOINT[$my_triangle],"@".$my_point_tab["E2"]."@")) $TRIANGLEPOINT[$my_triangle].="@".$my_point_tab["E2"]."@";
				}
			else
				{
				//Arete negative
				$my_point_tab=$edge[abs($val3)];
				if (!strstr($POINT[$my_point_tab["E2"]],"X".$my_body."X")) $POINT[$my_point_tab["E2"]].="X".$my_body."X";
				if (!strstr($POINT[$my_point_tab["E1"]],"X".$my_body."X")) $POINT[$my_point_tab["E1"]].="X".$my_body."X";
				if (!strstr($TRIANGLEPOINT[$my_triangle],"@".$my_point_tab["E2"]."@")) $TRIANGLEPOINT[$my_triangle].="@".$my_point_tab["E2"]."@";
				if (!strstr($TRIANGLEPOINT[$my_triangle],"@".$my_point_tab["E1"]."@")) $TRIANGLEPOINT[$my_triangle].="@".$my_point_tab["E1"]."@";
				}
			}
		}
	else
		{
		//Triangle Negatif
		$my_edge_tab=$face[abs($val2)];
		$TMP_TRIANGLE="";
		if (count($my_edge_tab)==0) continue;
		foreach ($my_edge_tab as $key3 => $val3)
			{
			$my_edge=$val3;
			if ($my_edge>0)
				{
				//Arete positive
				$my_point_tab=$edge[abs($val3)];
				if (!strstr($POINT[$my_point_tab["E1"]],"X".$my_body."X")) $POINT[$my_point_tab["E1"]].="X".$my_body."X";
				if (!strstr($POINT[$my_point_tab["E2"]],"X".$my_body."X")) $POINT[$my_point_tab["E2"]].="X".$my_body."X";
 				if (!strstr($TMP_TRIANGLE,"@".$my_point_tab["E1"]."@")) $TMP_TRIANGLE.="@".$my_point_tab["E1"]."@";
 				if (!strstr($TMP_TRIANGLE,"@".$my_point_tab["E2"]."@")) $TMP_TRIANGLE.="@".$my_point_tab["E2"]."@";
				}
			else
				{
				//Arete negative
				$my_point_tab=$edge[abs($val3)];
				if (!strstr($POINT[$my_point_tab["E2"]],"X".$my_body."X")) $POINT[$my_point_tab["E2"]].="X".$my_body."X";
				if (!strstr($POINT[$my_point_tab["E1"]],"X".$my_body."X")) $POINT[$my_point_tab["E1"]].="X".$my_body."X";
 				if (!strstr($TMP_TRIANGLE,"@".$my_point_tab["E2"]."@")) $TMP_TRIANGLE.="@".$my_point_tab["E2"]."@";
 				if (!strstr($TMP_TRIANGLE,"@".$my_point_tab["E1"]."@")) $TMP_TRIANGLE.="@".$my_point_tab["E1"]."@";
				}
			}

		$TMP_TRIANGLE=str_replace("@@","@",$TMP_TRIANGLE);
		$tmp=explode("@",$TMP_TRIANGLE);

		for ($i=count($tmp)-1;$i>=1;$i--)
			{
 			if (!strstr($TRIANGLEPOINT[$my_triangle],"@".$tmp[$i]."@")) $TRIANGLEPOINT[$my_triangle].="@".$tmp[$i]."@";
	
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////CENTRAGE DE L'EMBRYON ////////////////////

$gX="";
$gY="";
$gZ="";

$sumX=0;$sumY=0;$sumZ=0;
foreach ($point as $key => $val)
{
$x=$point[$key]["X"];
$y=$point[$key]["Y"];
$z=$point[$key]["Z"];

$sumX+=$x;
$sumY+=$y;
$sumZ+=$z;

}
$gX=$sumX/count($point);
$gY=$sumY/count($point);
$gZ=$sumZ/count($point);


// $handle=fopen($TOFILE,"w");


// fputs($handle,"mtllib $TOFILE.mtl\n");

print "### This file has been automaticaly generated from a Surface Evolver File\n";
print "### by Converter 1.0\n";
print "### Fabrice Daian 07/2008 (daian@ibdm.univ-mrs.fr)\n";
print "\n\n";
print "mtllib $TOFILE.mtl\n";


$corrpoint=(array) null;
$corrpoint[0]="NULL";

foreach ($body as $key => $val)
{
$object=$key;
print "\n\ng Object$object\n";
// fputs($handle,"g Object$object\n");

$INTERMEDIATE=count($corrpoint);
print "##Object$object vertices\n";
foreach ($POINT as $key2 => $val2)
	{
	
	if (strstr($val2,"X".$object."X")) 
		{
 		//print $key2."<br>";
//  		print "($key2) v ".$point[$key2]["X"]." ".$point[$key2]["Y"]." ".$point[$key2]["Z"];
		print "v ".(round($point[$key2]["X"],4)-$gX)." ".(round($point[$key2]["Y"],4)-$gY)." ".(round($point[$key2]["Z"],4)-$gZ)."\n";
 		//fputs($handle,"v ".(round($point[$key2]["X"],4)-$gX)." ".(round($point[$key2]["Y"],4)-$gY)." ".(round($point[$key2]["Z"],4)-$gZ)."\n");
		//$corrpoint[$key2]=count($corrpoint)+1;
		$corrpoint[count($corrpoint)]=$key2;
//  		print " (".count($corrpoint).")<bR>";
		}
	}



// print_r($val);
print "##Object$object faces\n";
foreach ($val as $key2 => $val2)
{


// print "==>".$TRIANGLEPOINT[$val2]."<br>";
$tpcur=$TRIANGLEPOINT[$val2];

$tpcur=str_replace("@@@","@",$tpcur);
$tpcur=str_replace("@@","@",$tpcur);

// print $tpcur."<br>";

	$tmp=explode("@",$tpcur);


$ligneface="";
		$ligneface="f ";
		for ($i=0;$i<count($tmp);$i++)
			{
			if (strlen(trim($tmp[$i]))==0) continue;
			

			//print "(".$tmp[$i].") ".$corrpoint[$tmp[$i]]." ";
			for ($j=$INTERMEDIATE;$j<count($corrpoint);$j++)
				{
				if ($corrpoint[$j]==$tmp[$i]) break;
				}
			$ligneface.=$j." ";

			}
		$ligneface=trim($ligneface);
  		print $ligneface."\n";
// 		fputs($handle,$ligneface."\n");


}




}

// fclose($handle);

?>




