summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/adigraph/adigraph.sty
blob: 5d8ad435831e4a1e99279bccadf3018225fdcb7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
%
% Copyright 2018 Luca Cappelletti
%
% Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
%
% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
%
\NeedsTeXFormat{LaTeX2e}[1994/06/01]
\ProvidesPackage{adigraph}
[2018/03/09 v1.2 LaTeX package for creating augmenting directed graphs]

\RequirePackage{etoolbox}
\RequirePackage{fp}
\RequirePackage{xparse}
\RequirePackage{xstring}
\RequirePackage{tikz}
\usetikzlibrary{calc}

\makeatletter
\newcommand*{\AdigraphRom}[1]{\expandafter\@slowromancap\romannumeral #1@}
\makeatother

\newcounter{AdigraphNumberOfPaths}
\setcounter{AdigraphNumberOfPaths}{0}

\newcounter{AdigraphCurrentPathNumber}
\setcounter{AdigraphCurrentPathNumber}{0}

\NewDocumentCommand{\AdigraphDrawNode}{m}{%
	%
	% #1 is node normalized id
	%
	\letcs{\nodeX}{adigraphNode#1X}
	\letcs{\nodeY}{adigraphNode#1Y}
	\letcs{\nodeLabel}{adigraphNode#1Label}
	\letcs{\nodeColor}{adigraphNode#1Color}
	%
	\node[vertex,\nodeColor] (#1) at (\nodeX,\nodeY) {\nodeLabel};
}

\NewDocumentCommand{\AdigraphDrawEdge}{m m}{%
	%
	% #1 is first node normalized id
	% #2 is second node normalized id
	%
	\ifbool{adigraphEdge#1v#2Drawn}{%
		% This edge was already drawn
	}{%
		\letcs{\firstX}{adigraphNode#1X}
		\letcs{\firstY}{adigraphNode#1Y}
		\letcs{\secondX}{adigraphNode#2X}
		\letcs{\secondY}{adigraphNode#2Y}

		\ifnumgreater{\secondX-\firstX}{0}{%
			\def\AdigraphLabelPosition{above}
		}{%
			\ifnumequal{\secondX-\firstX}{0}{%
				\ifnumgreater{\secondY-\firstY}{0}{%
					\def\AdigraphLabelPosition{left}
				}{%
					\def\AdigraphLabelPosition{right}
				}
			}{%
				\def\AdigraphLabelPosition{below}
			}
		}

		\booltrue{adigraphEdge#1v#2Drawn}
		\letcs{\edgeLabelPosition}{adigraphEdge#1v#2LabelPosition}
		\letcs{\edgeWeight}{adigraphEdge#1v#2Weight}
		\ifcsdef{adigraphEdge#2v#1Weight}{%
			\letcs{\AdigraphTwinEdgeWeight}{adigraphEdge#2v#1Weight}
		}{%
			\def\AdigraphTwinEdgeWeight{0}
		}
		\letcs{\edgeColor}{adigraphEdge#1v#2Color}
		\ifbool{adigraphEdge#1v#2HasLabel}{%
			\letcs{\edgeLabel}{adigraphEdge#1v#2Label}
		}{%
			\letcs{\edgeLabel}{adigraphEdge#1v#2Weight}
		}
		\ifnumequal{\edgeWeight}{0}{%
			% We don't draw edges with weight 0
		}{%
			\ifnumequal{\AdigraphTwinEdgeWeight}{0}{%
				% The twin edge won't be drawn
				% This edge will be a straight line
				\draw[edge,\edgeColor] (#1) to node[\edgeLabelPosition, \AdigraphLabelPosition] {\edgeLabel}(#2);
			}{%
				% Both edges have to be drawn, they will be curly
				\draw[edge,\edgeColor] (#1) to [bend right=20] node[\edgeLabelPosition, \AdigraphLabelPosition] {\edgeLabel}(#2);
			}%
		}%
	}
}

\NewDocumentCommand{\AdigraphMemorizeNode}{m m G{black}}{}

\NewDocumentCommand{\AdigraphBuildNode}{m > { \SplitArgument{ 2 } {,} } m G{#1}}{%
	%
	% #1 -> given node name, not normalized
	% #2 -> list of coordinates and color
	% #3 -> optional label
	%
	\ifblank{#1}{%
		%Do nothing, this is the tail of the list
	}{%
		\IfInteger{#1}{%
			\def\AdigraphNodeName{\AdigraphRom{#1}}%
			\listgadd{\AdigraphNodeList}{\AdigraphRom{#1}}
		}{%
			\def\AdigraphNodeName{#1}%
			\listgadd{\AdigraphNodeList}{#1}
		}%
		\RenewDocumentCommand{\AdigraphMemorizeNode}{m m G{black}}{%
			\csdef{adigraphNode\AdigraphNodeName Label}{#3}%
			\csdef{adigraphNode\AdigraphNodeName X}{##1}%
			\csdef{adigraphNode\AdigraphNodeName Y}{##2}%
			\csdef{adigraphNode\AdigraphNodeName Color}{##3}%
		}%
		\AdigraphMemorizeNode#2
	}
}

\NewDocumentCommand{\AdigraphMemorizeEdge}{m m G{black}}{}

\NewDocumentCommand{\AdigraphBuildEdge}{> {\SplitArgument{2}{,}} m m m m}{%
	%
	% #1 -> List of edge nodes and its color, validated
	% #2 -> Weight
	% #3 -> Label, defaults to weight
	% #4 -> Label position, defaults to midway
	%
	\RenewDocumentCommand{\AdigraphMemorizeEdge}{m m G{black}}{%
		\IfInteger{##1}{%
			\IfInteger{##2}{%
				\def\AdigraphEdgeName{\AdigraphRom{##1}v\AdigraphRom{##2}}%
				\listgadd{\AdigraphEdgeList}{\AdigraphRom{##1},\AdigraphRom{##2}}
			}{%
				\def\AdigraphEdgeName{\AdigraphRom{##1}v##2}%
				\listgadd{\AdigraphEdgeList}{\AdigraphRom{##1},##2}
			}%
		}{%
			\IfInteger{##2}{%
				\def\AdigraphEdgeName{##1v\AdigraphRom{##2}}%
				\listgadd{\AdigraphEdgeList}{##1,\AdigraphRom{##2}}
			}{%
				\def\AdigraphEdgeName{##1v##2}%
				\listgadd{\AdigraphEdgeList}{##1,##2}
			}%
		}%
		\newbool{adigraphEdge\AdigraphEdgeName HasLabel}
		\newbool{adigraphEdge\AdigraphEdgeName Drawn}
		\ifblank{#3}{%
			\boolfalse{adigraphEdge\AdigraphEdgeName HasLabel}
		}{%
			\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
		}%
		\ifblank{#2}{%
			\booltrue{adigraphEdge\AdigraphEdgeName HasLabel}
			\csdef{adigraphEdge\AdigraphEdgeName Weight}{1}%
		}{%
			\csdef{adigraphEdge\AdigraphEdgeName Weight}{#2}%
		}%
		\csdef{adigraphEdge\AdigraphEdgeName Label}{#3}%
		\csdef{adigraphEdge\AdigraphEdgeName Color}{##3}%
		\csdef{adigraphEdge\AdigraphEdgeName LabelPosition}{#4}%
	}
	\AdigraphMemorizeEdge#1
}

\NewDocumentCommand{\AdigraphBuildEdgeWrapper}{m G{} G{} G{midway}}{%
	%
	% #1 -> List of edge nodes and its color
	% #2 -> Weight
	% #3 -> Label
	% #4 -> Label position
	%
	\ifblank{#1}{%
		%Do nothing, this is the tail of the list
	}{%
		\AdigraphBuildEdge{#1}{#2}{#3}{#4}
	}
}

\NewDocumentCommand{\AdigraphElaboratePath}{m m G{black}}{}

\newcount\AdigraphCounter
\NewDocumentCommand{\AdigraphSimpleSum}{m m m}{%
	%
	% #1 -> First addendum
	% #2 -> Second addendum
	% #3 -> Variable to store sum
	%
	\AdigraphCounter #1\relax
	\advance\AdigraphCounter +#2\relax
	\FPset{#3}{\the\AdigraphCounter}
}

\NewDocumentCommand{\AdigraphPathBuilder}{m m m}{%
	%
	% #1 -> first node
	% #2 -> second node
	% #3 -> units
	%
	\ifcsdef{adigraphEdge#1v#2Weight}{%
		\letcs{\AdigraphWeightA}{adigraphEdge#1v#2Weight}
	}{%
		\def\AdigraphWeightA{0}
		\newbool{adigraphEdge#1v#2HasLabel}
		\ifcsdef{adigraphEdge#2v#1LabelPosition}{%
			\csdef{adigraphEdge#1v#2LabelPosition}{\csuse{adigraphEdge#2v#1LabelPosition}}
		}{%
			\csdef{adigraphEdge#1v#2LabelPosition}{midway}
		}
		\csdef{adigraphEdge#1v#2LabelPosition}{midway}
		\newbool{adigraphEdge#1v#2Drawn}
		\csdef{adigraphEdge#1v#2Color}{black}%
		\listgadd{\AdigraphEdgeList}{#1,#2}
	}
	\ifcsdef{adigraphEdge#2v#1Weight}{%
		\letcs{\AdigraphWeightB}{adigraphEdge#2v#1Weight}
	}{%
		\def\AdigraphWeightB{0}
		\newbool{adigraphEdge#2v#1HasLabel}
		\ifcsdef{adigraphEdge#1v#2LabelPosition}{%
			\csdef{adigraphEdge#2v#1LabelPosition}{\csuse{adigraphEdge#1v#2LabelPosition}}
		}{%
			\csdef{adigraphEdge#2v#1LabelPosition}{midway}
		}
		\newbool{adigraphEdge#2v#1Drawn}
		\csdef{adigraphEdge#2v#1Color}{black}%
		\listgadd{\AdigraphEdgeList}{#2,#1}
	}

	\ifnum\value{AdigraphCurrentPathNumber}=\value{AdigraphNumberOfPaths}
		\csdef{adigraphEdge#1v#2Color}{blue}%
		\csdef{adigraphEdge#2v#1Color}{red}%
	\else
		% Leaves the default colors
	\fi
	\expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightA}{-#3}{\resultA}
	\expandafter\AdigraphSimpleSum\expandafter{\AdigraphWeightB}{#3}{\resultB}
	\cslet{adigraphEdge#1v#2Weight}{\resultA}%
	\cslet{adigraphEdge#2v#1Weight}{\resultB}%
}

\NewDocumentCommand{\AdigraphBuildPath}{> { \SplitList{,} } m m}{%
	%
	% #1 -> List of nodes
	% #2 -> Units to send over path
	%
	\RenewDocumentCommand{\AdigraphElaboratePath}{m}{%
		\IfInteger{##1}{%
			\def\AdigraphCurrentNode{\AdigraphRom{##1}}%
		}{%
			\def\AdigraphCurrentNode{##1}%
		}%
		\ifdef{\AdigraphLastParsedNode}{%
			\expandafter\expandafter\expandafter\AdigraphPathBuilder\expandafter\expandafter\expandafter{\expandafter\AdigraphLastParsedNode\expandafter}\expandafter{\AdigraphCurrentNode}{#2}
		}{%
			% This is the first iteration
		}
		\IfInteger{##1}{%
			\def\AdigraphLastParsedNode{\AdigraphRom{##1}}%
		}{%
			\def\AdigraphLastParsedNode{##1}%
		}%
	}
	\ProcessList{#1}{\AdigraphElaboratePath}
	\undef{\AdigraphLastParsedNode}
}

\NewDocumentCommand{\AdigraphNodeBuilder}{ > { \SplitArgument{ 2 } {:} } m }{%
	\AdigraphBuildNode#1
}

\NewDocumentCommand{\AdigraphEdgeBuilder}{ > { \SplitArgument{ 3 } {:} } m }{%
	\AdigraphBuildEdgeWrapper#1
}

\NewDocumentCommand{\AdigraphEdgeDrawer}{ > { \SplitArgument{ 1 } {,} } m }{%
	\AdigraphDrawEdge#1
}

\NewDocumentCommand{\AdigraphProcessNodes}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphNodeBuilder}
	\forlistloop{\AdigraphDrawNode}{\AdigraphNodeList}
}

\NewDocumentCommand{\AdigraphProcessEdges}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphEdgeBuilder}
}

\NewDocumentCommand{\AdigraphProcessPaths}{> { \SplitArgument{ 1 } {:} } m}{%
	\stepcounter{AdigraphCurrentPathNumber}
	\AdigraphBuildPath#1
}

\NewDocumentCommand{\AdigraphCountPaths}{m}{%
	\stepcounter{AdigraphNumberOfPaths}
}

\NewDocumentCommand{\AdigraphCalculateOrientation}{m m m}{%
	\letcs{\firstX}{adigraphNode#2X}
	\letcs{\firstY}{adigraphNode#2Y}
	\letcs{\secondX}{adigraphNode#3X}
	\letcs{\secondY}{adigraphNode#3Y}

	\ifnumequal{\secondX-\firstX}{0}{%
		\FPset{#1}{90}
	}{%
		\FPsub{\deltaY}{\secondY}{\firstY}
		\FPsub{\deltaX}{\secondX}{\firstX}
		\FPdiv{\deltaDiv}{\deltaY}{\deltaX}
		\FParctan{\arcResult}{\deltaDiv}
		\FPmul{#1}{\arcResult}{57.2958}
	}
}

\NewDocumentCommand{\AdigraphExecuteCutBuilder}{m m G{black}}{%
\ifblank{#1}{%
% Reached end of list
}{%
\IfInteger{#1}{%
	\def\AdigraphFirstNode{\AdigraphRom{#1}}%
}{%
	\def\AdigraphFirstNode{#1}%
}%
\IfInteger{#2}{%
	\def\AdigraphSecondNode{\AdigraphRom{#2}}%
}{%
	\def\AdigraphSecondNode{#2}%
}%
\AdigraphCalculateOrientation{\orientation}{\AdigraphFirstNode}{\AdigraphSecondNode}
\draw[dashed,#3,rotate=\orientation] ([yshift=10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$) -- ([yshift=-10pt]$(\AdigraphFirstNode)!0.7!(\AdigraphSecondNode)$);
}
}

\NewDocumentCommand{\AdigraphCutBuilder}{> { \SplitArgument { 2 } { , } } m}{%
	\AdigraphExecuteCutBuilder #1
}

\NewDocumentCommand{\AdigraphProcessCuts}{> { \SplitList{;} } m}{%
	\ProcessList{#1}{\AdigraphCutBuilder}
}

\NewDocumentCommand{\Adigraph}{m m m m m}{%
	%
	% #1 -> Vertices
	% #2 -> Edges
	% #3 -> Augmenting paths
	% #4 -> Cuts
	% #5 -> Current augmenting paths
	%
	\def\AdigraphNodeList{}
	\def\AdigraphEdgeList{}
	\letcs{\AdigraphAugmentingPaths}{#3}
	\begin{tikzpicture}
		\tikzset{%
			vertex/.style={circle,draw,minimum size=2em},
			edge/.style={->,> = latex}
		}

		% vertices
		\ifblank{#1}{%
			% List is empty
		}{%
			\AdigraphProcessNodes{#1}
		}

		\ifblank{#2}{%
			% List is empty
		}{%
			\AdigraphProcessEdges{#2}
		}

		\ifblank{\AdigraphAugmentingPaths}{%
			% List is empty
		}{%
			\ifblank{#5}{%
				\defcounter{AdigraphNumberOfPaths}{100000}
			}{%
			}
			\forlistloop{\AdigraphCountPaths}{\AdigraphAugmentingPaths}
			\forlistloop{\AdigraphProcessPaths}{\AdigraphAugmentingPaths}
		}

		\ifblank{#2}{%
			% List is empty
		}{%
			\forlistloop{\AdigraphEdgeDrawer}{\AdigraphEdgeList}
		}

		%cuts
		\ifblank{#4}{%
			% List is empty
		}{%
			\AdigraphProcessCuts{#4}
		}
	\end{tikzpicture}
}

\NewDocumentCommand{\NewAdigraph}{m m G{}}{%
	%
	% #1 -> Variable to assign to as command
	% #2 -> Nodes
	% #3 -> Edges
	%
	\csdef{Adigraph#1AugmentingPaths}{}%
	\expandafter\NewDocumentCommand{\AdigraphProcessAugmentingPaths}{m}{%
		\listcsgadd{Adigraph#1AugmentingPaths}{##1}
	}
	\expandafter\NewDocumentCommand{\AdigraphProcessAugmentingPathsList}{> { \SplitList{;} } m}{%
		\AdigraphProcessAugmentingPaths##1
	}
	\expandafter\NewDocumentCommand\expandafter{\csname #1\endcsname}{m G{}}{%
		%
		% ##1 -> Augmenting path
		%
		\AdigraphProcessAugmentingPathsList{##1}
		\Adigraph{#2}{#3}{Adigraph#1AugmentingPaths}{##2}{##1}
	}%
}

\endinput
%%
%% End of file `adigraph.sty'.