dhermes / bezier

Helper for Bézier Curves, Triangles, and Higher Order Objects
Apache License 2.0
266 stars 36 forks source link

Add support for plotting 3D Curves #66

Open jhihn opened 7 years ago

jhihn commented 7 years ago

I'd like to know how to plot 3D curves with this library. I've got 2D solved but I get a not-implemented exception when I feed it a 3D curve.

dhermes commented 7 years ago

@jhihn First of all, thanks for using the library!

The NotImplemented is unfortunately an accurate description (as of 5b55152a09c6f7c01d43419d9c0b20e6b39d743d): I have not implemented 3D plotting.

However, it's still possible to do with matplotlib. What you could do is generate the points along the curve similar to how it's done in 2D

In [1]: import numpy as np

In [2]: import bezier

In [3]: nodes = np.asfortranarray([
   ...:     [0.0, 0.0, 0.0],
   ...:     [1.0, 2.0, 3.0],
   ...:     [2.0, 0.0, 9.0],
   ...: ])

In [4]: curve = bezier.Curve.from_nodes(nodes)

In [5]: s_vals = np.linspace(0.0, 1.0, 256)

In [6]: points = curve.evaluate_multi(s_vals)

In [7]: points.shape
Out[7]: (256, 3)

and then plot those in 3D:

In [8]: import matplotlib.pyplot as plt

In [9]: import mpl_toolkits.mplot3d

In [10]: import seaborn

In [11]: seaborn.set()

In [12]: fig = plt.figure()

In [13]: ax = fig.gca(projection='3d')

In [14]: ax.plot(points[:, 0], points[:, 1], points[:, 2])
Out[14]: [<mpl_toolkits.mplot3d.art3d.Line3D at 0x7f974d6ab748>]

In [15]: ax.view_init(azim=-15.0, elev=39.0)

In [16]: plt.show()

figure_1

jhihn commented 7 years ago

Thank you so much for your detailed and prompt reply! I'm unfamiliar with Python plotting and this was exactly what I need!