From 3ed0b34b6354f723b314e2b544380ad0d5f08f91 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Thu, 19 Jun 2014 14:39:22 -0700 Subject: [PATCH] optimized findModelByID --- libraries/models/src/ModelTree.cpp | 48 +++--------------------------- libraries/models/src/ModelTree.h | 4 +-- 2 files changed, 6 insertions(+), 46 deletions(-) diff --git a/libraries/models/src/ModelTree.cpp b/libraries/models/src/ModelTree.cpp index e22e6838da..3e0daab64a 100644 --- a/libraries/models/src/ModelTree.cpp +++ b/libraries/models/src/ModelTree.cpp @@ -492,52 +492,12 @@ void ModelTree::findModels(const AACube& cube, QVector foundModels) foundModels.swap(args._foundModels); } -class FindByIDArgs { -public: - uint32_t id; - bool found; - const ModelItem* foundModel; -}; - - -bool ModelTree::findByIDOperation(OctreeElement* element, void* extraData) { -//qDebug() << "ModelTree::findByIDOperation()...."; - - FindByIDArgs* args = static_cast(extraData); - ModelTreeElement* modelTreeElement = static_cast(element); - - // if already found, stop looking - if (args->found) { - return false; - } - - // as the tree element if it has this model - const ModelItem* foundModel = modelTreeElement->getModelWithID(args->id); - if (foundModel) { - args->foundModel = foundModel; - args->found = true; - return false; - } - - // keep looking - return true; +const ModelItem* ModelTree::findModelByID(uint32_t id, bool alreadyLocked) const { + ModelItemID modelID(id); + return findModelByModelItemID(modelID); } - -const ModelItem* ModelTree::findModelByID(uint32_t id, bool alreadyLocked) { - FindByIDArgs args = { id, false, NULL }; - - if (!alreadyLocked) { - lockForRead(); - } - recurseTreeWithOperation(findByIDOperation, &args); - if (!alreadyLocked) { - unlock(); - } - return args.foundModel; -} - -const ModelItem* ModelTree::findModelByModelItemID(const ModelItemID& modelID) { +const ModelItem* ModelTree::findModelByModelItemID(const ModelItemID& modelID) const { const ModelItem* foundModel = NULL; ModelTreeElement* containingElement = getContainingElement(modelID); if (containingElement) { diff --git a/libraries/models/src/ModelTree.h b/libraries/models/src/ModelTree.h index e44485f29c..a41865af7d 100644 --- a/libraries/models/src/ModelTree.h +++ b/libraries/models/src/ModelTree.h @@ -55,8 +55,8 @@ public: void addModel(const ModelItemID& modelID, const ModelItemProperties& properties); void deleteModel(const ModelItemID& modelID); const ModelItem* findClosestModel(glm::vec3 position, float targetRadius); - const ModelItem* findModelByID(uint32_t id, bool alreadyLocked = false); - const ModelItem* findModelByModelItemID(const ModelItemID& modelID); + const ModelItem* findModelByID(uint32_t id, bool alreadyLocked = false) const; + const ModelItem* findModelByModelItemID(const ModelItemID& modelID) const; /// finds all models that touch a sphere /// \param center the center of the sphere