public HashSet <Intersection> Resolve2()
        {
            if (!initialized)
            {
                initialized = true;
                for (int a = 0; a < nAxes; a++)
                {
                    startAndEndPoints[a].Sort(comparer);
                }
            }
            else
            {
                for (int a = 0; a < nAxes; a++)
                {
                    InsertionSortList <ListEntry> .InsertionSort(startAndEndPoints[a], comparer);
                }
            }

            HashSet <Intersection>[] sets = new HashSet <Intersection> [nAxes];

            for (int a = 0; a < nAxes; a++)
            {
                List <T> openList = new List <T>();
                sets[a] = new HashSet <Intersection>();
                var axisStartAndEndPoints = startAndEndPoints[a];

                for (int i = 0; i < axisStartAndEndPoints.Count; i++)
                {
                    ListEntry le = axisStartAndEndPoints[i];
                    if (le.IsStartPoint)
                    {
                        foreach (var o in openList)
                        {
                            // assume duplicateIntesections == false for now
                            if (le.Object.GetHashCode() < o.GetHashCode())
                            {
                                AddToHashSet(ref sets[a], le.Object, o);
                            }
                            else
                            {
                                AddToHashSet(ref sets[a], o, le.Object);
                            }
                        }
                        openList.Add(le.Object);
                    }
                    else
                    if (!openList.Remove(le.Object))
                    {
                        throw new Exception("Removal failed");
                    }
                }
            }

            for (int a = 1; a < nAxes; a++)
            {
                sets[0].IntersectWith(sets[a]);
            }

            return(sets[0]);
        }
예제 #2
0
        public void Initialize(T[] objects)
        {
            var list = new List <T>(objects);

            list.Sort(comparer);
            items = new InsertionSortList <T>(comparer, list);
        }
예제 #3
0
        public Dictionary <ISweepAndPruneObject, List <ISweepAndPruneObject> > Resolve2()
        {
            Dictionary <ISweepAndPruneObject, List <ISweepAndPruneObject> > dict = new Dictionary <ISweepAndPruneObject, List <ISweepAndPruneObject> >();

            if (!initialized)
            {
                initialized = true;
                startAndEndPoints.Sort(comparer);
            }
            else
            {
                InsertionSortList <ListEntry> .InsertionSort(startAndEndPoints, comparer);
            }

            // TODO: Add check so that s_i < e_i for all entries

            List <T> openList = new List <T>();

            for (int i = 0; i < startAndEndPoints.Count; i++)
            {
                ListEntry le = startAndEndPoints[i];
                if (le.IsStartPoint)            // add object to open list
                {
                    foreach (var o in openList) // TODO: perform precise collision detection
                    {
                        if (le.Object.Min.Y > o.Max.Y || le.Object.Max.Y < o.Min.Y)
                        {
                            continue;
                        }

                        if (duplicateIntersections)
                        {
                            //le.Object.Intersectees.Add(o);
                            //o.Intersectees.Add(le.Object);
                            AddToDictionary(ref dict, le.Object, o);
                            AddToDictionary(ref dict, o, le.Object);
                        }
                        else
                        {
                            // add intersection to object with lowest hashcode
                            if (le.Object.GetHashCode() < o.GetHashCode())
                            {
                                AddToDictionary(ref dict, le.Object, o);
                            }
                            else
                            {
                                AddToDictionary(ref dict, o, le.Object);
                            }
                            //if (le.Object.GetHashCode() < o.GetHashCode())
                            //    le.Object.Intersectees.Add(o);
                            //else
                            //    o.Intersectees.Add(le.Object);
                        }
                    }
                    openList.Add(le.Object);        // TODO: This is quite expensive
                }
                else
                if (!openList.Remove(le.Object))
                {
                    throw new Exception("Removal failed");
                }
            }
            return(dict);
        }