Use nalgebra

This commit is contained in:
Gender Shrapnel 2020-06-01 21:25:40 +02:00
parent b0bb187f75
commit 7c4a6a885e
5 changed files with 136 additions and 36 deletions

105
Cargo.lock generated
View File

@ -9,6 +9,15 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "approx"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0e60b75072ecd4168020818c0107f2857bb6c4e64252d8d3983f6263b40a5c3"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "arc-swap" name = "arc-swap"
version = "0.4.6" version = "0.4.6"
@ -69,7 +78,7 @@ dependencies = [
"block-padding", "block-padding",
"byte-tools", "byte-tools",
"byteorder", "byteorder",
"generic-array", "generic-array 0.12.3",
] ]
[[package]] [[package]]
@ -124,6 +133,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"futures", "futures",
"legion", "legion",
"nalgebra",
"rand 0.7.3", "rand 0.7.3",
"serde", "serde",
"serde_json", "serde_json",
@ -262,7 +272,7 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5"
dependencies = [ dependencies = [
"generic-array", "generic-array 0.12.3",
] ]
[[package]] [[package]]
@ -421,6 +431,15 @@ dependencies = [
"typenum", "typenum",
] ]
[[package]]
name = "generic-array"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed1e761351b56f54eb9dcd0cfaca9fd0daecf93918e1cfc01c8a3d26ee7adcd"
dependencies = [
"typenum",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.1.14" version = "0.1.14"
@ -678,6 +697,12 @@ version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
[[package]]
name = "libm"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
version = "0.3.4" version = "0.3.4"
@ -717,6 +742,15 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
[[package]]
name = "matrixmultiply"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4f7ec66360130972f34830bfad9ef05c6610a43938a467bcc9ab9369ab3478f"
dependencies = [
"rawpointer",
]
[[package]] [[package]]
name = "maybe-uninit" name = "maybe-uninit"
version = "2.0.0" version = "2.0.0"
@ -857,6 +891,24 @@ dependencies = [
"twoway", "twoway",
] ]
[[package]]
name = "nalgebra"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d630f146ae751cfb43db9e0ad6bd73819a546a38baa5d4a9de69823c389c78a1"
dependencies = [
"approx",
"generic-array 0.13.2",
"matrixmultiply",
"num-complex",
"num-rational",
"num-traits",
"rand 0.7.3",
"rand_distr",
"simba",
"typenum",
]
[[package]] [[package]]
name = "net2" name = "net2"
version = "0.2.34" version = "0.2.34"
@ -868,6 +920,16 @@ dependencies = [
"winapi 0.3.8", "winapi 0.3.8",
] ]
[[package]]
name = "num-complex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6b19411a9719e753aff12e5187b74d60d3dc449ec3f4dc21e3989c3f554bc95"
dependencies = [
"autocfg 1.0.0",
"num-traits",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.42" version = "0.1.42"
@ -878,6 +940,17 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "num-rational"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef"
dependencies = [
"autocfg 1.0.0",
"num-integer",
"num-traits",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.11" version = "0.2.11"
@ -885,6 +958,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" checksum = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096"
dependencies = [ dependencies = [
"autocfg 1.0.0", "autocfg 1.0.0",
"libm",
] ]
[[package]] [[package]]
@ -1222,6 +1296,15 @@ dependencies = [
"getrandom", "getrandom",
] ]
[[package]]
name = "rand_distr"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96977acbdd3a6576fb1d27391900035bf3863d4a16422973a409b488cf29ffb2"
dependencies = [
"rand 0.7.3",
]
[[package]] [[package]]
name = "rand_hc" name = "rand_hc"
version = "0.1.0" version = "0.1.0"
@ -1293,6 +1376,12 @@ dependencies = [
"rand_core 0.3.1", "rand_core 0.3.1",
] ]
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.3.0" version = "1.3.0"
@ -1494,6 +1583,18 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "simba"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d771bffb178827708f83829713e3a214bc8a9879a8f27ca548965bd849ed130"
dependencies = [
"approx",
"num-complex",
"num-traits",
"paste",
]
[[package]] [[package]]
name = "siphasher" name = "siphasher"
version = "0.2.3" version = "0.2.3"

View File

