Parasol Planning Library (PPL)
IOUtils.h
Go to the documentation of this file.
1 #ifndef PMPL_IO_UTILS_H_
2 #define PMPL_IO_UTILS_H_
3 
4 #include <string>
5 #include <iostream>
6 #include <fstream>
7 #include <vector>
8 
9 #include "CountingStreamBuffer.h"
10 #include "PMPLExceptions.h"
11 
12 class Robot;
13 
14 
15 /*------------------------------- Vizmo Debug --------------------------------*/
16 
18 extern std::ofstream* vdo;
19 
20 
22 void VDInit(std::string _filename);
23 
24 
26 void VDClose();
27 
28 
30 void VDComment(std::string _s);
31 
32 
34 void VDClearAll();
35 
36 
38 void VDClearLastTemp();
39 
40 
42 void VDClearComments();
43 
44 
46 template <typename CfgType>
47 void
48 VDAddNode(const CfgType& _cfg) {
49  if(vdo)
50  (*vdo) << "AddNode " << _cfg << std::endl;
51 }
52 
53 
55 template <typename CfgType>
56 void
57 VDRemoveNode(const CfgType& _cfg) {
58  if(vdo)
59  (*vdo) << "RemoveNode " << _cfg << std::endl;
60 }
61 
62 
64 template <typename CfgType>
65 void
66 VDAddEdge(const CfgType& _cfg1, const CfgType& _cfg2) {
67  if(vdo)
68  (*vdo) << "AddEdge " << _cfg1 << " " << _cfg2 << std::endl;
69 }
70 
71 
73 template <typename CfgType>
74 void
75 VDRemoveEdge(const CfgType& _cfg1, const CfgType& _cfg2) {
76  if(vdo)
77  (*vdo) << "RemoveEdge " << _cfg1 << " " << _cfg2 << std::endl;
78 }
79 
80 
82 template <typename CfgType>
83 void
84 VDAddTempCfg(const CfgType& _cfg, const bool _valid) {
85  if(vdo)
86  (*vdo) << "AddTempCfg " << _cfg << " " << _valid << std::endl;
87 }
88 
89 
91 template <typename CfgType>
92 void
93 VDAddTempRay(const CfgType& _cfg) {
94  if(vdo)
95  (*vdo) << "AddTempRay " << _cfg << std::endl;
96 }
97 
98 
100 template <typename CfgType>
101 void
102 VDAddTempEdge(const CfgType& _cfg1, const CfgType& _cfg2) {
103  if(vdo)
104  (*vdo) << "AddTempEdge " << _cfg1 << " " << _cfg2 << std::endl;
105 }
106 
107 
110 template <typename AbstractRoadmapType>
111 void
112 VDTrackRoadmap(AbstractRoadmapType* const _r) {
113  if(!vdo)
114  return;
115 
116  const std::string label = "VizmoDebug";
117  _r->InstallHook(AbstractRoadmapType::HookType::AddVertex, label,
118  [_r](const typename AbstractRoadmapType::vertex_iterator _vi){
119  VDAddNode(_vi->property());
120  });
121  _r->InstallHook(AbstractRoadmapType::HookType::DeleteVertex, label,
122  [_r](const typename AbstractRoadmapType::vertex_iterator _vi) {
123  VDRemoveNode(_vi->property());
124  });
125  _r->InstallHook(AbstractRoadmapType::HookType::AddEdge, label,
126  [_r](const typename AbstractRoadmapType::adj_edge_iterator _ei) {
127  const auto source = (*_ei).source(),
128  target = (*_ei).target();
129  VDAddEdge(_r->GetVertex(source), _r->GetVertex(target));
130  });
131  _r->InstallHook(AbstractRoadmapType::HookType::DeleteEdge, label,
132  [_r](const typename AbstractRoadmapType::adj_edge_iterator _ei) {
133  const auto source = (*_ei).source(),
134  target = (*_ei).target();
135  VDRemoveEdge(_r->GetVertex(source), _r->GetVertex(target));
136  });
137 }
138 
139 
140 /*----------------------------- Other IO Utils -------------------------------*/
141 
143 template <typename CfgType>
144 void
145 WritePath(const std::string& _filename, const std::vector<CfgType>& _path) {
146  std::ofstream ofs(_filename);
147  if(!ofs)
148  throw RunTimeException(WHERE) << "Cannot open file '" << _filename << "'";
149 
150  // Print header.
151  ofs << "VIZMO_PATH_FILE Path Version 2012" << std::endl
152  << "1" << std::endl
153  << _path.size() << std::endl;
154 
155  // Print path.
156  for(auto cit = _path.begin(); cit != _path.end(); ++cit)
157  ofs << *cit << std::endl;
158 }
159 
160 
162 template <typename CfgType>
163 std::vector<CfgType>
164 ReadPath(const std::string& _filename, Robot* const _robot) {
165  std::ifstream ifs(_filename);
166  if(!ifs)
167  throw RunTimeException(WHERE) << "Cannot open file '" << _filename << "'";
168 
169  // Create header.
170  std::string line1, line2;
171  std::getline(ifs, line1);
172  std::getline(ifs, line2);
173  size_t pathSize = 0;
174  ifs >> pathSize;
175 
176  if(line1 != std::string("VIZMO_PATH_FILE Path Version 2012")
177  or line2 != std::string("1")
178  or pathSize == 0)
179  throw ParseException(WHERE) << "Path file '" << _filename
180  << "' has ill-formed header.";
181 
182  // Make path.
183  std::vector<CfgType> path(pathSize, CfgType(_robot));
184  for(size_t i = 0; i < pathSize; ++i)
185  ifs >> path[i];
186 
187  // Make sure we reached the end of the path.
188  ifs >> std::ws;
189  const bool end = ifs.peek() == EOF;
190  if(!end)
191  throw ParseException(WHERE) << "Path file has content after " << pathSize
192  << " expected cfgs.";
193 
194  return path;
195 }
196 
197 
201 bool FileExists(const std::string& _filename);
202 
203 
206 void GoToNext(std::istream& _is);
207 
208 
212 bool IsCommentLine(const char _c);
213 
214 
218 std::string GetPathName(const std::string& _filename);
219 
220 
227 template <typename T>
228 T
229 ReadField(std::istream& _is, CountingStreamBuffer& _cbs,
230  const std::string& _desc) {
231  char c;
232  std::string line;
233  T element = T();
234  while(_is) {
235  c = _is.peek();
236  if(c == '#')
237  getline(_is, line);
238  else if(!isspace(c)) {
239  if (!(_is >> element))
240  throw ParseException(_cbs.Where(), _desc);
241  else
242  break;
243  }
244  else
245  _is.get(c);
246  }
247  if(_is.eof())
248  throw ParseException(_cbs.Where(), _desc + " End of file reached.");
249 
250  return element;
251 };
252 
253 
260 std::string ReadFieldString(std::istream& _is, CountingStreamBuffer& _cbs,
261  const std::string& _desc, const bool _toUpper = true);
262 
263 
268 std::vector<std::string> GetTokens(std::string _s, std::string _delimiters);
269 
270 /*----------------------------------------------------------------------------*/
271 
272 #endif
bool IsCommentLine(const char _c)
Definition: IOUtils.cpp:84
std::string ReadFieldString(std::istream &_is, CountingStreamBuffer &_cbs, const std::string &_desc, const bool _toUpper=true)
Definition: IOUtils.cpp:97
void VDAddNode(const CfgType &_cfg)
@TODO
Definition: IOUtils.h:48
void WritePath(const std::string &_filename, const std::vector< CfgType > &_path)
Write a list of Cfgs from a path to file.
Definition: IOUtils.h:145
void VDClearComments()
@TODO
Definition: IOUtils.cpp:52
void VDAddTempEdge(const CfgType &_cfg1, const CfgType &_cfg2)
@TODO
Definition: IOUtils.h:102
void VDAddEdge(const CfgType &_cfg1, const CfgType &_cfg2)
@TODO
Definition: IOUtils.h:66
void VDInit(std::string _filename)
@TODO
Definition: IOUtils.cpp:14
void VDAddTempCfg(const CfgType &_cfg, const bool _valid)
@TODO
Definition: IOUtils.h:84
std::vector< std::string > GetTokens(std::string _s, std::string _delimiters)
Definition: IOUtils.cpp:107
void GoToNext(std::istream &_is)
Definition: IOUtils.cpp:67
bool FileExists(const std::string &_filename)
Definition: IOUtils.cpp:60
void VDAddTempRay(const CfgType &_cfg)
@TODO
Definition: IOUtils.h:93
void VDClearAll()
@TODO
Definition: IOUtils.cpp:38
void VDRemoveNode(const CfgType &_cfg)
@TODO
Definition: IOUtils.h:57
T ReadField(std::istream &_is, CountingStreamBuffer &_cbs, const std::string &_desc)
Definition: IOUtils.h:229
void VDClose()
@TODO
Definition: IOUtils.cpp:21
std::vector< CfgType > ReadPath(const std::string &_filename, Robot *const _robot)
Read a list of Cfgs from a file to path.
Definition: IOUtils.h:164
void VDClearLastTemp()
@TODO
Definition: IOUtils.cpp:45
void VDComment(std::string _s)
@TODO
Definition: IOUtils.cpp:31
std::string GetPathName(const std::string &_filename)
Definition: IOUtils.cpp:90
void VDTrackRoadmap(AbstractRoadmapType *const _r)
Definition: IOUtils.h:112
std::ofstream * vdo
@TODO
Definition: IOUtils.cpp:11
void VDRemoveEdge(const CfgType &_cfg1, const CfgType &_cfg2)
@TODO
Definition: IOUtils.h:75
#define WHERE
Macro for retrieving info about file, function, and line number.
Definition: RuntimeUtils.h:32
Definition: CountingStreamBuffer.h:20
std::string Where() const
Definition: CountingStreamBuffer.cpp:48
Definition: Robot.h:31
Definition: PMPLExceptions.h:38
Definition: PMPLExceptions.h:62