fix div by 0 bug with percent_dist_crossed for empty paths

This commit is contained in:
Dustin Carlino 2019-08-19 14:41:56 -07:00
parent 0ba3ab92ed
commit 7001139dc0
4 changed files with 12 additions and 4 deletions

View File

@ -132,6 +132,14 @@ impl Path {
self.total_length
}
pub fn percent_dist_crossed(&self) -> f64 {
// Sometimes this happens
if self.total_length == Distance::ZERO {
return 1.0;
}
self.crossed_so_far / self.total_length
}
pub fn is_last_step(&self) -> bool {
self.steps.len() == 1
}

View File

@ -146,7 +146,7 @@ impl Car {
.blocked_since
.map(|t| now - t)
.unwrap_or(Duration::ZERO),
percent_dist_crossed: path.crossed_so_far() / path.total_length(),
percent_dist_crossed: path.percent_dist_crossed(),
trip_time_so_far: now - self.started_at,
}
}

View File

@ -672,7 +672,7 @@ impl DrivingSimState {
.blocked_since
.map(|t| now - t)
.unwrap_or(Duration::ZERO),
percent_dist_crossed: path.crossed_so_far() / path.total_length(),
percent_dist_crossed: path.percent_dist_crossed(),
trip_time_so_far: now - car.started_at,
});
}

View File

@ -272,7 +272,7 @@ impl WalkingSimState {
vehicle_type: None,
pos: ped.get_draw_ped(now, map).pos,
time_spent_blocked: ped.blocked_since.map(|t| now - t).unwrap_or(Duration::ZERO),
percent_dist_crossed: ped.path.crossed_so_far() / ped.path.total_length(),
percent_dist_crossed: ped.path.percent_dist_crossed(),
trip_time_so_far: now - ped.started_at,
});
}
@ -509,7 +509,7 @@ impl Pedestrian {
.blocked_since
.map(|t| now - t)
.unwrap_or(Duration::ZERO),
percent_dist_crossed: self.path.crossed_so_far() / self.path.total_length(),
percent_dist_crossed: self.path.percent_dist_crossed(),
trip_time_so_far: now - self.started_at,
}
}