forked from ricardojmendez/UnitySteer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3Pathway.cs
288 lines (250 loc) · 10.1 KB
/
Vector3Pathway.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// ----------------------------------------------------------------------------
//
// Written by Ricardo J. Mendez http://www.arges-systems.com/ based on
// OpenSteer's PolylinePathway.
//
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publfish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
//
// ----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace UnitySteer
{
/// <summary>
/// Represents a Pathway created from a list of Vector3s
/// </summary>
public class Vector3Pathway : IPathway
{
#region Properties
/// <summary>
/// List of segment lengths
/// </summary>
/// <value>The segment lengths.</value>
protected IList<float> Lengths { get; private set; }
/// <summary>
/// List of calculated normals between points.
/// </summary>
protected IList<Vector3> Normals { get; private set; }
public IList<Vector3> Path { get; protected set; }
public Vector3 FirstPoint
{
get { return Path.FirstOrDefault(); }
}
public Vector3 LastPoint
{
get { return Path.LastOrDefault(); }
}
public float TotalPathLength { get; protected set; }
public int SegmentCount
{
get { return Path.Count; }
}
public float Radius { get; set; }
#endregion
public Vector3Pathway()
{
Lengths = null;
Normals = null;
}
/// <summary>
/// Constructs a Vector3Pathway given an array of points and a path radius
/// </summary>
/// <param name="path">
/// List of Vector3 to be used for the path in world space <see cref="Vector3" />
/// </param>
/// <param name="radius">
/// Radius to use for the connections <see cref="System.Single" />
/// </param>
/// <remarks>
/// The current implementation assumes that all pathways will
/// have the same radius.
/// </remarks>
public Vector3Pathway(IList<Vector3> path, float radius)
{
Initialize(path, radius);
}
/// <summary>
/// Constructs the Pathway from a list of Vector3
/// </summary>
/// <param name="path">
/// A list of Vector3 defining the path points in world space<see cref="Vector3" />
/// </param>
/// <param name="radius">
/// Radius to use for the connections<see cref="System.Single" />
/// </param>
public void Initialize(IList<Vector3> path, float radius)
{
Path = new List<Vector3>(path);
Radius = radius;
PrecalculatePathData();
}
/// <summary>
/// Precalculates any necessary path data, such as segment normals.
/// </summary>
protected virtual void PrecalculatePathData()
{
// set data members, allocate arrays
var pointCount = Path.Count;
TotalPathLength = 0;
Lengths = new List<float>(pointCount);
Normals = new List<Vector3>(pointCount);
Lengths.Add(0);
Normals.Add(Vector3.zero);
// loop over all points
for (var i = 1; i < pointCount; i++)
{
// compute the segment length
var normal = Path[i] - Path[i - 1];
var length = normal.magnitude;
Lengths.Add(length);
Normals.Add(normal / length);
TotalPathLength += length;
}
}
/// <summary>
/// Given an arbitrary point ("A"), returns the nearest point ("P") on
/// this path. Also returns, via output arguments, the path Tangent at
/// P and a measure of how far A is Outside the Pathway's "tube". Note
/// that a negative distance indicates A is inside the Pathway.
/// </summary>
/// <param name="point">Reference point.</param>
/// <param name="pathRelative">Structure indicating the relative path position.</param>
/// <returns>The closest point to the received reference point.</returns>
public virtual Vector3 MapPointToPath(Vector3 point, ref PathRelativePosition pathRelative)
{
var minDistance = float.MaxValue;
var onPath = Vector3.zero;
pathRelative.SegmentIndex = -1;
// loop over all segments, find the one nearest to the given point
for (var i = 1; i < Path.Count; i++)
{
var segmentLength = Lengths[i];
var segmentNormal = Normals[i];
var chosenPoint = Vector3.zero;
var d = OpenSteerUtility.PointToSegmentDistance(point, Path[i - 1], Path[i],
segmentNormal, segmentLength,
ref chosenPoint);
if (!(d < minDistance)) continue;
minDistance = d;
onPath = chosenPoint;
pathRelative.Tangent = segmentNormal;
pathRelative.SegmentIndex = i;
}
// measure how far original point is Outside the Pathway's "tube"
pathRelative.Outside = (onPath - point).magnitude - Radius;
// return point on path
return onPath;
}
/// <summary>
/// Maps the reference point to a distance along the path.
/// </summary>
/// <param name="point">Reference point.</param>
/// <returns>The distance along the path for the point.</returns>
public virtual float MapPointToPathDistance(Vector3 point)
{
if (Path.Count < 2)
return 0;
var minDistance = float.MaxValue;
float segmentLengthTotal = 0;
float pathDistance = 0;
for (var i = 1; i < Path.Count; i++)
{
var segmentProjection = 0f;
var segmentLength = Lengths[i];
var segmentNormal = Normals[i];
var d = OpenSteerUtility.PointToSegmentDistance(point, Path[i - 1], Path[i],
segmentNormal, segmentLength,
ref segmentProjection);
if (d < minDistance)
{
minDistance = d;
pathDistance = segmentLengthTotal + segmentProjection;
}
segmentLengthTotal += segmentLength;
}
// return distance along path of onPath point
return pathDistance;
}
/// <summary>
/// Given a distance along the path, convert it to a specific point on the path.
/// </summary>
/// <param name="pathDistance">Path distance to calculate corresponding point for.</param>
/// <returns>The corresponding path point to the path distance.</returns>
public virtual Vector3 MapPathDistanceToPoint(float pathDistance)
{
// clip or wrap given path distance according to cyclic flag
var remaining = pathDistance;
if (pathDistance < 0)
return Path.First();
if (pathDistance >= TotalPathLength)
return Path.Last();
// step through segments, subtracting off segment lengths until
// locating the segment that contains the original pathDistance.
// Interpolate along that segment to find 3d point value to return.
var result = Vector3.zero;
for (var i = 1; i < Path.Count; i++)
{
var segmentLength = Lengths[i];
if (segmentLength < remaining)
{
remaining -= segmentLength;
}
else
{
var ratio = remaining / segmentLength;
result = Vector3.Lerp(Path[i - 1], Path[i], ratio);
break;
}
}
return result;
}
/// <summary>
/// Determines whether the received point is inside the path.
/// </summary>
/// <param name="point">Point to evaluate.</param>
/// <returns><c>true</c> if the point is inside the path; otherwise, <c>false</c>.</returns>
public bool IsInsidePath(Vector3 point)
{
var tStruct = new PathRelativePosition();
MapPointToPath(point, ref tStruct);
return tStruct.Outside < 0;
}
/// <summary>
/// Calculates how far Outside the path is the reference point.
/// </summary>
/// <param name="point">Reference point.</param>
/// <returns>How far Outside the path is the reference point.</returns>
public float HowFarOutsidePath(Vector3 point)
{
var tStruct = new PathRelativePosition();
MapPointToPath(point, ref tStruct);
return tStruct.Outside;
}
public virtual void DrawGizmos()
{
for (var i = 0; i < Path.Count - 1; i++)
{
Debug.DrawLine(Path[i], Path[i + 1], Color.green);
}
}
}
}