forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlane.cs
922 lines (839 loc) · 40.7 KB
/
Plane.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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace TheMaths
{
/// <summary>
/// Represents a plane in three dimensional space.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Plane : IEquatable<Plane>, IFormattable
{
/// <summary>
/// The normal vector of the plane.
/// </summary>
public Vector3 Normal;
/// <summary>
/// The distance of the plane along its normal from the origin.
/// </summary>
public float D;
/// <summary>
/// Initializes a new instance of the <see cref="Plane"/> struct.
/// </summary>
/// <param name="value">The value that will be assigned to all components.</param>
public Plane(float value)
{
Normal.X = Normal.Y = Normal.Z = D = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="Plane"/> struct.
/// </summary>
/// <param name="a">The X component of the normal.</param>
/// <param name="b">The Y component of the normal.</param>
/// <param name="c">The Z component of the normal.</param>
/// <param name="d">The distance of the plane along its normal from the origin.</param>
public Plane(float a, float b, float c, float d)
{
Normal.X = a;
Normal.Y = b;
Normal.Z = c;
D = d;
}
/// <summary>
/// Initializes a new instance of the <see cref="T:SharpDX.Plane" /> class.
/// </summary>
/// <param name="point">Any point that lies along the plane.</param>
/// <param name="normal">The normal vector to the plane.</param>
public Plane(Vector3 point, Vector3 normal)
{
this.Normal = normal;
this.D = -Vector3.Dot(normal, point);
}
/// <summary>
/// Initializes a new instance of the <see cref="Plane"/> struct.
/// </summary>
/// <param name="value">The normal of the plane.</param>
/// <param name="d">The distance of the plane along its normal from the origin</param>
public Plane(Vector3 value, float d)
{
Normal = value;
D = d;
}
/// <summary>
/// Initializes a new instance of the <see cref="Plane"/> struct.
/// </summary>
/// <param name="point1">First point of a triangle defining the plane.</param>
/// <param name="point2">Second point of a triangle defining the plane.</param>
/// <param name="point3">Third point of a triangle defining the plane.</param>
public Plane(Vector3 point1, Vector3 point2, Vector3 point3)
{
float x1 = point2.X - point1.X;
float y1 = point2.Y - point1.Y;
float z1 = point2.Z - point1.Z;
float x2 = point3.X - point1.X;
float y2 = point3.Y - point1.Y;
float z2 = point3.Z - point1.Z;
float yz = (y1 * z2) - (z1 * y2);
float xz = (z1 * x2) - (x1 * z2);
float xy = (x1 * y2) - (y1 * x2);
float invPyth = 1.0f / (float)(Math.Sqrt((yz * yz) + (xz * xz) + (xy * xy)));
Normal.X = yz * invPyth;
Normal.Y = xz * invPyth;
Normal.Z = xy * invPyth;
D = -((Normal.X * point1.X) + (Normal.Y * point1.Y) + (Normal.Z * point1.Z));
}
/// <summary>
/// Initializes a new instance of the <see cref="Plane"/> struct.
/// </summary>
/// <param name="values">The values to assign to the A, B, C, and D components of the plane. This must be an array with four elements.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
public Plane(float[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 4)
throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for Plane.");
Normal.X = values[0];
Normal.Y = values[1];
Normal.Z = values[2];
D = values[3];
}
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the A, B, C, or D component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the A component, 1 for the B component, 2 for the C component, and 3 for the D component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return Normal.X;
case 1: return Normal.Y;
case 2: return Normal.Z;
case 3: return D;
}
throw new ArgumentOutOfRangeException("index", "Indices for Plane run from 0 to 3, inclusive.");
}
set
{
switch (index)
{
case 0: Normal.X = value; break;
case 1: Normal.Y = value; break;
case 2: Normal.Z = value; break;
case 3: D = value; break;
default: throw new ArgumentOutOfRangeException("index", "Indices for Plane run from 0 to 3, inclusive.");
}
}
}
/// <summary>
/// Changes the coefficients of the normal vector of the plane to make it of unit length.
/// </summary>
public void Normalize()
{
float magnitude = 1.0f / (float)(Math.Sqrt((Normal.X * Normal.X) + (Normal.Y * Normal.Y) + (Normal.Z * Normal.Z)));
Normal.X *= magnitude;
Normal.Y *= magnitude;
Normal.Z *= magnitude;
D *= magnitude;
}
/// <summary>
/// Creates an array containing the elements of the plane.
/// </summary>
/// <returns>A four-element array containing the components of the plane.</returns>
public float[] ToArray()
{
return new float[] { Normal.X, Normal.Y, Normal.Z, D };
}
/// <summary>
/// Determines if there is an intersection between the current object and a point.
/// </summary>
/// <param name="point">The point to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public PlaneIntersectionType Intersects(ref Vector3 point)
{
return Collision.PlaneIntersectsPoint(ref this, ref point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Ray"/>.
/// </summary>
/// <param name="ray">The ray to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray)
{
float distance;
return Collision.RayIntersectsPlane(ref ray, ref this, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Ray"/>.
/// </summary>
/// <param name="ray">The ray to test.</param>
/// <param name="distance">When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray, out float distance)
{
return Collision.RayIntersectsPlane(ref ray, ref this, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Ray"/>.
/// </summary>
/// <param name="ray">The ray to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Ray ray, out Vector3 point)
{
return Collision.RayIntersectsPlane(ref ray, ref this, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Plane"/>.
/// </summary>
/// <param name="plane">The plane to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Plane plane)
{
return Collision.PlaneIntersectsPlane(ref this, ref plane);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Plane"/>.
/// </summary>
/// <param name="plane">The plane to test.</param>
/// <param name="line">When the method completes, contains the line of intersection
/// as a <see cref="Ray"/>, or a zero ray if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref Plane plane, out Ray line)
{
return Collision.PlaneIntersectsPlane(ref this, ref plane, out line);
}
/// <summary>
/// Determines if there is an intersection between the current object and a triangle.
/// </summary>
/// <param name="vertex1">The first vertex of the triangle to test.</param>
/// <param name="vertex2">The second vertex of the triangle to test.</param>
/// <param name="vertex3">The third vertex of the triangle to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public PlaneIntersectionType Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
{
return Collision.PlaneIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="BoundingBox"/>.
/// </summary>
/// <param name="box">The box to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public PlaneIntersectionType Intersects(ref BoundingBox box)
{
return Collision.PlaneIntersectsBox(ref this, ref box);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The sphere to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public PlaneIntersectionType Intersects(ref BoundingSphere sphere)
{
return Collision.PlaneIntersectsSphere(ref this, ref sphere);
}
/// <summary>
/// Builds a matrix that can be used to reflect vectors about a plane.
/// </summary>
/// <param name="plane">The plane for which the reflection occurs. This parameter is assumed to be normalized.</param>
/// <param name="result">When the method completes, contains the reflection matrix.</param>
public void Reflection(out Matrix result)
{
float x = this.Normal.X;
float y = this.Normal.Y;
float z = this.Normal.Z;
float x2 = -2.0f * x;
float y2 = -2.0f * y;
float z2 = -2.0f * z;
result.M11 = (x2 * x) + 1.0f;
result.M12 = y2 * x;
result.M13 = z2 * x;
result.M14 = 0.0f;
result.M21 = x2 * y;
result.M22 = (y2 * y) + 1.0f;
result.M23 = z2 * y;
result.M24 = 0.0f;
result.M31 = x2 * z;
result.M32 = y2 * z;
result.M33 = (z2 * z) + 1.0f;
result.M34 = 0.0f;
result.M41 = x2 * this.D;
result.M42 = y2 * this.D;
result.M43 = z2 * this.D;
result.M44 = 1.0f;
}
/// <summary>
/// Builds a matrix that can be used to reflect vectors about a plane.
/// </summary>
/// <returns>The reflection matrix.</returns>
public Matrix Reflection()
{
Matrix result;
Reflection(out result);
return result;
}
/// <summary>
/// Creates a matrix that flattens geometry into a shadow from this the plane onto which to project the geometry as a shadow.
/// This plane is assumed to be normalized
/// </summary>
/// <param name="light">The light direction. If the W component is 0, the light is directional light; if the
/// W component is 1, the light is a point light.</param>
/// <param name="result">When the method completes, contains the shadow matrix.</param>
public void Shadow(ref Vector4 light, out Matrix result)
{
float dot = (this.Normal.X * light.X) + (this.Normal.Y * light.Y) + (this.Normal.Z * light.Z) + (this.D * light.W);
float x = -this.Normal.X;
float y = -this.Normal.Y;
float z = -this.Normal.Z;
float d = -this.D;
result.M11 = (x * light.X) + dot;
result.M21 = y * light.X;
result.M31 = z * light.X;
result.M41 = d * light.X;
result.M12 = x * light.Y;
result.M22 = (y * light.Y) + dot;
result.M32 = z * light.Y;
result.M42 = d * light.Y;
result.M13 = x * light.Z;
result.M23 = y * light.Z;
result.M33 = (z * light.Z) + dot;
result.M43 = d * light.Z;
result.M14 = x * light.W;
result.M24 = y * light.W;
result.M34 = z * light.W;
result.M44 = (d * light.W) + dot;
}
/// <summary>
/// Creates a matrix that flattens geometry into a shadow from this the plane onto which to project the geometry as a shadow.
/// This plane is assumed to be normalized
/// </summary>
/// <param name="light">The light direction. If the W component is 0, the light is directional light; if the
/// W component is 1, the light is a point light.</param>
/// <returns>The shadow matrix.</returns>
public Matrix Shadow(Vector4 light)
{
Matrix result;
Shadow(ref light, out result);
return result;
}
/// <summary>
/// Builds a Matrix3x3 that can be used to reflect vectors about a plane for which the reflection occurs.
/// This plane is assumed to be normalized
/// </summary>
/// <param name="result">When the method completes, contains the reflection Matrix3x3.</param>
public void Reflection(out Matrix3x3 result)
{
float x = this.Normal.X;
float y = this.Normal.Y;
float z = this.Normal.Z;
float x2 = -2.0f * x;
float y2 = -2.0f * y;
float z2 = -2.0f * z;
result.M11 = (x2 * x) + 1.0f;
result.M12 = y2 * x;
result.M13 = z2 * x;
result.M21 = x2 * y;
result.M22 = (y2 * y) + 1.0f;
result.M23 = z2 * y;
result.M31 = x2 * z;
result.M32 = y2 * z;
result.M33 = (z2 * z) + 1.0f;
}
/// <summary>
/// Builds a Matrix3x3 that can be used to reflect vectors about a plane for which the reflection occurs.
/// This plane is assumed to be normalized
/// </summary>
/// <returns>The reflection Matrix3x3.</returns>
public Matrix3x3 Reflection3x3()
{
Matrix3x3 result;
Reflection(out result);
return result;
}
/// <summary>
/// Creates a Matrix3x3 that flattens geometry into a shadow.
/// </summary>
/// <param name="light">The light direction. If the W component is 0, the light is directional light; if the
/// W component is 1, the light is a point light.</param>
/// <param name="plane">The plane onto which to project the geometry as a shadow. This parameter is assumed to be normalized.</param>
/// <param name="result">When the method completes, contains the shadow Matrix3x3.</param>
public static void Shadow(ref Vector4 light, ref Plane plane, out Matrix3x3 result)
{
float dot = (plane.Normal.X * light.X) + (plane.Normal.Y * light.Y) + (plane.Normal.Z * light.Z) + (plane.D * light.W);
float x = -plane.Normal.X;
float y = -plane.Normal.Y;
float z = -plane.Normal.Z;
float d = -plane.D;
result.M11 = (x * light.X) + dot;
result.M21 = y * light.X;
result.M31 = z * light.X;
result.M12 = x * light.Y;
result.M22 = (y * light.Y) + dot;
result.M32 = z * light.Y;
result.M13 = x * light.Z;
result.M23 = y * light.Z;
result.M33 = (z * light.Z) + dot;
}
/// <summary>
/// Creates a Matrix3x3 that flattens geometry into a shadow.
/// </summary>
/// <param name="light">The light direction. If the W component is 0, the light is directional light; if the
/// W component is 1, the light is a point light.</param>
/// <param name="plane">The plane onto which to project the geometry as a shadow. This parameter is assumed to be normalized.</param>
/// <returns>The shadow Matrix3x3.</returns>
public static Matrix3x3 Shadow(Vector4 light, Plane plane)
{
Matrix3x3 result;
Shadow(ref light, ref plane, out result);
return result;
}
/// <summary>
/// Scales the plane by the given scaling factor.
/// </summary>
/// <param name="value">The plane to scale.</param>
/// <param name="scale">The amount by which to scale the plane.</param>
/// <param name="result">When the method completes, contains the scaled plane.</param>
public static void Multiply(ref Plane value, float scale, out Plane result)
{
result.Normal.X = value.Normal.X * scale;
result.Normal.Y = value.Normal.Y * scale;
result.Normal.Z = value.Normal.Z * scale;
result.D = value.D * scale;
}
/// <summary>
/// Scales the plane by the given scaling factor.
/// </summary>
/// <param name="value">The plane to scale.</param>
/// <param name="scale">The amount by which to scale the plane.</param>
/// <returns>The scaled plane.</returns>
public static Plane Multiply(Plane value, float scale)
{
return new Plane(value.Normal.X * scale, value.Normal.Y * scale, value.Normal.Z * scale, value.D * scale);
}
/// <summary>
/// Calculates the dot product of the specified vector and plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <param name="result">When the method completes, contains the dot product of the specified plane and vector.</param>
public static void Dot(ref Plane left, ref Vector4 right, out float result)
{
result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + (left.D * right.W);
}
/// <summary>
/// Calculates the dot product of the specified vector and plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <returns>The dot product of the specified plane and vector.</returns>
public static float Dot(Plane left, Vector4 right)
{
return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + (left.D * right.W);
}
/// <summary>
/// Calculates the dot product of a specified vector and the normal of the plane plus the distance value of the plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <param name="result">When the method completes, contains the dot product of a specified vector and the normal of the Plane plus the distance value of the plane.</param>
public static void DotCoordinate(ref Plane left, ref Vector3 right, out float result)
{
result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + left.D;
}
/// <summary>
/// Calculates the dot product of a specified vector and the normal of the plane plus the distance value of the plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <returns>The dot product of a specified vector and the normal of the Plane plus the distance value of the plane.</returns>
public static float DotCoordinate(Plane left, Vector3 right)
{
return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + left.D;
}
/// <summary>
/// Calculates the dot product of the specified vector and the normal of the plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <param name="result">When the method completes, contains the dot product of the specified vector and the normal of the plane.</param>
public static void DotNormal(ref Plane left, ref Vector3 right, out float result)
{
result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z);
}
/// <summary>
/// Calculates the dot product of the specified vector and the normal of the plane.
/// </summary>
/// <param name="left">The source plane.</param>
/// <param name="right">The source vector.</param>
/// <returns>The dot product of the specified vector and the normal of the plane.</returns>
public static float DotNormal(Plane left, Vector3 right)
{
return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z);
}
/// <summary>
/// Changes the coefficients of the normal vector of the plane to make it of unit length.
/// </summary>
/// <param name="plane">The source plane.</param>
/// <param name="result">When the method completes, contains the normalized plane.</param>
public static void Normalize(ref Plane plane, out Plane result)
{
float magnitude = 1.0f / (float)(Math.Sqrt((plane.Normal.X * plane.Normal.X) + (plane.Normal.Y * plane.Normal.Y) + (plane.Normal.Z * plane.Normal.Z)));
result.Normal.X = plane.Normal.X * magnitude;
result.Normal.Y = plane.Normal.Y * magnitude;
result.Normal.Z = plane.Normal.Z * magnitude;
result.D = plane.D * magnitude;
}
/// <summary>
/// Changes the coefficients of the normal vector of the plane to make it of unit length.
/// </summary>
/// <param name="plane">The source plane.</param>
/// <returns>The normalized plane.</returns>
public static Plane Normalize(Plane plane)
{
float magnitude = 1.0f / (float)(Math.Sqrt((plane.Normal.X * plane.Normal.X) + (plane.Normal.Y * plane.Normal.Y) + (plane.Normal.Z * plane.Normal.Z)));
return new Plane(plane.Normal.X * magnitude, plane.Normal.Y * magnitude, plane.Normal.Z * magnitude, plane.D * magnitude);
}
/// <summary>
/// Transforms a normalized plane by a quaternion rotation.
/// </summary>
/// <param name="plane">The normalized source plane.</param>
/// <param name="rotation">The quaternion rotation.</param>
/// <param name="result">When the method completes, contains the transformed plane.</param>
public static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
{
float x2 = rotation.X + rotation.X;
float y2 = rotation.Y + rotation.Y;
float z2 = rotation.Z + rotation.Z;
float wx = rotation.W * x2;
float wy = rotation.W * y2;
float wz = rotation.W * z2;
float xx = rotation.X * x2;
float xy = rotation.X * y2;
float xz = rotation.X * z2;
float yy = rotation.Y * y2;
float yz = rotation.Y * z2;
float zz = rotation.Z * z2;
float x = plane.Normal.X;
float y = plane.Normal.Y;
float z = plane.Normal.Z;
result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
result.D = plane.D;
}
/// <summary>
/// Transforms a normalized plane by a quaternion rotation.
/// </summary>
/// <param name="plane">The normalized source plane.</param>
/// <param name="rotation">The quaternion rotation.</param>
/// <returns>The transformed plane.</returns>
public static Plane Transform(Plane plane, Quaternion rotation)
{
Plane result;
float x2 = rotation.X + rotation.X;
float y2 = rotation.Y + rotation.Y;
float z2 = rotation.Z + rotation.Z;
float wx = rotation.W * x2;
float wy = rotation.W * y2;
float wz = rotation.W * z2;
float xx = rotation.X * x2;
float xy = rotation.X * y2;
float xz = rotation.X * z2;
float yy = rotation.Y * y2;
float yz = rotation.Y * z2;
float zz = rotation.Z * z2;
float x = plane.Normal.X;
float y = plane.Normal.Y;
float z = plane.Normal.Z;
result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
result.D = plane.D;
return result;
}
/// <summary>
/// Transforms an array of normalized planes by a quaternion rotation.
/// </summary>
/// <param name="planes">The array of normalized planes to transform.</param>
/// <param name="rotation">The quaternion rotation.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="planes"/> is <c>null</c>.</exception>
public static void Transform(Plane[] planes, ref Quaternion rotation)
{
if (planes == null)
throw new ArgumentNullException("planes");
float x2 = rotation.X + rotation.X;
float y2 = rotation.Y + rotation.Y;
float z2 = rotation.Z + rotation.Z;
float wx = rotation.W * x2;
float wy = rotation.W * y2;
float wz = rotation.W * z2;
float xx = rotation.X * x2;
float xy = rotation.X * y2;
float xz = rotation.X * z2;
float yy = rotation.Y * y2;
float yz = rotation.Y * z2;
float zz = rotation.Z * z2;
for (int i = 0; i < planes.Length; ++i)
{
float x = planes[i].Normal.X;
float y = planes[i].Normal.Y;
float z = planes[i].Normal.Z;
/*
* Note:
* Factor common arithmetic out of loop.
*/
planes[i].Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
planes[i].Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
planes[i].Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
}
}
/// <summary>
/// Transforms a normalized plane by a matrix.
/// </summary>
/// <param name="plane">The normalized source plane.</param>
/// <param name="transformation">The transformation matrix.</param>
/// <param name="result">When the method completes, contains the transformed plane.</param>
public static void Transform(ref Plane plane, ref Matrix transformation, out Plane result)
{
float x = plane.Normal.X;
float y = plane.Normal.Y;
float z = plane.Normal.Z;
float d = plane.D;
Matrix inverse;
Matrix.Invert(transformation, out inverse);
result.Normal.X = (((x * inverse.M11) + (y * inverse.M12)) + (z * inverse.M13)) + (d * inverse.M14);
result.Normal.Y = (((x * inverse.M21) + (y * inverse.M22)) + (z * inverse.M23)) + (d * inverse.M24);
result.Normal.Z = (((x * inverse.M31) + (y * inverse.M32)) + (z * inverse.M33)) + (d * inverse.M34);
result.D = (((x * inverse.M41) + (y * inverse.M42)) + (z * inverse.M43)) + (d * inverse.M44);
}
/// <summary>
/// Transforms a normalized plane by a matrix.
/// </summary>
/// <param name="plane">The normalized source plane.</param>
/// <param name="transformation">The transformation matrix.</param>
/// <returns>When the method completes, contains the transformed plane.</returns>
public static Plane Transform(Plane plane, Matrix transformation)
{
Plane result;
float x = plane.Normal.X;
float y = plane.Normal.Y;
float z = plane.Normal.Z;
float d = plane.D;
Matrix.Invert(transformation, out transformation);
result.Normal.X = (((x * transformation.M11) + (y * transformation.M12)) + (z * transformation.M13)) + (d * transformation.M14);
result.Normal.Y = (((x * transformation.M21) + (y * transformation.M22)) + (z * transformation.M23)) + (d * transformation.M24);
result.Normal.Z = (((x * transformation.M31) + (y * transformation.M32)) + (z * transformation.M33)) + (d * transformation.M34);
result.D = (((x * transformation.M41) + (y * transformation.M42)) + (z * transformation.M43)) + (d * transformation.M44);
return result;
}
/// <summary>
/// Transforms an array of normalized planes by a matrix.
/// </summary>
/// <param name="planes">The array of normalized planes to transform.</param>
/// <param name="transformation">The transformation matrix.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="planes"/> is <c>null</c>.</exception>
public static void Transform(Plane[] planes, ref Matrix transformation)
{
if (planes == null)
throw new ArgumentNullException("planes");
Matrix inverse;
Matrix.Invert(transformation, out inverse);
for (int i = 0; i < planes.Length; ++i)
{
Transform(ref planes[i], ref transformation, out planes[i]);
}
}
/// <summary>
/// Scales a plane by the given value.
/// </summary>
/// <param name="scale">The amount by which to scale the plane.</param>
/// <param name="plane">The plane to scale.</param>
/// <returns>The scaled plane.</returns>
public static Plane operator *(float scale, Plane plane)
{
return new Plane(plane.Normal.X * scale, plane.Normal.Y * scale, plane.Normal.Z * scale, plane.D * scale);
}
/// <summary>
/// Scales a plane by the given value.
/// </summary>
/// <param name="plane">The plane to scale.</param>
/// <param name="scale">The amount by which to scale the plane.</param>
/// <returns>The scaled plane.</returns>
public static Plane operator *(Plane plane, float scale)
{
return new Plane(plane.Normal.X * scale, plane.Normal.Y * scale, plane.Normal.Z * scale, plane.D * scale);
}
/// <summary>
/// Tests for equality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator ==(Plane left, Plane right)
{
return left.Equals(ref right);
}
/// <summary>
/// Tests for inequality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public static bool operator !=(Plane left, Plane right)
{
return !left.Equals(ref right);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(string format)
{
return string.Format(CultureInfo.CurrentCulture, "A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(format, CultureInfo.CurrentCulture),
Normal.Y.ToString(format, CultureInfo.CurrentCulture), Normal.Z.ToString(format, CultureInfo.CurrentCulture), D.ToString(format, CultureInfo.CurrentCulture));
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, "A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="System.String"/> that represents this instance.
/// </returns>
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format(formatProvider, "A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(format, formatProvider),
Normal.Y.ToString(format, formatProvider), Normal.Z.ToString(format, formatProvider), D.ToString(format, formatProvider));
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
unchecked
{
return (Normal.GetHashCode() * 397) ^ D.GetHashCode();
}
}
/// <summary>
/// Determines whether the specified <see cref="Vector4"/> is equal to this instance.
/// </summary>
/// <param name="value">The <see cref="Vector4"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Vector4"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public bool Equals(ref Plane value)
{
return Normal == value.Normal && D == value.D;
}
/// <summary>
/// Determines whether the specified <see cref="Vector4"/> is equal to this instance.
/// </summary>
/// <param name="value">The <see cref="Vector4"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Vector4"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
[MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining
public bool Equals(Plane value)
{
return Equals(ref value);
}
/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object value)
{
if (!(value is Plane))
return false;
var strongValue = (Plane)value;
return Equals(ref strongValue);
}
}
}