From 6ce6d8ae477ddc389f5325135d42c2f2953018b2 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Tue, 12 Aug 2014 14:29:55 -0700 Subject: [PATCH] dont try to create child elements that are smaller than the entities --- libraries/entities/src/EntityTree.cpp | 22 +++++++--- .../entities/src/MovingEntitiesOperator.cpp | 40 +++++++++++++++---- libraries/octree/src/OctreeElement.cpp | 14 ++++--- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index 5255310664..335e0178ca 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -155,10 +155,14 @@ OctreeElement* AddEntityOperator::PossiblyCreateChildAt(OctreeElement* element, // We only care if this happens while still searching for the new entity location. // Check to see if if (!_foundNew) { - int indexOfChildContainingNewEntity = element->getMyChildContaining(_newEntityBox); + 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); + if (childIndex == indexOfChildContainingNewEntity) { + return element->addChildAtIndex(childIndex); + } } } return NULL; @@ -488,10 +492,16 @@ OctreeElement* UpdateEntityOperator::PossiblyCreateChildAt(OctreeElement* elemen // We only care if this happens while still searching for the new entity location. // Check to see if if (!_foundNew) { - int indexOfChildContainingNewEntity = element->getMyChildContaining(_newEntityCube); + + 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 (_newEntityCube.getScale() <= childElementScale) { + //qDebug() << "UpdateEntityOperator::PossiblyCreateChildAt().... calling element->getMyChildContaining(_newEntityCube);"; + int indexOfChildContainingNewEntity = element->getMyChildContaining(_newEntityCube); - if (childIndex == indexOfChildContainingNewEntity) { - return element->addChildAtIndex(childIndex); + if (childIndex == indexOfChildContainingNewEntity) { + return element->addChildAtIndex(childIndex); + } } } return NULL; diff --git a/libraries/entities/src/MovingEntitiesOperator.cpp b/libraries/entities/src/MovingEntitiesOperator.cpp index f85c1ba190..47b6c447f2 100644 --- a/libraries/entities/src/MovingEntitiesOperator.cpp +++ b/libraries/entities/src/MovingEntitiesOperator.cpp @@ -127,21 +127,45 @@ bool MovingEntitiesOperator::PostRecursion(OctreeElement* element) { return keepSearching; // if we haven't yet found it, keep looking } -OctreeElement* MovingEntitiesOperator::PossiblyCreateChildAt(OctreeElement* element, int childIndex) { +OctreeElement* MovingEntitiesOperator::PossiblyCreateChildAt(OctreeElement* element, int childIndex) { + bool wantDebug = false; + + if (wantDebug) { + qDebug() << "MovingEntitiesOperator::PossiblyCreateChildAt().... "; + qDebug() << " _foundNewCount=" << _foundNewCount; + qDebug() << " _lookingCount=" << _lookingCount; + } + // 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 locations. if (_foundNewCount < _lookingCount) { + + float childElementScale = element->getAACube().getScale() / 2.0f; // all of our children will be half our scale // check against each of our entities foreach(const EntityToMoveDetails& details, _entitiesToMove) { - int indexOfChildContainingNewEntity = element->getMyChildContaining(details.newCube); + EntityTreeElement* entityTreeElement = static_cast(element); - // If the childIndex we were asked if we wanted to create contains this newCube, - // then we will create this branch and continue. We can exit this loop immediately - // because if we need this branch for any one entity then it doesn't matter if it's - // needed for more entities. - if (childIndex == indexOfChildContainingNewEntity) { - return element->addChildAtIndex(childIndex); + bool thisElementIsBestFit = entityTreeElement->bestFitBounds(details.newCube); + if (wantDebug) { + qDebug() << " thisElementIsBestFit=" << thisElementIsBestFit; + qDebug() << " details.newCube=" << details.newCube; + qDebug() << " element->getAACube()=" << element->getAACube(); + } + + // if the scale of our desired cube is smaller than our children, then consider making a child + if (details.newCube.getScale() <= childElementScale) { + //qDebug() << " calling element->getMyChildContaining(details.newCube); ---------"; + int indexOfChildContainingNewEntity = element->getMyChildContaining(details.newCube); + //qDebug() << " called element->getMyChildContaining(details.newCube); ---------"; + + // If the childIndex we were asked if we wanted to create contains this newCube, + // then we will create this branch and continue. We can exit this loop immediately + // because if we need this branch for any one entity then it doesn't matter if it's + // needed for more entities. + if (childIndex == indexOfChildContainingNewEntity) { + return element->addChildAtIndex(childIndex); + } } } } diff --git a/libraries/octree/src/OctreeElement.cpp b/libraries/octree/src/OctreeElement.cpp index 2cc1f0b880..dd7d569bc5 100644 --- a/libraries/octree/src/OctreeElement.cpp +++ b/libraries/octree/src/OctreeElement.cpp @@ -1489,14 +1489,18 @@ int OctreeElement::getMyChildContaining(const AACube& cube) const { float cubeScale = cube.getScale(); // TODO: consider changing this to assert() - if(cubeScale > ourScale) { - qDebug("UNEXPECTED -- OctreeElement::getMyChildContaining() " - "cubeScale=[%f] > ourScale=[%f] ", cubeScale, ourScale); + if (cubeScale > ourScale) { + qDebug() << "UNEXPECTED -- OctreeElement::getMyChildContaining() -- (cubeScale > ourScale)"; + qDebug() << " cube=" << cube; + qDebug() << " elements AACube=" << getAACube(); + qDebug() << " cubeScale=" << cubeScale; + qDebug() << " ourScale=" << ourScale; + assert(false); } // Determine which of our children the minimum and maximum corners of the cube live in... - glm::vec3 cubeCornerMinimum = cube.getCorner(); - glm::vec3 cubeCornerMaximum = cube.calcTopFarLeft(); + glm::vec3 cubeCornerMinimum = glm::clamp(cube.getCorner(), 0.0f, 1.0f); + glm::vec3 cubeCornerMaximum = glm::clamp(cube.calcTopFarLeft(), 0.0f, 1.0f); int childIndexCubeMinimum = getMyChildContainingPoint(cubeCornerMinimum); int childIndexCubeMaximum = getMyChildContainingPoint(cubeCornerMaximum);