Mercurial > hg > octave-lyh
annotate scripts/geometry/tsearchn.m @ 13485:59d266f7ed89
Removed plotter tab.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Wed, 27 Apr 2011 14:21:28 +0200 |
parents | d0b799dafede |
children | 72c96de7a403 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 2007-2011 David Bateman |
6823 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
6823 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
6823 | 18 |
19 ## -*- texinfo -*- | |
6846 | 20 ## @deftypefn {Function File} {[@var{idx}, @var{p}] =} tsearchn (@var{x}, @var{t}, @var{xi}) |
12575
d0b799dafede
Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
21 ## Search for the enclosing Delaunay convex hull. For @code{@var{t} = |
6823 | 22 ## delaunayn (@var{x})}, finds the index in @var{t} containing the |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
7017
diff
changeset
|
23 ## points @var{xi}. For points outside the convex hull, @var{idx} is NaN. |
9070
e9dc2ed2ec0f
Cleanup documentation for poly.texi, interp.texi, geometry.texi
Rik <rdrider0-list@yahoo.com>
parents:
9051
diff
changeset
|
24 ## If requested @code{tsearchn} also returns the Barycentric coordinates @var{p} |
6823 | 25 ## of the enclosing triangles. |
26 ## @seealso{delaunay, delaunayn} | |
27 ## @end deftypefn | |
28 | |
29 function [idx, p] = tsearchn (x, t, xi) | |
30 if (nargin != 3) | |
31 print_usage (); | |
32 endif | |
33 | |
34 nt = size (t, 1); | |
35 [m, n] = size (x); | |
36 mi = size (xi, 1); | |
10548
479536c5bb10
Replace lowercase nan with NaN for visual cue in scripts
Rik <code@nomad.inbox5.com>
parents:
9245
diff
changeset
|
37 idx = NaN (mi, 1); |
479536c5bb10
Replace lowercase nan with NaN for visual cue in scripts
Rik <code@nomad.inbox5.com>
parents:
9245
diff
changeset
|
38 p = NaN (mi, n + 1); |
6823 | 39 |
40 ni = [1:mi].'; | |
41 for i = 1 : nt | |
42 ## Only calculate the Barycentric coordinates for points that have not | |
43 ## already been found in a triangle. | |
44 b = cart2bary (x (t (i, :), :), xi(ni,:)); | |
45 | |
46 ## Our points xi are in the current triangle if | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
47 ## (all(b >= 0) && all (b <= 1)). However as we impose that |
6823 | 48 ## sum(b,2) == 1 we only need to test all(b>=0). Note need to add |
49 ## a small margin for rounding errors | |
50 intri = all (b >= -1e-12, 2); | |
51 idx(ni(intri)) = i; | |
52 p(ni(intri),:) = b(intri, :); | |
53 ni(intri) = []; | |
54 endfor | |
55 endfunction | |
56 | |
57 function Beta = cart2bary (T, P) | |
58 ## Conversion of Cartesian to Barycentric coordinates. | |
59 ## Given a reference simplex in N dimensions represented by a | |
60 ## (N+1)-by-(N) matrix, and arbitrary point P in cartesion coordinates, | |
61 ## represented by a N-by-1 row vector can be written as | |
62 ## | |
63 ## P = Beta * T | |
64 ## | |
65 ## Where Beta is a N+1 vector of the barycentric coordinates. A criteria | |
66 ## on Beta is that | |
67 ## | |
68 ## sum (Beta) == 1 | |
69 ## | |
70 ## and therefore we can write the above as | |
71 ## | |
72 ## P - T(end, :) = Beta(1:end-1) * (T(1:end-1,:) - ones(N,1) * T(end,:)) | |
73 ## | |
74 ## and then we can solve for Beta as | |
75 ## | |
76 ## Beta(1:end-1) = (P - T(end,:)) / (T(1:end-1,:) - ones(N,1) * T(end,:)) | |
77 ## Beta(end) = sum(Beta) | |
78 ## | |
79 ## Note below is generalize for multiple values of P, one per row. | |
80 [M, N] = size (P); | |
81 Beta = (P - ones (M,1) * T(end,:)) / (T(1:end-1,:) - ones(N,1) * T(end,:)); | |
82 Beta (:,end+1) = 1 - sum(Beta, 2); | |
83 endfunction | |
84 | |
85 %!shared x, tri | |
86 %! x = [-1,-1;-1,1;1,-1]; | |
87 %! tri = [1, 2, 3]; | |
88 %!test | |
89 %! [idx, p] = tsearchn (x,tri,[-1,-1]); | |
90 %! assert (idx, 1) | |
91 %! assert (p, [1,0,0], 1e-12) | |
92 %!test | |
93 %! [idx, p] = tsearchn (x,tri,[-1,1]); | |
94 %! assert (idx, 1) | |
95 %! assert (p, [0,1,0], 1e-12) | |
96 %!test | |
97 %! [idx, p] = tsearchn (x,tri,[1,-1]); | |
98 %! assert (idx, 1) | |
99 %! assert (p, [0,0,1], 1e-12) | |
100 %!test | |
101 %! [idx, p] = tsearchn (x,tri,[-1/3,-1/3]); | |
102 %! assert (idx, 1) | |
103 %! assert (p, [1/3,1/3,1/3], 1e-12) | |
104 %!test | |
105 %! [idx, p] = tsearchn (x,tri,[1,1]); | |
106 %! assert (idx, NaN) | |
107 %! assert (p, [NaN, NaN, NaN]) |