30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using SiliconStudio.Core.Serialization;
35 namespace SiliconStudio.Core.Mathematics
41 [StructLayout(LayoutKind.Sequential, Pack = 4)]
73 return Collision.RayIntersectsSphere(ref ray, ref
this, out distance);
85 return Collision.RayIntersectsSphere(ref ray, ref
this, out distance);
97 return Collision.RayIntersectsSphere(ref ray, ref
this, out point);
107 return Collision.PlaneIntersectsSphere(ref plane, ref
this);
119 return Collision.SphereIntersectsTriangle(ref
this, ref vertex1, ref vertex2, ref vertex3);
129 return Collision.BoxIntersectsSphere(ref box, ref
this);
139 return Collision.SphereIntersectsSphere(ref
this, ref sphere);
149 return Collision.SphereContainsPoint(ref
this, ref point);
161 return Collision.SphereContainsTriangle(ref
this, ref vertex1, ref vertex2, ref vertex3);
171 return Collision.SphereContainsBox(ref
this, ref box);
181 return Collision.SphereContainsSphere(ref
this, ref sphere);
193 for (
int i = 0; i < points.Length; ++i)
195 Vector3.Add(ref points[i], ref center, out center);
199 center /= (float)points.
Length;
203 for (
int i = 0; i < points.Length; ++i)
208 Vector3.DistanceSquared(ref center, ref points[i], out distance);
210 if (distance > radius)
215 radius = (float)Math.Sqrt(radius);
218 result.Center = center;
219 result.Radius = radius;
230 FromPoints(points, out result);
241 Vector3.Lerp(ref box.Minimum, ref box.Maximum, 0.5f, out result.Center);
243 float x = box.Minimum.X - box.Maximum.X;
244 float y = box.Minimum.Y - box.Maximum.Y;
245 float z = box.Minimum.Z - box.Maximum.Z;
247 float distance = (float)(Math.Sqrt((x * x) + (y * y) + (z * z)));
248 result.Radius = distance * 0.5f;
259 FromBox(ref box, out result);
271 Vector3 difference = value2.Center - value1.Center;
273 float length = difference.Length();
274 float radius = value1.Radius;
275 float radius2 = value2.Radius;
277 if (radius + radius2 >= length)
279 if (radius - radius2 >= length)
285 if (radius2 - radius >= length)
292 Vector3 vector = difference * (1.0f / length);
293 float min = Math.Min(-radius, length - radius2);
294 float max = (Math.Max(radius, length + radius2) - min) * 0.5f;
296 result.Center = value1.Center + vector * (max + min);
309 Merge(ref value1, ref value2, out result);
321 return left.Equals(right);
332 return !left.Equals(right);
343 return string.Format(CultureInfo.CurrentCulture,
"Center:{0} Radius:{1}", Center.ToString(), Radius.ToString());
358 return string.Format(CultureInfo.CurrentCulture,
"Center:{0} Radius:{1}", Center.ToString(
format, CultureInfo.CurrentCulture),
359 Radius.ToString(format, CultureInfo.CurrentCulture));
369 public string ToString(IFormatProvider formatProvider)
371 return string.Format(formatProvider,
"Center:{0} Radius:{1}", Center.ToString(), Radius.ToString());
385 return ToString(formatProvider);
387 return string.Format(formatProvider,
"Center:{0} Radius:{1}", Center.ToString(
format, formatProvider),
388 Radius.ToString(format, formatProvider));
399 return Center.GetHashCode() + Radius.GetHashCode();
411 return Center == value.Center && Radius == value.Radius;
421 public override bool Equals(
object value)
426 if (value.GetType() != GetType())
438 public static implicit
operator SlimDX.BoundingSphere(
BoundingSphere value)
440 return new SlimDX.BoundingSphere(value.Center, value.Radius);
448 public static implicit
operator BoundingSphere(SlimDX.BoundingSphere value)
450 return new BoundingSphere(value.Center, value.Radius);
460 public static implicit
operator Microsoft.Xna.Framework.BoundingSphere(BoundingSphere value)
462 return new Microsoft.Xna.Framework.BoundingSphere(value.Center, value.Radius);
470 public static implicit
operator BoundingSphere(Microsoft.Xna.Framework.BoundingSphere value)
472 return new BoundingSphere(value.Center, value.Radius);
Represents an axis-aligned bounding box in three dimensional space.
override int GetHashCode()
Returns a hash code for this instance.
override string ToString()
Returns a System.String that represents this instance.
static BoundingSphere Merge(BoundingSphere value1, BoundingSphere value2)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere that is the as large as the total combined...
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Represents a three dimensional mathematical vector.
float Length()
Calculates the length of the vector.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
bool Intersects(ref BoundingSphere sphere)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
static void Merge(ref BoundingSphere value1, ref BoundingSphere value2, out BoundingSphere result)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere that is the as large as the total combined...
ContainmentType Contains(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Determines whether the current objects contains a triangle.
Represents a bounding sphere in three dimensional space.
bool Intersects(ref Ray ray, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
static BoundingSphere FromPoints(Vector3[] points)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere that fully contains the given points...
static void FromBox(ref BoundingBox box, out BoundingSphere result)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere from a given box.
Represents a plane in three dimensional space.
ContainmentType
Describes how one bounding volume contains another.
Vector3 Center
The center of the sphere in three dimensional space.
string ToString(string format)
Returns a System.String that represents this instance.
Represents a three dimensional line based on a point in space and a direction.
static void FromPoints(Vector3[] points, out BoundingSphere result)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere that fully contains the given points...
PlaneIntersectionType
Describes the result of an intersection with a plane in three dimensions.
bool Intersects(ref Ray ray)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
BoundingSphere(Vector3 center, float radius)
Initializes a new instance of the SiliconStudio.Core.Mathematics.BoundingBox struct.
bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Determines if there is an intersection between the current object and a triangle. ...
ContainmentType Contains(ref BoundingSphere sphere)
Determines whether the current objects contains a SiliconStudio.Core.Mathematics.BoundingSphere.
static BoundingSphere FromBox(BoundingBox box)
Constructs a SiliconStudio.Core.Mathematics.BoundingSphere from a given box.
bool Intersects(ref BoundingBox box)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
ContainmentType Contains(ref Vector3 point)
Determines whether the current objects contains a point.
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
bool Intersects(ref Ray ray, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
PlaneIntersectionType Intersects(ref Plane plane)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
ContainmentType Contains(ref BoundingBox box)
Determines whether the current objects contains a SiliconStudio.Core.Mathematics.BoundingBox.
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
bool Equals(BoundingSphere value)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector4 is equal to this instance...
float Radius
The radious of the sphere.
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z