DotHelper.cpp
Go to the documentation of this file.
1 
18 #include "DotHelper.hpp"
19 
20 #include <set>
21 #include <fstream>
22 #include <iostream>
23 #include <math.h>
24 
25 namespace ISM {
26 
27  void DotHelper::storeISMToDot(const path outputPath, const std::string& patternName,
28  const std::string& topologyIdentifier,
29  const std::map<std::string, std::vector<ISM::VoteSpecifierPtr>>& objectDefinitons)
30  {
31  std::stringstream fileName;
32  fileName << topologyIdentifier << "_ISM.dot";
33  path filePath = outputPath / fileName.str();
34 
35  std::vector<std::pair<std::string, std::string> > alreadyTakenCombinations;
36  std::ofstream file;
37  std::ios_base::iostate exceptionMask = file.exceptions() |
38  std::ios::failbit |
39  std::ios::badbit;
40  file.exceptions(exceptionMask);
41  try
42  {
43  file.open(filePath.string());
44  file << "digraph " << topologyIdentifier << " {\n";
45 
46  std::set<std::string> patternNames;
47  for (const std::pair< std::string, std::vector<ISM::VoteSpecifierPtr>>& typeIt : objectDefinitons)
48  {
49  for (const VoteSpecifierPtr& voteIt : typeIt.second)
50  {
51  if (std::find(alreadyTakenCombinations.begin(),
52  alreadyTakenCombinations.end(),
53  std::make_pair(voteIt->patternName, voteIt->objectType))
54  == alreadyTakenCombinations.end())
55  {
56  file << voteIt->patternName << " -> " << voteIt->objectType << "[dir=\"back\"];\n";
57 
58  alreadyTakenCombinations.push_back(std::make_pair(voteIt->patternName,
59  voteIt->objectType));
60 
61  patternNames.insert(voteIt->patternName);
62  }
63  }
64  }
65 
66  for (std::string name : patternNames)
67  {
68  file << name << "[shape=\"box\"];\n";
69  }
70 
71  for (TrackPtr track : mTracksPerPattern[patternName]->tracks)
72  {
73  file << track->type << ";\n";
74  }
75 
76  file << "}\n\n";
77  file.flush();
78  file.close();
79  }
80  catch (std::ios_base::failure& e)
81  {
82  std::cerr << e.what() << "\n";
83  }
84  }
85 
86  void DotHelper::storeRelationsToDot(const path outputPath, const std::string& patternName,
87  const std::string& topologyIdentifier,
88  const ObjectRelations& relations,
89  ObjectRelations referenceRelations)
90  {
91  std::stringstream fileName;
92  fileName << topologyIdentifier << "_Relations.dot";
93  path filePath = outputPath / fileName.str();
94 
95  std::ofstream file;
96  std::ios_base::iostate exceptionMask = file.exceptions() |
97  std::ios::failbit |
98  std::ios::badbit;
99  file.exceptions(exceptionMask);
100 
101  try
102  {
103  file.open(filePath.string());
104  file << "graph " << topologyIdentifier << " {\n";
105 
106  if (referenceRelations.empty())
107  {
108  //If the reference topology has no relations, we don't check for removed or added relations.
109  for (const std::pair<unsigned int, ObjectRelationPtr>& relation : relations)
110  {
111  file << relation.second->getObjectTypeA() << " -- "
112  << relation.second->getObjectTypeB() << ";\n";
113  }
114  }
115  else
116  {
117  ObjectRelations addedRelations;
118  ObjectRelations removedRelations;
119  ObjectRelations unchangedRelations;
120 
121  //Find the relations that were changed compared to the reference topology.
122  for (const std::pair<unsigned int, ObjectRelationPtr>& relation : relations)
123  {
124  if (referenceRelations.find(relation.first) == referenceRelations.end())
125  {
126  addedRelations.insert(relation);
127  }
128  else
129  {
130  unchangedRelations.insert(relation);
131  }
132 
133  referenceRelations.erase(relation.first);
134  }
135 
136  removedRelations = referenceRelations;
137 
138  for (auto relation : unchangedRelations)
139  {
140  file << relation.second->getObjectTypeA() << " -- "
141  << relation.second->getObjectTypeB() << ";\n";
142  }
143 
144  for (auto relation : addedRelations)
145  {
146  file << relation.second->getObjectTypeA() << " -- "
147  << relation.second->getObjectTypeB() << "[color=\"green\", penwidth=\"2\"];\n";
148  }
149 
150  for (auto relation : removedRelations)
151  {
152  file << relation.second->getObjectTypeA() << " -- "
153  << relation.second->getObjectTypeB() << "[color=\"red\", style=\"dotted\", penwidth=\"2\"];\n";
154  }
155  }
156 
157  //Arange the nodes in a circle
158  double angle = 0;
159  double angleStepSize = 2 * M_PI / mTracksPerPattern[patternName]->tracks.size();
160 
161  //Calculate the radius of the circle so that neighbouring nodes have a distance equal to NODE_DISTANCE
162  double radius = sqrt((NODE_DISTANCE * NODE_DISTANCE) /
163  (sin(angleStepSize) * sin(angleStepSize) + cos(angleStepSize) * cos(angleStepSize)));
164 
165  for (TrackPtr& track : mTracksPerPattern[patternName]->tracks)
166  {
167  //Calculate x and y position values and round result to four places
168  double x = radius * floor(sin(angle) * 1000) / 1000;
169  double y = radius * floor(cos(angle) * 1000) / 1000;
170 
171  file << track->type << " [pos=\"" << x << "," << y << "!\"];\n";
172  angle += angleStepSize;
173  }
174 
175  file << "}\n\n";
176  file.flush();
177  file.close();
178  }
179  catch (std::ios_base::failure& e)
180  {
181  std::cerr << e.what() << "\n";
182  }
183  }
184 
185 }
std::map< std::string, ISM::TracksPtr > mTracksPerPattern
Definition: DotHelper.hpp:47
std::string patternName
void storeRelationsToDot(const path outputPath, const std::string &patternName, const std::string &topologyIdentifier, const ISM::ObjectRelations &relations, ISM::ObjectRelations referenceRelations=ObjectRelations())
Definition: DotHelper.cpp:86
void storeISMToDot(const path outputPath, const std::string &patternName, const std::string &topologyIdentifier, const std::map< std::string, std::vector< ISM::VoteSpecifierPtr >> &objectDefinitons)
Definition: DotHelper.cpp:27
const double NODE_DISTANCE
Definition: DotHelper.hpp:48
std::map< unsigned int, ISM::ObjectRelationPtr, std::less< unsigned > > ObjectRelations
boost::shared_ptr< VoteSpecifier > VoteSpecifierPtr
boost::shared_ptr< Track > TrackPtr
Definition: Track.hpp:55
this namespace contains all generally usable classes.


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40