Example #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();
        }
        private void Paint()
        {
            pnlPaint.Children.Clear();
            max = 0;
            for (int i = 0; i < data.Rows.Count; i++)
            {
                for (int j = 1; j < data.Columns.Count; j++)
                {
                    if (max < Convert.ToDouble(data.Rows[i][j]))
                    {
                        max = Convert.ToDouble(data.Rows[i][j]);
                    }
                }
            }

            double val = 10000000000;
            while (max < val)
            {
                val /= 10;
            }
            double scope = val / 10;
            while (max > val)
            {
                val += scope;
            }
            max = val;

            double x = 0, y = 0;
            int count = max > 1 && max < 10 ? (int)max : 10;
            for (int i = 0; i < count + 1; i++)
            {
                Line line = new Line()
                {
                    X1 = 50,
                    Y1 = 25 + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * i / count,
                    X2 = (pnlPaint.ActualWidth - 60) / data.Rows.Count * (data.Rows.Count - 1) + 50,
                    Y2 = 25 + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * i / count,
                    Fill = Brushes.LightGray,
                    Stroke = Brushes.LightGray,
                    StrokeThickness = 1.0
                };
                pnlPaint.Children.Add(line);
                TextBlock tb = new TextBlock()
                {
                    Text = (max == Math.Round(max) ? Math.Round(max * i / count) : Math.Round(max * i / count, 5)).ToString(),
                    FontSize = 10,
                    FontFamily = new System.Windows.Media.FontFamily("Tahoma")
                };
                tb.SetValue(Canvas.LeftProperty, 20.0D);
                tb.SetValue(Canvas.TopProperty, 25.0D + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * i / count - 5);
                pnlPaint.Children.Add(tb);
                TextBlock tb1 = new TextBlock()
                {
                    Text = (max == Math.Round(max) ? Math.Round(max * i / count) : Math.Round(max * i / count, 5)).ToString(),
                    FontSize = 10,
                    FontFamily = new System.Windows.Media.FontFamily("Tahoma")
                };
                tb1.SetValue(Canvas.LeftProperty, 60.0D + (pnlPaint.ActualWidth - 60) / data.Rows.Count * (data.Rows.Count - 1));
                tb1.SetValue(Canvas.TopProperty, 25.0D + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * i / count - 5);
                pnlPaint.Children.Add(tb1);
            }
            pnlPaint.Width = data.Rows.Count * 15 + 100;
            //Console.WriteLine("max: " + max);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                for (int j = 1; j < data.Columns.Count; j++)
                {
                    Color color = GetColorByIndex(j);
                    double value = Convert.ToDouble(data.Rows[i][j]);
                    //Console.WriteLine(i + ": " + value);

                    x = 50 + i * 15;
                    y = 25 + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * value / max;

                    if (i % 5 == 0 && j == 1)
                    {
                        Line line = new Line()
                        {
                            X1 = x,
                            Y1 = 25,
                            X2 = x,
                            Y2 = pnlPaint.ActualHeight - 150,
                            Fill = Brushes.LightGray,
                            Stroke = Brushes.LightGray,
                            StrokeThickness = 1.0
                        };
                        pnlPaint.Children.Add(line);

                        TextBlock tb = new TextBlock()
                        {
                            Text = data.Rows[i][0].ToString(),
                            FontSize = 12,
                            FontWeight = FontWeights.Bold,
                            FontFamily = new System.Windows.Media.FontFamily("Tahoma")
                        };

                        tb.SetValue(Canvas.LeftProperty, x);
                        tb.SetValue(Canvas.TopProperty, pnlPaint.ActualHeight - 140);
                        tb.RenderTransform = new RotateTransform(45);
                        pnlPaint.Children.Add(tb);
                    }

                    if (i != 0)
                    {
                        Line line = new Line()
                        {
                            X1 = 50 + (i - 1) * 15,
                            Y1 = 25 + (pnlPaint.ActualHeight - 175) - (pnlPaint.ActualHeight - 175) * Convert.ToDouble(data.Rows[i - 1][j]) / max,
                            X2 = x,
                            Y2 = y,
                            Fill = AgentVisualBase.GetGroupColor(j - 1),
                            Stroke = AgentVisualBase.GetGroupColor(j - 1),
                            StrokeThickness = 3.0
                        };
                        pnlPaint.Children.Add(line);
                    }
                    Ellipse el = new Ellipse()
                    {
                        Fill = AgentVisualBase.GetGroupColor(j - 1),
                        Stroke = AgentVisualBase.GetGroupColor(j - 1),
                        Width = 6,
                        Height = 6
                    };
                    el.SetValue(Canvas.LeftProperty, x - 3);
                    el.SetValue(Canvas.TopProperty, y - 3);
                    pnlPaint.Children.Add(el);
                }
            }
        }