TSVector4.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /* Copyright (C) <2009-2011> <Thorben Linneweber, Jitter Physics>
  2. *
  3. * This software is provided 'as-is', without any express or implied
  4. * warranty. In no event will the authors be held liable for any damages
  5. * arising from the use of this software.
  6. *
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. *
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. namespace TrueSync
  21. {
  22. /// <summary>
  23. /// A vector structure.
  24. /// </summary>
  25. [Serializable]
  26. public struct TSVector4
  27. {
  28. private static FP ZeroEpsilonSq = TSMath.Epsilon;
  29. internal static TSVector4 InternalZero;
  30. /// <summary>The X component of the vector.</summary>
  31. public FP x;
  32. /// <summary>The Y component of the vector.</summary>
  33. public FP y;
  34. /// <summary>The Z component of the vector.</summary>
  35. public FP z;
  36. /// <summary>The W component of the vector.</summary>
  37. public FP w;
  38. #region Static readonly variables
  39. /// <summary>
  40. /// A vector with components (0,0,0,0);
  41. /// </summary>
  42. public static readonly TSVector4 zero;
  43. /// <summary>
  44. /// A vector with components (1,1,1,1);
  45. /// </summary>
  46. public static readonly TSVector4 one;
  47. /// <summary>
  48. /// A vector with components
  49. /// (FP.MinValue,FP.MinValue,FP.MinValue);
  50. /// </summary>
  51. public static readonly TSVector4 MinValue;
  52. /// <summary>
  53. /// A vector with components
  54. /// (FP.MaxValue,FP.MaxValue,FP.MaxValue);
  55. /// </summary>
  56. public static readonly TSVector4 MaxValue;
  57. #endregion
  58. #region Private static constructor
  59. static TSVector4()
  60. {
  61. one = new TSVector4(1, 1, 1, 1);
  62. zero = new TSVector4(0, 0, 0, 0);
  63. MinValue = new TSVector4(FP.MinValue);
  64. MaxValue = new TSVector4(FP.MaxValue);
  65. InternalZero = zero;
  66. }
  67. #endregion
  68. public static TSVector4 Abs(TSVector4 other)
  69. {
  70. return new TSVector4(FP.Abs(other.x), FP.Abs(other.y), FP.Abs(other.z), FP.Abs(other.z));
  71. }
  72. /// <summary>
  73. /// Gets the squared length of the vector.
  74. /// </summary>
  75. /// <returns>Returns the squared length of the vector.</returns>
  76. public FP sqrMagnitude
  77. {
  78. get
  79. {
  80. return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z) + (this.w * this.w));
  81. }
  82. }
  83. /// <summary>
  84. /// Gets the length of the vector.
  85. /// </summary>
  86. /// <returns>Returns the length of the vector.</returns>
  87. public FP magnitude
  88. {
  89. get
  90. {
  91. FP num = sqrMagnitude;
  92. return FP.Sqrt(num);
  93. }
  94. }
  95. public static TSVector4 ClampMagnitude(TSVector4 vector, FP maxLength)
  96. {
  97. return Normalize(vector) * maxLength;
  98. }
  99. /// <summary>
  100. /// Gets a normalized version of the vector.
  101. /// </summary>
  102. /// <returns>Returns a normalized version of the vector.</returns>
  103. public TSVector4 normalized
  104. {
  105. get
  106. {
  107. TSVector4 result = new TSVector4(this.x, this.y, this.z, this.w);
  108. result.Normalize();
  109. return result;
  110. }
  111. }
  112. /// <summary>
  113. /// Constructor initializing a new instance of the structure
  114. /// </summary>
  115. /// <param name="x">The X component of the vector.</param>
  116. /// <param name="y">The Y component of the vector.</param>
  117. /// <param name="z">The Z component of the vector.</param>
  118. /// <param name="w">The W component of the vector.</param>
  119. public TSVector4(int x, int y, int z, int w)
  120. {
  121. this.x = (FP)x;
  122. this.y = (FP)y;
  123. this.z = (FP)z;
  124. this.w = (FP)w;
  125. }
  126. public TSVector4(FP x, FP y, FP z, FP w)
  127. {
  128. this.x = x;
  129. this.y = y;
  130. this.z = z;
  131. this.w = w;
  132. }
  133. /// <summary>
  134. /// Multiplies each component of the vector by the same components of the provided vector.
  135. /// </summary>
  136. public void Scale(TSVector4 other)
  137. {
  138. this.x = x * other.x;
  139. this.y = y * other.y;
  140. this.z = z * other.z;
  141. this.w = w * other.w;
  142. }
  143. /// <summary>
  144. /// Sets all vector component to specific values.
  145. /// </summary>
  146. /// <param name="x">The X component of the vector.</param>
  147. /// <param name="y">The Y component of the vector.</param>
  148. /// <param name="z">The Z component of the vector.</param>
  149. /// <param name="w">The W component of the vector.</param>
  150. public void Set(FP x, FP y, FP z, FP w)
  151. {
  152. this.x = x;
  153. this.y = y;
  154. this.z = z;
  155. this.w = w;
  156. }
  157. /// <summary>
  158. /// Constructor initializing a new instance of the structure
  159. /// </summary>
  160. /// <param name="xyz">All components of the vector are set to xyz</param>
  161. public TSVector4(FP xyzw)
  162. {
  163. this.x = xyzw;
  164. this.y = xyzw;
  165. this.z = xyzw;
  166. this.w = xyzw;
  167. }
  168. public static TSVector4 Lerp(TSVector4 from, TSVector4 to, FP percent)
  169. {
  170. return from + (to - from) * percent;
  171. }
  172. /// <summary>
  173. /// Builds a string from the JVector.
  174. /// </summary>
  175. /// <returns>A string containing all three components.</returns>
  176. #region public override string ToString()
  177. public override string ToString()
  178. {
  179. return string.Format("({0:f1}, {1:f1}, {2:f1}, {3:f1})", x.AsFloat(), y.AsFloat(), z.AsFloat(), w.AsFloat());
  180. }
  181. #endregion
  182. /// <summary>
  183. /// Tests if an object is equal to this vector.
  184. /// </summary>
  185. /// <param name="obj">The object to test.</param>
  186. /// <returns>Returns true if they are euqal, otherwise false.</returns>
  187. #region public override bool Equals(object obj)
  188. public override bool Equals(object obj)
  189. {
  190. if (!(obj is TSVector4)) return false;
  191. TSVector4 other = (TSVector4)obj;
  192. return (((x == other.x) && (y == other.y)) && (z == other.z) && (w == other.w));
  193. }
  194. #endregion
  195. /// <summary>
  196. /// Multiplies each component of the vector by the same components of the provided vector.
  197. /// </summary>
  198. public static TSVector4 Scale(TSVector4 vecA, TSVector4 vecB)
  199. {
  200. TSVector4 result;
  201. result.x = vecA.x * vecB.x;
  202. result.y = vecA.y * vecB.y;
  203. result.z = vecA.z * vecB.z;
  204. result.w = vecA.w * vecB.w;
  205. return result;
  206. }
  207. /// <summary>
  208. /// Tests if two JVector are equal.
  209. /// </summary>
  210. /// <param name="value1">The first value.</param>
  211. /// <param name="value2">The second value.</param>
  212. /// <returns>Returns true if both values are equal, otherwise false.</returns>
  213. #region public static bool operator ==(JVector value1, JVector value2)
  214. public static bool operator ==(TSVector4 value1, TSVector4 value2)
  215. {
  216. return (((value1.x == value2.x) && (value1.y == value2.y)) && (value1.z == value2.z) && (value1.w == value2.w));
  217. }
  218. #endregion
  219. /// <summary>
  220. /// Tests if two JVector are not equal.
  221. /// </summary>
  222. /// <param name="value1">The first value.</param>
  223. /// <param name="value2">The second value.</param>
  224. /// <returns>Returns false if both values are equal, otherwise true.</returns>
  225. #region public static bool operator !=(JVector value1, JVector value2)
  226. public static bool operator !=(TSVector4 value1, TSVector4 value2)
  227. {
  228. if ((value1.x == value2.x) && (value1.y == value2.y) && (value1.z == value2.z))
  229. {
  230. return (value1.w != value2.w);
  231. }
  232. return true;
  233. }
  234. #endregion
  235. /// <summary>
  236. /// Gets a vector with the minimum x,y and z values of both vectors.
  237. /// </summary>
  238. /// <param name="value1">The first value.</param>
  239. /// <param name="value2">The second value.</param>
  240. /// <returns>A vector with the minimum x,y and z values of both vectors.</returns>
  241. #region public static JVector Min(JVector value1, JVector value2)
  242. public static TSVector4 Min(TSVector4 value1, TSVector4 value2)
  243. {
  244. TSVector4 result;
  245. TSVector4.Min(ref value1, ref value2, out result);
  246. return result;
  247. }
  248. /// <summary>
  249. /// Gets a vector with the minimum x,y and z values of both vectors.
  250. /// </summary>
  251. /// <param name="value1">The first value.</param>
  252. /// <param name="value2">The second value.</param>
  253. /// <param name="result">A vector with the minimum x,y and z values of both vectors.</param>
  254. public static void Min(ref TSVector4 value1, ref TSVector4 value2, out TSVector4 result)
  255. {
  256. result.x = (value1.x < value2.x) ? value1.x : value2.x;
  257. result.y = (value1.y < value2.y) ? value1.y : value2.y;
  258. result.z = (value1.z < value2.z) ? value1.z : value2.z;
  259. result.w = (value1.w < value2.w) ? value1.w : value2.w;
  260. }
  261. #endregion
  262. /// <summary>
  263. /// Gets a vector with the maximum x,y and z values of both vectors.
  264. /// </summary>
  265. /// <param name="value1">The first value.</param>
  266. /// <param name="value2">The second value.</param>
  267. /// <returns>A vector with the maximum x,y and z values of both vectors.</returns>
  268. #region public static JVector Max(JVector value1, JVector value2)
  269. public static TSVector4 Max(TSVector4 value1, TSVector4 value2)
  270. {
  271. TSVector4 result;
  272. TSVector4.Max(ref value1, ref value2, out result);
  273. return result;
  274. }
  275. public static FP Distance(TSVector4 v1, TSVector4 v2)
  276. {
  277. return FP.Sqrt((v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y) + (v1.z - v2.z) * (v1.z - v2.z) + (v1.w - v2.w) * (v1.w - v2.w));
  278. }
  279. /// <summary>
  280. /// Gets a vector with the maximum x,y and z values of both vectors.
  281. /// </summary>
  282. /// <param name="value1">The first value.</param>
  283. /// <param name="value2">The second value.</param>
  284. /// <param name="result">A vector with the maximum x,y and z values of both vectors.</param>
  285. public static void Max(ref TSVector4 value1, ref TSVector4 value2, out TSVector4 result)
  286. {
  287. result.x = (value1.x > value2.x) ? value1.x : value2.x;
  288. result.y = (value1.y > value2.y) ? value1.y : value2.y;
  289. result.z = (value1.z > value2.z) ? value1.z : value2.z;
  290. result.w = (value1.w > value2.w) ? value1.w : value2.w;
  291. }
  292. #endregion
  293. /// <summary>
  294. /// Sets the length of the vector to zero.
  295. /// </summary>
  296. #region public void MakeZero()
  297. public void MakeZero()
  298. {
  299. x = FP.Zero;
  300. y = FP.Zero;
  301. z = FP.Zero;
  302. w = FP.Zero;
  303. }
  304. #endregion
  305. /// <summary>
  306. /// Checks if the length of the vector is zero.
  307. /// </summary>
  308. /// <returns>Returns true if the vector is zero, otherwise false.</returns>
  309. #region public bool IsZero()
  310. public bool IsZero()
  311. {
  312. return (this.sqrMagnitude == FP.Zero);
  313. }
  314. /// <summary>
  315. /// Checks if the length of the vector is nearly zero.
  316. /// </summary>
  317. /// <returns>Returns true if the vector is nearly zero, otherwise false.</returns>
  318. public bool IsNearlyZero()
  319. {
  320. return (this.sqrMagnitude < ZeroEpsilonSq);
  321. }
  322. #endregion
  323. /// <summary>
  324. /// Transforms a vector by the given matrix.
  325. /// </summary>
  326. /// <param name="position">The vector to transform.</param>
  327. /// <param name="matrix">The transform matrix.</param>
  328. /// <returns>The transformed vector.</returns>
  329. #region public static JVector Transform(JVector position, JMatrix matrix)
  330. public static TSVector4 Transform(TSVector4 position, TSMatrix4x4 matrix)
  331. {
  332. TSVector4 result;
  333. TSVector4.Transform(ref position, ref matrix, out result);
  334. return result;
  335. }
  336. public static TSVector4 Transform(TSVector position, TSMatrix4x4 matrix)
  337. {
  338. TSVector4 result;
  339. TSVector4.Transform(ref position, ref matrix, out result);
  340. return result;
  341. }
  342. /// <summary>
  343. /// Transforms a vector by the given matrix.
  344. /// </summary>
  345. /// <param name="vector">The vector to transform.</param>
  346. /// <param name="matrix">The transform matrix.</param>
  347. /// <param name="result">The transformed vector.</param>
  348. public static void Transform(ref TSVector vector, ref TSMatrix4x4 matrix, out TSVector4 result)
  349. {
  350. result.x = vector.x * matrix.M11 + vector.y * matrix.M12 + vector.z * matrix.M13 + matrix.M14;
  351. result.y = vector.x * matrix.M21 + vector.y * matrix.M22 + vector.z * matrix.M23 + matrix.M24;
  352. result.z = vector.x * matrix.M31 + vector.y * matrix.M32 + vector.z * matrix.M33 + matrix.M34;
  353. result.w = vector.x * matrix.M41 + vector.y * matrix.M42 + vector.z * matrix.M43 + matrix.M44;
  354. }
  355. public static void Transform(ref TSVector4 vector, ref TSMatrix4x4 matrix, out TSVector4 result)
  356. {
  357. result.x = vector.x * matrix.M11 + vector.y * matrix.M12 + vector.z * matrix.M13 + vector.w * matrix.M14;
  358. result.y = vector.x * matrix.M21 + vector.y * matrix.M22 + vector.z * matrix.M23 + vector.w * matrix.M24;
  359. result.z = vector.x * matrix.M31 + vector.y * matrix.M32 + vector.z * matrix.M33 + vector.w * matrix.M34;
  360. result.w = vector.x * matrix.M41 + vector.y * matrix.M42 + vector.z * matrix.M43 + vector.w * matrix.M44;
  361. }
  362. #endregion
  363. /// <summary>
  364. /// Calculates the dot product of two vectors.
  365. /// </summary>
  366. /// <param name="vector1">The first vector.</param>
  367. /// <param name="vector2">The second vector.</param>
  368. /// <returns>Returns the dot product of both vectors.</returns>
  369. #region public static FP Dot(JVector vector1, JVector vector2)
  370. public static FP Dot(TSVector4 vector1, TSVector4 vector2)
  371. {
  372. return TSVector4.Dot(ref vector1, ref vector2);
  373. }
  374. /// <summary>
  375. /// Calculates the dot product of both vectors.
  376. /// </summary>
  377. /// <param name="vector1">The first vector.</param>
  378. /// <param name="vector2">The second vector.</param>
  379. /// <returns>Returns the dot product of both vectors.</returns>
  380. public static FP Dot(ref TSVector4 vector1, ref TSVector4 vector2)
  381. {
  382. return ((vector1.x * vector2.x) + (vector1.y * vector2.y)) + (vector1.z * vector2.z) + (vector1.w * vector2.w);
  383. }
  384. #endregion
  385. /// <summary>
  386. /// Adds two vectors.
  387. /// </summary>
  388. /// <param name="value1">The first vector.</param>
  389. /// <param name="value2">The second vector.</param>
  390. /// <returns>The sum of both vectors.</returns>
  391. #region public static void Add(JVector value1, JVector value2)
  392. public static TSVector4 Add(TSVector4 value1, TSVector4 value2)
  393. {
  394. TSVector4 result;
  395. TSVector4.Add(ref value1, ref value2, out result);
  396. return result;
  397. }
  398. /// <summary>
  399. /// Adds to vectors.
  400. /// </summary>
  401. /// <param name="value1">The first vector.</param>
  402. /// <param name="value2">The second vector.</param>
  403. /// <param name="result">The sum of both vectors.</param>
  404. public static void Add(ref TSVector4 value1, ref TSVector4 value2, out TSVector4 result)
  405. {
  406. result.x = value1.x + value2.x;
  407. result.y = value1.y + value2.y;
  408. result.z = value1.z + value2.z;
  409. result.w = value1.w + value2.w;
  410. }
  411. #endregion
  412. /// <summary>
  413. /// Divides a vector by a factor.
  414. /// </summary>
  415. /// <param name="value1">The vector to divide.</param>
  416. /// <param name="scaleFactor">The scale factor.</param>
  417. /// <returns>Returns the scaled vector.</returns>
  418. public static TSVector4 Divide(TSVector4 value1, FP scaleFactor)
  419. {
  420. TSVector4 result;
  421. TSVector4.Divide(ref value1, scaleFactor, out result);
  422. return result;
  423. }
  424. /// <summary>
  425. /// Divides a vector by a factor.
  426. /// </summary>
  427. /// <param name="value1">The vector to divide.</param>
  428. /// <param name="scaleFactor">The scale factor.</param>
  429. /// <param name="result">Returns the scaled vector.</param>
  430. public static void Divide(ref TSVector4 value1, FP scaleFactor, out TSVector4 result)
  431. {
  432. result.x = value1.x / scaleFactor;
  433. result.y = value1.y / scaleFactor;
  434. result.z = value1.z / scaleFactor;
  435. result.w = value1.w / scaleFactor;
  436. }
  437. /// <summary>
  438. /// Subtracts two vectors.
  439. /// </summary>
  440. /// <param name="value1">The first vector.</param>
  441. /// <param name="value2">The second vector.</param>
  442. /// <returns>The difference of both vectors.</returns>
  443. #region public static JVector Subtract(JVector value1, JVector value2)
  444. public static TSVector4 Subtract(TSVector4 value1, TSVector4 value2)
  445. {
  446. TSVector4 result;
  447. TSVector4.Subtract(ref value1, ref value2, out result);
  448. return result;
  449. }
  450. /// <summary>
  451. /// Subtracts to vectors.
  452. /// </summary>
  453. /// <param name="value1">The first vector.</param>
  454. /// <param name="value2">The second vector.</param>
  455. /// <param name="result">The difference of both vectors.</param>
  456. public static void Subtract(ref TSVector4 value1, ref TSVector4 value2, out TSVector4 result)
  457. {
  458. result.x = value1.x - value2.x;
  459. result.y = value1.y - value2.y;
  460. result.z = value1.z - value2.z;
  461. result.w = value1.w - value2.w;
  462. }
  463. #endregion
  464. /// <summary>
  465. /// Gets the hashcode of the vector.
  466. /// </summary>
  467. /// <returns>Returns the hashcode of the vector.</returns>
  468. #region public override int GetHashCode()
  469. public override int GetHashCode()
  470. {
  471. return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode() ^ w.GetHashCode();
  472. }
  473. #endregion
  474. /// <summary>
  475. /// Inverses the direction of the vector.
  476. /// </summary>
  477. #region public static JVector Negate(JVector value)
  478. public void Negate()
  479. {
  480. this.x = -this.x;
  481. this.y = -this.y;
  482. this.z = -this.z;
  483. this.w = -this.w;
  484. }
  485. /// <summary>
  486. /// Inverses the direction of a vector.
  487. /// </summary>
  488. /// <param name="value">The vector to inverse.</param>
  489. /// <returns>The negated vector.</returns>
  490. public static TSVector4 Negate(TSVector4 value)
  491. {
  492. TSVector4 result;
  493. TSVector4.Negate(ref value, out result);
  494. return result;
  495. }
  496. /// <summary>
  497. /// Inverses the direction of a vector.
  498. /// </summary>
  499. /// <param name="value">The vector to inverse.</param>
  500. /// <param name="result">The negated vector.</param>
  501. public static void Negate(ref TSVector4 value, out TSVector4 result)
  502. {
  503. result.x = -value.x;
  504. result.y = -value.y;
  505. result.z = -value.z;
  506. result.w = -value.w;
  507. }
  508. #endregion
  509. /// <summary>
  510. /// Normalizes the given vector.
  511. /// </summary>
  512. /// <param name="value">The vector which should be normalized.</param>
  513. /// <returns>A normalized vector.</returns>
  514. #region public static JVector Normalize(JVector value)
  515. public static TSVector4 Normalize(TSVector4 value)
  516. {
  517. TSVector4 result;
  518. TSVector4.Normalize(ref value, out result);
  519. return result;
  520. }
  521. /// <summary>
  522. /// Normalizes this vector.
  523. /// </summary>
  524. public void Normalize()
  525. {
  526. FP num2 = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z) + (this.w * this.w);
  527. FP num = FP.One / FP.Sqrt(num2);
  528. this.x *= num;
  529. this.y *= num;
  530. this.z *= num;
  531. this.w *= num;
  532. }
  533. /// <summary>
  534. /// Normalizes the given vector.
  535. /// </summary>
  536. /// <param name="value">The vector which should be normalized.</param>
  537. /// <param name="result">A normalized vector.</param>
  538. public static void Normalize(ref TSVector4 value, out TSVector4 result)
  539. {
  540. FP num2 = ((value.x * value.x) + (value.y * value.y)) + (value.z * value.z) + (value.w * value.w);
  541. FP num = FP.One / FP.Sqrt(num2);
  542. result.x = value.x * num;
  543. result.y = value.y * num;
  544. result.z = value.z * num;
  545. result.w = value.w * num;
  546. }
  547. #endregion
  548. #region public static void Swap(ref JVector vector1, ref JVector vector2)
  549. /// <summary>
  550. /// Swaps the components of both vectors.
  551. /// </summary>
  552. /// <param name="vector1">The first vector to swap with the second.</param>
  553. /// <param name="vector2">The second vector to swap with the first.</param>
  554. public static void Swap(ref TSVector4 vector1, ref TSVector4 vector2)
  555. {
  556. FP temp;
  557. temp = vector1.x;
  558. vector1.x = vector2.x;
  559. vector2.x = temp;
  560. temp = vector1.y;
  561. vector1.y = vector2.y;
  562. vector2.y = temp;
  563. temp = vector1.z;
  564. vector1.z = vector2.z;
  565. vector2.z = temp;
  566. temp = vector1.w;
  567. vector1.w = vector2.w;
  568. vector2.w = temp;
  569. }
  570. #endregion
  571. /// <summary>
  572. /// Multiply a vector with a factor.
  573. /// </summary>
  574. /// <param name="value1">The vector to multiply.</param>
  575. /// <param name="scaleFactor">The scale factor.</param>
  576. /// <returns>Returns the multiplied vector.</returns>
  577. #region public static JVector Multiply(JVector value1, FP scaleFactor)
  578. public static TSVector4 Multiply(TSVector4 value1, FP scaleFactor)
  579. {
  580. TSVector4 result;
  581. TSVector4.Multiply(ref value1, scaleFactor, out result);
  582. return result;
  583. }
  584. /// <summary>
  585. /// Multiply a vector with a factor.
  586. /// </summary>
  587. /// <param name="value1">The vector to multiply.</param>
  588. /// <param name="scaleFactor">The scale factor.</param>
  589. /// <param name="result">Returns the multiplied vector.</param>
  590. public static void Multiply(ref TSVector4 value1, FP scaleFactor, out TSVector4 result)
  591. {
  592. result.x = value1.x * scaleFactor;
  593. result.y = value1.y * scaleFactor;
  594. result.z = value1.z * scaleFactor;
  595. result.w = value1.w * scaleFactor;
  596. }
  597. #endregion
  598. /// <summary>
  599. /// Calculates the dot product of two vectors.
  600. /// </summary>
  601. /// <param name="value1">The first vector.</param>
  602. /// <param name="value2">The second vector.</param>
  603. /// <returns>Returns the dot product of both.</returns>
  604. #region public static FP operator *(JVector value1, JVector value2)
  605. public static FP operator *(TSVector4 value1, TSVector4 value2)
  606. {
  607. return TSVector4.Dot(ref value1, ref value2);
  608. }
  609. #endregion
  610. /// <summary>
  611. /// Multiplies a vector by a scale factor.
  612. /// </summary>
  613. /// <param name="value1">The vector to scale.</param>
  614. /// <param name="value2">The scale factor.</param>
  615. /// <returns>Returns the scaled vector.</returns>
  616. #region public static JVector operator *(JVector value1, FP value2)
  617. public static TSVector4 operator *(TSVector4 value1, FP value2)
  618. {
  619. TSVector4 result;
  620. TSVector4.Multiply(ref value1, value2, out result);
  621. return result;
  622. }
  623. #endregion
  624. /// <summary>
  625. /// Multiplies a vector by a scale factor.
  626. /// </summary>
  627. /// <param name="value2">The vector to scale.</param>
  628. /// <param name="value1">The scale factor.</param>
  629. /// <returns>Returns the scaled vector.</returns>
  630. #region public static JVector operator *(FP value1, JVector value2)
  631. public static TSVector4 operator *(FP value1, TSVector4 value2)
  632. {
  633. TSVector4 result;
  634. TSVector4.Multiply(ref value2, value1, out result);
  635. return result;
  636. }
  637. #endregion
  638. /// <summary>
  639. /// Subtracts two vectors.
  640. /// </summary>
  641. /// <param name="value1">The first vector.</param>
  642. /// <param name="value2">The second vector.</param>
  643. /// <returns>The difference of both vectors.</returns>
  644. #region public static JVector operator -(JVector value1, JVector value2)
  645. public static TSVector4 operator -(TSVector4 value1, TSVector4 value2)
  646. {
  647. TSVector4 result; TSVector4.Subtract(ref value1, ref value2, out result);
  648. return result;
  649. }
  650. #endregion
  651. /// <summary>
  652. /// Adds two vectors.
  653. /// </summary>
  654. /// <param name="value1">The first vector.</param>
  655. /// <param name="value2">The second vector.</param>
  656. /// <returns>The sum of both vectors.</returns>
  657. #region public static JVector operator +(JVector value1, JVector value2)
  658. public static TSVector4 operator +(TSVector4 value1, TSVector4 value2)
  659. {
  660. TSVector4 result; TSVector4.Add(ref value1, ref value2, out result);
  661. return result;
  662. }
  663. #endregion
  664. /// <summary>
  665. /// Divides a vector by a factor.
  666. /// </summary>
  667. /// <param name="value1">The vector to divide.</param>
  668. /// <param name="scaleFactor">The scale factor.</param>
  669. /// <returns>Returns the scaled vector.</returns>
  670. public static TSVector4 operator /(TSVector4 value1, FP value2)
  671. {
  672. TSVector4 result;
  673. TSVector4.Divide(ref value1, value2, out result);
  674. return result;
  675. }
  676. public TSVector2 ToTSVector2()
  677. {
  678. return new TSVector2(this.x, this.y);
  679. }
  680. public TSVector ToTSVector()
  681. {
  682. return new TSVector(this.x, this.y, this.z);
  683. }
  684. }
  685. }