| 1 |
%%\input epsf |
| 2 |
%%\def\newpage{\vfill\eject} |
| 3 |
%%\advance\vsize1in |
| 4 |
%%\let\ora\overrightarrow |
| 5 |
%%\def\title#1{\hrule\vskip1mm#1\par\vskip1mm\hrule\vskip5mm} |
| 6 |
%%\def\figure#1{\par\centerline{\epsfbox{#1}}} |
| 7 |
%%\title{{\bf 3DANIM.MP: STANDARD ANIMATION DEFINITIONS IN METAPOST}} |
| 8 |
|
| 9 |
%% version 1.34, 17 August 2003 |
| 10 |
%% {\bf Denis Roegel} ({\tt [email protected]}) |
| 11 |
%% |
| 12 |
%%This package provides standard animation definitions and |
| 13 |
%%must be used with the {\bf 3d} package. |
| 14 |
|
| 15 |
if known three_d_anim_version: |
| 16 |
expandafter endinput % avoids loading this package twice |
| 17 |
fi; |
| 18 |
|
| 19 |
% First, we load the 3D package |
| 20 |
input 3d |
| 21 |
|
| 22 |
message "*** 3danim, v1.34 (c) D. Roegel, 17 August 2003 ***"; |
| 23 |
numeric three_d_anim_version; |
| 24 |
three_d_anim_version=1.34; |
| 25 |
|
| 26 |
%%\newpage |
| 27 |
%%\title{Computation of field parameters of an animation} |
| 28 |
numeric xmin_,ymin_,xmax_,ymax_; |
| 29 |
|
| 30 |
def compute_bbox= |
| 31 |
if known xmin_: |
| 32 |
xmin_:=min(xmin_,xpart(llcorner(currentpicture))); |
| 33 |
ymin_:=min(ymin_,ypart(llcorner(currentpicture))); |
| 34 |
xmax_:=max(xmax_,xpart(urcorner(currentpicture))); |
| 35 |
ymax_:=max(ymax_,ypart(urcorner(currentpicture))); |
| 36 |
else: |
| 37 |
xmin_=xpart(llcorner(currentpicture)); |
| 38 |
ymin_=ypart(llcorner(currentpicture)); |
| 39 |
xmax_=xpart(urcorner(currentpicture)); |
| 40 |
ymax_=ypart(urcorner(currentpicture)); |
| 41 |
fi; |
| 42 |
enddef; |
| 43 |
|
| 44 |
extra_endfig:=extra_endfig & "compute_bbox;"; |
| 45 |
|
| 46 |
boolean show_animation_parameters; |
| 47 |
show_animation_parameters=false; |
| 48 |
% The paper height is purely virtual; it is a paper size |
| 49 |
% such that the full animation can rest on it. |
| 50 |
% It must be compatible with the |PAPERSIZE| option |
| 51 |
% of |gs| in |write_script|. |
| 52 |
% If the size (i.e. the bounding box) of the animation is too large, |
| 53 |
% you may have to change the |paper_height| and the |gs| parameter. |
| 54 |
% The ratio between the paper height and width is assumed to be sqrt(2). |
| 55 |
numeric paper_height; |
| 56 |
paper_height=29.7; % A4 paper height in cm |
| 57 |
numeric output_res; |
| 58 |
output_res=36; % default output resolution of bitmap: 36 dots per inch |
| 59 |
|
| 60 |
% show bounding box of an animation, in PostScript points |
| 61 |
% and parameters for animation script |
| 62 |
vardef show_animation_bbox= |
| 63 |
save trx,try,h,w,delta,pnmx,pnmy,pnmw,pnmh,phbp,pwbp; |
| 64 |
w=xmax_-xmin_;h=ymax_-ymin_; |
| 65 |
if show_animation_parameters: |
| 66 |
message "animation bbox: (llx=" & decimal round(xmin_) |
| 67 |
& ",lly=" & decimal round(ymin_) |
| 68 |
& ",w=" & decimal round(w) & ",h=" & decimal round(h) & ")"; |
| 69 |
fi; |
| 70 |
% the lower left corner is put at position (20,20) |
| 71 |
trx=20-xmin_; |
| 72 |
try=20-ymin_; |
| 73 |
if show_animation_parameters: |
| 74 |
message "translate parameters: " |
| 75 |
& decimal round(trx) & " " & decimal round(try); |
| 76 |
fi; |
| 77 |
xmin_:=xmin_+trx;ymin_:=ymin_+try; |
| 78 |
delta=10; % extra space |
| 79 |
phbp=paper_height/2.54*72; % paper height in PostScript points |
| 80 |
pwbp=phbp/sqrt(2); % paper width in PostScript points |
| 81 |
% (assuming a sqrt(2) ratio) |
| 82 |
pnmx=round(xmin_*(output_res/72)-delta); |
| 83 |
pnmy=round((paper_height/2.54*72-ymin_-h)*(output_res/72)-delta); |
| 84 |
pnmw=round(w*(output_res/72)+2*delta); |
| 85 |
pnmh=round(h*(output_res/72)+2*delta); |
| 86 |
if show_animation_parameters: |
| 87 |
message "pnmcut parameters (with -r" & decimal output_res & "): " |
| 88 |
& decimal pnmx & " " & decimal pnmy & " " |
| 89 |
& decimal pnmw & " " & decimal pnmh; |
| 90 |
fi; |
| 91 |
if (pnmx<0) or (pnmy<0) or (pnmx+pnmw>=pwbp) or (pnmy+pnmh>=phbp): |
| 92 |
message "!! paper size overflow: you may need to reduce the size of"; |
| 93 |
message "!! the animation, or to change the paper size"; |
| 94 |
fi; |
| 95 |
write_script(round(trx),round(try), |
| 96 |
pnmx,pnmy,pnmw,pnmh,output_res,jobname,"create_animation.sh"); |
| 97 |
enddef; |
| 98 |
|
| 99 |
%%\newpage |
| 100 |
%%\title{Creation of a shell script to automate the animation} |
| 101 |
% This is UNIX targetted and may need to be customized. |
| 102 |
|
| 103 |
vardef write_script(expr trx,try,xmin,ymin,w,h,res,output,file)= |
| 104 |
save s; |
| 105 |
string s; |
| 106 |
def write_to_file(text arg)=write arg to file; enddef; |
| 107 |
write_to_file("#! /bin/sh"); |
| 108 |
write_to_file(""); |
| 109 |
write_to_file("/bin/rm -f "&output&".log"); |
| 110 |
write_to_file("for i in `ls "&output&".*| grep '"&output&".[0-9]'`;do"); |
| 111 |
if false: "endfor" fi % indentation hack for meta-mode.el |
| 112 |
write_to_file("echo $i"); |
| 113 |
write_to_file("echo '=============='"); |
| 114 |
s:="awk < $i '{print} /^%%Page: /{print "&ditto; |
| 115 |
s:=s&decimal trx&" "&decimal try&" translate\n"&ditto&"}' > $i.ps"; |
| 116 |
write_to_file(s); |
| 117 |
% ghostscript PostScript into ppm |
| 118 |
% (the paper size must be compatible with the definition of |paper_height|) |
| 119 |
s:="gs -sDEVICE=ppmraw -sPAPERSIZE=a4 -dNOPAUSE "; |
| 120 |
s:=s&"-r"&decimal res &" -sOutputFile=$i.ppm -q -- $i.ps"; |
| 121 |
write_to_file(s); |
| 122 |
write_to_file("/bin/rm -f $i.ps"); |
| 123 |
% possible alternative: |
| 124 |
% |s:="mogrify -compress -crop " & decimal(w) & "x" & decimal(h);| |
| 125 |
% |s:=s&"+"& decimal(xmin) &"+"&decimal(ymin);| |
| 126 |
% |s:=s&" -colors 32 -format gif $i.ppm";| |
| 127 |
s:="ppmquant 32 $i.ppm | pnmcut "& decimal(xmin) &" "&decimal(ymin); |
| 128 |
s:=s&" "&decimal(w)&" "&decimal(h) &" | "; |
| 129 |
s:=s&"ppmtogif > `expr $i.ppm : '\(.*\)ppm'`gif"; |
| 130 |
write_to_file(s); |
| 131 |
write_to_file("/bin/rm -f $i.ppm"); |
| 132 |
write_to_file("done"); |
| 133 |
write_to_file("/bin/rm -f "&output&".gif"); |
| 134 |
s:="gifmerge -10 -l1000 "; |
| 135 |
s:=s&output&".*.gif > "&output&".gif"; |
| 136 |
write_to_file(s); |
| 137 |
write_to_file("/bin/rm -f "&output&".*.gif"); |
| 138 |
write_to_file(EOF);% end of file |
| 139 |
enddef; |
| 140 |
|
| 141 |
% These definitions produce {\it one\/} image of some kind. |
| 142 |
|
| 143 |
% In the standard animations, the observer follows a circle, shown below: |
| 144 |
%%\figure{vect-fig.17} |
| 145 |
|
| 146 |
% Standard image 1: this is an example and may be adapted. |
| 147 |
% |name| is an object instance |
| 148 |
def one_image(expr name,i,a,rd,ang)= |
| 149 |
beginfig(i); |
| 150 |
set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1); |
| 151 |
Obs_phi:=90;Obs_dist:=2; |
| 152 |
point_of_view_obj(name,1,Obs_phi);% fix point 1 of object |name| |
| 153 |
draw_obj(name); |
| 154 |
rotate_obj_pv(name,1,vec_I,ang); |
| 155 |
draw_point(name,1);% show the rotation point |
| 156 |
draw_axes(red,green,blue); |
| 157 |
endfig; |
| 158 |
enddef; |
| 159 |
|
| 160 |
% Standard image 2: this is an example and may be adapted. |
| 161 |
% |name_a| and |name_b| are object instances. |
| 162 |
def one_image_two_objects(expr name_a,name_b,i,a,rd,ang)= |
| 163 |
beginfig(i); |
| 164 |
set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1); |
| 165 |
Obs_phi:=90;Obs_dist:=2; |
| 166 |
point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a| |
| 167 |
draw_obj(name_a);draw_obj(name_b); |
| 168 |
rotate_obj_pv(name_a,1,vec_I,ang); |
| 169 |
rotate_obj_pv(name_b,13,vec_J,-ang); |
| 170 |
%|rotate_obj_pp(name_b,13,7,-ang);| |
| 171 |
draw_point(name_a,1);% show the rotation point |
| 172 |
draw_axes(red,green,blue); |
| 173 |
endfig; |
| 174 |
enddef; |
| 175 |
|
| 176 |
%%\newpage |
| 177 |
% Standard image 3: this is an example and may be adapted. |
| 178 |
% |name_a|, |name_b| and |name_c| are object instances. |
| 179 |
def one_image_three_objects(expr name_a,name_b,name_c,i,a,rd,ang)= |
| 180 |
beginfig(i); |
| 181 |
set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1); |
| 182 |
Obs_phi:=90;Obs_dist:=2;h_field:=100;v_field:=150; |
| 183 |
point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a| |
| 184 |
draw_obj(name_a);draw_obj(name_b);draw_obj(name_c); |
| 185 |
new_vec(v_a); |
| 186 |
vec_def_(v_a,.03*cosd(-a*ang+90),.03*sind(-a*ang+90),0); |
| 187 |
translate_obj(name_c,v_a); |
| 188 |
free_vec(v_a); |
| 189 |
rotate_obj_pv(name_a,1,vec_I,ang); |
| 190 |
rotate_obj_pv(name_b,13,vec_J,-ang); |
| 191 |
%|rotate_obj_pp(name_b,13,7,-ang);| |
| 192 |
draw_point(name_a,1);% show the rotation point |
| 193 |
draw_axes(red,green,blue); |
| 194 |
endfig; |
| 195 |
enddef; |
| 196 |
|
| 197 |
% Standard image 4: this is an example and may be adapted. |
| 198 |
% |name_a| and |name_b| are object instances. |
| 199 |
def one_image_two_identical_objects(expr name_a,name_b,i,a,rd,ang)= |
| 200 |
beginfig(i); |
| 201 |
set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),2); |
| 202 |
Obs_phi:=90;Obs_dist:=2; |
| 203 |
point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a| |
| 204 |
draw_obj(name_a);draw_obj(name_b); |
| 205 |
rotate_obj_pv(name_a,1,vec_I,ang); |
| 206 |
rotate_obj_pv(name_b,13,vec_J,-ang); |
| 207 |
%|rotate_obj_pp(name_a,13,7,-ang);| |
| 208 |
draw_point(name_a,1);% show the rotation point |
| 209 |
draw_axes(red,green,blue); |
| 210 |
endfig; |
| 211 |
enddef; |
| 212 |
|
| 213 |
|
| 214 |
%%\newpage |
| 215 |
% An animation is a series of images, and these series are produced here. |
| 216 |
|
| 217 |
% Standard animation 1 |
| 218 |
% |name| is a class name |
| 219 |
def animate_object(expr name,imin,imax,index)= |
| 220 |
numeric ang;ang=360/(imax-imin+1); |
| 221 |
assign_obj("obj",name); |
| 222 |
for i:=imin upto imax:one_image("obj",i+index,i,5,ang);endfor; |
| 223 |
show_animation_bbox; |
| 224 |
enddef; |
| 225 |
|
| 226 |
% Standard animation 2 |
| 227 |
% |name_a| and |name_b| are class names |
| 228 |
def animate_two_objects(expr name_a,name_b,imin,imax,index)= |
| 229 |
numeric ang;ang=360/(imax-imin+1); |
| 230 |
assign_obj("obja",name_a);assign_obj("objb",name_b); |
| 231 |
translate_obj("objb",vec_K);translate_obj("objb",vec_K); |
| 232 |
for i:=imin upto imax: |
| 233 |
one_image_two_objects("obja","objb",i+index,i,10,ang); |
| 234 |
endfor; |
| 235 |
show_animation_bbox; |
| 236 |
enddef; |
| 237 |
|
| 238 |
% Standard animation 3 |
| 239 |
% |name_a|, |name_b| and |name_c| are class names |
| 240 |
vardef animate_three_objects(expr name_a,name_b,name_c,imin,imax,index)= |
| 241 |
numeric ang;ang=360/(imax-imin+1); |
| 242 |
assign_obj("obja",name_a);assign_obj("objb",name_b); |
| 243 |
assign_obj("objc",name_c); |
| 244 |
scale_obj("objb",.7); |
| 245 |
new_vec(v_a); |
| 246 |
vec_def_vec_(v_a,vec_K);vec_mult_(v_a,v_a,4);put_obj("objb",v_a,1,0,0,0); |
| 247 |
free_vec(v_a); |
| 248 |
scale_obj("objc",.5); |
| 249 |
translate_obj("objc",vec_K);translate_obj("objc",vec_K); |
| 250 |
for i:=imin upto imax: |
| 251 |
one_image_three_objects("obja","objb","objc",i+index,i,7,ang); |
| 252 |
endfor; |
| 253 |
show_animation_bbox; |
| 254 |
enddef; |
| 255 |
|
| 256 |
% Standard animation 4 |
| 257 |
% |name| is a class name |
| 258 |
def animate_two_identical_objects(expr name,imin,imax,index)= |
| 259 |
numeric ang;ang=360/(imax-imin+1); |
| 260 |
assign_obj("obja",name);assign_obj("objb",name); |
| 261 |
translate_obj("objb",vec_K);translate_obj("objb",vec_K); |
| 262 |
for i:=imin upto imax: |
| 263 |
one_image_two_identical_objects("obja","objb",i+index,i,10,ang); |
| 264 |
endfor; |
| 265 |
show_animation_bbox; |
| 266 |
enddef; |
| 267 |
|
| 268 |
endinput |
| 269 |
|