具有方向导数的贝塞尔曲面矩阵形式

问题描述 投票:0回答:1

我之前问过一个关于贝塞尔曲面的问题并得到了很好的答案,但进一步研究它们的构造,我决定修改代码并添加沿 u 和 w 方向导数的计算。

我在构造最终的贝塞尔曲面时遇到困难。我不知道如何合并我计算过的方向导数

Qu
Qw

我将变换后的控制点添加到曲面图中。我所有的计算和数组都是正确的。我只需要知道如何绘制它。我当前的代码(在 Jupyter 笔记本中)显示没有表面:

current output

import numpy as np
import matplotlib.pyplot as plt


def bernstein_poly(i, n, t):
    return comb(n, i) * (t**i) * ((1 - t)**(n - i))

def bernstein_matrix(n, t):
    return np.array([bernstein_poly(i, n, t) for i in range(n + 1)])

B = np.array([
    [[-15, 0, 15], [-15, 5, 5], [-15, 5, -5], [-15, 0, -15]], 
    [[-5, 5, 15], [-5, 5, 5], [-5, 5, -5], [-5, 5, -15]], 
    [[5, 5, 15], [5, 5, 5], [5, 5, -5], [5, 5, -15]], 
    [[15, 0, 15], [15, 5, 5], [15, 5, -5], [15, 0, -15]]
])
N = np.array([[-1, 3, -3, 1],
              [3, -6, 3, 0],
              [-3, 3, 0, 0],
              [1, 0, 0, 0]])
Nt = N.T

B_transformed = np.zeros((4, 4, 3))

for i in range(3):  
    B_transformed[:, :, i] = N @ B[:, :, i] @ Nt

print("Transformed control points matrix B_transformed:")
print(B_transformed)
u = 0.5
w = 0.5

U = np.array([u**3, u**2, u, 1])
W = np.array([[w**3], [w**2], [w], [1]])

print(U)
print(W)

Q = np.array([U @ B_transformed[:, :, i] @ W for i in range(3)])
print(" Q(0.5, 0.5):")
print(Q)

# Derivative formulas for u and w
U1 = np.array([3*u**2, 2*u, 1, 0])
W1 = np.array([[3*w**2], [2*w], [1], [0]])
print(U1)
print(W1)

# Directional derivative u
Qu = np.array([U1 @ B_transformed[:, :, i] @ W for i in range(3)])
print("Qu(0.5, 0.5):")
print(Qu)

# Directional derivative w
Qw = np.array([U @ B_transformed[:, :, i] @ W1 for i in range(3)])
print("Qw(0.5, 0.5):")
print(Qw) 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(Qw, Qu, Q, rstride=1, cstride=1, color='b', alpha=0.6, edgecolor='w')
ax.scatter(B[:, :, 0], B[:, :, 1], B[:, :, 2], color='r', s=50)
python matplotlib matrix bezier surface
1个回答
1
投票

要计算导数,您需要使用与我之前的答案相同的逻辑:

import numpy as np
import matplotlib.pyplot as plt
from scipy.special import comb

def bernstein_poly(i, n, t):
    return comb(n, i) * (t**i) * ((1 - t)**(n - i))

def bernstein_matrix(n, t):
    return np.array([bernstein_poly(i, n, t) for i in range(n + 1)])

B = np.array([
    [[-15, 0, 15], [-15, 5, 5], [-15, 5, -5], [-15, 0, -15]], 
    [[-5, 5, 15], [-5, 5, 5], [-5, 5, -5], [-5, 5, -15]], 
    [[5, 5, 15], [5, 5, 5], [5, 5, -5], [5, 5, -15]], 
    [[15, 0, 15], [15, 5, 5], [15, 5, -5], [15, 0, -15]]
])
N = np.array([[-1, 3, -3, 1],
              [3, -6, 3, 0],
              [-3, 3, 0, 0],
              [1, 0, 0, 0]])
Nt = N.T

B_transformed = np.zeros((4, 4, 3))

for i in range(3):  
    B_transformed[:, :, i] = N @ B[:, :, i] @ Nt

print("Transformed control points matrix B_transformed:")
print(B_transformed)

