|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | + Visualization Examples for the NURBS-Python Package |
| 5 | + Released under The MIT License |
| 6 | + Developed by Onur Rauf Bingol (c) 2018 |
| 7 | +""" |
| 8 | +from geomdl import BSpline |
| 9 | +from geomdl import utilities |
| 10 | + |
| 11 | +import numpy as np |
| 12 | +import matplotlib |
| 13 | +from mpl_toolkits.mplot3d import Axes3D |
| 14 | +import matplotlib.pyplot as plt |
| 15 | + |
| 16 | +# |
| 17 | +# Curve Evaluation |
| 18 | +# |
| 19 | + |
| 20 | +# Create a BSpline curve instance |
| 21 | +curve = BSpline.Curve() |
| 22 | + |
| 23 | +# Set evaluation delta |
| 24 | +curve.delta = 0.001 |
| 25 | + |
| 26 | +# Set up the NURBS curve |
| 27 | +curve.read_ctrlpts_from_txt("../curve3d/ex_curve3d02.cpt") |
| 28 | +curve.degree = 3 |
| 29 | + |
| 30 | +# Auto-generate knot vector |
| 31 | +curve.knotvector = utilities.generate_knot_vector(curve.degree, len(curve.ctrlpts)) |
| 32 | + |
| 33 | +# Evaulate curve |
| 34 | +curve.evaluate() |
| 35 | + |
| 36 | +# |
| 37 | +# Tangent Vector Evaluation |
| 38 | +# |
| 39 | + |
| 40 | +# Store tangent vectors in a list for plotting |
| 41 | +curvetans = [] |
| 42 | + |
| 43 | +# Evaluate curve tangent at u = 0.0175 |
| 44 | +ct1 = curve.tangent(0.0175, normalize=True) |
| 45 | +curvetans.append(ct1) |
| 46 | + |
| 47 | +# Evaluate curve tangent at u = 0.075 |
| 48 | +ct2 = curve.tangent(0.075, normalize=True) |
| 49 | +curvetans.append(ct2) |
| 50 | + |
| 51 | +# Evaluate curve tangent at u = 0.375 |
| 52 | +ct3 = curve.tangent(0.375, normalize=True) |
| 53 | +curvetans.append(ct3) |
| 54 | + |
| 55 | +# Evaluate curve tangent at u = 0.535 |
| 56 | +ct4 = curve.tangent(0.535, normalize=True) |
| 57 | +curvetans.append(ct4) |
| 58 | + |
| 59 | +# Evaluate curve tangent at u = 0.65 |
| 60 | +ct5 = curve.tangent(0.65, normalize=True) |
| 61 | +curvetans.append(ct5) |
| 62 | + |
| 63 | +# Evaluate curve tangent at u = 0.85 |
| 64 | +ct6 = curve.tangent(0.85, normalize=True) |
| 65 | +curvetans.append(ct6) |
| 66 | + |
| 67 | +# Evaluate curve tangent at u = 0.975 |
| 68 | +ct7 = curve.tangent(0.975, normalize=True) |
| 69 | +curvetans.append(ct7) |
| 70 | + |
| 71 | +# |
| 72 | +# Normal Vector Evaluation |
| 73 | +# |
| 74 | + |
| 75 | +# Store normal vectors in a list for plotting |
| 76 | +curvenorms = [] |
| 77 | + |
| 78 | +# Evaluate curve normal at u = 0.0175 |
| 79 | +cn1 = curve.normal(0.0175, normalize=True) |
| 80 | +curvenorms.append(cn1) |
| 81 | + |
| 82 | +# Evaluate curve normal at u = 0.075 |
| 83 | +cn2 = curve.normal(0.075, normalize=True) |
| 84 | +curvenorms.append(cn2) |
| 85 | + |
| 86 | +# Evaluate curve normal at u = 0.375 |
| 87 | +cn3 = curve.normal(0.375, normalize=True) |
| 88 | +curvenorms.append(cn3) |
| 89 | + |
| 90 | +# Evaluate curve normal at u = 0.535 |
| 91 | +cn4 = curve.normal(0.535, normalize=True) |
| 92 | +curvenorms.append(cn4) |
| 93 | + |
| 94 | +# Evaluate curve normal at u = 0.65 |
| 95 | +cn5 = curve.normal(0.65, normalize=True) |
| 96 | +curvenorms.append(cn5) |
| 97 | + |
| 98 | +# Evaluate curve normal at u = 0.85 |
| 99 | +cn6 = curve.normal(0.85, normalize=True) |
| 100 | +curvenorms.append(cn6) |
| 101 | + |
| 102 | +# Evaluate curve normal at u = 0.975 |
| 103 | +cn7 = curve.normal(0.975, normalize=True) |
| 104 | +curvenorms.append(cn7) |
| 105 | + |
| 106 | +# |
| 107 | +# Normal Vector Evaluation |
| 108 | +# |
| 109 | + |
| 110 | +# Store binormal vectors in a list for plotting |
| 111 | +curvebinorms = [] |
| 112 | + |
| 113 | +# Evaluate curve binormal vector at u = 0.0175 |
| 114 | +cbn1 = curve.binormal(0.0175, normalize=True) |
| 115 | +curvebinorms.append(cbn1) |
| 116 | + |
| 117 | +# Evaluate curve binormal vector at u = 0.075 |
| 118 | +cbn2 = curve.binormal(0.075, normalize=True) |
| 119 | +curvebinorms.append(cbn2) |
| 120 | + |
| 121 | +# Evaluate curve binormal vector at u = 0.375 |
| 122 | +cbn3 = curve.binormal(0.375, normalize=True) |
| 123 | +curvebinorms.append(cbn3) |
| 124 | + |
| 125 | +# Evaluate curve binormal vector at u = 0.535 |
| 126 | +cbn4 = curve.binormal(0.535, normalize=True) |
| 127 | +curvebinorms.append(cbn4) |
| 128 | + |
| 129 | +# Evaluate curve binormal vector at u = 0.65 |
| 130 | +cbn5 = curve.binormal(0.65, normalize=True) |
| 131 | +curvebinorms.append(cbn5) |
| 132 | + |
| 133 | +# Evaluate curve binormal vector at u = 0.85 |
| 134 | +cbn6 = curve.binormal(0.85, normalize=True) |
| 135 | +curvebinorms.append(cbn6) |
| 136 | + |
| 137 | +# Evaluate curve binormal vector at u = 0.975 |
| 138 | +cbn7 = curve.binormal(0.975, normalize=True) |
| 139 | +curvebinorms.append(cbn7) |
| 140 | + |
| 141 | +# |
| 142 | +# Control Points, Curve and Tangent Vector Plotting using Matplotlib |
| 143 | +# |
| 144 | + |
| 145 | +# Arrange control points and evaluated curve points for plotting |
| 146 | +ctrlpts = np.array(curve.ctrlpts) |
| 147 | +curvepts = np.array(curve.curvepts) |
| 148 | + |
| 149 | +# Convert tangent, normal and binormal vector lists into NumPy arrays |
| 150 | +ctarr = np.array(curvetans) |
| 151 | +cnarr = np.array(curvenorms) |
| 152 | +cbnarr = np.array(curvebinorms) |
| 153 | + |
| 154 | +# Draw the control points polygon, the 3D curve and the tangent vectors |
| 155 | +fig = plt.figure(figsize=(10.67, 8), dpi=96) |
| 156 | +ax = fig.gca(projection='3d') |
| 157 | + |
| 158 | +# Plot 3D lines |
| 159 | +ax.plot(ctrlpts[:, 0], ctrlpts[:, 1], ctrlpts[:, 2], color='black', linestyle='-.', marker='o') |
| 160 | +ax.plot(curvepts[:, 0], curvepts[:, 1], curvepts[:, 2], color='cyan', linestyle='-') |
| 161 | + |
| 162 | +# Plot tangent vectors |
| 163 | +ax.quiver(ctarr[:, 0, 0], ctarr[:, 0, 1], ctarr[:, 0, 2], ctarr[:, 1, 0], ctarr[:, 1, 1], ctarr[:, 1, 2], color='blue', length=2.5) |
| 164 | + |
| 165 | +# Plot normal vectors |
| 166 | +ax.quiver(cnarr[:, 0, 0], cnarr[:, 0, 1], cnarr[:, 0, 2], cnarr[:, 1, 0], cnarr[:, 1, 1], cnarr[:, 1, 2], color='red', length=2.5) |
| 167 | + |
| 168 | +# Plot normal vectors |
| 169 | +ax.quiver(cbnarr[:, 0, 0], cbnarr[:, 0, 1], cbnarr[:, 0, 2], cbnarr[:, 1, 0], cbnarr[:, 1, 1], cbnarr[:, 1, 2], color='green', length=2.5) |
| 170 | + |
| 171 | +# Add legend to 3D plot, @ref: https://stackoverflow.com/a/20505720 |
| 172 | +ctrlpts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='-.', color='black', marker='o') |
| 173 | +curvepts_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='cyan', marker='o') |
| 174 | +tangent_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='blue', marker='>') |
| 175 | +normal_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='red', marker='>') |
| 176 | +binormal_proxy = matplotlib.lines.Line2D([0], [0], linestyle='none', color='green', marker='>') |
| 177 | +ax.legend([ctrlpts_proxy, curvepts_proxy, tangent_proxy, normal_proxy, binormal_proxy], ['Control Points', 'Curve', 'Tangents', 'Normals', 'Binormals'], numpoints=1) |
| 178 | + |
| 179 | +# Display the 3D plot |
| 180 | +plt.show() |
0 commit comments