|
1 | | -function [obj,ind] = extract_subdomain(obj,bou,keep_inverse,centroid,nscreen) |
2 | | -% [obj,ind] = extract_subdomain(obj,bou,keep_inverse,centroid,nscreen) |
3 | | -% |
| 1 | +function [obj,ind] = extract_subdomain(obj,bou,varargin) |
| 2 | +% [obj,ind] = extract_subdomain(obj,bou) |
| 3 | +% |
4 | 4 | % Inputs: |
5 | 5 | % bou: a bbox, i.e.: [lon min, lon_max; |
6 | | -% lat_min, lat_max], or |
| 6 | +% lat_min, lat_max], or |
7 | 7 | % a NaN-delimited polygon of the domain to extract |
8 | 8 | % keep_inverse: = 0 [default] to get the sub-domain inside the bou polygon |
9 | 9 | % = 1 to get the sub-domain outside the bou polygon |
10 | 10 | % centroid: = 0 [default] inpolygon test is based on whether all vertices |
11 | 11 | % of the element are inside (outside) the bou polygon |
12 | 12 | % = 1 inpolygon test is based on whether the element centroid |
13 | 13 | % is inside (outside) the bou polygon |
| 14 | +% min_depth = topobathymetry value to keep elements inside bou so that they have a |
| 15 | +% sufficiently deep value at their centroid |
| 16 | +% max_depth = topobathymetry value to keep elements inside bou so that they have a |
| 17 | +% sufficiently shallow value at their centroid. |
14 | 18 | % nscreen: = 1 [default] display the notice to screen |
15 | 19 | % = 0 do not display the notice to screen |
16 | | -% |
| 20 | +% |
17 | 21 | % Outputs: |
18 | 22 | % obj: the subset mesh obj (only p and t, properties untouched) |
19 | 23 | % ind: an array of indices that can be used to map the mesh properties to |
20 | | -% the output mesh subset with a subsequent call to "map_mesh_properties". |
| 24 | +% the output mesh subset with a subsequent call to "map_mesh_properties". |
21 | 25 | % |
22 | | -p = obj.p; t = obj.t; |
23 | | -if nargin == 1 || (nargin == 3 && isempty(bou)) |
| 26 | +keep_inverse = 0 ; |
| 27 | +centroid = 0 ; |
| 28 | +min_depth = -99999; |
| 29 | +max_depth = +99999; |
| 30 | +nscreen = 1; |
| 31 | +% Otherwise, name value pairs specified. |
| 32 | +% Parse other varargin |
| 33 | +for kk = 1:2:length(varargin) |
| 34 | + if strcmp(varargin{kk},'keep_inverse') |
| 35 | + keep_inverse = varargin{kk+1}; |
| 36 | + elseif strcmp(varargin{kk},'centroid') |
| 37 | + centroid = varargin{kk+1}; |
| 38 | + elseif strcmp(varargin{kk},'nscreen') |
| 39 | + nscreen = varargin{kk+1}; |
| 40 | + elseif strcmp(varargin{kk},'min_depth') |
| 41 | + min_depth = varargin{kk+1}; |
| 42 | + elseif strcmp(varargin{kk},'max_depth') |
| 43 | + max_depth = varargin{kk+1}; |
| 44 | + |
| 45 | + end |
| 46 | +end |
| 47 | + |
| 48 | +p = obj.p; t = obj.t; b = obj.b; |
| 49 | +if nargin == 1 || (nargin == 2 && isempty(bou)) |
24 | 50 | plot(p(:,1),p(:,2),'k.'); |
25 | 51 | h = impoly; |
26 | 52 | bou = h.getPosition; |
27 | 53 | end |
28 | | -if nargin < 3 || isempty(keep_inverse) |
29 | | - keep_inverse = 0; |
30 | | -end |
31 | | -if nargin < 4 || isempty(centroid) |
32 | | - centroid = 0; |
33 | | -end |
34 | | -if nargin < 5 || isempty(nscreen) |
35 | | - nscreen = 1; |
36 | | -end |
| 54 | + |
37 | 55 | % converting bbox to polygon |
38 | 56 | if size(bou,1) == 2 |
39 | 57 | bou = [bou(1,1) bou(2,1); |
40 | | - bou(1,1) bou(2,2); |
| 58 | + bou(1,1) bou(2,2); |
41 | 59 | bou(1,2) bou(2,2); |
42 | | - bou(1,2) bou(2,1); |
| 60 | + bou(1,2) bou(2,1); |
43 | 61 | bou(1,1) bou(2,1)]; |
44 | 62 | end |
45 | 63 | nans = false; |
|
56 | 74 | in = inpoly(bxyc,bou,edges); |
57 | 75 | end |
58 | 76 | else |
59 | | - bxy1 = p(t(:,1),:); bxy2 = p(t(:,2),:); bxy3 = p(t(:,3),:); |
| 77 | + bxy1 = p(t(:,1),:); bxy2 = p(t(:,2),:); bxy3 = p(t(:,3),:); |
60 | 78 | if ~nans |
61 | | - in1 = inpoly(bxy1,bou); |
62 | | - in2 = inpoly(bxy2,bou); |
| 79 | + in1 = inpoly(bxy1,bou); |
| 80 | + in2 = inpoly(bxy2,bou); |
63 | 81 | in3 = inpoly(bxy3,bou); |
64 | 82 | else |
65 | | - in1 = inpoly(bxy1,bou,edges); |
66 | | - in2 = inpoly(bxy2,bou,edges); |
| 83 | + in1 = inpoly(bxy1,bou,edges); |
| 84 | + in2 = inpoly(bxy2,bou,edges); |
67 | 85 | in3 = inpoly(bxy3,bou,edges); |
68 | 86 | end |
69 | 87 | in = in1 & in2 & in3; |
70 | 88 | end |
| 89 | +if min_depth ~= -99999 | max_depth ~= +99999 |
| 90 | + bem = max(b(t),[],2); % only trim when all vertices |
| 91 | + selected = bem > min_depth & bem < max_depth; |
| 92 | + in = logical(in .* selected); |
| 93 | +end |
71 | 94 | if keep_inverse == 0 |
72 | 95 | t(~in,:) = []; |
73 | 96 | else |
|
76 | 99 | % Remove uncessary vertices and reorder triangulation |
77 | 100 | [p1,t,ind] = fixmesh(p,t); |
78 | 101 | % Put back into the msh obj |
79 | | -obj.p = p1; obj.t = t; |
| 102 | +obj.p = p1; obj.t = t; |
80 | 103 | % |
81 | 104 | if nscreen |
82 | 105 | % Displaying notice for mapping mesh properties |
|
0 commit comments