Skip to content

Commit 276dc28

Browse files
committed
update plot_coord
better defaults some more options an example slightly streamlined code
1 parent c3c43a4 commit 276dc28

File tree

2 files changed

+96
-55
lines changed

2 files changed

+96
-55
lines changed

plot_coord.m

+61-55
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,36 @@ function plot_coord(origin,varargin)
88
% ------------------------------------------------------------------------
99
% KEY VALUE DESCRIPTION
1010
% ------------------------------------------------------------------------
11+
% 'rotate' [AX AY AZ] 3x3 rotation matrix where AX,AY,AZ are the
12+
% vectors of each axis
13+
% OR
1114
% 'rotate' [u v w] Rotate coordinates 'u' degrees around the 1st axis
1215
% 'v' degrees around the 2nd axis
13-
% 'w' degrees around the 3rd axis
16+
% & 'w' degrees around the 3rd axis
1417
% 'order' [r1 r2 r3] Order of rotation, default [3 2 1] for [Z Y X]
15-
% 'axes' ['x' and/or 'y' and/or 'z']
16-
% Only plot axes listed
17-
% 'labels' [true/false]
18-
% Whether to print coordinate system labels
19-
% 'index' str String index on labels (default str='1')
2018
% ------------------------------------------------------------------------
21-
% 'length' L Length of axes lines
22-
% 'headlength' R Length of arrow head
23-
% 'arrowangle' a Angle of arrowhead "quills"
19+
% 'length' L Length of axes lines (default 0.05)
20+
% 'headlength' R Length of arrow head (default (0.02)
21+
% 'arrowangle' a Angle of arrowhead "quills" in degrees (default 25)
22+
% 'linewidth' w Linewidth of axes lines and arrow outline (default 0.4)
23+
% ------------------------------------------------------------------------
2424
% 'linecolour' [R G B] Red-Green-Blue colour of axis lines
2525
% 'headcolour' [R G B] Red-Green-Blue colour of arrowhead faces
2626
% 'headopacity' C Opacity of arrowhead faces
2727
% ------------------------------------------------------------------------
28+
% 'axes' ['x' and/or 'y' and/or 'z']
29+
% Only plot axes listed (default 'xyz')
30+
% 'labels' [true/false]
31+
% Whether to print coordinate system labels (default false)
32+
% 'labelframe' [true/false]
33+
% Whether to print a "frame" label (default false)
34+
% 'labelaxes' [true/false]
35+
% Whether to print coordinate system labels (default false)
36+
% 'labelshift' l Shift label away from end of axis/frame (default 0.01)
37+
% 'index' str String index on labels; omit if empty (default '')
38+
% ------------------------------------------------------------------------
39+
% 'fontsize' Font size for labels (default same as Matlab)
40+
% ------------------------------------------------------------------------
2841

2942
%% Parse inputs:
3043

@@ -35,12 +48,17 @@ function plot_coord(origin,varargin)
3548
p.addOptional('length',0.05);
3649
p.addOptional('headlength',0.02);
3750
p.addOptional('arrowangle',25);
38-
p.addOptional('index','1');
39-
p.addOptional('labels',true);
51+
p.addOptional('index','');
52+
p.addOptional('labels',false);
53+
p.addOptional('labelframe',false);
54+
p.addOptional('labelaxes',false);
4055
p.addOptional('axes','xyz');
4156
p.addParameter('linecolour',[0 0 0]);
57+
p.addParameter('linewidth',0.4);
4258
p.addParameter('headcolour',0.5*[1 1 1]);
4359
p.addParameter('headopacity',0.9);
60+
p.addParameter('labelshift',0.01);
61+
p.addParameter('fontsize',get(0,'defaulttextfontsize'));
4462
p.parse(origin,varargin{:})
4563

4664
O = p.Results.origin;
@@ -49,16 +67,29 @@ function plot_coord(origin,varargin)
4967
ang = p.Results.arrowangle;
5068

5169
r = p.Results.rotate;
52-
ro = p.Results.order;
53-
ni = p.Results.index;
70+
ro = p.Results.order;
71+
plot_axes = p.Results.axes;
72+
5473
ecol = p.Results.linecolour;
74+
lw = p.Results.linewidth;
75+
5576
col = p.Results.headcolour;
5677
opac = p.Results.headopacity;
78+
5779
labels_bool = p.Results.labels;
58-
plot_axes = p.Results.axes;
80+
labelframe_bool = p.Results.labelframe;
81+
labelaxes_bool = p.Results.labelaxes;
82+
labelshift = p.Results.labelshift;
83+
84+
fontsize = p.Results.fontsize;
5985

60-
% Constant that should actually be optional input
61-
nameshift = 0.01;
86+
%%
87+
88+
if isempty(p.Results.index)
89+
index_label = '';
90+
else
91+
index_label = ['_{',p.Results.index,'}'];
92+
end
6293

6394
%% Definition of a single axis (X)
6495
%
@@ -74,7 +105,6 @@ function plot_coord(origin,varargin)
74105
if numel(r) == 3
75106
c = cosd(r);
76107
s = sind(r);
77-
78108
RxRyRz(:,:,1) = [1 0 0; 0 c(1) -s(1); 0 s(1) c(1)];
79109
RxRyRz(:,:,2) = [c(2) 0 s(2); 0 1 0; -s(2) 0 c(2)];
80110
RxRyRz(:,:,3) = [c(3) -s(3) 0; s(3) c(3) 0; 0 0 1];
@@ -91,61 +121,37 @@ function plot_coord(origin,varargin)
91121
hold on
92122

93123
if strcmp(plot_axes,'xyz')
94-
if labels_bool
95-
text(-nameshift+O(1),-nameshift+O(2),O(3),['O_{',ni,'}']);
124+
if labels_bool || labelframe_bool
125+
text(-labelshift+O(1),-labelshift+O(2),-labelshift+O(3),['O',index_label],'fontsize',fontsize);
96126
end
97127
end
98128

129+
RX = R;
130+
RY = R*[0 -1 0;1 0 0;0 0 1]; % Rz(+90);
131+
RZ = R*[0 0 -1;0 1 0;1 0 0]; % Ry(-90);
132+
99133
for s = plot_axes
100134
switch s
101135
case 'x'
102-
plot_one_coord(O,R*ax, R*head1, R*head2, ['x_{',ni,'}'],[2*nameshift; 0; 0])
136+
plot_one_coord(O,RX*ax,RX*head1,RX*head2,['x',index_label],[labelshift; 0; 0])
103137
case 'y'
104-
plot_one_coord(O,R*Rz(+90)*ax,R*Rz(+90)*head1,R*Rz(+90)*head2,['y_{',ni,'}'],[0; nameshift; 0])
138+
plot_one_coord(O,RY*ax,RY*head1,RY*head2,['y',index_label],[0; labelshift; 0])
105139
case 'z'
106-
plot_one_coord(O,R*Ry(-90)*ax,R*Ry(-90)*head1,R*Ry(-90)*head2,['z_{',ni,'}'],[0; 0; nameshift])
140+
plot_one_coord(O,RZ*ax,RZ*head1,RZ*head2,['z',index_label],[0; 0; labelshift])
107141
end
108142
end
109143

110144
%% Nested functions
111145

112146
function plot_one_coord(O,a,head1,head2,name,ns)
113-
if labels_bool
114-
text(ns(1)+O(1)+a(1),ns(2)+O(2)+a(2),ns(3)+O(3)+a(3),name);
147+
if labels_bool || labelaxes_bool
148+
text(ns(1)+O(1)+a(1),ns(2)+O(2)+a(2),ns(3)+O(3)+a(3),name,'fontsize',fontsize);
115149
end
116150

117-
plot3(O(1)+[0 a(1)],O(2)+[0 a(2)],O(3)+[0 a(3)],'color',ecol);
118-
patch(O(1)+head1(1,:),O(2)+head1(2,:),O(3)+head1(3,:),ecol,'facealpha',opac,'facecolor',col);
119-
patch(O(1)+head2(1,:),O(2)+head2(2,:),O(3)+head2(3,:),ecol,'facealpha',opac,'facecolor',col);
151+
plot3(O(1)+[0 a(1)],O(2)+[0 a(2)],O(3)+[0 a(3)],'color',ecol,'linewidth',lw);
152+
patch(O(1)+head1(1,:),O(2)+head1(2,:),O(3)+head1(3,:),ecol,'facealpha',opac,'facecolor',col,'linewidth',lw);
153+
patch(O(1)+head2(1,:),O(2)+head2(2,:),O(3)+head2(3,:),ecol,'facealpha',opac,'facecolor',col,'linewidth',lw);
120154
end
121155

122156
end
123157

124-
%% Rotation matrices
125-
%
126-
% These could all be anonymous functions if we wanted.
127-
128-
function R = Rz(t)
129-
R = [cosd(t) -sind(t) 0;
130-
sind(t) cosd(t) 0;
131-
0 0 1];
132-
end
133-
134-
function R = Ry(t)
135-
R = [ cosd(t) 0 sind(t);
136-
0 1 0;
137-
-sind(t) 0 cosd(t)];
138-
end
139-
140-
function R = Rx(t)
141-
R = [1 0 0 ;
142-
0 cosd(t) -sind(t);
143-
0 sind(t) cosd(t)];
144-
end
145-
146-
% assert( all( Rx(90)*[0;1;0]==[ 0; 0; 1]) )
147-
% assert( all( Rz(90)*[0;1;0]==[-1; 0; 0]) )
148-
% assert( all( Ry(90)*[1;0;0]==[ 0; 0;-1]) )
149-
% assert( all( Rz(90)*[1;0;0]==[ 0; 1; 0]) )
150-
% assert( all( Rx(90)*[0;0;1]==[ 0;-1; 0]) )
151-
% assert( all( Ry(90)*[0;0;1]==[ 1; 0; 0]) )

plot_coord_example.m

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
%% plot_coord example
2+
3+
figure(1); clf; hold on
4+
5+
plot_coord([0;0;0])
6+
7+
axis equal
8+
view(3)
9+
10+
%% Using Euler angles
11+
12+
plot_coord(0.1*[-1;1;1],'rotate',[10 20 30],...
13+
'headcolour','green','labelaxes',true)
14+
15+
%% Using rotation matrix
16+
17+
AX = [ 1;1;0];
18+
AY = [-1;1;0];
19+
AZ = [ 0;0;1];
20+
21+
plot_coord(0.1*[1;1;1],'rotate',[AX,AY,AZ],...
22+
'headlength',0.03,...
23+
'headcolour','red',...
24+
'arrowangle',45,...
25+
'linewidth',2)
26+
27+
28+
%% Labels
29+
30+
AX = [ 1;1;0];
31+
AY = [-1;1;0];
32+
AZ = [ 0;0;1];
33+
34+
plot_coord(-0.1*[1;1;1],'rotate',[AX,AY,AZ],...
35+
'headcolour','blue','labels',true,'index','B')

0 commit comments

Comments
 (0)