Beispiel #1
0
    public static List <Vector4> ResampleControlPoints(List <Vector4> controlPoints, float segmentLength)
    {
        int nP = controlPoints.Count;

        //insert a point at the end and at the begining
        controlPoints.Insert(0, controlPoints[0] + (controlPoints[0] - controlPoints[1]) / 2.0f);
        controlPoints.Add(controlPoints[nP - 1] + (controlPoints[nP - 1] - controlPoints[nP - 2]) / 2.0f);

        var resampledControlPoints = new List <Vector4>();

        resampledControlPoints.Add(controlPoints[0]);
        resampledControlPoints.Add(controlPoints[1]);

        var currentPointId  = 1;
        var currentPosition = controlPoints[currentPointId];

        //distance = DisplaySettings.Get.DistanceContraint;
        float lerpValue = 0.0f;

        // Normalize the distance between control points
        while (true)
        {
            if (currentPointId + 2 >= controlPoints.Count)
            {
                break;
            }
            //if (currentPointId + 2 >= 100) break;

            var cp0 = controlPoints[currentPointId - 1];
            var cp1 = controlPoints[currentPointId];
            var cp2 = controlPoints[currentPointId + 1];
            var cp3 = controlPoints[currentPointId + 2];

            var found = false;

            for (; lerpValue <= 1; lerpValue += 0.01f)
            {
                var candidate = MyUtility.CubicInterpolate(cp0, cp1, cp2, cp3, lerpValue);
                var d         = Vector3.Distance(currentPosition, candidate);

                if (d > segmentLength)
                {
                    resampledControlPoints.Add(candidate);
                    currentPosition = candidate;
                    found           = true;
                    break;
                }
            }

            if (!found)
            {
                lerpValue = 0;
                currentPointId++;
            }
        }

        return(resampledControlPoints);
    }