1## Copyright (C) 2021 David Legland
2## All rights reserved.
3##
4## Redistribution and use in source and binary forms, with or without
5## modification, are permitted provided that the following conditions are met:
6##
7##     1 Redistributions of source code must retain the above copyright notice,
8##       this list of conditions and the following disclaimer.
9##     2 Redistributions in binary form must reproduce the above copyright
10##       notice, this list of conditions and the following disclaimer in the
11##       documentation and/or other materials provided with the distribution.
12##
13## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS''
14## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17## ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19## SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20## CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23##
24## The views and conclusions contained in the software and documentation are
25## those of the authors and should not be interpreted as representing official
26## policies, either expressed or implied, of the copyright holders.
27
28function b = isParallel(v1, v2, varargin)
29%ISPARALLEL Check parallelism of two vectors.
30%
31%   B = isParallel(V1, V2)
32%   where V1 and V2 are two row vectors of length ND, ND being the
33%   dimension, returns 1 if the vectors are parallel, and 0 otherwise.
34%
35%   Also works when V1 and V2 are two N-by-ND arrays with same number of
36%   rows. In this case, return a N-by-1 array containing 1 at the positions
37%   of parallel vectors.
38%
39%   Also works when one of V1 or V2 is N-by-1 and the other one is N-by-ND
40%   array, in this case return N-by-1 results.
41%
42%   B = isParallel(V1, V2, ACCURACY)
43%   specifies the accuracy for numerical computation. Default value is
44%   1e-14.
45%
46%
47%   Example
48%   isParallel([1 2], [2 4])
49%   ans =
50%       1
51%   isParallel([1 2], [1 3])
52%   ans =
53%       0
54%
55%   See also
56%   vectors2d, isPerpendicular, lines2d
57%
58
59% ------
60% Author: David Legland
61% e-mail: david.legland@inra.fr
62% Created: 2006-04-25
63% Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
64
65%   HISTORY
66%   2007-09-18 copy from isParallel3d, adapt to any dimension, and add psb
67%       to specify precision
68%   2007-01-16 fix bug
69%   2009-09-21 fix bug for array of 3 vectors
70%   2011-01-20 replace repmat by ones-indexing (faster)
71%   2011-06-16 use direct computation (faster)
72%   2017-08-31 use normalized vectors
73
74% default accuracy
75acc = 1e-14;
76if ~isempty(varargin)
77    acc = abs(varargin{1});
78end
79
80% normalize vectors
81v1 = normalizeVector(v1);
82v2 = normalizeVector(v2);
83
84% adapt size of inputs if needed
85n1 = size(v1, 1);
86n2 = size(v2, 1);
87if n1 ~= n2
88    if n1 == 1
89        v1 = v1(ones(n2,1), :);
90    elseif n2 == 1
91        v2 = v2(ones(n1,1), :);
92    end
93end
94
95% performs computation
96if size(v1, 2) == 2
97    % computation for plane vectors
98    b = abs(v1(:, 1) .* v2(:, 2) - v1(:, 2) .* v2(:, 1)) < acc;
99else
100    % computation in greater dimensions
101    b = vectorNorm(cross(v1, v2, 2)) < acc;
102end
103
104