// does this entity tree element contain the old entity
bool MovingEntitiesOperator::shouldRecurseSubTree(const OctreeElementPointer& element) {
    bool containsEntity = false;

    // If we don't have an old entity, then we don't contain the entity, otherwise
    // check the bounds
    if (_entitiesToMove.size() > 0) {
        const AACube& elementCube = element->getAACube();
        int detailIndex = 0;
        foreach(const EntityToMoveDetails& details, _entitiesToMove) {

            if (_wantDebug) {
                qCDebug(entities) << "MovingEntitiesOperator::shouldRecurseSubTree() details["<< detailIndex <<"]-----------------------------";
                qCDebug(entities) << "    element:" << element->getAACube();
                qCDebug(entities) << "    details.entity:" << details.entity->getEntityItemID();
                qCDebug(entities) << "    details.oldContainingElementCube:" << details.oldContainingElementCube;
                qCDebug(entities) << "    details.newCube:" << details.newCube;
                qCDebug(entities) << "    details.newCubeClamped:" << details.newCubeClamped;
                qCDebug(entities) << "    elementCube.contains(details.newCube)" << elementCube.contains(details.newCube);
                qCDebug(entities) << "    elementCube.contains(details.newCubeClamped)" << elementCube.contains(details.newCubeClamped);
                qCDebug(entities) << "--------------------------------------------------------------------------";
            }

            if (elementCube.contains(details.oldContainingElementCube) || elementCube.contains(details.newCubeClamped)) {
                containsEntity = true;
                break; // if it contains at least one, we're good to go
            }
            detailIndex++;
        }
    }
    return containsEntity;
}
bool UpdateEntityOperator::subTreeContainsNewEntity(OctreeElementPointer element) {
    bool elementContainsNewBox = element->getAACube().contains(_newEntityBox);

    if (_wantDebug) {
        bool elementContainsNewCube = element->getAACube().contains(_newEntityCube);
        qCDebug(entities) << "UpdateEntityOperator::subTreeContainsNewEntity()....";
        qCDebug(entities) << "    element->getAACube()=" << element->getAACube();
        qCDebug(entities) << "    _newEntityCube=" << _newEntityCube;
        qCDebug(entities) << "    _newEntityBox=" << _newEntityBox;
        qCDebug(entities) << "    elementContainsNewCube=" << elementContainsNewCube;
        qCDebug(entities) << "    elementContainsNewBox=" << elementContainsNewBox;
    }

    return elementContainsNewBox;
}
Esempio n. 3
0
bool AddEntityOperator::preRecursion(const OctreeElementPointer& element) {
    EntityTreeElementPointer entityTreeElement = std::static_pointer_cast<EntityTreeElement>(element);

    // In Pre-recursion, we're generally deciding whether or not we want to recurse this
    // path of the tree. For this operation, we want to recurse the branch of the tree if
    // any of the following are true:
    //   * We have not yet found the location for the new entity, and this branch contains the bounds of the new entity
    
    bool keepSearching = false; // assume we don't need to search any more
    
    // If we haven't yet found the new entity,  and this subTreeContains our new
    // entity, then we need to keep searching.
    if (!_foundNew && element->getAACube().contains(_newEntityBox)) {

        // If this element is the best fit for the new entity properties, then add/or update it
        if (entityTreeElement->bestFitBounds(_newEntityBox)) {
            _tree->addEntityMapEntry(_newEntity);
            entityTreeElement->addEntityItem(_newEntity);
            _foundNew = true;
            keepSearching = false;
        } else {
            keepSearching = true;
        }
    }
    
    return keepSearching; // if we haven't yet found it, keep looking
}
// does this entity tree element contain the old entity
bool UpdateEntityOperator::subTreeContainsOldEntity(OctreeElementPointer element) {

    // We've found cases where the old entity might be placed in an element that is not actually the best fit
    // so when we're searching the tree for the old element, we use the known cube for the known containing element
    bool elementContainsOldBox = element->getAACube().contains(_containingElementCube);

    if (_wantDebug) {
        bool elementContainsOldCube = element->getAACube().contains(_oldEntityCube);
        qCDebug(entities) << "UpdateEntityOperator::subTreeContainsOldEntity()....";
        qCDebug(entities) << "    element->getAACube()=" << element->getAACube();
        qCDebug(entities) << "    _oldEntityCube=" << _oldEntityCube;
        qCDebug(entities) << "    _oldEntityBox=" << _oldEntityBox;
        qCDebug(entities) << "    elementContainsOldCube=" << elementContainsOldCube;
        qCDebug(entities) << "    elementContainsOldBox=" << elementContainsOldBox;
    }
    return elementContainsOldBox;
}
Esempio n. 5
0
bool AddEntityOperator::postRecursion(const OctreeElementPointer& element) {
    // Post-recursion is the unwinding process. For this operation, while we
    // unwind we want to mark the path as being dirty if we changed it below.
    // We might have two paths, one for the old entity and one for the new entity.
    bool keepSearching = !_foundNew;

    // As we unwind, if we're in either of these two paths, we mark our element
    // as dirty.
    if ((_foundNew && element->getAACube().contains(_newEntityBox))) {
        element->markWithChangedTime();
    }
    return keepSearching; // if we haven't yet found it, keep looking
}
Esempio n. 6
0
// does this entity tree element contain the old entity
bool DeleteEntityOperator::subTreeContainsSomeEntitiesToDelete(const OctreeElementPointer& element) {
    bool containsEntity = false;

    // If we don't have an old entity, then we don't contain the entity, otherwise
    // check the bounds
    if (_entitiesToDelete.size() > 0) {
        const AACube& elementCube = element->getAACube();
        foreach(const EntityToDeleteDetails& details, _entitiesToDelete) {
            if (elementCube.contains(details.cube)) {
                containsEntity = true;
                break; // if it contains at least one, we're good to go
            }
        }
    }
    return containsEntity;
}
Esempio n. 7
0
OctreeElementPointer AddEntityOperator::possiblyCreateChildAt(const OctreeElementPointer& element, int childIndex) {
    // If we're getting called, it's because there was no child element at this index while recursing.
    // We only care if this happens while still searching for the new entity location.
    // Check to see if 
    if (!_foundNew) {
        float childElementScale = element->getAACube().getScale() / 2.0f; // all of our children will be half our scale
        // if the scale of our desired cube is smaller than our children, then consider making a child
        if (_newEntityBox.getLargestDimension() <= childElementScale) {
            int indexOfChildContainingNewEntity = element->getMyChildContaining(_newEntityBox);
        
            if (childIndex == indexOfChildContainingNewEntity) {
                return element->addChildAtIndex(childIndex);
            }
        }
    }
    return NULL; 
}
bool UpdateEntityOperator::preRecursion(OctreeElementPointer element) {
    EntityTreeElementPointer entityTreeElement = std::static_pointer_cast<EntityTreeElement>(element);
    
    // In Pre-recursion, we're generally deciding whether or not we want to recurse this
    // path of the tree. For this operation, we want to recurse the branch of the tree if
    // and of the following are true:
    //   * We have not yet found the old entity, and this branch contains our old entity
    //   * We have not yet found the new entity, and this branch contains our new entity
    //
    // Note: it's often the case that the branch in question contains both the old entity
    // and the new entity.
    
    bool keepSearching = false; // assume we don't need to search any more

    bool subtreeContainsOld = subTreeContainsOldEntity(element);
    bool subtreeContainsNew = subTreeContainsNewEntity(element);

    if (_wantDebug) {
        qCDebug(entities) << "---- UpdateEntityOperator::preRecursion().... ----";
        qCDebug(entities) << "    element=" << element->getAACube();
        qCDebug(entities) << "    subtreeContainsOld=" << subtreeContainsOld;
        qCDebug(entities) << "    subtreeContainsNew=" << subtreeContainsNew;
        qCDebug(entities) << "    _foundOld=" << _foundOld;
        qCDebug(entities) << "    _foundNew=" << _foundNew;
    }

    // If we haven't yet found the old entity, and this subTreeContains our old
    // entity, then we need to keep searching.
    if (!_foundOld && subtreeContainsOld) {

        if (_wantDebug) {
            qCDebug(entities) << "    OLD TREE CASE....";
            qCDebug(entities) << "    entityTreeElement=" << entityTreeElement.get();
            qCDebug(entities) << "    _containingElement=" << _containingElement.get();
        }

        // If this is the element we're looking for, then ask it to remove the old entity
        // and we can stop searching.
        if (entityTreeElement == _containingElement) {

            if (_wantDebug) {
                qCDebug(entities) << "    *** it's the OLD ELEMENT! ***";
            }

            // If the containgElement IS NOT the best fit for the new entity properties
            // then we need to remove it, and the updateEntity below will store it in the
            // correct element.
            if (_removeOld) {

                if (_wantDebug) {
                    qCDebug(entities) << "    *** REMOVING from ELEMENT ***";
                }

                // the entity knows what element it's in, so we remove it from that one
                // NOTE: we know we haven't yet added it to its new element because _removeOld is true
                EntityTreeElementPointer oldElement = _existingEntity->getElement();
                oldElement->removeEntityItem(_existingEntity);
                _tree->setContainingElement(_entityItemID, NULL);

                if (oldElement != _containingElement) {
                    qCDebug(entities) << "WARNING entity moved during UpdateEntityOperator recursion";
                    _containingElement->removeEntityItem(_existingEntity);
                }

                if (_wantDebug) {
                    qCDebug(entities) << "    *** REMOVING from MAP ***";
                }
            }
            _foundOld = true;
        } else {
            // if this isn't the element we're looking for, then keep searching
            keepSearching = true;
        }
    }

    // If we haven't yet found the new entity,  and this subTreeContains our new
    // entity, then we need to keep searching.
    if (!_foundNew && subtreeContainsNew) {

        if (_wantDebug) {
            qCDebug(entities) << "    NEW TREE CASE....";
            qCDebug(entities) << "    entityTreeElement=" << entityTreeElement.get();
            qCDebug(entities) << "    _containingElement=" << _containingElement.get();
            qCDebug(entities) << "    entityTreeElement->bestFitBounds(_newEntityBox)=" << entityTreeElement->bestFitBounds(_newEntityBox);
        }

        // If this element is the best fit for the new entity properties, then add/or update it
        if (entityTreeElement->bestFitBounds(_newEntityBox)) {

            if (_wantDebug) {
                qCDebug(entities) << "    *** THIS ELEMENT IS BEST FIT ***";
            }

            EntityTreeElementPointer oldElement = _existingEntity->getElement();
            // if we are the existing containing element, then we can just do the update of the entity properties
            if (entityTreeElement == oldElement) {

                if (_wantDebug) {
                    qCDebug(entities) << "    *** This is the same OLD ELEMENT ***";
                }
            
                // set the entity properties and mark our element as changed.
                _existingEntity->setProperties(_properties);
                if (_wantDebug) {
                    qCDebug(entities) << "    *** set properties ***";
                }
            } else {
                // otherwise, this is an add case.
                if (oldElement) {
                    oldElement->removeEntityItem(_existingEntity);
                    if (oldElement != _containingElement) {
                        qCDebug(entities) << "WARNING entity moved during UpdateEntityOperator recursion";
                    }
                }
                entityTreeElement->addEntityItem(_existingEntity);
                _tree->setContainingElement(_entityItemID, entityTreeElement);

                _existingEntity->setProperties(_properties); // still need to update the properties!
                if (_wantDebug) {
                    qCDebug(entities) << "    *** ADDING ENTITY to ELEMENT and MAP and SETTING PROPERTIES ***";
                }
            }
            _foundNew = true; // we found the new element
            _removeOld = false; // and it has already been removed from the old
        } else {
            keepSearching = true;
        }
    }

    if (_wantDebug) {
        qCDebug(entities) << "    FINAL --- keepSearching=" << keepSearching;
        qCDebug(entities) << "--------------------------------------------------";
    }

    
    return keepSearching; // if we haven't yet found it, keep looking
}
bool DirtyOctreeElementOperator::preRecursion(const OctreeElementPointer& element) {
    if (element == _element) {
        return false;
    }
    return element->getAACube().contains(_point);
}