diff options
| author | Danilo M. <danix@danix.xyz> | 2026-10-28 10:29:52 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-10-28 10:29:52 +0100 |
| commit | f5a4bab82e97de7d20d1e6b5cae250a150b71891 (patch) | |
| tree | fa5ca75297ac530cf3e547411774c44fd91e9d3b /tests/test_formula.py | |
| parent | 78e04c2afe4878aa29108785cce1e32b9072c265 (diff) | |
| download | generat3D-f5a4bab82e97de7d20d1e6b5cae250a150b71891.tar.gz generat3D-f5a4bab82e97de7d20d1e6b5cae250a150b71891.zip | |
Add constant, linear and TCB spline formula operations
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/test_formula.py')
| -rw-r--r-- | tests/test_formula.py | 38 |
1 files changed, 38 insertions, 0 deletions
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) |
