9using System.Collections.Generic;
20 public static class Infix {
27 public static float Clamped01(
this float f) {
28 return Mathf.Clamp01(f);
34 public static float Clamped(
this float f,
float min,
float max) {
35 return Mathf.Clamp(f, min, max);
41 public static float Abs(
this float f) {
49 public static float NaNOrInfTo(
this float f,
float valueIfNaNOrInf) {
50 if (
float.IsNaN(f) ||
float.IsNegativeInfinity(f) ||
51 float.IsPositiveInfinity(f))
53 return valueIfNaNOrInf;
60 public static bool IsInfinity(
this float f) {
61 return float.IsPositiveInfinity(f) ||
float.IsNegativeInfinity(f);
71 public static Vector2 Abs(
this Vector2 v) {
72 return new Vector2(Mathf.Abs(v.x), Mathf.Abs(v.y));
78 public static Vector3 WithZ(
this Vector2 v,
float z) {
79 return new Vector3(v.x, v.y, z);
84 public static Vector2 FlippedX(
this Vector2 v) {
85 return new Vector2(-v.x, v.y);
88 public static Vector2 FlippedY(
this Vector2 v) {
89 return new Vector2(v.x, -v.y);
100 public static Vector3 RotatedBy(
this Vector3 thisVector, Quaternion byQuaternion) {
101 return byQuaternion * thisVector;
110 public static Vector3 MovedTowards(
this Vector3 thisPosition,
111 Vector3 otherPosition,
112 float maxDistanceDelta) {
113 return Vector3.MoveTowards(thisPosition, otherPosition, maxDistanceDelta);
119 public static float Dot(
this Vector3 a, Vector3 b) {
120 return Vector3.Dot(a, b);
126 public static Vector3 Cross(
this Vector3 a, Vector3 b) {
127 return Vector3.Cross(a, b);
134 public static Vector3 RHCross(
this Vector3 a, Vector3 b) {
135 return Vector3.Cross(b, a);
141 public static float Angle(
this Vector3 a, Vector3 b) {
142 return Vector3.Angle(a, b);
148 public static float SignedAngle(
this Vector3 a, Vector3 b, Vector3 axis) {
149 float sign = Vector3.Dot(Vector3.Cross(a,b), axis) < 0f ? -1f : 1f;
150 return sign * Vector3.Angle(a, b);
156 public static Vector3 Exp(
this Vector3 v) {
157 return new Vector3(Mathf.Exp(v.x), Mathf.Exp(v.y), Mathf.Exp(v.z));
163 public static Vector3 Abs(
this Vector3 v) {
164 return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
170 public static Vector3 WithZ(
this Vector3 v,
float z) {
171 return new Vector3(v.x, v.y, z);
177 public static Vector2 ToVector2(
this Vector3 v) {
178 return new Vector2(v.x, v.y);
186 public static Vector3 NaNOrInfTo(
this Vector3 v,
float valueIfNaNOrInf) {
188 v.x.NaNOrInfTo(valueIfNaNOrInf),
189 v.y.NaNOrInfTo(valueIfNaNOrInf),
190 v.z.NaNOrInfTo(valueIfNaNOrInf));
193 public static bool ContainsNaNOrInf(
this Vector3 v) {
194 return v.ContainsNaN() ||
195 v.x.IsInfinity() || v.y.IsInfinity() || v.z.IsInfinity();
198 public static Vector3 FlippedX(
this Vector3 v) {
199 return new Vector3(-v.x, v.y, v.z);
202 public static Vector3 FlippedY(
this Vector3 v) {
203 return new Vector3(v.x, -v.y, v.z);
206 public static Vector3 FlippedZ(
this Vector3 v) {
207 return new Vector3(v.x, v.y, -v.z);
210 public static Vector3 ProjectOnPlane(
this Vector3 v, Vector3 planeNormal) {
211 return Vector3.ProjectOnPlane(v, planeNormal);
214 public static Vector3 WithLength(
this Vector3 v,
float newLength) {
215 return v.normalized * newLength;
218 public static Vector3 Lerp(
this Vector3 a, Vector3 b,
float t) {
219 return Vector3.Lerp(a, b, t);
222 public static Vector3 LerpUnclamped(
this Vector3 a, Vector3 b,
float t) {
223 return Vector3.LerpUnclamped(a, b, t);
228 #region List<Vector3>
230 public static Vector3 GetCentroid(
this List<Vector3> vs) {
231 return vs.Query().Fold((v, acc) => v + acc) / vs.Count;
235 public static Vector3 SetCentroid(
this List<Vector3> vs, Vector3 c) {
236 var oldC = vs.GetCentroid();
237 var toNewC = c - oldC;
238 for (var i = 0; i < vs.Count; i++) { vs[i] = vs[i] + toNewC; }
249 public static Vector3 WithXYZ(
this Vector4 v, Vector3 xyz) {
250 return new Vector4(xyz.x, xyz.y, xyz.z, v.w);
255 public static Vector3 WithXYZ(
this Vector4 v0, Vector4 v1) {
256 return new Vector4(v1.x, v1.y, v1.z, v0.w);
267 public static Vector3 GetRight(
this Quaternion q) {
268 return q * Vector3.right;
274 public static Vector3 GetUp(
this Quaternion q) {
275 return q * Vector3.up;
282 public static Vector3 GetForward(
this Quaternion q) {
283 return q * Vector3.forward;
290 public static Vector3 GetLeft(
this Quaternion q) {
291 return q * Vector3.left;
295 public static Quaternion Inverse(
this Quaternion q) {
296 return Quaternion.Inverse(q);
300 public static Matrix4x4 ToMatrix(
this Quaternion q) {
301 return Matrix4x4.Rotate(q);
305 public static Matrix4x4 GetMatrix(
this Quaternion q) {
306 return Matrix4x4.Rotate(q);
314 public static int Min(
this Vector2Int v) {
315 return Mathf.Min(v.x, v.y);
319 public static int Max(
this Vector2Int v) {
320 return Mathf.Max(v.x, v.y);
328 return Color.Lerp(c, other, t);
331 public static Color WithValue(
this Color c,
float newHSVValue) {
332 float h, s, v;
Color.RGBToHSV(c, out h, out s, out v);
333 return Color.HSVToRGB(h, s, newHSVValue);
336 public static Color WithSat(
this Color c,
float newHSVSat) {
337 float h, s, v;
Color.RGBToHSV(c, out h, out s, out v);
338 return Color.HSVToRGB(h, newHSVSat, v);
341 public static Color WithHSV(
this Color c,
float? h =
null,
float? s =
null,
344 float origH, origS, origV;
345 Color.RGBToHSV(c, out origH, out origS, out origV);
346 var useH = h.UnwrapOr(origH);
347 var useS = s.UnwrapOr(origS);
348 var useV = v.UnwrapOr(origV);
349 return Color.HSVToRGB(useH, useS, useV);
352 public static Color ShiftHue(
this Color c,
float shift) {
353 float origH, origS, origV;
354 Color.RGBToHSV(c, out origH, out origS, out origV);
356 while (shift < 0f && limit++ != 1000) {
359 var h = origH + shift;
361 return Color.HSVToRGB(h, origS, origV);
369 public static bool HasNthBit(
this int integer,
int bitIdx) {
370 return (integer & (1 << bitIdx)) != 0;
378 public static string PathCombine(
this string path0,
string path1) {
379 return Path.Combine(path0, path1);
385 public static string GetDirectoryName(
this string path) {
386 return Path.GetDirectoryName(path);