-
Notifications
You must be signed in to change notification settings - Fork 0
/
causet_saveas_tikz.m
240 lines (238 loc) · 12.2 KB
/
causet_saveas_tikz.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
function causet_saveas_tikz( coordinates, L, filename, ...
flags, drawingstyles, linkselectors, dimensions, fadingdepth )
%CAUSET_SAVEAS_TIKZ converts causet COORDINATES and links L to a TikZ
% picture file where only the first two (or three) coordinates are used.
%
% Arguments:
% COORDINATES positions of the points.
% L logical upper triangular (direct) link matrix.
% FILENAME location and filename.
%
% Optional arguments:
% FLAGS structure of flags.
% 'unit' factor to convert from numeric coordinates to cm.
% If it is a non-positive number, it will be assumed
% that parameters are set externally.
% Default: 1
% 'IsSubPicture' boolean flag to skip the \begin and \end macros of
% the tikzpicture environment.
% Default: false
% 'StartupCommands' text to be printed right after the start of the
% tikzpicture.
% Default: (none)
% 'EndCommands' text to be printed right before the end of the
% tikzpicture.
% Default: (none)
% DRAWINGSTYLES cell vector with length of the causet cardinality.
% Every cell is a vector of drawing style indices
% (starting from 1). If the input is a numeric vector,
% it is converted to a cell vector automatically.
% Default: all events, style 1
% LINKSELECTORS cell array of logical event selection vectors for
% differently coloured links.
% Default: all events, style 1
% DIMENSIONS vector, which sets the dimensions to be printed.
% Default: [ 2, 1 ]
% FADINGRANGE sets the coordinate-range for fading in 3D graphics.
%
% Copyright 2021, C. Minz. BSD 3-Clause License.
N = size( L, 1 );
if ( nargin < 4 ) || isempty( flags )
flags = struct( 'unit', 1 );
end
if ~isfield( flags, 'IsSubPicture' )
flags.IsSubPicture = false;
end
if nargin < 5
drawingstyles = ones( 1, N );
end
if ~iscell( drawingstyles )
drawingstyles = num2cell( drawingstyles );
end
if nargin < 6
linkselectors = { true( 1, N ) };
end
if nargin < 7
dimensions = [ 2, 1 ];
end
is3D = false;
if length( dimensions ) > 2
is3D = true;
% third dimension offset for planar coordinates:
offsetscaling3D = [ 0.3, 0.2 ];
% foreground-most and background-most fading:
fading3D = [ 1, 0 ];
scalings3D = coordinates( :, dimensions( 3 ) );
scalings3D = scalings3D - min( coordinates( :, dimensions( 3 ) ) );
scalings3D = 1 - scalings3D / max( scalings3D );
if nargin < 8
fadingdepth = 1;
end
ignore_faded = fadingdepth == 1;
end
linkcount = sum( sum( L ) );
pt_digits = floor( log10( N ) ) + 1;
pt_nameformat = [ '%0', num2str( pt_digits ), 'u' ];
% start picture:
fileID = fopen( filename, 'w' );
if ~flags.IsSubPicture
fprintf( fileID, '\\begin{tikzpicture}\n' );
end
fprintf( fileID, '\t%% ### This graphic is generated by causet_saveas_tikz.m\n' );
fprintf( fileID, '\t%% ### Number of elements: %d\n', N );
fprintf( fileID, '\t%% ### Number of links: %d\n', linkcount );
fprintf( fileID, '\t%% ### Average link density: %0.3f\n', linkcount / N );
if isfield( flags, 'StartupCommands' )
fprintf( fileID, '\t%% start-up commands:\n' );
fprintf( fileID, flags.StartupCommands );
end
fprintf( fileID, '\t%% parameters:\n' );
fprintf( fileID, '\t\\providecommand{\\firstHiddenEvent}{%d}\n', N + 1 );
if isfield( flags, 'unit') && ( flags.unit > 0 )
fprintf( fileID, '\t\\def\\CSunit{%0.3fcm}\n', flags.unit );
fprintf( fileID, '\t\\def\\CScoordinatesystem{%0.3f}\n', 1 );
fprintf( fileID, '\t\\def\\CScoordinatesystemshift{%0.3f}\n', -0.2 );
if is3D
fprintf( fileID, '\t\\def\\CSminscaling{%0.3f}\n', 0.3 );
fprintf( fileID, '\t\\def\\CSlblscaling{%0.3f}\n', 1 );
fprintf( fileID, '\t\\def\\CSlinkopacity{%0.3f}\n', 0.8 );
fprintf( fileID, '\t\\def\\CSmaxopacity{%0.3f}\n', fading3D( 1 ) );
fprintf( fileID, '\t\\def\\CSlblopacityfill{%0.3f}\n', 0.8 );
fprintf( fileID, '\t\\def\\CSlblopacitytext{%0.3f}\n', 1 );
fprintf( fileID, '\t\\def\\CSfading{%0.3f}\n', ( fading3D( 1 ) - fading3D( 2 ) ) / fadingdepth );
end
else
fprintf( fileID, '\t%% -- expected global definitions of:\n' );
if is3D
fprintf( fileID, '\t%% \\CSunit, \\CSminscaling, \\CSlblscaling, ' );
fprintf( fileID, '\\CSlinkopacity, \\CSmaxopacity, \\CSlblopacityfill, ' );
fprintf( fileID, '\\CSlblopacitytext, \\CSfading\n' );
else
fprintf( fileID, '\t%% \\CSunit\n' );
end
end
% define causet coordinate format:
pt_coordformat = [ '\t\\coordinate (p', pt_nameformat, ') at ', ...
'( %0.3f*\\CSunit, %0.3f*\\CSunit );\n' ];
% draw coordinate axes:
cs_xaxisformat = [ '\t\t\\draw[%s] ( \\CScoordinatesystemshift*\\CSunit, \\CScoordinatesystemshift*\\CSunit ) -- ', ...
'( %0.3f*\\CSunit-\\CScoordinatesystemshift*\\CSunit, \\CScoordinatesystemshift*\\CSunit ) ', ...
'node[%slbl, below] {$x_{%u}$};\n' ];
cs_yaxisformat = [ '\t\t\\draw[%s] ( \\CScoordinatesystemshift*\\CSunit, \\CScoordinatesystemshift*\\CSunit ) -- ', ...
'( \\CScoordinatesystemshift*\\CSunit, %0.3f*\\CSunit-\\CScoordinatesystemshift*\\CSunit ) ', ...
'node[%slbl, left] {$x_{%u}$};\n' ];
cs_zaxisformat = [ '\t\t\\draw[%s] ( \\CScoordinatesystemshift*\\CSunit, \\CScoordinatesystemshift*\\CSunit ) -- ', ...
'( %0.3f*\\CSunit+%0.3f*\\CScoordinatesystemshift*\\CSunit, %0.3f*\\CSunit+%0.3f*\\CScoordinatesystemshift*\\CSunit ) ', ...
'node[%slbl, above left] {$x_{%u}$};\n' ];
fprintf( fileID, '\t%% draw coordinate system:\n' );
fprintf( fileID, '\t\\ifdrawcoordinateaxes\n' );
if is3D
axismax = max( coordinates( :, dimensions( 3 ) ) );
fprintf( fileID, cs_zaxisformat, 'axis', axismax * offsetscaling3D( 1 ), 1 + offsetscaling3D( 1 ), ...
axismax * offsetscaling3D( 2 ), 1 + offsetscaling3D( 2 ), 'axis', dimensions( 3 ) - 1 );
end
axismax = max( coordinates( :, dimensions( 1 ) ) );
fprintf( fileID, cs_xaxisformat, 'axis', axismax, 'axis', dimensions( 1 ) - 1 );
axismax = max( coordinates( :, dimensions( 2 ) ) );
fprintf( fileID, cs_yaxisformat, 'axis', axismax, 'axis', dimensions( 2 ) - 1 );
fprintf( fileID, '\t\\fi\n' );
fprintf( fileID, '\t\\ifdrawcoordinatemap\n' );
if is3D
fprintf( fileID, cs_zaxisformat, 'axesmap', offsetscaling3D( 1 ), 1 - offsetscaling3D( 1 ), ...
offsetscaling3D( 2 ), 1 - offsetscaling3D( 2 ), 'axesmap', dimensions( 3 ) - 1 );
end
fprintf( fileID, cs_xaxisformat, 'axesmap', 1, 'axesmap', dimensions( 1 ) - 1 );
fprintf( fileID, cs_yaxisformat, 'axesmap', 1, 'axesmap', dimensions( 2 ) - 1 );
fprintf( fileID, '\t\\fi\n' );
if is3D
% put point coordinates:
fprintf( fileID, '\t%% define coordinates:\n' );
for i = 1 : N
fprintf( fileID, pt_coordformat, i, ...
coordinates( i, dimensions( 1 : 2 ) ) + ...
coordinates( i, dimensions( 3 ) ) * offsetscaling3D );
end
% draw causal set from background to foreground:
fprintf( fileID, '\t\\pgfmathsetmacro{\\CSlinkopacityoffset}{\\CSmaxopacity*\\CSlinkopacity-\\CSfading*\\CSlinkopacity}\n' );
fprintf( fileID, '\t\\pgfmathsetmacro{\\CSlinkopacityfactor}{\\CSfading*\\CSlinkopacity}\n' );
[ sorted3D, idx3D ] = sort( scalings3D, 'ascend' ); %#ok<ASGLU>
elementsdrawn = false( N, 1 );
draw_event = [ '\t\\ifnum\\firstHiddenEvent>%d \\node[%s, scale=\\CSeventscaling, opacity=\\CSeventopacity] (pn', pt_nameformat, ') at (p', pt_nameformat, ') {};\n' ];
draw_link = [ '\t\t\\draw[causalrel%d, opacity=%0.3f*\\CSlinkopacityfactor+\\CSlinkopacityoffset] (pn', pt_nameformat, ') -- (pn', pt_nameformat, ');\n' ];
draw_label = [ '\t\t\\ifdraweventlabels\\node[eventlbl, scale=\\CSeventscaling*\\CSlblscaling, fill opacity=\\CSeventopacity*\\CSlblopacityfill, text opacity=\\CSeventopacity*\\CSlblopacitytext] at (pn', pt_nameformat, '.east) {%d};\\fi\\fi\n' ];
for i = transpose( idx3D )
if ( ignore_faded == false ) || scalings3D( i ) > fadingdepth
fprintf( fileID, '\t%% draw element #%d and its links:\n', i );
fprintf( fileID, '\t\\pgfmathsetmacro{\\CSeventopacity}{%0.3f*\\CSfading+\\CSmaxopacity-\\CSfading}\n', scalings3D( i ) );
fprintf( fileID, '\t\\pgfmathsetmacro{\\CSeventscaling}{%0.3f+%0.3f*\\CSminscaling}\n', scalings3D( i ), 1 - scalings3D( i ) );
fprintf( fileID, draw_event, i, ...
EventStyleString( drawingstyles{ i } ), i, i );
for s = 1 : length( linkselectors )
for j = 1 : N
if elementsdrawn( j ) && ...
linkselectors{ s }( i ) && ...
linkselectors{ s }( j ) && ...
( L( i, j ) || L( j, i ) )
fprintf( fileID, draw_link, ...
s, scalings3D( j ), i, j );
end
end
end
fprintf( fileID, draw_label, i, i );
elementsdrawn( i ) = true;
end
end
else
% put point coordinates:
fprintf( fileID, '\t%% define coordinates:\n' );
for i = 1 : N
fprintf( fileID, pt_coordformat, i, coordinates( i, dimensions ) );
end
% draw causal set relations:
fprintf( fileID, '\t%% draw links:\n' );
for s = 1 : length( linkselectors )
fprintf( fileID, replace( causet_saveas_links2tikz( ...
L, linkselectors{ s }, s ), '\', '\\' ) );
end
% draw point coordinates:
fprintf( fileID, '\t%% draw elements:\n' );
draw_event = [ '\t\\ifnum\\firstHiddenEvent>%d \\node[%s] (pn', pt_nameformat, ') at (p', pt_nameformat, ') {};\n' ];
draw_label = [ '\t\t\\ifdraweventlabels\\node[eventlbl] at (pn', pt_nameformat, '.east) {%d};\\fi\\fi\n' ];
for i = 1 : N
fprintf( fileID, draw_event, i, ...
EventStyleString( drawingstyles{ i } ), i, i );
fprintf( fileID, draw_label, i, i );
end
% for idig = 1 : pt_digits
% if idig < pt_digits
% pt_namepadding = sprintf( [ '%0', num2str( pt_digits - idig ), 'u' ], 0 );
% else
% pt_namepadding = '';
% end
% fprintf( fileID, '\t\\foreach \\pi in {%d, ..., %d} {\n', 10^( idig - 1 ), min( 10^idig - 1, N ) );
% fprintf( fileID, [ '\t\t\\node[event] (pn', pt_namepadding, '\\pi) at (p', pt_namepadding, '\\pi) {};\n' ] );
% fprintf( fileID, [ '\t\t\\ifdraweventlabels\\node[eventlbl] at ($(pn', pt_namepadding, '\\pi.east)$) {\\pi};\\fi\n' ] );
% fprintf( fileID, '\t}\n' );
% end
end
% end picture:
if isfield( flags, 'EndCommands' )
fprintf( fileID, '\t%% end commands:\n' );
fprintf( fileID, flags.EndCommands );
end
if ~flags.IsSubPicture
fprintf( fileID, '\\end{tikzpicture}\n' );
end
fclose( fileID );
end
function s = EventStyleString( dstyles )
s = '';
draw_eventstyles_count = length( dstyles );
for j = 1 : draw_eventstyles_count
if j < draw_eventstyles_count
s = sprintf( '%sevent%d, ', s, dstyles( j ) );
else
s = sprintf( '%sevent%d', s, dstyles( j ) );
end
end
end