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

Commit 7029444

Browse files
Merge pull request #7 from SixLabors/MathF
Use MathF for .Net Core 2.0
2 parents f68740f + f6cd4db commit 7029444

File tree

9 files changed

+268
-120
lines changed

9 files changed

+268
-120
lines changed

build.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if not "%GitVersion_NuGetVersion%" == "" (
1414
)
1515
if not "%errorlevel%"=="0" goto failure
1616

17-
dotnet test ./tests/SixLabors.Core.Tests/SixLabors.Primitives.Tests.csproj
17+
dotnet test ./tests/SixLabors.Core.Tests/SixLabors.Core.Tests.csproj
1818

1919

2020
if not "%GitVersion_NuGetVersion%" == "" (

src/SixLabors.Core/MathF.cs

Lines changed: 7 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
// Copyright (c) Six Labors and contributors.
22
// Licensed under the Apache License, Version 2.0.
33

4-
using System;
54
using System.Runtime.CompilerServices;
65

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

87-
/// <summary>
88-
/// Converts a degree (360-periodic) angle to a radian (2*Pi-periodic) angle.
89-
/// </summary>
90-
/// <param name="degree">The angle in degrees.</param>
91-
/// <returns>
92-
/// The <see cref="float"/> representing the degree as radians.
93-
/// </returns>
94-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
95-
public static float ToRadians(float degree)
96-
{
97-
return degree * (PI / 180F);
98-
}
99-
10090
/// <summary>
10191
/// Returns e raised to the specified power.
10292
/// </summary>
@@ -171,19 +161,6 @@ public static float Pow(float x, float y)
171161
return (float)Math.Pow(x, y);
172162
}
173163

174-
/// <summary>
175-
/// Converts a radian (2*Pi-periodic) angle to a degree (360-periodic) angle.
176-
/// </summary>
177-
/// <param name="radian">The angle in radians.</param>
178-
/// <returns>
179-
/// The <see cref="float"/> representing the degree as radians.
180-
/// </returns>
181-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
182-
public static float ToDegree(float radian)
183-
{
184-
return radian / (PI / 180F);
185-
}
186-
187164
/// <summary>
188165
/// Rounds a single-precision floating-point value to the nearest integral value.
189166
/// </summary>
@@ -233,26 +210,6 @@ public static float Sin(float f)
233210
return (float)Math.Sin(f);
234211
}
235212

236-
/// <summary>
237-
/// Returns the result of a normalized sine cardinal function for the given value.
238-
/// SinC(x) = sin(pi*x)/(pi*x).
239-
/// </summary>
240-
/// <param name="f">A single-precision floating-point number to calculate the result for.</param>
241-
/// <returns>
242-
/// The sine cardinal of <paramref name="f" />.
243-
/// </returns>
244-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
245-
public static float SinC(float f)
246-
{
247-
if (Abs(f) > Constants.Epsilon)
248-
{
249-
f *= PI;
250-
return Clean(Sin(f) / f);
251-
}
252-
253-
return 1F;
254-
}
255-
256213
/// <summary>
257214
/// Returns the square root of a specified number.
258215
/// </summary>
@@ -269,23 +226,6 @@ public static float Sqrt(float f)
269226
{
270227
return (float)Math.Sqrt(f);
271228
}
272-
273-
/// <summary>
274-
/// Ensures that any passed float is correctly rounded to zero
275-
/// </summary>
276-
/// <param name="x">The value to clean.</param>
277-
/// <returns>
278-
/// The <see cref="float"/>
279-
/// </returns>.
280-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
281-
private static float Clean(float x)
282-
{
283-
if (Abs(x) < Constants.Epsilon)
284-
{
285-
return 0F;
286-
}
287-
288-
return x;
289-
}
290229
}
291-
}
230+
}
231+
#endif
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace SixLabors
8+
{
9+
/// <summary>
10+
/// Provides common mathematical methods.
11+
/// </summary>
12+
internal static class MathFExtensions
13+
{
14+
/// <summary>
15+
/// Converts a degree (360-periodic) angle to a radian (2*Pi-periodic) angle.
16+
/// </summary>
17+
/// <param name="degree">The angle in degrees.</param>
18+
/// <returns>
19+
/// The <see cref="float"/> representing the degree as radians.
20+
/// </returns>
21+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
22+
public static float DegreeToRadian(float degree)
23+
{
24+
return degree * (MathF.PI / 180F);
25+
}
26+
27+
/// <summary>
28+
/// Converts a radian (2*Pi-periodic) angle to a degree (360-periodic) angle.
29+
/// </summary>
30+
/// <param name="radian">The angle in radians.</param>
31+
/// <returns>
32+
/// The <see cref="float"/> representing the degree as radians.
33+
/// </returns>
34+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35+
public static float RadianToDegree(float radian)
36+
{
37+
return radian / (MathF.PI / 180F);
38+
}
39+
}
40+
}

