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