-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAleSpherical.cs
3057 lines (2556 loc) · 103 KB
/
AleSpherical.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* (c) AleProjects.com, 2018
* v.2.0
*
* MIT License
*
* Convention in variable names:
* Dec, Ra, Lon, Lat (short) - radians;
* Declination, RightAscension, Longitude, Latitude (full) - degrees;
*
* Headings are always in radians.
*
*
* Install System.ValueTuple package via Nuget
*
* PM> Install-Package "System.ValueTuple"
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
namespace AleProjects.Spherical
{
/// <summary>
/// Represents a basic vector in the 3D Euclidean space.
/// </summary>
public interface ICartesian
{
/// <summary>
/// X component of the vector.
/// </summary>
double X { get; }
/// <summary>
/// Y component of the vector.
/// </summary>
double Y { get; }
/// <summary>
/// Z component of the vector.
/// </summary>
double Z { get; }
/// <summary>
/// Sets x,y,z components of the vector.
/// </summary>
/// <param name="x">x component.</param>
/// <param name="y">y component.</param>
/// <param name="z">z component.</param>
void SetCartesian(double x, double y, double z);
}
/// <summary>
/// Represents a basic spherical coordinate.
/// </summary>
public interface IGeoCoordinate
{
/// <summary>
/// Latitude in degrees.
/// </summary>
double Latitude { get; }
/// <summary>
/// Longitude in degrees.
/// </summary>
double Longitude { get; }
}
/// <summary>
/// Basic implementation of a vector in the 3D Euclidean space.
/// </summary>
public class Cartesian : ICartesian
{
/// <summary>
/// X component of the vector.
/// </summary>
public double X { get; protected set; }
/// <summary>
/// Y component of the vector.
/// </summary>
public double Y { get; protected set; }
/// <summary>
/// Z component of the vector.
/// </summary>
public double Z { get; protected set; }
/// <summary>
/// Vector declination in radians.
/// </summary>
public double Dec
{
get => Math.Asin(Z / Length);
}
/// <summary>
/// Vector right ascension in radians.
/// </summary>
public double Ra
{
get => Math.Atan2(Y, X);
}
/// <summary>
/// Vector declination in degrees.
/// </summary>
public double Declination
{
get => Math.Asin(Z / Math.Sqrt(X * X + Y * Y + Z * Z)) * 180.0 / Math.PI;
}
/// <summary>
/// Vector right ascension in degrees.
/// </summary>
public double RightAscension
{
get => Math.Atan2(Y, X) * 180.0 / Math.PI;
}
/// <summary>
/// Length of the vector.
/// </summary>
public double Length
{
get => SphericalExtension._VectorLength(X, Y, Z);
}
/// <summary>
/// Creates a vector with X=1, Y=0, Z=0 what is the same as latitude=0 and Longitude=0.
/// </summary>
public Cartesian()
{
X = 1.0;
Y = 0.0;
Z = 0.0;
}
/// <summary>
/// Creates a vector with X,Y,Z components, optionally normalizes it.
/// </summary>
/// <param name="x">X component.</param>
/// <param name="y">Y component.</param>
/// <param name="z">Y component.</param>
/// <param name="normalize">Normalizes the vector when true.</param>
public Cartesian(double x, double y, double z, bool normalize)
{
if (normalize)
(x, y, z) = SphericalExtension._Normalized(x, y, z);
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Creates a normalized vector with given declination and right ascension.
/// </summary>
/// <param name="dec">Declination</param>
/// <param name="ra">Right ascension</param>
/// <param name="degrees">Considers dec and ra as given in degrees when true.</param>
public Cartesian(double dec, double ra, bool degrees = true)
{
if (degrees)
{
dec *= Math.PI / 180.0;
ra *= Math.PI / 180.0;
}
double cos_dec = Math.Cos(dec);
X = cos_dec * Math.Cos(ra);
Y = cos_dec * Math.Sin(ra);
Z = Math.Sin(dec);
}
/// <summary>
/// Creates a new vector from another vector.
/// </summary>
/// <param name="cartesian">The vector whose X,Y,Z used for the new vector.</param>
public Cartesian(ICartesian cartesian)
{
X = cartesian.X;
Y = cartesian.Y;
Z = cartesian.Z;
}
/// <summary>
/// Sets x,y,z components of this vector.
/// </summary>
/// <param name="x">x component.</param>
/// <param name="y">y component.</param>
/// <param name="z">z component.</param>
public virtual void SetCartesian(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Sets declination and right ascension of this vector.
/// </summary>
/// <param name="dec">Declination in radians.</param>
/// <param name="ra">Right ascension in radians.</param>
public virtual void SetSpherical(double dec, double ra)
{
double cos_dec = Math.Cos(dec);
X = cos_dec * Math.Cos(ra);
Y = cos_dec * Math.Sin(ra);
Z = Math.Sin(dec);
}
/// <summary>
/// Returns a string representation of the vector.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return string.Format("({0}; {1}; {2})", X, Y, Z);
}
}
/// <summary>
/// Basic value implementation of a vector in the 3D Euclidean space.
/// </summary>
public struct CartesianValue : ICartesian
{
/// <summary>
/// X component of the vector.
/// </summary>
public double X { get; set; }
/// <summary>
/// Y component of the vector.
/// </summary>
public double Y { get; set; }
/// <summary>
/// Z component of the vector.
/// </summary>
public double Z { get; set; }
public CartesianValue(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
public CartesianValue(ICartesian cartesian)
{
X = cartesian.X;
Y = cartesian.Y;
Z = cartesian.Z;
}
public CartesianValue(double dec, double ra, bool degrees = true)
{
if (degrees)
{
dec *= Math.PI / 180.0;
ra *= Math.PI / 180.0;
}
double cos_dec = Math.Cos(dec);
X = cos_dec * Math.Cos(ra);
Y = cos_dec * Math.Sin(ra);
Z = Math.Sin(dec);
}
/// <summary>
/// Sets x,y,z components of this vector.
/// </summary>
/// <param name="x">x component.</param>
/// <param name="y">y component.</param>
/// <param name="z">z component.</param>
public void SetCartesian(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
/// <summary>
/// Sets declination and right ascension of this vector.
/// </summary>
/// <param name="dec">Declination in radians.</param>
/// <param name="ra">Right ascension in radians.</param>
public void SetSpherical(double dec, double ra)
{
double cos_dec = Math.Cos(dec);
X = cos_dec * Math.Cos(ra);
Y = cos_dec * Math.Sin(ra);
Z = Math.Sin(dec);
}
/// <summary>
/// Sets normalized x,y,z components of this vector.
/// </summary>
/// <param name="x">x component.</param>
/// <param name="y">y component.</param>
/// <param name="z">z component.</param>
public void SetNormalized(double x, double y, double z)
{
(x, y, z) = SphericalExtension._Normalized(x, y, z);
X = x;
Y = y;
Z = z;
}
public override string ToString()
{
return string.Format("({0}; {1}; {2})", X, Y, Z);
}
}
/// <summary>
/// Basic value implementation of a geo point.
/// </summary>
public struct LatLonValue : IGeoCoordinate
{
/// <summary>
/// Latitude in degrees.
/// </summary>
public double Latitude { get; set; }
/// <summary>
/// Longitude in degrees.
/// </summary>
public double Longitude { get; set; }
public LatLonValue(double latitude, double longitude)
{
Latitude = latitude;
Longitude = longitude;
}
}
/// <summary>
/// Defines delegate type for user polyline section test
/// </summary>
/// <param name="before"></param>
/// <param name="start"></param>
/// <param name="finish"></param>
/// <param name="angle"></param>
/// <param name="azimuth"></param>
/// <returns>True if test succeeds.</returns>
public delegate bool PolylineSectionTest<T>(T before, T start, T finish, double angle, double bearing)
where T : ICartesian;
/// <summary>
/// Defines delegate type for user route section test
/// </summary>
/// <param name="before"></param>
/// <param name="start"></param>
/// <param name="finish"></param>
/// <param name="angle"></param>
/// <param name="azimuth"></param>
/// <param name="moveAzimuth"></param>
/// <param name="azimuthTolerance"></param>
/// <returns>True if test succeeds.</returns>
public delegate bool RouteSectionTest<T>(T before, T start, T finish, double angle, double bearing, double moveBearing, double bearingTolerance)
where T : ICartesian;
/// <summary>
/// Represents result of a test of a point and a polyline.
/// </summary>
public class PolylineTestResult
{
protected double _TotalDistance = -1.0;
public int Id { get; set; }
public int Fails { get; set; }
public int SectionIndex { get; set; }
public double AngleWithSectionPlane { get; set; } = -1.0;
public double SectionDirection { get; set; } = -1.0;
public double AngleWithNextVertex { get; set; } = -1.0;
public double[] SectionsAngles { get; protected set; }
public double TotalDistance(double sphereRadius)
{
if (_TotalDistance < 0.0 && SectionsAngles != null)
{
_TotalDistance = 0.0;
for (int i = 0; i < SectionsAngles.Length; i++)
_TotalDistance += SectionsAngles[i];
}
return _TotalDistance * sphereRadius;
}
public double DistanceToPolyline(double sphereRadius)
{
return AngleWithSectionPlane >= 0.0 ? AngleWithSectionPlane * sphereRadius : -1.0;
}
public double DistanceToEnd(double sphereRadius)
{
double result = AngleWithNextVertex;
int start = SectionIndex >= 0 ? SectionIndex + 1 : -1 - SectionIndex;
for (int i = start; i < SectionsAngles.Length; i++)
result += SectionsAngles[i];
return result * sphereRadius;
}
public static PolylineTestResult Create<T>(IEnumerable<T> polyline, bool reverse,
int sectionIndex, double sectionDir, double angleWithSectionPlane, double angleWithNextVertex)
where T : ICartesian
{
PolylineTestResult result = new PolylineTestResult()
{
SectionIndex = sectionIndex,
SectionDirection = sectionDir,
AngleWithSectionPlane = angleWithSectionPlane,
AngleWithNextVertex = angleWithNextVertex,
SectionsAngles = new double[polyline.Count() - 1]
};
IEnumerable<T> vertices = reverse ? polyline.Reverse().Skip(1) : polyline.Skip(1);
T previous = reverse ? polyline.Last() : polyline.First();
int i = 0;
foreach (var vertex in vertices)
{
result.SectionsAngles[i++] = previous.Angle(vertex);
previous = vertex;
}
return result;
}
}
/// <summary>
/// Represents result of a test of a route.
/// </summary>
public class RouteTestResult
{
public int Id { get; set; }
public int Fails { get; set; }
public int SectionIndex { get; set; }
public double AngleWithSectionPlane { get; set; } = -1.0;
public double SectionDirection { get; set; } = -1.0;
public double AngleWithNextVertex { get; set; } = -1.0;
}
/// <summary>
/// Provides extension methods for ICartesian and IGeoCoordinate objects.
/// </summary>
public static class SphericalExtension
{
public const double EPSILON = 2.2204460492503131e-016;
public const double EARTH_MEAN_RADIUS = 6371000;
private const string Error_Message_Not_Polygon = "Parameter is not a polygon.";
private const string Error_Message_Not_Convex = "Parameter is not a convex.";
private const string Error_Message_Not_Polyline = "Parameter is not a polyline.";
private const string Error_Message_Empty_Collection = "Collection can't be empty.";
// properties
/// <summary>
/// Maximum latitude in radians a map based on Mercator projection can display.
/// </summary>
public static double MaxMercatorMapsLat
{
get => Math.Atan(Math.Sinh(Math.PI));
}
// Auxiliary methods
private static T NewCartesian<T>(double x, double y, double z)
where T : ICartesian, new()
{
T result = new T();
result.SetCartesian(x, y, z);
return result;
}
private static T NewCartesian<T>(double dec, double ra)
where T : ICartesian, new()
{
T result = new T();
double cos_dec = Math.Cos(dec);
double x = cos_dec * Math.Cos(ra);
double y = cos_dec * Math.Sin(ra);
double z = Math.Sin(dec);
result.SetCartesian(x, y, z);
return result;
}
// Helping methods
#region Helping-methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double _VectorLength(double ax, double ay, double az)
{
return Math.Sqrt(ax * ax + ay * ay + az * az);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static (double, double, double) _Normalized(double ax, double ay, double az)
{
double l = Math.Sqrt(ax * ax + ay * ay + az * az);
if (l > 0.0)
{
ax /= l;
ay /= l;
az /= l;
}
return (ax, ay, az);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double _DotProduct(double ax, double ay, double az, double bx, double by, double bz)
{
return ax * bx + ay * by + az * bz;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static (double, double, double) _CrossProduct(double ax, double ay, double az, double bx, double by, double bz, bool normalize)
{
double x = ay * bz - az * by;
double y = -(ax * bz - az * bx);
double z = ax * by - ay * bx;
if (normalize)
{
double l = Math.Sqrt(x * x + y * y + z * z);
if (l > 0.0)
{
x /= l;
y /= l;
z /= l;
}
}
return (x, y, z);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double _TripleProduct(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz)
{
return ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) + az * (bx * cy - by * cx);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool _InsideTriangle(double ax, double ay, double az, double bx, double by, double bz, double cx, double cy, double cz, double dx, double dy, double dz)
{
double x1 = ax * (by * cz - bz * cy) - ay * (bx * cz - bz * cx) + az * (bx * cy - by * cx);
double x2 = ax * (cy * dz - cz * dy) - ay * (cx * dz - cz * dx) + az * (cx * dy - cy * dx);
if (x1 * x2 >= 0.0)
{
double x3 = ax * (dy * bz - dz * by) - ay * (dx * bz - dz * bx) + az * (dx * by - dy * bx);
if (x2 * x3 >= 0.0 && x1 * x3 >= 0.0)
{
double sx = bx + cx + dx;
double sy = by + cy + dy;
double sz = bz + cz + dz;
return ax * sx + ay * sy + az * sz >= 0.0;
}
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool _InsideTriangle(double ax, double ay, double az, in CartesianValue b, in CartesianValue c, in CartesianValue d)
{
double x1 = ax * (b.Y * c.Z - b.Z * c.Y) - ay * (b.X * c.Z - b.Z * c.X) + az * (b.X * c.Y - b.Y * c.X);
double x2 = ax * (c.Y * d.Z - c.Z * d.Y) - ay * (c.X * d.Z - c.Z * d.X) + az * (c.X * d.Y - c.Y * d.X);
if (x1 * x2 >= 0.0)
{
double x3 = ax * (d.Y * b.Z - d.Z * b.Y) - ay * (d.X * b.Z - d.Z * b.X) + az * (d.X * b.Y - d.Y * b.X);
if (x2 * x3 >= 0.0 && x1 * x3 >= 0.0)
{
double sx = b.X + c.X + d.X;
double sy = b.Y + c.Y + d.Y;
double sz = b.Z + c.Z + d.Z;
return ax * sx + ay * sy + az * sz >= 0.0;
}
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static double _Cosine(double ax, double ay, double az, double bx, double by, double bz)
{
double r = (ax * bx + ay * by + az * bz) / Math.Sqrt((ax * ax + ay * ay + az * az) * (bx * bx + by * by + bz * bz));
if (Math.Abs(r) > 1.0)
r = Math.Truncate(r);
return r;
}
#endregion Helping-methods
// ICartesian extension methods
/// <summary>
/// Extension method calculating a length of the vector.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Length of the vector.</returns>
public static double VectorLength<T>(this T cartesian)
where T : ICartesian
{
return _VectorLength(cartesian.X, cartesian.Y, cartesian.Z);
}
/// <summary>
/// Extension method calculating a declination of the vector (latitude for geopoint).
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Declination in radians.</returns>
public static double Dec<T>(this T cartesian)
where T : ICartesian
{
return Math.Asin(cartesian.Z / _VectorLength(cartesian.X, cartesian.Y, cartesian.Z));
}
/// <summary>
/// Extension method calculating raght ascension of the vector (longitude for geopoint).
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Raght ascension in radians.</returns>
public static double Ra<T>(this T cartesian)
where T : ICartesian
{
return Math.Atan2(cartesian.Y, cartesian.X);
}
/// <summary>
/// Extension method calculating a declination of the vector (latitude for geopoint).
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Declination in degrees.</returns>
public static double Declination<T>(this T cartesian)
where T : ICartesian
{
return Math.Asin(cartesian.Z / _VectorLength(cartesian.X, cartesian.Y, cartesian.Z)) * 180.0 / Math.PI;
}
/// <summary>
/// Extension method calculating raght ascension of the vector (longitude for geopoint).
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Raght ascension in degrees.</returns>
public static double RightAscension<T>(this T cartesian)
where T : ICartesian
{
return Math.Atan2(cartesian.Y, cartesian.X) * 180.0 / Math.PI;
}
/// <summary>
/// Extension method calculating latitude if the vector represents geopoint. Equal to the Declination<T> method.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Declination in degrees.</returns>
public static double Latitude<T>(this T cartesian)
where T : ICartesian
{
return Math.Asin(cartesian.Z / _VectorLength(cartesian.X, cartesian.Y, cartesian.Z)) * 180.0 / Math.PI;
}
/// <summary>
/// Extension method calculating longitude if the vector represents geopoint. Equal to the RightAscension<T> method.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <returns>Raght ascension in degrees.</returns>
public static double Longitude<T>(this T cartesian)
where T : ICartesian
{
return Math.Atan2(cartesian.Y, cartesian.X) * 180.0 / Math.PI;
}
/// <summary>
/// Extension method calculating rounded latitude if the vector represents geopoint. Equal to the Declination<T> method.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <param name="cartesian">Number of fractional digits.</param>
/// <returns>Declination in degrees.</returns>
public static double Latitude<T>(this T cartesian, int digits)
where T : ICartesian
{
return Math.Round(Math.Asin(cartesian.Z / _VectorLength(cartesian.X, cartesian.Y, cartesian.Z)) * 180.0 / Math.PI, digits);
}
/// <summary>
/// Extension method calculating rounded longitude if the vector represents geopoint. Equal to the RightAscension<T> method.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector.</param>
/// <param name="digits">Number of fractional digits.</param>
/// <returns>Raght ascension in degrees.</returns>
public static double Longitude<T>(this T cartesian, int digits)
where T : ICartesian
{
return Math.Round(Math.Atan2(cartesian.Y, cartesian.X) * 180.0 / Math.PI, digits);
}
/// <summary>
/// Tests if a vector is zero-vector, i.e X,Y,Z components are equal to 0 with given precision.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector to test.</param>
/// <param name="precision">Precision of comparing X,Y,Z components with 0.</param>
/// <returns></returns>
public static bool IsZeroVector<T>(this T cartesian, double precision)
where T : ICartesian
{
return Math.Abs(cartesian.X) <= precision &&
Math.Abs(cartesian.Y) <= precision &&
Math.Abs(cartesian.Z) <= precision;
}
/// <summary>
/// Extension method normalizing the specified vector.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector to normalize.</param>
/// <returns>Previous length of the vector.</returns>
public static void Normalize<T>(this T cartesian)
where T : ICartesian
{
var (x, y, z) = _Normalized(cartesian.X, cartesian.Y, cartesian.Z);
cartesian.SetCartesian(x, y, z);
}
public static T Add<T, U>(this T cartesian, U V, bool normalize)
where T : ICartesian, new()
where U : ICartesian
{
double x = cartesian.X + V.X;
double y = cartesian.Y + V.Y;
double z = cartesian.Z + V.Z;
if (normalize)
(x, y, z) = _Normalized(x, y, z);
return NewCartesian<T>(x, y, z);
}
/// <summary>
/// Extension method calculating dot product of two vectors.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="V">Second vector.</param>
/// <returns>Value of dot product.</returns>
public static double DotProduct<T, U>(this T cartesian, U V)
where T : ICartesian
where U : ICartesian
{
return _DotProduct(cartesian.X, cartesian.Y, cartesian.Z, V.X, V.Y, V.Z);
}
/// <summary>
/// Extension method calculating dot product of two vectors.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="x">X component of the second vector.</param>
/// <param name="y">Y component of the second vector.</param>
/// <param name="z">Z component of the second vector.</param>
/// <returns>Value of dot product.</returns>
public static double DotProduct<T>(this T cartesian, double x, double y, double z)
where T : ICartesian
{
return _DotProduct(cartesian.X, cartesian.Y, cartesian.Z, x, y, z);
}
/// <summary>
/// Extension method calculating cross product of two vectors. The second vector is represented by X,Y,Z components.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="x">X component of the second vector.</param>
/// <param name="y">Y component of the second vector.</param>
/// <param name="z">Z component of the second vector.</param>
/// <param name="normalize">When true, method returns normalized vector.</param>
/// <returns>Vector representing cross product.</returns>
public static T CrossProduct<T, U>(this U cartesian, double x, double y, double z, bool normalize)
where T : ICartesian, new()
where U : ICartesian
{
var (x1, y1, z1) = _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, x, y, z, normalize);
return NewCartesian<T>(x1, y1, z1);
}
/// <summary>
/// Extension method calculating cross product of two vectors.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="V">Second vector.</param>
/// <param name="normalize">When true, method returns normalized vector.</param>
/// <returns>Vector representing cross product.</returns>
public static T CrossProduct<T, U>(this T cartesian, U V, bool normalize)
where T : ICartesian, new()
where U : ICartesian
{
var (x, y, z) = _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, V.X, V.Y, V.Z, normalize);
return NewCartesian<T>(x, y, z);
}
public static T CrossProduct<T, U>(this U cartesian, in CartesianValue V, bool normalize)
where T : ICartesian, new()
where U : ICartesian
{
var (x, y, z) = _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, V.X, V.Y, V.Z, normalize);
return NewCartesian<T>(x, y, z);
}
/// <summary>
/// Extension method calculating cross product of two vectors. The second vector is represented by declination and right ascension.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian"></param>
/// <param name="dec">Declination in radians (latitude for geopoint)</param>
/// <param name="ra">Right ascension (longitude for geopoint)</param>
/// <returns>Vector representing cross product.</returns>
public static T CrossProduct<T, U>(this U cartesian, double dec, double ra)
where T : ICartesian, new()
where U : ICartesian
{
double cos_dec = Math.Cos(dec);
var (x, y, z) = _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, cos_dec * Math.Cos(ra), cos_dec * Math.Sin(ra), Math.Sin(dec), true);
return NewCartesian<T>(x, y, z);
}
/// <summary>
/// Extension method calculating cross product of two vectors. Type T must implement ICartesian interface.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="V">Second vector.</param>
/// <returns>Cross product decomposed to x,y,z components.</returns>
public static (double x, double y, double z) CrossProduct<T, U>(this T cartesian, U V)
where T : ICartesian
where U : ICartesian
{
return _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, V.X, V.Y, V.Z, false);
}
/// <summary>
/// Extension method calculating cross product of two vectors. The second vector is represented by X,Y,Z components. Type T must implement ICartesian interface.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="x">X component of the second vector.</param>
/// <param name="y">Y component of the second vector.</param>
/// <param name="z">Z component of the second vector.</param>
/// <returns>Cross product decomposed to x,y,z components.</returns>
public static (double x, double y, double z) CrossProduct<T>(this T cartesian, double x, double y, double z)
where T : ICartesian
{
return _CrossProduct(cartesian.X, cartesian.Y, cartesian.Z, x, y, z, false);
}
/// <summary>
/// Extension method calculating triple product of three vectors.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">First vector.</param>
/// <param name="V1">Second vector.</param>
/// <param name="V2">Third vector.</param>
/// <returns>Value of triple product.</returns>
public static double TripleProduct<T, U>(this T cartesian, U V1, U V2)
where T : ICartesian
where U : ICartesian
{
return _TripleProduct(cartesian.X, cartesian.Y, cartesian.Z, V1.X, V1.Y, V1.Z, V2.X, V2.Y, V2.Z);
}
/// <summary>
/// Extension method rotating a vector around an axis by an angle. The result, the initial vector, and the axis are the right hand rule vectors.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>
/// <param name="cartesian">Vector to rotate.</param>
/// <param name="angle">Rotation angle.</param>
/// <param name="axisX">Rotation axis X.</param>
/// <param name="axisY">Rotation axis Y.</param>
/// <param name="axisZ">Rotation axis Z.</param>
/// <returns>New vector obtained from rotation.</returns>
public static T Rotate<T, U>(this U cartesian, double angle, double axisX, double axisY, double axisZ)
where T : ICartesian, new()
where U : ICartesian
{
var (x, y, z) = _Normalized(axisX, axisY, axisZ);
double cosine = Math.Cos(angle);
double sine = Math.Sin(angle);
double one_minus_cos = 1.0 - cosine;
// rotation matrix by lines
// line 1
double M11 = cosine + one_minus_cos * x * x;
double M12 = one_minus_cos * x * y - sine * z;
double M13 = one_minus_cos * x * z + sine * y;
// line 2
double M21 = one_minus_cos * y * x + sine * z;
double M22 = cosine + one_minus_cos * y * y;
double M23 = one_minus_cos * y * z - sine * x;
// line 3
double M31 = one_minus_cos * z * x - sine * y;
double M32 = one_minus_cos * z * y + sine * x;
double M33 = cosine + one_minus_cos * z * z;
return NewCartesian<T>(
_DotProduct(cartesian.X, cartesian.Y, cartesian.Z, M11, M12, M13),
_DotProduct(cartesian.X, cartesian.Y, cartesian.Z, M21, M22, M23),
_DotProduct(cartesian.X, cartesian.Y, cartesian.Z, M31, M32, M33));
}
/// <summary>
/// This extension method rotates a sphere with given vector around some axis.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface and have default constructor.</typeparam>
/// <typeparam name="U">Type U must implement the ICartesian interface.</typeparam>