feat: yaaay first stem

This commit is contained in:
2024-04-18 00:02:50 +02:00
parent 1da13523ea
commit 2edd22136f
21 changed files with 297 additions and 102 deletions

View File

@ -8,6 +8,7 @@ default = ["console_error_panic_hook"]
[dependencies]
wasm-bindgen = "0.2.92"
web-sys = { version = "0.3.69", features = ["console"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }
console_error_panic_hook = { version = "0.1.7", optional = true }

View File

@ -0,0 +1,77 @@
import { test, expect } from "vitest"
import { encodeFloat, decodeFloat } from "./encode"
test("encode_float", () => {
const input = 1.23;
const encoded = encodeFloat(input)
const output = decodeFloat(encoded)
console.log(input, output)
expect(output).toBeCloseTo(input);
});
test("encode 2.0", () => {
const input = 2.0;
const encoded = encodeFloat(input)
expect(encoded).toEqual(1073741824)
});
test("floating point imprecision", () => {
let maxError = 0;
new Array(10_000).fill(null).forEach((_, i) => {
const input = i < 5_000 ? i : Math.random() * 100;
const encoded = encodeFloat(input);
const output = decodeFloat(encoded);
const error = Math.abs(input - output);
if (error > maxError) {
maxError = error;
}
});
expect(maxError).toBeLessThan(0.00001);
});
// Test with negative numbers
test("negative numbers", () => {
const inputs = [-1, -0.5, -123.456, -0.0001];
inputs.forEach(input => {
const encoded = encodeFloat(input);
const output = decodeFloat(encoded);
expect(output).toBeCloseTo(input);
});
});
// Test with very small numbers
test("very small numbers", () => {
const input = 1.2345e-38;
const encoded = encodeFloat(input)
const output = decodeFloat(encoded)
expect(output).toBeCloseTo(input);
});
// Test with zero
test("zero", () => {
const input = 0;
const encoded = encodeFloat(input)
const output = decodeFloat(encoded)
expect(output).toBe(0);
});
// Test with infinity
test("infinity", () => {
const input = Infinity;
const encoded = encodeFloat(input)
const output = decodeFloat(encoded)
expect(output).toBe(Infinity);
});
// Test with large numbers
test("large numbers", () => {
const inputs = [1e+5, 1e+10];
inputs.forEach(input => {
const encoded = encodeFloat(input);
const output = decodeFloat(encoded);
// Note: Large numbers may lose precision, hence using toBeCloseTo with a tolerance
expect(output).toBeCloseTo(input, 0);
});
});

View File

@ -0,0 +1,19 @@
// Create a buffer to hold the float as bytes
const buffer = new ArrayBuffer(4);
const view = new DataView(buffer);
export function encodeFloat(value: number): number {
// Write the number as a float to the buffer
view.setFloat32(0, value, true); // 'true' for little-endian
// Read the buffer as an integer
return view.getInt32(0, true);
}
export function decodeFloat(value: number): number {
// Write the integer back as an int32
view.setInt32(0, value, true);
// Read the buffer as a float
return view.getFloat32(0, true);
}

View File

@ -0,0 +1,54 @@
import { test, expect } from 'vitest';
import { fastHashArray, fastHashString } from './fastHash';
test('fastHashString doesnt produce clashes', () => {
const hashA = fastHashString('abcdef');
const hashB = fastHashString('abcdeg');
const hashC = fastHashString('abcdeg');
expect(hashA).not.toEqual(hashB);
expect(hashB).toEqual(hashC);
});
test("fastHashArray doesnt product collisions", () => {
const a = new Int32Array(1000);
const hash_a = fastHashArray(a);
a[0] = 1;
const hash_b = fastHashArray(a);
expect(hash_a).not.toEqual(hash_b);
});
test('fastHashArray is fast(ish) < 20ms', () => {
const a = new Int32Array(10_000);
const t0 = performance.now();
fastHashArray(a);
const t1 = performance.now();
a[0] = 1;
fastHashArray(a);
const t2 = performance.now();
expect(t1 - t0).toBeLessThan(20);
expect(t2 - t1).toBeLessThan(20);
});
// test if the fastHashArray function is deterministic
test('fastHashArray is deterministic', () => {
const a = new Int32Array(1000);
a[42] = 69;
const b = new Int32Array(1000);
b[42] = 69;
const hashA = fastHashArray(a);
const hashB = fastHashArray(b);
expect(hashA).toEqual(hashB);
});

View File

@ -0,0 +1,122 @@
// https://github.com/6502/sha256/blob/main/sha256.js
function sha256(data?: string | Uint8Array) {
let h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a,
h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19,
tsz = 0, bp = 0;
const k = [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2],
rrot = (x, n) => (x >>> n) | (x << (32 - n)),
w = new Uint32Array(64),
buf = new Uint8Array(64),
process = () => {
for (let j = 0, r = 0; j < 16; j++, r += 4) {
w[j] = (buf[r] << 24) | (buf[r + 1] << 16) | (buf[r + 2] << 8) | buf[r + 3];
}
for (let j = 16; j < 64; j++) {
let s0 = rrot(w[j - 15], 7) ^ rrot(w[j - 15], 18) ^ (w[j - 15] >>> 3);
let s1 = rrot(w[j - 2], 17) ^ rrot(w[j - 2], 19) ^ (w[j - 2] >>> 10);
w[j] = (w[j - 16] + s0 + w[j - 7] + s1) | 0;
}
let a = h0, b = h1, c = h2, d = h3, e = h4, f = h5, g = h6, h = h7;
for (let j = 0; j < 64; j++) {
let S1 = rrot(e, 6) ^ rrot(e, 11) ^ rrot(e, 25),
ch = (e & f) ^ ((~e) & g),
t1 = (h + S1 + ch + k[j] + w[j]) | 0,
S0 = rrot(a, 2) ^ rrot(a, 13) ^ rrot(a, 22),
maj = (a & b) ^ (a & c) ^ (b & c),
t2 = (S0 + maj) | 0;
h = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
}
h0 = (h0 + a) | 0; h1 = (h1 + b) | 0; h2 = (h2 + c) | 0; h3 = (h3 + d) | 0;
h4 = (h4 + e) | 0; h5 = (h5 + f) | 0; h6 = (h6 + g) | 0; h7 = (h7 + h) | 0;
bp = 0;
},
add = data => {
if (typeof data === "string") {
data = typeof TextEncoder === "undefined" ? Buffer.from(data) : (new TextEncoder).encode(data);
}
for (let i = 0; i < data.length; i++) {
buf[bp++] = data[i];
if (bp === 64) process();
}
tsz += data.length;
},
digest = () => {
buf[bp++] = 0x80; if (bp == 64) process();
if (bp + 8 > 64) {
while (bp < 64) buf[bp++] = 0x00;
process();
}
while (bp < 58) buf[bp++] = 0x00;
// Max number of bytes is 35,184,372,088,831
let L = tsz * 8;
buf[bp++] = (L / 1099511627776.) & 255;
buf[bp++] = (L / 4294967296.) & 255;
buf[bp++] = L >>> 24;
buf[bp++] = (L >>> 16) & 255;
buf[bp++] = (L >>> 8) & 255;
buf[bp++] = L & 255;
process();
let reply = new Uint8Array(32);
reply[0] = h0 >>> 24; reply[1] = (h0 >>> 16) & 255; reply[2] = (h0 >>> 8) & 255; reply[3] = h0 & 255;
reply[4] = h1 >>> 24; reply[5] = (h1 >>> 16) & 255; reply[6] = (h1 >>> 8) & 255; reply[7] = h1 & 255;
reply[8] = h2 >>> 24; reply[9] = (h2 >>> 16) & 255; reply[10] = (h2 >>> 8) & 255; reply[11] = h2 & 255;
reply[12] = h3 >>> 24; reply[13] = (h3 >>> 16) & 255; reply[14] = (h3 >>> 8) & 255; reply[15] = h3 & 255;
reply[16] = h4 >>> 24; reply[17] = (h4 >>> 16) & 255; reply[18] = (h4 >>> 8) & 255; reply[19] = h4 & 255;
reply[20] = h5 >>> 24; reply[21] = (h5 >>> 16) & 255; reply[22] = (h5 >>> 8) & 255; reply[23] = h5 & 255;
reply[24] = h6 >>> 24; reply[25] = (h6 >>> 16) & 255; reply[26] = (h6 >>> 8) & 255; reply[27] = h6 & 255;
reply[28] = h7 >>> 24; reply[29] = (h7 >>> 16) & 255; reply[30] = (h7 >>> 8) & 255; reply[31] = h7 & 255;
let res = "";
reply.forEach(x => res += ("0" + x.toString(16)).slice(-2));
return res;
};
if (data) add(data);
return { add, digest };
}
export function fastHashArray(arr: Int32Array): string {
return sha256(new Uint8Array(arr.buffer)).digest();
}
// Shamelessly copied from
// https://stackoverflow.com/a/8831937
export function fastHashString(input: string) {
if (input.length === 0) return 0;
let hash = 0;
for (let i = 0; i < input.length; i++) {
hash = (hash << 5) - hash + input.charCodeAt(i);
hash = hash & hash;
}
return hash;
}
export function fastHash(input: (string | Int32Array | number)[]) {
const s = sha256();
for (let i = 0; i < input.length; i++) {
const v = input[i]
if (typeof v === "string") {
s.add(v);
} else if (v instanceof Int32Array) {
s.add(new Uint8Array(v.buffer));
} else {
s.add(v.toString());
}
}
return s.digest()
}

View File

@ -0,0 +1,76 @@
import { expect, test } from 'vitest'
import { decode, encode, concat_encoded } from './flat_tree'
test("it correctly concats nested arrays", () => {
const input_a = encode([1, 2, 3]);
const input_b = 2;
const input_c = encode([4, 5, 6]);
const output = concat_encoded([input_a, input_b, input_c]);
const decoded = decode(output);
expect(decoded[0]).toEqual([1, 2, 3]);
});
// Original test case
test('it correctly decodes/encodes complex nested arrays', () => {
const input = [5, [6, 1], 1, 5, [5], [7, 2, [5, 1]]];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with empty array
test('it correctly handles an empty array', () => {
const input: number[] = [];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with nested empty arrays
test('it correctly handles nested empty arrays', () => {
const input = [5, [], [6, []], []];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with single-element array
test('it correctly handles a single-element array', () => {
const input = [42];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with deeply nested array
test('it correctly handles deeply nested arrays', () => {
const input = [[[[[1]]]]];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with large numbers
test('it correctly handles large numbers', () => {
const input = [2147483647, [-2147483648, 1234567890]];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with sequential nesting
test('it correctly handles sequential nesting', () => {
const input = [1, [2, [3, [4, [5]]]]];
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});
// Test with mixed data types (if supported)
// Note: This test assumes your implementation supports mixed types.
// If not, you can ignore or remove this test.
test('it correctly handles arrays with mixed data types', () => {
const input = [1, 'text', [true, [null, ['another text']]]];
//@ts-ignore
const decoded = decode(encode(input));
expect(decoded).toEqual(input);
});

View File

@ -0,0 +1,88 @@
type SparseArray<T = number> = (T | T[] | SparseArray<T>)[];
export function concat_encoded(input: (number | number[])[]): number[] {
if (input.length === 1 && Array.isArray(input[0])) {
return input[0]
}
const result = [0, 1]; // opening bracket
let last_closing_bracket = 1;
for (let i = 0; i < input.length; i++) {
const item = input[i];
if (Array.isArray(item)) {
result.push(...item);
if (item.length > 2) {
if (item[item.length - 2] !== 1 && item[item.length - 1] !== 1) {
result.push(1, 1); // add closing bracket if missing
}
}
last_closing_bracket = result.length - 1;
} else {
result[last_closing_bracket]++;
result.push(item);
}
}
return result
}
// Encodes a nested array into a flat array with bracket and distance notation
export function encode(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
for (let index = 0; index < array.length; index++) {
const item = array[index];
if (Array.isArray(item)) {
// Update the distance to the next bracket for the last opened bracket
encoded[missingBracketIndex] = encoded.length - missingBracketIndex;
if (item.length === 0) {
// Handle empty arrays by directly adding bracket notation
encoded.push(0, 1, 1, 1);
} else {
// Recursively encode non-empty arrays
const child = encode(item);
encoded.push(...child, 1, 0);
}
// Update missingBracketIndex to the position of the newly added bracket
missingBracketIndex = encoded.length - 1;
} else {
// Handle non-array items
encoded.push(item);
// Update the distance for the last opened bracket
if (missingBracketIndex) encoded[missingBracketIndex] = index + 2;
}
}
return encoded;
};
function decode_recursive(dense: number[], index = 0) {
const decoded: (number | number[])[] = [];
let nextBracketIndex = dense[index + 1] + index + 1; // Calculate the index of the next bracket
index += 2; // Skip the initial bracket notation
while (index < dense.length) {
if (index === nextBracketIndex) {
if (dense[index] === 0) { // Opening bracket detected
const [p, nextIndex, _nextBracketIndex] = decode_recursive(dense, index);
decoded.push(p);
index = nextIndex + 1;
nextBracketIndex = _nextBracketIndex;
} else { // Closing bracket detected
nextBracketIndex = dense[index + 1] + index + 1;
return [decoded, index, nextBracketIndex] as const;
}
} else if (index < nextBracketIndex) {
decoded.push(dense[index]); // Add regular number to decoded array
}
index++;
}
return [decoded, index, nextBracketIndex] as const;
}
export function decode(dense: number[]) {
return decode_recursive(dense, 0)[0];
}

View File

@ -1 +1,115 @@
pub fn extrude_path(path: &[i32]) {}
use crate::log;
use super::create_empty_geometry;
use glam::{Mat4, Quat, Vec3};
fn create_circle(res: usize) -> Vec<f32> {
let angle = (2.0 * std::f32::consts::PI) / res as f32;
let mut circle = Vec::new();
for i in 0..res {
circle.push((angle * i as f32).cos());
circle.push((angle * i as f32).sin());
}
circle
}
#[rustfmt::skip]
pub fn extrude_path(input_path: &[i32], res_x: usize) -> Vec<i32> {
let point_amount = input_path.len() / 4;
let face_amount = (point_amount - 1) * res_x * 2;
let vertices_amount = point_amount * res_x;
let circle = create_circle(res_x);
let mut geometry = create_empty_geometry(vertices_amount, face_amount);
let (_header,rest) = geometry.split_at_mut(5);
let (indices, rest) = rest.split_at_mut(face_amount*3);
let (_positions, _normals) = rest.split_at_mut(vertices_amount*3);
let positions: &mut [f32];
let normals: &mut [f32];
let path: &[f32];
unsafe {
path = std::slice::from_raw_parts(input_path.as_ptr() as *const f32, input_path.len());
positions = std::slice::from_raw_parts_mut(_positions.as_mut_ptr() as *mut f32, _positions.len());
normals = std::slice::from_raw_parts_mut(_normals.as_mut_ptr() as *mut f32, _normals.len());
}
normals[0] = 0.0;
for i in 0..point_amount {
let index_offset = i * res_x * 6;
let position_offset = i * res_x;
let point = Vec3::from_slice(&path[i*4..i*4+3]);
let thickness = path[i*4+3];
let next_point = if i == point_amount - 1 { point } else { Vec3::from_slice(&path[(i+1)*4..(i+1)*4+3]) };
let prev_point = if i == 0 { point } else { Vec3::from_slice(&path[(i-1)*4..(i-1)*4+3]) };
let mut v = if i == 0 {
point - next_point
} else if i == point_amount - 1 {
prev_point - point
} else {
prev_point - next_point
};
v = v.normalize();
let n = Vec3::new(0.0, 1.0, 0.0); // Assuming 'n' is the up vector or similar
let axis = n.cross(v);
let angle = n.dot(v).acos();
let quat = Quat::from_axis_angle(axis, angle);
let mat = Mat4::IDENTITY * Mat4::from_quat(quat);
for j in 0..res_x {
if i < point_amount - 1 {
let i_index_offset = index_offset + j * 6;
let i_position_offset = position_offset + j;
if j == res_x - 1 {
indices[i_index_offset ] = (i_position_offset + 1) as i32;
indices[i_index_offset + 1] = (i_position_offset - res_x + 1) as i32;
indices[i_index_offset + 2] = (i_position_offset) as i32;
indices[i_index_offset + 3] = (i_position_offset) as i32;
indices[i_index_offset + 4] = (i_position_offset + res_x) as i32;
indices[i_index_offset + 5] = (i_position_offset + 1) as i32;
} else {
indices[i_index_offset ] = (i_position_offset + res_x + 1) as i32;
indices[i_index_offset + 1] = (i_position_offset + 1) as i32;
indices[i_index_offset + 2] = (i_position_offset) as i32;
indices[i_index_offset + 3] = (i_position_offset) as i32;
indices[i_index_offset + 4] = (i_position_offset + res_x) as i32;
indices[i_index_offset + 5] = (i_position_offset + res_x + 1) as i32;
}
}
// construct the points
let idx = i * res_x * 3 + j * 3;
let circle_x = circle[j * 2 ] * thickness;
let circle_y = circle[j * 2 + 1] * thickness;
let _pt = Vec3::new(
point[0] + circle_x,
point[1],
point[2] + circle_y,
);
let pt = Mat4::transform_point3(&mat, _pt) + point;
normals[idx ] = circle_x;
normals[idx + 1] = 0.0;
normals[idx + 2] = circle_y;
positions[idx ] = pt[0];
positions[idx + 1] = pt[1];
positions[idx + 2] = pt[2];
}
}
geometry
}

View File

@ -1,3 +1,34 @@
pub mod calculate_normals;
pub mod extrude_path;
mod calculate_normals;
mod extrude_path;
pub use calculate_normals::*;
pub use extrude_path::*;
use crate::log;
#[rustfmt::skip]
pub fn create_empty_geometry(vertex_amount: usize, face_amount: usize) -> Vec<i32> {
log!(
"create_empty_geometry: vertex_amount: {}, face_amount: {}",
vertex_amount,
face_amount
);
let amount =
3 // definition (type, vertex_amount, face_amount)
+ 4 // opening and closing brackets
+ vertex_amount * 3 // positions
+ vertex_amount * 3 // normals
+ face_amount * 3; // faces
let mut vec: Vec<i32> = vec![0; amount];
vec[0] = 0; // opening bracket
vec[1] = amount as i32 - 2; // opening bracket
vec[2] = 1; // type: geometry
vec[3] = vertex_amount as i32;
vec[4] = face_amount as i32;
vec[amount - 2] = 1; // closing bracket
vec[amount - 1] = 1; // closing bracket
vec
}

View File

@ -1 +1,4 @@
export * from "./wasm-wrapper";
export * from "./flatTree"
export * from "./encoding"
export * from "./fastHash"

View File

@ -7,6 +7,14 @@ pub use helpers::*;
pub use tree::*;
pub mod geometry;
#[macro_export]
macro_rules! log {
($($arg:tt)*) => {{
use web_sys::console;
console::log_1(&format!($($arg)*).into());
}}
}
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

View File

@ -57,20 +57,34 @@ pub fn get_args(args: &[i32]) -> Vec<&[i32]> {
out_args
}
pub fn concat_args(args: Vec<&[i32]>) -> Vec<i32> {
let total_length: usize = args.iter().map(|arg| arg.len()).sum();
pub fn concat_args(mut data: Vec<Vec<i32>>) -> Vec<i32> {
let mut total_length = 4; // Start with 4 to account for [0, 1] at the start and [1, 1] at the end
let mut out_args = Vec::with_capacity(total_length + 4);
out_args.extend_from_slice(&[0, 1]);
for arg in args {
out_args.extend_from_slice(arg);
// Calculate the total length first to avoid reallocations
for vec in &data {
total_length += vec.len(); // +4 for [0, 1] and [1, 1] per inner vec
}
out_args.extend_from_slice(&[1, 1]);
let mut result = Vec::with_capacity(total_length);
out_args
// Add [0, 1] initially
// result.push(0);
// result.push(1);
// Process each vector
for vec in data.iter_mut() {
result.push(0);
result.push(1);
result.append(vec);
result.push(1);
result.push(1);
}
// Add [1, 1] at the end
// result.push(1);
// result.push(1);
result
}
pub fn wrap_arg(arg: &[i32]) -> Vec<i32> {