5 using System.Collections.Generic;
9 using System.Windows.Threading;
36 private DateTime dateTimeOfLastSimStep;
43 private bool wasSingleStepOrMaxSpeed;
48 private DateTime dateTimeOfLastPause;
60 protected override void Render(Point currentMouseLocation)
62 if (cellularTopology != null)
67 RenderSimulationTraffic(
Rendering.Continuous);
71 RenderSimulationTraffic(
Rendering.Single);
81 using (DrawingContext dc = visual.RenderOpen())
85 Pen carPen =
new Pen(Brushes.Green, 2.0 * PixelsPerMeter);
86 Pen gpsCarPen =
new Pen(Brushes.LightGreen, 2.0 * PixelsPerMeter);
87 DateTime dateTimeNow = DateTime.Now;
88 foreach (KeyValuePair<Car, Cell> frontCellWithCar
in 91 Car car = frontCellWithCar.Key;
92 Cell previousFirstCell = frontCellWithCar.Value;
98 foreach (
int carToRender
in 102 DrawCar(rendering, dc, dateTimeNow, 0, 4,
125 private void DrawCar(
Rendering rendering, DrawingContext dc, DateTime dateTimeNow,
int carOutsideOfTopology,
126 int carLength, Point previousLocation, Point currentLocation, Vector carDirectionNormalizedVector,
132 double timePortionElapsed =
133 (double)((dateTimeNow - dateTimeOfLastSimStep)
135 cellularTopology.
Parameters.CellularTopologyParameters.P2_SimulationStepInterval.Ticks;
136 timePortionElapsed = Math.Min(timePortionElapsed, 1.0);
137 location = previousLocation + (currentLocation + carDirectionNormalizedVector * carOutsideOfTopology *
138 cellularTopology.
Parameters.CellularTopologyParameters.P1_CellLength - previousLocation) *
143 location = currentLocation;
147 throw new ApplicationException(
"Not all of rendering types are implemented.");
151 if ((location - previousLocation).Length - (currentLocation - previousLocation).Length <=
152 carLength * cellularTopology.
Parameters.CellularTopologyParameters.P1_CellLength)
154 dc.DrawLine(carPen, TransformRealWorldPoint(location - carLength * carDirectionNormalizedVector),
155 TransformRealWorldPoint(location));
164 void simulation_SimulationPaused(
object sender, EventArgs e)
167 Application.Current.Dispatcher.Invoke(() => RenderSimulationTraffic(), DispatcherPriority.Render);
174 private void PauseApplied()
176 dateTimeOfLastPause = DateTime.Now;
183 private void PauseReleased()
185 dateTimeOfLastSimStep += DateTime.Now - dateTimeOfLastPause;
193 internal void SimulationFaster(
double rate)
197 dateTimeOfLastSimStep += (GetCurrentDateTime() - dateTimeOfLastSimStep).Divide(rate);
206 internal void SimulationSlower(
double rate)
210 DateTime dateTimeNow = GetCurrentDateTime();
211 dateTimeOfLastSimStep = dateTimeNow - (dateTimeNow - dateTimeOfLastSimStep).Multiply(rate);
219 private DateTime GetCurrentDateTime()
224 return dateTimeOfLastPause;
226 throw new ApplicationException(
"Simulation speed increased when simulation is not running");
238 wasSingleStepOrMaxSpeed =
true;
242 if (wasSingleStepOrMaxSpeed)
243 AdjustLastSimulationStepTime();
244 wasSingleStepOrMaxSpeed =
false;
252 private void AdjustLastSimulationStepTime()
254 dateTimeOfLastSimStep = DateTime.Now -
255 cellularTopology.
Parameters.CellularTopologyParameters.P2_SimulationStepInterval;
261 internal void StepForward()
263 wasSingleStepOrMaxSpeed =
true;
271 RenderSimulationTraffic();
277 internal void Pause()
280 RenderSimulationTraffic(
Rendering.Single);
282 RenderSimulationTraffic(
Rendering.Continuous);
285 wasSingleStepOrMaxSpeed =
true;
291 internal void DetachEventHandlerFromCellularTopology()
293 if (cellularTopology != null)
297 Simulation_SingleSimulationActionPerformed;
301 using (DrawingContext dc = visual.RenderOpen()) { }
308 internal void AttachEventHandlerFromCellularTopology()
322 void cellularTopology_NextTransitionFunctionStarted(
object sender,
DateTimeEventArgs e)
332 void Simulation_IsMaxSimulationSpeedChange(
object sender, EventArgs e)
337 wasSingleStepOrMaxSpeed =
true;
339 wasSingleStepOrMaxSpeed =
false;
366 private void Simulation_SingleSimulationActionPerformed(
object sender, EventArgs e)
368 Application.Current.Dispatcher.Invoke(() => RenderSimulationTraffic(), DispatcherPriority.Render);
Vector NormalizedDirectionVector
Normalized direction vector.
Simulation Simulation
Simulation object which controls the simulation on the cellular topology.
ReadOnlyDictionary< int, LocationAndDirection > CarCurrentGPSInputLocations
Dictionary mapping hash code of a recorded car to its location in current simulation step...
EventHandler< SimulationSpeedChangeEventArgs > SimulationSpeedChanged
Event of simulation speed change.
Mutex CarsDictionariesMutex
Mutex ensuring that cars are not rendering when the transition function is performing and vice versa...
EventHandler IsMaxSimulationSpeedChange
Event of activation or deactivation of maximum simulation speed.
Base class for drawing visual renderer.
CarsManager CarsManager
Cars manager reference.
EventHandler< DateTimeEventArgs > NextTransitionFunctionStarted
Event informing that next transition function already started.
Simulation traffic renderer.
Class representing event arguments consisting of property containing time information.
DateTime DateTime
Property containing time information.
int Outside
Number of cells which car used if they would exist after the end of the blind lane.
double NewSimulationSpeed
New simulation speed.
Parameters Parameters
Reference to simulation parameters.
SimulationState
Current state of the simulation.
EventHandler SimulationPaused
An event informing about applying of pause by the simulation control.
bool IsMaxSimulationSpeed
Indicates whether the simulation speed is maximal.
Point Location
Location of cell.
ReadOnlyDictionary< Car, Cell > CellsWithCarInPreviousStep
Read only dictionary containing all cars with their first cells which they occupied before last trans...
override void Render(Point currentMouseLocation)
Redraw the simulation traffic while distinguishing whether to use single or continous rendering Curre...
Event args containing old and new simulation speed.
SimulationState SimulationState
Current simulation status of simulation.
Class representing cellular topology.
Rendering
Enumeration type containing values representing rendering of one single simulation step and rendering...
EventHandler SingleSimulationActionPerformed
An event informing about finishing of simulation action.
GpsRecordsManager GpsRecordsManager
Gps records manager reference.
ReadOnlyDictionary< int, LocationAndDirection > CarPreviousGPSInputLocations
Dictionary mapping hash code of a recorded car to its location in previous simulation step...
double SimulationSpeed
Current simulation speed in model time speed divided by computer/user time speed
ReadOnlyDictionary< Car, Cell > CellsWithCarInCurrentStep
Read only dictionary containing all cars with their first cells which they occupied after last transi...
int Length
Car's length in number of cells it uses.
double OldSimulationSpeed
Old simulation speed.