forked from andrewkirillov/AForge.NET
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathVector4.cs
623 lines (575 loc) · 21.7 KB
/
Vector4.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// AForge Math Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2007-2011
// contacts@aforgenet.com
//
namespace AForge.Math
{
using System;
/// <summary>
/// 4D Vector structure with X, Y, Z and W coordinates.
/// </summary>
///
/// <remarks><para>The structure incapsulates X, Y, Z and W coordinates of a 4D vector and
/// provides some operations with it.</para></remarks>
///
[Serializable]
public struct Vector4
{
/// <summary>
/// X coordinate of the vector.
/// </summary>
public float X;
/// <summary>
/// Y coordinate of the vector.
/// </summary>
public float Y;
/// <summary>
/// Z coordinate of the vector.
/// </summary>
public float Z;
/// <summary>
/// W coordinate of the vector.
/// </summary>
public float W;
/// <summary>
/// Returns maximum value of the vector.
/// </summary>
///
/// <remarks><para>Returns maximum value of all 4 vector's coordinates.</para></remarks>
///
public float Max
{
get
{
float v1 = ( X > Y ) ? X : Y;
float v2 = ( Z > W ) ? Z : W;
return ( v1 > v2 ) ? v1 : v2;
}
}
/// <summary>
/// Returns minimum value of the vector.
/// </summary>
///
/// <remarks><para>Returns minimum value of all 4 vector's coordinates.</para></remarks>
///
public float Min
{
get
{
float v1 = ( X < Y ) ? X : Y;
float v2 = ( Z < W ) ? Z : W;
return ( v1 < v2 ) ? v1 : v2;
}
}
/// <summary>
/// Returns index of the coordinate with maximum value.
/// </summary>
///
/// <remarks><para>Returns index of the coordinate, which has the maximum value - 0 for X,
/// 1 for Y, 2 for Z or 3 for W.</para>
///
/// <para><note>If there are multiple coordinates which have the same maximum value, the
/// property returns smallest index.</note></para>
/// </remarks>
///
public int MaxIndex
{
get
{
float v1 = 0;
float v2 = 0;
int i1 = 0;
int i2 = 0;
if ( X >= Y )
{
v1 = X;
i1 = 0;
}
else
{
v1 = Y;
i1 = 1;
}
if ( Z >= W )
{
v2 = Z;
i2 = 2;
}
else
{
v2 = W;
i2 = 3;
}
return ( v1 >= v2 ) ? i1 : i2;
}
}
/// <summary>
/// Returns index of the coordinate with minimum value.
/// </summary>
///
/// <remarks><para>Returns index of the coordinate, which has the minimum value - 0 for X,
/// 1 for Y, 2 for Z or 3 for W.</para>
///
/// <para><note>If there are multiple coordinates which have the same minimum value, the
/// property returns smallest index.</note></para>
/// </remarks>
///
public int MinIndex
{
get
{
float v1 = 0;
float v2 = 0;
int i1 = 0;
int i2 = 0;
if ( X <= Y )
{
v1 = X;
i1 = 0;
}
else
{
v1 = Y;
i1 = 1;
}
if ( Z <= W )
{
v2 = Z;
i2 = 2;
}
else
{
v2 = W;
i2 = 3;
}
return ( v1 <= v2 ) ? i1 : i2;
}
}
/// <summary>
/// Returns vector's norm.
/// </summary>
///
/// <remarks><para>Returns Euclidean norm of the vector, which is a
/// square root of the sum: X<sup>2</sup>+Y<sup>2</sup>+Z<sup>2</sup>+W<sup>2</sup>.</para>
/// </remarks>
///
public float Norm
{
get { return (float) System.Math.Sqrt( X * X + Y * Y + Z * Z + W * W ); }
}
/// <summary>
/// Returns square of the vector's norm.
/// </summary>
///
/// <remarks><para>Return X<sup>2</sup>+Y<sup>2</sup>+Z<sup>2</sup>+W<sup>2</sup>, which is
/// a square of <see cref="Norm">vector's norm</see> or a <see cref="Dot">dot product</see> of this vector
/// with itself.</para></remarks>
///
public float Square
{
get { return X * X + Y * Y + Z * Z + W * W; }
}
/// <summary>
/// Initializes a new instance of the <see cref="Vector4"/> structure.
/// </summary>
///
/// <param name="x">X coordinate of the vector.</param>
/// <param name="y">Y coordinate of the vector.</param>
/// <param name="z">Z coordinate of the vector.</param>
/// <param name="w">W coordinate of the vector.</param>
///
public Vector4( float x, float y, float z, float w )
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref="Vector4"/> structure.
/// </summary>
///
/// <param name="value">Value, which is set to all 4 coordinates of the vector.</param>
///
public Vector4( float value )
{
X = Y = Z = W = value;
}
/// <summary>
/// Returns a string representation of this object.
/// </summary>
///
/// <returns>A string representation of this object.</returns>
///
public override string ToString( )
{
return string.Format( System.Globalization.CultureInfo.InvariantCulture,
"{0}, {1}, {2}, {3}", X, Y, Z, W );
}
/// <summary>
/// Returns array representation of the vector.
/// </summary>
///
/// <returns>Array with 4 values containing X/Y/Z/W coordinates.</returns>
///
public float[] ToArray( )
{
return new float[4] { X, Y, Z, W };
}
/// <summary>
/// Adds corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The vector to add to.</param>
/// <param name="vector2">The vector to add to the first vector.</param>
///
/// <returns>Returns a vector which coordinates are equal to sum of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 operator +( Vector4 vector1, Vector4 vector2 )
{
return new Vector4( vector1.X + vector2.X, vector1.Y + vector2.Y,
vector1.Z + vector2.Z, vector1.W + vector2.W );
}
/// <summary>
/// Adds corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The vector to add to.</param>
/// <param name="vector2">The vector to add to the first vector.</param>
///
/// <returns>Returns a vector which coordinates are equal to sum of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 Add( Vector4 vector1, Vector4 vector2 )
{
return vector1 + vector2;
}
/// <summary>
/// Adds a value to all coordinates of the specified vector.
/// </summary>
///
/// <param name="vector">Vector to add the specified value to.</param>
/// <param name="value">Value to add to all coordinates of the vector.</param>
///
/// <returns>Returns new vector with all coordinates increased by the specified value.</returns>
///
public static Vector4 operator +( Vector4 vector, float value )
{
return new Vector4( vector.X + value, vector.Y + value, vector.Z + value, vector.W + value );
}
/// <summary>
/// Adds a value to all coordinates of the specified vector.
/// </summary>
///
/// <param name="vector">Vector to add the specified value to.</param>
/// <param name="value">Value to add to all coordinates of the vector.</param>
///
/// <returns>Returns new vector with all coordinates increased by the specified value.</returns>
///
public static Vector4 Add( Vector4 vector, float value )
{
return vector + value;
}
/// <summary>
/// Subtracts corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The vector to subtract from.</param>
/// <param name="vector2">The vector to subtract from the first vector.</param>
///
/// <returns>Returns a vector which coordinates are equal to difference of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 operator -( Vector4 vector1, Vector4 vector2 )
{
return new Vector4( vector1.X - vector2.X, vector1.Y - vector2.Y,
vector1.Z - vector2.Z, vector1.W - vector2.W );
}
/// <summary>
/// Subtracts corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The vector to subtract from.</param>
/// <param name="vector2">The vector to subtract from the first vector.</param>
///
/// <returns>Returns a vector which coordinates are equal to difference of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 Subtract( Vector4 vector1, Vector4 vector2 )
{
return vector1 - vector2;
}
/// <summary>
/// Subtracts a value from all coordinates of the specified vector.
/// </summary>
///
/// <param name="vector">Vector to subtract the specified value from.</param>
/// <param name="value">Value to subtract from all coordinates of the vector.</param>
///
/// <returns>Returns new vector with all coordinates decreased by the specified value.</returns>
///
public static Vector4 operator -( Vector4 vector, float value )
{
return new Vector4( vector.X - value, vector.Y - value, vector.Z - value, vector.W - value );
}
/// <summary>
/// Subtracts a value from all coordinates of the specified vector.
/// </summary>
///
/// <param name="vector">Vector to subtract the specified value from.</param>
/// <param name="value">Value to subtract from all coordinates of the vector.</param>
///
/// <returns>Returns new vector with all coordinates decreased by the specified value.</returns>
///
public static Vector4 Subtract( Vector4 vector, float value )
{
return vector - value;
}
/// <summary>
/// Multiplies corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The first vector to multiply.</param>
/// <param name="vector2">The second vector to multiply.</param>
///
/// <returns>Returns a vector which coordinates are equal to multiplication of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 operator *( Vector4 vector1, Vector4 vector2 )
{
return new Vector4( vector1.X * vector2.X, vector1.Y * vector2.Y,
vector1.Z * vector2.Z, vector1.W * vector2.W );
}
/// <summary>
/// Multiplies corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The first vector to multiply.</param>
/// <param name="vector2">The second vector to multiply.</param>
///
/// <returns>Returns a vector which coordinates are equal to multiplication of corresponding
/// coordinates of the two specified vectors.</returns>
///
public static Vector4 Multiply( Vector4 vector1, Vector4 vector2 )
{
return vector1 * vector2;
}
/// <summary>
/// Multiplies coordinates of the specified vector by the specified factor.
/// </summary>
///
/// <param name="vector">Vector to multiply coordinates of.</param>
/// <param name="factor">Factor to multiple coordinates of the specified vector by.</param>
///
/// <returns>Returns new vector with all coordinates multiplied by the specified factor.</returns>
///
public static Vector4 operator *( Vector4 vector, float factor )
{
return new Vector4( vector.X * factor, vector.Y * factor, vector.Z * factor, vector.W * factor );
}
/// <summary>
/// Multiplies coordinates of the specified vector by the specified factor.
/// </summary>
///
/// <param name="vector">Vector to multiply coordinates of.</param>
/// <param name="factor">Factor to multiple coordinates of the specified vector by.</param>
///
/// <returns>Returns new vector with all coordinates multiplied by the specified factor.</returns>
///
public static Vector4 Multiply( Vector4 vector, float factor )
{
return vector * factor;
}
/// <summary>
/// Divides corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The first vector to divide.</param>
/// <param name="vector2">The second vector to devide.</param>
///
/// <returns>Returns a vector which coordinates are equal to coordinates of the first vector divided by
/// corresponding coordinates of the second vector.</returns>
///
public static Vector4 operator /( Vector4 vector1, Vector4 vector2 )
{
return new Vector4( vector1.X / vector2.X, vector1.Y / vector2.Y,
vector1.Z / vector2.Z, vector1.W / vector2.W );
}
/// <summary>
/// Divides corresponding coordinates of two vectors.
/// </summary>
///
/// <param name="vector1">The first vector to divide.</param>
/// <param name="vector2">The second vector to devide.</param>
///
/// <returns>Returns a vector which coordinates are equal to coordinates of the first vector divided by
/// corresponding coordinates of the second vector.</returns>
///
public static Vector4 Divide( Vector4 vector1, Vector4 vector2 )
{
return vector1 / vector2;
}
/// <summary>
/// Divides coordinates of the specified vector by the specified factor.
/// </summary>
///
/// <param name="vector">Vector to divide coordinates of.</param>
/// <param name="factor">Factor to divide coordinates of the specified vector by.</param>
///
/// <returns>Returns new vector with all coordinates divided by the specified factor.</returns>
///
public static Vector4 operator /( Vector4 vector, float factor )
{
return new Vector4( vector.X / factor, vector.Y / factor, vector.Z / factor, vector.W / factor );
}
/// <summary>
/// Divides coordinates of the specified vector by the specified factor.
/// </summary>
///
/// <param name="vector">Vector to divide coordinates of.</param>
/// <param name="factor">Factor to divide coordinates of the specified vector by.</param>
///
/// <returns>Returns new vector with all coordinates divided by the specified factor.</returns>
///
public static Vector4 Divide( Vector4 vector, float factor )
{
return vector / factor;
}
/// <summary>
/// Tests whether two specified vectors are equal.
/// </summary>
///
/// <param name="vector1">The left-hand vector.</param>
/// <param name="vector2">The right-hand vector.</param>
///
/// <returns>Returns <see langword="true"/> if the two vectors are equal or <see langword="false"/> otherwise.</returns>
///
public static bool operator ==( Vector4 vector1, Vector4 vector2 )
{
return ( ( vector1.X == vector2.X ) && ( vector1.Y == vector2.Y ) &&
( vector1.Z == vector2.Z ) && ( vector1.W == vector2.W ) );
}
/// <summary>
/// Tests whether two specified vectors are not equal.
/// </summary>
///
/// <param name="vector1">The left-hand vector.</param>
/// <param name="vector2">The right-hand vector.</param>
///
/// <returns>Returns <see langword="true"/> if the two vectors are not equal or <see langword="false"/> otherwise.</returns>
///
public static bool operator !=( Vector4 vector1, Vector4 vector2 )
{
return ( ( vector1.X != vector2.X ) || ( vector1.Y != vector2.Y ) ||
( vector1.Z != vector2.Z ) || ( vector1.W != vector2.W ) );
}
/// <summary>
/// Tests whether the vector equals to the specified one.
/// </summary>
///
/// <param name="vector">The vector to test equality with.</param>
///
/// <returns>Returns <see langword="true"/> if the two vectors are equal or <see langword="false"/> otherwise.</returns>
///
public bool Equals( Vector4 vector )
{
return ( ( vector.X == X ) && ( vector.Y == Y ) && ( vector.Z == Z ) && ( vector.W == W ) );
}
/// <summary>
/// Tests whether the vector equals to the specified object.
/// </summary>
///
/// <param name="obj">The object to test equality with.</param>
///
/// <returns>Returns <see langword="true"/> if the vector equals to the specified object or <see langword="false"/> otherwise.</returns>
///
public override bool Equals( Object obj )
{
if ( obj is Vector4 )
{
return Equals( (Vector4) obj );
}
return false;
}
/// <summary>
/// Returns the hashcode for this instance.
/// </summary>
///
/// <returns>A 32-bit signed integer hash code.</returns>
///
public override int GetHashCode( )
{
return X.GetHashCode( ) + Y.GetHashCode( ) + Z.GetHashCode( ) + W.GetHashCode( );
}
/// <summary>
/// Normalizes the vector by dividing it’s all coordinates with the vector's norm.
/// </summary>
///
/// <returns>Returns the value of vectors’ norm before normalization.</returns>
///
public float Normalize( )
{
float norm = (float) System.Math.Sqrt( X * X + Y * Y + Z * Z + W * W );
float invNorm = 1.0f / norm;
X *= invNorm;
Y *= invNorm;
Z *= invNorm;
W *= invNorm;
return norm;
}
/// <summary>
/// Inverse the vector.
/// </summary>
///
/// <returns>Returns a vector with all coordinates equal to 1.0 divided by the value of corresponding coordinate
/// in this vector (or equal to 0.0 if this vector has corresponding coordinate also set to 0.0).</returns>
///
public Vector4 Inverse( )
{
return new Vector4(
( X == 0 ) ? 0 : 1.0f / X,
( Y == 0 ) ? 0 : 1.0f / Y,
( Z == 0 ) ? 0 : 1.0f / Z,
( W == 0 ) ? 0 : 1.0f / W );
}
/// <summary>
/// Calculate absolute values of the vector.
/// </summary>
///
/// <returns>Returns a vector with all coordinates equal to absolute values of this vector's coordinates.</returns>
///
public Vector4 Abs( )
{
return new Vector4( System.Math.Abs( X ), System.Math.Abs( Y ), System.Math.Abs( Z ), System.Math.Abs( W ) );
}
/// <summary>
/// Calculates dot product of two vectors.
/// </summary>
///
/// <param name="vector1">First vector to use for dot product calculation.</param>
/// <param name="vector2">Second vector to use for dot product calculation.</param>
///
/// <returns>Returns dot product of the two specified vectors.</returns>
///
public static float Dot( Vector4 vector1, Vector4 vector2 )
{
return vector1.X * vector2.X + vector1.Y * vector2.Y +
vector1.Z * vector2.Z + vector1.W * vector2.W;
}
/// <summary>
/// Converts the vector to a 3D vector.
/// </summary>
///
/// <returns>Returns 3D vector which has X/Y/Z coordinates equal to X/Y/Z coordinates
/// of this vector divided by <see cref="W"/>.</returns>
///
public Vector3 ToVector3( )
{
return new Vector3( X / W, Y / W, Z / W );
}
}
}