MATLAB中冲浪图的面积计算

时间:2015-08-12 17:49:54

标签: matlab plot area surface

我有不规则的3D笛卡尔坐标,它构成了球体表面的八分之一。感谢Benoit_11 Answer to a previously posed question,现在可以在正常的命令行脚本中将表面绘制在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提供了我想要计算的唯一区域

Surface plot when the NaN have been removed

enter image description here

是否有人可以忽略提供的代码中的零来计算生成图1的数据的表面区域?

提前致谢

1 个答案:

答案 0 :(得分:2)

我认为你只需要检查一个场的四个点中的一个是否等于零。

这个怎么样:

% 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