示例#1
0
        internal void Remove(ref Int2 index, Grid2DEntry entry)
        {
            int cellIndex;
            int sortingHash;

            if (TryGetIndex(ref index, out cellIndex, out sortingHash))
            {
                cells.Elements[cellIndex].Remove(entry);
                if (cells.Elements[cellIndex].entries.count == 0)
                {
                    //The cell is now empty.  Give it back to the pool.
                    var toRemove = cells.Elements[cellIndex];
                    //There's no cleanup to do on the grid cell.
                    //Its list is empty, and the rest is just value types.
                    cells.RemoveAt(cellIndex);
                    cellPool.GiveBack(toRemove);
                    count--;
                }
            }


            //int sortingHash = index.GetSortingHash();
            //int minIndex = 0; //inclusive
            //int maxIndex = count; //exclusive
            //int i = 0;
            //while (maxIndex - minIndex > 0) //If the testing interval has a length of zero, we've done as much as we can.
            //{
            //    i = (maxIndex + minIndex) / 2;
            //    if (cells.Elements[i].sortingHash > sortingHash)
            //        maxIndex = i;
            //    else if (cells.Elements[i].sortingHash < sortingHash)
            //        minIndex = ++i;
            //    else
            //    {
            //        //Found an equal sorting hash!
            //        //The hash can collide, and we cannot add an entry to
            //        //an incorrect index.  It would break the 'cell responsibility'
            //        //used by the cell update process to avoid duplicate overlaps.
            //        //So, check if the index we found is ACTUALLY correct.
            //        if (cells.Elements[i].cellIndex.Y == index.Y && cells.Elements[i].cellIndex.Z == index.Z)
            //        {
            //            cells.Elements[i].Remove(entry);
            //            if (cells.Elements[i].entries.count == 0)
            //            {
            //                //The cell is now empty.  Give it back to the pool.
            //                var toRemove = cells.Elements[i];
            //                //There's no cleanup to do on the grid cell.
            //                //Its list is empty, and the rest is just value types.
            //                cells.RemoveAt(i);
            //                cellPool.GiveBack(toRemove);
            //                count--;
            //            }
            //            return;
            //        }
            //        //If it was not the correct index, let it continue searching.
            //    }

            //}
            ////Getting here should be impossible.
        }
