Basic Bounds implementations for most transforms and booleans + some test updates.

This commit is contained in:
Michael Smith 2016-02-21 01:22:44 -08:00
parent fd677aa909
commit cce795332f
21 changed files with 225 additions and 14 deletions

View File

@ -13,13 +13,13 @@ namespace OSCADSharp.ConsoleTests
{
static void Main(string[] args)
{
var obj = new Text3D("BBBB", 16);
var obj = new Cube(5, 10, 20) + new Sphere(10).Translate(-10, 5, 0);
var pos = obj.Position();
var cyl1 = new Cylinder(1, 100, true).Translate(pos);
var cyl2 = new Cylinder(1, 100, true).Rotate(0, 90, 0).Translate(pos);
var cyl3 = new Cylinder(1, 100, true).Rotate(90, 0, 0).Translate(pos);
var axisHelper = cyl1.Union(cyl2, cyl3).Color("Red");
//var pos = obj.Position();
//var cyl1 = new Cylinder(1, 100, true).Translate(pos);
//var cyl2 = new Cylinder(1, 100, true).Rotate(0, 90, 0).Translate(pos);
//var cyl3 = new Cylinder(1, 100, true).Rotate(90, 0, 0).Translate(pos);
//var axisHelper = cyl1.Union(cyl2, cyl3).Color("Red");
var topCorner = new Sphere().Translate(obj.Bounds().TopRight);
var botCorner = new Sphere().Translate(obj.Bounds().BottomLeft);

View File

@ -25,6 +25,15 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(sphere.Position(), diff.Position());
}
[TestMethod]
public void Difference_BoundsYieldsBoundsOfFirstChild()
{
var sphere = new Sphere().Translate(.25, .25, 1);
var diff = sphere.Difference(new Cube());
Assert.AreEqual(sphere.Bounds(), diff.Bounds());
}
[TestMethod]
public void Difference_MinusOperatorCreatesDifferenceObject()
{

View File

@ -15,5 +15,14 @@ namespace OSCADSharp.UnitTests
var pos = obj.Position();
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Intersection_BoundsThrowsNotSupportedException()
{
var obj = new Sphere().Intersection(new Text3D("Sup"));
var pos = obj.Bounds();
}
}
}

View File

@ -68,5 +68,21 @@ namespace OSCADSharp.UnitTests
Assert.AreEqual(0, unionChildrenCount);
}
[TestMethod]
public void Union_BoundsForUnionedObjectsBecomeExtremitiesOfBoth()
{
var obj = new Cube(5, 10, 20) + new Sphere(10).Translate(-10, 5, 0);
var bounds = obj.Bounds();
Assert.AreEqual(20, bounds.Z_Max);
Assert.AreEqual(10, bounds.Y_Max);
Assert.AreEqual(5, bounds.X_Max);
Assert.AreEqual(-5, bounds.Z_Min);
Assert.AreEqual(0, bounds.Y_Min);
Assert.AreEqual(-15, bounds.X_Min);
}
}
}

View File

@ -67,6 +67,7 @@
<Compile Include="Transforms\MinkowskiTests.cs" />
<Compile Include="Transforms\ResizeTests.cs" />
<Compile Include="Transforms\ScaleTests.cs" />
<Compile Include="Transforms\TranslateTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OSCADSharp\OSCADSharp.csproj">

View File

@ -11,9 +11,17 @@ namespace OSCADSharp.UnitTests
[ExpectedException(typeof(NotSupportedException))]
public void Minkowski_PositionThrowsNotSupportedException()
{
var obj = new Cylinder().Intersection(new Sphere()).Translate(0, 5, 5);
var obj = new Cylinder().Minkowski(new Sphere()).Translate(0, 5, 5);
var pos = obj.Position();
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Minkowski_BoundsThrowsNotSupportedException()
{
var obj = new Cylinder().Minkowski(new Sphere()).Translate(0, 5, 5);
obj.Bounds();
}
}
}

View File

@ -40,5 +40,24 @@ namespace OSCADSharp.UnitTests
var pos = cube.Mirror(1, 1, 0).Position();
}
[TestMethod]
public void Mirror_SingleAxisMirrorInvertsBounds()
{
OSCADObject cube = new Cube(5, 10, 20);
var boundsBefore = cube.Bounds();
cube = cube.Mirror(0, 1, 0);
Assert.AreEqual(cube.Bounds().Y_Max, boundsBefore.Y_Min);
}
[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public void Mirror_MultiAxisBoundsThrowsNotSupportedException()
{
var cube = new Cube(5, 10, 20);
var pos = cube.Mirror(1, 1, 0).Bounds();
}
}
}

