PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/LED_Editor/_Libs/Pencil.Gaming/Pencil.Gaming/Math/Vector2h.cs

https://gitlab.com/Alex_Green/led_engine
C# | 365 lines | 178 code | 57 blank | 130 comment | 1 complexity | 6a6cd252660d0c7f4ae2348089845fb1 MD5 | raw file
  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.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Runtime.Serialization;
  25. using System.Xml.Serialization;
  26. using Pencil.Gaming.MathUtils;
  27. namespace Pencil.Gaming.MathUtils
  28. {
  29. /// <summary>2-component Vector of the Half type. Occupies 4 Byte total.</summary>
  30. [Serializable, StructLayout(LayoutKind.Sequential)]
  31. public struct Vector2h : ISerializable, IEquatable<Vector2h>
  32. {
  33. #region Fields
  34. /// <summary>The X component of the Half2.</summary>
  35. public Half X;
  36. /// <summary>The Y component of the Half2.</summary>
  37. public Half Y;
  38. #endregion
  39. #region Constructors
  40. /// <summary>
  41. /// Constructs a new instance.
  42. /// </summary>
  43. /// <param name="value">The value that will initialize this instance.</param>
  44. public Vector2h(Half value)
  45. {
  46. X = value;
  47. Y = value;
  48. }
  49. /// <summary>
  50. /// Constructs a new instance.
  51. /// </summary>
  52. /// <param name="value">The value that will initialize this instance.</param>
  53. public Vector2h(Single value)
  54. {
  55. X = new Half(value);
  56. Y = new Half(value);
  57. }
  58. /// <summary>
  59. /// The new Half2 instance will avoid conversion and copy directly from the Half parameters.
  60. /// </summary>
  61. /// <param name="x">An Half instance of a 16-bit half-precision floating-point number.</param>
  62. /// <param name="y">An Half instance of a 16-bit half-precision floating-point number.</param>
  63. public Vector2h(Half x, Half y)
  64. {
  65. X = x;
  66. Y = y;
  67. }
  68. /// <summary>
  69. /// The new Half2 instance will convert the 2 parameters into 16-bit half-precision floating-point.
  70. /// </summary>
  71. /// <param name="x">32-bit single-precision floating-point number.</param>
  72. /// <param name="y">32-bit single-precision floating-point number.</param>
  73. public Vector2h(Single x, Single y)
  74. {
  75. X = new Half(x);
  76. Y = new Half(y);
  77. }
  78. /// <summary>
  79. /// The new Half2 instance will convert the 2 parameters into 16-bit half-precision floating-point.
  80. /// </summary>
  81. /// <param name="x">32-bit single-precision floating-point number.</param>
  82. /// <param name="y">32-bit single-precision floating-point number.</param>
  83. /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
  84. public Vector2h(Single x, Single y, bool throwOnError)
  85. {
  86. X = new Half(x, throwOnError);
  87. Y = new Half(y, throwOnError);
  88. }
  89. /// <summary>
  90. /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point.
  91. /// </summary>
  92. /// <param name="v">OpenTK.Vector2</param>
  93. public Vector2h(Vector2 v)
  94. {
  95. X = new Half(v.X);
  96. Y = new Half(v.Y);
  97. }
  98. /// <summary>
  99. /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point.
  100. /// </summary>
  101. /// <param name="v">OpenTK.Vector2</param>
  102. /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
  103. public Vector2h(Vector2 v, bool throwOnError)
  104. {
  105. X = new Half(v.X, throwOnError);
  106. Y = new Half(v.Y, throwOnError);
  107. }
  108. /// <summary>
  109. /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point.
  110. /// This is the fastest constructor.
  111. /// </summary>
  112. /// <param name="v">OpenTK.Vector2</param>
  113. public Vector2h(ref Vector2 v)
  114. {
  115. X = new Half(v.X);
  116. Y = new Half(v.Y);
  117. }
  118. /// <summary>
  119. /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point.
  120. /// </summary>
  121. /// <param name="v">OpenTK.Vector2</param>
  122. /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
  123. public Vector2h(ref Vector2 v, bool throwOnError)
  124. {
  125. X = new Half(v.X, throwOnError);
  126. Y = new Half(v.Y, throwOnError);
  127. }
  128. /// <summary>
  129. /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point.
  130. /// </summary>
  131. /// <param name="v">OpenTK.Vector2d</param>
  132. public Vector2h(Vector2d v)
  133. {
  134. X = new Half(v.X);
  135. Y = new Half(v.Y);
  136. }
  137. /// <summary>
  138. /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point.
  139. /// </summary>
  140. /// <param name="v">OpenTK.Vector2d</param>
  141. /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
  142. public Vector2h(Vector2d v, bool throwOnError)
  143. {
  144. X = new Half(v.X, throwOnError);
  145. Y = new Half(v.Y, throwOnError);
  146. }
  147. /// <summary>
  148. /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point.
  149. /// This is the faster constructor.
  150. /// </summary>
  151. /// <param name="v">OpenTK.Vector2d</param>
  152. public Vector2h(ref Vector2d v)
  153. {
  154. X = new Half(v.X);
  155. Y = new Half(v.Y);
  156. }
  157. /// <summary>
  158. /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point.
  159. /// </summary>
  160. /// <param name="v">OpenTK.Vector2d</param>
  161. /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param>
  162. public Vector2h(ref Vector2d v, bool throwOnError)
  163. {
  164. X = new Half(v.X, throwOnError);
  165. Y = new Half(v.Y, throwOnError);
  166. }
  167. #endregion Constructors
  168. #region Swizzle
  169. /// <summary>
  170. /// Gets or sets an OpenTK.Vector2h with the Y and X components of this instance.
  171. /// </summary>
  172. [XmlIgnore]
  173. public Vector2h Yx { get { return new Vector2h(Y, X); } set { Y = value.X; X = value.Y; } }
  174. #endregion
  175. #region Half -> Single
  176. /// <summary>
  177. /// Returns this Half2 instance's contents as Vector2.
  178. /// </summary>
  179. /// <returns>OpenTK.Vector2</returns>
  180. public Vector2 ToVector2()
  181. {
  182. return new Vector2(X, Y);
  183. }
  184. /// <summary>
  185. /// Returns this Half2 instance's contents as Vector2d.
  186. /// </summary>
  187. public Vector2d ToVector2d()
  188. {
  189. return new Vector2d(X, Y);
  190. }
  191. #endregion Half -> Single
  192. #region Conversions
  193. /// <summary>Converts OpenTK.Vector2 to OpenTK.Half2.</summary>
  194. /// <param name="v">The Vector2 to convert.</param>
  195. /// <returns>The resulting Half vector.</returns>
  196. public static explicit operator Vector2h(Vector2 v)
  197. {
  198. return new Vector2h(v);
  199. }
  200. /// <summary>Converts OpenTK.Vector2d to OpenTK.Half2.</summary>
  201. /// <param name="v">The Vector2d to convert.</param>
  202. /// <returns>The resulting Half vector.</returns>
  203. public static explicit operator Vector2h(Vector2d v)
  204. {
  205. return new Vector2h(v);
  206. }
  207. /// <summary>Converts OpenTK.Half2 to OpenTK.Vector2.</summary>
  208. /// <param name="h">The Half2 to convert.</param>
  209. /// <returns>The resulting Vector2.</returns>
  210. public static explicit operator Vector2(Vector2h h)
  211. {
  212. return new Vector2(h.X, h.Y);
  213. }
  214. /// <summary>Converts OpenTK.Half2 to OpenTK.Vector2d.</summary>
  215. /// <param name="h">The Half2 to convert.</param>
  216. /// <returns>The resulting Vector2d.</returns>
  217. public static explicit operator Vector2d(Vector2h h)
  218. {
  219. return new Vector2d(h.X, h.Y);
  220. }
  221. #endregion Conversions
  222. #region Constants
  223. /// <summary>The size in bytes for an instance of the Half2 struct is 4.</summary>
  224. public static readonly int SizeInBytes = 4;
  225. #endregion Constants
  226. #region ISerializable
  227. /// <summary>Constructor used by ISerializable to deserialize the object.</summary>
  228. /// <param name="info"></param>
  229. /// <param name="context"></param>
  230. public Vector2h(SerializationInfo info, StreamingContext context)
  231. {
  232. this.X = (Half)info.GetValue("X", typeof(Half));
  233. this.Y = (Half)info.GetValue("Y", typeof(Half));
  234. }
  235. /// <summary>Used by ISerialize to serialize the object.</summary>
  236. /// <param name="info"></param>
  237. /// <param name="context"></param>
  238. public void GetObjectData(SerializationInfo info, StreamingContext context)
  239. {
  240. info.AddValue("X", this.X);
  241. info.AddValue("Y", this.Y);
  242. }
  243. #endregion ISerializable
  244. #region Binary dump
  245. /// <summary>Updates the X and Y components of this instance by reading from a Stream.</summary>
  246. /// <param name="bin">A BinaryReader instance associated with an open Stream.</param>
  247. public void FromBinaryStream(BinaryReader bin)
  248. {
  249. X.FromBinaryStream(bin);
  250. Y.FromBinaryStream(bin);
  251. }
  252. /// <summary>Writes the X and Y components of this instance into a Stream.</summary>
  253. /// <param name="bin">A BinaryWriter instance associated with an open Stream.</param>
  254. public void ToBinaryStream(BinaryWriter bin)
  255. {
  256. X.ToBinaryStream(bin);
  257. Y.ToBinaryStream(bin);
  258. }
  259. #endregion Binary dump
  260. #region IEquatable<Half2> Members
  261. /// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Half2 vector.</summary>
  262. /// <param name="other">OpenTK.Half2 to compare to this instance..</param>
  263. /// <returns>True, if other is equal to this instance; false otherwise.</returns>
  264. public bool Equals(Vector2h other)
  265. {
  266. return (this.X.Equals(other.X) && this.Y.Equals(other.Y));
  267. }
  268. #endregion
  269. #region ToString()
  270. private static string listSeparator = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ListSeparator;
  271. /// <summary>Returns a string that contains this Half2's numbers in human-legible form.</summary>
  272. public override string ToString()
  273. {
  274. return String.Format("({0}{2} {1})", X, Y, listSeparator);
  275. }
  276. #endregion ToString()
  277. #region BitConverter
  278. /// <summary>Returns the Half2 as an array of bytes.</summary>
  279. /// <param name="h">The Half2 to convert.</param>
  280. /// <returns>The input as byte array.</returns>
  281. public static byte[] GetBytes(Vector2h h)
  282. {
  283. byte[] result = new byte[SizeInBytes];
  284. byte[] temp = Half.GetBytes(h.X);
  285. result[0] = temp[0];
  286. result[1] = temp[1];
  287. temp = Half.GetBytes(h.Y);
  288. result[2] = temp[0];
  289. result[3] = temp[1];
  290. return result;
  291. }
  292. /// <summary>Converts an array of bytes into Half2.</summary>
  293. /// <param name="value">A Half2 in it's byte[] representation.</param>
  294. /// <param name="startIndex">The starting position within value.</param>
  295. /// <returns>A new Half2 instance.</returns>
  296. public static Vector2h FromBytes(byte[] value, int startIndex)
  297. {
  298. Vector2h h2 = new Vector2h();
  299. h2.X = Half.FromBytes(value, startIndex);
  300. h2.Y = Half.FromBytes(value, startIndex + 2);
  301. return h2;
  302. }
  303. #endregion BitConverter
  304. }
  305. }