AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
Extensions.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
4 using System;
5 using System.Collections.Generic;
6 using System.Collections.ObjectModel;
7 using System.Linq;
8 using UnityEngine;
9 using Object = UnityEngine.Object;
10 
11 namespace HoloToolkit.Unity
12 {
16  public static class Extensions
17  {
23  public static float Duration(this AnimationCurve curve)
24  {
25  if (curve == null || curve.length <= 1)
26  {
27  return 0.0f;
28  }
29 
30  return Mathf.Abs(curve[curve.length - 1].time - curve[0].time);
31  }
32 
38  public static bool IsValid(this Ray ray)
39  {
40  return (ray.direction != Vector3.zero);
41  }
42 
43  #region Collections
44 
51  public static ReadOnlyCollection<TElement> AsReadOnly<TElement>(this IList<TElement> elements)
52  {
53  return new ReadOnlyCollection<TElement>(elements);
54  }
55 
62  public static ReadOnlyCollection<TElement> ToReadOnlyCollection<TElement>(this IEnumerable<TElement> elements)
63  {
64  return elements.ToArray().AsReadOnly();
65  }
66 
78  public static int SortedInsert<TElement>(this List<TElement> elements, TElement toInsert, IComparer<TElement> comparer = null)
79  {
80  var effectiveComparer = (comparer ?? Comparer<TElement>.Default);
81 
82 #if DEBUG || UNITY_EDITOR
83  for (int iElement = 0; iElement < (elements.Count - 1); iElement++)
84  {
85  var element = elements[iElement];
86  var nextElement = elements[iElement + 1];
87 
88  if (effectiveComparer.Compare(element, nextElement) > 0)
89  {
90  Debug.Assert(false, "elements must already be sorted to call this method.");
91  break;
92  }
93  }
94 #endif
95 
96  int searchResult = elements.BinarySearch(toInsert, effectiveComparer);
97 
98  int insertionIndex = (searchResult >= 0)
99  ? searchResult
100  : (~searchResult);
101 
102  elements.Insert(insertionIndex, toInsert);
103 
104  return insertionIndex;
105  }
106 
112  public static void DisposeElements<TElement>(this IEnumerable<TElement> elements)
113  where TElement : IDisposable
114  {
115  foreach (var element in elements)
116  {
117  if (element != null)
118  {
119  element.Dispose();
120  }
121  }
122  }
123 
129  public static void DisposeElements<TElement>(this IList<TElement> elements)
130  where TElement : IDisposable
131  {
132  for (int iElement = 0; iElement < elements.Count; iElement++)
133  {
134  var element = elements[iElement];
135 
136  if (element != null)
137  {
138  element.Dispose();
139  }
140  }
141  }
142 
143  #endregion
144 
145  #region Numerics
146 
155  public static bool Approximately(this float number, float other, float tolerance)
156  {
157  return (Mathf.Abs(number - other) <= tolerance);
158  }
159 
168  public static bool Approximately(this double number, double other, double tolerance)
169  {
170  return (Math.Abs(number - other) <= tolerance);
171  }
172 
173  #endregion
174 
175  #region UnityEngine.Object
176 
177  public static void DontDestroyOnLoad(this Object target)
178  {
179 #if UNITY_EDITOR // Skip Don't Destroy On Load when editor isn't playing so test runner passes.
180  if (UnityEditor.EditorApplication.isPlaying)
181 #endif
182  Object.DontDestroyOnLoad(target);
183  }
184 
185  #endregion
186 
187  #region GameObject
188 
195  public static bool IsInLayerMask(this GameObject gameObject, LayerMask layerMask)
196  {
197  LayerMask gameObjectMask = (1 << gameObject.layer);
198  return ((gameObjectMask & layerMask) == gameObjectMask);
199  }
200 
201  #endregion
202 
203  #region Comparer
204 
211  public static IComparer<TElement> GetReversed<TElement>(this IComparer<TElement> originalComparer)
212  {
213  return new ReverseComparer<TElement>(originalComparer);
214  }
215 
216  private class ReverseComparer<TElement> :
217  IComparer<TElement>
218  {
219  private readonly IComparer<TElement> originalComparer;
220 
221  public ReverseComparer(IComparer<TElement> originalComparer)
222  {
223  Debug.Assert(originalComparer != null, "originalComparer cannot be null.");
224 
225  this.originalComparer = originalComparer;
226  }
227 
228  public int Compare(TElement left, TElement right)
229  {
230  return originalComparer.Compare(right, left);
231  }
232  }
233 
234  #endregion
235  }
236 }
static bool IsValid(this Ray ray)
Determines whether or not a ray is valid.
Definition: Extensions.cs:38
A class with general purpose extensions methods.
Definition: Extensions.cs:16
static void DontDestroyOnLoad(this Object target)
Definition: Extensions.cs:177
static float Duration(this AnimationCurve curve)
Returns the absolute duration of the curve from first to last key frame
Definition: Extensions.cs:23
static bool Approximately(this double number, double other, double tolerance)
Checks if two numbers are approximately equal. Similar to Mathf.Approximately(float, float), but the tolerance can be specified.
Definition: Extensions.cs:168
static bool IsInLayerMask(this GameObject gameObject, LayerMask layerMask)
Determines whether or not a game object&#39;s layer is included in the specified layer mask...
Definition: Extensions.cs:195
static bool Approximately(this float number, float other, float tolerance)
Checks if two numbers are approximately equal. Similar to Mathf.Approximately(float, float), but the tolerance can be specified.
Definition: Extensions.cs:155