Ejemplo n.º 1
0
        private static CBORObject UnitsToCbor(IUnitDatabase units)
        {
            // var cborMap = CBORObject.NewMap();

            // cborMap.Add("simTimeElapsed", simTimeElapsed.ToString());

            var list = CBORObject.NewArray();

            foreach (var unit in units.EnumerateUnits())
            {
                var direction = Utility.QuaternionToDirectionVector(unit.Orientation);
                var dirZ      = Math.Atan2(direction.Y, direction.X);

                list.Add(CBORObject.NewMap()
                         .Add("pos", unit.Pos)
                         // .Add("direction", direction)
                         .Add("heading", dirZ)
                         // .Add("orientation", unit.Orientation)
                         .Add("speed", unit.Velocity.Length())
                         );
            }

            return(CBORObject.NewMap()
                   .Add("name", "units") // deprecated
                   .Add("topic", "units")
                   .Add("displayName", "Units")
                   .Add("mimeType", "application/cbor")
                   .Add("data", list)
                   );
        }
Ejemplo n.º 2
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();
        }
Ejemplo n.º 3
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();
            }
        }
Ejemplo n.º 4
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();
        }
Ejemplo n.º 5
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");
        }