| 1 | greg | 2.1 | #!/usr/bin/perl -w | 
| 2 | greg | 2.23 | # RCSid $Id: rtpict.pl,v 2.22 2022/04/11 04:03:36 greg Exp $ | 
| 3 | greg | 2.1 | # | 
| 4 |  |  | # Run rtrace in parallel mode to simulate rpict -n option | 
| 5 | greg | 2.8 | # May also be used to render layered images with -o* option | 
| 6 | greg | 2.1 | # | 
| 7 |  |  | #       G. Ward | 
| 8 |  |  | # | 
| 9 |  |  | use strict; | 
| 10 | greg | 2.8 | # we'll run rpict if no -n or -o* option | 
| 11 | greg | 2.1 | my $nprocs = 1; | 
| 12 |  |  | # rtrace options and the associated number of arguments | 
| 13 | greg | 2.9 | my %rtraceC = ('-dt',1, '-dc',1, '-dj',1, '-ds',1, '-dr',1, '-dp',1, | 
| 14 | greg | 2.16 | '-ss',1, '-st',1, '-e',1, '-am',1, '-P',1, '-PP',1, | 
| 15 | greg | 2.9 | '-ab',1, '-af',1, '-ai',1, '-aI',1, '-ae',1, '-aE',1, | 
| 16 |  |  | '-av',3, '-aw',1, '-aa',1, '-ar',1, '-ad',1, '-as',1, | 
| 17 | greg | 2.18 | '-me',3, '-ma',3, '-mg',1, '-ms',1, '-lr',1, '-lw',1, | 
| 18 |  |  | '-ap',2, '-am',1, '-ac',1, '-aC',1); | 
| 19 | greg | 2.1 | # boolean rtrace options | 
| 20 | greg | 2.9 | my @boolO = ('-w', '-bv', '-dv', '-i', '-u'); | 
| 21 | greg | 2.1 | # view options and the associated number of arguments | 
| 22 | greg | 2.9 | my %vwraysC = ('-vf',1, '-vtv',0, '-vtl',0, '-vth',0, '-vta',0, '-vts',0, '-vtc',0, | 
| 23 |  |  | '-x',1, '-y',1, '-vp',3, '-vd',3, '-vu',3, '-vh',1, '-vv',1, | 
| 24 | greg | 2.19 | '-vo',1, '-va',1, '-vs',1, '-vl',1, '-pa',1, '-pj',1, '-pd',1); | 
| 25 | greg | 2.2 | # options we need to silently ignore | 
| 26 | greg | 2.19 | my %ignoreC = ('-t',1, '-ps',1, '-pt',1, '-pm',1,); | 
| 27 | greg | 2.1 | # Starting options for rtrace (rpict values) | 
| 28 | greg | 2.9 | my @rtraceA = split(' ', 'rtrace -u- -dt .05 -dc .5 -ds .25 -dr 1 ' . | 
| 29 |  |  | '-aa .2 -ar 64 -ad 512 -as 128 -lr 7 -lw 1e-03'); | 
| 30 | greg | 2.22 | my @vwraysA = ('vwrays', '-pj', '.67'); | 
| 31 | greg | 2.9 | my @vwrightA = ('vwright', '-vtv'); | 
| 32 | greg | 2.17 | my @rpictA = ('rpict', '-ps', '1'); | 
| 33 | greg | 2.9 | my $outpatt = '^-o[vrxlLRXnNsmM]+'; | 
| 34 |  |  | my $refDepth = ""; | 
| 35 |  |  | my $irrad = 0; | 
| 36 | greg | 2.17 | my $persist = 0; | 
| 37 | greg | 2.20 | my $ambounce = 0; | 
| 38 |  |  | my $ambcache = 1; | 
| 39 |  |  | my $ambfile; | 
| 40 | greg | 2.8 | my $outlyr; | 
| 41 |  |  | my $outdir; | 
| 42 | greg | 2.1 | my $outpic; | 
| 43 | greg | 2.5 | my $outzbf; | 
| 44 | greg | 2.1 | OPTION:                         # sort through options | 
| 45 |  |  | while ($#ARGV >= 0 && "$ARGV[0]" =~ /^[-\@]/) { | 
| 46 |  |  | # Check for file inclusion | 
| 47 |  |  | if ("$ARGV[0]" =~ /^\@/) { | 
| 48 | greg | 2.14 | open my $handle, '<', substr($ARGV[0], 1) or die "No file for $ARGV[0]\n"; | 
| 49 | greg | 2.1 | shift @ARGV; | 
| 50 |  |  | chomp(my @args = <$handle>); | 
| 51 |  |  | close $handle; | 
| 52 |  |  | unshift @ARGV, split(/\s+/, "@args"); | 
| 53 |  |  | next OPTION; | 
| 54 |  |  | } | 
| 55 |  |  | # Check booleans | 
| 56 |  |  | for my $boopt (@boolO) { | 
| 57 | greg | 2.6 | if ("$ARGV[0]" =~ ('^' . $boopt . '[-+01tfynTFYN]?$')) { | 
| 58 | greg | 2.9 | if ("$ARGV[0]" eq '-i') { | 
| 59 | greg | 2.8 | $irrad = ! $irrad; | 
| 60 |  |  | } elsif ("$ARGV[0]" =~ /^-i[-+01tfynTFYN]/) { | 
| 61 |  |  | $irrad = ("$ARGV[0]" =~ /^-i[+1tyTY]/); | 
| 62 |  |  | } | 
| 63 | greg | 2.9 | push @rtraceA, $ARGV[0]; | 
| 64 |  |  | push @rpictA, shift(@ARGV); | 
| 65 | greg | 2.1 | next OPTION; | 
| 66 |  |  | } | 
| 67 |  |  | } | 
| 68 |  |  | # Check view options | 
| 69 |  |  | if (defined $vwraysC{$ARGV[0]}) { | 
| 70 | greg | 2.2 | push @vwraysA, $ARGV[0]; | 
| 71 | greg | 2.1 | my $isvopt = ("$ARGV[0]" =~ /^-v/); | 
| 72 | greg | 2.2 | push(@vwrightA, $ARGV[0]) if ($isvopt); | 
| 73 | greg | 2.1 | push @rpictA, shift(@ARGV); | 
| 74 | greg | 2.2 | for (my $i = $vwraysC{$rpictA[-1]}; $i-- > 0; ) { | 
| 75 |  |  | push @vwraysA, $ARGV[0]; | 
| 76 |  |  | push(@vwrightA, $ARGV[0]) if ($isvopt); | 
| 77 | greg | 2.1 | push @rpictA, shift(@ARGV); | 
| 78 |  |  | } | 
| 79 |  |  | next OPTION; | 
| 80 |  |  | } | 
| 81 |  |  | # Check rtrace options | 
| 82 |  |  | if (defined $rtraceC{$ARGV[0]}) { | 
| 83 | greg | 2.20 | if ("$ARGV[0]" =~ /^-PP?$/) { | 
| 84 |  |  | $persist = 1; | 
| 85 |  |  | } elsif ("$ARGV[0]" eq '-ab') { | 
| 86 |  |  | $ambounce = $ARGV[1]; | 
| 87 |  |  | } elsif ("$ARGV[0]" eq '-aa') { | 
| 88 |  |  | $ambcache = ($ARGV[1] > 0.0); | 
| 89 |  |  | } elsif ("$ARGV[0]" eq '-af') { | 
| 90 |  |  | $ambfile = "$ARGV[1]"; | 
| 91 |  |  | } | 
| 92 | greg | 2.2 | push @rtraceA, $ARGV[0]; | 
| 93 | greg | 2.1 | push @rpictA, shift(@ARGV); | 
| 94 | greg | 2.2 | for (my $i = $rtraceC{$rpictA[-1]}; $i-- > 0; ) { | 
| 95 |  |  | push @rtraceA, $ARGV[0]; | 
| 96 | greg | 2.1 | push @rpictA, shift(@ARGV); | 
| 97 |  |  | } | 
| 98 |  |  | next OPTION; | 
| 99 |  |  | } | 
| 100 | greg | 2.2 | # Check options to ignore | 
| 101 |  |  | if (defined $ignoreC{$ARGV[0]}) { | 
| 102 |  |  | push @rpictA, shift(@ARGV); | 
| 103 |  |  | for (my $i = $ignoreC{$rpictA[-1]}; $i-- > 0; ) { | 
| 104 |  |  | push @rpictA, shift(@ARGV); | 
| 105 |  |  | } | 
| 106 | greg | 2.1 | next OPTION; | 
| 107 |  |  | } | 
| 108 |  |  | # Check for output file or number of processes | 
| 109 | greg | 2.9 | if ("$ARGV[0]" eq '-o') { | 
| 110 | greg | 2.1 | shift @ARGV; | 
| 111 |  |  | $outpic = shift(@ARGV); | 
| 112 | greg | 2.9 | } elsif ("$ARGV[0]" eq '-z') { | 
| 113 | greg | 2.5 | push @rpictA, shift(@ARGV); | 
| 114 |  |  | $outzbf = $ARGV[0]; | 
| 115 |  |  | push @rpictA, shift(@ARGV); | 
| 116 | greg | 2.9 | } elsif ("$ARGV[0]" eq '-n') { | 
| 117 | greg | 2.1 | shift @ARGV; | 
| 118 |  |  | $nprocs = shift(@ARGV); | 
| 119 | greg | 2.9 | } elsif ("$ARGV[0]" eq '-d') { | 
| 120 | greg | 2.8 | shift @ARGV; | 
| 121 | greg | 2.9 | $refDepth = ' -d ' . shift(@ARGV); | 
| 122 | greg | 2.8 | } elsif ("$ARGV[0]" =~ "$outpatt") { | 
| 123 |  |  | push @rtraceA, $ARGV[0]; | 
| 124 |  |  | $outlyr = substr(shift(@ARGV), 2); | 
| 125 |  |  | $outdir = shift(@ARGV); | 
| 126 | greg | 2.1 | } else { | 
| 127 | greg | 2.2 | die "Unsupported option: " . $ARGV[0] . "\n"; | 
| 128 | greg | 2.1 | } | 
| 129 |  |  | } | 
| 130 |  |  | die "Number of processes must be positive" if ($nprocs <= 0); | 
| 131 | greg | 2.8 | if (defined $outdir) {          # check conflicting options | 
| 132 |  |  | die "Options -o and -o* are mutually exclusive\n" if (defined $outpic); | 
| 133 |  |  | die "Options -z and -o* are mutually exclusive\n" if (defined $outzbf); | 
| 134 |  |  | } | 
| 135 | greg | 2.4 | if (defined $outpic) {          # redirect output? | 
| 136 |  |  | die "File '$outpic' already exists\n" if (-e $outpic); | 
| 137 | greg | 2.1 | open STDOUT, '>', "$outpic"; | 
| 138 |  |  | } | 
| 139 | greg | 2.9 | ##################################################################### | 
| 140 |  |  | ##### May as well run rpict? | 
| 141 | greg | 2.17 | if ($nprocs == 1 && $persist == 0 && !defined($outdir)) { | 
| 142 | greg | 2.2 | push(@rpictA, $ARGV[0]) if ($#ARGV == 0); | 
| 143 | greg | 2.1 | exec @rpictA ; | 
| 144 |  |  | } | 
| 145 | greg | 2.9 | # OK, we're running rtrace in some capacity... | 
| 146 |  |  | push @rtraceA, ('-n', "$nprocs"); | 
| 147 | greg | 2.1 | die "Need single octree argument\n" if ($#ARGV != 0); | 
| 148 | greg | 2.5 | my $oct = $ARGV[0]; | 
| 149 |  |  | my $view = `@vwrightA 0`; | 
| 150 | greg | 2.9 | chomp $view; | 
| 151 | greg | 2.5 | my @res = split(/\s/, `@vwraysA -d`); | 
| 152 | greg | 2.9 | ##################################################################### | 
| 153 | greg | 2.22 | ##### Resort pixels to reduce ambient cache collisions? | 
| 154 | greg | 2.20 | if ($nprocs > 1 && $ambounce > 0 && $ambcache && defined($ambfile)) { | 
| 155 | greg | 2.22 | if (!defined($outzbf) && !defined($outdir)) { | 
| 156 |  |  | # Straight picture output, so just randomize sample order | 
| 157 |  |  | system "cnt $res[1] $res[3] | sort -R > /tmp/ord$$.txt"; | 
| 158 |  |  | die "sort error\n" if ( $? ); | 
| 159 |  |  | system "@vwraysA -ff -i < /tmp/ord$$.txt " . | 
| 160 |  |  | "| @rtraceA -ffa -ov '$oct' > /tmp/pix$$.txt"; | 
| 161 |  |  | die "Error running rtrace\n" if ( $? ); | 
| 162 | greg | 2.23 | system "( getinfo < /tmp/pix$$.txt | getinfo -a 'VIEW=$view'; " . | 
| 163 |  |  | "getinfo - < /tmp/pix$$.txt | rlam /tmp/ord$$.txt - " . | 
| 164 |  |  | "| sort -k2rn -k1n ) | pvalue -r -Y $res[3] +X $res[1]"; | 
| 165 | greg | 2.22 | die "rlam error\n" if ( $? ); | 
| 166 |  |  | unlink ("/tmp/ord$$.txt", "/tmp/pix$$.txt"); | 
| 167 |  |  | exit 0; | 
| 168 |  |  | } | 
| 169 |  |  | # Else randomize overture calculation to prime ambient cache | 
| 170 | greg | 2.21 | my $oxres = int($res[1]/6); | 
| 171 |  |  | my $oyres = int($res[3]/6); | 
| 172 | greg | 2.20 | print STDERR "Running $oxres by $oyres overture calculation " . | 
| 173 |  |  | "to populate '$ambfile'...\n"; | 
| 174 | greg | 2.22 | system "@vwraysA -x $oxres -y $oyres -pj 0 " . | 
| 175 | greg | 2.21 | "| sort -R | @rtraceA -faf -ov '$oct' > /dev/null"; | 
| 176 | greg | 2.20 | die "Failure running overture\n" if ( $? ); | 
| 177 |  |  | print STDERR "Finished overture.\n"; | 
| 178 |  |  | } | 
| 179 |  |  | ##################################################################### | 
| 180 | greg | 2.9 | ##### Generating picture with depth buffer? | 
| 181 |  |  | if (defined $outzbf) { | 
| 182 | greg | 2.22 | exec "@vwraysA -ff | @rtraceA -fff -olv @res '$oct' | " . | 
| 183 | greg | 2.13 | "rsplit -ih -iH -f -of '$outzbf' -oh -oH -of3 - | " . | 
| 184 | greg | 2.11 | "pvalue -r -df | getinfo -a 'VIEW=$view'"; | 
| 185 | greg | 2.5 | } | 
| 186 | greg | 2.9 | ##################################################################### | 
| 187 |  |  | ##### Base case with output picture only? | 
| 188 |  |  | if (! defined $outdir) { | 
| 189 | greg | 2.22 | exec "@vwraysA -ff | @rtraceA -ffc @res '$oct' | getinfo -a 'VIEW=$view'"; | 
| 190 | greg | 2.9 | } | 
| 191 |  |  | ##################################################################### | 
| 192 |  |  | ##### Layered image output case | 
| 193 |  |  | my @rsplitA = ("rsplit", "'-t   '", "-ih", "-iH", "-oh", "-oH"); | 
| 194 |  |  | # Supported rtrace -o* options and output file name.typ | 
| 195 |  |  | my %rtoutC = ( | 
| 196 |  |  | v =>    'radiance.hdr', | 
| 197 | greg | 2.10 | r =>    'r_refl.hdr', | 
| 198 |  |  | x =>    'r_unrefl.hdr', | 
| 199 |  |  | l =>    'd_effective.dpt', | 
| 200 |  |  | L =>    'd_firstsurf.dpt', | 
| 201 |  |  | R =>    'd_refl.dpt', | 
| 202 |  |  | X =>    'd_unrefl.dpt', | 
| 203 | greg | 2.9 | n =>    'perturbed.nrm', | 
| 204 |  |  | N =>    'unperturbed.nrm', | 
| 205 |  |  | s =>    'surface.idx', | 
| 206 |  |  | m =>    'modifier.idx', | 
| 207 |  |  | M =>    'material.idx' | 
| 208 |  |  | ); | 
| 209 |  |  | # Arguments for rsplit based on output file type | 
| 210 |  |  | my %rcodeC = ( | 
| 211 |  |  | '.hdr', ['-of3', '!pvalue -r -df -u'], | 
| 212 |  |  | '.dpt', ['-of', "!rcode_depth$refDepth -ff"], | 
| 213 |  |  | '.nrm', ['-of3', '!rcode_norm -ff'], | 
| 214 |  |  | '.idx', ['-oa', '!rcode_ident "-t       "'] | 
| 215 |  |  | ); | 
| 216 |  |  | if ($irrad) {           # adjust actions for -i+ option | 
| 217 |  |  | $rtoutC{'v'} = 'irradiance.hdr'; | 
| 218 |  |  | delete $rtoutC{'r'}; | 
| 219 |  |  | delete $rtoutC{'x'}; | 
| 220 |  |  | delete $rtoutC{'R'}; | 
| 221 |  |  | delete $rtoutC{'X'}; | 
| 222 |  |  | } | 
| 223 |  |  | if (! -d $outdir) { | 
| 224 |  |  | mkdir $outdir or die("Cannot create output directory '$outdir'\n"); | 
| 225 |  |  | } | 
| 226 |  |  | # construct rsplit command | 
| 227 |  |  | foreach my $oval (split //, $outlyr) { | 
| 228 |  |  | die "Duplicate or unsupported type '$oval' in -o$outlyr\n" | 
| 229 |  |  | if (!defined $rtoutC{$oval}); | 
| 230 | greg | 2.15 | my $outfile = "$outdir/$rtoutC{$oval}"; | 
| 231 |  |  | die "File '$outfile' already exists\n" if (-e $outfile); | 
| 232 | greg | 2.9 | my ($otyp) = ($rtoutC{$oval} =~ /(\.[^.]+)$/); | 
| 233 |  |  | push @rsplitA, $rcodeC{$otyp}[0]; | 
| 234 | greg | 2.15 | push @rsplitA, qq{'$rcodeC{$otyp}[1] > "$outfile"'}; | 
| 235 | greg | 2.9 | delete $rtoutC{$oval}; | 
| 236 |  |  | } | 
| 237 |  |  | # call rtrace + rsplit | 
| 238 | greg | 2.22 | exec "@vwraysA -ff | @rtraceA -fff @res '$oct' | getinfo -a 'VIEW=$view' | @rsplitA"; |