Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // Static storage of an output device and its base (abstract) implementation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #ifdef HAVE_VERSION_H
27 #include <version.h>
28 #endif
29 
30 #include <utils/common/RGBColor.h>
31 #include <utils/common/ToString.h>
35 #include <utils/geom/Boundary.h>
36 #include "BinaryFormatter.h"
37 
38 
39 // ===========================================================================
40 // member method definitions
41 // ===========================================================================
43 }
44 
45 
46 void
49  FileHelpers::writeByte(into, 2);
52  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
53  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
56 }
57 
58 
59 void
60 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
62  FileHelpers::writeInt(into, (int)list.size());
63  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
65  FileHelpers::writeString(into, *it);
66  }
67 }
68 
69 
70 bool
72  const std::string& rootElement,
73  const std::map<SumoXMLAttr, std::string>& attrs) {
74  if (myXMLStack.empty()) {
75  writeStaticHeader(into);
76  writeStringList(into, std::vector<std::string>());
77  writeStringList(into, std::vector<std::string>());
78  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
79  openTag(into, rootElement);
80  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
81  writeAttr(into, it->first, it->second);
82  }
83  return true;
84  }
85  }
86  return false;
87 }
88 
89 
90 void
91 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
92  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
93  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
94  }
95 }
96 
97 
98 void
99 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
100  myXMLStack.push_back(xmlElement);
102  const int tagNum = (int)xmlElement;
103  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum % 256));
104  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum / 256));
105 }
106 
107 
108 bool
109 BinaryFormatter::closeTag(std::ostream& into, const std::string& /*comment*/) {
110  if (!myXMLStack.empty()) {
112  myXMLStack.pop_back();
113  return true;
114  }
115  return false;
116 }
117 
118 
119 template<>
120 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
122  FileHelpers::writeByte(into, val);
123 }
124 
125 
126 template<>
127 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val) {
128  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
130  FileHelpers::writeInt(into, int(val * 100. + .5));
131  } else {
133  FileHelpers::writeFloat(into, val);
134  }
135 }
136 
137 
138 template<>
139 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
141  FileHelpers::writeInt(into, val);
142 }
143 
144 
145 template<>
146 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
148  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
149 }
150 
151 
152 template<>
153 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
155  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
156 }
157 
158 
159 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
160  if (val.z() != 0.) {
161  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
162  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
164  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
165  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
166  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
167  } else {
169  FileHelpers::writeFloat(into, val.x());
170  FileHelpers::writeFloat(into, val.y());
171  FileHelpers::writeFloat(into, val.z());
172  }
173  } else {
174  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
175  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
177  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
178  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
179  } else {
181  FileHelpers::writeFloat(into, val.x());
182  FileHelpers::writeFloat(into, val.y());
183  }
184  }
185 }
186 
187 
188 template<>
189 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
191  writePosition(into, val);
192 }
193 
194 
195 template<>
196 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
198  FileHelpers::writeInt(into, static_cast<int>(val.size()));
199  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
200  writePosition(into, *pos);
201  }
202 }
203 
204 
205 template<>
206 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
208  FileHelpers::writeFloat(into, val.xmin());
209  FileHelpers::writeFloat(into, val.ymin());
210  FileHelpers::writeFloat(into, val.xmax());
211  FileHelpers::writeFloat(into, val.ymax());
212 }
213 
214 
215 template<>
216 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
218  FileHelpers::writeByte(into, val.red());
219  FileHelpers::writeByte(into, val.green());
220  FileHelpers::writeByte(into, val.blue());
221  FileHelpers::writeByte(into, val.alpha());
222 }
223 
224 
225 template<>
226 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
228  FileHelpers::writeInt(into, (int)val.size());
229  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
231  FileHelpers::writeInt(into, *it);
232  }
233 }
234 
235 
236 /****************************************************************************/
double ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:131
SumoXMLTag
Numbers representing SUMO-XML - element names.
double xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:125
static StringBijection< SumoXMLNodeType > NodeTypes
node types
double z() const
Returns the z-position.
Definition: Position.h:67
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:83
double y() const
Returns the y-position.
Definition: Position.h:62
static std::ostream & writeFloat(std::ostream &strm, double value)
Writes a float binary.
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
double x() const
Returns the x-position.
Definition: Position.h:57
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:76
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
A class that stores a 2D geometrical boundary.
Definition: Boundary.h:42
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:39
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:119
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
#define VERSION_STRING
Definition: config.h:207
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:69
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:62
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:137
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.