summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/mp3d/3danim.mp
blob: 3e49922a69fa6b30f0cead74d8737b14a4bdb6ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
%%\input epsf
%%\def\newpage{\vfill\eject}
%%\advance\vsize1in
%%\let\ora\overrightarrow
%%\def\title#1{\hrule\vskip1mm#1\par\vskip1mm\hrule\vskip5mm}
%%\def\figure#1{\par\centerline{\epsfbox{#1}}}
%%\title{{\bf 3DANIM.MP: STANDARD ANIMATION DEFINITIONS IN METAPOST}}

%% version 1.34, 17 August 2003
%% {\bf Denis Roegel} ({\tt roegel@loria.fr})
%%
%%This package provides standard animation definitions and
%%must be used with the {\bf 3d} package.

if known three_d_anim_version: 
  expandafter endinput % avoids loading this package twice
fi;

% First, we load the 3D package
input 3d

message "*** 3danim,      v1.34 (c) D. Roegel, 17 August 2003 ***";
numeric three_d_anim_version;
three_d_anim_version=1.34;

%%\newpage
%%\title{Computation of field parameters of an animation}
numeric xmin_,ymin_,xmax_,ymax_;

def compute_bbox=
  if known xmin_:
    xmin_:=min(xmin_,xpart(llcorner(currentpicture)));
    ymin_:=min(ymin_,ypart(llcorner(currentpicture)));
    xmax_:=max(xmax_,xpart(urcorner(currentpicture)));
    ymax_:=max(ymax_,ypart(urcorner(currentpicture)));
  else:
    xmin_=xpart(llcorner(currentpicture));
    ymin_=ypart(llcorner(currentpicture));
    xmax_=xpart(urcorner(currentpicture));
    ymax_=ypart(urcorner(currentpicture));
  fi;
enddef;

extra_endfig:=extra_endfig & "compute_bbox;";

boolean show_animation_parameters;
show_animation_parameters=false;
% The paper height is purely virtual; it is a paper size
% such that the full animation can rest on it.
% It must be compatible with the |PAPERSIZE| option
% of |gs| in |write_script|.
% If the size (i.e. the bounding box) of the animation is too large,
% you may have to change the |paper_height| and the |gs| parameter.
% The ratio between the paper height and width is assumed to be sqrt(2).
numeric paper_height;
paper_height=29.7; % A4 paper height in cm
numeric output_res;
output_res=36; % default output resolution of bitmap: 36 dots per inch

% show bounding box of an animation, in PostScript points
% and parameters for animation script
vardef show_animation_bbox=
  save trx,try,h,w,delta,pnmx,pnmy,pnmw,pnmh,phbp,pwbp;
  w=xmax_-xmin_;h=ymax_-ymin_;
  if show_animation_parameters:
    message "animation bbox: (llx=" & decimal round(xmin_) 
      & ",lly=" & decimal round(ymin_)
      & ",w=" & decimal round(w) & ",h=" & decimal round(h) & ")";
  fi;
  % the lower left corner is put at position (20,20)
  trx=20-xmin_;
  try=20-ymin_;
  if show_animation_parameters:
    message "translate parameters: " 
    & decimal round(trx) & " " & decimal round(try);
  fi;
  xmin_:=xmin_+trx;ymin_:=ymin_+try;
  delta=10; % extra space
  phbp=paper_height/2.54*72; % paper height in PostScript points
  pwbp=phbp/sqrt(2); % paper width in PostScript points
                     % (assuming a sqrt(2) ratio)
  pnmx=round(xmin_*(output_res/72)-delta);
  pnmy=round((paper_height/2.54*72-ymin_-h)*(output_res/72)-delta);
  pnmw=round(w*(output_res/72)+2*delta);
  pnmh=round(h*(output_res/72)+2*delta);
  if show_animation_parameters:
    message "pnmcut parameters (with -r" & decimal output_res & "): " 
    & decimal pnmx & " " & decimal pnmy & " " 
    & decimal pnmw & " " & decimal pnmh;
  fi;
  if (pnmx<0) or (pnmy<0) or (pnmx+pnmw>=pwbp) or (pnmy+pnmh>=phbp):
    message "!! paper size overflow: you may need to reduce the size of";
    message "!! the animation, or to change the paper size";
  fi;
  write_script(round(trx),round(try),
    pnmx,pnmy,pnmw,pnmh,output_res,jobname,"create_animation.sh");    
