mirror of
https://github.com/eliasstepanik/OSCADSharpDotnet7.git
synced 2026-01-11 21:48:34 +00:00
+ Deleted Equals and GetHashCode overrides in Cube/Sphere.
+ Added an IsSameAs method to OSCADObject + Modified tests accordingly
This commit is contained in:
parent
7d59afbdd9
commit
8fe459a5ba
@ -15,30 +15,14 @@ namespace OSCADSharp.ConsoleTests
|
||||
{
|
||||
OSCADObject cube = new Cube(new Vector3(15, 15, 15));
|
||||
|
||||
cube = cube.Color("Red")
|
||||
.Mirror(1, 0, 0)
|
||||
.Resize(10, 20, 10)
|
||||
.Rotate(90, 90, 0)
|
||||
.Scale(1, 1, 2)
|
||||
.Translate(10, 5, 2);
|
||||
for (int i = 0; i < 1000; i++)
|
||||
{
|
||||
cube = cube.Translate(1, 0, 0);
|
||||
}
|
||||
|
||||
OSCADObject cylinder = new Cylinder(35.4, 50.8).Translate(10, 5, 2);
|
||||
|
||||
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);
|
||||
string script = cube.ToString();
|
||||
|
||||
//File.WriteAllLines("test.scad", new string[] { script.ToString() });
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
|
||||
@ -34,13 +34,13 @@ namespace OSCADSharp.UnitTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Cube_CloneYieldsEqualObject()
|
||||
public void Cube_CloneYieldsSameScript()
|
||||
{
|
||||
var cube = new Cube(new Vector3(1.5, 5.5, 8.7));
|
||||
|
||||
var clone = cube.Clone();
|
||||
|
||||
Assert.IsTrue(clone.Equals(cube));
|
||||
Assert.IsTrue(clone.IsSameAs(cube));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
|
||||
@ -55,7 +55,7 @@ namespace OSCADSharp.UnitTests
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void Sphere_CloneYieldsEqualObject()
|
||||
public void Sphere_CloneYieldsSameScript()
|
||||
{
|
||||
var sphere = new Sphere()
|
||||
{
|
||||
@ -67,7 +67,7 @@ namespace OSCADSharp.UnitTests
|
||||
|
||||
var clone = sphere.Clone();
|
||||
|
||||
Assert.IsTrue(sphere.Equals(clone));
|
||||
Assert.IsTrue(sphere.IsSameAs(clone));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -191,5 +191,17 @@ namespace OSCADSharp
|
||||
/// </summary>
|
||||
/// <returns>Clone of this object</returns>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,9 +23,12 @@ namespace OSCADSharp.Scripting
|
||||
this.innerCode = innerCode;
|
||||
}
|
||||
|
||||
//TODO: Assess perf and find a better performing way to apply indentation
|
||||
// this approach will likely be a big performance drain with deeply nested structures.
|
||||
// -MLS 2/12/2016
|
||||
// Assessed performance on this method by using Translate 1,000 times
|
||||
// on a Cube and converting it to a string. It completed in about 12.5 seconds.
|
||||
// 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()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@ -76,24 +76,6 @@ namespace OSCADSharp.Solids
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@ -80,28 +80,6 @@ namespace OSCADSharp.Solids
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user