Skip to content

Commit 0de7043

Browse files
author
Keith Roberts
authored
Adding threshold option to extract_subdomain (#177)
* Adding depth-based bounds option to extract_subdomain
1 parent d40d7b8 commit 0de7043

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

Examples/Example_11_Remeshing_Patches.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
% Example_11_Remeshing_Patches.m
2-
%
2+
%
33
% This example highlights a workflow on how to re-mesh and insert patches
44
% using user defined mesh sizing functions from OceanMesh2D preserving
55
% the pathces subdomain boundaries exactly.
@@ -38,7 +38,7 @@
3838
% Get out the msh class and put on nodestrings
3939
m = mshopts.grd;
4040
% Interpolate topobathy data onto the vertices of the mesh.
41-
m = interp(m,'SRTM15+V2.1.nc');
41+
m = interp(m,'SRTM15+V2.1.nc');
4242
%% Extract the region which you want to remesh
4343
% For the purpose of this example, we have drawn a random polygon on top
4444
% of the mesh for which we would like to remesh.
@@ -50,27 +50,27 @@
5050
173.5714 -43.6053
5151
173.0915 -44.1310];
5252
% This extracts the parent mesh but with the polygon removed.
53-
subdomain = extract_subdomain(m,hole);
53+
subdomain = extract_subdomain(m,hole);
5454
% Visualize the subdomain
5555
plot(subdomain);
5656
%% Get the boundary of this hole in the mesh
57-
% Follow the instructions in the title of the plot and click on one of the
57+
% Follow the instructions in the title of the plot and click on one of the
5858
% points on the hole's boundary and type the number into the screen.
5959
poly = get_poly(subdomain);
60-
%% Re-meshing
61-
% Using the polygon we just extracted (poly) and the original mesh size
62-
% function (fh), remesh this hole with Delaunay refinement mesh2d.
60+
%% Re-meshing
61+
% Using the polygon we just extracted (poly) and the original mesh size
62+
% function (fh), remesh this hole with Delaunay refinement mesh2d.
6363
% Recall poly is a cell-array so pass it the hole you want to mesh!
64-
subdomain_new = mesh2dgen(poly{1},fh);
64+
subdomain_new = mesh2dgen(poly{1},fh);
6565
%% Merge the patch in
6666
% Remove the subdomain from the parent mesh
67-
m_w_hole = extract_subdomain(m,hole,1);
67+
m_w_hole = extract_subdomain(m,hole,"keep_inverse",1);
6868
% Since the boundaries match identically, this is a trivial merge.
6969
m_new = plus(subdomain_new, m_w_hole, 'match');
70-
% Use a trivial nearest neighbor interpolation to put the bathy back on
70+
% Use a trivial nearest neighbor interpolation to put the bathy back on
7171
% the new mesh
7272
ind = nearest_neighbor_map(m, m_new);
73-
m_new.b = m.b(ind);
73+
m_new.b = m.b(ind);
7474
% Plot the bathy on the mesh with hole polygon in red
75-
plot(m_new,'type','bmesh'); hold on;
75+
plot(m_new,'type','bmesh'); hold on;
7676
plot(poly{1}(1:end-1,1),poly{1}(1:end-1,2),'r-','linewi',3);

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
138138
### Unreleased
139139

140140
## Added
141-
- `mesh2d` interface improvements to filter small polygons.
141+
- `mesh2d` interface improvements to filter small polygons.
142142
- Support for creation of `fort.20` files for forcing rivers by @Jiangchao3
143143

144144
## Changed
145145
- `msh.plot()` overhaul. All options specified via kwarg.
146+
- `utilities/extract_subdomain` now is called with kwargs.
146147

147148
## Fixed
148149
- Boundary labeling fix

utilities/extract_subdomain.m

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
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+
%
44
% Inputs:
55
% bou: a bbox, i.e.: [lon min, lon_max;
6-
% lat_min, lat_max], or
6+
% lat_min, lat_max], or
77
% a NaN-delimited polygon of the domain to extract
88
% keep_inverse: = 0 [default] to get the sub-domain inside the bou polygon
99
% = 1 to get the sub-domain outside the bou polygon
1010
% centroid: = 0 [default] inpolygon test is based on whether all vertices
1111
% of the element are inside (outside) the bou polygon
1212
% = 1 inpolygon test is based on whether the element centroid
1313
% 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.
1418
% nscreen: = 1 [default] display the notice to screen
1519
% = 0 do not display the notice to screen
16-
%
20+
%
1721
% Outputs:
1822
% obj: the subset mesh obj (only p and t, properties untouched)
1923
% 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".
2125
%
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))
2450
plot(p(:,1),p(:,2),'k.');
2551
h = impoly;
2652
bou = h.getPosition;
2753
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+
3755
% converting bbox to polygon
3856
if size(bou,1) == 2
3957
bou = [bou(1,1) bou(2,1);
40-
bou(1,1) bou(2,2);
58+
bou(1,1) bou(2,2);
4159
bou(1,2) bou(2,2);
42-
bou(1,2) bou(2,1);
60+
bou(1,2) bou(2,1);
4361
bou(1,1) bou(2,1)];
4462
end
4563
nans = false;
@@ -56,18 +74,23 @@
5674
in = inpoly(bxyc,bou,edges);
5775
end
5876
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),:);
6078
if ~nans
61-
in1 = inpoly(bxy1,bou);
62-
in2 = inpoly(bxy2,bou);
79+
in1 = inpoly(bxy1,bou);
80+
in2 = inpoly(bxy2,bou);
6381
in3 = inpoly(bxy3,bou);
6482
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);
6785
in3 = inpoly(bxy3,bou,edges);
6886
end
6987
in = in1 & in2 & in3;
7088
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
7194
if keep_inverse == 0
7295
t(~in,:) = [];
7396
else
@@ -76,7 +99,7 @@
7699
% Remove uncessary vertices and reorder triangulation
77100
[p1,t,ind] = fixmesh(p,t);
78101
% Put back into the msh obj
79-
obj.p = p1; obj.t = t;
102+
obj.p = p1; obj.t = t;
80103
%
81104
if nscreen
82105
% Displaying notice for mapping mesh properties

0 commit comments

Comments
 (0)