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>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
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.
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.
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.
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.
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.
46 %% Returns the edge between the two given points @var{p1} and @var{p2}.
48 %% Returns the edge going through point (@var{x0}, @var{y0}) and with direction
49 %% vector (@var{dx},@var{dy}).
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)).
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}.
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].
62 %% @seealso{edges2d, lines2d, drawEdge, clipEdge}
65 function edge = createEdge(varargin)
67 if length(varargin)==1
68 % Only one input parameter. It can be :
70 % - array of four parameters
71 % TODO : add control for arrays of lines.
75 % 4 parameters of the line in a single array.
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)];
84 error('wrong number of dimension for arg1 : can be 1 or 4');
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.
94 % first input parameter is first point, and second input is the
96 %edge = [v1(:,1), v1(:,2), v2(:,1), v2(:,2)];
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)];
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).
109 edge = [p(:,1) p(:,2) p(:,1)+varargin{2} p(:,2)+varargin{3}];
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}];
118 error('Wrong number of arguments in ''createLine'' ');