public void Reset()
 {
     _sceneObject = null;
     _nextBinReference = null;
     _previousBinReference = null;
     _objectLink = null;
 }
 protected void _FreeSceneBinReference(SceneContainerBinReference freeRef)
 {
     Assert.Fatal(freeRef != null, "doh");
     if (UseFreeList)
     {
         freeRef.Reset();
         freeRef._objectLink = _freeSceneBinReferences;
         _freeSceneBinReferences = freeRef;
     }
     //else, go garbage collector!
 }
        void _GetObjectsHelper(SceneContainerBinReference bin, List<ISceneObject> list)
        {
            for (SceneContainerBinReference walk = bin; walk != null; walk = walk._nextBinReference)
            {
                if (walk._sceneObject != null && walk._sceneObject.SceneContainerData._sequenceKey != _currentSequenceKey)
                {
                    list.Add(walk._sceneObject);
                    walk._sceneObject.SceneContainerData._sequenceKey = _currentSequenceKey;
                }

            }
        }
        protected void _FindObjectsInefficientHelper(SceneContainerQueryData query, SceneContainerBinReference bin, ref int objectsFound)
        {
            SceneContainerBinReference walk = bin;
            while (walk != null)
            {
                if (walk._sceneObject != null)
                {
                    // Have we dealt with this object already?
                    if (walk._sceneObject.SceneContainerData._sequenceKey == _currentSequenceKey)
                    {
                        // Move to next bin reference.
                        walk = walk._nextBinReference;
                        continue;
                    }
                    walk._sceneObject.SceneContainerData._sequenceKey = _currentSequenceKey;

                    if (_IntersectsWith(walk._sceneObject, query))
                        objectsFound++;
                }
                walk = walk._nextBinReference;
            }
        }
        protected void _FindInBin(SceneContainerBinReference bin, SceneContainerQueryData queryData)
        {
            object ignoreObject = queryData._ignoreObject;
            object[] ignoredObjects = queryData._ignoreObjects;
            ulong typebits = queryData._objectTypes._bits;

            // Step through Chain.
            while (bin != null)
            {
                // Fetch Scene Object Reference.
                ISceneContainerObject sceneObjectRef = bin._sceneObject;

                // Note: the following tries to get the object type directly from a TorqueObject rather than
                // going through the ISceneContainerObject interface.  This ends up being a LOT (a LOT) faster
                // for T2DSceneObjects even though it adds a branch.  It won't help when the object isn't a
                // TorqueObject as is the case of our T3D code, but we can consider other measures if that
                // becomes a problem (like making bin ref's point to SceneContainerData instead of scene object
                // and adding a pointer to iscene object in SceneContainerData as well as object type, but current
                // scheme is simpler and more effective for T2D).
                TorqueObject tobj = sceneObjectRef as TorqueObject;
                ulong ourType = tobj == null ? sceneObjectRef.ObjectType._bits : tobj._objectType;

                if ((ourType & typebits) == 0)
                {
                    // Move to next bin reference.
                    bin = bin._nextBinReference;
                    continue;
                }

                // get the scene container data for the object
                SceneContainerData scd = sceneObjectRef.SceneContainerData;

                // Have we dealt with this object already?
                if (scd._sequenceKey == _currentSequenceKey)
                {
                    // Move to next bin reference.
                    bin = bin._nextBinReference;
                    continue;
                }

                // Set the container sequence key to indicate that we've dealt with this object.
                scd._sequenceKey = _currentSequenceKey;

                // Check for ignored objects
                bool ignored = false;
                if (ignoreObject == sceneObjectRef)
                {
                    ignored = true;
                }
                else if (ignoredObjects != null)
                {
                    foreach (object o in ignoredObjects)
                        if (o == sceneObjectRef)
                        {
                            ignored = true;
                            break;
                        }
                }

                // Is the Object Not being ignored?
                if (!ignored)
                    _FoundObject(sceneObjectRef, queryData);

                // Move to next bin reference.
                bin = bin._nextBinReference;
            }
        }
        protected void _CreateBins()
        {
            // Create Scene Bin Array.
            _sceneBinArray = new SceneContainerBinReference[_binCount * _binCount];
            // if configured to do so, create individual array objects.
            // this can end up creating a lot of objects.
            if (PreAllocSceneBinArrayReferences)
                for (int i = 0; i < _sceneBinArray.Length; ++i)
                    _sceneBinArray[i] = new SceneContainerBinReference();

            // Initialise Scene Overflow Bin.
            _sceneOverflowBin = new SceneContainerBinReference();

            // Reset Free Scene Bin References.
            _freeSceneBinReferences = null;
            _CreateFreeListBlock();
        }
        protected SceneContainerBinReference _AllocateSceneBinReference()
        {
            if (UseFreeList && _freeSceneBinReferences == null)
                _CreateFreeListBlock();

            if (UseFreeList && _freeSceneBinReferences != null)
            {
                SceneContainerBinReference freeRef = _freeSceneBinReferences;
                // update free list head pointer
                _freeSceneBinReferences = _freeSceneBinReferences._objectLink;
                // unlink the freeRef
                freeRef._objectLink = null;

                // all fields should be null on free ref at this point
                freeRef.AssertIfNotNull();
                return freeRef;
            }
            else
                return new SceneContainerBinReference();
        }
        public override void Dispose()
        {
            _IsDisposed = true;
            for (uint i = 0; i < _sceneBinArray.Length; i++)
            {
                if (_sceneBinArray[i] != null)
                    _sceneBinArray[i].Reset(); // Just to make sure to not create a circular reference of disposed objects
                _sceneBinArray[i] = null;
            }

            _ResetRefs();
            _sceneOverflowBin.Reset(); // Just to make sure to not create a circular reference of disposed objects
            _sceneOverflowBin = null;
            _sceneBinArray = null;
            _sg.Reset();
            _sg = null;
            base.Dispose();
        }