From b73faf106720e84ec63a15f90517349caf949f47 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 08:40:52 +0000 Subject: [PATCH 1/3] Initial plan From a586daf62788d6274ec54a47e8ad7195373feac6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 09:02:31 +0000 Subject: [PATCH 2/3] Refactor code into modules: io, nwb/, world/ Co-authored-by: BlockCat <1307268+BlockCat@users.noreply.github.com> --- examples/layer.rs | 2 +- src/io.rs | 41 ++++++ src/lib.rs | 42 +------ src/nwb.rs | 193 ----------------------------- src/nwb/data.rs | 114 +++++++++++++++++ src/nwb/loader.rs | 86 +++++++++++++ src/nwb/mod.rs | 5 + src/ui/layers.rs | 4 +- src/world/mod.rs | 25 ++++ src/world/resources.rs | 54 ++++++++ src/{world.rs => world/systems.rs} | 110 +++------------- 11 files changed, 343 insertions(+), 333 deletions(-) create mode 100644 src/io.rs delete mode 100644 src/nwb.rs create mode 100644 src/nwb/data.rs create mode 100644 src/nwb/loader.rs create mode 100644 src/nwb/mod.rs create mode 100644 src/world/mod.rs create mode 100644 src/world/resources.rs rename src/{world.rs => world/systems.rs} (70%) diff --git a/examples/layer.rs b/examples/layer.rs index b3b37e3..e134c5b 100644 --- a/examples/layer.rs +++ b/examples/layer.rs @@ -1,4 +1,4 @@ -use bevy_dutch_road_highway_node_network::{nwb::NWBNetworkData, read_file, write_file}; +use bevy_dutch_road_highway_node_network::{io::{read_file, write_file}, nwb::NWBNetworkData}; use highway::generation::calculate_layer; use graph::{DirectedNetworkGraph, NetworkData}; diff --git a/src/io.rs b/src/io.rs new file mode 100644 index 0000000..d470c27 --- /dev/null +++ b/src/io.rs @@ -0,0 +1,41 @@ +use serde::{de::DeserializeOwned, Serialize}; +use std::{ + io::{Cursor, Write}, + path::Path, +}; + +pub fn write_file>( + value: &T, + path: P, +) -> Result<(), Box> { + use std::fs::File; + + println!("Started writing file: {:?}", path.as_ref()); + + let code = bincode::serialize(value)?; + let result = zstd::encode_all(Cursor::new(code), 0)?; + let mut file = File::create(&path)?; + + file.write_all(&result)?; + + println!("Finished writing file: {:?}", path.as_ref()); + + Ok(()) +} + +pub fn read_file>( + path: P, +) -> Result> { + use std::fs::File; + + println!("Started reading file: {:?}", path.as_ref()); + + let file = File::open(&path)?; + + let result = zstd::decode_all(file)?; + let d = bincode::deserialize(&result)?; + + println!("Finished reading file: {:?}", path.as_ref()); + + Ok(d) +} diff --git a/src/lib.rs b/src/lib.rs index 280c546..0e14774 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,48 +1,8 @@ #![feature(map_try_insert)] -use serde::{de::DeserializeOwned, Serialize}; -use std::{ - io::{Cursor, Write}, - path::Path, -}; pub mod camera; pub mod geo_coords; +pub mod io; pub mod nwb; pub mod ui; pub mod world; - -pub fn write_file>( - value: &T, - path: P, -) -> Result<(), Box> { - use std::fs::File; - - println!("Started writing file: {:?}", path.as_ref()); - - let code = bincode::serialize(value)?; - let result = zstd::encode_all(Cursor::new(code), 0)?; - let mut file = File::create(&path)?; - - file.write_all(&result)?; - - println!("Finished writing file: {:?}", path.as_ref()); - - Ok(()) -} - -pub fn read_file>( - path: P, -) -> Result> { - use std::fs::File; - - println!("Started reading file: {:?}", path.as_ref()); - - let file = File::open(&path)?; - - let result = zstd::decode_all(file)?; - let d = bincode::deserialize(&result)?; - - println!("Finished reading file: {:?}", path.as_ref()); - - Ok(d) -} diff --git a/src/nwb.rs b/src/nwb.rs deleted file mode 100644 index f5dba5b..0000000 --- a/src/nwb.rs +++ /dev/null @@ -1,193 +0,0 @@ -use bevy::math::Vec2; -use bevy_shapefile::{JunctionId, RoadId, RoadMap}; -use graph::{ - builder::{DirectedNetworkBuilder, EdgeBuilder, EdgeDirection, NodeBuilder}, - DirectedNetworkGraph, EdgeId, NetworkData, NodeId, ShortcutState, -}; -use rusqlite::{ - types::{FromSql, FromSqlError}, - Connection, -}; -use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, hash::Hash, path::Path}; - -#[derive(Clone, Debug, Default, Serialize, Deserialize)] -pub struct NWBNetworkData { - pub node_junctions: Vec<(JunctionId, Vec2)>, - edge_id: Vec, // for sql, not nwb road_id -} - -impl NetworkData for NWBNetworkData { - type NodeData = (JunctionId, Vec2); - type EdgeData = RoadId; - - fn node_data(&self, node: NodeId) -> &Self::NodeData { - &self.node_junctions[node.0 as usize] - } - - fn edge_data(&self, edge: EdgeId) -> &Self::EdgeData { - &self.edge_id[edge.0 as usize] - } - - fn with_size(node_size: usize, edge_size: usize) -> Self { - NWBNetworkData { - node_junctions: vec![(0.into(), Vec2::ZERO); node_size], - edge_id: vec![0.into(); edge_size], - } - } - - fn add_node(&mut self, node: NodeId, data: Self::NodeData) { - self.node_junctions[node.0 as usize] = data; - } - - fn add_edge(&mut self, edge: EdgeId, data: Self::EdgeData, _: ShortcutState) { - self.edge_id[edge.0 as usize] = data; - } - - fn edge_road_id(&self, edge: EdgeId) -> graph::ShortcutState { - ShortcutState::Single(self.edge_id[edge.0 as usize].num()) - } -} - -#[derive(Debug)] -pub struct JunctionNode { - pub junction_id: JunctionId, - pub location: Vec2, -} - -impl PartialEq for JunctionNode { - fn eq(&self, other: &Self) -> bool { - self.junction_id == other.junction_id - } -} - -impl Eq for JunctionNode {} - -impl Hash for JunctionNode { - fn hash(&self, state: &mut H) { - self.junction_id.hash(state); - } -} - -impl NodeBuilder for JunctionNode { - type Data = (JunctionId, Vec2); - - fn data(&self) -> Self::Data { - (self.junction_id, self.location) - } - - fn id(&self) -> u32 { - self.junction_id.num() as u32 - } -} - -#[derive(Debug, Clone)] -pub struct RoadEdge { - sql_id: RoadId, // Points to sql - distance: f32, - source: NodeId, - target: NodeId, - direction: EdgeDirection, -} - -impl EdgeBuilder for RoadEdge { - type Data = RoadId; - - fn data(&self) -> Self::Data { - self.sql_id - } - - fn source(&self) -> graph::NodeId { - self.source - } - - fn target(&self) -> graph::NodeId { - self.target - } - - fn weight(&self) -> f32 { - self.distance - } - - fn direction(&self) -> graph::builder::EdgeDirection { - self.direction - } - - fn road_id(&self) -> ShortcutState { - ShortcutState::Single(self.sql_id.num()) - } -} - -#[derive(Debug, Clone, Copy)] -struct RijRichting(EdgeDirection); - -impl FromSql for RijRichting { - fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult { - let rij_richting = String::column_result(value)?; - let rij_richting = rij_richting - .chars() - .next() - .ok_or(FromSqlError::InvalidType)?; - match rij_richting { - 'H' => Ok(RijRichting(EdgeDirection::Forward)), - 'T' => Ok(RijRichting(EdgeDirection::Backward)), - 'B' => Ok(RijRichting(EdgeDirection::Both)), - 'O' => Ok(RijRichting(EdgeDirection::Both)), - _ => Err(FromSqlError::InvalidType), - } - } -} - -pub fn preprocess_roadmap>( - roadmap: &RoadMap, - database: P, -) -> DirectedNetworkGraph { - let database = Connection::open(database).expect("Could not open database"); - - let mut builder: DirectedNetworkBuilder = DirectedNetworkBuilder::new(); - - - let statement = database - .prepare("SELECT id,JTE_ID_BEG, JTE_ID_END, RIJRICHTNG FROM wegvakken") - .expect("Could not prepare statement") - .query_map([], |f| { - let id: usize = f.get(0)?; - let junction_start: usize = f.get(1)?; - let junction_end: usize = f.get(2)?; - let rij_richting: RijRichting = f.get(3)?; - - let id = RoadId::from(id); - let junction_start = JunctionId::from(junction_start); - let junction_end = JunctionId::from(junction_end); - - Ok((id, (junction_start, junction_end, rij_richting))) - }) - .expect("Could not") - .map(|x| x.unwrap()) - .collect::>(); - - for (road_id, section) in roadmap.roads() { - let (road_id_start, road_id_end, rij_richting) = statement[&road_id]; - - let source = builder.add_node(JunctionNode { - junction_id: road_id_start, - location: *section.points.first().unwrap(), - }); - let target = builder.add_node(JunctionNode { - junction_id: road_id_end, - location: *section.points.last().unwrap(), - }); - - let distance = section.points.windows(2).map(|w| w[0].distance(w[1])).sum(); - - builder.add_edge(RoadEdge { - source, - target, - direction: rij_richting.0, - distance, - sql_id: *road_id, - }); - } - - builder.build() -} diff --git a/src/nwb/data.rs b/src/nwb/data.rs new file mode 100644 index 0000000..c5c24b9 --- /dev/null +++ b/src/nwb/data.rs @@ -0,0 +1,114 @@ +use bevy::math::Vec2; +use bevy_shapefile::{JunctionId, RoadId}; +use graph::{ + builder::{EdgeBuilder, EdgeDirection, NodeBuilder}, + EdgeId, NetworkData, NodeId, ShortcutState, +}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Default, Serialize, Deserialize)] +pub struct NWBNetworkData { + pub node_junctions: Vec<(JunctionId, Vec2)>, + edge_id: Vec, // for sql, not nwb road_id +} + +impl NetworkData for NWBNetworkData { + type NodeData = (JunctionId, Vec2); + type EdgeData = RoadId; + + fn node_data(&self, node: NodeId) -> &Self::NodeData { + &self.node_junctions[node.0 as usize] + } + + fn edge_data(&self, edge: EdgeId) -> &Self::EdgeData { + &self.edge_id[edge.0 as usize] + } + + fn with_size(node_size: usize, edge_size: usize) -> Self { + NWBNetworkData { + node_junctions: vec![(0.into(), Vec2::ZERO); node_size], + edge_id: vec![0.into(); edge_size], + } + } + + fn add_node(&mut self, node: NodeId, data: Self::NodeData) { + self.node_junctions[node.0 as usize] = data; + } + + fn add_edge(&mut self, edge: EdgeId, data: Self::EdgeData, _: ShortcutState) { + self.edge_id[edge.0 as usize] = data; + } + + fn edge_road_id(&self, edge: EdgeId) -> graph::ShortcutState { + ShortcutState::Single(self.edge_id[edge.0 as usize].num()) + } +} + +#[derive(Debug)] +pub struct JunctionNode { + pub junction_id: JunctionId, + pub location: Vec2, +} + +impl PartialEq for JunctionNode { + fn eq(&self, other: &Self) -> bool { + self.junction_id == other.junction_id + } +} + +impl Eq for JunctionNode {} + +impl std::hash::Hash for JunctionNode { + fn hash(&self, state: &mut H) { + self.junction_id.hash(state); + } +} + +impl NodeBuilder for JunctionNode { + type Data = (JunctionId, Vec2); + + fn data(&self) -> Self::Data { + (self.junction_id, self.location) + } + + fn id(&self) -> u32 { + self.junction_id.num() as u32 + } +} + +#[derive(Debug, Clone)] +pub struct RoadEdge { + pub(super) sql_id: RoadId, // Points to sql + pub(super) distance: f32, + pub(super) source: NodeId, + pub(super) target: NodeId, + pub(super) direction: EdgeDirection, +} + +impl EdgeBuilder for RoadEdge { + type Data = RoadId; + + fn data(&self) -> Self::Data { + self.sql_id + } + + fn source(&self) -> graph::NodeId { + self.source + } + + fn target(&self) -> graph::NodeId { + self.target + } + + fn weight(&self) -> f32 { + self.distance + } + + fn direction(&self) -> graph::builder::EdgeDirection { + self.direction + } + + fn road_id(&self) -> ShortcutState { + ShortcutState::Single(self.sql_id.num()) + } +} diff --git a/src/nwb/loader.rs b/src/nwb/loader.rs new file mode 100644 index 0000000..6d2260a --- /dev/null +++ b/src/nwb/loader.rs @@ -0,0 +1,86 @@ +use bevy_shapefile::{JunctionId, RoadId, RoadMap}; +use graph::{ + builder::{DirectedNetworkBuilder, EdgeDirection}, + DirectedNetworkGraph, +}; +use rusqlite::{ + types::{FromSql, FromSqlError}, + Connection, +}; +use std::{collections::HashMap, path::Path}; + +use super::data::{JunctionNode, NWBNetworkData, RoadEdge}; + +#[derive(Debug, Clone, Copy)] +struct RijRichting(EdgeDirection); + +impl FromSql for RijRichting { + fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult { + let rij_richting = String::column_result(value)?; + let rij_richting = rij_richting + .chars() + .next() + .ok_or(FromSqlError::InvalidType)?; + match rij_richting { + 'H' => Ok(RijRichting(EdgeDirection::Forward)), + 'T' => Ok(RijRichting(EdgeDirection::Backward)), + 'B' => Ok(RijRichting(EdgeDirection::Both)), + 'O' => Ok(RijRichting(EdgeDirection::Both)), + _ => Err(FromSqlError::InvalidType), + } + } +} + +pub fn preprocess_roadmap>( + roadmap: &RoadMap, + database: P, +) -> DirectedNetworkGraph { + let database = Connection::open(database).expect("Could not open database"); + + let mut builder: DirectedNetworkBuilder = + DirectedNetworkBuilder::new(); + + let statement = database + .prepare("SELECT id,JTE_ID_BEG, JTE_ID_END, RIJRICHTNG FROM wegvakken") + .expect("Could not prepare statement") + .query_map([], |f| { + let id: usize = f.get(0)?; + let junction_start: usize = f.get(1)?; + let junction_end: usize = f.get(2)?; + let rij_richting: RijRichting = f.get(3)?; + + let id = RoadId::from(id); + let junction_start = JunctionId::from(junction_start); + let junction_end = JunctionId::from(junction_end); + + Ok((id, (junction_start, junction_end, rij_richting))) + }) + .expect("Could not") + .map(|x| x.unwrap()) + .collect::>(); + + for (road_id, section) in roadmap.roads() { + let (road_id_start, road_id_end, rij_richting) = statement[&road_id]; + + let source = builder.add_node(JunctionNode { + junction_id: road_id_start, + location: *section.points.first().unwrap(), + }); + let target = builder.add_node(JunctionNode { + junction_id: road_id_end, + location: *section.points.last().unwrap(), + }); + + let distance = section.points.windows(2).map(|w| w[0].distance(w[1])).sum(); + + builder.add_edge(RoadEdge { + source, + target, + direction: rij_richting.0, + distance, + sql_id: *road_id, + }); + } + + builder.build() +} diff --git a/src/nwb/mod.rs b/src/nwb/mod.rs new file mode 100644 index 0000000..d028922 --- /dev/null +++ b/src/nwb/mod.rs @@ -0,0 +1,5 @@ +pub mod data; +pub mod loader; + +pub use data::NWBNetworkData; +pub use loader::preprocess_roadmap; diff --git a/src/ui/layers.rs b/src/ui/layers.rs index 7740aaa..c586024 100644 --- a/src/ui/layers.rs +++ b/src/ui/layers.rs @@ -77,11 +77,11 @@ fn load_or_calculate, F>( where F: Fn() -> DirectedNetworkGraph, { - if let Ok(network) = crate::read_file(&path) { + if let Ok(network) = crate::io::read_file(&path) { network } else { let network = calculate(); - crate::write_file(&network, path).expect("Could not write"); + crate::io::write_file(&network, path).expect("Could not write"); network } diff --git a/src/world/mod.rs b/src/world/mod.rs new file mode 100644 index 0000000..99d03bd --- /dev/null +++ b/src/world/mod.rs @@ -0,0 +1,25 @@ +pub mod resources; +pub mod systems; + +pub use resources::{ + LoadedMaterials, WorldConfig, WorldEntity, WorldEntitySelectionType, WorldTracker, +}; +pub use systems::convert; + +use bevy::prelude::*; + +pub struct WorldPlugin { + pub config: WorldConfig, +} + +impl Plugin for WorldPlugin { + fn build(&self, app: &mut bevy::prelude::App) { + app.insert_resource(WorldTracker::default()) + .insert_resource(self.config.clone()) + .add_systems(Startup, systems::init_materials) + .add_systems(Startup, systems::init_road_map) + .add_systems(Update, systems::mark_on_changed_preprocess) + .add_systems(Update, systems::colour_system) // Used for drawing the layers + .add_systems(Update, systems::visible_entities); + } +} diff --git a/src/world/resources.rs b/src/world/resources.rs new file mode 100644 index 0000000..ddbba01 --- /dev/null +++ b/src/world/resources.rs @@ -0,0 +1,54 @@ +use bevy::prelude::*; +use bevy_polyline::prelude::PolylineMaterial; +use bevy_shapefile::RoadId; +use std::collections::HashMap; + +#[derive(Debug, Clone, Resource)] +pub struct WorldConfig { + pub road_map_path: String, + pub geopackage_path: String, + pub directed_graph_path: String, + + pub selected_colour: Color, + pub normal_colour: Color, +} + +#[derive(Resource)] +pub struct LoadedMaterials { + pub normal_material: Handle, + pub selected_material: Handle, + + pub outgoing_material: Handle, + pub incoming_material: Handle, + pub route_material: Handle, +} + +#[derive(Debug, Clone, Component)] +pub struct WorldEntity { + pub id: RoadId, + pub selected: WorldEntitySelectionType, +} + +#[derive(Debug, Clone)] +pub enum WorldEntitySelectionType { + NotSelected, + BaseSelected, + BiDirection, + Outgoing, + Incoming, + Route, +} + +#[derive(Debug, Default, Resource)] +pub struct WorldTracker { + pub map: HashMap, +} + +impl WorldTracker { + pub fn track(&mut self, id: RoadId, entity: Entity) { + self.map.insert(id, entity); + } + pub fn remove(&mut self, id: RoadId) { + self.map.remove(&id); + } +} diff --git a/src/world.rs b/src/world/systems.rs similarity index 70% rename from src/world.rs rename to src/world/systems.rs index 43c5a05..ed80bf5 100644 --- a/src/world.rs +++ b/src/world/systems.rs @@ -1,6 +1,6 @@ use crate::{ camera::MainCamera, - nwb::{self}, + nwb, ui::{DirectedNetworkGraphContainer, PreProcess}, }; use bevy::{math::bounding::Aabb2d, prelude::*}; @@ -10,79 +10,15 @@ use bevy_polyline::prelude::{ use bevy_shapefile::{RoadId, RoadMap, RoadSection}; use graph::DirectedNetworkGraph; use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, path::Path, }; -pub struct WorldPlugin { - pub config: WorldConfig, -} - -#[derive(Resource)] -pub struct LoadedMaterials { - normal_material: Handle, - selected_material: Handle, - - outgoing_material: Handle, - incoming_material: Handle, - route_material: Handle, -} - -#[derive(Debug, Clone, Resource)] -pub struct WorldConfig { - pub road_map_path: String, - pub geopackage_path: String, - pub directed_graph_path: String, - - pub selected_colour: Color, - pub normal_colour: Color, -} - -#[derive(Debug, Clone, Component)] -pub struct WorldEntity { - pub id: RoadId, - pub selected: WorldEntitySelectionType, -} - -#[derive(Debug, Clone)] -pub enum WorldEntitySelectionType { - NotSelected, - BaseSelected, - BiDirection, - Outgoing, - Incoming, - Route, -} - -#[derive(Debug, Default, Resource)] -pub struct WorldTracker { - pub map: HashMap, -} - -impl WorldTracker { - pub fn track(&mut self, id: RoadId, entity: Entity) { - self.map.insert(id, entity); - } - pub fn remove(&mut self, id: RoadId) { - self.map.remove(&id); - } -} - -impl Plugin for WorldPlugin { - fn build(&self, app: &mut bevy::prelude::App) { - app.insert_resource(WorldTracker::default()) - .insert_resource(self.config.clone()) - .add_systems(Startup, init_materials) - .add_systems(Startup, init_road_map) - .add_systems(Update, mark_on_changed_preprocess) - .add_systems(Update, colour_system) // Used for drawing the layers - // .add_systems(Update, test_algorithm) - // .add_systems(Update, help) - .add_systems(Update, visible_entities); - } -} +use super::resources::{ + LoadedMaterials, WorldConfig, WorldEntity, WorldEntitySelectionType, WorldTracker, +}; -fn init_materials( +pub fn init_materials( config: Res, mut commands: Commands, mut polyline_materials: ResMut>, @@ -141,7 +77,7 @@ fn init_materials( }); } -fn init_road_map(config: Res, mut commands: Commands) { +pub fn init_road_map(config: Res, mut commands: Commands) { let road_map = load_road_map(&config); let network = load_graph(config, &road_map); @@ -152,37 +88,19 @@ fn init_road_map(config: Res, mut commands: Commands) { println!("Nodes: {}", network.nodes().len()); println!("Edges: {}", network.edges().len()); - // let out = network - // .nodes() - // .iter() - // .map(|nn| nn.out_len()) - // .collect::>(); - - // println!( - // "out_edges: [avg: {}, min: {}, max: {}", - // out.iter().sum::() as f32 / out.len() as f32, - // out.iter().min().unwrap(), - // out.iter().max().unwrap() - // ); - - // let next_level_edges = network::calculate_layer(30, &network, 2.0); - // println!("Collected phase1 edges: {}", next_level_edges.len()); - commands.insert_resource(road_map); commands.insert_resource(DirectedNetworkGraphContainer(network)); - - // commands.insert_resource(next_level_edges); } fn load_road_map(config: &Res) -> RoadMap { - if let Ok(road_map) = crate::read_file(&config.road_map_path) { + if let Ok(road_map) = crate::io::read_file(&config.road_map_path) { road_map } else { println!("File {:?} not found, creating...", config.road_map_path); let road_map = RoadMap::from_geopackage(&config.geopackage_path).expect("Could not load road map"); - crate::write_file(&road_map, &config.road_map_path).expect("Could not write road_map"); + crate::io::write_file(&road_map, &config.road_map_path).expect("Could not write road_map"); road_map } @@ -194,17 +112,17 @@ fn load_graph( ) -> DirectedNetworkGraph { let network_path = Path::new(&config.directed_graph_path); - if let Ok(network) = crate::read_file(network_path) { + if let Ok(network) = crate::io::read_file(network_path) { network } else { println!("File {:?} not found, creating...", network_path); let network = nwb::preprocess_roadmap(road_map, &config.geopackage_path); - crate::write_file(&network, network_path).expect("Could not write network"); + crate::io::write_file(&network, network_path).expect("Could not write network"); network } } -fn mark_on_changed_preprocess( +pub fn mark_on_changed_preprocess( mut tracker: ResMut, preprocess: Option>, mut q_camera: Query<(&Camera, &GlobalTransform, &mut Transform), (With,)>, @@ -216,7 +134,7 @@ fn mark_on_changed_preprocess( } } -fn visible_entities( +pub fn visible_entities( mut commands: Commands, materials: Res, road_map: Res, @@ -265,7 +183,7 @@ fn visible_entities( } } -fn colour_system( +pub fn colour_system( loaded_materials: Res, mut query: Query<(&mut WorldEntity, &mut PolylineMaterialHandle)>, ) { From 4fa2d234585444250e2caa009548e6f465f07f08 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 23 Feb 2026 09:23:18 +0000 Subject: [PATCH 3/3] Continue reducing code: simplify helpers, remove dead code, tighten visibility Co-authored-by: BlockCat <1307268+BlockCat@users.noreply.github.com> --- components/bevy_shapefile/src/spatial.rs | 69 ++++++------------------ components/highway/src/generation/mod.rs | 23 -------- src/camera.rs | 11 ---- src/main.rs | 10 ---- src/nwb/mod.rs | 4 +- src/ui/filter.rs | 4 +- src/ui/layers.rs | 1 - src/world/mod.rs | 9 ++-- 8 files changed, 22 insertions(+), 109 deletions(-) diff --git a/components/bevy_shapefile/src/spatial.rs b/components/bevy_shapefile/src/spatial.rs index 2c93710..6be5c81 100644 --- a/components/bevy_shapefile/src/spatial.rs +++ b/components/bevy_shapefile/src/spatial.rs @@ -80,70 +80,31 @@ impl FromSql for RoadSection { } } -fn read_f64<'a, I>(iter: &mut I, is_big_endian: bool) -> rusqlite::types::FromSqlResult +fn read_bytes<'a, I, const N: usize>(iter: &mut I) -> rusqlite::types::FromSqlResult<[u8; N]> where I: Iterator, { - let bytes: [u8; 8] = [ - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - ]; - - let value = if is_big_endian { - f64::from_be_bytes(bytes) - } else { - f64::from_le_bytes(bytes) - }; + let mut bytes = [0u8; N]; + for b in &mut bytes { + *b = *iter.next().ok_or(rusqlite::types::FromSqlError::InvalidType)?; + } + Ok(bytes) +} - Ok(value) +fn read_f64<'a, I>(iter: &mut I, is_big_endian: bool) -> rusqlite::types::FromSqlResult +where + I: Iterator, +{ + let bytes = read_bytes(iter)?; + Ok(if is_big_endian { f64::from_be_bytes(bytes) } else { f64::from_le_bytes(bytes) }) } fn read_u32<'a, I>(iter: &mut I, is_big_endian: bool) -> rusqlite::types::FromSqlResult where I: Iterator, { - let bytes: [u8; 4] = [ - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - *iter - .next() - .ok_or(rusqlite::types::FromSqlError::InvalidType)?, - ]; - let value = if is_big_endian { - u32::from_be_bytes(bytes) - } else { - u32::from_le_bytes(bytes) - }; - Ok(value) + let bytes = read_bytes(iter)?; + Ok(if is_big_endian { u32::from_be_bytes(bytes) } else { u32::from_le_bytes(bytes) }) } #[derive(Serialize, Deserialize, Debug)] diff --git a/components/highway/src/generation/mod.rs b/components/highway/src/generation/mod.rs index 1cd7412..22502cd 100644 --- a/components/highway/src/generation/mod.rs +++ b/components/highway/src/generation/mod.rs @@ -19,29 +19,6 @@ macro_rules! stopwatch { (end - start, value) }}; - ($x:block) => {{ - let start = std::time::Instant::now(); - let value = $x; - let end = std::time::Instant::now(); - - (end - start, value) - }}; - - (print $x:block) => {{ - let (duration, value) = stopwatch!($x); - - println!("Duration: {}µs", duration.as_micros()); - - value - }}; - - (print $x:expr) => {{ - let (duration, value) = stopwatch!($x); - - println!("Duration: {}µs", duration.as_micros()); - - value - }}; } pub fn calculate_layer( diff --git a/src/camera.rs b/src/camera.rs index 1cdcfaa..e838d62 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -48,17 +48,6 @@ fn load_camera(mut commands: Commands) { OrthographicProjection::default_3d(), )) .insert(MainCamera); - // commands - // .spawn(Camera3d { - // transform: , - // projection: Projection::Orthographic(Default::default()), - // camera: Camera { - // hdr: true, - // ..default() - // }, - // ..default() - // }) - // .insert(MainCamera); } fn camera_system_zoom( diff --git a/src/main.rs b/src/main.rs index 61180a1..dd739a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,3 @@ -#![feature(iter_collect_into)] -#![feature(map_try_insert)] -#![feature(test)] -extern crate test; - use bevy::{prelude::*, DefaultPlugins}; use bevy_dutch_road_highway_node_network::{ camera::{CameraConfig, CameraPlugin}, @@ -14,16 +9,11 @@ use bevy_polyline::PolylinePlugin; fn main() { App::new() .insert_resource(ClearColor(Color::linear_rgb(0.0, 0.0, 0.2))) - // .insert_resource(Msaa::Sample4) .add_plugins(DefaultPlugins) .add_plugins(HighwayUiPlugin) .add_plugins(PolylinePlugin) - // .add_plugins(LogDiagnosticsPlugin::default()) - // .add_plugins(FrameTimeDiagnosticsPlugin::default()) .add_plugins(WorldPlugin { config: WorldConfig { - // database_path: "data/database.db".into(), - // shapefile_path: "data/01-05-2024/Wegvakken/Wegvakken.shp".into(), geopackage_path: "data/01-02-2026/Wegvakken/Wegvakken.gpkg".into(), road_map_path: "data/road_map.data".into(), directed_graph_path: "data/directed_graph.graph".into(), diff --git a/src/nwb/mod.rs b/src/nwb/mod.rs index d028922..57672d3 100644 --- a/src/nwb/mod.rs +++ b/src/nwb/mod.rs @@ -1,5 +1,5 @@ -pub mod data; -pub mod loader; +mod data; +mod loader; pub use data::NWBNetworkData; pub use loader::preprocess_roadmap; diff --git a/src/ui/filter.rs b/src/ui/filter.rs index 16c76f1..8ad06fc 100644 --- a/src/ui/filter.rs +++ b/src/ui/filter.rs @@ -3,7 +3,5 @@ use bevy::app::{App, Plugin}; pub struct FilterUIPlugin; impl Plugin for FilterUIPlugin { - fn build(&self, app: &mut App) {} + fn build(&self, _app: &mut App) {} } - -pub struct FilterResource {} diff --git a/src/ui/layers.rs b/src/ui/layers.rs index c586024..52e14b4 100644 --- a/src/ui/layers.rs +++ b/src/ui/layers.rs @@ -195,7 +195,6 @@ impl PreProcess { .collect::>(); println!("Base line of: {}", road_data_level.len()); - // process_edges(0, &base, &mut road_data_level); for (layer_id, layer) in layers.iter().enumerate() { process_edges(layer_id as u8 + 1, layer, &mut road_data_level); diff --git a/src/world/mod.rs b/src/world/mod.rs index 99d03bd..73be206 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1,12 +1,11 @@ -pub mod resources; -pub mod systems; +mod resources; +mod systems; -pub use resources::{ - LoadedMaterials, WorldConfig, WorldEntity, WorldEntitySelectionType, WorldTracker, -}; +pub use resources::{WorldConfig, WorldEntity, WorldEntitySelectionType}; pub use systems::convert; use bevy::prelude::*; +use resources::WorldTracker; pub struct WorldPlugin { pub config: WorldConfig,