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 | using System.Xml.Serialization; |
||
| 30 | |||
| 31 | namespace OpenTK |
||
| 32 | { |
||
| 33 | /// <summary> |
||
| 34 | /// 3-component Vector of the Half type. Occupies 6 Byte total. |
||
| 35 | /// </summary> |
||
| 36 | [Serializable, StructLayout(LayoutKind.Sequential)] |
||
| 37 | public struct Vector3h : ISerializable, IEquatable<Vector3h> |
||
| 38 | { |
||
| 39 | #region Public Fields |
||
| 40 | |||
| 41 | /// <summary>The X component of the Half3.</summary> |
||
| 42 | public Half X; |
||
| 43 | |||
| 44 | /// <summary>The Y component of the Half3.</summary> |
||
| 45 | public Half Y; |
||
| 46 | |||
| 47 | /// <summary>The Z component of the Half3.</summary> |
||
| 48 | public Half Z; |
||
| 49 | |||
| 50 | #endregion Public Fields |
||
| 51 | |||
| 52 | #region Constructors |
||
| 53 | |||
| 54 | /// <summary> |
||
| 55 | /// The new Half3 instance will avoid conversion and copy directly from the Half parameters. |
||
| 56 | /// </summary> |
||
| 57 | /// <param name="x">An Half instance of a 16-bit half-precision floating-point number.</param> |
||
| 58 | /// <param name="y">An Half instance of a 16-bit half-precision floating-point number.</param> |
||
| 59 | /// <param name="z">An Half instance of a 16-bit half-precision floating-point number.</param> |
||
| 60 | public Vector3h(Half x, Half y, Half z) |
||
| 61 | { |
||
| 62 | this.X = x; |
||
| 63 | this.Y = y; |
||
| 64 | this.Z = z; |
||
| 65 | } |
||
| 66 | |||
| 67 | /// <summary> |
||
| 68 | /// The new Half3 instance will convert the 3 parameters into 16-bit half-precision floating-point. |
||
| 69 | /// </summary> |
||
| 70 | /// <param name="x">32-bit single-precision floating-point number.</param> |
||
| 71 | /// <param name="y">32-bit single-precision floating-point number.</param> |
||
| 72 | /// <param name="z">32-bit single-precision floating-point number.</param> |
||
| 73 | public Vector3h(Single x, Single y, Single z) |
||
| 74 | { |
||
| 75 | X = new Half(x); |
||
| 76 | Y = new Half(y); |
||
| 77 | Z = new Half(z); |
||
| 78 | } |
||
| 79 | |||
| 80 | /// <summary> |
||
| 81 | /// The new Half3 instance will convert the 3 parameters into 16-bit half-precision floating-point. |
||
| 82 | /// </summary> |
||
| 83 | /// <param name="x">32-bit single-precision floating-point number.</param> |
||
| 84 | /// <param name="y">32-bit single-precision floating-point number.</param> |
||
| 85 | /// <param name="z">32-bit single-precision floating-point number.</param> |
||
| 86 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 87 | public Vector3h(Single x, Single y, Single z, bool throwOnError) |
||
| 88 | { |
||
| 89 | X = new Half(x, throwOnError); |
||
| 90 | Y = new Half(y, throwOnError); |
||
| 91 | Z = new Half(z, throwOnError); |
||
| 92 | } |
||
| 93 | |||
| 94 | /// <summary> |
||
| 95 | /// The new Half3 instance will convert the Vector3 into 16-bit half-precision floating-point. |
||
| 96 | /// </summary> |
||
| 97 | /// <param name="v">OpenTK.Vector3</param> |
||
| 98 | [CLSCompliant(false)] |
||
| 99 | public Vector3h(Vector3 v) |
||
| 100 | { |
||
| 101 | X = new Half(v.X); |
||
| 102 | Y = new Half(v.Y); |
||
| 103 | Z = new Half(v.Z); |
||
| 104 | } |
||
| 105 | |||
| 106 | /// <summary> |
||
| 107 | /// The new Half3 instance will convert the Vector3 into 16-bit half-precision floating-point. |
||
| 108 | /// </summary> |
||
| 109 | /// <param name="v">OpenTK.Vector3</param> |
||
| 110 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 111 | [CLSCompliant(false)] |
||
| 112 | public Vector3h(Vector3 v, bool throwOnError) |
||
| 113 | { |
||
| 114 | X = new Half(v.X, throwOnError); |
||
| 115 | Y = new Half(v.Y, throwOnError); |
||
| 116 | Z = new Half(v.Z, throwOnError); |
||
| 117 | } |
||
| 118 | |||
| 119 | /// <summary> |
||
| 120 | /// The new Half3 instance will convert the Vector3 into 16-bit half-precision floating-point. |
||
| 121 | /// This is the fastest constructor. |
||
| 122 | /// </summary> |
||
| 123 | /// <param name="v">OpenTK.Vector3</param> |
||
| 124 | public Vector3h(ref Vector3 v) |
||
| 125 | { |
||
| 126 | X = new Half(v.X); |
||
| 127 | Y = new Half(v.Y); |
||
| 128 | Z = new Half(v.Z); |
||
| 129 | } |
||
| 130 | |||
| 131 | /// <summary> |
||
| 132 | /// The new Half3 instance will convert the Vector3 into 16-bit half-precision floating-point. |
||
| 133 | /// </summary> |
||
| 134 | /// <param name="v">OpenTK.Vector3</param> |
||
| 135 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 136 | public Vector3h(ref Vector3 v, bool throwOnError) |
||
| 137 | { |
||
| 138 | X = new Half(v.X, throwOnError); |
||
| 139 | Y = new Half(v.Y, throwOnError); |
||
| 140 | Z = new Half(v.Z, throwOnError); |
||
| 141 | } |
||
| 142 | |||
| 143 | /// <summary> |
||
| 144 | /// The new Half3 instance will convert the Vector3d into 16-bit half-precision floating-point. |
||
| 145 | /// </summary> |
||
| 146 | /// <param name="v">OpenTK.Vector3d</param> |
||
| 147 | public Vector3h(Vector3d v) |
||
| 148 | { |
||
| 149 | X = new Half(v.X); |
||
| 150 | Y = new Half(v.Y); |
||
| 151 | Z = new Half(v.Z); |
||
| 152 | } |
||
| 153 | |||
| 154 | /// <summary> |
||
| 155 | /// The new Half3 instance will convert the Vector3d into 16-bit half-precision floating-point. |
||
| 156 | /// </summary> |
||
| 157 | /// <param name="v">OpenTK.Vector3d</param> |
||
| 158 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 159 | public Vector3h(Vector3d v, bool throwOnError) |
||
| 160 | { |
||
| 161 | X = new Half(v.X, throwOnError); |
||
| 162 | Y = new Half(v.Y, throwOnError); |
||
| 163 | Z = new Half(v.Z, throwOnError); |
||
| 164 | } |
||
| 165 | |||
| 166 | /// <summary> |
||
| 167 | /// The new Half3 instance will convert the Vector3d into 16-bit half-precision floating-point. |
||
| 168 | /// This is the faster constructor. |
||
| 169 | /// </summary> |
||
| 170 | /// <param name="v">OpenTK.Vector3d</param> |
||
| 171 | [CLSCompliant(false)] |
||
| 172 | public Vector3h(ref Vector3d v) |
||
| 173 | { |
||
| 174 | X = new Half(v.X); |
||
| 175 | Y = new Half(v.Y); |
||
| 176 | Z = new Half(v.Z); |
||
| 177 | } |
||
| 178 | |||
| 179 | /// <summary> |
||
| 180 | /// The new Half3 instance will convert the Vector3d into 16-bit half-precision floating-point. |
||
| 181 | /// </summary> |
||
| 182 | /// <param name="v">OpenTK.Vector3d</param> |
||
| 183 | /// <param name="throwOnError">Enable checks that will throw if the conversion result is not meaningful.</param> |
||
| 184 | [CLSCompliant(false)] |
||
| 185 | public Vector3h(ref Vector3d v, bool throwOnError) |
||
| 186 | { |
||
| 187 | X = new Half(v.X, throwOnError); |
||
| 188 | Y = new Half(v.Y, throwOnError); |
||
| 189 | Z = new Half(v.Z, throwOnError); |
||
| 190 | } |
||
| 191 | |||
| 192 | #endregion Constructors |
||
| 193 | |||
| 194 | #region Swizzle |
||
| 195 | |||
| 196 | /// <summary> |
||
| 197 | /// Gets or sets an OpenTK.Vector2h with the X and Y components of this instance. |
||
| 198 | /// </summary> |
||
| 199 | [XmlIgnore] |
||
| 200 | public Vector2h Xy { get { return new Vector2h(X, Y); } set { X = value.X; Y = value.Y; } } |
||
| 201 | |||
| 202 | #endregion |
||
| 203 | |||
| 204 | #region Half -> Single |
||
| 205 | |||
| 206 | /// <summary> |
||
| 207 | /// Returns this Half3 instance's contents as Vector3. |
||
| 208 | /// </summary> |
||
| 209 | /// <returns>OpenTK.Vector3</returns> |
||
| 210 | public Vector3 ToVector3() |
||
| 211 | { |
||
| 212 | return new Vector3(X, Y, Z); |
||
| 213 | } |
||
| 214 | |||
| 215 | /// <summary> |
||
| 216 | /// Returns this Half3 instance's contents as Vector3d. |
||
| 217 | /// </summary> |
||
| 218 | public Vector3d ToVector3d() |
||
| 219 | { |
||
| 220 | return new Vector3d(X, Y, Z); |
||
| 221 | } |
||
| 222 | |||
| 223 | #endregion Half -> Single |
||
| 224 | |||
| 225 | #region Conversions |
||
| 226 | |||
| 227 | /// <summary>Converts OpenTK.Vector3 to OpenTK.Half3.</summary> |
||
| 228 | /// <param name="v3f">The Vector3 to convert.</param> |
||
| 229 | /// <returns>The resulting Half vector.</returns> |
||
| 230 | public static explicit operator Vector3h(Vector3 v3f) |
||
| 231 | { |
||
| 232 | return new Vector3h(v3f); |
||
| 233 | } |
||
| 234 | |||
| 235 | /// <summary>Converts OpenTK.Vector3d to OpenTK.Half3.</summary> |
||
| 236 | /// <param name="v3d">The Vector3d to convert.</param> |
||
| 237 | /// <returns>The resulting Half vector.</returns> |
||
| 238 | public static explicit operator Vector3h(Vector3d v3d) |
||
| 239 | { |
||
| 240 | return new Vector3h(v3d); |
||
| 241 | } |
||
| 242 | |||
| 243 | /// <summary>Converts OpenTK.Half3 to OpenTK.Vector3.</summary> |
||
| 244 | /// <param name="h3">The Half3 to convert.</param> |
||
| 245 | /// <returns>The resulting Vector3.</returns> |
||
| 246 | public static explicit operator Vector3(Vector3h h3) |
||
| 247 | { |
||
| 248 | Vector3 result = new Vector3(); |
||
| 249 | result.X = h3.X.ToSingle(); |
||
| 250 | result.Y = h3.Y.ToSingle(); |
||
| 251 | result.Z = h3.Z.ToSingle(); |
||
| 252 | return result; |
||
| 253 | } |
||
| 254 | |||
| 255 | /// <summary>Converts OpenTK.Half3 to OpenTK.Vector3d.</summary> |
||
| 256 | /// <param name="h3">The Half3 to convert.</param> |
||
| 257 | /// <returns>The resulting Vector3d.</returns> |
||
| 258 | public static explicit operator Vector3d(Vector3h h3) |
||
| 259 | { |
||
| 260 | Vector3d result = new Vector3d(); |
||
| 261 | result.X = h3.X.ToSingle(); |
||
| 262 | result.Y = h3.Y.ToSingle(); |
||
| 263 | result.Z = h3.Z.ToSingle(); |
||
| 264 | return result; |
||
| 265 | } |
||
| 266 | |||
| 267 | #endregion Conversions |
||
| 268 | |||
| 269 | #region Constants |
||
| 270 | |||
| 271 | /// <summary>The size in bytes for an instance of the Half3 struct is 6.</summary> |
||
| 272 | public static readonly int SizeInBytes = 6; |
||
| 273 | |||
| 274 | #endregion Constants |
||
| 275 | |||
| 276 | #region ISerializable |
||
| 277 | |||
| 278 | /// <summary>Constructor used by ISerializable to deserialize the object.</summary> |
||
| 279 | /// <param name="info"></param> |
||
| 280 | /// <param name="context"></param> |
||
| 281 | public Vector3h(SerializationInfo info, StreamingContext context) |
||
| 282 | { |
||
| 283 | this.X = (Half)info.GetValue("X", typeof(Half)); |
||
| 284 | this.Y = (Half)info.GetValue("Y", typeof(Half)); |
||
| 285 | this.Z = (Half)info.GetValue("Z", typeof(Half)); |
||
| 286 | } |
||
| 287 | |||
| 288 | /// <summary>Used by ISerialize to serialize the object.</summary> |
||
| 289 | /// <param name="info"></param> |
||
| 290 | /// <param name="context"></param> |
||
| 291 | public void GetObjectData(SerializationInfo info, StreamingContext context) |
||
| 292 | { |
||
| 293 | info.AddValue("X", this.X); |
||
| 294 | info.AddValue("Y", this.Y); |
||
| 295 | info.AddValue("Z", this.Z); |
||
| 296 | } |
||
| 297 | |||
| 298 | #endregion ISerializable |
||
| 299 | |||
| 300 | #region Binary dump |
||
| 301 | |||
| 302 | /// <summary>Updates the X,Y and Z components of this instance by reading from a Stream.</summary> |
||
| 303 | /// <param name="bin">A BinaryReader instance associated with an open Stream.</param> |
||
| 304 | public void FromBinaryStream(BinaryReader bin) |
||
| 305 | { |
||
| 306 | X.FromBinaryStream(bin); |
||
| 307 | Y.FromBinaryStream(bin); |
||
| 308 | Z.FromBinaryStream(bin); |
||
| 309 | } |
||
| 310 | |||
| 311 | /// <summary>Writes the X,Y and Z components of this instance into a Stream.</summary> |
||
| 312 | /// <param name="bin">A BinaryWriter instance associated with an open Stream.</param> |
||
| 313 | public void ToBinaryStream(BinaryWriter bin) |
||
| 314 | { |
||
| 315 | X.ToBinaryStream(bin); |
||
| 316 | Y.ToBinaryStream(bin); |
||
| 317 | Z.ToBinaryStream(bin); |
||
| 318 | } |
||
| 319 | |||
| 320 | #endregion Binary dump |
||
| 321 | |||
| 322 | #region IEquatable<Half3> Members |
||
| 323 | |||
| 324 | /// <summary>Returns a value indicating whether this instance is equal to a specified OpenTK.Half3 vector.</summary> |
||
| 325 | /// <param name="other">OpenTK.Half3 to compare to this instance..</param> |
||
| 326 | /// <returns>True, if other is equal to this instance; false otherwise.</returns> |
||
| 327 | public bool Equals(Vector3h other) |
||
| 328 | { |
||
| 329 | return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z)); |
||
| 330 | } |
||
| 331 | |||
| 332 | #endregion |
||
| 333 | |||
| 334 | #region ToString() |
||
| 335 | |||
| 336 | /// <summary>Returns a string that contains this Half3's numbers in human-legible form.</summary> |
||
| 337 | public override string ToString() |
||
| 338 | { |
||
| 339 | return String.Format("({0}, {1}, {2})", X.ToString(), Y.ToString(), Z.ToString()); |
||
| 340 | } |
||
| 341 | |||
| 342 | #endregion ToString() |
||
| 343 | |||
| 344 | #region BitConverter |
||
| 345 | |||
| 346 | /// <summary>Returns the Half3 as an array of bytes.</summary> |
||
| 347 | /// <param name="h">The Half3 to convert.</param> |
||
| 348 | /// <returns>The input as byte array.</returns> |
||
| 349 | public static byte[] GetBytes(Vector3h h) |
||
| 350 | { |
||
| 351 | byte[] result = new byte[SizeInBytes]; |
||
| 352 | |||
| 353 | byte[] temp = Half.GetBytes(h.X); |
||
| 354 | result[0] = temp[0]; |
||
| 355 | result[1] = temp[1]; |
||
| 356 | temp = Half.GetBytes(h.Y); |
||
| 357 | result[2] = temp[0]; |
||
| 358 | result[3] = temp[1]; |
||
| 359 | temp = Half.GetBytes(h.Z); |
||
| 360 | result[4] = temp[0]; |
||
| 361 | result[5] = temp[1]; |
||
| 362 | |||
| 363 | return result; |
||
| 364 | } |
||
| 365 | |||
| 366 | /// <summary>Converts an array of bytes into Half3.</summary> |
||
| 367 | /// <param name="value">A Half3 in it's byte[] representation.</param> |
||
| 368 | /// <param name="startIndex">The starting position within value.</param> |
||
| 369 | /// <returns>A new Half3 instance.</returns> |
||
| 370 | public static Vector3h FromBytes(byte[] value, int startIndex) |
||
| 371 | { |
||
| 372 | Vector3h h3 = new Vector3h(); |
||
| 373 | h3.X = Half.FromBytes(value, startIndex); |
||
| 374 | h3.Y = Half.FromBytes(value, startIndex + 2); |
||
| 375 | h3.Z = Half.FromBytes(value, startIndex + 4); |
||
| 376 | return h3; |
||
| 377 | } |
||
| 378 | |||
| 379 | #endregion BitConverter |
||
| 380 | } |
||
| 381 | } |