-
Notifications
You must be signed in to change notification settings - Fork 1
/
CrashEmulation.cs
223 lines (189 loc) · 5 KB
/
CrashEmulation.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zeptomoby.OrbitTools;
namespace SatTracker
{
public class SatVector: Vector
{
public Satellite Satellite { get; set; }
public SatVector(Double x, Double y, Double z):base(x, y, z)
{
}
public SatVector(Vector vect, Satellite sat):base(vect.X, vect.Y, vect.Z)
{
Satellite = sat;
}
}
public class GeomentryContainer<T> where T : Vector
{
public Vector Min { get; set; }
public Vector Max { get; set; }
private Double m_Step;
private Double m_Radius;
private int m_HalfSize;
private List<T>[, ,] m_Cells;
public GeomentryContainer(Double MaxRadius, Double step = 100)
{
m_HalfSize = (int)Math.Floor(MaxRadius / step) + 1;
this.m_Step = step;
this.m_Radius = step * m_HalfSize;
m_Cells = new List<T>[m_HalfSize * 2, m_HalfSize * 2, m_HalfSize * 2];
}
public List<T> GetData(Double x, Double y, Double z)
{
int xIndex = (int)Math.Floor(x / m_Step) + m_HalfSize;
int yIndex = (int)Math.Floor(y / m_Step) + m_HalfSize;
int zIndex = (int)Math.Floor(z / m_Step) + m_HalfSize;
if (xIndex >= m_HalfSize * 2 || yIndex >= m_HalfSize * 2 || zIndex >= m_HalfSize * 2)
return null;
if (m_Cells[xIndex, yIndex, zIndex] == null)
m_Cells[xIndex, yIndex, zIndex] = new List<T>();
return m_Cells[xIndex, yIndex, zIndex];
}
public List<T> GetData(T v)
{
return GetData(v.X, v.Y, v.Z);
}
public void Add(T sv)
{
if (sv == null)
return;
var cell = GetData(sv);
if(cell != null)
cell.Add(sv);
}
public void Remove(T sv)
{
if (sv == null)
return;
var cell = GetData(sv);
if (cell == null) return;
if(cell.Contains(sv))
cell.Remove(sv);
}
}
public class CrashEmulation
{
public class SituationEventArgs : EventArgs
{
public TimeSpan EllapsedTime { get; set; }
public SituationEventArgs()
{
}
public SituationEventArgs(TimeSpan Time)
{
this.EllapsedTime = Time;
}
}
public class CrashEventArgs : EventArgs
{
public List<Satellite> Sats { get; set; }
public TimeSpan Time { get; set; }
public Double Dist { get; set; }
public CrashEventArgs()
{
}
public CrashEventArgs(List<Satellite> Sats, Double dist, TimeSpan Time)
{
this.Sats = Sats;
this.Time = Time;
this.Dist = dist;
}
}
private SatVector[] positions;
private GeomentryContainer<SatVector> map;
private List<Satellite> m_sats;
private TimeSpan SimulationTime;
private DateTime SimulationStart;
private TimeSpan SimulationStep;
private double CrashDistance;
public CrashEmulation(List<Satellite> sats, DateTime SimStartTime, TimeSpan step, Double CrashDist)
{
this.SimulationStart = SimStartTime;
this.m_sats = sats;
this.SimulationStep = step;
this.CrashDistance = CrashDist;
map = new GeomentryContainer<SatVector>(200000, 1000);
}
private bool flagStop = false;
public void Start()
{
flagStop = false;
var worker = new BackgroundWorker();
worker.DoWork += (s, args) =>
{
while(!flagStop) Step();
};
worker.RunWorkerCompleted += (s, args) => { };
worker.RunWorkerAsync();
}
public void Stop()
{
flagStop = true;
}
private void Step()
{
if (positions == null)
positions = new SatVector[m_sats.Count];
Parallel.For(0, m_sats.Count, (i) =>
{
try
{
var startEpoch = SimulationStart.Subtract(m_sats[i].Orbit.EpochTime);
while (startEpoch > m_sats[i].Orbit.Period)
startEpoch = startEpoch.Subtract(m_sats[i].Orbit.Period);
map.Remove(positions[i]);
positions[i] = new SatVector(m_sats[i].PositionEci(startEpoch.TotalMinutes + SimulationTime.TotalMinutes).Position, m_sats[i]);
map.Add(positions[i]);
}
catch
{
}
});
try
{
for (int i = 0; i < positions.Length; i++)
{
var p = positions[i];
var ps = map.GetData(p);
if (ps == null)
continue;
foreach (var item in ps)
{
if (item == null)
continue;
var dist = (p.X - item.X) * (p.X - item.X) +
(p.Y - item.Y) * (p.Y - item.Y) +
(p.Z - item.Z) * (p.Z - item.Z);
if (dist > 0 && dist < CrashDistance * CrashDistance && p.Satellite.Name.Trim() != item.Satellite.Name.Trim())
{
OnCrash(new List<Satellite>() { p.Satellite, item.Satellite }, Math.Sqrt(dist), SimulationTime);
Stop();
}
}
}
}
catch
{
}
OnStep(SimulationTime);
SimulationTime = SimulationTime.Add(SimulationStep);
}
public event EventHandler<SituationEventArgs> StepSituiation;
public event EventHandler<CrashEventArgs> Crash;
protected virtual void OnStep(TimeSpan EllapsedTime)
{
if (StepSituiation != null)
StepSituiation(this, new SituationEventArgs(EllapsedTime));
}
protected virtual void OnCrash(List<Satellite> sats,Double dist, TimeSpan Time)
{
if (Crash != null)
Crash(this, new CrashEventArgs(sats, dist, Time));
}
}
}