summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/blockdraw_mp/bondgraph.mp
blob: ba5d861d14dcae360cb135184b412ff273870611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
%% bondgraph.mp
%% Copyright 2007 Henrik Tidefelt
% 
% This work may be distributed and/or modified under the 
% conditions of the LaTeX Project Public License, either version 1.3 
% of this license or (at your option) any later version.
% The latest version of this license is in 
% http://www.latex-project.org/lppl.txt 
% and version 1.3 or later is part of all distributions of LaTeX 
% version 2005/12/01 or later.
% 
% This work has the LPPL maintenance status `maintained´.
% 
% The Current Maintainer of this work is Henrik Tidefelt,
% tidefelt@isy.liu.se.
% 
% This work, referred to as blockdraw.mp, consists of the files
%   shiftoff.mp
%   blockdraw.mp
%   bondgraph.mp
%   blockdraw_mp.pdf
%
%
% === Notes regarding the backward incompatible changes of 2007-01-21. ===
%
% As it was discovered that MetaPost uses the verbatimtex construct only in
% the file it appears in, it was concluded that package files cannot contain
% any content that depends on the LaTeX preamble.  In other words, any
% btex ... etex constructs must be moved from package files to the application
% sources from where the package files are included.
%
% With the definition of, say, p-junction blocks in the application source,
% it is easy to change notation from the letters "p" and "s" to the numbers
% "0" and "1".
%
% The obvious drawback, beside backward incompatibility, is that this will
% cause a lot of code replication, making bond graph source files
% less concise, and require multiple file search-and-replace actions when
% changing junction notation for a collection of graphs.  However, since
% MetaPost only allows literal character strings in the btex ... etex
% construct, these inconveniences seem impossible to work around.  Ideas,
% anyone?

input blockdraw;
implicitdraw := false;

boolean junctionimplicitdraw;
junctionimplicitdraw := true;

boolean useopenbonds;
useopenbonds := false;

connectionlw := 0.5pt;
ahlength := 3mm;
bboxmargin := 1.5mm;

def withbbmargin(expr pic ) =
  begingroup
    save tmppic;
    picture tmppic;
    tmppic = pic;
    setbounds tmppic to bbox pic;
    tmppic
  endgroup
enddef;

def junction(expr txt, z ) =
  begingroup
    save tmppic;
    picture tmppic;
    tmppic := nullpicture;
    addto tmppic also shiftoff( txt scaled textscale, to_center );
    setbounds tmppic to (-smallblockr,-smallblockr)--(-smallblockr,smallblockr)--(smallblockr,smallblockr)--(smallblockr,-smallblockr)--cycle;
    tmppic := tmppic shifted z;
    if junctionimplicitdraw:
      draw tmppic;
    fi
    tmppic
  endgroup
enddef;


def junctionlbl(expr txt, arrowdir, lbl, z, arrow ) =
  begingroup
    save tmppic, ahlength, p, d;
    ahlength := 1mm;
    path p;
    numeric d;
    picture tmppic;
    tmppic := nullpicture;
    addto tmppic also shiftoff( txt scaled textscale, to_center );

    if arrowdir = to_top:
      p := ((lrcorner tmppic)--(urcorner tmppic)) shifted ( z + (connectionlw + 2*ahlength*sind(0.5*ahangle),0) );
      d = to_rt;
    elseif arrowdir = to_lft:
      p := ((urcorner tmppic)--(ulcorner tmppic)) shifted ( z + (0,connectionlw + 2*ahlength*sind(0.5*ahangle)) );
      d = to_top;
    elseif arrowdir = to_bot:
      p := ((urcorner tmppic)--(lrcorner tmppic)) shifted ( z + (connectionlw + 2*ahlength*sind(0.5*ahangle),0) );
      d = to_rt;
    elseif arrowdir = to_rt:
      p := ((ulcorner tmppic)--(urcorner tmppic)) shifted ( z + (0,connectionlw + 2*ahlength*sind(0.5*ahangle)) );
      d = to_top;
    fi

    setbounds tmppic to (-smallblockr,-smallblockr)--(-smallblockr,smallblockr)--(smallblockr,smallblockr)--(smallblockr,-smallblockr)--cycle;
    tmppic := tmppic shifted z;

    if junctionimplicitdraw:
      if arrow:
	drawarrow p withpen pencircle scaled connectionlw;
      fi
      draw conlabel( d, lbl, mspoint( p, 0.5, 0 ) );
      draw tmppic;
    fi
    tmppic
  endgroup
enddef;

def terminal(expr d, txt, z ) =
  begingroup
    save tmppic;
    picture tmppic;
    tmppic := shiftoff( withbbmargin( txt scaled textscale ), d ) shifted ( z - smallblockr * dir_to( d ) );
    if junctionimplicitdraw:
      draw tmppic;
    fi
    tmppic
  endgroup  
enddef;

def ignorepicture(expr pic ) =
  show 0
enddef;

def terminalto(expr j, txt, z ) =
  begingroup
    ignorepicture( terminal( to_dir( z - (center j) ), txt, z ) );
    bgconnect( pointpicture( z ) , j )
  endgroup
enddef;

def terminalfr(expr j, txt, z ) =
  begingroup
    ignorepicture( terminal( to_dir( z - (center j) ), txt, z ) );
    bgconnect( j, pointpicture( z ) )
  endgroup
enddef;

def bgconnect(expr pica, picb ) =
  begingroup
    save d;
    pair d;
    if (center pica) = (center picb):
      d := (1,0);
    else:
      d := unitvector( (center picb) - (center pica) );
    fi
    ((center pica)+d*smallblockr)--((center picb)-d*smallblockr)
  endgroup
enddef;

def bond(expr p ) =
  begingroup
    save t;
    numeric t;
    t := arctime arclength(p) - ahlength of p;
    save z;
    pair z;
    z = (point t of p) + dir(angle(direction t of p)+90) * ahlength*sind(0.5*ahangle);
    save res;
    path res;
    if useopenbonds:
      draw p--z withpen pencircle scaled connectionlw
    else:
      draw p withpen pencircle scaled connectionlw;
      res = (subpath (t,infinity) of p)--z--cycle;
      filldraw res withpen pencircle scaled connectionlw
    fi
  endgroup
enddef;

def causalmark(expr p, where ) =
  begingroup
    save d;
    numeric d;
    d = angle( direction where of p );
    if where = 0:
      d := d + 180;
    fi
    save z;
    pair z;
    z = ( point where of p ) + dir(d) * connectionlw;
    save res;
    path res;
    res = (z+dir(d+90)*ahlength*sind(0.5*ahangle))--(z+dir(d-90)*ahlength*sind(0.5*ahangle));
    draw res withpen pencircle scaled (2*connectionlw)
  endgroup
enddef;

def hbond(expr p ) =
  begingroup
    bond( p );
    causalmark( p, infinity )
  endgroup
enddef;

def tbond(expr p ) =
  begingroup
    bond( p );
    causalmark( p, 0 )
  endgroup
enddef;

def flowlabel(expr p, txt ) =
%  the midpoint has typically singular direction, so we can't do the obvious:
%  draw( conlabel( to_dir( dir( angle( direction (0.5*length(p)) of p ) - 90 ) ), txt, mspoint( p, 0.5, 0 ) ) )
  draw conlabel( to_dir( dir( angle( (point infinity of p) - (point 0 of p) ) - 90 ) ), txt, mspoint( p, 0.5, 0 ) ) withpen pencircle scaled connectionlw
enddef;

def effortlabel(expr p, txt ) =
%  the midpoint has typically singular direction, so we can't do the obvious:
%  draw( conlabel( to_dir( dir( angle( direction (0.5*length(p)) of p ) + 90 ) ), txt, mspoint( p, 0.5, 0 ) ) )
  draw conlabel( to_dir( dir( angle( (point infinity of p) - (point 0 of p) ) + 90 ) ), txt, mspoint( p, 0.5, 0 ) ) withpen pencircle scaled connectionlw
enddef;