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.022659996281927608,
      0.5672911769117026,
      0.22177962312871247
    ],
    [
      0.43475150600307066,
      0.3838674893182086,
      0.6666599669935431
    ],
    [
      0.5208172060859426,
      0.2964702563910885,
      0.017541610114136685
    ],
    [
      0.5102874235243413,
      0.8405273469579504,
      0.7637445683104783
    ],
    [
      0.16001758232290375,
      0.9863014920953063,
      0.7229409773466798
    ]
  ]
}

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.9124248657139222
      ],
      [
        0.09988754786822207
      ],
      [
        0.3895280583372148
      ],
      [
        0.8400278558827745
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.4531338505645528
      ],
      [
        0.18441198511362278
      ],
      [
        0.721376034881579
      ],
      [
        0.20851987486619883
      ]
    ]
  },
  {
    "version": 1,
    "M": 4,
    "basis_function": "B3",
    "closed": true,
    "control_points": [
      [
        0.47611568426180884
      ],
      [
        0.9302773244955215
      ],
      [
        0.6849124729992135
      ],
      [
        0.14325459502990578
      ]
    ]
  }
]

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.91242487],
       [0.09988755],
       [0.38952806],
       [0.84002786]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.45313385],
       [0.18441199],
       [0.72137603],
       [0.20851987]])), splinebox.spline_curves.Spline(M=4, basis_function=splinebox.basis_functions.B3(), closed=True, control_points=np.array([[0.47611568],
       [0.93027732],
       [0.68491247],
       [0.1432546 ]]))]

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

Gallery generated by Sphinx-Gallery