Skip to content

Run aabb in wasm #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions cetz-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use ciborium::de::from_reader;
use ciborium::ser::into_writer;
use serde::Deserialize;
use serde::Serialize;
use wasm_minimal_protocol::*;

initiate_protocol!();
Expand Down Expand Up @@ -103,3 +104,53 @@ pub fn cubic_extrema_func(input: &[u8]) -> Vec<u8> {
}
}
}

#[derive(Serialize, Deserialize)]
struct Bounds {
low: Point,
high: Point,
}

/// Compute the axis-aligned bounding box (aabb).
fn aabb(init: Option<Bounds>, pts: Vec<Point>) -> Result<Bounds, String> {
let mut bounds = match init {
Some(init) => init,
None => Bounds {
low: pts.first().unwrap().clone(),
high: pts.first().unwrap().clone(),
},
};
for pt in pts {
if pt.len() != 3 {
return Err("Point must have 3 dimensions".to_string());
}
for dim in 0..pt.len() {
if pt[dim] < bounds.low[dim] {
bounds.low[dim] = pt[dim];
}
if bounds.high[dim] < pt[dim] {
bounds.high[dim] = pt[dim];
}
}
}
Ok(bounds)
}

#[derive(Deserialize)]
struct AabbArgs {
pts: Vec<Point>,
init: Option<Bounds>,
}

#[wasm_func]
pub fn aabb_func(input: &[u8]) -> Result<Vec<u8>, String> {
match from_reader::<AabbArgs, _>(input) {
Ok(input) => {
let mut buf = Vec::new();
let bounds = aabb(input.init, input.pts)?;
into_writer(&bounds, &mut buf).unwrap();
Ok(buf)
}
Err(e) => Err(e.to_string()),
}
}
32 changes: 5 additions & 27 deletions src/aabb.typ
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
#import "vector.typ"
#let cetz-core = plugin("../cetz-core/cetz_core.wasm")

/// Compute an axis aligned bounding box (aabb) for a list of <Type>vectors</Type>.
///
/// - pts (array): List of <Type>vector</Type>s.
/// - init (aabb): Initial aabb
/// -> aabb
#let aabb(pts, init: none) = {
if type(pts) == array {
let bounds = if init == none and 0 < pts.len() {
let pt = pts.at(0)
(low: pt, high: pt)
} else {
init
}
for pt in pts {
assert(type(pt) == array and pt.len() == 3, message: repr(init) + repr(pts))
let (x, y, z) = pt

let (lo-x, lo-y, lo-z) = bounds.low
bounds.low = (calc.min(lo-x, x), calc.min(lo-y, y), calc.min(lo-z, z))

let (hi-x, hi-y, hi-z) = bounds.high
bounds.high = (calc.max(hi-x, x), calc.max(hi-y, y), calc.max(hi-z, z))
}
return bounds
} else if type(pts) == dictionary {
if init == none {
return pts
} else {
return aabb((pts.low, pts.high,), init: init)
}
}

panic("Expected array of vectors or bbox dictionary, got: " + repr(pts))
let args = (pts: pts, init: init)
let encoded = cbor.encode(args)
let bounds = cbor(cetz-core.aabb_func(encoded))
return bounds
}

/// Get the mid-point of an AABB as vector.
Expand Down
5 changes: 3 additions & 2 deletions src/process.typ
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
path-util.bounds(drawable.segments)
} else if drawable.type == "content" {
let (x, y, _, w, h,) = drawable.pos + (drawable.width, drawable.height)
((x + w / 2, y - h / 2, 0), (x - w / 2, y + h / 2, 0))
((x + w / 2, y - h / 2, 0.0), (x - w / 2, y + h / 2, 0.0))
},
init: bounds
)
Expand Down Expand Up @@ -81,7 +81,8 @@
let r = element(ctx, el)
if r != none {
if r.bounds != none {
bounds = aabb.aabb(r.bounds, init: bounds)
let pts = (r.bounds.low, r.bounds.high,)
bounds = aabb.aabb(pts, init: bounds)
}
ctx = r.ctx
drawables += r.drawables
Expand Down