示例#2
0
        internal void Add(ref Int2 index, Grid2DEntry entry)
        {
            int cellIndex;
            int sortingHash;

            if (TryGetIndex(ref index, out cellIndex, out sortingHash))
            {
                cells.Elements[cellIndex].Add(entry);
                return;
            }
            var cell = cellPool.Take();

            cell.Initialize(ref index, sortingHash);
            cell.Add(entry);
            cells.Insert(cellIndex, cell);
            count++;
            return;

            ////Take an index.  See if it's taken in the set.
            ////If it's already there, then add the entry to the cell.
            ////If it's not already there, create a new cell and add the entry to the cell and insert it at the index located.

            //int sortingHash = index.GetSortingHash();
            //int minIndex = 0; //inclusive
            //int maxIndex = count; //exclusive
            //int i = 0;
            //while (maxIndex - minIndex > 0) //If the testing interval has a length of zero, we've done as much as we can.
            //{
            //    i = (maxIndex + minIndex) / 2;
            //    if (cells.Elements[i].sortingHash > sortingHash)
            //        maxIndex = i;
            //    else if (cells.Elements[i].sortingHash < sortingHash)
            //        minIndex = ++i;
            //    else
            //    {
            //        //Found an equal sorting hash!
            //        //The hash can collide, and we cannot add an entry to
            //        //an incorrect index.  It would break the 'cell responsibility'
            //        //used by the cell update process to avoid duplicate overlaps.
            //        //So, check if the index we found is ACTUALLY correct.
            //        if (cells.Elements[i].cellIndex.Y == index.Y && cells.Elements[i].cellIndex.Z == index.Z)
            //        {
            //            cells.Elements[i].Add(entry);
            //            return;
            //        }
            //        //If it was not the correct index, let it continue searching.
            //    }

            //}
            //var cell = cellPool.Take();
            //cell.Initialize(ref index, sortingHash);
            //cell.Add(entry);
            //cells.Insert(i, cell);
            //count++;
        }
        internal void Add(ref Int2 index, Grid2DEntry entry)
        {
            int cellIndex;
            int sortingHash;
            if (TryGetIndex(ref index, out cellIndex, out sortingHash))
            {
                cells.Elements[cellIndex].Add(entry);
                return;
            }
            var cell = cellPool.Take();
            cell.Initialize(ref index, sortingHash);
            cell.Add(entry);
            cells.Insert(cellIndex, cell);
            count++;
            return;

            ////Take an index.  See if it's taken in the set.
            ////If it's already there, then add the entry to the cell.
            ////If it's not already there, create a new cell and add the entry to the cell and insert it at the index located.

            //int sortingHash = index.GetSortingHash();
            //int minIndex = 0; //inclusive
            //int maxIndex = count; //exclusive
            //int i = 0;
            //while (maxIndex - minIndex > 0) //If the testing interval has a length of zero, we've done as much as we can.
            //{
            //    i = (maxIndex + minIndex) / 2;
            //    if (cells.Elements[i].sortingHash > sortingHash)
            //        maxIndex = i;
            //    else if (cells.Elements[i].sortingHash < sortingHash)
            //        minIndex = ++i;
            //    else
            //    {
            //        //Found an equal sorting hash!
            //        //The hash can collide, and we cannot add an entry to 
            //        //an incorrect index.  It would break the 'cell responsibility' 
            //        //used by the cell update process to avoid duplicate overlaps.
            //        //So, check if the index we found is ACTUALLY correct.
            //        if (cells.Elements[i].cellIndex.Y == index.Y && cells.Elements[i].cellIndex.Z == index.Z)
            //        {
            //            cells.Elements[i].Add(entry);
            //            return;
            //        }
            //        //If it was not the correct index, let it continue searching.
            //    }

            //}
            //var cell = cellPool.Take();
            //cell.Initialize(ref index, sortingHash);
            //cell.Add(entry);
            //cells.Insert(i, cell);
            //count++;

        }
 internal void Remove(Grid2DEntry entry)
 {
     entries.Remove(entry);
 }
 internal void Add(Grid2DEntry entry)
 {
     //binary search for the approximately correct location.  This helps prevent large first-frame sort times.
     entries.Insert(GetIndex(entry.item.boundingBox.Min.X), entry);
 }
