#if REAL_T_IS_DOUBLE using real_t = System.Double; #else using real_t = System.Single; #endif using System; using System.Runtime.InteropServices; namespace Godot { /// /// 3-element structure that can be used to represent 3D grid coordinates or sets of integers. /// [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Vector3i : IEquatable { /// /// Enumerated index values for the axes. /// Returned by and . /// public enum Axis { X = 0, Y, Z } /// /// The vector's X component. Also accessible by using the index position `[0]`. /// public int x; /// /// The vector's Y component. Also accessible by using the index position `[1]`. /// public int y; /// /// The vector's Z component. Also accessible by using the index position `[2]`. /// public int z; /// /// Access vector components using their index. /// /// `[0]` is equivalent to `.x`, `[1]` is equivalent to `.y`, `[2]` is equivalent to `.z`. public int this[int index] { get { switch (index) { case 0: return x; case 1: return y; case 2: return z; default: throw new IndexOutOfRangeException(); } } set { switch (index) { case 0: x = value; return; case 1: y = value; return; case 2: z = value; return; default: throw new IndexOutOfRangeException(); } } } /// /// Returns a new vector with all components in absolute values (i.e. positive). /// /// A vector with called on each component. public Vector3i Abs() { return new Vector3i(Mathf.Abs(x), Mathf.Abs(y), Mathf.Abs(z)); } /// /// Returns a new vector with all components clamped between the /// components of `min` and `max` using /// . /// /// The vector with minimum allowed values. /// The vector with maximum allowed values. /// The vector with all components clamped. public Vector3i Clamp(Vector3i min, Vector3i max) { return new Vector3i ( Mathf.Clamp(x, min.x, max.x), Mathf.Clamp(y, min.y, max.y), Mathf.Clamp(z, min.z, max.z) ); } /// /// Returns the squared distance between this vector and `b`. /// This method runs faster than , so prefer it if /// you need to compare vectors or need the squared distance for some formula. /// /// The other vector to use. /// The squared distance between the two vectors. public int DistanceSquaredTo(Vector3i b) { return (b - this).LengthSquared(); } /// /// Returns the distance between this vector and `b`. /// /// The other vector to use. /// The distance between the two vectors. public real_t DistanceTo(Vector3i b) { return (b - this).Length(); } /// /// Returns the dot product of this vector and `b`. /// /// The other vector to use. /// The dot product of the two vectors. public int Dot(Vector3i b) { return x * b.x + y * b.y + z * b.z; } /// /// Returns the length (magnitude) of this vector. /// /// The length of this vector. public real_t Length() { int x2 = x * x; int y2 = y * y; int z2 = z * z; return Mathf.Sqrt(x2 + y2 + z2); } /// /// Returns the squared length (squared magnitude) of this vector. /// This method runs faster than , so prefer it if /// you need to compare vectors or need the squared length for some formula. /// /// The squared length of this vector. public int LengthSquared() { int x2 = x * x; int y2 = y * y; int z2 = z * z; return x2 + y2 + z2; } /// /// Returns the axis of the vector's largest value. See . /// If all components are equal, this method returns . /// /// The index of the largest axis. public Axis MaxAxis() { return x < y ? (y < z ? Axis.Z : Axis.Y) : (x < z ? Axis.Z : Axis.X); } /// /// Returns the axis of the vector's smallest value. See . /// If all components are equal, this method returns . /// /// The index of the smallest axis. public Axis MinAxis() { return x < y ? (x < z ? Axis.X : Axis.Z) : (y < z ? Axis.Y : Axis.Z); } /// /// Returns a vector composed of the of this vector's components and `mod`. /// /// A value representing the divisor of the operation. /// A vector with each component by `mod`. public Vector3i PosMod(int mod) { Vector3i v = this; v.x = Mathf.PosMod(v.x, mod); v.y = Mathf.PosMod(v.y, mod); v.z = Mathf.PosMod(v.z, mod); return v; } /// /// Returns a vector composed of the of this vector's components and `modv`'s components. /// /// A vector representing the divisors of the operation. /// A vector with each component by `modv`'s components. public Vector3i PosMod(Vector3i modv) { Vector3i v = this; v.x = Mathf.PosMod(v.x, modv.x); v.y = Mathf.PosMod(v.y, modv.y); v.z = Mathf.PosMod(v.z, modv.z); return v; } /// /// Returns a vector with each component set to one or negative one, depending /// on the signs of this vector's components, or zero if the component is zero, /// by calling on each component. /// /// A vector with all components as either `1`, `-1`, or `0`. public Vector3i Sign() { Vector3i v = this; v.x = Mathf.Sign(v.x); v.y = Mathf.Sign(v.y); v.z = Mathf.Sign(v.z); return v; } // Constants private static readonly Vector3i _zero = new Vector3i(0, 0, 0); private static readonly Vector3i _one = new Vector3i(1, 1, 1); private static readonly Vector3i _up = new Vector3i(0, 1, 0); private static readonly Vector3i _down = new Vector3i(0, -1, 0); private static readonly Vector3i _right = new Vector3i(1, 0, 0); private static readonly Vector3i _left = new Vector3i(-1, 0, 0); private static readonly Vector3i _forward = new Vector3i(0, 0, -1); private static readonly Vector3i _back = new Vector3i(0, 0, 1); /// /// Zero vector, a vector with all components set to `0`. /// /// Equivalent to `new Vector3i(0, 0, 0)` public static Vector3i Zero { get { return _zero; } } /// /// One vector, a vector with all components set to `1`. /// /// Equivalent to `new Vector3i(1, 1, 1)` public static Vector3i One { get { return _one; } } /// /// Up unit vector. /// /// Equivalent to `new Vector3i(0, 1, 0)` public static Vector3i Up { get { return _up; } } /// /// Down unit vector. /// /// Equivalent to `new Vector3i(0, -1, 0)` public static Vector3i Down { get { return _down; } } /// /// Right unit vector. Represents the local direction of right, /// and the global direction of east. /// /// Equivalent to `new Vector3i(1, 0, 0)` public static Vector3i Right { get { return _right; } } /// /// Left unit vector. Represents the local direction of left, /// and the global direction of west. /// /// Equivalent to `new Vector3i(-1, 0, 0)` public static Vector3i Left { get { return _left; } } /// /// Forward unit vector. Represents the local direction of forward, /// and the global direction of north. /// /// Equivalent to `new Vector3i(0, 0, -1)` public static Vector3i Forward { get { return _forward; } } /// /// Back unit vector. Represents the local direction of back, /// and the global direction of south. /// /// Equivalent to `new Vector3i(0, 0, 1)` public static Vector3i Back { get { return _back; } } /// /// Constructs a new with the given components. /// /// The vector's X component. /// The vector's Y component. /// The vector's Z component. public Vector3i(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } /// /// Constructs a new from an existing . /// /// The existing . public Vector3i(Vector3i vi) { this.x = vi.x; this.y = vi.y; this.z = vi.z; } /// /// Constructs a new from an existing /// by rounding the components via . /// /// The to convert. public Vector3i(Vector3 v) { this.x = Mathf.RoundToInt(v.x); this.y = Mathf.RoundToInt(v.y); this.z = Mathf.RoundToInt(v.z); } public static Vector3i operator +(Vector3i left, Vector3i right) { left.x += right.x; left.y += right.y; left.z += right.z; return left; } public static Vector3i operator -(Vector3i left, Vector3i right) { left.x -= right.x; left.y -= right.y; left.z -= right.z; return left; } public static Vector3i operator -(Vector3i vec) { vec.x = -vec.x; vec.y = -vec.y; vec.z = -vec.z; return vec; } public static Vector3i operator *(Vector3i vec, int scale) { vec.x *= scale; vec.y *= scale; vec.z *= scale; return vec; } public static Vector3i operator *(int scale, Vector3i vec) { vec.x *= scale; vec.y *= scale; vec.z *= scale; return vec; } public static Vector3i operator *(Vector3i left, Vector3i right) { left.x *= right.x; left.y *= right.y; left.z *= right.z; return left; } public static Vector3i operator /(Vector3i vec, int divisor) { vec.x /= divisor; vec.y /= divisor; vec.z /= divisor; return vec; } public static Vector3i operator /(Vector3i vec, Vector3i divisorv) { vec.x /= divisorv.x; vec.y /= divisorv.y; vec.z /= divisorv.z; return vec; } public static Vector3i operator %(Vector3i vec, int divisor) { vec.x %= divisor; vec.y %= divisor; vec.z %= divisor; return vec; } public static Vector3i operator %(Vector3i vec, Vector3i divisorv) { vec.x %= divisorv.x; vec.y %= divisorv.y; vec.z %= divisorv.z; return vec; } public static Vector3i operator &(Vector3i vec, int and) { vec.x &= and; vec.y &= and; vec.z &= and; return vec; } public static Vector3i operator &(Vector3i vec, Vector3i andv) { vec.x &= andv.x; vec.y &= andv.y; vec.z &= andv.z; return vec; } public static bool operator ==(Vector3i left, Vector3i right) { return left.Equals(right); } public static bool operator !=(Vector3i left, Vector3i right) { return !left.Equals(right); } public static bool operator <(Vector3i left, Vector3i right) { if (left.x == right.x) { if (left.y == right.y) return left.z < right.z; else return left.y < right.y; } return left.x < right.x; } public static bool operator >(Vector3i left, Vector3i right) { if (left.x == right.x) { if (left.y == right.y) return left.z > right.z; else return left.y > right.y; } return left.x > right.x; } public static bool operator <=(Vector3i left, Vector3i right) { if (left.x == right.x) { if (left.y == right.y) return left.z <= right.z; else return left.y < right.y; } return left.x < right.x; } public static bool operator >=(Vector3i left, Vector3i right) { if (left.x == right.x) { if (left.y == right.y) return left.z >= right.z; else return left.y > right.y; } return left.x > right.x; } public static implicit operator Vector3(Vector3i value) { return new Vector3(value.x, value.y, value.z); } public static explicit operator Vector3i(Vector3 value) { return new Vector3i(value); } public override bool Equals(object obj) { if (obj is Vector3i) { return Equals((Vector3i)obj); } return false; } public bool Equals(Vector3i other) { return x == other.x && y == other.y && z == other.z; } public override int GetHashCode() { return y.GetHashCode() ^ x.GetHashCode() ^ z.GetHashCode(); } public override string ToString() { return String.Format("({0}, {1}, {2})", new object[] { this.x.ToString(), this.y.ToString(), this.z.ToString() }); } public string ToString(string format) { return String.Format("({0}, {1}, {2})", new object[] { this.x.ToString(format), this.y.ToString(format), this.z.ToString(format) }); } } }