-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
Vector3.cs
414 lines (378 loc) · 15.3 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace System.Numerics
{
/// <summary>
/// A structure encapsulating three doule values and provides hardware accelerated methods.
/// </summary>
public partial struct Vector3
{
#region Public Static Properties
/// <summary>
/// Returns the vector (0,0,0).
/// </summary>
public static Vector3 Zero { get { return new Vector3(); } }
/// <summary>
/// Returns the vector (1,1,1).
/// </summary>
public static Vector3 One { get { return new Vector3(1.0, 1.0, 1.0); } }
/// <summary>
/// Returns the vector (1,0,0).
/// </summary>
public static Vector3 UnitX { get { return new Vector3(1.0, 0.0, 0.0); } }
/// <summary>
/// Returns the vector (0,1,0).
/// </summary> public static Vector3 UnitY { get { return new Vector3(0.0, 1.0, 0.0); } }
/// <summary>
/// Returns the vector (0,0,1).
/// </summary>
public static Vector3 UnitZ { get { return new Vector3(0.0, 0.0, 1.0); } }
#endregion Public Static Properties
#region Public Instance Methods
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
int hash = this.X.GetHashCode();
hash = HashCodeHelper.CombineHashCodes(hash, this.Y.GetHashCode());
hash = HashCodeHelper.CombineHashCodes(hash, this.Z.GetHashCode());
return hash;
}
/// <summary>
/// Returns a boolean indicating whether the given Object is equal to this Vector3 instance.
/// </summary>
/// <param name="obj">The Object to compare against.</param>
/// <returns>True if the Object is equal to this Vector3; False otherwise.</returns>
public override bool Equals(object obj)
{
if (!(obj is Vector3))
return false;
return Equals((Vector3)obj);
}
/// <summary>
/// Returns a String representing this Vector3 instance.
/// </summary>
/// <returns>The string representation.</returns>
public override string ToString()
{
return $"<{X} {Y} {Z}>";
}
/// <summary>
/// Returns the length of the vector.
/// </summary>
/// <returns>The vector's length.</returns>
public double Length()
{
if (Vector.IsHardwareAccelerated)
{
double ls = Vector3.Dot(this, this);
return Math.Sqrt(ls);
}
else
{
double ls = X * X + Y * Y + Z * Z;
return Math.Sqrt(ls);
}
}
/// <summary>
/// Returns the length of the vector squared. This operation is cheaper than Length().
/// </summary>
/// <returns>The vector's length squared.</returns>
public double LengthSquared()
{
if (Vector.IsHardwareAccelerated)
{
return Vector3.Dot(this, this);
}
else
{
return X * X + Y * Y + Z * Z;
}
}
#endregion Public Instance Methods
#region Public Static Methods
/// <summary>
/// Returns the Euclidean distance between the two given points.
/// </summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The distance.</returns>
public static double Distance(Vector3 value1, Vector3 value2)
{
if (Vector.IsHardwareAccelerated)
{
Vector3 difference = value1 - value2;
double ls = Vector3.Dot(difference, difference);
return Math.Sqrt(ls);
}
else
{
double dx = value1.X - value2.X;
double dy = value1.Y - value2.Y;
double dz = value1.Z - value2.Z;
double ls = dx * dx + dy * dy + dz * dz;
return Math.Sqrt(ls);
}
}
/// <summary>
/// Returns the Euclidean distance squared between the two given points.
/// </summary>
/// <param name="value1">The first point.</param>
/// <param name="value2">The second point.</param>
/// <returns>The distance squared.</returns>
public static double DistanceSquared(Vector3 value1, Vector3 value2)
{
if (Vector.IsHardwareAccelerated)
{
Vector3 difference = value1 - value2;
return Vector3.Dot(difference, difference);
}
else
{
double dx = value1.X - value2.X;
double dy = value1.Y - value2.Y;
double dz = value1.Z - value2.Z;
return dx * dx + dy * dy + dz * dz;
}
}
/// <summary>
/// Returns a vector with the same direction as the given vector, but with a length of 1.
/// </summary>
/// <param name="value">The vector to normalize.</param>
/// <returns>The normalized vector.</returns>
public static Vector3 Normalize(Vector3 value)
{
if (Vector.IsHardwareAccelerated)
{
double length = value.Length();
return value / length;
}
else
{
double ls = value.X * value.X + value.Y * value.Y + value.Z * value.Z;
double length = System.Math.Sqrt(ls);
return new Vector3(value.X / length, value.Y / length, value.Z / length);
}
}
/// <summary>
/// Computes the cross product of two vectors.
/// </summary>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <returns>The cross product.</returns>
public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
{
return new Vector3(
vector1.Y * vector2.Z - vector1.Z * vector2.Y,
vector1.Z * vector2.X - vector1.X * vector2.Z,
vector1.X * vector2.Y - vector1.Y * vector2.X);
}
/// <summary>
/// Returns the reflection of a vector off a surface that has the specified normal.
/// </summary>
/// <param name="vector">The source vector.</param>
/// <param name="normal">The normal of the surface being reflected off.</param>
/// <returns>The reflected vector.</returns>
public static Vector3 Reflect(Vector3 vector, Vector3 normal)
{
if (Vector.IsHardwareAccelerated)
{
double dot = Vector3.Dot(vector, normal);
Vector3 temp = normal * dot * 2;
return vector - temp;
}
else
{
double dot = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z;
double tempX = normal.X * dot * 2f;
double tempY = normal.Y * dot * 2f;
double tempZ = normal.Z * dot * 2f;
return new Vector3(vector.X - tempX, vector.Y - tempY, vector.Z - tempZ);
}
}
/// <summary>
/// Restricts a vector between a min and max value.
/// </summary>
/// <param name="value1">The source vector.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The restricted vector.</returns>
public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
{
// This compare order is very important!!!
// We must follow HLSL behavior in the case user specified min value is bigger than max value.
double x = value1.X;
x = (x > max.X) ? max.X : x;
x = (x < min.X) ? min.X : x;
double y = value1.Y;
y = (y > max.Y) ? max.Y : y;
y = (y < min.Y) ? min.Y : y;
double z = value1.Z;
z = (z > max.Z) ? max.Z : z;
z = (z < min.Z) ? min.Z : z;
return new Vector3(x, y, z);
}
/// <summary>
/// Linearly interpolates between two vectors based on the given weighting.
/// </summary>
/// <param name="value1">The first source vector.</param>
/// <param name="value2">The second source vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of the second source vector.</param>
/// <returns>The interpolated vector.</returns>
public static Vector3 Lerp(Vector3 value1, Vector3 value2, double amount)
{
if (Vector.IsHardwareAccelerated)
{
Vector3 firstInfluence = value1 * (1 - amount);
Vector3 secondInfluence = value2 * amount;
return firstInfluence + secondInfluence;
}
else
{
return new Vector3(
value1.X + (value2.X - value1.X) * amount,
value1.Y + (value2.Y - value1.Y) * amount,
value1.Z + (value2.Z - value1.Z) * amount);
}
}
/*
/// <summary>
/// Transforms a vector by the given matrix.
/// </summary>
/// <param name="position">The source vector.</param>
/// <param name="matrix">The transformation matrix.</param>
/// <returns>The transformed vector.</returns>
public static Vector3 Transform(Vector3 position, Matrix4x4 matrix)
{
return new Vector3(
position.X * matrix.M11 + position.Y * matrix.M21 + position.Z * matrix.M31 + matrix.M41,
position.X * matrix.M12 + position.Y * matrix.M22 + position.Z * matrix.M32 + matrix.M42,
position.X * matrix.M13 + position.Y * matrix.M23 + position.Z * matrix.M33 + matrix.M43);
}
/// <summary>
/// Transforms a vector normal by the given matrix.
/// </summary>
/// <param name="normal">The source vector.</param>
/// <param name="matrix">The transformation matrix.</param>
/// <returns>The transformed vector.</returns>
public static Vector3 TransformNormal(Vector3 normal, Matrix4x4 matrix)
{
return new Vector3(
normal.X * matrix.M11 + normal.Y * matrix.M21 + normal.Z * matrix.M31,
normal.X * matrix.M12 + normal.Y * matrix.M22 + normal.Z * matrix.M32,
normal.X * matrix.M13 + normal.Y * matrix.M23 + normal.Z * matrix.M33);
}
*/
/// <summary>
/// Transforms a vector by the given Quaternion rotation value.
/// </summary>
/// <param name="value">The source vector to be rotated.</param>
/// <param name="rotation">The rotation to apply.</param>
/// <returns>The transformed vector.</returns>
public static Vector3 Transform(Vector3 value, Quaternion rotation)
{
double x2 = rotation.X + rotation.X;
double y2 = rotation.Y + rotation.Y;
double z2 = rotation.Z + rotation.Z;
double wx2 = rotation.W * x2;
double wy2 = rotation.W * y2;
double wz2 = rotation.W * z2;
double xx2 = rotation.X * x2;
double xy2 = rotation.X * y2;
double xz2 = rotation.X * z2;
double yy2 = rotation.Y * y2;
double yz2 = rotation.Y * z2;
double zz2 = rotation.Z * z2;
return new Vector3(
value.X * (1.0 - yy2 - zz2) + value.Y * (xy2 - wz2) + value.Z * (xz2 + wy2),
value.X * (xy2 + wz2) + value.Y * (1.0 - xx2 - zz2) + value.Z * (yz2 - wx2),
value.X * (xz2 - wy2) + value.Y * (yz2 + wx2) + value.Z * (1.0 - xx2 - yy2));
}
#endregion Public Static Methods
#region Public operator methods
// All these methods should be inlined as they are implemented
// over JIT intrinsics
/// <summary>
/// Adds two vectors together.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>The summed vector.</returns>
public static Vector3 Add(Vector3 left, Vector3 right)
{
return left + right;
}
/// <summary>
/// Subtracts the second vector from the first.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>The difference vector.</returns>
public static Vector3 Subtract(Vector3 left, Vector3 right)
{
return left - right;
}
/// <summary>
/// Multiplies two vectors together.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>The product vector.</returns>
public static Vector3 Multiply(Vector3 left, Vector3 right)
{
return left * right;
}
/// <summary>
/// Multiplies a vector by the given scalar.
/// </summary>
/// <param name="left">The source vector.</param>
/// <param name="right">The scalar value.</param>
/// <returns>The scaled vector.</returns>
public static Vector3 Multiply(Vector3 left, double right)
{
return left * right;
}
/// <summary>
/// Multiplies a vector by the given scalar.
/// </summary>
/// <param name="left">The scalar value.</param>
/// <param name="right">The source vector.</param>
/// <returns>The scaled vector.</returns>
public static Vector3 Multiply(double left, Vector3 right)
{
return left * right;
}
/// <summary>
/// Divides the first vector by the second.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>The vector resulting from the division.</returns>
public static Vector3 Divide(Vector3 left, Vector3 right)
{
return left / right;
}
/// <summary>
/// Divides the vector by the given scalar.
/// </summary>
/// <param name="left">The source vector.</param>
/// <param name="divisor">The scalar value.</param>
/// <returns>The result of the division.</returns>
public static Vector3 Divide(Vector3 left, double divisor)
{
return left / divisor;
}
/// <summary>
/// Negates a given vector.
/// </summary>
/// <param name="value">The source vector.</param>
/// <returns>The negated vector.</returns>
public static Vector3 Negate(Vector3 value)
{
return -value;
}
#endregion Public operator methods
}
}