Documentation of MARTY
A Modern ARtificial Theoretical phYsicist
latexLink.h
1 // This file is part of MARTY.
2 //
3 // MARTY is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // MARTY is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with MARTY. If not, see <https://www.gnu.org/licenses/>.
15 
23 #pragma once
24 
25 #include "planargraph.h"
26 #include <map>
27 #include <sstream>
28 
29 namespace JSON {
30  class Node;
31 }
32 
33 namespace drawer {
34 
35 enum class ParticleType {
36 
37  None,
38  Scalar,
39  ChargedScalar,
40  Fermion,
41  Majorana,
42  Vector,
43  Gluon,
44  Gaugino,
45  Gluino,
46  Ghost,
47 };
48 
49 std::ostream& operator<<(std::ostream& out,
50  ParticleType type);
51 
52 class LatexLinker {
53 
54  public:
55 
56  struct Color {
57  using Type = unsigned char;
58 
59  Type R = 0;
60  Type G = 0;
61  Type B = 0;
62  Type A = 255;
63 
64  static Color fromStr(std::string const &str) {
65  if (str.empty())
66  return Color();
67  std::istringstream sin(str);
68  Color c;
69  int a;
70  if (sin.eof())
71  return c;
72  sin >> a;
73  c.R = a;
74  if (sin.eof())
75  return c;
76  sin >> a;
77  c.G = a;
78  if (sin.eof())
79  return c;
80  sin >> a;
81  c.B = a;
82  if (sin.eof())
83  return c;
84  sin >> a;
85  c.A = a;
86 
87  return c;
88  }
89  std::string toStr() const {
90  std::ostringstream sout;
91  sout << int(R) << " " << int(G) << " " << int(B) << " " << int(A);
92  return sout.str();
93  }
94  };
95 
96  inline static constexpr Color Black{0, 0, 0, 255};
97  inline static constexpr Color White{255, 255, 255, 255};
98  inline static constexpr Color Red{255, 0, 0, 255};
99  inline static constexpr Color Blue{0, 0, 255, 255};
100  inline static constexpr Color Green{0, 255, 0, 255};
101 
102  enum class NodeType {
103  None = 0,
104  Operator,
105  Cross,
106  PlainDisk,
107  HatchedDisk
108  };
109 
110  struct Node {
111  std::string name = "";
112  NodeType type = NodeType::None;
113  Color color = Black;
114  int size = 0;
115  };
116  struct Edge {
117  size_t i = 0;
118  size_t j = 0;
119  ParticleType type = ParticleType::Scalar;
120  std::string name = "";
121  int sign = true;
122  double curve = 0;
123  bool flipped = false;
124  Color color = Black;
125  int lineWidth = 3;
126  };
127 
128  LatexLinker() = default;
129 
130  LatexLinker(Graph const& graph);
131 
132  ~LatexLinker();
133 
134  LatexLinker(LatexLinker const&) = default;
135  LatexLinker(LatexLinker&&) = default;
136  LatexLinker& operator=(LatexLinker const&) = default;
137  LatexLinker& operator=(LatexLinker&&) = default;
138 
139  drawer::Graph const&getGraph() const;
140  drawer::Graph &getGraph();
141 
142  std::string getName() const {
143  return name;
144  }
145 
146  void setName(std::string const &t_name) {
147  name = t_name;
148  }
149 
150  void clear();
151 
152  void setGraph(drawer::Graph const& t_graph);
153 
154  void setParticlesType(size_t i,
155  size_t j,
156  ParticleType type = ParticleType::Scalar,
157  std::string const& t_name = "",
158  bool sign = true,
159  double curve = 0,
160  bool flipped = false,
161  Color color = Black,
162  int lineWidth = 3);
163 
164  void addOperator(size_t pos);
165 
166  void replaceParticlesType(size_t i,
167  size_t j,
168  ParticleType type = ParticleType::Scalar,
169  bool sign = true);
170 
171  void removeEdge(size_t i, size_t j);
172 
173  void setVertexName(size_t pos,
174  std::string const& name);
175 
176  void setEdgeName(size_t i,
177  size_t j,
178  std::string const& name);
179 
180  void setEdgeSign(size_t i,
181  size_t j,
182  bool sign);
183 
184  void write(std::ostream& out);
185 
186  void saveToNode(JSON::Node *node) const;
187 
188  void save(std::string const& fileName) const;
189 
190  void loadFromNode(JSON::Node *node);
191 
192  void load(std::string const& fileName);
193 
194  static void saveMultiple(std::string const& fileName,
195  std::vector<LatexLinker> const& links);
196 
197  static std::vector<LatexLinker> loadMultiple(std::string const& fileName);
198 
199  void exportPDF(std::string const& fileName,
200  std::string const& path = ".");
201 
202  void exportPNG(std::string const& fileName,
203  std::string const& path = ".");
204 
205  void scale(float factor);
206 
207  std::vector<Node>& getNodes() {
208  return nodes;
209  }
210 
211  std::vector<Node> const &getNodes() const {
212  return nodes;
213  }
214 
215  std::vector<Edge>& getEdges() {
216  return edges;
217  };
218 
219  std::vector<Edge> const& getEdges() const {
220  return edges;
221  }
222 
223  void removeNode(size_t pos);
224 
225  void addNode();
226 
227  std::string getNameExternal(size_t i) const;
228 
229  private:
230 
231  static
232  std::string getNameVertex(size_t pos);
233 
234  void getFlippedEdgeLabels() const;
235 
236  void getStringValue(std::ostream& out,
237  double value);
238 
239  void writeVertex(std::ostream & out,
240  size_t i,
241  Point const& vertex,
242  std::string const& nameVertex,
243  std::string const& nameParticle = "");
244 
245  void writeEdge(std::ostream & out,
246  ParticleType type,
247  std::string const& nameA,
248  std::string const& nameB,
249  bool sign = true,
250  std::string const& nameEdge = "",
251  bool flipLabel = false);
252 
253  void writeLoopEdge(std::ostream & out,
254  ParticleType type,
255  std::string const& nameA,
256  std::string const& nameB,
257  int mode,
258  bool sign = true,
259  std::string const& nameEdge = "",
260  bool flipLabel = false);
261 
262  friend
263  std::ostream& operator<<(std::ostream& out,
264  LatexLinker& linker);
265 
266  public:
267 
268  inline static bool autoLabel = true;
269 
270  inline static const std::string nameOrigin = "O";
271 
272  private:
273 
274  std::string name;
275 
276  float scaleFactor = 1.3;
277 
278  Graph graph;
279 
280  mutable
281  std::vector<Edge> edges;
282 
283  mutable
284  std::map<std::pair<size_t, size_t>, int> multiplicity;
285 
286  std::vector<Node> nodes;
287 };
288 
289 }
Definition: latexLink.h:52
Definition: node.h:39
Definition: drawer.h:29
std::ostream & operator<<(std::ostream &fout, csl::Type type)
Definition: latexLink.h:110
Definition: planargraph.h:169
Inherits from JSON::Object, specialized in JSON Node. A Node owns a vector of Object. The Node&#39;s children can either be Leaf or other Node objects. This allows to store the tree structure of a .json file.
Definition: jsonObject.h:97
Definition: latexLink.h:56
Definition: latexLink.h:116
Definition: planargraph.h:34
Definition: amplitudeSimplification.h:38
Contains all objects related to JSON reading / writing.
Definition: csldatahandler.h:30