Skip to content

Commit

Permalink
first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianMascolo committed May 9, 2024
1 parent 6881edc commit 80cc30e
Show file tree
Hide file tree
Showing 16 changed files with 637 additions and 0 deletions.
4 changes: 4 additions & 0 deletions gis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/target
Cargo.lock
.idea
/output
27 changes: 27 additions & 0 deletions gis/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "gis"
version = "0.3.0"
authors = [
"Carmine Spagnuolo <spagnuolocarmine@gmail.com>",
"Alessia Antelmi <aantelmi@unisa.it>",
"Matteo D'Auria <matdauria@unisa.it>",
"Daniele De Vinco <danieledevinco1996@gmail.com",
"Francesco Foglia <frafonia@gmail.com>",
"Pasquale Caramante <pasqcaramante@gmail.com>",
"Luca Postiglione <lucapostiglione10@gmail.com>",
"Giuseppe D'Ambrosio <giuseppe.dambrosio14@gmail.com>"
]
edition = "2021"
license = "MIT"
readme = "README.md"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
krabmaga = {path="/home/tirocinio/krABMaga"}
tinyfiledialogs = "3.9.1"
ndarray = "0.15.6"

[features]
visualization = ["krabmaga/visualization"]
visualization_wasm = ["krabmaga/visualization_wasm"]
49 changes: 49 additions & 0 deletions gis/Makefile.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[env]
ENV_DIR = {source="${CARGO_MAKE_PROFILE}", default_value = "debug", mapping = {release = "release"}}
CARGO_TARGET_DIR = {value = "target", condition = {env_not_set = ["CARGO_TARGET_DIR"]}}
CARGO_WASM_PATH = "${CARGO_TARGET_DIR}/wasm32-unknown-unknown/${ENV_DIR}/${CARGO_MAKE_CRATE_NAME}.wasm"
CARGO_PROFILE = "dev"
TARGET_DIR = "target"
CARGO_MANIFEST_DIR = "${CARGO_MAKE_WORKING_DIRECTORY}"

[env.release]
CARGO_RELEASE_ARGS = "--release"

[tasks.show-env]
command = "env"

[tasks.install-target-wasm32-unknown-unknown]
args = ["target", "install", "wasm32-unknown-unknown"]

command = "rustup"
[tasks.basic-http-server]
install_crate = {crate_name = "basic-http-server", binary = "basic-http-server", test_arg="--help"}

[tasks.wasm-bindgen-cli]
install_crate = {crate_name = "wasm-bindgen-cli", binary = "wasm-bindgen", test_arg="--help"}

[tasks.cargo-build-web]
args = ["build", "--target", "wasm32-unknown-unknown", "--features", "visualization_wasm", "@@split(CARGO_RELEASE_ARGS, )"]
command = "cargo"
dependencies = ["install-target-wasm32-unknown-unknown"]

[tasks.build-web]
args = ["--out-dir", "${TARGET_DIR}", "--out-name", "wasm", "--target", "web", "--no-typescript", "${CARGO_WASM_PATH}"]
command = "wasm-bindgen"
dependencies = ["cargo-build-web", "wasm-bindgen-cli"]

[tasks.build-native]
args = ["build", "--features", "visualization", "@@split(CARGO_RELEASE_ARGS, )"]
command = "cargo"

[tasks.run]
command = "${CARGO_TARGET_DIR}/${ENV_DIR}/${CARGO_MAKE_BINARY_EXECUTABLE_NAME}"
dependencies = ["build-native"]

[tasks.serve]
command = "basic-http-server"
args = ["-x"]
dependencies = ["build-web", "basic-http-server"]

[tasks.test]
disabled = true
24 changes: 24 additions & 0 deletions gis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# krABMaga template

A starting point to develop a simulation (and a visualization) for an agent-based model with krABMaga.
The project hierarchy is modeled as follows:
- assets: a folder to store the emoji assets used to represent agents. Other types of assets should be stored here.
- src:
- model: Rust files related to the simulation. The implementations in this folder should be strictly related to the simulation.
- visualization: Rust files related to the visualization.
- main.rs: The entry point of the project. There should be two `main`s, mutually exclusive, to run the simulation with or without the attached visualization.
- index.html: The entry point for the WebAssembly based visualization. Renders a simple page with the wasm.js output embedded in it.
- Makefile.toml: Cargo-make task sets to run the visualization natively or with WebAssembly.
- Cargo.toml: A simple Cargo.toml with krABMaga already defined as a dependency and with krABMaga features exposed as first-level features.

---

![](template.gif)

---

# How to run

