Пример #1
0
        // private readonly LoggingManager _log;
        // private readonly int _infoPin;

        public TrainControlStack(int unitId, LoggingManager log, INetworkDatabase network)
        {
            // _log = log;
            // var eh = _log.GetEntityHandle(typeof(TrainControlStack), unitId);
            // _infoPin = _log.GetSignalPin(eh, "info");

            _scheduleController = new ScheduleController(unitId, log);
            _waypointController = new WaypointController(unitId, log, network);
            _tractionController = new TractionController(unitId, log);
        }
Пример #2
0
        public WaypointController(int unitId, LoggingManager log, INetworkDatabase network)
        {
            _network      = network;
            _routePlanner = new RoutePlanner(network);

            _log = log;
            var eh = _log.GetEntityHandle(this.GetType(), unitId);

            _infoPin  = _log.GetSignalPin(eh, "info");
            _statePin = _log.GetSignalPin(eh, "state");
        }
Пример #3
0
        private static void DrawLinks(INetworkDatabase ndb, Context cr, PointD center, double scale)
        {
            foreach (var link in ndb.EnumerateSegmentLinks())
            {
                var pos = ndb.GetSegmentById(link.Segment1).GetEndpoint(link.Ep1);

                var c = aluminium6;
                c.A = 0.1;
                var pt = SimToCanvasSpace(new Vector3(pos.X, pos.Y, 0), center, scale);

                cr.SetSourceColor(c);
                cr.Arc(pt.X, pt.Y, 1, 0, 2 * Math.PI);
                cr.Fill();
            }
        }
Пример #4
0
        public static void RenderPng(SimulationCoordinateSpace coordinateSpace,
                                     INetworkDatabase ndb,
                                     IUnitDatabase units,
                                     IDictionary <int, TrainControlStateSummary>?controllerMap,
                                     string filename,
                                     int w, int h,
                                     double scale, int fontSize)
        {
            var center = new PointD(w / 2, h / 2);

            var     surf = new ImageSurface(Format.Argb32, w, h);
            Context cr   = new Context(surf);

            RenderToContext(coordinateSpace, ndb, units, controllerMap, cr, center, scale, fontSize);

            cr.Dispose();
            surf.WriteToPng(filename);
            surf.Finish();
            surf.Dispose();
        }
Пример #5
0
        public Simulation(SimulationCoordinateSpace coordSpace, INetworkDatabase network, IUnitDatabase units, LoggingManager log)
        {
            CoordSpace = coordSpace;
            Network    = network;
            Units      = units;
            _log       = log;

            _currentSegmentByUnitId = new int?[units.GetNumUnits()];
            _dirByUnitId            = new SegmentEndpoint[units.GetNumUnits()];
            _tByUnitId      = new float[units.GetNumUnits()];
            _unitProperties = new UnitProperties[units.GetNumUnits()];

            for (var unitIndex = 0; unitIndex < Units.GetNumUnits(); unitIndex++)
            {
                var unit = Units.GetUnitByIndex(unitIndex);

                var result = Network.FindSegmentAt(unit.Pos, unit.Orientation, 0.2f, (float)(Math.PI * 0.25));

                if (result == null)
                {
                    Console.Error.WriteLine($"Simulation: could not snap unit {unitIndex} to railroad network!");
                    continue;
                }

                var(segmentId, dir, t)             = result.Value;
                _currentSegmentByUnitId[unitIndex] = segmentId;
                _dirByUnitId[unitIndex]            = dir;
                _tByUnitId[unitIndex] = t;

                var eh = _log.GetEntityHandle(_unitProperties[unitIndex].GetType(), unitIndex);
                _unitProperties[unitIndex] = new UnitProperties {
                    Controller      = new TrainControlStack(unitIndex, _log, Network),
                    AccelerationPin = _log.GetSignalPin(eh, "acceleration"),
                    VelocityPin     = _log.GetSignalPin(eh, "velocity"),
                    SegmentIdPin    = _log.GetSignalPin(eh, "segmentId"),
                    TPin            = _log.GetSignalPin(eh, "t"),
                };

                _unitProperties[unitIndex].Controller.GoAutoSchedule();
            }
        }
Пример #6
0
        public static void RenderSvg(SimulationCoordinateSpace coordinateSpace,
                                     INetworkDatabase ndb,
                                     IUnitDatabase units,
                                     string filename)
        {
            var w        = 1000;
            var h        = 600;
            var scale    = 0.04;     // meters/pixel
            var fontSize = 8;

            var center = new PointD(w / 2, h / 2);

            var     surf = new SvgSurface(filename, w, h);
            Context cr   = new Context(surf);

            RenderToContext(coordinateSpace, ndb, units, null, cr, center, scale, fontSize);

            cr.Dispose();
            surf.Flush();
            surf.Finish();
            surf.Dispose();
        }
Пример #7
0
 public QuadTree(INetworkDatabase network, QuadTreeNode root)
 {
     _network = network;
     Root     = root;
 }
Пример #8
0
 public QuadTree(INetworkDatabase network, Vector3 boundsMin, Vector3 boundsMax)
 {
     _network = network;
     Root     = new QuadTreeNode(boundsMin, boundsMax);
 }
