MATLAB Answers

display surface normal using quiver3

24 views (last 30 days)
MK
MK on 5 Mar 2017
Commented: MK on 12 Mar 2017
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

Sign in to comment.

Accepted Answer

Rahul Kalampattel
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

  4 Comments

Show 1 older comment
Rahul Kalampattel
Rahul Kalampattel on 7 Mar 2017
U,V and W do specify the lengths of each vector, but by default the result is autoscaled.
From the documentation, "The quiver3 function automatically scales the vectors to prevent overlapping based on the distance between them".
You can turn this off by setting the scale parameter to 0, but it might be a bit messy.
Rahul Kalampattel
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')
MK
MK on 12 Mar 2017
Yes I saw the documentation as well. But since my vectors had a range of 0-1, I thought they would not have been upscaled, so I did not notice the magnification.
Regarding the colour, I was thinking more that the corresponding (u,v,w) vectors would give the (r,g,b) colours, since the range of (u,v,w) was 0-1. For now though, I have actually done away with the colour display since the vectors (after downscaling) are really small, and direction less clear. I'm actually now just working with the values instead.
By the way, thanks for the help!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by