Vector3.cs 41 KB

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