-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtility.cs
More file actions
249 lines (221 loc) · 8.08 KB
/
Utility.cs
File metadata and controls
249 lines (221 loc) · 8.08 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using UnityEngine;
public static class Utility {
public static System.Random rngGen = new System.Random();
/// <summary>
/// Low becomes High, High becomes low.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns>Value between 0-1</returns>
public static double NormalizedInverseSigmoid(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
double sigmoid = 1f / (1f + Math.Pow(normalize / (1 - normalize), 3));
return Clamp(sigmoid, 0, 1);
}
/// <summary>
/// Low becomes low, High becomes high.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns>Value between 0-1</returns>
public static double NormalizedSigmoid(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
double sigmoid = 1f / (1f + Math.Pow(normalize / (1 - normalize), -3));
return Clamp(sigmoid, 0, 1);
}
/// <summary>
/// Low becomes low, High becomes high.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns>Value between 0-1</returns>
public static double NormalizedExponential(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
return Clamp(normalize * normalize, 0, 1);
}
/// <summary>
/// Low becomes High, High becomes low.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns>Value between 0-1</returns>
public static double NormalizedInverseExponential(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
return Clamp(-(normalize * normalize) + 1, 0, 1);
}
/// <summary>
/// Low is low, High is High.
/// High rate of change when low, lowers rate of change when high.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns>Value between 0-1</returns>
public static double NormalizedParabolic(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
return Clamp(-((normalize - 1) * (normalize - 1)) + 1, 0,1);
}
/// <summary>
/// Low is low, High is low.
/// Peak at 0.5.
/// </summary>
/// <param name="value"></param>
/// <param name="maxValue"></param>
/// <returns></returns>
public static double NormalizedQuadratic(double value, double maxValue = 1f)
{
double normalize = value / maxValue;
return Clamp(-(normalize * normalize) + normalize, 0,1);
}
/// <summary>
/// Clamps the provided value so that it is not less than min and not more than max.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="val"></param>
/// <param name="min"></param>
/// <param name="max"></param>
/// <returns></returns>
public static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
public static double Lerp(double from, double to, double perc)
{
perc = Clamp(perc, 0.0, 1.0);
return from+((to - from) * perc);
}
public static void TryAdd<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (!dictionary.ContainsKey(key))
{
dictionary.Add(key, value);
}
}
/// <summary>
/// Replaces the values of x with the values of y.
/// Requires [StructLayout(LayoutKind.Sequential)] or (LayoutKind.Explicit).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="x"></param>
/// <param name="y"></param>
public static void Replace<T>(T x, T y)
where T : class
{
// replaces 'x' with 'y'
if (x == null) throw new ArgumentNullException("x");
if (y == null) throw new ArgumentNullException("y");
var size = Marshal.SizeOf(typeof(T));
var ptr = Marshal.AllocHGlobal(size);
Marshal.StructureToPtr(y, ptr, false);
Marshal.PtrToStructure(ptr, x);
Marshal.FreeHGlobal(ptr);
}
public static void Shuffle<T>(List<T> list)
{
List<T> shuffled = new List<T>(list.Count);
while (list.Count > 0)
{
int index = Utility.rngGen.Next(0, list.Count);
shuffled.Add(list[index]);
list.RemoveAt(index);
}
list.AddRange(shuffled);
}
public static bool scientificNotation = false;
public static string FormatBigNumber(double number)
{
//Debug.Log("Formatting " + number.ToString("0"));
int k = 0;
while(Math.Abs(number / Math.Pow(1000.0, k+1)) >= 1)
{
k++;
}
if(k > 0)
{
string s = (number / Math.Pow(1000.0, k)).ToString("N1");
if (scientificNotation)
{
return s + "e" + k * 3;
} else
{
switch (k)
{
case 1:
return s + "K";
case 2:
return s + "M";
case 3:
return s + "B";
case 4:
return s + "T";
case 5:
return s + "q";
case 6:
return s + "Q";
case 7:
return s + "s";
case 8:
return s + "S";
case 9:
return s + "O";
default:
//Fallback on scientific notation
return s + "e" + k * 3;
}
}
}
return number.ToString("N1");
}
public static DateTime? GetNistTime()
{
DateTime? dateTime = null;
//Try fetching 3 times
try
{
for (int i = 0; i < 3; i++)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://nist.time.gov/actualtime.cgi");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); //No caching
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (StreamReader stream = new StreamReader(response.GetResponseStream()))
{
string html = stream.ReadToEnd();//<timestamp time=\"1395772696469995\" delay=\"1395772696469995\"/>
string time = Regex.Match(html, @"(?<=\btime="")[^""]*").Value;
double milliseconds = Convert.ToInt64(time) / 1000.0;
dateTime = new DateTime(1970, 1, 1).AddMilliseconds(milliseconds);
}
return dateTime;
}
else
{
Debug.LogError("Couldnt get date/time " + response.StatusCode.ToString());
}
}
}
catch (Exception e)
{
Debug.LogError(e.Message);
}
return dateTime;
}
}