1 | #!/usr/bin/perl |
---|
2 | # |
---|
3 | # plybbox2cube: |
---|
4 | # This script takes the output of plybbox and generates a |
---|
5 | # ply file with the actual faces of a cube for the bounding |
---|
6 | # box. |
---|
7 | # |
---|
8 | # usage: |
---|
9 | # (see below) |
---|
10 | |
---|
11 | sub printUsage { |
---|
12 | print STDERR "\n"; |
---|
13 | print STDERR "Usage: ply2bboxcube in1.ply [in2.ply] ...\n"; |
---|
14 | print STDERR "\n"; |
---|
15 | print STDERR "And it will generate files called in1.bboxcube.ply,\n"; |
---|
16 | print STDERR "in2.bboxcube.ply, etc.....\n"; |
---|
17 | print STDERR "Which contain all 12 faces of the bounding box.\n"; |
---|
18 | print STDERR "\n"; |
---|
19 | exit -1; |
---|
20 | } |
---|
21 | |
---|
22 | if ($#ARGV == -1 || substr($ARGV[0], 0, 1) eq "-") { |
---|
23 | &printUsage(); |
---|
24 | |
---|
25 | } else { |
---|
26 | for ($i = 0; $i <= $#ARGV; $i++) { |
---|
27 | $inname = $ARGV[$i]; |
---|
28 | # Check to make sure it ends in .ply |
---|
29 | if (substr($inname, -4, 4) ne ".ply") { |
---|
30 | print STDERR "Err, $inname does not end in .ply, skipping...\n"; |
---|
31 | next; |
---|
32 | } |
---|
33 | # open the input file |
---|
34 | if (!open(IN, $inname)) { |
---|
35 | print STDERR "Err, cannot open input ply file $inname, ". |
---|
36 | "skipping....\n"; |
---|
37 | next; |
---|
38 | } |
---|
39 | #compute output file, make sure we're not overwriting input |
---|
40 | $outname = $inname; |
---|
41 | $outname =~ s/.ply/.bboxcube.ply/g; |
---|
42 | if ($outname eq $inname) { |
---|
43 | print STDERR "Assertion failure. $outname == $inname? skipping.\n"; |
---|
44 | next; |
---|
45 | } |
---|
46 | |
---|
47 | # open output file |
---|
48 | if (!open(OUT, ">$outname")) { |
---|
49 | print STDERR "Err, annot open output ply file $outname, ". |
---|
50 | "skipping....\n"; |
---|
51 | next; |
---|
52 | } |
---|
53 | |
---|
54 | # Run it |
---|
55 | close(IN); |
---|
56 | ($minx, $miny, $minz, $maxx, $maxy, $maxz) = |
---|
57 | split(' ', `plybbox $inname`); |
---|
58 | &plybboxcreatecube($inname, OUT); |
---|
59 | close(OUT); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | sub plybboxcreatecube { |
---|
64 | # Assume that these variables have been defined: |
---|
65 | # $minx, $miny, $minz |
---|
66 | # $maxx, $maxy, $maxz |
---|
67 | |
---|
68 | # |
---|
69 | # 3-----------1 |
---|
70 | # /| /| Z |
---|
71 | # 5-----------7 | | |
---|
72 | # | | | | | Y |
---|
73 | # | 6---------|-4 | / |
---|
74 | # |/ |/ |/ |
---|
75 | # 0-----------2 --------X |
---|
76 | # |
---|
77 | |
---|
78 | print OUT |
---|
79 | "ply |
---|
80 | format ascii 1.0 |
---|
81 | element vertex 8 |
---|
82 | property float x |
---|
83 | property float y |
---|
84 | property float z |
---|
85 | element face 12 |
---|
86 | property list uchar int vertex_indices |
---|
87 | end_header |
---|
88 | $minx $miny $minz |
---|
89 | $maxx $maxy $maxz |
---|
90 | $maxx $miny $minz |
---|
91 | $minx $maxy $maxz |
---|
92 | $maxx $maxy $minz |
---|
93 | $minx $miny $maxz |
---|
94 | $minx $maxy $minz |
---|
95 | $maxx $miny $maxz |
---|
96 | 3 0 2 5 |
---|
97 | 3 5 2 7 |
---|
98 | 3 2 4 7 |
---|
99 | 3 7 4 1 |
---|
100 | 3 4 6 1 |
---|
101 | 3 1 6 3 |
---|
102 | 3 6 0 5 |
---|
103 | 3 6 5 3 |
---|
104 | 3 5 7 3 |
---|
105 | 3 3 7 1 |
---|
106 | 3 6 4 2 |
---|
107 | 3 6 2 0 |
---|
108 | "; |
---|
109 | } |
---|