예제 #1
0
        public PointSet[] GetSetsToDraw(Point leftTopView, Point rightBottomView, Window window)//, Matrix m)
        {
            lock (world.tickLocker)
            {
                calcOtherWatch.Restart();
                calcWatch.Restart();
                PointSet[] setsToDraw =
                    world.pointsManager.getPointsSets(
                        leftTopView.X, rightBottomView.X, leftTopView.Y, rightBottomView.Y,
                        maxPartiesRendered);
                calcGetSetsTimes.Add(calcOtherWatch.ElapsedMilliseconds);

                calcOtherWatch.Restart();
                int clustersDrawed = 0;
                int totalElements  = 0;
                foreach (Cluster c in world.pointsManager.clusters)
                {
                    if (c != null && c.idX >= world.pointsManager.li && c.idX <= world.pointsManager.ri && c.idY >= world.pointsManager.ti && c.idY <= world.pointsManager.bi)
                    {
                        clustersDrawed++;
                        totalElements += c.container.Values.Count;
                    }
                }
                calcIterateClustersTimes.Add(calcOtherWatch.ElapsedMilliseconds);

                calcOtherWatch.Restart();
                int    coloredGeometrySetLength = 0;
                double foodGetTime        = 0;
                int    foodGetCount       = 0;
                double createGeometryTime = 0;
                int    geometriesCreated  = 0;
                int    geometriesRestored = 0;
                //Parallel.For(0, coloredGeometry.Length - 1, (i) =>
                for (int i = 0; i < setsToDraw.Length; i++)
                {
                    PointSet        set = setsToDraw[i];
                    ColoredGeometry cg  = new ColoredGeometry();

                    Object val     = set.linkedObjects[CacheLastHashGeometryType];
                    uint   oldHash = 0;
                    if (val != null)
                    {
                        oldHash = (uint)val;
                    }
                    uint nowHash = set.hash;
                    if (oldHash == nowHash)
                    {
                        Object val2 = set.linkedObjects[CacheColoredGeometryType];
                        if (val2 != null)
                        {
                            geometriesRestored++;
                            continue;
                        }
                    }
                    geometriesCreated++;
                    set.linkedObjects[CacheLastHashGeometryType] = nowHash;

                    switch (set.type)
                    {
                    case World.ZoaType:
                        break;

                    case World.FoodType:
                        double size  = (set.joinDist * 2);
                        double alpha =
                            (world.pointsManager.lowestPointSize * world.pointsManager.lowestPointSize * set.points.Count * 4) /     //total food area
                            (Math.PI * size * size / 4);
                        if (alpha > 1)
                        {
                            alpha = 1;
                        }
                        coloredGeometrySetLength++;

                        double fire     = 0;
                        double grass    = 0;
                        double ocean    = 0;
                        double toxicity = 0;
                        foreach (StaticPoint p in set.points)
                        {
                            Food f = (Food)p.linkedObjects[World.WorldObjectType];    //world.food[p.id];
                            fire     += f.fire;
                            grass    += f.grass;
                            ocean    += f.ocean;
                            toxicity += f.toxicity;
                        }
                        toxicity /= set.points.Count;

                        /*Geometry g = new RectangleGeometry(
                         *      new Rect(
                         *          new Point(set.x - size / 2, set.y - size / 2),
                         *          new Point(set.x + size / 2, set.y + size / 2))
                         *  );*/
                        Geometry g = new EllipseGeometry(
                            new Rect(set.x - size / 2, set.y - size / 2, size, size)
                            );
                        g.Freeze();

                        double sum = fire + grass + ocean;
                        Color  c   = Color.FromArgb(
                            (byte)(alpha * 255),            //coeff * 255),
                            (byte)(fire / sum * 255),
                            (byte)(grass / sum * 255),
                            (byte)(ocean / sum * 255));
                        cg = new ColoredGeometry(g, c, null);
                        break;
                    }
                    set.linkedObjects[CacheColoredGeometryType] = cg;
                }//);
                createGeometryTime /= coloredGeometrySetLength;
                foodGetTime        /= foodGetCount;
                foodGetTimes.Add(foodGetTime);
                createGeometryTimes.Add(createGeometryTime);
                calcGeometryTimes.Add(calcOtherWatch.ElapsedMilliseconds);

                calcTimes.Add(calcWatch.ElapsedMilliseconds);

                if (calcTimes.Count > checkTimes)
                {
                    calcTimes.RemoveAt(0);
                }
                if (calcGetSetsTimes.Count > checkTimes)
                {
                    calcGetSetsTimes.RemoveAt(0);
                }
                if (calcIterateClustersTimes.Count > checkTimes)
                {
                    calcIterateClustersTimes.RemoveAt(0);
                }
                if (calcGeometryTimes.Count > checkTimes)
                {
                    calcGeometryTimes.RemoveAt(0);
                }

                int calcTime = 0;
                foreach (double d in calcTimes)
                {
                    calcTime += (int)d;
                }
                calcTime /= calcTimes.Count;

                int calcGetSetsTime = 0;
                foreach (double d in calcGetSetsTimes)
                {
                    calcGetSetsTime += (int)d;
                }
                calcGetSetsTime /= calcGetSetsTimes.Count;

                int calcIterateClustersTime = 0;
                foreach (double d in calcIterateClustersTimes)
                {
                    calcIterateClustersTime += (int)d;
                }
                calcIterateClustersTime /= calcIterateClustersTimes.Count;

                int calcGeometryTime = 0;
                foreach (double d in calcGeometryTimes)
                {
                    calcGeometryTime += (int)d;
                }
                calcGeometryTime /= calcGeometryTimes.Count;

                int renderTime = 0;
                lock (renderTimes)
                    foreach (double d in renderTimes)
                    {
                        renderTime += (int)d;
                    }
                if (renderTimes.Count > 0)
                {
                    renderTime /= renderTimes.Count;
                }

                try
                {
                    window.Dispatcher.Invoke(() =>
                    {
                        if (window.Title != null)
                        {
                            window.Title = String.Format(
                                "Clusters: {14}x{15}x{16} isGeneratingLayer: {17}|{18}%" +
                                " ClustersDrawed: {0}/{1}, Elements(Rendered/MaxRendered/InViewedClusters/Total): {2}/{3}/{4}/{5} " +
                                //"ElapsedTime: CalcSets/CalcClusters/CalcGeometry/CalcAll {6}/{7}/{8}/{9}ms" +
                                " Render {10}/Total {11}ms Geometries(Restored/Created): {12}/{13} FixedLayer: {19}",
                                //" CacheSize(CG/Drawings): {14}/{15}",
                                clustersDrawed, world.pointsManager.clusters.Length,
                                setsToDraw.Length, maxPartiesRendered, totalElements, world.pointsManager.pointsCount,
                                calcGetSetsTime, calcIterateClustersTime, calcGeometryTime,
                                calcTime, renderTime, calcTime + renderTime,
                                geometriesRestored, geometriesCreated,
                                world.pointsManager.clusters.GetLength(0), world.pointsManager.clusters.GetLength(1), world.pointsManager.clusters.GetLength(2),
                                world.pointsManager.isGeneratingLayer, (int)(world.pointsManager.layerGeneratingProgress * 100),
                                world.pointsManager.fixedLayerId);//,
                        }
                        //geometriesCache.Count, drawingsCache.Count);
                    });
                }
                catch (Exception e) { }

                return(setsToDraw);
            }
        }
