Mercurial > octave
annotate libinterp/corefcn/tsearch.cc @ 19877:12ecb7212b44
move some files without external dependencies from dldfcn to corefcn
* __dsearchn__.cc, __ichol__.cc, __ilu__.cc, tsearch.cc: Move from
dldfcn to corefcn directory. Use DEFUN instead of DEFUN_DLD.
* libinterp/corefcn/module.mk, libinterp/dldfcn/module-files: Update.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 27 Feb 2015 19:44:28 -0500 |
parents | libinterp/dldfcn/tsearch.cc@4197fc428c7d |
children | 4f45eaf83908 |
rev | line source |
---|---|
6823 | 1 /* |
2 | |
19697
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18100
diff
changeset
|
3 Copyright (C) 2002-2015 Andreas Stahel |
6823 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
6823 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
6823 | 20 |
21 */ | |
22 | |
7016 | 23 // Author: Andreas Stahel <Andreas.Stahel@hta-bi.bfh.ch> |
24 | |
9786
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
25 #ifdef HAVE_CONFIG_H |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
26 #include <config.h> |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
27 #endif |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
28 |
6823 | 29 #include "lo-ieee.h" |
7231 | 30 #include "lo-math.h" |
6823 | 31 |
19877
12ecb7212b44
move some files without external dependencies from dldfcn to corefcn
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
32 #include "defun.h" |
9786
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
33 #include "error.h" |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
34 #include "oct-obj.h" |
2c279308f6ab
fix includes in some src/DLD-FUNCTIONS files
John W. Eaton <jwe@octave.org>
parents:
9245
diff
changeset
|
35 |
10550 | 36 inline double max (double a, double b, double c) |
6823 | 37 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
38 if (a < b) |
10550 | 39 return (b < c ? c : b); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
40 else |
10550 | 41 return (a < c ? c : a); |
6823 | 42 } |
43 | |
10550 | 44 inline double min (double a, double b, double c) |
6823 | 45 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
46 if (a > b) |
10550 | 47 return (b > c ? c : b); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
48 else |
10550 | 49 return (a > c ? c : a); |
6823 | 50 } |
51 | |
52 #define REF(x,k,i) x(static_cast<octave_idx_type>(elem((k), (i))) - 1) | |
53 | |
54 // for large data set the algorithm is very slow | |
55 // one should presort (how?) either the elements of the points of evaluation | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
56 // to cut down the time needed to decide which triangle contains the |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
57 // given point |
6823 | 58 |
59 // e.g., build up a neighbouring triangle structure and use a simplex-like | |
60 // method to traverse it | |
61 | |
19877
12ecb7212b44
move some files without external dependencies from dldfcn to corefcn
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
62 DEFUN (tsearch, args, , |
12ecb7212b44
move some files without external dependencies from dldfcn to corefcn
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
63 "-*- texinfo -*-\n\ |
12ecb7212b44
move some files without external dependencies from dldfcn to corefcn
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
64 @deftypefn {Built-in Function} {@var{idx} =} tsearch (@var{x}, @var{y}, @var{t}, @var{xi}, @var{yi})\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
65 Search for the enclosing Delaunay convex hull. For @code{@var{t} =\n\ |
6823 | 66 delaunay (@var{x}, @var{y})}, finds the index in @var{t} containing the\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
7231
diff
changeset
|
67 points @code{(@var{xi}, @var{yi})}. For points outside the convex hull,\n\ |
6823 | 68 @var{idx} is NaN.\n\ |
69 @seealso{delaunay, delaunayn}\n\ | |
70 @end deftypefn") | |
71 { | |
72 const double eps=1.0e-12; | |
73 | |
74 octave_value_list retval; | |
75 const int nargin = args.length (); | |
10550 | 76 if (nargin != 5) |
77 { | |
78 print_usage (); | |
79 return retval; | |
80 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
81 |
10550 | 82 const ColumnVector x (args(0).vector_value ()); |
83 const ColumnVector y (args(1).vector_value ()); | |
84 const Matrix elem (args(2).matrix_value ()); | |
85 const ColumnVector xi (args(3).vector_value ()); | |
86 const ColumnVector yi (args(4).vector_value ()); | |
6823 | 87 |
10550 | 88 if (error_state) |
89 return retval; | |
90 | |
91 const octave_idx_type nelem = elem.rows (); | |
6823 | 92 |
10550 | 93 ColumnVector minx (nelem); |
94 ColumnVector maxx (nelem); | |
95 ColumnVector miny (nelem); | |
96 ColumnVector maxy (nelem); | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
97 for (octave_idx_type k = 0; k < nelem; k++) |
6823 | 98 { |
10550 | 99 minx(k) = min (REF (x, k, 0), REF (x, k, 1), REF (x, k, 2)) - eps; |
100 maxx(k) = max (REF (x, k, 0), REF (x, k, 1), REF (x, k, 2)) + eps; | |
101 miny(k) = min (REF (y, k, 0), REF (y, k, 1), REF (y, k, 2)) - eps; | |
102 maxy(k) = max (REF (y, k, 0), REF (y, k, 1), REF (y, k, 2)) + eps; | |
6823 | 103 } |
104 | |
10550 | 105 const octave_idx_type np = xi.length (); |
106 ColumnVector values (np); | |
6823 | 107 |
18100
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
108 double x0, y0, a11, a12, a21, a22, det; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
109 x0 = y0 = 0.0; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
110 a11 = a12 = a21 = a22 = 0.0; |
6a71e5030df5
Follow coding convention of defining and initializing only 1 variable per line in liboctinterp.
Rik <rik@octave.org>
parents:
17787
diff
changeset
|
111 det = 0.0; |
6823 | 112 |
113 octave_idx_type k = nelem; // k is a counter of elements | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
114 for (octave_idx_type kp = 0; kp < np; kp++) |
6823 | 115 { |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
116 const double xt = xi(kp); |
6823 | 117 const double yt = yi(kp); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
118 |
6823 | 119 // check if last triangle contains the next point |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
120 if (k < nelem) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
121 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
122 const double dx1 = xt - x0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
123 const double dx2 = yt - y0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
124 const double c1 = (a22 * dx1 - a21 * dx2) / det; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
125 const double c2 = (-a12 * dx1 + a11 * dx2) / det; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
126 if (c1 >= -eps && c2 >= -eps && (c1 + c2) <= (1 + eps)) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
127 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
128 values(kp) = double(k+1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
129 continue; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
130 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
131 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
132 |
6823 | 133 // it doesn't, so go through all elements |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
134 for (k = 0; k < nelem; k++) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
135 { |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
136 OCTAVE_QUIT; |
10550 | 137 if (xt >= minx(k) && xt <= maxx(k) && yt >= miny(k) && yt <= maxy(k)) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
138 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
139 // element inside the minimum rectangle: examine it closely |
10550 | 140 x0 = REF (x, k, 0); |
141 y0 = REF (y, k, 0); | |
142 a11 = REF (x, k, 1) - x0; | |
143 a12 = REF (y, k, 1) - y0; | |
144 a21 = REF (x, k, 2) - x0; | |
145 a22 = REF (y, k, 2) - y0; | |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
146 det = a11 * a22 - a21 * a12; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
147 |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
148 // solve the system |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
149 const double dx1 = xt - x0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
150 const double dx2 = yt - y0; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
151 const double c1 = (a22 * dx1 - a21 * dx2) / det; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
152 const double c2 = (-a12 * dx1 + a11 * dx2) / det; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
153 if ((c1 >= -eps) && (c2 >= -eps) && ((c1 + c2) <= (1 + eps))) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
154 { |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
155 values(kp) = double(k+1); |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
156 break; |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
157 } |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
158 } //endif # examine this element closely |
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
159 } //endfor # each element |
6823 | 160 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
161 if (k == nelem) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
9786
diff
changeset
|
162 values(kp) = lo_ieee_nan_value (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
163 |
6823 | 164 } //endfor # kp |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
165 |
6823 | 166 retval(0) = values; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
167 |
6823 | 168 return retval; |
169 } | |
170 | |
171 /* | |
172 %!shared x, y, tri | |
173 %! x = [-1;-1;1]; | |
174 %! y = [-1;1;-1]; | |
175 %! tri = [1, 2, 3]; | |
176 %!assert (tsearch (x,y,tri,-1,-1), 1) | |
177 %!assert (tsearch (x,y,tri, 1,-1), 1) | |
178 %!assert (tsearch (x,y,tri,-1, 1), 1) | |
179 %!assert (tsearch (x,y,tri,-1/3, -1/3), 1) | |
180 %!assert (tsearch (x,y,tri, 1, 1), NaN) | |
181 | |
14501
60e5cf354d80
Update %!tests in DLD-FUNCTIONS/ directory with Octave coding conventions.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
182 %!error tsearch () |
6823 | 183 */ |