1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
#region License
/*
MIT License
Copyright © 2006 The Mono.Xna Team
All rights reserved.
Authors:
Olivier Dufour (Duff)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion License
using System;
using System.Collections.Generic;
using System.Globalization;
using System.ComponentModel;
namespace Microsoft.Xna.Framework
{
public struct BoundingSphere : IEquatable<BoundingSphere>
{
#region Public Fields
public Vector3 Center;
public float Radius;
#endregion Public Fields
#region Constructors
public BoundingSphere(Vector3 center, float radius)
{
this.Center = center;
this.Radius = radius;
}
#endregion Constructors
#region Public Methods
public BoundingSphere Transform(Matrix matrix)
{
BoundingSphere sphere = new BoundingSphere();
sphere.Center = Vector3.Transform(this.Center, matrix);
sphere.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33)))));
return sphere;
}
public void Transform(ref Matrix matrix, out BoundingSphere result)
{
result.Center = Vector3.Transform(this.Center, matrix);
result.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33)))));
}
public ContainmentType Contains(BoundingBox box)
{
//check if all corner is in sphere
bool inside = true;
foreach (Vector3 corner in box.GetCorners())
{
if (this.Contains(corner) == ContainmentType.Disjoint)
{
inside = false;
break;
}
}
if (inside)
return ContainmentType.Contains;
//check if the distance from sphere center to cube face < radius
double dmin = 0;
if (Center.X < box.Min.X)
dmin += (Center.X - box.Min.X) * (Center.X - box.Min.X);
else if (Center.X > box.Max.X)
dmin += (Center.X - box.Max.X) * (Center.X - box.Max.X);
if (Center.Y < box.Min.Y)
dmin += (Center.Y - box.Min.Y) * (Center.Y - box.Min.Y);
else if (Center.Y > box.Max.Y)
dmin += (Center.Y - box.Max.Y) * (Center.Y - box.Max.Y);
if (Center.Z < box.Min.Z)
dmin += (Center.Z - box.Min.Z) * (Center.Z - box.Min.Z);
else if (Center.Z > box.Max.Z)
dmin += (Center.Z - box.Max.Z) * (Center.Z - box.Max.Z);
if (dmin <= Radius * Radius)
return ContainmentType.Intersects;
//else disjoint
return ContainmentType.Disjoint;
}
public void Contains(ref BoundingBox box, out ContainmentType result)
{
result = this.Contains(box);
}
public ContainmentType Contains(BoundingFrustum frustum)
{
//check if all corner is in sphere
bool inside = true;
Vector3[] corners = frustum.GetCorners();
foreach (Vector3 corner in corners)
{
if (this.Contains(corner) == ContainmentType.Disjoint)
{
inside = false;
break;
}
}
if (inside)
return ContainmentType.Contains;
//check if the distance from sphere center to frustrum face < radius
double dmin = 0;
//TODO : calcul dmin
if (dmin <= Radius * Radius)
return ContainmentType.Intersects;
//else disjoint
return ContainmentType.Disjoint;
}
public ContainmentType Contains(BoundingSphere sphere)
{
float val = Vector3.Distance(sphere.Center, Center);
if (val > sphere.Radius + Radius)
return ContainmentType.Disjoint;
else if (val <= Radius - sphere.Radius)
return ContainmentType.Contains;
else
return ContainmentType.Intersects;
}
public void Contains(ref BoundingSphere sphere, out ContainmentType result)
{
result = Contains(sphere);
}
public ContainmentType Contains(Vector3 point)
{
float distance = Vector3.Distance(point, Center);
if (distance > this.Radius)
return ContainmentType.Disjoint;
else if (distance < this.Radius)
return ContainmentType.Contains;
return ContainmentType.Intersects;
}
public void Contains(ref Vector3 point, out ContainmentType result)
{
result = Contains(point);
}
public static BoundingSphere CreateFromBoundingBox(BoundingBox box)
{
// Find the center of the box.
Vector3 center = new Vector3((box.Min.X + box.Max.X) / 2.0f,
(box.Min.Y + box.Max.Y) / 2.0f,
(box.Min.Z + box.Max.Z) / 2.0f);
// Find the distance between the center and one of the corners of the box.
float radius = Vector3.Distance(center, box.Max);
return new BoundingSphere(center, radius);
}
public static void CreateFromBoundingBox(ref BoundingBox box, out BoundingSphere result)
{
result = CreateFromBoundingBox(box);
}
public static BoundingSphere CreateFromFrustum(BoundingFrustum frustum)
{
return BoundingSphere.CreateFromPoints(frustum.GetCorners());
}
public static BoundingSphere CreateFromPoints(IEnumerable<Vector3> points)
{
if (points == null)
throw new ArgumentNullException("points");
float radius = 0;
Vector3 center = new Vector3();
// First, we'll find the center of gravity for the point 'cloud'.
int num_points = 0; // The number of points (there MUST be a better way to get this instead of counting the number of points one by one?)
foreach (Vector3 v in points)
{
center += v; // If we actually knew the number of points, we'd get better accuracy by adding v / num_points.
++num_points;
}
center /= (float)num_points;
// Calculate the radius of the needed sphere (it equals the distance between the center and the point further away).
foreach (Vector3 v in points)
{
float distance = ((Vector3)(v - center)).Length();
if (distance > radius)
radius = distance;
}
return new BoundingSphere(center, radius);
}
public static BoundingSphere CreateMerged(BoundingSphere original, BoundingSphere additional)
{
Vector3 ocenterToaCenter = Vector3.Subtract(additional.Center, original.Center);
float distance = ocenterToaCenter.Length();
if (distance <= original.Radius + additional.Radius)//intersect
{
if (distance <= original.Radius - additional.Radius)//original contain additional
return original;
if (distance <= additional.Radius - original.Radius)//additional contain original
return additional;
}
//else find center of new sphere and radius
float leftRadius = Math.Max(original.Radius - distance, additional.Radius);
float Rightradius = Math.Max(original.Radius + distance, additional.Radius);
ocenterToaCenter = ocenterToaCenter + (((leftRadius - Rightradius) / (2 * ocenterToaCenter.Length())) * ocenterToaCenter);//oCenterToResultCenter
BoundingSphere result = new BoundingSphere();
result.Center = original.Center + ocenterToaCenter;
result.Radius = (leftRadius + Rightradius) / 2;
return result;
}
public static void CreateMerged(ref BoundingSphere original, ref BoundingSphere additional, out BoundingSphere result)
{
result = BoundingSphere.CreateMerged(original, additional);
}
public bool Equals(BoundingSphere other)
{
return this.Center == other.Center && this.Radius == other.Radius;
}
public override bool Equals(object obj)
{
if (obj is BoundingSphere)
return this.Equals((BoundingSphere)obj);
return false;
}
public override int GetHashCode()
{
return this.Center.GetHashCode() + this.Radius.GetHashCode();
}
public bool Intersects(BoundingBox box)
{
return box.Intersects(this);
}
public void Intersects(ref BoundingBox box, out bool result)
{
result = Intersects(box);
}
public bool Intersects(BoundingFrustum frustum)
{
if (frustum == null)
throw new NullReferenceException();
throw new NotImplementedException();
}
public bool Intersects(BoundingSphere sphere)
{
float val = Vector3.Distance(sphere.Center, Center);
if (val > sphere.Radius + Radius)
return false;
return true;
}
public void Intersects(ref BoundingSphere sphere, out bool result)
{
result = Intersects(sphere);
}
public PlaneIntersectionType Intersects(Plane plane)
{
float distance = Vector3.Dot(plane.Normal, this.Center) + plane.D;
if (distance > this.Radius)
return PlaneIntersectionType.Front;
if (distance < -this.Radius)
return PlaneIntersectionType.Back;
//else it intersect
return PlaneIntersectionType.Intersecting;
}
public void Intersects(ref Plane plane, out PlaneIntersectionType result)
{
result = Intersects(plane);
}
public Nullable<float> Intersects(Ray ray)
{
return ray.Intersects(this);
}
public void Intersects(ref Ray ray, out Nullable<float> result)
{
result = Intersects(ray);
}
public static bool operator == (BoundingSphere a, BoundingSphere b)
{
return a.Equals(b);
}
public static bool operator != (BoundingSphere a, BoundingSphere b)
{
return !a.Equals(b);
}
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "{{Center:{0} Radius:{1}}}", this.Center.ToString(), this.Radius.ToString());
}
#endregion Public Methods
}
}
|