コード例 #1
0
        public virtual void GetFistChildInPolyTreeTest()
        {
            PolyTree tree        = new PolyTree();
            PolyNode firstChild  = new PolyNode();
            PolyNode secondChild = new PolyNode();

            tree.AddChild(firstChild);
            tree.AddChild(secondChild);
            NUnit.Framework.Assert.AreSame(firstChild, tree.GetFirst());
        }
コード例 #2
0
        public virtual void GetTotalSizeDifferentPolyNodeTest()
        {
            PolyTree         tree     = new PolyTree();
            IList <PolyNode> allPolys = tree.m_AllPolys;

            allPolys.Add(new PolyNode());
            allPolys.Add(new PolyNode());
            tree.AddChild(new PolyNode());
            NUnit.Framework.Assert.AreEqual(1, tree.Total);
        }
コード例 #3
0
        public virtual void ClearPolyTreeTest()
        {
            PolyTree tree = new PolyTree();

            tree.AddChild(new PolyNode());
            IList <PolyNode> allPolys = tree.m_AllPolys;

            allPolys.Add(new PolyNode());
            NUnit.Framework.Assert.IsFalse(allPolys.IsEmpty());
            NUnit.Framework.Assert.AreEqual(1, tree.ChildCount);
            tree.Clear();
            NUnit.Framework.Assert.IsTrue(allPolys.IsEmpty());
            NUnit.Framework.Assert.AreEqual(0, tree.ChildCount);
        }
