]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/drawEdge.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / drawEdge.m
1 %% Copyright (c) 2011, INRA
2 %% 2004-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%     
14 %% 2. Redistributions in binary form must reproduce the above copyright notice, 
15 %%    this list of conditions and the following disclaimer in the documentation
16 %%    and/or other materials provided with the distribution.
17 %%
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
29 %%
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
33
34 %% -*- texinfo -*-
35 %% @deftypefn {Function File} {@var{h} = } drawEdge (@var{x1}, @var{y1}, @var{x2}, @var{y2})
36 %% @deftypefnx {Function File} {@var{h} = } drawEdge ([@var{x1} @var{y1} @var{x2} @var{y2}])
37 %% @deftypefnx {Function File} {@var{h} = } drawEdge ([@var{x1} @var{y1}], [@var{x2} @var{y2}])
38 %% @deftypefnx {Function File} {@var{h} = } drawEdge (@var{x1}, @var{y1}, @var{z1}, @var{x2}, @var{y2}, @var{z2})
39 %% @deftypefnx {Function File} {@var{h} = } drawEdge ([@var{x1} @var{y1} @var{z1} @var{x2} @var{y2} @var{z2}])
40 %% @deftypefnx {Function File} {@var{h} = } drawEdge ([@var{x1} @var{y1} @var{z1}], [@var{x2} @var{y2} @var{z2}])
41 %% @deftypefnx {Function File} {@var{h} = } drawEdge (@dots{}, @var{opt})
42 %% Draw an edge given by 2 points.
43 %%
44 %%   Draw an edge between the points (x1 y1) and  (x2 y2). Data can be bundled as an edge.
45 %%   The function supports 3D edges.
46 %%   Arguments can be single values or array of size [Nx1]. In this case,
47 %%   the function draws multiple edges.
48 %%   @var{opt}, being a set of pairwise options, can
49 %%   specify color, line width and so on. These are passed to function @code{line}.
50 %%   The function returns handle(s) to created edges(s).
51 %%
52 %%   @seealso{edges2d, drawCenteredEdge, drawLine, line}
53 %% @end deftypefn
54
55 function varargout = drawEdge(varargin)
56
57   % separate edge and optional arguments
58   [edge options] = parseInputArguments(varargin{:});
59
60   % draw the edges
61   if size(edge, 2)==4
62       h = drawEdge_2d(edge, options);
63   else
64       h = drawEdge_3d(edge, options);
65   end
66
67   % eventually return handle to created edges
68   if nargout>0
69       varargout{1}=h;
70   end
71
72 endfunction
73
74 function h = drawEdge_2d(edge, options)
75
76   h = -1*ones(size(edge, 1), 1);
77
78   for i=1:size(edge, 1)
79       if isnan(edge(i,1))
80           continue;
81       end
82       h(i) = line(...
83           [edge(i, 1) edge(i, 3)], ...
84           [edge(i, 2) edge(i, 4)], options{:});
85   end
86
87 endfunction
88
89 function h = drawEdge_3d(edge, options)
90
91   h = -1*ones(size(edge, 1), 1);
92
93   for i=1:size(edge, 1)
94       if isnan(edge(i,1))
95           continue;
96       end
97       h(i) = line( ...
98           [edge(i, 1) edge(i, 4)], ...
99           [edge(i, 2) edge(i, 5)], ...
100           [edge(i, 3) edge(i, 6)], options{:});
101   end
102
103 endfunction
104     
105 function [edge options] = parseInputArguments(varargin)
106
107   % default values for parameters
108   edge = [];
109
110   % find the number of arguments defining edges
111   nbVal=0;
112   for i=1:nargin
113       if isnumeric(varargin{i})
114           nbVal = nbVal+1;
115       else
116           % stop at the first non-numeric value
117           break;
118       end
119   end
120
121   % extract drawing options
122   options = varargin(nbVal+1:end);
123
124   % ensure drawing options have correct format
125   if length(options)==1
126       options = [{'color'}, options];
127   end
128
129   % extract edges characteristics
130   if nbVal==1
131       % all parameters in a single array
132       edge = varargin{1};
133
134   elseif nbVal==2
135       % parameters are two points, or two arrays of points, of size N*2.
136       p1 = varargin{1};
137       p2 = varargin{2};
138       edge = [p1 p2];
139       
140   elseif nbVal==4
141       % parameters are 4 parameters of the edge : x1 y1 x2 and y2
142       edge = [varargin{1} varargin{2} varargin{3} varargin{4}];
143       
144   elseif nbVal==6
145       % parameters are 6 parameters of the edge : x1 y1 z1 x2 y2 and z2
146       edge = [varargin{1} varargin{2} varargin{3} varargin{4} varargin{5} varargin{6}];
147   end
148
149 endfunction
150
151 %!demo
152 %!  close
153 %!  points = rand(4,4);
154 %!  colorstr = 'rgbm';
155 %!  for i=1:4
156 %!    drawEdge (points(i,:),'color',colorstr(i),'linewidth',2);
157 %!  end
158 %!  axis tight;
159
160 %!demo
161 %!  close
162 %!  drawEdge (rand(10,4),'linewidth',2);
163 %!  axis tight;
164