Skip to content

Commit

Permalink
Added MathUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
Gericom committed Nov 4, 2014
1 parent f602386 commit 0b4ef18
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 23 deletions.
12 changes: 3 additions & 9 deletions 3DS/DSP/ADPCM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibEveryFileExplorer.Math;

namespace _3DS.DSP
{
Expand Down Expand Up @@ -39,25 +40,18 @@ public Int16[] GetWaveData(byte[] Data, int Offset, int Length)
if (high >= 8) high -= 16;
if (low >= 8) low -= 16;
double val = (((high * Scale) << 11) + 1024.0 + (Coef1 * Last1 + Coef2 * Last2)) / 2048.0; //>> 11;
short samp = Clamp((int)val, short.MinValue, short.MaxValue);
short samp = (short)MathUtil.Clamp((int)val, short.MinValue, short.MaxValue);
DataOut.Add(samp);
Last2 = Last1;
Last1 = val;
val = (((low * Scale) << 11) + 1024.0 + (Coef1 * Last1 + Coef2 * Last2)) / 2048.0;//>> 11;
samp = Clamp((int)val, short.MinValue, short.MaxValue);
samp = (short)MathUtil.Clamp((int)val, short.MinValue, short.MaxValue);
DataOut.Add(samp);
Last2 = Last1;
Last1 = val;
}
}
return DataOut.ToArray();
}

private static short Clamp(int value, int min, int max)
{
if (value < min) value = min;
if (value > max) value = max;
return (short)value;
}
}
}
2 changes: 1 addition & 1 deletion LibEveryFileExplorer/3D/Triangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Vector3 Normal
get
{
Vector3 a = (PointB - PointA).Cross(PointC - PointA);
return a / (float)Math.Sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z);
return a / (float)System.Math.Sqrt(a.X * a.X + a.Y * a.Y + a.Z * a.Z);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibEveryFileExplorer/Collections/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Vector2(float X, float Y)

public float Length
{
get { return (float)Math.Sqrt(X * X + Y * Y); }
get { return (float)System.Math.Sqrt(X * X + Y * Y); }
}

public void Normalize()
Expand Down
2 changes: 1 addition & 1 deletion LibEveryFileExplorer/Collections/Vector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Vector3(float X, float Y, float Z)

public float Length
{
get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z); }
get { return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z); }
}

public void Normalize()
Expand Down
2 changes: 1 addition & 1 deletion LibEveryFileExplorer/Collections/Vector4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Vector4(float X, float Y, float Z, float W)

public float Length
{
get { return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W); }
get { return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W); }
}

public void Normalize()
Expand Down
1 change: 1 addition & 0 deletions LibEveryFileExplorer/LibEveryFileExplorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<Compile Include="IO\EndianBinaryReader.cs" />
<Compile Include="IO\EndianBinaryWriter.cs" />
<Compile Include="IO\IOUtil.cs" />
<Compile Include="Math\MathUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Resource.Designer.cs">
<AutoGen>True</AutoGen>
Expand Down
27 changes: 27 additions & 0 deletions LibEveryFileExplorer/Math/MathUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibEveryFileExplorer.Math
{
public class MathUtil
{
public static int Clamp(int value, int min, int max)
{
if (value < min) value = min;
if (value > max) value = max;
return (short)value;
}

public static float RadToDeg(float Radians)
{
return Radians * (180f / (float)System.Math.PI);
}

public static double RadToDeg(double Radians)
{
return Radians * (180.0 / System.Math.PI);
}
}
}
3 changes: 2 additions & 1 deletion MarioKart/MKDS/NKMD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Tao.OpenGl;
using MarioKart.UI;
using LibEveryFileExplorer.GFX;
using LibEveryFileExplorer.Math;

namespace MarioKart.MKDS
{
Expand Down Expand Up @@ -433,7 +434,7 @@ public KTPJEntry(EndianBinaryReader er, UInt16 Version)
if (Version <= 34)
{
float yangle = (float)Math.Atan2(Rotation.X, Rotation.Z);
Rotation = new Vector3(0, /*MathHelper.RadiansToDegrees(yangle)*/ yangle * (180f / (float)Math.PI), 0);
Rotation = new Vector3(0, MathUtil.RadToDeg(yangle), 0);
}
EnemyPositionID = er.ReadInt16();
ItemPositionID = er.ReadInt16();
Expand Down
12 changes: 3 additions & 9 deletions NDS/SND/ADPCM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using LibEveryFileExplorer.IO;
using LibEveryFileExplorer.Math;

namespace NDS.SND
{
Expand Down Expand Up @@ -57,19 +58,12 @@ public Int16[] GetWaveData(byte[] Data, int Offset, int Length)
StepTable[Index] * ((val >> 2) & 1);

int samp = Last + diff * ((((val >> 3) & 1) == 1) ? -1 : 1);
Last = Clamp(samp, short.MinValue, short.MaxValue);
Index = Clamp(Index + IndexTable[val & 7], 0, 88);
Last = (short)MathUtil.Clamp(samp, short.MinValue, short.MaxValue);
Index = (short)MathUtil.Clamp(Index + IndexTable[val & 7], 0, 88);
DataOut.Add((short)Last);
}
}
return DataOut.ToArray();
}

private static short Clamp(int value, int min, int max)
{
if (value < min) value = min;
if (value > max) value = max;
return (short)value;
}
}
}

0 comments on commit 0b4ef18

Please sign in to comment.