39 #include <cmdLine/cmdStdOutput.h>
49 #define OCTREE_DEBUG 0
53 static int octree_debug_level = 0;
57 static void DEBUG(
const char* fmt, ...)
62 vsprintf(buffer, fmt, ap);
65 DtInfo(
"OCT %*s%s\n", octree_debug_level*2,
"", buffer);
68 # define DEBUG_PUSH do { octree_debug_level++; } while(0)
69 # define DEBUG_POP do { octree_debug_level--; } while(0)
72 static inline void DEBUG(
const char* fmt, ...)
75 # define DEBUG_PUSH do { } while(0)
76 # define DEBUG_POP do { } while(0)
90 #define EXT_FMT "[%lf, %lf, %lf] - [%lf, %lf, %lf]"
91 #define EXT_ARGS(x) (x).minX(), (x).minY(), (x).minZ(), (x).maxX(), (x).maxY(), (x).maxZ()
92 #define OBJ_ARGS(x) ((const char*) (x)->markingText())
140 typedef std::pair<DtOctreeNode<T>*,
typename std::list<DtOctreeMember*>::iterator>
NodeBinding;
248 if ((*iter)->testKey(key))
250 callback((*iter)->myObject);
259 if (
myChildren[i] && (mask & theBitmaskList[i]))
261 myChildren[i]->visitObjects(key, extent, callback);
295 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
297 if (mask & theBitmaskList[childIndex])
306 DEBUG(
"add as child member (not enough child members yet)");
337 DEBUG(
"Deleting child %d\n", i);
361 double numBuckets = 0;
366 if ((*iter)->myExtent == matchExtent)
371 matchExtent = (*iter)->myExtent;
375 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
377 if (mask & theBitmaskList[childIndex])
383 retVal += (1.0 / numBuckets);
385 if (retVal >= MAX_CHILD_MEMBERS)
460 myChildren[childIndex]->addMember(member, depth);
495 for(
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
497 if (mask & theBitmaskList[childIndex])
563 if (!theBitmaskListCreated)
565 theBitmaskListCreated =
true;
566 for (
int childIndex = 0; childIndex <
NUM_BRANCHES; ++childIndex)
568 theBitmaskList[childIndex] = (1 << childIndex);
581 delete (*iter).second;
628 DtWarn(
"Member %s (" EXT_FMT ") added outside octree (" EXT_FMT ")\n",
675 T*
object = (*iter).first;
677 DtExtent roughExtent =
object->roughExtent();
680 if (member->
myExtent != roughExtent)
688 DtWarn(
"Object %s (" EXT_FMT ") moving outside octree (" EXT_FMT ")\n",
785 mySeenObjects.insert(
object);
786 return myInner(
object);
789 std::set<const DtVrfObject*> mySeenObjects;
793 template<
class T>
class DtOctreeDebugger :
public DtOctree<T>
796 DtOctreeDebugger(
const DtExtent& extent)
802 allObjects[object] = extent;
808 allObjects.erase(
object);
814 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
815 std::map<T*, DtExtent>::iterator end = allObjects.end();
817 for(; iter != end; ++iter)
819 T*
object = (*iter).first;
822 extent =
object->roughExtent();
830 DtDebugOctreeFunctor debugFunctor(callback);
833 std::map<T*, DtExtent>::iterator iter = allObjects.begin();
834 std::map<T*, DtExtent>::iterator end = allObjects.end();
836 for(; iter != end; ++iter)
838 T*
object = (*iter).first;
839 const DtExtent& objectExtent = (*iter).second;
841 if (objectExtent.
intersects(extent) && debugFunctor.mySeenObjects.count(
object) == 0)
852 std::map<T*, DtExtent> allObjects;
859 return new DtOctreeDebugger<DtVrfObject>(extent);
872 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
874 if (node == (*it).first)
885 for (
typename std::list<NodeBinding>::iterator it = myBindings.begin(); it != myBindings.end(); ++it)
887 if (node == (*it).first)
890 myBindings.erase(it);
898 for (
typename std::list<NodeBinding>::const_iterator it = myBindings.begin(); it != myBindings.end(); ++it)
900 (*it).first->myMembers.erase((*it).second);
unsigned int mySearchKey
Definition: octree.h:765
const double & x() const
Definition: point.h:180
static const int NUM_BRANCHES
The number of children of each node. Don't change this without rewriting everything.
Definition: octree.h:80
DtVrfObject is used to represent any type of simulated object (entities, aggregate, control objects) in a VR-Forces application.
Definition: vrfObject.h:215
virtual bool contains(const DtExtent &otherExtent) const
Determine if another extents box is entirely contained by this one.
virtual ~DtOctree()
Destructor. Deletes the octree and any members in it (though not the corresponding objects)...
Definition: octree.h:574
T * myObject
The object corresponding to this member.
Definition: octree.h:135
DtExtent myExtent
The extent of the member at the time it was added or last updated.
Definition: octree.h:138
void unbind()
Remove this member from all nodes.
Definition: octree.h:896
unsigned int mySearchKey
Definition: octree.h:147
static bool theBitmaskListCreated
Definition: octree.h:88
static void DEBUG(const char *fmt,...)
Definition: octree.h:72
bool coalesceEmptyChildren()
Look for any empty, childless nodes and coalesce them up to their parents.
Definition: octree.h:322
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:467
bool removeObjectOutsideArea(T *object)
Definition: octree.h:740
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:200
friend class DtOctreeDebugger
Definition: octree.h:528
MemberContainer myMemberMap
Mapping of T* pointers to DtOctreeMember objects in the tree.
Definition: octree.h:762
DtOctreeNode< T > * myRootNode
Root node of the octree.
Definition: octree.h:756
DtSpatialSelectionFunctorTemplate< T > SpatialSelectionFunctor
Definition: octree.h:556
#define EXT_FMT
Definition: octree.h:90
The DtSpatialVrfObjectManager is responsible for maintaining a spatial organization of all specified ...
Definition: spatialVrfObjectManager.h:34
DtOctree class.
Definition: octree.h:545
DtOctreeNode< T > ** myChildren
Child nodes of this node, if any.
Definition: octree.h:520
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:270
static const int MAX_BRANCH_DEPTH
Definition: octree.h:82
#define DEBUG_POP
Definition: octree.h:76
#define OBJ_ARGS(x)
Definition: octree.h:92
int numObjects()
Definition: octree.h:729
Octree support.
Definition: octree.h:44
DtOctreeMember class.
Definition: octree.h:100
#define DEBUG_PUSH
Definition: octree.h:75
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:559
bool intersects(const DtExtent &otherExtent) const
Determine if the two extents overlap (either partially or fully).
static const int MAX_NUM_NODES
Definition: octree.h:197
int numObjectsOutside()
Definition: octree.h:734
bool containsObject(T *object)
Definition: octree.h:709
~DtOctreeNode()
Destructor. No DtOctreeMember objects are deleted.
Definition: octree.h:212
static DtOctree< const DtVrfObject > * createOctree(const DtExtent &extent)
Definition: spatialVrfObjectManager.h:295
void bind(DtOctreeNode< T > *node)
Add this member to the specified node.
Definition: octree.h:870
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:509
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:611
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:144
void setY(const double &y)
Definition: point.h:195
std::pair< DtOctreeNode< T > *, typename std::list< DtOctreeMember * >::iterator > NodeBinding
Definition: octree.h:140
const DtPoint myCenter
The center of myRegion.
Definition: octree.h:512
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.
Definition: octree.h:238
const double & y() const
Definition: point.h:190
virtual void removeObject(T *object)
Remove a member from the octree.
Definition: octree.h:637
virtual ~DtSpatialSelectionFunctorTemplate()
Virtual Destructor which does nothing.
Definition: octree.h:156
std::list< DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:194
bool testKey(const unsigned int key)
Definition: octree.h:119
int numChildCandidates()
Return the number of members of this node that can go into child nodes.
Definition: octree.h:353
std::list< DtOctreeMember< T > * > MemberContainer
This class is only for use from DtOctree and DtOctreeDebugger.
Definition: octree.h:193
Contains the declaration of the DtExtent class.
static int theBitmaskList[NUM_BRANCHES]
Definition: octree.h:87
std::list< T * > OutsideAreaContainer
Definition: octree.h:553
virtual bool operator()(const DtVrfObject *object)=0
Abstract function call interface.
MemberContainer myMembers
Members which are in this node.
Definition: octree.h:524
unsigned int myDepth
Definition: octree.h:515
virtual void visitObjects(const DtExtent &extent, DtSpatialSelectionFunctorTemplate< T > &callback)
Call the given functor for every member that shares a node with the specified extent.
Definition: octree.h:588
void setZ(const double &z)
Definition: point.h:205
virtual void updatePositions()
Process any updates to the objects' positions that have happened since they were added to the octree...
Definition: octree.h:668
void setX(const double &x)
Definition: point.h:185
const double & z() const
Definition: point.h:200
void addMemberToChild(DtOctreeMember< T > *member, int childIndex, int &depth)
Add a member to a child of this node, creating the child if necessary.
Definition: octree.h:433
std::map< T *, DtOctreeMember< T > * >::iterator MemberIterator
Definition: octree.h:551
std::map< T *, DtOctreeMember< T > * > MemberContainer
Container and iterator typedefs for the STL structures used.
Definition: octree.h:550
OutsideAreaContainer myObjectsOutsideArea
List of objects which are at least partially outside the octree's root extent.
Definition: octree.h:759
#define EXT_ARGS(x)
Definition: octree.h:91
std::list< T * >::iterator OutsideAreaIterator
Definition: octree.h:554
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:400
DtOctreeMember(T *object, const DtExtent &extent)
Definition: octree.h:103
static const int MAX_CHILD_MEMBERS
The maximum length of myChildMembers; see implementation notes at the top of the class.
Definition: octree.h:85
static int sNumNodes
Definition: octree.h:196
void unbind(DtOctreeNode< T > *node)
Remove this member from the specified node.
Definition: octree.h:883