View File

@ -18,5 +18,15 @@ namespace OSCADSharp.UnitTests.Transforms
var pos = new Cube(5, 5, 20)
.Translate(30, 0, 0).Rotate(0, 90, 0).Resize(2, 2, 2).Position();
}
[TestMethod]
public void Resize_ResizeScalesBoundariesToFit()
{
var obj = new Cube(20, 20, 10).Resize(5, 5, 5);
var bounds = obj.Bounds();
Assert.AreEqual(new Vector3(5, 5, 5), bounds.TopRight);
Assert.AreEqual(new Vector3(0, 0, 0), bounds.BottomLeft);
}
}
}

View File

@ -19,5 +19,20 @@ namespace OSCADSharp.UnitTests.Transforms
Assert.AreEqual(new Vector3(20, 5, -65), obj.Position());
}
[TestMethod]
public void Scale_BoundsScaleWithObject()
{
var obj = new Cube(5, 5, 5).Scale(2, 2, 3);
var bounds = obj.Bounds();
Assert.AreEqual(bounds.X_Max, 10);
Assert.AreEqual(bounds.Y_Max, 10);
Assert.AreEqual(bounds.Z_Max, 15);
Assert.AreEqual(bounds.X_Min, 0);
Assert.AreEqual(bounds.Y_Min, 0);
Assert.AreEqual(bounds.Z_Min, 0);
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OSCADSharp.Solids;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OSCADSharp.UnitTests.Transforms
{
[TestClass]
public class TranslateTests
{
[TestMethod]
public void Translate_BoundsMoveWhenObjectIsTranslated()
{
var cube = new Cube();
var boundsBefore = cube.Bounds();
var boundsAfter = cube.Translate(5, 2, 3).Bounds();
Assert.AreEqual(boundsAfter.TopRight, boundsBefore.TopRight + new Vector3(5, 2, 3));
Assert.AreEqual(boundsAfter.BottomLeft, boundsBefore.BottomLeft + new Vector3(5, 2, 3));
}
}
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Spatial;
namespace OSCADSharp.Booleans
{
@ -24,5 +25,10 @@ namespace OSCADSharp.Booleans
{
return children[0].Position();
}
public override Bounds Bounds()
{
return children[0].Bounds();
}
}
}

View File

@ -1,4 +1,5 @@
using OSCADSharp.Scripting;
using OSCADSharp.Spatial;
using System;
using System.Collections.Generic;
using System.Linq;
@ -24,5 +25,10 @@ namespace OSCADSharp.Booleans
{
throw new NotSupportedException("Position is not supported on Intersected objects.");
}
public override Bounds Bounds()
{
throw new NotSupportedException("Bounds is not supported on Intersected objects.");
}
}
}

View File

@ -53,7 +53,40 @@ namespace OSCADSharp.Scripting
public override Bounds Bounds()
{
throw new NotImplementedException();
var newBottomLeft = new Vector3();
var newTopRight = new Vector3();
foreach (var child in this.children)
{
var bounds = child.Bounds();
if (bounds.X_Min < newBottomLeft.X)
{
newBottomLeft.X = bounds.X_Min;
}
if (bounds.Y_Min < newBottomLeft.Y)
{
newBottomLeft.Y = bounds.Y_Min;
}
if (bounds.Z_Min < newBottomLeft.Z)
{
newBottomLeft.Z = bounds.Z_Min;
}
if (bounds.X_Max > newTopRight.X)
{
newTopRight.X = bounds.X_Max;
}
if (bounds.Y_Max> newTopRight.Y)
{
newTopRight.Y = bounds.Y_Max;
}
if (bounds.Z_Max > newTopRight.Z)
{
newTopRight.Z = bounds.Z_Max;
}
}
return new Bounds(newBottomLeft, newTopRight);
}
}
}

View File

@ -64,5 +64,25 @@ namespace OSCADSharp.Spatial
/// </summary>
public double Z_Min { get { return TopRight.Z < BottomLeft.Z ? TopRight.Z : BottomLeft.Z; } }
#endregion
/// <summary>
/// Compares a set of bounds to another object
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
return obj.GetHashCode() == this.GetHashCode();
}
/// <summary>
/// Gets a hashcode based on the string representation of the vectors
/// that make up this set of bounds
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return String.Format("TR: {0}, BL: {1}", this.TopRight.ToString(), this.BottomLeft.ToString()).GetHashCode();
}
}
}

