function vector_field_viewer(dom) %VECTOR_FIELD_VIEWER Interactive figure showing a tangent vector field on a surface. % VECTOR_FIELD_VIEWER(DOM) opens a figure that renders a tangent vector % field on the surfacemesh DOM. The arrow scale can be adjusted with a % slider in the figure (purely client-side; no MATLAB callback needed). html = fileread(fullfile('app', 'dist', 'index.html')); data = build_data(dom); fig = figure; gl = uigridlayout(fig, [1 1], 'Padding', [0 0 0 0], ... 'RowHeight', {'1x'}, 'ColumnWidth', {'1x'}); uihtml(gl, 'HTMLSource', html, 'Data', data); end function data = build_data(dom) %BUILD_DATA Pack surface patches and a sampled tangent vector field. % Samples one arrow per patch at the patch center. The vector field is the % tangential projection of (1, 0, 0) onto the surface — a smooth "wind" % field that vanishes near the poles and is strongest near the equator. np = length(dom); px = cell(1, np); py = cell(1, np); pz = cell(1, np); for k = 1:np px{k} = real(dom.x{k}(:).'); py{k} = real(dom.y{k}(:).'); pz{k} = real(dom.z{k}(:).'); end % Sample one tangent vector at the center of each patch. vx = zeros(1, np); vy = zeros(1, np); vz = zeros(1, np); uu = zeros(1, np); vv = zeros(1, np); ww = zeros(1, np); for k = 1:np n = size(dom.x{k}, 1); mid = ceil(n / 2); cx = real(dom.x{k}(mid, mid)); cy = real(dom.y{k}(mid, mid)); cz = real(dom.z{k}(mid, mid)); % Outward unit normal (sphere: normal = normalised position) r = sqrt(cx^2 + cy^2 + cz^2); nx = cx / r; ny = cy / r; nz = cz / r; % Tangential projection of (1, 0, 0) dot_val = nx; % (1,0,0) · normal tu = 1 - dot_val * nx; tv = - dot_val * ny; tw = - dot_val * nz; % Normalise so all arrows have the same base length tmag = sqrt(tu^2 + tv^2 + tw^2); if tmag > 1e-10 tu = tu / tmag; tv = tv / tmag; tw = tw / tmag; end vx(k) = cx; vy(k) = cy; vz(k) = cz; uu(k) = tu; vv(k) = tv; ww(k) = tw; end data = struct(); data.type = 'vectorfield'; data.n = size(dom.x{1}, 1); data.x = px; data.y = py; data.z = pz; data.npatches = np; data.vectors = struct('x', vx, 'y', vy, 'z', vz, 'u', uu, 'v', vv, 'w', ww); end