u_values = np.linspace(0, 1, 50)
w_values = np.linspace(0, 1, 50)
u_grid, w_grid = np.meshgrid(u_values, w_values)

Q = np.zeros((u_grid.shape[0], u_grid.shape[1], 3))
Qu = np.zeros((u_grid.shape[0], u_grid.shape[1], 3))
Qw = np.zeros((u_grid.shape[0], u_grid.shape[1], 3))

for i in range(u_grid.shape[0]):
    for j in range(u_grid.shape[1]):
        u = u_grid[i, j]
        w = w_grid[i, j]

        U = np.array([u**3, u**2, u, 1])
        W = np.array([[w**3], [w**2], [w], [1]])
        
        U1 = np.array([3*u**2, 2*u, 1, 0])
        W1 = np.array([[3*w**2], [2*w], [1], [0]])
        
        Q[i, j, :] = np.array([U @ B_transformed[:, :, k] @ W for k in range(3)]).flatten()
        Qu[i, j, :] = np.array([U1 @ B_transformed[:, :, k] @ W for k in range(3)]).flatten()
        Qw[i, j, :] = np.array([U @ B_transformed[:, :, k] @ W1 for k in range(3)]).flatten()

print(" Q(0.5, 0.5):")
print(Q)

print("Qu(0.5, 0.5):")
print(Qu)

print("Qw(0.5, 0.5):")
print(Qw) 

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(Q[:, :, 0], Q[:, :, 1], Q[:, :, 2], rstride=1, cstride=1, color='b', alpha=0.6, edgecolor='w')
ax.scatter(B[:, :, 0], B[:, :, 1], B[:, :, 2], color='r', s=50)

plt.show()

返回:

Transformed control points matrix B_transformed:
[[[  0.   0.   0.]
  [  0.   0.   0.]
  [  0.   0.   0.]
  [  0.   0.   0.]]

 [[  0.   0.   0.]
  [  0. -45.   0.]
  [  0.  45.   0.]
  [  0. -15.   0.]]

 [[  0.   0.   0.]
  [  0.  45.   0.]
  [  0. -45.   0.]
  [ 30.  15.   0.]]

 [[  0.   0.   0.]
  [  0. -15.   0.]
  [  0.  15. -30.]
  [-15.   0.  15.]]]
 Q(0.5, 0.5):
[[[-15.           0.          15.        ]
  [-14.3877551    0.29987505  15.        ]
  [-13.7755102    0.58725531  15.        ]
  ...
  [ 13.7755102    0.58725531  15.        ]
  [ 14.3877551    0.29987505  15.        ]
  [ 15.           0.          15.        ]]

 [[-15.           0.29987505  14.3877551 ]
  [-14.3877551    0.58176509  14.3877551 ]
  [-13.7755102    0.85190972  14.3877551 ]
  ...
  [ 13.7755102    0.85190972  14.3877551 ]
  [ 14.3877551    0.58176509  14.3877551 ]
  [ 15.           0.29987505  14.3877551 ]]

 [[-15.           0.58725531  13.7755102 ]
  [-14.3877551    0.85190972  13.7755102 ]
  [-13.7755102    1.10553686  13.7755102 ]
  ...
  [ 13.7755102    1.10553686  13.7755102 ]
  [ 14.3877551    0.85190972  13.7755102 ]
  [ 15.           0.58725531  13.7755102 ]]

 ...

 [[-15.           0.58725531 -13.7755102 ]
  [-14.3877551    0.85190972 -13.7755102 ]
  [-13.7755102    1.10553686 -13.7755102 ]
  ...
  [ 13.7755102    1.10553686 -13.7755102 ]
  [ 14.3877551    0.85190972 -13.7755102 ]
  [ 15.           0.58725531 -13.7755102 ]]

 [[-15.           0.29987505 -14.3877551 ]
  [-14.3877551    0.58176509 -14.3877551 ]
  [-13.7755102    0.85190972 -14.3877551 ]
  ...
  [ 13.7755102    0.85190972 -14.3877551 ]
  [ 14.3877551    0.58176509 -14.3877551 ]
  [ 15.           0.29987505 -14.3877551 ]]

 [[-15.           0.         -15.        ]
  [-14.3877551    0.29987505 -15.        ]
  [-13.7755102    0.58725531 -15.        ]
  ...
  [ 13.7755102    0.58725531 -15.        ]
  [ 14.3877551    0.29987505 -15.        ]
  [ 15.           0.         -15.        ]]]
