gpt4 book ai didi

python - Python 中的曲面曲率 Matlab 等价物

转载 作者:太空狗 更新时间:2023-10-30 01:43:00 25 4
gpt4 key购买 nike

我试图计算由点数组 (x,y,z) 给出的曲面的曲率。最初我试图拟合多项式方程 z=a + bx + cx^2 + dy + exy + fy^2)然后计算高斯曲率

$ K = \frac{F_{xx}\cdot F_{yy}-{F_{xy}}^2}{(1+{F_x}^2+{F_y}^2)^2} $

然而,如果表面很复杂,问题就来了。我找到了这个 Matlab 代码来数值计算曲率。我想知道如何在 Python 中做同样的事情。

function [K,H,Pmax,Pmin] = surfature(X,Y,Z),
% SURFATURE - COMPUTE GAUSSIAN AND MEAN CURVATURES OF A SURFACE
% [K,H] = SURFATURE(X,Y,Z), WHERE X,Y,Z ARE 2D ARRAYS OF POINTS ON THE
% SURFACE. K AND H ARE THE GAUSSIAN AND MEAN CURVATURES, RESPECTIVELY.
% SURFATURE RETURNS 2 ADDITIONAL ARGUEMENTS,
% [K,H,Pmax,Pmin] = SURFATURE(...), WHERE Pmax AND Pmin ARE THE MINIMUM
% AND MAXIMUM CURVATURES AT EACH POINT, RESPECTIVELY.


% First Derivatives
[Xu,Xv] = gradient(X);
[Yu,Yv] = gradient(Y);
[Zu,Zv] = gradient(Z);

% Second Derivatives
[Xuu,Xuv] = gradient(Xu);
[Yuu,Yuv] = gradient(Yu);
[Zuu,Zuv] = gradient(Zu);

[Xuv,Xvv] = gradient(Xv);
[Yuv,Yvv] = gradient(Yv);
[Zuv,Zvv] = gradient(Zv);

% Reshape 2D Arrays into Vectors
Xu = Xu(:); Yu = Yu(:); Zu = Zu(:);
Xv = Xv(:); Yv = Yv(:); Zv = Zv(:);
Xuu = Xuu(:); Yuu = Yuu(:); Zuu = Zuu(:);
Xuv = Xuv(:); Yuv = Yuv(:); Zuv = Zuv(:);
Xvv = Xvv(:); Yvv = Yvv(:); Zvv = Zvv(:);

Xu = [Xu Yu Zu];
Xv = [Xv Yv Zv];
Xuu = [Xuu Yuu Zuu];
Xuv = [Xuv Yuv Zuv];
Xvv = [Xvv Yvv Zvv];

% First fundamental Coeffecients of the surface (E,F,G)
E = dot(Xu,Xu,2);
F = dot(Xu,Xv,2);
G = dot(Xv,Xv,2);

m = cross(Xu,Xv,2);
p = sqrt(dot(m,m,2));
n = m./[p p p];

% Second fundamental Coeffecients of the surface (L,M,N)
L = dot(Xuu,n,2);
M = dot(Xuv,n,2);
N = dot(Xvv,n,2);

[s,t] = size(Z);

% Gaussian Curvature
K = (L.*N - M.^2)./(E.*G - F.^2);
K = reshape(K,s,t);

% Mean Curvature
H = (E.*N + G.*L - 2.*F.*M)./(2*(E.*G - F.^2));
H = reshape(H,s,t);

% Principal Curvatures
Pmax = H + sqrt(H.^2 - K);
Pmin = H - sqrt(H.^2 - K);

最佳答案

我希望我来这里还不算太晚。我遇到了完全相同的问题(我工作的公司的产品)。

您必须考虑的第一件事是点必须代表矩形网格。 X是二维数组,Y是二维数组,Z是二维数组。如果您有一个非结构化浊点,具有单个矩阵形状 Nx3(第一列为 X,第二列为 Y,第三列为 Z),则您无法应用此 matlab 函数。

我开发了一个与此 Matlab 脚本等效的 Python,其中我只计算 Z 矩阵的平均曲率(我想您可以从该脚本中获得灵感并调整它以获得所有您想要的曲率),忽略 X 和 Y假设网格是正方形的。我认为您可以“掌握”我在做什么以及如何做,并根据您的需要进行调整:

注意:这假设您的数据点相隔 1 个单位。

def mean_curvature(Z):
Zy, Zx = numpy.gradient(Z)
Zxy, Zxx = numpy.gradient(Zx)
Zyy, _ = numpy.gradient(Zy)

H = (Zx**2 + 1)*Zyy - 2*Zx*Zy*Zxy + (Zy**2 + 1)*Zxx
H = -H/(2*(Zx**2 + Zy**2 + 1)**(1.5))

return H

关于python - Python 中的曲面曲率 Matlab 等价物,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11317579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com