Subversion Repositories AndroidProjects

Rev

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