File: drawEllipse3d.m

package info (click to toggle)
octave-matgeom 1.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,584 kB
  • sloc: objc: 469; makefile: 10
file content (256 lines) | stat: -rw-r--r-- 7,973 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
## Copyright (C) 2024 David Legland
## All rights reserved.
## 
## Redistribution and use in source and binary forms, with or without
## modification, are permitted provided that the following conditions are met:
## 
##     1 Redistributions of source code must retain the above copyright notice,
##       this list of conditions and the following disclaimer.
##     2 Redistributions in binary form must reproduce the above copyright
##       notice, this list of conditions and the following disclaimer in the
##       documentation and/or other materials provided with the distribution.
## 
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
## 
## The views and conclusions contained in the software and documentation are
## those of the authors and should not be interpreted as representing official
## policies, either expressed or implied, of the copyright holders.

function varargout = drawEllipse3d(varargin)
%DRAWELLIPSE3D Draw a 3D ellipse.
%
%   Possible calls for the function :
%   drawEllipse3d([XC YC ZC A B THETA PHI])
%   drawEllipse3d([XC YC ZC A B THETA PHI PSI])
%   drawEllipse3d([XC YC ZC A B], [THETA PHI])
%   drawEllipse3d([XC YC ZC A B], [THETA PHI PSI])
%   drawEllipse3d([XC YC ZC A B], THETA, PHI)
%   drawEllipse3d([XC YC ZC], A, B, THETA, PHI)
%   drawEllipse3d([XC YC ZC A B], THETA, PHI, PSI)
%   drawEllipse3d([XC YC ZC], A, B, THETA, PHI, PSI)
%   drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI)
%   drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI, PSI)
%
%   where XC, YC, ZY are coordinate of ellipse center, A and B are the
%   half-lengths of the major and minor axes of the ellipse,
%   PHI and THETA are 3D angle (in degrees) of the normal to the plane
%   containing the ellipse (PHI between 0 and 360 corresponding to
%   longitude, and THETA from 0 to 180, corresponding to angle with
%   vertical).
%   
%   H = drawEllipse3d(...)
%   return handle on the created LINE object
%   
%   Example
%     figure; axis([-10 10 -10 10 -10 10]); hold on;
%     ellXY = [0 0 0  8 5  0 0 0];
%     drawEllipse3d(ellXY, 'color', [.8 0 0], 'linewidth', 2)
%     ellXZ = [0 0 0  8 2  90 90 90];
%     drawEllipse3d(ellXZ, 'color', [0 .8 0], 'linewidth', 2)
%     ellYZ = [0 0 0  5 2  90 0 90];
%     drawEllipse3d(ellYZ, 'color', [0 0 .8], 'linewidth', 2)
% 

% ------
% Author: David Legland
% E-mail: david.legland@inrae.fr
% Created: 2008-05-07
% Copyright 2008-2023 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas)

%   Possible calls for the function, with number of arguments :
%   drawEllipse3d([XC YC ZC A B THETA PHI])             1
%   drawEllipse3d([XC YC ZC A B THETA PHI PSI])         1
%   drawEllipse3d([XC YC ZC A B], [THETA PHI])          2
%   drawEllipse3d([XC YC ZC A B], [THETA PHI PSI])      2
%   drawEllipse3d([XC YC ZC A B], THETA, PHI)           3
%   drawEllipse3d([XC YC ZC A B], THETA, PHI, PSI)      4
%   drawEllipse3d([XC YC ZC], A, B, THETA, PHI)         5
%   drawEllipse3d([XC YC ZC], A, B, THETA, PHI, PSI)    6
%   drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI)         7
%   drawEllipse3d(XC, YC, ZC, A, B, THETA, PHI, PSI)    8

% extract handle of axis to draw on
[hAx, varargin] = parseAxisHandle(varargin{:});

% extract drawing options
ind = find(cellfun(@ischar, varargin), 1, 'first');
options = {};
if ~isempty(ind)
    options = varargin(ind:end);
    varargin(ind:end) = [];
end

