public bool Add(T item, CullRegion region) { if (_root == null) { _root = new Node(_bounds, 0, null, this); } bool isContained; return(_root.Add(item, region, out isContained) > 0); }
public bool Remove(T item) { if (_nbElements == 0) { return(false); } if (!item.BoundingSphere.Intersect(_bounds)) { return(false); } bool result = false; if (_items != null) { var current = _items.First; while (current != null) { if (current.Value.Equals(item)) { break; } current = current.Next; } if (current != null) { CullRegion region = item.CullRegion as CullRegion; if (region != null) { region.Remove(this); } _items.Remove(current); _nbElements--; result = true; } } if (_northWest != null) { result |= _northWest.Remove(item); } if (_northEast != null) { result |= _northEast.Remove(item); } if (_southEast != null) { result |= _southEast.Remove(item); } if (_southWest != null) { result |= _southWest.Remove(item); } return(result); }
private void _AddItem(T item, CullRegion region) { if (_items == null) { _items = new LinkedList <T>(); } _items.AddLast(item); region.Add(this); ++_nbElements; }
//public bool ComputeDimensions(Scene scene, IBoundable boundable) //{ // var sphere = boundable.BoundingSphere; // if (sphere.Radius == 0) // return false; // if (_root == null || _root.Bounds.TestInside(sphere) != InsideTestResult.Inside) // { // float width = sphere.Radius * 2f; // float height = sphere.Radius * 2f; // float x = sphere.Center.X - sphere.Radius; // float y = sphere.Center.Z + sphere.Radius; // RectangleF bounds = new RectangleF(x, y, width, height); // if (_root != null) // { // bounds = bounds.Combine(_root.Bounds); // } // List<Node<T>.ValueStore> objects = null; // //get objects // if (_root != null) // { // objects = new List<Node<T>.ValueStore>(_root.NbElements); // _root.AddItems(objects); // } // _root = new Node<T>(bounds, 0, null, this); // //reallocate objects // if (objects != null) // { // foreach (var item in objects) // { // item.IsCulled = false; // _root._Add(item); // } // } // return true; // } // return false; //} public bool Add(T item) { var region = new CullRegion(this); if (Add(item, region)) { item.CullRegion = region; return(true); } return(false); }
private int _AddOrCreate(T item, RectangleF quadBound, ref Node node, CullRegion region, out bool isContained) { if (node != null) { return(node.Add(item, region, out isContained)); } else { if (item.BoundingSphere.Intersect(quadBound)) { node = new Node(quadBound, _level + 1, this, _quadTree); return(node.Add(item, region, out isContained)); } isContained = false; return(0); } }
//public void AddItems(List<ValueStore> collection) //{ // if (_nbElements == 0) // return; // if (_items != null) // { // foreach (var item in _items) // { // if (!item.IsCulled) // { // item.IsCulled = true; // collection.Add(item); // } // } // } // if (_northWest != null && _northWest._nbElements > 0) // _northWest.AddItems(collection); // if (_northEast != null && _northEast._nbElements > 0) // _northEast.AddItems(collection); // if (_southWest != null && _southWest._nbElements > 0) // _southWest.AddItems(collection); // if (_southEast != null && _southEast._nbElements > 0) // _southEast.AddItems(collection); //} public int Add(T item, CullRegion region, out bool isContained) { isContained = false; var test = _bounds.TestInside(item.BoundingSphere); if (test == InsideTestResult.Inside || test == InsideTestResult.PartialInside) { if (CanSubDivide) { float halfDx = _bounds.Width * 0.5f; float halfDz = _bounds.Height * 0.5f; var northWestRec = new RectangleF(_bounds.X, _bounds.Y, halfDx, halfDz); var northEastRec = new RectangleF(_bounds.X + halfDx, _bounds.Y, halfDx, halfDz); var southWestRec = new RectangleF(_bounds.X, _bounds.Y - halfDz, halfDx, halfDz); var southEastRec = new RectangleF(_bounds.X + halfDx, _bounds.Y - halfDz, halfDx, halfDz); bool b1, b2, b3, b4; int regions = _AddOrCreate(item, northWestRec, ref _northWest, region, out b1); regions += _AddOrCreate(item, northEastRec, ref _northEast, region, out b2); regions += _AddOrCreate(item, southWestRec, ref _southWest, region, out b3); regions += _AddOrCreate(item, southEastRec, ref _southEast, region, out b4); if (regions == 0 || b1 || b2 || b3 || b4) { _AddItem(item, region); return(regions + 1); } ++_nbElements; return(regions); } else { _AddItem(item, region); return(1); } } else if (test == InsideTestResult.Contained || test == InsideTestResult.PartialContained) { isContained = true; } return(0); }