dynamic_bvh.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*************************************************************************/
  2. /* dynamic_bvh.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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->childs[0]);
  37. _recurse_delete_node(p_node->childs[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 = 0;
  59. } else {
  60. if (!p_root->is_leaf()) {
  61. do {
  62. p_root = p_root->childs[p_leaf->volume.select_by_proximity(
  63. p_root->childs[0]->volume,
  64. p_root->childs[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), 0);
  69. if (prev) {
  70. prev->childs[p_root->get_index_in_parent()] = node;
  71. node->childs[0] = p_root;
  72. p_root->parent = node;
  73. node->childs[1] = p_leaf;
  74. p_leaf->parent = node;
  75. do {
  76. if (!prev->volume.contains(node->volume)) {
  77. prev->volume = prev->childs[0]->volume.merge(prev->childs[1]->volume);
  78. } else {
  79. break;
  80. }
  81. node = prev;
  82. } while (0 != (prev = node->parent));
  83. } else {
  84. node->childs[0] = p_root;
  85. p_root->parent = node;
  86. node->childs[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 = 0;
  95. return (0);
  96. } else {
  97. Node *parent = leaf->parent;
  98. Node *prev = parent->parent;
  99. Node *sibling = parent->childs[1 - leaf->get_index_in_parent()];
  100. if (prev) {
  101. prev->childs[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->childs[0]->volume.merge(prev->childs[1]->volume);
  107. if (pb.is_not_equal_to(prev->volume)) {
  108. prev = prev->parent;
  109. } else
  110. break;
  111. }
  112. return (prev ? prev : bvh_root);
  113. } else {
  114. bvh_root = sibling;
  115. sibling->parent = 0;
  116. _delete_node(parent);
  117. return (bvh_root);
  118. }
  119. }
  120. }
  121. void DynamicBVH::_fetch_leaves(Node *p_root, LocalVector<Node *> &r_leaves, int p_depth) {
  122. if (p_root->is_internal() && p_depth) {
  123. _fetch_leaves(p_root->childs[0], r_leaves, p_depth - 1);
  124. _fetch_leaves(p_root->childs[1], r_leaves, p_depth - 1);
  125. _delete_node(p_root);
  126. } else {
  127. r_leaves.push_back(p_root);
  128. }
  129. }
  130. // Partitions leaves such that leaves[0, n) are on the
  131. // left of axis, and leaves[n, count) are on the right
  132. // of axis. returns N.
  133. int DynamicBVH::_split(Node **leaves, int p_count, const Vector3 &p_org, const Vector3 &p_axis) {
  134. int begin = 0;
  135. int end = p_count;
  136. for (;;) {
  137. while (begin != end && leaves[begin]->is_left_of_axis(p_org, p_axis)) {
  138. ++begin;
  139. }
  140. if (begin == end) {
  141. break;
  142. }
  143. while (begin != end && !leaves[end - 1]->is_left_of_axis(p_org, p_axis)) {
  144. --end;
  145. }
  146. if (begin == end) {
  147. break;
  148. }
  149. // swap out of place nodes
  150. --end;
  151. Node *temp = leaves[begin];
  152. leaves[begin] = leaves[end];
  153. leaves[end] = temp;
  154. ++begin;
  155. }
  156. return begin;
  157. }
  158. DynamicBVH::Volume DynamicBVH::_bounds(Node **leaves, int p_count) {
  159. Volume volume = leaves[0]->volume;
  160. for (int i = 1, ni = p_count; i < ni; ++i) {
  161. volume = volume.merge(leaves[i]->volume);
  162. }
  163. return (volume);
  164. }
  165. void DynamicBVH::_bottom_up(Node **leaves, int p_count) {
  166. while (p_count > 1) {
  167. real_t minsize = Math_INF;
  168. int minidx[2] = { -1, -1 };
  169. for (int i = 0; i < p_count; ++i) {
  170. for (int j = i + 1; j < p_count; ++j) {
  171. const real_t sz = leaves[i]->volume.merge(leaves[j]->volume).get_size();
  172. if (sz < minsize) {
  173. minsize = sz;
  174. minidx[0] = i;
  175. minidx[1] = j;
  176. }
  177. }
  178. }
  179. Node *n[] = { leaves[minidx[0]], leaves[minidx[1]] };
  180. Node *p = _create_node_with_volume(nullptr, n[0]->volume.merge(n[1]->volume), nullptr);
  181. p->childs[0] = n[0];
  182. p->childs[1] = n[1];
  183. n[0]->parent = p;
  184. n[1]->parent = p;
  185. leaves[minidx[0]] = p;
  186. leaves[minidx[1]] = leaves[p_count - 1];
  187. --p_count;
  188. }
  189. }
  190. DynamicBVH::Node *DynamicBVH::_top_down(Node **leaves, int p_count, int p_bu_threshold) {
  191. static const Vector3 axis[] = { Vector3(1, 0, 0), Vector3(0, 1, 0), Vector3(0, 0, 1) };
  192. ERR_FAIL_COND_V(p_bu_threshold <= 1, nullptr);
  193. if (p_count > 1) {
  194. if (p_count > p_bu_threshold) {
  195. const Volume vol = _bounds(leaves, p_count);
  196. const Vector3 org = vol.get_center();
  197. int partition;
  198. int bestaxis = -1;
  199. int bestmidp = p_count;
  200. int splitcount[3][2] = { { 0, 0 }, { 0, 0 }, { 0, 0 } };
  201. int i;
  202. for (i = 0; i < p_count; ++i) {
  203. const Vector3 x = leaves[i]->volume.get_center() - org;
  204. for (int j = 0; j < 3; ++j) {
  205. ++splitcount[j][x.dot(axis[j]) > 0 ? 1 : 0];
  206. }
  207. }
  208. for (i = 0; i < 3; ++i) {
  209. if ((splitcount[i][0] > 0) && (splitcount[i][1] > 0)) {
  210. const int midp = (int)Math::abs(real_t(splitcount[i][0] - splitcount[i][1]));
  211. if (midp < bestmidp) {
  212. bestaxis = i;
  213. bestmidp = midp;
  214. }
  215. }
  216. }
  217. if (bestaxis >= 0) {
  218. partition = _split(leaves, p_count, org, axis[bestaxis]);
  219. ERR_FAIL_COND_V(partition == 0 || partition == p_count, nullptr);
  220. } else {
  221. partition = p_count / 2 + 1;
  222. }
  223. Node *node = _create_node_with_volume(nullptr, vol, nullptr);
  224. node->childs[0] = _top_down(&leaves[0], partition, p_bu_threshold);
  225. node->childs[1] = _top_down(&leaves[partition], p_count - partition, p_bu_threshold);
  226. node->childs[0]->parent = node;
  227. node->childs[1]->parent = node;
  228. return (node);
  229. } else {
  230. _bottom_up(leaves, p_count);
  231. return (leaves[0]);
  232. }
  233. }
  234. return (leaves[0]);
  235. }
  236. DynamicBVH::Node *DynamicBVH::_node_sort(Node *n, Node *&r) {
  237. Node *p = n->parent;
  238. ERR_FAIL_COND_V(!n->is_internal(), nullptr);
  239. if (p > n) {
  240. const int i = n->get_index_in_parent();
  241. const int j = 1 - i;
  242. Node *s = p->childs[j];
  243. Node *q = p->parent;
  244. ERR_FAIL_COND_V(n != p->childs[i], nullptr);
  245. if (q)
  246. q->childs[p->get_index_in_parent()] = n;
  247. else
  248. r = n;
  249. s->parent = n;
  250. p->parent = n;
  251. n->parent = q;
  252. p->childs[0] = n->childs[0];
  253. p->childs[1] = n->childs[1];
  254. n->childs[0]->parent = p;
  255. n->childs[1]->parent = p;
  256. n->childs[i] = p;
  257. n->childs[j] = s;
  258. SWAP(p->volume, n->volume);
  259. return (p);
  260. }
  261. return (n);
  262. }
  263. void DynamicBVH::clear() {
  264. if (bvh_root) {
  265. _recurse_delete_node(bvh_root);
  266. }
  267. lkhd = -1;
  268. opath = 0;
  269. }
  270. void DynamicBVH::optimize_bottom_up() {
  271. if (bvh_root) {
  272. LocalVector<Node *> leaves;
  273. _fetch_leaves(bvh_root, leaves);
  274. _bottom_up(&leaves[0], leaves.size());
  275. bvh_root = leaves[0];
  276. }
  277. }
  278. void DynamicBVH::optimize_top_down(int bu_threshold) {
  279. if (bvh_root) {
  280. LocalVector<Node *> leaves;
  281. _fetch_leaves(bvh_root, leaves);
  282. bvh_root = _top_down(&leaves[0], leaves.size(), bu_threshold);
  283. }
  284. }
  285. void DynamicBVH::optimize_incremental(int passes) {
  286. if (passes < 0)
  287. passes = total_leaves;
  288. if (bvh_root && (passes > 0)) {
  289. do {
  290. Node *node = bvh_root;
  291. unsigned bit = 0;
  292. while (node->is_internal()) {
  293. node = _node_sort(node, bvh_root)->childs[(opath >> bit) & 1];
  294. bit = (bit + 1) & (sizeof(unsigned) * 8 - 1);
  295. }
  296. _update(node);
  297. ++opath;
  298. } while (--passes);
  299. }
  300. }
  301. DynamicBVH::ID DynamicBVH::insert(const AABB &p_box, void *p_userdata) {
  302. Volume volume;
  303. volume.min = p_box.position;
  304. volume.max = p_box.position + p_box.size;
  305. Node *leaf = _create_node_with_volume(nullptr, volume, p_userdata);
  306. _insert_leaf(bvh_root, leaf);
  307. ++total_leaves;
  308. ID id;
  309. id.node = leaf;
  310. return id;
  311. }
  312. void DynamicBVH::_update(Node *leaf, int lookahead) {
  313. Node *root = _remove_leaf(leaf);
  314. if (root) {
  315. if (lookahead >= 0) {
  316. for (int i = 0; (i < lookahead) && root->parent; ++i) {
  317. root = root->parent;
  318. }
  319. } else
  320. root = bvh_root;
  321. }
  322. _insert_leaf(root, leaf);
  323. }
  324. bool DynamicBVH::update(const ID &p_id, const AABB &p_box) {
  325. ERR_FAIL_COND_V(!p_id.is_valid(), false);
  326. Node *leaf = p_id.node;
  327. Volume volume;
  328. volume.min = p_box.position;
  329. volume.max = p_box.position + p_box.size;
  330. if (leaf->volume.min.is_equal_approx(volume.min) && leaf->volume.max.is_equal_approx(volume.max)) {
  331. // noop
  332. return false;
  333. }
  334. Node *base = _remove_leaf(leaf);
  335. if (base) {
  336. if (lkhd >= 0) {
  337. for (int i = 0; (i < lkhd) && base->parent; ++i) {
  338. base = base->parent;
  339. }
  340. } else
  341. base = bvh_root;
  342. }
  343. leaf->volume = volume;
  344. _insert_leaf(base, leaf);
  345. return true;
  346. }
  347. void DynamicBVH::remove(const ID &p_id) {
  348. ERR_FAIL_COND(!p_id.is_valid());
  349. Node *leaf = p_id.node;
  350. _remove_leaf(leaf);
  351. _delete_node(leaf);
  352. --total_leaves;
  353. }
  354. void DynamicBVH::_extract_leaves(Node *p_node, List<ID> *r_elements) {
  355. if (p_node->is_internal()) {
  356. _extract_leaves(p_node->childs[0], r_elements);
  357. _extract_leaves(p_node->childs[1], r_elements);
  358. } else {
  359. ID id;
  360. id.node = p_node;
  361. r_elements->push_back(id);
  362. }
  363. }
  364. void DynamicBVH::set_index(uint32_t p_index) {
  365. ERR_FAIL_COND(bvh_root != nullptr);
  366. index = p_index;
  367. }
  368. uint32_t DynamicBVH::get_index() const {
  369. return index;
  370. }
  371. void DynamicBVH::get_elements(List<ID> *r_elements) {
  372. if (bvh_root) {
  373. _extract_leaves(bvh_root, r_elements);
  374. }
  375. }
  376. int DynamicBVH::get_leaf_count() const {
  377. return total_leaves;
  378. }
  379. int DynamicBVH::get_max_depth() const {
  380. if (bvh_root) {
  381. int depth = 1;
  382. int max_depth = 0;
  383. bvh_root->get_max_depth(depth, max_depth);
  384. return max_depth;
  385. } else {
  386. return 0;
  387. }
  388. }
  389. DynamicBVH::~DynamicBVH() {
  390. clear();
  391. }