Switch-Toolbox/Switch_Toolbox_Library/Maths/Math.cs

78 lines
2.7 KiB
C#
Raw Normal View History

2019-05-08 20:28:04 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;
2019-05-08 20:28:04 +00:00
namespace Toolbox.Library
2019-05-08 20:28:04 +00:00
{
public static class STMath
{
private const long SizeOfKb = 1024;
private const long SizeOfMb = SizeOfKb * 1024;
private const long SizeOfGb = SizeOfMb * 1024;
private const long SizeOfTb = SizeOfGb * 1024;
public static double ConvertBytesToMegabytes(long bytes)
{
return (bytes / SizeOfKb) / SizeOfKb;
}
static double ConvertKilobytesToMegabytes(long kilobytes)
{
return kilobytes / SizeOfKb;
}
public static string GetFileSize(this long value, int decimalPlaces = 0)
{
var asTb = Math.Round((double)value / SizeOfTb, decimalPlaces);
var asGb = Math.Round((double)value / SizeOfGb, decimalPlaces);
var asMb = Math.Round((double)value / SizeOfMb, decimalPlaces);
var asKb = Math.Round((double)value / SizeOfKb, decimalPlaces);
2019-06-18 20:27:36 +00:00
string chosenValue = asTb > 1 ? string.Format("{0} TB", asTb)
: asGb > 1 ? string.Format("{0} GB", asGb)
: asMb > 1 ? string.Format("{0} MB", asMb)
: asKb > 1 ? string.Format("{0} KB", asKb)
: string.Format("{0} bytes", Math.Round((double)value, decimalPlaces));
2019-05-08 20:28:04 +00:00
return chosenValue;
}
//From https://github.com/Ploaj/SSBHLib/blob/e37b0d83cd088090f7802be19b1d05ec998f2b6a/CrossMod/Tools/CrossMath.cs#L42
//Seems to give good results
2019-05-26 15:32:12 +00:00
public static Vector3 ToEulerAngles(double X, double Y, double Z, double W) {
return ToEulerAngles(new Quaternion((float)X, (float)Y, (float)Z, (float)W));
}
public static Vector3 ToEulerAngles(float X, float Y, float Z, float W) {
return ToEulerAngles(new Quaternion(X, Y, Z, W));
}
public static Vector3 ToEulerAngles(Quaternion q)
{
Matrix4 mat = Matrix4.CreateFromQuaternion(q);
float x, y, z;
y = (float)Math.Asin(Clamp(mat.M13, -1, 1));
if (Math.Abs(mat.M13) < 0.99999)
{
x = (float)Math.Atan2(-mat.M23, mat.M33);
z = (float)Math.Atan2(-mat.M12, mat.M11);
}
else
{
x = (float)Math.Atan2(mat.M32, mat.M22);
z = 0;
}
return new Vector3(x, y, z) * -1;
}
public static float Clamp(float v, float min, float max)
{
if (v < min) return min;
if (v > max) return max;
return v;
}
2019-05-08 20:28:04 +00:00
}
}