From 49b496046bf9f6493b3e1f75f083938fadb1a2b2 Mon Sep 17 00:00:00 2001 From: SamGondelman Date: Tue, 16 Oct 2018 14:35:39 -0700 Subject: [PATCH 1/2] queryAACube fixes --- interface/src/ui/overlays/Base3DOverlay.cpp | 2 - libraries/entities/src/EntityTree.cpp | 8 ++++ libraries/entities/src/MaterialEntityItem.cpp | 17 +++++++ libraries/entities/src/MaterialEntityItem.h | 2 + libraries/shared/src/SpatiallyNestable.cpp | 47 +++++++++++++------ libraries/shared/src/SpatiallyNestable.h | 1 + 6 files changed, 61 insertions(+), 16 deletions(-) diff --git a/interface/src/ui/overlays/Base3DOverlay.cpp b/interface/src/ui/overlays/Base3DOverlay.cpp index 1d8db69e26..14c1f1a15a 100644 --- a/interface/src/ui/overlays/Base3DOverlay.cpp +++ b/interface/src/ui/overlays/Base3DOverlay.cpp @@ -191,8 +191,6 @@ void Base3DOverlay::setProperties(const QVariantMap& originalProperties) { if (properties["parentID"].isValid()) { setParentID(QUuid(properties["parentID"].toString())); - bool success; - getParentPointer(success); // call this to hook-up the parent's back-pointers to its child overlays needRenderItemUpdate = true; } if (properties["parentJointIndex"].isValid()) { diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index a7c88ddc7d..3803dc1655 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -1933,6 +1933,14 @@ void EntityTree::fixupNeedsParentFixups() { } }); entity->locationChanged(true); + + // Update our parent's bounding box + bool success = false; + auto parent = entity->getParentPointer(success); + if (success && parent) { + parent->updateQueryAACube(); + } + entity->postParentFixup(); } else if (getIsServer() || _avatarIDs.contains(entity->getParentID())) { // this is a child of an avatar, which the entity server will never have diff --git a/libraries/entities/src/MaterialEntityItem.cpp b/libraries/entities/src/MaterialEntityItem.cpp index 06afda4283..f4953ca968 100644 --- a/libraries/entities/src/MaterialEntityItem.cpp +++ b/libraries/entities/src/MaterialEntityItem.cpp @@ -314,8 +314,25 @@ void MaterialEntityItem::applyMaterial() { _retryApply = true; } +AACube MaterialEntityItem::calculateInitialQueryAACube(bool& success) { + AACube aaCube = EntityItem::calculateInitialQueryAACube(success); + // A Material entity's queryAACube contains its parent's queryAACube + auto parent = getParentPointer(success); + if (success && parent) { + success = false; + AACube parentQueryAACube = parent->calculateInitialQueryAACube(success); + if (success) { + aaCube += parentQueryAACube.getMinimumPoint(); + aaCube += parentQueryAACube.getMaximumPoint(); + return aaCube; + } + } +} + void MaterialEntityItem::postParentFixup() { removeMaterial(); + _queryAACubeSet = false; // force an update so we contain our parent + updateQueryAACube(); applyMaterial(); } diff --git a/libraries/entities/src/MaterialEntityItem.h b/libraries/entities/src/MaterialEntityItem.h index 7177aaf718..8aaa833db9 100644 --- a/libraries/entities/src/MaterialEntityItem.h +++ b/libraries/entities/src/MaterialEntityItem.h @@ -85,6 +85,8 @@ public: void postParentFixup() override; + AACube calculateInitialQueryAACube(bool& success) override; + private: // URL for this material. Currently, only JSON format is supported. Set to "materialData" to use the material data to live edit a material. // The following fields are supported in the JSON: diff --git a/libraries/shared/src/SpatiallyNestable.cpp b/libraries/shared/src/SpatiallyNestable.cpp index 4b8768704a..fd2ff6e790 100644 --- a/libraries/shared/src/SpatiallyNestable.cpp +++ b/libraries/shared/src/SpatiallyNestable.cpp @@ -75,7 +75,10 @@ void SpatiallyNestable::setParentID(const QUuid& parentID) { }); bool success = false; - getParentPointer(success); + auto parent = getParentPointer(success); + if (success && parent) { + parent->updateQueryAACube(); + } } Transform SpatiallyNestable::getParentTransform(bool& success, int depth) const { @@ -155,12 +158,14 @@ void SpatiallyNestable::beParentOfChild(SpatiallyNestablePointer newChild) const _childrenLock.withWriteLock([&] { _children[newChild->getID()] = newChild; }); + // Our QueryAACube will automatically be updated to include our new child } void SpatiallyNestable::forgetChild(SpatiallyNestablePointer newChild) const { _childrenLock.withWriteLock([&] { _children.remove(newChild->getID()); }); + _queryAACubeSet = false; // We need to reset our queryAACube when we lose a child } void SpatiallyNestable::setParentJointIndex(quint16 parentJointIndex) { @@ -1092,7 +1097,23 @@ AACube SpatiallyNestable::getMaximumAACube(bool& success) const { return AACube(getWorldPosition(success) - glm::vec3(defaultAACubeSize / 2.0f), defaultAACubeSize); } -const float PARENTED_EXPANSION_FACTOR = 3.0f; +AACube SpatiallyNestable::calculateInitialQueryAACube(bool& success) { + success = false; + AACube maxAACube = getMaximumAACube(success); + if (!success) { + return AACube(); + } + + success = true; + if (shouldPuffQueryAACube()) { + // make an expanded AACube centered on the object + const float PARENTED_EXPANSION_FACTOR = 3.0f; + float scale = PARENTED_EXPANSION_FACTOR * maxAACube.getScale(); + return AACube(maxAACube.calcCenter() - glm::vec3(0.5f * scale), scale); + } else { + return maxAACube; + } +} bool SpatiallyNestable::updateQueryAACube() { if (!queryAACubeNeedsUpdate()) { @@ -1100,20 +1121,12 @@ bool SpatiallyNestable::updateQueryAACube() { } bool success; - AACube maxAACube = getMaximumAACube(success); + AACube initialQueryAACube = calculateInitialQueryAACube(success); if (!success) { return false; } - - if (shouldPuffQueryAACube()) { - // make an expanded AACube centered on the object - float scale = PARENTED_EXPANSION_FACTOR * maxAACube.getScale(); - _queryAACube = AACube(maxAACube.calcCenter() - glm::vec3(0.5f * scale), scale); - _queryAACubeIsPuffed = true; - } else { - _queryAACube = maxAACube; - _queryAACubeIsPuffed = false; - } + _queryAACube = initialQueryAACube; + _queryAACubeIsPuffed = shouldPuffQueryAACube(); forEachDescendant([&](const SpatiallyNestablePointer& descendant) { bool childSuccess; @@ -1128,6 +1141,12 @@ bool SpatiallyNestable::updateQueryAACube() { }); _queryAACubeSet = true; + + auto parent = getParentPointer(success); + if (success && parent) { + parent->updateQueryAACube(); + } + return true; } @@ -1158,7 +1177,7 @@ bool SpatiallyNestable::queryAACubeNeedsUpdate() const { // make sure children are still in their boxes, also. bool childNeedsUpdate = false; forEachDescendantTest([&](const SpatiallyNestablePointer& descendant) { - if (!childNeedsUpdate && descendant->queryAACubeNeedsUpdate()) { + if (descendant->queryAACubeNeedsUpdate() || !_queryAACube.contains(descendant->getQueryAACube())) { childNeedsUpdate = true; // Don't recurse further return false; diff --git a/libraries/shared/src/SpatiallyNestable.h b/libraries/shared/src/SpatiallyNestable.h index a50e687a9b..6949cacd8d 100644 --- a/libraries/shared/src/SpatiallyNestable.h +++ b/libraries/shared/src/SpatiallyNestable.h @@ -112,6 +112,7 @@ public: virtual glm::vec3 getParentAngularVelocity(bool& success) const; virtual AACube getMaximumAACube(bool& success) const; + virtual AACube calculateInitialQueryAACube(bool& success); virtual void setQueryAACube(const AACube& queryAACube); virtual bool queryAACubeNeedsUpdate() const; From cba4a4139a68bad82a226feb96bd8350cb2de32f Mon Sep 17 00:00:00 2001 From: Sam Gondelman Date: Tue, 16 Oct 2018 18:25:17 -0700 Subject: [PATCH 2/2] fix compile error --- libraries/entities/src/MaterialEntityItem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/entities/src/MaterialEntityItem.cpp b/libraries/entities/src/MaterialEntityItem.cpp index f4953ca968..9e9325660b 100644 --- a/libraries/entities/src/MaterialEntityItem.cpp +++ b/libraries/entities/src/MaterialEntityItem.cpp @@ -324,9 +324,9 @@ AACube MaterialEntityItem::calculateInitialQueryAACube(bool& success) { if (success) { aaCube += parentQueryAACube.getMinimumPoint(); aaCube += parentQueryAACube.getMaximumPoint(); - return aaCube; } } + return aaCube; } void MaterialEntityItem::postParentFixup() {