- To run only the simulation, run `cargo run --release`.
- To run the native visualization, run `cargo make run --profile release`.
- To serve the web visualization locally, run `cargo make serve --profile release`.
Binary file added gis/assets/emojis/runner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gis/assets/fonts/FiraSans-Bold.ttf
Binary file not shown.
26 changes: 26 additions & 0 deletions gis/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<html>
<head>
<meta charset="utf-8"/>
<style>
body {
background: linear-gradient(
135deg,
white 0%,
white 49%,
black 49%,
black 51%,
white 51%,
white 100%
) repeat;
background-size: 20px 20px;
}
canvas {
background-color: white;
}
</style>
</head>
<script type="module">
import init from './target/wasm.js'
init()
</script>
</html>
73 changes: 73 additions & 0 deletions gis/maps/map.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
0.2717266103028919,
23.719256114054062
],
[
28.912889334367378,
24.005122774762825
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
28.33285787739456,
23.989100126953318
],
[
29.23641715476103,
0.1329339011088848
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
29.10866173200887,
0.36682206957736696
],
[
3.3976994887509875,
0.3714554190081145
]
],
"type": "LineString"
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"coordinates": [
[
3.67130675551266,
0.3712397532143825
],
[
1.38106762594918564,
23.759071404593442
]
],
"type": "LineString"
}
}
]
}
41 changes: 41 additions & 0 deletions gis/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#[cfg(not(any(feature = "visualization", feature = "visualization_wasm")))]
use krabmaga::*;

// Visualization specific imports
#[cfg(any(feature = "visualization", feature = "visualization_wasm"))]
use {
crate::visualization::map_vis::MapVis, krabmaga::bevy::prelude::Color,
krabmaga::visualization::visualization::Visualization,
};

// Global imports (needed for the simulation to run)
use crate::model::map::Map;

mod model;

#[cfg(any(feature = "visualization", feature = "visualization_wasm"))]
mod visualization;

pub static DISCRETIZATION: f32 = 10.0 / 1.5;
pub static TOROIDAL: bool = true;

// Main used when only the simulation should run, without any visualization.
#[cfg(not(any(feature = "visualization", feature = "visualization_wasm")))]
fn main() {}

// Main used when a visualization feature is applied.
#[cfg(any(feature = "visualization", feature = "visualization_wasm"))]
fn main() {
// Initialize the simulation and its visualization here.

let num_agents = 0;
let dim: (f32, f32) = (50., 50.);

let state = Map::new(dim, num_agents);
Visualization::default()
.with_window_dimensions(400., 400.)
.with_simulation_dimensions(dim.0 as f32, dim.1 as f32)
.with_background_color(Color::NONE)
.with_name("Gis")
.start::<MapVis, Map>(MapVis, state);
}
101 changes: 101 additions & 0 deletions gis/src/model/map.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use super::person::Person;
use krabmaga::engine::fields::dense_number_grid_2d::DenseNumberGrid2D;
use krabmaga::engine::fields::field::Field;
use krabmaga::engine::fields::field_2d::Field2D;
use krabmaga::engine::location::{Int2D, Real2D};
use krabmaga::{
engine::{schedule::Schedule, state::State},
rand::{self, Rng},
};

pub struct Map {
pub step: u64,
pub field: Field2D<Person>,
pub gis_field: DenseNumberGrid2D<i32>,
pub dim: (f32, f32),
pub num_agents: u32,
}

impl Map {
pub fn new(dim: (f32, f32), num_agents: u32) -> Map {
Map {
step: 0,
field: Field2D::new(dim.0, dim.1, 1.0, false),
gis_field: DenseNumberGrid2D::new(dim.0 as i32, dim.1 as i32),
dim,
num_agents,
}
}
}

impl State for Map {
fn update(&mut self, _step: u64) {
self.field.lazy_update();
println!("{:?}", self.gis_field.locs.is_empty());
}

fn reset(&mut self) {
self.step = 0;
//self.field = DenseNumberGrid2D::new(self.dim.0, self.dim.1);
}

fn init(&mut self, schedule: &mut Schedule) {
self.step = 0;

let mut rng = rand::thread_rng();

for i in 0..self.num_agents {
let r1: f32 = rng.gen();
let r2: f32 = rng.gen();
let last_d = Real2D { x: 0., y: 0. };
let loc = Real2D {
x: (self.dim.0 * r1) as f32,
y: (self.dim.1 * r2) as f32,
};
let agent = Person {
id: i,
loc,
last_d,
dir_x: 1.0,
dir_y: 1.0,
};
// Put the agent in your state
self.field.set_object_location(agent, loc);
schedule.schedule_repeating(Box::new(agent), 0., 0);
}
}

fn as_any(&self) -> &dyn std::any::Any {
self
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}

fn as_state_mut(&mut self) -> &mut dyn State {
self
}

fn as_state(&self) -> &dyn State {
self
}

fn after_step(&mut self, _schedule: &mut Schedule) {
self.step += 1
}

fn set_gis(&mut self, vec: Vec<i32>) {
for (index, i) in vec.iter().enumerate() {
self.gis_field.set_value_location(
*i,
&Int2D {
x: index as i32 % self.dim.0 as i32,
y: index as i32 / self.dim.0 as i32,
},
);
}

//volendo posso mettere qua la generazione dell'agente
}
}
2 changes: 2 additions & 0 deletions gis/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod map;
pub mod person;
Loading

0 comments on commit 80cc30e

Please sign in to comment.