|
1 #!/usr/bin/perl |
|
2 |
|
3 # Make a nice multi-view picture of a luminaire |
|
4 # |
|
5 # This is based on objpict.pl that renders four-view |
|
6 # images of objects that are not light sources. |
|
7 # |
|
8 # Written by Axel Jacobs, |
|
9 # with input from Rob Guglielmetti and Gre |
|
10 |
|
11 use strict; |
|
12 use warnings; |
|
13 |
|
14 use File::Temp qw/ tempdir /; |
|
15 #my $td = tempdir( CLEANUP => 1 ); |
|
16 my $td = "tmp"; |
|
17 |
|
18 #my $xres = 1024; |
|
19 #my $yres = 1024; |
|
20 my $xres = 600; |
|
21 my $yres = 600; |
|
22 my $tiny = 0.01; |
|
23 my $rpict_cmd = "rpict -ab 0 -ds 0 -dv -av 0 0 0 -x $xres -y $yres"; |
|
24 my $pfilt_cmd = "pfilt -r .6 -x /2 -y /2"; |
|
25 |
|
26 my $material = "$td/lt.mat"; |
|
27 my $testroom1 = "$td/testroom1.rad"; |
|
28 my $testroom2 = "$td/testroom2.rad"; |
|
29 my $testroom3 = "$td/testroom3.rad"; |
|
30 my $testroom4 = "$td/testroom4.rad"; |
|
31 my $octree1 = "$td/lt1.oct"; |
|
32 my $octree2 = "$td/lt2.oct"; |
|
33 my $octree3 = "$td/lt3.oct"; |
|
34 my $octree4 = "$td/lt4.oct"; |
|
35 |
|
36 # We need at least one Radiance file or a scene on STDIN (but not both) |
|
37 if ($#ARGV < 0) { |
|
38 open(FH, ">$td/stdin.rad") or |
|
39 die("objview: Can't write to temporary file $td/stdin.rad\n"); |
|
40 while (<>) { |
|
41 print FH; |
|
42 } |
|
43 # Pretend stdin.rad was passed as argument. |
|
44 @ARGV = ("$td/stdin.rad"); |
|
45 } |
|
46 |
|
47 |
|
48 # Scale fitting and center at origin |
|
49 my $dimstr = `getbbox -h @ARGV`; |
|
50 chomp $dimstr; |
|
51 # Values returned by getbbox are indented and delimited with multiple spaces. |
|
52 $dimstr =~ s/^\s+//; # remove leading spaces |
|
53 my @dims = split(/\s+/, $dimstr); # convert to array |
|
54 |
|
55 # Find largest axes-aligned dimension |
|
56 my @diffs = ($dims[1]-$dims[0], $dims[3]-$dims[2], $dims[5]-$dims[4]); |
|
57 @diffs = reverse sort { $a <=> $b } @diffs; |
|
58 my $size = $diffs[0]; |
|
59 |
|
60 # Move fitting so centre is at origin |
|
61 my $xtrans = -1.0 * ($dims[0] + $dims[1]) / 2; |
|
62 my $ytrans = -1.0 * ($dims[2] + $dims[3]) / 2; |
|
63 my $ztrans = -1.0 * ($dims[4] + $dims[5]) / 2; |
|
64 # Scale so that largest object dimension is unity |
|
65 my $scale = 1 / $size; |
|
66 |
|
67 my $fitting = "$td/fitting.rad"; |
|
68 open(FH, ">$fitting") or |
|
69 die("Can\'t write to temporary file $fitting"); |
|
70 print FH "!xform -t $xtrans $ytrans $ztrans -s $scale @ARGV"; |
|
71 close FH; |
|
72 |
|
73 |
|
74 # Material for the room |
|
75 open(FH, ">$material") or |
|
76 die("Can\'t write to temporary file $material"); |
|
77 print FH "void plastic wall_mat 0 0 5 .5 .5 .5 0 0"; |
|
78 close FH; |
|
79 |
|
80 |
|
81 # Different 'room' geometry for different views |
|
82 my $OFFSET = 0.1; |
|
83 open(FH, ">$testroom1") or die("Can\'t write to temporary file $testroom1"); |
|
84 # C0-C180 |
|
85 print FH "wall_mat polygon box.4620 0 0 12 -$OFFSET -5 5 -$OFFSET 5 5 -$OFFSET 5 -5 -$OFFSET -5 -5"; |
|
86 close(FH); |
|
87 |
|
88 open(FH, ">$testroom2") or die("Can\'t write to temporary file $testroom2"); |
|
89 # C90-C270 |
|
90 print FH "wall_mat polygon box.1540 0 0 12 5 $OFFSET -5 5 $OFFSET 5 -5 $OFFSET 5 -5 $OFFSET -5"; |
|
91 close(FH); |
|
92 |
|
93 open(FH, ">$testroom3") or die("Can\'t write to temporary file $testroom3"); |
|
94 # Lower hemisphere |
|
95 print FH "wall_mat bubble lower 0 0 4 0 0 $dims[4] 5"; |
|
96 close(FH); |
|
97 |
|
98 open(FH, ">$testroom4") or die("Can\'t write to temporary file $testroom4"); |
|
99 # Upper hemisphere |
|
100 print FH "wall_mat bubble upper 0 0 4 0 0 $dims[5] 5"; |
|
101 close(FH); |
|
102 |
|
103 |
|
104 # Get bbox again, for the translated and scaled fitting. |
|
105 $dimstr = `getbbox -h $fitting`; |
|
106 chomp $dimstr; |
|
107 # Values returned by getbbox are indented and delimited with multiple spaces. |
|
108 $dimstr =~ s/^\s+//; # remove leading spaces |
|
109 @dims = split(/\s+/, $dimstr); # convert to array |
|
110 |
|
111 # Define the four views |
|
112 my $vw1 = "-vtl -vp 4.5 0 0 -vd -1 0 0 -vh 10 -vv 10"; |
|
113 my $vw2 = "-vtl -vp 0 -4.5 0 -vd 0 1 0 -vh 10 -vv 10"; |
|
114 my $zcent3 = $dims[4] - $tiny; |
|
115 my $vw3 = "-vta -vp 0 0 $zcent3 -vd 0 0 -1 -vu 0 1 0 -vh 180 -vv 180"; |
|
116 my $zcent4 = $dims[5] + $tiny; |
|
117 my $vw4 = "-vta -vp 0 0 $zcent4 -vd 0 0 1 -vu 0 1 0 -vh 180 -vv 180"; |
|
118 |
|
119 system "oconv $material $testroom1 $fitting > $octree1"; |
|
120 system "oconv $material $testroom2 $fitting > $octree2"; |
|
121 system "oconv $material $testroom3 $fitting > $octree3"; |
|
122 system "oconv $material $testroom4 $fitting > $octree4"; |
|
123 |
|
124 # Render four different views of the objects |
|
125 system "$rpict_cmd $vw1 $octree1 > $td/right.hdr"; |
|
126 system "$rpict_cmd $vw2 $octree2 > $td/front.hdr"; |
|
127 system "$rpict_cmd $vw3 $octree3 > $td/down.hdr"; |
|
128 system "$rpict_cmd $vw4 $octree4 > $td/up.hdr"; |
|
129 |
|
130 # Compose the four views into one image |
|
131 my $cmd; |
|
132 my $vtl = "$td/vtl.hdr"; |
|
133 my $vta = "$td/vta.hdr"; |
|
134 |
|
135 # Auto-expose right/front and down/up pairs separately |
|
136 system "pcompos $td/right.hdr 0 0 $td/front.hdr $xres 0 |$pfilt_cmd > $vtl"; |
|
137 system "pcompos $td/down.hdr 0 0 $td/up.hdr $xres 0 |$pfilt_cmd > $vta"; |
|
138 my $yres2 = $yres / 2; |
|
139 exec "pcompos $vtl 0 0 $vta 0 $yres2"; |
|
140 |
|
141 #EOF |