SUMO - Simulation of Urban MObility
BinaryFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Static storage of an output device and its base (abstract) implementation
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #ifdef HAVE_VERSION_H
33 #include <version.h>
34 #endif
35 
36 #include <utils/common/RGBColor.h>
37 #include <utils/common/ToString.h>
41 #include <utils/geom/Boundary.h>
42 #include "BinaryFormatter.h"
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
49 }
50 
51 
52 void
55  FileHelpers::writeByte(into, 2);
58  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
59  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
62 }
63 
64 
65 void
66 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
68  FileHelpers::writeInt(into, (int)list.size());
69  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
71  FileHelpers::writeString(into, *it);
72  }
73 }
74 
75 
76 bool
78  const std::string& rootElement,
79  const std::map<SumoXMLAttr, std::string>& attrs) {
80  if (myXMLStack.empty()) {
81  writeStaticHeader(into);
82  writeStringList(into, std::vector<std::string>());
83  writeStringList(into, std::vector<std::string>());
84  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
85  openTag(into, rootElement);
86  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
87  writeAttr(into, it->first, it->second);
88  }
89  return true;
90  }
91  }
92  return false;
93 }
94 
95 
96 void
97 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
98  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
99  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
100  }
101 }
102 
103 
104 void
105 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
106  myXMLStack.push_back(xmlElement);
108  const int tagNum = (int)xmlElement;
109  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum % 256));
110  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum / 256));
111 }
112 
113 
114 bool
115 BinaryFormatter::closeTag(std::ostream& into) {
116  if (!myXMLStack.empty()) {
118  myXMLStack.pop_back();
119  return true;
120  }
121  return false;
122 }
123 
124 
125 template<>
126 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
128  FileHelpers::writeByte(into, val);
129 }
130 
131 
132 template<>
133 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val) {
134  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
136  FileHelpers::writeInt(into, int(val * 100. + .5));
137  } else {
139  FileHelpers::writeFloat(into, val);
140  }
141 }
142 
143 
144 template<>
145 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
147  FileHelpers::writeInt(into, val);
148 }
149 
150 
151 template<>
152 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
154  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
155 }
156 
157 
158 template<>
159 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
161  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
162 }
163 
164 
165 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
166  if (val.z() != 0.) {
167  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
168  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
170  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
171  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
172  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
173  } else {
175  FileHelpers::writeFloat(into, val.x());
176  FileHelpers::writeFloat(into, val.y());
177  FileHelpers::writeFloat(into, val.z());
178  }
179  } else {
180  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
181  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
183  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
184  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
185  } else {
187  FileHelpers::writeFloat(into, val.x());
188  FileHelpers::writeFloat(into, val.y());
189  }
190  }
191 }
192 
193 
194 template<>
195 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
197  writePosition(into, val);
198 }
199 
200 
201 template<>
202 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
204  FileHelpers::writeInt(into, static_cast<int>(val.size()));
205  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
206  writePosition(into, *pos);
207  }
208 }
209 
210 
211 template<>
212 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
214  FileHelpers::writeFloat(into, val.xmin());
215  FileHelpers::writeFloat(into, val.ymin());
216  FileHelpers::writeFloat(into, val.xmax());
217  FileHelpers::writeFloat(into, val.ymax());
218 }
219 
220 
221 template<>
222 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
224  FileHelpers::writeByte(into, val.red());
225  FileHelpers::writeByte(into, val.green());
226  FileHelpers::writeByte(into, val.blue());
227  FileHelpers::writeByte(into, val.alpha());
228 }
229 
230 
231 template<>
232 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
234  FileHelpers::writeInt(into, (int)val.size());
235  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
237  FileHelpers::writeInt(into, *it);
238  }
239 }
240 
241 
242 /****************************************************************************/
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:137
SumoXMLTag
Numbers representing SUMO-XML - element names.
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:131
static StringBijection< SumoXMLNodeType > NodeTypes
node types
double z() const
Returns the z-position.
Definition: Position.h:72
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:89
double y() const
Returns the y-position.
Definition: Position.h:67
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:62
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:82
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:47
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:45
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:125
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:75
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:68
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:143
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.