Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

feat(assembler): size-adaptive optimize quality

Sidwebworkse3050e0f07f6
diff --git a/apps/assembler/src/actions/optimize.rs b/apps/assembler/src/actions/optimize.rs
index e28309c323..1004c83672 100644
--- a/apps/assembler/src/actions/optimize.rs
+++ b/apps/assembler/src/actions/optimize.rs
@@ -36,6 +36,12 @@ pub struct Opts {
pub tolerance: Option<f32>,
/// Auto-mode normalized error budget. `None` = lossless.
pub auto_error: Option<f32>,
+ /// Size-adaptive policy: the caller supplied no explicit quality knob, so the
+ /// service is free to scale the compression by the model's tessellated weight.
+ /// When set, `run_optimize` overrides `auto_error` from `input_bytes` (small
+ /// models keep the baseline high-quality budget; large models decimate harder,
+ /// still error-bounded). Off when any explicit knob was given (full override).
+ pub auto_scale: bool,
/// Draco quantization bits (position, normal, texcoord) — Draco codec only.
pub draco_bits: (i32, i32, i32),
/// Quantize normals to i16 (none/meshopt codecs).
@@ -197,6 +203,22 @@ impl Src {
}
}
+/// Map tessellated GLB weight → auto-mode error budget (normalized, a fraction of
+/// each mesh's extent). Log-scaled between two anchors: at/below `SMALL` the
+/// baseline high-quality budget (unchanged from today, so small models never
+/// regress), ramping up to `MAX_ERR` at/above `LARGE`. Still bounded and per-mesh
+/// adaptive — this decimates more on heavy models, it does not switch to unbounded
+/// ratio targeting.
+fn derive_auto_error(input_bytes: usize) -> f32 {
+ const SMALL: f64 = 512.0 * 1024.0; // 512 KiB
+ const LARGE: f64 = 32.0 * 1024.0 * 1024.0; // 32 MiB
+ const MAX_ERR: f64 = 0.02; // 2% of extent — heavier but still reasonable
+ let min_err = optimize::DEFAULT_AUTO_ERROR as f64; // 0.5%
+ let b = (input_bytes as f64).clamp(SMALL, LARGE);
+ let t = (b.ln() - SMALL.ln()) / (LARGE.ln() - SMALL.ln()); // 0..1
+ (min_err + (MAX_ERR - min_err) * t) as f32
+}
+
/// Resolve the format, load the source into GLB bytes, then walk the simplify
/// ladder — the first rung under both gates wins; if none fit, fail with
/// `cannot_fit_budget` (never store an over-cap blob).
@@ -217,6 +239,19 @@ fn run_optimize(
let glb = src.bytes();
let input_bytes = glb.len();
+ // Size-adaptive quality (only when the caller gave no explicit knob): scale the
+ // auto-mode error budget by the tessellated GLB weight, so small models keep
+ // the baseline high-quality budget and large ones decimate harder within a
+ // still-bounded, per-mesh-adaptive budget. This smooths the old fit-or-fail
+ // cliff (mid-size models now decimate proportionally instead of not at all
+ // until they bust the size gate). The gate + coarse ratio rungs stay the final
+ // safety net below.
+ let auto_error = if opts.auto_scale {
+ Some(derive_auto_error(input_bytes))
+ } else {
+ opts.auto_error
+ };
+
let ladder = if opts.ladder.is_empty() {
vec![None]
} else {
@@ -248,7 +283,7 @@ fn run_optimize(
codec: opts.codec,
simplify: rung,
tolerance: opts.tolerance,
- auto_error: opts.auto_error,
+ auto_error,
simplify_aggressive: opts.simplify_aggressive,
draco_bits: opts.draco_bits,
quantize_normals: opts.quantize_normals,
@@ -417,3 +452,22 @@ fn codec_name(c: optimize::Codec) -> &'static str {
optimize::Codec::Draco => "draco",
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn auto_error_scales_with_size() {
+ let base = optimize::DEFAULT_AUTO_ERROR;
+ // Small models keep the baseline high-quality budget (no regression).
+ assert!((derive_auto_error(64 * 1024) - base).abs() < 1e-6);
+ assert!((derive_auto_error(512 * 1024) - base).abs() < 1e-6);
+ // Large models decimate harder, capped at 2%.
+ assert!((derive_auto_error(64 * 1024 * 1024) - 0.02).abs() < 1e-6);
+ // Monotonic, strictly increasing through the ramp, and always within bounds.
+ let mid = derive_auto_error(4 * 1024 * 1024);
+ assert!(mid > base && mid < 0.02);
+ assert!(derive_auto_error(2 * 1024 * 1024) < mid);
+ }
+}
diff --git a/apps/assembler/src/main.rs b/apps/assembler/src/main.rs
index 4b1d690f64..3ccdff8cb3 100644
--- a/apps/assembler/src/main.rs
+++ b/apps/assembler/src/main.rs
@@ -558,6 +558,13 @@ pub fn optimize_opts(out: &Value, q: &Value) -> actions::optimize::Opts {
.unwrap_or(optimize::DEFAULT_AUTO_ERROR as f64) as f32,
)
.filter(|&e| e > 0.0),
+ // Size-adaptive quality applies only when the caller pinned no explicit
+ // quality knob (the eager-optimise job's case). Any explicit ladder /
+ // simplify / tolerance / auto_error is a full override and disables it.
+ auto_scale: q["ladder"].is_null()
+ && q["simplify"].is_null()
+ && q["tolerance_mm"].is_null()
+ && q["auto_error"].is_null(),
draco_bits: (
q["draco_position_bits"].as_i64().unwrap_or(14) as i32,
q["draco_normal_bits"].as_i64().unwrap_or(10) as i32,
diff --git a/packages/jobs/src/inngest/functions/tasks/model-optimize.ts b/packages/jobs/src/inngest/functions/tasks/model-optimize.ts
index 78de503aa6..64bf0238c2 100644
--- a/packages/jobs/src/inngest/functions/tasks/model-optimize.ts
+++ b/packages/jobs/src/inngest/functions/tasks/model-optimize.ts
@@ -168,9 +168,12 @@ export const modelOptimizeFunction = inngest.createFunction(
return {
source: { url: internalizeStorageUrl(source.data.signedUrl), format },
output: { path: optimizedPath }
- // quality omitted → the service defaults apply (codec meshopt, merge on,
- // normal quant on, auto simplify tolerance, aggressive ladder to fit the
- // size + render-weight gates).
+ // quality omitted → the service applies its size-adaptive policy: codec
+ // meshopt, merge on, normal quant on, and an auto simplify budget that
+ // scales with the model's tessellated weight (small models keep the
+ // baseline high-quality budget; large ones decimate harder, still
+ // error-bounded), then the ladder + size/render-weight gates as the
+ // final fit. Passing any explicit quality knob disables the scaling.
};
},
mintUploadUrls: async () => {