// Copyright (c) 2015 Robert Rouhani and other contributors (see CONTRIBUTORS file). // Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE using SharpNav.Geometry; #if MONOGAME using Vector3 = Microsoft.Xna.Framework.Vector3; #elif OPENTK using Vector3 = OpenTK.Vector3; #elif SHARPDX using Vector3 = SharpDX.Vector3; #endif namespace SharpNav.Pathfinding { /// /// An enumeration of the different places a point can be relative to a rectangular boundary on the XZ plane. /// public enum BoundarySide : byte { /// /// Not outside of the defined boundary. /// Internal = 0xff, /// /// Only outside of the defined bondary on the X axis, in the positive direction. /// PlusX = 0, /// /// Outside of the defined boundary on both the X and Z axes, both in the positive direction. /// PlusXPlusZ = 1, /// /// Only outside of the defined bondary on the Z axis, in the positive direction. /// PlusZ = 2, /// /// Outside of the defined boundary on both the X and Z axes, in the negative and positive directions respectively. /// MinusXPlusZ = 3, /// /// Only outside of the defined bondary on the X axis, in the negative direction. /// MinusX = 4, /// /// Outside of the defined boundary on both the X and Z axes, both in the negative direction. /// MinusXMinusZ = 5, /// /// Only outside of the defined bondary on the Z axis, in the negative direction. /// MinusZ = 6, /// /// Outside of the defined boundary on both the X and Z axes, in the positive and negative directions respectively. /// PlusXMinusZ = 7 } /// /// Extension methods for the enumeration. /// public static class BoundarySideExtensions { /// /// Gets the side in the exact opposite direction as a specified side. /// /// /// The value will always return . /// /// A side. /// The opposite side. public static BoundarySide GetOpposite(this BoundarySide side) { if (side == BoundarySide.Internal) return BoundarySide.Internal; return (BoundarySide)((int)(side + 4) % 8); } /// /// Gets the boundary side of a point relative to a bounding box. /// /// A point. /// A bounding box. /// The point's position relative to the bounding box. public static BoundarySide FromPoint(Vector3 pt, BBox3 bounds) { const int PlusX = 0x1; const int PlusZ = 0x2; const int MinusX = 0x4; const int MinusZ = 0x8; int outcode = 0; outcode |= (pt.X >= bounds.Max.X) ? PlusX : 0; outcode |= (pt.Z >= bounds.Max.Z) ? PlusZ : 0; outcode |= (pt.X < bounds.Min.X) ? MinusX : 0; outcode |= (pt.Z < bounds.Min.Z) ? MinusZ : 0; switch (outcode) { case PlusX: return BoundarySide.PlusX; case PlusX | PlusZ: return BoundarySide.PlusXPlusZ; case PlusZ: return BoundarySide.PlusZ; case MinusX | PlusZ: return BoundarySide.MinusXPlusZ; case MinusX: return BoundarySide.MinusX; case MinusX | MinusZ: return BoundarySide.MinusXMinusZ; case MinusZ: return BoundarySide.MinusZ; case PlusX | MinusZ: return BoundarySide.PlusXMinusZ; default: return BoundarySide.Internal; } } } }