From fd677aa909803f4706161db44617f6f7a1960acb Mon Sep 17 00:00:00 2001 From: Michael L Smith Date: Sat, 20 Feb 2016 23:19:28 -0800 Subject: [PATCH] + Initial Bounds implementation for Cube, Cylinder, Sphere and (sort of) Text3D --- OSCADSharp/OSCADSharp.ConsoleTests/Program.cs | 8 +++++--- .../OSCADSharp.UnitTests/Solids/CubeTests.cs | 18 ++++++++++++++++++ .../Solids/CylinderTests.cs | 18 ++++++++++++++++++ .../OSCADSharp.UnitTests/Solids/SphereTests.cs | 9 +++++++++ .../OSCADSharp.UnitTests/Solids/Text3DTests.cs | 9 +++++++++ OSCADSharp/OSCADSharp/Solids/Cube.cs | 10 +++++++++- OSCADSharp/OSCADSharp/Solids/Cylinder.cs | 11 ++++++++++- OSCADSharp/OSCADSharp/Solids/Sphere.cs | 3 ++- OSCADSharp/OSCADSharp/Solids/Text3D.cs | 9 ++++++++- 9 files changed, 88 insertions(+), 7 deletions(-) diff --git a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs index 4b37052..9e1edc4 100644 --- a/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs +++ b/OSCADSharp/OSCADSharp.ConsoleTests/Program.cs @@ -13,8 +13,7 @@ namespace OSCADSharp.ConsoleTests { static void Main(string[] args) { - var obj = new Cube(5, 5, 20) - .Translate(30, 0, 0).Rotate(0, 90, 0).Scale(2, 2, 2); + var obj = new Text3D("BBBB", 16); var pos = obj.Position(); var cyl1 = new Cylinder(1, 100, true).Translate(pos); @@ -22,7 +21,10 @@ namespace OSCADSharp.ConsoleTests var cyl3 = new Cylinder(1, 100, true).Rotate(90, 0, 0).Translate(pos); var axisHelper = cyl1.Union(cyl2, cyl3).Color("Red"); - string script = obj.Union(axisHelper).ToString(); + var topCorner = new Sphere().Translate(obj.Bounds().TopRight); + var botCorner = new Sphere().Translate(obj.Bounds().BottomLeft); + + string script = (obj + topCorner + botCorner).ToString(); File.WriteAllLines("test.scad", new string[] { script.ToString() }); //Console.ReadKey(); diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs index 06c768d..abf63a4 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/CubeTests.cs @@ -85,5 +85,23 @@ namespace OSCADSharp.UnitTests Assert.AreEqual(new Vector3(-5, 0, -15), cube.Position()); } + + [TestMethod] + public void Cube_BoundsAreInExpectedPositionNotCentered() + { + var obj = new Cube(5, 5, 20, false); + + Assert.AreEqual(new Vector3(5, 5, 20), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(), obj.Bounds().BottomLeft); + } + + [TestMethod] + public void Cube_BoundsAreInExpectedPositionCentered() + { + var obj = new Cube(5, 5, 20, true); + + Assert.AreEqual(new Vector3(2.5, 2.5, 10), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(-2.5, -2.5, -10), obj.Bounds().BottomLeft); + } } } diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs index 78396c5..b1acbce 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/CylinderTests.cs @@ -39,5 +39,23 @@ namespace OSCADSharp.UnitTests Assert.AreEqual(new Vector3(), cylinder.Position()); } + + [TestMethod] + public void Cylinder_BoundsAreInExpectedPositionNotCentered() + { + var obj = new Cylinder(5, 20); + + Assert.AreEqual(new Vector3(2.5, 2.5, 20), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(-2.5, -2.5, 0), obj.Bounds().BottomLeft); + } + + [TestMethod] + public void Cylinder_BoundsAreInExpectedPositionCentered() + { + var obj = new Cylinder(5, 20, true); + + Assert.AreEqual(new Vector3(2.5, 2.5, 10), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(-2.5, -2.5, -10), obj.Bounds().BottomLeft); + } } } diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs index f49acc8..2cc55d3 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/SphereTests.cs @@ -77,5 +77,14 @@ namespace OSCADSharp.UnitTests Assert.AreEqual(new Vector3(), sphere.Position()); } + + [TestMethod] + public void Sphere_BoundsAreInExpectedPositionCentered() + { + var obj = new Sphere(30); + + Assert.AreEqual(new Vector3(15, 15, 15), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(-15, -15, -15), obj.Bounds().BottomLeft); + } } } diff --git a/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs b/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs index e8ebcf3..429593c 100644 --- a/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs +++ b/OSCADSharp/OSCADSharp.UnitTests/Solids/Text3DTests.cs @@ -18,5 +18,14 @@ namespace OSCADSharp.UnitTests Assert.AreEqual(new Vector3(), text.Position()); } + + [TestMethod] + public void Text_BoundsBasedOnFontSize() + { + var obj = new Text3D("BBBB", 16); + + Assert.AreEqual(new Vector3(32, 8, 0.5), obj.Bounds().TopRight); + Assert.AreEqual(new Vector3(-32, -8, -0.5), obj.Bounds().BottomLeft); + } } } diff --git a/OSCADSharp/OSCADSharp/Solids/Cube.cs b/OSCADSharp/OSCADSharp/Solids/Cube.cs index 3f45dec..60d87a0 100644 --- a/OSCADSharp/OSCADSharp/Solids/Cube.cs +++ b/OSCADSharp/OSCADSharp/Solids/Cube.cs @@ -113,7 +113,15 @@ namespace OSCADSharp.Solids /// public override Bounds Bounds() { - throw new NotImplementedException(); + if(Center == false) + { + return new Bounds(new Vector3(), new Vector3(this.Size.X, this.Size.Y, this.Size.Z)); + } + else + { + return new Bounds(new Vector3(-this.Size.X / 2, -this.Size.Y / 2, -this.Size.Z / 2), + new Vector3(this.Size.X / 2, this.Size.Y / 2, this.Size.Z / 2)); + } } #endregion } diff --git a/OSCADSharp/OSCADSharp/Solids/Cylinder.cs b/OSCADSharp/OSCADSharp/Solids/Cylinder.cs index 4e4a150..b06c754 100644 --- a/OSCADSharp/OSCADSharp/Solids/Cylinder.cs +++ b/OSCADSharp/OSCADSharp/Solids/Cylinder.cs @@ -174,7 +174,16 @@ namespace OSCADSharp.Solids /// public override Bounds Bounds() { - throw new NotImplementedException(); + if(this.Center == false) + { + return new Bounds(new Vector3(-this.Radius, -this.Radius, 0), + new Vector3(this.Radius, this.Radius, this.Height)); + } + else + { + return new Bounds(new Vector3(-this.Radius, -this.Radius, -this.Height / 2), + new Vector3(this.Radius, this.Radius, this.Height / 2)); + } } #endregion } diff --git a/OSCADSharp/OSCADSharp/Solids/Sphere.cs b/OSCADSharp/OSCADSharp/Solids/Sphere.cs index cf9f083..9fd82e4 100644 --- a/OSCADSharp/OSCADSharp/Solids/Sphere.cs +++ b/OSCADSharp/OSCADSharp/Solids/Sphere.cs @@ -106,7 +106,8 @@ namespace OSCADSharp.Solids /// public override Bounds Bounds() { - throw new NotImplementedException(); + return new Bounds(new Vector3(-this.Radius, -this.Radius, -this.Radius), + new Vector3(this.Radius, this.Radius, this.Radius)); } #endregion } diff --git a/OSCADSharp/OSCADSharp/Solids/Text3D.cs b/OSCADSharp/OSCADSharp/Solids/Text3D.cs index 3a507b9..9f07cf1 100644 --- a/OSCADSharp/OSCADSharp/Solids/Text3D.cs +++ b/OSCADSharp/OSCADSharp/Solids/Text3D.cs @@ -144,11 +144,18 @@ namespace OSCADSharp.Solids /// /// Returns the approximate boundaries of this OpenSCAD object + /// + /// Disclaimer: The bounds for text are particularly inaccurate, since + /// OSCADSharp doesn't have all font data necessary to calculate the + /// boundaries for all the possible fonts that could be used /// /// public override Bounds Bounds() { - throw new NotImplementedException(); + double fontSize = this.Size ?? 8; + double xAmount = fontSize * this.Text.Length; + + return new Bounds(new Vector3(-xAmount/2, -fontSize/2, -.5), new Vector3(xAmount / 2, fontSize / 2, .5)); } #endregion }