using System;
namespace Engine.Core;
/// 
/// Represents a three-dimensional vector.
/// 
/// X position of the .
/// Y position of the .
/// Z position of the .
/// 
/// Initializes a new instance of the  struct with the specified positions.
/// 
[System.Diagnostics.DebuggerDisplay("{ToString(),nq}, Length: {Magnitude}, LengthSquared: {MagnitudeSquared}, Normalized: {Normalized.ToString(),nq}")]
public readonly struct Vector3D(float x, float y, float z) : IEquatable
{
    /// 
    /// The X coordinate of the .
    /// 
    public readonly float X = x;
    /// 
    /// The Y coordinate of the .
    /// 
    public readonly float Y = y;
    /// 
    /// The Z coordinate of the .
    /// 
    public readonly float Z = z;
    /// 
    /// The magnitude (length) of the .
    /// 
    public float Magnitude => Length(this);
    /// 
    /// The squared magnitude (length) of the .
    /// 
    public float MagnitudeSquared => LengthSquared(this);
    /// 
    /// The normalized form of the  (a  with the same direction and a magnitude of 1).
    /// 
    public Vector3D Normalized => Normalize(this);
    /// 
    /// Represents the unit  pointing upwards.
    /// 
    public readonly static Vector3D Up = new(0f, 1f, 0f);
    /// 
    /// Represents the unit  pointing downwards.
    /// 
    public readonly static Vector3D Down = new(0f, -1f, 0f);
    /// 
    /// Represents the unit  pointing leftwards.
    /// 
    public readonly static Vector3D Left = new(-1f, 0f, 0f);
    /// 
    /// Represents the unit  pointing rightwards.
    /// 
    public readonly static Vector3D Right = new(1f, 0f, 0f);
    /// 
    /// Represents the unit  pointing forwards.
    /// 
    public readonly static Vector3D Forward = new(0f, 0f, 1f);
    /// 
    /// Represents the unit  pointing backwards.
    public readonly static Vector3D Backward = new(0f, 0f, -1f);
    /// 
    /// Represents the zero .
    /// 
    public readonly static Vector3D Zero = new(0f, 0f, 0f);
    /// 
    /// Represents the  with both components equal to 1.
    /// 
    public readonly static Vector3D One = new(1f, 1f, 1f);
    public static Vector3D operator -(Vector3D vector) => new(0f - vector.X, 0f - vector.Y, 0f - vector.Z);
    public static Vector3D operator +(Vector3D left, Vector3D right) => new(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
    public static Vector3D operator -(Vector3D left, Vector3D right) => new(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
    public static Vector3D operator *(Vector3D vector, float value) => new(vector.X * value, vector.Y * value, vector.Z * value);
    public static Vector3D operator *(float value, Vector3D vector) => new(vector.X * value, vector.Y * value, vector.Z * value);
    public static Vector3D operator /(Vector3D vector, float value) => new(vector.X / value, vector.Y / value, vector.Z / value);
    public static bool operator ==(Vector3D left, Vector3D right) => left.X == right.X && left.Y == right.Y && left.Z == right.Z;
    public static bool operator !=(Vector3D left, Vector3D right) => left.X != right.X || left.Y != right.Y || left.Z != right.Z;
    public static implicit operator System.Numerics.Vector3(Vector3D vector) => new(vector.X, vector.Y, vector.Z);
    public static implicit operator Vector3D(System.Numerics.Vector3 vector) => new(vector.X, vector.Y, vector.Z);
    public static implicit operator Vector3D(Vector3DInt vector) => new(vector.X, vector.Y, vector.Z);
    public static implicit operator Vector3D(Vector2D vector) => new(vector.X, vector.Y, 0f);
    public static implicit operator Vector3D(System.Numerics.Vector2 vector) => new(vector.X, vector.Y, 0f);
    /// 
    /// Calculates the length of the .
    /// 
    /// The .
    /// The length of the .
    public static float Length(Vector3D vector) => Math.Sqrt(LengthSquared(vector));
    /// 
    /// Calculates the squared length of the .
    /// 
    /// The .
    /// The squared length of the .
    public static float LengthSquared(Vector3D vector) => vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z;
    /// 
    /// Calculates the distance between two s.
    /// 
    /// The start .
    /// The end .
    /// The distance between the two s.
    public static float Distance(Vector3D from, Vector3D to) => Length(FromTo(from, to));
    /// 
    /// Inverts the direction of the .
    /// 
    /// The .
    /// The inverted .
    public static Vector3D Invert(Vector3D vector) => -vector;
    /// 
    /// Adds two s.
    /// 
    /// The first .
    /// The second .
    /// The sum of the two s.
    public static Vector3D Add(Vector3D left, Vector3D right) => left + right;
    /// 
    /// Subtracts one  from another.
    /// 
    /// The  to subtract from.
    /// The  to subtract.
    /// The result of subtracting the second  from the first.
    public static Vector3D Subtract(Vector3D left, Vector3D right) => left - right;
    /// 
    /// Multiplies a  by a scalar value.
    /// 
    /// The .
    /// The scalar value.
    /// The result of multiplying the  by the scalar value.
    public static Vector3D Multiply(Vector3D vector, float value) => vector * value;
    /// 
    /// Divides a  by a scalar value.
    /// 
    /// The .
    /// The scalar value.
    /// The result of dividing the  by the scalar value.
    public static Vector3D Divide(Vector3D vector, float value) => vector / value;
    /// 
    /// Calculates the absolute value of each component of the vector.
    /// 
    /// The .
    /// The  with each component's absolute value.
    public static Vector3D Abs(Vector3D vector) => new(Math.Abs(vector.X), Math.Abs(vector.Y), Math.Abs(vector.Z));
    /// 
    /// Normalizes the  (creates a unit  with the same direction).
    /// 
    /// The  to normalize.
    /// The normalized .
    public static Vector3D Normalize(Vector3D vector) => vector / Length(vector);
    /// 
    /// Reflects a  off a surface with the specified normal.
    /// 
    /// The incident .
    /// The normal  of the surface.
    /// The reflected .
    public static Vector3D Reflect(Vector3D vector, Vector3D normal) => vector - 2f * Dot(vector, normal) * normal;
    /// 
    /// Calculates the  from one point to another.
    /// 
    /// The starting point.
    /// The ending point.
    /// The  from the starting point to the ending point.
    public static Vector3D FromTo(Vector3D from, Vector3D to) => to - from;
    /// 
    /// Scales a  by another  component-wise.
    /// 
    /// The  to scale.
    /// The  containing the scaling factors for each component.
    /// The scaled .
    public static Vector3D Scale(Vector3D vector, Vector3D scale) => new(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z);
    /// 
    /// Rotates a  around a axis by the specified angle (in radians).
    /// 
    /// The  to rotate.
    /// The  to rotate around.
    /// The angle to rotate by, in radians.
    /// The rotated .
    public static Vector3D Rotate(Vector3D vector, Vector3D axis, float angleInRadian) => vector * Math.Cos(angleInRadian) + Cross(axis, vector) * Math.Sin(angleInRadian) + axis * Dot(axis, vector) * (1f - Math.Cos(angleInRadian));
    /// 
    /// Returns the component-wise minimum of two s.
    /// 
    /// The first .
    /// The second .
    /// The  containing the minimum components from both input s.
    public static Vector3D Min(Vector3D left, Vector3D right) => new((left.X < right.X) ? left.X : right.X, (left.Y < right.Y) ? left.Y : right.Y, (left.Z < right.Z) ? left.Z : right.Z);
    /// 
    /// Returns the component-wise maximum of two s.
    /// 
    /// The first .
    /// The second .
    /// The  containing the maximum components from both input s.
    public static Vector3D Max(Vector3D left, Vector3D right) => new((left.X > right.X) ? left.X : right.X, (left.Y > right.Y) ? left.Y : right.Y, (left.Z > right.Z) ? left.Z : right.Z);
    /// 
    /// Clamps each component of a  between the corresponding component of two other s.
    /// 
    /// The  to clamp.
    /// The  representing the minimum values for each component.
    /// The  representing the maximum values for each component.
    /// A  with each component clamped between the corresponding components of the min and max s.
    public static Vector3D Clamp(Vector3D vector, Vector3D min, Vector3D max) => new(Math.Clamp(vector.X, min.X, max.X), Math.Clamp(vector.Y, min.Y, max.Y), Math.Clamp(vector.Z, min.Z, max.Z));
    /// 
    /// Performs linear interpolation between two s.
    /// 
    /// The starting  (t = 0).
    /// The ending  (t = 1).
    /// The interpolation parameter.
    /// The interpolated .
    public static Vector3D Lerp(Vector3D from, Vector3D to, float t) => from + FromTo(from, to) * t;
    /// 
    /// Calculates the cross product of two s.
    /// 
    /// The first .
    /// The second .
    /// The cross product of the two s.
    public static Vector3D Cross(Vector3D left, Vector3D right) => new(left.Y * right.Z - left.Z * right.Y, left.Z * right.X - left.X * right.Z, left.X * right.Y - left.Y * right.X);
    /// 
    /// Calculates the angle between two s.
    /// 
    /// The first .
    /// The second .
    /// The angle between the two s in radians.
    public static float Angle(Vector3D left, Vector3D right) => Math.Acos(Dot(left, right) / (Length(left) * Length(right)));
    /// 
    /// Calculates the dot product of two s.
    /// 
    /// The first .
    /// The second .
    /// The dot product of the two s.
    public static float Dot(Vector3D left, Vector3D right) => left.X * right.X + left.Y * right.Y + left.Z * right.Z;
    /// 
    /// Transforms the  using the specified .
    /// 
    /// The  to transform.
    /// The  to apply.
    /// The transformed .
    public static Vector3D Transform(Vector3D vector, ITransform3D transform)
        => Quaternion.RotateVector(vector, transform.Rotation).Add(transform.Position).Scale(transform.Scale);
    /// 
    /// Checks if two s are approximately equal within a specified epsilon range.
    /// 
    /// The first .
    /// The second .
    /// The epsilon range.
    ///  if the s are approximately equal; otherwise, .
    public static bool ApproximatelyEquals(Vector3D left, Vector3D right, float epsilon = float.Epsilon)
        => left.X.ApproximatelyEquals(right.X, epsilon) && left.Y.ApproximatelyEquals(right.Y, epsilon) && left.Z.ApproximatelyEquals(right.Z, epsilon);
    /// 
    /// Determines whether the specified object is equal to the current .
    /// 
    /// The object to compare with the current .
    ///  if the specified object is equal to the current ; otherwise, .
    public override bool Equals(object? obj) => obj is Vector3D vector3D && this == vector3D;
    public bool Equals(Vector3D other) => this == other;
    /// 
    /// Generates a hash code for the .
    /// 
    /// A hash code for the .
    public override int GetHashCode() => System.HashCode.Combine(X, Y, Z);
    /// 
    /// Converts the  to its string representation.
    /// 
    /// A string representation of the .
    public override string ToString() => $"{nameof(Vector3D)}({X}, {Y}, {Z})";
}
/// 
/// Provides extension methods for  type.
/// 
public static class Vector3DExtensions
{
    /// 
    public static float Length(this Vector3D vector) => Vector3D.Length(vector);
    /// 
    public static float LengthSquared(this Vector3D vector) => Vector3D.LengthSquared(vector);
    /// 
    public static float Distance(this Vector3D from, Vector3D to) => Vector3D.Distance(from, to);
    /// 
    public static Vector3D Invert(this Vector3D vector) => Vector3D.Invert(vector);
    /// 
    public static Vector3D Add(this Vector3D vector, Vector3D vectorToAdd) => Vector3D.Add(vector, vectorToAdd);
    /// 
    public static Vector3D Subtract(this Vector3D vector, Vector3D vectorToSubtract) => Vector3D.Subtract(vector, vectorToSubtract);
    /// 
    public static Vector3D Multiply(this Vector3D vector, float value) => Vector3D.Multiply(vector, value);
    /// 
    public static Vector3D Divide(this Vector3D vector, float value) => Vector3D.Divide(vector, value);
    /// 
    public static Vector3D Abs(this Vector3D vector) => Vector3D.Abs(vector);
    /// 
    public static Vector3D Reflect(this Vector3D vector, Vector3D normal) => Vector3D.Reflect(vector, normal);
    /// 
    public static Vector3D Normalize(this Vector3D vector) => Vector3D.Normalize(vector);
    /// 
    public static Vector3D FromTo(this Vector3D from, Vector3D to) => Vector3D.FromTo(from, to);
    /// 
    public static Vector3D Scale(this Vector3D vector, Vector3D scale) => Vector3D.Scale(vector, scale);
    /// 
    public static Vector3D Rotate(this Vector3D vector, Vector3D normal, float angleInRadian) => Vector3D.Rotate(vector, normal, angleInRadian);
    /// 
    public static Vector3D Min(this Vector3D left, Vector3D right) => Vector3D.Min(left, right);
    /// 
    public static Vector3D Max(this Vector3D left, Vector3D right) => Vector3D.Max(left, right);
    /// 
    public static Vector3D Clamp(this Vector3D vector, Vector3D min, Vector3D max) => Vector3D.Clamp(vector, min, max);
    /// 
    public static Vector3D Lerp(this Vector3D from, Vector3D to, float t) => Vector3D.Lerp(from, to, t);
    /// 
    public static Vector3D Cross(this Vector3D left, Vector3D right) => Vector3D.Cross(left, right);
    /// 
    public static float AngleBetween(this Vector3D left, Vector3D right) => Vector3D.Angle(left, right);
    /// 
    public static float Dot(this Vector3D left, Vector3D right) => Vector3D.Dot(left, right);
    /// 
    public static Vector3D Transform(this Vector3D vector, ITransform3D transform) => Vector3D.Transform(vector, transform);
    /// 
    public static Vector3D Transform(this ITransform3D transform, Vector3D vector) => Vector3D.Transform(vector, transform);
    /// 
    public static bool ApproximatelyEquals(this Vector3D left, Vector3D right, float epsilon = float.Epsilon) => Vector3D.ApproximatelyEquals(left, right, epsilon);
}