SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOVehicleParameter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Structure representing possible vehicle parameter
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 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 #include "SUMOVehicleParameter.h"
34 #include <utils/common/ToString.h>
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
49  : vtypeid(DEFAULT_VTYPE_ID), color(RGBColor::DEFAULT_COLOR),
50  depart(-1), departProcedure(DEPART_GIVEN),
51  departLane(0), departLaneProcedure(DEPART_LANE_DEFAULT),
52  departPos(0), departPosProcedure(DEPART_POS_DEFAULT),
53  departSpeed(-1), departSpeedProcedure(DEPART_SPEED_DEFAULT),
54  arrivalLane(0), arrivalLaneProcedure(ARRIVAL_LANE_DEFAULT),
55  arrivalPos(0), arrivalPosProcedure(ARRIVAL_POS_DEFAULT),
56  arrivalSpeed(-1), arrivalSpeedProcedure(ARRIVAL_SPEED_DEFAULT),
57  repetitionNumber(-1), repetitionsDone(-1), repetitionOffset(-1),
58  line(), fromTaz(), toTaz(), personCapacity(0), personNumber(0), setParameter(0) {
59 }
60 
61 
62 bool
63 SUMOVehicleParameter::defaultOptionOverrides(const OptionsCont& oc, const std::string& optionName) const {
64  return oc.isSet(optionName) && oc.getBool("defaults-override");
65 }
66 
67 
68 void
69 SUMOVehicleParameter::writeAs(const std::string& xmlElem, OutputDevice& dev,
70  const OptionsCont& oc) const {
71  dev.openTag(xmlElem).writeAttr(SUMO_ATTR_ID, id);
74  }
76 
77  // optional parameter
78  // departlane
79  if (wasSet(VEHPARS_DEPARTLANE_SET) && !defaultOptionOverrides(oc, "departlane")) {
80  std::string val;
81  switch (departLaneProcedure) {
82  case DEPART_LANE_GIVEN:
83  val = toString(departLane);
84  break;
85  case DEPART_LANE_RANDOM:
86  val = "random";
87  break;
88  case DEPART_LANE_FREE:
89  val = "free";
90  break;
92  val = "allowed";
93  break;
95  val = "best";
96  break;
98  default:
99  break;
100  }
102  } else if (oc.isSet("departlane")) {
103  dev.writeAttr(SUMO_ATTR_DEPARTLANE, oc.getString("departlane"));
104  }
105  // departpos
106  if (wasSet(VEHPARS_DEPARTPOS_SET) && !defaultOptionOverrides(oc, "departpos")) {
107  std::string val;
108  switch (departPosProcedure) {
109  case DEPART_POS_GIVEN:
110  val = toString(departPos);
111  break;
112  case DEPART_POS_RANDOM:
113  val = "random";
114  break;
116  val = "random_free";
117  break;
118  case DEPART_POS_FREE:
119  val = "free";
120  break;
122  val = "pwagSimple";
123  break;
125  val = "pwagGeneric";
126  break;
128  val = "maxSpeedGap";
129  break;
130  case DEPART_POS_BASE:
131  val = "base";
132  break;
133  case DEPART_POS_DEFAULT:
134  default:
135  break;
136  }
137  dev.writeAttr(SUMO_ATTR_DEPARTPOS, val);
138  } else if (oc.isSet("departpos")) {
139  dev.writeAttr(SUMO_ATTR_DEPARTPOS, oc.getString("departpos"));
140  }
141  // departspeed
142  if (wasSet(VEHPARS_DEPARTSPEED_SET) && !defaultOptionOverrides(oc, "departspeed")) {
143  std::string val;
144  switch (departSpeedProcedure) {
145  case DEPART_SPEED_GIVEN:
146  val = toString(departSpeed);
147  break;
148  case DEPART_SPEED_RANDOM:
149  val = "random";
150  break;
151  case DEPART_SPEED_MAX:
152  val = "max";
153  break;
155  default:
156  break;
157  }
159  } else if (oc.isSet("departspeed")) {
160  dev.writeAttr(SUMO_ATTR_DEPARTSPEED, oc.getString("departspeed"));
161  }
162 
163  // arrivallane
164  if (wasSet(VEHPARS_ARRIVALLANE_SET) && !defaultOptionOverrides(oc, "arrivallane")) {
165  std::string val;
166  switch (arrivalLaneProcedure) {
167  case ARRIVAL_LANE_GIVEN:
168  val = toString(arrivalLane);
169  break;
171  val = "current";
172  break;
174  default:
175  break;
176  }
178  } else if (oc.isSet("arrivallane")) {
179  dev.writeAttr(SUMO_ATTR_ARRIVALLANE, oc.getString("arrivallane"));
180  }
181  // arrivalpos
182  if (wasSet(VEHPARS_ARRIVALPOS_SET) && !defaultOptionOverrides(oc, "arrivalpos")) {
183  std::string val;
184  switch (arrivalPosProcedure) {
185  case ARRIVAL_POS_GIVEN:
186  val = toString(arrivalPos);
187  break;
188  case ARRIVAL_POS_RANDOM:
189  val = "random";
190  break;
191  case ARRIVAL_POS_MAX:
192  val = "max";
193  break;
194  case ARRIVAL_POS_DEFAULT:
195  default:
196  break;
197  }
199  } else if (oc.isSet("arrivalpos")) {
200  dev.writeAttr(SUMO_ATTR_ARRIVALPOS, oc.getString("arrivalpos"));
201  }
202  // arrivalspeed
203  if (wasSet(VEHPARS_ARRIVALSPEED_SET) && !defaultOptionOverrides(oc, "arrivalspeed")) {
204  std::string val;
205  switch (arrivalSpeedProcedure) {
206  case ARRIVAL_SPEED_GIVEN:
207  val = toString(arrivalSpeed);
208  break;
210  val = "current";
211  break;
213  default:
214  break;
215  }
217  } else if (oc.isSet("arrivalspeed")) {
218  dev.writeAttr(SUMO_ATTR_ARRIVALSPEED, oc.getString("arrivalspeed"));
219  }
220 
221  // color
222  if (wasSet(VEHPARS_COLOR_SET)) {
224  }
225  // repetition values
228  }
230 #ifdef HAVE_SUBSECOND_TIMESTEPS
232 #else
234 #endif
235  }
236  if (wasSet(VEHPARS_LINE_SET)) {
238  }
239  if (wasSet(VEHPARS_TAZ_SET)) {
241  }
244  }
247  }
248  dev.closeOpener();
249 }
250 
251 
252 bool
253 SUMOVehicleParameter::parseDepartLane(const std::string& val, const std::string& element, const std::string& id,
254  int& lane, DepartLaneDefinition& dld, std::string& error) {
255  bool ok = true;
256  if (val == "random") {
257  dld = DEPART_LANE_RANDOM;
258  } else if (val == "free") {
259  dld = DEPART_LANE_FREE;
260  } else if (val == "allowed") {
262  } else if (val == "best") {
263  dld = DEPART_LANE_BEST_FREE;
264  } else {
265  try {
266  lane = TplConvert::_2int(val.c_str());
267  dld = DEPART_LANE_GIVEN;
268  if (lane < 0) {
269  ok = false;
270  }
271  } catch (...) {
272  ok = false;
273  }
274  }
275  if (!ok) {
276  error = "Invalid departLane definition for " + element + " '" + id + "';\n must be one of (\"random\", \"free\", \"allowed\", \"best\", or an int>0)";
277  }
278  return ok;
279 }
280 
281 
282 bool
283 SUMOVehicleParameter::parseDepartPos(const std::string& val, const std::string& element, const std::string& id,
284  SUMOReal& pos, DepartPosDefinition& dpd, std::string& error) {
285  bool ok = true;
286  if (val == "random") {
287  dpd = DEPART_POS_RANDOM;
288  } else if (val == "random_free") {
290  } else if (val == "free") {
291  dpd = DEPART_POS_FREE;
292  } else if (val == "base") {
293  dpd = DEPART_POS_BASE;
294  } else if (val == "pwagSimple") {
296  } else if (val == "pwagGeneric") {
298  } else if (val == "maxSpeedGap") {
300  } else {
301  try {
302  pos = TplConvert::_2SUMOReal(val.c_str());
303  dpd = DEPART_POS_GIVEN;
304  } catch (...) {
305  ok = false;
306  }
307  }
308  if (!ok) {
309  error = "Invalid departPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"random_free\", \"free\", \"base\", \"pwagSimple\", \"pwagGeneric\", \"maxSpeedGap\", or a float)";
310  }
311  return ok;
312 }
313 
314 
315 bool
316 SUMOVehicleParameter::parseDepartSpeed(const std::string& val, const std::string& element, const std::string& id,
317  SUMOReal& speed, DepartSpeedDefinition& dsd, std::string& error) {
318  bool ok = true;
319  if (val == "random") {
320  dsd = DEPART_SPEED_RANDOM;
321  } else if (val == "max") {
322  dsd = DEPART_SPEED_MAX;
323  } else {
324  try {
325  speed = TplConvert::_2SUMOReal(val.c_str());
326  dsd = DEPART_SPEED_GIVEN;
327  if (speed < 0) {
328  ok = false;
329  }
330  } catch (...) {
331  ok = false;
332  }
333  }
334  if (!ok) {
335  error = "Invalid departSpeed definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float>0)";
336  }
337  return ok;
338 }
339 
340 
341 bool
342 SUMOVehicleParameter::parseArrivalLane(const std::string& val, const std::string& element, const std::string& id,
343  int& lane, ArrivalLaneDefinition& ald, std::string& error) {
344  bool ok = true;
345  if (val == "current") {
346  ald = ARRIVAL_LANE_CURRENT;
347  } else {
348  try {
349  lane = TplConvert::_2int(val.c_str());
350  ald = ARRIVAL_LANE_GIVEN;
351  if (lane < 0) {
352  ok = false;
353  }
354  } catch (...) {
355  ok = false;
356  }
357  }
358  if (!ok) {
359  error = "Invalid arrivalLane definition for " + element + " '" + id + "';\n must be one of (\"current\", or int>0)";
360  }
361  return ok;
362 }
363 
364 
365 bool
366 SUMOVehicleParameter::parseArrivalPos(const std::string& val, const std::string& element, const std::string& id,
367  SUMOReal& pos, ArrivalPosDefinition& apd, std::string& error) {
368  bool ok = true;
369  if (val == "random") {
370  apd = ARRIVAL_POS_RANDOM;
371  } else if (val == "max") {
372  apd = ARRIVAL_POS_MAX;
373  } else {
374  try {
375  pos = TplConvert::_2SUMOReal(val.c_str());
376  apd = ARRIVAL_POS_GIVEN;
377  } catch (...) {
378  ok = false;
379  }
380  }
381  if (!ok) {
382  error = "Invalid arrivalPos definition for " + element + " '" + id + "';\n must be one of (\"random\", \"max\", or a float)";
383  }
384  return ok;
385 }
386 
387 
388 bool
389 SUMOVehicleParameter::parseArrivalSpeed(const std::string& val, const std::string& element, const std::string& id,
390  SUMOReal& speed, ArrivalSpeedDefinition& asd, std::string& error) {
391  bool ok = true;
392  if (val == "current") {
393  asd = ARRIVAL_SPEED_CURRENT;
394  } else {
395  try {
396  speed = TplConvert::_2SUMOReal(val.c_str());
397  asd = ARRIVAL_SPEED_GIVEN;
398  } catch (...) {
399  ok = false;
400  }
401  }
402  if (!ok) {
403  error = "Invalid arrivalSpeed definition for " + element + " '" + id + "';\n must be one of (\"current\", or a float>0)";
404  }
405  return ok;
406 }
407 
408 
409 SUMOReal
410 SUMOVehicleParameter::interpretEdgePos(SUMOReal pos, SUMOReal maximumValue, SumoXMLAttr attr, const std::string& id) {
411  if (pos < 0) {
412  pos = maximumValue + pos;
413  }
414  if (pos > maximumValue) {
415  WRITE_WARNING("Invalid " + toString(attr) + " " + toString(pos) + " given for " + id + ". Inserting at edge end instead.");
416  pos = maximumValue;
417  }
418  return pos;
419 }
420 
421 /****************************************************************************/
422