31eb018Add interactive vector field demo with arrow scale sliderclaude[bot] 1function vector_field_viewer(dom)
2%VECTOR_FIELD_VIEWER Interactive figure showing a tangent vector field on a surface.
3% VECTOR_FIELD_VIEWER(DOM) opens a figure that renders a tangent vector
4% field on the surfacemesh DOM. The arrow scale can be adjusted with a
5% slider in the figure (purely client-side; no MATLAB callback needed).
7html = fileread(fullfile('app', 'dist', 'index.html'));
8data = build_data(dom);
10fig = figure;
11gl = uigridlayout(fig, [1 1], 'Padding', [0 0 0 0], ...
12 'RowHeight', {'1x'}, 'ColumnWidth', {'1x'});
13uihtml(gl, 'HTMLSource', html, 'Data', data);
14end
16function data = build_data(dom)
17%BUILD_DATA Pack surface patches and a sampled tangent vector field.
18% Samples one arrow per patch at the patch center. The vector field is the
19% tangential projection of (1, 0, 0) onto the surface — a smooth "wind"
20% field that vanishes near the poles and is strongest near the equator.
22np = length(dom);
23px = cell(1, np);
24py = cell(1, np);
25pz = cell(1, np);
26for k = 1:np
27 px{k} = real(dom.x{k}(:).');
28 py{k} = real(dom.y{k}(:).');
29 pz{k} = real(dom.z{k}(:).');
30end
32% Sample one tangent vector at the center of each patch.
33vx = zeros(1, np);
34vy = zeros(1, np);
35vz = zeros(1, np);
36uu = zeros(1, np);
37vv = zeros(1, np);
38ww = zeros(1, np);
40for k = 1:np
41 n = size(dom.x{k}, 1);
42 mid = ceil(n / 2);
43 cx = real(dom.x{k}(mid, mid));
44 cy = real(dom.y{k}(mid, mid));
45 cz = real(dom.z{k}(mid, mid));
47 % Outward unit normal (sphere: normal = normalised position)
48 r = sqrt(cx^2 + cy^2 + cz^2);
49 nx = cx / r; ny = cy / r; nz = cz / r;
51 % Tangential projection of (1, 0, 0)
52 dot_val = nx; % (1,0,0) · normal
53 tu = 1 - dot_val * nx;
54 tv = - dot_val * ny;
55 tw = - dot_val * nz;
57 % Normalise so all arrows have the same base length
58 tmag = sqrt(tu^2 + tv^2 + tw^2);
59 if tmag > 1e-10
60 tu = tu / tmag;
61 tv = tv / tmag;
62 tw = tw / tmag;
63 end
65 vx(k) = cx; vy(k) = cy; vz(k) = cz;
66 uu(k) = tu; vv(k) = tv; ww(k) = tw;
67end
69data = struct();
70data.type = 'vectorfield';
71data.n = size(dom.x{1}, 1);
72data.x = px;
73data.y = py;
74data.z = pz;
75data.npatches = np;
76data.vectors = struct('x', vx, 'y', vy, 'z', vz, 'u', uu, 'v', vv, 'w', ww);
77end