display surface normal using quiver3
9 views (last 30 days)
Show older comments
I have an MxN matrix Z after some processing, and wanted to retrieve the surface normals (as well as view it on a plot)
I've tried surfnorm(Z), but since it had looked pretty messy I thought of simply using quiver3, with the surface normals from the return value of surfnorm. Since the documentation states that quiver3(x,y,z,u,v,n) usage is such that the (u, v, n) vectors would be at location(x,y,z), I tried the following:
% I checked that surf(Z) and surf(X, Y, Z) gave the same plot
X = zeros(M, N);
Y = zeros(M, N);
for k = 1:M
X(k, :) = k;
end
for k = 1:N
Y(:, k) = k;
end
[U, V, W] = surfnorm(Z);
quiver3(X, Y, Z, U, V, W);
but I simply got a "cylindrical" vector plot (seemingly as if the origin of the vectors are the same (center of cylinder)). I tried with different Z matrices but the quiver3 plot was always similar looking.
Am I understanding any of the functions wrong? Would really appreciate some help.
surf result:
surfnorm result:
quiver3 result:
0 Comments
Accepted Answer
Rahul Kalampattel
on 6 Mar 2017
Edited: Rahul Kalampattel
on 6 Mar 2017
1. Use
[U,V,W] = surfnorm(X,Y,Z);
instead of
[U,V,W] = surfnorm(Z);
This will make sure that the quiver3 vectors are mapped to the right coordinates.
2. If you look at the last plot, the X/Y axes scales are 14 orders of magnitude larger than in your surf and surfnorm plots. The vectors from quiver3 are too long, which gives the illusion of a cylindrical plot. This might be fixed after step 1, but if not you can rescale them using
quiver3(X,Y,Z,U,V,W,0.5); % Half scale
3. If the two plots still aren't matching, this answer might help: https://au.mathworks.com/matlabcentral/answers/100252-why-do-the-surface-norms-calculated-by-surfnorm-and-quiver3-not-coincide-in-matlab-6-5-r13?s_tid=answers_rc1-1_p1_MLT
4 Comments
Rahul Kalampattel
on 7 Mar 2017
Also I'm not sure exactly what you want as far as having different coloured vectors in different directions goes, but this is one way to do it.
hold on
% Vectors with positive U are blue, the rest are red
quiver3(X,Y,Z,(U>=0).*U,(U>=0).*V,(U>=0).*W)
quiver3(X,Y,Z,(U<0).*U,(U<0).*V,(U<0).*W, 'color', 'r')
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!