Add BoundingSphere::Transform method

This commit is contained in:
Wojtek Figat
2021-02-23 09:53:44 +01:00
parent e4794c5754
commit 7fd542950f
3 changed files with 61 additions and 61 deletions

View File

@@ -2,6 +2,7 @@
#include "BoundingSphere.h" #include "BoundingSphere.h"
#include "BoundingBox.h" #include "BoundingBox.h"
#include "Matrix.h"
#include "Ray.h" #include "Ray.h"
#include "../Types/String.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.Center = value1.Center + vector * (max + min);
result.Radius = max; 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();
}

View File

@@ -81,18 +81,14 @@ namespace FlaxEngine
/// <returns>Whether the two objects intersected.</returns> /// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray) public bool Intersects(ref Ray ray)
{ {
float distance; return CollisionsHelper.RayIntersectsSphere(ref ray, ref this, out float _);
return CollisionsHelper.RayIntersectsSphere(ref ray, ref this, out distance);
} }
/// <summary> /// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Ray" />. /// Determines if there is an intersection between the current object and a <see cref="Ray" />.
/// </summary> /// </summary>
/// <param name="ray">The ray to test.</param> /// <param name="ray">The ray to test.</param>
/// <param name="distance"> /// <param name="distance">When the method completes, contains the distance of the intersection, or 0 if there was no intersection.</param>
/// When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.
/// </param>
/// <returns>Whether the two objects intersected.</returns> /// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray, out float distance) 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 <see cref="Ray" />. /// Determines if there is an intersection between the current object and a <see cref="Ray" />.
/// </summary> /// </summary>
/// <param name="ray">The ray to test.</param> /// <param name="ray">The ray to test.</param>
/// <param name="point"> /// <param name="point">When the method completes, contains the point of intersection, or <see cref="Vector3.Zero" /> if there was no intersection.</param>
/// When the method completes, contains the point of intersection,
/// or <see cref="Vector3.Zero" /> if there was no intersection.
/// </param>
/// <returns>Whether the two objects intersected.</returns> /// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray, out Vector3 point) public bool Intersects(ref Ray ray, out Vector3 point)
{ {
@@ -225,11 +218,7 @@ namespace FlaxEngine
/// <param name="count">The count of points to process to compute the bounding sphere.</param> /// <param name="count">The count of points to process to compute the bounding sphere.</param>
/// <param name="result">When the method completes, contains the newly constructed bounding sphere.</param> /// <param name="result">When the method completes, contains the newly constructed bounding sphere.</param>
/// <exception cref="System.ArgumentNullException">points</exception> /// <exception cref="System.ArgumentNullException">points</exception>
/// <exception cref="System.ArgumentOutOfRangeException"> /// <exception cref="System.ArgumentOutOfRangeException">start or count</exception>
/// start
/// or
/// count
/// </exception>
public static void FromPoints(Vector3[] points, int start, int count, out BoundingSphere result) public static void FromPoints(Vector3[] points, int start, int count, out BoundingSphere result)
{ {
if (points == null) if (points == null)
@@ -245,20 +234,19 @@ namespace FlaxEngine
int upperEnd = start + count; int upperEnd = start + count;
//Find the center of all points. // Find the center of all points
Vector3 center = Vector3.Zero; Vector3 center = Vector3.Zero;
for (int i = start; i < upperEnd; ++i) for (int i = start; i < upperEnd; ++i)
Vector3.Add(ref points[i], ref center, out center); 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; center /= (float)count;
//Find the radius of the sphere //Find the radius of the sphere
var radius = 0f; var radius = 0f;
for (int i = start; i < upperEnd; ++i) for (int i = start; i < upperEnd; ++i)
{ {
//We are doing a relative distance comparison to find the maximum distance // We are doing a relative distance comparison to find the maximum distance from the center of our sphere
//from the center of our sphere.
float distance; float distance;
Vector3.DistanceSquared(ref center, ref points[i], out distance); Vector3.DistanceSquared(ref center, ref points[i], out distance);
@@ -266,10 +254,10 @@ namespace FlaxEngine
radius = distance; radius = distance;
} }
//Find the real distance from the DistanceSquared. // Find the real distance from the DistanceSquared
radius = (float)Math.Sqrt(radius); radius = (float)Math.Sqrt(radius);
//Construct the sphere. // Construct the sphere
result.Center = center; result.Center = center;
result.Radius = radius; result.Radius = radius;
} }
@@ -283,7 +271,6 @@ namespace FlaxEngine
{ {
if (points == null) if (points == null)
throw new ArgumentNullException(nameof(points)); throw new ArgumentNullException(nameof(points));
FromPoints(points, 0, points.Length, out result); FromPoints(points, 0, points.Length, out result);
} }
@@ -294,8 +281,7 @@ namespace FlaxEngine
/// <returns>The newly constructed bounding sphere.</returns> /// <returns>The newly constructed bounding sphere.</returns>
public static BoundingSphere FromPoints(Vector3[] points) public static BoundingSphere FromPoints(Vector3[] points)
{ {
BoundingSphere result; FromPoints(points, out var result);
FromPoints(points, out result);
return result; return result;
} }
@@ -323,8 +309,7 @@ namespace FlaxEngine
/// <returns>The newly constructed bounding sphere.</returns> /// <returns>The newly constructed bounding sphere.</returns>
public static BoundingSphere FromBox(BoundingBox box) public static BoundingSphere FromBox(BoundingBox box)
{ {
BoundingSphere result; FromBox(ref box, out var result);
FromBox(ref box, out result);
return result; return result;
} }
@@ -387,20 +372,40 @@ namespace FlaxEngine
/// <returns>The newly constructed bounding sphere.</returns> /// <returns>The newly constructed bounding sphere.</returns>
public static BoundingSphere Merge(BoundingSphere value1, BoundingSphere value2) public static BoundingSphere Merge(BoundingSphere value1, BoundingSphere value2)
{ {
BoundingSphere result; Merge(ref value1, ref value2, out var result);
Merge(ref value1, ref value2, out result);
return result; return result;
} }
/// <summary>
/// Transforms the bounding sphere using the specified matrix.
/// </summary>
/// <param name="sphere">The sphere.</param>
/// <param name="matrix">The matrix.</param>
/// <remarks>The result transformed sphere.</remarks>
public static BoundingSphere Transform(BoundingSphere sphere, Matrix matrix)
{
Transform(ref sphere, ref matrix, out var result);
return result;
}
/// <summary>
/// Transforms the bounding sphere using the specified matrix.
/// </summary>
/// <param name="sphere">The sphere.</param>
/// <param name="matrix">The matrix.</param>
/// <param name="result">The result transformed sphere.</param>
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;
}
/// <summary> /// <summary>
/// Tests for equality between two objects. /// Tests for equality between two objects.
/// </summary> /// </summary>
/// <param name="left">The first value to compare.</param> /// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param> /// <param name="right">The second value to compare.</param>
/// <returns> /// <returns><c>true</c> if <paramref name="left" /> has the same value as <paramref name="right" />; otherwise, <c>false</c>.</returns>
/// <c>true</c> if <paramref name="left" /> has the same value as <paramref name="right" />; otherwise,
/// <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(BoundingSphere left, BoundingSphere right) public static bool operator ==(BoundingSphere left, BoundingSphere right)
{ {
@@ -412,10 +417,7 @@ namespace FlaxEngine
/// </summary> /// </summary>
/// <param name="left">The first value to compare.</param> /// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param> /// <param name="right">The second value to compare.</param>
/// <returns> /// <returns><c>true</c> if <paramref name="left" /> has a different value than <paramref name="right" />; otherwise, <c>false</c>.</returns>
/// <c>true</c> if <paramref name="left" /> has a different value than <paramref name="right" />; otherwise,
/// <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(BoundingSphere left, BoundingSphere right) public static bool operator !=(BoundingSphere left, BoundingSphere right)
{ {
@@ -425,9 +427,7 @@ namespace FlaxEngine
/// <summary> /// <summary>
/// Returns a <see cref="System.String" /> that represents this instance. /// Returns a <see cref="System.String" /> that represents this instance.
/// </summary> /// </summary>
/// <returns> /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public override string ToString() public override string ToString()
{ {
return string.Format(CultureInfo.CurrentCulture, return string.Format(CultureInfo.CurrentCulture,
@@ -440,9 +440,7 @@ namespace FlaxEngine
/// Returns a <see cref="System.String" /> that represents this instance. /// Returns a <see cref="System.String" /> that represents this instance.
/// </summary> /// </summary>
/// <param name="format">The format.</param> /// <param name="format">The format.</param>
/// <returns> /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(string format) public string ToString(string format)
{ {
if (format == null) if (format == null)
@@ -458,9 +456,7 @@ namespace FlaxEngine
/// Returns a <see cref="System.String" /> that represents this instance. /// Returns a <see cref="System.String" /> that represents this instance.
/// </summary> /// </summary>
/// <param name="formatProvider">The format provider.</param> /// <param name="formatProvider">The format provider.</param>
/// <returns> /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider) public string ToString(IFormatProvider formatProvider)
{ {
return string.Format(formatProvider, return string.Format(formatProvider,
@@ -474,9 +470,7 @@ namespace FlaxEngine
/// </summary> /// </summary>
/// <param name="format">The format.</param> /// <param name="format">The format.</param>
/// <param name="formatProvider">The format provider.</param> /// <param name="formatProvider">The format provider.</param>
/// <returns> /// <returns>A <see cref="System.String" /> that represents this instance.</returns>
/// A <see cref="System.String" /> that represents this instance.
/// </returns>
public string ToString(string format, IFormatProvider formatProvider) public string ToString(string format, IFormatProvider formatProvider)
{ {
if (format == null) if (format == null)
@@ -491,9 +485,7 @@ namespace FlaxEngine
/// <summary> /// <summary>
/// Returns a hash code for this instance. /// Returns a hash code for this instance.
/// </summary> /// </summary>
/// <returns> /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode() public override int GetHashCode()
{ {
unchecked unchecked
@@ -506,9 +498,7 @@ namespace FlaxEngine
/// Determines whether the specified <see cref="Vector4" /> is equal to this instance. /// Determines whether the specified <see cref="Vector4" /> is equal to this instance.
/// </summary> /// </summary>
/// <param name="value">The <see cref="Vector4" /> to compare with this instance.</param> /// <param name="value">The <see cref="Vector4" /> to compare with this instance.</param>
/// <returns> /// <returns><c>true</c> if the specified <see cref="Vector4" /> is equal to this instance; otherwise, <c>false</c>.</returns>
/// <c>true</c> if the specified <see cref="Vector4" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(ref BoundingSphere value) public bool Equals(ref BoundingSphere value)
{ {
@@ -519,9 +509,7 @@ namespace FlaxEngine
/// Determines whether the specified <see cref="Vector4" /> is equal to this instance. /// Determines whether the specified <see cref="Vector4" /> is equal to this instance.
/// </summary> /// </summary>
/// <param name="value">The <see cref="Vector4" /> to compare with this instance.</param> /// <param name="value">The <see cref="Vector4" /> to compare with this instance.</param>
/// <returns> /// <returns><c>true</c> if the specified <see cref="Vector4" /> is equal to this instance; otherwise, <c>false</c>.</returns>
/// <c>true</c> if the specified <see cref="Vector4" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(BoundingSphere value) public bool Equals(BoundingSphere value)
{ {
@@ -532,14 +520,11 @@ namespace FlaxEngine
/// Determines whether the specified <see cref="System.Object" /> is equal to this instance. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
/// </summary> /// </summary>
/// <param name="value">The <see cref="System.Object" /> to compare with this instance.</param> /// <param name="value">The <see cref="System.Object" /> to compare with this instance.</param>
/// <returns> /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object value) public override bool Equals(object value)
{ {
if (!(value is BoundingSphere)) if (!(value is BoundingSphere))
return false; return false;
var strongValue = (BoundingSphere)value; var strongValue = (BoundingSphere)value;
return Equals(ref strongValue); return Equals(ref strongValue);
} }

View File

@@ -218,6 +218,14 @@ public:
/// <param name="value2">The point to merge.</param> /// <param name="value2">The point to merge.</param>
/// <param name="result">When the method completes, contains the newly constructed bounding sphere.</param> /// <param name="result">When the method completes, contains the newly constructed bounding sphere.</param>
static void Merge(const BoundingSphere& value1, const Vector3& value2, BoundingSphere& result); static void Merge(const BoundingSphere& value1, const Vector3& value2, BoundingSphere& result);
/// <summary>
/// Transforms the bounding sphere using the specified matrix.
/// </summary>
/// <param name="sphere">The sphere.</param>
/// <param name="matrix">The matrix.</param>
/// <param name="result">The result transformed sphere.</param>
static void Transform(const BoundingSphere& sphere, const Matrix& matrix, BoundingSphere& result);
}; };
template<> template<>