Skip to content

Commit

Permalink
Merge pull request #35 from MagmaWorks/bugfix/perimter-from-perimeter
Browse files Browse the repository at this point in the history
Handle IPerimeter from Perimeter and Perimeter tests
  • Loading branch information
kpne authored Oct 14, 2024
2 parents ee22202 + 61b5efd commit da06b29
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Profiles/Perimeter/Perimeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class Perimeter : IPerimeter
public ILocalPolygon2d OuterEdge { get; set; }
public IList<ILocalPolygon2d> VoidEdges { get; set; }

private Perimeter() { }

public Perimeter(ILocalPolygon2d edge)
{
OuterEdge = edge;
Expand Down
7 changes: 6 additions & 1 deletion Profiles/Perimeter/PerimeterFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ public static partial class PerimeterFactory
{
public static IPerimeter CreatePerimeter(IProfile profile)
{
if (profile is IPerimeter perimeter)
{
return perimeter;
}

int divisions = 8;
Length tolerance = new Length(10, LengthUnit.Millimeter);
Length diameter = Length.Zero;
Expand Down Expand Up @@ -82,7 +87,7 @@ public static IPerimeter CreatePerimeter(IProfile profile)
case IZ z:
return CreateZ(z);

default: throw new ArgumentNullException(nameof(profile));
default: throw new ArgumentException($"Unable to convert type {profile.GetType().Name} to IPerimeter");
}
}

Expand Down
135 changes: 135 additions & 0 deletions Profiles/ProfileTests/PerimeterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
using MagmaWorks.Geometry;
using MagmaWorks.Taxonomy.Profiles;
using MagmaWorks.Taxonomy.Serialization.Profiles.Extensions;
using OasysUnits;
using OasysUnits.Units;

namespace ProfileTests
{
public class PerimeterTests
{
[Fact]
public void CreateProfileTest()
{
// Assemble
LengthUnit unit = LengthUnit.Centimeter;
var solidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(-50, unit)},
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(-50, unit)},
});
var voidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(-15, unit)},
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(-15, unit)},
});

// Act
IPerimeter profile = new Perimeter(solidEdge, new List<ILocalPolygon2d>() { voidEdge });

// Assert
Assert.Single(profile.VoidEdges);
Assert.Equal(4, profile.OuterEdge.Points.Count);
Assert.Equal(4, profile.VoidEdges[0].Points.Count);
}

[Fact]
public void InterfaceSurvivesRoundtripDeserializationTest()
{
// Assemble
LengthUnit unit = LengthUnit.Centimeter;
var solidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(-50, unit)},
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(-50, unit)},
});
var voidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(-15, unit)},
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(-15, unit)},
});

// Act
IPerimeter prfl = new Perimeter(solidEdge, new List<ILocalPolygon2d>() { voidEdge });
string json = prfl.ToJson();
IPerimeter prflDeserialized = json.FromJson<IPerimeter>();

// Assert
Assert.Single(prflDeserialized.VoidEdges);
Assert.Equal(4, prflDeserialized.OuterEdge.Points.Count);
Assert.Equal(4, prflDeserialized.VoidEdges[0].Points.Count);
Assert.Equivalent(prfl, prflDeserialized);
}

[Fact]
public void ConcreteImplementationSurvivesRoundtripDeserializationTest()
{
// Assemble
LengthUnit unit = LengthUnit.Centimeter;
var solidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(-50, unit)},
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(-50, unit)},
});
var voidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(-15, unit)},
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(-15, unit)},
});

// Act
IPerimeter prfl = new Perimeter(solidEdge, new List<ILocalPolygon2d>() { voidEdge });
string json = prfl.ToJson();
IPerimeter prflDeserialized = json.FromJson<Perimeter>();

// Assert
Assert.Single(prflDeserialized.VoidEdges);
Assert.Equal(4, prflDeserialized.OuterEdge.Points.Count);
Assert.Equal(4, prflDeserialized.VoidEdges[0].Points.Count);
Assert.Equivalent(prfl, prflDeserialized);
}

[Fact]
public void GetPerimeterTest()
{
// Assemble
LengthUnit unit = LengthUnit.Centimeter;
var solidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(50, unit)},
new LocalPoint2d() { Y = new Length(-25, unit), Z = new Length(-50, unit)},
new LocalPoint2d() { Y = new Length(25, unit), Z = new Length(-50, unit)},
});
var voidEdge = new LocalPolygon2d(new List<ILocalPoint2d>()
{
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(15, unit)},
new LocalPoint2d() { Y = new Length(-10, unit), Z = new Length(-15, unit)},
new LocalPoint2d() { Y = new Length(10, unit), Z = new Length(-15, unit)},
});

// Act
IPerimeter prfl = new Perimeter(solidEdge, new List<ILocalPolygon2d>() { voidEdge });
IPerimeter perimeter = new Perimeter(prfl);

// Assert
Assert.Single(perimeter.VoidEdges);
Assert.Equal(4, perimeter.OuterEdge.Points.Count);
Assert.Equal(4, perimeter.VoidEdges[0].Points.Count);
Assert.Equivalent(prfl, perimeter);
}
}
}

0 comments on commit da06b29

Please sign in to comment.