Exemple #1
0
        /// <summary>
        /// Union a and b together. This can either return a single item with a mesh on it
        /// or a group item that has the a and be items as children
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="doMeshCombine"></param>
        /// <returns></returns>
        public static IObject3D Plus(this IObject3D a, IObject3D b, bool doMeshCombine = false)
        {
            if (doMeshCombine)
            {
                var combine = new CombineObject3D_2();
                combine.Children.Add(a.Clone());
                combine.Children.Add(b.Clone());

                combine.Combine();

                var finalMesh = combine.VisibleMeshes().First().Mesh;
                return(new Object3D()
                {
                    Mesh = finalMesh
                });
            }
            else
            {
                var group = new GroupObject3D();
                group.Children.Add(a);
                group.Children.Add(b);
                return(group);
            }
        }
        public async Task CombineTests()
        {
            StartupMC();

            // Combine has correct results
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var offsetCubeB = new TranslateObject3D(cubeB, 10);
                Assert.IsTrue(offsetCubeB.GetAxisAlignedBoundingBox().Equals(new AxisAlignedBoundingBox(
                                                                                 0, -10, -10,
                                                                                 20, 10, 10), .001));

                var union = new CombineObject3D_2();
                union.Children.Add(cubeA);
                union.Children.Add(offsetCubeB);
                root.Children.Add(union);

                Assert.IsTrue(union.GetAxisAlignedBoundingBox().Equals(new AxisAlignedBoundingBox(
                                                                           -10, -10, -10,
                                                                           20, 10, 10), .001));

                Assert.IsTrue(root.GetAxisAlignedBoundingBox().Equals(new AxisAlignedBoundingBox(
                                                                          -10, -10, -10,
                                                                          20, 10, 10), .001));

                union.Combine();
                Assert.IsTrue(union.GetAxisAlignedBoundingBox().Equals(new AxisAlignedBoundingBox(
                                                                           -10, -10, -10,
                                                                           20, 10, 10), .001));

                union.Flatten(null);

                Assert.AreEqual(1, root.Children.Count());
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));
            }

            // Combine has correct results when inner content is changed
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var union = new CombineObject3D_2();
                union.Children.Add(cubeA);
                union.Children.Add(cubeB);
                root.Children.Add(union);

                union.Combine();

                Assert.AreEqual(5, root.Descendants().Count());
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  10, 10, 10).Equals(rootAabb, .001));

                var offsetCubeB = new TranslateObject3D(cubeB, 10);

                union.Combine();
                Assert.AreEqual(7, root.Descendants().Count());
                rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));
            }

            // now make sure undo has the right results for flatten
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var offsetCubeB = new TranslateObject3D(cubeB, 10);

                var combine = new CombineObject3D_2();
                combine.Children.Add(cubeA);
                combine.Children.Add(offsetCubeB);
                root.Children.Add(combine);
                Assert.AreEqual(5, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 cubeB, 4 offset cubeB, 5 offset sourceItem");

                combine.Combine();
                Assert.AreEqual(7, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 offset cubeB, 6 offset sourceItem, wrapped cubeB");
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));

                var undoBuffer = new UndoBuffer();
                combine.Flatten(undoBuffer);

                Assert.AreEqual(1, root.Descendants().Count());
                Assert.AreEqual(1, root.Children.Count());
                rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));

                undoBuffer.Undo();
                Assert.AreEqual(7, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 offset cubeB, 6 offset sourceItem, wrapped cubeB");
            }

            // now make sure undo has the right results for remove
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                cubeA.Name = "cubeA";
                var cubeB = await CubeObject3D.Create(20, 20, 20);

                cubeB.Name = "cubeB";

                var combine = new CombineObject3D_2();
                combine.Children.Add(cubeA);
                combine.Children.Add(cubeB);
                root.Children.Add(combine);
                Assert.AreEqual(3, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 cubeB");

                combine.Combine();
                Assert.AreEqual(5, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 wrapped cubeB");
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  10, 10, 10).Equals(rootAabb, .001));

                var undoBuffer = new UndoBuffer();
                combine.Remove(undoBuffer);

                Assert.AreEqual(2, root.Descendants().Count(), "Should have the 1 cubeA, 2 cubeB");
                rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  10, 10, 10).Equals(rootAabb, .001));

                undoBuffer.Undo();
                Assert.AreEqual(5, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 wrapped cubeB");
            }

            // now make sure undo has the right results for remove
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var offsetCubeB = new TranslateObject3D(cubeB, 10);

                var combine = new CombineObject3D_2();
                combine.Children.Add(cubeA);
                combine.Children.Add(offsetCubeB);
                root.Children.Add(combine);
                Assert.AreEqual(5, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 cubeB, 4 offset cubeB, 5 offset sourceItem");

                combine.Combine();
                Assert.AreEqual(7, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 offset cubeB, 6 offset sourceItem, wrapped cubeB");
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));

                var undoBuffer = new UndoBuffer();
                combine.Remove(undoBuffer);

                Assert.AreEqual(4, root.Descendants().Count(), "Should have the 1 cubeA, 2 cubeB, 3 offset cubeB, 4 offset sourceItem");
                rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));

                undoBuffer.Undo();
                Assert.AreEqual(7, root.Descendants().Count(), "Should have the 1 combine, 2 cubeA, 3 wrapped cubeA, 4 cubeB, 5 offset cubeB, 6 offset sourceItem, wrapped cubeB");
            }

            // make sure the MatterCAD add function is working
            {
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var offsetCubeB = new TranslateObject3D(cubeB, 10);

                var plus = cubeA.Plus(offsetCubeB, true);

                Assert.AreEqual(0, plus.Children.Count());
                Assert.IsTrue(plus.Mesh != null);
                var aabb = plus.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(aabb, .001));
            }

            // test single object combine
            {
                var root  = new Object3D();
                var cubeA = await CubeObject3D.Create(20, 20, 20);

                var cubeB = await CubeObject3D.Create(20, 20, 20);

                var offsetCubeB = new TranslateObject3D(cubeB, 10);

                var group = new Object3D();
                group.Children.Add(cubeA);
                group.Children.Add(offsetCubeB);

                var union = new CombineObject3D_2();
                union.Children.Add(group);

                root.Children.Add(union);

                union.Combine();
                Assert.AreEqual(8, root.Descendants().Count(), "group, union, wa, a, wtb, tb, b");

                union.Flatten(null);
                Assert.AreEqual(1, root.Descendants().Count(), "Should have the union result");

                Assert.AreEqual(1, root.Children.Count());
                var rootAabb = root.GetAxisAlignedBoundingBox();
                Assert.IsTrue(new AxisAlignedBoundingBox(
                                  -10, -10, -10,
                                  20, 10, 10).Equals(rootAabb, .001));
            }
        }