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{point} = } intersecLineEdge (@var{line}, @var{edge})
36 %% Return intersection between a line and an edge.
38 %% Returns the intersection point of lines @var{line} and edge @var{edge}.
39 %% @var{line} is a 1x4 array containing parametric representation of the line
40 %% (in the form [x0 y0 dx dy], see @code{createLine} for details).
41 %% @var{edge} is a 1x4 array containing coordinates of first and second point
42 %% (in the form [x1 y1 x2 y2], see @code{createEdge} for details).
44 %% In case of colinear line and edge, returns [Inf Inf].
45 %% If line does not intersect edge, returns [NaN NaN].
47 %% If each input is [N*4] array, the result is a [N*2] array containing
48 %% intersections for each couple of edge and line.
49 %% If one of the input has N rows and the other 1 row, the result is a
52 %% @seealso{lines2d, edges2d, intersectEdges, intersectLine}
55 function point = intersectLineEdge(lin, edge)
70 % indices of parallel lines
71 par = abs(dx1.*dy2-dx2.*dy1)<1e-14;
73 % indices of colinear lines
74 col = abs((x1-x0).*dy1-(y1-y0).*dx1)<1e-14 & par ;
83 % compute intersection points
85 xi(i) = ((y1(i)-y0(i)).*dx1(i).*dx2(i) + x0(i).*dy1(i).*dx2(i) - x1(i).*dy2(i).*dx1(i)) ./ ...
86 (dx2(i).*dy1(i)-dx1(i).*dy2(i)) ;
87 yi(i) = ((x1(i)-x0(i)).*dy1(i).*dy2(i) + y0(i).*dx1(i).*dy2(i) - y1(i).*dx2(i).*dy1(i)) ./ ...
88 (dx1(i).*dy2(i)-dx2(i).*dy1(i)) ;
90 xi(i) = ((y1(i)-y0).*dx1.*dx2(i) + x0.*dy1.*dx2(i) - x1(i).*dy2(i).*dx1) ./ ...
91 (dx2(i).*dy1-dx1.*dy2(i)) ;
92 yi(i) = ((x1(i)-x0).*dy1.*dy2(i) + y0.*dx1.*dy2(i) - y1(i).*dx2(i).*dy1) ./ ...
93 (dx1.*dy2(i)-dx2(i).*dy1) ;
95 xi(i) = ((y1-y0(i)).*dx1(i).*dx2 + x0(i).*dy1(i).*dx2 - x1(i).*dy2.*dx1(i)) ./ ...
96 (dx2.*dy1(i)-dx1(i).*dy2) ;
97 yi(i) = ((x1-x0(i)).*dy1(i).*dy2 + y0(i).*dx1(i).*dy2 - y1(i).*dx2.*dy1(i)) ./ ...
98 (dx1(i).*dy2-dx2.*dy1(i)) ;
102 out = find(~isPointOnEdge(point, edge));
103 point(out, :) = repmat([NaN NaN], [length(out) 1]);