| 1 |
%%%% |
| 2 |
%%%% This file belongs to the ROEX package. |
| 3 |
%%%% |
| 4 |
%%%% --------------------------------------------------------------------- |
| 5 |
%%%% MFT formatting commands |
| 6 |
%%%% --------------------------------------------------------------------- |
| 7 |
%%% length quicksort |
| 8 |
%%% length cycle zang pos_turn neg_turn |
| 9 |
%%% good enc |
| 10 |
%%% labels makelabel |
| 11 |
%%% length make_cycle make_join make_cyclic_join make_end make_edge |
| 12 |
%%% point predir postdir upredir upostdir udir |
| 13 |
%%% dotprod det |
| 14 |
%%% subpath pos_subpath neg_subpath |
| 15 |
%%% message info_ro info_es |
| 16 |
%%% draw roex_default |
| 17 |
%%% -- && |
| 18 |
%%%% --------------------------------------------------------------------- |
| 19 |
%%%% \TeX formatting commands |
| 20 |
%%%% --------------------------------------------------------------------- |
| 21 |
%%\vsize245mm |
| 22 |
%%\font\titfnt cmtt10 at 48 pt |
| 23 |
%%{\let\makefootline\empty \let\makeheadline\empty |
| 24 |
%%\vglue0ptplus1fill |
| 25 |
%%\centerline{\titfnt ROEX.MF} |
| 26 |
%%\bigskip |
| 27 |
%%\centerline{ver. 0.56 (Wednesday, October 25th, 1995)} |
| 28 |
%%\vfill\vfill\eject} |
| 29 |
%% % --- |
| 30 |
%% \vsize 245mm |
| 31 |
%% % an innocent formatting trick: the underscore character ending a name |
| 32 |
%% % will be typeset as an superscript asterisk |
| 33 |
%% \let\oriunderscore\_ |
| 34 |
%% \newif\ifbgroupopen\bgroupopenfalse |
| 35 |
%% \def\altdblbackslash#1{\bgroup\bgroupopentrue\it#1} |
| 36 |
%% \def\optegroup{\ifbgroupopen\egroup\fi} |
| 37 |
%% \def\underscoreasasterisk#1{% |
| 38 |
%% \ifx#1\relax\optegroup^*\else\oriunderscore#1\fi} |
| 39 |
%% \def\\#1{% |
| 40 |
%% \let\_\underscoreasasterisk |
| 41 |
%% \altdblbackslash{#1\relax}\optegroup |
| 42 |
%% \let\_\oriunderscore} |
| 43 |
%% % --- |
| 44 |
%% \def\dblhyph{--} |
| 45 |
%% \def\8#1{\def\eightparm{#1}\mathrel{\mathcode`\.="8000 \mathcode`\-="8000 |
| 46 |
%% \ifx\eightparm\dblhyph\setbox\shorthyf\hbox{\bf -\kern-.05em}\fi% |
| 47 |
%% #1\unkern}} % `..' and `--' |
| 48 |
%% % --- |
| 49 |
%% \def\MP{{\tenlogo META}\-{\tenlogo POST}} |
| 50 |
% ------------------------------------------------------------------------ |
| 51 |
% This is ROEX.MF file containing \MF definitions implementing |
| 52 |
% operations known as `remove overlap' and `expand stroke'. |
| 53 |
% ------------------------------------------------------------------------ |
| 54 |
% Authors: \bf{}B. Jackowski, P. Pianowski, M. Ry\'cko \& S. Soko\l{}owski |
| 55 |
% ------------------------------------------------------------------------ |
| 56 |
% H I S T O R Y |
| 57 |
% ver. 0.1 (1 / 9 VI 1994): |
| 58 |
% * incunabula version |
| 59 |
% ver. 0.5 (15 VIII / 1 IX 1995): |
| 60 |
% * pioneer version, released during the 9th Euro\TeX conference in Arnhem |
| 61 |
% ver. 0.55 (26 IX 1995): |
| 62 |
% * if a single path is an argument to |remove_overlap|, removing |
| 63 |
% of self-overlaps is performed, hence several adjustments, most |
| 64 |
% significant changes were introduced in |is_far_enough| and |
| 65 |
% |intersect_two_segments|; this ismprovement is, in fact, a prelude |
| 66 |
% towards a more general approach |
| 67 |
% * a bug trap added in |clean_path| |
| 68 |
% * positioning of labels not forced in |mark_nodes| |
| 69 |
% * |quicksort| more flexible |
| 70 |
% * more statistics available (optionally) in |find_minimal_secant| |
| 71 |
% * displaying information changed |
| 72 |
% * \TeX formatting comments collected at the beginning of the file |
| 73 |
% * a result of mental laps corrected in |build_node_structure| |
| 74 |
% (minimal secant has nothing to do with minimal distance between nodes) |
| 75 |
% * a silly bug removed in |prepare_input_data| (|W| instead of |W_|) |
| 76 |
% This version was released during the CyrTUG-95 meeting in Moscow |
| 77 |
% ver. 0.56 (27 X 1995): |
| 78 |
% * comments adjusted to a new distribution |
| 79 |
% * the name |miter_limit| changed to |miter_size| in order to avoid |
| 80 |
% misunderstanding, as in this implementation it is a dimen, while |
| 81 |
% in PostScript it is a dimensionless quantity |
| 82 |
% ------------------------------------------------------------------------ |
| 83 |
% S Y N O P S I S |
| 84 |
% ------------------------------------------------------------------------ |
| 85 |
% |
| 86 |
% Such operations as `remove overlap' and `expand stroke' are perhaps |
| 87 |
% particularly useful in the contex of exporting data from \MF to other |
| 88 |
% languages, e.g., to PostScript or HP-GL. Therefore the file ROEX.MF has |
| 89 |
% been included into the MFTOEPS package (which accomplishes export from \MF |
| 90 |
% to Encapsulated PostScript), although it can be used by ``normal'' \MF |
| 91 |
% users, too. Therefore our favourite macros (e.g., |pos_turn|, |neg_turn|, |
| 92 |
% |make_list|), are defined identically in both ROEX and MFTOEPS. |
| 93 |
% |
| 94 |
% We hope that tiny adjustments, if any, should be sufficient for transforming |
| 95 |
% the macros to the form accepted by both \MF and \MP. |
| 96 |
% |
| 97 |
% Sample \MF programs (i.e., simple examples) illustrating the use of the |
| 98 |
% ``interface'' macros, i.e., |remove_overlap|, |expand_stroke| and |
| 99 |
% |change_weight|, can be found in a subdirectory ROEXSAMP. It is instructive |
| 100 |
% to generate EPS files and then to play around with the results using |
| 101 |
% CorelDRAW! or Adobe Illustrator. |
| 102 |
% |
| 103 |
% |
| 104 |
% REMOVING OVERLAPS |
| 105 |
% |
| 106 |
% The command |remove_overlap_| requires three parameters. The first |
| 107 |
% parameter is a list of paths to be processed; the paths are assumed to |
| 108 |
% have a non-zero |turningnumber| and no self intersecting points (no |
| 109 |
% checking is performed, except that non-cyclic paths are ignored). |
| 110 |
% The second parameter is a list (possibly empty) of weights assigned |
| 111 |
% to paths; more exactly, it is a list of pairs |(i,w.i)|, where |i| is |
| 112 |
% the order number of a path and |w.i| is the respective weight. |
| 113 |
% If the weight is not specified it is assumed to be equal to |1|. |
| 114 |
% The last parameter is a suffix, i.e., the name of the resulting data |
| 115 |
% structure; given a suffix is |R|, |R.num| is the number of the resulting |
| 116 |
% paths, and |R1|, |R2|, ..., |R[R.num]| are the paths. If the suffix |
| 117 |
% contains an index, e.g., |P[x]q|, the user is responsible for providing |
| 118 |
% appropriate declarations prior to calling |remove_overlap|, |
| 119 |
% in this case: |numeric P[\\]q.num; path P[\\]q[\\]|. If a variable |
| 120 |
% |append_results| is assigned a definite value (by default it is undefined), |
| 121 |
% |R.num| is not zeroed at the stage of initialisation, thus the results |
| 122 |
% are accumulated (see example ROES-02.MF in the subdirectory ROEXSAMP). |
| 123 |
% |
| 124 |
% The algorithm assumes that a path |p| fills its interior with the colour |
| 125 |
% |w*turningnumber(p)|, where |w| is the weight assigned to |p|. If an |
| 126 |
% area is coloured by several paths, the colors are summed up. The user |
| 127 |
% decides which areas are the resulting ones. By default, these are areas |
| 128 |
% which have the interior painted with a colour $\ge1$ and the exterior |
| 129 |
% painted with a colour $\le0$. There is a two-parameter function |
| 130 |
% (parameters are numbers) that can be redefined by a user, |good_colors|, |
| 131 |
% which governs the decision. The user is responsible for a proper definition |
| 132 |
% of this function (the formula |good_colors(m,n) and good_colors(n,m)| must |
| 133 |
% be false; cf. the default definition of |good_colors| at the end of this |
| 134 |
% file). There is also a variable |background_color|, by default equal to |0|, |
| 135 |
% which determines the colour of the Euclidian plane. One more function that |
| 136 |
% is meant to be redefined by the user, if needed, is one-parameter function |
| 137 |
% |touch_path|; the function is applied to every input path at the stage of |
| 138 |
% initialisation, and can be used, e.g., for adjusting the direction of paths |
| 139 |
% (cf. example RO-04.MF in the subdirectory ROEXSAMP). |
| 140 |
% |
| 141 |
% The orientation of paths generated by the |remove_overlap| macro is defined |
| 142 |
% in such a way that in order to fill the resulting figure the internal |
| 143 |
% variable |turningcheck| should be set to zero prior to using the |fill| |
| 144 |
% command. |
| 145 |
% |
| 146 |
% Examples: |
| 147 |
% Assume that paths |A|, |B| and |C| are defined as follow (say, |w=h=1cm|): |
| 148 |
% |A=unitsquare xscaled 1/5w yscaled h shifted (2/5w,0);| |
| 149 |
% |B=A rotatedaround((1/2w,1/2h), 60);| |
| 150 |
% |C=B rotatedaround((1/2w,1/2h), 60);| |
| 151 |
% Calling |
| 152 |
% |remove_overlap (A,B,C) () R;| |
| 153 |
% will result in generating a single path |R1| (|R.num=1|) of a six-arm |
| 154 |
% propeller shape. Adding one more path: |
| 155 |
% |D=reverse fullcircle scaled 3/4w shifted (1/2w,1/2h);| |
| 156 |
% and calling |
| 157 |
% |remove_overlap (A,B,C,D) ((4,2)) R;| |
| 158 |
% (|D| has weight |=2|) will result in generating seven objects: six |
| 159 |
% ``tips'' of a propeler and a regular hexagon in the center. Try to guess |
| 160 |
% what would be the result of |
| 161 |
% |remove_overlap (A,B,C,D) () R;| |
| 162 |
% Not all paths need to intersect. For example, the following set of paths |
| 163 |
% |A=fullcircle scaled w shifted (1/2w,1/2h);| |
| 164 |
% |B=reverse unitsquare xscaled 1/5w yscaled 3/5w shifted (2/5w,1/5w);| |
| 165 |
% |C=B rotatedaround((1/2w,1/2h), 90);| |
| 166 |
% after calling |
| 167 |
% |remove_overlap (A,B,C) () R;| |
| 168 |
% will yield a circle surrounding a white cross. Since the orientation |
| 169 |
% of the resulting paths is important here, the |fill| commands should be |
| 170 |
% preceded by |
| 171 |
% |interim turningcheck:=0;| |
| 172 |
% assignment. |
| 173 |
% |
| 174 |
% |
| 175 |
% EXPANDING STROKES |
| 176 |
% |
| 177 |
% Expanding stroke means finding the trace of the outline of an imaginary pen |
| 178 |
% moving along a path. There are two commands accomplishing expanding stroke: |
| 179 |
% |expand_stroke| and |change_weight|. Both make use of the essentially the |
| 180 |
% same algorithm, except that the latter finds only one edge and ignores |
| 181 |
% non-cyclic paths. Both commands require three parameters: first and third |
| 182 |
% are analogous to the parameters of the |expand_stroke| macro (see above), |
| 183 |
% the second denotes the radius (not diameter) of the circular pen. |
| 184 |
% The algorithm works in such a way that the result of the |expand_stroke| |
| 185 |
% does not depend on the direction of a path for cyclic paths, namely, |
| 186 |
% the outer edge is always positively directed and the inner is negatively |
| 187 |
% directed, provided the radius is positive; if the radius is negative, |
| 188 |
% the outer edge is negatively directed and the inner one---positively. |
| 189 |
% For non-cyclic paths positive radius yields positively directed resulting |
| 190 |
% paths, negative radius---negatively oriented paths. Although the macro |
| 191 |
% |change_weight| is subdued to the same rules, the result depends both |
| 192 |
% on the direction of a path and on the sign of a radius. Let |t| and |r| |
| 193 |
% denote the turning number and the radius, respectively; there are |
| 194 |
% four cases: |
| 195 |
% 1) |t>0| and |r>0|: the resulting path is an outer edge positively |
| 196 |
% directed, |
| 197 |
% 2) |t>0| and |r<0|: the resulting path is an inner edge positively |
| 198 |
% directed, |
| 199 |
% 3) |t<0| and |r>0|: the resulting path is an inner edge negatively |
| 200 |
% directed, |
| 201 |
% 4) |t<0| and |r<0|: the resulting path is an outer edge negatively |
| 202 |
% directed. |
| 203 |
% Following PostScript, we introduced three variables which govern the shape |
| 204 |
% of joins and ends: |join_kind| (corresponds to |setlinejoin|), |end_kind| |
| 205 |
% (corresponds to |setlinecap|) and |miter_size| (corresponds, as the name |
| 206 |
% suggest, to |setmiterlimit|; however, here |miter_size| is a dimen, |
| 207 |
% while in PostScript miter limit is a dimensionless quantity). |
| 208 |
% Currently both |join_kind| and |end_kind| can receive value |0| or |1|, |
| 209 |
% while in PostScript value |2| is also admissible. (The latter option |
| 210 |
% will perhaps be included also into the ROEX package some day, but more |
| 211 |
% tempting is the implementation of extrapolated non-linear joins.) |
| 212 |
% Since the interpretation of |miter_size| (dimen) is slightly different than |
| 213 |
% the interpretation of |miter_limit| (a number), |miter_size| must merely |
| 214 |
% be non-negative, while |miter_limit| must be greater than or equal to $1$. |
| 215 |
% Roughly speaking, value |0| for |join_kind| and |end_kind| denotes cusp |
| 216 |
% joins, cut if necessary at miter limit; value |1| denotes rounded rounded |
| 217 |
% joins (for details see, e.g., ``PostScript Language Reference Manual,'' |
| 218 |
% second ed., Addison-Wesley Publishing Company, Ltd.). |
| 219 |
% |
| 220 |
% Example: |
| 221 |
% Assume that a path |A| is simply a square (say, |w=h=1cm|): |
| 222 |
% |A=unitsquare scaled w;| |
| 223 |
% After calling |
| 224 |
% |expand_stroke(A)(1mm)R;| |
| 225 |
% |R1| is a positively directed square of side |12mm|, and |R2| is |
| 226 |
% negatively directed square of side |8mm|. |
| 227 |
% ------------------------------------------------------------------------ |
| 228 |
% C A V E A T S , H I N T S A N D C O M M E N T S |
| 229 |
% * The employed algorithms expect that the results are well defined; |
| 230 |
% if the data are weird (e.g., self-loooping path are supplied) |
| 231 |
% the results, if any, may be weird as well. |
| 232 |
% * The case of curves partially overlapping is not handled and, frankly |
| 233 |
% speaking, we have no idea how to implement it efficiently and robustly; |
| 234 |
% if there are such pairs of paths in the input data, the algorithm almost |
| 235 |
% certainly will not produce good results. |
| 236 |
% * Only circular pens are implemented so far. |
| 237 |
% * Be aware of rounding errors, they may cause unpredictable results; |
| 238 |
% in some cases increasing accuracy by using a higher resolution may |
| 239 |
% help, but more adequate seems to be preparing better data (cf. the |
| 240 |
% program RO-07.MF in the subdirectory ROEXSAMP). |
| 241 |
% * Comments in the code are meant primarily for the authors; the user |
| 242 |
% is kindly requested not to complain fiercely if they are of a little |
| 243 |
% use to her/him. |
| 244 |
% * Unfortunately, \MF has no error-handling facility, hence a lot of |
| 245 |
% ``bug traps'' can be found in the code; messages issued in the case |
| 246 |
% of falling into such a trap are rather useless if you don't know the |
| 247 |
% details of the algorithm; this part of the program is certainly to be |
| 248 |
% improved; usually the error help says ``Better stop now! Algorithm |
| 249 |
% failed'' and this advice should be followed; in practice this means that |
| 250 |
% \MF is not able to recognize the details of the picture because of |
| 251 |
% very close nodes (intersection points). |
| 252 |
% * Usually, the first stage of removing overlaps (finding all intersection |
| 253 |
% points) is the longest one, the more segments paths have the longer it |
| 254 |
% lasts; a pity that \MF has no built-in function informing about all |
| 255 |
% intersection points/times of two B\'ezier curves. |
| 256 |
% * Improper definition of |good_colors| may result in erroneous behaviour of |
| 257 |
% the algorithm. |
| 258 |
% * One peculiar case is considered by the expanding stroke algorithm, |
| 259 |
% namely cyclic path of length 2; some more cases might have been taken |
| 260 |
% into account... |
| 261 |
% * There remain a lot of unsolved problems with numerical instability |
| 262 |
% connected with detecting tangent and close points. |
| 263 |
% * In The \MF{}book, p. 229, D. E. Knuth writes: |
| 264 |
% ``...tiny little loops won't hurt anything if you are filling cycles |
| 265 |
% in the correct direction.'' |
| 266 |
% Cf. also preceding dangerous band paragraph and exercise on pp. 228--229. |
| 267 |
% ROEX does much more complex things with paths than merely filling them, |
| 268 |
% hence tiny loops may cause some mess, the more so as the built-in |
| 269 |
% function |turningnumber| is very sensitive to such loops, e.g., it may |
| 270 |
% happen that |turningnumber(p)=1| while |turningnumber(reverse p)=0| |
| 271 |
% (cf. example RO-6.MF in the subdirectory ROEXSAMP); hence a hopefully |
| 272 |
% more robust (from the point of view of this application) function |
| 273 |
% is used, |check_turn|, which makes use of \MF's |fill| operation. |
| 274 |
% * There remain several suboptimal algorithms employed, partially on |
| 275 |
% purpose: less efficient algorithms are usually (although not necessarily) |
| 276 |
% more comprehensible and flexible (easier to modify), which is important |
| 277 |
% at the stage of developing a program. |
| 278 |
% * Parameters that may have influence on the process of removing |
| 279 |
% overlaps are |epsil.time|, |epsil.ang| (in degrees), |epsil.dist| (in |
| 280 |
% resolution-dependent units), and |max_idx|; the choice of good default |
| 281 |
% values will need some practice. |
| 282 |
% * Incompatible modifications may come, although we shall do our best |
| 283 |
% to avoid them. |
| 284 |
% ------------------------------------------------------------------------ |
| 285 |
% We follow the naming convention of The \MF{}book: |
| 286 |
% ``Private tokens always end with the underscore character.'' |
| 287 |
% Since the underscore is a rather illegible character, in a ``neat'' |
| 288 |
% printing (using MFT utility) it will appear as an superscript asterisk. |
| 289 |
% ------------------------------------------------------------------------ |
| 290 |
% D E F I N I T I O N S |
| 291 |
% ------------------------------------------------------------------------ |
| 292 |
% UNIVERSAL MACROS: |
| 293 |
% --- |
| 294 |
% Without the following redefinition: |
| 295 |
def -- = {curl 1} .. tension (1+eps) .. {curl 1} enddef; |
| 296 |
% the result of |p intersectiontimes reverse p|, where |p=(a,b)--(a+3c,b+3d)|, |
| 297 |
% |a|, |b|, |c|, |d| are arbitrary (sic!) \MF's numbers, yields the result |
| 298 |
% |(1/2,1/2)|, which contradicts the statement preceding the exercise 14.17 |
| 299 |
% on the page 137 of The \MF{}book. Since it is no longer a ``standard'' |
| 300 |
% macro, its formatting is slightly modified. |
| 301 |
% --- |
| 302 |
%%% length ]]] ]]]] |
| 303 |
%%% ; ] |
| 304 |
def ]]] = ] ] ] enddef; |
| 305 |
def ]]]] = ] ] ] ] enddef; % right brackets should be loners, indeed |
| 306 |
%%% ) ] ]] ]]] ]]]] |
| 307 |
% --- |
| 308 |
vardef distance(expr za,zb) = length(za-zb) enddef; % in fact, an alias |
| 309 |
% --- |
| 310 |
vardef interval(expr ta,tb,p) = |
| 311 |
save ta_,tb_; |
| 312 |
if cycle p: |
| 313 |
ta_:=ta mod length(p); tb_:=tb mod length(p); |
| 314 |
min(length(p)-abs(ta_-tb_), abs(ta_-tb_)) |
| 315 |
else: |
| 316 |
ta_:=min(max(0,ta),length(p)); tb_:=min(max(0,tb),length(p)); |
| 317 |
abs(ta_-tb_) |
| 318 |
fi |
| 319 |
enddef; |
| 320 |
% --- |
| 321 |
def make_list(expr k,l) suffix s = |
| 322 |
for i_:=k upto l: if i_>k: , fi \\ s[i_] endfor |
| 323 |
enddef; |
| 324 |
% --- |
| 325 |
vardef dec_pair(expr z) = |
| 326 |
"(" & decimal(xpart z) & "," & decimal(ypart z) & ")" |
| 327 |
enddef; |
| 328 |
% --- |
| 329 |
primarydef u det v = % dual operation to |dotprod| |
| 330 |
(xpart u * ypart v - xpart v * ypart u) |
| 331 |
enddef; |
| 332 |
% --- |
| 333 |
vardef zang(expr u,v) = % useful during testing |
| 334 |
% computes the angle form |u| to |v| (useful for testing) |
| 335 |
angle(u dotprod v,u det v) mod 360 % CAVEAT! rounding errors |
| 336 |
enddef; |
| 337 |
% --- |
| 338 |
vardef turn_ang(expr za,zb) = % more robust version of |zang| |
| 339 |
% The idea of computing the turn angle is based on the following observation: |
| 340 |
% |z reflectedabout (origin,right)=1/z| for a complex number |z| such that |
| 341 |
% |abs(z)=1|; recall also that multiplication of complex numbers |
| 342 |
% (|zscaled| operation) implies addition of their angle arguments. |
| 343 |
if (abs(za)>=epsil.len) and (abs(zb)>=epsil.len): % |eps| may be not enough |
| 344 |
angle(unitvector(za) zscaled (unitvector(zb) reflectedabout (origin,right))) |
| 345 |
else: whatever fi |
| 346 |
enddef; |
| 347 |
% --- |
| 348 |
def predir expr t of p = ((point t of p)-(precontrol t of p)) enddef; |
| 349 |
def postdir expr t of p = ((postcontrol t of p)-(point t of p)) enddef; |
| 350 |
def udir expr t of p = unitvector(direction t of p) enddef; |
| 351 |
def upredir expr t of p = unitvector(predir t of p) enddef; |
| 352 |
def upostdir expr t of p = unitvector(postdir t of p) enddef; |
| 353 |
% --- |
| 354 |
vardef pos_turn primary p = |
| 355 |
interim autorounding:=0; |
| 356 |
if check_turn(p)=0: show p; |
| 357 |
errhelp "I will leave the path intact, continue with crossed fingers."; |
| 358 |
errmessage "Cannot make positive turn (check_turn=0)"; |
| 359 |
elseif check_turn(p)<0: reverse fi \\ p |
| 360 |
enddef; |
| 361 |
% --- |
| 362 |
vardef neg_turn primary p = |
| 363 |
interim autorounding:=0; |
| 364 |
if check_turn(p)=0: show p; |
| 365 |
errhelp "I will leave the path intact, continue with crossed fingers."; |
| 366 |
errmessage "Cannot make negative turn (check_turn=0)"; |
| 367 |
elseif check_turn(p)>0: reverse fi \\ p |
| 368 |
enddef; |
| 369 |
% --- |
| 370 |
vardef check_turn primary p = % seems more adequate than |turningnumber| |
| 371 |
% |epsilon|=|totalweight currentpicture| after |fill unitsquare|, |
| 372 |
% |eps/epsilon=32|, i.e., we admit accuracy of 32 pixels (isn't it too many?) |
| 373 |
save r_,currentpicture; picture currentpicture; |
| 374 |
interim turningcheck:=0; interim autorounding:=0; |
| 375 |
currentpicture:=nullpicture; fill p; r_:=totalweight(currentpicture); |
| 376 |
if r_>eps: 1 elseif r_<-eps: -1 else: turningnumber(p) fi |
| 377 |
enddef; |
| 378 |
% --- |
| 379 |
def check_embedding(expr a,b)(suffix res) = |
| 380 |
begingroup |
| 381 |
% see comment in |check_turn| |
| 382 |
save napb_,panb_,currentpicture; picture currentpicture; |
| 383 |
interim turningcheck:=0; interim autorounding:=0; |
| 384 |
currentpicture:=nullpicture; fill pos_turn a; fill neg_turn b; cullit; |
| 385 |
panb_:=totalweight currentpicture; |
| 386 |
currentpicture:=nullpicture; fill neg_turn a; fill pos_turn b; cullit; |
| 387 |
napb_:=totalweight currentpicture; |
| 388 |
if (panb_<eps) and (napb_<>0): res:=1; % $a \subset b$ |
| 389 |
elseif (panb_<>0) and (napb_<eps): res:=2; % $b \subset a$ |
| 390 |
else: res:=0; fi % undefined result |
| 391 |
endgroup |
| 392 |
enddef; |
| 393 |
% --- |
| 394 |
vardef pos_subpath expr z of p = |
| 395 |
if not cycle p: subpath z of p |
| 396 |
else: |
| 397 |
if xpart(z)<=ypart(z): subpath z of p |
| 398 |
else: subpath (xpart(z),ypart(z)+length(p)) of p |
| 399 |
fi |
| 400 |
fi |
| 401 |
enddef; |
| 402 |
% --- |
| 403 |
vardef neg_subpath expr z of p = |
| 404 |
if not cycle p: subpath z of p |
| 405 |
else: reverse(pos_subpath (ypart z,xpart z) of p) fi |
| 406 |
enddef; |
| 407 |
% --- |
| 408 |
tertiarydef p && q = % |length(p)>0| |
| 409 |
(subpath(0,length(p)-1) of p) .. |
| 410 |
controls (postcontrol length(p)-1 of p) and (precontrol length(p) of p) |
| 411 |
.. q |
| 412 |
enddef; |
| 413 |
% --- |
| 414 |
def make_cycle expr p = % |length(p)>0| |
| 415 |
(subpath(0,length(p)-1) of p) .. |
| 416 |
controls (postcontrol length(p)-1 of p) and (precontrol length(p) of p) |
| 417 |
.. cycle |
| 418 |
enddef; |
| 419 |
% --- |
| 420 |
vardef is_line(expr B) = |
| 421 |
% checks if a B\'ezier segment |B| is an almost straight line; |
| 422 |
% recall that |z reflectedabout (origin,right)=1/z| for a complex |
| 423 |
% number |z| such that |length(z)=1|; recall also that the multiplication |
| 424 |
% of complex numbers (|zscale| operation) implies the addition of |
| 425 |
% their angle arguments |
| 426 |
save pa_,pb_,pc_,pd_,ba_,da_,dc_; pair pa_,pb_,pc_,pd_,ba_,da_,dc_; |
| 427 |
pa_:=point 0 of B; pd_:=point 1 of B; |
| 428 |
if distance(pa_,pd_)<epsil.dist: |
| 429 |
false % either really not a line or an uncertain situation (rounding errors) |
| 430 |
else: |
| 431 |
da_=unitvector(pd_-pa_) reflectedabout (origin,right); |
| 432 |
pb_:=postcontrol 0 of B; if distance(pa_,pb_)<epsil.dist: pb_:=pa_; fi |
| 433 |
pc_:=precontrol 1 of B; if distance(pd_,pc_)<epsil.dist: pc_:=pd_; fi |
| 434 |
if (pa_=pb_) and (pc_=pd_): true |
| 435 |
elseif (pa_=pb_): |
| 436 |
dc_=unitvector(pd_-pc_); abs(angle(dc_ zscaled da_))<epsil.ang |
| 437 |
elseif (pc_=pd_): |
| 438 |
ba_=unitvector(pb_-pa_); abs(angle(ba_ zscaled da_))<epsil.ang |
| 439 |
else: |
| 440 |
ba_=unitvector(pb_-pa_); dc_=unitvector(pd_-pc_); |
| 441 |
(abs(angle(ba_ zscaled da_))<epsil.ang) |
| 442 |
and (abs(angle(dc_ zscaled da_))<epsil.ang) |
| 443 |
fi |
| 444 |
fi |
| 445 |
enddef; |
| 446 |
% --- |
| 447 |
vardef is_tiny_bez(expr B) = |
| 448 |
% checks if B\'ezier segment |B| is negligibly small |
| 449 |
(distance((postcontrol 0 of B),(point 0 of B))<epsil.dist) |
| 450 |
and (distance((precontrol 1 of B),(point 0 of B))<epsil.dist) |
| 451 |
and (distance((point 1 of B),(point 0 of B))<epsil.dist) |
| 452 |
enddef; |
| 453 |
% --- |
| 454 |
vardef are_parallel(expr B,C) = |
| 455 |
% checks if B\'ezier segments |B| and |C| are linear and parallel |
| 456 |
save a_; |
| 457 |
if is_line(B) and is_line(C): |
| 458 |
a_:=turn_ang((point 0 of B)-(point 1 of B),(point 0 of C)-(point 1 of C)); |
| 459 |
(if known a_: abs(a_)<epsil.ang else: false fi) |
| 460 |
else: false fi |
| 461 |
enddef; |
| 462 |
% --- |
| 463 |
vardef tidy_lines(expr P) = |
| 464 |
% converts almost linear segments of a path |P| into a ``tidy'' lines (|--|) |
| 465 |
save B_; path B_; |
| 466 |
for i_:=1 upto length(P): if i_>1: & fi |
| 467 |
hide(B_:=subpath (i_-1,i_) of P) |
| 468 |
if is_line(B_): ((point 0 of B_)--(point 1 of B_)) else: B_ fi |
| 469 |
endfor if cycle P: & cycle fi |
| 470 |
enddef; |
| 471 |
% --- |
| 472 |
def add_bez(expr ta,tb, p) = |
| 473 |
.. controls (postcontrol ta of p) and (precontrol tb of p) .. (point tb of p) |
| 474 |
enddef; |
| 475 |
% --- |
| 476 |
vardef clean_path(expr P) = |
| 477 |
% this routine joins together colinear neighbouring segments and removes |
| 478 |
% ``tiny'' edges of a cyclic path |P| (performed at the end of removing |
| 479 |
% overlaps and expanding stroke); since some nodes may become ``midline'' |
| 480 |
% ones after cleaning, the operation is performed twice |
| 481 |
if cycle P: |
| 482 |
save P_,for_del_,not_del_,i_,j_; path P_; |
| 483 |
% mark all deletable nodes and one non-deletable node: |
| 484 |
for i_:=0 upto length(P)-1: |
| 485 |
if are_parallel(subpath (i_-1,i_) of P,subpath (i_,i_+1) of P) |
| 486 |
or is_tiny_bez(subpath(i_-1,i_) of P): |
| 487 |
for_del_[i_]:=1; |
| 488 |
else: |
| 489 |
not_del_:=i_; |
| 490 |
fi |
| 491 |
endfor; |
| 492 |
% BUG TRAP: |
| 493 |
if unknown not_del_: |
| 494 |
err_helpless; |
| 495 |
errmessage "ROEX ERROR: all nodes deleted during path cleaning"; |
| 496 |
fi |
| 497 |
% delete nodes: |
| 498 |
i_:=j_:=not_del_; % we start with |not_del_|: one of not deleted points |
| 499 |
P_:=(point j_ of P) |
| 500 |
forever: |
| 501 |
% invariant: |i_| recent not deleted point, |j_| current point |
| 502 |
hide(j_:=(j_+1) mod length(P)) |
| 503 |
if unknown for_del_[j_]: add_bez(i_,j_,P) \\ hide(i_:=j_) fi |
| 504 |
exitif j_=not_del_; |
| 505 |
endfor & cycle; |
| 506 |
tidy_lines(P_) |
| 507 |
else: P fi |
| 508 |
enddef; |
| 509 |
% --- |
| 510 |
vardef is_less(expr a,b) = (a<b) enddef; |
| 511 |
vardef quicksort@#(expr ii,jj)(suffix s)(text t) = |
| 512 |
% sorts |@#.s[ii..jj]| along with |@#.$[ii..jj]| for |$| in |t|, |
| 513 |
% using Tony Hoare's ``quick sort'' method; suffix |s| must must not occur |
| 514 |
% in the |t| list (no checking is performed); if both |s| and |t| are empty, |
| 515 |
% |t| is ignored. |
| 516 |
% REMARK 1: the algorithm has no explicit recursion, because of \MF's limits |
| 517 |
% on recursion level. |
| 518 |
% REMARK 2: the algorithm, of course, is not stable, i.e., it does not |
| 519 |
% preserve the order of equal items, but it does not matter here |
| 520 |
save i_,j_,k_,l_,cell_,stack_,incl_t_; boolean incl_t_; |
| 521 |
pair stack_[\\]; stack_.lev:=0; stack_[incr stack_.lev]:=(ii,jj); |
| 522 |
i_:=0; for $:=t: i_:=i_+1; endfor % ``measure'' |t|-list |
| 523 |
incl_t_:=(str s <> "") or ((str s = "") and (i_<>0)); |
| 524 |
forsuffixes $:= s if incl_t_: , t fi: |
| 525 |
if numeric @#.$[ii]: numeric cell_.$; |
| 526 |
elseif string @#.$[ii]: string cell_.$; |
| 527 |
elseif boolean @#.$[ii]: boolean cell_.$; |
| 528 |
fi |
| 529 |
endfor |
| 530 |
forever: |
| 531 |
exitif stack_.lev<=0; |
| 532 |
numeric i_,j_; (i_,j_)=stack_[stack_.lev]; stack_.lev:=stack_.lev-1; |
| 533 |
if i_<j_: |
| 534 |
forsuffixes $:= s if incl_t_: , t fi: cell_.$:=@#.$[i_]; endfor |
| 535 |
l_:=i_; |
| 536 |
for k_:=i_+1 upto j_: |
| 537 |
if is_less(@#.s[k_],cell_.s): |
| 538 |
forsuffixes $:=s if incl_t_: , t fi: |
| 539 |
@#.$[l_]:=@#.$[k_]; @#.$[k_]:=@#.$[l_+1]; |
| 540 |
endfor |
| 541 |
l_:=l_+1; |
| 542 |
fi |
| 543 |
endfor |
| 544 |
forsuffixes $:= s if incl_t_: , t fi: @#.$[l_]:=cell_.$; endfor |
| 545 |
stack_[incr stack_.lev]:=(i_,l_-1); stack_[incr stack_.lev]:=(l_+1,j_); |
| 546 |
fi |
| 547 |
endfor |
| 548 |
enddef; |
| 549 |
% --- |
| 550 |
% R-O MACROS: |
| 551 |
% --- |
| 552 |
% visualising macros (useful for testing): |
| 553 |
% --- |
| 554 |
def mark_nodes = |
| 555 |
if proofing>0: |
| 556 |
for i_:=1 upto NODE_.num: |
| 557 |
makelabel(decimal(i_) & ":" & decimal(NODE_.pth[i_]), |
| 558 |
point TIME_[NODE_.pth[i_]]tim[NODE_.tim[i_]] of PATH_[NODE_.pth[i_]]); |
| 559 |
endfor |
| 560 |
fi |
| 561 |
enddef; |
| 562 |
% --- |
| 563 |
def mark_area(expr i) = |
| 564 |
begingroup |
| 565 |
save j_,v_; j_:=i; mark_edge(j_); v_[j_]:=0; |
| 566 |
forever: j_:=EDGE_.out[j_]; exitif (j_=i) or (known v_.emerg); |
| 567 |
if known v_[j_]: v_.emerg:=0; else: mark_edge(j_); v_[j_]:=0; fi |
| 568 |
endfor |
| 569 |
endgroup |
| 570 |
enddef; |
| 571 |
% --- |
| 572 |
def mark_edge(expr i) = |
| 573 |
begingroup |
| 574 |
if proofing>0: |
| 575 |
save currentpen, currentpen_path; pen currentpen; path currentpen_path; |
| 576 |
makelabel(decimal(i), |
| 577 |
(point .5length(the_edge(i)) of the_edge(i))+ |
| 578 |
1pt*(udir .5length(the_edge(i)) of the_edge(i)) rotated 90); |
| 579 |
pickup pencircle scaled 1; |
| 580 |
draw (point .5length(the_edge(i)) of the_edge(i))-- |
| 581 |
((point .5length(the_edge(i)) of the_edge(i))+ |
| 582 |
(1pt*(udir .5length(the_edge(i)) of the_edge(i)) rotated 90)); |
| 583 |
makelabel("", point 0 of the_edge(i)); |
| 584 |
fi |
| 585 |
endgroup |
| 586 |
enddef; |
| 587 |
% --- |
| 588 |
def mark_edges = |
| 589 |
for i_:=-EDGE_.num upto EDGE_.num: if i_<>0: mark_edge(i_); fi endfor |
| 590 |
enddef; |
| 591 |
% --- |
| 592 |
def show_area(expr i) = |
| 593 |
begingroup |
| 594 |
save j_,v_; j_:=i; |
| 595 |
message "EDGE " & decimal(j_) & "/" & decimal(EDGE_.pth[j_]) & ":"; |
| 596 |
message "color " & |
| 597 |
if known EDGE_.col[j_]: decimal(EDGE_.col[j_]) else: "???" fi; |
| 598 |
v_[j_]:=0; |
| 599 |
forever: j_:=EDGE_.out[j_]; exitif (j_=i) or (known v_.emerg); |
| 600 |
if known v_[j_]: v_.emerg:=0; fi |
| 601 |
v_[j_]:=0; message " " & decimal(j_) & "/" & decimal(EDGE_.pth[j_]); |
| 602 |
endfor |
| 603 |
endgroup |
| 604 |
enddef; |
| 605 |
% --- |
| 606 |
def show_areas = |
| 607 |
for i_:=-EDGE_.num upto EDGE_.num: if i_<>0: show_area(i_); fi endfor |
| 608 |
enddef; |
| 609 |
% --- |
| 610 |
def err_helpless = |
| 611 |
errhelp "Better stop now! Algorithm failed."; |
| 612 |
enddef; |
| 613 |
% --- |
| 614 |
def err_extra_info(expr i,j) = |
| 615 |
message |
| 616 |
"========================== BEGIN OF ERROR INFO: =========================="; |
| 617 |
for k_:=i,j: |
| 618 |
if known k_: |
| 619 |
message "Edge " & decimal(k_) & |
| 620 |
" (a subpath of the path " & decimal(EDGE_.pth[k_]) & "):"; |
| 621 |
message "Color:"; show EDGE_.col[k_]; show the_edge(k_); |
| 622 |
fi |
| 623 |
endfor; |
| 624 |
enddef; |
| 625 |
% --- |
| 626 |
% principal macros: |
| 627 |
% --- |
| 628 |
vardef edge_path(expr i) = PATH_[EDGE_.pth[i]] enddef; |
| 629 |
vardef first_time(expr i) = |
| 630 |
TIME_[NODE_.pth[EDGE_.fnd[i]]]tim[NODE_.tim[EDGE_.fnd[i]]] |
| 631 |
enddef; |
| 632 |
vardef last_time(expr i) = |
| 633 |
TIME_[NODE_.pth[EDGE_.lnd[i]]]tim[NODE_.tim[EDGE_.lnd[i]]] |
| 634 |
enddef; |
| 635 |
% --- |
| 636 |
vardef the_edge(expr i) = |
| 637 |
if i>0: pos_subpath else: neg_subpath fi |
| 638 |
(first_time(i), last_time(i)) of edge_path(i) |
| 639 |
enddef; |
| 640 |
% --- |
| 641 |
vardef make_area(expr i) = |
| 642 |
save j_,q_,v_; path q_; j_:=i; v_[j_]:=0; q_:=the_edge(j_); |
| 643 |
forever: j_:=EDGE_.out[j_]; exitif (j_=i) or (known v_.emerg); |
| 644 |
if known v_[j_]: |
| 645 |
show_area(i); err_helpless; |
| 646 |
errmessage "RO ERROR: Edge " & decimal(j_) & " revisited"; |
| 647 |
v_.emerg:=0; |
| 648 |
fi |
| 649 |
v_[j_]:=0; q_:=q_ && the_edge(j_); |
| 650 |
endfor |
| 651 |
make_cycle(q_) |
| 652 |
enddef; |
| 653 |
% --- |
| 654 |
vardef is_tangent(expr i,j,k,l) = |
| 655 |
save e_,d_,pi_,pj_,ti_,tj_; path e_,pi_,pj_; |
| 656 |
if (TIME_[i]num=0) or (TIME_[j]num=0): true |
| 657 |
else: |
| 658 |
ti_.loc:=TIME_[i]tim[k]; |
| 659 |
ti_.prv:=TIME_[i]tim[(k-1) mod (TIME_[i]num+1)]; |
| 660 |
ti_.nxt:=TIME_[i]tim[(k+1) mod (TIME_[i]num+1)]; |
| 661 |
tj_.loc:=TIME_[j]tim[l]; |
| 662 |
tj_.prv:=TIME_[j]tim[(l-1) mod (TIME_[j]num+1)]; |
| 663 |
tj_.nxt:=TIME_[j]tim[(l+1) mod (TIME_[j]num+1)]; |
| 664 |
pi_:=PATH_[i] shifted (-point ti_.loc of PATH_[i]); |
| 665 |
pj_:=PATH_[j] shifted (-point tj_.loc of PATH_[j]); |
| 666 |
d_:=min( |
| 667 |
distance(point ti_.loc of pi_, point ti_.prv of pi_), |
| 668 |
distance(point ti_.loc of pi_, point ti_.nxt of pi_), |
| 669 |
distance(point tj_.loc of pj_, point tj_.prv of pj_), |
| 670 |
distance(point tj_.loc of pj_, point tj_.nxt of pj_)); |
| 671 |
% BUG TRAP 1: |
| 672 |
if d_<epsil.dist: |
| 673 |
err_helpless; |
| 674 |
errmessage "RO ERROR: Cannot check tangency (too short secants)"; |
| 675 |
fi |
| 676 |
e_:=enc.pth scaled (1/2[epsil.dist,d_]); |
| 677 |
save ta_,tb_,tc_,td_; |
| 678 |
save tt_; |
| 679 |
(tt_,ta_)=(pos_subpath (ti_.prv,ti_.loc) of pi_) intersectiontimes e_; |
| 680 |
save tt_; |
| 681 |
(tt_,tb_)=(pos_subpath (ti_.loc,ti_.nxt) of pi_) intersectiontimes e_; |
| 682 |
save tt_; |
| 683 |
(tt_,tc_)=(pos_subpath (tj_.prv,tj_.loc) of pj_) intersectiontimes e_; |
| 684 |
save tt_; |
| 685 |
(tt_,td_)=(pos_subpath (tj_.loc,tj_.nxt) of pj_) intersectiontimes e_; |
| 686 |
% BUG TRAP 2: |
| 687 |
if (ta_<0) or (tb_<0) or (tc_<0) or (td_<0): |
| 688 |
err_helpless; errmessage "RO ERROR: Cannot check tangency"; |
| 689 |
fi |
| 690 |
forsuffixes tt_:=tb_,tc_,td_: tt_:=(tt_-ta_) mod enc.len; endfor |
| 691 |
((tc_>=tb_) and (td_>=tb_)) or ((tc_<=tb_) and (td_<=tb_)) |
| 692 |
fi |
| 693 |
enddef; |
| 694 |
% --- |
| 695 |
vardef multi_path_case = PATH_.num>1 enddef; |
| 696 |
def prepare_input_data(text P)(text W) = |
| 697 |
% |P|: list of paths to be processed (non-cyclic paths are ignored); |
| 698 |
% |W|: list of weights given as pairs: (index, value) |
| 699 |
PATH_.num:=0; |
| 700 |
for P_:=P: if cycle P_: PATH_[incr PATH_.num]:=touch_path(P_); fi endfor |
| 701 |
for W_:=W: PATH_.wei[xpart W_]:=ypart W_; endfor |
| 702 |
for i_:=1 upto PATH_.num: |
| 703 |
if unknown PATH_.wei[i_]: PATH_.wei[i_]:=1; fi |
| 704 |
endfor |
| 705 |
enddef; |
| 706 |
% --- |
| 707 |
def initialise_removing_overlaps = |
| 708 |
% Given paths are |PATH_1|, |PATH_2|, ..., |PATH_[P.num]|; |
| 709 |
% if |PATH_[i][j]| is known, paths |PATH_[i]| and |PATH_[j]| at least touch |
| 710 |
% each other; |PATH_.wei[i]| is a weight of a path (corresponds to |
| 711 |
% multiplying a turning number by this value or, in other words, to |
| 712 |
% applying |PATH_.wei[i]| times a fill operation to the path |PATH_[i]|). |
| 713 |
numeric PATH_.num, PATH_[\\][\\], PATH_.wei[\\]; path PATH_[\\]; |
| 714 |
% |
| 715 |
% Lone paths are stored in variable |LONE_|; |LONE_.col[i]| determines |
| 716 |
% the color (being an integer number) of the plane surrounding the path |
| 717 |
% |LONE_[i]|; |LONE_.wei| is a weight inherited from |PATH_.wei| (see above). |
| 718 |
numeric LONE_.num, LONE_.col[\\], LONE_.wei[\\]; path LONE_[\\]; |
| 719 |
% |
| 720 |
% |TIME_[i]num| is the number of intersection points for paths |PATH_[i]|, |
| 721 |
% |TIME_[i]tim[j]| is the time of intersection of the |j|-th point of path |
| 722 |
% |PATH_[i]| (points are sorted with respect to time), |TIME_[i]ntp[j]| |
| 723 |
% marks non-tangent points (if known), |TIME_[i]nod[j]| is the node number |
| 724 |
% of |j|-th point of path |PATH_[i]| (only non-tangent points are considered |
| 725 |
% to be nodes, points on a path are numbered from |0|). |
| 726 |
numeric TIME_[\\]num, TIME_[\\]tim[\\], TIME_[\\]ntp[\\], TIME_[\\]nod[\\]; |
| 727 |
% |
| 728 |
% Variables with prefix |EDGE_| describe the edge structure that results from |
| 729 |
% intersecting process; the data structure is similar to Dijkstra's data |
| 730 |
% structure for the algorithm finding the convex hull of for a given |
| 731 |
% set of points (E. W. Dijkstra, ``A Discipline of programming'', |
| 732 |
% Prentice-Hall, Inc., 1976): the edges (edsges?) are numbered |
| 733 |
% |-EDGE_.num|, |-EDGE_.num+1|, ..., |-1|, |1|, ... |EDGE_.num-1|, |
| 734 |
% |EDGE_.num|; edges |i| and |-i| are in fact the same edge but |
| 735 |
% differently oriented, the positive value denotes the edge which |
| 736 |
% direction is consistent with the direction of the original path; |
| 737 |
% |EDGE_.out[i]| is the number of the leftmost edge outcoming from |
| 738 |
% the last node, i.e., |EDGE_.lnd[i]|; the number of the first node |
| 739 |
% of |i|-th edge is |EDGE_.fnd[i]|; color of |i|-th edge, i.e., the color |
| 740 |
% of the area surrounded by the edge and its leftmost successors is stored |
| 741 |
% in |EDGE_.col[i]|; |i|-th edge belongs to the path of |PATH_[EDGE_.pth[i]]|; |
| 742 |
% |EDGE_.aux[\\]| is an auxiliary variable; all intersecting paths |
| 743 |
% can be grouped into |SPOT_.num| of disjoint ``spots''; for |i=1|, |2|, ..., |
| 744 |
% |SPOT_.num|, |EDGE_.bed[i]| is the number of the edge which leftmost |
| 745 |
% successors form the area being a boundary of the intersecting paths |
| 746 |
% for a given spot and |EDGE_.bpa[i]| is the boundary (since there is |
| 747 |
% one-to-one correspondence between boundaries and spots, boundaries are |
| 748 |
% pairwise disjoint, too). |
| 749 |
numeric EDGE_.num, EDGE_.pth[\\], EDGE_.out[\\], EDGE_.aux[\\], EDGE_.col[\\], |
| 750 |
EDGE_.fnd[\\], EDGE_.lnd[\\], EDGE_.are[\\], EDGE_.bed[\\]; |
| 751 |
path EDGE_.bpa[\\]; |
| 752 |
% |
| 753 |
% Variables with prefix |NODE_| describe the node structure and are related |
| 754 |
% to the edge structure; the node is not a point on a plane but a point on a |
| 755 |
% path, hence several nodes may correspond to one Euclidian point; |
| 756 |
% |NODE_.num| is the number of nodes, |NODE_.pth[i]| is the number of a path |
| 757 |
% to which the node |i| belongs, |NODE_.tim[i]| is the corresponding time on |
| 758 |
% path |PATH_[NODE_.pth[i]]|, |NODE_.ped[i]| is the ordering number of a |
| 759 |
% positively-numbered edge leaving the node |i|, |NODE_.ned[i]| is the |
| 760 |
% ordering number of negatitively-numbered edge leaving node |i|, |
| 761 |
% |NODE_.nod[i]num| is the number of nodes coinciding with node |i| and |
| 762 |
% these are nodes |NODE_.nod[i]1|, |NODE_.nod[i]2|, ..., |
| 763 |
% |NODE_.nod[i][NODE_.nod[i]num]|. |
| 764 |
numeric NODE_.num, NODE_.pth[\\], NODE_.tim[\\], NODE_.ned[\\], |
| 765 |
NODE_.ped[\\], NODE_.nod[\\]num, NODE_.nod[\\][\\]; |
| 766 |
% |
| 767 |
% It often happens that intersecting paths form disjoint areas or that |
| 768 |
% there are paths that do not intersect; it is crucial for the colouring |
| 769 |
% algorithm to know the embedding ``hierarchy''; the hierarchy is stored in |
| 770 |
% a tree structure: the links (suffix |emb|) point upward (from leaves to |
| 771 |
% the root), moreover, with each leaf (node) is associated a ``level,'' |
| 772 |
% i.e., the number of leaves beneath this leaf; the information stored in |
| 773 |
% a leaf is either the number of a lone path or the number of an area being |
| 774 |
% the result of the intersecting process (negative value marks the former |
| 775 |
% case); the tree is built by adding at first lone paths and next boundary |
| 776 |
% paths, i.e., negatively oriented paths surrounding groups of paths (areas, |
| 777 |
% see below) resulting from the intersecting process; if there is a lone path |
| 778 |
% or a boundary path |q| embedded in a boundary path |p|, there must be also |
| 779 |
% an area which belongs to the group of areas surrounded by |p|, which apears |
| 780 |
% in the tree between |q| and |p|; such a structure is convenient at the |
| 781 |
% stage of finding colors of areas and lone paths. |
| 782 |
numeric TREE_.num,TREE_.pth[\\],TREE_.emb[\\],TREE_.lev[\\]; |
| 783 |
% |
| 784 |
% Finally there are areas which arise during intersecting process; |
| 785 |
% |AREA_1|, |AREA_2|, ..., |AREA_[AREA_.num]| are the ordering numbers |
| 786 |
% of edges which leftmost successors form areas such that areas arising |
| 787 |
% from |AREA_[i]| and |AREA_[j]| are either disjoint or have at most a common |
| 788 |
% edge, and, moreover, areas arising from |AREA_1|, |AREA_2|, ..., |
| 789 |
% |AREA_[AREA_.num]| exhaust the list of all possible areas in question |
| 790 |
% (plus lone paths, i.e., with no intersecting points); |AREA_.spt[i]| is |
| 791 |
% a spot number (areas of the same spot number are subsets of the same |
| 792 |
% boundary, different boundaries are disjoint); areas are sorted wrt spot |
| 793 |
% numbers, moreover, |AREA_[SPOT_[s-1]+1]| thru |AREA_[SPOT_[s]]| are areas |
| 794 |
% belonging to a spot |s|, |s=1|, |2|, ..., |SPOT_.num|. |
| 795 |
numeric AREA_.num, AREA_[\\], AREA_.spt[\\]; numeric SPOT_.num, SPOT_[\\]; |
| 796 |
enddef; |
| 797 |
% --- |
| 798 |
vardef is_far_enough(expr i,k,dk) = |
| 799 |
if act_idx_>=max_idx: were_more_:=1; false |
| 800 |
else: |
| 801 |
save z_; pair z_; z_:=point k+dk of PATH_[i]; |
| 802 |
true |
| 803 |
for j_:=0 upto TIME_[i]num: |
| 804 |
and |
| 805 |
if multi_path_case: |
| 806 |
(distance(point TIME_[i]tim[j_] of PATH_[i],z_)>=epsil.dist) |
| 807 |
else: |
| 808 |
(interval(TIME_[i]tim[j_],(k+dk),PATH_[i])>=epsil.time) |
| 809 |
fi |
| 810 |
endfor |
| 811 |
for j_:=0 upto ignored_.num: |
| 812 |
and |
| 813 |
if multi_path_case: |
| 814 |
(distance(point ignored_[j_] of PATH_[i],z_)>=epsil.dist) |
| 815 |
else: |
| 816 |
(interval(ignored_[j_],(k+dk),PATH_[i])>=epsil.time) |
| 817 |
fi |
| 818 |
endfor |
| 819 |
fi |
| 820 |
enddef; |
| 821 |
% --- |
| 822 |
def intersect_two_segments(expr i,j,k,l) = |
| 823 |
begingroup |
| 824 |
save pi_,pj_,stack_; path pi_,pj_,stack_[\\]; numeric stack_.lev; |
| 825 |
pi_:=subpath (k,k+1) of PATH_[i]; pj_:=subpath (l,l+1) of PATH_[j]; |
| 826 |
stack_.lev:=1; stack_[stack_.lev]:=pj_; |
| 827 |
forever: |
| 828 |
exitif stack_.lev<=0; |
| 829 |
pj_:=stack_[stack_.lev]; stack_.lev:=stack_.lev-1; |
| 830 |
save dk_,dl_; (dk_,dl_)=pi_ intersectiontimes pj_; |
| 831 |
if dk_>=0: |
| 832 |
if is_far_enough(i,k,dk_): |
| 833 |
act_idx_:=act_idx_+1; TIME_[i].tim[incr TIME_[i].num]:=k+dk_; |
| 834 |
else: ignored_[incr ignored_.num]:=k+dk_; |
| 835 |
fi |
| 836 |
if (dl_+epsil.time)<length(pj_): |
| 837 |
stack_[incr stack_.lev]:=subpath (dl_+epsil.time,length pj_) of pj_; |
| 838 |
fi |
| 839 |
if (dl_-epsil.time)>0: |
| 840 |
stack_[incr stack_.lev]:=subpath (0,dl_-epsil.time) of pj_; |
| 841 |
fi |
| 842 |
fi |
| 843 |
endfor |
| 844 |
endgroup |
| 845 |
enddef; |
| 846 |
% --- |
| 847 |
def intersect_two_paths(expr i,j) = |
| 848 |
begingroup |
| 849 |
save ignored_,were_more_,act_idx_; |
| 850 |
act_idx_:=0; ignored_.num:=-1; |
| 851 |
for k_:=0 upto length(PATH_[i])-1: |
| 852 |
for l_:=0 upto length(PATH_[j])-1: |
| 853 |
if (i<>j) or (k_<>l_): |
| 854 |
intersect_two_segments(i,j,k_,l_); |
| 855 |
fi |
| 856 |
endfor |
| 857 |
endfor |
| 858 |
if known were_more_: |
| 859 |
errhelp "Dangerous situation: rounding errors may screw up results."; |
| 860 |
errmessage "RO ERROR: there were more than " |
| 861 |
& decimal(max_idx) & " intersections (thus some were ignored)"; |
| 862 |
fi |
| 863 |
quicksort TIME_[i](0,TIME_[i].num)(tim)(); |
| 864 |
endgroup |
| 865 |
enddef; |
| 866 |
% --- |
| 867 |
def intersect_all_paths = |
| 868 |
for i_:=1 upto PATH_.num: TIME_[i_]num:=-1; endfor |
| 869 |
for i_:=1 upto PATH_.num: |
| 870 |
for j_:=i_+1 upto PATH_.num: |
| 871 |
if xpart(PATH_[i_] intersectiontimes PATH_[j_])>-1: |
| 872 |
PATH_[i_][j_]:=0; |
| 873 |
% the process is repeated twice (for both paths in turn) because we haven't |
| 874 |
% invented an efficient soultion to the following problem: |
| 875 |
% given a subpath |S| of a path |P| and a time |t.S|; find a time |t.P| such |
| 876 |
% that |point t.S of S=point t.P of P| |
| 877 |
intersect_two_paths(i_,j_); intersect_two_paths(j_,i_); |
| 878 |
fi |
| 879 |
endfor |
| 880 |
endfor |
| 881 |
if not multi_path_case: |
| 882 |
for i_:=1 upto PATH_.num: |
| 883 |
PATH_[i_][i_]:=0; intersect_two_paths(i_,i_); |
| 884 |
endfor |
| 885 |
fi |
| 886 |
enddef; |
| 887 |
% --- |
| 888 |
vardef find_minimal_secant = |
| 889 |
save secants_, intervals_; |
| 890 |
secants_.num:=0; intervals_.num:=0; |
| 891 |
minimal_secant:=minimal_interval:=infinity; |
| 892 |
for i_:=1 upto PATH_.num: |
| 893 |
for j_:=0 upto TIME_[i_].num: |
| 894 |
if TIME_[i_].num>0: |
| 895 |
secants_[if tracingremoving>1: incr fi \\ secants_.num]:= |
| 896 |
distance(point TIME_[i_]tim[j_] of PATH_[i_], |
| 897 |
point TIME_[i_]tim[(j_+1) mod (TIME_[i_].num+1)] of PATH_[i_]); |
| 898 |
if tracingremoving>1: |
| 899 |
secants_.pth[secants_.num]:=i_; secants_.tim[secants_.num]:=j_; |
| 900 |
fi |
| 901 |
minimal_secant:=min(minimal_secant,secants_[secants_.num]); |
| 902 |
intervals_[if tracingremoving>1: incr fi \\ intervals_.num]:= |
| 903 |
interval(TIME_[i_]tim[j_], |
| 904 |
TIME_[i_]tim[(j_+1) mod (TIME_[i_].num+1)], PATH_[i_]); |
| 905 |
if tracingremoving>1: |
| 906 |
intervals_.pth[intervals_.num]:=i_; intervals_.tim[intervals_.num]:=j_; |
| 907 |
fi |
| 908 |
minimal_interval:=min(minimal_interval,intervals_[intervals_.num]); |
| 909 |
fi |
| 910 |
endfor; |
| 911 |
endfor; |
| 912 |
if minimal_secant<>infinity: |
| 913 |
info_ro "Minimal secant = " & decimal(minimal_secant/pt) |
| 914 |
& "pt, i.e., " & decimal(minimal_secant) & "pxl, " & |
| 915 |
if minimal_secant<4/3epsil.dist: "CAVEAT!" else: "seems OK" fi |
| 916 |
& " (bound=" & decimal(epsil.dist) & "pxl)"; |
| 917 |
fi |
| 918 |
if minimal_interval<>infinity: |
| 919 |
info_ro "Minimal interval = " & decimal(minimal_interval) & ", " & |
| 920 |
if minimal_interval<4/3epsil.time: "CAVEAT!" else: "seems OK" fi |
| 921 |
& " (bound=" & decimal(epsil.time) & ")"; |
| 922 |
fi |
| 923 |
if tracingremoving>1: |
| 924 |
quicksort secants_(1,secants_.num)()(tim,pth); |
| 925 |
quicksort intervals_(1,intervals_.num)()(tim,pth); |
| 926 |
for i_:=1 upto secants_.num: |
| 927 |
info_ro "secant=" & decimal(secants_[i_]) |
| 928 |
& " path=" & decimal(secants_.pth[i_]) |
| 929 |
& " time=" & decimal(secants_.tim[i_]); |
| 930 |
endfor |
| 931 |
for i_:=1 upto intervals_.num: |
| 932 |
info_ro "interval=" & decimal(intervals_[i_]) |
| 933 |
& " path=" & decimal(intervals_.pth[i_]) |
| 934 |
& " time=" & decimal(intervals_.tim[i_]); |
| 935 |
endfor |
| 936 |
fi |
| 937 |
enddef; |
| 938 |
% --- |
| 939 |
def build_node_structure = |
| 940 |
begingroup |
| 941 |
save n_,Tik_,Tjl_; |
| 942 |
NODE_.num:=0; |
| 943 |
for i_:=1 upto PATH_.num: |
| 944 |
for j_:=i_ if multi_path_case: +1 fi upto PATH_.num: |
| 945 |
if known PATH_[i_][j_]: |
| 946 |
for k_:=0 upto TIME_[i_]num: |
| 947 |
for l_:=if i_=j_: k_ else: 0 fi upto TIME_[j_]num: |
| 948 |
if distance(point TIME_[i_]tim[k_] of PATH_[i_], |
| 949 |
point TIME_[j_]tim[l_] of PATH_[j_])<epsil.dist: |
| 950 |
if if multi_path_case: not is_tangent(i_,j_,k_,l_) else: true fi: |
| 951 |
TIME_[i_]ntp[k_]:=1; TIME_[j_]ntp[l_]:=1; |
| 952 |
if unknown TIME_[i_]nod[k_]: |
| 953 |
NODE_.num:=NODE_.num+1; TIME_[i_]nod[k_]:=NODE_.num; |
| 954 |
NODE_.pth[NODE_.num]:=i_; NODE_.tim[NODE_.num]:=k_; |
| 955 |
NODE_.nod[NODE_.num]num:=0; |
| 956 |
fi |
| 957 |
if unknown TIME_[j_]nod[l_]: |
| 958 |
NODE_.num:=NODE_.num+1; TIME_[j_]nod[l_]:=NODE_.num; |
| 959 |
NODE_.pth[NODE_.num]:=j_; NODE_.tim[NODE_.num]:=l_; |
| 960 |
NODE_.nod[NODE_.num]num:=0; |
| 961 |
fi |
| 962 |
Tik_:=TIME_[i_]nod[k_]; Tjl_:=TIME_[j_]nod[l_]; |
| 963 |
NODE_.nod[Tik_][incr NODE_.nod[Tik_]num]:=Tjl_; |
| 964 |
if (i_<>j_) or (k_<>l_): |
| 965 |
NODE_.nod[Tjl_][incr NODE_.nod[Tjl_]num]:=Tik_; |
| 966 |
fi |
| 967 |
fi |
| 968 |
fi |
| 969 |
endfor |
| 970 |
endfor |
| 971 |
fi |
| 972 |
endfor |
| 973 |
endfor |
| 974 |
% BUG TRAP: |
| 975 |
for i_:=1 upto PATH_.num: |
| 976 |
n_:=0; |
| 977 |
for j_:=0 upto TIME_[i_]num: if known TIME_[i_]ntp[j_]: n_:=n_+1; fi endfor |
| 978 |
if n_=1: |
| 979 |
err_helpless; |
| 980 |
errmessage "RO ERROR: Number of non-tangent points must not be 1 (path " |
| 981 |
& decimal(i_) & ")"; |
| 982 |
fi |
| 983 |
endfor |
| 984 |
endgroup |
| 985 |
enddef; |
| 986 |
% --- |
| 987 |
def identify_close_nodes = |
| 988 |
begingroup |
| 989 |
% It is assumed that `close points' and `coinciding points' means the same, |
| 990 |
% hence we make a transitive closure of the relation of `being close' |
| 991 |
% (in ``normal'' cases the relation is transitive, although from |
| 992 |
% a mathematical point of view it is obviously not): |
| 993 |
forever: |
| 994 |
% Is this loop really needed? I (BJ) could not devise a case where |
| 995 |
% more than two turns would be necessary and where the algorithm |
| 996 |
% would still work properly |
| 997 |
save changed_; |
| 998 |
for i_:=1 upto NODE_.num: |
| 999 |
save v_; |
| 1000 |
for j_:=1 upto NODE_.nod[i_]num: v_[NODE_.nod[i_][j_]]:=0; endfor |
| 1001 |
for j_:=1 upto NODE_.nod[i_]num: |
| 1002 |
k_:=NODE_.nod[i_][j_]; |
| 1003 |
for l_:=1 upto NODE_.nod[k_]num: |
| 1004 |
if NODE_.nod[k_][l_]<>i_: |
| 1005 |
if unknown v_[NODE_.nod[k_][l_]]: |
| 1006 |
NODE_.nod[i_][incr NODE_.nod[i_]num]:=NODE_.nod[k_][l_]; |
| 1007 |
changed_:=v_[NODE_.nod[k_][l_]]:=0; |
| 1008 |
fi |
| 1009 |
fi |
| 1010 |
endfor |
| 1011 |
endfor |
| 1012 |
endfor |
| 1013 |
exitif unknown changed_; |
| 1014 |
endfor |
| 1015 |
endgroup |
| 1016 |
enddef; |
| 1017 |
% --- |
| 1018 |
def build_edge_structure = |
| 1019 |
begingroup |
| 1020 |
numeric min_sec_[\\],min_sec_.tmp; |
| 1021 |
% |min_sec_[i]| is a minimal secant for |i|-th path, |i=1|, |2|, ..., |PATH_.num| |
| 1022 |
save i_,j_,k_; |
| 1023 |
EDGE_.num:=0; |
| 1024 |
if NODE_.num>0: |
| 1025 |
for i_:=1 upto PATH_.num: |
| 1026 |
for j_:=0 upto TIME_[i_]num: |
| 1027 |
if known TIME_[i_]ntp[j_]: |
| 1028 |
EDGE_.num:=EDGE_.num+1; EDGE_.pth[EDGE_.num]=i_; |
| 1029 |
EDGE_.pth[-EDGE_.num]=i_; EDGE_.fnd[EDGE_.num]:=TIME_[i_]nod[j_]; |
| 1030 |
% each path should contain at least two non-tangent nodes |
| 1031 |
k_:=j_+1; |
| 1032 |
forever: exitif known TIME_[i_]ntp[k_ mod (TIME_[i_]num+1)]; |
| 1033 |
k_:=k_+1; |
| 1034 |
endfor; |
| 1035 |
EDGE_.lnd[EDGE_.num]:=TIME_[i_]nod[k_ mod (TIME_[i_]num+1)]; |
| 1036 |
min_sec_.tmp:= |
| 1037 |
distance(point first_time(EDGE_.num) of edge_path(EDGE_.num), |
| 1038 |
point last_time(EDGE_.num) of edge_path(EDGE_.num)); |
| 1039 |
if if unknown min_sec_[i_]: true else: min_sec_[i_]>min_sec_.tmp fi: |
| 1040 |
min_sec_[i_]:=min_sec_.tmp; |
| 1041 |
fi |
| 1042 |
EDGE_.fnd[-EDGE_.num]:=EDGE_.lnd[EDGE_.num]; |
| 1043 |
EDGE_.lnd[-EDGE_.num]:=EDGE_.fnd[EDGE_.num]; |
| 1044 |
fi |
| 1045 |
endfor |
| 1046 |
endfor |
| 1047 |
for i_:=-EDGE_.num upto EDGE_.num: |
| 1048 |
if i_>0: NODE_.ped[EDGE_.fnd[i_]]:=i_; |
| 1049 |
elseif i_<0: NODE_.ned[EDGE_.fnd[i_]]:=i_; |
| 1050 |
fi |
| 1051 |
endfor |
| 1052 |
else: |
| 1053 |
info_ro "RO WARNING: no intersections detected."; |
| 1054 |
fi |
| 1055 |
endgroup |
| 1056 |
enddef; |
| 1057 |
% --- |
| 1058 |
def find_leftmost_edges = |
| 1059 |
% a simple method is used: a tiny circle is drawn in a node and its |
| 1060 |
% intersection points with all edges leaving the node are examined |
| 1061 |
begingroup |
| 1062 |
save ei_,ej_,i_,j_,k_,leftmost_; path ei_,ej_; numeric min_sec_.loc; |
| 1063 |
for i_:=-EDGE_.num upto EDGE_.num: if i_<>0: |
| 1064 |
if tracingleftmost>0: |
| 1065 |
message "@@@ " & decimal(i_) & "/" & decimal(EDGE_.pth[i_]) |
| 1066 |
& " (" & decimal(EDGE_.fnd[i_]) & "," & decimal(EDGE_.lnd[i_]) & "):"; |
| 1067 |
fi |
| 1068 |
numeric leftmost_.edg,leftmost_.tim,leftmost_.tmp; |
| 1069 |
min_sec_.loc:=min_sec_[EDGE_.pth[i_]]; |
| 1070 |
for k_:=1 upto NODE_.nod[EDGE_.lnd[i_]]num: |
| 1071 |
forsuffixes $:=ped,ned: |
| 1072 |
j_:=NODE_$[NODE_.nod[EDGE_.lnd[i_]][k_]]; |
| 1073 |
min_sec_.loc:=min(min_sec_.loc,min_sec_[EDGE_.pth[j_]]); |
| 1074 |
endfor |
| 1075 |
endfor |
| 1076 |
% BUG TRAP 1: (should not happen, see |find_minimal_secant|); |
| 1077 |
if min_sec_.loc<epsil.dist: |
| 1078 |
err_extra_info(i_,whatever); showvariable min_sec_; err_helpless; |
| 1079 |
errmessage "RO ERROR: Cannot continue searching for the leftmost edge"; |
| 1080 |
fi |
| 1081 |
ei_:=enc.pth scaled (1/2[epsil.dist,min_sec_.loc]) |
| 1082 |
shifted point infinity of the_edge(i_); |
| 1083 |
save tei_,tt_; (tei_,tt_)=ei_ intersectiontimes the_edge(i_); |
| 1084 |
for k_:=1 upto NODE_.nod[EDGE_.lnd[i_]]num: |
| 1085 |
forsuffixes $:=ped,ned: |
| 1086 |
j_:=NODE_$[NODE_.nod[EDGE_.lnd[i_]][k_]]; |
| 1087 |
ej_:=enc.pth scaled (1/2[epsil.dist,min_sec_.loc]) |
| 1088 |
shifted point 0 of the_edge(j_); |
| 1089 |
if tracingleftmost>0: |
| 1090 |
message " " & decimal(j_) & "/" & decimal(EDGE_.pth[j_]) |
| 1091 |
& " (" & decimal(EDGE_.fnd[j_]) & "," & decimal(EDGE_.lnd[j_]) & "):"; |
| 1092 |
fi |
| 1093 |
save tej_,tt_; (tej_,tt_)=ej_ intersectiontimes the_edge(j_); |
| 1094 |
% BUG TRAP 2: |
| 1095 |
if (tei_<0) or (tej_<0): |
| 1096 |
err_extra_info(i_,j_); showvariable min_sec_; |
| 1097 |
message "Times: " & decimal(tei_) & " " & decimal(tej_); |
| 1098 |
err_helpless; |
| 1099 |
errmessage "RO ERROR: Unsuccesful search for the leftmost edge"; |
| 1100 |
fi |
| 1101 |
% it happens that |i_=j_| if |multi_path_case=false| |
| 1102 |
leftmost_.tmp:=if (i_=-j_): 0 else: (tej_-tei_) mod enc.len fi; |
| 1103 |
if tracingleftmost>0: |
| 1104 |
message " " & decimal(leftmost_.tmp) & " " & dec_pair((tei_,tej_)); |
| 1105 |
fi |
| 1106 |
if if unknown leftmost_.tim: true else: leftmost_.tmp>leftmost_.tim fi: |
| 1107 |
leftmost_.edg:=j_; leftmost_.tim:=leftmost_.tmp; |
| 1108 |
fi |
| 1109 |
endfor |
| 1110 |
endfor |
| 1111 |
EDGE_.out[i_]:=leftmost_.edg; |
| 1112 |
if tracingleftmost>0: j_:=EDGE_.out[i_]; |
| 1113 |
message ">>> " & decimal(j_) & "/" & decimal(EDGE_.pth[j_]) |
| 1114 |
& " (" & decimal(EDGE_.fnd[j_]) & "," & decimal(EDGE_.lnd[j_]) & "):"; |
| 1115 |
fi |
| 1116 |
fi endfor |
| 1117 |
endgroup |
| 1118 |
enddef; |
| 1119 |
% --- |
| 1120 |
def build_area_structure = |
| 1121 |
begingroup |
| 1122 |
save i_,j_,v_; |
| 1123 |
AREA_.num:=0; |
| 1124 |
for i_:=1 upto EDGE_.num: % \MF's linear equation solver employed |
| 1125 |
EDGE_.col[i_]-PATH_.wei[EDGE_.pth[i_]]=EDGE_.col[-i_]; |
| 1126 |
endfor |
| 1127 |
% split to areas (edges surrounding the same area are assigned the same colour): |
| 1128 |
for i_:=-EDGE_.num upto EDGE_.num: |
| 1129 |
if (i_<>0) and (unknown EDGE_.are[i_]): |
| 1130 |
AREA_[incr AREA_.num]:=i_; |
| 1131 |
save v_; j_:=i_; v_[j_]:=0; EDGE_.are[j_]:=AREA_.num; |
| 1132 |
forever: j_:=EDGE_.out[j_]; exitif (j_=i_) or (known v_.emerg); |
| 1133 |
% BUG TRAP 1: |
| 1134 |
if j_=-i_: |
| 1135 |
err_extra_info(i_,whatever); show_area(i_); err_helpless; |
| 1136 |
errmessage "RO ERROR: strange area"; |
| 1137 |
fi |
| 1138 |
% BUG TRAP 2: |
| 1139 |
if known v_[j_]: |
| 1140 |
err_extra_info(i_,j_); show_area(i_); err_helpless; |
| 1141 |
errmessage "RO ERROR: Edge " & decimal(j_) & " revisited"; |
| 1142 |
v_.emerg:=0; |
| 1143 |
fi |
| 1144 |
v_[j_]:=0; EDGE_.are[j_]:=AREA_.num; |
| 1145 |
if known (EDGE_.col[i_]-EDGE_.col[j_]): |
| 1146 |
% BUG TRAP 3: |
| 1147 |
if (EDGE_.col[i_]-EDGE_.col[j_])<>0: |
| 1148 |
err_extra_info(i_,j_); show_area(i_); err_helpless; |
| 1149 |
errmessage "RO ERROR: Edges " & decimal(i_) & " and " & decimal(j_) |
| 1150 |
& " have inconsistent colors"; |
| 1151 |
fi |
| 1152 |
else: EDGE_.col[i_]=EDGE_.col[j_]; fi |
| 1153 |
endfor |
| 1154 |
fi |
| 1155 |
endfor |
| 1156 |
endgroup |
| 1157 |
enddef; |
| 1158 |
% --- |
| 1159 |
def build_spot_structure = |
| 1160 |
begingroup |
| 1161 |
save i_,j_; |
| 1162 |
% areas having a common edge belong to the same spot (\MF's linear |
| 1163 |
% equation solver employed): |
| 1164 |
for i_:=1 upto EDGE_.num: if (i_<>0): |
| 1165 |
if unknown (AREA_.spt[EDGE_.are[i_]]-AREA_.spt[EDGE_.are[-i_]]): |
| 1166 |
AREA_.spt[EDGE_.are[i_]]=AREA_.spt[EDGE_.are[-i_]]; |
| 1167 |
fi |
| 1168 |
fi endfor |
| 1169 |
% count different spots: |
| 1170 |
SPOT_.num:=0; |
| 1171 |
for i_:=1 upto AREA_.num: |
| 1172 |
if unknown AREA_.spt[i_]: AREA_.spt[i_]=incr SPOT_.num; fi |
| 1173 |
endfor; |
| 1174 |
% sort areas wrt spot numbers: |
| 1175 |
quicksort AREA_(1,AREA_.num)(spt)(); |
| 1176 |
% define |SPOT_[s]|, |s=1|, |2|, ..., |SPOT_.num|, such that |
| 1177 |
% |AREA_[SPOT_[s-1]+1]| thru |AREA_[SPOT_[s]]| are areas having the |
| 1178 |
% same spot number |s|: |
| 1179 |
SPOT_0=0; for i_:=1 upto AREA_.num: SPOT_[AREA_.spt[i_]]:=i_; endfor |
| 1180 |
% identify paths for which |check_turn=-1| (such areas are boundaries and |
| 1181 |
% should be unique for each spot): |
| 1182 |
i_:=0; |
| 1183 |
for j_:=1 upto AREA_.num: |
| 1184 |
if check_turn(make_area(AREA_[j_]))<=0: |
| 1185 |
i_:=i_+1; |
| 1186 |
EDGE_.bar[i_]:=j_; EDGE_.bed[i_]:=AREA_[j_]; |
| 1187 |
EDGE_.bpa[i_]:=make_area(AREA_[j_]); |
| 1188 |
fi |
| 1189 |
endfor |
| 1190 |
% BUG TRAP 4: |
| 1191 |
if i_<>SPOT_.num: |
| 1192 |
message "Number of spots=" & decimal(SPOT_.num) & |
| 1193 |
", number of boundaries=" & decimal(i_); |
| 1194 |
err_helpless; |
| 1195 |
errmessage "RO ERROR: Inconsistent number of spots and boundaries"; |
| 1196 |
fi |
| 1197 |
endgroup |
| 1198 |
enddef; |
| 1199 |
% --- |
| 1200 |
def update_tree_levels(expr n) = |
| 1201 |
% update levels above the inserted leaf (|n|): |
| 1202 |
begingroup |
| 1203 |
save i_,j_; |
| 1204 |
i_:=TREE_.emb[n]; j_:=TREE_.lev[n]; |
| 1205 |
forever: |
| 1206 |
exitif i_=0; |
| 1207 |
j_:=TREE_.lev[i_]:=max(j_+1,TREE_.lev[i_]); i_:=TREE_.emb[i_]; |
| 1208 |
endfor |
| 1209 |
endgroup |
| 1210 |
enddef; |
| 1211 |
% --- |
| 1212 |
vardef embedding_pair(expr p,n) = |
| 1213 |
% returns a pair of numbers, |(out_,in_)|, such that |out_| is either a tree |
| 1214 |
% address of an area surrounded |p| or zero if not found, and |in_| is a |
| 1215 |
% tree address of an area surrounding |p| or zero if not found; one branch |
| 1216 |
% is searched, starting from a zero level leaf, |n|. |
| 1217 |
save in_,out_,q_; path q_; in_:=out_:=0; n_:=n; |
| 1218 |
forever: |
| 1219 |
q_:=if TREE_.pth[n_]<0: LONE_[-TREE_.pth[n_]] |
| 1220 |
else: make_area(AREA_[TREE_.pth[n_]]) fi; |
| 1221 |
check_embedding(p_,q_,r_); |
| 1222 |
if r_=1: % |p_| $\subset$ |q_| |
| 1223 |
if in_=0: in_:=n_; fi % ``minimal'' surrounding path is to be found |
| 1224 |
elseif r_=2: % |q_| $\subset$ |p_| |
| 1225 |
out_:=n_; % ``maximal'' surrounded path is to be found |
| 1226 |
fi |
| 1227 |
exitif TREE_.emb[n_]=0; |
| 1228 |
n_:=TREE_.emb[n_]; |
| 1229 |
endfor |
| 1230 |
(out_,in_) |
| 1231 |
enddef; |
| 1232 |
% --- |
| 1233 |
def add_to_queue (expr p,q) = |
| 1234 |
% updates a queue, i.e., adds to a queue an area belonging to the spot |
| 1235 |
% of a boundary area, i.e., |AREA_[q]|, surrounding a path described by |
| 1236 |
% a tree address |p|. |
| 1237 |
begingroup |
| 1238 |
save p_,r_,s_,found_; path p_; boolean found_; |
| 1239 |
% |AREA_[q]| is a boundary, i.e., it is negatively oriented, |
| 1240 |
p_:=if TREE_.pth[p]<0: LONE_[-TREE_.pth[p]] |
| 1241 |
else: make_area(AREA_[TREE_.pth[p]]) fi; |
| 1242 |
% there must be a unique positively oriented area containing |p_|, belonging |
| 1243 |
% to the ``spot'' of the boundary |AREA_[q]|. |
| 1244 |
found_:=false; s_:=SPOT_[AREA_.spt[q]-1]; |
| 1245 |
% |s_| is the last area from a previous spot, |s_+1| will be the first |
| 1246 |
% area of the current spot |
| 1247 |
forever: s_:=s_+1; |
| 1248 |
if s_<>q: |
| 1249 |
check_embedding(make_area(AREA_[s_]),p_,r_); |
| 1250 |
found_:=(r_=2); % |r_=2| implies |p_| $\subset$ |make_area(AREA_[s_])| |
| 1251 |
fi |
| 1252 |
if found_: |
| 1253 |
if unknown LVQ_.inq[s_]: LVQ_[incr LVQ_.num]:=s_; LVQ_.inq[s_]:=1; fi |
| 1254 |
elseif s_=SPOT_[AREA_.spt[q]]: |
| 1255 |
% the list of candidates has been exhausted without a success, hence |
| 1256 |
% BUG TRAP: |
| 1257 |
err_helpless; |
| 1258 |
errmessage "RO ERROR: cannot build embedding tree (boundary " & |
| 1259 |
decimal(q) & ")"; |
| 1260 |
found_:=true; |
| 1261 |
fi |
| 1262 |
exitif found_; |
| 1263 |
endfor; |
| 1264 |
endgroup |
| 1265 |
enddef; |
| 1266 |
% --- |
| 1267 |
def add_to_tree (expr leaf) = |
| 1268 |
begingroup |
| 1269 |
save boundary_,found_,N_,p_; path p_; |
| 1270 |
TREE_.num:=TREE_.num+1; |
| 1271 |
N_:=TREE_.num; % abbreviation |
| 1272 |
TREE_.pth[N_]:=leaf; TREE_.emb[N_]:=0; TREE_.lev[N_]:=0; |
| 1273 |
p_:=if leaf<0: LONE_[-leaf] else: make_area(AREA_[leaf]) fi; |
| 1274 |
if (leaf>0) and (check_turn(p_)<0): boundary_:=1; fi |
| 1275 |
if N_>1: |
| 1276 |
for l_:=1 upto LVZ_.num: % climbing up from level zero |
| 1277 |
save out_,in_; (out_,in_)=embedding_pair(p_,LVZ_[l_]); |
| 1278 |
if (out_<>0) or (in_<>0): % a feasible branch found |
| 1279 |
if (out_=0) and (in_<>0): % to be added at the bottom, certain |
| 1280 |
TREE_.emb[N_]:=in_; |
| 1281 |
if in_=LVZ_[l_]: LVZ_[l_]:=N_; % replace bottom leaf |
| 1282 |
else: LVZ_[incr LVZ_.num]:=N_; fi % add new bottom leaf |
| 1283 |
elseif (out_<>0) and (in_=0): % to be added at the top, optional |
| 1284 |
if TREE_.emb[out_]<>N_: % we weren't here, add |
| 1285 |
% invariant: |TREE_.emb[out_]=0| |
| 1286 |
TREE_.emb[out_]:=N_; |
| 1287 |
TREE_.lev[N_]:=max(TREE_.lev[N_],TREE_.lev[out_]+1); |
| 1288 |
if known boundary_: add_to_queue(out_,leaf); fi |
| 1289 |
fi |
| 1290 |
else: % to be added in the midst, optional |
| 1291 |
if TREE_.emb[out_]<>N_: % we weren't here, add |
| 1292 |
% invariant: |TREE_.emb[out_]=in_| |
| 1293 |
TREE_.emb[out_]:=N_; TREE_.emb[N_]:=in_; |
| 1294 |
TREE_.lev[N_]:=max(TREE_.lev[N_],TREE_.lev[out_]+1); |
| 1295 |
if known boundary_: add_to_queue(out_,leaf); fi |
| 1296 |
fi |
| 1297 |
fi |
| 1298 |
found_:=1; update_tree_levels(N_); |
| 1299 |
fi |
| 1300 |
endfor; |
| 1301 |
fi |
| 1302 |
if unknown found_: LVZ_[incr LVZ_.num]:=N_; fi % a ``separate'' leaf appeared |
| 1303 |
endgroup |
| 1304 |
enddef; |
| 1305 |
% --- |
| 1306 |
def build_embedding_tree = |
| 1307 |
begingroup |
| 1308 |
save LVZ_,QUE_; |
| 1309 |
% |LVZ_1|, |LVZ_2|, ..., |LVZ_[LVZ_.num]| is the list of zero-level leaves, |
| 1310 |
% (a temporary data structure, used during building a tree), |LVQ_1|, |LVQ_2|, |
| 1311 |
% ..., |LVQ_[LVQ_.num]| is the list of leaves waiting in a queue (also |
| 1312 |
% a temporary data structure); if |LVQ_.inq[i]| is known, |i=1|, |2|, ..., |
| 1313 |
% |AREA_.num|, area |i| is already in a queue. |
| 1314 |
TREE_.num:=0; LONE_.num:=0; LVZ_.num:=0; LVQ_.num:=0; |
| 1315 |
% identify lone paths: |
| 1316 |
for i_:=1 upto PATH_.num: |
| 1317 |
if true for j_:=0 upto TIME_[i_]num: and (unknown TIME_[i_]ntp[j_]) endfor: |
| 1318 |
LONE_[incr LONE_.num]:=PATH_[i_]; LONE_.wei[LONE_.num]:=PATH_.wei[i_]; |
| 1319 |
fi |
| 1320 |
endfor |
| 1321 |
% build the tree: |
| 1322 |
for i_:=1 upto LONE_.num: add_to_tree(-i_); endfor |
| 1323 |
for i_:=1 upto SPOT_.num: add_to_tree(EDGE_.bar[i_]); endfor |
| 1324 |
for i_:=1 upto LVQ_.num: add_to_tree(LVQ_[i_]); endfor |
| 1325 |
endgroup |
| 1326 |
enddef; |
| 1327 |
% --- |
| 1328 |
def color_paths = % \MF's linear equation solver heavily exploited |
| 1329 |
begingroup |
| 1330 |
save i_,j_; |
| 1331 |
for i_:=1 upto TREE_.num: |
| 1332 |
if TREE_.emb[i_]=0: % outer path |
| 1333 |
if TREE_.pth[i_]<0: LONE_.col[-TREE_.pth[i_]]=background_color; |
| 1334 |
else: EDGE_.col[AREA_[TREE_.pth[i_]]]=background_color; fi |
| 1335 |
else: % inner path, inherits color from the surrounding path |
| 1336 |
j_:=TREE_.emb[i_]; |
| 1337 |
if TREE_.pth[i_]<0: |
| 1338 |
if TREE_.pth[j_]<0: |
| 1339 |
LONE_.col[-TREE_.pth[i_]]=LONE_.col[-TREE_.pth[j_]] |
| 1340 |
+LONE_.wei[-TREE_.pth[j_]]*check_turn(LONE_[-TREE_.pth[j_]]); |
| 1341 |
else: |
| 1342 |
LONE_.col[-TREE_.pth[i_]]=EDGE_.col[AREA_[TREE_.pth[j_]]]; |
| 1343 |
fi |
| 1344 |
else: |
| 1345 |
if TREE_.pth[j_]<0: |
| 1346 |
EDGE_.col[AREA_[TREE_.pth[i_]]]=LONE_.col[-TREE_.pth[j_]] |
| 1347 |
+LONE_.wei[-TREE_.pth[j_]]*check_turn(LONE_[-TREE_.pth[j_]]); |
| 1348 |
else: |
| 1349 |
if AREA_.spt[TREE_.pth[i_]]<>AREA_.spt[TREE_.pth[j_]]: |
| 1350 |
EDGE_.col[AREA_[TREE_.pth[i_]]]=EDGE_.col[AREA_[TREE_.pth[j_]]]; |
| 1351 |
fi |
| 1352 |
fi |
| 1353 |
fi |
| 1354 |
fi |
| 1355 |
endfor; |
| 1356 |
endgroup |
| 1357 |
enddef; |
| 1358 |
% --- |
| 1359 |
def recombine_edges(suffix R) = |
| 1360 |
% this routine can be used several times (after completing the process |
| 1361 |
% of finding the structure of paths after intersecting) with various |
| 1362 |
% definitions of |good_color| function in order to select various |
| 1363 |
% sets of areas |
| 1364 |
if not path R0: numeric R.num; path R[\\]; fi |
| 1365 |
if (unknown R.num) or (unknown append_results): R.num:=0; fi |
| 1366 |
% |R|: resulting data structure, namely, |R.num| is the number of output |
| 1367 |
% paths, |R1|, |R2|, ..., |R[R.num]| are the resulting paths |
| 1368 |
begingroup |
| 1369 |
save i_,j_,out_,in_; |
| 1370 |
for i_:=1 upto LONE_.num: |
| 1371 |
out_:=LONE_.col[i_]; in_:=out_+LONE_.wei[i_]*check_turn(LONE_[i_]); |
| 1372 |
if good_colors(in_,out_) or good_colors(out_,in_): |
| 1373 |
R[incr R.num]:=LONE_[i_]; |
| 1374 |
R[R.num]:=if good_colors(in_,out_): pos_turn else: neg_turn fi \\ R[R.num]; |
| 1375 |
fi |
| 1376 |
endfor |
| 1377 |
for i_:=-EDGE_.num upto EDGE_.num: if i_<>0: |
| 1378 |
EDGE_.aux[i_]:=whatever; |
| 1379 |
fi endfor |
| 1380 |
for i_:=-EDGE_.num upto EDGE_.num: if i_<>0: |
| 1381 |
% BUG TRAP 1: |
| 1382 |
if unknown EDGE_.col[i_]: |
| 1383 |
err_extra_info(i_,whatever); err_helpless; |
| 1384 |
errmessage "RO ERROR: Edge " & decimal(j_) & " not colored"; |
| 1385 |
fi |
| 1386 |
if good_colors(EDGE_.col[i_],EDGE_.col[-i_]) and (unknown EDGE_.aux[i_]): |
| 1387 |
save v_; |
| 1388 |
R.num:=R.num+1; j_:=i_; v_[j_]:=0; |
| 1389 |
EDGE_.aux[j_]:=0; R[R.num]:=the_edge(j_); |
| 1390 |
forever: j_:=EDGE_.out[j_]; exitif (j_=i_) or (known v_.emerg); |
| 1391 |
% BUG TRAP 2: |
| 1392 |
if known v_[j_]: |
| 1393 |
err_extra_info(i_,j_); err_helpless; |
| 1394 |
errmessage "RO ERROR: Edge " & decimal(j_) & " revisited"; |
| 1395 |
v_.emerg:=0; |
| 1396 |
fi |
| 1397 |
v_[j_]:=0; |
| 1398 |
if good_colors(EDGE_.col[j_],EDGE_.col[-j_]): |
| 1399 |
EDGE_.aux[j_]:=0; R[R.num]:=R[R.num] && the_edge(j_); |
| 1400 |
else: j_:=-j_; |
| 1401 |
fi |
| 1402 |
endfor |
| 1403 |
R[R.num]:=clean_path(clean_path(make_cycle(R[R.num]))); |
| 1404 |
fi |
| 1405 |
fi endfor |
| 1406 |
endgroup |
| 1407 |
enddef; |
| 1408 |
% --- |
| 1409 |
def remove_overlap (text P)(text W) suffix R = |
| 1410 |
begingroup interim autorounding:=0; |
| 1411 |
% |P|: list of paths to be processed (non-cyclic paths are ignored); |
| 1412 |
% |W|: list of weights given as pairs: (index, value) |
| 1413 |
% |R|: resulting data structure, i.e., |R.num| is the number of output paths, |
| 1414 |
% |R1|, |R2|, ..., |R[R.num]| are the resulting paths |
| 1415 |
info_ro "initialise_removing_overlaps"; initialise_removing_overlaps; |
| 1416 |
info_ro "prepare_input_data"; prepare_input_data(P)(W); |
| 1417 |
info_ro "intersect_all_paths"; intersect_all_paths; |
| 1418 |
info_ro "find_minimal_secant"; find_minimal_secant; |
| 1419 |
info_ro "build_node_structure"; build_node_structure; |
| 1420 |
info_ro "identify_close_nodes"; identify_close_nodes; |
| 1421 |
info_ro "build_edge_structure"; build_edge_structure; |
| 1422 |
info_ro "find_leftmost_edges"; find_leftmost_edges; |
| 1423 |
info_ro "build_area_structure"; build_area_structure; |
| 1424 |
info_ro "build_spot_structure"; build_spot_structure; |
| 1425 |
info_ro "build_embedding_tree"; build_embedding_tree; |
| 1426 |
info_ro "color_paths"; color_paths; |
| 1427 |
info_ro "recombine_edges"; recombine_edges(R); |
| 1428 |
endgroup |
| 1429 |
enddef; |
| 1430 |
% --- |
| 1431 |
% E-S MACROS: |
| 1432 |
% --- |
| 1433 |
vardef make_join@#(expr pa,pb)= |
| 1434 |
save kind_; string kind_; kind_:=str @#; if kind_="": kind_:="0" fi; |
| 1435 |
if (kind_<>"0") and (kind_<>"1"): |
| 1436 |
errhelp "Will use default."; |
| 1437 |
errmessage "ES ERROR: don't know how to join"; |
| 1438 |
kind_:="0"; |
| 1439 |
fi |
| 1440 |
if distance(point length(pa) of pa,point 0 of pb)<epsil.dist: |
| 1441 |
if (point length(pa) of pa)<>(point 0 of pb): |
| 1442 |
info_es "Points " & dec_pair(point length(pa) of pa) & |
| 1443 |
" and " & dec_pair(point 0 of pb) & " joined"; |
| 1444 |
if (tracingexpanding>0) and (proofing>0): |
| 1445 |
makelabel.lft.nodot("joined",point length(pa) of pa); |
| 1446 |
fi |
| 1447 |
fi |
| 1448 |
pa && pb |
| 1449 |
elseif kind_="0": |
| 1450 |
if miter_size<=0: % a special case, isn't it? |
| 1451 |
pa--pb |
| 1452 |
else: |
| 1453 |
save ta_,tb_,za_,da_,zb_,db_,zc_,zd_,ze_,zf_; |
| 1454 |
pair za_,da_,zb_,db_,zc_,zd_,ze_,zf_; |
| 1455 |
za_=point length(pa) of pa; |
| 1456 |
da_=direction length(pa) of pa; |
| 1457 |
zb_=point 0 of pb; |
| 1458 |
db_=direction 0 of pb; |
| 1459 |
zc_=whatever[za_,za_+da_]=whatever[ze_,ze_+(zb_-za_)]; |
| 1460 |
zd_=whatever[zb_,zb_+db_]=whatever[ze_,ze_+(zb_-za_)]; |
| 1461 |
ze_=.5[za_,zb_]+miter_size*(unitvector(da_-db_)); |
| 1462 |
% we used to check |turningnumber(za_--zc_--zd_--zb_--cycle)|, but it was |
| 1463 |
% not sufficiently robust |
| 1464 |
(ta_,tb_)=(za_--zc_) intersectiontimes (zd_--zb_); |
| 1465 |
if ta_<0: % |miter_size| in force: |
| 1466 |
pa |
| 1467 |
if distance(point length(pa) of pa,zc_)>=epsil.dist: --zc_ fi |
| 1468 |
if (distance(zc_,zd_)>=epsil.dist) |
| 1469 |
and (distance(point 0 of pb,zd_)>=epsil.dist): --zd_ fi |
| 1470 |
--pb |
| 1471 |
else: |
| 1472 |
zf_:=point ta_ of (za_--zc_); |
| 1473 |
if abs(zf_-.5[za_,zb_])>abs(ze_-.5[za_,zb_]): % |miter_size| in force: |
| 1474 |
pa |
| 1475 |
if distance(point length(pa) of pa,zc_)>=epsil.dist: --zc_ fi |
| 1476 |
if (distance(zc_,zd_)>=epsil.dist) |
| 1477 |
and (distance(point 0 of pb,zd_)>=epsil.dist): --zd_ fi |
| 1478 |
--pb |
| 1479 |
else: |
| 1480 |
pa |
| 1481 |
if (distance(point length(pa) of pa,zf_)>=epsil.dist) |
| 1482 |
and (distance(point 0 of pb,zf_)>=epsil.dist): --zf_ fi |
| 1483 |
--pb |
| 1484 |
fi |
| 1485 |
fi |
| 1486 |
fi |
| 1487 |
elseif kind_="1": |
| 1488 |
pa{direction length(pa) of pa}..{direction 0 of pb}pb |
| 1489 |
fi |
| 1490 |
enddef; |
| 1491 |
% --- |
| 1492 |
vardef make_cyclic_join@#(expr p)= |
| 1493 |
save kind_; string kind_; kind_:=str @#; if kind_="": kind_:="0" fi; |
| 1494 |
if (kind_<>"0") and (kind_<>"1"): |
| 1495 |
errhelp "Will use default."; |
| 1496 |
errmessage "ES ERROR: don't know how to join"; |
| 1497 |
kind_:="0"; |
| 1498 |
fi |
| 1499 |
if distance(point length(p) of p,point 0 of p)<epsil.dist: |
| 1500 |
if (point length(p) of p)<>(point 0 of p): |
| 1501 |
info_es "Points " & dec_pair(point length(p) of p) & |
| 1502 |
" and " & dec_pair(point 0 of p) & " joined (cycle)"; |
| 1503 |
if (tracingexpanding>0) and (proofing>0): |
| 1504 |
makelabel.lft.nodot("joined (cycle)",point length(p) of p); |
| 1505 |
fi |
| 1506 |
fi |
| 1507 |
make_cycle(p) |
| 1508 |
elseif kind_="0": |
| 1509 |
if miter_size<=0: % a special case, isn't it? |
| 1510 |
p--cycle |
| 1511 |
else: |
| 1512 |
save ta_,tb_,za_,da_,zb_,db_,zc_,zd_,ze_,zf_; pair za_,da_,zb_,db_,zc_,zd_,ze_,zf_; |
| 1513 |
za_=point length(p) of p; da_=direction length(p) of p; |
| 1514 |
zb_=point 0 of p; db_=direction 0 of p; |
| 1515 |
zc_=whatever[za_,za_+da_]=whatever[ze_,ze_+(zb_-za_)]; |
| 1516 |
zd_=whatever[zb_,zb_+db_]=whatever[ze_,ze_+(zb_-za_)]; |
| 1517 |
ze_=.5[za_,zb_]+miter_size*(unitvector(da_-db_)); |
| 1518 |
% we used to check |turningnumber(za_--zc_--zd_--zb_--cycle)|, but it was |
| 1519 |
% not sufficiently robust |
| 1520 |
(ta_,tb_)=(za_--zc_) intersectiontimes (zd_--zb_); |
| 1521 |
if ta_<0: % |miter_size| in force: |
| 1522 |
p |
| 1523 |
if distance(point length(p) of p,zc_)>=epsil.dist: --zc_ fi |
| 1524 |
if (distance(zc_,zd_)>=epsil.dist) |
| 1525 |
and (length((point 0 of p)-zd_)>=epsil.dist): --zd_ fi |
| 1526 |
--cycle |
| 1527 |
else: |
| 1528 |
zf_:=point ta_ of (za_--zc_); |
| 1529 |
if abs(zf_-.5[za_,zb_])>abs(ze_-.5[za_,zb_]): % |miter_size| in force: |
| 1530 |
p |
| 1531 |
if distance(point length(p) of p,zc_)>=epsil.dist: --zc_ fi |
| 1532 |
if (distance(zc_,zd_)>=epsil.dist) |
| 1533 |
and (distance(point 0 of p,zd_)>=epsil.dist): --zd_ fi |
| 1534 |
--cycle |
| 1535 |
else: |
| 1536 |
p |
| 1537 |
if (distance(point length(p) of p,zf_)>=epsil.dist) |
| 1538 |
and (distance(point 0 of p,zf_)>=epsil.dist): --zf_ fi |
| 1539 |
--cycle |
| 1540 |
fi |
| 1541 |
fi |
| 1542 |
fi |
| 1543 |
elseif kind_="1": |
| 1544 |
p{direction length(p) of p}..{direction 0 of p}cycle |
| 1545 |
fi |
| 1546 |
enddef; |
| 1547 |
% --- |
| 1548 |
vardef make_end@#(expr pr,pl) = |
| 1549 |
save kind_; string kind_; kind_:=str @#; if kind_="": kind_:="0" fi; |
| 1550 |
if (kind_<>"0") and (kind_<>"1"): |
| 1551 |
errhelp "Will use default."; |
| 1552 |
errmessage "ES ERROR: don't know how to end"; |
| 1553 |
kind_:="0"; |
| 1554 |
fi |
| 1555 |
if kind_="0": pr--pl--cycle |
| 1556 |
elseif kind_="1": |
| 1557 |
save za_,zb_; pair za_,zb_; |
| 1558 |
za_=1/2[point length(pr) of pr,point 0 of pl] |
| 1559 |
+(1/2((point length(pr) of pr)-(point 0 of pl)) rotated 90); |
| 1560 |
zb_=1/2[point length(pl) of pl,point length 0 of pr] |
| 1561 |
+(1/2((point length(pl) of pl)-(point 0 of pr)) rotated 90); |
| 1562 |
pr{direction length(pr) of pr}..za_..{direction 0 of pl}pl |
| 1563 |
{direction length(pl) of pl}..zb_..{direction 0 of pr}cycle |
| 1564 |
fi |
| 1565 |
enddef; |
| 1566 |
% --- |
| 1567 |
vardef opt_tensions(expr p,b) = |
| 1568 |
% for a given B\'ezier segment |p| and a distance |b|, an optimal pair of |
| 1569 |
% `tensions' $(\alpha,\beta)$ is found using least square method such that |
| 1570 |
% |bez_edge|$(p,b,\alpha,\beta)$ (see below) approximates the edge of |
| 1571 |
% a circular pen of diameter |b| traversing |p| (more on the employed |
| 1572 |
% method be found in the article of B. Jackowski and M. Ry\'cko: |
| 1573 |
% ``Labyrinth of \MF paths in outline,'' proceedings of the 8th European |
| 1574 |
% \TeX Conference, Sept. 26--30, 1994, Gda\'nsk, Poland) |
| 1575 |
% |
| 1576 |
save alpha_,beta_,gx_,gy_,n_,t_,ta_,tb_,tc_,td_,u_,v_,nu_,nv_,x_,y_; |
| 1577 |
numeric alpha_,beta_,n_,ta_,tb_,tc_,td_, |
| 1578 |
gx_[\\],gy_[\\],gx_.alpha[\\],gy_.alpha[\\],gx_.beta[\\],gy_.beta[\\], |
| 1579 |
u_.x,u_.y,v_.x,v_.y,nu_.x,nu_.y,nv_.x,nv_.y, |
| 1580 |
x_[\\],y_[\\]; |
| 1581 |
n_:=5; % perhaps for |n_|$=\infty$ algebraic formulas can be derived, but... |
| 1582 |
(u_.x,u_.y)=(postcontrol 0 of p)-(point 0 of p); |
| 1583 |
(v_.x,v_.y)=(precontrol 1 of p)-(point 1 of p); |
| 1584 |
(nu_.x,nu_.y)=unitvector(u_.x,u_.y); (nv_.x,nv_.y)=unitvector(v_.x,v_.y); |
| 1585 |
for t_:=0 upto n_: |
| 1586 |
(x_[t_],y_[t_])=(point t_/n_ of p)+b*((udir t_/n_ of p) rotated -90); |
| 1587 |
endfor |
| 1588 |
for t_:=1 upto n_-1: |
| 1589 |
td_:=t_/n_; ta_:=1-td_; tb_:=3ta_*ta_*td_; tc_:=3ta_*td_*td_; |
| 1590 |
ta_:=ta_*ta_*ta_; td_:=td_*td_*td_; |
| 1591 |
gx_[t_]=ta_*x_0+tb_*(x_0+alpha_*u_.x)+tc_*(x_[n_]+beta_*v_.x)+td_*x_[n_]; |
| 1592 |
gx_.alpha[t_]=tb_*nu_.x; gx_.beta[t_]=tc_*nv_.x; |
| 1593 |
gy_[t_]=ta_*y_0+tb_*(y_0+alpha_*u_.y)+tc_*(y_[n_]+beta_*v_.y)+td_*y_[n_]; |
| 1594 |
gy_.alpha[t_]=tb_*nu_.y; gy_.beta[t_]=tc_*nv_.y; |
| 1595 |
endfor |
| 1596 |
0=0 for t_:=1 upto n_-1: |
| 1597 |
+((gx_[t_]-x_[t_])*gx_.alpha[t_]+(gy_[t_]-y_[t_])*gy_.alpha[t_])/n_ |
| 1598 |
endfor; |
| 1599 |
0=0 for t_:=1 upto n_-1: |
| 1600 |
+((gx_[t_]-x_[t_])*gx_.beta[t_]+(gy_[t_]-y_[t_])*gy_.beta[t_])/n_ |
| 1601 |
endfor; |
| 1602 |
%| (u_.x,u_.y)=(postcontrol 0 of p)-(point 0 of p);| |
| 1603 |
%| (v_.x,v_.y)=(precontrol 1 of p)-(point 1 of p);| |
| 1604 |
%| ta_:=1/4length((u_.x,u_.y))+1/4length((v_.x,v_.y))| |
| 1605 |
%| +1/4length((postcontrol 0 of p)-(precontrol 1 of p))| |
| 1606 |
%| +1/4length((point 0 of p)-(point 1 of p));| |
| 1607 |
%| message "accuracy=" & decimal| |
| 1608 |
%| (0+for t_:=1 upto n_-1:+(((gx_[t_]-x_[t_])++(gy_[t_]-y_[t_]))/ta_)/n_| |
| 1609 |
%| endfor);| |
| 1610 |
%| message " alpha=" & decimal(alpha_) & " beta=" & decimal(beta_);| |
| 1611 |
%| for t_:=0 upto n_: fill fullcircle scaled 3 shifted (x_[t_],y_[t_]); endfor| |
| 1612 |
%| for t_:=1 upto n_-1: makelabel("g" & decimal(t_),(gx_[t_],gy_[t_])); endfor| |
| 1613 |
(alpha_,beta_) |
| 1614 |
enddef; |
| 1615 |
% --- |
| 1616 |
vardef bez_edge(expr p,b,uv) = |
| 1617 |
save za_,zb_,u_,v_; pair za_,zb_; u_:=xpart(uv); v_:=ypart(uv); |
| 1618 |
za_=b*((udir 0 of p) rotated -90); zb_=b*((udir 1 of p) rotated -90); |
| 1619 |
((point 0 of p)+za_) .. controls (u_[point 0 of p,postcontrol 0 of p]+za_) |
| 1620 |
and (v_[point 1 of p,precontrol 1 of p]+zb_) .. ((point 1 of p)+zb_) |
| 1621 |
enddef; |
| 1622 |
% --- |
| 1623 |
def remove_global_loops(suffix E) = |
| 1624 |
begingroup |
| 1625 |
% warning: we don't trust too much in the results of ex. 14.17 from |
| 1626 |
% The \MF{}book, hence a ``par force'' approach; there still exist |
| 1627 |
% weird cases (e.g., local loops) which remain unsolved, but in practice |
| 1628 |
% the following algorithm should suffice: |
| 1629 |
save opt_,ta_,tb_; pair opt_; |
| 1630 |
opt_:=(0,length(E)); |
| 1631 |
for i_:=0 upto length(E)-1: |
| 1632 |
for j_:=i_+2 upto length(E)-1: |
| 1633 |
numeric ta_,tb_; |
| 1634 |
(ta_,tb_)=(subpath (i_,i_+1) of E) |
| 1635 |
intersectiontimes (subpath (j_,j_+1) of E); |
| 1636 |
if (ta_>0) and ((ta_+i_)>xpart(opt_)) and ((tb_+j_)<ypart(opt_)): |
| 1637 |
opt_:=(ta_+i_,tb_+j_); |
| 1638 |
fi |
| 1639 |
endfor |
| 1640 |
endfor |
| 1641 |
if xpart(opt_)>0: |
| 1642 |
E:=make_cycle(subpath(xpart(opt_),ypart(opt_)) of E); |
| 1643 |
fi |
| 1644 |
endgroup |
| 1645 |
enddef; |
| 1646 |
% --- |
| 1647 |
vardef make_edge@#(expr p,b)= |
| 1648 |
save E_,e_,ta_,tb_,tc_,td_; path E_,e_[\\]; |
| 1649 |
for i_:=0 upto length(p)-1: |
| 1650 |
E_:=subpath (i_,i_+1) of p; e_[i_]=bez_edge(E_,b,opt_tensions(E_,b)); |
| 1651 |
endfor |
| 1652 |
E_:=e_0; |
| 1653 |
for i_:=1 upto length(p)-1: |
| 1654 |
if (length(p)=2) and (cycle p): % this is a peculiar case, indeed! |
| 1655 |
numeric ta_,tb_,tc_,td_; |
| 1656 |
(ta_,tb_)=E_ intersectiontimes e_[i_]; |
| 1657 |
(1-tc_,1-td_)=reverse(E_) intersectiontimes reverse(e_[i_]); |
| 1658 |
if ta_>=0: |
| 1659 |
E_:=(subpath(min(ta_,tc_),max(ta_,tc_)) of E_) |
| 1660 |
&& (subpath(min(tb_,td_),max(tb_,td_)) of e_[i_]); |
| 1661 |
else: E_:=make_join@#(E_,e_[i_]); |
| 1662 |
fi |
| 1663 |
else: |
| 1664 |
numeric ta_,tb_; |
| 1665 |
(ta_,tb_)=(subpath(length(E_)-1,length(E_)) of E_) |
| 1666 |
intersectiontimes e_[i_]; |
| 1667 |
if ta_>=0: |
| 1668 |
E_:=(subpath (0,length(E_)-1+ta_) of E_) |
| 1669 |
&& (subpath(tb_,1) of e_[i_]); |
| 1670 |
else: E_:=make_join@#(E_,e_[i_]); fi |
| 1671 |
fi |
| 1672 |
endfor |
| 1673 |
if cycle p: |
| 1674 |
remove_global_loops(E_); |
| 1675 |
if not (cycle E_): E_:=make_cyclic_join@#(E_); fi |
| 1676 |
fi |
| 1677 |
E_ |
| 1678 |
enddef; |
| 1679 |
% --- |
| 1680 |
def expand_stroke(text P)(expr b) suffix R = |
| 1681 |
begingroup interim autorounding:=0; |
| 1682 |
numeric PATH_.num; path PATH_[\\]; |
| 1683 |
PATH_.num:=0; for P_:=P: PATH_[incr PATH_.num]:=touch_path(P_); endfor |
| 1684 |
if not path R0: numeric R.num; path R[\\]; fi |
| 1685 |
if (unknown R.num) or (unknown append_results): R.num:=0; fi |
| 1686 |
if unknown join_kind: save join_kind; join_kind=0; fi |
| 1687 |
if unknown end_kind: save end_kind; end_kind=0; fi |
| 1688 |
for i_:=1 upto PATH_.num: |
| 1689 |
if not cycle PATH_[i_]: |
| 1690 |
R[incr R.num]:=make_end[end_kind] |
| 1691 |
(make_edge[join_kind](PATH_[i_],b), |
| 1692 |
reverse make_edge[join_kind](PATH_[i_],-b)); |
| 1693 |
else: |
| 1694 |
R[incr R.num]:=make_edge[join_kind](PATH_[i_],b); |
| 1695 |
R[incr R.num]:=reverse make_edge[join_kind](PATH_[i_],-b); |
| 1696 |
fi |
| 1697 |
endfor |
| 1698 |
for i_:=1 upto R.num: R[i_]:=clean_path(clean_path(R[i_])); endfor |
| 1699 |
endgroup |
| 1700 |
enddef; |
| 1701 |
% --- |
| 1702 |
def change_weight(text P)(expr b) suffix R = |
| 1703 |
begingroup interim autorounding:=0; |
| 1704 |
numeric PATH_.num; path PATH_[\\]; |
| 1705 |
PATH_.num:=0; for P_:=P: PATH_[incr PATH_.num]:=touch_path(P_); endfor |
| 1706 |
if not path R0: numeric R.num; path R[\\]; fi |
| 1707 |
if (unknown R.num) or (unknown append_results): R.num:=0; fi |
| 1708 |
if unknown join_kind: save join_kind; join_kind=0; fi |
| 1709 |
for i_:=1 upto PATH_.num: |
| 1710 |
% non-cyclic paths are ignored |
| 1711 |
if cycle PATH_[i_]: R[incr R.num]:=make_edge[join_kind](PATH_[i_],b); fi |
| 1712 |
endfor |
| 1713 |
endgroup |
| 1714 |
enddef; |
| 1715 |
% --- |
| 1716 |
def info_ro expr s = if tracingremoving>0: message s; message ""; fi enddef; |
| 1717 |
def info_es expr s = if tracingexpanding>0: message s; message ""; fi enddef; |
| 1718 |
% --- |
| 1719 |
% DEFAULTS: |
| 1720 |
% --- |
| 1721 |
def roex_default text t = |
| 1722 |
forsuffixes S_:=t: |
| 1723 |
if str S_ = "good_colors": |
| 1724 |
% the formula |good_colors(p,q) and good_colors(q,p)| must be |false|! |
| 1725 |
vardef good_colors(expr i,o) = ((i>=1) and (o<=0)) enddef; |
| 1726 |
elseif str S_ = "touch_path": |
| 1727 |
vardef touch_path(expr p) = p enddef; |
| 1728 |
elseif str S_ = "background_color": background_color:=0; |
| 1729 |
elseif str S_ = "miter_size": |
| 1730 |
miter_size:=10pixels_per_inch/72; % i.e., 10bp |
| 1731 |
% incidentally, |10bp| would convert to |10.00002| during export at |300dpi| |
| 1732 |
elseif str S_ = "epsil.ang": epsil.ang:=1/10; % in degrees |
| 1733 |
elseif str S_ = "epsil.dist": epsil.dist:=1/10pt; % ca |2/5|pxl at |300dpi| |
| 1734 |
elseif str S_ = "epsil.time": epsil.time:=1/100; |
| 1735 |
elseif str S_ = "epsil.len": epsil.len:=1/1000; % used in |turn_ang| |
| 1736 |
elseif str S_ = "max_idx": max_idx:=125; |
| 1737 |
elseif str S_ = "enc": |
| 1738 |
% |enc| is a prefix of a data structure used in checking tangent |
| 1739 |
% points and searching for the leftmost edge; |enc.pth| is in both |
| 1740 |
% cases scaled differently |
| 1741 |
vardef enc.pth = fullcircle enddef; enc.len:=length(enc.pth); |
| 1742 |
fi |
| 1743 |
endfor |
| 1744 |
enddef; |
| 1745 |
% |
| 1746 |
roex_default good_colors, touch_path, background_color, miter_size, |
| 1747 |
epsil.ang, epsil.dist, epsil.time, epsil.len, max_idx, enc; |
| 1748 |
% --- |
| 1749 |
numeric append_results; % initially unknown |
| 1750 |
newinternal tracingleftmost; tracingleftmost:=0; |
| 1751 |
newinternal tracingremoving; tracingremoving:=0; |
| 1752 |
newinternal tracingexpanding; tracingexpanding:=0; |
| 1753 |
% --- |
| 1754 |
endinput |
| 1755 |
%%\end |
| 1756 |
|