19 #ifndef B2_DYNAMIC_TREE_H
20 #define B2_DYNAMIC_TREE_H
23 #include <Box2D/Common/b2GrowableStack.h>
25 #define b2_nullNode (-1)
32 return child1 == b2_nullNode;
71 int32 CreateProxy(
const b2AABB& aabb,
void* userData);
74 void DestroyProxy(int32 proxyId);
80 bool MoveProxy(int32 proxyId,
const b2AABB& aabb1,
const b2Vec2& displacement);
84 void* GetUserData(int32 proxyId)
const;
87 const b2AABB& GetFatAABB(int32 proxyId)
const;
92 void Query(T* callback,
const b2AABB& aabb)
const;
101 template <
typename T>
105 void Validate()
const;
109 int32 GetHeight()
const;
113 int32 GetMaxBalance()
const;
116 float32 GetAreaRatio()
const;
119 void RebuildBottomUp();
124 void ShiftOrigin(
const b2Vec2& newOrigin);
128 int32 AllocateNode();
129 void FreeNode(int32 node);
131 void InsertLeaf(int32 node);
132 void RemoveLeaf(int32 node);
134 int32 Balance(int32 index);
136 int32 ComputeHeight()
const;
137 int32 ComputeHeight(int32 nodeId)
const;
139 void ValidateStructure(int32 index)
const;
140 void ValidateMetrics(int32 index)
const;
146 int32 m_nodeCapacity;
153 int32 m_insertionCount;
156 inline void* b2DynamicTree::GetUserData(int32 proxyId)
const
158 b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
159 return m_nodes[proxyId].userData;
162 inline const b2AABB& b2DynamicTree::GetFatAABB(int32 proxyId)
const
164 b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
165 return m_nodes[proxyId].aabb;
168 template <
typename T>
169 inline void b2DynamicTree::Query(T* callback,
const b2AABB& aabb)
const
174 while (stack.GetCount() > 0)
176 int32 nodeId = stack.Pop();
177 if (nodeId == b2_nullNode)
182 const b2TreeNode* node = m_nodes + nodeId;
188 bool proceed = callback->QueryCallback(nodeId);
189 if (proceed ==
false)
196 stack.Push(node->child1);
197 stack.Push(node->child2);
203 template <
typename T>
204 inline void b2DynamicTree::RayCast(T* callback,
const b2RayCastInput& input)
const
213 b2Vec2 v = b2Cross(1.0f, r);
219 float32 maxFraction = input.maxFraction;
224 b2Vec2 t = p1 + maxFraction * (p2 - p1);
232 while (stack.GetCount() > 0)
234 int32 nodeId = stack.Pop();
235 if (nodeId == b2_nullNode)
240 const b2TreeNode* node = m_nodes + nodeId;
249 b2Vec2 c = node->aabb.GetCenter();
250 b2Vec2 h = node->aabb.GetExtents();
251 float32 separation = b2Abs(b2Dot(v, p1 - c)) - b2Dot(abs_v, h);
252 if (separation > 0.0f)
260 subInput.p1 = input.p1;
261 subInput.p2 = input.p2;
262 subInput.maxFraction = maxFraction;
264 float32 value = callback->RayCastCallback(subInput, nodeId);
276 b2Vec2 t = p1 + maxFraction * (p2 - p1);
283 stack.Push(node->child1);
284 stack.Push(node->child2);