Prevent neighborhood_containing from crashing in North Leeds. [rebuild] [release]

This commit is contained in:
Dustin Carlino 2022-01-02 17:48:25 +00:00
parent 803f387428
commit 8d0b891406
3 changed files with 30 additions and 18 deletions

View File

@ -82,13 +82,13 @@ impl Partitioning {
Partitioning { neighborhoods }
}
pub fn neighborhood_containing(&self, find_block: &Block) -> NeighborhoodID {
pub fn neighborhood_containing(&self, find_block: &Block) -> Option<NeighborhoodID> {
// TODO We could probably build this mapping up when we do Perimeter::merge_all
for (id, (block, _)) in &self.neighborhoods {
if block.perimeter.contains(&find_block.perimeter) {
return *id;
return Some(*id);
}
}
panic!("Can't find neighborhood containing some block");
None
}
}

View File

@ -69,8 +69,16 @@ impl SelectBoundary {
for (idx, block) in blocks.into_iter().enumerate() {
let id = BlockID(idx);
let neighborhood = app.session.partitioning.neighborhood_containing(&block);
state.block_to_neighborhood.insert(id, neighborhood);
if let Some(neighborhood) = app.session.partitioning.neighborhood_containing(&block)
{
state.block_to_neighborhood.insert(id, neighborhood);
} else {
// TODO What happened?
error!(
"Block doesn't belong to any neighborhood?! {:?}",
block.perimeter
);
}
if initial_boundary.contains(&block.perimeter) {
state.selected.insert(id);
}
@ -91,11 +99,13 @@ impl SelectBoundary {
fn add_block(&mut self, ctx: &mut EventCtx, app: &App, id: BlockID) {
let color = if self.selected.contains(&id) {
SELECTED
} else {
} else if let Some(neighborhood) = self.block_to_neighborhood.get(&id) {
// Use the original color. This assumes the partitioning has been updated, of
// course
let neighborhood = self.block_to_neighborhood[&id];
app.session.partitioning.neighborhoods[&neighborhood].1
app.session.partitioning.neighborhoods[neighborhood].1
} else {
// TODO A broken case, block has no neighborhood
Color::RED
};
if self.frontier.contains(&id) {

View File

@ -1,4 +1,5 @@
use std::collections::{BTreeSet, HashMap, HashSet};
use std::fmt;
use anyhow::Result;
@ -162,9 +163,7 @@ impl Perimeter {
}
if debug_failures {
println!("\nCommon: {:?}", common);
self.debug();
other.debug();
println!("\nCommon: {:?}\n{:?}\n{:?}", common, self, other);
}
// Check if all of the common roads are at the end of each perimeter,
@ -396,13 +395,6 @@ impl Perimeter {
Block::from_perimeter(map, self)
}
fn debug(&self) {
println!("Perimeter:");
for id in &self.roads {
println!("- {:?} of {}", id.side, id.road);
}
}
/// Does this perimeter completely enclose the other?
pub fn contains(&self, other: &Perimeter) -> bool {
other
@ -412,6 +404,16 @@ impl Perimeter {
}
}
impl fmt::Debug for Perimeter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
writeln!(f, "Perimeter:")?;
for id in &self.roads {
writeln!(f, "- {:?} of {}", id.side, id.road)?;
}
Ok(())
}
}
impl Block {
fn from_perimeter(map: &Map, perimeter: Perimeter) -> Result<Block> {
// Trace along the perimeter and build the polygon