feat: add box node

This commit is contained in:
2024-04-17 15:34:28 +02:00
parent ddd0e6a0cf
commit 7579c6c00b
24 changed files with 364 additions and 99 deletions

View File

@@ -1,6 +1,7 @@
use macros::include_definition_file;
use utils::{evaluate_args, get_args};
use wasm_bindgen::prelude::*;
use web_sys::console;
include_definition_file!("src/input.json");
@@ -12,16 +13,19 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
let value_encoded = evaluate_args(args[0]);
// let value = decode_float(value_encoded[0], value_encoded[1]);
let length = args[1];
let length = (args[1][0]) as usize;
console::log_1(&format!("WASM(array): input: {:?} -> {:?}", args, value_encoded).into());
// construct array of length
let mut res: Vec<i32> = Vec::with_capacity(length[0] as usize * 2 + 2);
let mut res: Vec<i32> = Vec::with_capacity(length + 4);
res.push(0);
res.push((length[0]) * 2 + 2);
for _ in 0..length[0] as usize {
res.push(length as i32 + 4);
for _ in 0..length {
res.push(value_encoded[0]);
res.push(value_encoded[1])
}
res.push(1);
res.push(1);
res
}

6
nodes/max/plantarium/box/.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,28 @@
[package]
name = "box"
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.
utils = { version = "0.1.0", path = "../../../../packages/utils" }
macros = { version = "0.1.0", path = "../../../../packages/macros" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.69", features = ["console"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@@ -0,0 +1,6 @@
{
"scripts": {
"build": "wasm-pack build --release --out-name index --no-default-features",
"dev": "cargo watch -s 'pnpm build'"
}
}

View File

@@ -0,0 +1,11 @@
{
"outputs": [
"model"
],
"inputs": {
"size": {
"type": "float",
"value": 2
}
}
}

View File

@@ -0,0 +1,83 @@
use macros::include_definition_file;
use utils::{decode_float, encode_float, evaluate_args, get_args, set_panic_hook, wrap_arg};
use wasm_bindgen::prelude::*;
use web_sys::console;
include_definition_file!("src/input.json");
#[rustfmt::skip]
#[wasm_bindgen]
pub fn execute(input: &[i32]) -> Vec<i32> {
set_panic_hook();
let args = get_args(input);
console::log_1(&format!("WASM(cube): input: {:?} -> {:?}", input, args ).into());
let arg1 = evaluate_args(args[0]);
let decoded = decode_float(arg1[0]);
let p = encode_float(decoded);
let n = encode_float(-decoded);
// [[1,3, x, y, z, x, y,z,x,y,z]];
wrap_arg(&[
1, // 1: geometry
8, // 8 vertices
12, // 12 faces
/*
Indeces:
5----6
| 4--+-7
0-|--1 |
3----2
*/
// this are the indeces for the face
0, 1, 2,
0, 2, 3,
0, 3, 4,
4, 5, 0,
6, 1, 0,
5, 6, 0,
7, 2, 1,
6, 7, 1,
2, 7, 3,
3, 7, 4,
7, 6, 4,
4, 6, 5,
// this is the normal for every single face 1065353216 == 1.0f encoded is i32
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
0, 1065353216, 0,
// Bottom plate
p, n, n,
p, n, p,
n, n, p,
n, n, n,
// Top Plate
n, p, n,
p, p, n,
p, p, p,
n, p, p,
])
}

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

@@ -5,12 +5,16 @@ include_definition_file!("src/input.json");
#[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> {
let mut result = Vec::with_capacity(args.len() + 3);
let mut result = Vec::with_capacity(args.len() + 7);
result.push(0);
result.push(1);
result.push(0); // encoding the [ bracket
result.push(args[1] + 1);
result.push(0); // adding the node-type, math: 0
result.extend_from_slice(&args[2..]);
result.push(1);
result.push(1); // closing bracket
result.push(1);
result.push(1); // closing bracket
result
}

View File

@@ -2,7 +2,10 @@
"outputs": [],
"inputs": {
"input": {
"type": "float",
"type": [
"plant",
"model"
],
"external": true
}
}

View File

