38 !std::isfinite(seconds) || seconds <= 0 ||
39 !std::isfinite(ship.mslKm) || ship.mslKm == -1 ||
40 !
Finite({ship.position.x,ship.position.y,ship.position.z}))
60 state.Clear(); state.enabled =
true; state.predictionEnabled = predictionEnabled;
62 state.gearDown = ship.geardown;
63 state.onGround = !airborne;
71 const float elapsed = seconds;
77 for (
short u = 16; u < 48; ++u)
for (
short v = 16; v < 48; ++v)
81 state.Clear(); state.enabled =
true; state.predictionEnabled = predictionEnabled;
82 state.altitudeKm = ship.mslKm;
84 state.gearDown = ship.geardown;
85 state.onGround = !airborne;
86 if (!airborne) state.verticalKms = 0;
88 state.displayRunwayKm = airborne ? -100.f : ship.mslKm;
92 if (runway.type != 2 || runway.heading < 0 || runway.heading > 360 ||
93 !std::isfinite(runway.length) || runway.length <= 0 || !std::isfinite(runway.elevationKm))
continue;
94 const auto delta = runway.location-ship.position;
96 if (std::isfinite(range) && range < nearestRunwayKm)
98 nearestRunwayKm = range;
99 state.displayRunwayKm = runway.elevationKm;
102 if (!std::isfinite(state.verticalKms)) { state.Clear();
terrainTrackValid =
false;
return; }
108 forward = ship.velocity-ship.barycentric;
112 if (speed >= .001f) forward /= speed;
134 flight.
radiusKm = (std::clamp)(ship.bound,0.f,.1f);
151 float bestApproachRange = FLT_MAX;
152 for (
size_t targetIndex = 0; targetIndex <
bus->
waypoint.size(); ++targetIndex)
155 const bool selected = targetIndex == size_t(
bus->
WPtargetC);
156 const Point site = project(-target.location);
162 const float height = flight.
altitudeKm-site.z;
163 const bool runway = target.heading >= 0 && target.heading <= 360 &&
164 std::isfinite(target.length) && target.length > 0;
166 const bool alignedRunway = runway &&
Finite(site) &&
168 std::fabs(courseError) < 3.f && std::fabs(site.x) < (std::max)(.1f,site.y*.052408f);
169 const bool inbound = alignedRunway && site.y > 0 && site.y < 42.6f &&
174 const bool overRunway = alignedRunway && ship.geardown && site.y <= 0 &&
176 const bool runwayApproach = inbound || overRunway;
177 const bool padApproach = selected &&
ourcockpit.
dntranslimit > 0 && !runway && std::isfinite(target.length) && ship.geardown &&
Finite(site) && site.y > 0 && site.y < 5 &&
178 std::fabs(site.x) < (std::max)(.1f,site.y*.2f) && height >= -.003f && height < .6f;
179 if (target.type == 2 && (runwayApproach || padApproach) && std::fabs(site.y) < bestApproachRange)
184 bestApproachRange = std::fabs(site.y);
190 state.obstaclesComplete =
true;
191 constexpr int fineTriangles[8][3] = {{0,1,3},{1,4,3},{1,2,4},{2,5,4},
192 {3,4,6},{4,7,6},{4,5,7},{5,8,7}};
193 constexpr int coarseTriangles[2][3] = {{0,2,6},{2,8,6}};
194 struct FootprintBounds
196 float lowX = FLT_MAX, highX = -FLT_MAX, lowY = FLT_MAX, highY = -FLT_MAX;
197 void Include(Point p)
199 lowX = (std::min)(lowX,p.x); highX = (std::max)(highX,p.x);
200 lowY = (std::min)(lowY,p.y); highY = (std::max)(highY,p.y);
202 bool Contains(Point p)
const
203 {
return p.x >= lowX && p.x <= highX && p.y >= lowY && p.y <= highY; }
205 struct CellGeometry : FootprintBounds
207 std::array<Point,9> points;
211 std::array<FootprintBounds,GridSize> rowBounds;
214 auto& cell = state.cells[u*
GridSize+v];
216 cell.u = source.
tcp.
u; cell.v = source.tcp.v;
217 if (!ValidVertex(source) ||
219 !ValidVertex(
gridarray[u+1][v+1]))
continue;
220 const bool fine = u >= 16 && u < 47 && v >= 16 && v < 47;
225 RiseMath::Vector3 vertices[] = {a,(a+b)*.5f,b,(a+c)*.5f,(a+d)*.5f,(b+d)*.5f,c,(c+d)*.5f,d};
232 !ValidVertex(
gridarray[u-1][v+1]))
continue;
235 if (u != 16 && u != 46 && v != 16 && v != 46) vertices[4] =
insidearray[u][v][2];
239 Point points[9];
bool valid =
true;
240 for (
int i = 0; i < 9; ++i)
242 points[i] = project(vertices[i]);
245 if (!valid)
continue;
246 auto& shape = geometry[u*
GridSize+v];
248 std::copy(std::begin(points),std::end(points),shape.points.begin());
249 for (
auto p : points)
252 rowBounds[u].Include(p);
254 cell.valid =
true; cell.topKm = points[0].z;
255 for (
auto p : points) cell.topKm = (std::max)(cell.topKm,p.z);
257 for (
int i = 0; i < (fine ? 8 : 2); ++i)
259 const auto& indices = fine ? fineTriangles[i] : coarseTriangles[i];
260 Point triangle[] = {points[indices[0]],points[indices[1]],points[indices[2]]};
261 cell.severity =
Max(cell.severity,predictor.
Scan(triangle,
false,
true,&state.terrainTime));
262 if (!cell.obstaclesKnown && predictor.
Intersects(triangle)) state.obstaclesComplete =
false;
264 state.terrain =
Max(state.terrain,cell.severity);
266 state.terrainComplete = !airborne || !predictionEnabled || speed < .001f ? state.cells[31*
GridSize+31].valid : predictor.
Covered();
273 std::vector<Obstacle> elevatedObstacles;
274 elevatedObstacles.reserve(state.obstacles.size());
275 for (
auto& obstacle : state.obstacles)
277 std::array<Point,8> points;
278 for (
size_t i = 0; i < points.size(); ++i)
280 const auto p = obstacle.corners[i];
281 points[i] = project({p.x,p.y,p.z});
283 if (std::any_of(points.begin(),points.end(),[](Point p) { return !Finite(p); }))
continue;
286 std::sort(points.begin(),points.end(),[](Point a, Point b) { return a.x == b.x ? a.y < b.y : a.x < b.x; });
287 std::vector<Point> hull;
289 for (
auto p : points)
291 while (hull.size() > 1 &&
Cross(hull[hull.size()-2],hull.back(),p) <= 0) hull.pop_back();
294 const auto lower = hull.size();
295 for (
int i =
int(points.size())-2; i >= 0; --i)
297 const auto p = points[i];
298 while (hull.size() > lower &&
Cross(hull[hull.size()-2],hull.back(),p) <= 0) hull.pop_back();
301 if (hull.size() > 1) hull.pop_back();
303 for (
auto p : points) { center.x += p.x/8; center.y += p.y/8; }
304 bool surfaceOnly =
false;
305 for (
int u = 0; u <
GridSize && !surfaceOnly; ++u)
310 if (!rowBounds[u].Contains(center))
continue;
311 for (
int v = 0; v <
GridSize && !surfaceOnly; ++v)
313 if (!state.cells[u*
GridSize+v].valid)
continue;
315 const auto& shape = geometry[u*
GridSize+v];
316 if (!shape.Contains(center))
continue;
317 for (
int i = 0; i < (shape.fine ? 8 : 2); ++i)
319 const auto& indices = shape.fine ? fineTriangles[i] : coarseTriangles[i];
320 const Point a = shape.points[indices[0]], b = shape.points[indices[1]], c = shape.points[indices[2]];
322 if (std::fabs(
area) < 1e-10f)
continue;
324 if (wa >= -.0001f && wb >= -.0001f && wc >= -.0001f &&
325 obstacle.topKm <= wa*a.z+wb*b.z+wc*c.z+.003f) surfaceOnly =
true;
329 if (surfaceOnly)
continue;
330 for (
auto& p : hull) p.z = obstacle.topKm;
331 for (
size_t i = 1; i+1 < hull.size(); ++i)
333 Point triangle[] = {hull[0],hull[i],hull[i+1]};
334 obstacle.severity =
Max(obstacle.severity,predictor.
Scan(triangle,
true,
false,&state.obstacleTime));
336 state.obstacle =
Max(state.obstacle,obstacle.severity);
337 elevatedObstacles.push_back(obstacle);
339 state.obstacles.swap(elevatedObstacles);