bvh_pair.inc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. public:
  2. // note .. maybe this can be attached to another node structure?
  3. // depends which works best for cache.
  4. struct ItemPairs {
  5. struct Link {
  6. void set(BVHHandle h, void *ud) {
  7. handle = h;
  8. userdata = ud;
  9. }
  10. BVHHandle handle;
  11. void *userdata;
  12. };
  13. void clear() {
  14. num_pairs = 0;
  15. extended_pairs.reset();
  16. expanded_aabb = BOUNDS();
  17. }
  18. BOUNDS expanded_aabb;
  19. // maybe we can just use the number in the vector TODO
  20. int32_t num_pairs;
  21. LocalVector<Link> extended_pairs;
  22. void add_pair_to(BVHHandle h, void *p_userdata) {
  23. Link temp;
  24. temp.set(h, p_userdata);
  25. extended_pairs.push_back(temp);
  26. num_pairs++;
  27. }
  28. uint32_t find_pair_to(BVHHandle h) const {
  29. for (int n = 0; n < num_pairs; n++) {
  30. if (extended_pairs[n].handle == h) {
  31. return n;
  32. }
  33. }
  34. return -1;
  35. }
  36. bool contains_pair_to(BVHHandle h) const {
  37. return find_pair_to(h) != BVHCommon::INVALID;
  38. }
  39. // return success
  40. void *remove_pair_to(BVHHandle h) {
  41. void *userdata = nullptr;
  42. for (int n = 0; n < num_pairs; n++) {
  43. if (extended_pairs[n].handle == h) {
  44. userdata = extended_pairs[n].userdata;
  45. extended_pairs.remove_at_unordered(n);
  46. num_pairs--;
  47. break;
  48. }
  49. }
  50. return userdata;
  51. }
  52. // experiment : scale the pairing expansion by the number of pairs.
  53. // when the number of pairs is high, the density is high and a lower collision margin is better.
  54. // when there are few local pairs, a larger margin is more optimal.
  55. real_t scale_expansion_margin(real_t p_margin) const {
  56. real_t x = real_t(num_pairs) * (1.0 / 9.0);
  57. x = MIN(x, 1.0);
  58. x = 1.0 - x;
  59. return p_margin * x;
  60. }
  61. };