forked from microsoft/MixedRealityToolkit-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorExtensions.cs
More file actions
168 lines (145 loc) · 4.96 KB
/
Copy pathVectorExtensions.cs
File metadata and controls
168 lines (145 loc) · 4.96 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace HoloToolkit.Unity
{
/// <summary>
/// A collection of useful extension methods for Unity's Vector structs.
/// </summary>
public static class VectorExtensions
{
public static Vector2 Mul(this Vector2 value, Vector2 scale)
{
return new Vector2(value.x * scale.x, value.y * scale.y);
}
public static Vector2 Div(this Vector2 value, Vector2 scale)
{
return new Vector2(value.x / scale.x, value.y / scale.y);
}
public static Vector3 Mul(this Vector3 value, Vector3 scale)
{
return new Vector3(value.x * scale.x, value.y * scale.y, value.z * scale.z);
}
public static Vector3 Div(this Vector3 value, Vector3 scale)
{
return new Vector3(value.x / scale.x, value.y / scale.y, value.z / scale.z);
}
public static Vector3 RotateAround(this Vector3 point, Vector3 pivot, Quaternion rotation)
{
return rotation * (point - pivot) + pivot;
}
public static Vector3 RotateAround(this Vector3 point, Vector3 pivot, Vector3 eulerAngles)
{
return RotateAround(point, pivot, Quaternion.Euler(eulerAngles));
}
public static Vector3 TransformPoint(this Vector3 point, Vector3 translation, Quaternion rotation, Vector3 lossyScale)
{
return rotation * Vector3.Scale(lossyScale, point) + translation;
}
public static Vector3 InverseTransformPoint(this Vector3 point, Vector3 translation, Quaternion rotation, Vector3 lossyScale)
{
var scaleInv = new Vector3(1 / lossyScale.x, 1 / lossyScale.y, 1 / lossyScale.z);
return Vector3.Scale(scaleInv, (Quaternion.Inverse(rotation) * (point - translation)));
}
public static Vector2 Average(this IEnumerable<Vector2> vectors)
{
float x = 0f;
float y = 0f;
int count = 0;
foreach (var pos in vectors)
{
x += pos.x;
y += pos.y;
count++;
}
return new Vector2(x / count, y / count);
}
public static Vector3 Average(this IEnumerable<Vector3> vectors)
{
float x = 0f;
float y = 0f;
float z = 0f;
int count = 0;
foreach (var pos in vectors)
{
x += pos.x;
y += pos.y;
z += pos.z;
count++;
}
return new Vector3(x / count, y / count, z / count);
}
public static Vector2 Average(this ICollection<Vector2> vectors)
{
int count = vectors.Count;
if (count == 0)
{
return Vector2.zero;
}
float x = 0f;
float y = 0f;
foreach (var pos in vectors)
{
x += pos.x;
y += pos.y;
}
return new Vector2(x / count, y / count);
}
public static Vector3 Average(this ICollection<Vector3> vectors)
{
int count = vectors.Count;
if (count == 0)
{
return Vector3.zero;
}
float x = 0f;
float y = 0f;
float z = 0f;
foreach (var pos in vectors)
{
x += pos.x;
y += pos.y;
z += pos.z;
}
return new Vector3(x / count, y / count, z / count);
}
public static Vector2 Median(this IEnumerable<Vector2> vectors)
{
int count = vectors.Count();
if (count == 0)
{
return Vector2.zero;
}
return vectors.OrderBy(v => v.sqrMagnitude).ElementAt(count / 2);
}
public static Vector3 Median(this IEnumerable<Vector3> vectors)
{
int count = vectors.Count();
if (count == 0)
{
return Vector3.zero;
}
return vectors.OrderBy(v => v.sqrMagnitude).ElementAt(count / 2);
}
public static Vector2 Median(this ICollection<Vector2> vectors)
{
int count = vectors.Count;
if (count == 0)
{
return Vector2.zero;
}
return vectors.OrderBy(v => v.sqrMagnitude).ElementAt(count / 2);
}
public static Vector3 Median(this ICollection<Vector3> vectors)
{
int count = vectors.Count;
if (count == 0)
{
return Vector3.zero;
}
return vectors.OrderBy(v => v.sqrMagnitude).ElementAt(count / 2);
}
}
}