2
0

b2DynamicTree.cpp 16 KB

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