Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #7 from SixLabors/MathF
Browse files Browse the repository at this point in the history
Use MathF for .Net Core 2.0
  • Loading branch information
JimBobSquarePants committed Nov 17, 2017
2 parents f68740f + f6cd4db commit 7029444
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 120 deletions.
2 changes: 1 addition & 1 deletion build.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if not "%GitVersion_NuGetVersion%" == "" (
)
if not "%errorlevel%"=="0" goto failure

dotnet test ./tests/SixLabors.Core.Tests/SixLabors.Primitives.Tests.csproj
dotnet test ./tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj


if not "%GitVersion_NuGetVersion%" == "" (
Expand Down
74 changes: 7 additions & 67 deletions src/SixLabors.Core/MathF.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;

namespace SixLabors
#if NETCOREAPP2_0
[assembly: TypeForwardedTo(typeof(System.MathF))]
#else
namespace System
{
/// <summary>
/// Provides single-precision floating point constants and static methods for trigonometric, logarithmic, and other common mathematical functions.
/// </summary>
/// <remarks>MathF emulation on platforms that don't support it natively.</remarks>
// ReSharper disable InconsistentNaming
internal static class MathF
{
Expand Down Expand Up @@ -84,19 +87,6 @@ public static float Cos(float f)
return (float)Math.Cos(f);
}

/// <summary>
/// Converts a degree (360-periodic) angle to a radian (2*Pi-periodic) angle.
/// </summary>
/// <param name="degree">The angle in degrees.</param>
/// <returns>
/// The <see cref="float"/> representing the degree as radians.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToRadians(float degree)
{
return degree * (PI / 180F);
}

/// <summary>
/// Returns e raised to the specified power.
/// </summary>
Expand Down Expand Up @@ -171,19 +161,6 @@ public static float Pow(float x, float y)
return (float)Math.Pow(x, y);
}

/// <summary>
/// Converts a radian (2*Pi-periodic) angle to a degree (360-periodic) angle.
/// </summary>
/// <param name="radian">The angle in radians.</param>
/// <returns>
/// The <see cref="float"/> representing the degree as radians.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ToDegree(float radian)
{
return radian / (PI / 180F);
}

/// <summary>
/// Rounds a single-precision floating-point value to the nearest integral value.
/// </summary>
Expand Down Expand Up @@ -233,26 +210,6 @@ public static float Sin(float f)
return (float)Math.Sin(f);
}

/// <summary>
/// Returns the result of a normalized sine cardinal function for the given value.
/// SinC(x) = sin(pi*x)/(pi*x).
/// </summary>
/// <param name="f">A single-precision floating-point number to calculate the result for.</param>
/// <returns>
/// The sine cardinal of <paramref name="f" />.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float SinC(float f)
{
if (Abs(f) > Constants.Epsilon)
{
f *= PI;
return Clean(Sin(f) / f);
}

return 1F;
}

/// <summary>
/// Returns the square root of a specified number.
/// </summary>
Expand All @@ -269,23 +226,6 @@ public static float Sqrt(float f)
{
return (float)Math.Sqrt(f);
}

/// <summary>
/// Ensures that any passed float is correctly rounded to zero
/// </summary>
/// <param name="x">The value to clean.</param>
/// <returns>
/// The <see cref="float"/>
/// </returns>.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static float Clean(float x)
{
if (Abs(x) < Constants.Epsilon)
{
return 0F;
}

return x;
}
}
}
}
#endif
40 changes: 40 additions & 0 deletions src/SixLabors.Core/MathFExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;

namespace SixLabors
{
/// <summary>
/// Provides common mathematical methods.
/// </summary>
internal static class MathFExtensions
{
/// <summary>
/// Converts a degree (360-periodic) angle to a radian (2*Pi-periodic) angle.
/// </summary>
/// <param name="degree">The angle in degrees.</param>
/// <returns>
/// The <see cref="float"/> representing the degree as radians.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float DegreeToRadian(float degree)
{
return degree * (MathF.PI / 180F);
}

/// <summary>
/// Converts a radian (2*Pi-periodic) angle to a degree (360-periodic) angle.
/// </summary>
/// <param name="radian">The angle in radians.</param>
/// <returns>
/// The <see cref="float"/> representing the degree as radians.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float RadianToDegree(float radian)
{
return radian / (MathF.PI / 180F);
}
}
}
18 changes: 9 additions & 9 deletions src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public static class Matrix3x2Extensions
public static Matrix3x2 CreateScale(float scale, PointF centerPoint) => Matrix3x2.CreateScale(scale, centerPoint);

/// <summary>
/// Creates a skew matrix from the given angles in radians.
/// Creates a skew matrix from the given angles in degrees.
/// </summary>
/// <param name="degreesX">The X angle, in degrees.</param>
/// <param name="degreesY">The Y angle, in degrees.</param>
/// <returns>A skew matrix.</returns>
public static Matrix3x2 CreateSkewDegrees(float degreesX, float degreesY) => Matrix3x2.CreateSkew(MathF.ToRadians(degreesX), MathF.ToRadians(degreesY));
public static Matrix3x2 CreateSkewDegrees(float degreesX, float degreesY) => Matrix3x2.CreateSkew(MathFExtensions.DegreeToRadian(degreesX), MathFExtensions.DegreeToRadian(degreesY));

/// <summary>
/// Creates a skew matrix from the given angles in radians and a center point.
Expand All @@ -67,20 +67,20 @@ public static class Matrix3x2Extensions
public static Matrix3x2 CreateSkew(float radiansX, float radiansY, PointF centerPoint) => Matrix3x2.CreateSkew(radiansX, radiansY, centerPoint);

