Vibes C++ API
 All Classes Namespaces Files Functions Variables Typedefs Macros Groups
vibes.cpp
1 #include "vibes.h"
2 #include <sstream>
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cassert>
6 
7 //
8 // Vibes properties key,value system implementation
9 //
10 
11 namespace vibes {
12  std::string Value::toJSONString() const {
13  std::ostringstream ss;
14  switch (_type) {
15  case vt_integer:
16  ss<<_integer; break;
17  case vt_decimal:
18  ss<<_decimal; break;
19  case vt_string:
20  ss<<'"'<<_string<<'"'; break;
21  case vt_array:
22  ss << '[';
23  for (std::vector<Value>::const_iterator it = _array.begin(); it != _array.end(); ++it) {
24  if (it != _array.begin()) ss << ',';
25  ss << it->toJSONString();
26  }
27  ss << ']';
28  break;
29  case vt_object:
30  ss << '{' << _object->toJSON() << '}';
31  break;
32  case vt_none:
33  default:
34  break;
35  }
36  return ss.str();
37  }
38 
39  std::string Params::toJSON() const {
40  std::ostringstream ss;
41  for(std::map<std::string, Value>::const_iterator it = _values.begin(); it != _values.end(); ++it)
42  ss << (it==_values.begin()?"":", ") << "\"" << it->first << "\":" << it->second.toJSONString();
43  return ss.str();
44  }
45 
46  Value Params::pop(const std::string &key, const Value &value_not_found) {
47  KeyValueMap::iterator it = _values.find(key);
48  // Return empty value if not found
49  if (it == _values.end())
50  return value_not_found;
51  // Otherwise, return corresponding value and remove it from map
52  Value val = it->second;
53  _values.erase(it);
54  return val;
55  }
56 }
57 
58 //
59 // Vibes messaging implementation
60 //
61 
62 using namespace std;
63 
64 namespace vibes
65 {
66  //
67  // Global variables and utility functions
68  //
69 
70  /// Current communication file descriptor
71  FILE *channel=0;
72 
73  /// Current figure name (client-maintained state)
74  string current_fig="default";
75 
76  //
77  // Management of connection to the Vibes server
78  //
79 
80  void beginDrawing()
81  {
82  // Retrieve user-profile directory from envirnment variable
83  char * user_dir = getenv("USERPROFILE"); // Windows
84  if (!user_dir)
85  user_dir = getenv("HOME"); // POSIX
86  if (user_dir)
87  { // Environment variable found, connect to a file in user's profile directory
88  std::string file_name(user_dir);
89  file_name.append("/.vibes.json");
90  beginDrawing(file_name);
91  }
92  else
93  { // Connect to a file in working directory
94  beginDrawing("vibes.json");
95  }
96  }
97 
98  void beginDrawing(const std::string &fileName)
99  {
100  channel=fopen(fileName.c_str(),"a");
101  }
102 
103  void endDrawing()
104  {
105  fclose(channel);
106  }
107 
108  //
109  // Figure management
110  //
111 
112  void newFigure(const std::string &figureName)
113  {
114  std::string msg;
115  if (!figureName.empty()) current_fig = figureName;
116  msg ="{\"action\":\"new\","
117  "\"figure\":\""+(figureName.empty()?current_fig:figureName)+"\"}\n\n";
118  fputs(msg.c_str(),channel);
119  fflush(channel);
120  }
121 
122  void clearFigure(const std::string &figureName)
123  {
124  std::string msg;
125  msg="{\"action\":\"clear\","
126  "\"figure\":\""+(figureName.empty()?current_fig:figureName)+"\"}\n\n";
127  fputs(msg.c_str(),channel);
128  fflush(channel);
129  }
130 
131  void closeFigure(const std::string &figureName)
132  {
133  std::string msg;
134  msg="{\"action\":\"close\","
135  "\"figure\":\""+(figureName.empty()?current_fig:figureName)+"\"}\n\n";
136  fputs(msg.c_str(),channel);
137  fflush(channel);
138  }
139 
140  void saveImage(const std::string &fileName, const std::string &figureName)
141  {
142  std::string msg;
143  msg="{\"action\":\"export\","
144  "\"figure\":\""+(figureName.empty()?current_fig:figureName)+"\","
145  "\"file\":\""+fileName+"\"}\n\n";
146  fputs(msg.c_str(),channel);
147  fflush(channel);
148  }
149 
150  void selectFigure(const std::string &figureName)
151  {
152  current_fig = figureName;
153  }
154 
155 
156  //
157  // View settings
158  //
159 
160  void axisAuto(Params params)
161  {
162  // { "action": "view", "figure": "Fig2", "box": "auto" }
163 
164  if (params["figure"].empty()) params["figure"] = current_fig;
165  params["action"] = "view";
166  params["box"] = "auto";
167 
168  fputs(Value(params).toJSONString().append("\n\n").c_str(), channel);
169  fflush(channel);
170  }
171 
172  void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params)
173  {
174  //{ "action": "view", "figure": "Fig1", "box": [-6, -2, -3, 1] }
175 
176  if (params["figure"].empty()) params["figure"] = current_fig;
177  params["action"] = "view";
178  params["box"] = (Vec4d){x_lb,x_ub,y_lb,y_ub};
179 
180  fputs(Value(params).toJSONString().append("\n\n").c_str(), channel);
181  fflush(channel);
182  }
183 
184  //
185  // Drawing functions
186  //
187 
188  void drawBox(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params)
189  {
190  Params msg;
191  msg["action"] = "draw";
192  msg["figure"] = params.pop("figure",current_fig);
193  msg["shape"] = (params, "type", "box", "bounds", (Vec4d){x_lb,x_ub,y_lb,y_ub});
194 
195  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
196  fflush(channel);
197  }
198 
199  void drawBox(const vector<double> &bounds, Params params)
200  {
201  assert(!bounds.empty());
202  assert(bounds.size()%2 == 0);
203 
204  Params msg;
205  msg["action"] = "draw";
206  msg["figure"] = params.pop("figure",current_fig);
207  msg["shape"] = (params, "type", "box", "bounds", vector<Value>(bounds.begin(),bounds.end()));
208 
209  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
210  fflush(channel);
211  }
212 
213 
214  void drawEllipse(const double &cx, const double &cy, const double &a, const double &b, const double &rot, Params params)
215  {
216  Params msg;
217  msg["action"] = "draw";
218  msg["figure"] = params.pop("figure",current_fig);
219  msg["shape"] = (params, "type", "ellipse",
220  "center", (Vec2d){cx,cy},
221  "axis", (Vec2d){a,b},
222  "orientation", rot);
223 
224  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
225  fflush(channel);
226  }
227 
228  void drawConfidenceEllipse(const double &cx, const double &cy,
229  const double &sxx, const double &sxy, const double &syy,
230  const double &K, Params params)
231  {
232  Params msg;
233  msg["action"] = "draw";
234  msg["figure"] = params.pop("figure",current_fig);
235  msg["shape"] = (params, "type", "ellipse",
236  "center", (Vec2d){cx,cy},
237  "covariance", (Vec4d){sxx,sxy,sxy,syy},
238  "sigma", K);
239 
240  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
241  fflush(channel);
242  }
243 
244  void drawConfidenceEllipse(const vector<double> &center, const vector<double> &cov,
245  const double &K, Params params)
246  {
247  Params msg;
248  msg["action"] = "draw";
249  msg["figure"] = params.pop("figure",current_fig);
250  msg["shape"] = (params, "type", "ellipse",
251  "center", vector<Value>(center.begin(),center.end()),
252  "covariance", vector<Value>(cov.begin(),cov.end()),
253  "sigma", K);
254 
255  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
256  fflush(channel);
257  }
258 
259  void drawBoxes(const std::vector<std::vector<double> > &bounds, Params params)
260  {
261  Params msg;
262  msg["action"] = "draw";
263  msg["figure"] = params.pop("figure",current_fig);
264  msg["shape"] = (params, "type", "boxes",
265  "bounds", bounds);
266 
267  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
268  fflush(channel);
269  }
270 
271  void drawBoxesUnion(const std::vector<std::vector<double> > &bounds, Params params)
272  {
273  Params msg;
274  msg["action"] = "draw";
275  msg["figure"] = params.pop("figure",current_fig);
276  msg["shape"] = (params, "type", "boxes union",
277  "bounds", bounds);
278 
279  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
280  fflush(channel);
281  }
282 
283  void drawLine(const std::vector<std::vector<double> > &points, Params params)
284  {
285  Params msg;
286  msg["action"] = "draw";
287  msg["figure"] = params.pop("figure",current_fig);
288  msg["shape"] = (params, "type", "line",
289  "points", points);
290 
291  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
292  fflush(channel);
293  }
294 
295  void drawLine(const std::vector<double> &x, const std::vector<double> &y, Params params)
296  {
297  // Reshape x and y into a vector of points
298  std::vector<Value> points;
299  std::vector<double>::const_iterator itx = x.begin();
300  std::vector<double>::const_iterator ity = y.begin();
301  while (itx != x.end() && ity != y.end()) {
302  points.push_back( (Vec2d){*itx++,*ity++} );
303  }
304  // Send message
305  Params msg;
306  msg["action"] = "draw";
307  msg["figure"] = params.pop("figure",current_fig);
308  msg["shape"] = (params, "type", "line",
309  "points", points);
310 
311  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
312  fflush(channel);
313  }
314 
315 
316  void newGroup(const std::string &name, Params params)
317  {
318  // Send message
319  Params msg;
320  msg["action"] = "draw";
321  msg["figure"] = params.pop("figure",current_fig);
322  msg["shape"] = (params, "type", "group",
323  "name", name);
324 
325  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
326  fflush(channel);
327  }
328 
329  void clearGroup(const std::string &figureName, const std::string &groupName)
330  {
331  Params msg;
332  msg["action"] = "clear";
333  msg["figure"] = figureName;
334  msg["group"] = groupName;
335 
336  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
337  fflush(channel);
338  }
339 
340  void clearGroup(const std::string &groupName)
341  {
342  clearGroup(current_fig, groupName);
343  }
344 
345 
346  void removeObject(const std::string &figureName, const std::string &objectName)
347  {
348  Params msg;
349  msg["action"] = "delete";
350  msg["figure"] = figureName;
351  msg["object"] = objectName;
352 
353  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
354  fflush(channel);
355  }
356 
357  void removeObject(const std::string &objectName)
358  {
359  removeObject(current_fig, objectName);
360  }
361 
362  // Property modification
363  void setFigureProperties(const std::string &figureName, const Params &properties)
364  {
365  // Send message
366  Params msg;
367  msg["action"] = "set";
368  msg["figure"] = figureName;
369  msg["properties"] = properties;
370 
371  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
372  fflush(channel);
373  }
374 
375  void setFigureProperties(const Params &properties)
376  {
378  }
379 
380  void setObjectProperties(const std::string &figureName, const std::string &objectName, const Params &properties)
381  {
382  // Send message
383  Params msg;
384  msg["action"] = "set";
385  msg["figure"] = figureName;
386  msg["object"] = objectName;
387  msg["properties"] = properties;
388 
389  fputs(Value(msg).toJSONString().append("\n\n").c_str(), channel);
390  fflush(channel);
391  }
392 
393  void setObjectProperties(const std::string &objectName, const Params &properties)
394  {
395  setObjectProperties(current_fig, objectName, properties);
396  }
397 }
void axisAuto(Params params)
Set axes limits to the bounding box of the drawing.
Definition: vibes.cpp:160
void beginDrawing(const std::string &fileName)
Start VIBes in file saving mode. All commands are saved to the specified file.
Definition: vibes.cpp:98
Definition: vibes.cpp:11
void axisLimits(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params)
Specify the rectangle to be displayed: Lower-left corner (x_lb, y_lb) and a upper-right corner (x_ub...
Definition: vibes.cpp:172
void removeObject(const std::string &figureName, const std::string &objectName)
Delete the named graphical object objectName from figure figureName.
Definition: vibes.cpp:346
void setObjectProperties(const std::string &objectName, const Params &properties)
Assign properties to the named object objectName in current figure.
Definition: vibes.cpp:393
void drawLine(const std::vector< double > &x, const std::vector< double > &y, Params params)
Draw a 2-D line from the list of abscissae x and the list of ordinates y.
Definition: vibes.cpp:295
string current_fig
Current figure name (client-maintained state)
Definition: vibes.cpp:74
void clearGroup(const std::string &groupName)
Clear the contents of the group groupName in current figure.
Definition: vibes.cpp:340
void drawEllipse(const double &cx, const double &cy, const double &a, const double &b, const double &rot, Params params)
Draw an ellipse centered at (cx, cy), with semi-major and minor axes a and b, and rotated by rot degr...
Definition: vibes.cpp:214
void setObjectProperties(const std::string &figureName, const std::string &objectName, const Params &properties)
Assign properties to the named object objectName in figure figureName.
Definition: vibes.cpp:380
Vec< double, 2 > Vec2d
A 2D floating point vector.
Definition: vibes.h:110
void endDrawing()
Close connection to the viewer or the drawing file.
Definition: vibes.cpp:103
void drawBoxes(const std::vector< std::vector< double > > &bounds, Params params)
Draw a list of N-D rectangles from a list of list of bounds in the form ((x_lb_1, x_ub_1...
Definition: vibes.cpp:259
void drawConfidenceEllipse(const double &cx, const double &cy, const double &sxx, const double &sxy, const double &syy, const double &K, Params params)
Draw a 2-D confidence ellipse centered at (cx, cy), with covariance sxx, sxy, syy and scale K...
Definition: vibes.cpp:228
void clearFigure(const std::string &figureName)
Clears the contents of the figure figureName, or the current figure if argument is given...
Definition: vibes.cpp:122
Vec< double, 4 > Vec4d
A 4D floating point vector.
Definition: vibes.h:112
void beginDrawing()
Start VIBes in connected mode: connects to the VIBes viewer.
Definition: vibes.cpp:80
FILE * channel
Current communication file descriptor.
Definition: vibes.cpp:71
void setFigureProperties(const Params &properties)
Assign the given properties to the current figure.
Definition: vibes.cpp:375
void setFigureProperties(const std::string &figureName, const Params &properties)
Assign the given properties to the figure figureName.
Definition: vibes.cpp:363
void closeFigure(const std::string &figureName)
Close the figure named figureName, or the current figure if no argument is given. ...
Definition: vibes.cpp:131
void drawBoxesUnion(const std::vector< std::vector< double > > &bounds, Params params)
Computes and draw the union of a list of N-D rectangles, from a list of list of bounds in the form ((...
Definition: vibes.cpp:271
void newFigure(const std::string &figureName)
Create a new figure named figureName.
Definition: vibes.cpp:112
void clearGroup(const std::string &figureName, const std::string &groupName)
Clear the contents of the group groupName in figure figureName.
Definition: vibes.cpp:329
void drawBox(const vector< double > &bounds, Params params)
Draw a N-D box from a list of bounds in the form (x_lb, x_ub, y_lb, y_ub, z_lb, z_ub, ...)
Definition: vibes.cpp:199
void removeObject(const std::string &objectName)
Delete the named graphical object objectName from current figure.
Definition: vibes.cpp:357
void saveImage(const std::string &fileName, const std::string &figureName)
Definition: vibes.cpp:140
void drawLine(const std::vector< std::vector< double > > &points, Params params)
Draw a N-D line from the list of coordinates points in the form ((x_1, y_1, z_1, ...), (x_2, y_2, z_2, ...), ...)
Definition: vibes.cpp:283
void newGroup(const std::string &name, Params params)
Create a new group with the specified name.
Definition: vibes.cpp:316
void selectFigure(const std::string &figureName)
Select figureName as the current figure. Drawing operations will then apply to figureName.
Definition: vibes.cpp:150
void drawConfidenceEllipse(const vector< double > &center, const vector< double > &cov, const double &K, Params params)
Draw a N-D confidence ellipse centered at center, with covariance in cov and scale K...
Definition: vibes.cpp:244
void drawBox(const double &x_lb, const double &x_ub, const double &y_lb, const double &y_ub, Params params)
Draw a 2-D box with a left lower corner at (x_lb, y_lb) and a right upper corner at (x_ub...
Definition: vibes.cpp:188