Qu(0.5, 0.5):
[[[ 30.          15.           0.        ]
  [ 30.          14.3877551    0.        ]
  [ 30.          13.7755102    0.        ]
  ...
  [ 30.         -13.7755102    0.        ]
  [ 30.         -14.3877551    0.        ]
  [ 30.         -15.           0.        ]]

 [[ 30.          14.10037484   0.        ]
  [ 30.          13.52484934   0.        ]
  [ 30.          12.94932384   0.        ]
  ...
  [ 30.         -12.94932384   0.        ]
  [ 30.         -13.52484934   0.        ]
  [ 30.         -14.10037484   0.        ]]

 [[ 30.          13.23823407   0.        ]
  [ 30.          12.69789798   0.        ]
  [ 30.          12.1575619    0.        ]
  ...
  [ 30.         -12.1575619    0.        ]
  [ 30.         -12.69789798   0.        ]
  [ 30.         -13.23823407   0.        ]]

 ...

 [[ 30.          13.23823407   0.        ]
  [ 30.          12.69789798   0.        ]
  [ 30.          12.1575619    0.        ]
  ...
  [ 30.         -12.1575619    0.        ]
  [ 30.         -12.69789798   0.        ]
  [ 30.         -13.23823407   0.        ]]

 [[ 30.          14.10037484   0.        ]
  [ 30.          13.52484934   0.        ]
  [ 30.          12.94932384   0.        ]
  ...
  [ 30.         -12.94932384   0.        ]
  [ 30.         -13.52484934   0.        ]
  [ 30.         -14.10037484   0.        ]]

 [[ 30.          15.           0.        ]
  [ 30.          14.3877551    0.        ]
  [ 30.          13.7755102    0.        ]
  ...
  [ 30.         -13.7755102    0.        ]
  [ 30.         -14.3877551    0.        ]
  [ 30.         -15.           0.        ]]]
Qw(0.5, 0.5):
[[[  0.          15.         -30.        ]
  [  0.          14.10037484 -30.        ]
  [  0.          13.23823407 -30.        ]
  ...
  [  0.          13.23823407 -30.        ]
  [  0.          14.10037484 -30.        ]
  [  0.          15.         -30.        ]]

 [[  0.          14.3877551  -30.        ]
  [  0.          13.52484934 -30.        ]
  [  0.          12.69789798 -30.        ]
  ...
  [  0.          12.69789798 -30.        ]
  [  0.          13.52484934 -30.        ]
  [  0.          14.3877551  -30.        ]]

 [[  0.          13.7755102  -30.        ]
  [  0.          12.94932384 -30.        ]
  [  0.          12.1575619  -30.        ]
  ...
  [  0.          12.1575619  -30.        ]
  [  0.          12.94932384 -30.        ]
  [  0.          13.7755102  -30.        ]]

 ...

 [[  0.         -13.7755102  -30.        ]
  [  0.         -12.94932384 -30.        ]
  [  0.         -12.1575619  -30.        ]
  ...
  [  0.         -12.1575619  -30.        ]
  [  0.         -12.94932384 -30.        ]
  [  0.         -13.7755102  -30.        ]]

 [[  0.         -14.3877551  -30.        ]
  [  0.         -13.52484934 -30.        ]
  [  0.         -12.69789798 -30.        ]
  ...
  [  0.         -12.69789798 -30.        ]
  [  0.         -13.52484934 -30.        ]
  [  0.         -14.3877551  -30.        ]]

 [[  0.         -15.         -30.        ]
  [  0.         -14.10037484 -30.        ]
  [  0.         -13.23823407 -30.        ]
  ...
  [  0.         -13.23823407 -30.        ]
  [  0.         -14.10037484 -30.        ]
  [  0.         -15.         -30.        ]]]

以及下面的情节

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.