This repository was archived by the owner on Nov 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 318
Expand file tree
/
Copy pathVector3.cs
More file actions
477 lines (411 loc) · 13.5 KB
/
Vector3.cs
File metadata and controls
477 lines (411 loc) · 13.5 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
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
using System;
using System.Runtime.InteropServices;
namespace TrueCraft.API
{
/// <summary>
/// Represents the location of an object in 3D space.
/// </summary>
[StructLayout(LayoutKind.Explicit)]
public struct Vector3 : IEquatable<Vector3>
{
/// <summary>
/// The X component of this vector.
/// </summary>
[FieldOffset(0)]
public double X;
/// <summary>
/// The Y component of this vector.
/// </summary>
[FieldOffset(8)]
public double Y;
/// <summary>
/// The Z component of this vector.
/// </summary>
[FieldOffset(16)]
public double Z;
/// <summary>
/// Creates a new vector from the specified value.
/// </summary>
/// <param name="value">The value for the components of the vector.</param>
public Vector3(double value)
{
X = Y = Z = value;
}
/// <summary>
/// Creates a new vector from the specified values.
/// </summary>
/// <param name="x">The X component of the vector.</param>
/// <param name="y">The Y component of the vector.</param>
/// <param name="z">The Z component of the vector.</param>
public Vector3(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Creates a new vector from copying another.
/// </summary>
/// <param name="v">The vector to copy.</param>
public Vector3(Vector3 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
/// <summary>
/// Converts this Vector3 to a string in the format <x,y,z>.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("<{0},{1},{2}>", X, Y, Z);
}
#region Math
/// <summary>
/// Truncates the decimal component of each part of this Vector3.
/// </summary>
public Vector3 Floor()
{
return new Vector3(Math.Floor(X), Math.Floor(Y), Math.Floor(Z));
}
/// <summary>
/// Rounds the decimal component of each part of this Vector3.
/// </summary>
public Vector3 Round()
{
return new Vector3(Math.Round(X), Math.Round(Y), Math.Round(Z));
}
/// <summary>
/// Clamps the vector to within the specified value.
/// </summary>
/// <param name="value">Value.</param>
public void Clamp(double value)
{
if (Math.Abs(X) > value)
X = value * (X < 0 ? -1 : 1);
if (Math.Abs(Y) > value)
Y = value * (Y < 0 ? -1 : 1);
if (Math.Abs(Z) > value)
Z = value * (Z < 0 ? -1 : 1);
}
/// <summary>
/// Calculates the distance between two Vector3 objects.
/// </summary>
public double DistanceTo(Vector3 other)
{
return Math.Sqrt(Square(other.X - X) +
Square(other.Y - Y) +
Square(other.Z - Z));
}
public Vector3 Transform(Matrix matrix)
{
var x = (X * matrix.M11) + (Y * matrix.M21) + (Z * matrix.M31) + matrix.M41;
var y = (X * matrix.M12) + (Y * matrix.M22) + (Z * matrix.M32) + matrix.M42;
var z = (X * matrix.M13) + (Y * matrix.M23) + (Z * matrix.M33) + matrix.M43;
return new Vector3(x, y, z);
}
/// <summary>
/// Calculates the square of a num.
/// </summary>
private double Square(double num)
{
return num * num;
}
/// <summary>
/// Finds the distance of this vector from Vector3.Zero
/// </summary>
public double Distance
{
get
{
return DistanceTo(Zero);
}
}
/// <summary>
/// Returns the component-wise minumum of two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns></returns>
public static Vector3 Min(Vector3 value1, Vector3 value2)
{
return new Vector3(
Math.Min(value1.X, value2.X),
Math.Min(value1.Y, value2.Y),
Math.Min(value1.Z, value2.Z)
);
}
/// <summary>
/// Returns the component-wise maximum of two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns></returns>
public static Vector3 Max(Vector3 value1, Vector3 value2)
{
return new Vector3(
Math.Max(value1.X, value2.X),
Math.Max(value1.Y, value2.Y),
Math.Max(value1.Z, value2.Z)
);
}
/// <summary>
/// Calculates the dot product between two vectors.
/// </summary>
public static double Dot(Vector3 value1, Vector3 value2)
{
return value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z;
}
/// <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 of two vectors.</returns>
public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
{
Cross(ref vector1, ref vector2, out vector1);
return vector1;
}
/// <summary>
/// Computes the cross product of two vectors.
/// </summary>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="result">The cross product of two vectors as an output parameter.</param>
public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result)
{
var x = vector1.Y * vector2.Z - vector2.Y * vector1.Z;
var y = -(vector1.X * vector2.Z - vector2.X * vector1.Z);
var z = vector1.X * vector2.Y - vector2.X * vector1.Y;
result.X = x;
result.Y = y;
result.Z = z;
}
#endregion
#region Operators
public static bool operator !=(Vector3 a, Vector3 b)
{
return !a.Equals(b);
}
public static bool operator ==(Vector3 a, Vector3 b)
{
return a.Equals(b);
}
public static Vector3 operator +(Vector3 a, Vector3 b)
{
return new Vector3(
a.X + b.X,
a.Y + b.Y,
a.Z + b.Z);
}
public static Vector3 operator -(Vector3 a, Vector3 b)
{
return new Vector3(
a.X - b.X,
a.Y - b.Y,
a.Z - b.Z);
}
public static Vector3 operator +(Vector3 a, Size b)
{
return new Vector3(
a.X + b.Width,
a.Y + b.Height,
a.Z + b.Depth);
}
public static Vector3 operator -(Vector3 a, Size b)
{
return new Vector3(
a.X - b.Width,
a.Y - b.Height,
a.Z - b.Depth);
}
public static Vector3 operator -(Vector3 a)
{
return new Vector3(
-a.X,
-a.Y,
-a.Z);
}
public static Vector3 operator *(Vector3 a, Vector3 b)
{
return new Vector3(
a.X * b.X,
a.Y * b.Y,
a.Z * b.Z);
}
public static Vector3 operator /(Vector3 a, Vector3 b)
{
return new Vector3(
a.X / b.X,
a.Y / b.Y,
a.Z / b.Z);
}
public static Vector3 operator %(Vector3 a, Vector3 b)
{
return new Vector3(a.X % b.X, a.Y % b.Y, a.Z % b.Z);
}
public static Vector3 operator +(Vector3 a, double b)
{
return new Vector3(
a.X + b,
a.Y + b,
a.Z + b);
}
public static Vector3 operator -(Vector3 a, double b)
{
return new Vector3(
a.X - b,
a.Y - b,
a.Z - b);
}
public static Vector3 operator *(Vector3 a, double b)
{
return new Vector3(
a.X * b,
a.Y * b,
a.Z * b);
}
public static Vector3 operator /(Vector3 a, double b)
{
return new Vector3(
a.X / b,
a.Y / b,
a.Z / b);
}
public static Vector3 operator %(Vector3 a, double b)
{
return new Vector3(a.X % b, a.Y % b, a.Y % b);
}
public static Vector3 operator +(double a, Vector3 b)
{
return new Vector3(
a + b.X,
a + b.Y,
a + b.Z);
}
public static Vector3 operator -(double a, Vector3 b)
{
return new Vector3(
a - b.X,
a - b.Y,
a - b.Z);
}
public static Vector3 operator *(double a, Vector3 b)
{
return new Vector3(
a * b.X,
a * b.Y,
a * b.Z);
}
public static Vector3 operator /(double a, Vector3 b)
{
return new Vector3(
a / b.X,
a / b.Y,
a / b.Z);
}
public static Vector3 operator %(double a, Vector3 b)
{
return new Vector3(a % b.X, a % b.Y, a % b.Y);
}
#endregion
#region Conversion operators
public static implicit operator Vector3(Coordinates3D a)
{
return new Vector3(a.X, a.Y, a.Z);
}
public static explicit operator Vector3(Coordinates2D c)
{
return new Vector3(c.X, 0, c.Z);
}
public static implicit operator Vector3(Size s)
{
return new Vector3(s.Width, s.Height, s.Depth);
}
#endregion
#region Constants
/// <summary>
/// A vector with its components set to 0.0.
/// </summary>
public static readonly Vector3 Zero = new Vector3(0);
/// <summary>
/// A vector with its components set to 1.0.
/// </summary>
public static readonly Vector3 One = new Vector3(1);
/// <summary>
/// A vector that points upward.
/// </summary>
public static readonly Vector3 Up = new Vector3(0, 1, 0);
/// <summary>
/// A vector that points downward.
/// </summary>
public static readonly Vector3 Down = new Vector3(0, -1, 0);
/// <summary>
/// A vector that points to the left.
/// </summary>
public static readonly Vector3 Left = new Vector3(-1, 0, 0);
/// <summary>
/// A vector that points to the right.
/// </summary>
public static readonly Vector3 Right = new Vector3(1, 0, 0);
/// <summary>
/// A vector that points backward.
/// </summary>
public static readonly Vector3 Backwards = new Vector3(0, 0, -1);
/// <summary>
/// A vector that points forward.
/// </summary>
public static readonly Vector3 Forwards = new Vector3(0, 0, 1);
/// <summary>
/// A vector that points to the east.
/// </summary>
public static readonly Vector3 East = new Vector3(1, 0, 0);
/// <summary>
/// A vector that points to the west.
/// </summary>
public static readonly Vector3 West = new Vector3(-1, 0, 0);
/// <summary>
/// A vector that points to the north.
/// </summary>
public static readonly Vector3 North = new Vector3(0, 0, -1);
/// <summary>
/// A vector that points to the south.
/// </summary>
public static readonly Vector3 South = new Vector3(0, 0, 1);
#endregion
/// <summary>
/// Determines whether this and another vector are equal.
/// </summary>
/// <param name="other">The other vector.</param>
/// <returns></returns>
public bool Equals(Vector3 other)
{
return other.X.Equals(X) && other.Y.Equals(Y) && other.Z.Equals(Z);
}
/// <summary>
/// Determines whether this and another object are equal.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns></returns>
public override bool Equals(object obj)
{
return obj is Vector3 && Equals((Vector3)obj);
}
/// <summary>
/// Gets the hash code for this vector.
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
unchecked
{
int result = X.GetHashCode();
result = (result * 397) ^ Y.GetHashCode();
result = (result * 397) ^ Z.GetHashCode();
return result;
}
}
}
}