From f5a4bab82e97de7d20d1e6b5cae250a150b71891 Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 28 Oct 2026 10:29:52 +0100 Subject: Add constant, linear and TCB spline formula operations Co-Authored-By: Claude Haiku 5.5 --- tests/test_formula.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'tests') diff --git a/tests/test_formula.py b/tests/test_formula.py index 72f41ea..7440681 100644 --- a/tests/test_formula.py +++ b/tests/test_formula.py @@ -130,3 +130,41 @@ class GraphTest(unittest.TestCase): g.add_formula("c", [push("a"), push("a"), {"op": "add"}]) g.add_formula("d", [push("b"), push("c"), {"op": "add"}]) self.assertEqual(g.evaluate()["d"], 3) + + +from generat3d.formula import spline + + +def spline_ops(kind, knots): + return ([{"op": "push", "key": "a"}] + [val(k) for k in knots] + + [val(len(knots)), {"op": kind}]) + + +class SplineTest(unittest.TestCase): + LINE = [[0, 0], [1, 1], [2, 2], [3, 3]] + + def test_outside_range_holds_ends(self): + for kind in ("spline_constant", "spline_linear", "spline_tcb"): + self.assertEqual(spline(kind, -1, self.LINE), 0) + self.assertEqual(spline(kind, 9, self.LINE), 3) + + def test_constant(self): + self.assertEqual(spline("spline_constant", 1.7, [[0, 5], [1, 6], [2, 7]]), 6) + + def test_linear(self): + self.assertAlmostEqual(spline("spline_linear", 0.25, [[0, 0], [1, 4]]), 1.0) + + def test_tcb_hits_knots(self): + knots = [[0, 0, 0, 0, 0], [1, 3, 0, 0, 0], [2, 1, 0, 0, 0]] + self.assertEqual(spline("spline_tcb", 1, knots), 3) + + def test_tcb_zero_params_is_catmull_rom(self): + knots = [k + [0, 0, 0] for k in self.LINE] + self.assertAlmostEqual(spline("spline_tcb", 1.5, knots), 1.5) + + def test_spline_in_graph(self): + g = Graph() + g.add_prop("a", 0.5) + g.add_prop("b") + g.add_formula("b", spline_ops("spline_linear", [[0, 0], [1, 10]])) + self.assertAlmostEqual(g.evaluate()["b"], 5.0) -- cgit v1.2.3