ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/genbackg.csh
Revision: 2.2
Committed: Mon Aug 25 04:50:32 2008 UTC (15 years, 8 months ago) by greg
Content type: application/x-csh
Branch: MAIN
CVS Tags: rad5R4, rad5R2, rad4R2P2, rad5R0, rad5R1, rad4R2, rad4R1, rad4R0, rad4R2P1, rad5R3, HEAD
Changes since 2.1: +16 -12 lines
Log Message:
Made temporary files safe from malicious attackers

File Contents

# Content
1 #!/bin/csh -f
2 # RCSid: $Id: genbackg.csh,v 2.1 2003/02/22 02:07:23 greg Exp $
3 #
4 # Generate height field to surround a mesh with an irregular border
5 #
6 # 4/16/2002 Greg Ward
7 #
8 if ( $#argv < 3 ) goto userr
9 # Get arguments
10 set mat = $1
11 set nam = $2
12 set mesh = $3
13 # Get options
14 set smooth=""
15 set i = 4
16 while ( $i <= $#argv )
17 switch ($argv[$i])
18 case -c:
19 @ i++ ; set a=$i
20 @ i++ ; set b=$i
21 @ i++ ; set c=$i
22 set c00=($argv[$a] $argv[$b] $argv[$c])
23 @ i++ ; set a=$i
24 @ i++ ; set b=$i
25 @ i++ ; set c=$i
26 set c01=($argv[$a] $argv[$b] $argv[$c])
27 @ i++ ; set a=$i
28 @ i++ ; set b=$i
29 @ i++ ; set c=$i
30 set c11=($argv[$a] $argv[$b] $argv[$c])
31 @ i++ ; set a=$i
32 @ i++ ; set b=$i
33 @ i++ ; set c=$i
34 set c10=($argv[$a] $argv[$b] $argv[$c])
35 unset a b c
36 breaksw
37 case -r:
38 @ i++
39 set step = $argv[$i]
40 breaksw
41 case -b:
42 @ i++
43 set bord = $argv[$i]
44 breaksw
45 case -s:
46 set smooth="-s"
47 breaksw
48 default:
49 goto userr
50 endsw
51 @ i++
52 end
53 # Get corner points
54 set lim=(`getbbox -h $mesh`)
55 if (! $?bord ) then
56 set bord=`ev "($lim[2]-$lim[1]+$lim[4]-$lim[3])/20"`
57 endif
58 if (! $?c00 ) then
59 set c00=(`ev "$lim[1]-$bord" "$lim[3]-$bord" "($lim[5]+$lim[6])/2"`)
60 set c01=(`ev "$lim[2]+$bord" "$lim[3]-$bord" "($lim[5]+$lim[6])/2"`)
61 set c10=(`ev "$lim[1]-$bord" "$lim[4]+$bord" "($lim[5]+$lim[6])/2"`)
62 set c11=(`ev "$lim[2]+$bord" "$lim[4]+$bord" "($lim[5]+$lim[6])/2"`)
63 endif
64 # Get mesh resolution
65 if (! $?step ) then
66 set step=`ev "sqrt(($c11[1]-$c00[1])*($c11[1]-$c00[1])+($c11[2]-$c00[2])*($c11[2]-$c00[2]))/70"`
67 endif
68 set res=(`ev "ceil(sqrt(($c01[1]-$c00[1])*($c01[1]-$c00[1])+($c01[2]-$c00[2])*($c01[2]-$c00[2]))/$step)" "ceil(sqrt(($c10[1]-$c00[1])*($c10[1]-$c00[1])+($c10[2]-$c00[2])*($c10[2]-$c00[2]))/$step)"`)
69 set edgef=`mktemp /tmp/edgerad.XXXXXX`
70 cat > $edgef << _EOF_
71 void sphere c00
72 0 0 4
73 $c00 .001
74 void sphere c01
75 0 0 4
76 $c01 .001
77 void sphere c10
78 0 0 4
79 $c10 .001
80 void sphere c11
81 0 0 4
82 $c11 .001
83 void cylinder ex0
84 0 0 7
85 $c00 $c10 .001
86 void cylinder ex1
87 0 0 7
88 $c01 $c11 .001
89 void cylinder ey0
90 0 0 7
91 $c00 $c01 .001
92 void cylinder ey1
93 0 0 7
94 $c10 $c11 .001
95 _EOF_
96 set meshcal=`mkdir /tmp/meshcal.XXXXXX`
97 cat > $meshcal << _EOF_
98 lerp(x,y0,y1):(1-x)*y0+x*y1;
99 and(a,b):if(a,b,-1);
100 or(a,b):if(a,1,b);
101 not(a):if(a,-1,1);
102 max(a,b):if(a-b,a,b);
103 min(a,b):if(a-b,b,a);
104 EPS:1e-7;
105 XR:min(32,floor($res[1]/2));
106 YR:8;
107 eq(a,b):and(a-b+EPS,b-a+EPS);
108 sumfun2x(f,y,x0,x1):if(x1-x0+EPS,f(x0,y)+sumfun2x(f,y,x0+1,x1),0);
109 sumfun2(f,x0,x1,y0,y1):if(y1-y0+EPS,
110 sumfun2x(f,y0,x0,x1)+sumfun2(f,x0,x1,y0+1,y1), 0);
111 xpos(xf,yf)=lerp(xf,lerp(yf,$c00[1],$c10[1]),lerp(yf,$c01[1],$c11[1]));
112 ypos(xf,yf)=lerp(xf,lerp(yf,$c00[2],$c10[2]),lerp(yf,$c01[2],$c11[2]));
113 onedge=or(or(eq(x,0),eq(x,xmax-1)),or(eq(y,0),eq(y,ymax-1)));
114 z0:$lim[5]-$bord;
115 height(xo,yo)=z0+gi(1,xo,yo);
116 maxwt(xo,yo):if(and(eq(xo,0),eq(yo,0)),100,1/(xo*xo+yo*yo));
117 wt(xo,yo)=if(height(xo,yo)-1e9, 0, maxwt(xo,yo));
118 wtv(xo,yo)=wt(xo,yo)*height(xo,yo);
119 wts=sumfun2(wt,-XR,XR,-YR,YR);
120 sum=sumfun2(wtv,-XR,XR,-YR,YR);
121 inmesh=and(not(onedge),eq(sumfun2(wt,-1,1,-1,1),sumfun2(maxwt,-1,1,-1,1)));
122 lo=if(inmesh,1e10,if(wts-EPS,sum/wts,($lim[5]+$lim[6])/2));
123 _EOF_
124 # Generate height image
125 set meshoct=`/tmp/meshoct.XXXXXXX`
126 xform -m void $mesh | oconv - $edgef > $meshoct
127 set meshpic=`mktemp /tmp/meshpic.XXXXXX`
128 cnt $res[2] $res[1] \
129 | rcalc -e 'sp=$2/'"($res[1]-1)" -e 'tp=1-$1/'"($res[2]-1)" \
130 -f $meshcal -e "xp=xpos(sp,tp);yp=ypos(sp,tp)" \
131 -e '$1=xp;$2=yp;$3=z0;$4=0;$5=0;$6=1' \
132 | rtrace -w -faf -oL -x $res[1] -y $res[2] $meshoct \
133 | pvalue -r -b -df > $meshpic
134 rm $edgef $meshoct
135 # Output mesh surround
136 cat << _EOF_
137 # $0 $argv[*]
138 # Corner points:
139 # $c00
140 # $c01
141 # $c11
142 # $c10
143 _EOF_
144 pflip -v $meshpic \
145 | pcomb -f $meshcal - \
146 | pvalue -h -H -pG -d \
147 | gensurf $mat $nam 'xpos(s,t)' 'ypos(s,t)' - \
148 `ev $res[1]-1 $res[2]-1` \
149 -e 'valid(xf,yf)=1e9-Z`SYS(xf,yf)' \
150 -f $meshcal $smooth
151 rm $meshcal $meshpic
152 # All done -- exit
153 exit 0
154 # Usage error message
155 userr:
156 echo Usage: $0 "mat name mesh_file [-c corners][-b bord_width][-r step_size][-s]"
157 exit 1