| 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 | |
| 14 | void clear() { |
| 15 | num_pairs = 0; |
| 16 | extended_pairs.reset(); |
| 17 | expanded_aabb = BOUNDS(); |
| 18 | } |
| 19 | |
| 20 | BOUNDS expanded_aabb; |
| 21 | |
| 22 | // maybe we can just use the number in the vector TODO |
| 23 | int32_t num_pairs; |
| 24 | LocalVector<Link> extended_pairs; |
| 25 | |
| 26 | void add_pair_to(BVHHandle h, void *p_userdata) { |
| 27 | Link temp; |
| 28 | temp.set(h, p_userdata); |
| 29 | |
| 30 | extended_pairs.push_back(temp); |
| 31 | num_pairs++; |
| 32 | } |
| 33 | |
| 34 | uint32_t find_pair_to(BVHHandle h) const { |
| 35 | for (int n = 0; n < num_pairs; n++) { |
| 36 | if (extended_pairs[n].handle == h) { |
| 37 | return n; |
| 38 | } |
| 39 | } |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | bool contains_pair_to(BVHHandle h) const { |
| 44 | return find_pair_to(h) != BVHCommon::INVALID; |
| 45 | } |
| 46 | |
| 47 | // return success |
| 48 | void *remove_pair_to(BVHHandle h) { |
| 49 | void *userdata = nullptr; |
| 50 | |
| 51 | for (int n = 0; n < num_pairs; n++) { |
| 52 | if (extended_pairs[n].handle == h) { |
| 53 | userdata = extended_pairs[n].userdata; |
| 54 | extended_pairs.remove_at_unordered(n); |
| 55 | num_pairs--; |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return userdata; |
| 61 | } |
| 62 | |
| 63 | // experiment : scale the pairing expansion by the number of pairs. |
| 64 | // when the number of pairs is high, the density is high and a lower collision margin is better. |
| 65 | // when there are few local pairs, a larger margin is more optimal. |
| 66 | real_t scale_expansion_margin(real_t p_margin) const { |
| 67 | real_t x = real_t(num_pairs) * (1.0 / 9.0); |
| 68 | x = MIN(x, 1.0); |
| 69 | x = 1.0 - x; |
| 70 | return p_margin * x; |
| 71 | } |
| 72 | }; |
| 73 |