def test_nurbs_curve_endpoint_interpolation(): """ Test the NURBS curve end-point interpolation property """ # Define the array of control points P = np.zeros((3, 4)) P[:, 0] = [1.00, 2.00, 1.00] P[:, 1] = [2.00, 1.00, 2.00] P[:, 2] = [3.00, 2.00, 3.00] P[:, 3] = [4.00, 1.00, 4.00] # Define the array of control point weights W = np.asarray([2.00, 3.00, 1.00, 2.00]) # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the order of the basis polynomials p = 2 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve nurbsCurve = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Check the corner point values assert np.sum( (nurbsCurve.get_value(u=0.00).flatten() - P[:, 0])**2)**(1 / 2) < 1e-6 assert np.sum( (nurbsCurve.get_value(u=1.00).flatten() - P[:, -1])**2)**(1 / 2) < 1e-6
def test_nurbs_curve_example_1(): """ Test the B-Spline curve computation against a known example (Ex2.2 from the NURBS book) """ # Define the array of control points P2 = np.asarray([1, 3, 5]) P3 = np.asarray([2, 1, 4]) P4 = np.asarray([3, 0, 6]) P = np.zeros((3, 8)) P[:, 2] = P2 P[:, 3] = P3 P[:, 4] = P4 # Define the order of the basis polynomials p = 2 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 # u0 u1 u2 u3 u4 u5 u6 u7 u8 u9 u10 U = np.asarray( [0.00, 0.00, 0.00, 1.00, 2.00, 3.00, 4.00, 4.00, 5.00, 5.00, 5.00]) # Create the B-Spline curve myBSpline = nrb.NurbsCurve(control_points=P, knots=U, degree=p) # Evaluate the B-Spline curve numerically u = 5 / 2 # u-parameter values_numeric = myBSpline.get_value(u) # Evaluate the B-Spline curve analytically (NURBS book page 82) values_analytic = (1 / 8 * P2 + 6 / 8 * P3 + 1 / 8 * P4)[:, np.newaxis] # Check the error error = np.sum((values_analytic - values_numeric)**2)**(1 / 2) print('The two-norm of the evaluation error is : ', error) assert error < 1e-8
def test_nurbs_curve_arclength(): """ Test the NURBS curve arc-length computation """ # Define the array of control points P = np.zeros((3, 11)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.10, 0.50, 0.00] P[:, 2] = [0.20, 0.00, 0.50] P[:, 3] = [0.30, 0.50, 1.00] P[:, 4] = [0.40, 0.00, 0.50] P[:, 5] = [0.50, 0.50, 0.00] P[:, 6] = [0.60, 0.00, 0.50] P[:, 7] = [0.70, 0.50, 1.00] P[:, 8] = [0.80, 0.00, 0.50] P[:, 9] = [0.90, 0.00, 0.00] P[:, 10] = [1.00, 0.00, 0.00] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3]) # Define the order of the basis polynomials p = 4 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve myCurve = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Compute the arc length using fixed order quadrature length_fixed = myCurve.get_arclength() # Compute the arc length using adaptative quadrature def get_arclegth_differential(u): dCdu = myCurve.get_derivative(u, order=1) dLdu = np.sqrt( np.sum(dCdu**2, axis=0)) # dL/du = [(dx_0/du)^2 + ... + (dx_n/du)^2]^(1/2) return dLdu length_adaptative = scipy.integrate.quad(get_arclegth_differential, 0, 1)[0] # Check the arc length error arc_length_error = np.abs(length_fixed - length_adaptative) print("The arc length computation error is : ", arc_length_error) assert arc_length_error < 0.04
def test_nurbs_curve_second_derivative_endpoint(): """ Test the second derivative at the end points of the curve """ # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.10, 0.30, 0.00] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 2, 3, 2, 1]) # Define the order of the basis polynomials # Linear (p = 1), Quadratic (p = 2), Cubic (p = 3), etc. # Set p = n (number of control points minus one) to obtain a Bezier p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve nurbs3D = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Start point ddC_numeric_0 = nurbs3D.get_derivative(u=0.00, order=2)[:, 0] ddC_analytic_0 = p * (p - 1) / U[p + 1] * ( 1 / U[p + 2] * (W[2] / W[0]) * (P[:, 2] - P[:, 0]) - (1 / U[p + 1] + 1 / U[p + 2]) * (W[1] / W[0]) * (P[:, 1] - P[:, 0])) + 2 * (p / U[p + 1])**2 * (W[1] / W[0]) * ( 1 - W[1] / W[0]) * (P[:, 1] - P[:, 0]) # End point ddC_numeric_1 = nurbs3D.get_derivative(u=1.00, order=2)[:, 0] ddC_analytic_1 = p * (p - 1) / (1 - U[n]) * ( 1 / (1 - U[n - 1]) * (W[n - 2] / W[n]) * (P[:, n - 2] - P[:, n]) - (1 / (1 - U[n]) + 1 / (1 - U[n - 1])) * (W[n - 1] / W[n]) * (P[:, n - 1] - P[:, n])) + 2 * (p / (1 - U[n]))**2 * ( W[n - 1] / W[n]) * (1 - W[n - 1] / W[n]) * (P[:, n - 1] - P[:, n]) # Check the error error_0 = np.sum((ddC_analytic_0 - ddC_numeric_0)**2)**(1 / 2) error_1 = np.sum((ddC_analytic_1 - ddC_numeric_1)**2)**(1 / 2) print('The start point second derivative error is : ', error_0) print('The end point second derivative error is : ', error_1) assert error_0 < 1e-6 assert error_1 < 1e-6
def test_nurbs_curve_second_derivative_cfd(): """ Test the first derivative against central finite differences """ # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.00, 0.30, 0.05] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 1, 3, 1, 1]) # Define the order of the basis polynomials # Linear (p = 1), Quadratic (p = 2), Cubic (p = 3), etc. # Set p = n (number of control points minus one) to obtain a Bezier p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve nurbs3D = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Define a new u-parametrization suitable for finite differences h = 1e-5 hh = h + h**2 Nu = 1000 u = np.linspace( 0.00 + hh, 1.00 - hh, Nu) # Make sure that the limits [0, 1] also work when making changes # Compute the NURBS analytic derivative dC_analytic = nurbs3D.get_derivative(u, order=2) # Compute the NURBS central finite differences derivative a = +1 * nurbs3D.get_value(u - h) b = -2 * nurbs3D.get_value(u) c = +1 * nurbs3D.get_value(u + h) dC_cfd = (a + b + c) / h**2 # Check the error error = np.sum((dC_analytic - dC_cfd)**2)**(1 / 2) / u.size print('The two-norm of the second derivative error is : ', error) assert error < 1e-6
def test_nurbs_curve_integer_input(): """ Test the Bezier curve computation in scalar mode for real and complex input """ # Define the array of control points P = np.zeros((2, 2)) P[:, 0] = [0.00, 0.00] P[:, 1] = [1.00, 1.00] # Create the NURBS curve bezierCurve = nrb.NurbsCurve(control_points=P) # Check u=0 values_real = bezierCurve.get_value(u=0).flatten() assert np.sum((values_real - np.asarray([0.0, 0.0]))**2)**(1 / 2) < 1e-6 # Check u=1 values_real = bezierCurve.get_value(u=1).flatten() assert np.sum((values_real - np.asarray([1.0, 1.0]))**2)**(1 / 2) < 1e-6
def test_nurbs_curve_example_2(): """ Test the NURBS curve value against a known example (NURBS book section 7.5) """ # Create a circle using 4 arcs of 90 degrees # Define the array of control points P = np.zeros((2, 9)) P[:, 0] = [1.00, 0.00] P[:, 1] = [1.00, 1.00] P[:, 2] = [0.00, 1.00] P[:, 3] = [-1.00, 1.00] P[:, 4] = [-1.00, 0.00] P[:, 5] = [-1.00, -1.00] P[:, 6] = [0.00, -1.00] P[:, 7] = [1.00, -1.00] P[:, 8] = [1.00, 0.00] # Define the array of control point weights W = np.asarray([ 1, np.sqrt(2) / 2, 1, np.sqrt(2) / 2, 1, np.sqrt(2) / 2, 1, np.sqrt(2) / 2, 1 ]) # Define the order of the basis polynomials p = 2 # Define the knot vector (clamped spline) # p+1 zeros, n minus p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.asarray( [0, 0, 0, 1 / 4, 1 / 4, 1 / 2, 1 / 2, 3 / 4, 3 / 4, 1, 1, 1]) # Create the NURBS curve myCircle = nrb.NurbsCurve(P, W, p, U) # Define the u-parametrization u = np.linspace(0, 1, 101) # Check the radius error coords = myCircle.get_value(u) radius_error = np.sum((np.sum((coords)**2, axis=0) - 1)**2)**(1 / 2) print('The two-norm of the evaluation error is : ', radius_error) assert radius_error < 1e-8
def test_nurbs_curve_first_derivative_cs(): """ Test the first derivative against the complex step method """ # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.00, 0.30, 0.05] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 1, 3, 1, 1]) # Define the order of the basis polynomials # Linear (p = 1), Quadratic (p = 2), Cubic (p = 3), etc. # Set p = n (number of control points minus one) to obtain a Bezier p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve nurbs3D = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Define the u-parametrization u = np.linspace(0, 1, 101) h = 1e-12 # Compute the NURBS analytic derivative dC_analytic = nurbs3D.get_derivative(u, order=1) # Compute the NURBS complex step derivative dC_complex_step = np.imag(nurbs3D.get_value(u + h * 1j)) / h # Check the error error = np.sum((dC_analytic - dC_complex_step)**2)**(1 / 2) / u.size print('The two-norm of the first derivative error is : ', error) assert error < 1e-8
def test_nurbs_curve_point_projection(): # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.10, 0.30, 0.00] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 2, 3, 2, 1]) # Define the order of the basis polynomials # Linear (p = 1), Quadratic (p = 2), Cubic (p = 3), etc. # Set p = n (number of control points minus one) to obtain a Bezier p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve nurbs3D = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Compute a point on the NURBS curve u0 = 5 / 7 P = nurbs3D.get_value(u0) # Project the point on the NURBS curve u_opt = nurbs3D.project_point_to_curve(P) # Check the error error = np.sqrt((u0 - u_opt)**2) print('The error of the projected parameter value is : ', error) assert error < 1e-6
def test_nurbs_curve_endpoint_curvature(): """ Test the NURBS curve end-point curvature property """ # Define the array of control points P = np.zeros((3, 11)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.10, 0.50, 0.00] P[:, 2] = [0.20, 0.00, 0.50] P[:, 3] = [0.30, 0.50, 1.00] P[:, 4] = [0.40, 0.00, 0.50] P[:, 5] = [0.50, 0.50, 0.00] P[:, 6] = [0.60, 0.00, 0.50] P[:, 7] = [0.70, 0.50, 1.00] P[:, 8] = [0.80, 0.00, 0.50] P[:, 9] = [0.90, 0.00, 0.00] P[:, 10] = [1.00, 0.00, 0.00] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3]) # Define the order of the basis polynomials p = 4 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the NURBS curve myCurve = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # Get NURBS curve parameters p = myCurve.p U = myCurve.U P = myCurve.P W = myCurve.W n = np.shape(P)[1] - 1 # Get the endpoint curvature numerically curvature_1a = myCurve.get_curvature(u=0.00)[0] curvature_1b = myCurve.get_curvature(u=1.00)[0] # Get the endpoint curvature analytically curvature_2a = (p - 1) / p * (U[p+1] / U[p+2]) * (W[2] * W[0] / W[1]**2) * \ np.sum(np.cross(P[:, 1] - P[:, 0], P[:, 2] - P[:, 0])**2)**(1/2) * \ np.sum((P[:, 1] - P[:, 0])**2)**(-3/2) curvature_2b = (p - 1) / p * (1 - U[n]) / (1 - U[n-1]) * (W[n] * W[n-2] / W[n-1]**2) * \ np.sum(np.cross(P[:, n-1] - P[:, n], P[:, n-2] - P[:, n])**2)**(1/2) * \ np.sum((P[:, n-1] - P[:, n])**2)**(-3/2) # Check the error error_curvature_start = np.sqrt((curvature_1a - curvature_2a)**2) error_curvature_end = np.sqrt((curvature_1b - curvature_2b)**2) print('Start point curvature error : ', error_curvature_start) print('End point curvature error : ', error_curvature_end) assert error_curvature_start < 1e-10 assert error_curvature_end < 1e-6
P[:, 2] = [0.80, 0.60] P[:, 3] = [0.60, 0.20] P[:, 4] = [0.40, 0.20] # Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the order of the basis polynomials p = 2 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create and plot the B-Spline curve bspline2D = nrb.NurbsCurve(control_points=P, degree=p, knots=U) bspline2D.plot(frenet_serret=True) # -------------------------------------------------------------------------------------------------------------------- # # 3D B-Spline curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.00, 0.30, 0.05] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Define the order of the basis polynomials p = 2
import nurbspy as nrb import matplotlib.pyplot as plt # -------------------------------------------------------------------------------------------------------------------- # # Coons surface example # -------------------------------------------------------------------------------------------------------------------- # # Define the south boundary (rational Bézier curve) P = np.zeros((3, 4)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.33, 0.00, -0.40] P[:, 2] = [0.66, 0.10, 0.60] P[:, 3] = [1.00, 0.20, 0.40] W = np.asarray([1, 2, 2, 1]) nurbsCurve_south = nrb.NurbsCurve(control_points=P, weights=W) # Define the north boundary (rational Bézier curve) P = np.zeros((3, 4)) P[:, 0] = [0.05, 1.00, 0.00] P[:, 1] = [0.33, 1.15, 0.40] P[:, 2] = [0.66, 1.15, 0.00] P[:, 3] = [1.05, 1.25, 0.40] W = np.asarray([1, 2, 2, 1]) nurbsCurve_north = nrb.NurbsCurve(control_points=P, weights=W) # Define the west boundary (rational Bézier curve) P = np.zeros((3, 3)) P[:, 0] = nurbsCurve_south.P[:, 0] P[:, 1] = [-0.20, 0.50, -0.40]
theta_end = 2 * np.pi # End angle (set any arbitrary value) nurbsOuter = nrb.CircularArc(O, X, Y, R, theta_start, theta_end).NurbsCurve # Define the inner circle O = np.asarray([0.00, 0.00, 0.00]) # Circle center X = np.asarray([1.00, 0.00, 0.00]) # Abscissa direction Y = np.asarray([0.00, 1.00, 0.00]) # Ordinate direction R = 0.50 # Circle radius nurbsInner = nrb.CircularArc(O, X, Y, R, theta_start, theta_end).NurbsCurve # Define start closing boundary P = np.zeros((3, 2)) P[:, 0] = nurbsOuter.P[:, 0] P[:, 1] = nurbsInner.P[:, 0] W = np.asarray([nurbsOuter.W[0], nurbsInner.W[0]]) nurbsWest = nrb.NurbsCurve(control_points=P, weights=W) # Define the west boundary (rational Bézier curve) P = np.zeros((3, 2)) P[:, 0] = nurbsOuter.P[:, -1] P[:, 1] = nurbsInner.P[:, -1] W = np.asarray([nurbsOuter.W[-1], nurbsInner.W[-1]]) nurbsEast = nrb.NurbsCurve(control_points=P, weights=W) # Create and plot the Coons NURBS surface to represent the annulus coonsNurbsSurface = nrb.NurbsSurfaceCoons(nurbsOuter, nurbsInner, nurbsWest, nurbsEast).NurbsSurface # coonsNurbsSurface = nrb.NurbsSurfaceRuled(nurbsOuter, nurbsInner).NurbsSurface # Plot the annulus surface and some isoparametric curve fig, ax = coonsNurbsSurface.plot(surface=True,
import nurbspy as nrb import matplotlib.pyplot as plt # -------------------------------------------------------------------------------------------------------------------- # # Ruled surface example # -------------------------------------------------------------------------------------------------------------------- # # Define the lower NURBS curve (rational Bézier curve) P1 = np.zeros((3, 5)) P1[:, 0] = [0.00, 0.00, 0.00] P1[:, 1] = [0.25, 0.00, 0.50] P1[:, 2] = [0.50, 0.00, 0.50] P1[:, 3] = [0.75, 0.00, 0.00] P1[:, 4] = [1.00, 0.00, 0.00] W1 = np.asarray([1, 1, 2, 1, 1]) nurbsCurve1 = nrb.NurbsCurve(control_points=P1, weights=W1) # Define the lower NURBS curve (rational Bézier curve) P2 = np.zeros((3, 5)) P2[:, 0] = [0.00, 1.00, 0.50] P2[:, 1] = [0.25, 1.00, 0.00] P2[:, 2] = [0.50, 1.00, 0.00] P2[:, 3] = [0.75, 1.00, 0.50] P2[:, 4] = [1.00, 1.00, 0.50] W2 = np.asarray([1, 1, 2, 1, 1]) nurbsCurve2 = nrb.NurbsCurve(control_points=P2, weights=W2) # Create and plot the ruled NURBS surface ruledNurbsSurface = nrb.NurbsSurfaceRuled(nurbsCurve1, nurbsCurve2).NurbsSurface fig, ax = ruledNurbsSurface.plot(surface=True,
# Revolution surface example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.20, 0.00, 0.00] P[:, 1] = [0.50, 0.00, 0.25] P[:, 2] = [0.55, 0.00, 0.50] P[:, 3] = [0.45, 0.00, 0.75] P[:, 4] = [0.30, 0.00, 1.00] # Define the array of control point weights W = np.asarray([1, 2, 3, 2, 1]) # Create the generatrix NURBS curve nurbsGeneratrix = nrb.NurbsCurve(control_points=P, weights=W) # Set the a point to define the axis of revolution axis_point = np.asarray([0.0, 0.0, 0.0]) # Set a direction to define the axis of revolution (needs not be unitary) axis_direction = np.asarray([0.2, -0.2, 1.0]) # Set the revolution angle theta_start, theta_end = 0.00, 2 * np.pi # Create and plot the NURBS surface nurbsRevolutionSurface = nrb.NurbsSurfaceRevolution(nurbsGeneratrix, axis_point, axis_direction, theta_start, theta_end).NurbsSurface
# Maximum index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the array of control point weights W = np.asarray([1, 1, 3, 1, 1]) # Define the order of the basis polynomials p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create and plot the NURBS curve my_nurbs = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) my_nurbs.plot(frenet_serret=True) # -------------------------------------------------------------------------------------------------------------------- # # Check the analytic derivatives against a finite difference aproximation # -------------------------------------------------------------------------------------------------------------------- # # Define a u-parametrization suitable for finite differences h = 1e-4 hh = 2 * (h + h**2) Nu = 1000 u = np.linspace(0.00 + hh, 1.00 - hh, Nu) # Compute the NURBS derivatives analytically dC_exact = my_nurbs.get_derivative(u, order=1) ddC_exact = my_nurbs.get_derivative(u, order=2) dddC_exact = my_nurbs.get_derivative(u, order=3)
import nurbspy as nrb import matplotlib.pyplot as plt # -------------------------------------------------------------------------------------------------------------------- # # 2D NURBS curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((2, 5)) P[:, 0] = [0.20, 0.50] P[:, 1] = [0.40, 0.70] P[:, 2] = [0.80, 0.60] P[:, 3] = [0.60, 0.20] P[:, 4] = [0.40, 0.20] # Create the NURBS curve nurbs2D = nrb.NurbsCurve(control_points=P, degree=3) # Define point to be projected P0 = np.asarray([0.50, 0.50]) # Compute projected time u0 = nurbs2D.project_point_to_curve(P0) C0 = nurbs2D.get_value(u0) # Plot the NURBS curve and the projected point fig, ax = nurbs2D.plot() Px, Py = P0.flatten() Cx, Cy = C0.flatten() ax.plot([Px, Cx], [Py, Cy], color='black', linestyle='--',
import nurbspy as nrb import matplotlib.pyplot as plt # -------------------------------------------------------------------------------------------------------------------- # # Extruded surface example: # -------------------------------------------------------------------------------------------------------------------- # # Define the base NURBS curve (rational Bézier curve) P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.50, 0.80, 0.00] P[:, 2] = [0.75, 0.60, 0.00] P[:, 3] = [1.00, 0.30, 0.00] P[:, 4] = [0.80, 0.10, 0.00] W = np.asarray([1, 1, 1, 1, 1]) nurbsCurve = nrb.NurbsCurve(control_points=P, weights=W) # Set the extrusion direction (can be unitary or not) direction = np.asarray([1, 1, 5]) # Set the extrusion length length = 1 # Create and plot the ruled NURBS surface extrudedNurbsSurface = nrb.NurbsSurfaceExtruded(nurbsCurve, direction, length).NurbsSurface fig, ax = extrudedNurbsSurface.plot(surface=True, surface_color='blue', control_points=True) # Plot isoparametric curves extrudedNurbsSurface.plot_isocurve_u(fig, ax, np.linspace(0, 1, 5)) extrudedNurbsSurface.plot_isocurve_v(fig, ax, np.linspace(0, 1, 5))
# Define the array of control weights W = np.asarray([1, 1, 1, 1, 2, 2, 2, 2]) # Highest index of the control points (counting from zero) n = np.shape(P)[1] - 1 # Define the order of the basis polynomials p = 3 # Define the knot vector (clamped spline) # p+1 zeros, n-p equispaced points between 0 and 1, and p+1 ones. In total r+1 points where r=n+p+1 U = np.concatenate((np.zeros(p), np.linspace(0, 1, n - p + 2), np.ones(p))) # Create the B-Spline curve nurbs1 = nrb.NurbsCurve(control_points=P, weights=W, degree=p, knots=U) # -------------------------------------------------------------------------------------------------------------------- # # Create the second NURBS curve # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((2, 4)) P[:, 0] = [0.75, 0.00] P[:, 1] = [1.00, 0.50] P[:, 2] = [1.25, -0.50] P[:, 3] = [1.50, 0.00] # Define the array of control weights W = np.asarray([5, 2, 2, 4]) # Highest index of the control points (counting from zero)
import nurbspy as nrb import matplotlib.pyplot as plt # -------------------------------------------------------------------------------------------------------------------- # # 2D Bezier curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((2, 5)) P[:, 0] = [0.20, 0.50] P[:, 1] = [0.40, 0.70] P[:, 2] = [0.80, 0.60] P[:, 3] = [0.60, 0.20] P[:, 4] = [0.40, 0.20] # Create and plot the Bezier curve bezier2D = nrb.NurbsCurve(control_points=P) bezier2D.plot(frenet_serret=False) # -------------------------------------------------------------------------------------------------------------------- # # 3D Bezier curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.00, 0.30, 0.05] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Create and plot the Bezier curve bezier3D = nrb.NurbsCurve(control_points=P)
# -------------------------------------------------------------------------------------------------------------------- # # 2D rational Bezier curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((2, 5)) P[:, 0] = [0.20, 0.50] P[:, 1] = [0.40, 0.70] P[:, 2] = [0.80, 0.60] P[:, 3] = [0.60, 0.20] P[:, 4] = [0.40, 0.20] # Define the array of control point weights W = np.asarray([1, 1, 3, 1, 1]) # Create and plot the Bezier curve bezier2D = nrb.NurbsCurve(control_points=P, weights=W) bezier2D.plot(frenet_serret=True) # -------------------------------------------------------------------------------------------------------------------- # # 3D rational Bezier curve example # -------------------------------------------------------------------------------------------------------------------- # # Define the array of control points P = np.zeros((3, 5)) P[:, 0] = [0.00, 0.00, 0.00] P[:, 1] = [0.00, 0.30, 0.05] P[:, 2] = [0.25, 0.30, 0.30] P[:, 3] = [0.50, 0.30, -0.05] P[:, 4] = [0.50, 0.10, 0.10] # Define the array of control point weights W = np.asarray([1, 1, 3, 1, 1])