From f1b67935b3857c014c9c4922fd9c8b558c4f9344 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Sat, 29 Oct 2022 21:13:21 +0200 Subject: [PATCH] Optimize `BoundingFrustum::Intersects(BoundingSphere)` --- Source/Engine/Core/Math/BoundingFrustum.cpp | 11 +++++++++++ Source/Engine/Core/Math/BoundingFrustum.h | 5 +---- Source/Engine/Core/Math/Vector3.cs | 3 +++ Source/Engine/Core/Math/Vector3.h | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Core/Math/BoundingFrustum.cpp b/Source/Engine/Core/Math/BoundingFrustum.cpp index 88f35b8ca..11601d8e5 100644 --- a/Source/Engine/Core/Math/BoundingFrustum.cpp +++ b/Source/Engine/Core/Math/BoundingFrustum.cpp @@ -178,3 +178,14 @@ ContainmentType BoundingFrustum::Contains(const BoundingSphere& sphere) const return ContainmentType::Contains; } } + +bool BoundingFrustum::Intersects(const BoundingSphere& sphere) const +{ + for (int32 i = 0; i < 6; i++) + { + const Real distance = Vector3::Dot(_planes[i].Normal, sphere.Center) + _planes[i].D; + if (distance < -sphere.Radius) + return false; + } + return true; +} diff --git a/Source/Engine/Core/Math/BoundingFrustum.h b/Source/Engine/Core/Math/BoundingFrustum.h index 2ceed02ab..693db318e 100644 --- a/Source/Engine/Core/Math/BoundingFrustum.h +++ b/Source/Engine/Core/Math/BoundingFrustum.h @@ -235,10 +235,7 @@ public: /// /// The sphere. /// True if the current BoundingFrustum intersects a BoundingSphere, otherwise false. - FORCE_INLINE bool Intersects(const BoundingSphere& sphere) const - { - return Contains(sphere) != ContainmentType::Disjoint; - } + bool Intersects(const BoundingSphere& sphere) const; /// /// Checks whether the current BoundingFrustum intersects a BoundingBox. diff --git a/Source/Engine/Core/Math/Vector3.cs b/Source/Engine/Core/Math/Vector3.cs index 1bf05ee8c..cb967f3f4 100644 --- a/Source/Engine/Core/Math/Vector3.cs +++ b/Source/Engine/Core/Math/Vector3.cs @@ -907,6 +907,7 @@ namespace FlaxEngine /// First source vector. /// Second source vector. /// When the method completes, contains the dot product of the two vectors. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Dot(ref Vector3 left, ref Vector3 right, out Real result) { result = left.X * right.X + left.Y * right.Y + left.Z * right.Z; @@ -918,6 +919,7 @@ namespace FlaxEngine /// First source vector. /// Second source vector. /// The dot product of the two vectors. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Real Dot(ref Vector3 left, ref Vector3 right) { return left.X * right.X + left.Y * right.Y + left.Z * right.Z; @@ -929,6 +931,7 @@ namespace FlaxEngine /// First source vector. /// Second source vector. /// The dot product of the two vectors. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Real Dot(Vector3 left, Vector3 right) { return left.X * right.X + left.Y * right.Y + left.Z * right.Z; diff --git a/Source/Engine/Core/Math/Vector3.h b/Source/Engine/Core/Math/Vector3.h index d7390c499..179a4cbf7 100644 --- a/Source/Engine/Core/Math/Vector3.h +++ b/Source/Engine/Core/Math/Vector3.h @@ -622,7 +622,7 @@ public: } // dot product with another vector - static T Dot(const Vector3Base& a, const Vector3Base& b) + FORCE_INLINE static T Dot(const Vector3Base& a, const Vector3Base& b) { return a.X * b.X + a.Y * b.Y + a.Z * b.Z; }