SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NLDetectorBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Builds detectors for microsim
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
14 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
36 #include <iostream>
37 #include <microsim/MSNet.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSEdge.h>
49 #include <microsim/MSGlobals.h>
57 #include "NLDetectorBuilder.h"
59 
60 #ifdef HAVE_INTERNAL
61 #include <mesosim/MEInductLoop.h>
62 #include <mesosim/MELoop.h>
63 #include <mesosim/MESegment.h>
64 #endif
65 
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // method definitions
73 // ===========================================================================
74 /* -------------------------------------------------------------------------
75  * NLDetectorBuilder::E3DetectorDefinition-methods
76  * ----------------------------------------------------------------------- */
78  const std::string& device, SUMOReal haltingSpeedThreshold,
79  SUMOTime haltingTimeThreshold, int splInterval)
80  : myID(id), myDevice(device),
81  myHaltingSpeedThreshold(haltingSpeedThreshold),
82  myHaltingTimeThreshold(haltingTimeThreshold),
83  mySampleInterval(splInterval) {}
84 
85 
87 
88 
89 /* -------------------------------------------------------------------------
90  * NLDetectorBuilder-methods
91  * ----------------------------------------------------------------------- */
93  : myNet(net), myE3Definition(0) {}
94 
95 
97 
98 
99 void
101  const std::string& lane, SUMOReal pos, int splInterval,
102  const std::string& device, bool friendlyPos, bool splitByType) {
103  checkSampleInterval(splInterval, SUMO_TAG_E1DETECTOR, id);
104  // get and check the lane
105  MSLane* clane = getLaneChecking(lane, SUMO_TAG_E1DETECTOR, id);
106  if (!MSGlobals::gUseMesoSim) {
107  // get and check the position
108  pos = getPositionChecking(pos, clane, friendlyPos, id);
109  // build the loop
110  MSDetectorFileOutput* loop = createInductLoop(id, clane, pos, splitByType);
111  // add the file output
112  myNet.getDetectorControl().add(SUMO_TAG_INDUCTION_LOOP, loop, device, splInterval);
113  } else {
114 #ifdef HAVE_INTERNAL
115  if (pos < 0) {
116  pos = clane->getLength() + pos;
117  }
118  MESegment* s = MSGlobals::gMesoNet->getSegmentForEdge(clane->getEdge());
119  MESegment* prev = s;
120  SUMOReal cpos = 0;
121  while (cpos + prev->getLength() < pos && s != 0) {
122  prev = s;
123  cpos += s->getLength();
124  s = s->getNextSegment();
125  }
126  SUMOReal rpos = pos - cpos; //-prev->getLength();
127  if (rpos > prev->getLength() || rpos < 0) {
128  if (friendlyPos) {
129  rpos = prev->getLength() - (SUMOReal) 0.1;
130  } else {
131  throw InvalidArgument("The position of detector '" + id + "' lies beyond the lane's '" + lane + "' length.");
132  }
133  }
134  MEInductLoop* loop =
135  createMEInductLoop(id, prev, rpos);
136  myNet.getDetectorControl().add(SUMO_TAG_INDUCTION_LOOP, loop, device, splInterval);
137 #endif
138  }
139 }
140 
141 
142 void
144  const std::string& lane, SUMOReal pos,
145  const std::string& device, bool friendlyPos) {
146  // get and check the lane
148  // get and check the position
149  pos = getPositionChecking(pos, clane, friendlyPos, id);
150  // build the loop
151  MSDetectorFileOutput* loop = createInstantInductLoop(id, clane, pos, device);
152  // add the file output
154 }
155 
156 
157 void
159  const std::string& lane, SUMOReal pos, SUMOReal length,
160  bool cont, int splInterval,
161  const std::string& device,
162  SUMOTime haltingTimeThreshold,
163  SUMOReal haltingSpeedThreshold,
164  SUMOReal jamDistThreshold, bool friendlyPos) {
165  checkSampleInterval(splInterval, SUMO_TAG_E2DETECTOR, id);
166  MSLane* clane = getLaneChecking(lane, SUMO_TAG_E2DETECTOR, id);
167  // check whether the detector may lie over more than one lane
168  MSDetectorFileOutput* det = 0;
169  if (!cont) {
170  convUncontE2PosLength(id, clane, pos, length, friendlyPos);
171  det = buildSingleLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
172  myNet.getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, det, device, splInterval);
173  } else {
174  convContE2PosLength(id, clane, pos, length, friendlyPos);
175  det = buildMultiLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
176  myNet.getDetectorControl().add(SUMO_TAG_LANE_AREA_DETECTOR, det, device, splInterval);
177  }
178 }
179 
180 
181 void
183  const std::string& lane, SUMOReal pos, SUMOReal length,
184  bool cont,
186  const std::string& device,
187  SUMOTime haltingTimeThreshold,
188  SUMOReal haltingSpeedThreshold,
189  SUMOReal jamDistThreshold, bool friendlyPos) {
190  if (tlls.getActive() == 0) {
191  throw InvalidArgument("The detector '" + id + "' refers to the unknown lsa.");
192  }
193  MSLane* clane = getLaneChecking(lane, SUMO_TAG_E2DETECTOR, id);
194  // check whether the detector may lie over more than one lane
195  MSDetectorFileOutput* det = 0;
196  if (!cont) {
197  convUncontE2PosLength(id, clane, pos, length, friendlyPos);
198  det = buildSingleLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
200  } else {
201  convContE2PosLength(id, clane, pos, length, friendlyPos);
202  det = buildMultiLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
204  }
205  // add the file output
207 }
208 
209 
210 void
212  const std::string& lane, SUMOReal pos, SUMOReal length,
213  bool cont,
215  const std::string& tolane,
216  const std::string& device,
217  SUMOTime haltingTimeThreshold,
218  SUMOReal haltingSpeedThreshold,
219  SUMOReal jamDistThreshold, bool friendlyPos) {
220  if (tlls.getActive() == 0) {
221  throw InvalidArgument("The detector '" + id + "' refers to the unknown lsa.");
222  }
223  MSLane* clane = getLaneChecking(lane, SUMO_TAG_E2DETECTOR, id);
224  MSLane* ctoLane = getLaneChecking(tolane, SUMO_TAG_E2DETECTOR, id);
225  MSLink* link = MSLinkContHelper::getConnectingLink(*clane, *ctoLane);
226  if (link == 0) {
227  throw InvalidArgument(
228  "The detector output can not be build as no connection between lanes '"
229  + lane + "' and '" + tolane + "' exists.");
230  }
231  if (pos < 0) {
232  pos = -pos;
233  }
234  // check whether the detector may lie over more than one lane
235  MSDetectorFileOutput* det = 0;
236  if (!cont) {
237  convUncontE2PosLength(id, clane, pos, length, friendlyPos);
238  det = buildSingleLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
240  } else {
241  convContE2PosLength(id, clane, pos, length, friendlyPos);
242  det = buildMultiLaneE2Det(id, DU_USER_DEFINED, clane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
244  }
245  // add the file output
247 }
248 
249 
250 void
251 NLDetectorBuilder::convUncontE2PosLength(const std::string& id, MSLane* clane,
252  SUMOReal& pos, SUMOReal& length,
253  bool friendlyPos) {
254  // get and check the position
255  pos = getPositionChecking(pos, clane, friendlyPos, id);
256  // check length
257  if (length < 0) {
258  length = clane->getLength() + length;
259  }
260  if (length + pos > clane->getLength()) {
261  if (friendlyPos) {
262  length = clane->getLength() - pos - (SUMOReal) 0.1;
263  } else {
264  throw InvalidArgument("The length of detector '" + id + "' lies beyond the lane's '" + clane->getID() + "' length.");
265  }
266  }
267  if (length < 0) {
268  if (friendlyPos) {
269  length = (SUMOReal) 0.1;
270  } else {
271  throw InvalidArgument("The length of detector '" + id + "' is almost 0.");
272  }
273  }
274 }
275 
276 
277 void
278 NLDetectorBuilder::convContE2PosLength(const std::string& id, MSLane* clane,
279  SUMOReal& pos, SUMOReal& /*length*/,
280  bool friendlyPos) {
281  // get and check the position
282  pos = getPositionChecking(pos, clane, friendlyPos, id);
283  // length will be kept as is
284 }
285 
286 
287 void
289  const std::string& device, int splInterval,
290  SUMOReal haltingSpeedThreshold,
291  SUMOTime haltingTimeThreshold) {
292  checkSampleInterval(splInterval, SUMO_TAG_E3DETECTOR, id);
293  myE3Definition = new E3DetectorDefinition(id, device, haltingSpeedThreshold, haltingTimeThreshold, splInterval);
294 }
295 
296 
297 void
298 NLDetectorBuilder::addE3Entry(const std::string& lane,
299  SUMOReal pos, bool friendlyPos) {
300  if (myE3Definition == 0) {
301  return;
302  }
304  // get and check the position
305  pos = getPositionChecking(pos, clane, friendlyPos, myE3Definition->myID);
306  // build and save the entry
307  myE3Definition->myEntries.push_back(MSCrossSection(clane, pos));
308 }
309 
310 
311 void
312 NLDetectorBuilder::addE3Exit(const std::string& lane,
313  SUMOReal pos, bool friendlyPos) {
314  if (myE3Definition == 0) {
315  return;
316  }
318  // get and check the position
319  pos = getPositionChecking(pos, clane, friendlyPos, myE3Definition->myID);
320  // build and save the exit
321  myE3Definition->myExits.push_back(MSCrossSection(clane, pos));
322 }
323 
324 
325 std::string
327  if (myE3Definition == 0) {
328  return "<unknown>";
329  }
330  return myE3Definition->myID;
331 }
332 
333 
334 void
336  if (myE3Definition == 0) {
337  return;
338  }
342  // add to net
344  // clean up
345  delete myE3Definition;
346  myE3Definition = 0;
347 }
348 
349 
350 void
352  const std::string& vtype, SUMOTime frequency,
353  const std::string& device) {
355  new MSVTypeProbe(id, vtype, OutputDevice::getDevice(device), frequency);
356 }
357 
358 
359 void
360 NLDetectorBuilder::buildRouteProbe(const std::string& id, const std::string& edge,
361  SUMOTime frequency, SUMOTime begin,
362  const std::string& device) {
365  MSRouteProbe* probe = new MSRouteProbe(id, e, begin);
366  // add the file output
367  myNet.getDetectorControl().add(SUMO_TAG_ROUTEPROBE, probe, device, frequency, begin);
368 }
369 
370 
371 // -------------------
374  DetectorUsage usage,
375  MSLane* lane, SUMOReal pos, SUMOReal length,
376  SUMOTime haltingTimeThreshold,
377  SUMOReal haltingSpeedThreshold,
378  SUMOReal jamDistThreshold) {
379  return createSingleLaneE2Detector(id, usage, lane, pos,
380  length, haltingTimeThreshold, haltingSpeedThreshold,
381  jamDistThreshold);
382 }
383 
384 
387  MSLane* lane, SUMOReal pos, SUMOReal length,
388  SUMOTime haltingTimeThreshold,
389  SUMOReal haltingSpeedThreshold,
390  SUMOReal jamDistThreshold) {
392  lane, pos, haltingTimeThreshold, haltingSpeedThreshold,
393  jamDistThreshold);
394  static_cast<MS_E2_ZS_CollectorOverLanes*>(ret)->init(lane, length);
395  return ret;
396 }
397 
398 
401  MSLane* lane, SUMOReal pos, bool splitByType) {
402  return new MSInductLoop(id, lane, pos, splitByType);
403 }
404 
405 
408  MSLane* lane, SUMOReal pos, const std::string& od) {
409  return new MSInstantInductLoop(id, OutputDevice::getDevice(od), lane, pos);
410 }
411 
412 
413 #ifdef HAVE_INTERNAL
414 MEInductLoop*
415 NLDetectorBuilder::createMEInductLoop(const std::string& id,
416  MESegment* s, SUMOReal pos) {
417  return new MEInductLoop(id, s, pos);
418 }
419 #endif
420 
421 
424  DetectorUsage usage, MSLane* lane, SUMOReal pos, SUMOReal length,
425  SUMOTime haltingTimeThreshold, SUMOReal haltingSpeedThreshold, SUMOReal jamDistThreshold) {
426  return new MSE2Collector(id, usage, lane, pos, length, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
427 }
428 
429 
432  DetectorUsage usage, MSLane* lane, SUMOReal pos,
433  SUMOTime haltingTimeThreshold, SUMOReal haltingSpeedThreshold, SUMOReal jamDistThreshold) {
434  return new MS_E2_ZS_CollectorOverLanes(id, usage, lane, pos, haltingTimeThreshold, haltingSpeedThreshold, jamDistThreshold);
435 }
436 
437 
440  const CrossSectionVector& entries,
441  const CrossSectionVector& exits,
442  SUMOReal haltingSpeedThreshold,
443  SUMOTime haltingTimeThreshold) {
444  return new MSE3Collector(id, entries, exits, haltingSpeedThreshold, haltingTimeThreshold);
445 }
446 
447 
448 SUMOReal
450  const std::string& detid) {
451  // check whether it is given from the end
452  if (pos < 0) {
453  pos = lane->getLength() + pos;
454  }
455  // check whether it is on the lane
456  if (pos > lane->getLength()) {
457  if (friendlyPos) {
458  pos = lane->getLength() - (SUMOReal) 0.1;
459  } else {
460  throw InvalidArgument("The position of detector '" + detid + "' lies beyond the lane's '" + lane->getID() + "' length.");
461  }
462  }
463  if (pos < 0) {
464  if (friendlyPos) {
465  pos = (SUMOReal) 0.1;
466  } else {
467  throw InvalidArgument("The position of detector '" + detid + "' lies beyond the lane's '" + lane->getID() + "' length.");
468  }
469  }
470  return pos;
471 }
472 
473 
474 void
475 NLDetectorBuilder::createEdgeLaneMeanData(const std::string& id, SUMOTime frequency,
476  SUMOTime begin, SUMOTime end, const std::string& type,
477  const bool useLanes, const bool withEmpty, const bool printDefaults,
478  const bool withInternal, const bool trackVehicles,
479  const SUMOReal maxTravelTime, const SUMOReal minSamples,
480  const SUMOReal haltSpeed, const std::string& vTypes,
481  const std::string& device) {
482  if (begin < 0) {
483  throw InvalidArgument("Negative begin time for meandata dump '" + id + "'.");
484  }
485  if (end < 0) {
486  end = SUMOTime_MAX;
487  }
488  if (end <= begin) {
489  throw InvalidArgument("End before or at begin for meandata dump '" + id + "'.");
490  }
491  std::set<std::string> vt;
492  StringTokenizer st(vTypes);
493  while (st.hasNext()) {
494  vt.insert(st.next());
495  }
496  MSMeanData* det = 0;
497  if (type == "" || type == "performance" || type == "traffic") {
498  det = new MSMeanData_Net(id, begin, end, useLanes, withEmpty,
499  printDefaults, withInternal, trackVehicles,
500  maxTravelTime, minSamples, haltSpeed, vt);
501  } else if (type == "hbefa") {
502  det = new MSMeanData_HBEFA(id, begin, end, useLanes, withEmpty,
503  printDefaults, withInternal, trackVehicles,
504  maxTravelTime, minSamples, vt);
505  } else if (type == "harmonoise") {
506  det = new MSMeanData_Harmonoise(id, begin, end, useLanes, withEmpty,
507  printDefaults, withInternal, trackVehicles,
508  maxTravelTime, minSamples, vt);
509  } else {
510  throw InvalidArgument("Invalid type '" + type + "' for meandata dump '" + id + "'.");
511  }
512  if (det != 0) {
513  if (frequency < 0) {
514  frequency = end - begin;
515  }
516  MSNet::getInstance()->getDetectorControl().add(det, device, frequency, begin);
517  }
518 }
519 
520 
521 
522 
523 // ------ Value checking/adapting methods ------
524 MSEdge*
525 NLDetectorBuilder::getEdgeChecking(const std::string& edgeID, SumoXMLTag type,
526  const std::string& detid) {
527  // get and check the lane
528  MSEdge* edge = MSEdge::dictionary(edgeID);
529  if (edge == 0) {
530  throw InvalidArgument("The lane with the id '" + edgeID + "' is not known (while building " + toString(type) + " '" + detid + "').");
531  }
532  return edge;
533 }
534 
535 
536 MSLane*
537 NLDetectorBuilder::getLaneChecking(const std::string& laneID, SumoXMLTag type,
538  const std::string& detid) {
539  // get and check the lane
540  MSLane* lane = MSLane::dictionary(laneID);
541  if (lane == 0) {
542  throw InvalidArgument("The lane with the id '" + laneID + "' is not known (while building " + toString(type) + " '" + detid + "').");
543  }
544  return lane;
545 }
546 
547 
548 void
549 NLDetectorBuilder::checkSampleInterval(int splInterval, SumoXMLTag type, const std::string& id) {
550  if (splInterval < 0) {
551  throw InvalidArgument("Negative sampling frequency (in " + toString(type) + " '" + id + "').");
552  }
553  if (splInterval == 0) {
554  throw InvalidArgument("Sampling frequency must not be zero (in " + toString(type) + " '" + id + "').");
555  }
556 }
557 
558 
559 /****************************************************************************/
560