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.604877583261817,
      0.8973616373652878,
      0.7791097967086399
    ],
    [
      0.37550187193629625,
      0.4379707452372238,
      0.7850445897573707
    ],
    [
      0.7759177728451276,
      0.39128574367731783,
      0.7756093065693259
    ],
    [
      0.3647492023783949,
      0.8122016591542017,
      0.16711892862532685
    ],
    [
      0.3053146718031534,
      0.37587443728280723,
      0.6401996086353445
    ]
  ]
}

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.7852251610746872
      ],
      [
        0.2240824689224289
      ],
      [
        0.28012077574868366
      ],
      [
        0.880982949529004
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.389597136974574
      ],
      [
        0.5897895316795863
      ],
      [
        0.07767747578732154
      ],
      [
        0.4238467094377105
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.7397196848979872
      ],
      [
        0.202318598080804
      ],
      [
        0.6733506964130694
      ],
      [
        0.7992432891920798
      ]
    ]
  }
]

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.78522516],
       [0.22408247],
       [0.28012078],
       [0.88098295]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.38959714],
       [0.58978953],
       [0.07767748],
       [0.42384671]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.73971968],
       [0.2023186 ],
       [0.6733507 ],
       [0.79924329]]))]

Total running time of the script: (0 minutes 0.004 seconds)

Gallery generated by Sphinx-Gallery