#!/usr/bin/perl

$first = 1;
while (<>) {
    if (/^Motion notify ([\d]+) ([\d]+) (\-?[\d]+)/) {
	$x = $1;
	$y = $2;
	push @x, $x;
	push @y, $y;
	push @press, $3;
	if ($first) {
	    $xmin = $x;
	    $xmax = $x;
	    $ymin = $y;
	    $ymax = $y;
	} else {
	    $xmin = $x if $xmin > $x;
	    $xmax = $x if $xmax < $x;
	    $ymin = $y if $ymin > $y;
	    $ymax = $y if $ymax < $y;
	}
	$first = 0;
    }
}

$scalex = 540 / ($xmax - $xmin);
$scaley = 720 / ($ymax - $ymin);
if ($scalex < $scaley) {
    $scale = $scalex;
} else {
    $scale = $scaley;
}

print "%!PS-Adobe-1.0\n";
print "36 756 translate\n";
printf "%g %g scale\n", $scale, -$scale;
printf "%d %d translate\n", -$xmin, -$ymin;
print "1 setlinejoin\n";

print ".75 .75 .75 setrgbcolor\n";

printf "%g setlinewidth\n", 5 / $scale;

$str = "moveto";
for ($i = 0; $i < @x; $i++) {
    if ($press[$i] > -480) {
	print "$x[$i] $y[$i] $str % $press[$i]\n";
	$str = "lineto";
    } else {
	if ($str eq "lineto") {
	    print "stroke\n";
	    $str = "moveto";
	}
	print "% $x[$i] $y[$i] $press[$i]\n";
    }
}

if ($str eq "lineto") {
    print "stroke\n";
    $str = "moveto";
}

if (1) {
print "1 0 0 setrgbcolor\n";
printf "%g setlinewidth\n", 0.25 / $scale;

$str = "moveto";
for ($i = 0; $i < @x; $i++) {
    print "$x[$i] $y[$i] $str % $press[$i]\n";
    $str = "lineto";
}

print "stroke\n";
}

if (1) {
print "0.5 0.5 1 setrgbcolor\n";
printf "%g setlinewidth\n", 0.25 / $scale;

$sz = 2;
$sz2 = 2 * $sz;
for ($i = 0; $i < @x; $i++) {
    print "$x[$i] $y[$i] moveto -$sz -$sz rmoveto 0 $sz2 rlineto $sz2 0 rlineto 0 -$sz2 rlineto closepath fill\n";
    $str = "lineto";
}

print "stroke\n";
}

printf "%g setlinewidth\n", 0.25 / $scale;

print "0 0 0 setrgbcolor\n";

$show_predict = 0;

$str = "moveto";
$hushx = 0;
$hushy = 0;
$xforce = 0;
$yforce = 0;
for ($i = 0; $i < @x; $i++) {
    $x = $x[$i - 1];
    $y = $y[$i - 1];
    $xpred = ($x[$i] + $x[$i - 2]) * 0.5;
    $ypred = ($y[$i] + $y[$i - 2]) * 0.5;

    $xpred_noncausal = ($x[$i] + $x[$i - 2]) * 0.5;
    $xpred_causal = 2 * $x[$i - 2] - $x[$i - 3];
    $xpred = $xpred_noncausal + ($xpred_causal - $xpred_noncausal) * 0.4;
    $ypred_noncausal = ($y[$i] + $y[$i - 2]) * 0.5;
    $ypred_causal = 2 * $y[$i - 2] - $y[$i - 3];
    $ypred = $ypred_noncausal + ($ypred_causal - $ypred_noncausal) * 0.4;

    if ($show_predict) {
	$x = $xpred;
	$y = $ypred;
    } else {
	$dx = $x - $xpred;
	if ($hushx == 0) {
	    if (abs ($dx > 16)) {
		$x = $xpred;
		$hushx = 1;
	    }
	} elsif ($hushx == 1) {
	    $hushx = 2;
	} elsif ($hushx == 2) {
	    $hushx = 0;
	}

	$dy = $y - $ypred;
	if ($hushy == 0) {
	    if (abs ($dy > 16)) {
		$y = $ypred;
		$hushy = 1;
	    }
	} elsif ($hushy == 1) {
	    $hushy = 2;
	} elsif ($hushy == 2) {
	    $hushy = 0;
	}
    }

    $xfilt = ($x + $x1 + $x2) / 3.0;
    $x2 = $x1;
    $x1 = $x;
    $yfilt = ($y + $y1 + $y2) / 3.0;
    $y2 = $y1;
    $y1 = $y;
    if ($i >= 3) {
	print "$xfilt $yfilt $str % $press[$i]\n";
	$str = "lineto";
    }
}

print "stroke\n";

print "showpage";
