Mercurial > octave
annotate scripts/geometry/delaunayn.m @ 27919:1891570abac8
update Octave Project Developers copyright for the new year
In files that have the "Octave Project Developers" copyright notice,
update for 2020.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 06 Jan 2020 22:29:51 -0500 |
parents | b442ec6dda5c |
children | bd51beb6205e |
rev | line source |
---|---|
27919
1891570abac8
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27918
diff
changeset
|
1 ## Copyright (C) 2007-2020 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
2 ## |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
4 ## or <https://octave.org/COPYRIGHT.html/>. |
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
5 ## |
6823 | 6 ## |
7 ## This file is part of Octave. | |
8 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
9 ## Octave is free software: you can redistribute it and/or modify it |
6823 | 10 ## under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
11 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
12 ## (at your option) any later version. |
6823 | 13 ## |
14 ## Octave is distributed in the hope that it will be useful, but | |
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
17 ## GNU General Public License for more details. |
6823 | 18 ## |
19 ## You should have received a copy of the GNU General Public License | |
7016 | 20 ## along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23220
diff
changeset
|
21 ## <https://www.gnu.org/licenses/>. |
6823 | 22 |
23 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
24 ## @deftypefn {} {@var{T} =} delaunayn (@var{pts}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
20158
diff
changeset
|
25 ## @deftypefnx {} {@var{T} =} delaunayn (@var{pts}, @var{options}) |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
26 ## Compute the Delaunay triangulation for an N-dimensional set of points. |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
27 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
28 ## The Delaunay triangulation is a tessellation of the convex hull of a set of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
29 ## points such that no N-sphere defined by the N-triangles contains any other |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
30 ## points from the set. |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
31 ## |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
32 ## The input matrix @var{pts} of size [n, dim] contains n points in a space of |
20158
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
33 ## dimension dim. The return matrix @var{T} has size [m, dim+1]. Each row of |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19833
diff
changeset
|
34 ## @var{T} contains a set of indices back into the original set of points |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
35 ## @var{pts} which describes a simplex of dimension dim. For example, a 2-D |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
36 ## simplex is a triangle and 3-D simplex is a tetrahedron. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
37 ## |
21546
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
38 ## An optional second argument, which must be a string or cell array of |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
39 ## strings, contains options passed to the underlying qhull command. See the |
f7f97d7e9294
doc: Wrap m-file docstrings to 79 characters + newline (80 total).
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
40 ## documentation for the Qhull library for details |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
41 ## @url{http://www.qhull.org/html/qh-quick.htm#options}. |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
42 ## The default options depend on the dimension of the input: |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
43 ## |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
44 ## @itemize |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
45 ## @item 2-D and 3-D: @var{options} = @code{@{"Qt", "Qbb", "Qc", "Qz"@}} |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
46 ## |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
47 ## @item 4-D and higher: @var{options} = @code{@{"Qt", "Qbb", "Qc", "Qx"@}} |
6826 | 48 ## @end itemize |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
49 ## |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
50 ## If @var{options} is not present or @code{[]} then the default arguments are |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
51 ## used. Otherwise, @var{options} replaces the default argument list. |
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
52 ## To append user options to the defaults it is necessary to repeat the |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
53 ## default arguments in @var{options}. Use a null string to pass no arguments. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
54 ## |
19164
ba167badef9f
Deprecate delaunay3 and extend delaunay to 3-D inputs.
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
55 ## @seealso{delaunay, convhulln, voronoin, trimesh, tetramesh} |
6823 | 56 ## @end deftypefn |
57 | |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
58 function T = delaunayn (pts, varargin) |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
59 |
6823 | 60 if (nargin < 1) |
6826 | 61 print_usage (); |
6823 | 62 endif |
63 | |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 T = __delaunayn__ (pts, varargin{:}); |
6823 | 65 |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 if (isa (pts, "single")) |
19172
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
67 tol = 1e3 * eps ("single"); |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7208
diff
changeset
|
68 else |
19172
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
69 tol = 1e3 * eps; |
7795
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7208
diff
changeset
|
70 endif |
df9519e9990c
Handle single precision eps values
David Bateman <dbateman@free.fr>
parents:
7208
diff
changeset
|
71 |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
72 ## Try to remove the zero volume simplices. The volume of the i-th simplex is |
25328
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
73 ## given by abs(det(pts(T(i,1:end-1),:)-pts(T(i,2:end),:)))/factorial(ndim+1) |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 ## (reference http://en.wikipedia.org/wiki/Simplex). Any simplex with a |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
75 ## relative volume less than some arbitrary criteria is rejected. The |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
76 ## criteria we use is the volume of the simplex corresponding to an |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11563
diff
changeset
|
77 ## orthogonal simplex is equal edge length all equal to the edge length of |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
78 ## the original simplex. If the relative volume is 1e3*eps then the simplex |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
79 ## is rejected. Note division of the two volumes means that the factor |
25328
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
80 ## factorial(ndim+1) is dropped. |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
81 [nt, nd] = size (T); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
82 if (nd == 3) |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
83 ## 2-D case |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
84 np = rows (pts); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
85 ptsz = [pts, zeros(np, 1)]; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
86 p1 = ptsz(T(:,1), :); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
87 p2 = ptsz(T(:,2), :); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
88 p3 = ptsz(T(:,3), :); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
89 p12 = p1 - p2; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
90 p23 = p2 - p3; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
91 det = cross (p12, p23, 2); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
92 idx = abs (det(:,3) ./ sqrt (sumsq (p12, 2))) < tol & ... |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
93 abs (det(:,3) ./ sqrt (sumsq (p23, 2))) < tol; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
94 else |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
95 ## FIXME: Vectorize this for loop or convert delaunayn to .oct function |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
96 idx = []; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
97 for i = 1:nt |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
98 X = pts(T(i,1:end-1),:) - pts(T(i,2:end),:); |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
99 if (abs (det (X)) / sqrt (sumsq (X, 2)) < tol) |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
100 idx(end+1) = i; |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
101 endif |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
102 endfor |
3b96348d5ccd
delaunayn.m: Vectorize test for 2-D zero volume simplexes (bug #53689)
Rik <rik@octave.org>
parents:
25054
diff
changeset
|
103 endif |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 T(idx,:) = []; |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
105 |
6823 | 106 endfunction |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
107 |
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
108 |
19172
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
109 %!testif HAVE_QHULL |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
110 %! x = [-1, 0; 0, 1; 1, 0; 0, -1; 0, 0]; |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
111 %! assert (sortrows (sort (delaunayn (x), 2)), [1,2,5;1,4,5;2,3,5;3,4,5]); |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
112 |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
113 ## Test 3-D input |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
114 %!testif HAVE_QHULL |
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
115 %! x = [-1, -1, 1, 0, -1]; y = [-1, 1, 1, 0, -1]; z = [0, 0, 0, 1, 1]; |
21580
ecce63c99c3f
maint: Add semicolons to terminate code in %! blocks.
Rik <rik@octave.org>
parents:
21546
diff
changeset
|
116 %! assert (sortrows (sort (delaunayn ([x(:) y(:) z(:)]), 2)), [1,2,3,4;1,2,4,5]); |
19172
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
117 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
118 ## FIXME: Need tests for delaunayn |
13746
7ff0bdc3dc4c
Revamp geometry functions dependent on Qhull (Bug #34604, Bug #33346)
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
119 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
120 ## Input validation tests |
19172
4318cb91deac
delaunayn.m: Slight performance increase and addition of BIST tests.
Rik <rik@octave.org>
parents:
19164
diff
changeset
|
121 %!error delaunayn () |