コード例 #1
0
        private void timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            if (_useAgentVisual)
            {
                if (_use3D)
                {
                    meshGeometry.Clear();
                    ClearViewport();
                }

                for (int i = 0; i < scenario.agentsList.Count; i++)
                {
                    if (_use3D)
                    {
                        if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                        {
                            Point3D position = new Point3D(scenario.agentsList[i].GetPosition().X - scenario.map.GetMap().GetLength(0) / 2, 0, scenario.agentsList[i].GetPosition().Y - scenario.map.GetMap().GetLength(1) / 2);

                            int key = scenario.agentsList[i].Group;
                            if (!meshGeometry.ContainsKey(key))
                            {
                                meshGeometry.Add(key, new MeshGeometry3D());
                            }

                            if (scenario.agentsList[i] is HumanAgent)
                            {
                                meshGeometry[key] = HumanAgentVisual3D.AddAgentGeometry(position, new Size3D(1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3, 3.0D - new Random(scenario.agentsList[i].ID).NextDouble(), 1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3), meshGeometry[key]);
                            }
                            else if (scenario.agentsList[i] is BusAgent)
                            {
                                //TODO Раньше модели создавались в коде, теперь будут подгружаться из внешнего файла
                                //meshGeometry[key] = BusAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as BusAgent).Size, meshGeometry[key]);
                                mainViewport.Children.Add(BusAgentVisual3D.GetModel(position, (scenario.agentsList[i] as BusAgent).Size, (scenario.agentsList[i] as BusAgent).Angle));
                            }
                            else if (scenario.agentsList[i] is TrainAgent)
                            {
                                TrainAgent ag = (scenario.agentsList[i] as TrainAgent);
                                for (int g = 0; g < ag.Positions.Length; g++)
                                {
                                    if (ag.NeedDraw[g] == true)
                                    {
                                        position = new Point3D(ag.Positions[g].X - scenario.map.GetMap().GetLength(0) / 2, 0, ag.Positions[g].Y - scenario.map.GetMap().GetLength(1) / 2);
                                        //    meshGeometry[key] = TrainAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as TrainAgent).Size, meshGeometry[key]);
                                        mainViewport.Children.Add(TrainAgentVisual3D.GetModel(position, ag.Size, ag.Angles[g]));
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        if (agentVisuals.ContainsKey(scenario.agentsList[i].ID))
                        {
                            if (agentVisuals[scenario.agentsList[i].ID].Location != scenario.agentsList[i].GetPosition())
                            {
                                //double angle;
                                //TODO Здесь раньше расчитывался угол поворота, теперь это все перехало в реализацию агента
                                if (UseAnimation)
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].AngleAn = angle;
                                    agentVisuals[scenario.agentsList[i].ID].LocationAn = scenario.agentsList[i].GetPosition();
                                }
                                else
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].Angle = angle;
                                    agentVisuals[scenario.agentsList[i].ID].Location = scenario.agentsList[i].GetPosition();
                                }
                            }
                        }
                        else
                        {
                            if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                            {
                                if (scenario.agentsList[i] is HumanAgent)
                                {
                                    HumanAgentVisual ag = new HumanAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is BusAgent)
                                {
                                    BusAgentVisual ag = new BusAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is TrainAgent)
                                {
                                    TrainAgentVisual ag = new TrainAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                            }
                        }
                    }
                }
                if (Use3D)
                {
                    //Делаем группу для отрисовки агентов
                    Model3DGroup group = new Model3DGroup();
                    //Добавляем группы агентов в группу отрисофки
                    foreach (var pair in meshGeometry)
                    {
                        GeometryModel3D geom = new GeometryModel3D(pair.Value, new DiffuseMaterial(AgentVisualBase.GetGroupColor(pair.Key)));
                        group.Children.Add(geom);
                    }
                    //Создаем 3D модель
                    ModelVisual3D model = new ModelVisual3D();
                    model.Content = group;
                    //Чистим ViewPort TODO теперь он чистится раньше
                    //ClearViewport();
                    //Заполняем ViewPort моделью
                    mainViewport.Children.Add(model);
                    LightDirectionChanged();
                }
                else
                {
                    for (int i = 0; i < pnlMap.Children.Count; i++)
                    {
                        if (pnlMap.Children[i] is AgentVisualBase)
                        {
                            AgentVisualBase vag = pnlMap.Children[i] as AgentVisualBase;
                            if (!scenario.agentsList.Contains(vag.agentBase))
                            {
                                pnlMap.Children.Remove(vag);
                                agentVisuals.Remove(vag.agentBase.ID);
                                i--;
                            }
                        }
                    }
                }
            }
            if (!UseAnimation)
            {
                tbBenchmark.Text = ((200 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000) * 10).ToString();
            }
            else
            {
                tbBenchmark.Text = ((4000 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000) / 2).ToString();
            }
            tbSimulationTime.Text = new DateTime().Add(scenario.currentTime).ToString("HH:mm:ss");
            tbAgentsCount.Text    = scenario.agentsList.Count.ToString();
        }
