SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
12 // Copyright (C) 2012-2014 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
58 BinaryFormatter::writeStringList(std::ostream& into, const std::vector<std::string>& list) {
60  FileHelpers::writeInt(into, (int)list.size());
61  for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it) {
63  FileHelpers::writeString(into, *it);
64  }
65 }
66 
67 bool
69  const std::string& rootElement,
70  const std::string& /* attrs */,
71  const std::string& /* comment */) {
72  if (myXMLStack.empty()) {
74  FileHelpers::writeByte(into, 1);
77  writeStringList(into, SUMOXMLDefinitions::Tags.getStrings());
78  writeStringList(into, SUMOXMLDefinitions::Attrs.getStrings());
81  writeStringList(into, std::vector<std::string>());
82  writeStringList(into, std::vector<std::string>());
83 
84  if (SUMOXMLDefinitions::Tags.hasString(rootElement)) {
85  openTag(into, rootElement);
86  return true;
87  }
88  }
89  return false;
90 }
91 
92 
93 void
94 BinaryFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
95  if (SUMOXMLDefinitions::Tags.hasString(xmlElement)) {
96  openTag(into, (const SumoXMLTag)(SUMOXMLDefinitions::Tags.get(xmlElement)));
97  }
98 }
99 
100 
101 void
102 BinaryFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
103  myXMLStack.push_back(xmlElement);
105  FileHelpers::writeByte(into, static_cast<unsigned char>(xmlElement));
106 }
107 
108 
109 bool
110 BinaryFormatter::closeTag(std::ostream& into) {
111  if (!myXMLStack.empty()) {
113  FileHelpers::writeByte(into, static_cast<unsigned char>(myXMLStack.back()));
114  myXMLStack.pop_back();
115  return true;
116  }
117  return false;
118 }
119 
120 
121 template<>
122 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const bool& val) {
124  FileHelpers::writeByte(into, val);
125 }
126 
127 
128 template<>
129 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SUMOReal& val) {
130  if (into.precision() == 2 && val < 2e7 && val > -2e7) { // 2e7 is roughly INT_MAX/100
132  FileHelpers::writeInt(into, int(val * 100. + .5));
133  } else {
135  FileHelpers::writeFloat(into, val);
136  }
137 }
138 
139 
140 template<>
141 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const int& val) {
143  FileHelpers::writeInt(into, val);
144 }
145 
146 
147 template<>
148 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const unsigned int& val) {
150  FileHelpers::writeInt(into, val);
151 }
152 
153 
154 template<>
155 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLNodeType& val) {
157  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
158 }
159 
160 
161 template<>
162 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const SumoXMLEdgeFunc& val) {
164  FileHelpers::writeByte(into, static_cast<unsigned char>(val));
165 }
166 
167 
168 void BinaryFormatter::writePosition(std::ostream& into, const Position& val) {
169  if (val.z() != 0.) {
170  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
171  val.y() < 2e7 && val.y() > -2e7 && val.z() < 2e7 && val.z() > -2e7) { // 2e7 is roughly INT_MAX/100
173  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
174  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
175  FileHelpers::writeInt(into, int(val.z() * 100. + .5));
176  } else {
178  FileHelpers::writeFloat(into, val.x());
179  FileHelpers::writeFloat(into, val.y());
180  FileHelpers::writeFloat(into, val.z());
181  }
182  } else {
183  if (into.precision() == 2 && val.x() < 2e7 && val.x() > -2e7 &&
184  val.y() < 2e7 && val.y() > -2e7) { // 2e7 is roughly INT_MAX/100
186  FileHelpers::writeInt(into, int(val.x() * 100. + .5));
187  FileHelpers::writeInt(into, int(val.y() * 100. + .5));
188  } else {
190  FileHelpers::writeFloat(into, val.x());
191  FileHelpers::writeFloat(into, val.y());
192  }
193  }
194 }
195 
196 
197 template<>
198 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Position& val) {
200  FileHelpers::writeByte(into, static_cast<unsigned char>(attr));
201  writePosition(into, val);
202 }
203 
204 
205 template<>
206 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const PositionVector& val) {
208  FileHelpers::writeInt(into, static_cast<int>(val.size()));
209  for (PositionVector::const_iterator pos = val.begin(); pos != val.end(); ++pos) {
210  writePosition(into, *pos);
211  }
212 }
213 
214 
215 template<>
216 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const Boundary& val) {
218  FileHelpers::writeFloat(into, val.xmin());
219  FileHelpers::writeFloat(into, val.ymin());
220  FileHelpers::writeFloat(into, val.xmax());
221  FileHelpers::writeFloat(into, val.ymax());
222 }
223 
224 
225 template<>
226 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const RGBColor& val) {
228  FileHelpers::writeByte(into, val.red());
229  FileHelpers::writeByte(into, val.green());
230  FileHelpers::writeByte(into, val.blue());
231  FileHelpers::writeByte(into, val.alpha());
232 }
233 
234 
235 template<>
236 void BinaryFormatter::writeAttr(std::ostream& into, const SumoXMLAttr /* attr */, const std::vector<int>& val) {
238  FileHelpers::writeInt(into, (int)val.size());
239  for (std::vector<int>::const_iterator it = val.begin(); it != val.end(); ++it) {
241  FileHelpers::writeInt(into, *it);
242  }
243 }
244 
245 
246 /****************************************************************************/
SumoXMLTag
Numbers representing SUMO-XML - element names.
static StringBijection< SumoXMLNodeType > NodeTypes
SUMOReal ymin() const
Returns minimum y-coordinate.
Definition: Boundary.cpp:124
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:112
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:118
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
static void writeAttrHeader(std::ostream &into, const SumoXMLAttr attr, const DataType type)
writes the header for an arbitrary attribute
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.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
static std::ostream & writeByte(std::ostream &strm, unsigned char value)
Writes a byte binary.
#define VERSION_STRING
Definition: config.h:227
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...
unsigned char green() const
Returns the green-amount of the color.
Definition: RGBColor.h:83
#define SUMOReal
Definition: config.h:215
static void writePosition(std::ostream &into, const Position &val)
writes a position
SUMOReal ymax() const
Returns maximum y-coordinate.
Definition: Boundary.cpp:130
static StringBijection< int > Tags
The names of SUMO-XML elements for use in netbuild.
static StringBijection< SumoXMLEdgeFunc > EdgeFunctions
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.