aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/ModifiedBulletX/MonoXnaCompactMaths/Vector3.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/ModifiedBulletX/MonoXnaCompactMaths/Vector3.cs620
1 files changed, 620 insertions, 0 deletions
diff --git a/libraries/ModifiedBulletX/MonoXnaCompactMaths/Vector3.cs b/libraries/ModifiedBulletX/MonoXnaCompactMaths/Vector3.cs
new file mode 100644
index 0000000..fd4bcd2
--- /dev/null
+++ b/libraries/ModifiedBulletX/MonoXnaCompactMaths/Vector3.cs
@@ -0,0 +1,620 @@
1#region License
2/*
3MIT License
4Copyright © 2006 The Mono.Xna Team
5
6All rights reserved.
7
8Authors:
9 * Alan McGovern
10
11Permission is hereby granted, free of charge, to any person obtaining a copy
12of this software and associated documentation files (the "Software"), to deal
13in the Software without restriction, including without limitation the rights
14to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15copies of the Software, and to permit persons to whom the Software is
16furnished to do so, subject to the following conditions:
17
18The above copyright notice and this permission notice shall be included in all
19copies or substantial portions of the Software.
20
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28*/
29#endregion License
30
31using System;
32using System.ComponentModel;
33using System.Text;
34using System.Runtime.InteropServices;
35
36namespace MonoXnaCompactMaths
37{
38 [Serializable]
39 [StructLayout(LayoutKind.Sequential)]
40 //[TypeConverter(typeof(Vector3Converter))]
41 public struct Vector3 : IEquatable<Vector3>
42 {
43 #region Private Fields
44
45 private static Vector3 zero = new Vector3(0f, 0f, 0f);
46 private static Vector3 one = new Vector3(1f, 1f, 1f);
47 private static Vector3 unitX = new Vector3(1f, 0f, 0f);
48 private static Vector3 unitY = new Vector3(0f, 1f, 0f);
49 private static Vector3 unitZ = new Vector3(0f, 0f, 1f);
50 private static Vector3 up = new Vector3(0f, 1f, 0f);
51 private static Vector3 down = new Vector3(0f, -1f, 0f);
52 private static Vector3 right = new Vector3(1f, 0f, 0f);
53 private static Vector3 left = new Vector3(-1f, 0f, 0f);
54 private static Vector3 forward = new Vector3(0f, 0f, -1f);
55 private static Vector3 backward = new Vector3(0f, 0f, 1f);
56
57 #endregion Private Fields
58
59
60 #region Public Fields
61
62 public float X;
63 public float Y;
64 public float Z;
65
66 #endregion Public Fields
67
68
69 #region Properties
70
71 public static Vector3 Zero
72 {
73 get { return zero; }
74 }
75
76 public static Vector3 One
77 {
78 get { return one; }
79 }
80
81 public static Vector3 UnitX
82 {
83 get { return unitX; }
84 }
85
86 public static Vector3 UnitY
87 {
88 get { return unitY; }
89 }
90
91 public static Vector3 UnitZ
92 {
93 get { return unitZ; }
94 }
95
96 public static Vector3 Up
97 {
98 get { return up; }
99 }
100
101 public static Vector3 Down
102 {
103 get { return down; }
104 }
105
106 public static Vector3 Right
107 {
108 get { return right; }
109 }
110
111 public static Vector3 Left
112 {
113 get { return left; }
114 }
115
116 public static Vector3 Forward
117 {
118 get { return forward; }
119 }
120
121 public static Vector3 Backward
122 {
123 get { return backward; }
124 }
125
126 #endregion Properties
127
128
129 #region Constructors
130
131 public Vector3(float x, float y, float z)
132 {
133 this.X = x;
134 this.Y = y;
135 this.Z = z;
136 }
137
138
139 public Vector3(float value)
140 {
141 this.X = value;
142 this.Y = value;
143 this.Z = value;
144 }
145
146
147 /*public Vector3(Vector2 value, float z)
148 {
149 this.X = value.X;
150 this.Y = value.Y;
151 this.Z = z;
152 }*/
153
154
155 #endregion Constructors
156
157
158 #region Public Methods
159
160 public static Vector3 Add(Vector3 value1, Vector3 value2)
161 {
162 value1.X += value2.X;
163 value1.Y += value2.Y;
164 value1.Z += value2.Z;
165 return value1;
166 }
167
168 public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
169 {
170 result.X = value1.X + value2.X;
171 result.Y = value1.Y + value2.Y;
172 result.Z = value1.Z + value2.Z;
173 }
174
175 /*public static Vector3 Barycentric(Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2)
176 {
177 return new Vector3(
178 MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
179 MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2),
180 MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2));
181 }*/
182
183 /*public static void Barycentric(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, float amount1, float amount2, out Vector3 result)
184 {
185 result = new Vector3(
186 MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
187 MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2),
188 MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2));
189 }*/
190
191 /*public static Vector3 CatmullRom(Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount)
192 {
193 return new Vector3(
194 MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
195 MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount),
196 MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount));
197 }*/
198
199 /*public static void CatmullRom(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result)
200 {
201 result = new Vector3(
202 MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
203 MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount),
204 MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount));
205 }*/
206
207 /*public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
208 {
209 return new Vector3(
210 MathHelper.Clamp(value1.X, min.X, max.X),
211 MathHelper.Clamp(value1.Y, min.Y, max.Y),
212 MathHelper.Clamp(value1.Z, min.Z, max.Z));
213 }*/
214
215 /*public static void Clamp(ref Vector3 value1, ref Vector3 min, ref Vector3 max, out Vector3 result)
216 {
217 result = new Vector3(
218 MathHelper.Clamp(value1.X, min.X, max.X),
219 MathHelper.Clamp(value1.Y, min.Y, max.Y),
220 MathHelper.Clamp(value1.Z, min.Z, max.Z));
221 }*/
222
223 public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
224 {
225 Cross(ref vector1, ref vector2, out vector1);
226 return vector1;
227 }
228
229 public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result)
230 {
231 result = new Vector3(vector1.Y * vector2.Z - vector2.Y * vector1.Z,
232 -(vector1.X * vector2.Z - vector2.X * vector1.Z),
233 vector1.X * vector2.Y - vector2.X * vector1.Y);
234 }
235
236 public static float Distance(Vector3 vector1, Vector3 vector2)
237 {
238 float result;
239 DistanceSquared(ref vector1, ref vector2, out result);
240 return (float)Math.Sqrt(result);
241 }
242
243 public static void Distance(ref Vector3 value1, ref Vector3 value2, out float result)
244 {
245 DistanceSquared(ref value1, ref value2, out result);
246 result = (float)Math.Sqrt(result);
247 }
248
249 public static float DistanceSquared(Vector3 value1, Vector3 value2)
250 {
251 float result;
252 DistanceSquared(ref value1, ref value2, out result);
253 return result;
254 }
255
256 public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result)
257 {
258 result = (value1.X - value2.X) * (value1.X - value2.X) +
259 (value1.Y - value2.Y) * (value1.Y - value2.Y) +
260 (value1.Z - value2.Z) * (value1.Z - value2.Z);
261 }
262
263 public static Vector3 Divide(Vector3 value1, Vector3 value2)
264 {
265 value1.X /= value2.X;
266 value1.Y /= value2.Y;
267 value1.Z /= value2.Z;
268 return value1;
269 }
270
271 public static Vector3 Divide(Vector3 value1, float value2)
272 {
273 float factor = 1 / value2;
274 value1.X *= factor;
275 value1.Y *= factor;
276 value1.Z *= factor;
277 return value1;
278 }
279
280 public static void Divide(ref Vector3 value1, float divisor, out Vector3 result)
281 {
282 float factor = 1 / divisor;
283 result.X = value1.X * factor;
284 result.Y = value1.Y * factor;
285 result.Z = value1.Z * factor;
286 }
287
288 public static void Divide(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
289 {
290 result.X = value1.X / value2.X;
291 result.Y = value1.Y / value2.Y;
292 result.Z = value1.Z / value2.Z;
293 }
294
295 public static float Dot(Vector3 vector1, Vector3 vector2)
296 {
297 return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z;
298 }
299
300 public static void Dot(ref Vector3 vector1, ref Vector3 vector2, out float result)
301 {
302 result = vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z;
303 }
304
305 public override bool Equals(object obj)
306 {
307 return (obj is Vector3) ? this == (Vector3)obj : false;
308 }
309
310 public bool Equals(Vector3 other)
311 {
312 return this == other;
313 }
314
315 public override int GetHashCode()
316 {
317 return (int)(this.X + this.Y + this.Z);
318 }
319
320 /*public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount)
321 {
322 Vector3 result = new Vector3();
323 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
324 return result;
325 }*/
326
327 /*public static void Hermite(ref Vector3 value1, ref Vector3 tangent1, ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result)
328 {
329 result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
330 result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
331 result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount);
332 }*/
333
334 public float Length()
335 {
336 float result;
337 DistanceSquared(ref this, ref zero, out result);
338 return (float)Math.Sqrt(result);
339 }
340
341 public float LengthSquared()
342 {
343 float result;
344 DistanceSquared(ref this, ref zero, out result);
345 return result;
346 }
347
348 /*public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
349 {
350 return new Vector3(
351 MathHelper.Lerp(value1.X, value2.X, amount),
352 MathHelper.Lerp(value1.Y, value2.Y, amount),
353 MathHelper.Lerp(value1.Z, value2.Z, amount));
354 }*/
355
356 /*public static void Lerp(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
357 {
358 result = new Vector3(
359 MathHelper.Lerp(value1.X, value2.X, amount),
360 MathHelper.Lerp(value1.Y, value2.Y, amount),
361 MathHelper.Lerp(value1.Z, value2.Z, amount));
362 }*/
363
364 /*public static Vector3 Max(Vector3 value1, Vector3 value2)
365 {
366 return new Vector3(
367 MathHelper.Max(value1.X, value2.X),
368 MathHelper.Max(value1.Y, value2.Y),
369 MathHelper.Max(value1.Z, value2.Z));
370 }*/
371
372 /*public static void Max(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
373 {
374 result = new Vector3(
375 MathHelper.Max(value1.X, value2.X),
376 MathHelper.Max(value1.Y, value2.Y),
377 MathHelper.Max(value1.Z, value2.Z));
378 }*/
379
380 /*public static Vector3 Min(Vector3 value1, Vector3 value2)
381 {
382 return new Vector3(
383 MathHelper.Min(value1.X, value2.X),
384 MathHelper.Min(value1.Y, value2.Y),
385 MathHelper.Min(value1.Z, value2.Z));
386 }*/
387
388 /*public static void Min(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
389 {
390 result = new Vector3(
391 MathHelper.Min(value1.X, value2.X),
392 MathHelper.Min(value1.Y, value2.Y),
393 MathHelper.Min(value1.Z, value2.Z));
394 }*/
395
396 public static Vector3 Multiply(Vector3 value1, Vector3 value2)
397 {
398 value1.X *= value2.X;
399 value1.Y *= value2.Y;
400 value1.Z *= value2.Z;
401 return value1;
402 }
403
404 public static Vector3 Multiply(Vector3 value1, float scaleFactor)
405 {
406 value1.X *= scaleFactor;
407 value1.Y *= scaleFactor;
408 value1.Z *= scaleFactor;
409 return value1;
410 }
411
412 public static void Multiply(ref Vector3 value1, float scaleFactor, out Vector3 result)
413 {
414 result.X = value1.X * scaleFactor;
415 result.Y = value1.Y * scaleFactor;
416 result.Z = value1.Z * scaleFactor;
417 }
418
419 public static void Multiply(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
420 {
421 result.X = value1.X * value2.X;
422 result.Y = value1.Y * value2.Y;
423 result.Z = value1.Z * value2.Z;
424 }
425
426 public static Vector3 Negate(Vector3 value)
427 {
428 value = new Vector3(-value.X, -value.Y, -value.Z);
429 return value;
430 }
431
432 public static void Negate(ref Vector3 value, out Vector3 result)
433 {
434 result = new Vector3(-value.X, -value.Y, -value.Z);
435 }
436
437 public void Normalize()
438 {
439 Normalize(ref this, out this);
440 }
441
442 public static Vector3 Normalize(Vector3 vector)
443 {
444 Normalize(ref vector, out vector);
445 return vector;
446 }
447
448 public static void Normalize(ref Vector3 value, out Vector3 result)
449 {
450 float factor;
451 Distance(ref value, ref zero, out factor);
452 factor = 1f / factor;
453 result.X = value.X * factor;
454 result.Y = value.Y * factor;
455 result.Z = value.Z * factor;
456 }
457
458 public static Vector3 Reflect(Vector3 vector, Vector3 normal)
459 {
460 throw new NotImplementedException();
461 }
462
463 public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result)
464 {
465 throw new NotImplementedException();
466 }
467
468 /*public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount)
469 {
470 return new Vector3(
471 MathHelper.SmoothStep(value1.X, value2.X, amount),
472 MathHelper.SmoothStep(value1.Y, value2.Y, amount),
473 MathHelper.SmoothStep(value1.Z, value2.Z, amount));
474 }*/
475
476 /*public static void SmoothStep(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
477 {
478 result = new Vector3(
479 MathHelper.SmoothStep(value1.X, value2.X, amount),
480 MathHelper.SmoothStep(value1.Y, value2.Y, amount),
481 MathHelper.SmoothStep(value1.Z, value2.Z, amount));
482 }*/
483
484 public static Vector3 Subtract(Vector3 value1, Vector3 value2)
485 {
486 value1.X -= value2.X;
487 value1.Y -= value2.Y;
488 value1.Z -= value2.Z;
489 return value1;
490 }
491
492 public static void Subtract(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
493 {
494 result.X = value1.X - value2.X;
495 result.Y = value1.Y - value2.Y;
496 result.Z = value1.Z - value2.Z;
497 }
498
499 public override string ToString()
500 {
501 StringBuilder sb = new StringBuilder(32);
502 sb.Append("{X:");
503 sb.Append(this.X);
504 sb.Append(" Y:");
505 sb.Append(this.Y);
506 sb.Append(" Z:");
507 sb.Append(this.Z);
508 sb.Append("}");
509 return sb.ToString();
510 }
511
512 public static Vector3 Transform(Vector3 position, Matrix matrix)
513 {
514 Transform(ref position, ref matrix, out position);
515 return position;
516 }
517
518 public static void Transform(ref Vector3 position, ref Matrix matrix, out Vector3 result)
519 {
520 result = new Vector3((position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41,
521 (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42,
522 (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43);
523 }
524
525 public static Vector3 TransformNormal(Vector3 normal, Matrix matrix)
526 {
527 TransformNormal(ref normal, ref matrix, out normal);
528 return normal;
529 }
530
531 public static void TransformNormal(ref Vector3 normal, ref Matrix matrix, out Vector3 result)
532 {
533 result = new Vector3((normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31),
534 (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32),
535 (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33));
536 }
537
538 #endregion Public methods
539
540
541 #region Operators
542
543 public static bool operator ==(Vector3 value1, Vector3 value2)
544 {
545 return value1.X == value2.X
546 && value1.Y == value2.Y
547 && value1.Z == value2.Z;
548 }
549
550 public static bool operator !=(Vector3 value1, Vector3 value2)
551 {
552 return !(value1 == value2);
553 }
554
555 public static Vector3 operator +(Vector3 value1, Vector3 value2)
556 {
557 value1.X += value2.X;
558 value1.Y += value2.Y;
559 value1.Z += value2.Z;
560 return value1;
561 }
562
563 public static Vector3 operator -(Vector3 value)
564 {
565 value = new Vector3(-value.X, -value.Y, -value.Z);
566 return value;
567 }
568
569 public static Vector3 operator -(Vector3 value1, Vector3 value2)
570 {
571 value1.X -= value2.X;
572 value1.Y -= value2.Y;
573 value1.Z -= value2.Z;
574 return value1;
575 }
576
577 public static Vector3 operator *(Vector3 value1, Vector3 value2)
578 {
579 value1.X *= value2.X;
580 value1.Y *= value2.Y;
581 value1.Z *= value2.Z;
582 return value1;
583 }
584
585 public static Vector3 operator *(Vector3 value, float scaleFactor)
586 {
587 value.X *= scaleFactor;
588 value.Y *= scaleFactor;
589 value.Z *= scaleFactor;
590 return value;
591 }
592
593 public static Vector3 operator *(float scaleFactor, Vector3 value)
594 {
595 value.X *= scaleFactor;
596 value.Y *= scaleFactor;
597 value.Z *= scaleFactor;
598 return value;
599 }
600
601 public static Vector3 operator /(Vector3 value1, Vector3 value2)
602 {
603 value1.X /= value2.X;
604 value1.Y /= value2.Y;
605 value1.Z /= value2.Z;
606 return value1;
607 }
608
609 public static Vector3 operator /(Vector3 value, float divider)
610 {
611 float factor = 1 / divider;
612 value.X *= factor;
613 value.Y *= factor;
614 value.Z *= factor;
615 return value;
616 }
617
618 #endregion
619 }
620}