@ -10,6 +10,7 @@ license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
futures = { version = "0.3", default-features = false, features = ["alloc"] } futures = { version = "0.3", default-features = false, features = ["alloc"] }
legion = "0.2.1" legion = "0.2.1"
nalgebra = "0.21"
rand = "0.7.3" rand = "0.7.3"
serde = { version = "1.0", features= ["derive"] } serde = { version = "1.0", features= ["derive"] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -1,27 +1,23 @@
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use legion::prelude::*; use legion::prelude::*;
use nalgebra::{Vector3, Point3};
use rand::prelude::*; use rand::prelude::*;
use tokio::{sync::watch, time}; use tokio::{sync::watch, time};
use crate::state::{Object, State}; use crate::state::{Object, State};
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Name { struct Name (String);
name: String,
}
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Position { struct Id (u64);
x: f64,
y: f64,
}
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Velocity { struct Position (Point3<f64>);
dx: f64,
dy: f64, #[derive(Clone, Debug, PartialEq)]
} struct Velocity (Vector3<f64>);
pub struct Simulation { pub struct Simulation {
world: World, world: World,
@ -34,11 +30,13 @@ pub struct Simulation {
fn render(world: &mut World) -> Vec<Object> { fn render(world: &mut World) -> Vec<Object> {
let mut result = vec![]; let mut result = vec![];
for (name, pos) in <(Read<Name>, Read<Position>)>::query().iter(world) { for (id, name, pos) in <(Read<Id>, Read<Name>, Read<Position>)>::query().iter(world) {
result.push(Object { result.push(Object {
name: name.name.clone(), id: id.0,
x: pos.x, name: name.0.clone(),
y: pos.y, x: pos.0.x,
y: pos.0.y,
z: pos.0.z
}); });
} }
@ -55,14 +53,14 @@ impl Simulation {
(), (),
(0..999).map(|n| { (0..999).map(|n| {
( (
Name { Id(n),
name: format!("Entity {}", n), Name (format!("Entity {}", n)),
}, Position(Point3::new(0.0, 0.0, 0.0)),
Position { x: 0.0, y: 0.0 }, Velocity(Vector3::new(
Velocity { rng.gen_range(-1.0, 1.0),
dx: rng.gen_range(0.0, 1.0), rng.gen_range(-1.0, 1.0),
dy: rng.gen_range(0.0, 1.0), rng.gen_range(-1.0, 1.0),
}, )),
) )
}), }),
); );
@ -80,8 +78,7 @@ impl Simulation {
let now = Instant::now(); let now = Instant::now();
let dt = now.duration_since(self.last); let dt = now.duration_since(self.last);
for (mut pos, vel) in update_query.iter(&mut self.world) { for (mut pos, vel) in update_query.iter(&mut self.world) {
pos.x += vel.dx * dt.as_secs_f64(); pos.0 += vel.0 * dt.as_secs_f64();
pos.y += vel.dy * dt.as_secs_f64();
} }
self.last = now; self.last = now;
self.iteration += 1; self.iteration += 1;

View File

@ -3,8 +3,10 @@ use serde::Serialize;
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]
pub struct Object { pub struct Object {
pub name: String, pub name: String,
pub id: u64,
pub x: f64, pub x: f64,
pub y: f64, pub y: f64,
pub z: f64,
} }
#[derive(Clone, Debug, Serialize)] #[derive(Clone, Debug, Serialize)]

View File

@ -6,10 +6,10 @@
<body> <body>
<h1>Hello, world!</h1> <h1>Hello, world!</h1>
<p id="iteration">Iteration: {{ iteration }}<p> <p id="iteration">Iteration: {{ iteration }} messages 0.<p>
<ul id="objects"> <ul id="objects">
{% for object in objects %} {% for object in objects %}
<li>{{ object.name }} @ {{ object.x }}, {{ object.y }}</li> <li id="object-{{ object.id }}">{{ object.name }} @ {{ object.x }}, {{ object.y }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
@ -21,18 +21,17 @@
console.log("Connected"); console.log("Connected");
ws.send("MOAR"); ws.send("MOAR");
} }
let messages = 0;
ws.onmessage = function(e) { ws.onmessage = function(e) {
const data = JSON.parse(e.data); const data = JSON.parse(e.data);
messages += 1;
document.getElementById("iteration").innerText = `Iteration: ${data.iteration}`; document.getElementById("iteration").innerText = `Iteration: ${data.iteration} messages ${messages}.`;
const ul = document.getElementById("objects");
while(ul.firstChild) { ul.removeChild(ul.lastChild) }
for (n in data.objects) { for (n in data.objects) {
const ob = data.objects[n]; const ob = data.objects[n];
const li = document.createElement("li"); const li = document.getElementById(`object-${ob.id}`);
li.innerText = `${ob.name} @ ${ob.x}, ${ob.y}`; li.innerText = `${ob.name} @ ${ob.x}, ${ob.y}`;
ul.append(li);
} }
ws.send("MOAR"); ws.send("MOAR");
}; };