]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/intersectLineEdge.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / intersectLineEdge.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{point} = } intersecLineEdge (@var{line}, @var{edge})
36 %% Return intersection between a line and an edge.
37 %%
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). 
43 %%   
44 %%   In case of colinear line and edge, returns [Inf Inf].
45 %%   If line does not intersect edge, returns [NaN NaN].
46 %%
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
50 %%   [N*2] array.
51 %%
52 %%   @seealso{lines2d, edges2d, intersectEdges, intersectLine}
53 %% @end deftypefn
54
55 function point = intersectLineEdge(lin, edge)
56   x0 =  lin(:,1);
57   y0 =  lin(:,2);
58   dx1 = lin(:,3);
59   dy1 = lin(:,4);
60   x1 =  edge(:,1);
61   y1 =  edge(:,2);
62   x2 = edge(:,3);
63   y2 = edge(:,4);
64   dx2 = x2-x1;
65   dy2 = y2-y1;
66
67   N1 = length(x0);
68   N2 = length(x1);
69
70   % indices of parallel lines
71   par = abs(dx1.*dy2-dx2.*dy1)<1e-14;
72
73   % indices of colinear lines
74   col = abs((x1-x0).*dy1-(y1-y0).*dx1)<1e-14 & par ;
75
76   xi(col) = Inf;
77   yi(col) = Inf;
78   xi(par & ~col) = NaN;
79   yi(par & ~col) = NaN;
80
81   i = ~par;
82
83   % compute intersection points
84   if N1==N2
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)) ;
89   elseif N1==1
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) ;
94   elseif N2==1
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)) ;
99   end
100
101   point   = [xi' yi'];
102   out     = find(~isPointOnEdge(point, edge));
103   point(out, :) = repmat([NaN NaN], [length(out) 1]);
104
105 endfunction
106