From 7fd542950f8d8318628835e29e25b37a9d5799e2 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 23 Feb 2021 09:53:44 +0100 Subject: [PATCH] Add `BoundingSphere::Transform` method --- Source/Engine/Core/Math/BoundingSphere.cpp | 7 ++ Source/Engine/Core/Math/BoundingSphere.cs | 107 +++++++++------------ Source/Engine/Core/Math/BoundingSphere.h | 8 ++ 3 files changed, 61 insertions(+), 61 deletions(-) diff --git a/Source/Engine/Core/Math/BoundingSphere.cpp b/Source/Engine/Core/Math/BoundingSphere.cpp index 3cdacfcc5..0b3ed6ba7 100644 --- a/Source/Engine/Core/Math/BoundingSphere.cpp +++ b/Source/Engine/Core/Math/BoundingSphere.cpp @@ -2,6 +2,7 @@ #include "BoundingSphere.h" #include "BoundingBox.h" +#include "Matrix.h" #include "Ray.h" #include "../Types/String.h" @@ -194,3 +195,9 @@ void BoundingSphere::Merge(const BoundingSphere& value1, const Vector3& value2, result.Center = value1.Center + vector * (max + min); result.Radius = max; } + +void BoundingSphere::Transform(const BoundingSphere& sphere, const Matrix& matrix, BoundingSphere& result) +{ + Vector3::Transform(sphere.Center, matrix, result.Center); + result.Radius = sphere.Radius * matrix.GetScaleVector().GetAbsolute().MaxValue(); +} diff --git a/Source/Engine/Core/Math/BoundingSphere.cs b/Source/Engine/Core/Math/BoundingSphere.cs index 0a9bc3784..1d05f44c3 100644 --- a/Source/Engine/Core/Math/BoundingSphere.cs +++ b/Source/Engine/Core/Math/BoundingSphere.cs @@ -81,18 +81,14 @@ namespace FlaxEngine /// Whether the two objects intersected. public bool Intersects(ref Ray ray) { - float distance; - return CollisionsHelper.RayIntersectsSphere(ref ray, ref this, out distance); + return CollisionsHelper.RayIntersectsSphere(ref ray, ref this, out float _); } /// /// Determines if there is an intersection between the current object and a . /// /// The ray to test. - /// - /// When the method completes, contains the distance of the intersection, - /// or 0 if there was no intersection. - /// + /// When the method completes, contains the distance of the intersection, or 0 if there was no intersection. /// Whether the two objects intersected. public bool Intersects(ref Ray ray, out float distance) { @@ -103,10 +99,7 @@ namespace FlaxEngine /// Determines if there is an intersection between the current object and a . /// /// The ray to test. - /// - /// When the method completes, contains the point of intersection, - /// or if there was no intersection. - /// + /// When the method completes, contains the point of intersection, or if there was no intersection. /// Whether the two objects intersected. public bool Intersects(ref Ray ray, out Vector3 point) { @@ -225,11 +218,7 @@ namespace FlaxEngine /// The count of points to process to compute the bounding sphere. /// When the method completes, contains the newly constructed bounding sphere. /// points - /// - /// start - /// or - /// count - /// + /// start or count public static void FromPoints(Vector3[] points, int start, int count, out BoundingSphere result) { if (points == null) @@ -245,20 +234,19 @@ namespace FlaxEngine int upperEnd = start + count; - //Find the center of all points. + // Find the center of all points Vector3 center = Vector3.Zero; for (int i = start; i < upperEnd; ++i) Vector3.Add(ref points[i], ref center, out center); - //This is the center of our sphere. + // This is the center of our sphere center /= (float)count; //Find the radius of the sphere var radius = 0f; for (int i = start; i < upperEnd; ++i) { - //We are doing a relative distance comparison to find the maximum distance - //from the center of our sphere. + // We are doing a relative distance comparison to find the maximum distance from the center of our sphere float distance; Vector3.DistanceSquared(ref center, ref points[i], out distance); @@ -266,10 +254,10 @@ namespace FlaxEngine radius = distance; } - //Find the real distance from the DistanceSquared. + // Find the real distance from the DistanceSquared radius = (float)Math.Sqrt(radius); - //Construct the sphere. + // Construct the sphere result.Center = center; result.Radius = radius; } @@ -283,7 +271,6 @@ namespace FlaxEngine { if (points == null) throw new ArgumentNullException(nameof(points)); - FromPoints(points, 0, points.Length, out result); } @@ -294,8 +281,7 @@ namespace FlaxEngine /// The newly constructed bounding sphere. public static BoundingSphere FromPoints(Vector3[] points) { - BoundingSphere result; - FromPoints(points, out result); + FromPoints(points, out var result); return result; } @@ -323,8 +309,7 @@ namespace FlaxEngine /// The newly constructed bounding sphere. public static BoundingSphere FromBox(BoundingBox box) { - BoundingSphere result; - FromBox(ref box, out result); + FromBox(ref box, out var result); return result; } @@ -387,20 +372,40 @@ namespace FlaxEngine /// The newly constructed bounding sphere. public static BoundingSphere Merge(BoundingSphere value1, BoundingSphere value2) { - BoundingSphere result; - Merge(ref value1, ref value2, out result); + Merge(ref value1, ref value2, out var result); return result; } + /// + /// Transforms the bounding sphere using the specified matrix. + /// + /// The sphere. + /// The matrix. + /// The result transformed sphere. + public static BoundingSphere Transform(BoundingSphere sphere, Matrix matrix) + { + Transform(ref sphere, ref matrix, out var result); + return result; + } + + /// + /// Transforms the bounding sphere using the specified matrix. + /// + /// The sphere. + /// The matrix. + /// The result transformed sphere. + public static void Transform(ref BoundingSphere sphere, ref Matrix matrix, out BoundingSphere result) + { + Vector3.Transform(ref sphere.Center, ref matrix, out result.Center); + result.Radius = sphere.Radius * matrix.ScaleVector.Absolute.MaxValue; + } + /// /// Tests for equality between two objects. /// /// The first value to compare. /// The second value to compare. - /// - /// true if has the same value as ; otherwise, - /// false. - /// + /// true if has the same value as ; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(BoundingSphere left, BoundingSphere right) { @@ -412,10 +417,7 @@ namespace FlaxEngine /// /// The first value to compare. /// The second value to compare. - /// - /// true if has a different value than ; otherwise, - /// false. - /// + /// true if has a different value than ; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(BoundingSphere left, BoundingSphere right) { @@ -425,9 +427,7 @@ namespace FlaxEngine /// /// Returns a that represents this instance. /// - /// - /// A that represents this instance. - /// + /// A that represents this instance. public override string ToString() { return string.Format(CultureInfo.CurrentCulture, @@ -440,9 +440,7 @@ namespace FlaxEngine /// Returns a that represents this instance. /// /// The format. - /// - /// A that represents this instance. - /// + /// A that represents this instance. public string ToString(string format) { if (format == null) @@ -458,9 +456,7 @@ namespace FlaxEngine /// Returns a that represents this instance. /// /// The format provider. - /// - /// A that represents this instance. - /// + /// A that represents this instance. public string ToString(IFormatProvider formatProvider) { return string.Format(formatProvider, @@ -474,9 +470,7 @@ namespace FlaxEngine /// /// The format. /// The format provider. - /// - /// A that represents this instance. - /// + /// A that represents this instance. public string ToString(string format, IFormatProvider formatProvider) { if (format == null) @@ -491,9 +485,7 @@ namespace FlaxEngine /// /// Returns a hash code for this instance. /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() { unchecked @@ -506,9 +498,7 @@ namespace FlaxEngine /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// + /// true if the specified is equal to this instance; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(ref BoundingSphere value) { @@ -519,9 +509,7 @@ namespace FlaxEngine /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// + /// true if the specified is equal to this instance; otherwise, false. [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Equals(BoundingSphere value) { @@ -532,14 +520,11 @@ namespace FlaxEngine /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// + /// true if the specified is equal to this instance; otherwise, false. public override bool Equals(object value) { if (!(value is BoundingSphere)) return false; - var strongValue = (BoundingSphere)value; return Equals(ref strongValue); } diff --git a/Source/Engine/Core/Math/BoundingSphere.h b/Source/Engine/Core/Math/BoundingSphere.h index bc6aa1e6e..17f6ec053 100644 --- a/Source/Engine/Core/Math/BoundingSphere.h +++ b/Source/Engine/Core/Math/BoundingSphere.h @@ -218,6 +218,14 @@ public: /// The point to merge. /// When the method completes, contains the newly constructed bounding sphere. static void Merge(const BoundingSphere& value1, const Vector3& value2, BoundingSphere& result); + + /// + /// Transforms the bounding sphere using the specified matrix. + /// + /// The sphere. + /// The matrix. + /// The result transformed sphere. + static void Transform(const BoundingSphere& sphere, const Matrix& matrix, BoundingSphere& result); }; template<>