adding tests for simplify

This commit is contained in:
Lars Brubaker 2019-04-22 14:15:55 -07:00
parent d5959d1ace
commit 86dd918963
2 changed files with 126 additions and 1 deletions

View file

@ -429,6 +429,11 @@ namespace MatterHackers.MatterControl.DesignTools
this.Z = z;
this.Bottom = bottom;
}
public override string ToString()
{
return $"Z={Z:0.###} Bottom={Bottom}";
}
}
public class HitPlanes : List<HitPlane>
@ -440,6 +445,58 @@ namespace MatterHackers.MatterControl.DesignTools
this.minimumSupportHeight = minimumSupportHeight;
}
/// <summary>
/// Modify the list to have Bottom <-> Top, Bottom <-> Top items exactly.
/// Remove any internal Planes that are not required. This may reduce the set to no items.
/// </summary>
public void Simplify()
{
// sort the list on Z
this.Sort((a, b) =>
{
return a.Z.CompareTo(b.Z);
});
// remove items until the first item is a bottom
while (Count > 0 && !this[0].Bottom)
{
this.RemoveAt(0);
}
// remove any items that are between a bottom and a top
int currentBottom = 0;
while (Count > currentBottom)
{
var top = GetNextTop(currentBottom);
if (top != -1)
{
// remove everything between the top and the bottom
for (int i = top - 1; i > currentBottom; i--)
{
this.RemoveAt(i);
}
// move the bottom up past the current top
currentBottom += 2;
}
else // there is not a top above this bottom
{
// remove the bottom
this.RemoveAt(currentBottom);
break;
}
}
}
public void Merge(HitPlanes other)
{
this.Simplify();
other.Simplify();
// now both lists are only start->end, start->end
// merge them, considering minimumSupportHeight
}
public int GetNextBottom(int i)
{
HitPlanes planes = this;
@ -472,10 +529,12 @@ namespace MatterHackers.MatterControl.DesignTools
return -1;
}
public int GetNextTop(int i)
public int GetNextTop(int start)
{
HitPlanes planes = this;
var i = start;
if (!planes[i].Bottom)
{
// skip the one we are
@ -502,6 +561,17 @@ namespace MatterHackers.MatterControl.DesignTools
}
else // move up to the next plane and re-evaluate
{
// if we started on a bottom
// and we are the last top
// and we are far enough away from the start bottom
if (this[start].Bottom
&& i == this.Count - 1
&& this[i].Z - this[start].Z > minimumSupportHeight)
{
// we are on the last top of the part and have move up from some other part
return i;
}
i++;
}
}

View file

@ -609,6 +609,61 @@ namespace MatterControl.Tests.MatterControl
int bottom = planes.GetNextBottom(0);
Assert.AreEqual(-1, bottom, "The boxes are sitting on the bed and no support is required");
}
// simplify working as expected
{
var planes = new SupportGenerator.HitPlanes(.1)
{
new SupportGenerator.HitPlane(0, false),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, false),
new SupportGenerator.HitPlane(0.0302, true),
new SupportGenerator.HitPlane(0.0497, true),
new SupportGenerator.HitPlane(0.762, true),
new SupportGenerator.HitPlane(0.762, true),
new SupportGenerator.HitPlane(0.762, false),
new SupportGenerator.HitPlane(0.762, false),
new SupportGenerator.HitPlane(15.95, false),
new SupportGenerator.HitPlane(15.9697, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(20, true),
new SupportGenerator.HitPlane(25, false),
};
planes.Simplify();
Assert.AreEqual(4, planes.Count, "After simplify there should be two ranges");
}
{
var planes = new SupportGenerator.HitPlanes(.1)
{
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, true),
new SupportGenerator.HitPlane(0, false),
new SupportGenerator.HitPlane(0.0302, true),
new SupportGenerator.HitPlane(0.0497, true),
new SupportGenerator.HitPlane(0.762, true),
new SupportGenerator.HitPlane(0.762, true),
new SupportGenerator.HitPlane(0.762, false),
new SupportGenerator.HitPlane(0.762, false),
new SupportGenerator.HitPlane(15.95, false),
new SupportGenerator.HitPlane(15.9697, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
new SupportGenerator.HitPlane(16, false),
};
planes.Simplify();
Assert.AreEqual(2, planes.Count, "After simplify there should one range");
}
}
}
}