Note
Go to the end to download the full example code.
Shape Interpolation#
Given two shapes as triangulated 3D models, how can we smoothly interpolate another model between them using signed distance fields
import numpy as np
import pyvista as pv
import mirage as mr
Animating the entire interpolation
obj1 = mr.SpaceObject('icosahedron.obj').clean()
obj2 = mr.SpaceObject('duck.obj').clean()
pl = pv.Plotter()
pl.open_gif('shape_interpolation.gif')
for frac1 in np.concatenate((np.linspace(0, 1, 20), np.linspace(1, 0, 20))):
weights = np.array([1 - frac1, frac1]).astype(float)
mr.tic()
obj_merged = mr.merge_shapes([obj1, obj2], weights)
mr.toc()
pl.add_mesh(obj_merged._mesh, color='lightblue', name='mesh', smooth_shading=True)
pl.add_text(
f'{weights[0] * 100:3.0f}% Icosahedron \n{weights[1] * 100:3.0f}% Duck',
font='courier',
name='label',
)
pl.write_frame()
pl.close()

Model file duck.obj not found in current model folder ('/Users/liamrobinson/Documents/maintained-research/mirage/mirage/resources/models'), checking model repository...
Attempting to download duck.obj and its associated material file from the model repository...
Requesting: https://raw.githubusercontent.com/ljrobins/mirage-models/main//Non-Convex/duck.obj
Model files were downloaded successfully!
Elapsed time: 5.45e-01 seconds
Elapsed time: 5.00e-01 seconds
Elapsed time: 4.58e-01 seconds
Elapsed time: 4.53e-01 seconds
Elapsed time: 4.41e-01 seconds
Elapsed time: 4.50e-01 seconds
Elapsed time: 4.42e-01 seconds
Elapsed time: 4.46e-01 seconds
Elapsed time: 4.37e-01 seconds
Elapsed time: 4.48e-01 seconds
Elapsed time: 4.42e-01 seconds
Elapsed time: 4.59e-01 seconds
Elapsed time: 4.27e-01 seconds
Elapsed time: 4.30e-01 seconds
Elapsed time: 4.29e-01 seconds
Elapsed time: 4.55e-01 seconds
Elapsed time: 4.59e-01 seconds
Elapsed time: 4.96e-01 seconds
Elapsed time: 4.60e-01 seconds
Elapsed time: 6.17e-01 seconds
Elapsed time: 5.51e-01 seconds
Elapsed time: 5.50e-01 seconds
Elapsed time: 4.79e-01 seconds
Elapsed time: 4.47e-01 seconds
Elapsed time: 4.46e-01 seconds
Elapsed time: 4.82e-01 seconds
Elapsed time: 4.53e-01 seconds
Elapsed time: 4.31e-01 seconds
Elapsed time: 4.57e-01 seconds
Elapsed time: 4.46e-01 seconds
Elapsed time: 4.80e-01 seconds
Elapsed time: 4.49e-01 seconds
Elapsed time: 4.90e-01 seconds
Elapsed time: 4.86e-01 seconds
Elapsed time: 4.61e-01 seconds
Elapsed time: 4.64e-01 seconds
Elapsed time: 4.57e-01 seconds
Elapsed time: 5.37e-01 seconds
Elapsed time: 5.02e-01 seconds
Elapsed time: 5.09e-01 seconds
Individual interpolation steps in a grid
pl = pv.Plotter(shape=(2, 2))
for i, weight1 in enumerate(np.linspace(0, 1, 4)):
weights = np.array([1 - weight1, weight1]).astype(float)
obj_merged = mr.merge_shapes(
[
mr.SpaceObject('icosahedron.obj').clean(),
mr.SpaceObject('torus.obj').clean(),
],
weights,
)
pl.subplot(i // 2, i % 2)
pl.add_mesh(obj_merged._mesh, color='lightblue', name='mesh', smooth_shading=True)
pl.add_text(
f'{weights[0] * 100:3.0f}% Icosahedron \n{weights[1] * 100:3.0f}% Torus',
font='courier',
name='label',
)
pl.show()

Model file torus.obj not found in current model folder ('/Users/liamrobinson/Documents/maintained-research/mirage/mirage/resources/models'), checking model repository...
Attempting to download torus.obj and its associated material file from the model repository...
Requesting: https://raw.githubusercontent.com/ljrobins/mirage-models/main//Non-Convex/torus.obj
Requesting: https://raw.githubusercontent.com/ljrobins/mirage-models/main//Non-Convex/torus.mtl
Model files were downloaded successfully!
Total running time of the script: (0 minutes 30.127 seconds)