forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathfExtensions.cs
More file actions
62 lines (53 loc) · 1.88 KB
/
Copy pathMathfExtensions.cs
File metadata and controls
62 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// Extension methods and helper functions for various math data
/// </summary>
public static class MathExtensions
{
public static int MostSignificantBit(this int x)
{
x |= (x >> 1);
x |= (x >> 2);
x |= (x >> 4);
x |= (x >> 8);
x |= (x >> 16);
return x & ~(x >> 1);
}
public static int PowerOfTwoGreaterThanOrEqualTo(this int v)
{
if (Mathf.IsPowerOfTwo(v))
{
return v;
}
return Mathf.NextPowerOfTwo(v);
}
public static Int3 PowerOfTwoGreaterThanOrEqualTo(this Int3 v)
{
return new Int3(PowerOfTwoGreaterThanOrEqualTo(v.x),
PowerOfTwoGreaterThanOrEqualTo(v.y),
PowerOfTwoGreaterThanOrEqualTo(v.z));
}
public static int CubicToLinearIndex(Int3 ndx, Int3 size)
{
return (ndx.x) +
(ndx.y * size.x) +
(ndx.z * size.x * size.y);
}
public static Int3 LinearToCubicIndex(int linearIndex, Int3 size)
{
return new Int3((linearIndex / 1) % size.x,
(linearIndex / size.x) % size.y,
(linearIndex / (size.x * size.y)) % size.z);
}
public static Vector3 ClampComponentwise(Vector3 value, Vector3 min, Vector3 max)
{
return new Vector3(Mathf.Clamp(value.x, min.x, max.x),
Mathf.Clamp(value.y, min.y, max.y),
Mathf.Clamp(value.z, min.z, max.z));
}
}
}