Ejemplo n.º 1
0
        /// <summary>
        /// Insert a control point at index.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="controlPoint"></param>
        public SplineModificationInfo InsertControlPoint(int index, KochanekBartelsControlPoint controlPoint)
        {
            ControlPoints.Insert(index, controlPoint);

            if (ControlPoints.Count < 3 && InterpolatedPoints.Count < 2 * Steps)
            {
                Debug.LogWarning("KochanekBartelsSpline: Control point was added but the line can only be interpolated when there are at least 3 control points.");
                return(new SplineModificationInfo(0, 0, 0));
            }
            else if (ControlPoints.Count == 3 && InterpolatedPoints.Count == Steps)
            {
                this.SetControlPoints(ControlPoints.ToArray());
                return(new SplineModificationInfo(0, Steps, 2 * Steps));
            }
            else if (ControlPoints.Count == 3)
            {
                this.SetControlPoints(ControlPoints.ToArray());
                return(new SplineModificationInfo(0, 0, 2 * Steps));
            }

            //determine which segments have to be reinterpolated
            int start = (index - 2) >= 0 ? (index - 2) : 0;
            int end   = (index + 1) <= (ControlPoints.Count - 2) ? (index + 1) : (ControlPoints.Count - 2);

            for (int i = start; i <= end; i++)
            {
                try
                {
                    if (index == ControlPoints.Count - 1)
                    {
                        //if we are inserting the control point as the new last element
                        //dont try to remove the segment before the new control point because it doesnt exist yet
                        if (i != index - 1)
                        {
                            //dont remove at added index so a new segment is added
                            InterpolatedPoints.RemoveRange(i * Steps, Steps);
                        }
                    }
                    else
                    {
                        if (i != index)
                        {
                            //dont remove at added index so a new segment is added
                            InterpolatedPoints.RemoveRange(i * Steps, Steps);
                        }
                    }
                    InterpolatedPoints.InsertRange(i * Steps, InterpolateSegment(i));
                }
                catch (ArgumentException exception)
                {
                    Debug.LogError("Can't insert control point at index that doesnt exist yet, add instead!\n" + exception);
                }
            }

            return(new SplineModificationInfo(start * Steps, (end - start) * Steps, (end - start + 1) * Steps));
        }
        /// <summary>
        /// Delete the control point at index.
        /// If the index is not the first or last control point the curve will skip the deleted control point and connect the control point before and after the deleted one.
        /// </summary>
        /// <param name="index"></param>
        public override SplineModificationInfo deleteControlPoint(int index)
        {
            //if ((ControlPoints.Count - 1) < 3) {
            //    Debug.LogError("Cannot remove more control points, minimum number is 3.");
            //    return new SplineModificationInfo(0,0,0);
            //}

            //Check if there is only one point left
            if (ControlPoints.Count == 1 && index == 0)
            {
                //nothing left to remove
            }
            else if (index == (ControlPoints.Count - 1))
            {
                InterpolatedPoints.RemoveRange((index - 1) * Steps, Steps);
            }
            else
            {
                InterpolatedPoints.RemoveRange(index * Steps, Steps);
            }

            ControlPoints.RemoveAt(index);

            if (ControlPoints.Count == 2)
            {
                int startIndex = (index - 1) >= 0 ? (index - 1) : 0;
                return(new SplineModificationInfo(startIndex * Steps, Steps, 0));
            }
            else if (ControlPoints.Count < 2)
            {
                return(new SplineModificationInfo(0, index * Steps, 0));
            }

            //determine which segments have to be reinterpolated
            int start = (index - 2) >= 0 ? (index - 2) : 0;
            int end   = (index + 1) <= (ControlPoints.Count - 2) ? (index + 1) : (ControlPoints.Count - 2);

            for (int i = start; i <= end; i++)
            {
                try
                {
                    InterpolatedPoints.RemoveRange(i * Steps, Steps);
                    InterpolatedPoints.InsertRange(i * Steps, InterpolateSegment(i));
                }
                catch (ArgumentException exception)
                {
                    Debug.LogError("Can't delete control point at index that doesnt exist yet!\n" + exception);
                }
            }

            return(new SplineModificationInfo(start * Steps, (end - start + 2) * Steps, (end - start + 1) * Steps));
        }
 /// <summary>
 /// Recalculate the complete spline.
 /// </summary>
 /// <param name="steps">Steps for each segment</param>
 private void InterpolateSpline()
 {
     InterpolatedPoints.Clear();
     if (ControlPoints.Count < 3)
     {
         Debug.LogError("Not enough control points! Minimum is 3.");
     }
     for (int i = 0; i < ControlPoints.Count - 1; i++)
     {
         List <Vector3> points = InterpolateSegment(i);
         if (points != null && points.Count > 0)
         {
             InterpolatedPoints.AddRange(points);
         }
     }
 }
        /// <summary>
        /// Reset the existing control point at index.
        /// </summary>
        /// <param name="index"></param>
        /// <param name="controlPoints"></param>
        public SplineModificationInfo setControlPoint(int index, KochanekBartelsControlPoint controlPoint)
        {
            ControlPoints[index] = controlPoint;

            int start = (index - 2) >= 0 ? (index - 2) : 0;
            int end   = (index + 1) <= (ControlPoints.Count - 2) ? (index + 1) : (ControlPoints.Count - 2);

            for (int i = start; i <= end; i++)
            {
                try
                {
                    InterpolatedPoints.RemoveRange(i * Steps, Steps);
                    InterpolatedPoints.InsertRange(i * Steps, InterpolateSegment(i));
                }
                catch (ArgumentException exception)
                {
                    Debug.LogError("Can't set control point that doesnt exist yet, add instead!\n" + exception);
                }
            }

            return(new SplineModificationInfo(start * Steps, (end - start + 1) * Steps, (end - start + 1) * Steps));
        }