|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +from interpolatepy.b_spline_cubic import CubicBSplineInterpolation |
| 5 | +from mpl_toolkits.mplot3d import Axes3D # noqa: F401 |
| 6 | + |
| 7 | + |
| 8 | +def example_8_8() -> None: |
| 9 | + """ |
| 10 | + Implementation of Example 8.8 from the document: |
| 11 | + 3D cubic B-spline interpolation through the given points. |
| 12 | + """ |
| 13 | + # Define the points to interpolate as given in the matrix |
| 14 | + points = np.array( |
| 15 | + [ |
| 16 | + [83, -54, 119], |
| 17 | + [-64, 10, 124], |
| 18 | + [42, 79, 226], |
| 19 | + [-98, 23, 222], |
| 20 | + [-13, 125, 102], |
| 21 | + [140, 81, 92], |
| 22 | + [43, 32, 92], |
| 23 | + [-65, -17, 134], |
| 24 | + [-45, -89, 182], |
| 25 | + [71, 90, 192], |
| 26 | + ] |
| 27 | + ) |
| 28 | + |
| 29 | + # Create the cubic B-spline interpolation |
| 30 | + # Use chord length parameterization as recommended for 3D curves |
| 31 | + interpolation = CubicBSplineInterpolation(points, method="chord_length", auto_derivatives=True) |
| 32 | + |
| 33 | + # Create a figure for 3D visualization |
| 34 | + fig = plt.figure(figsize=(12, 10)) |
| 35 | + ax = fig.add_subplot(111, projection="3d") |
| 36 | + |
| 37 | + # Plot the B-spline curve with control polygon using inherited method |
| 38 | + interpolation.plot_3d(num_points=200, show_control_polygon=True, ax=ax) |
| 39 | + |
| 40 | + # Add the interpolation points |
| 41 | + ax.scatter( |
| 42 | + points[:, 0], |
| 43 | + points[:, 1], |
| 44 | + points[:, 2], |
| 45 | + color="green", |
| 46 | + marker="o", |
| 47 | + s=100, |
| 48 | + label="Interpolation points", |
| 49 | + ) |
| 50 | + |
| 51 | + # Set labels and adjust the view |
| 52 | + ax.set_xlabel("X") |
| 53 | + ax.set_ylabel("Y") |
| 54 | + ax.set_zlabel("Z") |
| 55 | + ax.set_title("3D Cubic B-spline Interpolation (Example 8.8)") |
| 56 | + |
| 57 | + # Add a legend |
| 58 | + ax.legend() |
| 59 | + |
| 60 | + # Adjust the viewing angle to better see the 3D shape |
| 61 | + ax.view_init(elev=30, azim=45) |
| 62 | + |
| 63 | + plt.tight_layout() |
| 64 | + plt.show() |
| 65 | + |
| 66 | + # Print some information about the interpolation |
| 67 | + print("Cubic B-spline Interpolation Information:") |
| 68 | + print(f"Number of interpolation points: {len(points)}") |
| 69 | + print(f"Degree of B-spline: {interpolation.degree}") |
| 70 | + print(f"Control points:\n [{(interpolation.control_points)}]") |
| 71 | + print(f"Knots vector: \n[{interpolation.knots}]") |
| 72 | + print(f"V0: \n[{interpolation.v0}]") |
| 73 | + print(f"Vn: \n[{interpolation.vn}]") |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + example_8_8() |
0 commit comments