Tanoda
Infix.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System.Collections.Generic;
10using System.IO;
11using Leap.Unity.Query;
12using UnityEngine;
13
15
20 public static class Infix {
21
22 #region Float
23
27 public static float Clamped01(this float f) {
28 return Mathf.Clamp01(f);
29 }
30
34 public static float Clamped(this float f, float min, float max) {
35 return Mathf.Clamp(f, min, max);
36 }
37
41 public static float Abs(this float f) {
42 return Mathf.Abs(f);
43 }
44
49 public static float NaNOrInfTo(this float f, float valueIfNaNOrInf) {
50 if (float.IsNaN(f) || float.IsNegativeInfinity(f) ||
51 float.IsPositiveInfinity(f))
52 {
53 return valueIfNaNOrInf;
54 }
55 return f;
56 }
57
60 public static bool IsInfinity(this float f) {
61 return float.IsPositiveInfinity(f) || float.IsNegativeInfinity(f);
62 }
63
64 #endregion
65
66 #region Vector2
67
71 public static Vector2 Abs(this Vector2 v) {
72 return new Vector2(Mathf.Abs(v.x), Mathf.Abs(v.y));
73 }
74
78 public static Vector3 WithZ(this Vector2 v, float z) {
79 return new Vector3(v.x, v.y, z);
80 }
81
82
83
84 public static Vector2 FlippedX(this Vector2 v) {
85 return new Vector2(-v.x, v.y);
86 }
87
88 public static Vector2 FlippedY(this Vector2 v) {
89 return new Vector2(v.x, -v.y);
90 }
91
92 #endregion
93
94 #region Vector3
95
100 public static Vector3 RotatedBy(this Vector3 thisVector, Quaternion byQuaternion) {
101 return byQuaternion * thisVector;
102 }
103
110 public static Vector3 MovedTowards(this Vector3 thisPosition,
111 Vector3 otherPosition,
112 float maxDistanceDelta) {
113 return Vector3.MoveTowards(thisPosition, otherPosition, maxDistanceDelta);
114 }
115
119 public static float Dot(this Vector3 a, Vector3 b) {
120 return Vector3.Dot(a, b);
121 }
122
126 public static Vector3 Cross(this Vector3 a, Vector3 b) {
127 return Vector3.Cross(a, b);
128 }
129
134 public static Vector3 RHCross(this Vector3 a, Vector3 b) {
135 return Vector3.Cross(b, a);
136 }
137
141 public static float Angle(this Vector3 a, Vector3 b) {
142 return Vector3.Angle(a, b);
143 }
144
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);
151 }
152
156 public static Vector3 Exp(this Vector3 v) {
157 return new Vector3(Mathf.Exp(v.x), Mathf.Exp(v.y), Mathf.Exp(v.z));
158 }
159
163 public static Vector3 Abs(this Vector3 v) {
164 return new Vector3(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
165 }
166
170 public static Vector3 WithZ(this Vector3 v, float z) {
171 return new Vector3(v.x, v.y, z);
172 }
173
177 public static Vector2 ToVector2(this Vector3 v) {
178 return new Vector2(v.x, v.y);
179 }
180
186 public static Vector3 NaNOrInfTo(this Vector3 v, float valueIfNaNOrInf) {
187 return new Vector3(
188 v.x.NaNOrInfTo(valueIfNaNOrInf),
189 v.y.NaNOrInfTo(valueIfNaNOrInf),
190 v.z.NaNOrInfTo(valueIfNaNOrInf));
191 }
192
193 public static bool ContainsNaNOrInf(this Vector3 v) {
194 return v.ContainsNaN() ||
195 v.x.IsInfinity() || v.y.IsInfinity() || v.z.IsInfinity();
196 }
197
198 public static Vector3 FlippedX(this Vector3 v) {
199 return new Vector3(-v.x, v.y, v.z);
200 }
201
202 public static Vector3 FlippedY(this Vector3 v) {
203 return new Vector3(v.x, -v.y, v.z);
204 }
205
206 public static Vector3 FlippedZ(this Vector3 v) {
207 return new Vector3(v.x, v.y, -v.z);
208 }
209
210 public static Vector3 ProjectOnPlane(this Vector3 v, Vector3 planeNormal) {
211 return Vector3.ProjectOnPlane(v, planeNormal);
212 }
213
214 public static Vector3 WithLength(this Vector3 v, float newLength) {
215 return v.normalized * newLength;
216 }
217
218 public static Vector3 Lerp(this Vector3 a, Vector3 b, float t) {
219 return Vector3.Lerp(a, b, t);
220 }
221
222 public static Vector3 LerpUnclamped(this Vector3 a, Vector3 b, float t) {
223 return Vector3.LerpUnclamped(a, b, t);
224 }
225
226 #endregion
227
228 #region List<Vector3>
229
230 public static Vector3 GetCentroid(this List<Vector3> vs) {
231 return vs.Query().Fold((v, acc) => v + acc) / vs.Count;
232 }
233
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; }
239 return toNewC;
240 }
241
242 #endregion
243
244 #region Vector4
245
249 public static Vector3 WithXYZ(this Vector4 v, Vector3 xyz) {
250 return new Vector4(xyz.x, xyz.y, xyz.z, v.w);
251 }
255 public static Vector3 WithXYZ(this Vector4 v0, Vector4 v1) {
256 return new Vector4(v1.x, v1.y, v1.z, v0.w);
257 }
258
259 #endregion
260
261 #region Quaternion
262
267 public static Vector3 GetRight(this Quaternion q) {
268 return q * Vector3.right;
269 }
270
274 public static Vector3 GetUp(this Quaternion q) {
275 return q * Vector3.up;
276 }
277
282 public static Vector3 GetForward(this Quaternion q) {
283 return q * Vector3.forward;
284 }
285
290 public static Vector3 GetLeft(this Quaternion q) {
291 return q * Vector3.left;
292 }
293
295 public static Quaternion Inverse(this Quaternion q) {
296 return Quaternion.Inverse(q);
297 }
298
300 public static Matrix4x4 ToMatrix(this Quaternion q) {
301 return Matrix4x4.Rotate(q);
302 }
303
305 public static Matrix4x4 GetMatrix(this Quaternion q) {
306 return Matrix4x4.Rotate(q);
307 }
308
309 #endregion
310
311 #region Vector2Int
312
314 public static int Min(this Vector2Int v) {
315 return Mathf.Min(v.x, v.y);
316 }
317
319 public static int Max(this Vector2Int v) {
320 return Mathf.Max(v.x, v.y);
321 }
322
323 #endregion
324
325 #region Color
326
327 public static Color Lerp(this Color c, Color other, float t) {
328 return Color.Lerp(c, other, t);
329 }
330
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);
334 }
335
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);
339 }
340
341 public static Color WithHSV(this Color c, float? h = null, float? s = null,
342 float? v = null)
343 {
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);
350 }
351
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);
355 int limit = 0;
356 while (shift < 0f && limit++ != 1000) {
357 shift += 1f;
358 }
359 var h = origH + shift;
360 h %= 1f;
361 return Color.HSVToRGB(h, origS, origV);
362 }
363
364 #endregion
365
366 #region Integer
367
369 public static bool HasNthBit(this int integer, int bitIdx) {
370 return (integer & (1 << bitIdx)) != 0;
371 }
372
373 #endregion
374
375 #region String
376
378 public static string PathCombine(this string path0, string path1) {
379 return Path.Combine(path0, path1);
380 }
381
385 public static string GetDirectoryName(this string path) {
386 return Path.GetDirectoryName(path);
387 }
388
389 #endregion
390
391 }
392
393}
UnityEngine.Color Color
Definition: TestScript.cs:32