Vector4.cs 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. #region --- License ---
  2. /*
  3. Copyright (c) 2006 - 2008 The Open Toolkit library.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #endregion --- License ---
  21. using System;
  22. using System.Linq;
  23. using System.Runtime.InteropServices;
  24. namespace MatterHackers.VectorMath
  25. {
  26. /// <summary>Represents a 4D vector using four double-precision floating-point numbers.</summary>
  27. [Serializable]
  28. [StructLayout(LayoutKind.Sequential)]
  29. public struct Vector4 : IEquatable<Vector4>
  30. {
  31. #region Fields
  32. /// <summary>
  33. /// The X component of the Vector4d.
  34. /// </summary>
  35. public double X;
  36. /// <summary>
  37. /// The Y component of the Vector4d.
  38. /// </summary>
  39. public double Y;
  40. /// <summary>
  41. /// The Z component of the Vector4d.
  42. /// </summary>
  43. public double Z;
  44. /// <summary>
  45. /// The W component of the Vector4d.
  46. /// </summary>
  47. public double W;
  48. /// <summary>
  49. /// Defines a unit-length Vector4d that points towards the X-axis.
  50. /// </summary>
  51. public static Vector4 UnitX = new Vector4(1, 0, 0, 0);
  52. /// <summary>
  53. /// Defines a unit-length Vector4d that points towards the Y-axis.
  54. /// </summary>
  55. public static Vector4 UnitY = new Vector4(0, 1, 0, 0);
  56. /// <summary>
  57. /// Defines a unit-length Vector4d that points towards the Z-axis.
  58. /// </summary>
  59. public static Vector4 UnitZ = new Vector4(0, 0, 1, 0);
  60. /// <summary>
  61. /// Defines a unit-length Vector4d that points towards the W-axis.
  62. /// </summary>
  63. public static Vector4 UnitW = new Vector4(0, 0, 0, 1);
  64. /// <summary>
  65. /// Defines a zero-length Vector4d.
  66. /// </summary>
  67. public static Vector4 Zero = new Vector4(0, 0, 0, 0);
  68. /// <summary>
  69. /// Defines an instance with all components set to 1.
  70. /// </summary>
  71. public static readonly Vector4 One = new Vector4(1, 1, 1, 1);
  72. /// <summary>
  73. /// Defines the size of the Vector4d struct in bytes.
  74. /// </summary>
  75. public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4());
  76. #endregion Fields
  77. #region Constructors
  78. /// <summary>
  79. /// Constructs a new Vector4d.
  80. /// </summary>
  81. /// <param name="x">The x component of the Vector4d.</param>
  82. /// <param name="y">The y component of the Vector4d.</param>
  83. /// <param name="z">The z component of the Vector4d.</param>
  84. /// <param name="w">The w component of the Vector4d.</param>
  85. public Vector4(double x, double y, double z, double w)
  86. {
  87. this.X = x;
  88. this.Y = y;
  89. this.Z = z;
  90. this.W = w;
  91. }
  92. /// <summary>
  93. /// Constructs a new Vector4d from the given Vector2d.
  94. /// </summary>
  95. /// <param name="v">The Vector2d to copy components from.</param>
  96. public Vector4(Vector2 v)
  97. {
  98. X = v.X;
  99. Y = v.Y;
  100. Z = 0.0f;
  101. W = 0.0f;
  102. }
  103. /// <summary>
  104. /// Constructs a new Vector4d from the given Vector3d.
  105. /// </summary>
  106. /// <param name="v">The Vector3d to copy components from.</param>
  107. public Vector4(Vector3 v)
  108. {
  109. X = v.X;
  110. Y = v.Y;
  111. Z = v.Z;
  112. W = 0.0f;
  113. }
  114. /// <summary>
  115. /// Constructs a new Vector4d from the specified Vector3d and w component.
  116. /// </summary>
  117. /// <param name="v">The Vector3d to copy components from.</param>
  118. /// <param name="w">The w component of the new Vector4.</param>
  119. public Vector4(Vector3 v, double w)
  120. {
  121. X = v.X;
  122. Y = v.Y;
  123. Z = v.Z;
  124. this.W = w;
  125. }
  126. /// <summary>
  127. /// Constructs a new Vector4d from the given Vector4d.
  128. /// </summary>
  129. /// <param name="v">The Vector4d to copy components from.</param>
  130. public Vector4(Vector4 v)
  131. {
  132. X = v.X;
  133. Y = v.Y;
  134. Z = v.Z;
  135. W = v.W;
  136. }
  137. #endregion Constructors
  138. public static Vector4 Parse(string s)
  139. {
  140. var result = Vector4.Zero;
  141. var values = s.Split(',').Select(sValue =>
  142. {
  143. double.TryParse(sValue, out double number);
  144. return number;
  145. }).ToArray();
  146. for (int i = 0; i < Math.Min(4, values.Length); i++)
  147. {
  148. result[i] = values[i];
  149. }
  150. return result;
  151. }
  152. #region Public Members
  153. #region Properties
  154. public double this[int index]
  155. {
  156. get
  157. {
  158. switch (index)
  159. {
  160. case 0:
  161. return X;
  162. case 1:
  163. return Y;
  164. case 2:
  165. return Z;
  166. case 3:
  167. return W;
  168. default:
  169. return 0;
  170. }
  171. }
  172. set
  173. {
  174. switch (index)
  175. {
  176. case 0:
  177. X = value;
  178. break;
  179. case 1:
  180. Y = value;
  181. break;
  182. case 2:
  183. Z = value;
  184. break;
  185. case 3:
  186. W = value;
  187. break;
  188. default:
  189. throw new Exception();
  190. }
  191. }
  192. }
  193. #endregion Properties
  194. #region Instance
  195. #region public double Length
  196. /// <summary>
  197. /// Gets the length (magnitude) of the vector.
  198. /// </summary>
  199. /// <see cref="LengthFast"/>
  200. /// <seealso cref="LengthSquared"/>
  201. public double Length
  202. {
  203. get
  204. {
  205. return System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
  206. }
  207. }
  208. #endregion public double Length
  209. #region public double LengthSquared
  210. /// <summary>
  211. /// Gets the square of the vector length (magnitude).
  212. /// </summary>
  213. /// <remarks>
  214. /// This property avoids the costly square root operation required by the Length property. This makes it more suitable
  215. /// for comparisons.
  216. /// </remarks>
  217. /// <see cref="Length"/>
  218. public double LengthSquared
  219. {
  220. get
  221. {
  222. return X * X + Y * Y + Z * Z + W * W;
  223. }
  224. }
  225. #endregion public double LengthSquared
  226. #region public void Normalize()
  227. /// <summary>
  228. /// Scales the Vector4d to unit length.
  229. /// </summary>
  230. public void Normalize()
  231. {
  232. double scale = 1.0 / this.Length;
  233. X *= scale;
  234. Y *= scale;
  235. Z *= scale;
  236. W *= scale;
  237. }
  238. #endregion public void Normalize()
  239. public bool IsValid()
  240. {
  241. if(double.IsNaN(X) || double.IsInfinity(X)
  242. || double.IsNaN(Y) || double.IsInfinity(Y)
  243. || double.IsNaN(Z) || double.IsInfinity(Z)
  244. || double.IsNaN(W) || double.IsInfinity(W))
  245. {
  246. return false;
  247. }
  248. return true;
  249. }
  250. #endregion Instance
  251. #region Static
  252. #region Add
  253. /// <summary>
  254. /// Adds two vectors.
  255. /// </summary>
  256. /// <param name="a">Left operand.</param>
  257. /// <param name="b">Right operand.</param>
  258. /// <returns>Result of operation.</returns>
  259. public static Vector4 Add(Vector4 a, Vector4 b)
  260. {
  261. Add(ref a, ref b, out a);
  262. return a;
  263. }
  264. /// <summary>
  265. /// Adds two vectors.
  266. /// </summary>
  267. /// <param name="a">Left operand.</param>
  268. /// <param name="b">Right operand.</param>
  269. /// <param name="result">Result of operation.</param>
  270. public static void Add(ref Vector4 a, ref Vector4 b, out Vector4 result)
  271. {
  272. result = new Vector4(a.X + b.X, a.Y + b.Y, a.Z + b.Z, a.W + b.W);
  273. }
  274. #endregion Add
  275. #region Subtract
  276. /// <summary>
  277. /// Subtract one Vector from another
  278. /// </summary>
  279. /// <param name="a">First operand</param>
  280. /// <param name="b">Second operand</param>
  281. /// <returns>Result of subtraction</returns>
  282. public static Vector4 Subtract(Vector4 a, Vector4 b)
  283. {
  284. Subtract(ref a, ref b, out a);
  285. return a;
  286. }
  287. /// <summary>
  288. /// Subtract one Vector from another
  289. /// </summary>
  290. /// <param name="a">First operand</param>
  291. /// <param name="b">Second operand</param>
  292. /// <param name="result">Result of subtraction</param>
  293. public static void Subtract(ref Vector4 a, ref Vector4 b, out Vector4 result)
  294. {
  295. result = new Vector4(a.X - b.X, a.Y - b.Y, a.Z - b.Z, a.W - b.W);
  296. }
  297. #endregion Subtract
  298. #region Multiply
  299. /// <summary>
  300. /// Multiplies a vector by a scalar.
  301. /// </summary>
  302. /// <param name="vector">Left operand.</param>
  303. /// <param name="scale">Right operand.</param>
  304. /// <returns>Result of the operation.</returns>
  305. public static Vector4 Multiply(Vector4 vector, double scale)
  306. {
  307. Multiply(ref vector, scale, out vector);
  308. return vector;
  309. }
  310. /// <summary>
  311. /// Multiplies a vector by a scalar.
  312. /// </summary>
  313. /// <param name="vector">Left operand.</param>
  314. /// <param name="scale">Right operand.</param>
  315. /// <param name="result">Result of the operation.</param>
  316. public static void Multiply(ref Vector4 vector, double scale, out Vector4 result)
  317. {
  318. result = new Vector4(vector.X * scale, vector.Y * scale, vector.Z * scale, vector.W * scale);
  319. }
  320. /// <summary>
  321. /// Multiplies a vector by the components a vector (scale).
  322. /// </summary>
  323. /// <param name="vector">Left operand.</param>
  324. /// <param name="scale">Right operand.</param>
  325. /// <returns>Result of the operation.</returns>
  326. public static Vector4 Multiply(Vector4 vector, Vector4 scale)
  327. {
  328. Multiply(ref vector, ref scale, out vector);
  329. return vector;
  330. }
  331. /// <summary>
  332. /// Multiplies a vector by the components of a vector (scale).
  333. /// </summary>
  334. /// <param name="vector">Left operand.</param>
  335. /// <param name="scale">Right operand.</param>
  336. /// <param name="result">Result of the operation.</param>
  337. public static void Multiply(ref Vector4 vector, ref Vector4 scale, out Vector4 result)
  338. {
  339. result = new Vector4(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z, vector.W * scale.W);
  340. }
  341. #endregion Multiply
  342. #region Divide
  343. /// <summary>
  344. /// Divides a vector by a scalar.
  345. /// </summary>
  346. /// <param name="vector">Left operand.</param>
  347. /// <param name="scale">Right operand.</param>
  348. /// <returns>Result of the operation.</returns>
  349. public static Vector4 Divide(Vector4 vector, double scale)
  350. {
  351. Divide(ref vector, scale, out vector);
  352. return vector;
  353. }
  354. /// <summary>
  355. /// Divides a vector by a scalar.
  356. /// </summary>
  357. /// <param name="vector">Left operand.</param>
  358. /// <param name="scale">Right operand.</param>
  359. /// <param name="result">Result of the operation.</param>
  360. public static void Divide(ref Vector4 vector, double scale, out Vector4 result)
  361. {
  362. Multiply(ref vector, 1 / scale, out result);
  363. }
  364. /// <summary>
  365. /// Divides a vector by the components of a vector (scale).
  366. /// </summary>
  367. /// <param name="vector">Left operand.</param>
  368. /// <param name="scale">Right operand.</param>
  369. /// <returns>Result of the operation.</returns>
  370. public static Vector4 Divide(Vector4 vector, Vector4 scale)
  371. {
  372. Divide(ref vector, ref scale, out vector);
  373. return vector;
  374. }
  375. /// <summary>
  376. /// Divide a vector by the components of a vector (scale).
  377. /// </summary>
  378. /// <param name="vector">Left operand.</param>
  379. /// <param name="scale">Right operand.</param>
  380. /// <param name="result">Result of the operation.</param>
  381. public static void Divide(ref Vector4 vector, ref Vector4 scale, out Vector4 result)
  382. {
  383. result = new Vector4(vector.X / scale.X, vector.Y / scale.Y, vector.Z / scale.Z, vector.W / scale.W);
  384. }
  385. #endregion Divide
  386. #region Min
  387. /// <summary>
  388. /// Calculate the component-wise minimum of two vectors
  389. /// </summary>
  390. /// <param name="a">First operand</param>
  391. /// <param name="b">Second operand</param>
  392. /// <returns>The component-wise minimum</returns>
  393. public static Vector4 Min(Vector4 a, Vector4 b)
  394. {
  395. a.X = a.X < b.X ? a.X : b.X;
  396. a.Y = a.Y < b.Y ? a.Y : b.Y;
  397. a.Z = a.Z < b.Z ? a.Z : b.Z;
  398. a.W = a.W < b.W ? a.W : b.W;
  399. return a;
  400. }
  401. /// <summary>
  402. /// Calculate the component-wise minimum of two vectors
  403. /// </summary>
  404. /// <param name="a">First operand</param>
  405. /// <param name="b">Second operand</param>
  406. /// <param name="result">The component-wise minimum</param>
  407. public static void Min(ref Vector4 a, ref Vector4 b, out Vector4 result)
  408. {
  409. result.X = a.X < b.X ? a.X : b.X;
  410. result.Y = a.Y < b.Y ? a.Y : b.Y;
  411. result.Z = a.Z < b.Z ? a.Z : b.Z;
  412. result.W = a.W < b.W ? a.W : b.W;
  413. }
  414. #endregion Min
  415. #region Max
  416. /// <summary>
  417. /// Calculate the component-wise maximum of two vectors
  418. /// </summary>
  419. /// <param name="a">First operand</param>
  420. /// <param name="b">Second operand</param>
  421. /// <returns>The component-wise maximum</returns>
  422. public static Vector4 Max(Vector4 a, Vector4 b)
  423. {
  424. a.X = a.X > b.X ? a.X : b.X;
  425. a.Y = a.Y > b.Y ? a.Y : b.Y;
  426. a.Z = a.Z > b.Z ? a.Z : b.Z;
  427. a.W = a.W > b.W ? a.W : b.W;
  428. return a;
  429. }
  430. /// <summary>
  431. /// Calculate the component-wise maximum of two vectors
  432. /// </summary>
  433. /// <param name="a">First operand</param>
  434. /// <param name="b">Second operand</param>
  435. /// <param name="result">The component-wise maximum</param>
  436. public static void Max(ref Vector4 a, ref Vector4 b, out Vector4 result)
  437. {
  438. result.X = a.X > b.X ? a.X : b.X;
  439. result.Y = a.Y > b.Y ? a.Y : b.Y;
  440. result.Z = a.Z > b.Z ? a.Z : b.Z;
  441. result.W = a.W > b.W ? a.W : b.W;
  442. }
  443. #endregion Max
  444. #region Clamp
  445. /// <summary>
  446. /// Clamp a vector to the given minimum and maximum vectors
  447. /// </summary>
  448. /// <param name="vec">Input vector</param>
  449. /// <param name="min">Minimum vector</param>
  450. /// <param name="max">Maximum vector</param>
  451. /// <returns>The clamped vector</returns>
  452. public static Vector4 Clamp(Vector4 vec, Vector4 min, Vector4 max)
  453. {
  454. vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  455. vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  456. vec.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
  457. vec.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W;
  458. return vec;
  459. }
  460. /// <summary>
  461. /// Clamp a vector to the given minimum and maximum vectors
  462. /// </summary>
  463. /// <param name="vec">Input vector</param>
  464. /// <param name="min">Minimum vector</param>
  465. /// <param name="max">Maximum vector</param>
  466. /// <param name="result">The clamped vector</param>
  467. public static void Clamp(ref Vector4 vec, ref Vector4 min, ref Vector4 max, out Vector4 result)
  468. {
  469. result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  470. result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  471. result.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
  472. result.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W;
  473. }
  474. #endregion Clamp
  475. #region Normalize
  476. /// <summary>
  477. /// Scale a vector to unit length
  478. /// </summary>
  479. /// <param name="vec">The input vector</param>
  480. /// <returns>The normalized vector</returns>
  481. public static Vector4 Normalize(Vector4 vec)
  482. {
  483. double scale = 1.0 / vec.Length;
  484. vec.X *= scale;
  485. vec.Y *= scale;
  486. vec.Z *= scale;
  487. vec.W *= scale;
  488. return vec;
  489. }
  490. /// <summary>
  491. /// Scale a vector to unit length
  492. /// </summary>
  493. /// <param name="vec">The input vector</param>
  494. /// <param name="result">The normalized vector</param>
  495. public static void Normalize(ref Vector4 vec, out Vector4 result)
  496. {
  497. double scale = 1.0 / vec.Length;
  498. result.X = vec.X * scale;
  499. result.Y = vec.Y * scale;
  500. result.Z = vec.Z * scale;
  501. result.W = vec.W * scale;
  502. }
  503. #endregion Normalize
  504. #region Dot
  505. /// <summary>
  506. /// Calculate the dot product of two vectors
  507. /// </summary>
  508. /// <param name="left">First operand</param>
  509. /// <param name="right">Second operand</param>
  510. /// <returns>The dot product of the two inputs</returns>
  511. public static double Dot(Vector4 left, Vector4 right)
  512. {
  513. return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
  514. }
  515. /// <summary>
  516. /// Calculate the dot product of two vectors
  517. /// </summary>
  518. /// <param name="left">First operand</param>
  519. /// <param name="right">Second operand</param>
  520. /// <param name="result">The dot product of the two inputs</param>
  521. public static void Dot(ref Vector4 left, ref Vector4 right, out double result)
  522. {
  523. result = left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
  524. }
  525. #endregion Dot
  526. #region Lerp
  527. /// <summary>
  528. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  529. /// </summary>
  530. /// <param name="a">First input vector</param>
  531. /// <param name="b">Second input vector</param>
  532. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  533. /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
  534. public static Vector4 Lerp(Vector4 a, Vector4 b, double blend)
  535. {
  536. a.X = blend * (b.X - a.X) + a.X;
  537. a.Y = blend * (b.Y - a.Y) + a.Y;
  538. a.Z = blend * (b.Z - a.Z) + a.Z;
  539. a.W = blend * (b.W - a.W) + a.W;
  540. return a;
  541. }
  542. /// <summary>
  543. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  544. /// </summary>
  545. /// <param name="a">First input vector</param>
  546. /// <param name="b">Second input vector</param>
  547. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  548. /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param>
  549. public static void Lerp(ref Vector4 a, ref Vector4 b, double blend, out Vector4 result)
  550. {
  551. result.X = blend * (b.X - a.X) + a.X;
  552. result.Y = blend * (b.Y - a.Y) + a.Y;
  553. result.Z = blend * (b.Z - a.Z) + a.Z;
  554. result.W = blend * (b.W - a.W) + a.W;
  555. }
  556. #endregion Lerp
  557. #region Barycentric
  558. /// <summary>
  559. /// Interpolate 3 Vectors using Barycentric coordinates
  560. /// </summary>
  561. /// <param name="a">First input Vector</param>
  562. /// <param name="b">Second input Vector</param>
  563. /// <param name="c">Third input Vector</param>
  564. /// <param name="u">First Barycentric Coordinate</param>
  565. /// <param name="v">Second Barycentric Coordinate</param>
  566. /// <returns>a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</returns>
  567. public static Vector4 BaryCentric(Vector4 a, Vector4 b, Vector4 c, double u, double v)
  568. {
  569. return a + u * (b - a) + v * (c - a);
  570. }
  571. /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary>
  572. /// <param name="a">First input Vector.</param>
  573. /// <param name="b">Second input Vector.</param>
  574. /// <param name="c">Third input Vector.</param>
  575. /// <param name="u">First Barycentric Coordinate.</param>
  576. /// <param name="v">Second Barycentric Coordinate.</param>
  577. /// <param name="result">Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</param>
  578. public static void BaryCentric(ref Vector4 a, ref Vector4 b, ref Vector4 c, double u, double v, out Vector4 result)
  579. {
  580. result = a; // copy
  581. Vector4 temp = b; // copy
  582. Subtract(ref temp, ref a, out temp);
  583. Multiply(ref temp, u, out temp);
  584. Add(ref result, ref temp, out result);
  585. temp = c; // copy
  586. Subtract(ref temp, ref a, out temp);
  587. Multiply(ref temp, v, out temp);
  588. Add(ref result, ref temp, out result);
  589. }
  590. #endregion Barycentric
  591. #region Transform
  592. /// <summary>Transform a Vector by the given Matrix</summary>
  593. /// <param name="vec">The vector to transform</param>
  594. /// <param name="mat">The desired transformation</param>
  595. /// <returns>The transformed vector</returns>
  596. public static Vector4 Transform(Vector4 vec, Matrix4X4 mat)
  597. {
  598. Vector4 result;
  599. Transform(vec, ref mat, out result);
  600. return result;
  601. }
  602. /// <summary>Transform a Vector by the given Matrix</summary>
  603. /// <param name="vec">The vector to transform</param>
  604. /// <param name="mat">The desired transformation</param>
  605. /// <param name="result">The transformed vector</param>
  606. public static void Transform(Vector4 vec, ref Matrix4X4 mat, out Vector4 result)
  607. {
  608. result = new Vector4(
  609. vec.X * mat.Row0.X + vec.Y * mat.Row1.X + vec.Z * mat.Row2.X + vec.W * mat.Row3.X,
  610. vec.X * mat.Row0.Y + vec.Y * mat.Row1.Y + vec.Z * mat.Row2.Y + vec.W * mat.Row3.Y,
  611. vec.X * mat.Row0.Z + vec.Y * mat.Row1.Z + vec.Z * mat.Row2.Z + vec.W * mat.Row3.Z,
  612. vec.X * mat.Row0.W + vec.Y * mat.Row1.W + vec.Z * mat.Row2.W + vec.W * mat.Row3.W);
  613. }
  614. /// <summary>
  615. /// Transforms a vector by a quaternion rotation.
  616. /// </summary>
  617. /// <param name="vec">The vector to transform.</param>
  618. /// <param name="quat">The quaternion to rotate the vector by.</param>
  619. /// <returns>The result of the operation.</returns>
  620. public static Vector4 Transform(Vector4 vec, Quaternion quat)
  621. {
  622. Vector4 result;
  623. Transform(ref vec, ref quat, out result);
  624. return result;
  625. }
  626. /// <summary>
  627. /// Transforms a vector by a quaternion rotation.
  628. /// </summary>
  629. /// <param name="vec">The vector to transform.</param>
  630. /// <param name="quat">The quaternion to rotate the vector by.</param>
  631. /// <param name="result">The result of the operation.</param>
  632. public static void Transform(ref Vector4 vec, ref Quaternion quat, out Vector4 result)
  633. {
  634. Quaternion v = new Quaternion(vec.X, vec.Y, vec.Z, vec.W), i, t;
  635. Quaternion.Invert(ref quat, out i);
  636. Quaternion.Multiply(ref quat, ref v, out t);
  637. Quaternion.Multiply(ref t, ref i, out v);
  638. result = new Vector4(v.X, v.Y, v.Z, v.W);
  639. }
  640. #endregion Transform
  641. #endregion Static
  642. #region Swizzle
  643. /// <summary>
  644. /// Gets or sets an OpenTK.Vector2d with the X and Y components of this instance.
  645. /// </summary>
  646. public Vector2 Xy { get { return new Vector2(X, Y); } set { X = value.X; Y = value.Y; } }
  647. /// <summary>
  648. /// Gets or sets an OpenTK.Vector3d with the X, Y and Z components of this instance.
  649. /// </summary>
  650. public Vector3 Xyz { get { return new Vector3(X, Y, Z); } set { X = value.X; Y = value.Y; Z = value.Z; } }
  651. #endregion Swizzle
  652. #region Operators
  653. /// <summary>
  654. /// Adds two instances.
  655. /// </summary>
  656. /// <param name="left">The first instance.</param>
  657. /// <param name="right">The second instance.</param>
  658. /// <returns>The result of the calculation.</returns>
  659. public static Vector4 operator +(Vector4 left, Vector4 right)
  660. {
  661. left.X += right.X;
  662. left.Y += right.Y;
  663. left.Z += right.Z;
  664. left.W += right.W;
  665. return left;
  666. }
  667. /// <summary>
  668. /// Subtracts two instances.
  669. /// </summary>
  670. /// <param name="left">The first instance.</param>
  671. /// <param name="right">The second instance.</param>
  672. /// <returns>The result of the calculation.</returns>
  673. public static Vector4 operator -(Vector4 left, Vector4 right)
  674. {
  675. left.X -= right.X;
  676. left.Y -= right.Y;
  677. left.Z -= right.Z;
  678. left.W -= right.W;
  679. return left;
  680. }
  681. /// <summary>
  682. /// Negates an instance.
  683. /// </summary>
  684. /// <param name="vec">The instance.</param>
  685. /// <returns>The result of the calculation.</returns>
  686. public static Vector4 operator -(Vector4 vec)
  687. {
  688. vec.X = -vec.X;
  689. vec.Y = -vec.Y;
  690. vec.Z = -vec.Z;
  691. vec.W = -vec.W;
  692. return vec;
  693. }
  694. /// <summary>
  695. /// Multiplies an instance by a scalar.
  696. /// </summary>
  697. /// <param name="vec">The instance.</param>
  698. /// <param name="scale">The scalar.</param>
  699. /// <returns>The result of the calculation.</returns>
  700. public static Vector4 operator *(Vector4 vec, double scale)
  701. {
  702. vec.X *= scale;
  703. vec.Y *= scale;
  704. vec.Z *= scale;
  705. vec.W *= scale;
  706. return vec;
  707. }
  708. /// <summary>
  709. /// Multiplies an instance by a scalar.
  710. /// </summary>
  711. /// <param name="scale">The scalar.</param>
  712. /// <param name="vec">The instance.</param>
  713. /// <returns>The result of the calculation.</returns>
  714. public static Vector4 operator *(double scale, Vector4 vec)
  715. {
  716. vec.X *= scale;
  717. vec.Y *= scale;
  718. vec.Z *= scale;
  719. vec.W *= scale;
  720. return vec;
  721. }
  722. /// <summary>
  723. /// Divides an instance by a scalar.
  724. /// </summary>
  725. /// <param name="vec">The instance.</param>
  726. /// <param name="scale">The scalar.</param>
  727. /// <returns>The result of the calculation.</returns>
  728. public static Vector4 operator /(Vector4 vec, double scale)
  729. {
  730. double mult = 1 / scale;
  731. vec.X *= mult;
  732. vec.Y *= mult;
  733. vec.Z *= mult;
  734. vec.W *= mult;
  735. return vec;
  736. }
  737. /// <summary>
  738. /// Compares two instances for equality.
  739. /// </summary>
  740. /// <param name="left">The first instance.</param>
  741. /// <param name="right">The second instance.</param>
  742. /// <returns>True, if left equals right; false otherwise.</returns>
  743. public static bool operator ==(Vector4 left, Vector4 right)
  744. {
  745. return left.Equals(right);
  746. }
  747. /// <summary>
  748. /// Compares two instances for inequality.
  749. /// </summary>
  750. /// <param name="left">The first instance.</param>
  751. /// <param name="right">The second instance.</param>
  752. /// <returns>True, if left does not equa lright; false otherwise.</returns>
  753. public static bool operator !=(Vector4 left, Vector4 right)
  754. {
  755. return !left.Equals(right);
  756. }
  757. #endregion Operators
  758. #region Overrides
  759. #region public override string ToString()
  760. /// <summary>
  761. /// Returns a System.String that represents the current Vector4d.
  762. /// </summary>
  763. /// <returns></returns>
  764. public override string ToString()
  765. {
  766. return String.Format("{0}, {1}, {2}, {3}", X, Y, Z, W);
  767. }
  768. /// <summary>
  769. /// Returns a System.String that represents the current Vector4d, formatting each element with format.
  770. /// </summary>
  771. /// <param name="format"></param>
  772. /// <returns></returns>
  773. public string ToString(string format = "")
  774. {
  775. return X.ToString(format) + ", " + Y.ToString(format) + ", " + Z.ToString(format) + ", " + W.ToString(format);
  776. }
  777. #endregion public override string ToString()
  778. #region public override int GetHashCode()
  779. /// <summary>
  780. /// Returns the hashcode for this instance.
  781. /// </summary>
  782. /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
  783. public override int GetHashCode()
  784. {
  785. return new { X, Y, Z, W }.GetHashCode();
  786. }
  787. public static ulong GetLongHashCode(double data, ulong hash = 14695981039346656037)
  788. {
  789. return ComputeHash(BitConverter.GetBytes(data), hash);
  790. }
  791. // FNV-1a (64-bit) non-cryptographic hash function.
  792. // Adapted from: http://github.com/jakedouglas/fnv-java
  793. public static ulong ComputeHash(byte[] bytes, ulong hash = 14695981039346656037)
  794. {
  795. const ulong fnv64Prime = 0x100000001b3;
  796. for (var i = 0; i < bytes.Length; i++)
  797. {
  798. hash = hash ^ bytes[i];
  799. hash *= fnv64Prime;
  800. }
  801. return hash;
  802. }
  803. /// <summary>
  804. /// return a 64 bit hash code proposed by Jon Skeet
  805. // http://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order
  806. /// </summary>
  807. /// <returns></returns>
  808. public ulong GetLongHashCode(ulong hash = 14695981039346656037)
  809. {
  810. hash = GetLongHashCode(X, hash);
  811. hash = GetLongHashCode(Y, hash);
  812. hash = GetLongHashCode(Z, hash);
  813. hash = GetLongHashCode(W, hash);
  814. return hash;
  815. }
  816. #endregion public override int GetHashCode()
  817. #region public override bool Equals(object obj)
  818. /// <summary>
  819. /// Indicates whether this instance and a specified object are equal.
  820. /// </summary>
  821. /// <param name="obj">The object to compare to.</param>
  822. /// <returns>True if the instances are equal; false otherwise.</returns>
  823. public override bool Equals(object obj)
  824. {
  825. if (!(obj is Vector4))
  826. return false;
  827. return this.Equals((Vector4)obj);
  828. }
  829. /// <summary>
  830. /// Indicates whether this instance and a specified object are equal within an error range.
  831. /// </summary>
  832. /// <param name="OtherVector"></param>
  833. /// <param name="ErrorValue"></param>
  834. /// <returns>True if the instances are equal; false otherwise.</returns>
  835. public bool Equals(Vector4 OtherVector, double ErrorValue)
  836. {
  837. if ((X < OtherVector.X + ErrorValue && X > OtherVector.X - ErrorValue) &&
  838. (Y < OtherVector.Y + ErrorValue && Y > OtherVector.Y - ErrorValue) &&
  839. (Z < OtherVector.Z + ErrorValue && Z > OtherVector.Z - ErrorValue) &&
  840. (W < OtherVector.W + ErrorValue && W > OtherVector.W - ErrorValue))
  841. {
  842. return true;
  843. }
  844. return false;
  845. }
  846. #endregion public override bool Equals(object obj)
  847. #endregion Overrides
  848. #endregion Public Members
  849. #region IEquatable<Vector4d> Members
  850. /// <summary>Indicates whether the current vector is equal to another vector.</summary>
  851. /// <param name="other">A vector to compare with this vector.</param>
  852. /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
  853. public bool Equals(Vector4 other)
  854. {
  855. return
  856. X == other.X &&
  857. Y == other.Y &&
  858. Z == other.Z &&
  859. W == other.W;
  860. }
  861. #endregion IEquatable<Vector4d> Members
  862. }
  863. }