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
00030 namespace OpenTK
00031 {
00032
00034 [Serializable, StructLayout(LayoutKind.Sequential)]
00035 public struct Vector2h : ISerializable, IEquatable<Vector2h>
00036 {
00037 #region Fields
00038
00040 public Half X;
00041
00043 public Half Y;
00044
00045 #endregion
00046
00047 #region Constructors
00048
00054 public Vector2h(Half x, Half y)
00055 {
00056 X = x;
00057 Y = y;
00058 }
00059
00065 public Vector2h(Single x, Single y)
00066 {
00067 X = new Half(x);
00068 Y = new Half(y);
00069 }
00070
00077 public Vector2h(Single x, Single y, bool throwOnError)
00078 {
00079 X = new Half(x, throwOnError);
00080 Y = new Half(y, throwOnError);
00081 }
00082
00087 [CLSCompliant(false)]
00088 public Vector2h(Vector2 v)
00089 {
00090 X = new Half(v.X);
00091 Y = new Half(v.Y);
00092 }
00093
00099 [CLSCompliant(false)]
00100 public Vector2h(Vector2 v, bool throwOnError)
00101 {
00102 X = new Half(v.X, throwOnError);
00103 Y = new Half(v.Y, throwOnError);
00104 }
00105
00111 public Vector2h(ref Vector2 v)
00112 {
00113 X = new Half(v.X);
00114 Y = new Half(v.Y);
00115 }
00116
00122 public Vector2h(ref Vector2 v, bool throwOnError)
00123 {
00124 X = new Half(v.X, throwOnError);
00125 Y = new Half(v.Y, throwOnError);
00126 }
00127
00132 public Vector2h(Vector2d v)
00133 {
00134 X = new Half(v.X);
00135 Y = new Half(v.Y);
00136 }
00137
00143 public Vector2h(Vector2d v, bool throwOnError)
00144 {
00145 X = new Half(v.X, throwOnError);
00146 Y = new Half(v.Y, throwOnError);
00147 }
00148
00154 [CLSCompliant(false)]
00155 public Vector2h(ref Vector2d v)
00156 {
00157 X = new Half(v.X);
00158 Y = new Half(v.Y);
00159 }
00160
00166 [CLSCompliant(false)]
00167 public Vector2h(ref Vector2d v, bool throwOnError)
00168 {
00169 X = new Half(v.X, throwOnError);
00170 Y = new Half(v.Y, throwOnError);
00171 }
00172
00173 #endregion Constructors
00174
00175 #region Half -> Single
00176
00181 public Vector2 ToVector2()
00182 {
00183 return new Vector2(X, Y);
00184 }
00185
00189 public Vector2d ToVector2d()
00190 {
00191 return new Vector2d(X, Y);
00192 }
00193
00194 #endregion Half -> Single
00195
00196 #region Conversions
00197
00201 public static explicit operator Vector2h(Vector2 v)
00202 {
00203 return new Vector2h(v);
00204 }
00205
00209 public static explicit operator Vector2h(Vector2d v)
00210 {
00211 return new Vector2h(v);
00212 }
00213
00217 public static explicit operator Vector2(Vector2h h)
00218 {
00219 return new Vector2(h.X, h.Y);
00220 }
00221
00225 public static explicit operator Vector2d(Vector2h h)
00226 {
00227 return new Vector2d(h.X, h.Y);
00228 }
00229
00230 #endregion Conversions
00231
00232 #region Constants
00233
00235 public static readonly int SizeInBytes = 4;
00236
00237 #endregion Constants
00238
00239 #region ISerializable
00240
00244 public Vector2h(SerializationInfo info, StreamingContext context)
00245 {
00246 this.X = (Half)info.GetValue("X", typeof(Half));
00247 this.Y = (Half)info.GetValue("Y", typeof(Half));
00248 }
00249
00253 public void GetObjectData(SerializationInfo info, StreamingContext context)
00254 {
00255 info.AddValue("X", this.X);
00256 info.AddValue("Y", this.Y);
00257 }
00258
00259 #endregion ISerializable
00260
00261 #region Binary dump
00262
00265 public void FromBinaryStream(BinaryReader bin)
00266 {
00267 X.FromBinaryStream(bin);
00268 Y.FromBinaryStream(bin);
00269 }
00270
00273 public void ToBinaryStream(BinaryWriter bin)
00274 {
00275 X.ToBinaryStream(bin);
00276 Y.ToBinaryStream(bin);
00277 }
00278
00279 #endregion Binary dump
00280
00281 #region IEquatable<Half2> Members
00282
00286 public bool Equals(Vector2h other)
00287 {
00288 return (this.X.Equals(other.X) && this.Y.Equals(other.Y));
00289 }
00290
00291 #endregion
00292
00293 #region ToString()
00294
00296 public override string ToString()
00297 {
00298 return String.Format("({0}, {1})", X.ToString(), Y.ToString());
00299 }
00300
00301 #endregion ToString()
00302
00303 #region BitConverter
00304
00308 public static byte[] GetBytes(Vector2h h)
00309 {
00310 byte[] result = new byte[SizeInBytes];
00311
00312 byte[] temp = Half.GetBytes(h.X);
00313 result[0] = temp[0];
00314 result[1] = temp[1];
00315 temp = Half.GetBytes(h.Y);
00316 result[2] = temp[0];
00317 result[3] = temp[1];
00318
00319 return result;
00320 }
00321
00326 public static Vector2h FromBytes(byte[] value, int startIndex)
00327 {
00328 Vector2h h2 = new Vector2h();
00329 h2.X = Half.FromBytes(value, startIndex);
00330 h2.Y = Half.FromBytes(value, startIndex + 2);
00331 return h2;
00332 }
00333
00334 #endregion BitConverter
00335 }
00336 }