Skip to content

Commit

Permalink
Replace Math with MathF
Browse files Browse the repository at this point in the history
  • Loading branch information
aelurum committed Jan 31, 2025
1 parent a8bb6c7 commit 185348d
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 40 deletions.
1 change: 1 addition & 0 deletions AssetStudio/AssetStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net472' ">
<PackageReference Include="System.IO.Compression" Version="4.3.0" />
<PackageReference Include="System.Text.Json" Version="9.0.0" />
<PackageReference Include="Microsoft.Bcl.Numerics" Version="9.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion AssetStudio/Classes/AnimationClip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ public Quaternion[] UnpackQuats()
}

int lastComponent = (int)(flags & 3);
q[lastComponent] = (float)Math.Sqrt(1 - sum);
q[lastComponent] = MathF.Sqrt(1 - sum);
if ((flags & 4) != 0u)
q[lastComponent] = -q[lastComponent];
data[i] = q;
Expand Down
4 changes: 2 additions & 2 deletions AssetStudio/Classes/Mesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,7 @@ private void DecompressCompressedMesh()
var zsqr = 1 - x * x - y * y;
float z;
if (zsqr >= 0f)
z = (float)Math.Sqrt(zsqr);
z = MathF.Sqrt(zsqr);
else
{
z = 0;
Expand Down Expand Up @@ -1036,7 +1036,7 @@ private void DecompressCompressedMesh()
var zsqr = 1 - x * x - y * y;
float z;
if (zsqr >= 0f)
z = (float)Math.Sqrt(zsqr);
z = MathF.Sqrt(zsqr);
else
{
z = 0;
Expand Down
10 changes: 5 additions & 5 deletions AssetStudio/Classes/Texture2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ private int GetImageDataSize(TextureFormat textureFormat)
{
case TextureFormat.ASTC_RGBA_5x5:
// https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_astc/
imgDataSize = (int)(Math.Floor((m_Width + 4) / 5f) * Math.Floor((m_Height + 4) / 5f) * 16);
imgDataSize = (int)(MathF.Floor((m_Width + 4) / 5f) * MathF.Floor((m_Height + 4) / 5f) * 16);
break;
case TextureFormat.ASTC_RGBA_6x6:
imgDataSize = (int)(Math.Floor((m_Width + 5) / 6f) * Math.Floor((m_Height + 5) / 6f) * 16);
imgDataSize = (int)(MathF.Floor((m_Width + 5) / 6f) * MathF.Floor((m_Height + 5) / 6f) * 16);
break;
case TextureFormat.ASTC_RGBA_8x8:
imgDataSize = (int)(Math.Floor((m_Width + 7) / 8f) * Math.Floor((m_Height + 7) / 8f) * 16);
imgDataSize = (int)(MathF.Floor((m_Width + 7) / 8f) * MathF.Floor((m_Height + 7) / 8f) * 16);
break;
case TextureFormat.ASTC_RGBA_10x10:
imgDataSize = (int)(Math.Floor((m_Width + 9) / 10f) * Math.Floor((m_Height + 9) / 10f) * 16);
imgDataSize = (int)(MathF.Floor((m_Width + 9) / 10f) * MathF.Floor((m_Height + 9) / 10f) * 16);
break;
case TextureFormat.ASTC_RGBA_12x12:
imgDataSize = (int)(Math.Floor((m_Width + 11) / 12f) * Math.Floor((m_Height + 11) / 12f) * 16);
imgDataSize = (int)(MathF.Floor((m_Width + 11) / 12f) * MathF.Floor((m_Height + 11) / 12f) * 16);
break;
case TextureFormat.DXT1:
case TextureFormat.EAC_R:
Expand Down
6 changes: 3 additions & 3 deletions AssetStudio/Math/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public override int GetHashCode()

public override bool Equals(object other)
{
if (!(other is Vector2))
if (!(other is Vector2 vector2))
return false;
return Equals((Vector2)other);
return Equals(vector2);
}

public bool Equals(Vector2 other)
Expand All @@ -73,7 +73,7 @@ public void Normalize()

public float Length()
{
return (float)Math.Sqrt(LengthSquared());
return MathF.Sqrt(LengthSquared());
}

public float LengthSquared()
Expand Down
6 changes: 3 additions & 3 deletions AssetStudio/Math/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ public override int GetHashCode()

public override bool Equals(object other)
{
if (!(other is Vector3))
if (!(other is Vector3 vector3))
return false;
return Equals((Vector3)other);
return Equals(vector3);
}

public bool Equals(Vector3 other)
Expand Down Expand Up @@ -79,7 +79,7 @@ public void Normalize()

public float Length()
{
return (float)Math.Sqrt(LengthSquared());
return MathF.Sqrt(LengthSquared());
}

public float LengthSquared()
Expand Down
6 changes: 3 additions & 3 deletions AssetStudio/Math/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public override int GetHashCode()

public override bool Equals(object other)
{
if (!(other is Vector4))
if (!(other is Vector4 vector4))
return false;
return Equals((Vector4)other);
return Equals(vector4);
}

public bool Equals(Vector4 other)
Expand Down Expand Up @@ -93,7 +93,7 @@ public void Normalize()

public float Length()
{
return (float)Math.Sqrt(LengthSquared());
return MathF.Sqrt(LengthSquared());
}

public float LengthSquared()
Expand Down
2 changes: 1 addition & 1 deletion AssetStudioGUI/AssetStudioGUIForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@ private void PreviewMesh(Mesh m_Mesh)
{
if (m_Mesh.m_VertexCount > 0)
{
viewMatrixData = Matrix4.CreateRotationY(-(float)Math.PI / 4) * Matrix4.CreateRotationX(-(float)Math.PI / 6);
viewMatrixData = Matrix4.CreateRotationY(-MathF.PI / 4) * Matrix4.CreateRotationX(-MathF.PI / 6);
#region Vertices
if (m_Mesh.m_Vertices == null || m_Mesh.m_Vertices.Length == 0)
{
Expand Down
8 changes: 4 additions & 4 deletions AssetStudioUtility/SpriteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ private static Image<Bgra32> CutImage(Sprite m_Sprite, Texture2D m_Texture2D, Re
var height = (int)(m_Texture2D.m_Height / downscaleMultiplier);
originalImage.Mutate(x => x.Resize(width, height));
}
var rectX = (int)Math.Floor(textureRect.x);
var rectY = (int)Math.Floor(textureRect.y);
var rectRight = (int)Math.Ceiling(textureRect.x + textureRect.width);
var rectBottom = (int)Math.Ceiling(textureRect.y + textureRect.height);
var rectX = (int)MathF.Floor(textureRect.x);
var rectY = (int)MathF.Floor(textureRect.y);
var rectRight = (int)MathF.Ceiling(textureRect.x + textureRect.width);
var rectBottom = (int)MathF.Ceiling(textureRect.y + textureRect.height);
rectRight = Math.Min(rectRight, originalImage.Width);
rectBottom = Math.Min(rectBottom, originalImage.Height);
var rect = new Rectangle(rectX, rectY, rectRight - rectX, rectBottom - rectY);
Expand Down
36 changes: 18 additions & 18 deletions AssetStudioUtility/Texture2DConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ private bool DecodeRHalf(byte[] image_data, byte[] buff)
{
buff[i] = 0;
buff[i + 1] = 0;
buff[i + 2] = (byte)Math.Round(Half.ToHalf(image_data, i / 2) * 255f);
buff[i + 2] = (byte)MathF.Round(Half.ToHalf(image_data, i / 2) * 255f);
buff[i + 3] = 255;
}
return true;
Expand All @@ -451,8 +451,8 @@ private bool DecodeRGHalf(byte[] image_data, byte[] buff)
for (var i = 0; i < outPutDataSize; i += 4)
{
buff[i] = 0;
buff[i + 1] = (byte)Math.Round(Half.ToHalf(image_data, i + 2) * 255f);
buff[i + 2] = (byte)Math.Round(Half.ToHalf(image_data, i) * 255f);
buff[i + 1] = (byte)MathF.Round(Half.ToHalf(image_data, i + 2) * 255f);
buff[i + 2] = (byte)MathF.Round(Half.ToHalf(image_data, i) * 255f);
buff[i + 3] = 255;
}
return true;
Expand All @@ -462,10 +462,10 @@ private bool DecodeRGBAHalf(byte[] image_data, byte[] buff)
{
for (var i = 0; i < outPutDataSize; i += 4)
{
buff[i] = (byte)Math.Round(Half.ToHalf(image_data, i * 2 + 4) * 255f);
buff[i + 1] = (byte)Math.Round(Half.ToHalf(image_data, i * 2 + 2) * 255f);
buff[i + 2] = (byte)Math.Round(Half.ToHalf(image_data, i * 2) * 255f);
buff[i + 3] = (byte)Math.Round(Half.ToHalf(image_data, i * 2 + 6) * 255f);
buff[i] = (byte)MathF.Round(Half.ToHalf(image_data, i * 2 + 4) * 255f);
buff[i + 1] = (byte)MathF.Round(Half.ToHalf(image_data, i * 2 + 2) * 255f);
buff[i + 2] = (byte)MathF.Round(Half.ToHalf(image_data, i * 2) * 255f);
buff[i + 3] = (byte)MathF.Round(Half.ToHalf(image_data, i * 2 + 6) * 255f);
}
return true;
}
Expand All @@ -476,7 +476,7 @@ private bool DecodeRFloat(byte[] image_data, byte[] buff)
{
buff[i] = 0;
buff[i + 1] = 0;
buff[i + 2] = (byte)Math.Round(BitConverter.ToSingle(image_data, i) * 255f);
buff[i + 2] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i) * 255f);
buff[i + 3] = 255;
}
return true;
Expand All @@ -487,8 +487,8 @@ private bool DecodeRGFloat(byte[] image_data, byte[] buff)
for (var i = 0; i < outPutDataSize; i += 4)
{
buff[i] = 0;
buff[i + 1] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 2 + 4) * 255f);
buff[i + 2] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 2) * 255f);
buff[i + 1] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 2 + 4) * 255f);
buff[i + 2] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 2) * 255f);
buff[i + 3] = 255;
}
return true;
Expand All @@ -498,10 +498,10 @@ private bool DecodeRGBAFloat(byte[] image_data, byte[] buff)
{
for (var i = 0; i < outPutDataSize; i += 4)
{
buff[i] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 4 + 8) * 255f);
buff[i + 1] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 4 + 4) * 255f);
buff[i + 2] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 4) * 255f);
buff[i + 3] = (byte)Math.Round(BitConverter.ToSingle(image_data, i * 4 + 12) * 255f);
buff[i] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 4 + 8) * 255f);
buff[i + 1] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 4 + 4) * 255f);
buff[i + 2] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 4) * 255f);
buff[i + 3] = (byte)MathF.Round(BitConverter.ToSingle(image_data, i * 4 + 12) * 255f);
}
return true;
}
Expand Down Expand Up @@ -548,13 +548,13 @@ private bool DecodeRGB9e5Float(byte[] image_data, byte[] buff)
{
var n = BitConverter.ToInt32(image_data, i);
var scale = n >> 27 & 0x1f;
var scalef = Math.Pow(2, scale - 24);
var scalef = MathF.Pow(2, scale - 24);
var b = n >> 18 & 0x1ff;
var g = n >> 9 & 0x1ff;
var r = n & 0x1ff;
buff[i] = (byte)Math.Round(b * scalef * 255f);
buff[i + 1] = (byte)Math.Round(g * scalef * 255f);
buff[i + 2] = (byte)Math.Round(r * scalef * 255f);
buff[i] = (byte)MathF.Round(b * scalef * 255f);
buff[i + 1] = (byte)MathF.Round(g * scalef * 255f);
buff[i + 2] = (byte)MathF.Round(r * scalef * 255f);
buff[i + 3] = 255;
}
return true;
Expand Down

0 comments on commit 185348d

Please sign in to comment.