Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BoundingBox.cs
Go to the documentation of this file.
1using System;
6using Microsoft.Xna.Framework.Design;
7
9
12public struct BoundingBox : IEquatable<BoundingBox>
13{
14 public const int CornerCount = 8;
15
16 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
17 public Vector3 Min;
18
19 [SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
20 public Vector3 Max;
21
23 {
24 return new Vector3[8]
25 {
26 new Vector3(Min.X, Max.Y, Max.Z),
27 new Vector3(Max.X, Max.Y, Max.Z),
28 new Vector3(Max.X, Min.Y, Max.Z),
29 new Vector3(Min.X, Min.Y, Max.Z),
30 new Vector3(Min.X, Max.Y, Min.Z),
31 new Vector3(Max.X, Max.Y, Min.Z),
32 new Vector3(Max.X, Min.Y, Min.Z),
33 new Vector3(Min.X, Min.Y, Min.Z)
34 };
35 }
36
37 public void GetCorners(Vector3[] corners)
38 {
39 if (corners == null)
40 {
41 throw new ArgumentNullException("corners");
42 }
43 if (corners.Length < 8)
44 {
46 }
47 corners[0].X = Min.X;
48 corners[0].Y = Max.Y;
49 corners[0].Z = Max.Z;
50 corners[1].X = Max.X;
51 corners[1].Y = Max.Y;
52 corners[1].Z = Max.Z;
53 corners[2].X = Max.X;
54 corners[2].Y = Min.Y;
55 corners[2].Z = Max.Z;
56 corners[3].X = Min.X;
57 corners[3].Y = Min.Y;
58 corners[3].Z = Max.Z;
59 corners[4].X = Min.X;
60 corners[4].Y = Max.Y;
61 corners[4].Z = Min.Z;
62 corners[5].X = Max.X;
63 corners[5].Y = Max.Y;
64 corners[5].Z = Min.Z;
65 corners[6].X = Max.X;
66 corners[6].Y = Min.Y;
67 corners[6].Z = Min.Z;
68 corners[7].X = Min.X;
69 corners[7].Y = Min.Y;
70 corners[7].Z = Min.Z;
71 }
72
74 {
75 Min = min;
76 Max = max;
77 }
78
80 {
81 if (Min == other.Min)
82 {
83 return Max == other.Max;
84 }
85 return false;
86 }
87
88 public override bool Equals(object obj)
89 {
90 bool result = false;
91 if (obj is BoundingBox)
92 {
93 result = Equals((BoundingBox)obj);
94 }
95 return result;
96 }
97
98 public override int GetHashCode()
99 {
100 return Min.GetHashCode() + Max.GetHashCode();
101 }
102
103 public override string ToString()
104 {
106 return string.Format(currentCulture, "{{Min:{0} Max:{1}}}", new object[2]
107 {
108 Min.ToString(),
109 Max.ToString()
110 });
111 }
112
114 {
115 BoundingBox result = default(BoundingBox);
116 Vector3.Min(ref original.Min, ref additional.Min, out result.Min);
117 Vector3.Max(ref original.Max, ref additional.Max, out result.Max);
118 return result;
119 }
120
122 {
125 result.Min = result2;
126 result.Max = result3;
127 }
128
130 {
131 BoundingBox result = default(BoundingBox);
132 result.Min.X = sphere.Center.X - sphere.Radius;
133 result.Min.Y = sphere.Center.Y - sphere.Radius;
134 result.Min.Z = sphere.Center.Z - sphere.Radius;
135 result.Max.X = sphere.Center.X + sphere.Radius;
136 result.Max.Y = sphere.Center.Y + sphere.Radius;
137 result.Max.Z = sphere.Center.Z + sphere.Radius;
138 return result;
139 }
140
142 {
143 result.Min.X = sphere.Center.X - sphere.Radius;
144 result.Min.Y = sphere.Center.Y - sphere.Radius;
145 result.Min.Z = sphere.Center.Z - sphere.Radius;
146 result.Max.X = sphere.Center.X + sphere.Radius;
147 result.Max.Y = sphere.Center.Y + sphere.Radius;
148 result.Max.Z = sphere.Center.Z + sphere.Radius;
149 }
150
152 {
153 if (points == null)
154 {
155 throw new ArgumentNullException();
156 }
157 bool flag = false;
158 Vector3 value = new Vector3(float.MaxValue);
159 Vector3 value2 = new Vector3(float.MinValue);
160 foreach (Vector3 point in points)
161 {
162 Vector3 value3 = point;
165 flag = true;
166 }
167 if (!flag)
168 {
170 }
171 return new BoundingBox(value, value2);
172 }
173
175 {
176 if (Max.X < box.Min.X || Min.X > box.Max.X)
177 {
178 return false;
179 }
180 if (Max.Y < box.Min.Y || Min.Y > box.Max.Y)
181 {
182 return false;
183 }
184 if (Max.Z < box.Min.Z || Min.Z > box.Max.Z)
185 {
186 return false;
187 }
188 return true;
189 }
190
191 public void Intersects(ref BoundingBox box, out bool result)
192 {
193 result = false;
194 if (!(Max.X < box.Min.X) && !(Min.X > box.Max.X) && !(Max.Y < box.Min.Y) && !(Min.Y > box.Max.Y) && !(Max.Z < box.Min.Z) && !(Min.Z > box.Max.Z))
195 {
196 result = true;
197 }
198 }
199
201 {
202 if (null == frustum)
203 {
205 }
206 return frustum.Intersects(this);
207 }
208
210 {
211 Vector3 vector = default(Vector3);
212 vector.X = ((plane.Normal.X >= 0f) ? Min.X : Max.X);
213 vector.Y = ((plane.Normal.Y >= 0f) ? Min.Y : Max.Y);
214 vector.Z = ((plane.Normal.Z >= 0f) ? Min.Z : Max.Z);
215 Vector3 vector2 = default(Vector3);
216 vector2.X = ((plane.Normal.X >= 0f) ? Max.X : Min.X);
217 vector2.Y = ((plane.Normal.Y >= 0f) ? Max.Y : Min.Y);
218 vector2.Z = ((plane.Normal.Z >= 0f) ? Max.Z : Min.Z);
219 float num = plane.Normal.X * vector.X + plane.Normal.Y * vector.Y + plane.Normal.Z * vector.Z;
220 if (num + plane.D > 0f)
221 {
222 return PlaneIntersectionType.Front;
223 }
224 num = plane.Normal.X * vector2.X + plane.Normal.Y * vector2.Y + plane.Normal.Z * vector2.Z;
225 if (num + plane.D < 0f)
226 {
227 return PlaneIntersectionType.Back;
228 }
229 return PlaneIntersectionType.Intersecting;
230 }
231
233 {
234 Vector3 vector = default(Vector3);
235 vector.X = ((plane.Normal.X >= 0f) ? Min.X : Max.X);
236 vector.Y = ((plane.Normal.Y >= 0f) ? Min.Y : Max.Y);
237 vector.Z = ((plane.Normal.Z >= 0f) ? Min.Z : Max.Z);
238 Vector3 vector2 = default(Vector3);
239 vector2.X = ((plane.Normal.X >= 0f) ? Max.X : Min.X);
240 vector2.Y = ((plane.Normal.Y >= 0f) ? Max.Y : Min.Y);
241 vector2.Z = ((plane.Normal.Z >= 0f) ? Max.Z : Min.Z);
242 float num = plane.Normal.X * vector.X + plane.Normal.Y * vector.Y + plane.Normal.Z * vector.Z;
243 if (num + plane.D > 0f)
244 {
245 result = PlaneIntersectionType.Front;
246 return;
247 }
248 num = plane.Normal.X * vector2.X + plane.Normal.Y * vector2.Y + plane.Normal.Z * vector2.Z;
249 if (num + plane.D < 0f)
250 {
251 result = PlaneIntersectionType.Back;
252 }
253 else
254 {
255 result = PlaneIntersectionType.Intersecting;
256 }
257 }
258
259 public float? Intersects(Ray ray)
260 {
261 float num = 0f;
262 float num2 = float.MaxValue;
263 if (Math.Abs(ray.Direction.X) < 1E-06f)
264 {
265 if (ray.Position.X < Min.X || ray.Position.X > Max.X)
266 {
267 return null;
268 }
269 }
270 else
271 {
272 float num3 = 1f / ray.Direction.X;
273 float num4 = (Min.X - ray.Position.X) * num3;
274 float num5 = (Max.X - ray.Position.X) * num3;
275 if (num4 > num5)
276 {
277 float num6 = num4;
278 num4 = num5;
279 num5 = num6;
280 }
281 num = MathHelper.Max(num4, num);
283 if (num > num2)
284 {
285 return null;
286 }
287 }
288 if (Math.Abs(ray.Direction.Y) < 1E-06f)
289 {
290 if (ray.Position.Y < Min.Y || ray.Position.Y > Max.Y)
291 {
292 return null;
293 }
294 }
295 else
296 {
297 float num7 = 1f / ray.Direction.Y;
298 float num8 = (Min.Y - ray.Position.Y) * num7;
299 float num9 = (Max.Y - ray.Position.Y) * num7;
300 if (num8 > num9)
301 {
302 float num10 = num8;
303 num8 = num9;
304 num9 = num10;
305 }
306 num = MathHelper.Max(num8, num);
308 if (num > num2)
309 {
310 return null;
311 }
312 }
313 if (Math.Abs(ray.Direction.Z) < 1E-06f)
314 {
315 if (ray.Position.Z < Min.Z || ray.Position.Z > Max.Z)
316 {
317 return null;
318 }
319 }
320 else
321 {
322 float num11 = 1f / ray.Direction.Z;
323 float num12 = (Min.Z - ray.Position.Z) * num11;
324 float num13 = (Max.Z - ray.Position.Z) * num11;
325 if (num12 > num13)
326 {
327 float num14 = num12;
328 num12 = num13;
329 num13 = num14;
330 }
331 num = MathHelper.Max(num12, num);
333 if (num > num2)
334 {
335 return null;
336 }
337 }
338 return num;
339 }
340
341 public void Intersects(ref Ray ray, out float? result)
342 {
343 result = null;
344 float num = 0f;
345 float num2 = float.MaxValue;
346 if (Math.Abs(ray.Direction.X) < 1E-06f)
347 {
348 if (ray.Position.X < Min.X || ray.Position.X > Max.X)
349 {
350 return;
351 }
352 }
353 else
354 {
355 float num3 = 1f / ray.Direction.X;
356 float num4 = (Min.X - ray.Position.X) * num3;
357 float num5 = (Max.X - ray.Position.X) * num3;
358 if (num4 > num5)
359 {
360 float num6 = num4;
361 num4 = num5;
362 num5 = num6;
363 }
364 num = MathHelper.Max(num4, num);
366 if (num > num2)
367 {
368 return;
369 }
370 }
371 if (Math.Abs(ray.Direction.Y) < 1E-06f)
372 {
373 if (ray.Position.Y < Min.Y || ray.Position.Y > Max.Y)
374 {
375 return;
376 }
377 }
378 else
379 {
380 float num7 = 1f / ray.Direction.Y;
381 float num8 = (Min.Y - ray.Position.Y) * num7;
382 float num9 = (Max.Y - ray.Position.Y) * num7;
383 if (num8 > num9)
384 {
385 float num10 = num8;
386 num8 = num9;
387 num9 = num10;
388 }
389 num = MathHelper.Max(num8, num);
391 if (num > num2)
392 {
393 return;
394 }
395 }
396 if (Math.Abs(ray.Direction.Z) < 1E-06f)
397 {
398 if (ray.Position.Z < Min.Z || ray.Position.Z > Max.Z)
399 {
400 return;
401 }
402 }
403 else
404 {
405 float num11 = 1f / ray.Direction.Z;
406 float num12 = (Min.Z - ray.Position.Z) * num11;
407 float num13 = (Max.Z - ray.Position.Z) * num11;
408 if (num12 > num13)
409 {
410 float num14 = num12;
411 num12 = num13;
412 num13 = num14;
413 }
414 num = MathHelper.Max(num12, num);
416 if (num > num2)
417 {
418 return;
419 }
420 }
421 result = num;
422 }
423
425 {
426 Vector3.Clamp(ref sphere.Center, ref Min, ref Max, out var result);
428 if (!(result2 > sphere.Radius * sphere.Radius))
429 {
430 return true;
431 }
432 return false;
433 }
434
435 public void Intersects(ref BoundingSphere sphere, out bool result)
436 {
439 result = !(result3 > sphere.Radius * sphere.Radius);
440 }
441
443 {
444 if (Max.X < box.Min.X || Min.X > box.Max.X)
445 {
446 return ContainmentType.Disjoint;
447 }
448 if (Max.Y < box.Min.Y || Min.Y > box.Max.Y)
449 {
450 return ContainmentType.Disjoint;
451 }
452 if (Max.Z < box.Min.Z || Min.Z > box.Max.Z)
453 {
454 return ContainmentType.Disjoint;
455 }
456 if (!(Min.X <= box.Min.X) || !(box.Max.X <= Max.X) || !(Min.Y <= box.Min.Y) || !(box.Max.Y <= Max.Y) || !(Min.Z <= box.Min.Z) || !(box.Max.Z <= Max.Z))
457 {
458 return ContainmentType.Intersects;
459 }
460 return ContainmentType.Contains;
461 }
462
464 {
465 result = ContainmentType.Disjoint;
466 if (!(Max.X < box.Min.X) && !(Min.X > box.Max.X) && !(Max.Y < box.Min.Y) && !(Min.Y > box.Max.Y) && !(Max.Z < box.Min.Z) && !(Min.Z > box.Max.Z))
467 {
468 result = ((Min.X <= box.Min.X && box.Max.X <= Max.X && Min.Y <= box.Min.Y && box.Max.Y <= Max.Y && Min.Z <= box.Min.Z && box.Max.Z <= Max.Z) ? ContainmentType.Contains : ContainmentType.Intersects);
469 }
470 }
471
473 {
474 if (null == frustum)
475 {
477 }
478 if (!frustum.Intersects(this))
479 {
480 return ContainmentType.Disjoint;
481 }
482 Vector3[] cornerArray = frustum.cornerArray;
483 foreach (Vector3 point in cornerArray)
484 {
485 if (Contains(point) == ContainmentType.Disjoint)
486 {
487 return ContainmentType.Intersects;
488 }
489 }
491 }
492
494 {
495 if (!(Min.X <= point.X) || !(point.X <= Max.X) || !(Min.Y <= point.Y) || !(point.Y <= Max.Y) || !(Min.Z <= point.Z) || !(point.Z <= Max.Z))
496 {
497 return ContainmentType.Disjoint;
498 }
499 return ContainmentType.Contains;
500 }
501
502 public void Contains(ref Vector3 point, out ContainmentType result)
503 {
504 result = ((Min.X <= point.X && point.X <= Max.X && Min.Y <= point.Y && point.Y <= Max.Y && Min.Z <= point.Z && point.Z <= Max.Z) ? ContainmentType.Contains : ContainmentType.Disjoint);
505 }
506
508 {
509 Vector3.Clamp(ref sphere.Center, ref Min, ref Max, out var result);
511 float radius = sphere.Radius;
512 if (result2 > radius * radius)
513 {
514 return ContainmentType.Disjoint;
515 }
516 if (!(Min.X + radius <= sphere.Center.X) || !(sphere.Center.X <= Max.X - radius) || !(Max.X - Min.X > radius) || !(Min.Y + radius <= sphere.Center.Y) || !(sphere.Center.Y <= Max.Y - radius) || !(Max.Y - Min.Y > radius) || !(Min.Z + radius <= sphere.Center.Z) || !(sphere.Center.Z <= Max.Z - radius) || !(Max.X - Min.X > radius))
517 {
518 return ContainmentType.Intersects;
519 }
521 }
522
524 {
527 float radius = sphere.Radius;
528 if (result3 > radius * radius)
529 {
530 result = ContainmentType.Disjoint;
531 }
532 else
533 {
534 result = ((Min.X + radius <= sphere.Center.X && sphere.Center.X <= Max.X - radius && Max.X - Min.X > radius && Min.Y + radius <= sphere.Center.Y && sphere.Center.Y <= Max.Y - radius && Max.Y - Min.Y > radius && Min.Z + radius <= sphere.Center.Z && sphere.Center.Z <= Max.Z - radius && Max.X - Min.X > radius) ? ContainmentType.Contains : ContainmentType.Intersects);
535 }
536 }
537
538 internal void SupportMapping(ref Vector3 v, out Vector3 result)
539 {
540 result.X = ((v.X >= 0f) ? Max.X : Min.X);
541 result.Y = ((v.Y >= 0f) ? Max.Y : Min.Y);
542 result.Z = ((v.Z >= 0f) ? Max.Z : Min.Z);
543 }
544
545 public static bool operator ==(BoundingBox a, BoundingBox b)
546 {
547 return a.Equals(b);
548 }
549
550 public static bool operator !=(BoundingBox a, BoundingBox b)
551 {
552 if (!(a.Min != b.Min))
553 {
554 return a.Max != b.Max;
555 }
556 return true;
557 }
558}
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 Abs(double value)
bool Equals(BoundingBox other)
static void CreateFromSphere(ref BoundingSphere sphere, out BoundingBox result)
ContainmentType Contains(Vector3 point)
void SupportMapping(ref Vector3 v, out Vector3 result)
bool Intersects(BoundingSphere sphere)
void Contains(ref BoundingBox box, out ContainmentType result)
void Intersects(ref BoundingBox box, out bool result)
void GetCorners(Vector3[] corners)
void Contains(ref Vector3 point, out ContainmentType result)
override bool Equals(object obj)
void Contains(ref BoundingSphere sphere, out ContainmentType result)
void Intersects(ref BoundingSphere sphere, out bool result)
bool Intersects(BoundingBox box)
ContainmentType Contains(BoundingBox box)
bool Intersects(BoundingFrustum frustum)
static BoundingBox CreateFromPoints(IEnumerable< Vector3 > points)
static bool operator==(BoundingBox a, BoundingBox b)
BoundingBox(Vector3 min, Vector3 max)
ContainmentType Contains(BoundingSphere sphere)
static BoundingBox CreateFromSphere(BoundingSphere sphere)
static bool operator!=(BoundingBox a, BoundingBox b)
PlaneIntersectionType Intersects(Plane plane)
void Intersects(ref Plane plane, out PlaneIntersectionType result)
void Intersects(ref Ray ray, out float? result)
static void CreateMerged(ref BoundingBox original, ref BoundingBox additional, out BoundingBox result)
ContainmentType Contains(BoundingFrustum frustum)
static BoundingBox CreateMerged(BoundingBox original, BoundingBox additional)
static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
Definition Vector3.cs:273
static Vector3 Min(Vector3 value1, Vector3 value2)
Definition Vector3.cs:241
override string ToString()
Definition Vector3.cs:85
static Vector3 Max(Vector3 value1, Vector3 value2)
Definition Vector3.cs:257
static float DistanceSquared(Vector3 value1, Vector3 value2)
Definition Vector3.cs:149