// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) VRMADA, All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using UltimateXR.Core;
using UltimateXR.Extensions.System;
using UnityEngine;
namespace UltimateXR.Extensions.Unity.Math
{
///
/// extensions.
///
public static class Vector4Ext
{
#region Public Types & Data
///
/// Represents a NaN vector.
///
public static ref readonly Vector4 NaN => ref s_nan;
#endregion
#region Public Methods
///
/// Compares two Unity Vector4 objects for equality with a specified precision threshold.
///
/// The first Vector4 to compare
/// The second Vector4 to compare
///
/// The precision threshold for float comparisons. Defaults to
/// .
///
///
/// true if the Vector4 objects are equal; otherwise, false.
///
///
/// This method performs a component-wise comparison between two Vector4 objects.
/// Each component is compared using the specified precision threshold for float comparisons.
///
public static bool EqualsUsingPrecision(this Vector4 a, Vector4 b, float precisionThreshold = UxrConstants.Math.DefaultPrecisionThreshold)
{
return Mathf.Abs(a.x - b.x) <= precisionThreshold &&
Mathf.Abs(a.y - b.y) <= precisionThreshold &&
Mathf.Abs(a.z - b.z) <= precisionThreshold &&
Mathf.Abs(a.w - b.w) <= precisionThreshold;
}
///
/// Checks whether the given vector has any NaN component.
///
/// Source vector
/// Whether any of the vector components has a NaN value
public static bool IsNaN(this in Vector4 self)
{
return float.IsNaN(self.x) || float.IsNaN(self.y) || float.IsNaN(self.z) || float.IsNaN(self.w);
}
///
/// Checks whether the given vector has any infinity component.
///
/// Source vector
/// Whether any of the vector components has an infinity value
public static bool IsInfinity(this in Vector4 self)
{
return float.IsInfinity(self.x) || float.IsInfinity(self.y) || float.IsInfinity(self.z) || float.IsInfinity(self.w);
}
///
/// Checks whether the given vector contains valid data.
///
/// Source vector
/// Whether the vector contains all valid values
public static bool IsValid(this in Vector4 self)
{
return !self.IsNaN() && !self.IsInfinity();
}
///
/// Replaces NaN component values with valid values.
///
/// Vector whose NaN values to replace
/// Vector with valid values
/// Result vector
public static Vector4 FillNanWith(this in Vector4 self, in Vector4 other)
{
float[] result = new float[VectorLength];
for (int i = 0; i < VectorLength; ++i)
{
result[i] = float.IsNaN(self[i]) ? other[i] : self[i];
}
return result.ToVector4();
}
///
/// Computes the absolute value of each component in a vector.
///
/// Source vector
/// Vector whose components are the absolute values
public static Vector4 Abs(this in Vector4 self)
{
return new Vector4(Mathf.Abs(self.x), Mathf.Abs(self.y), Mathf.Abs(self.z), Mathf.Abs(self.w));
}
///
/// Clamps values component by component.
///
/// Vector whose components to clamp
/// Minimum values
/// Maximum values
/// Clamped vector
public static Vector4 Clamp(this in Vector4 self, in Vector4 min, in Vector4 max)
{
float[] result = new float[VectorLength];
for (int i = 0; i < VectorLength; ++i)
{
result[i] = Mathf.Clamp(self[i], min[i], max[i]);
}
return result.ToVector4();
}
///
/// returns a vector with all components containing 1/component, checking for divisions by 0. Divisions by 0 have a
/// result of 0.
///
/// Source vector
/// Result vector
public static Vector4 Inverse(this in Vector4 self)
{
return new Vector4(Mathf.Approximately(self.x, 0f) ? 0f : 1f / self.x,
Mathf.Approximately(self.y, 0f) ? 0f : 1f / self.y,
Mathf.Approximately(self.z, 0f) ? 0f : 1f / self.z,
Mathf.Approximately(self.w, 0f) ? 0f : 1f / self.w);
}
///
/// Multiplies two component by component.
///
/// Operand A
/// Operand B
/// Result of multiplying both vectors component by component
public static Vector4 Multiply(this in Vector4 self, in Vector4 other)
{
return new Vector4(self.x * other.x,
self.y * other.y,
self.z * other.z,
self.w * other.w);
}
///
/// Divides a by another, checking for divisions by 0. Divisions by 0 have a result of 0.
///
/// Dividend
/// Divisor
/// Result vector
public static Vector4 Divide(this in Vector4 self, in Vector4 divisor)
{
return self.Multiply(divisor.Inverse());
}
///
/// Converts a Vector4 to a Quaternion component by component.
///
/// Source vector
/// Quaternion result
public static Quaternion ToQuaternion(this in Vector4 self)
{
return new Quaternion(self.x, self.y, self.z, self.w);
}
///
/// Transforms an array of floats to a component by component. If there are not enough values to
/// read, the remaining values are set to NaN.
///
/// Source data
/// Result vector
public static Vector4 ToVector4(this float[] data)
{
return data.Length switch
{
0 => NaN,
1 => new Vector4(data[0], float.NaN, float.NaN, float.NaN),
2 => new Vector4(data[0], data[1], float.NaN, float.NaN),
3 => new Vector4(data[0], data[1], data[2], float.NaN),
_ => new Vector4(data[0], data[1], data[2], data[3])
};
}
///
/// Tries to parse a from a string.
///
/// Source string
/// Parsed vector or NaN if there was an error
/// Whether the vector was parsed successfully
public static bool TryParse(string s, out Vector4 result)
{
try
{
result = Parse(s);
return true;
}
catch
{
result = NaN;
return false;
}
}
///
/// Parses a from a string.
///
/// Source string
/// Parsed vector
public static Vector4 Parse(string s)
{
s.ThrowIfNullOrWhitespace(nameof(s));
// Remove the parentheses
s = s.TrimStart(' ', '(', '[');
s = s.TrimEnd(' ', ')', ']');
// split the items
string[] sArray = s.Split(s_cardinalSeparator, VectorLength);
// store as an array
float[] result = new float[VectorLength];
for (int i = 0; i < sArray.Length; ++i)
{
result[i] = float.TryParse(sArray[i],
NumberStyles.Float,
CultureInfo.InvariantCulture.NumberFormat,
out float f)
? f
: float.NaN;
}
return result.ToVector4();
}
///
/// Tries to parse a from a string, asynchronously.
///
/// Source string
/// Optional cancellation token, to cancel the operation
/// Awaitable task returning the parsed vector or null if there was an error
public static Task ParseAsync(string s, CancellationToken ct = default)
{
return Task.Run(() => TryParse(s, out Vector4 result) ? result : (Vector4?)null, ct);
}
#endregion
#region Private Types & Data
private const int VectorLength = 4;
private const string CardinalSeparator = ",";
private static readonly char[] s_cardinalSeparator = CardinalSeparator.ToCharArray();
private static readonly Vector4 s_nan = float.NaN * Vector4.one;
#endregion
}
}