TSVector.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 TSVector
  27. {
  28. private static FP ZeroEpsilonSq = TSMath.Epsilon;
  29. internal static TSVector InternalZero;
  30. internal static TSVector Arbitrary;
  31. /// <summary>The X component of the vector.</summary>
  32. public FP x;
  33. /// <summary>The Y component of the vector.</summary>
  34. public FP y;
  35. /// <summary>The Z component of the vector.</summary>
  36. public FP z;
  37. #region Static readonly variables
  38. /// <summary>
  39. /// A vector with components (0,0,0);
  40. /// </summary>
  41. public static readonly TSVector zero;
  42. /// <summary>
  43. /// A vector with components (-1,0,0);
  44. /// </summary>
  45. public static readonly TSVector left;
  46. /// <summary>
  47. /// A vector with components (1,0,0);
  48. /// </summary>
  49. public static readonly TSVector right;
  50. /// <summary>
  51. /// A vector with components (0,1,0);
  52. /// </summary>
  53. public static readonly TSVector up;
  54. /// <summary>
  55. /// A vector with components (0,-1,0);
  56. /// </summary>
  57. public static readonly TSVector down;
  58. /// <summary>
  59. /// A vector with components (0,0,-1);
  60. /// </summary>
  61. public static readonly TSVector back;
  62. /// <summary>
  63. /// A vector with components (0,0,1);
  64. /// </summary>
  65. public static readonly TSVector forward;
  66. /// <summary>
  67. /// A vector with components (1,1,1);
  68. /// </summary>
  69. public static readonly TSVector one;
  70. /// <summary>
  71. /// A vector with components
  72. /// (FP.MinValue,FP.MinValue,FP.MinValue);
  73. /// </summary>
  74. public static readonly TSVector MinValue;
  75. /// <summary>
  76. /// A vector with components
  77. /// (FP.MaxValue,FP.MaxValue,FP.MaxValue);
  78. /// </summary>
  79. public static readonly TSVector MaxValue;
  80. #endregion
  81. #region Private static constructor
  82. static TSVector()
  83. {
  84. one = new TSVector(1, 1, 1);
  85. zero = new TSVector(0, 0, 0);
  86. left = new TSVector(-1, 0, 0);
  87. right = new TSVector(1, 0, 0);
  88. up = new TSVector(0, 1, 0);
  89. down = new TSVector(0, -1, 0);
  90. back = new TSVector(0, 0, -1);
  91. forward = new TSVector(0, 0, 1);
  92. MinValue = new TSVector(FP.MinValue);
  93. MaxValue = new TSVector(FP.MaxValue);
  94. Arbitrary = new TSVector(1, 1, 1);
  95. InternalZero = zero;
  96. }
  97. #endregion
  98. public static TSVector Abs(TSVector other) {
  99. return new TSVector(FP.Abs(other.x), FP.Abs(other.y), FP.Abs(other.z));
  100. }
  101. /// <summary>
  102. /// Gets the squared length of the vector.
  103. /// </summary>
  104. /// <returns>Returns the squared length of the vector.</returns>
  105. public FP sqrMagnitude {
  106. get {
  107. return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
  108. }
  109. }
  110. /// <summary>
  111. /// Gets the length of the vector.
  112. /// </summary>
  113. /// <returns>Returns the length of the vector.</returns>
  114. public FP magnitude {
  115. get {
  116. FP num = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
  117. return FP.Sqrt(num);
  118. }
  119. }
  120. public static TSVector ClampMagnitude(TSVector vector, FP maxLength) {
  121. return Normalize(vector) * maxLength;
  122. }
  123. /// <summary>
  124. /// Gets a normalized version of the vector.
  125. /// </summary>
  126. /// <returns>Returns a normalized version of the vector.</returns>
  127. public TSVector normalized {
  128. get {
  129. TSVector result = new TSVector(this.x, this.y, this.z);
  130. result.Normalize();
  131. return result;
  132. }
  133. }
  134. /// <summary>
  135. /// Constructor initializing a new instance of the structure
  136. /// </summary>
  137. /// <param name="x">The X component of the vector.</param>
  138. /// <param name="y">The Y component of the vector.</param>
  139. /// <param name="z">The Z component of the vector.</param>
  140. public TSVector(int x,int y,int z)
  141. {
  142. this.x = (FP)x;
  143. this.y = (FP)y;
  144. this.z = (FP)z;
  145. }
  146. public TSVector(FP x, FP y, FP z)
  147. {
  148. this.x = x;
  149. this.y = y;
  150. this.z = z;
  151. }
  152. /// <summary>
  153. /// Multiplies each component of the vector by the same components of the provided vector.
  154. /// </summary>
  155. public void Scale(TSVector other) {
  156. this.x = x * other.x;
  157. this.y = y * other.y;
  158. this.z = z * other.z;
  159. }
  160. /// <summary>
  161. /// Sets all vector component to specific values.
  162. /// </summary>
  163. /// <param name="x">The X component of the vector.</param>
  164. /// <param name="y">The Y component of the vector.</param>
  165. /// <param name="z">The Z component of the vector.</param>
  166. public void Set(FP x, FP y, FP z)
  167. {
  168. this.x = x;
  169. this.y = y;
  170. this.z = z;
  171. }
  172. /// <summary>
  173. /// Constructor initializing a new instance of the structure
  174. /// </summary>
  175. /// <param name="xyz">All components of the vector are set to xyz</param>
  176. public TSVector(FP xyz)
  177. {
  178. this.x = xyz;
  179. this.y = xyz;
  180. this.z = xyz;
  181. }
  182. public static TSVector Lerp(TSVector from, TSVector to, FP percent) {
  183. return from + (to - from) * percent;
  184. }
  185. /// <summary>
  186. /// Builds a string from the JVector.
  187. /// </summary>
  188. /// <returns>A string containing all three components.</returns>
  189. #region public override string ToString()
  190. public override string ToString() {
  191. return string.Format("({0:f1}, {1:f1}, {2:f1})", x.AsFloat(), y.AsFloat(), z.AsFloat());
  192. }
  193. #endregion
  194. /// <summary>
  195. /// Tests if an object is equal to this vector.
  196. /// </summary>
  197. /// <param name="obj">The object to test.</param>
  198. /// <returns>Returns true if they are euqal, otherwise false.</returns>
  199. #region public override bool Equals(object obj)
  200. public override bool Equals(object obj)
  201. {
  202. if (!(obj is TSVector)) return false;
  203. TSVector other = (TSVector)obj;
  204. return (((x == other.x) && (y == other.y)) && (z == other.z));
  205. }
  206. #endregion
  207. /// <summary>
  208. /// Multiplies each component of the vector by the same components of the provided vector.
  209. /// </summary>
  210. public static TSVector Scale(TSVector vecA, TSVector vecB) {
  211. TSVector result;
  212. result.x = vecA.x * vecB.x;
  213. result.y = vecA.y * vecB.y;
  214. result.z = vecA.z * vecB.z;
  215. return result;
  216. }
  217. /// <summary>
  218. /// Tests if two JVector are equal.
  219. /// </summary>
  220. /// <param name="value1">The first value.</param>
  221. /// <param name="value2">The second value.</param>
  222. /// <returns>Returns true if both values are equal, otherwise false.</returns>
  223. #region public static bool operator ==(JVector value1, JVector value2)
  224. public static bool operator ==(TSVector value1, TSVector value2)
  225. {
  226. return (((value1.x == value2.x) && (value1.y == value2.y)) && (value1.z == value2.z));
  227. }
  228. #endregion
  229. /// <summary>
  230. /// Tests if two JVector are not equal.
  231. /// </summary>
  232. /// <param name="value1">The first value.</param>
  233. /// <param name="value2">The second value.</param>
  234. /// <returns>Returns false if both values are equal, otherwise true.</returns>
  235. #region public static bool operator !=(JVector value1, JVector value2)
  236. public static bool operator !=(TSVector value1, TSVector value2)
  237. {
  238. if ((value1.x == value2.x) && (value1.y == value2.y))
  239. {
  240. return (value1.z != value2.z);
  241. }
  242. return true;
  243. }
  244. #endregion
  245. /// <summary>
  246. /// Gets a vector with the minimum x,y and z values of both vectors.
  247. /// </summary>
  248. /// <param name="value1">The first value.</param>
  249. /// <param name="value2">The second value.</param>
  250. /// <returns>A vector with the minimum x,y and z values of both vectors.</returns>
  251. #region public static JVector Min(JVector value1, JVector value2)
  252. public static TSVector Min(TSVector value1, TSVector value2)
  253. {
  254. TSVector result;
  255. TSVector.Min(ref value1, ref value2, out result);
  256. return result;
  257. }
  258. /// <summary>
  259. /// Gets a vector with the minimum x,y and z values of both vectors.
  260. /// </summary>
  261. /// <param name="value1">The first value.</param>
  262. /// <param name="value2">The second value.</param>
  263. /// <param name="result">A vector with the minimum x,y and z values of both vectors.</param>
  264. public static void Min(ref TSVector value1, ref TSVector value2, out TSVector result)
  265. {
  266. result.x = (value1.x < value2.x) ? value1.x : value2.x;
  267. result.y = (value1.y < value2.y) ? value1.y : value2.y;
  268. result.z = (value1.z < value2.z) ? value1.z : value2.z;
  269. }
  270. #endregion
  271. /// <summary>
  272. /// Gets a vector with the maximum x,y and z values of both vectors.
  273. /// </summary>
  274. /// <param name="value1">The first value.</param>
  275. /// <param name="value2">The second value.</param>
  276. /// <returns>A vector with the maximum x,y and z values of both vectors.</returns>
  277. #region public static JVector Max(JVector value1, JVector value2)
  278. public static TSVector Max(TSVector value1, TSVector value2)
  279. {
  280. TSVector result;
  281. TSVector.Max(ref value1, ref value2, out result);
  282. return result;
  283. }
  284. public static FP Distance(TSVector v1, TSVector v2) {
  285. 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));
  286. }
  287. /// <summary>
  288. /// Gets a vector with the maximum x,y and z values of both vectors.
  289. /// </summary>
  290. /// <param name="value1">The first value.</param>
  291. /// <param name="value2">The second value.</param>
  292. /// <param name="result">A vector with the maximum x,y and z values of both vectors.</param>
  293. public static void Max(ref TSVector value1, ref TSVector value2, out TSVector result)
  294. {
  295. result.x = (value1.x > value2.x) ? value1.x : value2.x;
  296. result.y = (value1.y > value2.y) ? value1.y : value2.y;
  297. result.z = (value1.z > value2.z) ? value1.z : value2.z;
  298. }
  299. #endregion
  300. /// <summary>
  301. /// Sets the length of the vector to zero.
  302. /// </summary>
  303. #region public void MakeZero()
  304. public void MakeZero()
  305. {
  306. x = FP.Zero;
  307. y = FP.Zero;
  308. z = FP.Zero;
  309. }
  310. #endregion
  311. /// <summary>
  312. /// Checks if the length of the vector is zero.
  313. /// </summary>
  314. /// <returns>Returns true if the vector is zero, otherwise false.</returns>
  315. #region public bool IsZero()
  316. public bool IsZero()
  317. {
  318. return (this.sqrMagnitude == FP.Zero);
  319. }
  320. /// <summary>
  321. /// Checks if the length of the vector is nearly zero.
  322. /// </summary>
  323. /// <returns>Returns true if the vector is nearly zero, otherwise false.</returns>
  324. public bool IsNearlyZero()
  325. {
  326. return (this.sqrMagnitude < ZeroEpsilonSq);
  327. }
  328. #endregion
  329. /// <summary>
  330. /// Transforms a vector by the given matrix.
  331. /// </summary>
  332. /// <param name="position">The vector to transform.</param>
  333. /// <param name="matrix">The transform matrix.</param>
  334. /// <returns>The transformed vector.</returns>
  335. #region public static JVector Transform(JVector position, JMatrix matrix)
  336. public static TSVector Transform(TSVector position, TSMatrix matrix)
  337. {
  338. TSVector result;
  339. TSVector.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="position">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 position, ref TSMatrix matrix, out TSVector result)
  349. {
  350. FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M21)) + (position.z * matrix.M31);
  351. FP num1 = ((position.x * matrix.M12) + (position.y * matrix.M22)) + (position.z * matrix.M32);
  352. FP num2 = ((position.x * matrix.M13) + (position.y * matrix.M23)) + (position.z * matrix.M33);
  353. result.x = num0;
  354. result.y = num1;
  355. result.z = num2;
  356. }
  357. /// <summary>
  358. /// Transforms a vector by the transposed of the given Matrix.
  359. /// </summary>
  360. /// <param name="position">The vector to transform.</param>
  361. /// <param name="matrix">The transform matrix.</param>
  362. /// <param name="result">The transformed vector.</param>
  363. public static void TransposedTransform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
  364. {
  365. FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M12)) + (position.z * matrix.M13);
  366. FP num1 = ((position.x * matrix.M21) + (position.y * matrix.M22)) + (position.z * matrix.M23);
  367. FP num2 = ((position.x * matrix.M31) + (position.y * matrix.M32)) + (position.z * matrix.M33);
  368. result.x = num0;
  369. result.y = num1;
  370. result.z = num2;
  371. }
  372. #endregion
  373. /// <summary>
  374. /// Calculates the dot product of two vectors.
  375. /// </summary>
  376. /// <param name="vector1">The first vector.</param>
  377. /// <param name="vector2">The second vector.</param>
  378. /// <returns>Returns the dot product of both vectors.</returns>
  379. #region public static FP Dot(JVector vector1, JVector vector2)
  380. public static FP Dot(TSVector vector1, TSVector vector2)
  381. {
  382. return TSVector.Dot(ref vector1, ref vector2);
  383. }
  384. /// <summary>
  385. /// Calculates the dot product of both vectors.
  386. /// </summary>
  387. /// <param name="vector1">The first vector.</param>
  388. /// <param name="vector2">The second vector.</param>
  389. /// <returns>Returns the dot product of both vectors.</returns>
  390. public static FP Dot(ref TSVector vector1, ref TSVector vector2)
  391. {
  392. return ((vector1.x * vector2.x) + (vector1.y * vector2.y)) + (vector1.z * vector2.z);
  393. }
  394. #endregion
  395. // Projects a vector onto another vector.
  396. public static TSVector Project(TSVector vector, TSVector onNormal)
  397. {
  398. FP sqrtMag = Dot(onNormal, onNormal);
  399. if (sqrtMag < TSMath.Epsilon)
  400. return zero;
  401. else
  402. return onNormal * Dot(vector, onNormal) / sqrtMag;
  403. }
  404. // Projects a vector onto a plane defined by a normal orthogonal to the plane.
  405. public static TSVector ProjectOnPlane(TSVector vector, TSVector planeNormal)
  406. {
  407. return vector - Project(vector, planeNormal);
  408. }
  409. // Returns the angle in degrees between /from/ and /to/. This is always the smallest
  410. public static FP Angle(TSVector from, TSVector to)
  411. {
  412. return TSMath.Acos(TSMath.Clamp(Dot(from.normalized, to.normalized), -FP.ONE, FP.ONE)) * TSMath.Rad2Deg;
  413. }
  414. // The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
  415. // If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
  416. // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
  417. public static FP SignedAngle(TSVector from, TSVector to, TSVector axis)
  418. {
  419. TSVector fromNorm = from.normalized, toNorm = to.normalized;
  420. FP unsignedAngle = TSMath.Acos(TSMath.Clamp(Dot(fromNorm, toNorm), -FP.ONE, FP.ONE)) * TSMath.Rad2Deg;
  421. FP sign = TSMath.Sign(Dot(axis, Cross(fromNorm, toNorm)));
  422. return unsignedAngle * sign;
  423. }
  424. /// <summary>
  425. /// Adds two vectors.
  426. /// </summary>
  427. /// <param name="value1">The first vector.</param>
  428. /// <param name="value2">The second vector.</param>
  429. /// <returns>The sum of both vectors.</returns>
  430. #region public static void Add(JVector value1, JVector value2)
  431. public static TSVector Add(TSVector value1, TSVector value2)
  432. {
  433. TSVector result;
  434. TSVector.Add(ref value1, ref value2, out result);
  435. return result;
  436. }
  437. /// <summary>
  438. /// Adds to vectors.
  439. /// </summary>
  440. /// <param name="value1">The first vector.</param>
  441. /// <param name="value2">The second vector.</param>
  442. /// <param name="result">The sum of both vectors.</param>
  443. public static void Add(ref TSVector value1, ref TSVector value2, out TSVector result)
  444. {
  445. FP num0 = value1.x + value2.x;
  446. FP num1 = value1.y + value2.y;
  447. FP num2 = value1.z + value2.z;
  448. result.x = num0;
  449. result.y = num1;
  450. result.z = num2;
  451. }
  452. #endregion
  453. /// <summary>
  454. /// Divides a vector by a factor.
  455. /// </summary>
  456. /// <param name="value1">The vector to divide.</param>
  457. /// <param name="scaleFactor">The scale factor.</param>
  458. /// <returns>Returns the scaled vector.</returns>
  459. public static TSVector Divide(TSVector value1, FP scaleFactor) {
  460. TSVector result;
  461. TSVector.Divide(ref value1, scaleFactor, out result);
  462. return result;
  463. }
  464. /// <summary>
  465. /// Divides a vector by a factor.
  466. /// </summary>
  467. /// <param name="value1">The vector to divide.</param>
  468. /// <param name="scaleFactor">The scale factor.</param>
  469. /// <param name="result">Returns the scaled vector.</param>
  470. public static void Divide(ref TSVector value1, FP scaleFactor, out TSVector result) {
  471. result.x = value1.x / scaleFactor;
  472. result.y = value1.y / scaleFactor;
  473. result.z = value1.z / scaleFactor;
  474. }
  475. /// <summary>
  476. /// Subtracts two vectors.
  477. /// </summary>
  478. /// <param name="value1">The first vector.</param>
  479. /// <param name="value2">The second vector.</param>
  480. /// <returns>The difference of both vectors.</returns>
  481. #region public static JVector Subtract(JVector value1, JVector value2)
  482. public static TSVector Subtract(TSVector value1, TSVector value2)
  483. {
  484. TSVector result;
  485. TSVector.Subtract(ref value1, ref value2, out result);
  486. return result;
  487. }
  488. /// <summary>
  489. /// Subtracts to vectors.
  490. /// </summary>
  491. /// <param name="value1">The first vector.</param>
  492. /// <param name="value2">The second vector.</param>
  493. /// <param name="result">The difference of both vectors.</param>
  494. public static void Subtract(ref TSVector value1, ref TSVector value2, out TSVector result)
  495. {
  496. FP num0 = value1.x - value2.x;
  497. FP num1 = value1.y - value2.y;
  498. FP num2 = value1.z - value2.z;
  499. result.x = num0;
  500. result.y = num1;
  501. result.z = num2;
  502. }
  503. #endregion
  504. /// <summary>
  505. /// The cross product of two vectors.
  506. /// </summary>
  507. /// <param name="vector1">The first vector.</param>
  508. /// <param name="vector2">The second vector.</param>
  509. /// <returns>The cross product of both vectors.</returns>
  510. #region public static JVector Cross(JVector vector1, JVector vector2)
  511. public static TSVector Cross(TSVector vector1, TSVector vector2)
  512. {
  513. TSVector result;
  514. TSVector.Cross(ref vector1, ref vector2, out result);
  515. return result;
  516. }
  517. /// <summary>
  518. /// The cross product of two vectors.
  519. /// </summary>
  520. /// <param name="vector1">The first vector.</param>
  521. /// <param name="vector2">The second vector.</param>
  522. /// <param name="result">The cross product of both vectors.</param>
  523. public static void Cross(ref TSVector vector1, ref TSVector vector2, out TSVector result)
  524. {
  525. FP num3 = (vector1.y * vector2.z) - (vector1.z * vector2.y);
  526. FP num2 = (vector1.z * vector2.x) - (vector1.x * vector2.z);
  527. FP num = (vector1.x * vector2.y) - (vector1.y * vector2.x);
  528. result.x = num3;
  529. result.y = num2;
  530. result.z = num;
  531. }
  532. #endregion
  533. /// <summary>
  534. /// Gets the hashcode of the vector.
  535. /// </summary>
  536. /// <returns>Returns the hashcode of the vector.</returns>
  537. #region public override int GetHashCode()
  538. public override int GetHashCode()
  539. {
  540. return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
  541. }
  542. #endregion
  543. /// <summary>
  544. /// Inverses the direction of the vector.
  545. /// </summary>
  546. #region public static JVector Negate(JVector value)
  547. public void Negate()
  548. {
  549. this.x = -this.x;
  550. this.y = -this.y;
  551. this.z = -this.z;
  552. }
  553. /// <summary>
  554. /// Inverses the direction of a vector.
  555. /// </summary>
  556. /// <param name="value">The vector to inverse.</param>
  557. /// <returns>The negated vector.</returns>
  558. public static TSVector Negate(TSVector value)
  559. {
  560. TSVector result;
  561. TSVector.Negate(ref value,out result);
  562. return result;
  563. }
  564. /// <summary>
  565. /// Inverses the direction of a vector.
  566. /// </summary>
  567. /// <param name="value">The vector to inverse.</param>
  568. /// <param name="result">The negated vector.</param>
  569. public static void Negate(ref TSVector value, out TSVector result)
  570. {
  571. FP num0 = -value.x;
  572. FP num1 = -value.y;
  573. FP num2 = -value.z;
  574. result.x = num0;
  575. result.y = num1;
  576. result.z = num2;
  577. }
  578. #endregion
  579. /// <summary>
  580. /// Normalizes the given vector.
  581. /// </summary>
  582. /// <param name="value">The vector which should be normalized.</param>
  583. /// <returns>A normalized vector.</returns>
  584. #region public static JVector Normalize(JVector value)
  585. public static TSVector Normalize(TSVector value)
  586. {
  587. TSVector result;
  588. TSVector.Normalize(ref value, out result);
  589. return result;
  590. }
  591. /// <summary>
  592. /// Normalizes this vector.
  593. /// </summary>
  594. public void Normalize()
  595. {
  596. FP num2 = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
  597. FP num = FP.One / FP.Sqrt(num2);
  598. this.x *= num;
  599. this.y *= num;
  600. this.z *= num;
  601. }
  602. /// <summary>
  603. /// Normalizes the given vector.
  604. /// </summary>
  605. /// <param name="value">The vector which should be normalized.</param>
  606. /// <param name="result">A normalized vector.</param>
  607. public static void Normalize(ref TSVector value, out TSVector result)
  608. {
  609. FP num2 = ((value.x * value.x) + (value.y * value.y)) + (value.z * value.z);
  610. FP num = FP.One / FP.Sqrt(num2);
  611. result.x = value.x * num;
  612. result.y = value.y * num;
  613. result.z = value.z * num;
  614. }
  615. #endregion
  616. #region public static void Swap(ref JVector vector1, ref JVector vector2)
  617. /// <summary>
  618. /// Swaps the components of both vectors.
  619. /// </summary>
  620. /// <param name="vector1">The first vector to swap with the second.</param>
  621. /// <param name="vector2">The second vector to swap with the first.</param>
  622. public static void Swap(ref TSVector vector1, ref TSVector vector2)
  623. {
  624. FP temp;
  625. temp = vector1.x;
  626. vector1.x = vector2.x;
  627. vector2.x = temp;
  628. temp = vector1.y;
  629. vector1.y = vector2.y;
  630. vector2.y = temp;
  631. temp = vector1.z;
  632. vector1.z = vector2.z;
  633. vector2.z = temp;
  634. }
  635. #endregion
  636. /// <summary>
  637. /// Multiply a vector with a factor.
  638. /// </summary>
  639. /// <param name="value1">The vector to multiply.</param>
  640. /// <param name="scaleFactor">The scale factor.</param>
  641. /// <returns>Returns the multiplied vector.</returns>
  642. #region public static JVector Multiply(JVector value1, FP scaleFactor)
  643. public static TSVector Multiply(TSVector value1, FP scaleFactor)
  644. {
  645. TSVector result;
  646. TSVector.Multiply(ref value1, scaleFactor, out result);
  647. return result;
  648. }
  649. /// <summary>
  650. /// Multiply a vector with a factor.
  651. /// </summary>
  652. /// <param name="value1">The vector to multiply.</param>
  653. /// <param name="scaleFactor">The scale factor.</param>
  654. /// <param name="result">Returns the multiplied vector.</param>
  655. public static void Multiply(ref TSVector value1, FP scaleFactor, out TSVector result)
  656. {
  657. result.x = value1.x * scaleFactor;
  658. result.y = value1.y * scaleFactor;
  659. result.z = value1.z * scaleFactor;
  660. }
  661. #endregion
  662. /// <summary>
  663. /// Calculates the cross product of two vectors.
  664. /// </summary>
  665. /// <param name="value1">The first vector.</param>
  666. /// <param name="value2">The second vector.</param>
  667. /// <returns>Returns the cross product of both.</returns>
  668. #region public static JVector operator %(JVector value1, JVector value2)
  669. public static TSVector operator %(TSVector value1, TSVector value2)
  670. {
  671. TSVector result; TSVector.Cross(ref value1, ref value2, out result);
  672. return result;
  673. }
  674. #endregion
  675. /// <summary>
  676. /// Calculates the dot product of two vectors.
  677. /// </summary>
  678. /// <param name="value1">The first vector.</param>
  679. /// <param name="value2">The second vector.</param>
  680. /// <returns>Returns the dot product of both.</returns>
  681. #region public static FP operator *(JVector value1, JVector value2)
  682. public static FP operator *(TSVector value1, TSVector value2)
  683. {
  684. return TSVector.Dot(ref value1, ref value2);
  685. }
  686. #endregion
  687. /// <summary>
  688. /// Multiplies a vector by a scale factor.
  689. /// </summary>
  690. /// <param name="value1">The vector to scale.</param>
  691. /// <param name="value2">The scale factor.</param>
  692. /// <returns>Returns the scaled vector.</returns>
  693. #region public static JVector operator *(JVector value1, FP value2)
  694. public static TSVector operator *(TSVector value1, FP value2)
  695. {
  696. TSVector result;
  697. TSVector.Multiply(ref value1, value2,out result);
  698. return result;
  699. }
  700. #endregion
  701. /// <summary>
  702. /// Multiplies a vector by a scale factor.
  703. /// </summary>
  704. /// <param name="value2">The vector to scale.</param>
  705. /// <param name="value1">The scale factor.</param>
  706. /// <returns>Returns the scaled vector.</returns>
  707. #region public static JVector operator *(FP value1, JVector value2)
  708. public static TSVector operator *(FP value1, TSVector value2)
  709. {
  710. TSVector result;
  711. TSVector.Multiply(ref value2, value1, out result);
  712. return result;
  713. }
  714. #endregion
  715. /// <summary>
  716. /// Subtracts two vectors.
  717. /// </summary>
  718. /// <param name="value1">The first vector.</param>
  719. /// <param name="value2">The second vector.</param>
  720. /// <returns>The difference of both vectors.</returns>
  721. #region public static JVector operator -(JVector value1, JVector value2)
  722. public static TSVector operator -(TSVector value1, TSVector value2)
  723. {
  724. TSVector result; TSVector.Subtract(ref value1, ref value2, out result);
  725. return result;
  726. }
  727. #endregion
  728. /// <summary>
  729. /// Adds two vectors.
  730. /// </summary>
  731. /// <param name="value1">The first vector.</param>
  732. /// <param name="value2">The second vector.</param>
  733. /// <returns>The sum of both vectors.</returns>
  734. #region public static JVector operator +(JVector value1, JVector value2)
  735. public static TSVector operator +(TSVector value1, TSVector value2)
  736. {
  737. TSVector result; TSVector.Add(ref value1, ref value2, out result);
  738. return result;
  739. }
  740. #endregion
  741. /// <summary>
  742. /// Divides a vector by a factor.
  743. /// </summary>
  744. /// <param name="value1">The vector to divide.</param>
  745. /// <param name="scaleFactor">The scale factor.</param>
  746. /// <returns>Returns the scaled vector.</returns>
  747. public static TSVector operator /(TSVector value1, FP value2) {
  748. TSVector result;
  749. TSVector.Divide(ref value1, value2, out result);
  750. return result;
  751. }
  752. public TSVector2 ToTSVector2() {
  753. return new TSVector2(this.x, this.y);
  754. }
  755. public TSVector4 ToTSVector4()
  756. {
  757. return new TSVector4(this.x, this.y, this.z, FP.One);
  758. }
  759. }
  760. }