예제 #2
0
        public void Render(DrawingContext drawingContext, PointSet[] setsToDraw, Matrix m)
        {
            renderWatch.Restart();
            DrawingGroup group = new DrawingGroup();

            group.Transform = new MatrixTransform(m);

            foreach (PointSet set in setsToDraw)
            {
                Object          drawingObj = set.linkedObjects[CacheDrawingsType];
                GeometryDrawing drawing;

                Object val     = set.linkedObjects[CacheLastHashDrawingType];
                uint   oldHash = 0;
                if (val != null)
                {
                    oldHash = (uint)val;
                }
                uint nowHash = set.hash;

                if (drawingObj != null && oldHash == nowHash)
                {
                    drawing = drawingObj as GeometryDrawing;
                }
                else
                {
                    ColoredGeometry cg = (ColoredGeometry)set.linkedObjects[CacheColoredGeometryType];
                    drawing = new GeometryDrawing(
                        (cg.brushColor == null) ? null : new SolidColorBrush((Color)cg.brushColor),
                        (cg.penColor == null) ? null : new Pen(new SolidColorBrush((Color)cg.penColor), 1),
                        cg.g);
                    set.linkedObjects[CacheDrawingsType] = drawing;
                }
                set.linkedObjects[CacheLastHashDrawingType] = nowHash;

                drawing.Freeze();
                group.Children.Add(drawing);
            }
            //group.Freeze();


            foreach (Cluster c in world.pointsManager.clusters)
            {
                if (c != null && c.idZ == -1)// && c.idX >= world.pointsManager.li && c.idX <= world.pointsManager.ri && c.idY >= world.pointsManager.ti && c.idY <= world.pointsManager.bi)
                {
                    Geometry g = new RectangleGeometry(
                        new Rect(
                            new Point(c.x, c.y),
                            new Point(c.x + c.size, c.y + c.size))
                        );
                    g.Freeze();
                    Color color = Colors.DarkGreen;
                    group.Children.Add(
                        new GeometryDrawing(null, new Pen(new SolidColorBrush(color), c.idZ * 10 + 1), g)
                        );
                }
            }

            group.Children.Add(
                new GeometryDrawing(
                    Brushes.Black, null, new EllipseGeometry(new Point(0, 0), 20, 20)
                    )
                );

            drawingContext.DrawDrawing(group);

            lock (renderTimes)
            {
                renderTimes.Add(renderWatch.ElapsedMilliseconds);
                if (renderTimes.Count > checkTimes)
                {
                    renderTimes.RemoveAt(0);
                }
            }
        }