AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
MathfExtensions.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 UnityEngine;
5 
6 namespace HoloToolkit.Unity
7 {
11  public static class MathExtensions
12  {
13  public static int MostSignificantBit(this int x)
14  {
15  x |= (x >> 1);
16  x |= (x >> 2);
17  x |= (x >> 4);
18  x |= (x >> 8);
19  x |= (x >> 16);
20 
21  return x & ~(x >> 1);
22  }
23 
24  public static int PowerOfTwoGreaterThanOrEqualTo(this int v)
25  {
26  if (Mathf.IsPowerOfTwo(v))
27  {
28  return v;
29  }
30 
31  return Mathf.NextPowerOfTwo(v);
32  }
33 
35  {
36  return new Int3(PowerOfTwoGreaterThanOrEqualTo(v.x),
37  PowerOfTwoGreaterThanOrEqualTo(v.y),
38  PowerOfTwoGreaterThanOrEqualTo(v.z));
39  }
40 
41  public static int CubicToLinearIndex(Int3 ndx, Int3 size)
42  {
43  return (ndx.x) +
44  (ndx.y * size.x) +
45  (ndx.z * size.x * size.y);
46  }
47 
48  public static Int3 LinearToCubicIndex(int linearIndex, Int3 size)
49  {
50  return new Int3((linearIndex / 1) % size.x,
51  (linearIndex / size.x) % size.y,
52  (linearIndex / (size.x * size.y)) % size.z);
53  }
54 
55  public static Vector3 ClampComponentwise(Vector3 value, Vector3 min, Vector3 max)
56  {
57  return new Vector3(Mathf.Clamp(value.x, min.x, max.x),
58  Mathf.Clamp(value.y, min.y, max.y),
59  Mathf.Clamp(value.z, min.z, max.z));
60  }
61  }
62 }
static int PowerOfTwoGreaterThanOrEqualTo(this int v)
Extension methods and helper functions for various math data
static int CubicToLinearIndex(Int3 ndx, Int3 size)
3D integer class - operates similarly to Unity's Vector3D
Definition: Int3.cs:16
static Int3 LinearToCubicIndex(int linearIndex, Int3 size)
static int MostSignificantBit(this int x)
static Vector3 ClampComponentwise(Vector3 value, Vector3 min, Vector3 max)
static Int3 PowerOfTwoGreaterThanOrEqualTo(this Int3 v)