src/SixLabors.Core/Primitives/Matrix3x2Extensions.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ public static class Matrix3x2Extensions
5050
public static Matrix3x2 CreateScale(float scale, PointF centerPoint) => Matrix3x2.CreateScale(scale, centerPoint);
5151

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

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

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

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

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

9393
/// <summary>
94-
/// Creates a rotation matrix using the given rotation in radians and a center point.
94+
/// Creates a rotation matrix using the given rotation in degrees and a center point.
9595
/// </summary>
9696
/// <param name="degrees">The amount of rotation, in degrees.</param>
9797
/// <param name="centerPoint">The center point.</param>
9898
/// <returns>A rotation matrix.</returns>
99-
public static Matrix3x2 CreateRotationDegrees(float degrees, PointF centerPoint) => Matrix3x2.CreateRotation(MathF.ToRadians(degrees), centerPoint);
99+
public static Matrix3x2 CreateRotationDegrees(float degrees, PointF centerPoint) => Matrix3x2.CreateRotation(MathFExtensions.DegreeToRadian(degrees), centerPoint);
100100
}
101-
}
101+
}
Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,49 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

3-
<PropertyGroup>
4-
<Description>Low level primitives for use across Six Labors projects..</Description>
5-
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
6-
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix>
7-
<Authors>Six Labors</Authors>
8-
<TargetFramework>netstandard1.1</TargetFramework>
9-
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10-
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11-
<AssemblyName>SixLabors.Core</AssemblyName>
12-
<PackageId>SixLabors.Core</PackageId>
13-
<PackageTags>rectangle;point;size,primitives</PackageTags>
14-
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl>
15-
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl>
16-
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
17-
<RepositoryType>git</RepositoryType>
18-
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl>
19-
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
20-
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
21-
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
22-
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
23-
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
24-
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
25-
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
26-
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
27-
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
28-
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
29-
<DebugType Condition="$(codecov) == 'true'">full</DebugType>
30-
<RootNamespace>SixLabors</RootNamespace>
31-
</PropertyGroup>
3+
<PropertyGroup>
4+
<Description>Low level primitives for use across Six Labors projects..</Description>
5+
<VersionPrefix Condition="$(packageversion) != ''">$(packageversion)</VersionPrefix>
6+
<VersionPrefix Condition="$(packageversion) == ''">0.1.0-alpha2</VersionPrefix>
7+
<Authors>Six Labors</Authors>
8+
<TargetFrameworks>netstandard1.1;netcoreapp2.0;</TargetFrameworks>
9+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
10+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
11+
<AssemblyName>SixLabors.Core</AssemblyName>
12+
<PackageId>SixLabors.Core</PackageId>
13+
<PackageTags>rectangle;point;size,primitives</PackageTags>
14+
<PackageIconUrl>https://raw.githubusercontent.com/SixLabors/Home/master/logo.png</PackageIconUrl>
15+
<PackageProjectUrl>https://github.com/SixLabors/Core</PackageProjectUrl>
16+
<PackageLicenseUrl>http://www.apache.org/licenses/LICENSE-2.0</PackageLicenseUrl>
17+
<RepositoryType>git</RepositoryType>
18+
<RepositoryUrl>https://github.com/SixLabors/Core</RepositoryUrl>
19+
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
20+
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
21+
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
22+
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
23+
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
24+
<GenerateNeutralResourcesLanguageAttribute>false</GenerateNeutralResourcesLanguageAttribute>
25+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
26+
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
27+
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
28+
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
29+
<DebugType Condition="$(codecov) == 'true'">full</DebugType>
30+
<RootNamespace>SixLabors</RootNamespace>
31+
</PropertyGroup>
3232

