using UnityEngine;
using System;
/**
* @class SplineSegment
*
* @brief This class represents a pair of two control nodes that define a segment on the Spline.
*
* A spline segment is represented by two control nodes. This class stores two references to such nodes and provides
* useful functions that allow you to convert a parameter that represents a point on the segment to a normalized
* spline parameter that represents the same point on the spline.
* This class becomes quite useful when handling Bézier curves!
*/
///
/// This class represents a pair of two control nodes that define a segment on the Spline.
///
///
/// A spline segment is represented by two control nodes. This class stores two references to such nodes and provides
/// useful functions that allow you to convert a parameter that represents a point on the segment to a normalized
/// spline parameter that represents the same point on the spline.
/// This class becomes quite useful when handling Bézier curves!
///
public class SplineSegment
{
private readonly Spline parentSpline;
private readonly SplineNode startNode;
private readonly SplineNode endNode;
public Spline ParentSpline { get{ return parentSpline; } } ///< Returns a reference to the containing spline.
public SplineNode StartNode { get{ return startNode; } } ///< Returns a reference to the spline segment's start point.
public SplineNode EndNode { get{ return endNode; } } ///< Returns a reference to the spline segment's end point.
public float Length {
get {
return (startNode.Parameters[parentSpline].Length * parentSpline.Length);
}
} ///< Returns the actual length of the spline segment.
public float NormalizedLength {
get {
return startNode.Parameters[parentSpline].Length;
}
} ///< Returns the normlaized length of the segment in the spline.
///
/// Initializes a new instance of the class.
///
///
/// The spline that contains the segment.
///
///
/// The segment's start node.
///
///
/// The segment's end node.
///
///
/// Is thrown when pSpline is null /> .
///
public SplineSegment( Spline pSpline, SplineNode sNode, SplineNode eNode )
{
if( pSpline != null )
{
parentSpline = pSpline;
startNode = sNode;
endNode = eNode;
}
else
{
throw new ArgumentNullException( "Parent Spline must not be null" );
}
}
///
/// This method converts a parameter [0..1] representing a point on the segment to a normalized parameter [0..1] representing a point on the whole spline.
///
///
/// A normalized spline parameter.
///
///
/// The normalized segment parameter.
///
public float ConvertSegmentToSplineParamter( float param )
{
return (startNode.Parameters[parentSpline].PosInSpline + param * startNode.Parameters[parentSpline].Length);
}
///
/// This method converts a parameter [0..1] representing a point on the whole spline to a normalized parameter [0..1] representing a point on the segment.
///
///
/// The normalized segment parameter.
///
///
/// The normalized spline parameter.
///
public float ConvertSplineToSegmentParamter( float param )
{
if( param < startNode.Parameters[parentSpline].PosInSpline )
return 0;
if( param > startNode.Parameters[parentSpline].PosInSpline + startNode.Parameters[parentSpline].Length )
return 1;
return ( (param - startNode.Parameters[parentSpline].PosInSpline) / startNode.Parameters[parentSpline].Length );
}
///
/// This method clamps a normalized spline parameter to spline parameters defining the segment. The returned parameter will only represent points on the segment.
///
///
/// A clamped spline parameter that will only represent points on the segment.
///
///
/// A normalized spline parameter.
///
public float ClampParameterToSegment( float param )
{
if( param < startNode.Parameters[parentSpline].PosInSpline )
return startNode.Parameters[parentSpline].PosInSpline;
if( param > startNode.Parameters[parentSpline].PosInSpline + startNode.Parameters[parentSpline].Length )
return startNode.Parameters[parentSpline].PosInSpline + startNode.Parameters[parentSpline].Length;
return param;
}
///
/// This method checks whether a normalized spline parameter lies within the parameter interval of this spline segment.
///
///
/// True if the parameter lies within the parameter interval of this spline segment.
///
///
/// A normalized spline parameter.
///
public bool IsParameterInRange( float param )
{
if( Mathf.Approximately( param, startNode.Parameters[parentSpline].PosInSpline ) )
return true;
if( Mathf.Approximately( param, startNode.Parameters[parentSpline].PosInSpline + startNode.Parameters[parentSpline].Length ) )
return true;
if( param < startNode.Parameters[parentSpline].PosInSpline )
return false;
if( param > startNode.Parameters[parentSpline].PosInSpline + startNode.Parameters[parentSpline].Length )
return false;
return true;
}
}