MATLAB中Surf图的面积计算

Area Calculation of Surf plot in MATLAB

我有不规则的 3D 笛卡尔坐标,它占球体表面的八分之一。感谢 Benoit_11 现在可以在普通命令行脚本中在 MATLAB cftool 之外绘制曲面

从那时起,我一直在尝试使用从该区域内的其他答案拼凑而成的以下代码来计算曲面的面积。该代码主要计算产生表面的顶点的面积,然后将它们全部相加以产生一个面积。

surface = [ansx1,ansy1,ansz1];
[m,n] = size(zdata1);
area = 0;
for i = 1:m-1
      for j = 1:n-1
          v0_1 = [xdata1(i,j)     ydata1(i,j)     zdata1(i,j)    ];
          v1_1 = [xdata1(i,j+1)   ydata1(i,j+1)   zdata1(i,j+1)  ];
          v2_1 = [xdata1(i+1,j)   ydata1(i+1,j)   zdata1(i+1,j)  ];
          v3_1 = [xdata1(i+1,j+1) ydata1(i+1,j+1) zdata1(i+1,j+1)];
          a_1= v1_1 - v0_1;
          b_1 = v2_1 - v0_1;
          c_1 = v3_1 - v0_1;
          A_1 = 1/2*(norm(cross(a_1, c_1)) + norm(cross(b_1, c_1)));
          area = area + A_1;
      end
end
fprintf('\nTotal area is: %f\n\n', area);`

但是我遇到的问题是计算出的表面高估了可能的表面。这是由于从原始矩阵中删除了 NaN 并将其替换为 0,这导致了图 1。图 2 提供了我想要计算的唯一区域

有没有人有办法忽略提供的代码中的零以计算生成图 1 的数据的表面积?

提前致谢

我认为你只需要检查字段的四个点之一是否为零。

这个怎么样:

% example surface
[X,Y,Z] = peaks(30);

% manipulate it
[lza, lzb] = size(Z);
for nza = 1:lza
   for nzb = 1:lzb
      if Z(nza,nzb) < 0
         Z(nza,nzb) = Z(nza,nzb)-1;
      else
         Z(nza,nzb) = 0;
      end
   end
end

surfc(X,Y,Z)

% start calculating the surface area
A = 0;
lX = length(X);
lY = length(Y);

for nx = 1:lX-1
   for ny = 1:lY-1

      eX = [X(ny,nx)   X(ny,nx+1)
         X(ny+1,nx) X(ny+1,nx+1)];
      eY = [Y(ny,nx)   Y(ny,nx+1)
         Y(ny+1,nx) Y(ny+1,nx+1)];
      eZ = [Z(ny,nx)   Z(ny,nx+1)
         Z(ny+1,nx) Z(ny+1,nx+1)];

      % check the field
      if eZ(1,1)==0 || eZ(1,2)==0 || eZ(2,1)==0 || eZ(2,2)==0
         continue
      end

      % take two triangles, calculate the cross product to get the surface area
      % and sum them.
      v1 = [eX(1,1) eY(1,1) eZ(1,1)];
      v2 = [eX(1,2) eY(1,2) eZ(1,2)];
      v3 = [eX(2,1) eY(2,1) eZ(2,1)];
      v4 = [eX(2,2) eY(2,2) eZ(2,2)];
      A  = A + norm(cross(v2-v1,v3-v1))/2;
      A  = A + norm(cross(v2-v4,v3-v4))/2;

   end
end