-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathVector3.cs
333 lines (274 loc) · 11 KB
/
Vector3.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
321
322
323
324
325
326
327
328
329
330
331
332
333
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using static System.Runtime.CompilerServices.MethodImplOptions;
using SNVector3 = System.Numerics.Vector3;
namespace Plato
{
[DataContract]
public readonly partial struct Vector3
{
// Fields
[DataMember] public readonly SNVector3 Value;
// Constructor
[MethodImpl(AggressiveInlining)]
public Vector3(SNVector3 v) => Value = v;
[MethodImpl(AggressiveInlining)]
public Vector3(Number x, Number y, Number z) => Value = new(x, y, z);
[MethodImpl(AggressiveInlining)]
public Vector3(Number x) => Value = new(x);
// Properties
public Number X
{
[MethodImpl(AggressiveInlining)]
get => Value.X;
}
public Number Y
{
[MethodImpl(AggressiveInlining)]
get => Value.Y;
}
public Number Z
{
[MethodImpl(AggressiveInlining)]
get => Value.Z;
}
// Immutable "setters"
[MethodImpl(AggressiveInlining)]
public Vector3 WithX(Number x)
=> new(x, Y, Z);
[MethodImpl(AggressiveInlining)]
public Vector3 WithY(Number y)
=> new(X, y, Z);
[MethodImpl(AggressiveInlining)]
public Vector3 WithZ(Number z)
=> new(X, Y, z);
// Implicit casts
[MethodImpl(AggressiveInlining)]
public static Vector3 FromSystem(SNVector3 v) => Unsafe.As<SNVector3, Vector3>(ref v);
[MethodImpl(AggressiveInlining)]
public static implicit operator SNVector3(Vector3 v) => v.Value;
[MethodImpl(AggressiveInlining)]
public static implicit operator Vector3(SNVector3 v) => FromSystem(v);
// Static operators
[MethodImpl(AggressiveInlining)]
public static Vector3 operator +(Vector3 left, Vector3 right) => left.Value + right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator -(Vector3 left, Vector3 right) => left.Value - right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator *(Vector3 left, Vector3 right) => left.Value * right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator *(Vector3 left, Number scalar) => left.Value * scalar;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator *(Number scalar, Vector3 right) => scalar * right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator /(Vector3 left, Vector3 right) => left.Value / right.Value;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator /(Vector3 left, Number scalar) => left.Value / scalar;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator %(Vector3 left, Vector3 right)
=> left - right * (left / right).Truncate;
[MethodImpl(AggressiveInlining)]
public static Vector3 operator %(Vector3 left, Number scalar)
=> left % new Vector3(scalar);
[MethodImpl(AggressiveInlining)]
public static Vector3 operator -(Vector3 value) => -value.Value;
/// <summary>
/// Returns the dot product of two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Dot(Vector3 right) => SNVector3.Dot(Value, right);
/// <summary>
/// Returns the dot product of two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Cross(Vector3 right) => SNVector3.Cross(Value, right);
/// <summary>
/// Returns the Euclidean distance between two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number Distance(Vector3 value2) => SNVector3.Distance(Value, value2);
/// <summary>
/// Returns the squared Euclidean distance between two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Number DistanceSquared(Vector3 value2) => SNVector3.DistanceSquared(Value, value2);
/// <summary>
/// Returns a vector that clamps each element of the <see cref="Vector3"/> between the corresponding elements of the minimum and maximum vectors.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Clamp(Vector3 min, Vector3 max) => SNVector3.Clamp(Value, min, max);
/// <summary>
/// Returns a normalized version of the specified <see cref="Vector3"/>.
/// </summary>
public Vector3 Normalize
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Normalize(Value);
}
/// <summary>
/// Returns the length of the <see cref="Vector3"/>.
/// </summary>
public Number Length
{
[MethodImpl(AggressiveInlining)] get => Value.Length();
}
/// <summary>
/// Returns the squared length of the <see cref="Vector3"/>.
/// </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 Vector3 Reflect(Vector3 normal) => SNVector3.Reflect(Value, normal);
/// <summary>
/// Returns a vector whose elements are the absolute values of each element.
/// </summary>
public Vector3 Abs
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Abs(Value);
}
/// <summary>
/// Returns the square root of each element.
/// </summary>
public Vector3 SquareRoot
{
[MethodImpl(AggressiveInlining)] get => SNVector3.SquareRoot(Value);
}
/// <summary>
/// Returns the sine of each element.
/// </summary>
public Vector3 Sin
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Sin(Value);
}
/// <summary>
/// Returns the cosine of each element.
/// </summary>
public Vector3 Cos
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Cos(Value);
}
/// <summary>
/// Returns a vector whose elements are the sine and cosine of each element.
/// </summary>
public (Vector3 Sin, Vector3 Cos) SinCos
{
[MethodImpl(AggressiveInlining)]
get
{
var (sin, cos) = SNVector3.SinCos(Value);
return (sin, cos);
}
}
/// <summary>
/// Converts degrees to radians for each element.
/// </summary>
public Vector3 DegreesToRadians
{
[MethodImpl(AggressiveInlining)] get => SNVector3.DegreesToRadians(Value);
}
/// <summary>
/// Converts radians to degrees for each element.
/// </summary>
public Vector3 RadiansToDegrees
{
[MethodImpl(AggressiveInlining)] get => SNVector3.RadiansToDegrees(Value);
}
/// <summary>
/// Returns the exponential of each element.
/// </summary>
public Vector3 Exp
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Exp(Value);
}
/// <summary>
/// Returns the natural logarithm (base e) of each element.
/// </summary>
public Vector3 Log
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Log(Value);
}
/// <summary>
/// Returns the base-2 logarithm of each element.
/// </summary>
public Vector3 Log2
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Log2(Value);
}
/// <summary>
/// Transforms by a 4x4 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Transform(Matrix4x4 matrix) => SNVector3.Transform(Value, matrix);
/// <summary>
/// Transforms by a quaternion rotation.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Transform(Quaternion rotation) => SNVector3.Transform(Value, rotation);
/// <summary>
/// Transforms a normal vector by a 4x4 matrix.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 TransformNormal(Matrix4x4 matrix) => SNVector3.TransformNormal(Value, matrix);
/// <summary>
/// Returns the maximum of two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Max(Vector3 value2) => SNVector3.Max(Value, value2);
/// <summary>
/// Returns the minimum of two <see cref="Vector3"/> instances.
/// </summary>
[MethodImpl(AggressiveInlining)]
public Vector3 Min(Vector3 value2) => SNVector3.Min(Value, value2);
/// <summary>
/// Returns a truncated version of the specified <see cref="Vector3"/>.
/// </summary>
public Vector3 Truncate
{
[MethodImpl(AggressiveInlining)] get => SNVector3.Truncate(Value);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest even integer
/// </summary>
public Vector3 Round
{
[MethodImpl(AggressiveInlining)]
get => SNVector3.Round(Value, MidpointRounding.ToEven);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards zero.
/// </summary>
public Vector3 RoundTowardsZero
{
[MethodImpl(AggressiveInlining)]
get => SNVector3.Round(Value, MidpointRounding.ToZero);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, away from zero.
/// </summary>
public Vector3 RoundAwayFromZero
{
[MethodImpl(AggressiveInlining)]
get => SNVector3.Round(Value, MidpointRounding.AwayFromZero);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards negative infinity
/// </summary>
public Vector3 Floor
{
[MethodImpl(AggressiveInlining)]
get => SNVector3.Round(Value, MidpointRounding.ToNegativeInfinity);
}
/// <summary>
/// Rounds each element of the specified <see cref="Vector2"/> to the nearest integer, towards positive infinity
/// </summary>
public Vector3 Ceiling
{
[MethodImpl(AggressiveInlining)]
get => SNVector3.Round(Value, MidpointRounding.ToPositiveInfinity);
}
}
}