Untitled

 avatar
unknown
plain_text
a year ago
1.4 kB
4
Indexable
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

def loft_between_polylines(polyline1, polyline2):
    if len(polyline1) != len(polyline2):
        raise ValueError("Both polylines must have the same number of vertices")
    
    num_vertices = len(polyline1)
    
    # Convert the vertices to numpy arrays for easier manipulation
    polyline1 = np.array(polyline1)
    polyline2 = np.array(polyline2)
    
    # Create a parameter that goes from 0 to 1
    t = np.linspace(0, 1, num_vertices)
    
    # Interpolate between the two polylines using the parameter t
    interpolated_vertices = (1 - t[:, np.newaxis]) * polyline1 + t[:, np.newaxis] * polyline2
    
    return interpolated_vertices

# Define two example polylines as lists of vertices
polyline1 = [(0, 0, 0), (1, 2, 1), (2, 3, 2), (3, 2, 3)]
polyline2 = [(0, 0, 5), (1, 1, 6), (2, 2, 7), (3, 3, 8)]

# Perform lofting between the polylines
interpolated_vertices = loft_between_polylines(polyline1, polyline2)

# Plot the result in 3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(*polyline1.T, marker='o', label='Polyline 1')
ax.plot(*polyline2.T, marker='o', label='Polyline 2')
ax.plot(*interpolated_vertices.T, marker='o', label='Interpolated')

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()

plt.show()