View File

@ -59,7 +59,7 @@ namespace OSCADSharp.Transforms
public override Bounds Bounds()
{
throw new NotImplementedException();
return this.obj.Bounds();
}
}
}

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OSCADSharp.Spatial;
namespace OSCADSharp.Transforms
{
@ -21,5 +22,10 @@ namespace OSCADSharp.Transforms
{
throw new NotSupportedException("Position is not supported on Minkowskied objects.");
}
public override Bounds Bounds()
{
throw new NotSupportedException("Bounds is not supported on Minkowskied objects.");
}
}
}

View File

@ -75,9 +75,25 @@ namespace OSCADSharp.Transforms
(this.Normal.Y != 0 && (this.Normal.X != 0 || this.Normal.Z != 0));
}
// TODO: As with Position, will yield incorrect positions if mirroring on multiple axes
// fix mirrored positions for multiple-axis mirroring
public override Bounds Bounds()
{
throw new NotImplementedException();
if (this.isMoreThanOneAxis())
{
throw new NotSupportedException("Getting the position of an object that's been mirrored on more than one axis is not currently supported.");
}
var oldBounds = this.obj.Bounds();
var newBottomLeft = new Vector3(this.Normal.X != 0 ? oldBounds.BottomLeft.X * -1 : oldBounds.BottomLeft.X,
this.Normal.Y != 0 ? oldBounds.BottomLeft.Y * -1 : oldBounds.BottomLeft.Y,
this.Normal.Z != 0 ? oldBounds.BottomLeft.Z * -1 : oldBounds.BottomLeft.Z);
var newTopRight = new Vector3(this.Normal.X != 0 ? oldBounds.TopRight.X * -1 : oldBounds.TopRight.X,
this.Normal.Y != 0 ? oldBounds.TopRight.Y * -1 : oldBounds.TopRight.Y,
this.Normal.Z != 0 ? oldBounds.TopRight.Z * -1 : oldBounds.TopRight.Z);
return new Bounds(newBottomLeft, newTopRight);
}
}
}

View File

@ -57,7 +57,14 @@ namespace OSCADSharp.Transforms
public override Bounds Bounds()
{
throw new NotImplementedException();
var oldBounds = obj.Bounds();
double xScaleFactor = this.Size.X > 0 ? this.Size.X / Math.Abs(oldBounds.X_Max - oldBounds.X_Min) : 1;
double yScaleFactor = this.Size.Y > 0 ? this.Size.Y / Math.Abs(oldBounds.Y_Max - oldBounds.Y_Min) : 1;
double zScaleFactor = this.Size.Z > 0 ? this.Size.Z / Math.Abs(oldBounds.Z_Max - oldBounds.Z_Min) : 1;
Vector3 scaleMultiplier = new Vector3(xScaleFactor, yScaleFactor, zScaleFactor);
return new Bounds(oldBounds.BottomLeft * scaleMultiplier, oldBounds.TopRight * scaleMultiplier);
}
}
}

View File

@ -57,7 +57,9 @@ namespace OSCADSharp.Transforms
public override Bounds Bounds()
{
throw new NotImplementedException();
var oldBounds = obj.Bounds();
return new Bounds(Matrix.GetRotatedPoint(oldBounds.BottomLeft, this.Angle.X, this.Angle.Y, this.Angle.Z),
Matrix.GetRotatedPoint(oldBounds.TopRight, this.Angle.X, this.Angle.Y, this.Angle.Z));
}
}
}

View File

@ -57,7 +57,8 @@ namespace OSCADSharp.Transforms
public override Bounds Bounds()
{
throw new NotImplementedException();
var oldBounds = obj.Bounds();
return new Bounds(oldBounds.BottomLeft * this.ScaleFactor, oldBounds.TopRight * this.ScaleFactor);
}
}
}

View File

@ -54,7 +54,8 @@ namespace OSCADSharp.Transforms
public override Bounds Bounds()
{
throw new NotImplementedException();
var oldBounds = obj.Bounds();
return new Bounds(oldBounds.BottomLeft + this.Vector, oldBounds.TopRight + this.Vector);
}
}
}