#!/usr/bin/perl
#
# perl makethickline.pl infile outfile
#
# infile has lines represented by
# x0 y0 z0 x1 y1 z1 w c        
# to join points (x0,y0,z0) and (x1,y1,z1) with a line of thickness w
#  and color c (the index to some cmap file, but this program doesnt
#  have to take care of cmap stuff)

$infile = $ARGV[0];
$outfile = $ARGV[1];

open (IN,"$infile");
open (OUT,">".$outfile);

while ( $x = <IN> )
{
    @tmp = split /[\t\n\r\ ]+/, $x;
    $start = 0;
    while (0 == length($tmp[$start]))   {$start++;}
    $x0 = $tmp[$start+0];
    $y0 = $tmp[$start+1];
    $z0 = $tmp[$start+2];
    $x1 = $tmp[$start+3];
    $y1 = $tmp[$start+4];
    $z1 = $tmp[$start+5];
    $w  = $tmp[$start+6];
    $c  = $tmp[$start+7];

    # draw one rectangle

    # first corner is at (a0,b0,c0)

    $a0 = $x0 + $w/2;
    $b0 = $y0 + $w/2;
    $c0 = $z0 + $w * ($x1-$x0+$y1-$y0) / (2*($z0-$z1));
    $wcur = sqrt ( ($z0-$c0)**2 + ($y0-$b0)**2 + ($x0-$a0)**2 );

    print "$w $wcur\n";

    $a0 = $x0 + (1.0*$w/$wcur)*($a0-$x0);
    $b0 = $y0 + (1.0*$w/$wcur)*($b0-$y0);
    $c0 = $z0 + (1.0*$w/$wcur)*($c0-$z0);

    # second corner
    
    $a1 = $x0 -($a0-$x0);
    $b1 = $y0 -($b0-$y0);
    $c1 = $z0 -($c0-$z0);

    #third corner

    $a2 = $a1 + ($x1-$x0);
    $b2 = $b1 + ($y1-$y0);
    $c2 = $c1 + ($z1-$z0);

    #fourth corner

    $a3 = $a0 + ($x1-$x0);
    $b3 = $b0 + ($y1-$y0);
    $c3 = $c0 + ($z1-$z0);

    print OUT "mesh -c $c -s solid {\n2 2\n";
    print OUT "$a0 $b0 $c0\n";
    print OUT "$a1 $b1 $c1\n";
    print OUT "$a3 $b3 $c3\n";
    print OUT "$a2 $b2 $c2\n";

    print OUT "}\n";
#    print OUT "$a0 $b0 $c0 2\n";
#    print OUT "$a1 $b1 $c1 2\n";
#    print OUT "$a3 $b3 $c3 2\n";
#    print OUT "$a2 $b2 $c2 2\n";


    # draw second rectangle

    # first corner is at (p0,q0,r0)
    # (p,q,r)-(x0,y0,z0) is orthogonal to both 
    # (x1,y1,z1)-(x0,y0,z0)  and (a0,b0,c0)-(x0,y0,z0) 

    $p0 = $x0 + ( ($y1-$y0)*($c0-$z0) - ($z1-$z0)*($b0-$y0) );
    $q0 = $y0 + ( ($z1-$z0)*($a0-$x0) - ($x1-$x0)*($c0-$z0) );
    $r0 = $z0 + ( ($x1-$x0)*($b0-$y0) - ($y1-$y0)*($a0-$x0) );
    $wcur = sqrt ( ($z0-$r0)**2 + ($y0-$q0)**2 + ($x0-$p0)**2 );

    print "$w $wcur\n";

    $p0 = $x0 + ($w/$wcur)*($p0-$x0);
    $q0 = $y0 + ($w/$wcur)*($q0-$y0);
    $r0 = $z0 + ($w/$wcur)*($r0-$z0);

    # second corner

    $p1 = $x0 -($p0-$x0);
    $q1 = $y0 -($q0-$y0);
    $r1 = $z0 -($r0-$z0);

    #third corner

    $p2 = $p1 + ($x1-$x0);
    $q2 = $q1 + ($y1-$y0);
    $r2 = $r1 + ($z1-$z0);

    #fourth corner

    $p3 = $p0 + ($x1-$x0);
    $q3 = $q0 + ($y1-$y0);
    $r3 = $r0 + ($z1-$z0);

    print OUT "mesh -c $c -s solid {\n2 2\n";
    print OUT "$p0 $q0 $r0\n";
    print OUT "$p1 $q1 $r1\n";
    print OUT "$p3 $q3 $r3\n";
    print OUT "$p2 $q2 $r2\n";
    print OUT "}\n";


#    print OUT "$p0 $q0 $r0 1\n";
#    print OUT "$p1 $q1 $r1 1\n";
#    print OUT "$p3 $q3 $r3 1\n";
#    print OUT "$p2 $q2 $r2 1\n";

    $L1 = sqrt ( ($a0-$a1)**2 + ($b0-$b1)**2 + ($c0-$c1)**2);
    $L2 = sqrt ( ($p0-$p1)**2 + ($q0-$q1)**2 + ($r0-$r1)**2);
    print "$L1 $L2\n";

    $L1 = sqrt ( ($a2-$a3)**2 + ($b2-$b3)**2 + ($c2-$c3)**2);
    $L2 = sqrt ( ($p2-$p3)**2 + ($q2-$q3)**2 + ($r2-$r3)**2);
    print "$L1 $L2\n";

}