enddef;

%%\newpage
%%\title{Creation of a shell script to automate the animation}
% This is UNIX targetted and may need to be customized.

vardef write_script(expr trx,try,xmin,ymin,w,h,res,output,file)=
  save s;
  string s;
  def write_to_file(text arg)=write arg to file; enddef;
  write_to_file("#! /bin/sh");
  write_to_file("");
  write_to_file("/bin/rm -f "&output&".log");
  write_to_file("for i in `ls "&output&".*| grep '"&output&".[0-9]'`;do");
    if false: "endfor" fi % indentation hack for meta-mode.el
  write_to_file("echo $i");
  write_to_file("echo '=============='");
  s:="awk  < $i '{print} /^%%Page: /{print "&ditto;
  s:=s&decimal trx&" "&decimal try&" translate\n"&ditto&"}' > $i.ps";
  write_to_file(s);
    % ghostscript PostScript into ppm
    % (the paper size must be compatible with the definition of |paper_height|)
  s:="gs -sDEVICE=ppmraw -sPAPERSIZE=a4 -dNOPAUSE ";
  s:=s&"-r"&decimal res &" -sOutputFile=$i.ppm -q -- $i.ps";
  write_to_file(s);
  write_to_file("/bin/rm -f $i.ps");
    % possible alternative:
    %    |s:="mogrify -compress -crop " & decimal(w) & "x" & decimal(h);|
    %    |s:=s&"+"& decimal(xmin) &"+"&decimal(ymin);|
    %    |s:=s&" -colors 32 -format gif $i.ppm";|
  s:="ppmquant 32 $i.ppm | pnmcut "& decimal(xmin) &" "&decimal(ymin);
  s:=s&" "&decimal(w)&" "&decimal(h) &" | ";
  s:=s&"ppmtogif > `expr $i.ppm : '\(.*\)ppm'`gif";
  write_to_file(s);
  write_to_file("/bin/rm -f $i.ppm");
  write_to_file("done");
  write_to_file("/bin/rm -f "&output&".gif");
  s:="gifmerge -10 -l1000 ";
  s:=s&output&".*.gif > "&output&".gif";
  write_to_file(s);
  write_to_file("/bin/rm -f "&output&".*.gif");
  write_to_file(EOF);% end of file
enddef;

% These definitions produce {\it one\/} image of some kind.

% In the standard animations, the observer follows a circle, shown below:
%%\figure{vect-fig.17}

% Standard image 1: this is an example and may be adapted.
% |name| is an object instance
def one_image(expr name,i,a,rd,ang)=
  beginfig(i);
    set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1);
    Obs_phi:=90;Obs_dist:=2;
    point_of_view_obj(name,1,Obs_phi);% fix point 1 of object |name|
    draw_obj(name);
    rotate_obj_pv(name,1,vec_I,ang);
    draw_point(name,1);% show the rotation point
    draw_axes(red,green,blue);
  endfig;
enddef;

% Standard image 2: this is an example and may be adapted.
% |name_a| and |name_b| are object instances.
def one_image_two_objects(expr name_a,name_b,i,a,rd,ang)=
  beginfig(i);
    set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1);
    Obs_phi:=90;Obs_dist:=2;
    point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a|
    draw_obj(name_a);draw_obj(name_b);
    rotate_obj_pv(name_a,1,vec_I,ang);
    rotate_obj_pv(name_b,13,vec_J,-ang);
      %|rotate_obj_pp(name_b,13,7,-ang);|
    draw_point(name_a,1);% show the rotation point
    draw_axes(red,green,blue);
  endfig;
enddef;

