Jypeli 10
The simple game programming library
Vector3D.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25#if UseDouble
26using Scalar = System.Double;
27#else
28using Scalar = System.Single;
29#endif
30using System;
31using System.Runtime.InteropServices;
33using System.Xml.Serialization;
34namespace AdvanceMath
35{
40 [StructLayout(LayoutKind.Sequential, Size = Vector3D.Size)]
41 [AdvBrowsableOrder("X,Y,Z")]
42#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT && !WINDOWS_PHONE && !NETFX_CORE
43 [Serializable]
44 [System.ComponentModel.TypeConverter(typeof(AdvTypeConverter<Vector3D>))]
45#endif
46 public struct Vector3D : IVector<Vector3D>
47 {
48 #region const fields
52 public const int Count = 3;
56 public const int Size = sizeof(Scalar) * Count;
57 #endregion
58 #region readonly fields
62 public static readonly Vector3D One = new Vector3D(1, 1, 1);
66 public static readonly Vector3D Zero = new Vector3D();
70 public static readonly Vector3D XAxis = new Vector3D(1, 0, 0);
74 public static readonly Vector3D YAxis = new Vector3D(0, 1, 0);
78 public static readonly Vector3D ZAxis = new Vector3D(0, 0, 1);
79
80 private static readonly string FormatString = MatrixHelper.CreateVectorFormatString(Count);
82 #endregion
83 #region static methods
84 public static void Copy(ref Vector3D vector, Scalar[] destArray)
85 {
86 Copy(ref vector, destArray, 0);
87 }
88 public static void Copy(ref Vector3D vector, Scalar[] destArray, int index)
89 {
90 ThrowHelper.CheckCopy(destArray, index, Count);
91
92 destArray[index] = vector.X;
93 destArray[++index] = vector.Y;
94 destArray[++index] = vector.Z;
95 }
96 public static void Copy(Scalar[] sourceArray, out Vector3D result)
97 {
98 Copy(sourceArray, 0, out result);
99 }
100 public static void Copy(Scalar[] sourceArray, int index, out Vector3D result)
101 {
102 ThrowHelper.CheckCopy(sourceArray, index, Count);
103
104 result.X = sourceArray[index];
105 result.Y = sourceArray[++index];
106 result.Z = sourceArray[++index];
107 }
108 public static void Copy(ref Vector4D source, out Vector3D dest)
109 {
110 dest.X = source.X;
111 dest.Y = source.Y;
112 dest.Z = source.Z;
113 }
114 public static void Copy(ref Vector2D source, ref Vector3D dest)
115 {
116 dest.X = source.X;
117 dest.Y = source.Y;
118 }
119
120 public static Vector3D Clamp(Vector3D value, Vector3D min, Vector3D max)
121 {
122 Vector3D result;
123 MathHelper.Clamp(ref value.X, ref min.X, ref max.X, out result.X);
124 MathHelper.Clamp(ref value.Y, ref min.Y, ref max.Y, out result.Y);
125 MathHelper.Clamp(ref value.Z, ref min.Z, ref max.Z, out result.Z);
126 return result;
127 }
128 public static void Clamp(ref Vector3D value, ref Vector3D min, ref Vector3D max, out Vector3D result)
129 {
130 MathHelper.Clamp(ref value.X, ref min.X, ref max.X, out result.X);
131 MathHelper.Clamp(ref value.Y, ref min.Y, ref max.Y, out result.Y);
132 MathHelper.Clamp(ref value.Z, ref min.Z, ref max.Z, out result.Z);
133 }
134
135 public static Vector3D Lerp(Vector3D left, Vector3D right, Scalar amount)
136 {
137 Vector3D result;
138 Lerp(ref left, ref right, ref amount, out result);
139 return result;
140 }
141 public static void Lerp(ref Vector3D left, ref Vector3D right, ref Scalar amount, out Vector3D result)
142 {
143 result.X = (right.X - left.X) * amount + left.X;
144 result.Y = (right.Y - left.Y) * amount + left.Y;
145 result.Z = (right.Z - left.Z) * amount + left.Z;
146 }
147 public static Vector3D Lerp(Vector3D left, Vector3D right, Vector3D amount)
148 {
149 Vector3D result;
150 Lerp(ref left, ref right, ref amount, out result);
151 return result;
152 }
153 public static void Lerp(ref Vector3D left, ref Vector3D right, ref Vector3D amount, out Vector3D result)
154 {
155 result.X = (right.X - left.X) * amount.X + left.X;
156 result.Y = (right.Y - left.Y) * amount.Y + left.Y;
157 result.Z = (right.Z - left.Z) * amount.Z + left.Z;
158 }
159
160 public static Scalar Distance(Vector3D left, Vector3D right)
161 {
162 Scalar result;
163 Distance(ref left, ref right, out result);
164 return result;
165 }
166 public static void Distance(ref Vector3D left, ref Vector3D right, out Scalar result)
167 {
168 Vector3D diff;
169 Subtract(ref left, ref right, out diff);
170 GetMagnitude(ref diff, out result);
171 }
172 public static Scalar DistanceSq(Vector3D left, Vector3D right)
173 {
174 Scalar result;
175 DistanceSq(ref left, ref right, out result);
176 return result;
177 }
178 public static void DistanceSq(ref Vector3D left, ref Vector3D right, out Scalar result)
179 {
180 Vector3D diff;
181 Subtract(ref left, ref right, out diff);
182 GetMagnitudeSq(ref diff, out result);
183 }
184
192 public static Vector3D Add(Vector3D left, Vector3D right)
193 {
194 Vector3D result;
195 result.X = left.X + right.X;
196 result.Y = left.Y + right.Y;
197 result.Z = left.Z + right.Z;
198 return result;
199 }
200 public static void Add(ref Vector3D left, ref Vector3D right, out Vector3D result)
201 {
202 result.X = left.X + right.X;
203 result.Y = left.Y + right.Y;
204 result.Z = left.Z + right.Z;
205 }
206
207 public static Vector3D Add(Vector2D left, Vector3D right)
208 {
209 Vector3D result;
210 Add(ref left, ref right, out result);
211 return result;
212 }
213 public static void Add(ref Vector2D left, ref Vector3D right, out Vector3D result)
214 {
215 result.X = left.X + right.X;
216 result.Y = left.Y + right.Y;
217 result.Z = right.Z;
218 }
219 public static Vector3D Add(Vector3D left, Vector2D right)
220 {
221 Vector3D result;
222 Add(ref left, ref right, out result);
223 return result;
224 }
225 public static void Add(ref Vector3D left, ref Vector2D right, out Vector3D result)
226 {
227 result.X = left.X + right.X;
228 result.Y = left.Y + right.Y;
229 result.Z = left.Z;
230 }
231
232
240 public static Vector3D Subtract(Vector3D left, Vector3D right)
241 {
242 Vector3D result;
243 result.X = left.X - right.X;
244 result.Y = left.Y - right.Y;
245 result.Z = left.Z - right.Z;
246 return result;
247 }
248 public static void Subtract(ref Vector3D left, ref Vector3D right, out Vector3D result)
249 {
250 result.X = left.X - right.X;
251 result.Y = left.Y - right.Y;
252 result.Z = left.Z - right.Z;
253 }
254
255 public static Vector3D Subtract(Vector2D left, Vector3D right)
256 {
257 Vector3D result;
258 Subtract(ref left, ref right, out result);
259 return result;
260 }
261 public static void Subtract(ref Vector2D left, ref Vector3D right, out Vector3D result)
262 {
263 result.X = left.X - right.X;
264 result.Y = left.Y - right.Y;
265 result.Z = -right.Z;
266 }
267 public static Vector3D Subtract(Vector3D left, Vector2D right)
268 {
269 Vector3D result;
270 Subtract(ref left, ref right, out result);
271 return result;
272 }
273 public static void Subtract(ref Vector3D left, ref Vector2D right, out Vector3D result)
274 {
275 result.X = left.X - right.X;
276 result.Y = left.Y - right.Y;
277 result.Z = left.Z;
278 }
279
287 public static Vector3D Multiply(Vector3D source, Scalar scalar)
288 {
289 Vector3D result;
290 result.X = source.X * scalar;
291 result.Y = source.Y * scalar;
292 result.Z = source.Z * scalar;
293 return result;
294 }
295 public static void Multiply(ref Vector3D source, ref Scalar scalar, out Vector3D result)
296 {
297 result.X = source.X * scalar;
298 result.Y = source.Y * scalar;
299 result.Z = source.Z * scalar;
300 }
308 public static Vector3D Multiply(Scalar scalar, Vector3D source)
309 {
310 Vector3D result;
311 result.X = scalar * source.X;
312 result.Y = scalar * source.Y;
313 result.Z = scalar * source.Z;
314 return result;
315 }
316 public static void Multiply(ref Scalar scalar, ref Vector3D source, out Vector3D result)
317 {
318 result.X = scalar * source.X;
319 result.Y = scalar * source.Y;
320 result.Z = scalar * source.Z;
321 }
328 public static Vector3D Transform(Matrix3x3 matrix, Vector3D vector)
329 {
330 Vector3D result;
331
332 result.X = matrix.m00 * vector.X + matrix.m01 * vector.Y + matrix.m02 * vector.Z;
333 result.Y = matrix.m10 * vector.X + matrix.m11 * vector.Y + matrix.m12 * vector.Z;
334 result.Z = matrix.m20 * vector.X + matrix.m21 * vector.Y + matrix.m22 * vector.Z;
335
336 return result;
337 }
338 public static void Transform(ref Matrix3x3 matrix, ref Vector3D vector, out Vector3D result)
339 {
340 Scalar X = vector.X;
341 Scalar Y = vector.Y;
342 result.X = matrix.m00 * X + matrix.m01 * Y + matrix.m02 * vector.Z;
343 result.Y = matrix.m10 * X + matrix.m11 * Y + matrix.m12 * vector.Z;
344 result.Z = matrix.m20 * X + matrix.m21 * Y + matrix.m22 * vector.Z;
345 }
352 public static Vector3D Transform(Vector3D vector, Matrix3x3 matrix)
353 {
354 Vector3D result;
355
356 result.X = matrix.m00 * vector.X + matrix.m01 * vector.Y + matrix.m02 * vector.Z;
357 result.Y = matrix.m10 * vector.X + matrix.m11 * vector.Y + matrix.m12 * vector.Z;
358 result.Z = matrix.m20 * vector.X + matrix.m21 * vector.Y + matrix.m22 * vector.Z;
359
360 return result;
361 }
362 public static void Transform(ref Vector3D vector, ref Matrix3x3 matrix, out Vector3D result)
363 {
364 Scalar X = vector.X;
365 Scalar Y = vector.Y;
366 result.X = matrix.m00 * X + matrix.m01 * Y + matrix.m02 * vector.Z;
367 result.Y = matrix.m10 * X + matrix.m11 * Y + matrix.m12 * vector.Z;
368 result.Z = matrix.m20 * X + matrix.m21 * Y + matrix.m22 * vector.Z;
369 }
381 public static Vector3D Transform(Matrix4x4 matrix, Vector3D vector)
382 {
383 Vector3D result;
384
385 Scalar inverseW = 1 / (matrix.m30 * vector.X + matrix.m31 * vector.Y + matrix.m32 * vector.Z + matrix.m33);
386 result.X = ((matrix.m00 * vector.X) + (matrix.m01 * vector.Y) + (matrix.m02 * vector.Z) + matrix.m03) * inverseW;
387 result.Y = ((matrix.m10 * vector.X) + (matrix.m11 * vector.Y) + (matrix.m12 * vector.Z) + matrix.m13) * inverseW;
388 result.Z = ((matrix.m20 * vector.X) + (matrix.m21 * vector.Y) + (matrix.m22 * vector.Z) + matrix.m23) * inverseW;
389
390 return result;
391 }
392 public static void Transform(ref Matrix4x4 matrix, ref Vector3D vector, out Vector3D result)
393 {
394 Scalar X = vector.X;
395 Scalar Y = vector.Y;
396 Scalar inverseW = 1 / (matrix.m30 * X + matrix.m31 * Y + matrix.m32 * vector.Z + matrix.m33);
397 result.X = ((matrix.m00 * X) + (matrix.m01 * Y) + (matrix.m02 * vector.Z) + matrix.m03) * inverseW;
398 result.Y = ((matrix.m10 * X) + (matrix.m11 * Y) + (matrix.m12 * vector.Z) + matrix.m13) * inverseW;
399 result.Z = ((matrix.m20 * X) + (matrix.m21 * Y) + (matrix.m22 * vector.Z) + matrix.m23) * inverseW;
400 }
401
402 public static Vector3D Multiply(Quaternion quat, Vector3D vector)
403 {
404 // nVidia SDK implementation
405 Vector3D uv, uuv;
406 Vector3D qvec;// = new Vector3D(quat.X, quat.Y, quat.Z);
407
408 qvec.X = quat.X;
409 qvec.Y = quat.Y;
410 qvec.Z = quat.Z;
411 Vector3D.Cross(ref qvec, ref vector, out uv);
412 Vector3D.Cross(ref qvec, ref uv, out uuv);
413 Vector3D.Cross(ref qvec, ref uv, out uuv);
414 Vector3D.Multiply(ref uv, ref quat.W, out uv);
415 Vector3D.Add(ref uv, ref uuv, out uv);
416 Vector3D.Multiply(ref uv, ref MathHelper.Two, out uv);
417 Vector3D.Add(ref vector, ref uv, out uv);
418 return uv;
419
420 //uv = qvec ^ vector;
421 //uuv = qvec ^ uv;
422 //uv *= (2 * quat.W);
423 //uuv *= 2;
424
425 //return vector + uv + uuv;
426
427 // get the rotation matriX of the Quaternion and multiplY it times the vector
428 //return quat.ToRotationMatrix() * vector;
429 }
430 public static void Multiply(ref Quaternion quat, ref Vector3D vector, out Vector3D result)
431 {
432 Vector3D uv, uuv;
433 Vector3D qvec;
434 qvec.X = quat.X;
435 qvec.Y = quat.Y;
436 qvec.Z = quat.Z;
437 Vector3D.Cross(ref qvec, ref vector, out uv);
438 Vector3D.Cross(ref qvec, ref uv, out uuv);
439 Vector3D.Cross(ref qvec, ref uv, out uuv);
440 Vector3D.Multiply(ref uv, ref quat.W, out uv);
441 Vector3D.Add(ref uv, ref uuv, out uv);
442 Vector3D.Multiply(ref uv, ref MathHelper.Two, out uv);
443 Vector3D.Add(ref vector, ref uv, out result);
444 }
452 public static Scalar Dot(Vector3D left, Vector3D right)
453 {
454 return left.Y * right.Y + left.X * right.X + left.Z * right.Z;
455 }
456 public static void Dot(ref Vector3D left, ref Vector3D right, out Scalar result)
457 {
458 result = left.Y * right.Y + left.X * right.X + left.Z * right.Z;
459 }
467 public static Vector3D Cross(Vector3D left, Vector3D right)
468 {
469 Vector3D result;
470 result.X = left.Y * right.Z - left.Z * right.Y;
471 result.Y = left.Z * right.X - left.X * right.Z;
472 result.Z = left.X * right.Y - left.Y * right.X;
473 return result;
474 }
475 public static void Cross(ref Vector3D left, ref Vector3D right, out Vector3D result)
476 {
477 Scalar X = left.Y * right.Z - left.Z * right.Y;
478 Scalar Y = left.Z * right.X - left.X * right.Z;
479 result.Z = left.X * right.Y - left.Y * right.X;
480 result.X = X;
481 result.Y = Y;
482 }
488 public static Scalar GetMagnitudeSq(Vector3D source)
489 {
490 return source.X * source.X + source.Y * source.Y + source.Z * source.Z;
491 }
492 public static void GetMagnitudeSq(ref Vector3D source, out Scalar result)
493 {
494 result = source.X * source.X + source.Y * source.Y + source.Z * source.Z;
495 }
501 public static Scalar GetMagnitude(Vector3D source)
502 {
503 return MathHelper.Sqrt(source.X * source.X + source.Y * source.Y + source.Z * source.Z);
504 }
505 public static void GetMagnitude(ref Vector3D source, out Scalar result)
506 {
507 result = MathHelper.Sqrt(source.X * source.X + source.Y * source.Y + source.Z * source.Z);
508 }
515 public static Vector3D SetMagnitude(Vector3D source, Scalar magnitude)
516 {
517 Vector3D result;
518 SetMagnitude(ref source, ref magnitude, out result);
519 return result;
520 }
521 public static void SetMagnitude(ref Vector3D source, ref Scalar magnitude, out Vector3D result)
522 {
523 Scalar oldmagnitude;
524 GetMagnitude(ref source, out oldmagnitude);
525 if (oldmagnitude > 0 && magnitude != 0)
526 {
527 oldmagnitude = (magnitude / oldmagnitude);
528 Multiply(ref source, ref oldmagnitude, out result);
529 }
530 else
531 {
532 result = Zero;
533 }
534 }
535
542 public static Vector3D Normalize(Vector3D source)
543 {
544 Scalar oldmagnitude;
545 GetMagnitude(ref source, out oldmagnitude);
546 if (oldmagnitude == 0) { return Zero; }
547 oldmagnitude = (1 / oldmagnitude);
548 Vector3D result;
549 result.X = source.X * oldmagnitude;
550 result.Y = source.Y * oldmagnitude;
551 result.Z = source.Z * oldmagnitude;
552 return result;
553 }
554 public static void Normalize(ref Vector3D source, out Vector3D result)
555 {
556 Scalar oldmagnitude;
557 GetMagnitude(ref source, out oldmagnitude);
558 if (oldmagnitude == 0) { result = Zero; return; }
559 oldmagnitude = (1 / oldmagnitude);
560 result.X = source.X * oldmagnitude;
561 result.Y = source.Y * oldmagnitude;
562 result.Z = source.Z * oldmagnitude;
563 }
564 public static void Normalize(ref Vector3D source)
565 {
566 Normalize(ref source, out source);
567 }
573 public static Vector3D Negate(Vector3D source)
574 {
575 Vector3D result;
576 result.X = -source.X;
577 result.Y = -source.Y;
578 result.Z = -source.Z;
579 return result;
580 }
581 public static void Negate(ref Vector3D source)
582 {
583 Negate(ref source, out source);
584 }
585 public static void Negate(ref Vector3D source, out Vector3D result)
586 {
587 result.X = -source.X;
588 result.Y = -source.Y;
589 result.Z = -source.Z;
590 }
591
599 public static Vector3D Project(Vector3D left, Vector3D right)
600 {
601 Vector3D result;
602 Project(ref left, ref right, out result);
603 return result;
604 }
605 public static void Project(ref Vector3D left, ref Vector3D right, out Vector3D result)
606 {
607 Scalar tmp, magsq;
608 Dot(ref left, ref right, out tmp);
609 GetMagnitudeSq(ref right, out magsq);
610 tmp /= magsq;
611 Multiply(ref right, ref tmp, out result);
612 }
613
614 public static Vector3D Hermite(Vector3D value1, Vector3D tangent1, Vector3D value2, Vector3D tangent2, Scalar amount)
615 {
616 Vector3D result;
617 Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
618 return result;
619 }
620 public static void Hermite(ref Vector3D value1, ref Vector3D tangent1, ref Vector3D value2, ref Vector3D tangent2, Scalar amount, out Vector3D result)
621 {
622 Scalar h1, h2, h3, h4;
623 MathHelper.HermiteHelper(amount, out h1, out h2, out h3, out h4);
624 result.X = h1 * value1.X + h2 * value2.X + h3 * tangent1.X + h4 * tangent2.X;
625 result.Y = h1 * value1.Y + h2 * value2.Y + h3 * tangent1.Y + h4 * tangent2.Y;
626 result.Z = h1 * value1.Z + h2 * value2.Z + h3 * tangent1.Z + h4 * tangent2.Z;
627 }
628
629 public static Vector3D CatmullRom(Vector3D value1, Vector3D value2, Vector3D value3, Vector3D value4, Scalar amount)
630 {
631 Vector3D result;
632 CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
633 return result;
634 }
635 public static void CatmullRom(ref Vector3D value1, ref Vector3D value2, ref Vector3D value3, ref Vector3D value4, Scalar amount, out Vector3D result)
636 {
637 Scalar amountSq = amount * amount;
638 Scalar amountCu = amountSq * amount;
639 result.X =
640 0.5f * ((2 * value2.X) +
641 (-value1.X + value3.X) * amount +
642 (2 * value1.X - 5 * value2.X + 4 * value3.X - value4.X) * amountSq +
643 (-value1.X + 3 * value2.X - 3 * value3.X + value4.X) * amountCu);
644 result.Y =
645 0.5f * ((2 * value2.Y) +
646 (-value1.Y + value3.Y) * amount +
647 (2 * value1.Y - 5 * value2.Y + 4 * value3.Y - value4.Y) * amountSq +
648 (-value1.Y + 3 * value2.Y - 3 * value3.Y + value4.Y) * amountCu);
649 result.Z =
650 0.5f * ((2 * value2.Z) +
651 (-value1.Z + value3.Z) * amount +
652 (2 * value1.Z - 5 * value2.Z + 4 * value3.Z - value4.Z) * amountSq +
653 (-value1.Z + 3 * value2.Z - 3 * value3.Z + value4.Z) * amountCu);
654 }
655
656 public static Vector3D Max(Vector3D value1, Vector3D value2)
657 {
658 Vector3D result;
659 Max(ref value1, ref value2, out result);
660 return result;
661 }
662 public static void Max(ref Vector3D value1, ref Vector3D value2, out Vector3D result)
663 {
664 result.X = (value1.X < value2.X) ? (value2.X) : (value1.X);
665 result.Y = (value1.Y < value2.Y) ? (value2.Y) : (value1.Y);
666 result.Z = (value1.Z < value2.Z) ? (value2.Z) : (value1.Z);
667 }
668
669 public static Vector3D Min(Vector3D value1, Vector3D value2)
670 {
671 Vector3D result;
672 Min(ref value1, ref value2, out result);
673 return result;
674 }
675 public static void Min(ref Vector3D value1, ref Vector3D value2, out Vector3D result)
676 {
677 result.X = (value1.X > value2.X) ? (value2.X) : (value1.X);
678 result.Y = (value1.Y > value2.Y) ? (value2.Y) : (value1.Y);
679 result.Z = (value1.Z > value2.Z) ? (value2.Z) : (value1.Z);
680 }
681
682 #endregion
683 #region fields
687 [AdvBrowsable]
688 [XmlAttribute]
689#if !WINDOWS_STOREAPP
690 [System.ComponentModel.Description("The Magnitude on the X-Axis")]
691#endif
692 public Scalar X;
696 [AdvBrowsable]
697 [XmlAttribute]
698#if !WINDOWS_STOREAPP
699 [System.ComponentModel.Description("The Magnitude on the Y-Axis")]
700#endif
701 public Scalar Y;
705 [AdvBrowsable]
706 [XmlAttribute]
707#if !WINDOWS_STOREAPP
708 [System.ComponentModel.Description("The Magnitude on the Z-Axis")]
709#endif
710 public Scalar Z;
711 #endregion
712 #region constructors
719 [InstanceConstructor("X,Y,Z")]
721 {
722 //this.Vector2D = Vector2D.Zero;
723 this.X = X;
724 this.Y = Y;
725 this.Z = Z;
726 }
727 public Vector3D(Scalar[] vals) : this(vals, 0) { }
728 public Vector3D(Scalar[] vals, int index)
729 {
730 Copy(vals, index, out this);
731 }
732
733 #endregion
734 #region indexers
735#if UNSAFE
743 public Scalar this[int index]
744 {
745 get
746 {
747 ThrowHelper.CheckIndex("index", index, Count);
748 unsafe
749 {
750 fixed (Scalar* ptr = &this.X)
751 {
752 return ptr[index];
753 }
754 }
755 }
756 set
757 {
758 ThrowHelper.CheckIndex("index", index, Count);
759 unsafe
760 {
761 fixed (Scalar* ptr = &this.X)
762 {
763 ptr[index] = value;
764 }
765 }
766 }
767 }
768#endif
769 #endregion
770 #region public properties
775 [XmlIgnore]
777 {
778 get
779 {
780 return MathHelper.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
781 }
782 set
783 {
784 this = SetMagnitude(this, value);
785 }
786 }
791 {
792 get
793 {
794 return this.X * this.X + this.Y * this.Y + this.Z * this.Z;
795 }
796 }
802 {
803 get
804 {
805 return Normalize(this);
806 }
807 }
808
812 int IAdvanceValueType.Count { get { return Count; } }
813 #endregion
814 #region public methods
815 public Scalar[] ToArray()
816 {
817 Scalar[] array = new Scalar[Count];
818 Copy(ref this, array, 0);
819 return array;
820 }
821 public void CopyFrom(Scalar[] array, int index)
822 {
823 Copy(array, index, out this);
824 }
825 public void CopyTo(Scalar[] array, int index)
826 {
827 Copy(ref this, array, index);
828 }
829 #endregion
830 #region operators
838 public static Vector3D operator +(Vector3D left, Vector3D right)
839 {
840 Vector3D result;
841 result.X = left.X + right.X;
842 result.Y = left.Y + right.Y;
843 result.Z = left.Z + right.Z;
844 return result;
845 }
846 public static Vector3D operator +(Vector2D left, Vector3D right)
847 {
848 Vector3D result;
849 Add(ref left, ref right, out result);
850 return result;
851 }
852 public static Vector3D operator +(Vector3D left, Vector2D right)
853 {
854 Vector3D result;
855 Add(ref left, ref right, out result);
856 return result;
857 }
865 public static Vector3D operator -(Vector3D left, Vector3D right)
866 {
867 Vector3D result;
868 result.X = left.X - right.X;
869 result.Y = left.Y - right.Y;
870 result.Z = left.Z - right.Z;
871 return result;
872 }
873 public static Vector3D operator -(Vector2D left, Vector3D right)
874 {
875 Vector3D result;
876 Subtract(ref left, ref right, out result);
877 return result;
878 }
879 public static Vector3D operator -(Vector3D left, Vector2D right)
880 {
881 Vector3D result;
882 Subtract(ref left, ref right, out result);
883 return result;
884 }
892 public static Vector3D operator *(Vector3D source, Scalar scalar)
893 {
894 Vector3D result;
895 result.X = source.X * scalar;
896 result.Y = source.Y * scalar;
897 result.Z = source.Z * scalar;
898 return result;
899 }
907 public static Vector3D operator *(Scalar scalar, Vector3D source)
908 {
909 Vector3D result;
910 result.X = scalar * source.X;
911 result.Y = scalar * source.Y;
912 result.Z = scalar * source.Z;
913 return result;
914 }
922 public static Scalar operator *(Vector3D left, Vector3D right)
923 {
924 return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
925 }
937 public static Vector3D operator *(Matrix4x4 matrix, Vector3D vector)
938 {
939 Vector3D result;
940
941 Scalar inverseW = 1 / (matrix.m30 * vector.X + matrix.m31 * vector.Y + matrix.m32 * vector.Z + matrix.m33);
942 result.X = ((matrix.m00 * vector.X) + (matrix.m01 * vector.Y) + (matrix.m02 * vector.Z) + matrix.m03) * inverseW;
943 result.Y = ((matrix.m10 * vector.X) + (matrix.m11 * vector.Y) + (matrix.m12 * vector.Z) + matrix.m13) * inverseW;
944 result.Z = ((matrix.m20 * vector.X) + (matrix.m21 * vector.Y) + (matrix.m22 * vector.Z) + matrix.m23) * inverseW;
945
946 return result;
947 }
954 public static Vector3D operator *(Matrix3x3 matrix, Vector3D vector)
955 {
956 Vector3D result;
957
958 result.X = matrix.m00 * vector.X + matrix.m01 * vector.Y + matrix.m02 * vector.Z;
959 result.Y = matrix.m10 * vector.X + matrix.m11 * vector.Y + matrix.m12 * vector.Z;
960 result.Z = matrix.m20 * vector.X + matrix.m21 * vector.Y + matrix.m22 * vector.Z;
961
962 return result;
963 }
964 public static Vector3D operator *(Quaternion quat, Vector3D vector)
965 {
966 // nVidia SDK implementation
967 Vector3D uv, uuv;
968 Vector3D qvec;// = new Vector3D(quat.X, quat.Y, quat.Z);
969
970 qvec.X = quat.X;
971 qvec.Y = quat.Y;
972 qvec.Z = quat.Z;
973 Cross(ref qvec, ref vector, out uv);
974 Cross(ref qvec, ref uv, out uuv);
975 Cross(ref qvec, ref uv, out uuv);
976 Multiply(ref uv, ref quat.W, out uv);
977 Add(ref uv, ref uuv, out uv);
978 Multiply(ref uv, ref MathHelper.Two, out uv);
979 Add(ref vector, ref uv, out uv);
980 return uv;
981
982 //uv = qvec ^ vector;
983 //uuv = qvec ^ uv;
984 //uv *= (2 * quat.W);
985 //uuv *= 2;
986
987 //return vector + uv + uuv;
988
989 // get the rotation matriX of the Quaternion and multiplY it times the vector
990 //return quat.ToRotationMatrix() * vector;
991 }
998 public static Vector3D operator *(Vector3D vector, Matrix3x3 matrix)
999 {
1000 Vector3D result;
1001
1002 result.X = matrix.m00 * vector.X + matrix.m01 * vector.Y + matrix.m02 * vector.Z;
1003 result.Y = matrix.m10 * vector.X + matrix.m11 * vector.Y + matrix.m12 * vector.Z;
1004 result.Z = matrix.m20 * vector.X + matrix.m21 * vector.Y + matrix.m22 * vector.Z;
1005
1006 return result;
1007 }
1013 public static Vector3D operator -(Vector3D source)
1014 {
1015 Vector3D result;
1016 result.X = -source.X;
1017 result.Y = -source.Y;
1018 result.Z = -source.Z;
1019 return result;
1020 }
1030 public static Vector3D operator ^(Vector3D left, Vector3D right)
1031 {
1032 Vector3D result;
1033 result.X = left.Y * right.Z - left.Z * right.Y;
1034 result.Y = left.Z * right.X - left.X * right.Z;
1035 result.Z = left.X * right.Y - left.Y * right.X;
1036 return result;
1037 }
1044 public static bool operator ==(Vector3D left, Vector3D right)
1045 {
1046 return left.X == right.X && left.Y == right.Y && left.Z == right.Z;
1047 }
1054 public static bool operator !=(Vector3D left, Vector3D right)
1055 {
1056 return !(left.X == right.X && left.Y == right.Y && left.Z == right.Z);
1057 }
1058
1059 public static explicit operator Vector3D(Vector2D source)
1060 {
1061 Vector3D result;
1062 result.X = source.X;
1063 result.Y = source.Y;
1064 result.Z = 0;
1065 return result;
1066 }
1067
1068 public static explicit operator Vector3D(Vector4D source)
1069 {
1070 Vector3D result;
1071 result.X = source.X;
1072 result.Y = source.Y;
1073 result.Z = source.Z;
1074 return result;
1075 }
1076 #endregion
1077 #region overrides
1078 private string ToStringInternal(string FormatString)
1079 {
1080 return string.Format(FormatString, X, Y, Z);
1081 }
1082 public string ToString(string format)
1083 {
1084 return ToStringInternal(string.Format(FormatableString, format));
1085 }
1086 public override string ToString()
1087 {
1089 }
1090
1091#if !CompactFramework && !WindowsCE && !PocketPC && !XBOX360 && !SILVERLIGHT
1092 public static bool TryParse(string s, out Vector3D result)
1093 {
1094 if (s == null)
1095 {
1096 result = Zero;
1097 return false;
1098 }
1099 string[] vals = ParseHelper.SplitStringVector(s);
1100 if (vals.Length != Count)
1101 {
1102 result = Zero;
1103 return false;
1104 }
1105 if (Scalar.TryParse(vals[0], out result.X) &&
1106 Scalar.TryParse(vals[1], out result.Y) &&
1107 Scalar.TryParse(vals[2], out result.Z))
1108 {
1109 return true;
1110 }
1111 else
1112 {
1113 result = Zero;
1114 return false;
1115 }
1116 }
1117#endif
1118 [ParseMethod]
1119 public static Vector3D Parse(string s)
1120 {
1121 if (s == null)
1122 {
1123 throw new ArgumentNullException("s");
1124 }
1125 string[] vals = ParseHelper.SplitStringVector(s);
1126 if (vals.Length != Count)
1127 {
1129 }
1130 Vector3D value;
1131 value.X = Scalar.Parse(vals[0]);
1132 value.Y = Scalar.Parse(vals[1]);
1133 value.Z = Scalar.Parse(vals[2]);
1134 return value;
1135 }
1136
1146 public override int GetHashCode()
1147 {
1148 return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
1149 }
1150
1157 public override bool Equals(object obj)
1158 {
1159 return (obj is Vector3D) && Equals((Vector3D)obj);
1160 }
1161 public bool Equals(Vector3D other)
1162 {
1163 return Equals(ref this, ref other);
1164 }
1165 public static bool Equals(Vector3D left, Vector3D right)
1166 {
1167 return
1168 left.X == right.X &&
1169 left.Y == right.Y &&
1170 left.Z == right.Z;
1171 }
1172 public static bool Equals(ref Vector3D left, ref Vector3D right)
1173 {
1174 return
1175 left.X == right.X &&
1176 left.Y == right.Y &&
1177 left.Z == right.Z;
1178 }
1179 #endregion
1180 }
1181}
System.Single Scalar
Definition: Clamped.cs:29
System.Single Scalar
Definition: Vector3D.cs:28
static Scalar Clamp(Scalar value, Scalar min, Scalar max)
Definition: MathHelper.cs:111
static Scalar Sqrt(Scalar d)
Definition: MathHelper.cs:314
static void HermiteHelper(Scalar amount, out Scalar h1, out Scalar h2, out Scalar h3, out Scalar h4)
Definition: MathHelper.cs:86
static Scalar Two
Definition: MathHelper.cs:52
static string CreateVectorFormatableString(int Count)
Definition: MatrixHelper.cs:51
static string CreateVectorFormatString(int Count)
Definition: MatrixHelper.cs:47
static string[] SplitStringVector(string s)
Definition: ParseHelper.cs:39
static void ThrowVectorFormatException(string value, int count, string format)
Definition: ThrowHelper.cs:54
static void CheckCopy(Scalar[] array, int index, int count)
Definition: ThrowHelper.cs:35
int Count
Gets a 32-bit integer that represents the total number of elements in all the dimensions of IAdvanceV...
Definition: Interfaces.cs:41
A 3x3 matrix which can represent rotations around axes.
Definition: Matrix3x3.cs:62
Summary description for Quaternion.
Definition: Quaternion.cs:82
This is the Vector Class.
Definition: Vector2D.cs:50
Scalar X
This is the X value. (Usually represents a horizontal position or direction.)
Definition: Vector2D.cs:796
Scalar Y
This is the Y value. (Usually represents a vertical position or direction.)
Definition: Vector2D.cs:805
A Vector with 3 dimensions.
Definition: Vector3D.cs:47
static Vector3D Add(Vector3D left, Vector2D right)
Definition: Vector3D.cs:219
static readonly Vector3D XAxis
Vector3D(1,0,0)
Definition: Vector3D.cs:70
const int Size
The Size of the class in bytes;
Definition: Vector3D.cs:56
Scalar MagnitudeSq
Gets the Squared Magnitude of the Vector3D.
Definition: Vector3D.cs:791
static void Copy(Scalar[] sourceArray, int index, out Vector3D result)
Definition: Vector3D.cs:100
static void Copy(ref Vector4D source, out Vector3D dest)
Definition: Vector3D.cs:108
static void Normalize(ref Vector3D source)
Definition: Vector3D.cs:564
override int GetHashCode()
Provides a unique hash code based on the member variables of this class. This should be done because ...
Definition: Vector3D.cs:1146
static Vector3D Min(Vector3D value1, Vector3D value2)
Definition: Vector3D.cs:669
static bool operator!=(Vector3D left, Vector3D right)
Specifies whether the Vector3Ds do not contain the same coordinates.
Definition: Vector3D.cs:1054
static bool Equals(ref Vector3D left, ref Vector3D right)
Definition: Vector3D.cs:1172
Scalar X
This is the X value.
Definition: Vector3D.cs:692
void CopyFrom(Scalar[] array, int index)
Definition: Vector3D.cs:821
static void Negate(ref Vector3D source)
Definition: Vector3D.cs:581
static void Transform(ref Vector3D vector, ref Matrix3x3 matrix, out Vector3D result)
Definition: Vector3D.cs:362
static void Multiply(ref Vector3D source, ref Scalar scalar, out Vector3D result)
Definition: Vector3D.cs:295
static void Multiply(ref Quaternion quat, ref Vector3D vector, out Vector3D result)
Definition: Vector3D.cs:430
static void Transform(ref Matrix3x3 matrix, ref Vector3D vector, out Vector3D result)
Definition: Vector3D.cs:338
static void Add(ref Vector2D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:213
static void Subtract(ref Vector3D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:248
static Scalar Dot(Vector3D left, Vector3D right)
Does a Dot Operation Also know as an Inner Product.
Definition: Vector3D.cs:452
static Vector3D Subtract(Vector3D left, Vector3D right)
Subtracts 2 Vector3Ds.
Definition: Vector3D.cs:240
static Vector3D Lerp(Vector3D left, Vector3D right, Scalar amount)
Definition: Vector3D.cs:135
void CopyTo(Scalar[] array, int index)
Definition: Vector3D.cs:825
static void Negate(ref Vector3D source, out Vector3D result)
Definition: Vector3D.cs:585
static Vector3D Lerp(Vector3D left, Vector3D right, Vector3D amount)
Definition: Vector3D.cs:147
static void Normalize(ref Vector3D source, out Vector3D result)
Definition: Vector3D.cs:554
static void Subtract(ref Vector2D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:261
static void Lerp(ref Vector3D left, ref Vector3D right, ref Vector3D amount, out Vector3D result)
Definition: Vector3D.cs:153
static Vector3D Transform(Vector3D vector, Matrix3x3 matrix)
vector * matrix [1x3 * 3x3 = 1x3]
Definition: Vector3D.cs:352
static Vector3D Negate(Vector3D source)
Negates a Vector3D.
Definition: Vector3D.cs:573
static void Min(ref Vector3D value1, ref Vector3D value2, out Vector3D result)
Definition: Vector3D.cs:675
static void Copy(ref Vector3D vector, Scalar[] destArray, int index)
Definition: Vector3D.cs:88
static void Distance(ref Vector3D left, ref Vector3D right, out Scalar result)
Definition: Vector3D.cs:166
bool Equals(Vector3D other)
Definition: Vector3D.cs:1161
static Vector3D operator-(Vector3D left, Vector3D right)
Subtracts 2 Vector3Ds.
Definition: Vector3D.cs:865
static Vector3D Multiply(Scalar scalar, Vector3D source)
Does Scaler Multiplication on a Vector3D.
Definition: Vector3D.cs:308
static readonly Vector3D ZAxis
Vector3D(0,0,1)
Definition: Vector3D.cs:78
Vector3D(Scalar[] vals, int index)
Definition: Vector3D.cs:728
Vector3D(Scalar[] vals)
Definition: Vector3D.cs:727
static void Add(ref Vector3D left, ref Vector2D right, out Vector3D result)
Definition: Vector3D.cs:225
static readonly Vector3D One
Vector3D(1,1,1)
Definition: Vector3D.cs:62
static Vector3D operator+(Vector3D left, Vector3D right)
Adds 2 Vectors2Ds.
Definition: Vector3D.cs:838
static Vector3D Cross(Vector3D left, Vector3D right)
Does a Cross Operation Also know as an Outer Product.
Definition: Vector3D.cs:467
static void Subtract(ref Vector3D left, ref Vector2D right, out Vector3D result)
Definition: Vector3D.cs:273
static Vector3D Normalize(Vector3D source)
This returns the Normalized Vector3D that is passed. This is also known as a Unit Vector.
Definition: Vector3D.cs:542
static Vector3D Hermite(Vector3D value1, Vector3D tangent1, Vector3D value2, Vector3D tangent2, Scalar amount)
Definition: Vector3D.cs:614
static Scalar GetMagnitudeSq(Vector3D source)
Gets the Squared Magnitude of the Vector3D that is passed.
Definition: Vector3D.cs:488
Scalar Magnitude
Gets or Sets the Magnitude (Length) of the Vector3D.
Definition: Vector3D.cs:777
static readonly Vector3D Zero
Vector3D(0,0,0)
Definition: Vector3D.cs:66
static void GetMagnitude(ref Vector3D source, out Scalar result)
Definition: Vector3D.cs:505
static Scalar GetMagnitude(Vector3D source)
Gets the Magnitude of the Vector3D that is passed.
Definition: Vector3D.cs:501
static void Project(ref Vector3D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:605
static Vector3D Clamp(Vector3D value, Vector3D min, Vector3D max)
Definition: Vector3D.cs:120
const int Count
The number of Scalar values in the class.
Definition: Vector3D.cs:52
static Vector3D Subtract(Vector3D left, Vector2D right)
Definition: Vector3D.cs:267
static Vector3D Subtract(Vector2D left, Vector3D right)
Definition: Vector3D.cs:255
string ToStringInternal(string FormatString)
Definition: Vector3D.cs:1078
Vector3D(Scalar X, Scalar Y, Scalar Z)
Creates a New Vector3D Instance on the Stack.
Definition: Vector3D.cs:720
static void Copy(ref Vector2D source, ref Vector3D dest)
Definition: Vector3D.cs:114
static Vector3D Multiply(Vector3D source, Scalar scalar)
Does Scaler Multiplication on a Vector3D.
Definition: Vector3D.cs:287
static Scalar Distance(Vector3D left, Vector3D right)
Definition: Vector3D.cs:160
static void Copy(ref Vector3D vector, Scalar[] destArray)
Definition: Vector3D.cs:84
static Vector3D Parse(string s)
Definition: Vector3D.cs:1119
static Vector3D Transform(Matrix4x4 matrix, Vector3D vector)
Transforms the given 3-D vector by the matrix, projecting the result back into w = 1....
Definition: Vector3D.cs:381
string ToString(string format)
Definition: Vector3D.cs:1082
static void Hermite(ref Vector3D value1, ref Vector3D tangent1, ref Vector3D value2, ref Vector3D tangent2, Scalar amount, out Vector3D result)
Definition: Vector3D.cs:620
static bool TryParse(string s, out Vector3D result)
Definition: Vector3D.cs:1092
static void DistanceSq(ref Vector3D left, ref Vector3D right, out Scalar result)
Definition: Vector3D.cs:178
static readonly Vector3D YAxis
Vector3D(0,1,0)
Definition: Vector3D.cs:74
static Vector3D Multiply(Quaternion quat, Vector3D vector)
Definition: Vector3D.cs:402
override bool Equals(object obj)
Compares this Vector to another object. This should be done because the equality operators (==,...
Definition: Vector3D.cs:1157
static void Add(ref Vector3D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:200
static Vector3D CatmullRom(Vector3D value1, Vector3D value2, Vector3D value3, Vector3D value4, Scalar amount)
Definition: Vector3D.cs:629
Vector3D Normalized
Gets the Normalized Vector3D. (Unit Vector)
Definition: Vector3D.cs:802
static Vector3D Add(Vector3D left, Vector3D right)
Adds 2 Vectors2Ds.
Definition: Vector3D.cs:192
static readonly string FormatableString
Definition: Vector3D.cs:81
static Vector3D SetMagnitude(Vector3D source, Scalar magnitude)
Sets the Magnitude of a Vector3D.
Definition: Vector3D.cs:515
static void Dot(ref Vector3D left, ref Vector3D right, out Scalar result)
Definition: Vector3D.cs:456
static void GetMagnitudeSq(ref Vector3D source, out Scalar result)
Definition: Vector3D.cs:492
static bool Equals(Vector3D left, Vector3D right)
Definition: Vector3D.cs:1165
static Vector3D operator*(Vector3D source, Scalar scalar)
Does Scaler Multiplication on a Vector3D.
Definition: Vector3D.cs:892
Scalar[] ToArray()
Definition: Vector3D.cs:815
Scalar Y
This is the Y value.
Definition: Vector3D.cs:701
static void Clamp(ref Vector3D value, ref Vector3D min, ref Vector3D max, out Vector3D result)
Definition: Vector3D.cs:128
static void Max(ref Vector3D value1, ref Vector3D value2, out Vector3D result)
Definition: Vector3D.cs:662
static Vector3D Transform(Matrix3x3 matrix, Vector3D vector)
matrix * vector [3x3 * 3x1 = 3x1]
Definition: Vector3D.cs:328
static void Cross(ref Vector3D left, ref Vector3D right, out Vector3D result)
Definition: Vector3D.cs:475
static Vector3D Max(Vector3D value1, Vector3D value2)
Definition: Vector3D.cs:656
static Vector3D operator^(Vector3D left, Vector3D right)
Does a "2D" Cross Product also know as an Outer Product.
Definition: Vector3D.cs:1030
static void Lerp(ref Vector3D left, ref Vector3D right, ref Scalar amount, out Vector3D result)
Definition: Vector3D.cs:141
static void Copy(Scalar[] sourceArray, out Vector3D result)
Definition: Vector3D.cs:96
static Scalar DistanceSq(Vector3D left, Vector3D right)
Definition: Vector3D.cs:172
Scalar Z
This is the Z value.
Definition: Vector3D.cs:710
static Vector3D Project(Vector3D left, Vector3D right)
Thie Projects the left Vector3D onto the Right Vector3D.
Definition: Vector3D.cs:599
int IAdvanceValueType. Count
The Number of Variables accesable though the indexer.
Definition: Vector3D.cs:812
static void Transform(ref Matrix4x4 matrix, ref Vector3D vector, out Vector3D result)
Definition: Vector3D.cs:392
static void CatmullRom(ref Vector3D value1, ref Vector3D value2, ref Vector3D value3, ref Vector3D value4, Scalar amount, out Vector3D result)
Definition: Vector3D.cs:635
static void Multiply(ref Scalar scalar, ref Vector3D source, out Vector3D result)
Definition: Vector3D.cs:316
override string ToString()
Definition: Vector3D.cs:1086
static readonly string FormatString
Definition: Vector3D.cs:80
static Vector3D Add(Vector2D left, Vector3D right)
Definition: Vector3D.cs:207
static bool operator==(Vector3D left, Vector3D right)
Specifies whether the Vector3Ds contain the same coordinates.
Definition: Vector3D.cs:1044
static void SetMagnitude(ref Vector3D source, ref Scalar magnitude, out Vector3D result)
Definition: Vector3D.cs:521
A Vector with 4 dimensions.
Definition: Vector4D.cs:48
Scalar X
This is the X value.
Definition: Vector4D.cs:710
Scalar Y
This is the Y value.
Definition: Vector4D.cs:719
Scalar Z
This is the Z value.
Definition: Vector4D.cs:728