1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2008-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 /****************************************************************************/
10 /// @file    MSMoveReminder.cpp
11 /// @author  Daniel Krajzewicz
12 /// @author  Michael Behrisch
13 /// @author  Jakob Erdmann
14 /// @date    2008-10-27
15 /// @version $Id$
16 ///
17 // Something on a lane to be noticed about vehicle movement
18 /****************************************************************************/
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include "MSLane.h"
27 #include "MSMoveReminder.h"
28 
29 
30 // ===========================================================================
31 // method definitions
32 // ===========================================================================
MSMoveReminder(const std::string & description,MSLane * const lane,const bool doAdd)33 MSMoveReminder::MSMoveReminder(const std::string& description, MSLane* const lane, const bool doAdd) :
34     myLane(lane),
35     myDescription(description) {
36     if (myLane != nullptr && doAdd) {
37         // add reminder to lane
38         myLane->addMoveReminder(this);
39     }
40 }
41 
42 
43 void
updateDetector(SUMOTrafficObject & veh,double entryPos,double leavePos,SUMOTime entryTime,SUMOTime currentTime,SUMOTime leaveTime,bool cleanUp)44 MSMoveReminder::updateDetector(SUMOTrafficObject& veh, double entryPos, double leavePos,
45                                SUMOTime entryTime, SUMOTime currentTime, SUMOTime leaveTime,
46                                bool cleanUp) {
47     // each vehicle is tracked linearly across its segment. For each vehicle,
48     // the time and position of the previous call are maintained and only
49     // the increments are sent to notifyMoveInternal
50     if (entryTime > currentTime) {
51         return; // calibrator may insert vehicles a tiny bit into the future; ignore those
52     }
53     auto j = myLastVehicleUpdateValues.find(&veh);
54     if (j != myLastVehicleUpdateValues.end()) {
55         // the vehicle already has reported its values before; use these
56         // however, if this was called from prepareDetectorForWriting the time
57         // only has a resolution of DELTA_T and might be invalid
58         const SUMOTime previousEntryTime = j->second.first;
59         if (previousEntryTime <= currentTime) {
60             entryTime = previousEntryTime;
61             entryPos = j->second.second;
62         }
63     }
64     assert(entryTime <= currentTime);
65     if ((entryTime < leaveTime) && (entryPos < leavePos)) {
66         const double timeOnLane = STEPS2TIME(currentTime - entryTime);
67         const double speed = (leavePos - entryPos) / STEPS2TIME(leaveTime - entryTime);
68         myLastVehicleUpdateValues[&veh] = std::pair<SUMOTime, double>(currentTime, entryPos + speed * timeOnLane);
69         assert(timeOnLane >= 0);
70         assert(speed >= 0);
71         notifyMoveInternal(veh, timeOnLane, timeOnLane, speed, speed, speed * timeOnLane, speed * timeOnLane, 0.);
72     } else {
73         // it would be natrual to
74         // assert(entryTime == leaveTime);
75         // assert(entryPos == leavePos);
76         // However, in the presence of calibrators, vehicles may jump a bit
77         myLastVehicleUpdateValues[&veh] = std::pair<SUMOTime, double>(leaveTime, leavePos);
78     }
79     if (cleanUp) {
80         // clean up after the vehicle has left the area of this reminder
81         removeFromVehicleUpdateValues(veh);
82     }
83 }
84 
85 
86 void
removeFromVehicleUpdateValues(SUMOTrafficObject & veh)87 MSMoveReminder::removeFromVehicleUpdateValues(SUMOTrafficObject& veh) {
88     myLastVehicleUpdateValues.erase(&veh);
89 }
90 /****************************************************************************/
91 
92