| 1 | 
greg | 
2.1 | 
#!/bin/csh -f | 
| 2 | 
  | 
  | 
# RCSid: $Id$ | 
| 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 | 
  | 
  | 
cat > /tmp/edge$$.rad << _EOF_ | 
| 70 | 
  | 
  | 
void sphere c00 | 
| 71 | 
  | 
  | 
0 0 4 | 
| 72 | 
  | 
  | 
$c00 .001 | 
| 73 | 
  | 
  | 
void sphere c01 | 
| 74 | 
  | 
  | 
0 0 4 | 
| 75 | 
  | 
  | 
$c01 .001 | 
| 76 | 
  | 
  | 
void sphere c10 | 
| 77 | 
  | 
  | 
0 0 4 | 
| 78 | 
  | 
  | 
$c10 .001 | 
| 79 | 
  | 
  | 
void sphere c11 | 
| 80 | 
  | 
  | 
0 0 4 | 
| 81 | 
  | 
  | 
$c11 .001 | 
| 82 | 
  | 
  | 
void cylinder ex0 | 
| 83 | 
  | 
  | 
0 0 7 | 
| 84 | 
  | 
  | 
$c00 $c10 .001 | 
| 85 | 
  | 
  | 
void cylinder ex1 | 
| 86 | 
  | 
  | 
0 0 7 | 
| 87 | 
  | 
  | 
$c01 $c11 .001 | 
| 88 | 
  | 
  | 
void cylinder ey0 | 
| 89 | 
  | 
  | 
0 0 7 | 
| 90 | 
  | 
  | 
$c00 $c01 .001 | 
| 91 | 
  | 
  | 
void cylinder ey1 | 
| 92 | 
  | 
  | 
0 0 7 | 
| 93 | 
  | 
  | 
$c10 $c11 .001 | 
| 94 | 
  | 
  | 
_EOF_ | 
| 95 | 
  | 
  | 
cat > /tmp/mesh$$.cal << _EOF_ | 
| 96 | 
  | 
  | 
lerp(x,y0,y1):(1-x)*y0+x*y1; | 
| 97 | 
  | 
  | 
and(a,b):if(a,b,-1); | 
| 98 | 
  | 
  | 
or(a,b):if(a,1,b); | 
| 99 | 
  | 
  | 
not(a):if(a,-1,1); | 
| 100 | 
  | 
  | 
max(a,b):if(a-b,a,b); | 
| 101 | 
  | 
  | 
min(a,b):if(a-b,b,a); | 
| 102 | 
  | 
  | 
EPS:1e-7; | 
| 103 | 
  | 
  | 
XR:min(32,floor($res[1]/2)); | 
| 104 | 
  | 
  | 
YR:8; | 
| 105 | 
  | 
  | 
eq(a,b):and(a-b+EPS,b-a+EPS); | 
| 106 | 
  | 
  | 
sumfun2x(f,y,x0,x1):if(x1-x0+EPS,f(x0,y)+sumfun2x(f,y,x0+1,x1),0); | 
| 107 | 
  | 
  | 
sumfun2(f,x0,x1,y0,y1):if(y1-y0+EPS, | 
| 108 | 
  | 
  | 
        sumfun2x(f,y0,x0,x1)+sumfun2(f,x0,x1,y0+1,y1), 0); | 
| 109 | 
  | 
  | 
xpos(xf,yf)=lerp(xf,lerp(yf,$c00[1],$c10[1]),lerp(yf,$c01[1],$c11[1])); | 
| 110 | 
  | 
  | 
ypos(xf,yf)=lerp(xf,lerp(yf,$c00[2],$c10[2]),lerp(yf,$c01[2],$c11[2])); | 
| 111 | 
  | 
  | 
onedge=or(or(eq(x,0),eq(x,xmax-1)),or(eq(y,0),eq(y,ymax-1))); | 
| 112 | 
  | 
  | 
z0:$lim[5]-$bord; | 
| 113 | 
  | 
  | 
height(xo,yo)=z0+gi(1,xo,yo); | 
| 114 | 
  | 
  | 
