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 Vector3h : ISerializable, IEquatable<Vector3h>
00038 {
00039 #region Public Fields
00040
00042 public Half X;
00043
00045 public Half Y;
00046
00048 public Half Z;
00049
00050 #endregion Public Fields
00051
00052 #region Constructors
00053
00060 public Vector3h(Half x, Half y, Half z)
00061 {
00062 this.X = x;
00063 this.Y = y;
00064 this.Z = z;
00065 }
00066
00073 public Vector3h(Single x, Single y, Single z)
00074 {
00075 X = new Half(x);
00076 Y = new Half(y);
00077 Z = new Half(z);
00078 }
00079
00087 public Vector3h(Single x, Single y, Single z, bool throwOnError)
00088 {
00089 X = new Half(x, throwOnError);
00090 Y = new Half(y, throwOnError);
00091 Z = new Half(z, throwOnError);
00092 }
00093
00098 [CLSCompliant(false)]
00099 public Vector3h(Vector3 v)
00100 {
00101 X = new Half(v.X);
00102 Y = new Half(v.Y);
00103 Z = new Half(v.Z);
00104 }
00105
00111 [CLSCompliant(false)]
00112 public Vector3h(Vector3 v, bool throwOnError)
00113 {
00114 X = new Half(v.X, throwOnError);
00115 Y = new Half(v.Y, throwOnError);
00116 Z = new Half(v.Z, throwOnError);
00117 }
00118
00124 public Vector3h(ref Vector3 v)
00125 {
00126 X = new Half(v.X);
00127 Y = new Half(v.Y);
00128 Z = new Half(v.Z);
00129 }
00130
00136 public Vector3h(ref Vector3 v, bool throwOnError)
00137 {
00138 X = new Half(v.X, throwOnError);
00139 Y = new Half(v.Y, throwOnError);
00140 Z = new Half(v.Z, throwOnError);
00141 }
00142
00147 public Vector3h(Vector3d v)
00148 {
00149 X = new Half(v.X);
00150 Y = new Half(v.Y);
00151 Z = new Half(v.Z);
00152 }
00153
00159 public Vector3h(Vector3d v, bool throwOnError)
00160 {
00161 X = new Half(v.X, throwOnError);
00162 Y = new Half(v.Y, throwOnError);
00163 Z = new Half(v.Z, throwOnError);
00164 }
00165
00171 [CLSCompliant(false)]
00172 public Vector3h(ref Vector3d v)
00173 {
00174 X = new Half(v.X);
00175 Y = new Half(v.Y);
00176 Z = new Half(v.Z);
00177 }
00178
00184 [CLSCompliant(false)]
00185 public Vector3h(ref Vector3d v, bool throwOnError)
00186 {
00187 X = new Half(v.X, throwOnError);
00188 Y = new Half(v.Y, throwOnError);
00189 Z = new Half(v.Z, throwOnError);
00190 }
00191
00192 #endregion Constructors
00193
00194 #region Swizzle
00195
00199 [XmlIgnore]
00200 public Vector2h Xy { get { return new Vector2h(X, Y); } set { X = value.X; Y = value.Y; } }
00201
00202 #endregion
00203
00204 #region Half -> Single
00205
00210 public Vector3 ToVector3()
00211 {
00212 return new Vector3(X, Y, Z);
00213 }
00214
00218 public Vector3d ToVector3d()
00219 {
00220 return new Vector3d(X, Y, Z);
00221 }
00222
00223 #endregion Half -> Single
00224
00225 #region Conversions
00226
00230 public static explicit operator Vector3h(Vector3 v3f)
00231 {
00232 return new Vector3h(v3f);
00233 }
00234
00238 public static explicit operator Vector3h(Vector3d v3d)
00239 {
00240 return new Vector3h(v3d);
00241 }
00242
00246 public static explicit operator Vector3(Vector3h h3)
00247 {
00248 Vector3 result = new Vector3();
00249 result.X = h3.X.ToSingle();
00250 result.Y = h3.Y.ToSingle();
00251 result.Z = h3.Z.ToSingle();
00252 return result;
00253 }
00254
00258 public static explicit operator Vector3d(Vector3h h3)
00259 {
00260 Vector3d result = new Vector3d();
00261 result.X = h3.X.ToSingle();
00262 result.Y = h3.Y.ToSingle();
00263 result.Z = h3.Z.ToSingle();
00264 return result;
00265 }
00266
00267 #endregion Conversions
00268
00269 #region Constants
00270
00272 public static readonly int SizeInBytes = 6;
00273
00274 #endregion Constants
00275
00276 #region ISerializable
00277
00281 public Vector3h(SerializationInfo info, StreamingContext context)
00282 {
00283 this.X = (Half)info.GetValue("X", typeof(Half));
00284 this.Y = (Half)info.GetValue("Y", typeof(Half));
00285 this.Z = (Half)info.GetValue("Z", typeof(Half));
00286 }
00287
00291 public void GetObjectData(SerializationInfo info, StreamingContext context)
00292 {
00293 info.AddValue("X", this.X);
00294 info.AddValue("Y", this.Y);
00295 info.AddValue("Z", this.Z);
00296 }
00297
00298 #endregion ISerializable
00299
00300 #region Binary dump
00301
00304 public void FromBinaryStream(BinaryReader bin)
00305 {
00306 X.FromBinaryStream(bin);
00307 Y.FromBinaryStream(bin);
00308 Z.FromBinaryStream(bin);
00309 }
00310
00313 public void ToBinaryStream(BinaryWriter bin)
00314 {
00315 X.ToBinaryStream(bin);
00316 Y.ToBinaryStream(bin);
00317 Z.ToBinaryStream(bin);
00318 }
00319
00320 #endregion Binary dump
00321
00322 #region IEquatable<Half3> Members
00323
00327 public bool Equals(Vector3h other)
00328 {
00329 return (this.X.Equals(other.X) && this.Y.Equals(other.Y) && this.Z.Equals(other.Z));
00330 }
00331
00332 #endregion
00333
00334 #region ToString()
00335
00337 public override string ToString()
00338 {
00339 return String.Format("({0}, {1}, {2})", X.ToString(), Y.ToString(), Z.ToString());
00340 }
00341
00342 #endregion ToString()
00343
00344 #region BitConverter
00345
00349 public static byte[] GetBytes(Vector3h h)
00350 {
00351 byte[] result = new byte[SizeInBytes];
00352
00353 byte[] temp = Half.GetBytes(h.X);
00354 result[0] = temp[0];
00355 result[1] = temp[1];
00356 temp = Half.GetBytes(h.Y);
00357 result[2] = temp[0];
00358 result[3] = temp[1];
00359 temp = Half.GetBytes(h.Z);
00360 result[4] = temp[0];
00361 result[5] = temp[1];
00362
00363 return result;
00364 }
00365
00370 public static Vector3h FromBytes(byte[] value, int startIndex)
00371 {
00372 Vector3h h3 = new Vector3h();
00373 h3.X = Half.FromBytes(value, startIndex);
00374 h3.Y = Half.FromBytes(value, startIndex + 2);
00375 h3.Z = Half.FromBytes(value, startIndex + 4);
00376 return h3;
00377 }
00378
00379 #endregion BitConverter
00380 }
00381 }