]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/linePosition.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / linePosition.m
1 %% Copyright (c) 2011, INRA
2 %% 2004-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 PUR@var{pos}E
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 %% @var{pos}SIBILITY 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{pos} =} linePosition (@var{point}, @var{line})
36 %% Position of a point on a line.
37 %% 
38 %%   Computes position of point @var{point} on the line @var{line}, relative to origin
39 %%   point and direction vector of the line.
40 %%   @var{line} has the form [x0 y0 dx dy],
41 %%   @var{point} has the form [x y], and is assumed to belong to line.
42 %%
43 %%   If @var{line} is an array of NL lines, return NL positions, corresponding to
44 %%   each line.
45 %%
46 %%   If @var{point} is an array of NP points, return NP positions, corresponding
47 %%   to each point.
48 %%
49 %%   If @var{point} is an array of NP points and @var{line}S is an array of NL lines,
50 %%   return an array of [NP NL] position, corresponding to each couple
51 %%   point-line.
52 %%
53 %% Example
54 %%
55 %% @example
56 %%   line = createLine([10 30], [30 90]);
57 %%   linePosition([20 60], line)
58 %%   ans =
59 %%       .5
60 %% @end example
61 %%
62 %% @seealso{lines2d, createLine, projPointOnLine, isPointOnLine}
63 %% @end deftypefn
64
65 function d = linePosition(point, lin)
66
67   % number of inputs
68   Nl = size(lin, 1);
69   Np = size(point, 1);
70
71   if Np == Nl
72       % if both inputs have the same size, no problem
73       dxl = lin(:, 3);
74       dyl = lin(:, 4);
75       dxp = point(:, 1) - lin(:, 1);
76       dyp = point(:, 2) - lin(:, 2);
77
78   elseif Np == 1
79       % one point, several lines
80       dxl = lin(:, 3);
81       dyl = lin(:, 4);
82       dxp = point(ones(Nl, 1), 1) - lin(:, 1);
83       dyp = point(ones(Nl, 1), 2) - lin(:, 2);
84       
85   elseif Nl == 1
86       % one lin, several points
87       dxl = lin(ones(Np, 1), 3);
88       dyl = lin(ones(Np, 1), 4);
89       dxp = point(:, 1) - lin(1);
90       dyp = point(:, 2) - lin(2);
91       
92   else
93       % expand one of the array to have the same size
94       dxl = repmat(lin(:,3)', Np, 1);
95       dyl = repmat(lin(:,4)', Np, 1);
96       dxp = repmat(point(:,1), 1, Nl) - repmat(lin(:,1)', Np, 1);
97       dyp = repmat(point(:,2), 1, Nl) - repmat(lin(:,2)', Np, 1);
98   end
99
100   % compute position
101   d = (dxp.*dxl + dyp.*dyl) ./ (dxl.^2 + dyl.^2);
102
103 endfunction
104
105 %!demo
106 %!  point = [20 60;10 30;25 75];
107 %!  lin = createLine([10 30], [30 90]);
108 %!  pos = linePosition(point, lin)
109 %!  
110 %!  plot(point(:,1),point(:,2),'ok');
111 %!  hold on
112 %!  drawLine(lin,'color','r');
113 %!  plot(lin(1)+lin(3)*pos,lin(2)+lin(4)*pos,'xb')
114 %!  hold off
115
116 %!test
117 %!  point = [20 60];
118 %!  lin = createLine([10 30], [30 90]);
119 %!  res = .5;
120 %!  pos = linePosition(point, lin);
121 %!  assert (res, pos);
122
123 %!test
124 %!  point = [20 60;10 30;25 75];
125 %!  lin = createLine([10 30], [30 90]);
126 %!  res = [.5; 0; .75];
127 %!  pos = linePosition(point, lin);
128 %!  assert (res, pos);
129
130 %!test
131 %!  point = [20 60];
132 %!  lin1 = createLine([10 30], [30 90]);
133 %!  lin2 = createLine([0 0], [20 60]);
134 %!  lin3 = createLine([20 60], [40 120]);
135 %!  lines = [lin1;lin2;lin3];
136 %!  res = [.5; 1; 0];
137 %!  pos = linePosition(point, lines);
138 %!  assert (res, pos);
139