Root/
Source at commit e82667e created 10 years 4 months ago. By Werner Almesberger, cameo/fped2d2z.pl: new options -x and -y to flip on X and Y axis | |
---|---|
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 | |
15 | sub 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 | |
28 | while ($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 | |
46 | while ($ARGV[0] =~ /=/) { |
47 | $map{$`} = $'; |
48 | shift @ARGV; |
49 | } |
50 | |
51 | $pfx = shift @ARGV; |
52 | &usage unless defined $pfx; |
53 | |
54 | $skip = 1; |
55 | while (<>) { |
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 | |
73 | for $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 = (($xmax+$xmin)/2-$1)." $2" if $flip_y; |
78 | |
79 | # re-scan, so that we can flip on both axes |
80 | die unless $s =~ |
81 | /^(-?[0-9]*\.[0-9]*)\s+(-?[0-9]*\.[0-9]*)/; |
82 | $s = "$1 ".(($ymax+$ymin)/2-$2) if $flip_x; |
83 | } |
84 | $t .= "$s\n"; |
85 | } |
86 | $z{$z} = "$t\n"; |
87 | } |
88 | |
89 | if ($reverse) { |
90 | for (keys %z) { |
91 | ($t{$zmax-$_} = $z{$_}) =~ |
92 | s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$3 $1/mg; |
93 | } |
94 | %z = %t; |
95 | print "0 0 $zmax\n\n"; |
96 | } elsif (defined $top) { |
97 | for (keys %z) { |
98 | ($t{$top-$_} = $z{$_}) =~ |
99 | s/^(-?\d+(\.\d*)?)\s+(-?\d+(\.\d*)?)/$1 $3/mg; |
100 | } |
101 | %z = %t; |
102 | } else { |
103 | print "0 0 0\n\n"; |
104 | } |
105 | |
106 | # sort, to make output easier to examine manually |
107 | for (sort { $b <=> $a } keys %z) { |
108 | $s = defined $map{$_} ? $map{$_} : $_; |
109 | $z{$_} =~ s/\s+-?\d+(\.\d*)?$/$& $s/gm; |
110 | print $z{$_}; |
111 | } |
112 |
Branches:
master