Vector3.cs 39 KB

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