1
0
mirror of git://projects.qi-hardware.com/cae-tools.git synced 2024-12-23 20:21:09 +02:00
cae-tools/cameo/fped2d2z.pl

112 lines
2.5 KiB
Perl
Executable File

#!/usr/bin/perl
#
# fped2d2z.pl - Convert fped 2D stacks into 2.5D paths with Z information
#
# Written 2012, 2013 by Werner Almesberger
# Copyright 2012, 2013 Werner Almesberger
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
sub usage
{
print STDERR "usage: $0 [-r [-x] [-y] |-t Zmax] [Zin=Zout ...] prefix ".
"[file ...]\n\n";
print STDERR " Zin=Zout replace Zin with Zout\n";
print STDERR " -t Zmax set the top of the piece\n";
print STDERR " -r reverse Z stacking. Also swaps X and Y.\n";
print STDERR " -x flip on the X axis\n";
print STDERR " -y flip on the Y axis\n";
exit(1);
}
while ($ARGV[0] =~ /^-/) {
if ($ARGV[0] eq "-r") {
$reverse = 1;
} elsif ($ARGV[0] eq "-t") {
shift @ARGV;
$top = $ARGV[0];
} elsif ($ARGV[0] eq "-x") {
$flip_x = 1;
} elsif ($ARGV[0] eq "-y") {
$flip_y = 1;
} elsif ($ARGV[0] =~ /^-[^0-9]/) {
last;
} else {
&usage;
}
shift @ARGV;
}
while ($ARGV[0] =~ /=/) {
$map{$`} = $';
shift @ARGV;
}
$pfx = shift @ARGV;
&usage unless defined $pfx;
$skip = 1;
while (<>) {
if (/^# $pfx.*?(\d+(\.\d*)?)\s*$/) {
$z = $1;
$skip = 0;
} elsif (/^# /) {
$skip = 1;
}
next if $skip;
if (/^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
$xmin = $1 if $1 < $xmin || !defined $xmin;
$xmax = $1 if $1 > $xmax || !defined $xmax;
$ymin = $2 if $2 < $ymin || !defined $ymin;
$ymax = $2 if $2 > $ymax || !defined $ymax;
}
$z{$z} .= $_;
$zmax = $z if $z > $zmax || !defined $zmax;
}
for $z (keys %z) {
undef $t;
for $s (split(/\n/, $z{$z})) {
if ($s =~ /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
$s = (($xmax+$xmin)/2-$1)." $2" if $flip_y;
# re-scan, so that we can flip on both axes
die unless $s =~
/^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/;
$s = "$1 ".(($ymax+$ymin)/2-$2) if $flip_x;
}
$t .= "$s\n";
}
$z{$z} = "$t\n";
}
if ($reverse) {
for (keys %z) {
($t{$zmax-$_} = $z{$_}) =~
s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$3 $1/mg;
}
%z = %t;
print "0 0 $zmax\n\n";
} elsif (defined $top) {
for (keys %z) {
($t{$top-$_} = $z{$_}) =~
s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$1 $3/mg;
}
%z = %t;
} else {
print "0 0 0\n\n";
}
# sort, to make output easier to examine manually
for (sort { $b <=> $a } keys %z) {
$s = defined $map{$_} ? $map{$_} : $_;
$z{$_} =~ s/\s+-?\d+(\.\d*)?$/$& $s/gm;
print $z{$_};
}