34 #include <tbb/atomic.h>
36 #include <cmdLine/cmdStdOutput.h>
39 #include <boost/unordered_map.hpp>
40 #include <tbb/spin_rw_mutex.h>
48 #define OCTREE_DEBUG 0
52 static int octree_debug_level = 0;
61 vsprintf(buffer, fmt, ap);
64 DtInfo(
"OCT %*s%s\n", octree_debug_level*2,
"", buffer);
67 # define DEBUG_PUSH do { octree_debug_level++; } while(0)
68 # define DEBUG_POP do { octree_debug_level--; } while(0)
74 # define DEBUG_PUSH do { } while(0)
75 # define DEBUG_POP do { } while(0)
89 #define EXT_FMT "[%lf, %lf, %lf] - [%lf, %lf, %lf]"
90 #define EXT_ARGS(x) (x).minX(), (x).minY(), (x).minZ(), (x).maxX(), (x).maxY(), (x).maxZ()
91 #define OBJ_ARGS(x) ((const char*) (x)->objectName())
122 boost::unordered_map<unsigned int, unsigned int>::const_iterator iter =
mySearchKeys.find(threadId);
124 if ((iter !=
mySearchKeys.end()) && (key == iter->second))
129 lock.upgrade_to_writer();
146 typedef std::pair<DtOctreeNode<T>*,
typename std::list<DtOctreeMember*>::iterator>
NodeBinding;
255 if ((*iter)->testKey(key))
257 if (!(*iter)->myObject->isDeleted() && callback((*iter)->myObject))
274 if (
myChildren[i] && (mask & theBitmaskList[i]))
276 myChildren[i]->visitObjects(key, extent, callback);
310 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
312 if (mask & theBitmaskList[childIndex])
321 DEBUG_OUTPUT(
"add as child member (not enough child members yet)");
376 double numBuckets = 0;
381 if ((*iter)->myExtent == matchExtent)
386 matchExtent = (*iter)->myExtent;
390 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
392 if (mask & theBitmaskList[childIndex])
398 retVal += (1.0 / numBuckets);
400 if (retVal >= MAX_CHILD_MEMBERS)
475 myChildren[childIndex]->addMember(member, depth);
510 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
512 if (mask & theBitmaskList[childIndex])
578 if (!theBitmaskListCreated)
580 theBitmaskListCreated =
true;
581 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
583 theBitmaskList[childIndex] = (1 << childIndex);
596 delete (*iter).second;
605 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
632 tbb::spin_rw_mutex::scoped_lock lock(
myMutex);
645 DtWarn(
"Member %s (" EXT_FMT ") added outside octree (" EXT_FMT ")\n",
659 tbb::spin_rw_mutex::scoped_lock* lock = 0;
663 lock =
new tbb::spin_rw_mutex::scoped_lock(
myMutex,
true);
699 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
705 T*
object = (*iter).first;
707 if (object->isValid())
710 DtExtent roughExtent =
object->extent();
713 if (member->
myExtent != roughExtent)
721 DtWarn(
"Object %s (" EXT_FMT ") moving outside octree (" EXT_FMT ")\n",
723 lock.upgrade_to_writer();
726 lock.downgrade_to_reader();
747 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
823 mySeenObjects.insert(
object);
824 return myInner(
object);
827 std::set<const DtSimObject*> mySeenObjects;
831 template<
class T>
class DtOctreeDebugger :
public DtOctree<T>
834 DtOctreeDebugger(
const DtExtent& extent)
840 allObjects[object] = extent;
846 allObjects.erase(
object);
852 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
853 std::map<T*, DtExtent>::iterator end = allObjects.end();
855 for(; iter != end; ++iter)
857 T*
object = (*iter).first;
860 extent =
object->roughExtent();
868 DtDebugOctreeFunctor debugFunctor(callback);
871 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
872 std::map<T*, DtExtent>::iterator end = allObjects.end();
874 for(; iter != end; ++iter)
876 T*
object = (*iter).first;
877 const DtExtent& objectExtent = (*iter).second;
879 if (objectExtent.
intersects(extent) && debugFunctor.mySeenObjects.count(
object) == 0)
890 std::map<T*, DtExtent> allObjects;
897 return new DtOctreeDebugger<DtSimObject>(extent);
910 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
912 if (node == (*it).first)
923 for (
typename std::list<NodeBinding>::iterator it = myBindings.begin(); it != myBindings.end(); ++it)
925 if (node == (*it).first)
928 myBindings.erase(it);
936 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
938 (*it).first->myMembers.erase((*it).second);
const double & x() const
Definition: point.h:188
static const int NUM_BRANCHES
The number of children of each node. Don't change this without rewriting everything.
Definition: octree.h:79
virtual bool contains(const DtExtent &otherExtent) const
Determine if another extents box is entirely contained by this one. On the border counts as "containe...
virtual ~DtOctree()
Destructor. Deletes the octree and any members in it (though not the corresponding objects)...
Definition: octree.h:589
T * myObject
The object corresponding to this member.
Definition: octree.h:141
DtExtent myExtent
The extent of the member at the time it was added or last updated.
Definition: octree.h:144
void unbind()
Remove this member from all nodes.
Definition: octree.h:934
static bool theBitmaskListCreated
Definition: octree.h:87
bool coalesceEmptyChildren()
Look for any empty, childless nodes and coalesce them up to their parents. Returns true if this node ...
Definition: octree.h:337
void split(int &depth)
Create child nodes to hold all the members that can go into child nodes, and move those members into ...
Definition: octree.h:482
bool removeObjectOutsideArea(T *object)
Definition: octree.h:777
DtOctreeNode(const DtExtent ®ion, const unsigned int depth)
Constructor. The extent covered by an octree node may not be changed once the octree is created...
Definition: octree.h:207
friend class DtOctreeDebugger
Definition: octree.h:543
MemberContainer myMemberMap
Mapping of T* pointers to DtOctreeMember objects in the tree.
Definition: octree.h:800
DtOctreeNode< T > * myRootNode
Root node of the octree.
Definition: octree.h:794
DtSpatialSelectionFunctorTemplate< T > SpatialSelectionFunctor
Definition: octree.h:571
#define EXT_FMT
Definition: octree.h:89
tbb::spin_rw_mutex myMutex
Definition: octree.h:792
The DtSpatialVrfObjectManager is responsible for maintaining a spatial organization of all specified ...
Definition: spatialVrfObjectManager.h:31
DtOctree class.
Definition: octree.h:560
DtOctreeNode< T > ** myChildren
Child nodes of this node, if any. This is NULL for a childless node. For nodes with children...
Definition: octree.h:535
The DtExtent represents an axis-aligned 3d bounding box.
Definition: extent.h:43
virtual void addMember(DtOctreeMember< T > *member, int &depth)
Add a member to this node or one of its children (splitting the node if necessary).
Definition: octree.h:285
static const int MAX_BRANCH_DEPTH
Definition: octree.h:81
static void DEBUG_OUTPUT(const char *fmt,...)
Definition: octree.h:71
#define DEBUG_POP
Definition: octree.h:75
tbb::atomic< unsigned int > mySearchKey
Definition: octree.h:803
#define OBJ_ARGS(x)
Definition: octree.h:91
int numObjects()
Definition: octree.h:766
Octree support.
Definition: octree.h:43
double maxZ() const
Access and/or mutate individual values.
double minZ() const
Access and/or mutate individual values.
DtOctreeMember class.
Definition: octree.h:99
#define DEBUG_PUSH
Definition: octree.h:74
virtual bool operator()(const T *object)=0
Abstract function call interface.
DtOctree(const DtExtent ®ion)
Constructor. The extent covered by an octree may not be changed once the octree is created...
Definition: octree.h:574
bool intersects(const DtExtent &otherExtent) const
Determine if the two extents overlap (either partially or fully). A shared "wall" counts as intersect...
static const int MAX_NUM_NODES
Definition: octree.h:204
int numObjectsOutside()
Definition: octree.h:771
bool containsObject(T *object)
Definition: octree.h:745
~DtOctreeNode()
Destructor. No DtOctreeMember objects are deleted.
Definition: octree.h:219
static DtOctree< const DtSimObject > * createOctree(const DtExtent &extent)
Definition: spatialVrfObjectManager.h:295
void bind(DtOctreeNode< T > *node)
Add this member to the specified node.
Definition: octree.h:908
double minX() const
Access and/or mutate individual values.
const DtExtent myRegion
The region intersecting members contained by this node (and this node's children).
Definition: octree.h:524
boost::unordered_map< unsigned int, unsigned int > mySearchKeys
Definition: octree.h:153
virtual void addObject(T *object, const DtExtent &extent)
TODO: Add search key to outside octree list so that objects which fall completely outside the octree ...
Definition: octree.h:627
std::list< NodeBinding > myBindings
A list of the nodes this member is bound to, if any, and iterators to this member's position in the b...
Definition: octree.h:150
double minY() const
Access and/or mutate individual values.
void setY(const double &y)
Definition: point.h:203
virtual bool operator()(const DtSimObject *object)=0
Abstract function call interface.
tbb::spin_rw_mutex mySearchKeysMutex
Definition: octree.h:154
std::pair< DtOctreeNode< T > *, typename std::list< DtOctreeMember * >::iterator > NodeBinding
Definition: octree.h:146
const DtPoint myCenter
The center of myRegion.
Definition: octree.h:527
virtual void visitObjects(const unsigned int key, const DtExtent &extent, DtSpatialSelectionFunctorTemplate< T > &callback)
Call the given functor for every member that shares a node with the specified extent. This does not test for exact intersection; it only returns a list of objects that might intersect the given extent.
Definition: octree.h:245
const double & y() const
Definition: point.h:198
virtual ~DtSpatialSelectionFunctorTemplate()
Virtual Destructor which does nothing.
Definition: octree.h:163
std::list< DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:201
bool testKey(const unsigned int key)
Definition: octree.h:117
Represents a single simulation object in the exercise. Object may be VRF simulation, or non-VRF simulated, and may be simulated by the local sim engine, or by a remote sim engine. Only public (external) state of the object is available. All data is read-only. All access to data of the object is thread safe within the simulation frame.
Definition: simObject.h:79
int numChildCandidates()
Return the number of members of this node that can go into child nodes.
Definition: octree.h:368
std::list< DtOctreeMember< T > * > MemberContainer
This class is only for use from DtOctree and DtOctreeDebugger.
Definition: octree.h:200
Contains the declaration of the DtExtent class.
static int theBitmaskList[NUM_BRANCHES]
Definition: octree.h:86
std::list< T * > OutsideAreaContainer
Definition: octree.h:568
MemberContainer myMembers
Members which are in this node. If myChildren is not NULL, then this must not contain any members tha...
Definition: octree.h:539
unsigned int myDepth
Definition: octree.h:530
virtual void visitObjects(const DtExtent &extent, DtSpatialSelectionFunctorTemplate< T > &callback)
Call the given functor for every member that shares a node with the specified extent. This does not test for exact intersection; it only iterates over any objects that might intersect the given extent (i.e. there may be extras).
Definition: octree.h:603
double maxX() const
Access and/or mutate individual values.
void setZ(const double &z)
Definition: point.h:213
virtual void updatePositions()
Process any updates to the objects' positions that have happened since they were added to the octree...
Definition: octree.h:697
void setX(const double &x)
Definition: point.h:193
const double & z() const
Definition: point.h:208
double maxY() const
Access and/or mutate individual values.
virtual void removeObject(T *object, bool needsLock=true)
Remove a member from the octree.
Definition: octree.h:654
void addMemberToChild(DtOctreeMember< T > *member, int childIndex, int &depth)
Add a member to a child of this node, creating the child if necessary. This function will call addMem...
Definition: octree.h:448
std::map< T *, DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:566
std::map< T *, DtOctreeMember< T > * > MemberContainer
Container and iterator typedefs for the STL structures used.
Definition: octree.h:565
OutsideAreaContainer myObjectsOutsideArea
List of objects which are at least partially outside the octree's root extent.
Definition: octree.h:797
#define EXT_ARGS(x)
Definition: octree.h:90
std::list< T * >::iterator OutsideAreaIterator
Definition: octree.h:569
int intersectionMask(const DtExtent &extent) const
Returns a bit-mask where the ith bit is set iff the ith child of this node intersects the specified e...
Definition: octree.h:415
DtOctreeMember(T *object, const DtExtent &extent)
Definition: octree.h:102
static const int MAX_CHILD_MEMBERS
The maximum length of myChildMembers; see implementation notes at the top of the class.
Definition: octree.h:84
static int sNumNodes
Definition: octree.h:203
void unbind(DtOctreeNode< T > *node)
Remove this member from the specified node.
Definition: octree.h:921