Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
BoundingSphere.cs
Go to the documentation of this file.
1using System;
6using Microsoft.Xna.Framework.Design;
7
9
12public struct BoundingSphere : IEquatable<BoundingSphere>
13{
14 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
16
17 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
18 public float Radius;
19
21 {
22 if (radius < 0f)
23 {
25 }
26 Center = center;
27 Radius = radius;
28 }
29
31 {
32 if (Center == other.Center)
33 {
34 return Radius == other.Radius;
35 }
36 return false;
37 }
38
39 public override bool Equals(object obj)
40 {
41 bool result = false;
43 {
44 result = Equals((BoundingSphere)obj);
45 }
46 return result;
47 }
48
49 public override int GetHashCode()
50 {
51 return Center.GetHashCode() + Radius.GetHashCode();
52 }
53
54 public override string ToString()
55 {
57 return string.Format(currentCulture, "{{Center:{0} Radius:{1}}}", new object[2]
58 {
60 Radius.ToString(currentCulture)
61 });
62 }
63
65 {
66 Vector3.Subtract(ref additional.Center, ref original.Center, out var result);
67 float num = result.Length();
68 float radius = original.Radius;
69 float radius2 = additional.Radius;
70 if (radius + radius2 >= num)
71 {
72 if (radius - radius2 >= num)
73 {
74 return original;
75 }
76 if (radius2 - radius >= num)
77 {
78 return additional;
79 }
80 }
81 Vector3 vector = result * (1f / num);
82 float num2 = MathHelper.Min(0f - radius, num - radius2);
83 float num3 = MathHelper.Max(radius, num + radius2);
84 float num4 = (num3 - num2) * 0.5f;
86 result2.Center = original.Center + vector * (num4 + num2);
87 result2.Radius = num4;
88 return result2;
89 }
90
92 {
94 float num = result2.Length();
95 float radius = original.Radius;
96 float radius2 = additional.Radius;
97 if (radius + radius2 >= num)
98 {
99 if (radius - radius2 >= num)
100 {
101 result = original;
102 return;
103 }
104 if (radius2 - radius >= num)
105 {
106 result = additional;
107 return;
108 }
109 }
110 Vector3 vector = result2 * (1f / num);
111 float num2 = MathHelper.Min(0f - radius, num - radius2);
112 float num3 = MathHelper.Max(radius, num + radius2);
113 float num4 = (num3 - num2) * 0.5f;
114 result.Center = original.Center + vector * (num4 + num2);
115 result.Radius = num4;
116 }
117
119 {
120 BoundingSphere result = default(BoundingSphere);
121 Vector3.Lerp(ref box.Min, ref box.Max, 0.5f, out result.Center);
123 result.Radius = result2 * 0.5f;
124 return result;
125 }
126
128 {
129 Vector3.Lerp(ref box.Min, ref box.Max, 0.5f, out result.Center);
131 result.Radius = result2 * 0.5f;
132 }
133
135 {
136 if (points == null)
137 {
138 throw new ArgumentNullException("points");
139 }
141 if (!enumerator.MoveNext())
142 {
144 }
150 Vector3 value6 = (value5 = (value4 = (value3 = (value2 = (value = enumerator.Current)))));
151 foreach (Vector3 point in points)
152 {
153 if (point.X < value6.X)
154 {
155 value6 = point;
156 }
157 if (point.X > value5.X)
158 {
159 value5 = point;
160 }
161 if (point.Y < value4.Y)
162 {
163 value4 = point;
164 }
165 if (point.Y > value3.Y)
166 {
167 value3 = point;
168 }
169 if (point.Z < value2.Z)
170 {
171 value2 = point;
172 }
173 if (point.Z > value.Z)
174 {
175 value = point;
176 }
177 }
182 float num;
183 if (result > result2)
184 {
185 if (result > result3)
186 {
188 num = result * 0.5f;
189 }
190 else
191 {
193 num = result3 * 0.5f;
194 }
195 }
196 else if (result2 > result3)
197 {
199 num = result2 * 0.5f;
200 }
201 else
202 {
204 num = result3 * 0.5f;
205 }
206 Vector3 vector = default(Vector3);
207 foreach (Vector3 point2 in points)
208 {
209 vector.X = point2.X - result4.X;
210 vector.Y = point2.Y - result4.Y;
211 vector.Z = point2.Z - result4.Z;
212 float num2 = vector.Length();
213 if (num2 > num)
214 {
215 num = (num + num2) * 0.5f;
216 result4 += (1f - num / num2) * vector;
217 }
218 }
220 result5.Center = result4;
221 result5.Radius = num;
222 return result5;
223 }
224
226 {
227 if (frustum == null)
228 {
229 throw new ArgumentNullException("frustum");
230 }
231 return CreateFromPoints(frustum.cornerArray);
232 }
233
235 {
236 Vector3.Clamp(ref Center, ref box.Min, ref box.Max, out var result);
238 if (!(result2 > Radius * Radius))
239 {
240 return true;
241 }
242 return false;
243 }
244
245 public void Intersects(ref BoundingBox box, out bool result)
246 {
249 result = !(result3 > Radius * Radius);
250 }
251
253 {
254 if (null == frustum)
255 {
257 }
258 frustum.Intersects(ref this, out var result);
259 return result;
260 }
261
263 {
264 return plane.Intersects(this);
265 }
266
268 {
269 plane.Intersects(ref this, out result);
270 }
271
272 public float? Intersects(Ray ray)
273 {
274 return ray.Intersects(this);
275 }
276
277 public void Intersects(ref Ray ray, out float? result)
278 {
279 ray.Intersects(ref this, out result);
280 }
281
283 {
285 float radius = Radius;
286 float radius2 = sphere.Radius;
287 if (!(radius * radius + 2f * radius * radius2 + radius2 * radius2 > result))
288 {
289 return false;
290 }
291 return true;
292 }
293
294 public void Intersects(ref BoundingSphere sphere, out bool result)
295 {
297 float radius = Radius;
298 float radius2 = sphere.Radius;
299 result = ((radius * radius + 2f * radius * radius2 + radius2 * radius2 > result2) ? true : false);
300 }
301
303 {
304 if (!box.Intersects(this))
305 {
306 return ContainmentType.Disjoint;
307 }
308 float num = Radius * Radius;
309 Vector3 vector = default(Vector3);
310 vector.X = Center.X - box.Min.X;
311 vector.Y = Center.Y - box.Max.Y;
312 vector.Z = Center.Z - box.Max.Z;
313 if (vector.LengthSquared() > num)
314 {
315 return ContainmentType.Intersects;
316 }
317 vector.X = Center.X - box.Max.X;
318 vector.Y = Center.Y - box.Max.Y;
319 vector.Z = Center.Z - box.Max.Z;
320 if (vector.LengthSquared() > num)
321 {
322 return ContainmentType.Intersects;
323 }
324 vector.X = Center.X - box.Max.X;
325 vector.Y = Center.Y - box.Min.Y;
326 vector.Z = Center.Z - box.Max.Z;
327 if (vector.LengthSquared() > num)
328 {
329 return ContainmentType.Intersects;
330 }
331 vector.X = Center.X - box.Min.X;
332 vector.Y = Center.Y - box.Min.Y;
333 vector.Z = Center.Z - box.Max.Z;
334 if (vector.LengthSquared() > num)
335 {
336 return ContainmentType.Intersects;
337 }
338 vector.X = Center.X - box.Min.X;
339 vector.Y = Center.Y - box.Max.Y;
340 vector.Z = Center.Z - box.Min.Z;
341 if (vector.LengthSquared() > num)
342 {
343 return ContainmentType.Intersects;
344 }
345 vector.X = Center.X - box.Max.X;
346 vector.Y = Center.Y - box.Max.Y;
347 vector.Z = Center.Z - box.Min.Z;
348 if (vector.LengthSquared() > num)
349 {
350 return ContainmentType.Intersects;
351 }
352 vector.X = Center.X - box.Max.X;
353 vector.Y = Center.Y - box.Min.Y;
354 vector.Z = Center.Z - box.Min.Z;
355 if (vector.LengthSquared() > num)
356 {
357 return ContainmentType.Intersects;
358 }
359 vector.X = Center.X - box.Min.X;
360 vector.Y = Center.Y - box.Min.Y;
361 vector.Z = Center.Z - box.Min.Z;
362 if (vector.LengthSquared() > num)
363 {
364 return ContainmentType.Intersects;
365 }
367 }
368
370 {
371 box.Intersects(ref this, out var result2);
372 if (!result2)
373 {
374 result = ContainmentType.Disjoint;
375 return;
376 }
377 float num = Radius * Radius;
378 result = ContainmentType.Intersects;
379 Vector3 vector = default(Vector3);
380 vector.X = Center.X - box.Min.X;
381 vector.Y = Center.Y - box.Max.Y;
382 vector.Z = Center.Z - box.Max.Z;
383 if (vector.LengthSquared() > num)
384 {
385 return;
386 }
387 vector.X = Center.X - box.Max.X;
388 vector.Y = Center.Y - box.Max.Y;
389 vector.Z = Center.Z - box.Max.Z;
390 if (vector.LengthSquared() > num)
391 {
392 return;
393 }
394 vector.X = Center.X - box.Max.X;
395 vector.Y = Center.Y - box.Min.Y;
396 vector.Z = Center.Z - box.Max.Z;
397 if (vector.LengthSquared() > num)
398 {
399 return;
400 }
401 vector.X = Center.X - box.Min.X;
402 vector.Y = Center.Y - box.Min.Y;
403 vector.Z = Center.Z - box.Max.Z;
404 if (vector.LengthSquared() > num)
405 {
406 return;
407 }
408 vector.X = Center.X - box.Min.X;
409 vector.Y = Center.Y - box.Max.Y;
410 vector.Z = Center.Z - box.Min.Z;
411 if (vector.LengthSquared() > num)
412 {
413 return;
414 }
415 vector.X = Center.X - box.Max.X;
416 vector.Y = Center.Y - box.Max.Y;
417 vector.Z = Center.Z - box.Min.Z;
418 if (vector.LengthSquared() > num)
419 {
420 return;
421 }
422 vector.X = Center.X - box.Max.X;
423 vector.Y = Center.Y - box.Min.Y;
424 vector.Z = Center.Z - box.Min.Z;
425 if (!(vector.LengthSquared() > num))
426 {
427 vector.X = Center.X - box.Min.X;
428 vector.Y = Center.Y - box.Min.Y;
429 vector.Z = Center.Z - box.Min.Z;
430 if (!(vector.LengthSquared() > num))
431 {
432 result = ContainmentType.Contains;
433 }
434 }
435 }
436
438 {
439 if (null == frustum)
440 {
442 }
443 if (!frustum.Intersects(this))
444 {
445 return ContainmentType.Disjoint;
446 }
447 float num = Radius * Radius;
448 Vector3[] cornerArray = frustum.cornerArray;
449 Vector3 vector2 = default(Vector3);
450 for (int i = 0; i < cornerArray.Length; i++)
451 {
452 Vector3 vector = cornerArray[i];
453 vector2.X = vector.X - Center.X;
454 vector2.Y = vector.Y - Center.Y;
455 vector2.Z = vector.Z - Center.Z;
456 if (vector2.LengthSquared() > num)
457 {
458 return ContainmentType.Intersects;
459 }
460 }
461 return ContainmentType.Contains;
462 }
463
465 {
466 float num = Vector3.DistanceSquared(point, Center);
467 if (!(num < Radius * Radius))
468 {
469 return ContainmentType.Disjoint;
470 }
471 return ContainmentType.Contains;
472 }
473
474 public void Contains(ref Vector3 point, out ContainmentType result)
475 {
477 result = ((result2 < Radius * Radius) ? ContainmentType.Contains : ContainmentType.Disjoint);
478 }
479
481 {
482 Vector3.Distance(ref Center, ref sphere.Center, out var result);
483 float radius = Radius;
484 float radius2 = sphere.Radius;
485 if (!(radius + radius2 >= result))
486 {
487 return ContainmentType.Disjoint;
488 }
489 if (!(radius - radius2 >= result))
490 {
491 return ContainmentType.Intersects;
492 }
494 }
495
497 {
499 float radius = Radius;
500 float radius2 = sphere.Radius;
501 result = ((radius + radius2 >= result2) ? ((radius - radius2 >= result2) ? ContainmentType.Contains : ContainmentType.Intersects) : ContainmentType.Disjoint);
502 }
503
504 internal void SupportMapping(ref Vector3 v, out Vector3 result)
505 {
506 float num = v.Length();
507 float num2 = Radius / num;
508 result.X = Center.X + v.X * num2;
509 result.Y = Center.Y + v.Y * num2;
510 result.Z = Center.Z + v.Z * num2;
511 }
512
514 {
515 BoundingSphere result = default(BoundingSphere);
516 result.Center = Vector3.Transform(Center, matrix);
517 float val = matrix.M11 * matrix.M11 + matrix.M12 * matrix.M12 + matrix.M13 * matrix.M13;
518 float val2 = matrix.M21 * matrix.M21 + matrix.M22 * matrix.M22 + matrix.M23 * matrix.M23;
519 float val3 = matrix.M31 * matrix.M31 + matrix.M32 * matrix.M32 + matrix.M33 * matrix.M33;
520 float num = Math.Max(val, Math.Max(val2, val3));
521 result.Radius = Radius * (float)Math.Sqrt(num);
522 return result;
523 }
524
525 public void Transform(ref Matrix matrix, out BoundingSphere result)
526 {
527 result.Center = Vector3.Transform(Center, matrix);
528 float val = matrix.M11 * matrix.M11 + matrix.M12 * matrix.M12 + matrix.M13 * matrix.M13;
529 float val2 = matrix.M21 * matrix.M21 + matrix.M22 * matrix.M22 + matrix.M23 * matrix.M23;
530 float val3 = matrix.M31 * matrix.M31 + matrix.M32 * matrix.M32 + matrix.M33 * matrix.M33;
531 float num = Math.Max(val, Math.Max(val2, val3));
532 result.Radius = Radius * (float)Math.Sqrt(num);
533 }
534
536 {
537 return a.Equals(b);
538 }
539
541 {
542 if (!(a.Center != b.Center))
543 {
544 return a.Radius != b.Radius;
545 }
546 return true;
547 }
548}
static float Max(float value1, float value2)
Definition MathHelper.cs:41
static float Min(float value1, float value2)
Definition MathHelper.cs:36
bool ICollection< KeyValuePair< TKey, TValue > >. Contains(KeyValuePair< TKey, TValue > keyValuePair)
static CultureInfo CurrentCulture
static double Sqrt(double d)
static byte Max(byte val1, byte val2)
Definition Math.cs:738
ContainmentType Contains(BoundingFrustum frustum)
BoundingSphere Transform(Matrix matrix)
bool Intersects(BoundingFrustum frustum)
void Intersects(ref Ray ray, out float? result)
void Intersects(ref Plane plane, out PlaneIntersectionType result)
bool Equals(BoundingSphere other)
static void CreateMerged(ref BoundingSphere original, ref BoundingSphere additional, out BoundingSphere result)
ContainmentType Contains(BoundingSphere sphere)
ContainmentType Contains(Vector3 point)
void Contains(ref Vector3 point, out ContainmentType result)
static BoundingSphere CreateFromBoundingBox(BoundingBox box)
PlaneIntersectionType Intersects(Plane plane)
void Intersects(ref BoundingSphere sphere, out bool result)
static BoundingSphere CreateFromPoints(IEnumerable< Vector3 > points)
static BoundingSphere CreateMerged(BoundingSphere original, BoundingSphere additional)
void SupportMapping(ref Vector3 v, out Vector3 result)
void Contains(ref BoundingSphere sphere, out ContainmentType result)
void Transform(ref Matrix matrix, out BoundingSphere result)
static BoundingSphere CreateFromFrustum(BoundingFrustum frustum)
BoundingSphere(Vector3 center, float radius)
void Contains(ref BoundingBox box, out ContainmentType result)
void Intersects(ref BoundingBox box, out bool result)
ContainmentType Contains(BoundingBox box)
bool Intersects(BoundingSphere sphere)
static bool operator==(BoundingSphere a, BoundingSphere b)
static bool operator!=(BoundingSphere a, BoundingSphere b)
static void CreateFromBoundingBox(ref BoundingBox box, out BoundingSphere result)
static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
Definition Vector3.cs:273
static Vector3 Transform(Vector3 position, Matrix matrix)
Definition Vector3.cs:407
static Vector3 Subtract(Vector3 value1, Vector3 value2)
Definition Vector3.cs:745
override string ToString()
Definition Vector3.cs:85
static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
Definition Vector3.cs:307
static float Distance(Vector3 value1, Vector3 value2)
Definition Vector3.cs:131
static float DistanceSquared(Vector3 value1, Vector3 value2)
Definition Vector3.cs:149