10 use strict; |
10 use strict; |
11 use warnings; |
11 use warnings; |
12 use Math::Trig; |
12 use Math::Trig; |
13 use File::Temp qw/ tempdir /; |
13 use File::Temp qw/ tempdir /; |
14 |
14 |
15 my $td = tempdir( CLEANUP => 1 ); |
15 my $td = tempdir( CLEANUP => 1 ); |
16 my $oct = "$td/ltview.oct"; |
16 my $oct = "$td/ltview.oct"; |
17 my $room = "$td/room.rad"; |
17 my $room = "$td/room.rad"; |
18 my $box; # Overall box dimensions |
18 my $box; # Overall box dimensions |
19 my $rif = "$td/ltview.rif"; |
19 my $rif = "$td/ltview.rif"; |
20 my $lumi = "$td/lumi.rad"; # Fitting as given on cmd line, or generated by ies2rad |
20 my $lumi = "$td/lumi.rad"; # Fitting as given on cmd line, or generated by ies2rad |
21 my $lumi2 = "$td/lumi2.rad"; # Fitting centred at origin |
21 my $lumi2 = "$td/lumi2.rad"; # Fitting centred at origin |
22 my $raddev = "x11"; # Default output device. Overwrite with -o |
22 my $raddev = "x11"; # Default output device. Overwrite with -o |
23 my $is_ies = 0; # Input file is IES photometry, not a Radiance luminaire |
23 my $is_ies = 0; # Input file is IES photometry, not a Radiance luminaire |
24 |
24 |
25 while (@ARGV) { |
25 while (@ARGV) { |
26 $_ = $ARGV[0]; |
26 $_ = $ARGV[0]; |
27 if (m/-i/) { |
27 if (m/-i/) { |
28 $is_ies = 1; |
28 $is_ies = 1; |
29 } elsif (m/-o/) { # output device (rvu -devices) |
29 } elsif (m/-o/) { # output device (rvu -devices) |
30 $raddev = $ARGV[1]; |
30 $raddev = $ARGV[1]; |
31 shift @ARGV; |
31 shift @ARGV; |
32 } elsif (m/-b/) { |
32 } elsif (m/-b/) { |
33 $box = $ARGV[1]; # Box dimensions |
33 $box = $ARGV[1]; # Box dimensions |
34 shift @ARGV; |
34 shift @ARGV; |
35 } elsif (m/^-\w/) { |
35 } elsif (m/^-\w/) { |
36 die("ltview: Bad option: '$_'\n"); |
36 die("ltview: Bad option: '$_'\n"); |
37 } else { |
37 } else { |
38 last; |
38 last; |
39 } |
39 } |
40 shift @ARGV; |
40 shift @ARGV; |
41 } |
41 } |
42 |
42 |
43 # We need exactly one Radiance luminaires or IES file |
43 # We need exactly one Radiance luminaires or IES file |
44 if (! $#ARGV == 0) { |
44 if ( !$#ARGV == 0 ) { |
45 die("ltview: Need one Radiance luminaire or IES file.\n"); |
45 die("ltview: Need one Radiance luminaire or IES file.\n"); |
46 } elsif ($is_ies == 0) { |
46 } elsif ( $is_ies == 0 ) { |
47 # Input file is a Radiance luminaire |
47 |
48 $lumi = $ARGV[0]; |
48 # Input file is a Radiance luminaire |
|
49 $lumi = $ARGV[0]; |
49 } else { |
50 } else { |
50 # Input file is IES photometry |
51 |
51 system qq[ ies2rad -p $td -o lumi "$ARGV[0]" ]; |
52 # Input file is IES photometry |
|
53 system qq[ ies2rad -p $td -o lumi "$ARGV[0]" ]; |
52 } |
54 } |
53 |
|
54 |
55 |
55 # Work out centre of luminaire |
56 # Work out centre of luminaire |
56 my $dimstr = `getbbox -h "$lumi"`; |
57 my $dimstr = `getbbox -h "$lumi"`; |
57 chomp $dimstr; |
58 chomp $dimstr; |
|
59 |
58 # Values returned by getbbox are indented and delimited with multiple spaces. |
60 # Values returned by getbbox are indented and delimited with multiple spaces. |
59 $dimstr =~ s/^\s+//; # remove leading spaces |
61 $dimstr =~ s/^\s+//; # remove leading spaces |
60 my @dims = split(/\s+/, $dimstr); # convert to array |
62 my @dims = split( /\s+/, $dimstr ); # convert to array |
61 |
63 |
62 # Find largest axes-aligned luminaire dimension |
64 # Find largest axes-aligned luminaire dimension |
63 # The box will be ten times as large, unless overwritten with -b option. |
65 # The box will be ten times as large, unless overwritten with -b option. |
64 my @diffs = reverse sort { $a <=> $b } ($dims[1]-$dims[0], $dims[3]-$dims[2], $dims[5]-$dims[4]); |
66 my @diffs = reverse sort { $a <=> $b } |
|
67 ( $dims[1] - $dims[0], $dims[3] - $dims[2], $dims[5] - $dims[4] ); |
65 my $lsize = $diffs[0]; |
68 my $lsize = $diffs[0]; |
66 |
69 |
67 # Centre fitting at origin |
70 # Centre fitting at origin |
68 my $xtrans = -1.0 * ($dims[0] + $dims[1]) / 2; |
71 my $xtrans = -1.0 * ( $dims[0] + $dims[1] ) / 2; |
69 my $ytrans = -1.0 * ($dims[2] + $dims[3]) / 2; |
72 my $ytrans = -1.0 * ( $dims[2] + $dims[3] ) / 2; |
70 my $ztrans = -1.0 * ($dims[4] + $dims[5]) / 2; |
73 my $ztrans = -1.0 * ( $dims[4] + $dims[5] ) / 2; |
71 system qq[ xform -t $xtrans $ytrans $ztrans "$lumi" > $lumi2 ]; |
74 system qq[ xform -t $xtrans $ytrans $ztrans "$lumi" > $lumi2 ]; |
72 |
|
73 |
75 |
74 # Make the enclosing box |
76 # Make the enclosing box |
75 my $b2; |
77 my $b2; |
76 if (defined $box) { |
78 if ( defined $box ) { |
77 # Room dimensions are giving explicitly. |
79 |
78 $b2 = $box / 2; |
80 # Room dimensions are giving explicitly. |
|
81 $b2 = $box / 2; |
79 } else { |
82 } else { |
80 # Box dimensions are ten times largest luminaire bbox dimensions. |
83 |
81 $b2 = $lsize * 10.0 / 2.0; |
84 # Box dimensions are ten times largest luminaire bbox dimensions. |
|
85 $b2 = $lsize * 10.0 / 2.0; |
82 } |
86 } |
83 |
87 |
84 open(FH, ">$room") or |
88 open( FH, ">$room" ) |
85 die("ltview: Can't write to temporary file '$room'\n"); |
89 or die("ltview: Can't write to temporary file '$room'\n"); |
86 print FH <<EndOfRoom; |
90 print FH <<EndOfRoom; |
87 void plastic wall_mat 0 0 5 .2 .2 .2 0 0 |
91 void plastic wall_mat 0 0 5 .2 .2 .2 0 0 |
88 |
92 |
89 # Don't generate -y face so we can look into the box |
93 # Don't generate -y face so we can look into the box |
90 #wall_mat polygon box.1540 0 0 12 $b2 -$b2 -$b2 $b2 -$b2 $b2 -$b2 -$b2 $b2 -$b2 -$b2 -$b2 |
94 #wall_mat polygon box.1540 0 0 12 $b2 -$b2 -$b2 $b2 -$b2 $b2 -$b2 -$b2 $b2 -$b2 -$b2 -$b2 |
95 wall_mat polygon box.6457 0 0 12 -$b2 $b2 $b2 -$b2 -$b2 $b2 $b2 -$b2 $b2 $b2 $b2 $b2 |
99 wall_mat polygon box.6457 0 0 12 -$b2 $b2 $b2 -$b2 -$b2 $b2 $b2 -$b2 $b2 $b2 $b2 $b2 |
96 EndOfRoom |
100 EndOfRoom |
97 close(FH); |
101 close(FH); |
98 |
102 |
99 my $scene = "$room $lumi2"; |
103 my $scene = "$room $lumi2"; |
|
104 |
100 # Make this work under Windoze |
105 # Make this work under Windoze |
101 if ( $^O =~ /MSWin32/ ) { |
106 if ( $^O =~ /MSWin32/ ) { |
102 $scene =~ s{\\}{/}g; |
107 $scene =~ s{\\}{/}g; |
103 $oct =~ s{\\}{/}g; |
108 $oct =~ s{\\}{/}g; |
104 $raddev = "qt"; |
109 $raddev = "qt"; |
105 } |
110 } |
106 |
111 |
107 # Tweak scene bounding box so we get a nice view covering all of the box, without |
112 # Tweak scene bounding box so we get a nice view covering all of the box, without |
108 # having a wasteful black border around it. Must work for arbitrary box dims. |
113 # having a wasteful black border around it. Must work for arbitrary box dims. |
109 my $zone = 1.1 * $b2 * ( 1 + 1/tan(22.5*pi/180) ); |
114 my $zone = 1.1 * $b2 * ( 1 + 1 / tan( 22.5 * pi / 180 ) ); |
110 |
115 |
111 open(FH, ">$rif") or |
116 open( FH, ">$rif" ) |
112 die("ltview: Can't write to temporary file '$rif'\n"); |
117 or die("ltview: Can't write to temporary file '$rif'\n"); |
113 print FH <<EndOfRif; |
118 print FH <<EndOfRif; |
114 scene= $scene |
119 scene= $scene |
115 ZONE= Interior -$zone $zone -$zone $zone -$zone $zone |
120 ZONE= Interior -$zone $zone -$zone $zone -$zone $zone |
116 UP= Z |
121 UP= Z |
117 view= y |
122 view= y |