+ Deleted Equals and GetHashCode overrides in Cube/Sphere.

+ Added an IsSameAs method to OSCADObject
+ Modified tests accordingly
This commit is contained in:
Mike Smith 2016-02-13 13:01:02 -08:00
parent 7d59afbdd9
commit 8fe459a5ba
7 changed files with 28 additions and 69 deletions

View File

@ -15,30 +15,14 @@ namespace OSCADSharp.ConsoleTests
{ {
OSCADObject cube = new Cube(new Vector3(15, 15, 15)); OSCADObject cube = new Cube(new Vector3(15, 15, 15));
cube = cube.Color("Red") for (int i = 0; i < 1000; i++)
.Mirror(1, 0, 0) {
.Resize(10, 20, 10) cube = cube.Translate(1, 0, 0);
.Rotate(90, 90, 0) }
.Scale(1, 1, 2)
.Translate(10, 5, 2);
OSCADObject cylinder = new Cylinder(35.4, 50.8).Translate(10, 5, 2); string script = cube.ToString();
var combined = cube.Intersection(cylinder).Color("Blue");
combined = cube.Clone().Mirror(0, 0, 1).Union(combined);
var text = new Text3D("Hello").Translate(-30, 0, 0);
combined = text.Union(combined);
string script = text.ToString();
File.WriteAllLines("test.scad", new string[] { script.ToString() });
var vec1 = new Vector3(5, 5, 5);
var vec2 = new Vector3(10, 10, 10);
Console.WriteLine(vec1 - vec2);
//File.WriteAllLines("test.scad", new string[] { script.ToString() });
Console.ReadKey(); Console.ReadKey();
} }
} }

View File

@ -34,13 +34,13 @@ namespace OSCADSharp.UnitTests
} }
[TestMethod] [TestMethod]
public void Cube_CloneYieldsEqualObject() public void Cube_CloneYieldsSameScript()
{ {
var cube = new Cube(new Vector3(1.5, 5.5, 8.7)); var cube = new Cube(new Vector3(1.5, 5.5, 8.7));
var clone = cube.Clone(); var clone = cube.Clone();
Assert.IsTrue(clone.Equals(cube)); Assert.IsTrue(clone.IsSameAs(cube));
} }
[TestMethod] [TestMethod]

View File

@ -55,7 +55,7 @@ namespace OSCADSharp.UnitTests
} }
[TestMethod] [TestMethod]
public void Sphere_CloneYieldsEqualObject() public void Sphere_CloneYieldsSameScript()
{ {
var sphere = new Sphere() var sphere = new Sphere()
{ {
@ -67,7 +67,7 @@ namespace OSCADSharp.UnitTests
var clone = sphere.Clone(); var clone = sphere.Clone();
Assert.IsTrue(sphere.Equals(clone)); Assert.IsTrue(sphere.IsSameAs(clone));
} }
} }
} }

View File

@ -191,5 +191,17 @@ namespace OSCADSharp
/// </summary> /// </summary>
/// <returns>Clone of this object</returns> /// <returns>Clone of this object</returns>
public abstract OSCADObject Clone(); public abstract OSCADObject Clone();
/// <summary>
/// Indicates whether this OSCADObject is the same as another OSCADObject.
/// This processes the scripts for both objects and is computationally expensive.
/// DO NOT use on deeply nested structures.
/// </summary>
/// <param name="other">OSCADObject to compare to</param>
/// <returns>True if scripts are exactly the same, false otherwise</returns>
public bool IsSameAs(OSCADObject other)
{
return this.ToString() == other.ToString();
}
} }
} }

View File

@ -23,9 +23,12 @@ namespace OSCADSharp.Scripting
this.innerCode = innerCode; this.innerCode = innerCode;
} }
//TODO: Assess perf and find a better performing way to apply indentation // Assessed performance on this method by using Translate 1,000 times
// this approach will likely be a big performance drain with deeply nested structures. // on a Cube and converting it to a string. It completed in about 12.5 seconds.
// -MLS 2/12/2016 // This method was the bottleneck by far at 60% of the CPU time spent.
// Although this is probably okay for generating scripts (that is a huge script), we should
// refrain from using the stringified version of deeply nested objects for equivalence checks
// -MLS 2/13/2016
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -76,24 +76,6 @@ namespace OSCADSharp.Solids
Center = this.Center Center = this.Center
}; };
} }
public override bool Equals(object other)
{
if (other.GetType() == typeof(Cube))
{
Cube otherSphere = other as Cube;
return this.GetHashCode() == other.GetHashCode();
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
#endregion #endregion
} }
} }

View File

@ -80,28 +80,6 @@ namespace OSCADSharp.Solids
Radius = this.Radius Radius = this.Radius
}; };
} }
public override bool Equals(object other)
{
// This logic is because we want to prevent the weird scenario
// where perhaps this sphere is being compared to a string that's an
// exact match to this sphere's string output. Since the hashcode is
// borrowed from the string output
if(other.GetType() == typeof(Sphere))
{
Sphere otherSphere = other as Sphere;
return this.GetHashCode() == other.GetHashCode();
}
else
{
return false;
}
}
public override int GetHashCode()
{
return this.ToString().GetHashCode();
}
#endregion #endregion
} }
} }