Vector3Float.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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.Collections.Generic;
  23. using System.Runtime.InteropServices;
  24. namespace MatterHackers.VectorMath
  25. {
  26. /// <summary>
  27. /// Represents a 3D vector using three float-precision floating-point numbers.
  28. /// </summary>
  29. [Serializable]
  30. [StructLayout(LayoutKind.Sequential)]
  31. public struct Vector3Float : IEquatable<Vector3Float>
  32. {
  33. #region Fields
  34. /// <summary>
  35. /// The X component of the Vector3Float.
  36. /// </summary>
  37. public float X;
  38. /// <summary>
  39. /// The Y component of the Vector3Float.
  40. /// </summary>
  41. public float Y;
  42. /// <summary>
  43. /// The Z component of the Vector3Float.
  44. /// </summary>
  45. public float Z;
  46. #endregion Fields
  47. #region Constants
  48. /// <summary>
  49. /// Defines a unit-length Vector3Floatd that points towards the X-axis.
  50. /// </summary>
  51. public static readonly Vector3Float UnitX = new Vector3Float(1, 0, 0);
  52. /// <summary>
  53. /// Defines a unit-length Vector3Floatd that points towards the Y-axis.
  54. /// </summary>
  55. public static readonly Vector3Float UnitY = new Vector3Float(0, 1, 0);
  56. /// <summary>
  57. /// /// Defines a unit-length Vector3Floatd that points towards the Z-axis.
  58. /// </summary>
  59. public static readonly Vector3Float UnitZ = new Vector3Float(0, 0, 1);
  60. /// <summary>
  61. /// Defines a zero-length Vector3Float.
  62. /// </summary>
  63. public static readonly Vector3Float Zero = new Vector3Float(0, 0, 0);
  64. /// <summary>
  65. /// Defines an instance with all components set to 1.
  66. /// </summary>
  67. public static readonly Vector3Float One = new Vector3Float(1, 1, 1);
  68. /// <summary>
  69. /// Defines an instance with all components set to positive infinity.
  70. /// </summary>
  71. public static readonly Vector3Float PositiveInfinity = new Vector3Float(float.PositiveInfinity, float.PositiveInfinity, float.PositiveInfinity);
  72. /// <summary>
  73. /// Defines an instance with all components set to negative infinity.
  74. /// </summary>
  75. public static readonly Vector3Float NegativeInfinity = new Vector3Float(float.NegativeInfinity, float.NegativeInfinity, float.NegativeInfinity);
  76. /// <summary>
  77. /// Defines the size of the Vector3Floatd struct in bytes.
  78. /// </summary>
  79. public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3Float());
  80. #endregion Constants
  81. #region Constructors
  82. /// <summary>
  83. /// Constructs a new Vector3Float.
  84. /// </summary>
  85. /// <param name="x">The x component of the Vector3Float.</param>
  86. /// <param name="y">The y component of the Vector3Float.</param>
  87. /// <param name="z">The z component of the Vector3Float.</param>
  88. public Vector3Float(double x, double y, double z)
  89. : this((float)x, (float)y, (float)z)
  90. {
  91. }
  92. public Vector3Float(float x, float y, float z)
  93. {
  94. this.X = x;
  95. this.Y = y;
  96. this.Z = z;
  97. }
  98. /// <summary>
  99. /// Constructs a new instance from the given Vector2d.
  100. /// </summary>
  101. /// <param name="v">The Vector2d to copy components from.</param>
  102. public Vector3Float(Vector2 v, double z = 0)
  103. {
  104. X = (float)v.X;
  105. Y = (float)v.Y;
  106. this.Z = (float)z;
  107. }
  108. /// <summary>
  109. /// Constructs a new instance from the given Vector3Floatd.
  110. /// </summary>
  111. /// <param name="v">The Vector3Floatd to copy components from.</param>
  112. public Vector3Float(Vector3Float v)
  113. {
  114. X = v.X;
  115. Y = v.Y;
  116. Z = v.Z;
  117. }
  118. public Vector3Float(float[] floatArray)
  119. {
  120. X = floatArray[0];
  121. Y = floatArray[1];
  122. Z = floatArray[2];
  123. }
  124. /// <summary>
  125. /// Constructs a new instance from the given Vector4d.
  126. /// </summary>
  127. /// <param name="v">The Vector4d to copy components from.</param>
  128. public Vector3Float(Vector4 v)
  129. {
  130. X = (float)v.X;
  131. Y = (float)v.Y;
  132. Z = (float)v.Z;
  133. }
  134. public Vector3Float(Vector3 position)
  135. {
  136. this.X = (float)position.X;
  137. this.Y = (float)position.Y;
  138. this.Z = (float)position.Z;
  139. }
  140. #endregion Constructors
  141. #region Properties
  142. public float this[int index]
  143. {
  144. get
  145. {
  146. switch (index)
  147. {
  148. case 0:
  149. return X;
  150. case 1:
  151. return Y;
  152. case 2:
  153. return Z;
  154. default:
  155. return 0;
  156. }
  157. }
  158. set
  159. {
  160. switch (index)
  161. {
  162. case 0:
  163. X = value;
  164. break;
  165. case 1:
  166. Y = value;
  167. break;
  168. case 2:
  169. Z = value;
  170. break;
  171. default:
  172. throw new Exception();
  173. }
  174. }
  175. }
  176. #endregion Properties
  177. public Vector3 AsVector3()
  178. {
  179. return new Vector3(this);
  180. }
  181. #region Public Members
  182. #region Instance
  183. /// <summary>
  184. /// Gets the length (magnitude) of the vector.
  185. /// </summary>
  186. /// <see cref="LengthFast"/>
  187. /// <seealso cref="LengthSquared"/>
  188. public float Length
  189. {
  190. get => (float)Math.Sqrt(X * X + Y * Y + Z * Z);
  191. }
  192. /// <summary>
  193. /// Gets the square of the vector length (magnitude).
  194. /// </summary>
  195. /// <remarks>
  196. /// This property avoids the costly square root operation required by the Length property. This makes it more suitable
  197. /// for comparisons.
  198. /// </remarks>
  199. /// <see cref="Length"/>
  200. /// <seealso cref="LengthFast"/>
  201. public float LengthSquared
  202. {
  203. get => X * X + Y * Y + Z * Z;
  204. }
  205. #region public void Normalize()
  206. /// <summary>
  207. /// Returns a normalized Vector of this.
  208. /// </summary>
  209. /// <returns></returns>
  210. public Vector3Float GetNormal()
  211. {
  212. Vector3Float temp = this;
  213. temp.Normalize();
  214. return temp;
  215. }
  216. /// <summary>
  217. /// Scales the Vector3Floatd to unit length.
  218. /// </summary>
  219. public void Normalize()
  220. {
  221. float scale = 1.0f / this.Length;
  222. X *= scale;
  223. Y *= scale;
  224. Z *= scale;
  225. }
  226. #endregion public void Normalize()
  227. #region public float[] ToArray()
  228. public float[] ToArray()
  229. {
  230. return new float[] { X, Y, Z };
  231. }
  232. #endregion public float[] ToArray()
  233. #endregion Instance
  234. #region Swizzle
  235. /// <summary>
  236. /// Gets or sets an OpenTK.Vector2d with the X and Y components of this instance.
  237. /// </summary>
  238. public Vector2 Xy
  239. {
  240. get => new Vector2(X, Y);
  241. set
  242. {
  243. #if true
  244. throw new NotImplementedException();
  245. #else
  246. x = value.x; y = value.y;
  247. #endif
  248. }
  249. }
  250. #endregion Swizzle
  251. #region Operators
  252. /// <summary>
  253. /// Adds two instances.
  254. /// </summary>
  255. /// <param name="left">The first instance.</param>
  256. /// <param name="right">The second instance.</param>
  257. /// <returns>The result of the calculation.</returns>
  258. public static Vector3Float operator +(Vector3Float left, Vector3Float right)
  259. {
  260. left.X += right.X;
  261. left.Y += right.Y;
  262. left.Z += right.Z;
  263. return left;
  264. }
  265. /// <summary>
  266. /// Subtracts two instances.
  267. /// </summary>
  268. /// <param name="left">The first instance.</param>
  269. /// <param name="right">The second instance.</param>
  270. /// <returns>The result of the calculation.</returns>
  271. public static Vector3Float operator -(Vector3Float left, Vector3Float right)
  272. {
  273. left.X -= right.X;
  274. left.Y -= right.Y;
  275. left.Z -= right.Z;
  276. return left;
  277. }
  278. /// <summary>
  279. /// Negates an instance.
  280. /// </summary>
  281. /// <param name="vec">The instance.</param>
  282. /// <returns>The result of the calculation.</returns>
  283. public static Vector3Float operator -(Vector3Float vec)
  284. {
  285. vec.X = -vec.X;
  286. vec.Y = -vec.Y;
  287. vec.Z = -vec.Z;
  288. return vec;
  289. }
  290. /// <summary>
  291. /// Component wise multiply two vectors together, x*x, y*y, z*z.
  292. /// </summary>
  293. /// <param name="vecA"></param>
  294. /// <param name="vecB"></param>
  295. /// <returns></returns>
  296. public static Vector3Float operator *(Vector3Float vecA, Vector3Float vecB)
  297. {
  298. vecA.X *= vecB.X;
  299. vecA.Y *= vecB.Y;
  300. vecA.Z *= vecB.Z;
  301. return vecA;
  302. }
  303. /// <summary>
  304. /// Multiplies an instance by a scalar.
  305. /// </summary>
  306. /// <param name="vec">The instance.</param>
  307. /// <param name="scale">The scalar.</param>
  308. /// <returns>The result of the calculation.</returns>
  309. public static Vector3Float operator *(Vector3Float vec, double scale)
  310. {
  311. vec.X *= (float)scale;
  312. vec.Y *= (float)scale;
  313. vec.Z *= (float)scale;
  314. return vec;
  315. }
  316. /// <summary>
  317. /// Multiplies an instance by a scalar.
  318. /// </summary>
  319. /// <param name="scale">The scalar.</param>
  320. /// <param name="vec">The instance.</param>
  321. /// <returns>The result of the calculation.</returns>
  322. public static Vector3Float operator *(float scale, Vector3Float vec)
  323. {
  324. vec.X *= scale;
  325. vec.Y *= scale;
  326. vec.Z *= scale;
  327. return vec;
  328. }
  329. /// <summary>
  330. /// Creates a new vector which is the numerator divide by each component of the vector.
  331. /// </summary>
  332. /// <param name="numerator"></param>
  333. /// <param name="vec"></param>
  334. /// <returns>The result of the calculation.</returns>
  335. public static Vector3Float operator /(float numerator, Vector3Float vec)
  336. {
  337. return new Vector3Float((numerator / vec.X), (numerator / vec.Y), (numerator / vec.Z));
  338. }
  339. /// <summary>
  340. /// Divides an instance by a scalar.
  341. /// </summary>
  342. /// <param name="vec">The instance.</param>
  343. /// <param name="scale">The scalar.</param>
  344. /// <returns>The result of the calculation.</returns>
  345. public static Vector3Float operator /(Vector3Float vec, double scale)
  346. {
  347. float mult = 1 / (float)scale;
  348. vec.X *= mult;
  349. vec.Y *= mult;
  350. vec.Z *= mult;
  351. return vec;
  352. }
  353. /// <summary>
  354. /// Compares two instances for equality.
  355. /// </summary>
  356. /// <param name="left">The first instance.</param>
  357. /// <param name="right">The second instance.</param>
  358. /// <returns>True, if left equals right; false otherwise.</returns>
  359. public static bool operator ==(Vector3Float left, Vector3Float right)
  360. {
  361. return left.Equals(right);
  362. }
  363. /// <summary>
  364. /// Compares two instances for inequality.
  365. /// </summary>
  366. /// <param name="left">The first instance.</param>
  367. /// <param name="right">The second instance.</param>
  368. /// <returns>True, if left does not equa lright; false otherwise.</returns>
  369. public static bool operator !=(Vector3Float left, Vector3Float right)
  370. {
  371. return !left.Equals(right);
  372. }
  373. #endregion Operators
  374. #region Overrides
  375. #region public override string ToString()
  376. /// <summary>
  377. /// Returns a System.String that represents the current Vector3Float.
  378. /// </summary>
  379. /// <returns></returns>
  380. public override string ToString()
  381. {
  382. return $"[{X}, {Y}, {Z}]";
  383. }
  384. #endregion public override string ToString()
  385. #region public override int GetHashCode()
  386. /// <summary>
  387. /// return a 64 bit hash code proposed by Jon Skeet
  388. // http://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order
  389. /// </summary>
  390. /// <returns></returns>
  391. public ulong GetLongHashCode(ulong hash = 14695981039346656037)
  392. {
  393. hash = Vector4.GetLongHashCode(X, hash);
  394. hash = Vector4.GetLongHashCode(Y, hash);
  395. hash = Vector4.GetLongHashCode(Z, hash);
  396. return hash;
  397. }
  398. /// <summary>
  399. /// Returns the hashcode for this instance.
  400. /// </summary>
  401. /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
  402. public override int GetHashCode()
  403. {
  404. return new { X, Y, Z }.GetHashCode();
  405. }
  406. #endregion public override int GetHashCode()
  407. #region public override bool Equals(object obj)
  408. /// <summary>
  409. /// Indicates whether this instance and a specified object are equal.
  410. /// </summary>
  411. /// <param name="obj">The object to compare to.</param>
  412. /// <returns>True if the instances are equal; false otherwise.</returns>
  413. public override bool Equals(object obj)
  414. {
  415. if (!(obj is Vector3Float))
  416. return false;
  417. return this.Equals((Vector3Float)obj);
  418. }
  419. /// <summary>
  420. /// Indicates whether this instance and a specified object are equal within an error range.
  421. /// </summary>
  422. /// <param name="OtherVector"></param>
  423. /// <param name="ErrorValue"></param>
  424. /// <returns>True if the instances are equal; false otherwise.</returns>
  425. public bool Equals(Vector3Float OtherVector, double ErrorValue)
  426. {
  427. if ((X < OtherVector.X + ErrorValue && X > OtherVector.X - ErrorValue) &&
  428. (Y < OtherVector.Y + ErrorValue && Y > OtherVector.Y - ErrorValue) &&
  429. (Z < OtherVector.Z + ErrorValue && Z > OtherVector.Z - ErrorValue))
  430. {
  431. return true;
  432. }
  433. return false;
  434. }
  435. #endregion public override bool Equals(object obj)
  436. #endregion Overrides
  437. #endregion Public Members
  438. #region IEquatable<Vector3Float> Members
  439. /// <summary>Indicates whether the current vector is equal to another vector.</summary>
  440. /// <param name="other">A vector to compare with this vector.</param>
  441. /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
  442. public bool Equals(Vector3Float other)
  443. {
  444. return
  445. X == other.X &&
  446. Y == other.Y &&
  447. Z == other.Z;
  448. }
  449. #endregion IEquatable<Vector3Float> Members
  450. }
  451. public static class Vector3FloatEx
  452. {
  453. #region Static
  454. #region Add
  455. /// <summary>
  456. /// Adds two vectors.
  457. /// </summary>
  458. /// <param name="a">Left operand.</param>
  459. /// <param name="b">Right operand.</param>
  460. /// <returns>Result of operation.</returns>
  461. public static Vector3Float Add(Vector3Float a, Vector3Float b)
  462. {
  463. Add(ref a, ref b, out a);
  464. return a;
  465. }
  466. /// <summary>
  467. /// Adds two vectors.
  468. /// </summary>
  469. /// <param name="a">Left operand.</param>
  470. /// <param name="b">Right operand.</param>
  471. /// <param name="result">Result of operation.</param>
  472. public static void Add(ref Vector3Float a, ref Vector3Float b, out Vector3Float result)
  473. {
  474. result = new Vector3Float(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
  475. }
  476. #endregion Add
  477. #region Subtract
  478. /// <summary>
  479. /// Subtract one Vector from another
  480. /// </summary>
  481. /// <param name="a">First operand</param>
  482. /// <param name="b">Second operand</param>
  483. /// <returns>Result of subtraction</returns>
  484. public static Vector3Float Subtract(Vector3Float a, Vector3Float b)
  485. {
  486. Subtract(ref a, ref b, out a);
  487. return a;
  488. }
  489. /// <summary>
  490. /// Subtract one Vector from another
  491. /// </summary>
  492. /// <param name="a">First operand</param>
  493. /// <param name="b">Second operand</param>
  494. /// <param name="result">Result of subtraction</param>
  495. public static void Subtract(ref Vector3Float a, ref Vector3Float b, out Vector3Float result)
  496. {
  497. result = new Vector3Float(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
  498. }
  499. #endregion Subtract
  500. #region Multiply
  501. /// <summary>
  502. /// Multiplies a vector by a scalar.
  503. /// </summary>
  504. /// <param name="vector">Left operand.</param>
  505. /// <param name="scale">Right operand.</param>
  506. /// <returns>Result of the operation.</returns>
  507. public static Vector3Float Multiply(Vector3Float vector, float scale)
  508. {
  509. Multiply(ref vector, scale, out vector);
  510. return vector;
  511. }
  512. /// <summary>
  513. /// Multiplies a vector by a scalar.
  514. /// </summary>
  515. /// <param name="vector">Left operand.</param>
  516. /// <param name="scale">Right operand.</param>
  517. /// <param name="result">Result of the operation.</param>
  518. public static void Multiply(ref Vector3Float vector, float scale, out Vector3Float result)
  519. {
  520. result = new Vector3Float(vector.X * scale, vector.Y * scale, vector.Z * scale);
  521. }
  522. /// <summary>
  523. /// Multiplies a vector by the components a vector (scale).
  524. /// </summary>
  525. /// <param name="vector">Left operand.</param>
  526. /// <param name="scale">Right operand.</param>
  527. /// <returns>Result of the operation.</returns>
  528. public static Vector3Float Multiply(Vector3Float vector, Vector3Float scale)
  529. {
  530. Multiply(ref vector, ref scale, out vector);
  531. return vector;
  532. }
  533. /// <summary>
  534. /// Multiplies a vector by the components of a vector (scale).
  535. /// </summary>
  536. /// <param name="vector">Left operand.</param>
  537. /// <param name="scale">Right operand.</param>
  538. /// <param name="result">Result of the operation.</param>
  539. public static void Multiply(ref Vector3Float vector, ref Vector3Float scale, out Vector3Float result)
  540. {
  541. result = new Vector3Float(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z);
  542. }
  543. #endregion Multiply
  544. #region Divide
  545. /// <summary>
  546. /// Divides a vector by a scalar.
  547. /// </summary>
  548. /// <param name="vector">Left operand.</param>
  549. /// <param name="scale">Right operand.</param>
  550. /// <returns>Result of the operation.</returns>
  551. public static Vector3Float Divide(Vector3Float vector, float scale)
  552. {
  553. Divide(ref vector, scale, out vector);
  554. return vector;
  555. }
  556. /// <summary>
  557. /// Divides a vector by a scalar.
  558. /// </summary>
  559. /// <param name="vector">Left operand.</param>
  560. /// <param name="scale">Right operand.</param>
  561. /// <param name="result">Result of the operation.</param>
  562. public static void Divide(ref Vector3Float vector, float scale, out Vector3Float result)
  563. {
  564. Multiply(ref vector, 1 / scale, out result);
  565. }
  566. /// <summary>
  567. /// Divides a vector by the components of a vector (scale).
  568. /// </summary>
  569. /// <param name="vector">Left operand.</param>
  570. /// <param name="scale">Right operand.</param>
  571. /// <returns>Result of the operation.</returns>
  572. public static Vector3Float Divide(Vector3Float vector, Vector3Float scale)
  573. {
  574. Divide(ref vector, ref scale, out vector);
  575. return vector;
  576. }
  577. /// <summary>
  578. /// Divide a vector by the components of a vector (scale).
  579. /// </summary>
  580. /// <param name="vector">Left operand.</param>
  581. /// <param name="scale">Right operand.</param>
  582. /// <param name="result">Result of the operation.</param>
  583. public static void Divide(ref Vector3Float vector, ref Vector3Float scale, out Vector3Float result)
  584. {
  585. result = new Vector3Float(vector.X / scale.X, vector.Y / scale.Y, vector.Z / scale.Z);
  586. }
  587. #endregion Divide
  588. #region ComponentMin
  589. public static float ComponentMin(this Vector3Float vector3)
  590. {
  591. return Math.Min(vector3.X, Math.Min(vector3.Y, vector3.Z));
  592. }
  593. public static float[] ToFloatArray(this List<Vector3Float> list)
  594. {
  595. var array = new float[list.Count * 3];
  596. for (var i = 0; i < list.Count; i++)
  597. {
  598. array[i * 3 + 0] = (float)list[i].X;
  599. array[i * 3 + 1] = (float)list[i].Y;
  600. array[i * 3 + 2] = (float)list[i].Z;
  601. }
  602. return array;
  603. }
  604. /// <summary>
  605. /// Calculate the component-wise minimum of two vectors
  606. /// </summary>
  607. /// <param name="a">First operand</param>
  608. /// <param name="b">Second operand</param>
  609. /// <returns>The component-wise minimum</returns>
  610. public static Vector3Float ComponentMin(this Vector3Float a, Vector3Float b)
  611. {
  612. a.X = a.X < b.X ? a.X : b.X;
  613. a.Y = a.Y < b.Y ? a.Y : b.Y;
  614. a.Z = a.Z < b.Z ? a.Z : b.Z;
  615. return a;
  616. }
  617. /// <summary>
  618. /// Calculate the component-wise minimum of two vectors
  619. /// </summary>
  620. /// <param name="a">First operand</param>
  621. /// <param name="b">Second operand</param>
  622. /// <param name="result">The component-wise minimum</param>
  623. public static void ComponentMin(this Vector3Float a, ref Vector3Float b, out Vector3Float result)
  624. {
  625. result.X = a.X < b.X ? a.X : b.X;
  626. result.Y = a.Y < b.Y ? a.Y : b.Y;
  627. result.Z = a.Z < b.Z ? a.Z : b.Z;
  628. }
  629. #endregion ComponentMin
  630. #region ComponentMax
  631. public static float ComponentMax(this Vector3Float vector3)
  632. {
  633. return Math.Max(vector3.X, Math.Max(vector3.Y, vector3.Z));
  634. }
  635. /// <summary>
  636. /// Calculate the component-wise maximum of two vectors
  637. /// </summary>
  638. /// <param name="a">First operand</param>
  639. /// <param name="b">Second operand</param>
  640. /// <returns>The component-wise maximum</returns>
  641. public static Vector3Float ComponentMax(this Vector3Float a, Vector3Float b)
  642. {
  643. a.X = a.X > b.X ? a.X : b.X;
  644. a.Y = a.Y > b.Y ? a.Y : b.Y;
  645. a.Z = a.Z > b.Z ? a.Z : b.Z;
  646. return a;
  647. }
  648. /// <summary>
  649. /// Calculate the component-wise maximum of two vectors
  650. /// </summary>
  651. /// <param name="a">First operand</param>
  652. /// <param name="b">Second operand</param>
  653. /// <param name="result">The component-wise maximum</param>
  654. public static void ComponentMax(this Vector3Float a, ref Vector3Float b, out Vector3Float result)
  655. {
  656. result.X = a.X > b.X ? a.X : b.X;
  657. result.Y = a.Y > b.Y ? a.Y : b.Y;
  658. result.Z = a.Z > b.Z ? a.Z : b.Z;
  659. }
  660. #endregion ComponentMax
  661. #region Min
  662. /// <summary>
  663. /// Returns the Vector3d with the minimum magnitude
  664. /// </summary>
  665. /// <param name="left">Left operand</param>
  666. /// <param name="right">Right operand</param>
  667. /// <returns>The minimum Vector3Float</returns>
  668. public static Vector3Float Min(Vector3Float left, Vector3Float right)
  669. {
  670. return left.LengthSquared < right.LengthSquared ? left : right;
  671. }
  672. #endregion Min
  673. #region Max
  674. /// <summary>
  675. /// Returns the Vector3d with the minimum magnitude
  676. /// </summary>
  677. /// <param name="left">Left operand</param>
  678. /// <param name="right">Right operand</param>
  679. /// <returns>The minimum Vector3Float</returns>
  680. public static Vector3Float Max(Vector3Float left, Vector3Float right)
  681. {
  682. return left.LengthSquared >= right.LengthSquared ? left : right;
  683. }
  684. #endregion Max
  685. #region Clamp
  686. /// <summary>
  687. /// Clamp a vector to the given minimum and maximum vectors
  688. /// </summary>
  689. /// <param name="vec">Input vector</param>
  690. /// <param name="min">Minimum vector</param>
  691. /// <param name="max">Maximum vector</param>
  692. /// <returns>The clamped vector</returns>
  693. public static Vector3Float Clamp(Vector3Float vec, Vector3Float min, Vector3Float max)
  694. {
  695. vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  696. vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  697. vec.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
  698. return vec;
  699. }
  700. /// <summary>
  701. /// Clamp a vector to the given minimum and maximum vectors
  702. /// </summary>
  703. /// <param name="vec">Input vector</param>
  704. /// <param name="min">Minimum vector</param>
  705. /// <param name="max">Maximum vector</param>
  706. /// <param name="result">The clamped vector</param>
  707. public static void Clamp(ref Vector3Float vec, ref Vector3Float min, ref Vector3Float max, out Vector3Float result)
  708. {
  709. result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
  710. result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
  711. result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
  712. }
  713. #endregion Clamp
  714. #region Normalize
  715. /// <summary>
  716. /// Scale a vector to unit length
  717. /// </summary>
  718. /// <param name="vec">The input vector</param>
  719. /// <returns>The normalized vector</returns>
  720. public static Vector3Float Normalize(Vector3Float vec)
  721. {
  722. float scale = 1.0f / vec.Length;
  723. vec.X *= scale;
  724. vec.Y *= scale;
  725. vec.Z *= scale;
  726. return vec;
  727. }
  728. /// <summary>
  729. /// Scale a vector to unit length
  730. /// </summary>
  731. /// <param name="vec">The input vector</param>
  732. /// <param name="result">The normalized vector</param>
  733. public static void Normalize(ref Vector3Float vec, out Vector3Float result)
  734. {
  735. float scale = 1.0f / vec.Length;
  736. result.X = vec.X * scale;
  737. result.Y = vec.Y * scale;
  738. result.Z = vec.Z * scale;
  739. }
  740. #endregion Normalize
  741. #region Dot
  742. /// <summary>
  743. /// Calculate the dot (scalar) product of two vectors
  744. /// </summary>
  745. /// <param name="left">First operand</param>
  746. /// <param name="right">Second operand</param>
  747. /// <returns>The dot product of the two inputs</returns>
  748. public static float Dot(this Vector3Float left, Vector3Float right)
  749. {
  750. return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
  751. }
  752. /// <summary>
  753. /// Calculate the dot (scalar) product of two vectors
  754. /// </summary>
  755. /// <param name="left">First operand</param>
  756. /// <param name="right">Second operand</param>
  757. /// <param name="result">The dot product of the two inputs</param>
  758. public static void Dot(this Vector3Float left, ref Vector3Float right, out float result)
  759. {
  760. result = left.X * right.X + left.Y * right.Y + left.Z * right.Z;
  761. }
  762. #endregion Dot
  763. #region Cross
  764. /// <summary>
  765. /// Calculate the cross (vector) product of two vectors
  766. /// </summary>
  767. /// <param name="left">First operand</param>
  768. /// <param name="right">Second operand</param>
  769. /// <returns>The cross product of the two inputs</returns>
  770. public static Vector3Float Cross(this Vector3Float left, Vector3Float right)
  771. {
  772. Vector3Float result;
  773. left.Cross(ref right, out result);
  774. return result;
  775. }
  776. /// <summary>
  777. /// Calculate the cross (vector) product of two vectors
  778. /// </summary>
  779. /// <param name="left">First operand</param>
  780. /// <param name="right">Second operand</param>
  781. /// <returns>The cross product of the two inputs</returns>
  782. /// <param name="result">The cross product of the two inputs</param>
  783. public static void Cross(this Vector3Float left, ref Vector3Float right, out Vector3Float result)
  784. {
  785. result = new Vector3Float(left.Y * right.Z - left.Z * right.Y,
  786. left.Z * right.X - left.X * right.Z,
  787. left.X * right.Y - left.Y * right.X);
  788. }
  789. #endregion Cross
  790. #region Utility
  791. /// <summary>
  792. /// Checks if 3 points are collinear (all lie on the same line).
  793. /// </summary>
  794. /// <param name="a"></param>
  795. /// <param name="b"></param>
  796. /// <param name="c"></param>
  797. /// <param name="epsilon"></param>
  798. /// <returns></returns>
  799. public static bool Collinear(Vector3Float a, Vector3Float b, Vector3Float c, float epsilon = .000001f)
  800. {
  801. // Return true if a, b, and c all lie on the same line.
  802. return Math.Abs(Cross(b - a, c - a).Length) < epsilon;
  803. }
  804. #endregion Utility
  805. #region Lerp
  806. /// <summary>
  807. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  808. /// </summary>
  809. /// <param name="a">First input vector</param>
  810. /// <param name="b">Second input vector</param>
  811. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  812. /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
  813. public static Vector3Float Lerp(Vector3Float a, Vector3Float b, float blend)
  814. {
  815. a.X = blend * (b.X - a.X) + a.X;
  816. a.Y = blend * (b.Y - a.Y) + a.Y;
  817. a.Z = blend * (b.Z - a.Z) + a.Z;
  818. return a;
  819. }
  820. /// <summary>
  821. /// Returns a new Vector that is the linear blend of the 2 given Vectors
  822. /// </summary>
  823. /// <param name="a">First input vector</param>
  824. /// <param name="b">Second input vector</param>
  825. /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
  826. /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param>
  827. public static void Lerp(ref Vector3Float a, ref Vector3Float b, float blend, out Vector3Float result)
  828. {
  829. result.X = blend * (b.X - a.X) + a.X;
  830. result.Y = blend * (b.Y - a.Y) + a.Y;
  831. result.Z = blend * (b.Z - a.Z) + a.Z;
  832. }
  833. #endregion Lerp
  834. #region Barycentric
  835. /// <summary>
  836. /// Interpolate 3 Vectors using Barycentric coordinates
  837. /// </summary>
  838. /// <param name="a">First input Vector</param>
  839. /// <param name="b">Second input Vector</param>
  840. /// <param name="c">Third input Vector</param>
  841. /// <param name="u">First Barycentric Coordinate</param>
  842. /// <param name="v">Second Barycentric Coordinate</param>
  843. /// <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>
  844. public static Vector3Float BaryCentric(Vector3Float a, Vector3Float b, Vector3Float c, float u, float v)
  845. {
  846. return a + u * (b - a) + v * (c - a);
  847. }
  848. /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary>
  849. /// <param name="a">First input Vector.</param>
  850. /// <param name="b">Second input Vector.</param>
  851. /// <param name="c">Third input Vector.</param>
  852. /// <param name="u">First Barycentric Coordinate.</param>
  853. /// <param name="v">Second Barycentric Coordinate.</param>
  854. /// <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>
  855. public static void BaryCentric(ref Vector3Float a, ref Vector3Float b, ref Vector3Float c, float u, float v, out Vector3Float result)
  856. {
  857. result = a; // copy
  858. Vector3Float temp = b; // copy
  859. Subtract(ref temp, ref a, out temp);
  860. Multiply(ref temp, u, out temp);
  861. Add(ref result, ref temp, out result);
  862. temp = c; // copy
  863. Subtract(ref temp, ref a, out temp);
  864. Multiply(ref temp, v, out temp);
  865. Add(ref result, ref temp, out result);
  866. }
  867. #endregion Barycentric
  868. #region Transform
  869. /// <summary>Transform a direction vector by the given Matrix
  870. /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
  871. /// </summary>
  872. /// <param name="vec">The vector to transform</param>
  873. /// <param name="mat">The desired transformation</param>
  874. /// <returns>The transformed vector</returns>
  875. public static Vector3Float TransformVector(this Vector3Float vec, Matrix4X4 mat)
  876. {
  877. return new Vector3Float(
  878. vec.Dot(new Vector3Float(mat.Column0)),
  879. vec.Dot(new Vector3Float(mat.Column1)),
  880. vec.Dot(new Vector3Float(mat.Column2)));
  881. }
  882. /// <summary>Transform a direction vector by the given Matrix
  883. /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
  884. /// </summary>
  885. /// <param name="vec">The vector to transform</param>
  886. /// <param name="mat">The desired transformation</param>
  887. /// <param name="result">The transformed vector</param>
  888. public static void TransformVector(this Vector3Float vec, ref Matrix4X4 mat, out Vector3Float result)
  889. {
  890. #if true
  891. throw new NotImplementedException();
  892. #else
  893. result.x = vec.x * mat.Row0.x +
  894. vec.y * mat.Row1.x +
  895. vec.z * mat.Row2.x;
  896. result.y = vec.x * mat.Row0.y +
  897. vec.y * mat.Row1.y +
  898. vec.z * mat.Row2.y;
  899. result.z = vec.x * mat.Row0.z +
  900. vec.y * mat.Row1.z +
  901. vec.z * mat.Row2.z;
  902. #endif
  903. }
  904. /// <summary>Transform a Normal by the given Matrix</summary>
  905. /// <remarks>
  906. /// This calculates the inverse of the given matrix, use TransformNormalInverse if you
  907. /// already have the inverse to avoid this extra calculation
  908. /// </remarks>
  909. /// <param name="norm">The normal to transform</param>
  910. /// <param name="mat">The desired transformation</param>
  911. /// <returns>The transformed normal</returns>
  912. public static Vector3Float TransformNormal(this Vector3Float norm, Matrix4X4 mat)
  913. {
  914. mat.Invert();
  915. return norm.TransformNormalInverse(mat);
  916. }
  917. /// <summary>Transform a Normal by the given Matrix</summary>
  918. /// <remarks>
  919. /// This calculates the inverse of the given matrix, use TransformNormalInverse if you
  920. /// already have the inverse to avoid this extra calculation
  921. /// </remarks>
  922. /// <param name="norm">The normal to transform</param>
  923. /// <param name="mat">The desired transformation</param>
  924. /// <param name="result">The transformed normal</param>
  925. public static void TransformNormal(this Vector3Float norm, ref Matrix4X4 mat, out Vector3Float result)
  926. {
  927. Matrix4X4 Inverse = Matrix4X4.Invert(mat);
  928. norm.TransformNormalInverse(ref Inverse, out result);
  929. }
  930. /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
  931. /// <remarks>
  932. /// This version doesn't calculate the inverse matrix.
  933. /// Use this version if you already have the inverse of the desired transform to hand
  934. /// </remarks>
  935. /// <param name="norm">The normal to transform</param>
  936. /// <param name="invMat">The inverse of the desired transformation</param>
  937. /// <returns>The transformed normal</returns>
  938. public static Vector3Float TransformNormalInverse(this Vector3Float norm, Matrix4X4 invMat)
  939. {
  940. return new Vector3Float(
  941. norm.Dot(new Vector3Float(invMat.Row0)),
  942. norm.Dot(new Vector3Float(invMat.Row1)),
  943. norm.Dot(new Vector3Float(invMat.Row2)));
  944. }
  945. /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
  946. /// <remarks>
  947. /// This version doesn't calculate the inverse matrix.
  948. /// Use this version if you already have the inverse of the desired transform to hand
  949. /// </remarks>
  950. /// <param name="norm">The normal to transform</param>
  951. /// <param name="invMat">The inverse of the desired transformation</param>
  952. /// <param name="result">The transformed normal</param>
  953. public static void TransformNormalInverse(this Vector3Float norm, ref Matrix4X4 invMat, out Vector3Float result)
  954. {
  955. #if true
  956. throw new NotImplementedException();
  957. #else
  958. result.x = norm.x * invMat.Row0.x +
  959. norm.y * invMat.Row0.y +
  960. norm.z * invMat.Row0.z;
  961. result.y = norm.x * invMat.Row1.x +
  962. norm.y * invMat.Row1.y +
  963. norm.z * invMat.Row1.z;
  964. result.z = norm.x * invMat.Row2.x +
  965. norm.y * invMat.Row2.y +
  966. norm.z * invMat.Row2.z;
  967. #endif
  968. }
  969. /// <summary>Transform a Position by the given Matrix</summary>
  970. /// <param name="pos">The position to transform</param>
  971. /// <param name="mat">The desired transformation</param>
  972. /// <returns>The transformed position</returns>
  973. public static Vector3Float TransformPosition(this Vector3Float pos, Matrix4X4 mat)
  974. {
  975. #if true
  976. throw new NotImplementedException();
  977. #else
  978. return new Vector3Float(
  979. Vector3Float.Dot(pos, new Vector3Float((float)mat.Column0)) + mat.Row3.x,
  980. Vector3Float.Dot(pos, new Vector3Float((float)mat.Column1)) + mat.Row3.y,
  981. Vector3Float.Dot(pos, new Vector3Float((float)mat.Column2)) + mat.Row3.z);
  982. #endif
  983. }
  984. /// <summary>Transform a Position by the given Matrix</summary>
  985. /// <param name="pos">The position to transform</param>
  986. /// <param name="mat">The desired transformation</param>
  987. /// <param name="result">The transformed position</param>
  988. public static void TransformPosition(this Vector3Float pos, ref Matrix4X4 mat, out Vector3Float result)
  989. {
  990. #if true
  991. throw new NotImplementedException();
  992. #else
  993. result.x = pos.x * mat.Row0.x +
  994. pos.y * mat.Row1.x +
  995. pos.z * mat.Row2.x +
  996. mat.Row3.x;
  997. result.y = pos.x * mat.Row0.y +
  998. pos.y * mat.Row1.y +
  999. pos.z * mat.Row2.y +
  1000. mat.Row3.y;
  1001. result.z = pos.x * mat.Row0.z +
  1002. pos.y * mat.Row1.z +
  1003. pos.z * mat.Row2.z +
  1004. mat.Row3.z;
  1005. #endif
  1006. }
  1007. /// <summary>
  1008. /// Transform all the vectors in the array by the given Matrix.
  1009. /// </summary>
  1010. /// <param name="boundsVerts"></param>
  1011. /// <param name="rotationQuaternion"></param>
  1012. public static void Transform(this Vector3Float[] vecArray, Matrix4X4 mat)
  1013. {
  1014. for (int i = 0; i < vecArray.Length; i++)
  1015. {
  1016. vecArray[i] = Transform(vecArray[i], mat);
  1017. }
  1018. }
  1019. /// <summary>Transform a Vector by the given Matrix</summary>
  1020. /// <param name="vec">The vector to transform</param>
  1021. /// <param name="mat">The desired transformation</param>
  1022. /// <returns>The transformed vector</returns>
  1023. public static Vector3Float Transform(this Vector3Float vec, Matrix4X4 mat)
  1024. {
  1025. Vector3Float result;
  1026. Transform(vec, ref mat, out result);
  1027. return result;
  1028. }
  1029. /// <summary>Transform a Vector by the given Matrix</summary>
  1030. /// <param name="vec">The vector to transform</param>
  1031. /// <param name="mat">The desired transformation</param>
  1032. /// <param name="result">The transformed vector</param>
  1033. public static void Transform(this Vector3Float inVec, ref Matrix4X4 mat, out Vector3Float result)
  1034. {
  1035. Vector3 vec = new Vector3(inVec);
  1036. Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0);
  1037. Vector4.Transform(v4, ref mat, out v4);
  1038. result = new Vector3Float(v4.Xyz);
  1039. }
  1040. /// <summary>
  1041. /// Transforms a vector by a quaternion rotation.
  1042. /// </summary>
  1043. /// <param name="vec">The vector to transform.</param>
  1044. /// <param name="quat">The quaternion to rotate the vector by.</param>
  1045. /// <returns>The result of the operation.</returns>
  1046. public static Vector3Float Transform(this Vector3Float vec, Quaternion quat)
  1047. {
  1048. #if true
  1049. throw new NotImplementedException();
  1050. #else
  1051. Vector3Float result;
  1052. Transform(ref vec, ref quat, out result);
  1053. return result;
  1054. #endif
  1055. }
  1056. /// <summary>
  1057. /// Transforms a vector by a quaternion rotation.
  1058. /// </summary>
  1059. /// <param name="vec">The vector to transform.</param>
  1060. /// <param name="quat">The quaternion to rotate the vector by.</param>
  1061. /// <param name="result">The result of the operation.</param>
  1062. public static void Transform(this Vector3Float vec, ref Quaternion quat, out Vector3Float result)
  1063. {
  1064. #if true
  1065. throw new NotImplementedException();
  1066. #else
  1067. // Since vec.W == 0, we can optimize quat * vec * quat^-1 as follows:
  1068. // vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec)
  1069. Vector3Float xyz = quat.Xyz, temp, temp2;
  1070. Vector3Float.Cross(ref xyz, ref vec, out temp);
  1071. Vector3Float.Multiply(ref vec, quat.W, out temp2);
  1072. Vector3Float.Add(ref temp, ref temp2, out temp);
  1073. Vector3Float.Cross(ref xyz, ref temp, out temp);
  1074. Vector3Float.Multiply(ref temp, 2, out temp);
  1075. Vector3Float.Add(ref vec, ref temp, out result);
  1076. #endif
  1077. }
  1078. /// <summary>
  1079. /// Transform all the vectors in the array by the quaternion rotation.
  1080. /// </summary>
  1081. /// <param name="boundsVerts"></param>
  1082. /// <param name="rotationQuaternion"></param>
  1083. public static void Transform(this Vector3Float[] vecArray, Quaternion rotationQuaternion)
  1084. {
  1085. for (int i = 0; i < vecArray.Length; i++)
  1086. {
  1087. vecArray[i] = Transform(vecArray[i], rotationQuaternion);
  1088. }
  1089. }
  1090. /// <summary>
  1091. /// Transform a Vector3d by the given Matrix, and project the resulting Vector4 back to a Vector3Float
  1092. /// </summary>
  1093. /// <param name="vec">The vector to transform</param>
  1094. /// <param name="mat">The desired transformation</param>
  1095. /// <returns>The transformed vector</returns>
  1096. public static Vector3Float TransformPerspective(this Vector3Float vec, Matrix4X4 mat)
  1097. {
  1098. #if true
  1099. throw new NotImplementedException();
  1100. #else
  1101. Vector3Float result;
  1102. TransformPerspective(ref vec, ref mat, out result);
  1103. return result;
  1104. #endif
  1105. }
  1106. /// <summary>Transform a Vector3d by the given Matrix, and project the resulting Vector4d back to a Vector3d</summary>
  1107. /// <param name="vec">The vector to transform</param>
  1108. /// <param name="mat">The desired transformation</param>
  1109. /// <param name="result">The transformed vector</param>
  1110. public static void TransformPerspective(this Vector3Float vec, ref Matrix4X4 mat, out Vector3Float result)
  1111. {
  1112. #if true
  1113. throw new NotImplementedException();
  1114. #else
  1115. Vector4 v = new Vector4(vec);
  1116. Vector4.Transform(ref v, ref mat, out v);
  1117. result.x = v.x / v.w;
  1118. result.y = v.y / v.w;
  1119. result.z = v.z / v.w;
  1120. #endif
  1121. }
  1122. #endregion Transform
  1123. #region CalculateAngle
  1124. /// <summary>
  1125. /// Calculates the angle (in radians) between two vectors.
  1126. /// </summary>
  1127. /// <param name="first">The first vector.</param>
  1128. /// <param name="second">The second vector.</param>
  1129. /// <returns>Angle (in radians) between the vectors.</returns>
  1130. /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
  1131. public static float CalculateAngle(this Vector3Float first, Vector3Float second)
  1132. {
  1133. return (float)Math.Acos((first.Dot(second)) / (first.Length * second.Length));
  1134. }
  1135. /// <summary>Calculates the angle (in radians) between two vectors.</summary>
  1136. /// <param name="first">The first vector.</param>
  1137. /// <param name="second">The second vector.</param>
  1138. /// <param name="result">Angle (in radians) between the vectors.</param>
  1139. /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
  1140. public static void CalculateAngle(this Vector3Float first, ref Vector3Float second, out float result)
  1141. {
  1142. float temp;
  1143. first.Dot(ref second, out temp);
  1144. result = (float)Math.Acos(temp / (first.Length * second.Length));
  1145. }
  1146. #endregion CalculateAngle
  1147. #endregion Static
  1148. }
  1149. }