Exemple #1
0
 public void Reset()
 {
     AlphaCut   = default(double);
     ActiveEdge = default(TEdge);
     Case       = IntersectionCase.NotIntersecting;
     ActiveLine = default(BoundaryLine);
 }
        /// <summary>
        /// Depending on the enum value the intervals are merged or are staying the same
        /// </summary>
        /// <param name="intervals">The enumerable of intervals coming from the user</param>
        /// <returns>The edited list with merged intervals</returns>
        public static List<Interval> Merge(IEnumerable<Interval> intervals)
        {
            List<Interval> sortedList = new List<Interval>();
            sortedList.AddRange(intervals.OrderBy(e => e.Start));

            for (int i = 0; i < sortedList.Count() - 1; i++)
            {
                IntersectionCase intersectionCase = Compare(sortedList[i], sortedList[i + 1]);
                switch (intersectionCase)
                {
                    case IntersectionCase.AIntersectsB:
                        sortedList[i].End = sortedList[i + 1].End;
                        sortedList.RemoveAt(i + 1);
                        i--;
                        break;
                    //Doesn't happen because the list is sorted
                    case IntersectionCase.BIntersectsA:
                        sortedList[i].Start = sortedList[i + 1].Start;
                        sortedList.RemoveAt(i + 1);
                        i--;
                        break;
                    //Doesn't happen because the list is sorted
                    case IntersectionCase.AWithinB:
                        sortedList.RemoveAt(i);
                        i--;
                        break;
                    case IntersectionCase.BWithinA:
                        sortedList.RemoveAt(i + 1);
                        i--;
                        break;
                    case IntersectionCase.NoIntersection:
                    default:
                        break;
                }
            }
            return sortedList;
        }
Exemple #3
0
        //Return end of edge if parallel and overlapping.
        public static bool FindFirst(Line edge, Line line, ref IntersectionCase intersectionCase, out double alpha)
        {
            bool notParallel = PolygonClipping.ComputeIntersection(
                edge.Start.Position,
                edge.End.Position,
                line.Start.Position,
                line.End.Position,
                out alpha,
                out double alpha2,
                out Vector vector);

            if (notParallel)
            {
                if (1 >= alpha2)
                {
                    if (accuracy <= alpha2)
                    {
                        if (accuracy <= alpha && 1 >= alpha)
                        {
                            if (alpha > 1 - accuracy)
                            {
                                alpha            = 1;
                                intersectionCase = IntersectionCase.EndOfEdge;
                                if (alpha2 > 1 - accuracy)
                                {
                                    intersectionCase = IntersectionCase.EndOfEdgeAndLine;
                                }
                                return(true);
                            }
                            else
                            {
                                if (alpha2 > 1 - accuracy)
                                {
                                    intersectionCase = IntersectionCase.EndOfLine;
                                }
                                else
                                {
                                    intersectionCase = IntersectionCase.InMiddle;
                                }
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        if (alpha2 > -accuracy)
                        {
                            intersectionCase = IntersectionCase.StartOfLine;
                            return(true);
                        }
                    }
                }
                intersectionCase = IntersectionCase.NotIntersecting;
                return(false);
            }
            else
            {
                if (Math.Abs(alpha) <= accuracy)
                {
                    double absRidge = (edge.Start.Position - edge.End.Position).L2Norm();
                    double absLine  = (edge.Start.Position - line.End.Position).L2Norm();
                    //Only when in same direction!
                    double add = (edge.Start.Position - edge.End.Position + edge.Start.Position - line.End.Position).L2Norm();
                    if (add < absLine + absRidge)
                    {
                        return(false);
                    }

                    alpha = absLine / absRidge;
                    if (alpha >= 1 - accuracy && alpha <= 1)
                    {
                        alpha            = 1;
                        intersectionCase = IntersectionCase.EndOfEdgeAndLine;
                        return(true);
                    }
                    if (absLine / absRidge < 1)
                    {
                        intersectionCase = IntersectionCase.EndOfLine;
                        return(true);
                    }
                    alpha            = 1;
                    intersectionCase = IntersectionCase.EndOfEdge;
                    return(true);
                }
                intersectionCase = IntersectionCase.NotIntersecting;
                return(false);
            }
        }
Exemple #4
0
        public (IEnumerator <Edge <T> >, IntersectionCase) CutOut(
            IEnumerator <Edge <T> > edgeEnum,
            Edge <T> outerEdge,
            CutterState <Edge <T> > state)
        {
            bool             keepOnRunning    = true;
            Edge <T>         activeEdge       = default(Edge <T>);
            IntersectionCase intersectionCase = IntersectionCase.NotIntersecting;

            while (keepOnRunning)
            {
                keepOnRunning = false;
                double alphaCut = 0;
                state.ActiveLine = boundary.Current;
                while (edgeEnum.MoveNext() && !keepOnRunning)
                {
                    activeEdge    = edgeEnum.Current;
                    keepOnRunning = LineIntersect.Find(activeEdge, state.ActiveLine, ref intersectionCase, out alphaCut);
                    if (alphaCut < LineIntersect.accuracy)
                    {
                        intersectionCase = IntersectionCase.NotIntersecting;
                        keepOnRunning    = false;
                    }
                }
                switch (intersectionCase)
                {
                case IntersectionCase.NotIntersecting:
                case IntersectionCase.InMiddle:
                    //keepOnRunning = false;
                    //IEnumerator<Edge<T>> cellEnum = meshIntersecter.GetAfterCutEdgeEnumerator(state.ActiveEdge.Cell.Edges, state.ActiveEdge);
                    //return (cellEnum, intersectionCase);
                    keepOnRunning = false;
                    break;

                case IntersectionCase.EndOfLine:
                    activeEdge = meshIntersecter.AddLineSegment(activeEdge, alphaCut, boundary.LineIndex());
                    edgeEnum   = meshIntersecter.GetConnectedEdgeEnum(activeEdge);
                    outerEdge  = activeEdge;
                    if (!boundary.MoveNext())
                    {
                        keepOnRunning = false;
                    }
                    break;

                case IntersectionCase.EndOfEdge:
                    meshIntersecter.AddEdge(activeEdge, boundary.LineIndex());
                    edgeEnum = meshIntersecter.GetConnectedEdgeEnum(activeEdge);
                    //edgeEnum.MoveNext();
                    outerEdge = activeEdge;
                    break;

                case IntersectionCase.EndOfEdgeAndLine:
                    meshIntersecter.AddEdge(activeEdge, boundary.LineIndex());
                    edgeEnum  = meshIntersecter.GetConnectedEdgeEnum(activeEdge);
                    outerEdge = activeEdge;
                    if (!boundary.MoveNext())
                    {
                        keepOnRunning = false;
                    }
                    break;

                case IntersectionCase.StartOfLine:
                default:
                    throw new InvalidOperationException();
                }
            }
            MeshIntersecter <T> .AfterCutEdgeEnumerator cellEnumerator = meshIntersecter.GetNeighborFromLineDirection(outerEdge, state.ActiveLine);
            cellEnumerator.Cell.IntersectionVertex = outerEdge.End.ID;
            return(cellEnumerator, intersectionCase);
        }