/// <summary>
/// Creates a skew matrix from the given angles in radians and a center point.
/// Creates a skew matrix from the given angles in degrees and a center point.
/// </summary>
/// <param name="degreesX">The X angle, in degrees.</param>
/// <param name="degreesY">The Y angle, in degrees.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>A skew matrix.</returns>
public static Matrix3x2 CreateSkewDegrees(float degreesX, float degreesY, PointF centerPoint) => Matrix3x2.CreateSkew(MathF.ToRadians(degreesX), MathF.ToRadians(degreesY), centerPoint);
public static Matrix3x2 CreateSkewDegrees(float degreesX, float degreesY, PointF centerPoint) => Matrix3x2.CreateSkew(MathFExtensions.DegreeToRadian(degreesX), MathFExtensions.DegreeToRadian(degreesY), centerPoint);

/// <summary>
/// Creates a rotation matrix using the given rotation in radians.
/// Creates a rotation matrix using the given rotation in degrees.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <returns>A rotation matrix.</returns>
public static Matrix3x2 CreateRotationDegrees(float degrees) => Matrix3x2.CreateRotation(MathF.ToRadians(degrees));
public static Matrix3x2 CreateRotationDegrees(float degrees) => Matrix3x2.CreateRotation(MathFExtensions.DegreeToRadian(degrees));

/// <summary>
/// Creates a rotation matrix using the given rotation in radians and a center point.
Expand All @@ -91,11 +91,11 @@ public static class Matrix3x2Extensions
public static Matrix3x2 CreateRotation(float radians, PointF centerPoint) => Matrix3x2.CreateRotation(radians, centerPoint);

/// <summary>
/// Creates a rotation matrix using the given rotation in radians and a center point.
/// Creates a rotation matrix using the given rotation in degrees and a center point.
/// </summary>
/// <param name="degrees">The amount of rotation, in degrees.</param>
/// <param name="centerPoint">The center point.</param>
/// <returns>A rotation matrix.</returns>
public static Matrix3x2 CreateRotationDegrees(float degrees, PointF centerPoint) => Matrix3x2.CreateRotation(MathF.ToRadians(degrees), centerPoint);
public static Matrix3x2 CreateRotationDegrees(float degrees, PointF centerPoint) => Matrix3x2.CreateRotation(MathFExtensions.DegreeToRadian(degrees), centerPoint);
}
}
}
85 changes: 43 additions & 42 deletions src/SixLabors.Core/SixLabors.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Low level primitives for use across Six Labors projects..</Description>
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix>
<Authors>Six Labors</Authors>
<TargetFramework>netstandard1.1</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>SixLabors.Core</AssemblyName>
<PackageId>SixLabors.Core</PackageId>
<PackageTags>rectangle;point;size,primitives</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<DebugType Condition="$(codecov) == 'true'">full</DebugType>
<RootNamespace>SixLabors</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<Description>Low level primitives for use across Six Labors projects..</Description>
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix>
<Authors>Six Labors</Authors>
<TargetFrameworks>netstandard1.1;netcoreapp2.0;</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<AssemblyName>SixLabors.Core</AssemblyName>
<PackageId>SixLabors.Core</PackageId>
<PackageTags>rectangle;point;size,primitives</PackageTags>
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl>
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl>
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<DebugType Condition="$(codecov) == 'true'">full</DebugType>
<RootNamespace>SixLabors</RootNamespace>
</PropertyGroup>

<PropertyGroup>
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup>
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1'">
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
</ItemGroup>
</Project>
59 changes: 59 additions & 0 deletions tests/SixLabors.Core.Tests/Helpers/FloatRoundingComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.

using System;
using System.Collections.Generic;
using System.Numerics;

namespace SixLabors.Tests.Helpers
{
/// <summary>
/// Allows the comparison of single-precision floating point values by precision.
/// </summary>
public struct FloatRoundingComparer : IEqualityComparer<float>, IEqualityComparer<Vector4>
{
/// <summary>
/// Initializes a new instance of the <see cref="FloatRoundingComparer"/> struct.
/// </summary>
/// <param name="precision">The number of decimal places (valid values: 0-7)</param>
public FloatRoundingComparer(int precision)
{
Guard.MustBeBetweenOrEqualTo(precision, 0, 7, nameof(precision));
this.Precision = precision;
}

/// <summary>
/// Gets the number of decimal places (valid values: 0-7)
/// </summary>
public int Precision { get; }

/// <inheritdoc />
public bool Equals(float x, float y)
{
float xp = (float)Math.Round(x, this.Precision, MidpointRounding.AwayFromZero);
float yp = (float)Math.Round(y, this.Precision, MidpointRounding.AwayFromZero);

return Comparer<float>.Default.Compare(xp, yp) == 0;
}

/// <inheritdoc />
public bool Equals(Vector4 x, Vector4 y)
{
return this.Equals(x.X, y.X) && this.Equals(x.Y, y.Y) && this.Equals(x.Z, y.Z) && this.Equals(x.W, y.W);
}

/// <inheritdoc />
public int GetHashCode(float obj)
{
unchecked
{
int hashCode = obj.GetHashCode();
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode();
return hashCode;
}
}

/// <inheritdoc />
public int GetHashCode(Vector4 obj) => HashHelpers.Combine(obj.GetHashCode(), this.Precision.GetHashCode());
}
}
Loading

0 comments on commit 7029444

Please sign in to comment.