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