Example #1
0
        private static void BenchmarkSAP1D(bool useInitialize)
        {
            int     nObjects  = 500;
            int     nResolves = 10;
            Vector2 minPoint  = new Vector2(-100, -100);
            Vector2 maxPoint  = new Vector2(100, 100);
            float   minRadius = 0.2f;
            float   maxRadius = 4f;
            Random  r         = new Random();

            float[] tResolves = new float[nResolves];

            ISweepAndPrune <Object> sap = new SweepAndPrune2D <Object>(false);

            Object[] list = new Object[nObjects];
            for (int i = 0; i < nObjects; i++)
            {
                list[i] = RandomObject(minPoint, maxPoint, minRadius, maxRadius);
            }

            //DateTime time = DateTime.Now;
            Stopwatch sw = new Stopwatch();

            sw.Start();
            if (useInitialize)
            {
                sap.Initialize(list);
            }
            else
            {
                foreach (Object o in list)
                {
                    sap.AddObject(o);
                }
            }

            float tInsertion = sw.ElapsedTicks * 1000f / Stopwatch.Frequency; sw.Reset(); sw.Start();

            //float tInsertion = (float)(DateTime.Now - time).TotalMilliseconds; time = DateTime.Now;

            for (int i = 0; i < nResolves; i++)
            {
                sap.Resolve();
                tResolves[i] = sw.ElapsedTicks * 1000f / Stopwatch.Frequency; sw.Reset(); sw.Start();
            }

            Console.WriteLine("RESULTS (" + (useInitialize ? "init" : "no init") + ")");
            Console.WriteLine("=================");
            Console.WriteLine(String.Format("Insertion: {0:0.000} ms", tInsertion));
            for (int i = 0; i < nResolves; i++)
            {
                Console.WriteLine(String.Format("Resolve #{0}: {1:0.000} ms", i, tResolves[i]));
                //AddNRandomObjects(5, ref sap, minPoint, maxPoint, minRadius, maxRadius);
                //RemoveNRandomObjects(5, ref sap);
                MoveObjects(list, 3);
            }
            int nIntersections = 0;

            //foreach (var o in sap.All)
            //    nIntersections += o.Intersectees.Count;
            Console.WriteLine("Number of intersections last run: " + nIntersections);
            Console.WriteLine("");
        }