AR + GPS Location  3.0.0
All Classes Namespaces Functions Variables Properties Events Pages
Spline.cs
1 using System.Collections.Generic;
2 using UnityEngine;
3 
4 namespace ARLocation
5 {
6  public enum SplineType
7  {
8  CatmullromSpline,
9  LinearSpline,
10  }
11 
12  public abstract class Spline
13  {
14 
18  public Vector3[] Points { get; protected set; }
19 
23  protected Curve[] segments;
24 
28  protected int segmentCount = 0;
29 
33  public float Length { get; protected set; }
34 
35  protected float[] lengths;
36 
41  public abstract void CalculateSegments(int n);
42 
48  public Vector3 GetPointAtArcLength(float s)
49  {
50  s = Mathf.Clamp(s, 0, Length);
51 
52  for (var i = 0; i < segmentCount; i++)
53  {
54  if (s <= lengths[i])
55  {
56  var offset = i == 0 ? 0 : lengths[i - 1];
57  return segments[i].GetPointAtLength(s - offset);
58  }
59  }
60  return segments[segmentCount - 1].GetPoint(1);
61  }
62 
70  {
71  s = Mathf.Clamp(s, 0, Length);
72 
73  for (var i = 0; i < segmentCount; i++)
74  {
75  if (s <= lengths[i])
76  {
77  var offset = i == 0 ? 0 : lengths[i - 1];
78  return segments[i].GetPointAndTangentAtLength(s - offset);
79  }
80  }
81  return segments[segmentCount - 1].GetPointAndTangentAtLength(1);
82  }
83 
91  public void DrawCurveWithLineRenderer(LineRenderer renderer, System.Func<Vector3, Vector3> func, int n = 100)
92  {
93  var points = new List<Vector3>();
94 
95  float s = 0.0f;
96  while (s <= Length)
97  {
98  var pointData = GetPointAndTangentAtArcLength(s);
99  points.Add(func(pointData.point));
100  s += Length / (n + 1.0f);
101  }
102 
103  var arr = points.ToArray();
104  renderer.positionCount = arr.Length;
105  renderer.SetPositions(arr);
106  }
107 
114  public Vector3[] SamplePoints(int n, System.Func<Vector3, Vector3> func)
115  {
116  var sample = new Vector3[n + 2];
117  var delta = Length / (n + 1.0f);
118 
119  var s = 0.0f;
120  for (var i = 0; i < (n + 2); i++)
121  {
122  sample[i] = func(GetPointAtArcLength(s));
123  s += delta;
124  }
125 
126  return sample;
127  }
128 
134  public Vector3[] SamplePoints(int n)
135  {
136  return SamplePoints(n, (p) => p);
137  }
138 
142  public void DrawGizmos()
143  {
144  DrawPointsGizmos();
145  DrawCurveLengthGizmos();
146  }
147 
148  private void DrawPointsGizmos()
149  {
150  foreach (var p in Points)
151  {
152  Gizmos.color = Color.blue;
153  Gizmos.DrawSphere(p, 0.1f);
154  }
155  }
156 
157  private void DrawCurveLengthGizmos()
158  {
159  var p = GetPointAtArcLength(0f);
160  float s = 0.0f;
161  while (s <= Length)
162  {
163  Gizmos.color = Color.green;
164  var pointData = GetPointAndTangentAtArcLength(s);
165  Vector3 n = pointData.point;
166  Gizmos.DrawLine(p, n);
167  p = n;
168  s += 0.1f;
169  Gizmos.color = Color.magenta;
170  var tan = pointData.tangent;
171  Gizmos.color = Color.blue;
172  Gizmos.DrawLine(n, n + tan);
173  }
174  }
175  }
176 }
ARLocation.Spline.DrawGizmos
void DrawGizmos()
Draw the curve and sample point using Gizmos.
Definition: Spline.cs:142
ARLocation.Spline.segmentCount
int segmentCount
The number of segments that make up the spline.
Definition: Spline.cs:28
ARLocation.Spline.GetPointAndTangentAtArcLength
CurvePointData GetPointAndTangentAtArcLength(float s)
Returns a CurvePointData whith the point and tangent of the spline at a given arc-length.
Definition: Spline.cs:69
ARLocation.Spline.segments
Curve[] segments
The CatmullRom curve-segments of the spline.
Definition: Spline.cs:23
ARLocation.Spline.Length
float Length
The full (estimated) length of the spline.
Definition: Spline.cs:33
ARLocation.Spline.CalculateSegments
abstract void CalculateSegments(int n)
Calculate the catmull-rom segments. Also estimates the curve's length.
ARLocation.Spline.DrawCurveWithLineRenderer
void DrawCurveWithLineRenderer(LineRenderer renderer, System.Func< Vector3, Vector3 > func, int n=100)
Draws the curve using a given LineRenderer, with points being processed by a given function beforehan...
Definition: Spline.cs:91
ARLocation.CurvePointData
A struct holding a pair of point/tangent values.
Definition: Curve.cs:10
ARLocation.Spline.SamplePoints
Vector3[] SamplePoints(int n, System.Func< Vector3, Vector3 > func)
Calculates a sample of (N+2) equidistant points along the spline.
Definition: Spline.cs:114
ARLocation.Curve
Definition: Curve.cs:17
ARLocation.Spline.SamplePoints
Vector3[] SamplePoints(int n)
Calculates a sample of (N+2) equidistant points along the spline.
Definition: Spline.cs:134
ARLocation.Spline.GetPointAtArcLength
Vector3 GetPointAtArcLength(float s)
Returns the point of the spline at a given arc-length.
Definition: Spline.cs:48
ARLocation.Spline
Definition: Spline.cs:13
ARLocation
Definition: ARLocationConfigInspector.cs:7
ARLocation.Spline.Points
Vector3[] Points
The points interpolated of the spline.
Definition: Spline.cs:18