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.15766601573183248,
      0.42259009960167093,
      0.22542307108586646
    ],
    [
      0.6083450597460384,
      0.4458612316799615,
      0.5603097990827618
    ],
    [
      0.5091312705036174,
      0.7885268964385748,
      0.4930071387306414
    ],
    [
      0.06502196718353992,
      0.5634841346252751,
      0.925068939306385
    ],
    [
      0.007413705273537463,
      0.7916397386813127,
      0.4522353414107272
    ]
  ]
}

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.7011744871036247
      ],
      [
        0.23981767953081412
      ],
      [
        0.800088816810553
      ],
      [
        0.9939176315430331
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.05799935243811649
      ],
      [
        0.3165404130641145
      ],
      [
        0.868626420377861
      ],
      [
        0.5353343094901992
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.49532633001930293
      ],
      [
        0.1365410112724511
      ],
      [
        0.4113199511131832
      ],
      [
        0.3209705449837128
      ]
    ]
  }
]

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.70117449],
       [0.23981768],
       [0.80008882],
       [0.99391763]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.05799935],
       [0.31654041],
       [0.86862642],
       [0.53533431]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.49532633],
       [0.13654101],
       [0.41131995],
       [0.32097054]]))]

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

Gallery generated by Sphinx-Gallery