-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVector2.cs
320 lines (263 loc) · 11.6 KB
/
Vector2.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using static System.Runtime.CompilerServices.MethodImplOptions;
using SNVector2 = System.Numerics.Vector2;
namespace Plato
{
[DataContract]
public readonly partial struct Vector2
{
// Fields
[DataMember] public readonly SNVector2 Value;
// Constructor
[MethodImpl(AggressiveInlining)]
public Vector2(Number x, Number y) => Value = new(x, y);
[MethodImpl(AggressiveInlining)]
public Vector2(Number x) => Value = new(x);
[MethodImpl(AggressiveInlining)]
public Vector2(SNVector2 x) => Value = x;
//-------------------------------------------------------------------------------------
// Properties
//-------------------------------------------------------------------------------------
public Number X { [MethodImpl(AggressiveInlining)] get => Value.X; }
public Number Y { [MethodImpl(AggressiveInlining)] get => Value.Y; }
//-------------------------------------------------------------------------------------
// Immutable "setters"
//-------------------------------------------------------------------------------------
[MethodImpl(AggressiveInlining)]
public Vector2 WithX(Number x) => new(x, Y);
[MethodImpl(AggressiveInlining)]
public Vector2 WithY(Number y) => new(X, y);
// Implicit casts
[MethodImpl(AggressiveInlining)]
public static Vector2 FromSystem(SNVector2 v) => Unsafe.As<SNVector2, Vector2>(ref v);
[MethodImpl(AggressiveInlining)]
public static implicit operator SNVector2(Vector2 v) => v.Value;
[MethodImpl(AggressiveInlining)]
public static implicit operator Vector2(SNVector2 v) => FromSystem(v);
// Static operators
[MethodImpl(AggressiveInlining)]
public static Vector2 operator +(Vector2 left, Vector2 right) => left.Value + right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator -(Vector2 left, Vector2 right) => left.Value - right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator *(Vector2 left, Vector2 right) => left.Value * right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator *(Vector2 left, Number scalar) => left.Value * scalar;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator *(Number scalar, Vector2 right) => scalar * right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator /(Vector2 left, Vector2 right) => left.Value / right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator /(Vector2 left, Number scalar) => left.Value / scalar;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator %(Vector2 left, Vector2 right) => left - right * (left / right).Truncate;
[MethodImpl(AggressiveInlining)]
public static Vector2 operator %(Vector2 left, Number scalar) => left % new Vector2(scalar);
[MethodImpl(AggressiveInlining)]
public static Vector2 operator -(Vector2 value) => -value.Value;
/// <summary>
/// Returns the dot product of two <see cref="Vector2"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Dot(Vector2 right) => SNVector2.Dot(Value, right);
/// <summary>
/// Returns the Euclidean distance between two <see cref="Vector2"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Distance(Vector2 value2) => SNVector2.Distance(Value, value2);
/// <summary>
/// Returns the squared Euclidean distance between two <see cref="Vector2"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number DistanceSquared(Vector2 value2) => SNVector2.DistanceSquared(Value, value2);
/// <summary>
/// Returns a vector that clamps each element of the <see cref="Vector2"/> between the corresponding elements of the minimum and maximum vectors.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Clamp(Vector2 min, Vector2 max) => SNVector2.Clamp(Value, min, max);
/// <summary>
/// Returns a normalized version of the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Normalize
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Normalize(Value);
}
/// <summary>
/// Returns the length of the <see cref="Vector2"/>.
/// </summary>
public Number Length
{
[MethodImpl(AggressiveInlining)] get => Value.Length();
}
/// <summary>
/// Returns the squared length of the <see cref="Vector2"/>.
/// </summary>
public Number LengthSquared
{
[MethodImpl(AggressiveInlining)]
get => Value.LengthSquared();
}
/// <summary>
/// Returns a vector that is the reflection of the specified vector off a plane defined by the specified normal.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Reflect(Vector2 normal) => SNVector2.Reflect(Value, normal);
/// <summary>
/// Returns a vector whose elements are the absolute values of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Abs
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Abs(Value);
}
/// <summary>
/// Returns the square root of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 SquareRoot
{
[MethodImpl(AggressiveInlining)] get => SNVector2.SquareRoot(Value);
}
/// <summary>
/// Returns the sine of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Sin
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Sin(Value);
}
/// <summary>
/// Returns the cosine of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Cos
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Cos(Value);
}
/// <summary>
/// Returns a vector whose elements are the sine and cosine of each element in the specified <see cref="Vector2"/>.
/// </summary>
public (Vector2 Sin, Vector2 Cos) SinCos
{
[MethodImpl(AggressiveInlining)]
get
{
var (sin, cos) = SNVector2.SinCos(Value);
return (sin, cos);
}
}
/// <summary>
/// Converts degrees to radians for each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 DegreesToRadians
{
[MethodImpl(AggressiveInlining)] get => SNVector2.DegreesToRadians(Value);
}
/// <summary>
/// Converts radians to degrees for each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 RadiansToDegrees
{
[MethodImpl(AggressiveInlining)] get => SNVector2.RadiansToDegrees(Value);
}
/// <summary>
/// Returns the exponential of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Exp
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Exp(Value);
}
/// <summary>
/// Returns the natural logarithm (base e) of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Log
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Log(Value);
}
/// <summary>
/// Returns the base-2 logarithm of each element in the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Log2
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Log2(Value);
}
/// <summary>
/// Transforms a <see cref="Vector2"/> by a 3x2 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Transform(Matrix3x2 matrix) => SNVector2.Transform(Value, matrix);
/// <summary>
/// Transforms a <see cref="Vector2"/> by a 4x4 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Transform(Matrix4x4 matrix) => SNVector2.Transform(Value, matrix);
/// <summary>
/// Transforms a <see cref="Vector2"/> by a quaternion rotation.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Transform(Quaternion rotation) => SNVector2.Transform(Value, rotation);
/// <summary>
/// Transforms a normal vector by a 3x2 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 TransformNormal(Matrix3x2 matrix) => SNVector2.TransformNormal(Value, matrix);
/// <summary>
/// Transforms a normal vector by a 4x4 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 TransformNormal(Matrix4x4 matrix) => SNVector2.TransformNormal(Value, matrix);
/// <summary>
/// Returns the maximum of two <see cref="Vector2"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Max(Vector2 value2) => SNVector2.Max(Value, value2);
/// <summary>
/// Returns the minimum of two <see cref="Vector2"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector2 Min(Vector2 value2) => SNVector2.Min(Value, value2);
/// <summary>
/// Returns a truncated version of the specified <see cref="Vector2"/>.
/// </summary>
public Vector2 Truncate
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Truncate(Value);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest even integer.
/// </summary>
public Vector2 Round
{
[MethodImpl(AggressiveInlining)]
get => SNVector2.Round(Value, MidpointRounding.ToEven);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards zero.
/// </summary>
public Vector2 RoundTowardsZero
{
[MethodImpl(AggressiveInlining)] get => SNVector2.Round(Value, MidpointRounding.ToZero);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, away from zero.
/// </summary>
public Vector2 RoundAwayFromZero
{
[MethodImpl(AggressiveInlining)]
get => SNVector2.Round(Value, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards negative infinity
/// </summary>
public Vector2 Floor
{
[MethodImpl(AggressiveInlining)]
get => SNVector2.Round(Value, MidpointRounding.ToNegativeInfinity);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards positive infinity
/// </summary>
public Vector2 Ceiling
{
[MethodImpl(AggressiveInlining)]
get => SNVector2.Round(Value, MidpointRounding.ToPositiveInfinity);
}
}
}