From 54463ab6687b1585ed071b0e04b6b5e4c0124267 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Wed, 3 Sep 2014 13:17:47 -0700 Subject: [PATCH] add findContentInCube() and typedef CubeList --- libraries/octree/src/Octree.cpp | 36 +++++++++++++++++++++++++++++++++ libraries/octree/src/Octree.h | 4 ++++ 2 files changed, 40 insertions(+) diff --git a/libraries/octree/src/Octree.cpp b/libraries/octree/src/Octree.cpp index c11d23c2ec..2cf90fcb90 100644 --- a/libraries/octree/src/Octree.cpp +++ b/libraries/octree/src/Octree.cpp @@ -19,6 +19,7 @@ #include // to load voxels from file #include +#include #include #include @@ -730,6 +731,12 @@ public: bool found; }; +class ContentArgs { +public: + AACube cube; + CubeList* cubes; +}; + bool findCapsulePenetrationOp(OctreeElement* element, void* extraData) { CapsuleArgs* args = static_cast(extraData); @@ -772,6 +779,25 @@ bool findShapeCollisionsOp(OctreeElement* element, void* extraData) { return false; } +bool findContentInCubeOp(OctreeElement* element, void* extraData) { + ContentArgs* args = static_cast(extraData); + + // coarse check against bounds + AACube cube = element->getAACube(); + cube.scale(TREE_SCALE); + if (!cube.touches(args->cube)) { + return false; + } + if (!element->isLeaf()) { + return true; // recurse on children + } + if (element->hasContent()) { + args->cubes->push_back(cube); + return true; + } + return false; +} + bool Octree::findCapsulePenetration(const glm::vec3& start, const glm::vec3& end, float radius, glm::vec3& penetration, Octree::lockType lockType, bool* accurateResult) { @@ -840,6 +866,16 @@ bool Octree::findShapeCollisions(const Shape* shape, CollisionList& collisions, return args.found; } +bool Octree::findContentInCube(const AACube& cube, CubeList& cubes) { + if (!tryLockForRead()) { + return false; + } + ContentArgs args = { cube, &cubes }; + recurseTreeWithOperation(findContentInCubeOp, &args); + unlock(); + return true; +} + class GetElementEnclosingArgs { public: OctreeElement* element; diff --git a/libraries/octree/src/Octree.h b/libraries/octree/src/Octree.h index 7ab22598ef..dc11bb8d1c 100644 --- a/libraries/octree/src/Octree.h +++ b/libraries/octree/src/Octree.h @@ -35,6 +35,7 @@ class Shape; #include #include +#include /// derive from this class to use the Octree::recurseTreeWithOperator() method class RecurseOctreeOperator { @@ -46,6 +47,7 @@ public: // Callback function, for recuseTreeWithOperation typedef bool (*RecurseOctreeOperation)(OctreeElement* element, void* extraData); typedef enum {GRADIENT, RANDOM, NATURAL} creationMode; +typedef QVector CubeList; const bool NO_EXISTS_BITS = false; const bool WANT_EXISTS_BITS = true; @@ -280,6 +282,8 @@ public: bool findShapeCollisions(const Shape* shape, CollisionList& collisions, Octree::lockType = Octree::TryLock, bool* accurateResult = NULL); + bool findContentInCube(const AACube& cube, CubeList& cubes); + OctreeElement* getElementEnclosingPoint(const glm::vec3& point, Octree::lockType lockType = Octree::TryLock, bool* accurateResult = NULL);