get halloween working with small lines. refactor a Line::maybe_new.

This commit is contained in:
Dustin Carlino 2019-02-01 12:01:54 -08:00
parent f5f8a57640
commit 92134f9125
10 changed files with 38 additions and 26 deletions

View File

@ -63,10 +63,10 @@ fn diff_all(primary_sim: &Sim, secondary_sim: &Sim) -> DiffAllState {
let mut lines: Vec<Line> = Vec::new();
for (trip, pt1) in &stats1.canonical_pt_per_trip {
if let Some(pt2) = stats2.canonical_pt_per_trip.get(trip) {
if pt1 == pt2 {
same_trips += 1;
if let Some(l) = Line::maybe_new(*pt1, *pt2) {
lines.push(l);
} else {
lines.push(Line::new(*pt1, *pt2));
same_trips += 1;
}
}
}

View File

@ -82,7 +82,7 @@ fn diff_trip(trip: TripID, ctx: &mut PluginCtx) -> DiffTripState {
let pt1 = primary_sim.get_stats().canonical_pt_per_trip.get(&trip);
let pt2 = secondary_sim.get_stats().canonical_pt_per_trip.get(&trip);
let line = if pt1.is_some() && pt2.is_some() {
Some(Line::new(*pt1.unwrap(), *pt2.unwrap()))
Line::maybe_new(*pt1.unwrap(), *pt2.unwrap())
} else {
None
};

View File

@ -44,11 +44,12 @@ impl Plugin for WarpState {
&ctx.primary.draw_map,
) {
let at = ctx.canvas.center_to_map_pt();
if at.epsilon_eq(pt) {
if let Some(l) = Line::maybe_new(at, pt) {
*self = WarpState::Warping(Instant::now(), l, id);
} else {
ctx.primary.current_selection = Some(id);
return false;
}
*self = WarpState::Warping(Instant::now(), Line::new(at, pt), id);
} else {
return false;
}

View File

@ -15,6 +15,13 @@ impl Line {
Line(pt1, pt2)
}
pub fn maybe_new(pt1: Pt2D, pt2: Pt2D) -> Option<Line> {
if pt1.epsilon_eq(pt2) {
return None;
}
Some(Line::new(pt1, pt2))
}
pub fn infinite(&self) -> InfiniteLine {
InfiniteLine(self.0, self.1)
}

View File

@ -16,14 +16,15 @@ pub struct PolyLine {
impl PolyLine {
pub fn new(pts: Vec<Pt2D>) -> PolyLine {
assert!(pts.len() >= 2);
let length = pts.windows(2).fold(Distance::ZERO, |so_far, pair| {
so_far + pair[0].dist_to(pair[1])
});
// This checks no lines are too small. Could take the other approach and automatically
// squish down points here and make sure the final result is at least EPSILON_DIST.
// But probably better for the callers to do this -- they have better understanding of what
// needs to be squished down, why, and how.
if pts.windows(2).any(|pair| pair[0].epsilon_eq(pair[1])) {
let length = pts.windows(2).fold(Distance::ZERO, |so_far, pair| {
so_far + pair[0].dist_to(pair[1])
});
panic!(
"PL with total length {} and {} pts has ~dupe pts: {:?}",
length,
@ -32,10 +33,6 @@ impl PolyLine {
);
}
let length = pts.windows(2).fold(Distance::ZERO, |so_far, pair| {
so_far + Line::new(pair[0], pair[1]).length()
});
// Can't have duplicates! If the polyline ever crosses back on itself, all sorts of things
// are broken.
let seen_pts: HashSet<HashablePt2D> =

View File

@ -26,12 +26,18 @@ struct UI {
impl UI {
fn new(flags: Flags) -> UI {
let map = Map::new(
&flags.load_map,
MapEdits::new("map name"),
&mut Timer::new("load map for Halloween"),
)
.unwrap();
// TODO Consolidate with sim::load
let map: Map = if flags.load_map.contains("data/raw_maps/") {
Map::new(
&flags.load_map,
MapEdits::new("map name"),
&mut Timer::new("load map"),
)
.unwrap()
} else {
abstutil::read_binary(&flags.load_map, &mut Timer::new("load map")).unwrap()
};
UI {
draw_map: DrawMap::new(map),
cycler: Cycler::new(ANIMATION_PERIOD_S),

View File

@ -120,14 +120,15 @@ impl DrawBuilding {
.translate(-1.0 * (1.0 - percent) * dx, -1.0 * (1.0 - percent) * dy),
);
let new_line = Line::new(
if let Some(new_line) = Line::maybe_new(
self.line.pt1(),
Pt2D::new(
self.line.pt1().x() + percent * dx,
self.line.pt1().y() + percent * dy,
),
);
g.draw_rounded_line(PATH, LINE_WIDTH, &new_line);
) {
g.draw_rounded_line(PATH, LINE_WIDTH, &new_line);
}
}
fn get_bounds(&self) -> Bounds {

View File

@ -75,8 +75,8 @@ fn trim_front_path(bldg_points: &Vec<Pt2D>, path: Line) -> Line {
for bldg_line in bldg_points.windows(2) {
let l = Line::new(bldg_line[0], bldg_line[1]);
if let Some(hit) = l.intersection(&path) {
if !hit.epsilon_eq(path.pt2()) {
return Line::new(hit, path.pt2());
if let Some(l) = Line::maybe_new(hit, path.pt2()) {
return l;
}
}
}

View File

@ -182,7 +182,7 @@ fn generalized_trim_back(
endpoints = Pt2D::approx_dedupe(endpoints, Distance::meters(1.0));
let center = Pt2D::center(&endpoints);
endpoints.sort_by_key(|pt| Line::new(center, *pt).angle().normalized_degrees() as i64);
endpoints.sort_by_key(|pt| center.angle_to(*pt).normalized_degrees() as i64);
endpoints
}

View File

@ -1,3 +1,3 @@
#!/bin/bash
grep -R --exclude-dir=.git --exclude-dir=target --exclude-dir=data --exclude-dir=initial_maps --exclude=Cargo.lock --color=auto "$@"
grep -IR --exclude-dir=.git --exclude-dir=target --exclude-dir=data --exclude-dir=initial_maps --exclude=Cargo.lock --color=auto "$@"