feat: add array and sum node

This commit is contained in:
max_richter 2024-04-10 23:50:41 +02:00
parent e2940183f1
commit 644bcd6997
26 changed files with 377 additions and 11 deletions

36
Cargo.lock generated
View File

@ -80,6 +80,18 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110"
[[package]]
name = "array"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.79" version = "0.1.79"
@ -3305,6 +3317,18 @@ version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "sum"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]] [[package]]
name = "swift-rs" name = "swift-rs"
version = "1.0.6" version = "1.0.6"
@ -3969,6 +3993,18 @@ dependencies = [
"serde_json", "serde_json",
] ]
[[package]]
name = "triangle"
version = "0.1.0"
dependencies = [
"console_error_panic_hook",
"plantarium",
"serde",
"serde-wasm-bindgen",
"wasm-bindgen",
"wasm-bindgen-test",
]
[[package]] [[package]]
name = "try-lock" name = "try-lock"
version = "0.2.5" version = "0.2.5"

View File

@ -1,4 +1,3 @@
type SparseArray<T = number> = (T | T[] | SparseArray<T>)[]; type SparseArray<T = number> = (T | T[] | SparseArray<T>)[];
// Encodes a nested array into a flat array with bracket and distance notation // Encodes a nested array into a flat array with bracket and distance notation

View File

