/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ namespace OpenTraffic.Model.SpeedFunctions { using System; using System.Xml.Serialization; [Serializable] public class DensityPartlyLinear : SpeedFlow { [XmlIgnore] public override float MinSpeed { get { return vMin; } } [XmlIgnore] public override float MaxDensity { get { return points != null ? points[points.Length - 1].Density : 0; } } [XmlIgnore] private SpeedFlowPoint[] points; public DensityPartlyLinear() : base(null, null) { } public DensityPartlyLinear(string name, string description) : base(name, description) { } public DensityPartlyLinear(string name, string description, float sMax, float sMin, SpeedFlowPoint[] points) : base(name, description) { this.vMax = sMax; this.vMin = sMin; this.points = points; } public override float GetSpeedByDensity(float density) { SpeedFlowPoint max = null, min = null; min = new SpeedFlowPoint(0, vMax); foreach (SpeedFlowPoint p in points) { if (density <= p.Density) { max = p; break; } min = p; } if (max == null) { min = points[points.Length - 2]; max = points[points.Length - 1]; } System.Diagnostics.Debug.Assert((min != null) && (max != null)); // This could be optimized by using lookup for. We just need to be careful // with the maxcapacity case. // (max.Flow-min.Flow)*(max.Speed-min.Speed) float speed = min.Speed + (max.Speed - min.Speed) * (density - min.Density) / (max.Density - min.Density); speed = Math.Max(speed, vMin); return speed; } } }