traffic_sim/lib.rs
1#![warn(missing_docs)]
2
3//! This is a simple traffic simulation module with a focus on performance and realism.
4//!
5//! This is a personal project, with currently poor documentation and is not quite feature complete.
6//!
7//! ## Goals
8//!
9//! The goal of this package is to be capable of simulating any road network in a realistic manner,
10//! with reasonable performance. This includes simulating things such as:
11//!
12//! * Car following
13//! * Lane-changing decisions and dynamics
14//! * Yielding at give way/stop signs
15//! * Merging and diverging
16//! * Simple path finding
17//!
18//! The primary gap right now is the lack of a lane-changing.
19//! There are pieces in place, but it is in general a very hard problem to solve.
20//!
21//! # Hello world
22//! This sample code demonstrates how to simulate a single vehicle on a straight, single-lane road.
23//!
24//! ```
25//! use traffic_sim::{Simulation, LinkAttributes, VehicleAttributes};
26//! use traffic_sim::math::{LineSegment2d, Point2d};
27//!
28//! // Create a simulation
29//! let mut simulation = Simulation::new();
30//!
31//! // Add a link, which is a single lane of traffic
32//! // This one is a straight line 100m long
33//! let link_id = simulation.add_link(&LinkAttributes {
34//! curve: &LineSegment2d::from_ends(Point2d::new(0.0, 0.0), Point2d::new(100.0, 0.0)),
35//! speed_limit: 16.6667, // m/s (60km/h)
36//! });
37//!
38//! // Add a vehicle to the start of the link we just created
39//! let veh_id = simulation.add_vehicle(&VehicleAttributes {
40//! width: 2.0, // m
41//! length: 5.0, // m
42//! wheel_base: 1.5, // m
43//! max_acc: 2.0, // m
44//! comf_dec: 2.0, // m
45//! }, link_id);
46//!
47//! // Simulate 10 frames, each advancing time by 0.1s.
48//! // Each frame, print out the coordinates of our single vehicle
49//! for _ in 0..10 {
50//! simulation.step(0.1);
51//! println!("Vehicle is at {:?}", simulation.get_vehicle(veh_id).position());
52//! }
53//! ```
54
55pub use cgmath;
56pub use group::LinkGroup;
57pub use light::TrafficLight;
58pub use link::{Link, LinkAttributes, TrafficControl};
59pub use simulation::Simulation;
60use slotmap::{new_key_type, SlotMap};
61pub use slotmap::{Key, KeyData};
62pub use util::Interval;
63pub use vehicle::{Vehicle, VehicleAttributes};
64
65mod conflict;
66mod debug;
67mod group;
68mod light;
69mod link;
70pub mod math;
71mod simulation;
72mod util;
73mod vehicle;
74
75new_key_type! {
76 /// Unique ID of a [Link].
77 pub struct LinkId;
78 /// Unique ID of a [Vehicle].
79 pub struct VehicleId;
80 /// Unique ID of a [TrafficLight].
81 pub struct TrafficLightId;
82}
83
84type LinkSet = SlotMap<LinkId, Link>;
85type VehicleSet = SlotMap<VehicleId, Vehicle>;