Set vector direction to point away from a closed surface (or a point coordinate)?

8 次查看(过去 30 天)
Dear all,
I am plotting a closed potato-shaped surface using "patch", then adding some tangent planes using
plane=surf(x,y,z)
But when I am plotting the normals of those planes, using
normals=plane.FaceNormals
some of them are pointing "inwards" towards the potato, while others point outward away from it. How can I make them all point away from the surface (or the center) of the potato?
Thank you kindly!

回答(1 个)

David Goodmanson
David Goodmanson 2017-12-19
Hi LMS,
Say you set up a point B in the middle of the volume. Assuming the surface is not excessively curved, from B you should be able to reach every point on the skin with a straight line without leaving the potato. Suppose dface is the displacement vector from B to the middle of a face. The the dot product of dface with an outward face normal should be positive. The code below checks for that and and changes the sign of the face normal accordingly.
I'm assuming you have m x n matrices of the x,y and z coordinates. If you fed x and y vectors to surf, then you will have to use meshgrid to get the appropriate x and y matrices.
nvecs is a (m-1) x (n-1) x 3 matrix of the FaceNormal vectors, which you got in the way you described.
v = cat(3,x,y,z); % construct vertices of faces
B = mean(mean(v)); % center, approximately
v = v - B; % translate to put center point at the origin
% find centers of faces
vcf = (1/4)*(v(1:end-1,1:end-1,:)+v(1:end-1,2:end,:) ...
+ v( 2:end,1:end-1,:)+v( 2:end,2:end,:));
sdot = sign(dot(vcf,nvecs,3)) % want these all to be positive
nvecs_new = nvecs.*repmat(sdot,1,1,3);
sdot = sign(dot(vcf,nvecs_new,3)) % check, all positive
I think the third line will have to be done with repmat if you don't have a fairly new version of Matlab.
You can eyeball a value for B, but it would have to be entered as B(1,1,:) = [bx by bz]

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by