示例#6
0
        internal void UpdateOverlaps(Grid2DSortAndSweep owner)
        {
            //Sort along x axis using insertion sort; the list will be nearly sorted, so very few swaps are necessary.
            for (int i = 1; i < entries.count; i++)
            {
                var entry = entries.Elements[i];
                for (int j = i - 1; j >= 0; j--)
                {
                    if (entry.item.boundingBox.Min.X < entries.Elements[j].item.boundingBox.Min.X)
                    {
                        entries.Elements[j + 1] = entries.Elements[j];
                        entries.Elements[j]     = entry;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //Sweep the list looking for overlaps.
            for (int i = 0; i < entries.count; i++)
            {
                Grid2DEntry a = entries.Elements[i];
                Grid2DEntry b;
                //TODO: Microoptimize
                for (int j = i + 1; j < entries.count && a.item.boundingBox.Max.X >= (b = entries.Elements[j]).item.boundingBox.Min.X; j++)
                {
                    if (!(a.item.boundingBox.Min.Y > b.item.boundingBox.Max.Y || a.item.boundingBox.Max.Y < b.item.boundingBox.Min.Y ||
                          a.item.boundingBox.Min.Z > b.item.boundingBox.Max.Z || a.item.boundingBox.Max.Z < b.item.boundingBox.Min.Z))
                    {
                        //Now we know this pair is overlapping, but we do not know if this overlap is already added.
                        //Rather than use a hashset or other heavy structure to check, rely on the rules of the grid.

                        //It's possible to avoid adding pairs entirely unless we are the designated 'responsible' cell.
                        //All other cells will defer to the cell 'responsible' for a pair.
                        //A simple rule for determining the cell which is responsible is to choose the cell which is the
                        //smallest index in the shared cells.  So first, compute that cell's index.

                        Int2 minimumSharedIndex = a.previousMin;

                        if (minimumSharedIndex.Y < b.previousMin.Y)
                        {
                            minimumSharedIndex.Y = b.previousMin.Y;
                        }
                        if (minimumSharedIndex.Y > b.previousMax.Y)
                        {
                            minimumSharedIndex.Y = b.previousMax.Y;
                        }

                        if (minimumSharedIndex.Z < b.previousMin.Z)
                        {
                            minimumSharedIndex.Z = b.previousMin.Z;
                        }
                        if (minimumSharedIndex.Z > b.previousMax.Z)
                        {
                            minimumSharedIndex.Z = b.previousMax.Z;
                        }

                        //Is our cell the minimum cell?
                        if (minimumSharedIndex.Y == cellIndex.Y && minimumSharedIndex.Z == cellIndex.Z)
                        {
                            owner.TryToAddOverlap(a.item, b.item);
                        }
                    }
                }
            }
        }
示例#7
0
 internal void Remove(Grid2DEntry entry)
 {
     entries.Remove(entry);
 }
示例#8
0
 internal void Add(Grid2DEntry entry)
 {
     //binary search for the approximately correct location.  This helps prevent large first-frame sort times.
     entries.Insert(GetIndex(entry.item.boundingBox.Min.X), entry);
 }
示例#9
0
        internal void Remove(ref Int2 index, Grid2DEntry entry)
        {
            int cellIndex;
            int sortingHash;
            if (TryGetIndex(ref index, out cellIndex, out sortingHash))
            {
                cells.Elements[cellIndex].Remove(entry);
                if (cells.Elements[cellIndex].entries.Count == 0)
                {
                    //The cell is now empty.  Give it back to the pool.
                    var toRemove = cells.Elements[cellIndex];
                    //There's no cleanup to do on the grid cell.
                    //Its list is empty, and the rest is just value types.
                    cells.RemoveAt(cellIndex);
                    cellPool.GiveBack(toRemove);
                    count--;
                }
            }


            //int sortingHash = index.GetSortingHash();
            //int minIndex = 0; //inclusive
            //int maxIndex = count; //exclusive
            //int i = 0;
            //while (maxIndex - minIndex > 0) //If the testing interval has a length of zero, we've done as much as we can.
            //{
            //    i = (maxIndex + minIndex) / 2;
            //    if (cells.Elements[i].sortingHash > sortingHash)
            //        maxIndex = i;
            //    else if (cells.Elements[i].sortingHash < sortingHash)
            //        minIndex = ++i;
            //    else
            //    {
            //        //Found an equal sorting hash!
            //        //The hash can collide, and we cannot add an entry to 
            //        //an incorrect index.  It would break the 'cell responsibility' 
            //        //used by the cell update process to avoid duplicate overlaps.
            //        //So, check if the index we found is ACTUALLY correct.
            //        if (cells.Elements[i].cellIndex.Y == index.Y && cells.Elements[i].cellIndex.Z == index.Z)
            //        {
            //            cells.Elements[i].Remove(entry);
            //            if (cells.Elements[i].entries.count == 0)
            //            {
            //                //The cell is now empty.  Give it back to the pool.
            //                var toRemove = cells.Elements[i];
            //                //There's no cleanup to do on the grid cell.
            //                //Its list is empty, and the rest is just value types.
            //                cells.RemoveAt(i);
            //                cellPool.GiveBack(toRemove);
            //                count--;
            //            }
            //            return;
            //        }
            //        //If it was not the correct index, let it continue searching.
            //    }

            //}
            ////Getting here should be impossible.

        }