@@ -1,5 +1,5 @@
use macros::include_definition_file;
use utils::{decode_float, encode_float};
use utils::{concat_args, decode_float, encode_float, get_args};
use wasm_bindgen::prelude::*;
use web_sys::console;
@@ -10,42 +10,27 @@ include_definition_file!("src/inputs.json");
pub fn execute(input: &[i32]) -> Vec<i32> {
utils::set_panic_hook();
let size = input[2];
let decoded = decode_float(input[2]);
let negative_size = encode_float(-decoded);
let args = get_args(input);
console::log_1(&format!("WASM(output_node): input: {:?} -> {}", input, decoded).into());
console::log_1(&format!("WASM(output_node): input: {:?}", args).into());
// [[1,3, x, y, z, x, y,z,x,y,z]];
vec![
0, 1, // opening bracket
0, 19, // opening bracket + distance to next bracket
1, // 1: geometry
3, // 3 vertices
1, // 1 face
// thise are the indeces for the face
0, 2, 1,
// this is the normal for the single face 1065353216 == 1.0f encoded is i32
0, 1065353216, 0,
//
negative_size, // x -> point 1
0, // y
0, // z
//
size, // x -> point 2
0, // y
0, // z
//
0, // x -> point 3
0, // y
size, // z
//
1, 1, 1, 1, // closing brackets
]
let mut output:Vec<&[i32]> = Vec::new();
for arg in args {
// let decoded = decode_float(result[0], result[1]);
if arg.len() < 3 {
continue;
}
output.push( match arg[2] {
// stem
0 => &[0,1,1,1],
// geometry
1 => arg,
_ => &[0,1,1,1],
})
}
concat_args(output)
// console::log_1(&format!("WASM: output: {:?}", decoded).into());
// result
}

View File

@@ -22,7 +22,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
);
vec![
0, // opening bracket
0, 1, 0, // opening bracket
11, // opening bracket
0, // type: plant
0, // alpha: 0
@@ -36,5 +36,7 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
1, // thickness
1, // closing bracket
1, //closing bracket
1, // closing bracket
1, //closing bracket
]
}

View File

@@ -15,15 +15,13 @@ pub fn execute(input: &[i32]) -> Vec<i32> {
let length = (input.len() - 2) / 2;
(0..length).for_each(|i| {
let mantissa = input[2 + i * 2];
let exponent = input[2 + i * 2 + 1];
// console::log_1(&format!("WASM(sum_node): i: {} sum: {:?}", i, sum).into());
sum += decode_float(mantissa, exponent);
sum += decode_float(input[2 + i * 2]);
});
let encoded_sum = encode_float(sum);
// console::log_1(&format!("WASM(sum_node): result: {:?}", sum).into());
vec![0, 3, encoded_sum.0, encoded_sum.1]
vec![0, 2, encoded_sum]
}

View File

@@ -18,9 +18,11 @@ wasm-bindgen = "0.2.84"
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
utils = { version = "0.1.0", path = "../../../../packages/utils" }
macros = { version = "0.1.0", path = "../../../../packages/macros" }
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.69", features = ["console"] }
[dev-dependencies]
wasm-bindgen-test = "0.3.34"

View File

@@ -0,0 +1,11 @@
{
"outputs": [
"model"
],
"inputs": {
"size": {
"type": "float",
"value": 2
}
}
}

View File

@@ -1,24 +1,44 @@
use macros::include_definition_file;
use utils::{concat_args, decode_float, encode_float};
use wasm_bindgen::prelude::*;
use web_sys::console;
include_definition_file!("src/input.json");
#[rustfmt::skip]
#[wasm_bindgen]
pub fn get_outputs() -> Vec<String> {
vec!["float".to_string()]
}
pub fn execute(input: &[i32]) -> Vec<i32> {
#[wasm_bindgen]
pub fn get_input_types() -> String {
r#"{
"min": { "type": "float", "value": 2 },
"max": { "type": "float", "value": 2 },
"seed": { "type": "seed" }
}"#
.to_string()
}
let size = input[2];
let decoded = decode_float(input[2]);
let negative_size = encode_float(-decoded);
#[wasm_bindgen]
pub fn execute(args: &[i32]) -> Vec<i32> {
// construct vertices of a triangle
// let min = evaluate_parameters(var_min);
console::log_1(&format!("WASM(triangle): input: {:?} -> {}", input, decoded).into());
// [[1,3, x, y, z, x, y,z,x,y,z]];
concat_args(vec![&[
0, 19, // opening bracket + distance to next bracket
1, // 1: geometry
3, // 3 vertices
1, // 1 face
// this are the indeces for the face
0, 2, 1,
// this is the normal for the single face 1065353216 == 1.0f encoded is i32
0, 1065353216, 0,
//
negative_size, // x -> point 1
0, // y
0, // z
//
size, // x -> point 2
0, // y
0, // z
//
0, // x -> point 3
0, // y
size, // z
//
1, 1 // closing brackets
]])
vec![1, 2, 3, 4, args[0]]
}