Note
Go to the end to download the full example code.
Saving and loading splines#
import sys
import numpy as np
import splinebox.basis_functions
import splinebox.spline_curves
We start by creating a random spline.
spline = splinebox.spline_curves.Spline(
M=5, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.random.rand(5, 3)
)
Let’s save the spline:
spline.to_json("spline.json")
Here is what the json file looks like:
with open("spline.json") as f:
sys.stdout.write(f.read())
{
"version": 1,
"M": 5,
"basis_function": "B3",
"closed": true,
"control_points": [
[
0.5016554724171337,
0.4416238786606088,
0.47326702721774216
],
[
0.6524722460097683,
0.6053770432208045,
0.695026971010336
],
[
0.7070316286033038,
0.5142548801152815,
0.6724847941328402
],
[
0.37819506061997454,
0.5616607913638806,
0.49958695237608364
],
[
0.8575693930955125,
0.11137196391037596,
0.2556912503928378
]
]
}
Next, we will create a new spline based on the json file.
loaded_spline = splinebox.spline_curves.Spline.from_json("spline.json")
You can also save multiple splines in a single json file.
splines = []
for _ in range(3):
spline = splinebox.spline_curves.Spline(
M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.random.rand(4, 1)
)
splines.append(spline)
splinebox.spline_curves.splines_to_json("splines.json", splines)
Here is what a json file with multiple splines looks like:
with open("splines.json") as f:
sys.stdout.write(f.read())
[
{
"version": 1,
"M": 4,
"basis_function": "B3",
"closed": true,
"control_points": [
[
0.031686973636141036
],
[
0.7250896209096424
],
[
0.7219699327486673
],
[
0.7516050255478786
]
]
},
{
"version": 1,
"M": 4,
"basis_function": "B3",
"closed": true,
"control_points": [
[
0.5901515014187777
],
[
0.3668762902814704
],
[
0.36227291731273736
],
[
0.37737078500584276
]
]
},
{
"version": 1,
"M": 4,
"basis_function": "B3",
"closed": true,
"control_points": [
[
0.7830228207359472
],
[
0.7605990520865326
],
[
0.8375530694167694
],
[
0.75948785047537
]
]
}
]
Lastly, we load multiple splines from a single json file.
splines = splinebox.spline_curves.splines_from_json("splines.json")
print(splines)
[splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.03168697],
[0.72508962],
[0.72196993],
[0.75160503]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.5901515 ],
[0.36687629],
[0.36227292],
[0.37737079]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.78302282],
[0.76059905],
[0.83755307],
[0.75948785]]))]
Total running time of the script: (0 minutes 0.005 seconds)