-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemElements.cs
More file actions
611 lines (570 loc) · 19.5 KB
/
SystemElements.cs
File metadata and controls
611 lines (570 loc) · 19.5 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
namespace KSP_Library
{
public struct VecCart
{
public double X;
public double Y;
public double Z;
public void Rotate(VecCart axis, double angle)
{
//double a;
//double b;
//double c;
//// z-axis rotation
//X = a * Math.Cos(angle) - Y * Math.Sin(angle);
//Y = b * Math.Sin(angle) + Y * Math.Cos(angle);
//Z = c;
/*x' = x*cos q - y*sin q
y' = x*sin q + y*cos q
z' = z*/
angle = (angle / 180) * Math.PI;
double[,] R = new double[3, 3];
R[0, 0] = Math.Cos(angle) + Math.Pow(axis.X, 2) * (1 - Math.Cos(angle));
R[0, 1] = axis.Y * axis.X * (1 - Math.Cos(angle)) + axis.Z * Math.Sin(angle);
R[0, 2] = axis.Z * axis.X * (1 - Math.Cos(angle)) - axis.Y * Math.Sin(angle);
R[1, 0] = axis.X * axis.Y * (1 - Math.Cos(angle)) - axis.Z * Math.Sin(angle);
R[1, 1] = Math.Cos(angle) + Math.Pow(axis.Y, 2) * 1 - Math.Cos(angle);
R[1, 2] = axis.Z * axis.Y * (1 - Math.Cos(angle)) + axis.X * Math.Sin(angle);
R[2, 0] = axis.X * axis.Z * (1 - Math.Cos(angle)) + axis.Y * Math.Sin(angle);
R[2, 1] = axis.Y * axis.Z * (1 - Math.Cos(angle)) - axis.X * Math.Sin(angle);
R[2, 2] = Math.Cos(angle) + Math.Pow(axis.Y, 2) * 1 - Math.Cos(angle);
double x = X * R[0, 0] + Y * R[1, 0] + Z * R[2, 0];
double y = X * R[0, 1] + Y * R[1, 1] + Z * R[2, 1];
double z = X * R[0, 2] + Y * R[1, 2] + Z * R[2, 2];
X = x;
Y = y;
Z = z;
}
public VecSphere ToVecSphere()
{
VecSphere radVecSphere = new VecSphere()
{
RA = Math.PI / 2 - Math.Atan2(Y, X),
Decl = Math.Atan2(Math.Sqrt(Math.Pow(X, 2) + Math.Pow(Y, 2)), Z)
};
radVecSphere.ConvertToDeg();
return radVecSphere;
}
public override int GetHashCode()
{
int newHash = X.GetHashCode();
newHash ^= Y.GetHashCode();
newHash ^= Z.GetHashCode();
return newHash;
}
public override bool Equals(object obj)
{
if (((VecCart)obj).X == X && ((VecCart)obj).Y == Y && ((VecCart)obj).Z == Z)
{
return true;
}
else return false;
}
}
public struct VecSphere
{
public double RA;
public double Decl;
public void RefineCoords()
{
while (RA < 0)
{
RA += 360;
}
while (RA >= 360)
{
RA -= 360;
}
while (Decl < 0)
{
Decl += 360;
}
while (Decl >= 360)
{
Decl -= 360;
}
}
public VecCart ToVecCart()
{
VecSphere temp = this;
temp.ConvertToRad();
return new VecCart()
{
X = Math.Sin(Math.PI / 2 - temp.Decl) * Math.Cos(temp.RA),
Y = Math.Sin(Math.PI / 2 - temp.Decl) * Math.Sin(temp.RA),
Z = Math.Cos(Math.PI / 2 - temp.Decl)
};
}
public VecSphere TranslateCoords(Plane refPlane)
{
VecSphere axis = new VecSphere()
{
RA = (refPlane.NP.RA + 270) % 360,
Decl = 0
};
VecCart cart = this.ToVecCart();
cart.Rotate(axis.ToVecCart(), 90 - refPlane.NP.Decl);
VecSphere newCoords = cart.ToVecSphere();
double relAxisRA;
if (refPlane.NP.Decl < 0 ^ refPlane.Origin.RA - axis.RA > 180)
{
relAxisRA = 360 - Plane.FindCAngle(axis, refPlane.Origin);
}
else
{
relAxisRA = Plane.FindCAngle(axis, refPlane.Origin);
}
newCoords.RA += relAxisRA - axis.RA;
newCoords.RefineCoords();
return newCoords;
}
public void ConvertToRad()
{
RA = convertDegToRad(RA);
Decl = convertDegToRad(Decl);
}
public void ConvertToDeg()
{
RA = convertRadToDeg(RA);
Decl = convertRadToDeg(Decl);
}
private double convertDegToRad(double deg)
{
return (deg / 180) * Math.PI;
//while (rad < 0)
//{
// rad += 2 * Math.PI;
//}
//while (rad >= 2 * Math.PI)
//{
// rad -= 2 * Math.PI;
//}
//return rad;
}
private double convertRadToDeg(double rad)
{
return (rad / Math.PI) * 180;
//while (deg < 0)
//{
// deg += 360;
//}
//while (deg >= 360)
//{
// deg -= 360;
//}
//return Math.Round(deg, 4);
}
public override int GetHashCode()
{
int newHash = RA.GetHashCode();
newHash ^= Decl.GetHashCode();
return newHash;
}
public override bool Equals(object obj)
{
if(obj.GetType() != typeof(VecSphere))
{
return false;
}
else if (((VecSphere)obj).GetHashCode() != this.GetHashCode())
{
return false;
}
else return true;
}
}
public class Plane
{
public string Name { get; set; }
public VecSphere NP { get; set; }
public VecSphere Origin { get; set; }
public Plane RefPlane { get; set; }
public double Incl { get; set; }
public double LAN { get; set; }
// constructors
public Plane(string name = "Default Plane")
{
Name = name;
NP = new VecSphere()
{
RA = 270,
Decl = 90
};
Origin = new VecSphere()
{
RA = 0,
Decl = 0
};
}
public Plane(VecSphere np, string name = "")
{
Name = name;
NP = np;
ResetRefPlane();
}
public Plane(Plane refPlane, VecSphere np, string name = "")
{
Name = name;
RefPlane = refPlane;
Incl = FindCAngle(np, refPlane.NP);
NP = np;
// find: LAN, Origin (relOrigin, relNP)
VecSphere rotAxis = new VecSphere()
{
RA = (refPlane.NP.RA + 90) % 360,
Decl = 0
};
VecCart rotAxisCart = rotAxis.ToVecCart();
VecCart NPCart = NP.ToVecCart();
NPCart.Rotate(rotAxisCart, Incl);
VecSphere relNP = NPCart.ToVecSphere();
VecSphere refRotAxis = new VecSphere()
{
Decl = 0
};
if (NP.Decl >= 0 ^ refPlane.Origin.RA - rotAxis.RA > 180)
{
refRotAxis.RA = FindCAngle(refPlane.Origin, rotAxis);
}
else
{
refRotAxis.RA = 360 - FindCAngle(refPlane.Origin, rotAxis);
}
relNP.RA += (refRotAxis.RA - rotAxis.RA);
LAN = (relNP.RA + 90) % 360;
//VecSphere relOrigin = new VecSphere()
//{
// RA = (relNP.RA + 90) % 360,
// Decl = 0
//};
//VecCart relOriginCart = relOrigin.ToVecCart();
//VecSphere rotAxisInv = new VecSphere()
//{
// RA = (rotAxis.RA + 180) % 360,
// Decl = 0
//};
//relOriginCart.Rotate(rotAxisInv.ToVecCart(), refPlane.Incl);
//Origin = relOriginCart.ToVecSphere();
//if (NP.Decl >= 0 ^ refPlane.Origin.RA - rotAxis.RA > 180)
//{
// refRotAxis.RA = FindCAngle(refPlane.Origin, rotAxis);
//}
//else
//{
// refRotAxis.RA = 360 - FindCAngle(refPlane.Origin, rotAxis);
//}
}
public Plane(Plane refPlane, double lan, double incl, string name = "")
{
Name = name;
RefPlane = refPlane;
Incl = incl;
LAN = lan;
// find: NP, Origin
VecSphere relNP = new VecSphere()
{
RA = lan - 90,
Decl = 90 - incl
};
NP = RefPlane.TranslateCoords(relNP, false);
VecSphere relOrigin = new VecSphere()
{
RA = lan,
Decl = 0
};
Origin = RefPlane.TranslateCoords(relOrigin, false);
}
private void setLAN()
{
VecSphere radRefNP = NP;
radRefNP.ConvertToRad();
VecSphere radOrigin = Origin;
radOrigin.ConvertToRad();
double radLAN = Math.Cos(Math.Sqrt(Math.Pow(radOrigin.RA, 2) + Math.Pow(radRefNP.RA, 2) - 2 * radOrigin.RA * radRefNP.RA * Math.Cos(radRefNP.Decl - radOrigin.Decl)));
LAN = radLAN * (180 / Math.PI);
}
private void setIncl()
{
Incl = FindCAngle(NP, RefPlane.NP);
}
public void ResetRefPlane()
{
RefPlane = new Plane();
LAN = NP.RA + 90;
Incl = 90 - NP.Decl;
Origin = new VecSphere()
{
RA = LAN,
Decl = 0
};
}
public VecSphere TranslateCoords(VecSphere vec, bool fixToRel = true)
{
VecCart vecCart = vec.ToVecCart();
Plane rotPlane = new Plane(NP); // uses this Plane with reset RefPlane
double rot1;
if (RefPlane.Origin.Decl < 0)
{
rot1 = 360 - FindCAngle(rotPlane.Origin, RefPlane.Origin);
}
else
{
rot1 = FindCAngle(rotPlane.Origin, RefPlane.Origin);
}
double rot2;
rot2 = rotPlane.Origin.RA;
VecSphere axis;
if (!fixToRel)
{
axis = new VecSphere()
{
RA = rotPlane.Origin.RA + 180,
Decl = rotPlane.Origin.Decl
};
}
else
{
axis = rotPlane.Origin;
}
double angle = Plane.FindCAngle(rotPlane.NP, rotPlane.RefPlane.NP);
VecCart axisCart = axis.ToVecCart();
vecCart.Rotate(axisCart, angle);
VecSphere vecSphere = vecCart.ToVecSphere();
vecSphere.RA += LAN;
return vecSphere;
}
public static double FindCAngle(VecSphere vs1, VecSphere vs2)
{
vs1.ConvertToRad();
vs2.ConvertToRad();
double vec1NPDif = Math.PI / 2 - vs1.Decl;
double vec2NPDif = Math.PI / 2 - vs2.Decl;
double raDif = Math.Abs(vs2.RA - vs1.RA);
// spherical law of cosines
double cAngle = Math.Acos(Math.Cos(vec1NPDif) * Math.Cos(vec2NPDif) + Math.Sin(vec1NPDif) * Math.Sin(vec2NPDif) * Math.Cos(raDif));
return cAngle * (180 / Math.PI);
}
//private double translateRA(VecSphere vec, double obl)
//{
// double radRA = convertDegToRad(vec.RA);
// double radDecl = convertDegToRad(vec.Decl);
// double radObl = convertDegToRad(obl);
// double num = Math.Sin(radRA) * Math.Cos(radObl) - Math.Tan(radDecl) * Math.Sin(radObl);
// double denom = Math.Cos(radRA);
// double value = Math.Atan(num / denom);
// if (denom < 0)
// {
// return convertRadToDeg(value + Math.PI);
// }
// else return convertRadToDeg(value);
//}
//private double translateDecl(VecSphere vec, double obl)
//{
// double radRA = convertDegToRad(vec.RA);
// double radDecl = convertDegToRad(vec.Decl);
// double radObl = convertDegToRad(obl);
// double value = Math.Asin(Math.Sin(radDecl) * Math.Cos(radObl) + Math.Cos(radDecl) * Math.Sin(radObl) * Math.Sin(radRA));
// return convertRadToDeg(value);
//}
//private double translateRAReverse(double ra, double decl, double obl)
//{
// double radRA = convertDegToRad(ra);
// double radDecl = convertDegToRad(decl);
// double radObl = convertDegToRad(obl);
// double num = Math.Sin(radRA) * Math.Cos(radObl) - Math.Tan(radDecl) * Math.Sin(radObl);
// double denom = Math.Cos(radRA);
// double value = Math.Atan(num / denom);
// double Rnum = Math.Sin(radRA) * Math.Cos(radObl) - Math.Tan(radDecl) * Math.Sin(radObl);
// double Rdenom = Math.Cos(radRA);
// if (denom < 0)
// {
// return convertRadToDeg(value + Math.PI);
// }
// else return convertRadToDeg(value);
//}
//private double translateDeclReverse(double ra, double decl, double obl)
//{
// double radRA = convertDegToRad(ra);
// double radDecl = convertDegToRad(decl);
// double radObl = convertDegToRad(obl);
// double value = Math.Asin(Math.Sin(radDecl) * Math.Cos(radObl) + Math.Cos(radDecl) * Math.Sin(radObl) * Math.Sin(radRA));
// return convertRadToDeg(value);
//}
// overrides
public override string ToString()
{
string str = Name + "\nNPRA: " +
NP.RA + "\nNPDecl: " +
NP.Decl + "\nOrRA: " +
Origin.RA + "\nOrDecl: " +
Origin.Decl +"\n";
if(RefPlane != null)
{
str += "\nRefPlane:\nNPRA: " +
RefPlane.NP.RA + "\nNPDecl: " +
RefPlane.NP.Decl + "\nOrRA: " +
RefPlane.Origin.RA + "\nOrDecl: " +
RefPlane.Origin.Decl + "\n";
}
return str;
}
public override bool Equals(object obj)
{
if (obj is Plane && ((Plane)obj).GetHashCode() == GetHashCode())
{
return true;
}
else return false;
}
public override int GetHashCode()
{
int newHash = NP.GetHashCode();
newHash ^= Origin.GetHashCode();
return newHash;
}
}
public abstract class SolarSystem
{
public Body[] Bodies { get; set; }
// constructors
public SolarSystem() { }
public SolarSystem(Body[] bodies)
{
Bodies = bodies;
}
// methods
public virtual Body GetSystemBody(string bodyName)
{
return Bodies.Single(b => b.Name == bodyName.ToUpper());
}
public virtual Body GetSystemBody(int bodyIndex)
{
return Bodies[bodyIndex];
}
public override int GetHashCode()
{
return Bodies.GetHashCode();
}
public override bool Equals(object obj)
{
if (((SolarSystem)obj).GetHashCode() == this.GetHashCode())
{
return true;
}
else return false;
}
public override string ToString()
{
StringBuilder bodyNames = new StringBuilder();
foreach (Body body in Bodies)
{
bodyNames.Append(body.ToString() + "\n");
}
return bodyNames.ToString();
}
}
public interface IOrbital
{
Plane OrbitalPlane { get; set; }
Body ParentBody { get; set; }
long SemiMajorAxis { get; set; }
double Eccentricity { get; set; }
double Inclination { get; set; }
double ArgPer { get; set; }
double LongAsc { get; set; }
}
public interface ILaplace
{
Plane LaplacePlane { get; set; }
int apPrecPer { get; set; }
int nodPrecPer { get; set; }
}
public abstract class Body
{
public int ID { get; set; }
public string Name { get; set; }
public long GM { get; set; }
public int Radius { get; set; }
public int AtmosphereHeight { get; set; }
public Plane EqPlane { get; set; }
public double SidRotPeriod { get; set; }
// overrides
public override bool Equals(object obj)
{
if (((Body)obj).GetHashCode() == this.GetHashCode())
{
return true;
}
else return false;
}
public override int GetHashCode()
{
int newHash = GM.GetHashCode();
newHash ^= Radius.GetHashCode();
newHash ^= AtmosphereHeight.GetHashCode();
newHash ^= EqPlane.GetHashCode();
newHash ^= SidRotPeriod.GetHashCode();
return newHash;
}
public override string ToString()
{
return Name;
}
}
public class FixedOrbitingObject : IOrbital
{
public Plane OrbitalPlane { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public Body ParentBody { get; set; }
public long SemiMajorAxis { get; set; }
public double Eccentricity { get; set; }
public double Inclination { get; set; }
public double ArgPer { get; set; }
public double LongAsc { get; set; }
}
public class OrbitingObject : FixedOrbitingObject, ILaplace
{
public Plane LaplacePlane { get; set; }
public int apPrecPer { get; set; }
public int nodPrecPer { get; set; }
}
public class FixedOrbitingBody : Body, IOrbital
{
public Plane OrbitalPlane { get; set; }
public Body ParentBody { get; set; }
public long SemiMajorAxis { get; set; }
public double Eccentricity { get; set; }
public double Inclination { get; set; }
public double ArgPer { get; set; }
public double LongAsc { get; set; }
public long SOIRad { get; set; }
public void CalculateSOIRad()
{
}
}
public class OrbitingBody : FixedOrbitingBody, ILaplace
{
public Plane LaplacePlane { get; set; }
public int apPrecPer { get; set; }
public int nodPrecPer { get; set; }
}
public class Star : Body
{
public Plane ReferencePlane { get; set; }
new public BigInteger GM { get; set; }
}
public struct BigGM
{
public static BigInteger ENotation(double value, double displacement)
{
return (BigInteger)value * (BigInteger)Math.Pow(10, displacement);
}
}
}