diff --git a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs index 78818943a..1f819493b 100644 --- a/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs +++ b/MatterControlLib/DesignTools/Operations/FitToBoundsObject3D_2.cs @@ -35,6 +35,7 @@ using MatterHackers.MatterControl.PartPreviewWindow; using MatterHackers.MeshVisualizer; using MatterHackers.PolygonMesh; using MatterHackers.VectorMath; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.ComponentModel; @@ -49,10 +50,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations private AxisAlignedBoundingBox cacheAabb; - private Vector3 cacheBounds; - - private Matrix4X4 cacheRequestedMatrix = new Matrix4X4(); - private Matrix4X4 cacheThisMatrix; + private bool rebuildAabbCache = true; public FitToBoundsObject3D_2() { @@ -120,27 +118,32 @@ namespace MatterHackers.MatterControl.DesignTools.Operations var fitToBounds = new FitToBoundsObject3D_2(); using (fitToBounds.RebuildLock()) { - using (new CenterAndHeightMantainer(itemToFit)) + var startingAabb = itemToFit.GetAxisAlignedBoundingBox(); + itemToFit.Translate(-startingAabb.Center); + + // add the fit item + var scaleItem = new Object3D(); + fitToBounds.Children.Add(scaleItem); + scaleItem.Children.Add(itemToFit); + + // create an object that just represents the bounds in the scene + var fitBounds = new Object3D() { - var aabb = itemToFit.GetAxisAlignedBoundingBox(); - var bounds = new Object3D() - { - Visible = false, - Color = new Color(Color.Red, 100), - Mesh = PlatonicSolids.CreateCube() - }; + Visible = false, + Color = new Color(Color.Red, 100), + Mesh = PlatonicSolids.CreateCube() + }; + // add the item that holds the bounds + fitToBounds.Children.Add(fitBounds); - // add all the children - var scaleItem = new Object3D(); - fitToBounds.Children.Add(scaleItem); - scaleItem.Children.Add(itemToFit); - fitToBounds.Children.Add(bounds); + fitToBounds.boundsSize.X = startingAabb.XSize; + fitToBounds.boundsSize.Y = startingAabb.YSize; + fitToBounds.boundsSize.Z = startingAabb.ZSize; + await fitToBounds.Rebuild(); - fitToBounds.boundsSize.X = aabb.XSize; - fitToBounds.boundsSize.Y = aabb.YSize; - fitToBounds.boundsSize.Z = aabb.ZSize; - await fitToBounds.Rebuild(); - } + var finalAabb = fitToBounds.GetAxisAlignedBoundingBox(); + fitToBounds.Translate(startingAabb.Center - finalAabb.Center); + fitToBounds.rebuildAabbCache = true; } return fitToBounds; @@ -169,9 +172,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { if (Children.Count == 2) { - if (cacheRequestedMatrix != matrix - || cacheThisMatrix != Matrix - || cacheBounds != boundsSize) + if (rebuildAabbCache) { using (FitBounds.RebuildLock()) { @@ -179,9 +180,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations cacheAabb = base.GetAxisAlignedBoundingBox(matrix); FitBounds.Visible = false; } - cacheRequestedMatrix = matrix; - cacheThisMatrix = Matrix; - cacheBounds = boundsSize; + + rebuildAabbCache = false; } return cacheAabb; @@ -210,7 +210,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations || invalidateType.InvalidateType.HasFlag(InvalidateType.Mesh) || invalidateType.InvalidateType.HasFlag(InvalidateType.Children)) { - cacheThisMatrix = Matrix4X4.Identity; + rebuildAabbCache = true; base.OnInvalidate(invalidateType); } @@ -227,9 +227,6 @@ namespace MatterHackers.MatterControl.DesignTools.Operations AdjustChildSize(null, null); UpdateBoundsItem(); - - cacheRequestedMatrix = new Matrix4X4(); - var after = this.GetAxisAlignedBoundingBox(); } } @@ -298,6 +295,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations FitBounds.Matrix *= Matrix4X4.CreateTranslation( transformAabb.Center - FitBounds.GetAxisAlignedBoundingBox().Center); } + + rebuildAabbCache = true; } } } diff --git a/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs b/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs index 35df9bd7e..264705e43 100644 --- a/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs +++ b/MatterControlLib/DesignTools/Operations/FitToCylinderObject3D.cs @@ -53,14 +53,11 @@ namespace MatterHackers.MatterControl.DesignTools.Operations public class FitToCylinderObject3D : TransformWrapperObject3D, IEditorDraw { - private Vector3 boundsSize; - private AxisAlignedBoundingBox cacheAabb; - private Vector3 cacheBounds; + private bool rebuildAabbCache = true; private Matrix4X4 cacheRequestedMatrix = new Matrix4X4(); - private Matrix4X4 cacheThisMatrix; public FitToCylinderObject3D() { @@ -85,29 +82,33 @@ namespace MatterHackers.MatterControl.DesignTools.Operations var fitToBounds = new FitToCylinderObject3D(); using (fitToBounds.RebuildLock()) { - using (new CenterAndHeightMantainer(itemToFit)) + var startingAabb = itemToFit.GetAxisAlignedBoundingBox(); + + // add the fit item + var scaleItem = new Object3D(); + fitToBounds.Children.Add(scaleItem); + scaleItem.Children.Add(itemToFit); + + // create an object that just represents the bounds in the scene + var fitBounds = new Object3D() { - var aabb = itemToFit.GetAxisAlignedBoundingBox(); - var bounds = new Object3D() - { - Visible = false, - Color = new Color(Color.Red, 100), - Mesh = PlatonicSolids.CreateCube() - }; + Visible = false, + Color = new Color(Color.Red, 100), + Mesh = PlatonicSolids.CreateCube() + }; + // add the item that holds the bounds + fitToBounds.Children.Add(fitBounds); - // add all the children - var scaleItem = new Object3D(); - fitToBounds.Children.Add(scaleItem); - scaleItem.Children.Add(itemToFit); - fitToBounds.Children.Add(bounds); + fitToBounds.Diameter = Math.Sqrt(startingAabb.XSize * startingAabb.XSize + startingAabb.YSize * startingAabb.YSize); + fitToBounds.SizeZ = startingAabb.ZSize; - fitToBounds.Diameter = Math.Sqrt(aabb.XSize * aabb.XSize + aabb.YSize * aabb.YSize); - fitToBounds.boundsSize.Z = aabb.ZSize; + fitToBounds.SizeZ = startingAabb.ZSize; - fitToBounds.SizeZ = aabb.ZSize; + await fitToBounds.Rebuild(); - await fitToBounds.Rebuild(); - } + var finalAabb = fitToBounds.GetAxisAlignedBoundingBox(); + fitToBounds.Translate(startingAabb.Center - finalAabb.Center); + fitToBounds.rebuildAabbCache = true; } return fitToBounds; @@ -127,9 +128,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { if (Children.Count == 2) { - if (cacheRequestedMatrix != matrix - || cacheThisMatrix != Matrix - || cacheBounds != boundsSize) + if (rebuildAabbCache) { using (FitBounds.RebuildLock()) { @@ -138,8 +137,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations FitBounds.Visible = false; } cacheRequestedMatrix = matrix; - cacheThisMatrix = Matrix; - cacheBounds = boundsSize; + rebuildAabbCache = false; } return cacheAabb; @@ -168,7 +166,7 @@ namespace MatterHackers.MatterControl.DesignTools.Operations || invalidateType.InvalidateType.HasFlag(InvalidateType.Mesh) || invalidateType.InvalidateType.HasFlag(InvalidateType.Children)) { - cacheThisMatrix = Matrix4X4.Identity; + rebuildAabbCache = true; base.OnInvalidate(invalidateType); } @@ -339,15 +337,14 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { var transformAabb = ItemWithTransform.GetAxisAlignedBoundingBox(); var fitAabb = FitBounds.GetAxisAlignedBoundingBox(); - var fitSize = fitAabb.Size; - if (boundsSize.X != 0 && boundsSize.Y != 0 && boundsSize.Z != 0 - && (fitSize != boundsSize - || fitAabb.Center != transformAabb.Center)) + if (Diameter != 0 + && SizeZ != 0 + && (fitAabb.XSize != Diameter || fitAabb.ZSize != SizeZ)) { FitBounds.Matrix *= Matrix4X4.CreateScale( - boundsSize.X / fitSize.X, - boundsSize.Y / fitSize.Y, - boundsSize.Z / fitSize.Z); + fitAabb.XSize / Diameter, + fitAabb.YSize / Diameter, + fitAabb.ZSize / SizeZ); FitBounds.Matrix *= Matrix4X4.CreateTranslation( transformAabb.Center - FitBounds.GetAxisAlignedBoundingBox().Center); } @@ -360,6 +357,8 @@ namespace MatterHackers.MatterControl.DesignTools.Operations { var test = GetCenteringTransformExpandedToRadius(UntransformedChildren, Diameter / 2); } + + rebuildAabbCache = true; } } } diff --git a/Submodules/agg-sharp b/Submodules/agg-sharp index 0d0a7c339..3c2ed0432 160000 --- a/Submodules/agg-sharp +++ b/Submodules/agg-sharp @@ -1 +1 @@ -Subproject commit 0d0a7c33939389da7dc8eedf98e408aa494cee44 +Subproject commit 3c2ed0432e5c799a2ed31eb2389bae56dfa4834e diff --git a/Tests/MatterControl.Tests/MatterControl/InteractiveSceneTests.cs b/Tests/MatterControl.Tests/MatterControl/InteractiveSceneTests.cs index 77ac9f9f5..efc6f23b5 100644 --- a/Tests/MatterControl.Tests/MatterControl/InteractiveSceneTests.cs +++ b/Tests/MatterControl.Tests/MatterControl/InteractiveSceneTests.cs @@ -39,6 +39,7 @@ using MatterHackers.MatterControl.Tests.Automation; using MatterHackers.VectorMath; using NUnit.Framework; using System.Linq; +using System.Threading.Tasks; namespace MatterControl.Tests.MatterControl { @@ -306,12 +307,7 @@ namespace MatterControl.Tests.MatterControl } [Test, Category("InteractiveScene")] - public void AabbCalculatedCorrectlyForPinchedFitObjects() - { - DoAabbCalculatedCorrectlyForPinchedFitObjects(); - } - - async void DoAabbCalculatedCorrectlyForPinchedFitObjects() + public async Task AabbCalculatedCorrectlyForPinchedFitObjects() { AggContext.StaticData = new FileSystemStaticData(TestContext.CurrentContext.ResolveProjectPath(4, "StaticData")); MatterControlUtilities.OverrideAppDataLocation(TestContext.CurrentContext.ResolveProjectPath(4)); @@ -401,7 +397,7 @@ namespace MatterControl.Tests.MatterControl await pinch.Rebuild(); root.Children.Add(pinch); var rootAabb = root.GetAxisAlignedBoundingBox(); - Assert.IsTrue(rootAabb.Equals(new AxisAlignedBoundingBox(new Vector3(1, -10, -10), new Vector3(21, 10, 10)), .001)); + Assert.IsTrue(rootAabb.Equals(new AxisAlignedBoundingBox(new Vector3(.5, -10, -10), new Vector3(21, 10, 10)), .001)); } }