コード例 #4
0
        internal static List <SliceLine2D> ConvertPolyTreeWithOffsetToSliceLine2D(float sliceHeight, List <PolyTree> polytrees, float insideOffset, float outsideOffset)
        {
            //calc model points using polygon offset
            var result = new List <SliceLine2D>();

            if (sliceHeight != 10.65f)
            {
                return(result);
            }

            var decimalCorrectionFactor        = 10000f;
            var selectedPrinterProjectorVector = new Vector2(RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionX, RenderEngine.PrintJob.SelectedPrinter.ProjectorResolutionY);

            var clipperOffset = new ClipperOffset();
            var holes         = new List <PolyNode>();
            var outsides      = new List <PolyNode>();

            foreach (var polygon in polytrees)
            {
                foreach (var pointInPath in polygon._allPolys)
                {
                    if (pointInPath.Contour.Count > 2)
                    {
                        clipperOffset.Clear();
                        clipperOffset = new ClipperOffset();

                        var results = new PolyTree();
                        clipperOffset.AddPath(pointInPath.Contour, JoinType.jtMiter, EndType.etClosedPolygon);

                        if (!pointInPath.IsHole)
                        {
                            //detax -2
                            //abs -1
                            clipperOffset.Execute(ref results, outsideOffset * decimalCorrectionFactor);

                            foreach (var offsetPolygon in results.Childs)
                            {
                                outsides.Add(offsetPolygon);
                            }
                        }
                        else
                        {
                            //detax 2
                            //abs 1
                            clipperOffset.Execute(ref results, insideOffset * decimalCorrectionFactor);

                            foreach (var offsetPolygon in results.Childs)
                            {
                                holes.Add(offsetPolygon);
                            }
                        }
                    }
                }

                //prevent line crossing first union the holes (removes holes intersections)
                var unionHolesPolyTree = new PolyTree();
                if (holes.Count > 1)
                {
                    var clipper = new Clipper();
                    foreach (var hole in holes)
                    {
                        clipper.AddPath(hole.Contour, PolyType.ptSubject, true);
                    }


                    clipper.Execute(ClipType.ctUnion, unionHolesPolyTree, PolyFillType.pftNonZero, PolyFillType.pftNonZero);
                }
                else if (holes.Count == 1)
                {
                    unionHolesPolyTree.AddChild(holes[0]);
                }


                //prevent line crossing second union the outsides (removes outside intersections)
                var unionOutsidesPolyTree = new PolyTree();
                if (outsides.Count > 1)
                {
                    var clipper = new Clipper();
                    foreach (var outside in outsides)
                    {
                        clipper.AddPath(outside.Contour, PolyType.ptSubject, true);
                    }


                    clipper.Execute(ClipType.ctUnion, unionOutsidesPolyTree, PolyFillType.pftNonZero, PolyFillType.pftNonZero);
                }
                else if (outsides.Count == 1)
                {
                    unionOutsidesPolyTree.AddChild(outsides[0]);
                }

                //subtract holes from outsides
                var diffPolyTree        = new PolyTree();
                var diffPolyTreeClipper = new Clipper();
                foreach (var unionPolyNode in unionOutsidesPolyTree.Childs)
                {
                    diffPolyTreeClipper.AddPath(unionPolyNode.Contour, PolyType.ptSubject, true);
                }

                foreach (var unionPolyNode in unionHolesPolyTree.Childs)
                {
                    diffPolyTreeClipper.AddPath(unionPolyNode.Contour, PolyType.ptClip, true);
                }

                diffPolyTreeClipper.Execute(ClipType.ctDifference, diffPolyTree, PolyFillType.pftNonZero);

                //    var triangle3D = new Triangle();


                //            foreach (var offsetPolygon in results.Childs)
                //            {
                //                //change order to CCW
                //                var directionIsCW = Clipper.Orientation(offsetPolygon.Contour);
                //                if (directionIsCW)
                //                {
                //                    Clipper.ReversePaths(new List<List<IntPoint>>() { offsetPolygon.Contour });
                //                }

                //                //convert to 3d triangle and determine normal
                //                for (var contourIndex = 0; contourIndex < offsetPolygon.Contour.Count; contourIndex++)
                //                {
                //                    triangle3D.Vectors[0].Position = new Vector3(offsetPolygon.Contour[contourIndex].X, offsetPolygon.Contour[contourIndex].Y, 0);

                //                    if (contourIndex == offsetPolygon.Contour.Count - 1)
                //                    {
                //                        triangle3D.Vectors[1].Position = new Vector3(offsetPolygon.Contour[0].X, offsetPolygon.Contour[0].Y, 0);
                //                        triangle3D.Vectors[2].Position = new Vector3(offsetPolygon.Contour[0].X, offsetPolygon.Contour[0].Y, 1);
                //                    }
                //                    else
                //                    {
                //                        triangle3D.Vectors[1].Position = new Vector3(offsetPolygon.Contour[contourIndex + 1].X, offsetPolygon.Contour[contourIndex + 1].Y, 0);
                //                        triangle3D.Vectors[2].Position = new Vector3(offsetPolygon.Contour[contourIndex + 1].X, offsetPolygon.Contour[contourIndex + 1].Y, 1);
                //                    }

                //                    triangle3D.Vectors[0].Position /= decimalCorrectionFactor;
                //                    triangle3D.Vectors[1].Position /= decimalCorrectionFactor;
                //                    triangle3D.Vectors[2].Position /= decimalCorrectionFactor;

                //                    triangle3D.CalcNormal();

                //                    var triangleNormal = triangle3D.Normal;
                //                    if (!pointInPath.IsHole)
                //                    {
                //                        triangleNormal = triangleNormal * -1;
                //                    }


                //                    var line = new SliceLine2D();
                //                    line.Normal = triangleNormal;

                //                    if (contourIndex == offsetPolygon.Contour.Count - 1)
                //                    {
                //                        line.p1 = new SlicePoint2D() { X = triangle3D.Vectors[0].Position.X, Y = triangle3D.Vectors[0].Position.Y};
                //                        //line.p2 = new SlicePoint2D() { X = offsetPolygon.Contour[0].X / decimalCorrectionFactor, Y = offsetPolygon.Contour[0].Y / decimalCorrectionFactor };
                //                        //

                //                        line.p2 = new SlicePoint2D() { X = triangle3D.Vectors[1].Position.X, Y = triangle3D.Vectors[1].Position.Y };
                //                    }
                //                    else
                //                    {
                //                        line.p1 = new SlicePoint2D() { X = triangle3D.Vectors[0].Position.X, Y = triangle3D.Vectors[0].Position.Y};
                //                        line.p2 = new SlicePoint2D() { X = triangle3D.Vectors[1].Position.X, Y = triangle3D.Vectors[1].Position.Y};
                //                    }

                //                    result.Add(line);

                //                }
                //            }
                //        }
                //    }
            }

            return(result);
        }