Debian Codegraph @

texmf-dist/metapost/mp3d/3d.mp

raw
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 3D.MP: 3-DIMENSIONAL REPRESENTATIONS IN METAPOST}}
8
9 %% version 1.34, 17 August 2003
10 %% {\bf Denis Roegel} ({\tt [email protected]})
11
12 %% This package provides definitions enabling the manipulation
13 %% and animation of 3-dimensional objects.
14 %% Such objects can be included in a \TeX{} file or used on web pages
15 %% for instance. See the documentation enclosed in the distribution for
16 %% more details.
17
18 %% Thanks to John Hobby and Ulrik Vieth for helpful hints.
19
20 %% PROJECTS FOR THE FUTURE:
21
22 %% $-$ take light sources into account and show shadows and darker faces
23
24 %% $-$ handle overlapping of objects ({\it obj\_name\/} can be used when
25 %% going through all faces)
26
27 if known three_d_version:
28 expandafter endinput % avoids loading this package twice
29 fi;
30
31 message "*** 3d, v1.34 (c) D. Roegel, 17 August 2003 ***";
32 numeric three_d_version;
33 three_d_version=1.34;
34
35 % This package needs |3dgeom| in a few places. |3dgeom| also loads |3d|
36 % but that's not a problem.
37 %
38 input 3dgeom;
39
40 %%\newpage
41 %%\title{Vector operations}
42
43 % components of vector |i|
44 def xval(expr i)=vec[i]x enddef;
45 def yval(expr i)=vec[i]y enddef;
46 def zval(expr i)=vec[i]z enddef;
47
48 % vector (or point) equality (absolute version)
49 def vec_eq_(expr i,j)=
50 ((xval(i)=xval(j)) and (yval(i)=yval(j)) and (zval(i)=zval(j)))
51 enddef;
52
53 % vector (or point) equality (local version)
54 def vec_eq(expr i,j)=vec_eq_(pnt(i),pnt(j)) enddef;
55
56 % vector inequality (absolute version)
57 def vec_neq_(expr i,j)=(not vec_eq_(i,j)) enddef;
58
59 % vector inequality (local version)
60 def vec_neq(expr i,j)=(not vec_eq(i,j)) enddef;
61
62 % definition of vector |i| by its coordinates (absolute version)
63 def vec_def_(expr i,xi,yi,zi)= vec[i]x:=xi;vec[i]y:=yi;vec[i]z:=zi; enddef;
64
65 % definition of vector |i| by its coordinates (local version)
66 def vec_def(expr i,xi,yi,zi)= vec_def_(pnt(i),xi,yi,zi) enddef;
67
68 % a point is stored as a vector (absolute version)
69 let set_point_ = vec_def_;
70
71 % a point is stored as a vector (local version)
72 let set_point = vec_def;
73
74 def set_point_vec_(expr i,v)=
75 set_point_(i,xval(v),yval(v),zval(v))
76 enddef;
77
78 def set_point_vec(expr i,v)=set_point_vec_(pnt(i),v) enddef;
79
80 let vec_def_vec_=set_point_vec_;
81 let vec_def_vec=set_point_vec;
82
83 % vector sum: |vec[k]| $\leftarrow$ |vec[i]|$+$|vec[j]| (absolute version)
84 def vec_sum_(expr k,i,j)=
85 vec[k]x:=vec[i]x+vec[j]x;
86 vec[k]y:=vec[i]y+vec[j]y;
87 vec[k]z:=vec[i]z+vec[j]z;
88 enddef;
89
90 % vector sum: |vec[k]| $\leftarrow$ |vec[i]|$+$|vec[j]| (local version)
91 def vec_sum(expr k,i,j)=vec_sum_(pnt(k),pnt(i),pnt(j)) enddef;
92
93 % vector translation: |vec[i]| $\leftarrow$ |vec[i]|$+$|vec[v]|
94 def vec_translate_(expr i,v)=vec_sum_(i,i,v) enddef;
95
96 % Here, the second parameter is absolute, because this is probably
97 % the most common case.
98 def vec_translate(expr i,v)=vec_translate_(pnt(i),v) enddef;
99
100 % vector difference: |vec[k]| $\leftarrow$ |vec[i]|$-$|vec[j]|
101 def vec_diff_(expr k,i,j)=
102 vec[k]x:=vec[i]x-vec[j]x;
103 vec[k]y:=vec[i]y-vec[j]y;
104 vec[k]z:=vec[i]z-vec[j]z;
105 enddef;
106
107 def vec_diff(expr k,i,j)=vec_diff_(pnt(k),pnt(i),pnt(j)) enddef;
108
109 % dot product of |vec[i]| and |vec[j]|
110 vardef vec_dprod_(expr i,j)=
111 (vec[i]x*vec[j]x+vec[i]y*vec[j]y+vec[i]z*vec[j]z)
112 enddef;
113
114 vardef vec_dprod(expr i,j)=vec_dprod_(pnt(i),pnt(j)) enddef;
115
116 % modulus of |vec[i]|, absolute version
117 % In the computation, we try to avoid overflows or underflows;
118 % we perform a scaling in order to avoid losing too much
119 % information in certain cases
120 vardef vec_mod_(expr i)=
121 save prod,m_;
122 hide(
123 new_vec(v_a);
124 m_=max(abs(xval(i)),abs(yval(i)),abs(zval(i)));
125 if m_>0:vec_mult_(v_a,i,1/m_);else:vec_def_vec_(v_a,vec_null);fi;
126 prod=m_*sqrt(vec_dprod_(v_a,v_a));
127 free_vec(v_a);
128 )
129 prod
130 enddef;
131
132 % modulus of |vec[i]|, local version
133 % If the return value must be compared to 0,
134 % use |vec_eq| with |vec_null| instead.
135 vardef vec_mod(expr i)= vec_mod_(pnt(i)) enddef;
136
137 % unit vector |vec[i]| corresponding to vector |vec[j]|
138 % only non-null vectors are changed
139 def vec_unit_(expr i,j)=
140 if vec_mod_(j)>0: vec_mult_(i,j,1/vec_mod_(j));
141 else:vec_def_vec_(i,j);
142 fi;
143 enddef;
144
145 def vec_unit(expr i,j)=vec_unit_(pnt(i),pnt(j)) enddef;
146
147 % vector product: |vec[k]| $\leftarrow$ |vec[i]| $\land$ |vec[j]|
148 def vec_prod_(expr k,i,j)=
149 vec[k]x:=vec[i]y*vec[j]z-vec[i]z*vec[j]y;
150 vec[k]y:=vec[i]z*vec[j]x-vec[i]x*vec[j]z;
151 vec[k]z:=vec[i]x*vec[j]y-vec[i]y*vec[j]x;
152 enddef;
153
154 def vec_prod(expr k,i,j)=vec_prod_(pnt(k),pnt(i),pnt(j)) enddef;
155
156 % scalar multiplication: |vec[j]| $\leftarrow$ |vec[i]*v| (absolute version)
157 def vec_mult_(expr j,i,v)=
158 vec[j]x:=v*vec[i]x;vec[j]y:=v*vec[i]y;vec[j]z:=v*vec[i]z;
159 enddef;
160
161 % scalar multiplication: |vec[j]| $\leftarrow$ |vec[i]*v| (local version)
162 def vec_mult(expr j,i,v)=vec_mult_(pnt(j),pnt(i),v) enddef;
163
164 % middle of two points (absolute version)
165 def mid_point_(expr k,i,j)= vec_sum_(k,i,j);vec_mult_(k,k,.5); enddef;
166
167 % middle of two points (local version)
168 def mid_point(expr k,i,j)= mid_point_(pnt(k),pnt(i),pnt(j)); enddef;
169
170 %%\newpage
171 %%\title{Vector rotation}
172 % Rotation of |vec[v]| around |vec[axis]| by an angle |alpha|
173
174 %% The vector $\vec{v}$ is first projected on the axis
175 %% giving vectors $\vec{a}$ and $\vec{h}$:
176 %%\figure{vect-fig.9}
177 %% If we set
178 %% $\vec{b}={\ora{axis}\over \left\Vert\vcenter{\ora{axis}}\right\Vert}$,
179 %% the rotated vector $\vec{v'}$ is equal to $\vec{h}+\vec{f}$
180 %% where $\vec{f}=\cos\alpha \cdot \vec{a} + \sin\alpha\cdot \vec{c}$.
181 %% and $\vec{h}=(\vec{v}\cdot\vec{b})\vec{b}$
182 %%\figure{vect-fig.10}
183
184 % The rotation is independent of |vec[axis]|'s module.
185 % |v| = old and new vector
186 % |axis| = rotation axis
187 % |alpha| = rotation angle
188 %
189 vardef vec_rotate_(expr v,axis,alpha)=
190 new_vec(v_a);new_vec(v_b);new_vec(v_c);
191 new_vec(v_d);new_vec(v_e);new_vec(v_f);
192 new_vec(v_g);new_vec(v_h);
193 vec_mult_(v_b,axis,1/vec_mod_(axis));
194 vec_mult_(v_h,v_b,vec_dprod_(v_b,v)); % projection of |v| on |axis|
195 vec_diff_(v_a,v,v_h);
196 vec_prod_(v_c,v_b,v_a);
197 vec_mult_(v_d,v_a,cosd(alpha));
198 vec_mult_(v_e,v_c,sind(alpha));
199 vec_sum_(v_f,v_d,v_e);
200 vec_sum_(v,v_f,v_h);
201 free_vec(v_h);free_vec(v_g);
202 free_vec(v_f);free_vec(v_e);free_vec(v_d);
203 free_vec(v_c);free_vec(v_b);free_vec(v_a);
204 enddef;
205
206 % The second parameter is left absolute because this is probably the most
207 % common case.
208 vardef vec_rotate(expr v,axis,alpha)=vec_rotate_(pnt(v),axis,alpha) enddef;
209
210 %%\newpage
211 %%\title{Operations on objects}
212 % |iname| is the handler for an instance of an object of class |name|
213 % |iname| must be a letter string
214 % |vardef| is not used because at some point we give other names
215 % to |assign_obj| with |let| and this cannot be done with |vardef|.
216 % (see MFbook for details)
217 def assign_obj(expr iname,name)=
218 begingroup
219 save tmpdef;
220 string tmpdef; % we need to add double quotes (char 34)
221 tmpdef="def " & iname & "_class=" & ditto & name & ditto & " enddef";
222 scantokens tmpdef;
223 def_obj(iname);
224 endgroup
225 enddef;
226
227 % |name| is the the name of an object instance
228 % It must be made only of letters (or underscores), but no digits.
229 def def_obj(expr name)=
230 scantokens begingroup
231 save tmpdef;string tmpdef;
232 tmpdef="def_" & obj_class_(name) & "(" & ditto & name & ditto & ")";
233 tmpdef
234 endgroup
235 enddef;
236
237 % This macro puts an object back where it was right at the beginning,
238 % or rather, where the |set| definition puts it (which may be different
239 % than the initial position, in case it depends on parameters).
240 % |iname| is the name of an object instance.
241 vardef reset_obj(expr iname)=
242 save tmpdef;
243 string tmpdef;
244 define_current_point_offset_(iname);
245 tmpdef="set_" & obj_class_(iname) & "_points";
246 scantokens tmpdef(iname);
247 enddef;
248
249 % Put an object at position given by |pos| (a vector) and
250 % with orientations given by angles |psi|, |theta|, |phi|.
251 % The object is scaled by |scale|.
252 % |iname| is the name of an object instance.
253 % If the shape of the object has been changed since it was
254 % created, these changes are lost.
255 vardef put_obj(expr iname,pos,scale,psi,theta,phi)=
256 reset_obj(iname);scale_obj(iname,scale);
257 new_vec(v_x);new_vec(v_y);new_vec(v_z);
258 vec_def_vec_(v_x,vec_I);
259 vec_def_vec_(v_y,vec_J);
260 vec_def_vec_(v_z,vec_K);
261 rotate_obj_abs_pv(iname,point_null,v_z,psi);
262 vec_rotate_(v_x,v_z,psi);vec_rotate_(v_y,v_z,psi);
263 rotate_obj_abs_pv(iname,point_null,v_y,theta);
264 vec_rotate_(v_x,v_y,theta);vec_rotate_(v_z,v_y,theta);
265 rotate_obj_abs_pv(iname,point_null,v_x,phi);
266 vec_rotate_(v_y,v_x,phi);vec_rotate_(v_z,v_x,phi);
267 free_vec(v_z);free_vec(v_y);free_vec(v_x);
268 translate_obj(iname,pos);
269 enddef;
270
271 %%\newpage
272 %%\title{Rotation, translation and scaling of objects}
273 % Rotation of an object instance |name| around an axis
274 % going through a point |p| (local to the object)
275 % and directed by vector |vec[v]|. The angle of rotation is |a|.
276 vardef rotate_obj_pv(expr name,p,v,a)=
277 define_current_point_offset_(name);
278 rotate_obj_abs_pv(name,pnt(p),v,a);
279 enddef;
280
281 vardef rotate_obj_abs_pv(expr name,p,v,a)=
282 define_current_point_offset_(name);
283 new_vec(v_a);
284 for i:=1 upto obj_points_(name):
285 vec_diff_(v_a,pnt(i),p);
286 vec_rotate_(v_a,v,a);
287 vec_sum_(pnt(i),v_a,p);
288 endfor;
289 free_vec(v_a);
290 enddef;
291
292 % Rotation of an object instance |name| around an axis
293 % going through a point |p| (local to the object)
294 % and directed by vector $\ora{pq}$. The angle of rotation is |a|.
295 vardef rotate_obj_pp(expr name,p,q,a)=
296 define_current_point_offset_(name);
297 new_vec(v_a);new_vec(axis);
298 vec_diff_(axis,pnt(q),pnt(p));
299 for i:=1 upto obj_points_(name):
300 vec_diff_(v_a,pnt(i),pnt(p));
301 vec_rotate_(v_a,axis,a);
302 vec_sum_(pnt(i),v_a,pnt(p));
303 endfor;
304 free_vec(axis);free_vec(v_a);
305 enddef;
306
307 % Translation of an object instance |name| by a vector |vec[v]|.
308 vardef translate_obj(expr name,v)=
309 define_current_point_offset_(name);
310 for i:=1 upto obj_points_(name):
311 vec_sum_(pnt(i),pnt(i),v);
312 endfor;
313 enddef;
314
315 % Scalar multiplication of an object instance |name| by a scalar |v|.
316 vardef scale_obj(expr name,v)=
317 define_current_point_offset_(name);
318 for i:=1 upto obj_points_(name):
319 vec_mult(i,i,v);
320 endfor;
321 enddef;
322
323
324 %%\newpage
325 %%\title{Functions to build new points in space}
326 % Rotation in a plane: this is useful to define a regular polygon.
327 % |k| is a new point obtained from point |j| by rotation around |o|
328 % by a angle $\alpha$ equal to the angle from |i| to |j|.
329 %%\figure{vect-fig.11}
330 vardef rotate_in_plane_(expr k,o,i,j)=
331 save cosalpha,sinalpha,alpha;
332 new_vec(v_a);new_vec(v_b);new_vec(v_c);
333 vec_diff_(v_a,i,o);vec_diff_(v_b,j,o);vec_prod_(v_c,v_a,v_b);
334 cosalpha=vec_dprod_(v_a,v_b)/vec_mod_(v_a)/vec_mod_(v_b);
335 sinalpha=sqrt(1-cosalpha**2);
336 alpha=angle((cosalpha,sinalpha));
337 vec_rotate_(v_b,v_c,alpha);
338 vec_sum_(k,o,v_b);
339 free_vec(v_c);free_vec(v_b);free_vec(v_a);
340 enddef;
341
342 vardef rotate_in_plane(expr k,o,i,j)=
343 rotate_in_plane_(pnt(k),o,pnt(i),pnt(j))
344 enddef;
345
346 % Build a point on a adjacent face.
347 %% The middle $m$ of points $i$ and $j$ is such that
348 %% $\widehat{(\ora{om},\ora{mc})}=\alpha$
349 %% This is useful to define regular polyhedra
350 %%\figure{vect-fig.7}
351 vardef new_face_point_(expr c,o,i,j,alpha)=
352 new_vec(v_a);new_vec(v_b);new_vec(v_c);new_vec(v_d);new_vec(v_e);
353 vec_diff_(v_a,i,o);vec_diff_(v_b,j,o);
354 vec_sum_(v_c,v_a,v_b);
355 vec_mult_(v_d,v_c,.5);
356 vec_diff_(v_e,i,j);
357 vec_sum_(c,v_d,o);
358 vec_rotate_(v_d,v_e,alpha);
359 vec_sum_(c,v_d,c);
360 free_vec(v_e);free_vec(v_d);free_vec(v_c);free_vec(v_b);free_vec(v_a);
361 enddef;
362
363 vardef new_face_point(expr c,o,i,j,alpha)=
364 new_face_point_(pnt(c),pnt(o),pnt(i),pnt(j),alpha)
365 enddef;
366
367 vardef new_abs_face_point(expr c,o,i,j,alpha)=
368 new_face_point_(c,o,pnt(i),pnt(j),alpha)
369 enddef;
370
371 %%\newpage
372 %%\title{Computation of the projection of a point on the ``screen''}
373 % |p| is the projection of |m|
374 % |m| = point in space (3 coordinates)
375 % |p| = point of the intersection plane
376 %%\figure{vect-fig.8}
377 vardef project_point(expr p,m)=
378 save tmpalpha;
379 new_vec(v_a);new_vec(v_b);
380 if projection_type=2: % oblique
381 if point_in_plane_p_pl_(m)(projection_plane):
382 % |m| is on the projection plane
383 vec_diff_(v_a,m,ObliqueCenter_);
384 y[p]:=drawing_scale*vec_dprod_(v_a,ProjJ_);
385 x[p]:=drawing_scale*vec_dprod_(v_a,ProjK_);
386 else: % |m| is not on the projection plane
387 new_line_(l)(m,ObliqueCenter_);
388 vec_diff_(l2,l2,Obs);
389 vec_sum_(l2,l2,m);
390 % (the direction does not depend on Obs)
391 if def_inter_p_l_pl_(v_a)(l)(projection_plane):
392 vec_diff_(v_a,v_a,ObliqueCenter_);
393 y[p]:=drawing_scale*vec_dprod_(v_a,ProjJ_);
394 x[p]:=drawing_scale*vec_dprod_(v_a,ProjK_);
395 else: message "Point " & decimal m & " cannot be projected";
396 x[p]:=too_big_;y[p]=too_big_;
397 fi;
398 free_line(l);
399 fi;
400 else:
401 vec_diff_(v_b,m,Obs); % vector |Obs|-|m|
402 % |vec[v_a]| is |vec[v_b]| expressed in (|ObsI_|,|ObsJ_|,|ObsK_|)
403 % coordinates.
404 vec[v_a]x:=vec[IObsI_]x*vec[v_b]x
405 +vec[IObsJ_]x*vec[v_b]y+vec[IObsK_]x*vec[v_b]z;
406 vec[v_a]y:=vec[IObsI_]y*vec[v_b]x
407 +vec[IObsJ_]y*vec[v_b]y+vec[IObsK_]y*vec[v_b]z;
408 vec[v_a]z:=vec[IObsI_]z*vec[v_b]x
409 +vec[IObsJ_]z*vec[v_b]y+vec[IObsK_]z*vec[v_b]z;
410 if vec[v_a]x<Obs_dist: % then, point |m| is too close
411 message "Point " & decimal m & " too close -> not drawn";
412 x[p]:=too_big_;y[p]=too_big_;
413 else:
414 if (angle(vec[v_a]x,vec[v_a]z)>h_field/2)
415 or (angle(vec[v_a]x,vec[v_a]y)>v_field/2):
416 message "Point " & decimal m & " out of screen -> not drawn";
417 x[p]:=too_big_;y[p]=too_big_;
418 else:
419 if projection_type=0: % central perspective
420 tmpalpha:=Obs_dist/vec[v_a]x;
421 else:
422 tmpalpha:=1; % parallel
423 fi;
424 y[p]:=drawing_scale*tmpalpha*vec[v_a]y;
425 x[p]:=drawing_scale*tmpalpha*vec[v_a]z;
426 fi;
427 fi;
428 fi;
429 free_vec(v_b);free_vec(v_a);
430 enddef;
431
432 % At some point, we may need to do an oblique projection
433 % of vectors |ObsK_| and |ObsI_| on a plane, and to normalize
434 % and orthogonalize the projections (with the projection of |ObsK_|
435 % keeping the same direction). This is done here,
436 % where we take two vectors, a direction (line) and
437 % a plane, and return two vectors. This function assumes
438 % there is an intersection between line |l| and plane |p|.
439 % We do not test it here.
440
441 vardef project_vectors(expr va,vb)(expr k,i)(text l)(text p)=
442 save vc;new_vec(vc);
443 if proj_v_v_l_pl_(va,k)(l)(p): % |va| is the projection of vector |k|
444 else: message "THIS SHOULD NOT HAPPEN";
445 fi;
446 if proj_v_v_l_pl_(vb,i)(l)(p): % |vb| is the projection of vector |i|
447 else: message "THIS SHOULD NOT HAPPEN";
448 fi;
449 % now, we orthonormalize these vectors:
450 vec_prod_(vc,va,vb);
451 vec_unit_(va,va);vec_unit_(vc,vc);vec_prod_(vb,vc,va);
452 free_vec(vc);
453 enddef;
454
455 % Object projection
456 % This is a mere iteration on |project_point|
457 def project_obj(expr name)=
458 define_current_point_offset_(name);
459 for i:=1 upto obj_points_(name):
460 project_point(ipnt_(i),pnt(i));endfor;
461 enddef;
462
463 % Projection screen
464 vardef show_projection_screen=
465 save dx,dy;
466 dx=Obs_dist*sind(h_field/2)/cosd(h_field/2);
467 dy=Obs_dist*sind(v_field/2)/cosd(v_field/2);
468 new_vec(pa);new_vec(pb);new_vec(pc);new_vec(pd);new_vec(op);
469 new_vec(w);new_vec(h);
470 vec_mult_(op,ObsI_,Obs_dist);vec_sum_(op,op,Obs); % center of screen
471 vec_mult_(w,ObsK_,dx);vec_mult_(h,ObsJ_,dy);
472 vec_sum_(pa,op,w);vec_sum_(pa,pa,h); % upper right corner
473 vec_mult_(w,w,-2);vec_mult_(h,h,-2);
474 vec_sum_(pb,pa,w);vec_sum_(pc,pb,h);vec_sum_(pd,pa,h);
475 message "Screen at corners:";
476 show_point("urcorner: ",pa);
477 show_point("ulcorner: ",pb);
478 show_point("llcorner: ",pc);
479 show_point("lrcorner: ",pd);
480 show_point("Obs:",Obs);
481 free_vec(h);free_vec(w);
482 free_vec(op);free_vec(pd);free_vec(pc);free_vec(pb);free_vec(pa);
483 enddef;
484
485
486 %%\newpage
487 %%\title{Draw one face, hiding it if it is hidden}
488 % The order of the vertices determines what is the visible side
489 % of the face. The order must be clockwise when the face is seen.
490 % |drawhidden| is a boolean; if |true| only hidden faces are drawn; if |false|,
491 % only visible faces are drawn. Therefore, |draw_face| is called twice
492 % by |draw_faces|.
493 vardef draw_face(text vertices)(expr col,drawhidden)=
494 save p,num,overflow,i,j,k,nv;
495 path p;boolean overflow;
496 overflow=false;
497 forsuffixes $=vertices:
498 if z[ipnt_($)]=(too_big_,too_big_):overflow:=true; fi;
499 exitif overflow;
500 endfor;
501 if overflow: message "Face can not be drawn, due to overflow";
502 else:
503 p=forsuffixes $=vertices:z[ipnt_($)]--endfor cycle;
504 % we do now search for three distinct and non-aligned suffixes:
505 % usually, the first three suffixes do
506 new_vec(normal_vec);new_vec(v_a);new_vec(v_b);new_vec(v_c);
507 % first, we copy all the indexes in an array, so that
508 % it is easier to go through them
509 i=1; % num0 is not used
510 forsuffixes $=vertices:num[i]=$;i:=i+1;endfor;
511 nv=i-1;
512 for $:=1 upto nv:
513 for $$:=$+1 upto nv:
514 for $$$:=$$+1 upto nv:
515 vec_diff_(v_a,pnt(num[$$]),pnt(num[$]));
516 vec_diff_(v_b,pnt(num[$$$]),pnt(num[$$]));
517 vec_prod_(normal_vec,v_a,v_b);
518 exitif vec_neq_(normal_vec,vec_null);
519 % |vec_mod_| must not be used for such a test
520 endfor;
521 exitif vec_neq_(normal_vec,vec_null);
522 endfor;
523 exitif vec_neq_(normal_vec,vec_null);
524 endfor;
525 if projection_type=0: % perspective
526 vec_diff_(v_c,pnt(num1),Obs);
527 else: % parallel
528 vec_def_vec_(v_c,ObsI_);
529 fi;
530 if filled_faces:
531 if vec_dprod_(normal_vec,v_c)<0:
532 fill p withcolor col;drawcontour(p,contour_width,contour_color)();
533 else: % |draw p dashed evenly;| if this is done, you must ensure
534 % that hidden faces are (re)drawn at the end
535 fi;
536 else:
537 if vec_dprod_(normal_vec,v_c)<0:%visible
538 if not drawhidden:drawcontour(p,contour_width,contour_color)();fi;
539 else: % hidden
540 if drawhidden:
541 drawcontour(p,contour_width,contour_color)(dashed evenly);
542 fi;
543 fi;
544 fi;
545 free_vec(v_c);free_vec(v_b);free_vec(v_a);free_vec(normal_vec);
546 fi;
547 enddef;
548
549 % |p| is the path to draw (a face contour), |thickness| is the pen width
550 % |col| is the color and |type| is a line modifier.
551 def drawcontour(expr p,thickness,col)(text type)=
552 if draw_contours and (thickness>0):
553 pickup pencircle scaled thickness;
554 draw p withcolor background; % avoid strange overlapping dashes
555 draw p type withcolor col;
556 pickup pencircle scaled .4pt;
557 fi;
558 enddef;
559
560 %%\newpage
561 % Variables for face handling. First, we have an array for lists of vertices
562 % corresponding to faces.
563 string face_points_[];% analogous to |vec| arrays
564
565 % Then, we have an array of colors. A color needs to be a string
566 % representing an hexadecimal RGB coding of a color.
567 string face_color_[];
568
569 % |name| is the name of an object instance
570 vardef draw_faces(expr name)=
571 save tmpdef;string tmpdef;
572 define_current_face_offset_(name);
573 % first the hidden faces (dashes must be drawn first):
574 for i:=1 upto obj_faces_(name):
575 tmpdef:="draw_face(" & face_points_[face(i)]
576 & ")(hexcolor(" & ditto & face_color_[face(i)] & ditto
577 & "),true)";scantokens tmpdef;
578 endfor;
579 % then, the visible faces:
580 for i:=1 upto obj_faces_(name):
581 tmpdef:="draw_face(" & face_points_[face(i)]
582 & ")(hexcolor(" & ditto & face_color_[face(i)] & ditto
583 & "),false)";scantokens tmpdef;
584 endfor;
585 enddef;
586
587 % Draw point |n| of object instance |name|
588 vardef draw_point(expr name,n)=
589 define_current_point_offset_(name);
590 project_point(ipnt_(n),pnt(n));
591 if z[ipnt_(n)] <> (too_big_,too_big_):
592 pickup pencircle scaled 5pt;
593 drawdot(z[ipnt_(n)]);
594 pickup pencircle scaled .4pt;
595 fi;
596 enddef;
597
598 vardef draw_axes(expr r,g,b)=
599 project_point(1,vec_null);
600 project_point(2,vec_I);
601 project_point(3,vec_J);
602 project_point(4,vec_K);
603 if (z1<>(too_big_,too_big_)):
604 if (z2<>(too_big_,too_big_)):
605 drawarrow z1--z2 dashed evenly withcolor r;
606 fi;
607 if (z3<>(too_big_,too_big_)):
608 drawarrow z1--z3 dashed evenly withcolor g;
609 fi;
610 if (z4<>(too_big_,too_big_)):
611 drawarrow z1--z4 dashed evenly withcolor b;
612 fi;
613 fi;
614 enddef;
615
616 % Draw a polygonal line through the list of points
617 % This implementation does not work if you call
618 % |draw_lines(i,i+4)| because \MP{} adds parentheses around
619 % the value of |i|.
620 def draw_lines(text vertices)=
621 begingroup % so that we can |let| |draw_lines|
622 save j,num,np;
623 % first, we copy all the indexes in an array, so that
624 % it is easier to go through them
625 j=1;
626 for $=vertices:num[j]=$;j:=j+1;endfor;
627 np=j-1;
628 for j:=1 upto np-1:
629 draw z[ipnt_(num[j])]--z[ipnt_(num[j+1])];
630 endfor;
631 endgroup
632 enddef;
633
634 let draw_line=draw_lines;
635
636 % Draw an arrow between points |i| and |j| of current object
637 % This is used from the |draw| definition of an object.
638 def draw_arrow(expr i,j)=
639 drawarrow z[ipnt_(i)]--z[ipnt_(j)];
640 enddef;
641
642 % Draw a line between points |i| of object |obja| and |j| of |objb|
643 % This is used when outside an object (i.e., we can't presuppose
644 % any object offset)
645 vardef draw_line_inter(expr obja, i, objb, j)=
646 project_point(1,pnt_obj(obja,i));
647 project_point(2,pnt_obj(objb,j));
648 draw z1--z2;
649 enddef;
650
651 % Draw an arrow between points |i| of object |obja| and |j| of |objb|
652 % This is used when outside an object (i.e., we can't presuppose
653 % any object offset)
654 vardef draw_arrow_inter(expr obja, i, objb, j)=
655 project_point(1,pnt_obj(obja,i));
656 project_point(2,pnt_obj(objb,j));
657 draw z1--z2;
658 enddef;
659
660 %%\newpage
661 % Definition of a macro |obj_name| returning an object name
662 % when given an absolute
663 % face number. This definition is built incrementally through a string,
664 % everytime a new object is defined.
665 % |obj_name| is defined by |redefine_obj_name_|.
666
667 % Initial definition
668 string index_to_name_;
669 index_to_name_="def obj_name(expr i)=if i<1:";
670
671 % |name| is the name of an object instance
672 % |n| is the absolute index of its last face
673 def redefine_obj_name_(expr name,n)=
674 index_to_name_:=index_to_name_ & "elseif i<=" & decimal n & ":" & ditto
675 & name & ditto;
676 scantokens begingroup index_to_name_ & "fi;enddef;" endgroup;
677 enddef;
678
679 % |i| is an absolute face number
680 % |vertices| is a string representing a list of vertices
681 % |rgbcolor| is a string representing a color in rgb hexadecimal
682 def set_face(expr i,vertices,rgbcolor)=
683 face_points_[i]:=vertices;face_color_[i]:=rgbcolor;
684 enddef;
685
686 % |i| is a local face number
687 % |vertices| is a string representing a list of vertices
688 % |rgbcolor| is a string representing a color in rgb hexadecimal
689 def set_obj_face(expr i,vertices,rgbcolor)=set_face(face(i),vertices,rgbcolor)
690 enddef;
691
692 % |i| is a local face number of object |inst|
693 % |rgbcolor| is a string representing a color in rgb hexadecimal
694 def set_obj_face_color(expr inst,i,rgbcolor)=
695 face_color_[face_obj(inst,i)]:=rgbcolor;
696 enddef;
697
698
699 %%\newpage
700 %%\title{Compute the vectors corresponding to the observer's viewpoint}
701 % (vectors |ObsI_|,|ObsJ_| and |ObsK_| in the |vec_I|,|vec_J|,
702 % |vec_K| reference; and vectors |IObsI_|,|IObsJ_| and |IObsK_|
703 % which are |vec_I|,|vec_J|,|vec_K|
704 % in the |ObsI_|,|ObsJ_|,|ObsK_| reference)
705 %%\figure{vect-fig.16}
706 %% (here, $\psi>0$, $\theta<0$ and $\phi>0$; moreover,
707 %% $\vert\theta\vert \leq 90^\circ$)
708
709 def compute_reference(expr psi,theta,phi)=
710 % |ObsI_| defines the direction of observation;
711 % |ObsJ_| and |ObsK_| the orientation
712 % (but one of these two vectors is enough,
713 % since |ObsK_| = |ObsI_| $\land$ |ObsJ_|)
714 % The vectors are found by rotations of |vec_I|,|vec_J|,|vec_K|.
715 vec_def_vec_(ObsI_,vec_I);vec_def_vec_(ObsJ_,vec_J);
716 vec_def_vec_(ObsK_,vec_K);
717 vec_rotate_(ObsI_,ObsK_,psi);
718 vec_rotate_(ObsJ_,ObsK_,psi);% gives ($u$,$v$,$z$)
719 vec_rotate_(ObsI_,ObsJ_,theta);
720 vec_rotate_(ObsK_,ObsJ_,theta);% gives ($Obs_x$,$v$,$w$)
721 vec_rotate_(ObsJ_,ObsI_,phi);
722 vec_rotate_(ObsK_,ObsI_,phi);% gives ($Obs_x$,$Obs_y$,$Obs_z$)
723 % The passage matrix $P$ from |vec_I|,|vec_J|,|vec_K|
724 % to |ObsI_|,|ObsJ_|,|ObsK_| is the matrix
725 % composed of the vectors |ObsI_|,|ObsJ_| and |ObsK_| expressed
726 % in the base |vec_I|,|vec_J|,|vec_K|.
727 % We have $X=P X'$ where $X$ are the coordinates of a point
728 % in |vec_I|,|vec_J|,|vec_K|
729 % and $X'$ the coordinates of the same point in |ObsI_|,|ObsJ_|,|ObsK_|.
730 % In order to get $P^{-1}$, it suffices to build vectors using
731 % the previous rotations in the inverse order.
732 vec_def_vec_(IObsI_,vec_I);vec_def_vec_(IObsJ_,vec_J);
733 vec_def_vec_(IObsK_,vec_K);
734 vec_rotate_(IObsK_,IObsI_,-phi);vec_rotate_(IObsJ_,IObsI_,-phi);
735 vec_rotate_(IObsK_,IObsJ_,-theta);vec_rotate_(IObsI_,IObsJ_,-theta);
736 vec_rotate_(IObsJ_,IObsK_,-psi);vec_rotate_(IObsI_,IObsK_,-psi);
737 enddef;
738
739 %%\newpage
740 %%\title{Point of view}
741 % This macro computes the three angles necessary for |compute_reference|
742 % |name| = name of an instance of an object
743 % |target| = target point (local to object |name|)
744 % |phi| = angle
745 vardef point_of_view_obj(expr name,target,phi)=
746 define_current_point_offset_(name);% enables |pnt|
747 point_of_view_abs(pnt(target),phi);
748 enddef;
749
750 % Compute absolute perspective. |target| is an absolute point number
751 % |phi| = angle
752 % This function also computes two vectors needed in case
753 % of an oblique projection.
754 vardef point_of_view_abs(expr target,phi)=
755 save psi,theta;
756 new_vec(v_a);
757 vec_diff_(v_a,target,Obs);
758 vec_mult_(v_a,v_a,1/vec_mod_(v_a));
759 psi=angle((vec[v_a]x,vec[v_a]y));
760 theta=-angle((vec[v_a]x++vec[v_a]y,vec[v_a]z));
761 compute_reference(psi,theta,phi);
762 if projection_type=2: % oblique
763 % we start by checking that at a minimum the three points defining
764 % the projection plane have different indexes; it doesn't mean
765 % the plane if well defined, but if two values are identical,
766 % the plane can't be well defined.
767 if ((projection_plane1<>projection_plane2) and
768 (projection_plane1<>projection_plane3) and
769 (projection_plane2<>projection_plane3)):
770 new_line_(l)(Obs,Obs);
771 vec_sum_(l2,ObsI_,Obs);
772 if def_inter_p_l_pl_(ObliqueCenter_)(l)(projection_plane):
773 project_vectors(ProjK_,ProjJ_)(ObsK_,ObsJ_)(l)(projection_plane);
774 % define the projection direction
775 set_line_(projection_direction)(Obs,ObliqueCenter_);
776 else:
777 message "Anomalous oblique projection:";
778 message " the observer is watching parallely to the plane";
779 fi;
780 free_line(l);
781 else:
782 message "Anomalous projection plane; did you define it?";
783 fi;
784 fi;
785 free_vec(v_a);
786 enddef;
787
788
789 % Distance between the observer and point |n| of object |name|
790 % Result is put in |dist|
791 vardef obs_distance(text dist)(expr name,n)=
792 new_vec(v_a);
793 define_current_point_offset_(name);% enables |pnt|
794 dist:=vec_mod_(v_a,pnt(n),Obs);
795 free_vec(v_a);
796 enddef;
797
798 %%\newpage
799 %%\title{Vector and point allocation}
800 % Allocation is done through a stack of vectors
801 numeric last_vec_;
802 last_vec_=0;
803
804 % vector allocation
805 % (this must not be a |vardef| because the vector |v| saved is not saved
806 % in this macro, but in the calling context)
807 def new_vec(text v)=
808 save v;
809 new_vec_(v);
810 enddef;
811
812 def new_vec_(text v)=
813 v:=incr(last_vec_);
814 %|message "Vector " & decimal (last_vec_+1) & " allocated";|
815 enddef;
816
817 let new_point = new_vec;
818 let new_point_ = new_vec_;
819
820 def new_points(text p)(expr n)=
821 save p;
822 numeric p[];
823 for i:=1 upto n:new_point_(p[i]);endfor;
824 enddef;
825
826 % Free a vector
827 % A vector can only be freed safely when it was the last vector created.
828 def free_vec(expr i)=
829 if i=last_vec_: last_vec_:=last_vec_-1;
830 %|message "Vector " & decimal i & " freed";|
831 else: errmessage("Vector " & decimal i & " can't be freed!");
832 fi;
833 enddef;
834
835 let free_point = free_vec;
836
837 def free_points(text p)(expr n)=
838 for i:=n step-1 until 1:free_point(p[i]);endfor;
839 enddef;
840
841 %%\title{Debugging}
842
843 def show_vec(expr t,i)=
844 message "Vector " & t & "="
845 & "(" & decimal vec[i]x & "," & decimal vec[i]y & ","
846 & decimal vec[i]z & ")";
847 enddef;
848
849 % One can write |show_point("2",pnt_obj("obj",2));|
850 let show_point=show_vec;
851
852 def show_pair(expr t,zz)=
853 message t & "=(" & decimal xpart(zz) & "," & decimal ypart(zz) & ")";
854 enddef;
855
856 %%\newpage
857 %%\title{Access to object features}
858 % |a| must be a string representing a class name, such as |"dodecahedron"|.
859 % |b| is the tail of a macro name.
860
861 def obj_(expr a,b,i)=
862 scantokens
863 begingroup save n;string n;n=a & b & i;n
864 endgroup
865 enddef;
866
867 def obj_points_(expr name)=
868 obj_(obj_class_(name),"_points",name)
869 enddef;
870
871 def obj_faces_(expr name)=
872 obj_(obj_class_(name),"_faces",name)
873 enddef;
874
875 vardef obj_point_offset_(expr name)=
876 obj_(obj_class_(name),"_point_offset",name)
877 enddef;
878
879 vardef obj_face_offset_(expr name)=
880 obj_(obj_class_(name),"_face_offset",name)
881 enddef;
882
883 def obj_class_(expr name)=obj_(name,"_class","") enddef;
884
885 %%\newpage
886 def define_point_offset_(expr name,o)=
887 begingroup save n,tmpdef;
888 string n,tmpdef;
889 n=obj_class_(name) & "_point_offset" & name;
890 expandafter numeric scantokens n;
891 scantokens n:=last_point_offset_;
892 last_point_offset_:=last_point_offset_+o;
893 tmpdef="def " & obj_class_(name) & "_points" & name &
894 "=" & decimal o & " enddef";
895 scantokens tmpdef;
896 endgroup
897 enddef;
898
899 def define_face_offset_(expr name,o)=
900 begingroup save n,tmpdef;
901 string n,tmpdef;
902 n=obj_class_(name) & "_face_offset" & name;
903 expandafter numeric scantokens n;
904 scantokens n:=last_face_offset_;
905 last_face_offset_:=last_face_offset_+o;
906 tmpdef="def " & obj_class_(name) & "_faces" & name &
907 "=" & decimal o & " enddef";
908 scantokens tmpdef;
909 endgroup
910 enddef;
911
912 def define_current_point_offset_(expr name)=
913 save current_point_offset_;
914 numeric current_point_offset_;
915 current_point_offset_:=obj_point_offset_(name);
916 enddef;
917
918 def define_current_face_offset_(expr name)=
919 save current_face_offset_;
920 numeric current_face_offset_;
921 current_face_offset_:=obj_face_offset_(name);
922 enddef;
923
924
925 %%\newpage
926 %%\title{Drawing an object}
927 % |name| is an object instance
928 vardef draw_obj(expr name)=
929 save tmpdef;
930 string tmpdef;
931 current_obj:=name;
932 tmpdef="draw_" & obj_class_(name);
933 project_obj(name);% compute screen coordinates
934 save overflow; boolean overflow; overflow=false;
935 for $:=1 upto obj_points_(name):
936 if z[ipnt_($)]=(too_big_,too_big_):overflow:=true;
937 x[ipnt_($)] := 10; % so that the figure can be drawn anyway
938 y[ipnt_($)] := 10;
939 % why can't I write z[ipnt_($)]:=(10,10); ?
940 fi;
941 exitif overflow;
942 endfor;
943 if overflow:
944 message "Figure has overflows";
945 message " (at least one point is not visible ";
946 message " and had to be drawn at a wrong place)";
947 fi;
948 scantokens tmpdef(name);
949 enddef;
950
951 %%\title{Normalization of an object}
952 % This macro translates an object so that a list of vertices is centered
953 % on the origin, and the last vertex is put on a sphere whose radius is 1.
954 % |name| is the name of the object and |vertices| is a list
955 % of points whose barycenter will define the center of the object.
956 % (|vertices| need not be the list of all vertices)
957 vardef normalize_obj(expr name)(text vertices)=
958 save nvertices,last;
959 nvertices=0;
960 new_vec(v_a);vec_def_(v_a,0,0,0)
961 forsuffixes $=vertices:
962 vec_sum_(v_a,v_a,pnt($));
963 nvertices:=nvertices+1;
964 last:=$;
965 endfor;
966 vec_mult_(v_a,v_a,-1/nvertices);
967 translate_obj(name,v_a);% object centered on the origin
968 scale_obj(name,1/vec_mod(last));
969 free_vec(v_a);
970 enddef;
971
972
973 %%\newpage
974 %%\title{General definitions}
975 % Vector arrays
976 numeric vec[]x,vec[]y,vec[]z;
977
978 % Reference vectors $\vec{0}$, $\vec{\imath}$, $\vec{\jmath}$ and $\vec{k}$
979 % and their definition
980 new_vec(vec_null);new_vec(vec_I);new_vec(vec_J);new_vec(vec_K);
981 vec_def_(vec_null,0,0,0);
982 vec_def_(vec_I,1,0,0);vec_def_(vec_J,0,1,0);vec_def_(vec_K,0,0,1);
983 numeric point_null;
984 point_null=vec_null;
985
986 % Observer
987 new_point(Obs);
988 % default value:
989 set_point_(Obs,0,0,20);
990
991 % Observer's vectors
992 new_vec(ObsI_);new_vec(ObsJ_);new_vec(ObsK_);
993 % default values:
994 vec_def_vec_(ObsI_,vec_I);
995 vec_def_vec_(ObsJ_,vec_J);
996 vec_def_vec_(ObsK_,vec_K);
997
998 new_vec(IObsI_);new_vec(IObsJ_);new_vec(IObsK_);
999
1000 % These vectors will be vectors of the projection plane,
1001 % in case of oblique projections:
1002 new_vec(ProjK_);new_vec(ProjJ_); % there is no |ProjI_|
1003
1004 % This will be the center of the projection plane, in oblique projections
1005 new_point(ObliqueCenter_);
1006
1007
1008 % distance observer/plane (must be $>0$)
1009 numeric Obs_dist; % represents |Obs_dist| $\times$ |drawing_scale|
1010 % default value:
1011 Obs_dist=2; % means |Obs_dist| $\times$ |drawing_scale|
1012
1013 % current object being drawn
1014 string current_obj;
1015
1016 % kind of projection: 0 for linear (or central) perspective, 1 for parallel,
1017 % 2 for oblique projection
1018 % (default is 0)
1019 numeric projection_type;
1020 projection_type:=0;
1021
1022 % Definition of a projection plane (only used in oblique projections)
1023 %
1024 new_plane_(projection_plane)(1,1,1); % the initial value is irrelevant
1025
1026 % Definition of a projection direction (only used in oblique projections)
1027 new_line_(projection_direction)(1,1); % the initial value is irrelevant
1028
1029 % this positions the observer at vector |p| (the point observed)
1030 % + |d| (distance) * (k-(i+j))
1031 def isometric_projection(expr i,j,k,p,d,phi)=
1032 trimetric_projection(i,j,k,1,1,1,p,d,phi);
1033 enddef;
1034
1035 % this positions the observer at vector |p| (the point observed)
1036 % + |d| (distance) * (ak-(i+j))
1037 def dimetric_projection(expr i,j,k,a,p,d,phi)=
1038 trimetric_projection(i,j,k,1,1,a,p,d,phi);
1039 enddef;
1040
1041 % this positions the observer at vector |p| (the point observed)
1042 % + |d| (distance) * (k-(i+j))
1043 % |a|, |b| and |c| are multiplicative factors to vectors |i|, |j| and |k|
1044 vardef trimetric_projection(expr i,j,k,a,b,c,p,d,phi)=
1045 save v_a,v_b,v_c;
1046 new_vec(v_a);new_vec(v_b);new_vec(v_c);
1047 vec_mult_(v_a,i,a);vec_mult_(v_b,j,b);vec_mult_(v_c,k,c);
1048 vec_sum_(Obs,v_a,v_b);
1049 vec_diff_(Obs,v_c,Obs);
1050 vec_mult_(Obs,Obs,d);
1051 vec_sum_(Obs,Obs,p);
1052 point_of_view_abs(p,phi);
1053 projection_type:=1;
1054 free_vec(v_c);free_vec(v_b);free_vec(v_a);
1055 enddef;
1056
1057 % |hor| is an horizontal plane (in the sense that it will represent
1058 % the horizontal for the observer)
1059 % |p| is the point in space that the observer targets (center of screen)
1060 % |a| is an angle (45 degrees corresponds to cavalier drawing)
1061 % |b| is an angle (see examples defined below)
1062 % |d| is the distance of the observer
1063 vardef oblique_projection(text hor)(expr p,a,b,d)=
1064 save _l,v_a,v_b,v_c,xxx_,obsJangle_;
1065 new_vec(v_a);new_vec(v_b);new_vec(v_c);
1066 % we first compute a horizontal line:
1067 new_line_(_l)(1,1);
1068 if def_inter_l_pl_pl(_l)(hor)(projection_plane):
1069 vec_diff_(v_a,_l2,_l1); % horizontal vector
1070 % then, we find a normal to the projection plane:
1071 def_normal_p_(v_b)(projection_plane);
1072 % complete the line and the vector by a third vector (=vertical)
1073 vec_prod_(v_c,v_a,v_b);
1074 % we make |v_a| a copy of |v_b| since we no longer need |v_b|
1075 vec_def_vec_(v_a,v_b);
1076 % we rotate |v_b| by an angle |a| around |v_c|
1077 vec_rotate_(v_b,v_c,a);
1078 % we rotate |v_b| by an angle |b| around |v_a|
1079 vec_rotate_(v_b,v_a,b);
1080 % we put the observer at the distance |d| of |p| in
1081 % the direction of |v_b|:
1082 vec_unit_(v_b,v_b);
1083 vec_mult_(v_b,v_b,d);vec_sum_(Obs,p,v_b);
1084 % We now have to make sure that point |p| and point |Obs|
1085 % are on different sides of the projection plane. For this,
1086 % we compute two dot products:
1087 new_vec(v_d);new_vec(v_e);
1088 vec_diff_(v_d,p,_l1);vec_diff_(v_e,Obs,_l1);
1089 if vec_dprod_(v_d,v_a)*vec_dprod_(v_e,v_a)>=0:
1090 % |p| and |Obs| are on the same side of the projection plane
1091 % |Obs| needs to be recomputed.
1092 vec_mult_(v_b,v_b,-1);
1093 vec_sum_(Obs,p,v_b);
1094 fi;
1095 free_vec(v_e);free_vec(v_d);
1096 projection_type:=2; % needs to be set before |point_of_view_abs|
1097 point_of_view_abs(p,90); % this computes |ObliqueCenter_|
1098 % and now, make sure the vectors defining the observer are right:
1099 % Create the plane containing lines _l and projection_direction
1100 % (defined by point_of_view_abs):
1101 new_plane_(xxx_)(1,1,1);
1102 def_plane_pl_l_l(xxx_)(_l)(projection_direction);
1103 % Compute the angle of |ObsK_| with this plane:
1104 obsJangle_=vangle_v_pl_(ObsK_)(xxx_);
1105 % rotate |ObsJ_| and |ObsK_| by |obsJangle_| around |ObsI_|
1106 vec_rotate_(ObsJ_,ObsI_,obsJangle_);
1107 vec_rotate_(ObsK_,ObsI_,obsJangle_);
1108 if abs(vangle_v_pl_(ObsK_)(xxx_))>1: % the rotation was done
1109 % in the wrong direction
1110 vec_rotate_(ObsJ_,ObsI_,-2obsJangle_);
1111 vec_rotate_(ObsK_,ObsI_,-2obsJangle_);
1112 fi;
1113 % |vec_rotate_(ObsJ_,ObsI_,45);| % planometric test
1114 % |vec_rotate_(ObsK_,ObsI_,45);| % planometric test
1115 free_plane(xxx_);
1116 % and now, |ProjJ_| and |ProjK_| must be recomputed:
1117 project_vectors(ProjK_,ProjJ_)(ObsK_,ObsJ_)%
1118 (projection_direction)(projection_plane);
1119 else:
1120 message "Error: the ``horizontal plane'' cannot be";
1121 message " parallel to the projection plane.";
1122 fi;
1123 free_line(_l);
1124 free_vec(v_c);free_vec(v_b);free_vec(v_a);
1125 enddef;
1126
1127 % These two are the most common values for the third parameter
1128 % of |oblique_projection|
1129 numeric CAVALIER;CAVALIER=45;
1130 numeric CABINET;CABINET=angle((1,.5)); % atn(.5)
1131
1132 % Screen Size
1133 % The screen size is defined through two angles: the horizontal field
1134 % and the vertical field
1135 numeric h_field,v_field;
1136 h_field=100; % degrees
1137 v_field=70; % degrees
1138
1139 % Observer's orientation, defined by three angles
1140 numeric Obs_psi,Obs_theta,Obs_phi;
1141 % default value:
1142 Obs_psi=0;Obs_theta=90;Obs_phi=0;
1143
1144 % This array relates an absolute object point number to the
1145 % absolute point number (that is, to the |vec| array).
1146 % The absolute object point number is the rank of a point
1147 % with respect to all object points. The absolute point number
1148 % considers in addition the extra points, such as |Obs|, which do
1149 % not belong to an object.
1150 % If |i| is an absolute object point number, |points_[i]|
1151 % is the absolute point number.
1152 numeric points_[];
1153
1154 % |name| is the name of an object instance
1155 % |npoints| is its number of defining points
1156 def new_obj_points(expr name,npoints)=
1157 define_point_offset_(name,npoints);define_current_point_offset_(name);
1158 for i:=1 upto obj_points_(name):new_point_(pnt(i));endfor;
1159 enddef;
1160
1161 % |name| is the name of an object instance
1162 % |nfaces| is its number of defining faces
1163 def new_obj_faces(expr name,nfaces)=
1164 define_face_offset_(name,nfaces);define_current_face_offset_(name);
1165 redefine_obj_name_(name,current_face_offset_+nfaces);
1166 enddef;
1167
1168 %%\newpage
1169 % Absolute point number corresponding to object point number |i|
1170 % This macro must only be used within the function defining an object
1171 % (such as |def_cube|) or the function drawing an object (such as
1172 % |draw_cube|).
1173 def ipnt_(expr i)=i+current_point_offset_ enddef;
1174 def pnt(expr i)=points_[ipnt_(i)] enddef;
1175
1176 def face(expr i)=(i+current_face_offset_) enddef;
1177
1178 % Absolute point number corresponding to local point |n|
1179 % in object instance |name|
1180 vardef pnt_obj(expr name,n)=
1181 points_[n+obj_point_offset_(name)]
1182 %hide(define_current_point_offset_(name);) pnt(n) % HAS SIDE EFFECTS
1183 enddef;
1184
1185 % Absolute face number corresponding to local face |n|
1186 % in object instance |name|
1187 vardef face_obj(expr name,n)=
1188 (n+obj_face_offset_(name))
1189 %hide(define_current_face_offset_(name);) face(n) % HAS SIDE EFFECTS
1190 enddef;
1191
1192
1193 % Scale
1194 numeric drawing_scale;
1195 drawing_scale=2cm;
1196
1197 % Color
1198 % This function is useful when a color is expressed in hexadecimal.
1199 % This does the opposite from |tohexcolor|
1200 def hexcolor(expr s)=
1201 (hex(substring (0,2) of s)/255,hex(substring (2,4) of s)/255,
1202 hex(substring (4,6) of s)/255)
1203 enddef;
1204
1205 % Convert a color triple into a hexadecimal color string.
1206 % |rv|, |gv| and |bv| are values between 0 and 1.
1207 % This does the opposite from |hexcolor|
1208 vardef tohexcolor(expr rv,gv,bv)=
1209 save dig;numeric dig[];
1210 hide(
1211 dig2=floor(rv*255);dig1=floor((dig2)/16);dig2:=dig2-16*dig1;
1212 dig4=floor(gv*255);dig3=floor((dig4)/16);dig4:=dig4-16*dig3;
1213 dig6=floor(bv*255);dig5=floor((dig6)/16);dig6:=dig6-16*dig5;
1214 for i:=1 upto 6:
1215 if dig[i]<10:dig[i]:=dig[i]+48;
1216 else:dig[i]:=dig[i]+87;
1217 fi;
1218 endfor;
1219 )
1220 char(dig1)&char(dig2)&char(dig3)&char(dig4)&char(dig5)&char(dig6)
1221 enddef;
1222
1223 % Conversions
1224
1225 % Returns a string encoding the integer |n| as follows:
1226 % if $n=10*a+b$ with $b<10$,
1227 % |alphabetize|(|n|)=|alphabetize|(|a|) |&| |char (65+b)|
1228 % For instance, alphabetize(3835) returns "DIDF"
1229 % This function is useful in places where digits are not allowed.
1230 def alphabetize(expr n)=
1231 if (n>9):
1232 alphabetize(floor(n/10)) & fi
1233 char(65+n-10*floor(n/10))
1234 enddef;
1235
1236 % Filling and contours
1237 boolean filled_faces,draw_contours;
1238 filled_faces=true;
1239 draw_contours=true;
1240 numeric contour_width; % thickness of contours
1241 contour_width=1pt;
1242 color contour_color; % face contours
1243 contour_color=black;
1244
1245 % Overflow control
1246 % An overflow can occur when an object is too close from the observer
1247 % or if an object is out of sight. We use a special value to mark
1248 % coordinates which would lead to an overflow.
1249 numeric too_big_;
1250 too_big_=4000;
1251
1252
1253 % Object offset (the points defining an object are arranged
1254 % in a single array, and the objects are easier to manipulate
1255 % if the point numbers are divided into a number and an offset).
1256 numeric last_point_offset_,last_face_offset_;
1257 last_point_offset_=0;last_face_offset_=0;
1258
1259 endinput