-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine.cs
More file actions
293 lines (233 loc) · 9.72 KB
/
Line.cs
File metadata and controls
293 lines (233 loc) · 9.72 KB
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
289
290
291
292
293
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text.Json.Serialization;
using System.Xml.Serialization;
using MeshWiz.Utility;
namespace MeshWiz.Math;
[StructLayout(LayoutKind.Sequential)]
public readonly struct Line<TVec, TNum>(TVec start, TVec end)
: ILine<TVec, TNum>, IBounded<TVec>, IEquatable<Line<TVec, TNum>>
where TVec : unmanaged, IVec<TVec, TNum>
where TNum : unmanaged, IFloatingPointIeee754<TNum>
{
public readonly TVec Start = start, End = end;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
TVec IDiscreteCurve<TVec, TNum>.Start => Start;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
TVec IDiscreteCurve<TVec, TNum>.End => End;
public Line<TVec, TNum> Normalized() => FromAxisVector(Start, Direction);
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public TVec MidPoint => (Start + End) * Numbers<TNum>.Half;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
bool ICurve<TVec, TNum>.IsClosed => false;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public TNum Length => AxisVector.Length;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public TVec AxisVector => End - Start;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public TVec Direction => AxisVector.Normalized();
[Pure]
public Line<TVec, TNum> Reversed() => new(End, Start);
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
TNum IDiscreteCurve<TVec, TNum>.Length => AxisVector.Length;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public TNum SquaredLength => AxisVector.SquaredLength;
[JsonIgnore, XmlIgnore, SoapIgnore, IgnoreDataMember, Pure]
public AABB<TVec> Bounds => AABB<TVec>.From(Start, End);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> FromAxisVector(TVec start, TVec direction)
=> new(start, start + direction);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out TVec start, out TVec end)
{
start = Start;
end = End;
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> FromAxisVector(TVec direction)
=> new(TVec.Zero, direction);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TVec Traverse(TNum t)
=> TVec.Lerp(Start, End, t);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TVec TraverseOnCurve(TNum t)
=> Traverse(TNum.Clamp(t, TNum.Zero, TNum.One));
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> operator +(Line<TVec, TNum> l, Line<TVec, TNum> r)
=> FromAxisVector(l.Start + r.Start, l.AxisVector + r.AxisVector);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> operator +(Line<TVec, TNum> l, TVec r)
=> new(l.Start + r, l.End + r);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> operator +(TVec l, Line<TVec, TNum> r)
=> r + l;
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> operator -(Line<TVec, TNum> l, Line<TVec, TNum> r)
=> FromAxisVector(l.Start - r.Start, l.AxisVector - r.AxisVector);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Line<TVec, TNum> operator *(Line<TVec, TNum> l, TNum r)
=> FromAxisVector(l.Start * r, l.AxisVector * r);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TNum DistanceTo(TVec p)
=> ClosestPoint(p).DistanceTo(p);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TNum DistanceToSegment(TVec p)
=> ClosestPointOnSegment(p).DistanceTo(p);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TNum SquaredDistanceTo(TVec p)
=> ClosestPoint(p).SquaredDistanceTo(p);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TNum SquaredDistanceToSegment(TVec p)
=> ClosestPointOnSegment(p).SquaredDistanceTo(p);
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TVec ClosestPoint(TVec p)
{
var v = p - Start;
var ndir = Direction;
var dotProduct = v.Dot(ndir);
var alongVector = dotProduct * ndir;
return Start + alongVector;
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public TVec ClosestPointOnSegment(TVec p)
{
var v = p - Start;
var direction = AxisVector;
var length = direction.Length;
var ndir = direction / length;
var dotProduct = v.Dot(ndir);
dotProduct = TNum.Clamp(dotProduct, TNum.Zero, length);
var alongVector = dotProduct * ndir;
return Start + alongVector;
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public (TVec closest, TVec onSeg) ClosestPoints(TVec p)
{
var v = p - Start;
var direction = AxisVector;
var length = direction.Length;
var ndir = direction / length;
var dotProduct = v.Dot(ndir);
var closest = ndir * dotProduct + Start;
dotProduct = TNum.Clamp(dotProduct, TNum.Zero, length);
var onSeg = Start + dotProduct * ndir;
return (closest, onSeg);
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public (TNum closest, TNum onSeg) GetClosestPositions(TVec p)
{
var v = p - Start;
var direction = AxisVector;
var length = direction.Length;
var ndir = direction / length;
var closest = v.Dot(ndir) / length;
var onSeg = TNum.Clamp(closest, TNum.Zero, TNum.One);
return (closest, onSeg);
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public Line<TVec, TNum> Section(TNum start, TNum end)
=> new(Traverse(start), Traverse(end));
public Polyline<TVec, TNum> ToPolyline() => new(Start, End);
public Polyline<TVec, TNum> ToPolyline(PolylineTessellationParameter<TNum> _) => new(Start, End);
/// <inheritdoc />
public TVec GetTangent(TNum t)
=> Direction;
/// <inheritdoc />
public TVec EntryDirection => Direction;
/// <inheritdoc />
public TVec ExitDirection => Direction;
/// <inheritdoc />
public AABB<TVec> BBox => AABB.From(Start, End);
public Line<TOtherVec, TOther> To<TOtherVec, TOther>()
where TOtherVec : unmanaged, IVec<TOtherVec, TOther>
where TOther : unmanaged, IFloatingPointIeee754<TOther> =>
new(TOtherVec.FromComponentsConstrained<TVec, TNum>(Start),
TOtherVec.FromComponentsConstrained<TVec, TNum>(End));
/// <inheritdoc />
public bool Equals(Line<TVec, TNum> other) => this == other;
/// <inheritdoc />
public override bool Equals(object? obj) => obj is Line<TVec, TNum> other && this == other;
/// <inheritdoc />
public override int GetHashCode() => HashCode.Combine(Start, End);
public static bool operator ==(Line<TVec, TNum> left, Line<TVec, TNum> right) =>
left.Start == right.Start && left.End == right.End;
public static bool operator !=(Line<TVec, TNum> left, Line<TVec, TNum> right) =>
left.Start != right.Start || left.End != right.End;
}
public static class Line
{
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryIntersect<TNum>(
in Line<Vec2<TNum>, TNum> a,
in Line<Vec2<TNum>, TNum> b,
out TNum alongA)
where TNum : unmanaged, IFloatingPointIeee754<TNum>
{
alongA = default;
var p = a.Start;
var r = a.AxisVector;
var q = b.Start;
var s = b.AxisVector;
var pq = q - p;
var rxs = r.Cross(s);
var colinear = TNum.Abs(rxs) < TNum.CreateTruncating(1e-8);
if (colinear) return false;
alongA = pq.Cross(s) / rxs;
return true;
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryIntersectOnSegment<TNum>(
in Line<Vec2<TNum>, TNum> a,
in Line<Vec2<TNum>, TNum> b,
out TNum alongA)
where TNum : unmanaged, IFloatingPointIeee754<TNum>
{
alongA = default;
var p = a.Start;
var r = a.AxisVector;
var q = b.Start;
var s = b.AxisVector;
var pq = q - p;
var rxs = r.Cross(s);
var colinear = TNum.Abs(rxs) < TNum.CreateTruncating(1e-8);
if (colinear) return false;
var t = pq.Cross(s) / rxs;
rxs = -rxs;
pq = -pq;
var t2 = pq.Cross(r) / rxs;
alongA = t;
return TNum.Clamp(t, TNum.Zero, TNum.One) == t
&& TNum.Clamp(t2, TNum.Zero, TNum.One) == t2;
}
[Pure, MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryIntersectOnSegment<TNum>(
in Line<Vec2<TNum>, TNum> a,
in Line<Vec2<TNum>, TNum> b,
out TNum alongA,
out TNum alongB)
where TNum : unmanaged, IFloatingPointIeee754<TNum>
{
alongA = default;
alongB = default;
var p = a.Start;
var r = a.AxisVector;
var q = b.Start;
var s = b.AxisVector;
var pq = q - p;
var rxs = r.Cross(s);
var colinear = TNum.Abs(rxs) < TNum.CreateTruncating(1e-8);
if (colinear)
return false;
var t = pq.Cross(s) / rxs;
rxs = -rxs;
pq = -pq;
var t2 = pq.Cross(r) / rxs;
alongA = t;
alongB = t2;
return TNum.Clamp(t, TNum.Zero, TNum.One) == t
&& TNum.Clamp(t2, TNum.Zero, TNum.One) == t2;
}
}