feat: add snapToGrid and showGrid settings

This commit is contained in:
2024-04-22 16:52:52 +02:00
parent ad197db873
commit 1de0094c85
25 changed files with 290 additions and 99 deletions

View File

@ -1,6 +1,6 @@
type SparseArray<T = number> = (T | T[] | SparseArray<T>)[];
export function concat_encoded(input: (number | number[])[]): number[] {
export function concatEncodedArrays(input: (number | number[])[]): number[] {
if (input.length === 1 && Array.isArray(input[0])) {
return input[0]
@ -26,11 +26,13 @@ export function concat_encoded(input: (number | number[])[]): number[] {
}
}
result.push(1, 1); // closing bracket
return result
}
// Encodes a nested array into a flat array with bracket and distance notation
export function encode(array: SparseArray): number[] {
export function encodeNestedArray(array: SparseArray): number[] {
const encoded = [0, 0]; // Initialize encoded array with root bracket notation
let missingBracketIndex = 1; // Track where to insert the distance to the next bracket
@ -44,7 +46,7 @@ export function encode(array: SparseArray): number[] {
encoded.push(0, 1, 1, 1);
} else {
// Recursively encode non-empty arrays
const child = encode(item);
const child = encodeNestedArray(item);
encoded.push(...child, 1, 0);
}
// Update missingBracketIndex to the position of the newly added bracket
@ -59,7 +61,12 @@ export function encode(array: SparseArray): number[] {
return encoded;
};
function decode_recursive(dense: number[], index = 0) {
function decode_recursive(dense: number[] | Int32Array, index = 0) {
if (dense instanceof Int32Array) {
dense = Array.from(dense)
}
const decoded: (number | number[])[] = [];
let nextBracketIndex = dense[index + 1] + index + 1; // Calculate the index of the next bracket
@ -83,6 +90,6 @@ function decode_recursive(dense: number[], index = 0) {
return [decoded, index, nextBracketIndex] as const;
}
export function decode(dense: number[]) {
export function decodeNestedArray(dense: number[] | Int32Array) {
return decode_recursive(dense, 0)[0];
}

View File

@ -67,7 +67,7 @@ pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec<i32> {
let i_index_offset = index_offset + j * 6;
let i_position_offset = position_offset + j;
log!("i: {}, j: {}, i_index_offset: {}, i_position_offset: {} res_x: {}", i, j, i_index_offset, i_position_offset,res_x);
//log!("i: {}, j: {}, i_index_offset: {}, i_position_offset: {} res_x: {}", i, j, i_index_offset, i_position_offset,res_x);
if j == res_x - 1 {
indices[i_index_offset ] = (i_position_offset + 1) as i32;

View File

@ -39,9 +39,13 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
// skip over the bracket encoding
idx += 2;
} else {
if depth == 1 {
arg_start_index = idx + 2;
}
// skip to the next bracket if we are at depth > 0
idx = next_bracket_index;
}
continue;
}
@ -56,6 +60,12 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
idx += 1;
}
println!("idx: {}, length: {}, asi: {}", idx, length, arg_start_index);
if arg_start_index < length {
out_args.push(&args[arg_start_index..]);
}
out_args
}
@ -101,6 +111,8 @@ pub fn wrap_arg(arg: &[i32]) -> Vec<i32> {
pub fn evaluate_node(input_args: &[i32]) -> i32 {
let node_type = input_args[0];
println!("node_type: {} -> {:?}", node_type, input_args);
match node_type {
0 => crate::nodes::math_node(&input_args[1..]),
1 => crate::nodes::random_node(&input_args[1..]),
@ -109,12 +121,21 @@ pub fn evaluate_node(input_args: &[i32]) -> i32 {
}
pub fn evaluate_vec3(input_args: &[i32]) -> Vec<f32> {
if input_args.len() == 3 {
return vec![
decode_float(input_args[0]),
decode_float(input_args[1]),
decode_float(input_args[2]),
];
}
let args = get_args(input_args);
assert!(
args.len() == 3,
"Failed to evaluate Vec3 - Expected 3 arguments, got {}",
args.len()
"Failed to evaluate Vec3 - Expected 3 arguments, got {} \n {:?}",
args.len(),
args
);
let x = evaluate_float(args[0]);
@ -125,16 +146,18 @@ pub fn evaluate_vec3(input_args: &[i32]) -> Vec<f32> {
}
pub fn evaluate_float(arg: &[i32]) -> f32 {
decode_float(evaluate_arg(arg))
decode_float(evaluate_int(arg))
}
pub fn evaluate_arg(input_args: &[i32]) -> i32 {
pub fn evaluate_int(input_args: &[i32]) -> i32 {
if input_args.len() == 1 {
return input_args.to_vec()[0];
return input_args[0];
}
let args = get_args(input_args);
println!("args: {:?}", args);
let mut resolved: Vec<i32> = Vec::new();
for arg in args {
@ -144,7 +167,7 @@ pub fn evaluate_arg(input_args: &[i32]) -> i32 {
resolved.push(arg[2]);
resolved.push(arg[3]);
} else {
resolved.push(evaluate_arg(arg));
resolved.push(evaluate_int(arg));
}
}
@ -160,6 +183,32 @@ mod tests {
use super::*;
#[rustfmt::skip]
#[test]
fn test_split_args() {
let input = [
0, 1, 0, 28, 0, 2, 1048576000, 0, 20, 0, 4, 0, 0, 1073741824, 0, 9, 0, 5, 0, 0,
1073741824, 1073741824, 1, 1, 1, 0, 1, 1, 1, 4, 1041865114, 1, 5, 1086324736,
1053609165, 54,
];
// this should be the output
/* [
[ 0, 2, 1048576000, 0, 20, 0, 4, 0, 0, 1073741824, 0, 9, 0, 5, 0, 0, 1073741824, 1073741824, 1, 1, 1, 0, 1, 1, 1, 4, 1041865114 ],
1086324736,
1053609165,
54
] */
let args = get_args(&input);
println!("{:?}", args[0]);
assert_eq!(args[0].len(), 27);
assert_eq!(args[1][0], 1086324736);
assert_eq!(args[2][0], 1053609165);
}
#[test]
fn test_recursive_evaluation() {
let input = vec![