comparison scripts/geometry/tsearchn.m @ 31546:c664627d601e

tsearchn.m: Use Octave coding conventions. * tsearchn.m: Use function name in error() messages. Use "endif" rather than bare "end". Capitalize function input parameters in error() messages to match documentation. Cuddle parentheses to variable when performing indexing. Use '!' for logical not operator. Use two newlines between end of function and start of BIST tests. Add BIST tests for input validation.
author Rik <rik@octave.org>
date Fri, 25 Nov 2022 09:41:12 -0800
parents 72ef3d097059
children 5f11de0e7440
comparison
equal deleted inserted replaced
31545:85a073f7c5f9 31546:c664627d601e
44 if (nargin != 3) 44 if (nargin != 3)
45 print_usage (); 45 print_usage ();
46 endif 46 endif
47 47
48 if (columns (x) != columns (xi)) 48 if (columns (x) != columns (xi))
49 error ("columns (x) should equal columns (xi)") 49 error ("tsearchn: number of columns of X and XI must match");
50 end 50 endif
51 51
52 if (max (t(:)) > rows (x)) 52 if (max (t(:)) > rows (x))
53 error ("triangles should only access points in x") 53 error ("tsearchn: triangulation T must not access points outside X");
54 end 54 endif
55 55
56 if (nargout <= 1 && columns (x) == 2) # pass to the faster tsearch.cc 56 if (nargout <= 1 && columns (x) == 2) # pass to the faster tsearch.cc
57 idx = tsearch (x(:,1), x(:,2), t, xi(:,1), xi(:,2)); 57 idx = tsearch (x(:,1), x(:,2), t, xi(:,1), xi(:,2));
58 return 58 return;
59 endif 59 endif
60 60
61 nt = rows (t); 61 nt = rows (t);
62 [m, n] = size (x); 62 [m, n] = size (x);
63 mi = rows (xi); 63 mi = rows (xi);
86 86
87 ## The points xi are inside the current simplex if 87 ## The points xi are inside the current simplex if
88 ## (all (b >= 0) && all (b <= 1)). As sum (b,2) == 1, we only need to 88 ## (all (b >= 0) && all (b <= 1)). As sum (b,2) == 1, we only need to
89 ## test all(b>=0). 89 ## test all(b>=0).
90 inside = all (b >= -1e-12, 2); # -1e-12 instead of 0 for rounding errors 90 inside = all (b >= -1e-12, 2); # -1e-12 instead of 0 for rounding errors
91 idx (ni (inside)) = i; 91 idx(ni(inside)) = i;
92 p(ni(inside), :) = b(inside, :); 92 p(ni(inside), :) = b(inside, :);
93 ni = ni (~inside); 93 ni = ni(! inside);
94 endfor 94 endfor
95 95
96 endfunction 96 endfunction
97
97 98
98 %!shared x, tri 99 %!shared x, tri
99 %! x = [-1,-1;-1,1;1,-1]; 100 %! x = [-1,-1;-1,1;1,-1];
100 %! tri = [1, 2, 3]; 101 %! tri = [1, 2, 3];
101 %!test 102 %!test
116 %! assert (p, [1/3,1/3,1/3], 1e-12); 117 %! assert (p, [1/3,1/3,1/3], 1e-12);
117 %!test 118 %!test
118 %! [idx, p] = tsearchn (x,tri,[1,1]); 119 %! [idx, p] = tsearchn (x,tri,[1,1]);
119 %! assert (idx, NaN); 120 %! assert (idx, NaN);
120 %! assert (p, [NaN, NaN, NaN]); 121 %! assert (p, [NaN, NaN, NaN]);
122
123 ## Test input validation
124 %!error <Invalid call> tsearchn ()
125 %!error <Invalid call> tsearchn (1)
126 %!error <Invalid call> tsearchn (1, 2)
127 %!error <number of columns of X and XI must match> tsearchn ([1,2], 3, 4)
128 %!error <T must not access points outside X> tsearchn (1, 2, 3)