/// <summary>
        /// Generates the xySegments into segments and inputs them into the input road network
        /// </summary>
        /// <param name="arn"></param>
        /// <returns></returns>
        public ArbiterRoadNetwork GenerateSegments(ArbiterRoadNetwork arn)
        {
            foreach (SimpleSegment ss in segments)
            {
                // seg
                ArbiterSegmentId asi = new ArbiterSegmentId(int.Parse(ss.Id));
                ArbiterSegment   asg = new ArbiterSegment(asi);
                arn.ArbiterSegments.Add(asi, asg);
                asg.RoadNetwork = arn;
                asg.SpeedLimits = new ArbiterSpeedLimit();
                asg.SpeedLimits.MaximumSpeed = 13.4112;                 // 30mph max speed

                // way1
                ArbiterWayId awi1 = new ArbiterWayId(1, asi);
                ArbiterWay   aw1  = new ArbiterWay(awi1);
                aw1.Segment = asg;
                asg.Ways.Add(awi1, aw1);
                asg.Way1 = aw1;

                // way2
                ArbiterWayId awi2 = new ArbiterWayId(2, asi);
                ArbiterWay   aw2  = new ArbiterWay(awi2);
                aw2.Segment = asg;
                asg.Ways.Add(awi2, aw2);
                asg.Way2 = aw2;

                // make lanes
                foreach (SimpleLane sl in ss.Lanes)
                {
                    // lane
                    ArbiterLaneId ali;
                    ArbiterLane   al;

                    // get way of lane id
                    if (ss.Way1Lanes.Contains(sl))
                    {
                        ali = new ArbiterLaneId(GenerationTools.GetId(sl.Id)[1], awi1);
                        al  = new ArbiterLane(ali);
                        aw1.Lanes.Add(ali, al);
                        al.Way = aw1;
                    }
                    else
                    {
                        ali = new ArbiterLaneId(GenerationTools.GetId(sl.Id)[1], awi2);
                        al  = new ArbiterLane(ali);
                        aw2.Lanes.Add(ali, al);
                        al.Way = aw2;
                    }

                    // add to display
                    arn.DisplayObjects.Add(al);

                    // width
                    al.Width = sl.LaneWidth == 0 ? TahoeParams.T * 2.0 : sl.LaneWidth * 0.3048;

                    if (sl.LaneWidth == 0)
                    {
                        Console.WriteLine("lane: " + ali.ToString() + " contains no lane width, setting to 4m");
                    }

                    // lane boundaries
                    al.BoundaryLeft  = this.GenerateLaneBoundary(sl.LeftBound);
                    al.BoundaryRight = this.GenerateLaneBoundary(sl.RightBound);

                    // add lane to seg
                    asg.Lanes.Add(ali, al);

                    // waypoints
                    List <ArbiterWaypoint> waypointList = new List <ArbiterWaypoint>();

                    // generate waypoints
                    foreach (SimpleWaypoint sw in sl.Waypoints)
                    {
                        // waypoint
                        ArbiterWaypointId awi = new ArbiterWaypointId(GenerationTools.GetId(sw.ID)[2], ali);
                        ArbiterWaypoint   aw  = new ArbiterWaypoint(sw.Position, awi);
                        aw.Lane = al;

                        // stop
                        if (sl.Stops.Contains(sw.ID))
                        {
                            aw.IsStop = true;
                        }

                        // checkpoint
                        foreach (SimpleCheckpoint sc in sl.Checkpoints)
                        {
                            if (sw.ID == sc.WaypointId)
                            {
                                aw.IsCheckpoint = true;
                                aw.CheckpointId = int.Parse(sc.CheckpointId);
                                arn.Checkpoints.Add(aw.CheckpointId, aw);
                            }
                        }

                        // add
                        asg.Waypoints.Add(awi, aw);
                        arn.ArbiterWaypoints.Add(awi, aw);
                        al.Waypoints.Add(awi, aw);
                        waypointList.Add(aw);
                        arn.DisplayObjects.Add(aw);
                        arn.LegacyWaypointLookup.Add(sw.ID, aw);
                    }

                    al.WaypointList = waypointList;

                    // lane partitions
                    List <ArbiterLanePartition> alps = new List <ArbiterLanePartition>();
                    al.Partitions = alps;

                    // generate lane partitions
                    for (int i = 0; i < waypointList.Count - 1; i++)
                    {
                        // create lane partition
                        ArbiterLanePartitionId alpi = new ArbiterLanePartitionId(waypointList[i].WaypointId, waypointList[i + 1].WaypointId, ali);
                        ArbiterLanePartition   alp  = new ArbiterLanePartition(alpi, waypointList[i], waypointList[i + 1], asg);
                        alp.Lane = al;
                        waypointList[i].NextPartition         = alp;
                        waypointList[i + 1].PreviousPartition = alp;
                        alps.Add(alp);
                        arn.DisplayObjects.Add(alp);

                        // crete initial user partition
                        ArbiterUserPartitionId      aupi = new ArbiterUserPartitionId(alp.PartitionId, waypointList[i].WaypointId, waypointList[i + 1].WaypointId);
                        ArbiterUserPartition        aup  = new ArbiterUserPartition(aupi, alp, waypointList[i], waypointList[i + 1]);
                        List <ArbiterUserPartition> aups = new List <ArbiterUserPartition>();
                        aups.Add(aup);
                        alp.UserPartitions = aups;
                        alp.SetDefaultSparsePolygon();
                        arn.DisplayObjects.Add(aup);
                    }

                    // path segments of lane
                    List <IPathSegment> ips          = new List <IPathSegment>();
                    List <Coordinates>  pathSegments = new List <Coordinates>();
                    pathSegments.Add(alps[0].Initial.Position);

                    // loop
                    foreach (ArbiterLanePartition alPar in alps)
                    {
                        ips.Add(new LinePathSegment(alPar.Initial.Position, alPar.Final.Position));
                        // make new segment
                        pathSegments.Add(alPar.Final.Position);
                    }

                    // generate lane partition path
                    LinePath partitionPath = new LinePath(pathSegments);
                    al.SetLanePath(partitionPath);
                    al.PartitionPath = new Path(ips, CoordinateMode.AbsoluteProjected);

                    // safeto zones
                    foreach (ArbiterWaypoint aw in al.Waypoints.Values)
                    {
                        if (aw.IsStop)
                        {
                            LinePath.PointOnPath end = al.GetClosestPoint(aw.Position);
                            double dist = -30;
                            LinePath.PointOnPath begin = al.LanePath().AdvancePoint(end, ref dist);
                            if (dist != 0)
                            {
                                begin = al.LanePath().StartPoint;
                            }
                            ArbiterSafetyZone asz = new ArbiterSafetyZone(al, end, begin);
                            asz.isExit = true;
                            asz.Exit   = aw;
                            al.SafetyZones.Add(asz);
                            arn.DisplayObjects.Add(asz);
                            arn.ArbiterSafetyZones.Add(asz);
                        }
                    }
                }
            }

            return(arn);
        }
        /// <summary>
        /// Generates the arbiter zones form the internal xy zones for the input road network
        /// </summary>
        /// <param name="arn"></param>
        /// <returns></returns>
        /// <remarks>TODO: add zone cost map, adjacency of parking spots, figure out width</remarks>
        public ArbiterRoadNetwork GenerateZones(ArbiterRoadNetwork arn)
        {
            Dictionary <ArbiterZoneId, ArbiterZone> zones = new Dictionary <ArbiterZoneId, ArbiterZone>();
            List <IArbiterWaypoint> waypoints             = new List <IArbiterWaypoint>();

            foreach (SimpleZone sz in xyZones)
            {
                ArbiterZoneId azi = new ArbiterZoneId(int.Parse(sz.ZoneID));

                #region Generate Perimeter

                // old perim
                ZonePerimeter zp = sz.Perimeter;

                // perim id
                ArbiterPerimeterId api = new ArbiterPerimeterId(GenerationTools.GetId(zp.PerimeterID)[1], azi);

                #region Perimeter Waypoints

                List <ArbiterPerimeterWaypoint> perimeterWaypoints = new List <ArbiterPerimeterWaypoint>();

                foreach (PerimeterPoint pp in zp.PerimeterPoints)
                {
                    // id
                    ArbiterPerimeterWaypointId apwi = new ArbiterPerimeterWaypointId(
                        GenerationTools.GetId(pp.ID)[2], api);

                    // point
                    ArbiterPerimeterWaypoint apw = new ArbiterPerimeterWaypoint(apwi, pp.position);

                    // add
                    perimeterWaypoints.Add(apw);
                    waypoints.Add(apw);
                    arn.DisplayObjects.Add(apw);
                    arn.LegacyWaypointLookup.Add(pp.ID, apw);
                }

                #endregion

                // generate perimeter
                ArbiterPerimeter ap = new ArbiterPerimeter(api, perimeterWaypoints);
                arn.DisplayObjects.Add(ap);

                // set per in points
                foreach (ArbiterPerimeterWaypoint apw in perimeterWaypoints)
                {
                    apw.Perimeter = ap;
                }

                #region Set Defined Links

                // set links among perimeter nodes
                for (int i = 1; i <= ap.PerimeterPoints.Count; i++)
                {
                    ArbiterPerimeterWaypointId apwi = new ArbiterPerimeterWaypointId(i, ap.PerimeterId);
                    ArbiterPerimeterWaypoint   apw  = ap.PerimeterPoints[apwi];

                    if (i < ap.PerimeterPoints.Count)
                    {
                        ArbiterPerimeterWaypointId apwiNext = new ArbiterPerimeterWaypointId(i + 1, ap.PerimeterId);
                        apw.NextPerimeterPoint = ap.PerimeterPoints[apwiNext];
                    }
                    else
                    {
                        ArbiterPerimeterWaypointId apwiNext = new ArbiterPerimeterWaypointId(1, ap.PerimeterId);
                        apw.NextPerimeterPoint = ap.PerimeterPoints[apwiNext];
                    }
                }

                #endregion

                #endregion

                #region Generate Parking Spots

                List <ArbiterParkingSpot> parkingSpots = new List <ArbiterParkingSpot>();

                #region Parking Spots

                foreach (ParkingSpot ps in sz.ParkingSpots)
                {
                    // spot id
                    int apsiNum = GenerationTools.GetId(ps.SpotID)[1];
                    ArbiterParkingSpotId apsi = new ArbiterParkingSpotId(apsiNum, azi);

                    // spot width
                    double spotWidth;

                    // check if spot width not set
                    if (ps.SpotWidth == null || ps.SpotWidth == "" || ps.SpotWidth == "0")
                    {
                        spotWidth = 3.0;
                    }
                    else
                    {
                        // convert feet to meters
                        spotWidth = double.Parse(ps.SpotWidth) * 0.3048;
                    }

                    // spot
                    ArbiterParkingSpot aps = new ArbiterParkingSpot(spotWidth, apsi);
                    arn.DisplayObjects.Add(aps);

                    // waypoints
                    List <ArbiterParkingSpotWaypoint> parkingSpotWaypoints = new List <ArbiterParkingSpotWaypoint>();

                    #region Parking Spot Waypoints

                    #region Waypoint 1

                    // id
                    int apwi1Number = GenerationTools.GetId(ps.Waypoint1.ID)[2];
                    ArbiterParkingSpotWaypointId apwi1 = new ArbiterParkingSpotWaypointId(apwi1Number, apsi);

                    // generate waypoint 1
                    ArbiterParkingSpotWaypoint apw1 = new ArbiterParkingSpotWaypoint(
                        ps.Waypoint1.Position, apwi1, aps);

                    // set
                    parkingSpotWaypoints.Add(apw1);
                    waypoints.Add(apw1);
                    arn.DisplayObjects.Add(apw1);
                    arn.LegacyWaypointLookup.Add(ps.Waypoint1.ID, apw1);
                    apw1.ParkingSpot = aps;

                    // checkpoint or not?
                    if (ps.CheckpointWaypointID == ps.Waypoint1.ID)
                    {
                        apw1.IsCheckpoint = true;
                        apw1.CheckpointId = int.Parse(ps.CheckpointID);
                        aps.Checkpoint    = apw1;
                        arn.Checkpoints.Add(apw1.CheckpointId, apw1);
                    }
                    else
                    {
                        aps.NormalWaypoint = apw1;
                    }

                    #endregion

                    #region Waypoint 2

                    // id
                    int apwi2Number = GenerationTools.GetId(ps.Waypoint2.ID)[2];
                    ArbiterParkingSpotWaypointId apwi2 = new ArbiterParkingSpotWaypointId(apwi2Number, apsi);

                    // generate waypoint 2
                    ArbiterParkingSpotWaypoint apw2 = new ArbiterParkingSpotWaypoint(
                        ps.Waypoint2.Position, apwi2, aps);

                    // set
                    parkingSpotWaypoints.Add(apw2);
                    waypoints.Add(apw2);
                    arn.DisplayObjects.Add(apw2);
                    arn.LegacyWaypointLookup.Add(ps.Waypoint2.ID, apw2);
                    apw2.ParkingSpot = aps;

                    // checkpoint or not?
                    if (ps.CheckpointWaypointID == ps.Waypoint2.ID)
                    {
                        apw2.IsCheckpoint = true;
                        apw2.CheckpointId = int.Parse(ps.CheckpointID);
                        aps.Checkpoint    = apw2;
                        arn.Checkpoints.Add(apw2.CheckpointId, apw2);
                    }
                    else
                    {
                        aps.NormalWaypoint = apw2;
                    }

                    #endregion

                    #endregion

                    // set waypoints
                    aps.SetWaypoints(parkingSpotWaypoints);

                    // add
                    parkingSpots.Add(aps);
                }

                #endregion

                #endregion

                #region Create Zone

                // create zone
                ArbiterZone az = new ArbiterZone(azi, ap, parkingSpots, arn);

                // zone
                az.SpeedLimits = new ArbiterSpeedLimit();
                az.SpeedLimits.MaximumSpeed = 2.24;
                az.SpeedLimits.MinimumSpeed = 2.24;

                // add to final dictionary
                zones.Add(az.ZoneId, az);
                arn.DisplayObjects.Add(az);

                #endregion
            }

            // set zones
            arn.ArbiterZones = zones;

            // add waypoints
            foreach (IArbiterWaypoint iaw in waypoints)
            {
                // set waypoint
                arn.ArbiterWaypoints.Add(iaw.AreaSubtypeWaypointId, iaw);
            }

            // return
            return(arn);
        }