Пример #9
0
 public RoutePlanner(INetworkDatabase network)
 {
     _network = network;
 }
Пример #10
0
        private static void RenderToContext(SimulationCoordinateSpace coordinateSpace,
                                            INetworkDatabase ndb,
                                            IUnitDatabase units,
                                            IDictionary <int, TrainControlStateSummary>?controllerMap,
                                            Context cr,
                                            PointD center,
                                            double scale, int fontSize)
        {
            //Console.WriteLine("RenderFullView start");

            cr.SetSourceColor(aluminium1);
            cr.Paint();

//            cr.MoveTo(0, h);
//            cr.ShowText($"Scale: full width = {(w / scale)} meters");

            // Draw quadtree
            var maybeQuadTree = ndb.GetQuadTreeIfYouHaveOne();

            if (maybeQuadTree is {} quadTree)
            {
                DrawQuadTreeNode(quadTree.Root, cr, center, scale);
            }

            // Draw rail links
            DrawLinks(ndb, cr, center, scale);

            // Draw railway
            foreach (var seg in ndb.EnumerateSegments())
            {
                Trace.Assert(seg.ControlPoints.Length == 2);

                var start = SimToCanvasSpace(seg.ControlPoints[0], center, scale);
                var end   = SimToCanvasSpace(seg.ControlPoints[1], center, scale);

                cr.LineWidth = railLineWidth;
                cr.SetSourceColor(seg.Oneway == false ? aluminium6 : aluminium5);
                cr.MoveTo(start);
                cr.LineTo(end);
                cr.Stroke();

//                DrawTextRegular(cr, $"{seg.Id}", new PointD((start.X + end.X) / 2, (start.Y + end.Y) / 2), 9);
            }

            // Draw stations
            foreach (var station in ndb.EnumerateStations())
            {
                PointD pos0 = new PointD();
                cr.SetSourceColor(skyBlue2);

                foreach (var stop in station.Stops)
                {
                    var pos = ndb.GetSegmentById(stop.SegmentId).GetPoint(stop.T);

                    var posCS = SimToCanvasSpace(pos, center, scale);
                    pos0 = posCS;

                    cr.LineWidth = 0.5;
                    DrawCrosshair(cr, posCS, 5);
                }

                cr.SetSourceColor(aluminium2);
                DrawTextRegular(cr, station.Name, pos0, fontSize);
            }

            // Draw trains
            int unitIndex = 0;

            foreach (var unit in units.EnumerateUnits())
            {
                var pos  = unit.Pos;
                var head = unit.Pos + Vector3.Transform(new Vector3((float)(5 / scale), 0, 0), unit.Orientation);

                var posCS  = SimToCanvasSpace(pos, center, scale);
                var headCS = SimToCanvasSpace(head, center, scale);

                var info = $"{unit.Class.Name}\n{unit.Velocity.Length() * 3.6:F1} km/h";
                if (controllerMap != null && controllerMap.ContainsKey(unitIndex))
                {
                    info += "\n" + controllerMap[unitIndex].SchedulerState;

                    var route = controllerMap[unitIndex].SegmentsToFollow;

                    if (route != null)
                    {
                        foreach (var entry in route)
                        {
                            // try {
                            // Console.WriteLine($"Segment => {entry.SegmentId}");
                            var seg = ndb.GetSegmentById(entry.SegmentId);
                            Trace.Assert(seg.ControlPoints.Length == 2);

                            var startXyz = seg.GetEndpoint(entry.EntryEp);
                            var endXyz   = entry.GoalT >= 0 ? seg.GetPoint(entry.GoalT) : seg.GetEndpoint(entry.EntryEp.Other());

                            var start = SimToCanvasSpace(startXyz, center, scale);
                            var end   = SimToCanvasSpace(endXyz, center, scale);

                            cr.LineWidth = railLineWidth * 2;
                            cr.SetSourceColor(plum1);
                            cr.MoveTo(start);
                            cr.LineTo(end);
                            cr.Stroke();
                            // }
                            // catch (System.InvalidOperationException ex) {
                            // }
                        }
                    }
                }

                cr.LineWidth = railLineWidth * 2;
                cr.SetSourceColor(chameleon1);
                cr.MoveTo(posCS.X * 2 - headCS.X, posCS.Y * 2 - headCS.Y);
                cr.LineTo(headCS);
                cr.Stroke();

                cr.SetSourceColor(chameleon3);
                DrawTextBold(cr, info, posCS, fontSize);

                unitIndex++;
            }

            // Draw info about agents
            // if (agents != null)
            // {
            //     int i = 0;

            //     foreach (var agent in agents)
            //     {
            //         DrawTextRegular(cr, agent.ToString(), new PointD(0, i * 10), 9);
            //         i++;
            //     }
            // }

            cr.LineWidth = 1;
            cr.SetSourceColor(scarledRed1);
            DrawCrosshair(cr, center, 10);

            //Console.WriteLine("RenderFullView done");
        }
 public NetworkRepository(INetworkDatabase networkDb)
 {
     this.networkDb = networkDb;
     neo4JDB = new Neo4jDB();
     InitAndLoadUsers();
 }