summaryrefslogtreecommitdiff
path: root/Other/AstarPathfindingDemo/Packages/com.arongranberg.astar/Core/Geometry/IntBounds.cs
blob: 008b16ff1b57414ed5f99f6ccd99e683ed29e0a8 (plain)
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
using Unity.Mathematics;

namespace Pathfinding {
	/// <summary>
	/// Integer bounding box.
	/// Works almost like UnityEngine.BoundsInt but with a slightly nicer and more efficient api.
	///
	/// Uses an exclusive upper bound (max field).
	/// </summary>
	public struct IntBounds {
		public int3 min, max;

		public IntBounds (int xmin, int ymin, int zmin, int xmax, int ymax, int zmax) {
			min = new int3(xmin, ymin, zmin);
			max = new int3(xmax, ymax, zmax);
		}

		public IntBounds(int3 min, int3 max) {
			this.min = min;
			this.max = max;
		}

		public int3 size => max - min;
		public int volume {
			get {
				var s = size;
				return s.x * s.y * s.z;
			}
		}

		/// <summary>
		/// Returns the intersection bounding box between the two bounds.
		/// The intersection bounds is the volume which is inside both bounds.
		/// If the rects do not have an intersection, an invalid rect is returned.
		/// See: IsValid
		/// </summary>
		public static IntBounds Intersection (IntBounds a, IntBounds b) {
			return new IntBounds(
				math.max(a.min, b.min),
				math.min(a.max, b.max)
				);
		}

		public IntBounds Offset (int3 offset) {
			return new IntBounds(min + offset, max + offset);
		}

		public bool Contains (IntBounds other) {
			return math.all(other.min >= min & other.max <= max);
		}

		public override string ToString() => "(" + min.ToString() + " <= x < " + max.ToString() + ")";
		public override bool Equals (object _b) {
			var b = (IntBounds)_b;
			return this == b;
		}

		public override int GetHashCode() => min.GetHashCode() ^ (max.GetHashCode() << 2);

		public static bool operator ==(IntBounds a, IntBounds b) => math.all(a.min == b.min & a.max == b.max);

		public static bool operator !=(IntBounds a, IntBounds b) => !(a == b);
	}
}