Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: makeStream refuses a bend tighter than its banks instead of folding the ribbon

gaoypeng3f8476e7f965
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
index d23c693c..2ffddc54 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
@@ -257,6 +257,16 @@ export function makeStream(opts = {}) {
.divideScalar((after - before) * length);
return { position, tangent, across, longitudinal, acrossDerivative };
}
+ // A bank further from the centreline than the bend's radius folds the
+ // ribbon back over itself (downward-facing water and bed). Refuse it.
+ const reach = opts.bed === false ? 0.5 : 0.625;
+ for (let i = 0; i <= segments; i++) {
+ const u = i / segments, radius = 1 / Math.max(1e-9, frame(u).acrossDerivative.length());
+ if (radius <= widthAt(u) * reach)
+ throw new RangeError(`makeStream: the path bends tighter (radius ${radius.toFixed(2)} m at u=${u.toFixed(3)}) `
+ + `than the ${opts.bed === false ? 'water' : 'bed'} half-width ${(widthAt(u) * reach).toFixed(2)} m; `
+ + 'narrow the stream or widen the bend');
+ }
function height(s, l, t) {
const bank = 1 - Math.pow(Math.min(1, Math.abs(l) / (widthAt(s / length) * 0.5)), 6);
let h = 0;
diff --git a/harness/tests/scene_runtime/lib/test_library.py b/harness/tests/scene_runtime/lib/test_library.py
index 4642a291..a9a6dada 100644
--- a/harness/tests/scene_runtime/lib/test_library.py
+++ b/harness/tests/scene_runtime/lib/test_library.py
@@ -308,3 +308,24 @@ export async function createScene({ renderer }) {
compiled = report["compile"]
assert compiled["gpu"], "this claim is only worth a real GPU"
assert compiled["programs"] >= len(modules), compiled
+
+
+# Named bugs where a factory once built silently broken output from input it
+# should refuse: (module, call, a phrase the RangeError must carry).
+REFUSALS = [
+ # a bend tighter than the bank folded the ribbon into down-facing water
+ ("stream.js", "makeStream({ points: [[0, 1, 0], [6, .9, 4], [0, .8, 8], [6, .7, 12]], width: 2 })",
+ "bends tighter"),
+]
+
+
[email protected](("module", "call", "phrase"), REFUSALS, ids=[r[0] for r in REFUSALS])
+def test_factories_refuse_input_they_would_silently_break(module, call, phrase):
+ out = measure(f"""
+import * as M from './lib/{module}';
+const {{ {call.split('(')[0]} }} = M;
+let error = null;
+try {{ {call}; }} catch (e) {{ error = e instanceof RangeError ? e.message : 'not a RangeError: ' + e; }}
+console.log(JSON.stringify({{ error }}));
+""", (module,))
+ assert out["error"] and phrase in out["error"], out