b2DynamicTree.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. /*
  2. * Copyright (c) 2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <Box2D/Collision/b2DynamicTree.h>
  19. #include <memory.h>
  20. #include <string.h>
  21. b2DynamicTree::b2DynamicTree()
  22. {
  23. m_root = b2_nullNode;
  24. m_nodeCapacity = 16;
  25. m_nodeCount = 0;
  26. m_nodes = (b2TreeNode*)b2Alloc(m_nodeCapacity * sizeof(b2TreeNode));
  27. memset(m_nodes, 0, m_nodeCapacity * sizeof(b2TreeNode));
  28. // Build a linked list for the free list.
  29. for (int32 i = 0; i < m_nodeCapacity - 1; ++i)
  30. {
  31. m_nodes[i].next = i + 1;
  32. m_nodes[i].height = -1;
  33. }
  34. m_nodes[m_nodeCapacity-1].next = b2_nullNode;
  35. m_nodes[m_nodeCapacity-1].height = -1;
  36. m_freeList = 0;
  37. m_path = 0;
  38. m_insertionCount = 0;
  39. }
  40. b2DynamicTree::~b2DynamicTree()
  41. {
  42. // This frees the entire tree in one shot.
  43. b2Free(m_nodes);
  44. }
  45. // Allocate a node from the pool. Grow the pool if necessary.
  46. int32 b2DynamicTree::AllocateNode()
  47. {
  48. // Expand the node pool as needed.
  49. if (m_freeList == b2_nullNode)
  50. {
  51. b2Assert(m_nodeCount == m_nodeCapacity);
  52. // The free list is empty. Rebuild a bigger pool.
  53. b2TreeNode* oldNodes = m_nodes;
  54. m_nodeCapacity *= 2;
  55. m_nodes = (b2TreeNode*)b2Alloc(m_nodeCapacity * sizeof(b2TreeNode));
  56. memcpy(m_nodes, oldNodes, m_nodeCount * sizeof(b2TreeNode));
  57. b2Free(oldNodes);
  58. // Build a linked list for the free list. The parent
  59. // pointer becomes the "next" pointer.
  60. for (int32 i = m_nodeCount; i < m_nodeCapacity - 1; ++i)
  61. {
  62. m_nodes[i].next = i + 1;
  63. m_nodes[i].height = -1;
  64. }
  65. m_nodes[m_nodeCapacity-1].next = b2_nullNode;
  66. m_nodes[m_nodeCapacity-1].height = -1;
  67. m_freeList = m_nodeCount;
  68. }
  69. // Peel a node off the free list.
  70. int32 nodeId = m_freeList;
  71. m_freeList = m_nodes[nodeId].next;
  72. m_nodes[nodeId].parent = b2_nullNode;
  73. m_nodes[nodeId].child1 = b2_nullNode;
  74. m_nodes[nodeId].child2 = b2_nullNode;
  75. m_nodes[nodeId].height = 0;
  76. m_nodes[nodeId].userData = NULL;
  77. ++m_nodeCount;
  78. return nodeId;
  79. }
  80. // Return a node to the pool.
  81. void b2DynamicTree::FreeNode(int32 nodeId)
  82. {
  83. b2Assert(0 <= nodeId && nodeId < m_nodeCapacity);
  84. b2Assert(0 < m_nodeCount);
  85. m_nodes[nodeId].next = m_freeList;
  86. m_nodes[nodeId].height = -1;
  87. m_freeList = nodeId;
  88. --m_nodeCount;
  89. }
  90. // Create a proxy in the tree as a leaf node. We return the index
  91. // of the node instead of a pointer so that we can grow
  92. // the node pool.
  93. int32 b2DynamicTree::CreateProxy(const b2AABB& aabb, void* userData)
  94. {
  95. int32 proxyId = AllocateNode();
  96. // Fatten the aabb.
  97. b2Vec2 r(b2_aabbExtension, b2_aabbExtension);
  98. m_nodes[proxyId].aabb.lowerBound = aabb.lowerBound - r;
  99. m_nodes[proxyId].aabb.upperBound = aabb.upperBound + r;
  100. m_nodes[proxyId].userData = userData;
  101. m_nodes[proxyId].height = 0;
  102. InsertLeaf(proxyId);
  103. return proxyId;
  104. }
  105. void b2DynamicTree::DestroyProxy(int32 proxyId)
  106. {
  107. b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
  108. b2Assert(m_nodes[proxyId].IsLeaf());
  109. RemoveLeaf(proxyId);
  110. FreeNode(proxyId);
  111. }
  112. bool b2DynamicTree::MoveProxy(int32 proxyId, const b2AABB& aabb, const b2Vec2& displacement)
  113. {
  114. b2Assert(0 <= proxyId && proxyId < m_nodeCapacity);
  115. b2Assert(m_nodes[proxyId].IsLeaf());
  116. if (m_nodes[proxyId].aabb.Contains(aabb))
  117. {
  118. return false;
  119. }
  120. RemoveLeaf(proxyId);
  121. // Extend AABB.
  122. b2AABB b = aabb;
  123. b2Vec2 r(b2_aabbExtension, b2_aabbExtension);
  124. b.lowerBound = b.lowerBound - r;
  125. b.upperBound = b.upperBound + r;
  126. // Predict AABB displacement.
  127. b2Vec2 d = b2_aabbMultiplier * displacement;
  128. if (d.x < 0.0f)
  129. {
  130. b.lowerBound.x += d.x;
  131. }
  132. else
  133. {
  134. b.upperBound.x += d.x;
  135. }
  136. if (d.y < 0.0f)
  137. {
  138. b.lowerBound.y += d.y;
  139. }
  140. else
  141. {
  142. b.upperBound.y += d.y;
  143. }
  144. m_nodes[proxyId].aabb = b;
  145. InsertLeaf(proxyId);
  146. return true;
  147. }
  148. void b2DynamicTree::InsertLeaf(int32 leaf)
  149. {
  150. ++m_insertionCount;
  151. if (m_root == b2_nullNode)
  152. {
  153. m_root = leaf;
  154. m_nodes[m_root].parent = b2_nullNode;
  155. return;
  156. }
  157. // Find the best sibling for this node
  158. b2AABB leafAABB = m_nodes[leaf].aabb;
  159. int32 index = m_root;
  160. while (m_nodes[index].IsLeaf() == false)
  161. {
  162. int32 child1 = m_nodes[index].child1;
  163. int32 child2 = m_nodes[index].child2;
  164. float32 area = m_nodes[index].aabb.GetPerimeter();
  165. b2AABB combinedAABB;
  166. combinedAABB.Combine(m_nodes[index].aabb, leafAABB);
  167. float32 combinedArea = combinedAABB.GetPerimeter();
  168. // Cost of creating a new parent for this node and the new leaf
  169. float32 cost = 2.0f * combinedArea;
  170. // Minimum cost of pushing the leaf further down the tree
  171. float32 inheritanceCost = 2.0f * (combinedArea - area);
  172. // Cost of descending into child1
  173. float32 cost1;
  174. if (m_nodes[child1].IsLeaf())
  175. {
  176. b2AABB aabb;
  177. aabb.Combine(leafAABB, m_nodes[child1].aabb);
  178. cost1 = aabb.GetPerimeter() + inheritanceCost;
  179. }
  180. else
  181. {
  182. b2AABB aabb;
  183. aabb.Combine(leafAABB, m_nodes[child1].aabb);
  184. float32 oldArea = m_nodes[child1].aabb.GetPerimeter();
  185. float32 newArea = aabb.GetPerimeter();
  186. cost1 = (newArea - oldArea) + inheritanceCost;
  187. }
  188. // Cost of descending into child2
  189. float32 cost2;
  190. if (m_nodes[child2].IsLeaf())
  191. {
  192. b2AABB aabb;
  193. aabb.Combine(leafAABB, m_nodes[child2].aabb);
  194. cost2 = aabb.GetPerimeter() + inheritanceCost;
  195. }
  196. else
  197. {
  198. b2AABB aabb;
  199. aabb.Combine(leafAABB, m_nodes[child2].aabb);
  200. float32 oldArea = m_nodes[child2].aabb.GetPerimeter();
  201. float32 newArea = aabb.GetPerimeter();
  202. cost2 = newArea - oldArea + inheritanceCost;
  203. }
  204. // Descend according to the minimum cost.
  205. if (cost < cost1 && cost < cost2)
  206. {
  207. break;
  208. }
  209. // Descend
  210. if (cost1 < cost2)
  211. {
  212. index = child1;
  213. }
  214. else
  215. {
  216. index = child2;
  217. }
  218. }
  219. int32 sibling = index;
  220. // Create a new parent.
  221. int32 oldParent = m_nodes[sibling].parent;
  222. int32 newParent = AllocateNode();
  223. m_nodes[newParent].parent = oldParent;
  224. m_nodes[newParent].userData = NULL;
  225. m_nodes[newParent].aabb.Combine(leafAABB, m_nodes[sibling].aabb);
  226. m_nodes[newParent].height = m_nodes[sibling].height + 1;
  227. if (oldParent != b2_nullNode)
  228. {
  229. // The sibling was not the root.
  230. if (m_nodes[oldParent].child1 == sibling)
  231. {
  232. m_nodes[oldParent].child1 = newParent;
  233. }
  234. else
  235. {
  236. m_nodes[oldParent].child2 = newParent;
  237. }
  238. m_nodes[newParent].child1 = sibling;
  239. m_nodes[newParent].child2 = leaf;
  240. m_nodes[sibling].parent = newParent;
  241. m_nodes[leaf].parent = newParent;
  242. }
  243. else
  244. {
  245. // The sibling was the root.
  246. m_nodes[newParent].child1 = sibling;
  247. m_nodes[newParent].child2 = leaf;
  248. m_nodes[sibling].parent = newParent;
  249. m_nodes[leaf].parent = newParent;
  250. m_root = newParent;
  251. }
  252. // Walk back up the tree fixing heights and AABBs
  253. index = m_nodes[leaf].parent;
  254. while (index != b2_nullNode)
  255. {
  256. index = Balance(index);
  257. int32 child1 = m_nodes[index].child1;
  258. int32 child2 = m_nodes[index].child2;
  259. b2Assert(child1 != b2_nullNode);
  260. b2Assert(child2 != b2_nullNode);
  261. m_nodes[index].height = 1 + b2Max(m_nodes[child1].height, m_nodes[child2].height);
  262. m_nodes[index].aabb.Combine(m_nodes[child1].aabb, m_nodes[child2].aabb);
  263. index = m_nodes[index].parent;
  264. }
  265. //Validate();
  266. }
  267. void b2DynamicTree::RemoveLeaf(int32 leaf)
  268. {
  269. if (leaf == m_root)
  270. {
  271. m_root = b2_nullNode;
  272. return;
  273. }
  274. int32 parent = m_nodes[leaf].parent;
  275. int32 grandParent = m_nodes[parent].parent;
  276. int32 sibling;
  277. if (m_nodes[parent].child1 == leaf)
  278. {
  279. sibling = m_nodes[parent].child2;
  280. }
  281. else
  282. {
  283. sibling = m_nodes[parent].child1;
  284. }
  285. if (grandParent != b2_nullNode)
  286. {
  287. // Destroy parent and connect sibling to grandParent.
  288. if (m_nodes[grandParent].child1 == parent)
  289. {
  290. m_nodes[grandParent].child1 = sibling;
  291. }
  292. else
  293. {
  294. m_nodes[grandParent].child2 = sibling;
  295. }
  296. m_nodes[sibling].parent = grandParent;
  297. FreeNode(parent);
  298. // Adjust ancestor bounds.
  299. int32 index = grandParent;
  300. while (index != b2_nullNode)
  301. {
  302. index = Balance(index);
  303. int32 child1 = m_nodes[index].child1;
  304. int32 child2 = m_nodes[index].child2;
  305. m_nodes[index].aabb.Combine(m_nodes[child1].aabb, m_nodes[child2].aabb);
  306. m_nodes[index].height = 1 + b2Max(m_nodes[child1].height, m_nodes[child2].height);
  307. index = m_nodes[index].parent;
  308. }
  309. }
  310. else
  311. {
  312. m_root = sibling;
  313. m_nodes[sibling].parent = b2_nullNode;
  314. FreeNode(parent);
  315. }
  316. //Validate();
  317. }
  318. // Perform a left or right rotation if node A is imbalanced.
  319. // Returns the new root index.
  320. int32 b2DynamicTree::Balance(int32 iA)
  321. {
  322. b2Assert(iA != b2_nullNode);
  323. b2TreeNode* A = m_nodes + iA;
  324. if (A->IsLeaf() || A->height < 2)
  325. {
  326. return iA;
  327. }
  328. int32 iB = A->child1;
  329. int32 iC = A->child2;
  330. b2Assert(0 <= iB && iB < m_nodeCapacity);
  331. b2Assert(0 <= iC && iC < m_nodeCapacity);
  332. b2TreeNode* B = m_nodes + iB;
  333. b2TreeNode* C = m_nodes + iC;
  334. int32 balance = C->height - B->height;
  335. // Rotate C up
  336. if (balance > 1)
  337. {
  338. int32 iF = C->child1;
  339. int32 iG = C->child2;
  340. b2TreeNode* F = m_nodes + iF;
  341. b2TreeNode* G = m_nodes + iG;
  342. b2Assert(0 <= iF && iF < m_nodeCapacity);
  343. b2Assert(0 <= iG && iG < m_nodeCapacity);
  344. // Swap A and C
  345. C->child1 = iA;
  346. C->parent = A->parent;
  347. A->parent = iC;
  348. // A's old parent should point to C
  349. if (C->parent != b2_nullNode)
  350. {
  351. if (m_nodes[C->parent].child1 == iA)
  352. {
  353. m_nodes[C->parent].child1 = iC;
  354. }
  355. else
  356. {
  357. b2Assert(m_nodes[C->parent].child2 == iA);
  358. m_nodes[C->parent].child2 = iC;
  359. }
  360. }
  361. else
  362. {
  363. m_root = iC;
  364. }
  365. // Rotate
  366. if (F->height > G->height)
  367. {
  368. C->child2 = iF;
  369. A->child2 = iG;
  370. G->parent = iA;
  371. A->aabb.Combine(B->aabb, G->aabb);
  372. C->aabb.Combine(A->aabb, F->aabb);
  373. A->height = 1 + b2Max(B->height, G->height);
  374. C->height = 1 + b2Max(A->height, F->height);
  375. }
  376. else
  377. {
  378. C->child2 = iG;
  379. A->child2 = iF;
  380. F->parent = iA;
  381. A->aabb.Combine(B->aabb, F->aabb);
  382. C->aabb.Combine(A->aabb, G->aabb);
  383. A->height = 1 + b2Max(B->height, F->height);
  384. C->height = 1 + b2Max(A->height, G->height);
  385. }
  386. return iC;
  387. }
  388. // Rotate B up
  389. if (balance < -1)
  390. {
  391. int32 iD = B->child1;
  392. int32 iE = B->child2;
  393. b2TreeNode* D = m_nodes + iD;
  394. b2TreeNode* E = m_nodes + iE;
  395. b2Assert(0 <= iD && iD < m_nodeCapacity);
  396. b2Assert(0 <= iE && iE < m_nodeCapacity);
  397. // Swap A and B
  398. B->child1 = iA;
  399. B->parent = A->parent;
  400. A->parent = iB;
  401. // A's old parent should point to B
  402. if (B->parent != b2_nullNode)
  403. {
  404. if (m_nodes[B->parent].child1 == iA)
  405. {
  406. m_nodes[B->parent].child1 = iB;
  407. }
  408. else
  409. {
  410. b2Assert(m_nodes[B->parent].child2 == iA);
  411. m_nodes[B->parent].child2 = iB;
  412. }
  413. }
  414. else
  415. {
  416. m_root = iB;
  417. }
  418. // Rotate
  419. if (D->height > E->height)
  420. {
  421. B->child2 = iD;
  422. A->child1 = iE;
  423. E->parent = iA;
  424. A->aabb.Combine(C->aabb, E->aabb);
  425. B->aabb.Combine(A->aabb, D->aabb);
  426. A->height = 1 + b2Max(C->height, E->height);
  427. B->height = 1 + b2Max(A->height, D->height);
  428. }
  429. else
  430. {
  431. B->child2 = iE;
  432. A->child1 = iD;
  433. D->parent = iA;
  434. A->aabb.Combine(C->aabb, D->aabb);
  435. B->aabb.Combine(A->aabb, E->aabb);
  436. A->height = 1 + b2Max(C->height, D->height);
  437. B->height = 1 + b2Max(A->height, E->height);
  438. }
  439. return iB;
  440. }
  441. return iA;
  442. }
  443. int32 b2DynamicTree::GetHeight() const
  444. {
  445. if (m_root == b2_nullNode)
  446. {
  447. return 0;
  448. }
  449. return m_nodes[m_root].height;
  450. }
  451. //
  452. float32 b2DynamicTree::GetAreaRatio() const
  453. {
  454. if (m_root == b2_nullNode)
  455. {
  456. return 0.0f;
  457. }
  458. const b2TreeNode* root = m_nodes + m_root;
  459. float32 rootArea = root->aabb.GetPerimeter();
  460. float32 totalArea = 0.0f;
  461. for (int32 i = 0; i < m_nodeCapacity; ++i)
  462. {
  463. const b2TreeNode* node = m_nodes + i;
  464. if (node->height < 0)
  465. {
  466. // Free node in pool
  467. continue;
  468. }
  469. totalArea += node->aabb.GetPerimeter();
  470. }
  471. return totalArea / rootArea;
  472. }
  473. // Compute the height of a sub-tree.
  474. int32 b2DynamicTree::ComputeHeight(int32 nodeId) const
  475. {
  476. b2Assert(0 <= nodeId && nodeId < m_nodeCapacity);
  477. b2TreeNode* node = m_nodes + nodeId;
  478. if (node->IsLeaf())
  479. {
  480. return 0;
  481. }
  482. int32 height1 = ComputeHeight(node->child1);
  483. int32 height2 = ComputeHeight(node->child2);
  484. return 1 + b2Max(height1, height2);
  485. }
  486. int32 b2DynamicTree::ComputeHeight() const
  487. {
  488. int32 height = ComputeHeight(m_root);
  489. return height;
  490. }
  491. void b2DynamicTree::ValidateStructure(int32 index) const
  492. {
  493. if (index == b2_nullNode)
  494. {
  495. return;
  496. }
  497. if (index == m_root)
  498. {
  499. b2Assert(m_nodes[index].parent == b2_nullNode);
  500. }
  501. const b2TreeNode* node = m_nodes + index;
  502. int32 child1 = node->child1;
  503. int32 child2 = node->child2;
  504. if (node->IsLeaf())
  505. {
  506. b2Assert(child1 == b2_nullNode);
  507. b2Assert(child2 == b2_nullNode);
  508. b2Assert(node->height == 0);
  509. return;
  510. }
  511. b2Assert(0 <= child1 && child1 < m_nodeCapacity);
  512. b2Assert(0 <= child2 && child2 < m_nodeCapacity);
  513. b2Assert(m_nodes[child1].parent == index);
  514. b2Assert(m_nodes[child2].parent == index);
  515. ValidateStructure(child1);
  516. ValidateStructure(child2);
  517. }
  518. void b2DynamicTree::ValidateMetrics(int32 index) const
  519. {
  520. if (index == b2_nullNode)
  521. {
  522. return;
  523. }
  524. const b2TreeNode* node = m_nodes + index;
  525. int32 child1 = node->child1;
  526. int32 child2 = node->child2;
  527. if (node->IsLeaf())
  528. {
  529. b2Assert(child1 == b2_nullNode);
  530. b2Assert(child2 == b2_nullNode);
  531. b2Assert(node->height == 0);
  532. return;
  533. }
  534. b2Assert(0 <= child1 && child1 < m_nodeCapacity);
  535. b2Assert(0 <= child2 && child2 < m_nodeCapacity);
  536. int32 height1 = m_nodes[child1].height;
  537. int32 height2 = m_nodes[child2].height;
  538. int32 height;
  539. height = 1 + b2Max(height1, height2);
  540. b2Assert(node->height == height);
  541. b2AABB aabb;
  542. aabb.Combine(m_nodes[child1].aabb, m_nodes[child2].aabb);
  543. b2Assert(aabb.lowerBound == node->aabb.lowerBound);
  544. b2Assert(aabb.upperBound == node->aabb.upperBound);
  545. ValidateMetrics(child1);
  546. ValidateMetrics(child2);
  547. }
  548. void b2DynamicTree::Validate() const
  549. {
  550. ValidateStructure(m_root);
  551. ValidateMetrics(m_root);
  552. int32 freeCount = 0;
  553. int32 freeIndex = m_freeList;
  554. while (freeIndex != b2_nullNode)
  555. {
  556. b2Assert(0 <= freeIndex && freeIndex < m_nodeCapacity);
  557. freeIndex = m_nodes[freeIndex].next;
  558. ++freeCount;
  559. }
  560. b2Assert(GetHeight() == ComputeHeight());
  561. b2Assert(m_nodeCount + freeCount == m_nodeCapacity);
  562. }
  563. int32 b2DynamicTree::GetMaxBalance() const
  564. {
  565. int32 maxBalance = 0;
  566. for (int32 i = 0; i < m_nodeCapacity; ++i)
  567. {
  568. const b2TreeNode* node = m_nodes + i;
  569. if (node->height <= 1)
  570. {
  571. continue;
  572. }
  573. b2Assert(node->IsLeaf() == false);
  574. int32 child1 = node->child1;
  575. int32 child2 = node->child2;
  576. int32 balance = b2Abs(m_nodes[child2].height - m_nodes[child1].height);
  577. maxBalance = b2Max(maxBalance, balance);
  578. }
  579. return maxBalance;
  580. }
  581. void b2DynamicTree::RebuildBottomUp()
  582. {
  583. int32* nodes = (int32*)b2Alloc(m_nodeCount * sizeof(int32));
  584. int32 count = 0;
  585. // Build array of leaves. Free the rest.
  586. for (int32 i = 0; i < m_nodeCapacity; ++i)
  587. {
  588. if (m_nodes[i].height < 0)
  589. {
  590. // free node in pool
  591. continue;
  592. }
  593. if (m_nodes[i].IsLeaf())
  594. {
  595. m_nodes[i].parent = b2_nullNode;
  596. nodes[count] = i;
  597. ++count;
  598. }
  599. else
  600. {
  601. FreeNode(i);
  602. }
  603. }
  604. while (count > 1)
  605. {
  606. float32 minCost = b2_maxFloat;
  607. int32 iMin = -1, jMin = -1;
  608. for (int32 i = 0; i < count; ++i)
  609. {
  610. b2AABB aabbi = m_nodes[nodes[i]].aabb;
  611. for (int32 j = i + 1; j < count; ++j)
  612. {
  613. b2AABB aabbj = m_nodes[nodes[j]].aabb;
  614. b2AABB b;
  615. b.Combine(aabbi, aabbj);
  616. float32 cost = b.GetPerimeter();
  617. if (cost < minCost)
  618. {
  619. iMin = i;
  620. jMin = j;
  621. minCost = cost;
  622. }
  623. }
  624. }
  625. int32 index1 = nodes[iMin];
  626. int32 index2 = nodes[jMin];
  627. b2TreeNode* child1 = m_nodes + index1;
  628. b2TreeNode* child2 = m_nodes + index2;
  629. int32 parentIndex = AllocateNode();
  630. b2TreeNode* parent = m_nodes + parentIndex;
  631. parent->child1 = index1;
  632. parent->child2 = index2;
  633. parent->height = 1 + b2Max(child1->height, child2->height);
  634. parent->aabb.Combine(child1->aabb, child2->aabb);
  635. parent->parent = b2_nullNode;
  636. child1->parent = parentIndex;
  637. child2->parent = parentIndex;
  638. nodes[jMin] = nodes[count-1];
  639. nodes[iMin] = parentIndex;
  640. --count;
  641. }
  642. m_root = nodes[0];
  643. b2Free(nodes);
  644. Validate();
  645. }
  646. void b2DynamicTree::ShiftOrigin(const b2Vec2& newOrigin)
  647. {
  648. // Build array of leaves. Free the rest.
  649. for (int32 i = 0; i < m_nodeCapacity; ++i)
  650. {
  651. m_nodes[i].aabb.lowerBound -= newOrigin;
  652. m_nodes[i].aabb.upperBound -= newOrigin;
  653. }
  654. }