Add tests for World* functions

This commit is contained in:
John Lewin 2018-02-04 16:31:23 -08:00
parent cd1c6c3269
commit a81d4dc5de
2 changed files with 289 additions and 1 deletions

View file

@ -38,6 +38,7 @@ using MatterHackers.MatterControl;
using MatterHackers.MatterControl.Library;
using MatterHackers.MatterControl.Tests.Automation;
using MatterHackers.MeshVisualizer;
using MatterHackers.VectorMath;
using Newtonsoft.Json;
using NUnit.Framework;
@ -162,6 +163,293 @@ namespace MatterHackers.PolygonMesh.UnitTests
Assert.AreEqual(1, Directory.GetFiles(Path.Combine(tempPath, "Assets")).Length, "Only 1 asset should exist");
}
private readonly int RootMaterialIndex = 1;
private readonly int SuperGroupMaterialIndex = 2;
private readonly int GroupMaterialIndex = 3;
private readonly int RedMaterialIndex = 4;
private readonly int GreenMaterialIndex = 5;
private readonly int BlueMaterialIndex = 6;
private readonly Matrix4X4 RootMatrix = Matrix4X4.Identity;
private readonly Matrix4X4 SuperGroupMatrix = Matrix4X4.CreateScale(2);
private readonly Matrix4X4 GroupMatrix = Matrix4X4.Identity;
private readonly Matrix4X4 RedMatrix = Matrix4X4.CreateTranslation(10, 0, 0);
private readonly Matrix4X4 GreenMatrix = Matrix4X4.CreateTranslation(15, 0, 0);
private readonly Matrix4X4 BlueMatrix = Matrix4X4.CreateTranslation(20, 0, 0);
private readonly PrintOutputTypes RootOutputType = PrintOutputTypes.Solid;
private readonly PrintOutputTypes SuperGroupOutputType = PrintOutputTypes.Hole;
private readonly PrintOutputTypes GroupOutputType = PrintOutputTypes.Solid;
private readonly PrintOutputTypes RedOutputType = PrintOutputTypes.Support;
private readonly PrintOutputTypes GreenOutputType = PrintOutputTypes.Support;
private readonly PrintOutputTypes BlueOutputType = PrintOutputTypes.Hole;
public InteractiveScene SampleScene()
{
Object3D group;
var scene = new InteractiveScene()
{
Color = Color.Black,
MaterialIndex = this.RootMaterialIndex,
OutputType = this.RootOutputType
};
var supergroup = new Object3D()
{
Name = "SuperGroup",
Color = Color.Violet,
MaterialIndex = this.SuperGroupMaterialIndex,
Matrix = this.SuperGroupMatrix,
OutputType = this.SuperGroupOutputType
};
scene.Children.Add(supergroup);
group = new Object3D()
{
Name = "GroupA",
Color = Color.Pink,
MaterialIndex = this.GroupMaterialIndex,
OutputType = this.GroupOutputType
};
supergroup.Children.Add(group);
group.Children.Add(new Object3D
{
Name = nameof(Color.Red),
Color = Color.Red,
MaterialIndex = this.RedMaterialIndex,
Matrix = this.RedMatrix,
OutputType = this.RedOutputType
});
group = new Object3D()
{
Name = "GroupB",
Color = Color.Pink,
MaterialIndex = this.GroupMaterialIndex,
OutputType = this.GroupOutputType
};
supergroup.Children.Add(group);
group.Children.Add(new Object3D
{
Name = nameof(Color.Green),
Color = Color.Green,
MaterialIndex = this.GreenMaterialIndex,
Matrix = this.GreenMatrix,
OutputType = this.GreenOutputType
});
group = new Object3D()
{
Name = "GroupB",
Color = Color.Pink,
MaterialIndex = this.GroupMaterialIndex,
OutputType = this.GroupOutputType
};
supergroup.Children.Add(group);
group.Children.Add(new Object3D
{
Name = nameof(Color.Blue),
Color = Color.Blue,
MaterialIndex = this.BlueMaterialIndex,
Matrix = this.BlueMatrix,
OutputType = this.BlueOutputType
});
return scene;
}
[Test]
public void WorldColorBasicTest()
{
var scene = SampleScene();
var superGroup = scene.Descendants().Where(d => d.Name == "SuperGroup").FirstOrDefault();
var redItem = scene.Descendants().Where(d => d.Name == nameof(Color.Red)).FirstOrDefault();
var greenItem = scene.Descendants().Where(d => d.Name == nameof(Color.Green)).FirstOrDefault();
var blueItem = scene.Descendants().Where(d => d.Name == nameof(Color.Blue)).FirstOrDefault();
// Validate root
Assert.AreEqual(Color.Black, scene.Color, "Color property on root should be Black");
Assert.AreEqual(Color.Black, scene.WorldColor(), "WorldColor on root should be Black");
// Validate red node
Assert.AreEqual(Color.Red, redItem.Color, "Color property on node should be Red");
Assert.AreEqual(Color.Pink, redItem.WorldColor(redItem.Parent), "WorldColor on Red up to parent node should be Pink");
Assert.AreEqual(Color.Violet, redItem.WorldColor(superGroup), "WorldColor on Red up to supergroup should be Violet");
// Validate green node
Assert.AreEqual(Color.Green, greenItem.Color, "Color property on node should be Green");
Assert.AreEqual(Color.Pink, greenItem.WorldColor(greenItem.Parent), "WorldColor on Green up to parent node should be Pink");
Assert.AreEqual(Color.Violet, greenItem.WorldColor(superGroup), "WorldColor on Green up to supergroup should be Violet");
// Validate green node
Assert.AreEqual(Color.Blue, blueItem.Color, "Color property on node should be Green");
Assert.AreEqual(Color.Pink, blueItem.WorldColor(blueItem.Parent), "WorldColor on Blue up to parent node should be Pink");
Assert.AreEqual(Color.Violet, blueItem.WorldColor(superGroup), "WorldColor on Blue up to supergroup should be Violet");
// Validate WorldColor with null param
Assert.AreEqual(Color.Black, redItem.WorldColor(null), "WorldColor on Red with null param should be root color (Black)");
}
[Test]
public void WorldMaterialIndexBasicTest()
{
var scene = SampleScene();
var superGroup = scene.Descendants().Where(d => d.Name == "SuperGroup").FirstOrDefault();
var redItem = scene.Descendants().Where(d => d.Name == nameof(Color.Red)).FirstOrDefault();
var greenItem = scene.Descendants().Where(d => d.Name == nameof(Color.Green)).FirstOrDefault();
var blueItem = scene.Descendants().Where(d => d.Name == nameof(Color.Blue)).FirstOrDefault();
// Validate root
Assert.AreEqual(this.RootMaterialIndex, scene.MaterialIndex, "MaterialIndex property on root should be RootMaterialIndex");
Assert.AreEqual(this.RootMaterialIndex, scene.WorldMaterialIndex(), "WorldMaterialIndex on root should be RootMaterialIndex");
// Validate red node
Assert.AreEqual(this.RedMaterialIndex, redItem.MaterialIndex, "MaterialIndex property on node should be RedMaterialIndex");
Assert.AreEqual(this.GroupMaterialIndex, redItem.WorldMaterialIndex(redItem.Parent), "WorldMaterialIndex on Red up to parent node should be GroupMaterialIndex");
Assert.AreEqual(this.SuperGroupMaterialIndex, redItem.WorldMaterialIndex(superGroup), "WorldMaterialIndex on Red up to supergroup should be SuperGroupMaterialIndex");
// Validate green node
Assert.AreEqual(this.GreenMaterialIndex, greenItem.MaterialIndex, "MaterialIndex property on node should be GreenMaterialIndex");
Assert.AreEqual(this.GroupMaterialIndex, greenItem.WorldMaterialIndex(greenItem.Parent), "WorldMaterialIndex on Green up to parent node should be GroupMaterialIndex");
Assert.AreEqual(this.SuperGroupMaterialIndex, greenItem.WorldMaterialIndex(superGroup), "WorldMaterialIndex on Green up to supergroup should be SuperGroupMaterialIndex");
// Validate green node
Assert.AreEqual(this.BlueMaterialIndex, blueItem.MaterialIndex, "MaterialIndex property on node should be BlueMaterialIndex");
Assert.AreEqual(this.GroupMaterialIndex, blueItem.WorldMaterialIndex(blueItem.Parent), "WorldMaterialIndex on Blue up to parent node should be GroupMaterialIndex");
Assert.AreEqual(this.SuperGroupMaterialIndex, blueItem.WorldMaterialIndex(superGroup), "WorldMaterialIndex on Blue up to supergroup should be SuperGroupMaterialIndex");
// Validate MaterialIndex with null param
Assert.AreEqual(this.RootMaterialIndex, redItem.WorldMaterialIndex(null), "WorldMaterialIndex on Red with null param should be root color (RootMaterialIndex)");
}
[Test]
public void WorldMatrixBasicTest()
{
var scene = SampleScene();
var superGroup = scene.Descendants().Where(d => d.Name == "SuperGroup").FirstOrDefault();
var redItem = scene.Descendants().Where(d => d.Name == nameof(Color.Red)).FirstOrDefault();
var greenItem = scene.Descendants().Where(d => d.Name == nameof(Color.Green)).FirstOrDefault();
var blueItem = scene.Descendants().Where(d => d.Name == nameof(Color.Blue)).FirstOrDefault();
// Validate root
Assert.AreEqual(this.RootMatrix, scene.Matrix, "Matrix property on root should be RootMatrix");
Assert.AreEqual(this.RootMatrix, scene.WorldMatrix(), "WorldMatrix on root should be RootMatrix");
// Validate red node
Assert.AreEqual(this.RedMatrix, redItem.Matrix, "Matrix property on node should be RedMatrix");
Assert.AreEqual(redItem.Matrix * this.GroupMatrix, redItem.WorldMatrix(redItem.Parent), "WorldMatrix on Red up to parent node should be GroupMatrix");
Assert.AreEqual(this.RedMatrix * this.GroupMatrix * this.SuperGroupMatrix, redItem.WorldMatrix(superGroup), "WorldMatrix on Red up to supergroup invalid");
// Validate green node
Assert.AreEqual(this.GreenMatrix, greenItem.Matrix, "Matrix property on node should be GreenMatrix");
Assert.AreEqual(this.GreenMatrix * this.GroupMatrix, greenItem.WorldMatrix(greenItem.Parent), "WorldMatrix on Green up to parent node should be GroupMatrix");
Assert.AreEqual(this.GreenMatrix * this.GroupMatrix * this.SuperGroupMatrix, greenItem.WorldMatrix(superGroup), "WorldMatrix on Green up to supergroup should be SuperGroupMatrix");
// Validate green node
Assert.AreEqual(this.BlueMatrix, blueItem.Matrix, "Matrix property on node should be BlueMatrix");
Assert.AreEqual(this.BlueMatrix * this.GroupMatrix, blueItem.WorldMatrix(blueItem.Parent), "WorldMatrix on Blue up to parent node should be GroupMatrix");
Assert.AreEqual(this.BlueMatrix * this.GroupMatrix * this.SuperGroupMatrix, blueItem.WorldMatrix(superGroup), "WorldMatrix on Blue up to supergroup should be SuperGroupMatrix");
// Validate Matrix with null param
Assert.AreEqual(this.RedMatrix * this.GroupMatrix * this.SuperGroupMatrix, redItem.WorldMatrix(null), "WorldMatrix on Red with null param should be root color (RootMatrix)");
}
[Test]
public void WorldOutputTypeBasicTest()
{
var scene = SampleScene();
var superGroup = scene.Descendants().Where(d => d.Name == "SuperGroup").FirstOrDefault();
var redItem = scene.Descendants().Where(d => d.Name == nameof(Color.Red)).FirstOrDefault();
var greenItem = scene.Descendants().Where(d => d.Name == nameof(Color.Green)).FirstOrDefault();
var blueItem = scene.Descendants().Where(d => d.Name == nameof(Color.Blue)).FirstOrDefault();
// Validate root
Assert.AreEqual(this.RootOutputType, scene.OutputType, "OutputType property on root should be RootOutputType");
Assert.AreEqual(this.RootOutputType, scene.WorldOutputType(), "WorldOutputType on root should be RootOutputType");
// Validate red node
Assert.AreEqual(this.RedOutputType, redItem.OutputType, "OutputType property on node should be RedOutputType");
Assert.AreEqual(this.GroupOutputType, redItem.WorldOutputType(redItem.Parent), "WorldOutputType on Red up to parent node should be GroupOutputType");
Assert.AreEqual(this.SuperGroupOutputType, redItem.WorldOutputType(superGroup), "WorldOutputType on Red up to supergroup should be SuperGroupOutputType");
// Validate green node
Assert.AreEqual(this.GreenOutputType, greenItem.OutputType, "OutputType property on node should be GreenOutputType");
Assert.AreEqual(this.GroupOutputType, greenItem.WorldOutputType(greenItem.Parent), "WorldOutputType on Green up to parent node should be GroupOutputType");
Assert.AreEqual(this.SuperGroupOutputType, greenItem.WorldOutputType(superGroup), "WorldOutputType on Green up to supergroup should be SuperGroupOutputType");
// Validate green node
Assert.AreEqual(this.BlueOutputType, blueItem.OutputType, "OutputType property on node should be BlueOutputType");
Assert.AreEqual(this.GroupOutputType, blueItem.WorldOutputType(blueItem.Parent), "WorldOutputType on Blue up to parent node should be GroupOutputType");
Assert.AreEqual(this.SuperGroupOutputType, blueItem.WorldOutputType(superGroup), "WorldOutputType on Blue up to supergroup should be SuperGroupOutputType");
// Validate OutputType with null param
Assert.AreEqual(this.RootOutputType, redItem.WorldOutputType(null), "WorldOutputType on Red with null param should be root color (RootOutputType)");
}
[Test]
public void WorldFunctionNonExistingAncestorOverride()
{
var scene = SampleScene();
var redItem = scene.Descendants().Where(d => d.Name == nameof(Color.Red)).FirstOrDefault();
var nonAncestor = new Object3D();
// ************************************* WorldColor *************************************
// Validate root
Assert.AreEqual(Color.Black, scene.Color, "Color property on root should be Black");
Assert.AreEqual(Color.Black, scene.WorldColor(), "WorldColor on root should be Black");
// Validate red node
Assert.AreEqual(Color.Red, redItem.Color, "Color property on node should be Red");
// Validate WorldColor with non-ancestor param
Assert.AreEqual(Color.Black, redItem.WorldColor(nonAncestor), "WorldColor on Red with non-ancestor should be root color (Black)");
// ************************************* MaterialIndex *************************************
// Validate root
Assert.AreEqual(this.RootMaterialIndex, scene.MaterialIndex, "MaterialIndex property on root should be RootMaterialIndex");
Assert.AreEqual(this.RootMaterialIndex, scene.WorldMaterialIndex(), "WorldMaterialIndex on root should be RootMaterialIndex");
// Validate red node
Assert.AreEqual(this.RedMaterialIndex, redItem.MaterialIndex, "Color property on node should be Red");
// Validate WorldColor with non-ancestor param
Assert.AreEqual(this.RootMaterialIndex, redItem.WorldMaterialIndex(nonAncestor), "WorldMaterialIndex on Red with non-ancestor should be RootMaterialIndex");
// ************************************* WorldMaxtrix *************************************
// Validate root
Assert.AreEqual(this.RootMatrix, scene.Matrix, "Matrix property on root should be RootMatrix");
Assert.AreEqual(this.RootMatrix, scene.WorldMatrix(), "WorldMatrix on root should be RootMatrix");
// Validate red node
Assert.AreEqual(this.RedMatrix, redItem.Matrix, "Matrix property on node should be RedMatrix");
// Validate WorldColor with non-ancestor param
Assert.AreEqual(this.RedMatrix * this.GroupMatrix * this.SuperGroupMatrix, redItem.WorldMaterialIndex(nonAncestor), "WorldMaterialIndex on Red with non-ancestor should be RootMaterialIndex");
// ************************************* WorldOutputType *************************************
// Validate root
Assert.AreEqual(this.RootOutputType, scene.OutputType, "OutputType property on root should be RootOutputType");
Assert.AreEqual(this.RootOutputType, scene.WorldOutputType(), "WorldOutputType on root should be RootOutputType");
// Validate red node
Assert.AreEqual(this.RedOutputType, redItem.OutputType, "Color property on node should be Red");
// Validate WorldColor with non-ancestor param
Assert.AreEqual(this.RootOutputType, redItem.WorldOutputType(nonAncestor), "WorldOutputType on Red with non-ancestor should be RootOutputType");
}
public static string GetSceneTempPath()
{
string tempPath = TestContext.CurrentContext.ResolveProjectPath(4, "Tests", "temp", "scenetests");