how to plot an arrow given yaw/pitch/roll

57 views (last 30 days)
What's the easiest way to plot a simple arrow of arbitrary dimensions given a starting position (x,y,z) and euler angles (roll,pitch,yaw)?

Accepted Answer

Mike Garrity
Mike Garrity on 5 Aug 2015
Consider the following simple example:
pt = [0 0 0];
dir = [1 0 0 1];
h = quiver3(pt(1),pt(2),pt(3), dir(1),dir(2),dir(3));
xlim([-1 1])
ylim([-1 1])
zlim([-1 1])
If we look at h, we'll see that it has properties named UData, VData, and WData.
h =
Quiver with properties:
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
XData: 0
YData: 0
ZData: 0
UData: 1
VData: 0
WData: 0
This are the components of the vector. To change the direction, we want to transform those. The makehgtform command will give us a transform matrix for a set of rotations. We can use that to calculate new values for the UData, VData, and WData properties.
We can use it like this:
xfm = makehgtform('xrotate',pi/3,'yrotate',pi/5,'zrotate',pi/2);
newdir = xfm * dir';
h.UData = newdir(1);
h.VData = newdir(2);
h.WData = newdir(3);
You may have noticed something odd here. The vector dir is a 1x4 vector even though we've only got 3 directions. The reason is that makehgtform returns a 4x4 matrix. It does that so that it can support translations. You don't care about translations in this case, so we can ignore that.
To animate this, we'd do it in a loop and add a drawnow:
for theta = linspace(0,pi,64)
for phi = linspace(-pi,pi,64)
for psi = linspace(0,2*pi,64)
xfm = makehgtform('xrotate',theta,'yrotate',phi,'zrotate',psi);
newdir = xfm * dir';
h.UData = newdir(1);
h.VData = newdir(2);
h.WData = newdir(3);
drawnow
end
end
end
You may need to fiddle with the order in which you give the rotations to makehgtform. You'll see various orderings referred to as "Euler angles". The help for makehgtform will show you some other things you can do with it, such as the axisrotate option.
  1 Comment
Art
Art on 12 Aug 2015
Mike, I have a follow up question you may be able to answer:
If I were plotting this arrow on Matlab's Globe model, I would need to find the local U/V/Wdata at the lat/long/alt of the arrow starting point (I think?). Then I would use this as the arrow's starting "dir" in your above example, prior to doing the desired theta/phi/psi rotations.
In other words, the X/Y/Z positions I have are in ECEF coord, and I'd need to first rotate the initial arrow vector to point due north in the local NED frame, then rotate again per your example.
I can convert the X/Y/Z to Lat/Long/Alt, but how can find initial north pointing U/V/Wdata at any point on the earth given lat/long/alt?

Sign in to comment.

More Answers (1)

Art
Art on 6 Aug 2015
Thanks everyone for the suggestions! And thanks Mike Garrity for the clear, concise and exampled description, always a plus for me. After playing around with all suggestions, yours was exactly what I needed.
  2 Comments
Joel Sande
Joel Sande on 12 Apr 2016
Hi, How to do this from 1 point pointing in 3 different directions ?
zhaozhong chen
zhaozhong chen on 25 May 2018
a late answer. Just type
h = quiver3(pt(1),pt(2),pt(3), dir(1),dir(2),dir(3));
%from the first answer and then
%then create two dir, dir 2 and dir 3
hold on
h2 = quiver3(pt(1),pt(2),pt(3), dir2(1),dir2(2),dir2(3))
h3 = quiver3(pt(1),pt(2),pt(3), dir3(1),dir3(2),dir3(3))
hold off %so you'll have 3 directions from the same points
xlim([-1 1])
ylim([-1 1])
zlim([-1 1])

Sign in to comment.

Categories

Find more on Discrete Data Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!