@ -51,6 +51,9 @@ export class RemoteNodeRegistry implements NodeRegistry {
const a = performance.now(); const a = performance.now();
nodeIds.push("max/plantarium/random"); nodeIds.push("max/plantarium/random");
nodeIds.push("max/plantarium/float"); nodeIds.push("max/plantarium/float");
nodeIds.push("max/plantarium/array");
nodeIds.push("max/plantarium/sum");
for (const id of nodeIds) { for (const id of nodeIds) {
const nodeUrl = `${this.url}/n/${id}`; const nodeUrl = `${this.url}/n/${id}`;
const response = await fetch(nodeUrl); const response = await fetch(nodeUrl);
@ -96,6 +99,8 @@ wasm = val;`);
export class MemoryNodeRegistry implements NodeRegistry { export class MemoryNodeRegistry implements NodeRegistry {
status: "loading" | "ready" | "error" = "ready";
async load(nodeIds: string[]) { async load(nodeIds: string[]) {
// Do nothing // Do nothing
} }

View File

@ -135,7 +135,12 @@ export class MemoryRuntimeExecutor implements RuntimeExecutor {
} }
// execute the node and store the result // execute the node and store the result
results[node.id] = node.tmp.type.execute(...Object.values(inputs)) as number; try {
console.log(`Executing node ${node.tmp.type.id || node.id}`, inputs);
results[node.id] = node.tmp.type.execute(...Object.values(inputs)) as number;
} catch (e) {
console.error(`Error executing node ${node.tmp.type.id || node.id}`, e);
}
} }
} }

View File

@ -8,7 +8,8 @@
console.log("INPUT"); console.log("INPUT");
console.log(input); console.log(input);
const encoded = encode(input); let encoded = encode(input);
encoded = [0, 3, 5, ...encoded.slice(2).slice(0, -4), 5, 5, 1, 1];
console.log("ENCODED"); console.log("ENCODED");
console.log(encoded); console.log(encoded);

6
nodes/max/plantarium/array/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@ -0,0 +1,30 @@
[package]
name = "array"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@ -0,0 +1,5 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features"
}
}

View File

@ -0,0 +1,44 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"length": { "type": "float", "value": 2 }
}"#
.to_string()
}
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(var_length: i32) -> Vec<f64> {
utils::set_panic_hook();
let length = var_length;//evaluate_parameters(var_length);
// construct array of length
let mut res = Vec::new();
for _ in 0..length as usize {
res.push(2.0);
}
log("executing array");
return res;
}

View File

@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

View File

@ -1,5 +1,5 @@
mod utils; mod utils;
use plantarium::evaluate_parameters; use plantarium::{evaluate_parameters, unwrap_float};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
// lifted from the `console_log` example // lifted from the `console_log` example
@ -23,10 +23,8 @@ pub fn get_input_types() -> String {
.to_string() .to_string()
} }
#[wasm_bindgen] #[wasm_bindgen]
pub fn execute(var_value: JsValue) -> f64 { pub fn execute(var_value: f64) -> f64 {
utils::set_panic_hook(); utils::set_panic_hook();
let res = evaluate_parameters(var_value); return var_value;
return res;
} }

6
nodes/max/plantarium/sum/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@ -0,0 +1,30 @@
[package]
name = "sum"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@ -0,0 +1,5 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features"
}
}

View File

@ -0,0 +1,33 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"array": { "type": "float", "value": 2, "external": true }
}"#
.to_string()
}
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(array: &[f64]) -> f64 {
utils::set_panic_hook();
let mut sum = 0.0;
array.iter().for_each(|x| sum += x);
return sum;
}

View File

@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

View File

@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log

View File

@ -0,0 +1,30 @@
[package]
name = "triangle"
version = "0.1.0"
authors = ["Max Richter <jim-x@web.de>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[features]
default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.84"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
plantarium = { version = "0.1.0", path = "../../../../packages/plantarium" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"

View File

@ -0,0 +1,5 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features"
}
}

View File

@ -0,0 +1,48 @@
mod utils;
use plantarium::{evaluate_parameters, unwrap_int, unwrap_string};
use wasm_bindgen::prelude::*;
// lifted from the `console_log` example
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_namespace = console)]
fn log(s: &str);
}
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
#[wasm_bindgen]
pub fn get_input_types() -> String {
utils::set_panic_hook();
r#"{
"min": { "type": "float", "value": 2 },
"max": { "type": "float", "value": 2 },
"seed": { "type": "seed" }
}"#
.to_string()
}
struct El {
value: Option<f64>,
array: Option<Vec<f64>>,
nested: Option<Box<El>>,
}
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(var_min: JsValue, var_max: JsValue, var_seed: JsValue) -> Vec<f64> {
utils::set_panic_hook();
// construct vertices of a triangle
let min= evaluate_parameters(var_min);
return vec![
0.0, 0.0, 0.0,
min, 0.0, 0.0,
min, min, 0.0
];
}

View File

@ -0,0 +1,10 @@
pub fn set_panic_hook() {
// When the `console_error_panic_hook` feature is enabled, we can call the
// `set_panic_hook` function at least once during initialization, and then
// we will get better error messages if our code ever panics.
//
// For more details see
// https://github.com/rustwasm/console_error_panic_hook#readme
#[cfg(feature = "console_error_panic_hook")]
console_error_panic_hook::set_once();
}

View File

@ -0,0 +1,13 @@
//! Test suite for the Web and headless browsers.
#![cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
#[wasm_bindgen_test]
fn pass() {
assert_eq!(1 + 1, 2);
}

View File

@ -27,7 +27,7 @@
import { Color } from "three/src/math/Color.js"; import { Color } from "three/src/math/Color.js";
import { CubicBezierCurve } from "three/src/extras/curves/CubicBezierCurve.js"; import { CubicBezierCurve } from "three/src/extras/curves/CubicBezierCurve.js";
import { Vector2 } from "three/src/math/Vector2.js"; import { Vector2 } from "three/src/math/Vector2.js";
import { createEdgeGeometry } from "./createEdgeGeometry"; import { createEdgeGeometry } from "./createEdgeGeometry.js";
export let from: { x: number; y: number }; export let from: { x: number; y: number };
export let to: { x: number; y: number }; export let to: { x: number; y: number };

View File

@ -36,8 +36,13 @@ export async function getNode(id: `${string}/${string}/${string}`) {
const wrapper = await getNodeWasm(id); const wrapper = await getNodeWasm(id);
const outputs = wrapper.get_outputs(); const outputs = wrapper.get_outputs();
const inputTypes = JSON.parse(wrapper.get_input_types()); const rawInputs = wrapper.get_input_types();
try {
const inputTypes = JSON.parse(rawInputs);
return { id, outputs, inputs: inputTypes }
} catch (e) {
console.log("Failed to parse input types for node", { id, rawInputs });
}
return { id, outputs, inputs: inputTypes }
} }