<?PHP
// --- Convert RAW image airfoil into .DAT file - Caltabiano Salvatore - Frontier2 ---
// --- Convert RAW image airfoil shape into .DAT file ---
// --- By Caltabiano Salvatore - Frontier2 - 2006 September 06 - V 1.00 PHP Version ---
class Airfoil{
var $info;
var $cols;
var $rows;
var $data;
var $coords;
var $coordsr;
var $coordsl;
var $ratio;
var $filename;
var $filenameout;
function Airfoil()
{
$this->info = "Airfoil coords :";
$this->cols = 0;
$this->rows = 0;
$this->ratio = 1;
$this->data = NULL;
$this->coords = NULL;
$this->coordsr = NULL;
$this->coordsl = NULL;
$this->filename = "airfoil.raw";
$this->filenameout = "airfoil.dat";
}
function load()
{
$fpin = fopen($this->filename, "r+b");
if ( $fpin )
{
$data = "";
$curdata = "";
do
{
$curdata = fread($fpin,8192);
$data .= $curdata;
}while($curdata != "");
fclose($fpin);
}
if ( isset($data) ) $this->data = $data;
// USE THIS CICLE IF RAW FILE IMAGE IS RGB AND NOT GREY SCALE
//for ( $j=0 ; isset($data[$j]) ; $j += 3 )
// $this->data .= $data[$j];
$filenameout = explode(".",basename($this->filename));
$filenameout[0] .= ".DAT";
$this->filenameout = $filenameout[0];
$info = explode(".",basename($this->filename));
$this->info = $info[0];
}
function calculate()
{
if ( !isset($this->data) )
return FALSE;
if ( $this->cols == 0 || $this->rows == 0 )
return FALSE;
for( $c=$this->cols-1 ; $c>=0 ; $c-- )
{
for( $r=0 ; $r<$this->rows-1 ; $r++ )
{
if ( ord($this->data[($this->rows*$r+$c)]) == 0 )
{
$x = $c;
$y = (-($r-intval($this->rows/2-1)));
if ( isset($coords) && $y+1 == $coords['y'])
$coords = array ( "x" => $x, "y" => $y );
else
{
$coords = array ( "x" => $x, "y" => $y );
$this->coords[] = $coords;
}
}
}
}
return TRUE;
}
function sort2()
{
if ( !isset($this->coords) )
return FALSE;
$coordsr = $this->coords[0];
$coordsl = $this->coords[0];
for ( $j=0 ; isset($this->coords[$j]) ; $j++)
{
if ( $this->coords[$j]['x'] >= $coordsr['x'] )
$coordsr = $this->coords[$j];
if ( $this->coords[$j]['x'] <= $coordsl['x'] )
$coordsl = $this->coords[$j];
if ( $j>0 && $this->coords[$j]['x'] == $this->coords[$j-1]['x'] )
$coordsdown[] = $this->coords[$j];
else
$coordsup[] = $this->coords[$j];
}
if ( isset($coordsdown) ) $coordsdown = array_reverse($coordsdown);
$this->coordsr = $coordsr;
$this->coordsl = $coordsl;
$this->ratio = (1+($coordsr['x']-$coordsl['x']));
if ( isset($coordsup) && isset($coordsdown) )
$this->coords = array_merge($coordsup,$coordsdown);
}
function displayCoords($step=5)
{
if ( !isset($this->coords) )
return FALSE;
echo $this->info."
";
for ( $j=0 ; isset($this->coords[$j]) ; $j++)
if ( $j%$step == 0 )
echo $this->coords[$j]['x']/$this->ratio." ".$this->coords[$j]['y']/$this->ratio."
";
}
function displayCoordsRL()
{
if ( !isset($this->coords) )
return FALSE;
echo "LEFT: [".$this->coordsl['x']/$this->ratio."][".$this->coordsl['y']/$this->ratio."]<BR>\n";
echo "RIGHT: [".$this->coordsr['x']/$this->ratio."][".$this->coordsr['y']/$this->ratio."]<BR>\n";
}
function saveCoords($step=5)
{
if ( !isset($this->coords) )
return FALSE;
$fpout = fopen("temp/".$this->filenameout, "w+t");
{
fwrite($fpout,$this->info."
");
for ( $j=0 ; isset($this->coords[$j]) ; $j++)
{
if ( $j%$step == 0 ){
fwrite($fpout,$this->coords[$j]['x']/$this->ratio." ".$this->coords[$j]['y']/$this->ratio);
if ( isset($this->coords[$j+1]) )
fwrite($fpout,"
");
}
}
fclose($fpout);
}
}
}
?>