Пример #1
0
        private static void CompareSAP1DFunctionalities()
        {
            int     nObjects  = 500;
            Vector2 minPoint  = new Vector2(-100, -100);
            Vector2 maxPoint  = new Vector2(100, 100);
            float   minRadius = 0.2f;
            float   maxRadius = 4f;

            //ISAP1D<Object> sap = new SweepAndPrune1D<Object>(false);
            ISweepAndPrune <Object> sap  = new SweepAndPrune2D <Object>(false);
            ISweepAndPrune <Object> sap2 = new SweepAndPrune1D <Object>(false);

            //List<Object> objects = new List<Object>();

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

            //Object[] objects2 = new List<Object>(objects).ToArray();

            foreach (var o in objects)
            {
                sap.AddObject(o);
            }
            foreach (var o in objects2)
            {
                sap2.AddObject(o);
            }

            sap.Resolve();
            sap2.Resolve();
            Console.WriteLine("Total collisions (I / II): " + CountIntersections(objects) + " / " + CountIntersections(objects2));
            Console.WriteLine("According to brute force: " + BruteForceResolve(new List <Object>(objects)));

            for (int i = 0; i < objects.Length; i++)
            {
                if (Object.ReferenceEquals(objects[i], objects2[i]))
                {
                    throw new Exception("Not clones, same objects!");
                }
                //if (objects[i].Intersectees.Count != objects2[i].Intersectees.Count)
                //    throw new Exception("Algorithms are not returning the same results!");

                for (int j = 0; j < objects2[i].Intersectees.Count; j++)
                {
                    var o = objects2[i].Intersectees[j];
                    if (!objects[i].Intersectees.Contains(o) && !objects[((Object)o).ID - 100000].Intersectees.Contains(objects[i]))
                    {
                        Console.WriteLine("Two returned intersection for " + VersatileObject(objects2[i]) + " and " + VersatileObject((Object)objects2[i].Intersectees[j]));
                        //throw new Exception("Not equal!");
                        System.Diagnostics.Debugger.Break();
                    }
                }
            }
            Console.WriteLine("Everything went fine.");
        }
Пример #2
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("");
        }