#!/usr/bin/perl
# Make a nice multi-view picture of a luminaire
#
# This is based on objpict.pl that renders four-view
# images of objects that are not light sources.
#
# Written by Axel Jacobs,
# with input from Rob Guglielmetti and Gre
use strict;
use warnings;
use File::Temp qw/ tempdir /;
#my $td = tempdir( CLEANUP => 1 );
my $td = "tmp";
#my $xres = 1024;
#my $yres = 1024;
my $xres = 600;
my $yres = 600;
my $tiny = 0.01;
my $rpict_cmd = "rpict -ab 0 -ds 0 -dv -av 0 0 0 -x $xres -y $yres";
my $pfilt_cmd = "pfilt -r .6 -x /2 -y /2";
my $material = "$td/lt.mat";
my $testroom1 = "$td/testroom1.rad";
my $testroom2 = "$td/testroom2.rad";
my $testroom3 = "$td/testroom3.rad";
my $testroom4 = "$td/testroom4.rad";
my $octree1 = "$td/lt1.oct";
my $octree2 = "$td/lt2.oct";
my $octree3 = "$td/lt3.oct";
my $octree4 = "$td/lt4.oct";
# We need at least one Radiance file or a scene on STDIN (but not both)
if ($#ARGV < 0) {
open(FH, ">$td/stdin.rad") or
die("objview: Can't write to temporary file $td/stdin.rad\n");
while (<>) {
print FH;
}
# Pretend stdin.rad was passed as argument.
@ARGV = ("$td/stdin.rad");
}
# Scale fitting and center at origin
my $dimstr = `getbbox -h @ARGV`;
chomp $dimstr;
# Values returned by getbbox are indented and delimited with multiple spaces.
$dimstr =~ s/^\s+//; # remove leading spaces
my @dims = split(/\s+/, $dimstr); # convert to array
# Find largest axes-aligned dimension
my @diffs = ($dims[1]-$dims[0], $dims[3]-$dims[2], $dims[5]-$dims[4]);
@diffs = reverse sort { $a <=> $b } @diffs;
my $size = $diffs[0];
# Move fitting so centre is at origin
my $xtrans = -1.0 * ($dims[0] + $dims[1]) / 2;
my $ytrans = -1.0 * ($dims[2] + $dims[3]) / 2;
my $ztrans = -1.0 * ($dims[4] + $dims[5]) / 2;
# Scale so that largest object dimension is unity
my $scale = 1 / $size;
my $fitting = "$td/fitting.rad";
open(FH, ">$fitting") or
die("Can\'t write to temporary file $fitting");
print FH "!xform -t $xtrans $ytrans $ztrans -s $scale @ARGV";
close FH;
# Material for the room
open(FH, ">$material") or
die("Can\'t write to temporary file $material");
print FH "void plastic wall_mat 0 0 5 .5 .5 .5 0 0";
close FH;
# Different 'room' geometry for different views
my $OFFSET = 0.1;
open(FH, ">$testroom1") or die("Can\'t write to temporary file $testroom1");
# C0-C180
print FH "wall_mat polygon box.4620 0 0 12 -$OFFSET -5 5 -$OFFSET 5 5 -$OFFSET 5 -5 -$OFFSET -5 -5";
close(FH);
open(FH, ">$testroom2") or die("Can\'t write to temporary file $testroom2");
# C90-C270
print FH "wall_mat polygon box.1540 0 0 12 5 $OFFSET -5 5 $OFFSET 5 -5 $OFFSET 5 -5 $OFFSET -5";
close(FH);
open(FH, ">$testroom3") or die("Can\'t write to temporary file $testroom3");
# Lower hemisphere
print FH "wall_mat bubble lower 0 0 4 0 0 $dims[4] 5";
close(FH);
open(FH, ">$testroom4") or die("Can\'t write to temporary file $testroom4");
# Upper hemisphere
print FH "wall_mat bubble upper 0 0 4 0 0 $dims[5] 5";
close(FH);
# Get bbox again, for the translated and scaled fitting.
$dimstr = `getbbox -h $fitting`;
chomp $dimstr;
# Values returned by getbbox are indented and delimited with multiple spaces.
$dimstr =~ s/^\s+//; # remove leading spaces
@dims = split(/\s+/, $dimstr); # convert to array
# Define the four views
my $vw1 = "-vtl -vp 4.5 0 0 -vd -1 0 0 -vh 10 -vv 10";
my $vw2 = "-vtl -vp 0 -4.5 0 -vd 0 1 0 -vh 10 -vv 10";
my $zcent3 = $dims[4] - $tiny;
my $vw3 = "-vta -vp 0 0 $zcent3 -vd 0 0 -1 -vu 0 1 0 -vh 180 -vv 180";
my $zcent4 = $dims[5] + $tiny;
my $vw4 = "-vta -vp 0 0 $zcent4 -vd 0 0 1 -vu 0 1 0 -vh 180 -vv 180";
system "oconv $material $testroom1 $fitting > $octree1";
system "oconv $material $testroom2 $fitting > $octree2";
system "oconv $material $testroom3 $fitting > $octree3";
system "oconv $material $testroom4 $fitting > $octree4";
# Render four different views of the objects
system "$rpict_cmd $vw1 $octree1 > $td/right.hdr";
system "$rpict_cmd $vw2 $octree2 > $td/front.hdr";
system "$rpict_cmd $vw3 $octree3 > $td/down.hdr";
system "$rpict_cmd $vw4 $octree4 > $td/up.hdr";
# Compose the four views into one image
my $cmd;
my $vtl = "$td/vtl.hdr";
my $vta = "$td/vta.hdr";
# Auto-expose right/front and down/up pairs separately
system "pcompos $td/right.hdr 0 0 $td/front.hdr $xres 0 |$pfilt_cmd > $vtl";
system "pcompos $td/down.hdr 0 0 $td/up.hdr $xres 0 |$pfilt_cmd > $vta";
my $yres2 = $yres / 2;
exec "pcompos $vtl 0 0 $vta 0 $yres2";
#EOF