Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* |
||
| 3 | Copyright (c) 2006 - 2008 The Open Toolkit library. |
||
| 4 | |||
| 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
||
| 6 | this software and associated documentation files (the "Software"), to deal in |
||
| 7 | the Software without restriction, including without limitation the rights to |
||
| 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
||
| 9 | of the Software, and to permit persons to whom the Software is furnished to do |
||
| 10 | so, subject to the following conditions: |
||
| 11 | |||
| 12 | The above copyright notice and this permission notice shall be included in all |
||
| 13 | copies or substantial portions of the Software. |
||
| 14 | |||
| 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
| 21 | SOFTWARE. |
||
| 22 | */ |
||
| 23 | #endregion |
||
| 24 | |||
| 25 | using System; |
||
| 26 | using System.IO; |
||
| 27 | using System.Runtime.InteropServices; |
||
| 28 | using System.Runtime.Serialization; |
||
| 29 | |||
| 30 | namespace OpenTK |
||
| 31 | { |
||
| 32 | |||
| 33 | /// <summary>2-component Vector of the Half type. Occupies 4 Byte total.</summary> |
||
| 34 | [Serializable, StructLayout(LayoutKind.Sequential)] |
||
| 35 | public struct Vector2h : ISerializable, IEquatable<Vector2h> |
||
| 36 | { |
||
| 37 | #region Fields |
||
| 38 | |||
| 39 | /// <summary>The X component of the Half2.</summary> |
||
| 40 | public Half X; |
||
| 41 | |||
| 42 | /// <summary>The Y component of the Half2.</summary> |
||
| 43 | public Half Y; |
||
| 44 | |||
| 45 | #endregion |
||
| 46 | |||
| 47 | #region Constructors |
||
| 48 | |||
| 49 | /// <summary> |
||
| 50 | /// The new Half2 instance will avoid conversion and copy directly from the Half parameters. |
||
| 51 | /// </summary> |
||
| 52 | /// <param name="x">An Half instance of a 16-bit half-precision floating-point number.</param> |
||
| 53 | /// <param name="y">An Half instance of a 16-bit half-precision floating-point number.</param> |
||
| 54 | public Vector2h(Half x, Half y) |
||
| 55 | { |
||
| 56 | X = x; |
||
| 57 | Y = y; |
||
| 58 | } |
||
| 59 | |||
| 60 | /// <summary> |
||
| 61 | /// The new Half2 instance will convert the 2 parameters into 16-bit half-precision floating-point. |
||
| 62 | /// </summary> |
||
| 63 | /// <param name="x">32-bit single-precision floating-point number.</param> |
||
| 64 | /// <param name="y">32-bit single-precision floating-point number.</param> |
||
| 65 | public Vector2h(Single x, Single y) |
||
| 66 | { |
||
| 67 | X = new Half(x); |
||
| 68 | Y = new Half(y); |
||
| 69 | } |
||
| 70 | |||
| 71 | /// <summary> |
||
| 72 | /// The new Half2 instance will convert the 2 parameters into 16-bit half-precision floating-point. |
||
| 73 | /// </summary> |
||
| 74 | /// <param name="x">32-bit single-precision floating-point number.</param> |
||
| 75 | /// <param name="y">32-bit single-precision floating-point number.</param> |
||
| 76 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 77 | public Vector2h(Single x, Single y, bool throwOnError) |
||
| 78 | { |
||
| 79 | X = new Half(x, throwOnError); |
||
| 80 | Y = new Half(y, throwOnError); |
||
| 81 | } |
||
| 82 | |||
| 83 | /// <summary> |
||
| 84 | /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point. |
||
| 85 | /// </summary> |
||
| 86 | /// <param name="v">OpenTK.Vector2</param> |
||
| 87 | [CLSCompliant(false)] |
||
| 88 | public Vector2h(Vector2 v) |
||
| 89 | { |
||
| 90 | X = new Half(v.X); |
||
| 91 | Y = new Half(v.Y); |
||
| 92 | } |
||
| 93 | |||
| 94 | /// <summary> |
||
| 95 | /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point. |
||
| 96 | /// </summary> |
||
| 97 | /// <param name="v">OpenTK.Vector2</param> |
||
| 98 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 99 | [CLSCompliant(false)] |
||
| 100 | public Vector2h(Vector2 v, bool throwOnError) |
||
| 101 | { |
||
| 102 | X = new Half(v.X, throwOnError); |
||
| 103 | Y = new Half(v.Y, throwOnError); |
||
| 104 | } |
||
| 105 | |||
| 106 | /// <summary> |
||
| 107 | /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point. |
||
| 108 | /// This is the fastest constructor. |
||
| 109 | /// </summary> |
||
| 110 | /// <param name="v">OpenTK.Vector2</param> |
||
| 111 | public Vector2h(ref Vector2 v) |
||
| 112 | { |
||
| 113 | X = new Half(v.X); |
||
| 114 | Y = new Half(v.Y); |
||
| 115 | } |
||
| 116 | |||
| 117 | /// <summary> |
||
| 118 | /// The new Half2 instance will convert the Vector2 into 16-bit half-precision floating-point. |
||
| 119 | /// </summary> |
||
| 120 | /// <param name="v">OpenTK.Vector2</param> |
||
| 121 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 122 | public Vector2h(ref Vector2 v, bool throwOnError) |
||
| 123 | { |
||
| 124 | X = new Half(v.X, throwOnError); |
||
| 125 | Y = new Half(v.Y, throwOnError); |
||
| 126 | } |
||
| 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 | |||
| 138 | /// <summary> |
||
| 139 | /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point. |
||
| 140 | /// </summary> |
||
| 141 | /// <param name="v">OpenTK.Vector2d</param> |
||
| 142 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 143 | public Vector2h(Vector2d v, bool throwOnError) |
||
| 144 | { |
||
| 145 | X = new Half(v.X, throwOnError); |
||
| 146 | Y = new Half(v.Y, throwOnError); |
||
| 147 | } |
||
| 148 | |||
| 149 | /// <summary> |
||
| 150 | /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point. |
||
| 151 | /// This is the faster constructor. |
||
| 152 | /// </summary> |
||
| 153 | /// <param name="v">OpenTK.Vector2d</param> |
||
| 154 | [CLSCompliant(false)] |
||
| 155 | public Vector2h(ref Vector2d v) |
||
| 156 | { |
||
| 157 | X = new Half(v.X); |
||
| 158 | Y = new Half(v.Y); |
||
| 159 | } |
||
| 160 | |||
| 161 | /// <summary> |
||
| 162 | /// The new Half2 instance will convert the Vector2d into 16-bit half-precision floating-point. |
||
| 163 | /// </summary> |
||
| 164 | /// <param name="v">OpenTK.Vector2d</param> |
||
| 165 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 166 | [CLSCompliant(false)] |
||
| 167 | public Vector2h(ref Vector2d v, bool throwOnError) |
||
| 168 | { |
||
| 169 | X = new Half(v.X, throwOnError); |
||
| 170 | Y = new Half(v.Y, throwOnError); |
||
| 171 | } |
||
| 172 | |||
| 173 | #endregion Constructors |
||
| 174 | |||
| 175 | #region Half -> Single |
||
| 176 | |||
| 177 | /// <summary> |
||
| 178 | /// Returns this Half2 instance's contents as Vector2. |
||
| 179 | /// </summary> |
||
| 180 | /// <returns>OpenTK.Vector2</returns> |
||
| 181 | public Vector2 ToVector2() |
||
| 182 | { |
||
| 183 | return new Vector2(X, Y); |
||
| 184 | } |
||
| 185 | |||
| 186 | /// <summary> |
||
| 187 | /// Returns this Half2 instance's contents as Vector2d. |
||
| 188 | /// </summary> |
||
| 189 | public Vector2d ToVector2d() |
||
| 190 | { |
||
| 191 | return new Vector2d(X, Y); |
||
| 192 | } |
||
| 193 | |||
| 194 | #endregion Half -> Single |
||
| 195 | |||
| 196 | #region Conversions |
||
| 197 | |||
| 198 | /// <summary>Converts OpenTK.Vector2 to OpenTK.Half2.</summary> |
||
| 199 | /// <param name="v">The Vector2 to convert.</param> |
||
| 200 | /// <returns>The resulting Half vector.</returns> |
||
| 201 | public static explicit operator Vector2h(Vector2 v) |
||
| 202 | { |
||
| 203 | return new Vector2h(v); |
||
| 204 | } |
||
| 205 | |||
| 206 | /// <summary>Converts OpenTK.Vector2d to OpenTK.Half2.</summary> |
||
| 207 | /// <param name="v">The Vector2d to convert.</param> |
||
| 208 | /// <returns>The resulting Half vector.</returns> |
||
| 209 | public static explicit operator Vector2h(Vector2d v) |
||
| 210 | { |
||
| 211 | return new Vector2h(v); |
||
| 212 | } |
||
| 213 | |||
| 214 | /// <summary>Converts OpenTK.Half2 to OpenTK.Vector2.</summary> |
||
| 215 | /// <param name="h">The Half2 to convert.</param> |
||
| 216 | /// <returns>The resulting Vector2.</returns> |
||
| 217 | public static explicit operator Vector2(Vector2h h) |
||
| 218 | { |
||
| 219 | return new Vector2(h.X, h.Y); |
||
| 220 | } |
||
| 221 | |||
| 222 | /// <summary>Converts OpenTK.Half2 to OpenTK.Vector2d.</summary> |
||
| 223 | /// <param name="h">The Half2 to convert.</param> |
||
| 224 | /// <returns>The resulting Vector2d.</returns> |
||
| 225 | public static explicit operator Vector2d(Vector2h h) |
||
| 226 | { |
||
| 227 | return new Vector2d(h.X, h.Y); |
||
| 228 | } |
||
| 229 | |||
| 230 | #endregion Conversions |
||
| 231 | |||
| 232 | #region Constants |
||
| 233 | |||
| 234 | /// <summary>The size in bytes for an instance of the Half2 struct is 4.</summary> |
||
| 235 | public static readonly int SizeInBytes = 4; |
||
| 236 | |||
| 237 | #endregion Constants |
||
| 238 | |||
| 239 | #region ISerializable |
||
| 240 | |||
| 241 | /// <summary>Constructor used by ISerializable to deserialize the object.</summary> |
||
| 242 | /// <param name="info"></param> |
||
| 243 | /// <param name="context"></param> |
||
| 244 | public Vector2h(SerializationInfo info, StreamingContext context) |
||
| 245 | { |
||
| 246 | this.X = (Half)info.GetValue("X", typeof(Half)); |
||
| 247 | this.Y = (Half)info.GetValue("Y", typeof(Half)); |
||
| 248 | } |
||
| 249 | |||
| 250 | /// <summary>Used by ISerialize to serialize the object.</summary> |
||
| 251 | /// <param name="info"></param> |
||
| 252 | /// <param name="context"></param> |
||
| 253 | public void GetObjectData(SerializationInfo info, StreamingContext context) |
||
| 254 | { |
||
| 255 | info.AddValue("X", this.X); |
||
| 256 | info.AddValue("Y", this.Y); |
||
| 257 | } |
||
| 258 | |||
| 259 | #endregion ISerializable |
||
| 260 | |||
| 261 | #region Binary dump |
||
| 262 | |||
| 263 | /// <summary>Updates the X and Y components of this instance by reading from a Stream.</summary> |
||
| 264 | /// <param name="bin">A BinaryReader instance associated with an open Stream.</param> |
||
| 265 | public void FromBinaryStream(BinaryReader bin) |
||
| 266 | { |
||
| 267 | X.FromBinaryStream(bin); |
||
| 268 | Y.FromBinaryStream(bin); |
||
| 269 | } |
||
| 270 | |||
| 271 | /// <summary>Writes the X and Y components of this instance into a Stream.</summary> |
||
| 272 | /// <param name="bin">A BinaryWriter instance associated with an open Stream.</param> |
||
| 273 | public void ToBinaryStream(BinaryWriter bin) |
||
| 274 | { |
||
| 275 | X.ToBinaryStream(bin); |
||
| 276 | Y.ToBinaryStream(bin); |
||
| 277 | } |
||
| 278 | |||
| 279 | #endregion Binary dump |
||
| 280 | |||
| 281 | #region IEquatable<Half2> Members |
||
| 282 | |||
| 283 | /// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Half2 vector.</summary> |
||
| 284 | /// <param name="other">OpenTK.Half2 to compare to this instance..</param> |
||
| 285 | /// <returns>True, if other is equal to this instance; false otherwise.</returns> |
||
| 286 | public bool Equals(Vector2h other) |
||
| 287 | { |
||
| 288 | return (this.X.Equals(other.X) && this.Y.Equals(other.Y)); |
||
| 289 | } |
||
| 290 | |||
| 291 | #endregion |
||
| 292 | |||
| 293 | #region ToString() |
||
| 294 | |||
| 295 | /// <summary>Returns a string that contains this Half2's numbers in human-legible form.</summary> |
||
| 296 | public override string ToString() |
||
| 297 | { |
||
| 298 | return String.Format("({0}, {1})", X.ToString(), Y.ToString()); |
||
| 299 | } |
||
| 300 | |||
| 301 | #endregion ToString() |
||
| 302 | |||
| 303 | #region BitConverter |
||
| 304 | |||
| 305 | /// <summary>Returns the Half2 as an array of bytes.</summary> |
||
| 306 | /// <param name="h">The Half2 to convert.</param> |
||
| 307 | /// <returns>The input as byte array.</returns> |
||
| 308 | public static byte[] GetBytes(Vector2h h) |
||
| 309 | { |
||
| 310 | byte[] result = new byte[SizeInBytes]; |
||
| 311 | |||
| 312 | byte[] temp = Half.GetBytes(h.X); |
||
| 313 | result[0] = temp[0]; |
||
| 314 | result[1] = temp[1]; |
||
| 315 | temp = Half.GetBytes(h.Y); |
||
| 316 | result[2] = temp[0]; |
||
| 317 | result[3] = temp[1]; |
||
| 318 | |||
| 319 | return result; |
||
| 320 | } |
||
| 321 | |||
| 322 | /// <summary>Converts an array of bytes into Half2.</summary> |
||
| 323 | /// <param name="value">A Half2 in it's byte[] representation.</param> |
||
| 324 | /// <param name="startIndex">The starting position within value.</param> |
||
| 325 | /// <returns>A new Half2 instance.</returns> |
||
| 326 | public static Vector2h FromBytes(byte[] value, int startIndex) |
||
| 327 | { |
||
| 328 | Vector2h h2 = new Vector2h(); |
||
| 329 | h2.X = Half.FromBytes(value, startIndex); |
||
| 330 | h2.Y = Half.FromBytes(value, startIndex + 2); |
||
| 331 | return h2; |
||
| 332 | } |
||
| 333 | |||
| 334 | #endregion BitConverter |
||
| 335 | } |
||
| 336 | } |