33-
<PropertyGroup>
34-
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet>
35-
</PropertyGroup>
33+
<PropertyGroup>
34+
<CodeAnalysisRuleSet>..\SixLabors.ruleset</CodeAnalysisRuleSet>
35+
</PropertyGroup>
3636

37-
<ItemGroup>
38-
<AdditionalFiles Include="..\..\stylecop.json" />
39-
</ItemGroup>
40-
41-
<ItemGroup>
42-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
43-
<PrivateAssets>All</PrivateAssets>
44-
</PackageReference>
45-
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
46-
</ItemGroup>
37+
<ItemGroup>
38+
<AdditionalFiles Include="..\..\stylecop.json" />
39+
</ItemGroup>
4740

41+
<ItemGroup>
42+
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
43+
<PrivateAssets>All</PrivateAssets>
44+
</PackageReference>
45+
</ItemGroup>
46+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.1'">
47+
<PackageReference Include="System.Numerics.Vectors" Version="4.4.0" />
48+
</ItemGroup>
4849
</Project>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Six Labors and contributors.
2+
// Licensed under the Apache License, Version 2.0.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Numerics;
7+
8+
namespace SixLabors.Tests.Helpers
9+
{
10+
/// <summary>
11+
/// Allows the comparison of single-precision floating point values by precision.
12+
/// </summary>
13+
public struct FloatRoundingComparer : IEqualityComparer<float>, IEqualityComparer<Vector4>
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="FloatRoundingComparer"/> struct.
17+
/// </summary>
18+
/// <param name="precision">The number of decimal places (valid values: 0-7)</param>
19+
public FloatRoundingComparer(int precision)
20+
{
21+
Guard.MustBeBetweenOrEqualTo(precision, 0, 7, nameof(precision));
22+
this.Precision = precision;
23+
}
24+
25+
/// <summary>
26+
/// Gets the number of decimal places (valid values: 0-7)
27+
/// </summary>
28+
public int Precision { get; }
29+
30+
/// <inheritdoc />
31+
public bool Equals(float x, float y)
32+
{
33+
float xp = (float)Math.Round(x, this.Precision, MidpointRounding.AwayFromZero);
34+
float yp = (float)Math.Round(y, this.Precision, MidpointRounding.AwayFromZero);
35+
36+
return Comparer<float>.Default.Compare(xp, yp) == 0;
37+
}
38+
39+
/// <inheritdoc />
40+
public bool Equals(Vector4 x, Vector4 y)
41+
{
42+
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);
43+
}
44+
45+
/// <inheritdoc />
46+
public int GetHashCode(float obj)
47+
{
48+
unchecked
49+
{
50+
int hashCode = obj.GetHashCode();
51+
hashCode = (hashCode * 397) ^ this.Precision.GetHashCode();
52+
return hashCode;
53+
}
54+
}
55+
56+
/// <inheritdoc />
57+
public int GetHashCode(Vector4 obj) => HashHelpers.Combine(obj.GetHashCode(), this.Precision.GetHashCode());
58+
}
59+
}

0 commit comments

Comments
 (0)