]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/polygons2d/distancePointPolyline.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / polygons2d / distancePointPolyline.m
1 ## Copyright (C) 2003-2011 David Legland <david.legland@grignon.inra.fr>
2 ## Copyright (C) 2012 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
3 ## All rights reserved.
4 ##
5 ## Redistribution and use in source and binary forms, with or without
6 ## modification, are permitted provided that the following conditions are met:
7 ##
8 ##     1 Redistributions of source code must retain the above copyright notice,
9 ##       this list of conditions and the following disclaimer.
10 ##     2 Redistributions in binary form must reproduce the above copyright
11 ##       notice, this list of conditions and the following disclaimer in the
12 ##       documentation and/or other materials provided with the distribution.
13 ##
14 ## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
15 ## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 ## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 ## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 ## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 ## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 ## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 ##
25 ## The views and conclusions contained in the software and documentation are
26 ## those of the authors and should not be interpreted as representing official
27 ## policies, either expressed or implied, of the copyright holders.
28
29 ## -*- texinfo -*-
30 ## @deftypefn {Function File} {@var{dist} = } distancePointPolyline (@var{point},@var{poly})
31 ##  Compute shortest distance between a point and a polyline
32 ##   Example:
33 ## @example
34 ##       pt1 = [30 20];
35 ##       pt2 = [30 5];
36 ##       poly = [10 10;50 10;50 50;10 50];
37 ##       distancePointPolyline([pt1;pt2], poly)
38 ##       ans =
39 ##           10
40 ##            5
41 ## @end example
42 ##
43 ## @seealso{polygons2d, points2d,distancePointEdge, projPointOnPolyline}
44 ## @end deftypefn
45
46 function varargout = distancePointPolyline(point, poly, varargin)
47
48   # number of points
49   Np = size(point, 1);
50
51   # allocate memory for result
52   minDist = inf * ones(Np, 1);
53
54   # process each point
55   for p = 1:Np
56       # construct the set of edges
57       edges = [poly(1:end-1, :) poly(2:end, :)];
58       
59       # compute distance between current each point and all edges
60       dist = distancePointEdge(point(p, :), edges);
61
62       # update distance if necessary
63       minDist(p) = min(dist);
64   end
65
66   # process output arguments
67   if nargout<=1
68       varargout{1} = minDist;
69   end
70
71 endfunction