36 #include <cmdLine/cmdStdOutput.h>
43 #include <tbb/spin_rw_mutex.h>
48 #include <unordered_map>
57 #define OCTREE_DEBUG 0
61 static int octree_debug_level = 0;
70 vsprintf(buffer, fmt, ap);
73 DtInfo(
"OCT %*s%s\n", octree_debug_level*2,
"", buffer);
76 # define DEBUG_PUSH do { octree_debug_level++; } while(0)
77 # define DEBUG_POP do { octree_debug_level--; } while(0)
83 # define DEBUG_PUSH do { } while(0)
84 # define DEBUG_POP do { } while(0)
98 #define EXT_FMT "[%lf, %lf, %lf] - [%lf, %lf, %lf]"
99 #define EXT_ARGS(x) (x).minX(), (x).minY(), (x).minZ(), (x).maxX(), (x).maxY(), (x).maxZ()
100 #define OBJ_ARGS(x) ((const char*) (x)->objectName().c_str())
131 std::unordered_map<unsigned int, unsigned int>::const_iterator iter =
mySearchKeys.find(threadId);
133 if ((iter !=
mySearchKeys.end()) && (key == iter->second))
138 lock.upgrade_to_writer();
155 typedef std::pair<DtOctreeNode<T>*,
typename std::list<DtOctreeMember*>::iterator>
NodeBinding;
264 if ((*iter)->testKey(key))
266 if (!(*iter)->myObject->isDeleted() && callback((*iter)->myObject))
283 if (
myChildren[i] && (mask & theBitmaskList[i]))
285 myChildren[i]->visitObjects(key, extent, callback);
319 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
321 if (mask & theBitmaskList[childIndex])
330 DEBUG_OUTPUT(
"add as child member (not enough child members yet)");
385 double numBuckets = 0;
390 if ((*iter)->myExtent == matchExtent)
395 matchExtent = (*iter)->myExtent;
399 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
401 if (mask & theBitmaskList[childIndex])
407 retVal += (1.0 / numBuckets);
409 if (retVal >= MAX_CHILD_MEMBERS)
484 myChildren[childIndex]->addMember(member, depth);
519 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
521 if (mask & theBitmaskList[childIndex])
587 if (!theBitmaskListCreated)
589 theBitmaskListCreated =
true;
590 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
592 theBitmaskList[childIndex] = (1 << childIndex);
605 delete (*iter).second;
614 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
641 tbb::spin_rw_mutex::scoped_lock lock(
myMutex);
644 if (DtDebug.okToPrint())
646 const bool contained =
myRootNode->myRegion.contains(extent);
647 const DtUUID host =
object->embarkedOn();
648 DtDebug(
"[octree addObject] name=%s embarked=%s host=%s\n"
649 " passedExtent = " EXT_FMT "\n"
657 contained ?
"INSIDE" :
"OUTSIDE");
672 DtWarn(
"Member %s (" EXT_FMT ") added outside octree (" EXT_FMT ")\n",
686 tbb::spin_rw_mutex::scoped_lock* lock = 0;
690 lock =
new tbb::spin_rw_mutex::scoped_lock(
myMutex,
true);
726 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
732 T*
object = (*iter).first;
734 if (object->checkValidity())
737 DtExtent roughExtent =
object->extent();
740 if (member->
myExtent != roughExtent)
743 if (DtDebug.okToPrint() &&
object->embarkedOn().isValid())
745 DtDebug(
"[octree updatePositions] EMBARKED %s extent changed to " EXT_FMT " => %s\n",
747 myRootNode->myRegion.contains(roughExtent) ?
"re-place INSIDE" :
"move OUTSIDE");
756 DtWarn(
"Object %s (" EXT_FMT ") moving outside octree (" EXT_FMT ")\n",
758 lock.upgrade_to_writer();
761 lock.downgrade_to_reader();
782 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
858 mySeenObjects.insert(
object);
859 return myInner(
object);
862 std::set<const DtSimObject*> mySeenObjects;
866 template<
class T>
class DtOctreeDebugger :
public DtOctree<T>
869 DtOctreeDebugger(
const DtExtent& extent)
875 allObjects[object] = extent;
881 allObjects.erase(
object);
887 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
888 std::map<T*, DtExtent>::iterator end = allObjects.end();
890 for(; iter != end; ++iter)
892 T*
object = (*iter).first;
895 extent =
object->roughExtent();
903 DtDebugOctreeFunctor debugFunctor(callback);
906 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
907 std::map<T*, DtExtent>::iterator end = allObjects.end();
909 for(; iter != end; ++iter)
911 T*
object = (*iter).first;
912 const DtExtent& objectExtent = (*iter).second;
914 if (objectExtent.
intersects(extent) && debugFunctor.mySeenObjects.count(
object) == 0)
925 std::map<T*, DtExtent> allObjects;
932 return new DtOctreeDebugger<DtSimObject>(extent);
945 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
947 if (node == (*it).first)
958 for (
typename std::list<NodeBinding>::iterator it = myBindings.begin(); it != myBindings.end(); ++it)
960 if (node == (*it).first)
963 myBindings.erase(it);
971 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
973 (*it).first->myMembers.erase((*it).second);
UUID is a unique ID wrapper class.
Definition: uuid.h:59
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:88
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:598
T * myObject
The object corresponding to this member.
Definition: octree.h:150
DtExtent myExtent
The extent of the member at the time it was added or last updated.
Definition: octree.h:153
void unbind()
Remove this member from all nodes.
Definition: octree.h:969
static bool theBitmaskListCreated
Definition: octree.h:96
bool coalesceEmptyChildren()
Look for any empty, childless nodes and coalesce them up to their parents. Returns true if this node ...
Definition: octree.h:346
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:491
bool removeObjectOutsideArea(T *object)
Definition: octree.h:812
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:216
friend class DtOctreeDebugger
Definition: octree.h:552
MemberContainer myMemberMap
Mapping of T* pointers to DtOctreeMember objects in the tree.
Definition: octree.h:835
DtOctreeNode< T > * myRootNode
Root node of the octree.
Definition: octree.h:829
DtSpatialSelectionFunctorTemplate< T > SpatialSelectionFunctor
Definition: octree.h:580
Contains the DtUUID class declaration.
#define EXT_FMT
Definition: octree.h:98
tbb::spin_rw_mutex myMutex
Definition: octree.h:827
virtual bool isValid() const
The DtSpatialVrfObjectManager is responsible for maintaining a spatial organization of all specified ...
Definition: spatialVrfObjectManager.h:31
DtOctree class.
Definition: octree.h:569
DtOctreeNode< T > ** myChildren
Child nodes of this node, if any. This is NULL for a childless node. For nodes with children...
Definition: octree.h:544
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:294
static const int MAX_BRANCH_DEPTH
Definition: octree.h:90
static void DEBUG_OUTPUT(const char *fmt,...)
Definition: octree.h:80
#define DEBUG_POP
Definition: octree.h:84
#define OBJ_ARGS(x)
Definition: octree.h:100
int numObjects()
Definition: octree.h:801
Octree support.
Definition: octree.h:52
double maxZ() const
Access and/or mutate individual values.
double minZ() const
Access and/or mutate individual values.
DtOctreeMember class.
Definition: octree.h:108
#define DEBUG_PUSH
Definition: octree.h:83
std::unordered_map< unsigned int, unsigned int > mySearchKeys
Definition: octree.h:162
virtual bool operator()(const T *object)=0
Abstract function call interface.
std::atomic< unsigned int > mySearchKey
Definition: octree.h:838
DtOctree(const DtExtent ®ion)
Constructor. The extent covered by an octree may not be changed once the octree is created...
Definition: octree.h:583
bool intersects(const DtExtent &otherExtent) const
Determine if the two extents overlap (either partially or fully). A shared "wall" counts as intersect...
virtual bool operator()(const DtSimObject *object) override=0
Abstract function call interface.
static const int MAX_NUM_NODES
Definition: octree.h:213
int numObjectsOutside()
Definition: octree.h:806
bool containsObject(T *object)
Definition: octree.h:780
~DtOctreeNode()
Destructor. No DtOctreeMember objects are deleted.
Definition: octree.h:228
static DtOctree< const DtSimObject > * createOctree(const DtExtent &extent)
Definition: spatialVrfObjectManager.h:294
void bind(DtOctreeNode< T > *node)
Add this member to the specified node.
Definition: octree.h:943
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:533
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:636
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:159
double minY() const
Access and/or mutate individual values.
void setY(const double &y)
Definition: point.h:203
tbb::spin_rw_mutex mySearchKeysMutex
Definition: octree.h:163
std::pair< DtOctreeNode< T > *, typename std::list< DtOctreeMember * >::iterator > NodeBinding
Definition: octree.h:155
const DtPoint myCenter
The center of myRegion.
Definition: octree.h:536
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:254
const double & y() const
Definition: point.h:198
virtual ~DtSpatialSelectionFunctorTemplate()
Virtual Destructor which does nothing.
Definition: octree.h:172
std::list< DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:210
bool testKey(const unsigned int key)
Definition: octree.h:126
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:80
int numChildCandidates()
Return the number of members of this node that can go into child nodes.
Definition: octree.h:377
std::list< DtOctreeMember< T > * > MemberContainer
This class is only for use from DtOctree and DtOctreeDebugger.
Definition: octree.h:209
Contains the declaration of the DtExtent class.
static int theBitmaskList[NUM_BRANCHES]
Definition: octree.h:95
std::list< T * > OutsideAreaContainer
Definition: octree.h:577
MemberContainer myMembers
Members which are in this node. If myChildren is not NULL, then this must not contain any members tha...
Definition: octree.h:548
unsigned int myDepth
Definition: octree.h:539
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:612
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:724
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 const DtString & uuidString() const
virtual void removeObject(T *object, bool needsLock=true)
Remove a member from the octree.
Definition: octree.h:681
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:457
std::map< T *, DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:575
std::map< T *, DtOctreeMember< T > * > MemberContainer
Container and iterator typedefs for the STL structures used.
Definition: octree.h:574
OutsideAreaContainer myObjectsOutsideArea
List of objects which are at least partially outside the octree's root extent.
Definition: octree.h:832
#define EXT_ARGS(x)
Definition: octree.h:99
std::list< T * >::iterator OutsideAreaIterator
Definition: octree.h:578
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:424
DtOctreeMember(T *object, const DtExtent &extent)
Definition: octree.h:111
static const int MAX_CHILD_MEMBERS
The maximum length of myChildMembers; see implementation notes at the top of the class.
Definition: octree.h:93
static int sNumNodes
Definition: octree.h:212
void unbind(DtOctreeNode< T > *node)
Remove this member from the specified node.
Definition: octree.h:956