if length(varargin)==1
    % get center and radius
    elli3d = varargin{1};
    xc = elli3d(:,1);
    yc = elli3d(:,2);
    zc = elli3d(:,3);
    a  = elli3d(:,4);
    b  = elli3d(:,5);
    
    % get colatitude of normal
    if size(elli3d, 2)>=6
        theta = elli3d(:,6);
    else
        theta = zeros(size(elli3d, 1), 1);
    end

    % get azimut of normal
    if size(elli3d, 2)>=7
        phi     = elli3d(:,7);
    else
        phi = zeros(size(elli3d, 1), 1);
    end
    
    % get roll
    if size(elli3d, 2)==8
        psi = elli3d(:,8);
    else
        psi = zeros(size(elli3d, 1), 1);
    end
    
elseif length(varargin)==2
    % get center and radius
    elli3d = varargin{1};
    xc = elli3d(:,1);
    yc = elli3d(:,2);
    zc = elli3d(:,3);
    a  = elli3d(:,4);
    b  = elli3d(:,5);
    
    % get angle of normal
    angle = varargin{2};
    theta   = angle(:,1);
    phi     = angle(:,2);
    
    % get roll
    if size(angle, 2)==3
        psi = angle(:,3);
    else
        psi = zeros(size(angle, 1), 1);
    end

elseif length(varargin)==3    
    % get center and radius
    elli3d = varargin{1};
    xc = elli3d(:,1);
    yc = elli3d(:,2);
    zc = elli3d(:,3);
    a  = elli3d(:,4);
    b  = elli3d(:,5);
    
    % get angle of normal and roll
    theta   = varargin{2};
    phi     = varargin{3};
    psi     = zeros(size(phi, 1), 1);
    
elseif length(varargin)==4
    % get center and radius
    elli3d = varargin{1};
    xc = elli3d(:,1);
    yc = elli3d(:,2);
    zc = elli3d(:,3);
    
    if size(elli3d, 2)==5
        a  = elli3d(:,4);
        b  = elli3d(:,5);
    end
    
    theta   = varargin{2};
    phi     = varargin{3};
    psi     = varargin{4};
    
elseif length(varargin)==5
    % get center and radius
    elli3d = varargin{1};
    xc      = elli3d(:,1);
    yc      = elli3d(:,2);
    zc      = elli3d(:,3);
    a       = varargin{2};
    b       = varargin{3};
    theta   = varargin{4};
    phi     = varargin{5};
    psi     = zeros(size(phi, 1), 1);

elseif length(varargin)==6
    elli3d = varargin{1};
    xc      = elli3d(:,1);
    yc      = elli3d(:,2);
    zc      = elli3d(:,3);
    a       = varargin{2};
    b       = varargin{3};
    theta   = varargin{4};
    phi     = varargin{5};
    psi     = varargin{6};
  
elseif length(varargin)==7   
    xc      = varargin{1};
    yc      = varargin{2};
    zc      = varargin{3};
    a       = varargin{4};
    b       = varargin{5};
    theta   = varargin{6};
    phi     = varargin{7};
    psi     = zeros(size(phi, 1), 1);
    
elseif length(varargin)==8   
    xc      = varargin{1};
    yc      = varargin{2};
    zc      = varargin{3};
    a       = varargin{4};
    b       = varargin{5};
    theta   = varargin{6};
    phi     = varargin{7};
    psi     = varargin{8};

else
    error('drawEllipse3d: please specify center and radius');
end

% uses 60 intervals
t = linspace(0, 2*pi, 61)';

nElli = size(xc, 1);

% save hold state
holdState = ishold(hAx);
hold(hAx, 'on');

% iterate over ellipses to draw
for i = 1:nElli
    % polyline approximation of ellipse, centered and parallel to main axes
    xt = a(i) * cos(t);
    yt = b(i) * sin(t);
    zt = zeros(length(t), 1);
    elli2d = [xt yt zt];
    
    % compute transformation from local basis to world basis
    trans = localToGlobal3d(xc(i), yc(i), zc(i), theta(i), phi(i), psi(i));
    
    % transform points composing the ellipse
    elli3d = transformPoint3d(elli2d, trans);
    
    % draw the curve
    h = drawPolyline3d(hAx, elli3d, options{:});
end

% restore hold state
if ~holdState
    hold(hAx, 'off');
end

% format output arguments
if nargout > 0
    varargout = {h};
end