28 void Reset(std::vector<Node> nodes = {},
bool preserveRecalculationCooldown =
false)
33 for (std::size_t i = 1; i <
nodes_.size(); ++i)
48 return std::hypot(a.
x - b.
x, a.
y - b.
y) * 2000.0f;
52 return std::fmod(std::atan2(b.
x - a.
x, a.
y - b.
y) * 180.0f / 3.14159265358979323846f + 360.0f, 360.0f);
56 return std::fmod(to - from + 540.0f, 360.0f) - 180.0f;
60 if (bearing <= -130.0f || bearing >= 130.0f)
return 5;
61 if (bearing <= -60.0f)
return 2;
62 if (bearing <= -30.0f)
return 1;
63 if (bearing >= 60.0f)
return 4;
64 if (bearing >= 30.0f)
return 3;
71 return (std::max)(120.0f, (std::min)(1000.0f,
TravelMeters(speedKph, 16.0f)));
77 return (std::max)(15.0f, (std::min)(100.0f,
TravelMeters(speedKph, 3.0f)));
80 std::vector<Cue>
Update(
Point position,
float speedKph,
float elapsedSeconds = 0.0f)
82 std::vector<Cue> cues;
83 if (
nodes_.empty() ||
completed_ || !std::isfinite(position.
x) || !std::isfinite(position.
y))
return cues;
85 const float seconds = std::isfinite(elapsedSeconds) ?
86 (std::max)(0.0f, (std::min)(1.0f, elapsedSeconds)) : 0.0f;
106 for (std::size_t i = 1; i <
nodes_.size(); ++i)
110 if (t >= 0.0f && t <= 1.0f && distance < best)
141 cues.push_back({
Completed, 0, 0.0f, 0 });
144 float deviationMeters;
157 std::size_t maneuver =
next_;
159 const std::size_t end = maneuver <
nodes_.size() ? maneuver :
nodes_.size() - 1;
161 for (std::size_t i =
next_ + 1; i <= end; ++i)
173 else if (remaining <= warningMeters &&
warned_ != maneuver)
186 const bool nextTurnAnnounced = maneuver <
nodes_.size() && (
warned_ == maneuver ||
executed_ == maneuver);
189 if (cues.empty() && !nextTurnAnnounced && (joinedFirstRoad || passedTurn) && remaining >= 50.0f)
201 return std::isfinite(speedKph) ? (std::max)(0.0f, speedKph) / 3.6f * seconds : 0.0f;
203 static constexpr std::size_t
None = (std::numeric_limits<std::size_t>::max)();
219 float offRoute = 0.0f;
245 if (
SegmentDistance(position, a, b, t) > 20.0f || t <= 0.0f || t > 1.0f)
return false;
246 const float dx = position.
x - previous.
x, dy = position.
y - previous.
y;
247 const float ex = b.x - a.x, ey = b.y - a.y;
248 const float forward = dx * ex + dy * ey;
251 return forward > 0.0f && forward * forward >= 0.5f * (dx * dx + dy * dy) * (ex * ex + ey * ey);
261 if (moved < 0.05f)
return;
265 float best = (std::min)(20.0f, currentDistance - 2.0f);
266 const float lookAhead = (std::min)(250.0f, (std::max)(50.0f, moved * 1.5f));
267 float skippedMeters = 0.0f;
268 std::size_t recovered =
next_;
269 for (std::size_t i =
next_ + 1; i <
nodes_.size() && skippedMeters <= lookAhead; ++i)
271 const auto a =
nodes_[i - 1].position;
272 const auto b =
nodes_[i].position;
277 if (t > 0.0f && t <= 1.0f && forward > 0.0f && distance < best)
284 if (recovered >
next_)
286 for (std::size_t i =
next_; i < recovered; ++i)
298 const float dx = b.
x - a.
x, dy = b.
y - a.
y;
299 const float lengthSquared = dx * dx + dy * dy;
300 t = lengthSquared > 0.0f ? ((p.
x - a.
x) * dx + (p.
y - a.
y) * dy) / lengthSquared : 0.0f;
301 const float clamped = (std::max)(0.0f, (std::min)(1.0f, t));
309 const float arrivalMeters = 50.0f;
312 for (std::size_t i =
next_ + 1; i <
nodes_.size() && remaining <= arrivalMeters; ++i)
314 if (remaining > arrivalMeters)
return false;
316 if (
next_ == 0)
return false;
324 if (i + 1 ==
nodes_.size())
return distance <= 20.0f;
325 if (distance <= 4.0f)
return true;
326 if (i == 0)
return false;
329 const auto a =
nodes_[i - 1].position;
330 const auto b =
nodes_[i].position;
331 const Point projected = { a.x + t * (b.x - a.x), a.y + t * (b.y - a.y) };
332 if (t >= 1.0f &&
DistanceMeters(p, projected) <= 20.0f)
return true;
333 if (i + 1 <
nodes_.size())
337 if (nextT > 0.0f && nextT <= 1.0f && nextDistance <= 5.0f && nextDistance <
DistanceMeters(p, projected))
353 for (std::size_t i = 1; i + 1 <
nodes_.size(); ++i)
361 std::size_t exit = i;
362 float internalMeters = 0.0f;
363 while (exit + 2 <
nodes_.size())
366 if (length > 15.0f || internalMeters + length > 20.0f)
break;
367 internalMeters += length;
390 return !cue.continuesStreet || (cue.direction != 1 && cue.direction != 3);
std::size_t ManeuverExit(std::size_t i) const
std::vector< std::size_t > maneuverExits_
void RecoverPassedNodes(Point position)
bool initialContinuePending_
bool Reached(Point p, std::size_t i) const
std::size_t NextNodeIndex() const
void Reset(std::vector< Node > nodes={}, bool preserveRecalculationCooldown=false)
static float SegmentDistance(Point p, Point a, Point b, float &t)
static float TravelMeters(float speedKph, float seconds)
bool ShouldSpeakExecution(std::size_t i) const
static float ExecuteDistanceMeters(float speedKph)
std::vector< Node > nodes_
float TurnAt(std::size_t i) const
bool ReachedRouteEnd(Point position) const
static float Heading(Point a, Point b)
bool ShouldRecalculate(Point position, float seconds, float &deviationMeters)
bool IsFollowingCurrentEdge(Point previous, Point position) const
static constexpr std::size_t None
static float DistanceMeters(Point a, Point b)
bool IsManeuver(std::size_t i) const
std::vector< Cue > Update(Point position, float speedKph, float elapsedSeconds=0.0f)
float rerouteWaitSeconds_
static float WarningDistanceMeters(float speedKph)
static float HeadingChange(float to, float from)
Cue ManeuverCue(CueType type, std::size_t i, float meters) const
float bestProgressMeters_
float recalculationCooldownSeconds_
std::vector< float > cumulativeMeters_
static int Direction(float bearing)
float WireDirection() const