35 #include <cmdLine/cmdStdOutput.h>
42 #include <tbb/spin_rw_mutex.h>
47 #include <unordered_map>
56 #define OCTREE_DEBUG 0
60 static int octree_debug_level = 0;
69 vsprintf(buffer, fmt, ap);
72 DtInfo(
"OCT %*s%s\n", octree_debug_level*2,
"", buffer);
75 # define DEBUG_PUSH do { octree_debug_level++; } while(0)
76 # define DEBUG_POP do { octree_debug_level--; } while(0)
82 # define DEBUG_PUSH do { } while(0)
83 # define DEBUG_POP do { } while(0)
97 #define EXT_FMT "[%lf, %lf, %lf] - [%lf, %lf, %lf]"
98 #define EXT_ARGS(x) (x).minX(), (x).minY(), (x).minZ(), (x).maxX(), (x).maxY(), (x).maxZ()
99 #define OBJ_ARGS(x) ((const char*) (x)->objectName())
130 std::unordered_map<unsigned int, unsigned int>::const_iterator iter =
mySearchKeys.find(threadId);
132 if ((iter !=
mySearchKeys.end()) && (key == iter->second))
137 lock.upgrade_to_writer();
154 typedef std::pair<DtOctreeNode<T>*,
typename std::list<DtOctreeMember*>::iterator>
NodeBinding;
263 if ((*iter)->testKey(key))
265 if (!(*iter)->myObject->isDeleted() && callback((*iter)->myObject))
282 if (
myChildren[i] && (mask & theBitmaskList[i]))
284 myChildren[i]->visitObjects(key, extent, callback);
318 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
320 if (mask & theBitmaskList[childIndex])
329 DEBUG_OUTPUT(
"add as child member (not enough child members yet)");
384 double numBuckets = 0;
389 if ((*iter)->myExtent == matchExtent)
394 matchExtent = (*iter)->myExtent;
398 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
400 if (mask & theBitmaskList[childIndex])
406 retVal += (1.0 / numBuckets);
408 if (retVal >= MAX_CHILD_MEMBERS)
483 myChildren[childIndex]->addMember(member, depth);
518 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
520 if (mask & theBitmaskList[childIndex])
586 if (!theBitmaskListCreated)
588 theBitmaskListCreated =
true;
589 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
591 theBitmaskList[childIndex] = (1 << childIndex);
604 delete (*iter).second;
613 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
640 tbb::spin_rw_mutex::scoped_lock lock(
myMutex);
653 DtWarn(
"Member %s (" EXT_FMT ") added outside octree (" EXT_FMT ")\n",
667 tbb::spin_rw_mutex::scoped_lock* lock = 0;
671 lock =
new tbb::spin_rw_mutex::scoped_lock(
myMutex,
true);
707 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
713 T*
object = (*iter).first;
715 if (object->isValid())
718 DtExtent roughExtent =
object->extent();
721 if (member->
myExtent != roughExtent)
729 DtWarn(
"Object %s (" EXT_FMT ") moving outside octree (" EXT_FMT ")\n",
731 lock.upgrade_to_writer();
734 lock.downgrade_to_reader();
755 tbb::spin_rw_mutex::scoped_lock lock(
myMutex,
false);
831 mySeenObjects.insert(
object);
832 return myInner(
object);
835 std::set<const DtSimObject*> mySeenObjects;
839 template<
class T>
class DtOctreeDebugger :
public DtOctree<T>
842 DtOctreeDebugger(
const DtExtent& extent)
848 allObjects[object] = extent;
854 allObjects.erase(
object);
860 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
861 std::map<T*, DtExtent>::iterator end = allObjects.end();
863 for(; iter != end; ++iter)
865 T*
object = (*iter).first;
868 extent =
object->roughExtent();
876 DtDebugOctreeFunctor debugFunctor(callback);
879 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
880 std::map<T*, DtExtent>::iterator end = allObjects.end();
882 for(; iter != end; ++iter)
884 T*
object = (*iter).first;
885 const DtExtent& objectExtent = (*iter).second;
887 if (objectExtent.
intersects(extent) && debugFunctor.mySeenObjects.count(
object) == 0)
898 std::map<T*, DtExtent> allObjects;
905 return new DtOctreeDebugger<DtSimObject>(extent);
918 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
920 if (node == (*it).first)
931 for (
typename std::list<NodeBinding>::iterator it = myBindings.begin(); it != myBindings.end(); ++it)
933 if (node == (*it).first)
936 myBindings.erase(it);
944 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
946 (*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:87
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:597
T * myObject
The object corresponding to this member.
Definition: octree.h:149
DtExtent myExtent
The extent of the member at the time it was added or last updated.
Definition: octree.h:152
void unbind()
Remove this member from all nodes.
Definition: octree.h:942
static bool theBitmaskListCreated
Definition: octree.h:95
bool coalesceEmptyChildren()
Look for any empty, childless nodes and coalesce them up to their parents. Returns true if this node ...
Definition: octree.h:345
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:490
bool removeObjectOutsideArea(T *object)
Definition: octree.h:785
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:215
friend class DtOctreeDebugger
Definition: octree.h:551
MemberContainer myMemberMap
Mapping of T* pointers to DtOctreeMember objects in the tree.
Definition: octree.h:808
DtOctreeNode< T > * myRootNode
Root node of the octree.
Definition: octree.h:802
DtSpatialSelectionFunctorTemplate< T > SpatialSelectionFunctor
Definition: octree.h:579
#define EXT_FMT
Definition: octree.h:97
tbb::spin_rw_mutex myMutex
Definition: octree.h:800
The DtSpatialVrfObjectManager is responsible for maintaining a spatial organization of all specified ...
Definition: spatialVrfObjectManager.h:31
DtOctree class.
Definition: octree.h:568
DtOctreeNode< T > ** myChildren
Child nodes of this node, if any. This is NULL for a childless node. For nodes with children...
Definition: octree.h:543
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:293
static const int MAX_BRANCH_DEPTH
Definition: octree.h:89
static void DEBUG_OUTPUT(const char *fmt,...)
Definition: octree.h:79
#define DEBUG_POP
Definition: octree.h:83
#define OBJ_ARGS(x)
Definition: octree.h:99
int numObjects()
Definition: octree.h:774
Octree support.
Definition: octree.h:51
double maxZ() const
Access and/or mutate individual values.
double minZ() const
Access and/or mutate individual values.
DtOctreeMember class.
Definition: octree.h:107
#define DEBUG_PUSH
Definition: octree.h:82
std::unordered_map< unsigned int, unsigned int > mySearchKeys
Definition: octree.h:161
virtual bool operator()(const T *object)=0
Abstract function call interface.
std::atomic< unsigned int > mySearchKey
Definition: octree.h:811
DtOctree(const DtExtent ®ion)
Constructor. The extent covered by an octree may not be changed once the octree is created...
Definition: octree.h:582
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:212
int numObjectsOutside()
Definition: octree.h:779
bool containsObject(T *object)
Definition: octree.h:753
~DtOctreeNode()
Destructor. No DtOctreeMember objects are deleted.
Definition: octree.h:227
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:916
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:532
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:635
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:158
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:162
std::pair< DtOctreeNode< T > *, typename std::list< DtOctreeMember * >::iterator > NodeBinding
Definition: octree.h:154
const DtPoint myCenter
The center of myRegion.
Definition: octree.h:535
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:253
const double & y() const
Definition: point.h:198
virtual ~DtSpatialSelectionFunctorTemplate()
Virtual Destructor which does nothing.
Definition: octree.h:171
std::list< DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:209
bool testKey(const unsigned int key)
Definition: octree.h:125
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:376
std::list< DtOctreeMember< T > * > MemberContainer
This class is only for use from DtOctree and DtOctreeDebugger.
Definition: octree.h:208
Contains the declaration of the DtExtent class.
static int theBitmaskList[NUM_BRANCHES]
Definition: octree.h:94
std::list< T * > OutsideAreaContainer
Definition: octree.h:576
MemberContainer myMembers
Members which are in this node. If myChildren is not NULL, then this must not contain any members tha...
Definition: octree.h:547
unsigned int myDepth
Definition: octree.h:538
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:611
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:705
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:662
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:456
std::map< T *, DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:574
std::map< T *, DtOctreeMember< T > * > MemberContainer
Container and iterator typedefs for the STL structures used.
Definition: octree.h:573
OutsideAreaContainer myObjectsOutsideArea
List of objects which are at least partially outside the octree's root extent.
Definition: octree.h:805
#define EXT_ARGS(x)
Definition: octree.h:98
std::list< T * >::iterator OutsideAreaIterator
Definition: octree.h:577
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:423
DtOctreeMember(T *object, const DtExtent &extent)
Definition: octree.h:110
static const int MAX_CHILD_MEMBERS
The maximum length of myChildMembers; see implementation notes at the top of the class.
Definition: octree.h:92
static int sNumNodes
Definition: octree.h:211
void unbind(DtOctreeNode< T > *node)
Remove this member from the specified node.
Definition: octree.h:929