Ejemplo n.º 1
0
        /// <summary>
        /// Проверка самопересечения полилинии
        /// </summary>
        public static PolygonValidateResult IsValidPolygon(
            [NotNull] this Polyline pline,
            [NotNull] out List <Point3d> intersections,
            double tolerance = 0.01)
        {
            intersections = new List <Point3d>();
            var result = PolygonValidateResult.OK;

            if (!pline.Closed)
            {
                result += 1;
            }

            var t = new Tolerance(tolerance, tolerance);

            using (var curve1 = pline.GetGeCurve(t))
                using (var curve2 = pline.GetGeCurve(t))
                    using (var curveInter = new CurveCurveIntersector3d(curve1, curve2, pline.Normal, t))
                    {
                        var interCount = curveInter.NumberOfIntersectionPoints;
                        var overlaps   = curveInter.OverlapCount();
                        if (!pline.Closed)
                        {
                            overlaps += 1;
                        }
                        if (overlaps < pline.NumberOfVertices)
                        {
                            result += 2;
                        }
                        if (interCount > overlaps)
                        {
                            result += 4;
                            var plPts = pline.GetPoints().DistinctPoints().Select(s => s.Convert3d()).ToList();
                            for (var i = 0; i < interCount; i++)
                            {
                                intersections.Add(curveInter.GetIntersectionPoint(i));
                            }
                            var onlyIntersects = intersections.Except(plPts).ToList();
                            if (onlyIntersects.Any())
                            {
                                intersections = onlyIntersects;
                            }
                        }
                    }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Полилиния пересекает сама себя
        /// </summary>
        /// <param name="poly"></param>
        /// <returns></returns>
        public static bool PolylineIsSelfIntersecting(Polyline poly)
        {
            Curve3d curve3D = poly.GetGeCurve();
            CurveCurveIntersector3d intersector = new CurveCurveIntersector3d(curve3D, curve3D, Vector3d.ZAxis);

            return(intersector.NumberOfIntersectionPoints > 0);

            /*
             * Dictionary<Point3d, int?> polyPts = new Dictionary<Point3d, int?>();
             * int vertCount = poly.NumberOfVertices;
             * for (int i = 0; i < vertCount; i++)
             * {
             *  Point3d pt3d = poly.GetPoint3dAt(i);
             *  //Если точки повторяются, то есть самопересечение
             *  //Но допускается повторение первой и последней точек
             *  int? existPtNum = null;
             *  polyPts.TryGetValue(pt3d, out existPtNum);
             *  if (existPtNum == null//Такой точки не было
             || (i == vertCount - 1 && existPtNum == 0))//Такая тока была, но это замыкание, а не самопересечение
             || {
             ||     polyPts[pt3d] = i;
             || }
             || else
             ||     return true;
             ||}
             ||//Использование метода IntersectWith
             ||Point3dCollection intersectionPts = new Point3dCollection();
             ||poly.IntersectWith(poly, Intersect.OnBothOperands, intersectionPts, IntPtr.Zero, IntPtr.Zero);
             ||
             ||foreach (Point3d intersectionPt in intersectionPts)
             ||{
             || if (!polyPts.ContainsKey(intersectionPt))
             || {
             ||     return true;
             || }
             ||}
             ||
             ||return false;
             */
        }