Mercurial > octave
annotate scripts/plot/draw/shrinkfaces.m @ 29359:7854d5752dd2
maint: merge stable to default.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 10 Feb 2021 10:10:40 -0500 |
parents | 41c54377d67f 0a5b15007766 |
children | 796f54d4ddbf |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 ######################################################################## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 ## |
29358
0a5b15007766
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
27978
diff
changeset
|
3 ## Copyright (C) 2012-2021 The Octave Project Developers |
27918
b442ec6dda5c
use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents:
27106
diff
changeset
|
4 ## |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 ## See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 ## distribution or <https://octave.org/copyright/>. |
14574 | 7 ## |
8 ## This file is part of Octave. | |
9 ## | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
24478
diff
changeset
|
10 ## Octave is free software: you can redistribute it and/or modify it |
14574 | 11 ## 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:
24478
diff
changeset
|
12 ## the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22737
diff
changeset
|
13 ## (at your option) any later version. |
14574 | 14 ## |
15 ## Octave is distributed in the hope that it will be useful, but | |
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22737
diff
changeset
|
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22737
diff
changeset
|
18 ## GNU General Public License for more details. |
14574 | 19 ## |
20 ## You should have received a copy of the GNU General Public License | |
21 ## 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:
24478
diff
changeset
|
22 ## <https://www.gnu.org/licenses/>. |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 ## |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 ######################################################################## |
14574 | 25 |
26 ## -*- texinfo -*- | |
20852
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19865
diff
changeset
|
27 ## @deftypefn {} {} shrinkfaces (@var{p}, @var{sf}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19865
diff
changeset
|
28 ## @deftypefnx {} {@var{nfv} =} shrinkfaces (@var{p}, @var{sf}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19865
diff
changeset
|
29 ## @deftypefnx {} {@var{nfv} =} shrinkfaces (@var{fv}, @var{sf}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19865
diff
changeset
|
30 ## @deftypefnx {} {@var{nfv} =} shrinkfaces (@var{f}, @var{v}, @var{sf}) |
516bb87ea72e
2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents:
19865
diff
changeset
|
31 ## @deftypefnx {} {[@var{nf}, @var{nv}] =} shrinkfaces (@dots{}) |
14574 | 32 ## |
18334 | 33 ## Reduce the size of faces in a patch by the shrink factor @var{sf}. |
34 ## | |
35 ## The patch object can be specified by a graphics handle (@var{p}), a patch | |
36 ## structure (@var{fv}) with the fields @qcode{"faces"} and @qcode{"vertices"}, | |
37 ## or as two separate matrices (@var{f}, @var{v}) of faces and vertices. | |
14574 | 38 ## |
18334 | 39 ## The shrink factor @var{sf} is a positive number specifying the percentage |
40 ## of the original area the new face will occupy. If no factor is given the | |
41 ## default is 0.3 (a reduction to 30% of the original size). A factor greater | |
42 ## than 1.0 will result in the expansion of faces. | |
43 ## | |
44 ## Given a patch handle as the first input argument and no output parameters, | |
45 ## perform the shrinking of the patch faces in place and redraw the patch. | |
14574 | 46 ## |
47 ## If called with one output argument, return a structure with fields | |
17281
bc924baa2c4e
doc: Add new @qcode macro for code samples which are quoted.
Rik <rik@octave.org>
parents:
17122
diff
changeset
|
48 ## @qcode{"faces"}, @qcode{"vertices"}, and @qcode{"facevertexcdata"} |
18334 | 49 ## containing the data after shrinking. This structure can be used directly |
50 ## as an input argument to the @code{patch} function. | |
14574 | 51 ## |
18334 | 52 ## @strong{Caution:}: Performing the shrink operation on faces which are not |
53 ## convex can lead to undesirable results. | |
14574 | 54 ## |
18334 | 55 ## Example: a triangulated 3/4 circle and the corresponding shrunken version. |
14574 | 56 ## |
57 ## @example | |
58 ## @group | |
59 ## [phi r] = meshgrid (linspace (0, 1.5*pi, 16), linspace (1, 2, 4)); | |
60 ## tri = delaunay (phi(:), r(:)); | |
61 ## v = [r(:).*sin(phi(:)) r(:).*cos(phi(:))]; | |
62 ## clf () | |
63 ## p = patch ("Faces", tri, "Vertices", v, "FaceColor", "none"); | |
64 ## fv = shrinkfaces (p); | |
65 ## patch (fv) | |
66 ## axis equal | |
67 ## grid on | |
68 ## @end group | |
69 ## @end example | |
70 ## | |
71 ## @seealso{patch} | |
72 ## @end deftypefn | |
73 | |
74 function [nf, nv] = shrinkfaces (varargin) | |
75 | |
28895
41c54377d67f
maint: remove unnecessary checks on nargout in m-files.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
76 if (nargin < 1 || nargin > 3) |
14574 | 77 print_usage (); |
78 endif | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
79 |
14574 | 80 sf = 0.3; |
18334 | 81 colors = []; |
14574 | 82 p = varargin{1}; |
83 | |
24478
59041be1994b
Use new function isgraphics to simplify m-files.
Rik <rik@octave.org>
parents:
24423
diff
changeset
|
84 if (isscalar (p) && isgraphics (p, "patch") && nargin < 3) |
14574 | 85 faces = get (p, "Faces"); |
86 vertices = get (p, "Vertices"); | |
87 colors = get (p, "FaceVertexCData"); | |
88 if (nargin == 2) | |
89 sf = varargin{2}; | |
90 endif | |
91 elseif (isstruct (p) && nargin < 3) | |
92 faces = p.faces; | |
93 vertices = p.vertices; | |
94 if (isfield (p, "facevertexcdata")) | |
95 colors = p.facevertexcdata; | |
96 endif | |
97 if (nargin == 2) | |
98 sf = varargin{2}; | |
99 endif | |
100 elseif (ismatrix (p) && nargin >= 2 && ismatrix (varargin{2})) | |
101 faces = p; | |
102 vertices = varargin{2}; | |
103 if (nargin == 3) | |
104 sf = varargin{3}; | |
105 endif | |
106 else | |
107 print_usage (); | |
108 endif | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
109 |
14574 | 110 if (! isscalar (sf) || sf <= 0) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14872
diff
changeset
|
111 error ("shrinkfaces: scale factor must be a positive scalar"); |
14574 | 112 endif |
113 | |
18334 | 114 nc = columns (vertices); |
115 if (nc < 2 || nc > 3) | |
17122
eaab03308c0b
doc: Rewrite docstrings for most plot functions.
Rik <rik@octave.org>
parents:
16828
diff
changeset
|
116 error ("shrinkfaces: only 2-D and 3-D patches are supported"); |
14574 | 117 endif |
118 | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
119 m = columns (faces); |
14574 | 120 if (m < 3) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14872
diff
changeset
|
121 error ("shrinkfaces: faces must consist of at least 3 vertices"); |
14574 | 122 endif |
123 | |
124 v = vertices(faces'(:), :); | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
125 if (isempty (colors) || rows (colors) == rows (faces)) |
14574 | 126 c = colors; |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
127 elseif (rows (colors) == rows (vertices)) |
14574 | 128 c = colors(faces'(:), :); |
129 else | |
18334 | 130 c = []; # Discard inconsistent color data. |
14574 | 131 endif |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14621
diff
changeset
|
132 sv = rows (v); |
18334 | 133 ## We have to deal with a possibly very large number of vertices, so use |
134 ## sparse as midpoint (1/m, ..., 1/m) in generalized barycentric coordinates. | |
135 midpoints = full (kron (speye (sv / m), ones (m, m) / m) * sparse (v)); | |
14574 | 136 v = sqrt (sf) * (v - midpoints) + midpoints; |
137 f = reshape (1:sv, m, sv / m)'; | |
19593
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
138 |
14574 | 139 switch (nargout) |
140 case 0 | |
24423
1d52db450db4
Change ishandle to return true for Java objects for Matlab compatibility.
Rik <rik@octave.org>
parents:
24388
diff
changeset
|
141 if (ishghandle (p)) |
15202
f3b5cadfd6d5
fix missing semicolons in various .m files
John W. Eaton <jwe@octave.org>
parents:
14872
diff
changeset
|
142 ## avoid exceptions |
27106
6bfd3a890185
Consolidate separate calls to set() in to one call for interpreter efficiency.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
143 set (p, "FaceVertexCData", [], "CData", [], |
6bfd3a890185
Consolidate separate calls to set() in to one call for interpreter efficiency.
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
144 "Vertices", v, "Faces", f, "FaceVertexCData", c); |
14574 | 145 else |
146 nf = struct ("faces", f, "vertices", v, "facevertexcdata", c); | |
147 endif | |
148 case 1 | |
149 nf = struct ("faces", f, "vertices", v, "facevertexcdata", c); | |
150 case 2 | |
151 nf = f; | |
152 nv = v; | |
153 endswitch | |
154 | |
155 endfunction | |
156 | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
157 |
14574 | 158 %!demo |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
159 %! clf; |
14574 | 160 %! faces = [1 2 3; 1 3 4]; |
161 %! vertices = [0 0; 1 0; 1 1; 0 1]; | |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
162 %! patch ("Faces", faces, "Vertices", vertices, "FaceColor", "none"); |
14574 | 163 %! fv = shrinkfaces (faces, vertices, 0.25); |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
164 %! patch (fv); |
22366
837df5db5710
Make graphic functions visually compatible w/Matlab.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
165 %! axis auto; # Kludge required for Octave |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
166 %! axis equal; |
24388 | 167 %! title ("shrinkfaces() on triangular shapes"); |
14574 | 168 |
169 %!demo | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
170 %! clf; |
14574 | 171 %! faces = [1 2 3 4; 5 6 7 8]; |
172 %! vertices = [0 0; 1 0; 2 1; 1 1; 2 0; 3 0; 4 1; 3.5 1]; | |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
173 %! patch ("Faces", faces, "Vertices", vertices, "FaceColor", "none"); |
14574 | 174 %! fv = shrinkfaces (faces, vertices, 0.25); |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
175 %! patch (fv); |
22366
837df5db5710
Make graphic functions visually compatible w/Matlab.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
176 %! axis auto; # Kludge required for Octave |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
177 %! axis equal; |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
178 %! grid on; |
24388 | 179 %! title ("shrinkfaces() on rhomboid shapes"); |
14574 | 180 |
181 %!demo | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
182 %! clf; |
14574 | 183 %! faces = [1 2 3 4]; |
184 %! vertices = [-1 2; 0 0; 1 2; 0 1]; | |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
185 %! patch ("Faces", faces, "Vertices", vertices, "FaceColor", "none"); |
14574 | 186 %! fv = shrinkfaces (faces, vertices, 0.25); |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
187 %! patch (fv); |
22366
837df5db5710
Make graphic functions visually compatible w/Matlab.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
188 %! axis auto; # Kludge required for Octave |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
189 %! axis equal; |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
190 %! grid on; |
24388 | 191 %! title ("shrinkfaces() does not work on concave shapes"); |
14574 | 192 |
193 %!demo | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
194 %! clf; |
14574 | 195 %! [phi r] = meshgrid (linspace (0, 1.5*pi, 16), linspace (1, 2, 4)); |
196 %! tri = delaunay (phi(:), r(:)); | |
197 %! v = [r(:).*sin(phi(:)) r(:).*cos(phi(:))]; | |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
198 %! p = patch ("Faces", tri, "Vertices", v, "FaceColor", "none"); |
14574 | 199 %! fv = shrinkfaces (p); |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
200 %! patch (fv); |
22366
837df5db5710
Make graphic functions visually compatible w/Matlab.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
201 %! axis auto; # Kludge required for Octave |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
202 %! axis equal; |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
203 %! grid on; |
27967
6628a284d56d
fix more spelling errors (bug #57613)
Mike Miller <mtmiller@octave.org>
parents:
26376
diff
changeset
|
204 %! title ("shrinkfaces() on 2-D complex shapes tessellated with triangles"); |
14574 | 205 |
206 %!demo | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
207 %! clf; |
22737
7abc25e6206a
maint: Clean up code base to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
22366
diff
changeset
|
208 %! N = 10; # N intervals per axis |
14574 | 209 %! [x, y, z] = meshgrid (linspace (-4,4,N+1)); |
210 %! val = x.^3 + y.^3 + z.^3; | |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
211 %! fv = isosurface (x, y, z, val, 3, z, "noshare"); |
14574 | 212 %! |
22302
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
213 %! p = patch ("Faces", fv.faces, "Vertices", fv.vertices, "FaceVertexCData", ... |
1c4cd12987f5
Use Octave syntax in graphics demos.
Rik <rik@octave.org>
parents:
22161
diff
changeset
|
214 %! fv.facevertexcdata, "FaceColor", "interp", "EdgeColor", "black"); |
22366
837df5db5710
Make graphic functions visually compatible w/Matlab.
Rik <rik@octave.org>
parents:
22323
diff
changeset
|
215 %! axis auto; # Kludge required for Octave |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
216 %! axis equal; |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
217 %! view (115, 30); |
28892
3cac3ceb9629
maint: Use coding style with parentheses after function name to distinguish from a variable.
Rik <rik@octave.org>
parents:
27978
diff
changeset
|
218 %! drawnow (); |
14574 | 219 %! shrinkfaces (p, 0.6); |
24388 | 220 %! title ("shrinkfaces() on 3-D complex shapes"); |
14574 | 221 |
222 %!shared faces, vertices, nfv, nfv2 | |
223 %! faces = [1 2 3]; | |
224 %! vertices = [0 0 0; 1 0 0; 1 1 0]; | |
225 %! nfv = shrinkfaces (faces, vertices, 0.7); | |
226 %! nfv2 = shrinkfaces (nfv, 1/0.7); | |
16828
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
227 %!assert (isfield (nfv, "faces")) |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
228 %!assert (isfield (nfv, "vertices")) |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
229 %!assert (size (nfv.faces), [1 3]) |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
230 %!assert (size (nfv.vertices), [3 3]) |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
231 %!assert (norm (nfv2.vertices - vertices), 0, 2*eps) |
ddac88d32d6a
Make demos in plot m-files compatible with Matlab for running comparison script.
Rik <rik@octave.org>
parents:
15332
diff
changeset
|
232 |
19833
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19697
diff
changeset
|
233 ## Test input validation |
28895
41c54377d67f
maint: remove unnecessary checks on nargout in m-files.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
234 %!error <Invalid call> shrinkfaces () |
41c54377d67f
maint: remove unnecessary checks on nargout in m-files.
Rik <rik@octave.org>
parents:
28892
diff
changeset
|
235 %!error <Invalid call> shrinkfaces (1,2,3,4) |
18334 | 236 %!error [a,b,c] = shrinkfaces (1) |
237 %!error <scale factor must be a positive scalar> shrinkfaces (nfv, ones (2)) | |
238 %!error <scale factor must be a positive scalar> shrinkfaces (nfv, 0) | |
239 %!error <only 2-D and 3-D patches are supported> shrinkfaces (faces, ones (3,1)) | |
240 %!error <only 2-D and 3-D patches are supported> shrinkfaces (faces, ones (3,4)) | |
241 %!error <faces must consist of at least 3 vertices> shrinkfaces (faces(1:2), vertices) |