Ejemplo n.º 1
0
    public XingType GetXingType(Node parent, Node test, int j)
    {
        XingType type = XingType.Free;

        List <float> dLines      = new List <float>();
        List <float> dIntersects = new List <float>();

        float dMinIntersect = float.MaxValue;
        int   iIntersect    = -1;

        List <float> dP1toIntersects = new List <float>();
        List <int>   iIntersects     = new List <int>();

        Vector3
            tPos = test.Position,
            pPos = parent.Position,
            sPos, ePos;

        for (int i = 0; i < Segments.Count; i++)
        {
            if (Segments[i].StartNode != parent && Segments[i].EndNode != parent)
            {
                sPos = Segments[i].StartNode.Position;
                ePos = Segments[i].EndNode.Position;
                dLines.Add(LineHelper.GetDistanceToLine(tPos, sPos, ePos));

                if (LineHelper.DoSegmentsIntersect(pPos, tPos, sPos, ePos))
                {
                    float dIntersect = LineHelper.GetDistanceToLine(pPos, sPos, ePos);

                    if (dIntersect < dMinIntersect)
                    {
                        dMinIntersect = dIntersect;
                        iIntersect    = i;
                    }
                }
            }
            else
            {
                dLines.Add(float.MaxValue);
            }
        }

        if (iIntersect != -1)
        {
            type = XingType.Crossing;

            Segment intersect = Segments[iIntersect];
            sPos = intersect.StartNode.Position;
            ePos = intersect.EndNode.Position;
            tPos = LineHelper.ProjectPointToLine(tPos, sPos, ePos);

            if ((tPos - sPos).magnitude < (sPos - ePos).magnitude / 4)
            {
                type = XingType.Ending;
                tPos = sPos;
            }
            else if ((tPos - ePos).magnitude < (sPos - ePos).magnitude / 4)
            {
                type = XingType.Ending;
                tPos = ePos;
            }
        }
        else
        {
            float   minDistance    = dLines.Min();
            Segment closestSegment = Segments[dLines.IndexOf(minDistance)];
            sPos = closestSegment.StartNode.Position;
            ePos = closestSegment.EndNode.Position;

            if (minDistance < (sPos - ePos).magnitude / 2)
            {
                type = XingType.Ending;
                tPos = LineHelper.ProjectPointToLine(tPos, sPos, ePos);

                if ((tPos - sPos).magnitude < (sPos - ePos).magnitude / 4)
                {
                    tPos = sPos;
                }
                else if ((tPos - ePos).magnitude < (sPos - ePos).magnitude / 4)
                {
                    tPos = ePos;
                }
            }
        }

        if ((tPos - pPos).magnitude < GetLengthPotential(test.Value) / 2)
        {
            type = XingType.Trivial;
        }

        test.Position = tPos;
        return(type);
    }