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{line} =} createLine(varargin)
36 %% Create a straight line from 2 points, or from other inputs
38 %% Line is represented in a parametric form : [x0 y0 dx dy]
43 %% L = createLine(p1, p2);
44 %% Returns the line going through the two given points.
46 %% L = createLine(x0, y0, dx, dy);
47 %% Returns the line going through point (x0, y0) and with direction
50 %% L = createLine(LINE);
51 %% where LINE is an array of 4 values, creates the line going through the
52 %% point (LINE(1) LINE(2)), and with direction given by vector (LINE(3)
55 %% L = createLine(THETA);
56 %% Create a polar line originated at (0,0) and with angle THETA.
58 %% L = createLine(RHO, THETA);
59 %% Create a polar line with normal theta, and with min distance to origin
60 %% equal to rho. rho can be negative, in this case, the line is the same
61 %% as with CREATELINE(-rho, theta+pi), but the orientation is different.
64 %% Note: in all cases, parameters can be vertical arrays of the same
65 %% dimension. The result is then an array of lines, of dimensions [N*4].
67 %% NOTE : A line can also be represented with a 1*5 array :
69 %% whith 't' being one of the following :
70 %% - t=0 : line is a singleton (x0,y0)
71 %% - t=1 : line is an edge segment, between points (x0,y0) and (x0+dx,
73 %% - t=Inf : line is a Ray, originated from (x0,y0) and going to infinity
74 %% in the direction(dx,dy).
75 %% - t=-Inf : line is a Ray, originated from (x0,y0) and going to infinity
76 %% in the direction(-dx,-dy).
77 %% - t=NaN : line is a real straight line, and contains all points
78 %% verifying the above equation.
79 %% This seems us a convenient way to represent uniformly all kind of lines
80 %% (including edges, rays, and even point).
82 %% NOTE2 : Any line object can be represented using a 1x6 array :
83 %% [x0 y0 dx dy t0 t1]
84 %% the first 4 parameters define the supporting line,
85 %% t0 represent the position of the first point on the line,
86 %% and t1 the position of the last point.
87 %% * for edges : t0 = 0, and t1=1
88 %% * for straight lines : t0 = -inf, t1=inf
89 %% * for rays : t0=0, t1=inf (or t0=-inf,t1=0 for inverted ray).
90 %% I propose to call these objects 'lineArc'
92 %% @seealso{lines2d, createEdge, createRay}
95 function line = createLine(varargin)
97 if length(varargin)==1
98 % Only one input parameter. It can be :
100 % - array of four parameters
101 % TODO : add control for arrays of lines.
105 % 4 parameters of the line in a single array.
107 elseif size(var, 2)==1
108 % 1 parameter : angle of the line, going through origin.
109 line = [zeros(size(var)) zeros(size(var)) cos(var) sin(var)];
111 error('wrong number of dimension for arg1 : can be 1 or 4');
114 elseif length(varargin)==2
115 % 2 input parameters. They can be :
116 % - line angle and signed distance to origin.
117 % - 2 points, then 2 arrays of 1*2 double.
121 % first param is angle of line, and second param is signed distance
123 line = [v1.*cos(v2) v1.*sin(v2) -sin(v2) cos(v2)];
125 % first input parameter is first point, and second input is the
127 line = [v1(:,1), v1(:,2), v2(:,1)-v1(:,1), v2(:,2)-v1(:,2)];
130 elseif length(varargin)==3
131 % 3 input parameters :
132 % first one is a point belonging to the line,
133 % second and third ones are direction vector of the line (dx and dy).
135 line = [p(:,1) p(:,2) varargin{2} varargin{3}];
137 elseif length(varargin)==4
138 % 4 input parameters :
139 % they are x0, y0 (point belongng to line) and dx, dy (direction vector
141 % All parameters should have the same size.
142 line = [varargin{1} varargin{2} varargin{3} varargin{4}];
144 error('Wrong number of arguments in ''createLine'' ');
152 %! lin = createLine(p1, p2);
153 %! assert (p1, lin(1,1:2), 1e-6);
154 %! assert (p2-p1, lin(1,3:4), 1e-6);
159 %! lin = createLine(p1, p2);
160 %! assert (2, size(lin, 1));
161 %! assert (p1, lin(:,1:2), 1e-6);
162 %! assert (p2-p1, lin(:,3:4), 1e-6);