SUMO - Simulation of Urban MObility
BinaryFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Static storage of an output device and its base (abstract) implementation
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2012-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_VERSION_H
34 #include <version.h>
35 #endif
36 
37 #include <utils/common/RGBColor.h>
38 #include <utils/common/ToString.h>
42 #include <utils/geom/Boundary.h>
43 #include "BinaryFormatter.h"
44 
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
50 }
51 
52 
53 void
56  FileHelpers::writeByte(into, 2);
59  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
60  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
63 }
64 
65 
66 void
67 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
69  FileHelpers::writeInt(into, (int)list.size());
70  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
72  FileHelpers::writeString(into, *it);
73  }
74 }
75 
76 
77 bool
79  const std::string& rootElement,
80  const std::map<SumoXMLAttr, std::string>& attrs) {
81  if (myXMLStack.empty()) {
82  writeStaticHeader(into);
83  writeStringList(into, std::vector<std::string>());
84  writeStringList(into, std::vector<std::string>());
85  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
86  openTag(into, rootElement);
87  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
88  writeAttr(into, it->first, it->second);
89  }
90  return true;
91  }
92  }
93  return false;
94 }
95 
96 
97 void
98 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
99  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
100  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
101  }
102 }
103 
104 
105 void
106 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
107  myXMLStack.push_back(xmlElement);
109  const int tagNum = (int)xmlElement;
110  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum % 256));
111  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum / 256));
112 }
113 
114 
115 bool
116 BinaryFormatter::closeTag(std::ostream& into) {
117  if (!myXMLStack.empty()) {
119  myXMLStack.pop_back();
120  return true;
121  }
122  return false;
123 }
124 
125 
126 template<>
127 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
129  FileHelpers::writeByte(into, val);
130 }
131 
132 
133 template<>
134 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val) {
135  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
137  FileHelpers::writeInt(into, int(val * 100. + .5));
138  } else {
140  FileHelpers::writeFloat(into, val);
141  }
142 }
143 
144 
145 template<>
146 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
148  FileHelpers::writeInt(into, val);
149 }
150 
151 
152 template<>
153 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
155  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
156 }
157 
158 
159 template<>
160 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
162  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
163 }
164 
165 
166 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
167  if (val.z() != 0.) {
168  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
169  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
171  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
172  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
173  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
174  } else {
176  FileHelpers::writeFloat(into, val.x());
177  FileHelpers::writeFloat(into, val.y());
178  FileHelpers::writeFloat(into, val.z());
179  }
180  } else {
181  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
182  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
184  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
185  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
186  } else {
188  FileHelpers::writeFloat(into, val.x());
189  FileHelpers::writeFloat(into, val.y());
190  }
191  }
192 }
193 
194 
195 template<>
196 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
198  writePosition(into, val);
199 }
200 
201 
202 template<>
203 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
205  FileHelpers::writeInt(into, static_cast<int>(val.size()));
206  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
207  writePosition(into, *pos);
208  }
209 }
210 
211 
212 template<>
213 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
215  FileHelpers::writeFloat(into, val.xmin());
216  FileHelpers::writeFloat(into, val.ymin());
217  FileHelpers::writeFloat(into, val.xmax());
218  FileHelpers::writeFloat(into, val.ymax());
219 }
220 
221 
222 template<>
223 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
225  FileHelpers::writeByte(into, val.red());
226  FileHelpers::writeByte(into, val.green());
227  FileHelpers::writeByte(into, val.blue());
228  FileHelpers::writeByte(into, val.alpha());
229 }
230 
231 
232 template<>
233 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
235  FileHelpers::writeInt(into, (int)val.size());
236  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
238  FileHelpers::writeInt(into, *it);
239  }
240 }
241 
242 
243 /****************************************************************************/
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:138
SumoXMLTag
Numbers representing SUMO-XML - element names.
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:132
static StringBijection< SumoXMLNodeType > NodeTypes
node types
double z() const
Returns the z-position.
Definition: Position.h:73
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
double y() const
Returns the y-position.
Definition: Position.h:68
bool closeTag(std::ostream &into)
Closes the most recently opened tag.
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
double x() const
Returns the x-position.
Definition: Position.h:63
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:48
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
BinaryFormatter()
Constructor.
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
double xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:126
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
#define VERSION_STRING
Definition: config.h:210
std::vector< SumoXMLTag > myXMLStack
The stack of begun xml elements.
static void writeStringList(std::ostream &into, const std::vector< std::string > &list)
writes a list of strings
static void writeAttr(dummy &into, const SumoXMLAttr attr, const T &val)
writes an arbitrary attribute
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type=BF_INVALID)
writes the header for an arbitrary attribute
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
static void writePosition(std::ostream &into, const Position &val)
writes a position
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
double ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:144
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.