maxwt(xo,yo):if(and(eq(xo,0),eq(yo,0)),100,1/(xo*xo+yo*yo)); | 
| 115 | 
  | 
  | 
wt(xo,yo)=if(height(xo,yo)-1e9, 0, maxwt(xo,yo)); | 
| 116 | 
  | 
  | 
wtv(xo,yo)=wt(xo,yo)*height(xo,yo); | 
| 117 | 
  | 
  | 
wts=sumfun2(wt,-XR,XR,-YR,YR); | 
| 118 | 
  | 
  | 
sum=sumfun2(wtv,-XR,XR,-YR,YR); | 
| 119 | 
  | 
  | 
inmesh=and(not(onedge),eq(sumfun2(wt,-1,1,-1,1),sumfun2(maxwt,-1,1,-1,1))); | 
| 120 | 
  | 
  | 
lo=if(inmesh,1e10,if(wts-EPS,sum/wts,($lim[5]+$lim[6])/2)); | 
| 121 | 
  | 
  | 
_EOF_ | 
| 122 | 
  | 
  | 
# Generate height image | 
| 123 | 
  | 
  | 
xform -m void $mesh | oconv - /tmp/edge$$.rad > /tmp/mesh$$.oct | 
| 124 | 
  | 
  | 
cnt $res[2] $res[1] \ | 
| 125 | 
  | 
  | 
        | rcalc -e 'sp=$2/'"($res[1]-1)" -e 'tp=1-$1/'"($res[2]-1)" \ | 
| 126 | 
  | 
  | 
        -f /tmp/mesh$$.cal -e "xp=xpos(sp,tp);yp=ypos(sp,tp)" \ | 
| 127 | 
  | 
  | 
        -e '$1=xp;$2=yp;$3=z0;$4=0;$5=0;$6=1' \ | 
| 128 | 
  | 
  | 
        | rtrace -w -faf -oL -x $res[1] -y $res[2] /tmp/mesh$$.oct \ | 
| 129 | 
  | 
  | 
        | pvalue -r -b -df > /tmp/mesh$$.pic | 
| 130 | 
  | 
  | 
rm /tmp/edge$$.rad /tmp/mesh$$.oct | 
| 131 | 
  | 
  | 
# Output mesh surround | 
| 132 | 
  | 
  | 
cat << _EOF_ | 
| 133 | 
  | 
  | 
# $0 $argv[*] | 
| 134 | 
  | 
  | 
# Corner points: | 
| 135 | 
  | 
  | 
#       $c00 | 
| 136 | 
  | 
  | 
#       $c01 | 
| 137 | 
  | 
  | 
#       $c11 | 
| 138 | 
  | 
  | 
#       $c10 | 
| 139 | 
  | 
  | 
_EOF_ | 
| 140 | 
  | 
  | 
pflip -v /tmp/mesh$$.pic \ | 
| 141 | 
  | 
  | 
        | pcomb -f /tmp/mesh$$.cal - \ | 
| 142 | 
  | 
  | 
        | pvalue -h -H -pG -d \ | 
| 143 | 
  | 
  | 
        | gensurf $mat $nam 'xpos(s,t)' 'ypos(s,t)' - \ | 
| 144 | 
  | 
  | 
                `ev $res[1]-1 $res[2]-1` \ | 
| 145 | 
  | 
  | 
                -e 'valid(xf,yf)=1e9-Z`SYS(xf,yf)' \ | 
| 146 | 
  | 
  | 
                -f /tmp/mesh$$.cal $smooth | 
| 147 | 
  | 
  | 
rm /tmp/mesh$$.cal /tmp/mesh$$.pic | 
| 148 | 
  | 
  | 
# All done -- exit | 
| 149 | 
  | 
  | 
exit 0 | 
| 150 | 
  | 
  | 
# Usage error message | 
| 151 | 
  | 
  | 
userr: | 
| 152 | 
  | 
  | 
echo Usage: $0 "mat name mesh_file [-c corners][-b bord_width][-r step_size][-s]" | 
| 153 | 
  | 
  | 
exit 1 |