]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/createEdge.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / createEdge.m
1 %% Copyright (c) 2011, INRA
2 %% 2003-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{edge} = } createEdge (@var{p1}, @var{p2})
36 %% @deftypefnx {Function File} {@var{edge} = } createEdge (@var{x0}, @var{y0}, @var{dx}, @var{dy})
37 %% @deftypefnx {Function File} {@var{edge} = } createEdge (@var{param})
38 %% @deftypefnx {Function File} {@var{edge} = } createEdge (@var{line}, @var{d})
39 %% Create an edge between two points, or from a line.
40 %%
41 %%   The internal format for edge representation is given by coordinates of
42 %%   two points : [x1 y1 x2 y2].
43 %%   This function can serve as a line to edge converter.
44 %%
45 %%
46 %%   Returns the edge between the two given points @var{p1} and @var{p2}.
47 %%   
48 %%   Returns the edge going through point (@var{x0}, @var{y0}) and with direction
49 %%   vector (@var{dx},@var{dy}).
50 %%
51 %%   When @var{param} is an array of 4 values, creates the edge going through the
52 %%   point (param(1) param(2)), and with direction vector given by
53 %%   (param(3) param(4)).
54 %%   
55 %% When @var{line} is given, creates the edge contained in @var{line}, with same
56 %% direction and start point, but with length given by @var{d}.
57 %%
58 %%
59 %%   Note: in all cases, parameters can be vertical arrays of the same
60 %%   dimension. The result is then an array of edges, of dimensions [N*4].
61 %%
62 %% @seealso{edges2d, lines2d, drawEdge, clipEdge}
63 %% @end deftypefn
64
65 function edge = createEdge(varargin)
66
67   if length(varargin)==1
68       % Only one input parameter. It can be :
69       % - line angle
70       % - array of four parameters
71       % TODO : add control for arrays of lines.
72       var = varargin{1};
73       
74       if size(var, 2)==4
75           % 4 parameters of the line in a single array.
76           %edge = var;
77           edge = zeros(size(var));
78           edge(:, 1:2) = var(:,1:2);
79           edge(:, 3:4) = edge(:, 1:2)+var(:,3:4);
80       elseif size(var, 2)==1
81           % 1 parameter : angle of the line, going through origin.
82           edge = [zeros(size(var,1)) zeros(size(var,1)) cos(var) sin(var)];
83       else
84           error('wrong number of dimension for arg1 : can be 1 or 4');
85       end
86       
87   elseif length(varargin)==2    
88       % 2 input parameters. They can be :
89       % - 2 points, then 2 arrays of 1*2 double,
90       % - a line, and a distance.
91       v1 = varargin{1};
92       v2 = varargin{2};
93       if size(v1, 2)==2
94           % first input parameter is first point, and second input is the
95           % second point.
96           %edge = [v1(:,1), v1(:,2), v2(:,1), v2(:,2)];
97           edge = [v1 v2];
98       else
99           % first input parameter is a line, and second one a distance.
100           angle = atan2(v1(:,4), v1(:,3));
101           edge = [v1(:,1), v1(:,2), v1(:,1)+v2.*cos(angle), v1(:,2)+v2.*sin(angle)];
102       end
103       
104   elseif length(varargin)==3
105       % 3 input parameters :
106       % first one is a point belonging to the line,
107       % second and third ones are direction vector of the line (dx and dy).
108       p = varargin{1};
109       edge = [p(:,1) p(:,2) p(:,1)+varargin{2} p(:,2)+varargin{3}];
110      
111   elseif length(varargin)==4
112       % 4 input parameters :
113       % they are x0, y0 (point belonging to line) and dx, dy (direction
114       % vector of the line).
115       % All parameters should have the same size.
116       edge = [varargin{1} varargin{2} varargin{1}+varargin{3} varargin{2}+varargin{4}];
117   else
118       error('Wrong number of arguments in ''createLine'' ');
119   end
120
121 endfunction