コード例 #2
0
        private void timer_Tick(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;

            if (_useAgentVisual)
            {
                if (_use3D)
                {
                    meshGeometry.Clear();
                    ClearViewport();
                }

                for (int i = 0; i < scenario.agentsList.Count; i++)
                {
                    if (_use3D)
                    {
                        if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                        {
                            Point3D position = new Point3D(scenario.agentsList[i].GetPosition().X - scenario.map.GetMap().GetLength(0) / 2, 0, scenario.agentsList[i].GetPosition().Y - scenario.map.GetMap().GetLength(1) / 2);

                            int key = scenario.agentsList[i].Group;
                            if (!meshGeometry.ContainsKey(key))
                            {
                                meshGeometry.Add(key, new MeshGeometry3D());
                            }

                            if (scenario.agentsList[i] is HumanAgent)
                            {
                                meshGeometry[key] = HumanAgentVisual3D.AddAgentGeometry(position, new Size3D(1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3, 3.0D - new Random(scenario.agentsList[i].ID).NextDouble(), 1.0D - new Random(scenario.agentsList[i].ID).NextDouble() / 3), meshGeometry[key]);
                            }
                            else if (scenario.agentsList[i] is BusAgent)
                            {
                                //TODO Раньше модели создавались в коде, теперь будут подгружаться из внешнего файла
                                //meshGeometry[key] = BusAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as BusAgent).Size, meshGeometry[key]);
                                mainViewport.Children.Add(BusAgentVisual3D.GetModel(position, (scenario.agentsList[i] as BusAgent).Size, (scenario.agentsList[i] as BusAgent).Angle));
                            }
                            else if (scenario.agentsList[i] is TrainAgent)
                            {
                                TrainAgent ag = (scenario.agentsList[i] as TrainAgent);
                                for (int g = 0; g < ag.Positions.Length; g++)
                                {
                                    if (ag.NeedDraw[g] == true)
                                    {
                                        position = new Point3D(ag.Positions[g].X - scenario.map.GetMap().GetLength(0) / 2, 0, ag.Positions[g].Y - scenario.map.GetMap().GetLength(1) / 2);
                                        //    meshGeometry[key] = TrainAgentVisual3D.AddAgentGeometry(position, (scenario.agentsList[i] as TrainAgent).Size, meshGeometry[key]);
                                        mainViewport.Children.Add(TrainAgentVisual3D.GetModel(position, ag.Size, ag.Angles[g]));
                                    }
                                }

                            }
                        }
                    }
                    else
                    {
                        if (agentVisuals.ContainsKey(scenario.agentsList[i].ID))
                        {
                            if (agentVisuals[scenario.agentsList[i].ID].Location != scenario.agentsList[i].GetPosition())
                            {
                                //double angle;
                                //TODO Здесь раньше расчитывался угол поворота, теперь это все перехало в реализацию агента
                                if (UseAnimation)
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].AngleAn = angle;
                                    agentVisuals[scenario.agentsList[i].ID].LocationAn = scenario.agentsList[i].GetPosition();
                                }
                                else
                                {
                                    //agentVisuals[scenario.agentsList[i].ID].Angle = angle;
                                    agentVisuals[scenario.agentsList[i].ID].Location = scenario.agentsList[i].GetPosition();
                                }
                            }
                        }
                        else
                        {
                            if (scenario.agentsList[i].GetPosition() != new Point(-1, -1))
                            {
                                if (scenario.agentsList[i] is HumanAgent)
                                {
                                    HumanAgentVisual ag = new HumanAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is BusAgent)
                                {
                                    BusAgentVisual ag = new BusAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                                else if (scenario.agentsList[i] is TrainAgent)
                                {
                                    TrainAgentVisual ag = new TrainAgentVisual(scenario.agentsList[i]);
                                    agentVisuals.Add(scenario.agentsList[i].ID, ag);
                                    pnlMap.Children.Add(ag);
                                }
                            }
                        }
                    }
                }
                if (Use3D)
                {
                    //Делаем группу для отрисовки агентов
                    Model3DGroup group = new Model3DGroup();
                    //Добавляем группы агентов в группу отрисофки
                    foreach (var pair in meshGeometry)
                    {
                        GeometryModel3D geom = new GeometryModel3D(pair.Value, new DiffuseMaterial(AgentVisualBase.GetGroupColor(pair.Key)));
                        group.Children.Add(geom);
                    }
                    //Создаем 3D модель
                    ModelVisual3D model = new ModelVisual3D();
                    model.Content = group;
                    //Чистим ViewPort TODO теперь он чистится раньше
                    //ClearViewport();
                    //Заполняем ViewPort моделью
                    mainViewport.Children.Add(model);
                    LightDirectionChanged();
                }
                else
                {
                    for (int i = 0; i < pnlMap.Children.Count; i++)
                    {
                        if (pnlMap.Children[i] is AgentVisualBase)
                        {
                            AgentVisualBase vag = pnlMap.Children[i] as AgentVisualBase;
                            if (!scenario.agentsList.Contains(vag.agentBase))
                            {
                                pnlMap.Children.Remove(vag);
                                agentVisuals.Remove(vag.agentBase.ID);
                                i--;
                            }
                        }
                    }
                }
            }
            if (!UseAnimation)
            {
                tbBenchmark.Text = ((200 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000) * 10).ToString();
            }
            else
            {
                tbBenchmark.Text = ((4000 - scenario.Benchmark - (DateTime.Now - now).Ticks / 10000)/2).ToString();
            }
            tbSimulationTime.Text = new DateTime().Add(scenario.currentTime).ToString("HH:mm:ss");
            tbAgentsCount.Text = scenario.agentsList.Count.ToString();
        }