dynamic_bvh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**************************************************************************/
  2. /* dynamic_bvh.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "dynamic_bvh.h"
  31. void DynamicBVH::_delete_node(Node *p_node) {
  32. node_allocator.free(p_node);
  33. }
  34. void DynamicBVH::_recurse_delete_node(Node *p_node) {
  35. if (!p_node->is_leaf()) {
  36. _recurse_delete_node(p_node->children[0]);
  37. _recurse_delete_node(p_node->children[1]);
  38. }
  39. if (p_node == bvh_root) {
  40. bvh_root = nullptr;
  41. }
  42. _delete_node(p_node);
  43. }
  44. DynamicBVH::Node *DynamicBVH::_create_node(Node *p_parent, void *p_data) {
  45. Node *node = node_allocator.alloc();
  46. node->parent = p_parent;
  47. node->data = p_data;
  48. return (node);
  49. }
  50. DynamicBVH::Node *DynamicBVH::_create_node_with_volume(Node *p_parent, const Volume &p_volume, void *p_data) {
  51. Node *node = _create_node(p_parent, p_data);
  52. node->volume = p_volume;
  53. return node;
  54. }
  55. void DynamicBVH::_insert_leaf(Node *p_root, Node *p_leaf) {
  56. if (!bvh_root) {
  57. bvh_root = p_leaf;
  58. p_leaf->parent = nullptr;
  59. } else {
  60. if (!p_root->is_leaf()) {
  61. do {
  62. p_root = p_root->children[p_leaf->volume.select_by_proximity(
  63. p_root->children[0]->volume,
  64. p_root->children[1]->volume)];
  65. } while (!p_root->is_leaf());
  66. }
  67. Node *prev = p_root->parent;
  68. Node *node = _create_node_with_volume(prev, p_leaf->volume.merge(p_root->volume), nullptr);
  69. if (prev) {
  70. prev->children[p_root->get_index_in_parent()] = node;
  71. node->children[0] = p_root;
  72. p_root->parent = node;
  73. node->children[1] = p_leaf;
  74. p_leaf->parent = node;
  75. do {
  76. if (!prev->volume.contains(node->volume)) {
  77. prev->volume = prev->children[0]->volume.merge(prev->children[1]->volume);
  78. } else {
  79. break;
  80. }
  81. node = prev;
  82. } while (nullptr != (prev = node->parent));
  83. } else {
  84. node->children[0] = p_root;
  85. p_root->parent = node;
  86. node->children[1] = p_leaf;
  87. p_leaf->parent = node;
  88. bvh_root = node;
  89. }
  90. }
  91. }
  92. DynamicBVH::Node *DynamicBVH::_remove_leaf(Node *leaf) {
  93. if (leaf == bvh_root) {
  94. bvh_root = nullptr;
  95. return (nullptr);
  96. } else {
  97. Node *parent = leaf->parent;
  98. Node *prev = parent->parent;
  99. Node *sibling = parent->children[1 - leaf->get_index_in_parent()];
  100. if (prev) {
  101. prev->children[parent->get_index_in_parent()] = sibling;
  102. sibling->parent = prev;
  103. _delete_node(parent);
  104. while (prev) {
  105. const Volume pb = prev->volume;
  106. prev->volume = prev->children[0]->volume.merge(prev->children[1]->volume);
  107. if (pb.is_not_equal_to(prev->volume)) {
  108. prev = prev->parent;
  109. } else {
  110. break;
  111. }
  112. }
  113. return (prev ? prev : bvh_root);
  114. } else {
  115. bvh_root = sibling;
  116. sibling->parent = nullptr;
  117. _delete_node(parent);
  118. return (bvh_root);
  119. }
  120. }
  121. }
  122. void DynamicBVH::_fetch_leaves(Node *p_root, LocalVector<Node *> &r_leaves, int p_depth) {
  123. if (p_root->is_internal() && p_depth) {
  124. _fetch_leaves(p_root->children[0], r_leaves, p_depth - 1);
  125. _fetch_leaves(p_root->children[1], r_leaves, p_depth - 1);
  126. _delete_node(p_root);
  127. } else {
  128. r_leaves.push_back(p_root);
  129. }
  130. }
  131. // Partitions leaves such that leaves[0, n) are on the
  132. // left of axis, and leaves[n, count) are on the right
  133. // of axis. returns N.
  134. int DynamicBVH::_split(Node **leaves, int p_count, const Vector3 &p_org, const Vector3 &p_axis) {
  135. int begin = 0;
  136. int end = p_count;
  137. for (;;) {
  138. while (begin != end && leaves[begin]->is_left_of_axis(p_org, p_axis)) {
  139. ++begin;
  140. }
  141. if (begin == end) {
  142. break;
  143. }
  144. while (begin != end && !leaves[end - 1]->is_left_of_axis(p_org, p_axis)) {
  145. --end;
  146. }
  147. if (begin == end) {
  148. break;
  149. }
  150. // swap out of place nodes
  151. --end;
  152. Node *temp = leaves[begin];
  153. leaves[begin] = leaves[end];
  154. leaves[end] = temp;
  155. ++begin;
  156. }
  157. return begin;
  158. }
  159. DynamicBVH::Volume DynamicBVH::_bounds(Node **leaves, int p_count) {
  160. Volume volume = leaves[0]->volume;
  161. for (int i = 1, ni = p_count; i < ni; ++i) {
  162. volume = volume.merge(leaves[i]->volume);
  163. }
  164. return (volume);
  165. }
  166. void DynamicBVH::_bottom_up(Node **leaves, int p_count) {
  167. while (p_count > 1) {
  168. real_t minsize = INFINITY;
  169. int minidx[2] = { -1, -1 };
  170. for (int i = 0; i < p_count; ++i) {
  171. for (int j = i + 1; j < p_count; ++j) {
  172. const real_t sz = leaves[i]->volume.merge(leaves[j]->volume).get_size();
  173. if (sz < minsize) {
  174. minsize = sz;
  175. minidx[0] = i;
  176. minidx[1] = j;
  177. }
  178. }
  179. }
  180. Node *n[] = { leaves[minidx[0]], leaves[minidx[1]] };
  181. Node *p = _create_node_with_volume(nullptr, n[0]->volume.merge(n[1]->volume), nullptr);
  182. p->children[0] = n[0];
  183. p->children[1] = n[1];
  184. n[0]->parent = p;
  185. n[1]->parent = p;
  186. leaves[minidx[0]] = p;
  187. leaves[minidx[1]] = leaves[p_count - 1];
  188. --p_count;
  189. }
  190. }
  191. DynamicBVH::Node *DynamicBVH::_top_down(Node **leaves, int p_count, int p_bu_threshold) {
  192. static const Vector3 axis[] = { Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1) };
  193. ERR_FAIL_COND_V(p_bu_threshold <= 1, nullptr);
  194. if (p_count > 1) {
  195. if (p_count > p_bu_threshold) {
  196. const Volume vol = _bounds(leaves, p_count);
  197. const Vector3 org = vol.get_center();
  198. int partition;
  199. int bestaxis = -1;
  200. int bestmidp = p_count;
  201. int splitcount[3][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 } };
  202. int i;
  203. for (i = 0; i < p_count; ++i) {
  204. const Vector3 x = leaves[i]->volume.get_center() - org;
  205. for (int j = 0; j < 3; ++j) {
  206. ++splitcount[j][x.dot(axis[j]) > 0 ? 1 : 0];
  207. }
  208. }
  209. for (i = 0; i < 3; ++i) {
  210. if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0)) {
  211. const int midp = (int)Math::abs(real_t(splitcount[i][0] - splitcount[i][1]));
  212. if (midp < bestmidp) {
  213. bestaxis = i;
  214. bestmidp = midp;
  215. }
  216. }
  217. }
  218. if (bestaxis >= 0) {
  219. partition = _split(leaves, p_count, org, axis[bestaxis]);
  220. ERR_FAIL_COND_V(partition == 0 || partition == p_count, nullptr);
  221. } else {
  222. partition = p_count / 2 + 1;
  223. }
  224. Node *node = _create_node_with_volume(nullptr, vol, nullptr);
  225. node->children[0] = _top_down(&leaves[0], partition, p_bu_threshold);
  226. node->children[1] = _top_down(&leaves[partition], p_count - partition, p_bu_threshold);
  227. node->children[0]->parent = node;
  228. node->children[1]->parent = node;
  229. return (node);
  230. } else {
  231. _bottom_up(leaves, p_count);
  232. return (leaves[0]);
  233. }
  234. }
  235. return (leaves[0]);
  236. }
  237. DynamicBVH::Node *DynamicBVH::_node_sort(Node *n, Node *&r) {
  238. Node *p = n->parent;
  239. ERR_FAIL_COND_V(!n->is_internal(), nullptr);
  240. if (p > n) {
  241. const int i = n->get_index_in_parent();
  242. const int j = 1 - i;
  243. Node *s = p->children[j];
  244. Node *q = p->parent;
  245. ERR_FAIL_COND_V(n != p->children[i], nullptr);
  246. if (q) {
  247. q->children[p->get_index_in_parent()] = n;
  248. } else {
  249. r = n;
  250. }
  251. s->parent = n;
  252. p->parent = n;
  253. n->parent = q;
  254. p->children[0] = n->children[0];
  255. p->children[1] = n->children[1];
  256. n->children[0]->parent = p;
  257. n->children[1]->parent = p;
  258. n->children[i] = p;
  259. n->children[j] = s;
  260. SWAP(p->volume, n->volume);
  261. return (p);
  262. }
  263. return (n);
  264. }
  265. void DynamicBVH::clear() {
  266. if (bvh_root) {
  267. _recurse_delete_node(bvh_root);
  268. }
  269. lkhd = -1;
  270. opath = 0;
  271. }
  272. void DynamicBVH::optimize_bottom_up() {
  273. if (bvh_root) {
  274. LocalVector<Node *> leaves;
  275. _fetch_leaves(bvh_root, leaves);
  276. _bottom_up(&leaves[0], leaves.size());
  277. bvh_root = leaves[0];
  278. }
  279. }
  280. void DynamicBVH::optimize_top_down(int bu_threshold) {
  281. if (bvh_root) {
  282. LocalVector<Node *> leaves;
  283. _fetch_leaves(bvh_root, leaves);
  284. bvh_root = _top_down(&leaves[0], leaves.size(), bu_threshold);
  285. }
  286. }
  287. void DynamicBVH::optimize_incremental(int passes) {
  288. if (passes < 0) {
  289. passes = total_leaves;
  290. }
  291. if (passes > 0) {
  292. do {
  293. if (!bvh_root) {
  294. break;
  295. }
  296. Node *node = bvh_root;
  297. unsigned bit = 0;
  298. while (node->is_internal()) {
  299. node = _node_sort(node, bvh_root)->children[(opath >> bit) & 1];
  300. bit = (bit + 1) & (sizeof(unsigned) * 8 - 1);
  301. }
  302. _update(node);
  303. ++opath;
  304. } while (--passes);
  305. }
  306. }
  307. DynamicBVH::ID DynamicBVH::insert(const AABB &p_box, void *p_userdata) {
  308. Volume volume;
  309. volume.min = p_box.position;
  310. volume.max = p_box.position + p_box.size;
  311. Node *leaf = _create_node_with_volume(nullptr, volume, p_userdata);
  312. _insert_leaf(bvh_root, leaf);
  313. ++total_leaves;
  314. ID id;
  315. id.node = leaf;
  316. return id;
  317. }
  318. void DynamicBVH::_update(Node *leaf, int lookahead) {
  319. Node *root = _remove_leaf(leaf);
  320. if (root) {
  321. if (lookahead >= 0) {
  322. for (int i = 0; (i < lookahead) && root->parent; ++i) {
  323. root = root->parent;
  324. }
  325. } else {
  326. root = bvh_root;
  327. }
  328. }
  329. _insert_leaf(root, leaf);
  330. }
  331. bool DynamicBVH::update(const ID &p_id, const AABB &p_box) {
  332. ERR_FAIL_COND_V(!p_id.is_valid(), false);
  333. Node *leaf = p_id.node;
  334. Volume volume;
  335. volume.min = p_box.position;
  336. volume.max = p_box.position + p_box.size;
  337. if (leaf->volume.min.is_equal_approx(volume.min) && leaf->volume.max.is_equal_approx(volume.max)) {
  338. // noop
  339. return false;
  340. }
  341. Node *base = _remove_leaf(leaf);
  342. if (base) {
  343. if (lkhd >= 0) {
  344. for (int i = 0; (i < lkhd) && base->parent; ++i) {
  345. base = base->parent;
  346. }
  347. } else {
  348. base = bvh_root;
  349. }
  350. }
  351. leaf->volume = volume;
  352. _insert_leaf(base, leaf);
  353. return true;
  354. }
  355. void DynamicBVH::remove(const ID &p_id) {
  356. ERR_FAIL_COND(!p_id.is_valid());
  357. Node *leaf = p_id.node;
  358. _remove_leaf(leaf);
  359. _delete_node(leaf);
  360. --total_leaves;
  361. }
  362. void DynamicBVH::_extract_leaves(Node *p_node, List<ID> *r_elements) {
  363. if (p_node->is_internal()) {
  364. _extract_leaves(p_node->children[0], r_elements);
  365. _extract_leaves(p_node->children[1], r_elements);
  366. } else {
  367. ID id;
  368. id.node = p_node;
  369. r_elements->push_back(id);
  370. }
  371. }
  372. void DynamicBVH::set_index(uint32_t p_index) {
  373. ERR_FAIL_COND(bvh_root != nullptr);
  374. index = p_index;
  375. }
  376. uint32_t DynamicBVH::get_index() const {
  377. return index;
  378. }
  379. void DynamicBVH::get_elements(List<ID> *r_elements) {
  380. if (bvh_root) {
  381. _extract_leaves(bvh_root, r_elements);
  382. }
  383. }
  384. int DynamicBVH::get_leaf_count() const {
  385. return total_leaves;
  386. }
  387. int DynamicBVH::get_max_depth() const {
  388. if (bvh_root) {
  389. int depth = 1;
  390. int max_depth = 0;
  391. bvh_root->get_max_depth(depth, max_depth);
  392. return max_depth;
  393. } else {
  394. return 0;
  395. }
  396. }
  397. DynamicBVH::~DynamicBVH() {
  398. clear();
  399. }