Vcsn  2.3
Be Rational
tikz.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <algorithm>
4 #include <iostream>
5 #include <unordered_map>
6 #include <unordered_set>
7 #include <vector>
8 
9 #include <vcsn/algos/grail.hh> // printer
10 
11 #include <vcsn/dyn/fwd.hh>
12 
13 namespace vcsn
14 {
15 
16  /*--------------------------.
17  | tikz(automaton, stream). |
18  `--------------------------*/
19 
20  namespace detail
21  {
25  template <Automaton Aut>
26  class tikzer: public printer<Aut>
27  {
28  public:
30  using typename super_t::automaton_t;
31  using typename super_t::state_t;
32  using typename super_t::polynomial_t;
33  using typename super_t::transition_t;
34  using typename super_t::weight_t;
35 
36  using super_t::aut_;
37  using super_t::os_;
38  using super_t::ps_;
39  using super_t::ws_;
40 
41  using super_t::super_t;
42 
46  void print_finitial_(const std::string& kind, const weight_t& w) const
47  {
48  if (!ws_.is_zero(w))
49  {
50  os_ << ',' << kind;
51  if (ws_.show_one() || !ws_.is_one(w))
52  {
53  os_ << ',' << kind << " text=$\\left\\langle ";
54  ws_.print(w, os_, format::latex) << "\\right\\rangle$";
55  }
56  }
57  }
58 
59  void operator()()
60  {
61  os_ <<
62  "\\documentclass{standalone}\n"
63  " \\usepackage{tikz}\n"
64  " \\usetikzlibrary{arrows.meta, automata, bending,"
65  " positioning, shapes.misc}\n"
66  " \\tikzstyle{automaton}=[shorten >=1pt,"
67  " >={Stealth[bend,round]}, initial text=]\n"
68  " \\tikzstyle{accepting}=[accepting by arrow]\n"
69  "\n"
70  "\\begin{document}\n"
71  "\\begin{tikzpicture}[automaton, auto]\n"
72  ;
73 
74  print_states_();
76 
77  os_ <<
78  "\\end{tikzpicture}\n"
79  "\\end{document}";
80  }
81 
82  private:
84  void print_states_() const
85  {
86  state_t prev = aut_->null_state();
87  for (auto s : aut_->states())
88  {
89  os_ << " \\node[state";
90  if (aut_->is_lazy(s))
91  os_ << ",dashed";
92  else
93  {
94  print_finitial_("initial", aut_->get_initial_weight(s));
95  print_finitial_("accepting", aut_->get_final_weight(s));
96  }
97  if (aut_->state_has_name(s))
98  os_ << ",rounded rectangle";
99  os_ << "] (";
100  aut_->print_state(s, os_);
101  os_ << ')';
102  if (prev != aut_->null_state())
103  {
104  os_ << " [right=of ";
105  aut_->print_state(prev, os_);
106  os_ << ']';
107  }
108  os_ << " {$";
109  aut_->print_state_name(s, os_, format::latex);
110  os_ << "$};\n";
111  prev = s;
112  }
113  }
114 
116  void print_transitions_(const state_t src, const state_t dst,
117  const polynomial_t& entry) const
118  {
119  os_ << " \\path[->] (";
120  aut_->print_state(src, os_);
121  os_ << ") edge"
122  << (src == dst ? "[loop above]" : "")
123  << " node"
124  << " {$";
125  ps_.print(entry, os_, format::latex, ", ");
126  os_ << "$} (";
127  aut_->print_state(dst, os_);
128  os_ << ");\n";
129  }
130 
133  {
134  // For each src state, the destinations, sorted.
135  std::map<state_t, polynomial_t> dsts;
136  for (auto src : aut_->states())
137  if (!aut_->is_lazy(src))
138  {
139  dsts.clear();
140  for (auto t: out(aut_, src))
141  // Bypass weight_of(set), because we know that the weight is
142  // nonzero, and that there is only one weight per letter.
143  ps_.new_weight(dsts[aut_->dst_of(t)],
144  aut_->label_of(t), aut_->weight_of(t));
145  for (const auto& p: dsts)
146  print_transitions_(src, p.first, p.second);
147  }
148  }
149  };
150  }
151 
155  template <Automaton AutPtr>
156  std::ostream&
157  tikz(const AutPtr& aut, std::ostream& out)
158  {
159  detail::tikzer<AutPtr> t{aut, out};
160  t();
161  return out;
162  }
163 }
Factor common bits in automaton formatting.
Definition: printer.hh:24
const polynomialset_t ps_
Short-hand to the polynomialset used to print the entries.
Definition: printer.hh:128
const weightset_t & ws_
Short-hand to the weightset.
Definition: printer.hh:126
Format automaton to TikZ format.
Definition: tikz.hh:26
weight_t_of< automaton_t > weight_t
Definition: printer.hh:44
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
Definition: automaton.hh:64
typename polynomialset_t::value_t polynomial_t
Definition: printer.hh:46
std::ostream & tikz(const AutPtr &aut, std::ostream &out)
Print automaton to TikZ format.
Definition: tikz.hh:157
Definition: a-star.hh:8
void operator()()
Definition: tikz.hh:59
transition_t_of< automaton_t > transition_t
Definition: printer.hh:42
state_t_of< automaton_t > state_t
Definition: printer.hh:37
std::ostream & os_
Output stream.
Definition: printer.hh:122
void print_finitial_(const std::string &kind, const weight_t &w) const
Format an initial/final weight.
Definition: tikz.hh:46
Print for LaTeX.
Definition: format.hh:22
automaton_t aut_
The automaton we have to output.
Definition: printer.hh:120
void print_transitions_()
Print all the transitions, sorted by src state, then dst state.
Definition: tikz.hh:132
void print_states_() const
Pretty-print states.
Definition: tikz.hh:84
void print_transitions_(const state_t src, const state_t dst, const polynomial_t &entry) const
Print the transitions between state src and state dst.
Definition: tikz.hh:116