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 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54 }
55 
56 
57 void
60  FileHelpers::writeByte(into, 2);
63  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
64  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
67 }
68 
69 
70 void
71 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
73  FileHelpers::writeInt(into, (int)list.size());
74  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
76  FileHelpers::writeString(into, *it);
77  }
78 }
79 
80 
81 bool
83  const std::string& rootElement,
84  const std::map<SumoXMLAttr, std::string>& attrs) {
85  if (myXMLStack.empty()) {
86  writeStaticHeader(into);
87  writeStringList(into, std::vector<std::string>());
88  writeStringList(into, std::vector<std::string>());
89  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
90  openTag(into, rootElement);
91  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
92  writeAttr(into, it->first, it->second);
93  }
94  return true;
95  }
96  }
97  return false;
98 }
99 
100 
101 void
102 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
103  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
104  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
105  }
106 }
107 
108 
109 void
110 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
111  myXMLStack.push_back(xmlElement);
113  const int tagNum = (int)xmlElement;
114  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum % 256));
115  FileHelpers::writeByte(into, static_cast<unsigned char>(tagNum / 256));
116 }
117 
118 
119 bool
120 BinaryFormatter::closeTag(std::ostream& into) {
121  if (!myXMLStack.empty()) {
123  myXMLStack.pop_back();
124  return true;
125  }
126  return false;
127 }
128 
129 
130 template<>
131 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
133  FileHelpers::writeByte(into, val);
134 }
135 
136 
137 template<>
138 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
139  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
141  FileHelpers::writeInt(into, int(val * 100. + .5));
142  } else {
144  FileHelpers::writeFloat(into, val);
145  }
146 }
147 
148 
149 template<>
150 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
152  FileHelpers::writeInt(into, val);
153 }
154 
155 
156 template<>
157 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
159  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
160 }
161 
162 
163 template<>
164 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
166  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
167 }
168 
169 
170 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
171  if (val.z() != 0.) {
172  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
173  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
175  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
176  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
177  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
178  } else {
180  FileHelpers::writeFloat(into, val.x());
181  FileHelpers::writeFloat(into, val.y());
182  FileHelpers::writeFloat(into, val.z());
183  }
184  } else {
185  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
186  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
188  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
189  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
190  } else {
192  FileHelpers::writeFloat(into, val.x());
193  FileHelpers::writeFloat(into, val.y());
194  }
195  }
196 }
197 
198 
199 template<>
200 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
202  writePosition(into, val);
203 }
204 
205 
206 template<>
207 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
209  FileHelpers::writeInt(into, static_cast<int>(val.size()));
210  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
211  writePosition(into, *pos);
212  }
213 }
214 
215 
216 template<>
217 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
219  FileHelpers::writeFloat(into, val.xmin());
220  FileHelpers::writeFloat(into, val.ymin());
221  FileHelpers::writeFloat(into, val.xmax());
222  FileHelpers::writeFloat(into, val.ymax());
223 }
224 
225 
226 template<>
227 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
229  FileHelpers::writeByte(into, val.red());
230  FileHelpers::writeByte(into, val.green());
231  FileHelpers::writeByte(into, val.blue());
232  FileHelpers::writeByte(into, val.alpha());
233 }
234 
235 
236 template<>
237 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
239  FileHelpers::writeInt(into, (int)val.size());
240  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
242  FileHelpers::writeInt(into, *it);
243  }
244 }
245 
246 
247 /****************************************************************************/
SumoXMLTag
Numbers representing SUMO-XML - element names.
static StringBijection< SumoXMLNodeType > NodeTypes
static void writeStaticHeader(std::ostream &into)
writes the part of the header which is always unchanged.
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:142
bool closeTag(std::ostream &into)
Closes the most recently opened tag.
static std::ostream & writeFloat(std::ostream &strm, SUMOReal value)
Writes a float binary.
SUMOReal xmin() const
Returns minimum x-coordinate.
Definition: Boundary.cpp:130
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
SUMOReal xmax() const
Returns maximum x-coordinate.
Definition: Boundary.cpp:136
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.
unsigned char blue() const
Returns the blue-amount of the color.
Definition: RGBColor.h:91
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.
unsigned char alpha() const
Returns the alpha-amount of the color.
Definition: RGBColor.h:99
BinaryFormatter()
Constructor.
SUMOReal z() const
Returns the z-position.
Definition: Position.h:73
static std::ostream & writeInt(std::ostream &strm, int value)
Writes an integer binary.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
#define VERSION_STRING
Definition: version.h:1
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 ...
static StringBijection< int > Attrs
The names of SUMO-XML attributes for use in netbuild.
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
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 green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:208
static void writePosition(std::ostream &into, const Position &val)
writes a position
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:148
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
edge functions
unsigned char red() const
Returns the red-amount of the color.
Definition: RGBColor.h:75
static std::ostream & writeString(std::ostream &strm, const std::string &value)
Writes a string binary.