00001 #region --- License ---
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #endregion
00024
00025 using System;
00026 using System.IO;
00027 using System.Runtime.InteropServices;
00028 using System.Runtime.Serialization;
00029 using System.Xml.Serialization;
00030
00031 namespace OpenTK
00032 {
00036 [Serializable, StructLayout(LayoutKind.Sequential)]
00037 public struct Vector4h : ISerializable, IEquatable<Vector4h>
00038 {
00039 #region Public Fields
00040
00042 public Half X;
00043
00045 public Half Y;
00046
00048 public Half Z;
00049
00051 public Half W;
00052
00053 #endregion Public Fields
00054
00055 #region Constructors
00056
00064 public Vector4h(Half x, Half y, Half z, Half w)
00065 {
00066 this.X = x;
00067 this.Y = y;
00068 this.Z = z;
00069 this.W = w;
00070 }
00071
00079 public Vector4h(Single x, Single y, Single z, Single w)
00080 {
00081 X = new Half(x);
00082 Y = new Half(y);
00083 Z = new Half(z);
00084 W = new Half(w);
00085 }
00086
00095 public Vector4h(Single x, Single y, Single z, Single w, bool throwOnError)
00096 {
00097 X = new Half(x, throwOnError);
00098 Y = new Half(y, throwOnError);
00099 Z = new Half(z, throwOnError);
00100 W = new Half(w, throwOnError);
00101 }
00102
00107 [CLSCompliant(false)]
00108 public Vector4h(Vector4 v)
00109 {
00110 X = new Half(v.X);
00111 Y = new Half(v.Y);
00112 Z = new Half(v.Z);
00113 W = new Half(v.W);
00114 }
00115
00121 [CLSCompliant(false)]
00122 public Vector4h(Vector4 v, bool throwOnError)
00123 {
00124 X = new Half(v.X, throwOnError);
00125 Y = new Half(v.Y, throwOnError);
00126 Z = new Half(v.Z, throwOnError);
00127 W = new Half(v.W, throwOnError);
00128 }
00129
00135 public Vector4h(ref Vector4 v)
00136 {
00137 X = new Half(v.X);
00138 Y = new Half(v.Y);
00139 Z = new Half(v.Z);
00140 W = new Half(v.W);
00141 }
00142
00148 public Vector4h(ref Vector4 v, bool throwOnError)
00149 {
00150 X = new Half(v.X, throwOnError);
00151 Y = new Half(v.Y, throwOnError);
00152 Z = new Half(v.Z, throwOnError);
00153 W = new Half(v.W, throwOnError);
00154 }
00155
00160 public Vector4h(Vector4d v)
00161 {
00162 X = new Half(v.X);
00163 Y = new Half(v.Y);
00164 Z = new Half(v.Z);
00165 W = new Half(v.W);
00166 }
00167
00173 public Vector4h(Vector4d v, bool throwOnError)
00174 {
00175 X = new Half(v.X, throwOnError);
00176 Y = new Half(v.Y, throwOnError);
00177 Z = new Half(v.Z, throwOnError);
00178 W = new Half(v.W, throwOnError);
00179 }
00180
00186 [CLSCompliant(false)]
00187 public Vector4h(ref Vector4d v)
00188 {
00189 X = new Half(v.X);
00190 Y = new Half(v.Y);
00191 Z = new Half(v.Z);
00192 W = new Half(v.W);
00193 }
00194
00200 [CLSCompliant(false)]
00201 public Vector4h(ref Vector4d v, bool throwOnError)
00202 {
00203 X = new Half(v.X, throwOnError);
00204 Y = new Half(v.Y, throwOnError);
00205 Z = new Half(v.Z, throwOnError);
00206 W = new Half(v.W, throwOnError);
00207 }
00208
00209 #endregion Constructors
00210
00211 #region Swizzle
00212
00216 [XmlIgnore]
00217 public Vector2h Xy { get { return new Vector2h(X, Y); } set { X = value.X; Y = value.Y; } }
00218
00222 [XmlIgnore]
00223 public Vector3h Xyz { get { return new Vector3h(X, Y, Z); } set { X = value.X; Y = value.Y; Z = value.Z; } }
00224
00225 #endregion
00226
00227 #region Half -> Single
00228
00233 public Vector4 ToVector4()
00234 {
00235 return new Vector4(X, Y, Z, W);
00236 }
00237
00241 public Vector4d ToVector4d()
00242 {
00243 return new Vector4d(X, Y, Z, W);
00244 }
00245
00246 #endregion Half -> Single
00247
00248 #region Conversions
00249
00253 public static explicit operator Vector4h(Vector4 v4f)
00254 {
00255 return new Vector4h(v4f);
00256 }
00257
00261 public static explicit operator Vector4h(Vector4d v4d)
00262 {
00263 return new Vector4h(v4d);
00264 }
00265
00269 public static explicit operator Vector4(Vector4h h4)
00270 {
00271 Vector4 result = new Vector4();
00272 result.X = h4.X.ToSingle();
00273 result.Y = h4.Y.ToSingle();
00274 result.Z = h4.Z.ToSingle();
00275 result.W = h4.W.ToSingle();
00276 return result;
00277 }
00278
00282 public static explicit operator Vector4d(Vector4h h4)
00283 {
00284 Vector4d result = new Vector4d();
00285 result.X = h4.X.ToSingle();
00286 result.Y = h4.Y.ToSingle();
00287 result.Z = h4.Z.ToSingle();
00288 result.W = h4.W.ToSingle();
00289 return result;
00290 }
00291
00292 #endregion Conversions
00293
00294 #region Constants
00295
00297 public static readonly int SizeInBytes = 8;
00298
00299 #endregion Constants
00300
00301 #region ISerializable
00302
00306 public Vector4h(SerializationInfo info, StreamingContext context)
00307 {
00308 this.X = (Half)info.GetValue("X", typeof(Half));
00309 this.Y = (Half)info.GetValue("Y", typeof(Half));
00310 this.Z = (Half)info.GetValue("Z", typeof(Half));
00311 this.W = (Half)info.GetValue("W", typeof(Half));
00312 }
00313
00317 public void GetObjectData(SerializationInfo info, StreamingContext context)
00318 {
00319 info.AddValue("X", this.X);
00320 info.AddValue("Y", this.Y);
00321 info.AddValue("Z", this.Z);
00322 info.AddValue("W", this.W);
00323 }
00324
00325 #endregion ISerializable
00326
00327 #region Binary dump
00328
00331 public void FromBinaryStream(BinaryReader bin)
00332 {
00333 X.FromBinaryStream(bin);
00334 Y.FromBinaryStream(bin);
00335 Z.FromBinaryStream(bin);
00336 W.FromBinaryStream(bin);
00337 }
00338
00341 public void ToBinaryStream(BinaryWriter bin)
00342 {
00343 X.ToBinaryStream(bin);
00344 Y.ToBinaryStream(bin);
00345 Z.ToBinaryStream(bin);
00346 W.ToBinaryStream(bin);
00347 }
00348
00349 #endregion Binary dump
00350
00351 #region IEquatable<Half4> Members
00352
00356 public bool Equals(Vector4h other)
00357 {
00358 return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z) && this.W.Equals(other.W));
00359 }
00360
00361 #endregion
00362
00363 #region ToString()
00364
00366 public override string ToString()
00367 {
00368 return String.Format("({0}, {1}, {2}, {3})", X.ToString(), Y.ToString(), Z.ToString(), W.ToString());
00369 }
00370
00371 #endregion ToString()
00372
00373 #region BitConverter
00374
00378 public static byte[] GetBytes(Vector4h h)
00379 {
00380 byte[] result = new byte[SizeInBytes];
00381
00382 byte[] temp = Half.GetBytes(h.X);
00383 result[0] = temp[0];
00384 result[1] = temp[1];
00385 temp = Half.GetBytes(h.Y);
00386 result[2] = temp[0];
00387 result[3] = temp[1];
00388 temp = Half.GetBytes(h.Z);
00389 result[4] = temp[0];
00390 result[5] = temp[1];
00391 temp = Half.GetBytes(h.W);
00392 result[6] = temp[0];
00393 result[7] = temp[1];
00394
00395 return result;
00396 }
00397
00402 public static Vector4h FromBytes(byte[] value, int startIndex)
00403 {
00404 Vector4h h4 = new Vector4h();
00405 h4.X = Half.FromBytes(value, startIndex);
00406 h4.Y = Half.FromBytes(value, startIndex + 2);
00407 h4.Z = Half.FromBytes(value, startIndex + 4);
00408 h4.W = Half.FromBytes(value, startIndex + 6);
00409 return h4;
00410 }
00411
00412 #endregion BitConverter
00413 }
00414 }