/ concept-collection / surfacefun-interactive
Sign in
concept-collection / surfacefun-interactive
66 lines · 2.2 KBBlameHistoryRaw
1function mesh_refiner(dom, varargin)
2%MESH_REFINER Interactive refinement figure for a surfacemesh.
3% MESH_REFINER(DOM) opens a figure that renders the surfacemesh DOM and shows
4% a slider that refines it interactively. MESH_REFINER(DOM, 'MaxLevel', L)
5% sets the maximum refinement level (default 3).
6%
7% This holds all the wiring: it loads the prebuilt figure app, sends the mesh
8% to it (script -> figure), and refines the mesh whenever the slider in the
9% figure asks for a new level (figure -> script). The end-user script only
10% has to build a domain and call this.
12maxLevel = 3;
13for i = 1:2:numel(varargin) - 1
14 if strcmpi(varargin{i}, 'MaxLevel')
15 maxLevel = varargin{i + 1};
16 end
17end
19% The figure app is the prebuilt single-file page, relative to the project root
20% (the current working directory when a top-level script is run).
21html = fileread(fullfile('app', 'dist', 'index.html'));
23fig = figure;
24gl = uigridlayout(fig, [1 1], 'Padding', [0 0 0 0], ...
25 'RowHeight', {'1x'}, 'ColumnWidth', {'1x'});
26uihtml(gl, 'HTMLSource', html, 'Data', mesh_to_data(dom, 0, maxLevel), ...
27 'HTMLEventReceivedFcn', @(src, ev) on_refine(src, ev, dom, maxLevel));
28end
30function on_refine(src, ev, dom0, maxLevel)
31% Slider in the figure -> refine the base mesh to the requested level and send
32% the new patches back to the figure.
33if ~strcmp(ev.HTMLEventName, 'refine')
34 return
35end
36level = max(0, min(maxLevel, round(ev.HTMLEventData)));
37if level == 0
38 dom = dom0;
39else
40 dom = refine(dom0, level);
41end
42sendEventToHTMLSource(src, 'mesh', mesh_to_data(dom, level, maxLevel));
43end
45function data = mesh_to_data(dom, level, maxLevel)
46% Pack a surfacemesh into a plain struct the figure app can render: one flat
47% (column-major) x/y/z array per patch, each an n-by-n grid. real() drops any
48% all-zero imaginary part the solver may carry.
49np = length(dom);
50px = cell(1, np);
51py = cell(1, np);
52pz = cell(1, np);
53for k = 1:np
54 px{k} = real(dom.x{k}(:).');
55 py{k} = real(dom.y{k}(:).');
56 pz{k} = real(dom.z{k}(:).');
57end
58data = struct();
59data.n = size(dom.x{1}, 1);
60data.x = px;
61data.y = py;
62data.z = pz;
63data.npatches = np;
64data.level = level;
65data.maxLevel = maxLevel;
66end
moveopenescclose