Root/cameo/fped2d2z.pl

1#!/usr/bin/perl
2#
3# fped2d2z.pl - Convert fped 2D stacks into 2.5D paths with Z information
4#
5# Written 2012, 2013 by Werner Almesberger
6# Copyright 2012, 2013 Werner Almesberger
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13
14
15sub usage
16{
17    print STDERR "usage: $0 [-r [-x] [-y] |-t Zmax] [Zin=Zout ...] prefix ".
18        "[file ...]\n\n";
19    print STDERR " Zin=Zout replace Zin with Zout\n";
20    print STDERR " -t Zmax set the top of the piece\n";
21    print STDERR " -r reverse Z stacking. Also swaps X and Y.\n";
22    print STDERR " -x flip on the X axis\n";
23    print STDERR " -y flip on the Y axis\n";
24    exit(1);
25}
26
27
28while ($ARGV[0] =~ /^-/) {
29    if ($ARGV[0] eq "-r") {
30        $reverse = 1;
31    } elsif ($ARGV[0] eq "-t") {
32        shift @ARGV;
33        $top = $ARGV[0];
34    } elsif ($ARGV[0] eq "-x") {
35        $flip_x = 1;
36    } elsif ($ARGV[0] eq "-y") {
37        $flip_y = 1;
38    } elsif ($ARGV[0] =~ /^-[^0-9]/) {
39        last;
40    } else {
41        &usage;
42    }
43    shift @ARGV;
44}
45
46while ($ARGV[0] =~ /=/) {
47    $map{$`} = $';
48    shift @ARGV;
49}
50
51$pfx = shift @ARGV;
52&usage unless defined $pfx;
53
54$skip = 1;
55while (<>) {
56    if (/^# $pfx.*?(\d+(\.\d*)?)\s*$/) {
57        $z = $1;
58        $skip = 0;
59    } elsif (/^# /) {
60        $skip = 1;
61    }
62    next if $skip;
63    if (/^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
64        $xmin = $1 if $1 < $xmin || !defined $xmin;
65        $xmax = $1 if $1 > $xmax || !defined $xmax;
66        $ymin = $2 if $2 < $ymin || !defined $ymin;
67        $ymax = $2 if $2 > $ymax || !defined $ymax;
68    }
69    $z{$z} .= $_;
70    $zmax = $z if $z > $zmax || !defined $zmax;
71}
72
73for $z (keys %z) {
74    undef $t;
75    for $s (split(/\n/, $z{$z})) {
76        if ($s =~ /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/) {
77            $s = sprintf("%.6f", ($xmax+$xmin)/2-$1)." $2"
78                if $flip_y;
79
80            # re-scan, so that we can flip on both axes
81            die unless $s =~
82                /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/;
83            $s = "$1 ".(($ymax+$ymin)/2-$2) if $flip_x;
84        }
85        $t .= "$s\n";
86    }
87    $z{$z} = "$t\n";
88}
89
90if ($reverse) {
91    for (keys %z) {
92        ($t{$zmax-$_} = $z{$_}) =~
93            s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$3 $1/mg;
94    }
95    %z = %t;
96    print "0 0 $zmax\n\n";
97} elsif (defined $top) {
98    for (keys %z) {
99        ($t{$top-$_} = $z{$_}) =~
100            s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$1 $3/mg;
101    }
102    %z = %t;
103} else {
104    print "0 0 0\n\n";
105}
106
107# sort, to make output easier to examine manually
108for (sort { $b <=> $a } keys %z) {
109    $s = defined $map{$_} ? $map{$_} : $_;
110    $z{$_} =~ s/\s+-?\d+(\.\d*)?$/$& $s/gm;
111    print $z{$_};
112}
113

Archive Download this file

Branches:
master



interactive