Esempio n. 1
0
    public List <int> RefinePath(Dictionary <int, Vector3> closestPositions, float meanDist)
    {
        Debug.Log("Start RefinePath");
        float minCircleCirc = 2f / Statics.meanDist;
        int   forecastDepth = (int)minCircleCirc;

        Debug.Log("Path count = " + Count + ", refine with depth " + forecastDepth);
        if (Count > 0)
        {
            int j = 0;
            while (j < Count - 1)
            {
                var point = GetPosition(j);
                var next  = true;
                for (int i = 1; i < forecastDepth; i++)
                {
                    if (j + i < Count - 1)
                    {
                        var nextPoint = GetPosition(j + i);
                        var distance  = Vector3.Distance(point, nextPoint);
                        if (i == 1 && distance > meanDist * 1.01f)
                        {
                            var midPoint = Vector3.Lerp(point, nextPoint, Mathf.Clamp01(meanDist / distance));
                            InsertPositionAt(j + 1, midPoint);
                            if (line.hasNormals)
                            {
                                line.InsertNormalAt(j + 1, line.GetNormal(j));
                            }
                            next = false;
                            break;
                        }
                        if (distance < meanDist * 0.99f)
                        {
                            bool removed = false;
                            for (int k = j + 1; k <= j + i; k++)
                            {
                                if (k < Count - 1)
                                {
                                    line.RemovePosition(k);
                                    line.RemoveNormal(k);
                                    removed = true;
                                }
                            }
                            if (removed)
                            {
                                next = false;
                                break;
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                if (next)
                {
                    j++;
                }
            }
        }

        var closeNodePositions = new List <int> ();

        if (closestPositions != null)
        {
            foreach (var pair in closestPositions)
            {
                var index = pair.Key;
                if (index < line.positionCount)
                {
                    var vector3 = GetPosition(index);
                    for (int node = 0; node < Count; node++)
                    {
                        if (Vector3.Distance(GetPosition(node), vector3) < meanDist)
                        {
                            closeNodePositions.Add(node);
                            break;
                        }
                    }
                }
            }
        }

        Debug.Log("Refined Path " + pathNumber + ", length after: " + Count);
        return(closeNodePositions);
    }