%%\newpage
% Standard image 3: this is an example and may be adapted.
% |name_a|, |name_b| and |name_c| are object instances.
def one_image_three_objects(expr name_a,name_b,name_c,i,a,rd,ang)=
  beginfig(i);
    set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),1);
    Obs_phi:=90;Obs_dist:=2;h_field:=100;v_field:=150;
    point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a|
    draw_obj(name_a);draw_obj(name_b);draw_obj(name_c);
    new_vec(v_a);
    vec_def_(v_a,.03*cosd(-a*ang+90),.03*sind(-a*ang+90),0);
    translate_obj(name_c,v_a);
    free_vec(v_a);
    rotate_obj_pv(name_a,1,vec_I,ang);
    rotate_obj_pv(name_b,13,vec_J,-ang);
      %|rotate_obj_pp(name_b,13,7,-ang);|
    draw_point(name_a,1);% show the rotation point
    draw_axes(red,green,blue);
  endfig;
enddef;

% Standard image 4: this is an example and may be adapted.
% |name_a| and |name_b| are object instances.
def one_image_two_identical_objects(expr name_a,name_b,i,a,rd,ang)=
  beginfig(i);
    set_point_(Obs,-rd*cosd(a*ang),-rd*sind(a*ang),2);
    Obs_phi:=90;Obs_dist:=2;
    point_of_view_obj(name_a,1,Obs_phi);% fix point 1 of object |name_a|
    draw_obj(name_a);draw_obj(name_b);
    rotate_obj_pv(name_a,1,vec_I,ang);
    rotate_obj_pv(name_b,13,vec_J,-ang);
      %|rotate_obj_pp(name_a,13,7,-ang);|
    draw_point(name_a,1);% show the rotation point
    draw_axes(red,green,blue);
  endfig;
enddef;


%%\newpage
% An animation is a series of images, and these series are produced here.

% Standard animation 1
% |name| is a class name
def animate_object(expr name,imin,imax,index)=
  numeric ang;ang=360/(imax-imin+1);
  assign_obj("obj",name);
  for i:=imin upto imax:one_image("obj",i+index,i,5,ang);endfor;
  show_animation_bbox;
enddef;

% Standard animation 2
% |name_a| and |name_b| are class names
def animate_two_objects(expr name_a,name_b,imin,imax,index)=
  numeric ang;ang=360/(imax-imin+1);
  assign_obj("obja",name_a);assign_obj("objb",name_b);
  translate_obj("objb",vec_K);translate_obj("objb",vec_K);
  for i:=imin upto imax:
    one_image_two_objects("obja","objb",i+index,i,10,ang);
  endfor;
  show_animation_bbox;
enddef;

% Standard animation 3
% |name_a|, |name_b| and |name_c| are class names
vardef animate_three_objects(expr name_a,name_b,name_c,imin,imax,index)=
  numeric ang;ang=360/(imax-imin+1);
  assign_obj("obja",name_a);assign_obj("objb",name_b);
  assign_obj("objc",name_c);
  scale_obj("objb",.7);
  new_vec(v_a);
  vec_def_vec_(v_a,vec_K);vec_mult_(v_a,v_a,4);put_obj("objb",v_a,1,0,0,0);
  free_vec(v_a);
  scale_obj("objc",.5);
  translate_obj("objc",vec_K);translate_obj("objc",vec_K);
  for i:=imin upto imax:
    one_image_three_objects("obja","objb","objc",i+index,i,7,ang);
  endfor;
  show_animation_bbox;
enddef;

% Standard animation 4
% |name| is a class name
def animate_two_identical_objects(expr name,imin,imax,index)=
  numeric ang;ang=360/(imax-imin+1);
  assign_obj("obja",name);assign_obj("objb",name);
  translate_obj("objb",vec_K);translate_obj("objb",vec_K);
  for i:=imin upto imax:
    one_image_two_identical_objects("obja","objb",i+index,i,10,ang);
  endfor;
  show_animation_bbox;
enddef;

endinput