diff options
author | RĂ©mi Verschelde <rverschelde@gmail.com> | 2019-09-03 12:26:43 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-03 12:26:43 +0200 |
commit | 726711d8c5bcabd914c2a561ac92fa43c2adf0e2 (patch) | |
tree | d27107a82c3c7a8a086b476798ca5a5e7a5041f9 /core/math | |
parent | 35ee5be1dd43063d16507b53f93d60e6875f762e (diff) | |
parent | e7febd72d66ffe36c50d79684805142d8153f666 (diff) |
Merge pull request #31756 from raphael10241024/fast_aabb_transform
a faster function to transform aabb
Diffstat (limited to 'core/math')
-rw-r--r-- | core/math/transform.h | 39 |
1 files changed, 23 insertions, 16 deletions
diff --git a/core/math/transform.h b/core/math/transform.h index 1fdc6398a1..90e2b07583 100644 --- a/core/math/transform.h +++ b/core/math/transform.h @@ -158,22 +158,29 @@ _FORCE_INLINE_ Plane Transform::xform_inv(const Plane &p_plane) const { } _FORCE_INLINE_ AABB Transform::xform(const AABB &p_aabb) const { - /* define vertices */ - Vector3 x = basis.get_axis(0) * p_aabb.size.x; - Vector3 y = basis.get_axis(1) * p_aabb.size.y; - Vector3 z = basis.get_axis(2) * p_aabb.size.z; - Vector3 pos = xform(p_aabb.position); - //could be even further optimized - AABB new_aabb; - new_aabb.position = pos; - new_aabb.expand_to(pos + x); - new_aabb.expand_to(pos + y); - new_aabb.expand_to(pos + z); - new_aabb.expand_to(pos + x + y); - new_aabb.expand_to(pos + x + z); - new_aabb.expand_to(pos + y + z); - new_aabb.expand_to(pos + x + y + z); - return new_aabb; + + /* http://dev.theomader.com/transform-bounding-boxes/ */ + Vector3 min = p_aabb.position; + Vector3 max = p_aabb.position + p_aabb.size; + Vector3 tmin, tmax; + for (int i = 0; i < 3; i++) { + tmin[i] = tmax[i] = origin[i]; + for (int j = 0; j < 3; j++) { + real_t e = basis[i][j] * min[j]; + real_t f = basis[i][j] * max[j]; + if (e < f) { + tmin[i] += e; + tmax[i] += f; + } else { + tmin[i] += f; + tmax[i] += e; + } + } + } + AABB r_aabb; + r_aabb.position = tmin; + r_aabb.size = tmax - tmin; + return r_aabb; } _FORCE_INLINE_ AABB Transform::xform_inv(const AABB &p_aabb) const { |